1 /*****************************************************************************
2 * prefs.m: MacOS X module for vlc
3 *****************************************************************************
4 * Copyright (C) 2002-2006 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, 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 - a redraw view action
34 - the children action should generate a list of the treeitems children (to be used by VLCPrefs datasource)
36 The class is sort of a mix of wxwindows intfs, PrefsTreeCtrl and ConfigTreeData
38 /* VLCConfigControl are subclassed NSView's containing and managing individual config items
39 the classes are VERY closely related to wxwindows ConfigControls */
41 /*****************************************************************************
43 *****************************************************************************/
44 #include <stdlib.h> /* malloc(), free() */
45 #include <sys/param.h> /* for MAXPATHLEN */
52 #include <vlc_common.h>
53 #include <vlc_config_cat.h>
57 #import "simple_prefs.h"
58 #import "prefs_widgets.h"
61 /* /!\ Warning: Unreadable code :/ */
63 @interface VLCTreeItem : NSObject
66 NSMutableArray *_children;
67 NSMutableArray *_options;
68 NSMutableArray *_subviews;
71 - (id)initWithName:(NSString*)name;
73 - (int)numberOfChildren;
74 - (VLCTreeItem *)childAtIndex:(NSInteger)i_index;
77 - (NSMutableArray *)children;
78 - (NSMutableArray *)options;
79 - (void)showView:(NSScrollView *)o_prefs_view;
86 @interface VLCTreeSubCategoryItem : VLCTreeItem
90 + (VLCTreeSubCategoryItem *)subCategoryTreeItemWithSubCategory:(int)subCategory;
91 - (id)initWithSubCategory:(int)subCategory;
95 /* Plugin daughters */
96 @interface VLCTreePluginItem : VLCTreeItem
98 module_config_t * _configItems;
99 unsigned int _configSize;
101 + (VLCTreePluginItem *)pluginTreeItemWithPlugin:(module_t *)plugin;
102 - (id)initWithPlugin:(module_t *)plugin;
104 - (module_config_t *)configItems;
105 - (unsigned int)configSize;
109 @interface VLCTreeCategoryItem : VLCTreeItem
113 + (VLCTreeCategoryItem *)categoryTreeItemWithCategory:(int)category;
114 - (id)initWithCategory:(int)category;
117 - (VLCTreeSubCategoryItem *)itemRepresentingSubCategory:(int)category;
120 /* individual options. */
121 @interface VLCTreeLeafItem : VLCTreeItem
123 module_config_t * _configItem;
125 - (id)initWithConfigItem:(module_config_t *)configItem;
127 - (module_config_t *)configItem;
130 @interface VLCTreeMainItem : VLCTreePluginItem
133 - (VLCTreeCategoryItem *)itemRepresentingCategory:(int)category;
138 /*****************************************************************************
139 * VLCPrefs implementation
140 *****************************************************************************/
141 @implementation VLCPrefs
143 static VLCPrefs *_o_sharedMainInstance = nil;
145 + (VLCPrefs *)sharedInstance
147 return _o_sharedMainInstance ? _o_sharedMainInstance : [[self alloc] init];
152 if( _o_sharedMainInstance ) {
157 _o_sharedMainInstance = [super init];
159 o_empty_view = [[NSView alloc] init];
160 _rootTreeItem = [[VLCTreeMainItem alloc] init];
163 return _o_sharedMainInstance;
168 [o_empty_view release];
169 [_rootTreeItem release];
178 [o_prefs_view setBorderType: NSGrooveBorder];
179 [o_prefs_view setHasVerticalScroller: YES];
180 [o_prefs_view setDrawsBackground: NO];
181 [o_prefs_view setDocumentView: o_empty_view];
182 [o_tree selectRowIndexes: [NSIndexSet indexSetWithIndex: 0] byExtendingSelection: NO];
185 - (void)setTitle: (NSString *) o_title_name
187 [o_title setStringValue: o_title_name];
192 [[o_basicFull_matrix cellAtRow:0 column:0] setState: NSOffState];
193 [[o_basicFull_matrix cellAtRow:0 column:1] setState: NSOnState];
195 [o_prefs_window center];
196 [o_prefs_window makeKeyAndOrderFront:self];
197 [_rootTreeItem resetView];
202 [o_prefs_window setTitle: _NS("Preferences")];
203 [o_save_btn setTitle: _NS("Save")];
204 [o_cancel_btn setTitle: _NS("Cancel")];
205 [o_reset_btn setTitle: _NS("Reset All")];
206 [[o_basicFull_matrix cellAtRow: 0 column: 0] setStringValue: _NS("Basic")];
207 [[o_basicFull_matrix cellAtRow: 0 column: 1] setStringValue: _NS("All")];
210 - (IBAction)savePrefs: (id)sender
212 /* TODO: call savePrefs on Root item */
213 [_rootTreeItem applyChanges];
214 config_SaveConfigFile( p_intf, NULL );
215 [o_prefs_window orderOut:self];
218 - (IBAction)closePrefs: (id)sender
220 [o_prefs_window orderOut:self];
223 - (IBAction)resetAll: (id)sender
225 NSBeginInformationalAlertSheet(_NS("Reset Preferences"), _NS("Cancel"),
226 _NS("Continue"), nil, o_prefs_window, self,
227 @selector(sheetDidEnd: returnCode: contextInfo:), NULL, nil,
228 _NS("Beware this will reset the VLC media player preferences.\n"
229 "Are you sure you want to continue?") );
232 - (void)sheetDidEnd:(NSWindow *)o_sheet returnCode:(int)i_return
233 contextInfo:(void *)o_context
235 if( i_return == NSAlertAlternateReturn )
237 config_ResetAll( p_intf );
238 [_rootTreeItem resetView];
242 - (IBAction)buttonAction: (id)sender
244 [o_prefs_window orderOut: self];
245 [[o_basicFull_matrix cellAtRow:0 column:0] setState: NSOnState];
246 [[o_basicFull_matrix cellAtRow:0 column:1] setState: NSOffState];
247 [[[VLCMain sharedInstance] simplePreferences] showSimplePrefs];
250 - (void)loadConfigTree
254 - (void)outlineViewSelectionIsChanging:(NSNotification *)o_notification
258 /* update the document view to the view of the selected tree item */
259 - (void)outlineViewSelectionDidChange:(NSNotification *)o_notification
261 [[o_tree itemAtRow:[o_tree selectedRow]] showView: o_prefs_view];
262 [o_tree expandItem:[o_tree itemAtRow:[o_tree selectedRow]]];
267 @implementation VLCPrefs (NSTableDataSource)
269 - (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
271 return (item == nil) ? [_rootTreeItem numberOfChildren] : [item numberOfChildren];
274 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
276 return (item == nil) ? [_rootTreeItem numberOfChildren] : [item numberOfChildren];
279 - (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item
281 return (item == nil) ? (id)[_rootTreeItem childAtIndex:index]: (id)[item childAtIndex:index];
284 - (id)outlineView:(NSOutlineView *)outlineView
285 objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
287 return (item == nil) ? @"" : [item name];
293 @implementation VLCTreeMainItem
295 - (VLCTreeCategoryItem *)itemRepresentingCategory:(int)category
297 for( int i = 0; i < [[self children] count]; i++ )
299 VLCTreeCategoryItem * categoryItem = [[self children] objectAtIndex:i];
300 if( [categoryItem category] == category )
306 - (bool)isSubCategoryGeneral:(int)category
308 if(category == SUBCAT_VIDEO_GENERAL ||
309 category == SUBCAT_ADVANCED_MISC ||
310 category == SUBCAT_INPUT_GENERAL ||
311 category == SUBCAT_INTERFACE_GENERAL ||
312 category == SUBCAT_SOUT_GENERAL||
313 category == SUBCAT_PLAYLIST_GENERAL||
314 category == SUBCAT_AUDIO_GENERAL )
321 /* Creates and returns the array of children
322 * Loads children incrementally */
323 - (NSMutableArray *)children
325 if( _children ) return _children;
326 _children = [[NSMutableArray alloc] init];
328 intf_thread_t *p_intf = VLCIntf;
330 /* List the modules */
332 module_t ** modules = module_list_get( &count );
333 if( !modules ) return nil;
335 /* Build a tree of the plugins */
336 /* Add the capabilities */
337 for( i = 0; i < count; i++ )
339 VLCTreeCategoryItem * categoryItem = nil;
340 VLCTreeSubCategoryItem * subCategoryItem = nil;
341 VLCTreePluginItem * pluginItem = nil;
342 module_config_t *p_configs = NULL;
344 unsigned int confsize;
346 module_t * p_module = modules[i];
348 /* Exclude empty plugins (submodules don't have config */
349 /* options, they are stored in the parent module) */
350 if( module_is_main( p_module) )
353 _configItems = module_config_get( p_module, &confsize );
354 _configSize = confsize;
356 pluginItem = [VLCTreePluginItem pluginTreeItemWithPlugin: p_module];
357 confsize = [pluginItem configSize];
359 p_configs = [pluginItem configItems];
362 for( j = 0; j < confsize; j++ )
364 int configType = p_configs[j].i_type;
365 if( configType == CONFIG_CATEGORY )
367 categoryItem = [self itemRepresentingCategory:p_configs[j].value.i];
370 categoryItem = [VLCTreeCategoryItem categoryTreeItemWithCategory:p_configs[j].value.i];
371 if(categoryItem) [[self children] addObject:categoryItem];
374 else if( configType == CONFIG_SUBCATEGORY )
376 lastsubcat = p_configs[j].value.i;
377 if( categoryItem && ![self isSubCategoryGeneral:lastsubcat] )
379 subCategoryItem = [categoryItem itemRepresentingSubCategory:lastsubcat];
382 subCategoryItem = [VLCTreeSubCategoryItem subCategoryTreeItemWithSubCategory:lastsubcat];
383 if(subCategoryItem) [[categoryItem children] addObject:subCategoryItem];
388 if( module_is_main( p_module) && (configType & CONFIG_ITEM) )
390 if( categoryItem && [self isSubCategoryGeneral:lastsubcat] )
392 [[categoryItem options] addObject:[[VLCTreeLeafItem alloc] initWithConfigItem:&p_configs[j]]];
394 else if( subCategoryItem )
396 [[subCategoryItem options] addObject:[[VLCTreeLeafItem alloc] initWithConfigItem:&p_configs[j]]];
399 else if( !module_is_main( p_module) && (configType & CONFIG_ITEM))
401 if( ![[subCategoryItem children] containsObject: pluginItem] )
403 [[subCategoryItem children] addObject:pluginItem];
406 [[pluginItem options] addObject:[[VLCTreeLeafItem alloc] initWithConfigItem:&p_configs[j]]];
410 module_list_free( modules );
416 @implementation VLCTreeCategoryItem
417 + (VLCTreeCategoryItem *)categoryTreeItemWithCategory:(int)category
419 if(!config_CategoryNameGet( category )) return nil;
420 return [[[[self class] alloc] initWithCategory:category] autorelease];
422 - (id)initWithCategory:(int)category
424 NSString * name = [[VLCMain sharedInstance] localizedString: config_CategoryNameGet( category )];
425 if(self = [super initWithName:name])
427 _category = category;
428 //_help = [[[VLCMain sharedInstance] localizedString: config_CategoryHelpGet( category )] retain];
433 - (VLCTreeSubCategoryItem *)itemRepresentingSubCategory:(int)subCategory
435 assert( [self isKindOfClass:[VLCTreeCategoryItem class]] );
436 for( int i = 0; i < [[self children] count]; i++ )
438 VLCTreeSubCategoryItem * subCategoryItem = [[self children] objectAtIndex:i];
439 if( [subCategoryItem subCategory] == subCategory )
440 return subCategoryItem;
452 @implementation VLCTreeSubCategoryItem
453 - (id)initWithSubCategory:(int)subCategory
455 NSString * name = [[VLCMain sharedInstance] localizedString: config_CategoryNameGet( subCategory )];
456 if(self = [super initWithName:name])
458 _subCategory = subCategory;
459 //_help = [[[VLCMain sharedInstance] localizedString: config_CategoryHelpGet( subCategory )] retain];
464 + (VLCTreeSubCategoryItem *)subCategoryTreeItemWithSubCategory:(int)subCategory
466 if(!config_CategoryNameGet( subCategory )) return nil;
467 return [[[[self class] alloc] initWithSubCategory:subCategory] autorelease];
478 @implementation VLCTreePluginItem
479 - (id)initWithPlugin:(module_t *)plugin
481 NSString * name = [[VLCMain sharedInstance] localizedString: module_get_name( plugin, false )?:""];
482 if(self = [super initWithName:name])
484 _configItems = module_config_get( plugin, &_configSize );
486 //_help = [[[VLCMain sharedInstance] localizedString: config_CategoryHelpGet( subCategory )] retain];
491 + (VLCTreePluginItem *)pluginTreeItemWithPlugin:(module_t *)plugin
493 return [[[[self class] alloc] initWithPlugin:plugin] autorelease];
498 module_config_free( _configItems );
502 - (module_config_t *)configItems
507 - (unsigned int)configSize
515 @implementation VLCTreeLeafItem
517 - (id)initWithConfigItem: (module_config_t *) configItem
519 NSString * name = [[[VLCMain sharedInstance] localizedString:configItem->psz_name] autorelease];
520 self = [super initWithName:name];
523 _configItem = configItem;
528 - (module_config_t *)configItem
535 #pragma mark (Root class for all TreeItems)
536 @implementation VLCTreeItem
538 - (id)initWithName:(NSString*)name
543 _name = [name retain];
557 - (VLCTreeItem *)childAtIndex:(NSInteger)i_index
559 return [[self children] objectAtIndex:i_index];
562 - (int)numberOfChildren
564 return [[self children] count];
569 return [[_name retain] autorelease];
572 - (void)showView:(NSScrollView *)prefsView
577 [[VLCPrefs sharedInstance] setTitle: [self name]];
578 s_vrc = [[prefsView contentView] bounds]; s_vrc.size.height -= 4;
579 view = [[VLCFlippedView alloc] initWithFrame: s_vrc];
580 [view setAutoresizingMask: NSViewWidthSizable | NSViewMinYMargin | NSViewMaxYMargin];
584 _subviews = [[NSMutableArray alloc] initWithCapacity:10];
587 for( i = 0; i < [[self options] count]; i++)
589 VLCTreeLeafItem * item = [[self options] objectAtIndex:i];
591 VLCConfigControl *control;
592 control = [VLCConfigControl newControl:[item configItem] withView:view];
595 [control setAutoresizingMask: NSViewMaxYMargin | NSViewWidthSizable];
596 [_subviews addObject: control];
607 NSEnumerator *enumerator = [_subviews objectEnumerator];
608 VLCConfigControl *widget;
611 while( ( widget = [enumerator nextObject] ) )
612 if( i_max_label < [widget labelSize] )
613 i_max_label = [widget labelSize];
615 enumerator = [_subviews objectEnumerator];
616 while( ( widget = [enumerator nextObject] ) )
620 i_widget = [widget viewType];
621 i_yPos += [VLCConfigControl calcVerticalMargin:i_widget lastItem:i_lastItem];
622 [widget setYPos:i_yPos];
623 frame = [widget frame];
624 frame.size.width = [view frame].size.width - LEFTMARGIN - RIGHTMARGIN;
625 [widget setFrame:frame];
626 [widget alignWithXPosition: i_max_label];
627 i_yPos += [widget frame].size.height;
628 i_lastItem = i_widget;
629 [view addSubview:widget];
632 frame = [view frame];
633 frame.size.height = i_yPos;
634 [view setFrame:frame];
635 [prefsView setDocumentView:view];
641 for( i = 0 ; i < [_subviews count] ; i++ )
642 [[_subviews objectAtIndex:i] applyChanges];
644 for( i = 0 ; i < [_children count] ; i++ )
645 [[_children objectAtIndex:i] applyChanges];
651 for( i = 0 ; i < [_subviews count] ; i++ )
652 [[_subviews objectAtIndex:i] resetValues];
654 for( i = 0 ; i < [_options count] ; i++ )
655 [[_options objectAtIndex:i] resetView];
657 for( i = 0 ; i < [_children count] ; i++ )
658 [[_children objectAtIndex:i] resetView];
662 - (NSMutableArray *)children
664 if(!_children) _children = [[NSMutableArray alloc] init];
668 - (NSMutableArray *)options
670 if(!_options) _options = [[NSMutableArray alloc] init];
676 @implementation VLCFlippedView