1 /*****************************************************************************
2 * prefs.m: MacOS X module for vlc
3 *****************************************************************************
4 * Copyright (C) 2002-2003 VideoLAN
5 * $Id: prefs.m,v 1.40 2004/02/06 03:52:09 hartman Exp $
7 * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
8 * Derk-Jan Hartman <hartman at videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
28 #include <stdlib.h> /* malloc(), free() */
37 #define GENERAL_ID 1242
38 #define MODULE_ID 1243
39 #define CAPABILITY_ID 1244
41 /*****************************************************************************
42 * VLCPrefs implementation
43 *****************************************************************************/
44 @implementation VLCPrefs
52 o_empty_view = [[[NSView alloc] init] retain];
60 [o_empty_view release];
66 p_intf = [NSApp getIntf];
67 b_advanced = config_GetInt( p_intf, "advanced" );
70 [o_advanced_ckb setState: b_advanced];
71 [o_prefs_view setBorderType: NSGrooveBorder];
72 [o_prefs_view setHasVerticalScroller: YES];
73 [o_prefs_view setDrawsBackground: NO];
74 [o_prefs_view setRulersVisible: NO];
75 [o_prefs_view setDocumentView: o_empty_view];
76 [o_tree selectRow:0 byExtendingSelection:NO];
77 [o_tree expandItem:[o_tree itemAtRow:0]];
82 [o_prefs_window setTitle: _NS("Preferences")];
83 [o_save_btn setTitle: _NS("Save")];
84 [o_cancel_btn setTitle: _NS("Cancel")];
85 [o_reset_btn setTitle: _NS("Reset All")];
86 [o_advanced_ckb setTitle: _NS("Advanced")];
91 // show first tree item
92 [[o_prefs_view window] center];
93 [[o_prefs_view window] makeKeyAndOrderFront:self];
96 - (IBAction)savePrefs: (id)sender
98 // walk trough all treeitems and tell them all to save
99 config_SaveConfigFile( p_intf, NULL );
100 [o_prefs_window orderOut:self];
103 - (IBAction)closePrefs: (id)sender
105 [o_prefs_window orderOut:self];
108 - (IBAction)resetAll: (id)sender
110 NSBeginInformationalAlertSheet(_NS("Reset Preferences"), _NS("Cancel"), _NS("Continue"),
111 nil, o_prefs_window, self, @selector(sheetDidEnd: returnCode: contextInfo:), NULL, nil,
112 _NS("Beware this will reset your VLC media player preferences.\n"
113 "Are you sure you want to continue?") );
116 - (void)sheetDidEnd:(NSWindow *)o_sheet returnCode:(int)i_return contextInfo:(void *)o_context
118 if( i_return == NSAlertAlternateReturn )
120 config_ResetAll( p_intf );
121 // show first config treeitem
125 - (IBAction)advancedToggle: (id)sender
127 b_advanced = !b_advanced;
128 [o_advanced_ckb setState: b_advanced];
129 // walk trough all treeitems and set advanced state
132 - (void)outlineViewSelectionDidChange:(NSNotification *)o_notification
134 // a tree item will be shown
137 - (void)showViewForID:(int)i_id
143 @implementation VLCPrefs (NSTableDataSource)
145 - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
146 return (item == nil) ? [[VLCTreeItem rootItem] numberOfChildren] : [item numberOfChildren];
149 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
150 return (item == nil) ? YES : ([item numberOfChildren] >= 0);
153 - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item {
154 return (item == nil) ? [[VLCTreeItem rootItem] childAtIndex:index] : [item childAtIndex:index];
157 - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item {
158 return (item == nil) ? @"" : (id)[item name];
163 @implementation VLCTreeItem
165 static VLCTreeItem *o_root_item = nil;
167 #define IsALeafNode ((id)-1)
169 - (id)initWithID: (int)i_id parent: (VLCTreeItem *)o_parent_item
176 o_parent = o_parent_item;
181 + (VLCTreeItem *)rootItem {
182 if (o_root_item == nil) o_root_item = [[VLCTreeItem alloc] initWithID: ROOT_ID parent:nil];
188 if( psz_help ) free( psz_help );
189 if( psz_section ) free( psz_section );
190 if (o_name) [o_name release];
191 if (o_children != IsALeafNode) [o_children release];
195 /* Creates and returns the array of children
196 * Loads children incrementally */
197 - (NSArray *)children
199 if (o_children == NULL)
201 intf_thread_t *p_intf = [NSApp getIntf];
202 vlc_list_t *p_list = NULL;
203 module_t *p_module = NULL;
204 module_config_t *p_item = NULL;
207 /* List the modules */
208 p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
209 if( !p_list ) return nil;
211 if( [self objectID] == ROOT_ID )
213 /* Create the General Settings and Modules items */
214 o_children = [[NSMutableArray alloc] initWithCapacity:2];
216 [o_children addObject:[[VLCTreeItem alloc] initWithID: GENERAL_ID parent:self]];
217 [o_children addObject:[[VLCTreeItem alloc] initWithID: MODULE_ID parent:self]];
220 else if( [self objectID] == GENERAL_ID )
223 * Build a tree of the main options
225 o_name = [_NS("General Settings") copy];
226 psz_help = strdup( GENERAL_HELP );
227 psz_section = strdup( GENERAL_TITLE );
229 for( i_index = 0; i_index < p_list->i_count; i_index++ )
231 p_module = (module_t *)p_list->p_values[i_index].p_object;
232 if( !strcmp( p_module->psz_object_name, "main" ) )
235 if( p_module == NULL )
237 msg_Err( p_intf, "could not find the main module in our preferences" );
240 if( i_index < p_list->i_count )
242 /* Enumerate config categories and store a reference so we can
243 * generate their config panel them when it is asked by the user. */
244 p_item = p_module->p_config;
245 o_children = [[NSMutableArray alloc] initWithCapacity:10];
251 switch( p_item->i_type )
253 case CONFIG_HINT_CATEGORY:
254 o_now = [[VLCTreeItem alloc] initWithID: p_module->i_object_id parent:self];
255 [o_now setName: [[NSApp localizedString: p_item->psz_text] retain]];
256 [o_children addObject: o_now];
260 while( p_item->i_type != CONFIG_HINT_END && p_item++ );
262 //[o_children sortUsingSelector:@selector(caseInsensitiveCompare:)];
265 else if( [self objectID] == MODULE_ID )
271 /* Build a list of the capabilities */
272 o_name = [_NS("Modules") copy];
273 psz_help = strdup( PLUGIN_HELP );
274 psz_section = strdup( PLUGIN_TITLE );
276 o_children = [[NSMutableArray alloc] initWithCapacity:10];
277 for( i_index = 0; i_index < p_list->i_count; i_index++ )
279 p_module = (module_t *)p_list->p_values[i_index].p_object;
281 /* Exclude the main module */
282 if( !strcmp( p_module->psz_object_name, "main" ) )
285 /* Exclude empty modules */
286 if( p_module->b_submodule )
287 p_item = ((module_t *)p_module->p_parent)->p_config;
289 p_item = p_module->p_config;
291 if( !p_item ) continue;
294 if( p_item->i_type & CONFIG_ITEM )
297 while( p_item->i_type != CONFIG_HINT_END && p_item++ );
298 if( p_item->i_type == CONFIG_HINT_END ) continue;
300 i_total = [o_children count];
303 for( i_counter = 0; i_counter < i_total; i_counter++ )
305 if( [[[o_children objectAtIndex: i_counter] name] isEqualToString:
306 [NSApp localizedString: p_module->psz_capability]] )
315 VLCTreeItem *o_now = [[VLCTreeItem alloc] initWithID: CAPABILITY_ID parent:self];
316 [o_now setName: [NSApp localizedString: p_module->psz_capability]];
317 [o_children addObject:o_now];
321 //[o_children sortUsingSelector:@selector(caseInsensitiveCompare:)];
323 else if( [self objectID] == CAPABILITY_ID )
325 /* add the modules */
326 o_children = [[NSMutableArray alloc] initWithCapacity:3];
327 for( i_index = 0; i_index < p_list->i_count; i_index++ )
329 p_module = (module_t *)p_list->p_values[i_index].p_object;
331 /* Exclude the main module */
332 if( !strcmp( p_module->psz_object_name, "main" ) )
335 /* Exclude empty modules */
336 if( p_module->b_submodule )
337 p_item = ((module_t *)p_module->p_parent)->p_config;
339 p_item = p_module->p_config;
341 if( !p_item ) continue;
344 if( p_item->i_type & CONFIG_ITEM )
347 while( p_item->i_type != CONFIG_HINT_END && p_item++ );
348 if( p_item->i_type == CONFIG_HINT_END ) continue;
350 if( [[self name] isEqualToString:
351 [NSApp localizedString: p_module->psz_capability]] )
353 VLCTreeItem *o_now = [[VLCTreeItem alloc] initWithID: p_module->i_object_id parent:self];
354 psz_help = strdup(GetCapabilityHelp( p_module->psz_capability, 1));
355 psz_section = strdup(GetCapabilityHelp( p_module->psz_capability, 2));
356 [o_now setName: [[NSApp localizedString:p_module->psz_object_name] retain]];
357 [o_children addObject:o_now];
364 /* all the other stuff are leafs */
365 o_children = IsALeafNode;
378 if( o_name ) return o_name;
381 - (void)setName:(NSString *)a_name;
383 if( o_name ) [o_name release];
384 o_name = [a_name copy];
387 - (VLCTreeItem *)childAtIndex:(int)i_index
389 return [[self children] objectAtIndex:i_index];
392 - (int)numberOfChildren
394 id i_tmp = [self children];
395 return (i_tmp == IsALeafNode) ? (-1) : (int)[i_tmp count];