1 /*****************************************************************************
2 * prefs.m: MacOS X module for vlc
3 *****************************************************************************
4 * Copyright (C) 2002-2005 VideoLAN
7 * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
8 * Derk-Jan Hartman <hartman at videolan dot 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 /* VLCPrefs manages the main preferences dialog
26 the class is related to wxwindows intf, PrefsPanel */
27 /* VLCTreeItem should contain:
28 - the children of the treeitem
29 - the associated prefs widgets
30 - the documentview with all the prefs widgets in it
31 - a saveChanges action
32 - a revertChanges action
33 - an advanced action (to hide/show advanced options)
34 - a redraw view action
35 - the children action should generate a list of the treeitems children (to be used by VLCPrefs datasource)
37 The class is sort of a mix of wxwindows intfs, PrefsTreeCtrl and ConfigTreeData
39 /* VLCConfigControl are subclassed NSView's containing and managing individual config items
40 the classes are VERY closely related to wxwindows ConfigControls */
42 /*****************************************************************************
44 *****************************************************************************/
45 #include <stdlib.h> /* malloc(), free() */
46 #include <sys/param.h> /* for MAXPATHLEN */
53 /*****************************************************************************
54 * VLCPrefs implementation
55 *****************************************************************************/
56 @implementation VLCPrefs
58 static VLCPrefs *_o_sharedMainInstance = nil;
60 + (VLCPrefs *)sharedInstance
62 return _o_sharedMainInstance ? _o_sharedMainInstance : [[self alloc] init];
67 if( _o_sharedMainInstance ) {
72 _o_sharedMainInstance = [super init];
74 o_empty_view = [[NSView alloc] init];
77 return _o_sharedMainInstance;
82 [o_empty_view release];
89 b_advanced = config_GetInt( p_intf, "advanced" );
92 [o_advanced_ckb setState: b_advanced];
93 [o_prefs_view setBorderType: NSGrooveBorder];
94 [o_prefs_view setHasVerticalScroller: YES];
95 [o_prefs_view setDrawsBackground: NO];
96 [o_prefs_view setRulersVisible: NO];
97 [o_prefs_view setDocumentView: o_empty_view];
98 [o_tree selectRow:0 byExtendingSelection:NO];
103 /* load our nib (if not already loaded) */
104 [NSBundle loadNibNamed:@"Preferences" owner:self];
106 /* Show View for the currently select treeitem */
107 /* [self showViewForID: [[o_tree itemAtRow:[o_tree selectedRow]] getObjectID]
108 andName: [[o_tree itemAtRow:[o_tree selectedRow]] getName]]; */
109 [o_prefs_window center];
110 [o_prefs_window makeKeyAndOrderFront:self];
115 [o_prefs_window setTitle: _NS("Preferences")];
116 [o_save_btn setTitle: _NS("Save")];
117 [o_cancel_btn setTitle: _NS("Cancel")];
118 [o_reset_btn setTitle: _NS("Reset All")];
119 [o_advanced_ckb setTitle: _NS("Advanced")];
122 - (IBAction)savePrefs: (id)sender
124 /* TODO: call savePrefs on Root item */
125 config_SaveConfigFile( p_intf, NULL );
126 [o_prefs_window orderOut:self];
129 - (IBAction)closePrefs: (id)sender
131 [o_prefs_window orderOut:self];
134 - (IBAction)resetAll: (id)sender
136 NSBeginInformationalAlertSheet(_NS("Reset Preferences"), _NS("Cancel"), _NS("Continue"),
137 nil, o_prefs_window, self, @selector(sheetDidEnd: returnCode: contextInfo:), NULL, nil,
138 _NS("Beware this will reset your VLC media player preferences.\n"
139 "Are you sure you want to continue?") );
142 - (void)sheetDidEnd:(NSWindow *)o_sheet returnCode:(int)i_return contextInfo:(void *)o_context
144 if( i_return == NSAlertAlternateReturn )
146 config_ResetAll( p_intf );
147 [self showViewForID: [[o_tree itemAtRow:[o_tree selectedRow]] getObjectID]
148 andName: [[o_tree itemAtRow:[o_tree selectedRow]] getName]];
152 - (IBAction)advancedToggle: (id)sender
154 b_advanced = !b_advanced;
155 [o_advanced_ckb setState: b_advanced];
156 /* refresh the view of the current treeitem */
157 /* [self showViewForID: [[o_tree itemAtRow:[o_tree selectedRow]] getObjectID]
158 andName: [[o_tree itemAtRow:[o_tree selectedRow]] getName]]; */
161 - (void)loadConfigTree
165 - (void)outlineViewSelectionIsChanging:(NSNotification *)o_notification
169 /* update the document view to the view of the selected tree item */
170 - (void)outlineViewSelectionDidChange:(NSNotification *)o_notification
173 [self showViewForID: [[o_tree itemAtRow:[o_tree selectedRow]] getObjectID]
174 andName: [[o_tree itemAtRow:[o_tree selectedRow]] getName]];*/
179 @implementation VLCPrefs (NSTableDataSource)
181 - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
182 return (item == nil) ? [[VLCTreeItem rootItem] numberOfChildren] : [item numberOfChildren];
185 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
186 return (item == nil) ? YES : ([item numberOfChildren] != -1);
189 - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item {
190 return (item == nil) ? [[VLCTreeItem rootItem] childAtIndex:index] : [item childAtIndex:index];
193 - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item {
194 return (item == nil) ? @"" : (id)[item getName];
199 @implementation VLCTreeItem
201 static VLCTreeItem *o_root_item = nil;
203 #define IsALeafNode ((id)-1)
205 - (id)initWithName: (NSString *)o_item_name ID: (int)i_id parent:(VLCTreeItem *)o_parent_item
211 o_name = [o_item_name copy];
213 o_parent = o_parent_item;
218 + (VLCTreeItem *)rootItem {
219 if (o_root_item == nil) o_root_item = [[VLCTreeItem alloc] initWithName:@"main" ID: 0 parent:nil];
225 if (o_children != IsALeafNode) [o_children release];
230 /* Creates and returns the array of children
231 * Loads children incrementally */
232 - (NSArray *)children
234 if( o_children == NULL )
236 intf_thread_t *p_intf = VLCIntf;
238 module_t *p_module = NULL;
239 module_config_t *p_item;
242 /* List the modules */
243 p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
244 if( !p_list ) return nil;
246 if( [[self getName] isEqualToString: @"main"] )
249 * Find the main module
251 for( i_index = 0; i_index < p_list->i_count; i_index++ )
253 p_module = (module_t *)p_list->p_values[i_index].p_object;
254 if( !strcmp( p_module->psz_object_name, "main" ) )
257 if( p_module == NULL )
259 msg_Err( p_intf, "could not find the main module in our preferences" );
262 if( i_index < p_list->i_count )
264 /* We found the main module */
266 /* Enumerate config categories and store a reference so we can
267 * generate their config panel them when it is asked by the user. */
268 p_item = p_module->p_config;
269 o_children = [[NSMutableArray alloc] initWithCapacity:10];
273 NSString *o_child_name;
275 switch( p_item->i_type )
277 case CONFIG_HINT_CATEGORY:
278 o_child_name = [[VLCMain sharedInstance] localizedString: p_item->psz_text];
279 [o_children addObject:[[VLCTreeItem alloc] initWithName: o_child_name
280 ID: p_module->i_object_id parent:self]];
284 while( p_item->i_type != CONFIG_HINT_END && p_item++ );
286 /* Add the modules item */
287 [o_children addObject:[[VLCTreeItem alloc] initWithName: _NS("Modules")
292 o_children = IsALeafNode;
295 else if( [[self getName] isEqualToString: _NS("Modules")] )
297 /* Add the capabilities */
298 o_children = [[NSMutableArray alloc] initWithCapacity:10];
299 for( i_index = 0; i_index < p_list->i_count; i_index++ )
301 p_module = (module_t *)p_list->p_values[i_index].p_object;
303 /* Exclude the main module */
304 if( !strcmp( p_module->psz_object_name, "main" ) )
307 /* Exclude empty modules */
308 p_item = p_module->p_config;
309 if( !p_item ) continue;
312 if( p_item->i_type & CONFIG_ITEM )
315 while( p_item->i_type != CONFIG_HINT_END && p_item++ );
316 if( p_item->i_type == CONFIG_HINT_END ) continue;
318 /* Create the capability tree if it doesn't already exist */
319 NSString *o_capability;
320 o_capability = [[VLCMain sharedInstance] localizedString: p_module->psz_capability];
321 if( !p_module->psz_capability || !*p_module->psz_capability )
323 /* Empty capability ? Let's look at the submodules */
324 module_t * p_submodule;
325 for( j = 0; j < p_module->i_children; j++ )
327 p_submodule = (module_t*)p_module->pp_children[ j ];
328 if( p_submodule->psz_capability && *p_submodule->psz_capability )
330 o_capability = [[VLCMain sharedInstance] localizedString: p_submodule->psz_capability];
331 BOOL b_found = FALSE;
332 for( j = 0; j < (int)[o_children count]; j++ )
334 if( [[[o_children objectAtIndex:j] getName] isEqualToString: o_capability] )
342 [o_children addObject:[[VLCTreeItem alloc] initWithName: o_capability
349 BOOL b_found = FALSE;
350 for( j = 0; j < (int)[o_children count]; j++ )
352 if( [[[o_children objectAtIndex:j] getName] isEqualToString: o_capability] )
360 [o_children addObject:[[VLCTreeItem alloc] initWithName: o_capability
365 else if( [[o_parent getName] isEqualToString: _NS("Modules")] )
367 /* Now add the modules */
368 o_children = [[NSMutableArray alloc] initWithCapacity:10];
369 for( i_index = 0; i_index < p_list->i_count; i_index++ )
371 p_module = (module_t *)p_list->p_values[i_index].p_object;
373 /* Exclude the main module */
374 if( !strcmp( p_module->psz_object_name, "main" ) )
377 /* Exclude empty modules */
378 p_item = p_module->p_config;
379 if( !p_item ) continue;
382 if( p_item->i_type & CONFIG_ITEM )
385 while( p_item->i_type != CONFIG_HINT_END && p_item++ );
386 if( p_item->i_type == CONFIG_HINT_END ) continue;
388 /* Check the capability */
389 NSString *o_capability;
390 o_capability = [[VLCMain sharedInstance] localizedString: p_module->psz_capability];
391 if( !p_module->psz_capability || !*p_module->psz_capability )
393 /* Empty capability ? Let's look at the submodules */
394 module_t * p_submodule;
395 for( j = 0; j < p_module->i_children; j++ )
397 p_submodule = (module_t*)p_module->pp_children[ j ];
398 if( p_submodule->psz_capability && *p_submodule->psz_capability )
400 o_capability = [[VLCMain sharedInstance] localizedString: p_submodule->psz_capability];
401 if( [o_capability isEqualToString: [self getName]] )
403 [o_children addObject:[[VLCTreeItem alloc] initWithName:
404 [[VLCMain sharedInstance] localizedString: p_module->psz_object_name ]
405 ID: p_module->i_object_id parent:self]];
410 else if( [o_capability isEqualToString: [self getName]] )
412 [o_children addObject:[[VLCTreeItem alloc] initWithName:
413 [[VLCMain sharedInstance] localizedString: p_module->psz_object_name ]
414 ID: p_module->i_object_id parent:self]];
420 /* all the other stuff are leafs */
421 o_children = IsALeafNode;
423 vlc_list_release( p_list );
433 - (NSString *)getName
438 - (VLCTreeItem *)childAtIndex:(int)i_index {
439 return [[self children] objectAtIndex:i_index];
442 - (int)numberOfChildren {
443 id i_tmp = [self children];
444 return (i_tmp == IsALeafNode) ? (-1) : (int)[i_tmp count];
447 - (BOOL)hasPrefs:(NSString *)o_module_name
449 intf_thread_t *p_intf = VLCIntf;
452 char *psz_module_name;
455 psz_module_name = (char *)[o_module_name UTF8String];
457 /* look for module */
458 p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
460 for( i_index = 0; i_index < p_list->i_count; i_index++ )
462 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
464 if( !strcmp( p_parser->psz_object_name, psz_module_name ) )
466 BOOL b_has_prefs = p_parser->i_config_items != 0;
467 vlc_list_release( p_list );
468 return( b_has_prefs );
472 vlc_list_release( p_list );
480 @implementation VLCFlippedView