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>
55 #import "CompatibilityFixes.h"
58 #import "simple_prefs.h"
59 #import "prefs_widgets.h"
61 #import <vlc_modules.h>
63 /* /!\ Warning: Unreadable code :/ */
65 @interface VLCTreeItem : NSObject
68 NSMutableArray *_children;
69 NSMutableArray *_options;
70 NSMutableArray *_subviews;
73 - (id)initWithName:(NSString*)name;
75 - (int)numberOfChildren;
76 - (VLCTreeItem *)childAtIndex:(NSInteger)i_index;
79 - (NSMutableArray *)children;
80 - (NSMutableArray *)options;
81 - (void)showView:(NSScrollView *)o_prefs_view;
88 @interface VLCTreeSubCategoryItem : VLCTreeItem
92 + (VLCTreeSubCategoryItem *)subCategoryTreeItemWithSubCategory:(int)subCategory;
93 - (id)initWithSubCategory:(int)subCategory;
97 /* Plugin daughters */
98 @interface VLCTreePluginItem : VLCTreeItem
100 module_config_t * _configItems;
101 unsigned int _configSize;
103 + (VLCTreePluginItem *)pluginTreeItemWithPlugin:(module_t *)plugin;
104 - (id)initWithPlugin:(module_t *)plugin;
106 - (module_config_t *)configItems;
107 - (unsigned int)configSize;
111 @interface VLCTreeCategoryItem : VLCTreeItem
115 + (VLCTreeCategoryItem *)categoryTreeItemWithCategory:(int)category;
116 - (id)initWithCategory:(int)category;
119 - (VLCTreeSubCategoryItem *)itemRepresentingSubCategory:(int)category;
122 /* individual options. */
123 @interface VLCTreeLeafItem : VLCTreeItem
125 module_config_t * _configItem;
127 - (id)initWithConfigItem:(module_config_t *)configItem;
129 - (module_config_t *)configItem;
132 @interface VLCTreeMainItem : VLCTreePluginItem
135 - (VLCTreeCategoryItem *)itemRepresentingCategory:(int)category;
140 /*****************************************************************************
141 * VLCPrefs implementation
142 *****************************************************************************/
143 @implementation VLCPrefs
145 static VLCPrefs *_o_sharedMainInstance = nil;
147 + (VLCPrefs *)sharedInstance
149 return _o_sharedMainInstance ? _o_sharedMainInstance : [[self alloc] init];
154 if( _o_sharedMainInstance ) {
159 _o_sharedMainInstance = [super init];
161 o_empty_view = [[NSView alloc] init];
162 _rootTreeItem = [[VLCTreeMainItem alloc] init];
165 return _o_sharedMainInstance;
170 [o_empty_view release];
171 [_rootTreeItem release];
180 [o_prefs_window setCollectionBehavior: NSWindowCollectionBehaviorFullScreenAuxiliary];
183 [o_prefs_view setBorderType: NSGrooveBorder];
184 [o_prefs_view setHasVerticalScroller: YES];
185 [o_prefs_view setDrawsBackground: NO];
186 [o_prefs_view setDocumentView: o_empty_view];
187 [o_tree selectRowIndexes: [NSIndexSet indexSetWithIndex: 0] byExtendingSelection: NO];
190 - (void)setTitle: (NSString *) o_title_name
192 [o_title setStringValue: o_title_name];
197 [[o_basicFull_matrix cellAtRow:0 column:0] setState: NSOffState];
198 [[o_basicFull_matrix cellAtRow:0 column:1] setState: NSOnState];
200 [o_prefs_window center];
201 [o_prefs_window makeKeyAndOrderFront:self];
202 [_rootTreeItem resetView];
207 [o_prefs_window setTitle: _NS("Preferences")];
208 [o_save_btn setTitle: _NS("Save")];
209 [o_cancel_btn setTitle: _NS("Cancel")];
210 [o_reset_btn setTitle: _NS("Reset All")];
211 [[o_basicFull_matrix cellAtRow: 0 column: 0] setStringValue: _NS("Basic")];
212 [[o_basicFull_matrix cellAtRow: 0 column: 1] setStringValue: _NS("All")];
215 - (IBAction)savePrefs: (id)sender
217 /* TODO: call savePrefs on Root item */
218 [_rootTreeItem applyChanges];
219 config_SaveConfigFile( p_intf );
220 [o_prefs_window orderOut:self];
223 - (IBAction)closePrefs: (id)sender
225 [o_prefs_window orderOut:self];
228 - (IBAction)resetAll: (id)sender
230 NSBeginInformationalAlertSheet(_NS("Reset Preferences"), _NS("Cancel"),
231 _NS("Continue"), nil, o_prefs_window, self,
232 @selector(sheetDidEnd: returnCode: contextInfo:), NULL, nil,
233 _NS("Beware this will reset the VLC media player preferences.\n"
234 "Are you sure you want to continue?") );
237 - (void)sheetDidEnd:(NSWindow *)o_sheet returnCode:(int)i_return
238 contextInfo:(void *)o_context
240 if( i_return == NSAlertAlternateReturn )
242 config_ResetAll( p_intf );
243 [_rootTreeItem resetView];
247 - (IBAction)buttonAction: (id)sender
249 [o_prefs_window orderOut: self];
250 [[o_basicFull_matrix cellAtRow:0 column:0] setState: NSOnState];
251 [[o_basicFull_matrix cellAtRow:0 column:1] setState: NSOffState];
252 [[[VLCMain sharedInstance] simplePreferences] showSimplePrefs];
255 - (void)loadConfigTree
259 - (void)outlineViewSelectionIsChanging:(NSNotification *)o_notification
263 /* update the document view to the view of the selected tree item */
264 - (void)outlineViewSelectionDidChange:(NSNotification *)o_notification
266 [[o_tree itemAtRow:[o_tree selectedRow]] showView: o_prefs_view];
267 [o_tree expandItem:[o_tree itemAtRow:[o_tree selectedRow]]];
272 @implementation VLCPrefs (NSTableDataSource)
274 - (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
276 return (item == nil) ? [_rootTreeItem numberOfChildren] : [item numberOfChildren];
279 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
281 return (item == nil) ? [_rootTreeItem numberOfChildren] : [item numberOfChildren];
284 - (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item
286 return (item == nil) ? (id)[_rootTreeItem childAtIndex:index]: (id)[item childAtIndex:index];
289 - (id)outlineView:(NSOutlineView *)outlineView
290 objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
292 return (item == nil) ? @"" : [item name];
298 @implementation VLCTreeMainItem
300 - (VLCTreeCategoryItem *)itemRepresentingCategory:(int)category
302 NSUInteger childrenCount = [[self children] count];
303 for( int i = 0; i < childrenCount; i++ )
305 VLCTreeCategoryItem * categoryItem = [[self children] objectAtIndex:i];
306 if( [categoryItem category] == category )
312 - (bool)isSubCategoryGeneral:(int)category
314 if(category == SUBCAT_VIDEO_GENERAL ||
315 category == SUBCAT_ADVANCED_MISC ||
316 category == SUBCAT_INPUT_GENERAL ||
317 category == SUBCAT_INTERFACE_GENERAL ||
318 category == SUBCAT_SOUT_GENERAL||
319 category == SUBCAT_PLAYLIST_GENERAL||
320 category == SUBCAT_AUDIO_GENERAL )
327 /* Creates and returns the array of children
328 * Loads children incrementally */
329 - (NSMutableArray *)children
331 if( _children ) return _children;
332 _children = [[NSMutableArray alloc] init];
334 intf_thread_t *p_intf = VLCIntf;
336 /* List the modules */
338 module_t ** modules = module_list_get( &count );
339 if( !modules ) return nil;
341 /* Build a tree of the plugins */
342 /* Add the capabilities */
343 for( i = 0; i < count; i++ )
345 VLCTreeCategoryItem * categoryItem = nil;
346 VLCTreeSubCategoryItem * subCategoryItem = nil;
347 VLCTreePluginItem * pluginItem = nil;
348 module_config_t *p_configs = NULL;
350 unsigned int confsize;
352 module_t * p_module = modules[i];
354 /* Exclude empty plugins (submodules don't have config */
355 /* options, they are stored in the parent module) */
356 if( module_is_main( p_module ) ) {
358 _configItems = module_config_get( p_module, &confsize );
359 _configSize = confsize;
361 pluginItem = [VLCTreePluginItem pluginTreeItemWithPlugin: p_module];
362 confsize = [pluginItem configSize];
364 p_configs = [pluginItem configItems];
367 for( j = 0; j < confsize; j++ )
369 int configType = p_configs[j].i_type;
370 if( configType == CONFIG_CATEGORY )
372 categoryItem = [self itemRepresentingCategory:p_configs[j].value.i];
375 categoryItem = [VLCTreeCategoryItem categoryTreeItemWithCategory:p_configs[j].value.i];
376 if(categoryItem) [[self children] addObject:categoryItem];
379 else if( configType == CONFIG_SUBCATEGORY )
381 lastsubcat = p_configs[j].value.i;
382 if( categoryItem && ![self isSubCategoryGeneral:lastsubcat] )
384 subCategoryItem = [categoryItem itemRepresentingSubCategory:lastsubcat];
387 subCategoryItem = [VLCTreeSubCategoryItem subCategoryTreeItemWithSubCategory:lastsubcat];
388 if(subCategoryItem) [[categoryItem children] addObject:subCategoryItem];
393 if( module_is_main( p_module) && CONFIG_ITEM(configType) )
395 if( categoryItem && [self isSubCategoryGeneral:lastsubcat] )
397 [[categoryItem options] addObject:[[VLCTreeLeafItem alloc] initWithConfigItem:&p_configs[j]]];
399 else if( subCategoryItem )
401 [[subCategoryItem options] addObject:[[VLCTreeLeafItem alloc] initWithConfigItem:&p_configs[j]]];
404 else if( !module_is_main( p_module) && CONFIG_ITEM(configType))
406 if( ![[subCategoryItem children] containsObject: pluginItem] )
408 [[subCategoryItem children] addObject:pluginItem];
411 [[pluginItem options] addObject:[[VLCTreeLeafItem alloc] initWithConfigItem:&p_configs[j]]];
415 module_list_free( modules );
421 @implementation VLCTreeCategoryItem
422 + (VLCTreeCategoryItem *)categoryTreeItemWithCategory:(int)category
424 if(!config_CategoryNameGet( category )) return nil;
425 return [[[[self class] alloc] initWithCategory:category] autorelease];
427 - (id)initWithCategory:(int)category
429 NSString * name = [[VLCMain sharedInstance] localizedString: config_CategoryNameGet( category )];
430 if(self = [super initWithName:name])
432 _category = category;
433 //_help = [[[VLCMain sharedInstance] localizedString: config_CategoryHelpGet( category )] retain];
438 - (VLCTreeSubCategoryItem *)itemRepresentingSubCategory:(int)subCategory
440 assert( [self isKindOfClass:[VLCTreeCategoryItem class]] );
441 NSUInteger childrenCount = [[self children] count];
442 for( NSUInteger i = 0; i < childrenCount; i++ )
444 VLCTreeSubCategoryItem * subCategoryItem = [[self children] objectAtIndex:i];
445 if( [subCategoryItem subCategory] == subCategory )
446 return subCategoryItem;
458 @implementation VLCTreeSubCategoryItem
459 - (id)initWithSubCategory:(int)subCategory
461 NSString * name = [[VLCMain sharedInstance] localizedString: config_CategoryNameGet( subCategory )];
462 if(self = [super initWithName:name])
464 _subCategory = subCategory;
465 //_help = [[[VLCMain sharedInstance] localizedString: config_CategoryHelpGet( subCategory )] retain];
470 + (VLCTreeSubCategoryItem *)subCategoryTreeItemWithSubCategory:(int)subCategory
472 if(!config_CategoryNameGet( subCategory )) return nil;
473 return [[[[self class] alloc] initWithSubCategory:subCategory] autorelease];
484 @implementation VLCTreePluginItem
485 - (id)initWithPlugin:(module_t *)plugin
487 NSString * name = [[VLCMain sharedInstance] localizedString: module_get_name( plugin, false )?:""];
488 if(self = [super initWithName:name])
490 _configItems = module_config_get( plugin, &_configSize );
492 //_help = [[[VLCMain sharedInstance] localizedString: config_CategoryHelpGet( subCategory )] retain];
497 + (VLCTreePluginItem *)pluginTreeItemWithPlugin:(module_t *)plugin
499 return [[[[self class] alloc] initWithPlugin:plugin] autorelease];
504 module_config_free( _configItems );
508 - (module_config_t *)configItems
513 - (unsigned int)configSize
521 @implementation VLCTreeLeafItem
523 - (id)initWithConfigItem: (module_config_t *) configItem
525 NSString * name = [[VLCMain sharedInstance] localizedString:configItem->psz_name];
526 self = [super initWithName:name];
530 _configItem = configItem;
535 - (module_config_t *)configItem
542 #pragma mark (Root class for all TreeItems)
543 @implementation VLCTreeItem
545 - (id)initWithName:(NSString*)name
550 _name = [name retain];
564 - (VLCTreeItem *)childAtIndex:(NSInteger)i_index
566 return [[self children] objectAtIndex:i_index];
569 - (int)numberOfChildren
571 return [[self children] count];
576 return [[_name retain] autorelease];
579 - (void)showView:(NSScrollView *)prefsView
584 [[VLCPrefs sharedInstance] setTitle: [self name]];
585 s_vrc = [[prefsView contentView] bounds]; s_vrc.size.height -= 4;
586 view = [[VLCFlippedView alloc] initWithFrame: s_vrc];
587 [view setAutoresizingMask: NSViewWidthSizable | NSViewMinYMargin | NSViewMaxYMargin];
591 _subviews = [[NSMutableArray alloc] initWithCapacity:10];
593 NSUInteger count = [[self options] count];
594 for( NSUInteger i = 0; i < count; i++)
596 VLCTreeLeafItem * item = [[self options] objectAtIndex:i];
598 VLCConfigControl *control;
599 control = [VLCConfigControl newControl:[item configItem] withView:view];
602 [control setAutoresizingMask: NSViewMaxYMargin | NSViewWidthSizable];
603 [_subviews addObject: control];
614 NSEnumerator *enumerator = [_subviews objectEnumerator];
615 VLCConfigControl *widget;
618 while( ( widget = [enumerator nextObject] ) )
619 if( i_max_label < [widget labelSize] )
620 i_max_label = [widget labelSize];
622 enumerator = [_subviews objectEnumerator];
623 while( ( widget = [enumerator nextObject] ) )
627 i_widget = [widget viewType];
628 i_yPos += [VLCConfigControl calcVerticalMargin:i_widget lastItem:i_lastItem];
629 [widget setYPos:i_yPos];
630 frame = [widget frame];
631 frame.size.width = [view frame].size.width - LEFTMARGIN - RIGHTMARGIN;
632 [widget setFrame:frame];
633 [widget alignWithXPosition: i_max_label];
634 i_yPos += [widget frame].size.height;
635 i_lastItem = i_widget;
636 [view addSubview:widget];
639 frame = [view frame];
640 frame.size.height = i_yPos;
641 [view setFrame:frame];
642 [prefsView setDocumentView:view];
648 NSUInteger count = [_subviews count];
649 for( i = 0 ; i < count ; i++ )
650 [[_subviews objectAtIndex:i] applyChanges];
652 count = [_children count];
653 for( i = 0 ; i < count ; i++ )
654 [[_children objectAtIndex:i] applyChanges];
660 NSUInteger count = [_subviews count];
661 for( i = 0 ; i < count ; i++ )
662 [[_subviews objectAtIndex:i] resetValues];
664 count = [_options count];
665 for( i = 0 ; i < count ; i++ )
666 [[_options objectAtIndex:i] resetView];
668 count = [_children count];
669 for( i = 0 ; i < count ; i++ )
670 [[_children objectAtIndex:i] resetView];
674 - (NSMutableArray *)children
676 if(!_children) _children = [[NSMutableArray alloc] init];
680 - (NSMutableArray *)options
682 if(!_options) _options = [[NSMutableArray alloc] init];
688 @implementation VLCFlippedView