1 /*****************************************************************************
2 * prefs.m: MacOS X module for vlc
3 *****************************************************************************
4 * Copyright (C) 2002-2006 VLC authors and 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., 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_prefs_window center];
198 [o_prefs_window makeKeyAndOrderFront:self];
199 [_rootTreeItem resetView];
204 [o_prefs_window setTitle: _NS("Preferences")];
205 [o_save_btn setTitle: _NS("Save")];
206 [o_cancel_btn setTitle: _NS("Cancel")];
207 [o_reset_btn setTitle: _NS("Reset All")];
208 [o_showBasic_btn setTitle: _NS("Show Basic")];
211 - (IBAction)savePrefs: (id)sender
213 /* TODO: call savePrefs on Root item */
214 [_rootTreeItem applyChanges];
215 config_SaveConfigFile( p_intf );
216 [o_prefs_window orderOut:self];
219 - (IBAction)closePrefs: (id)sender
221 [o_prefs_window orderOut:self];
224 - (IBAction)resetAll: (id)sender
226 NSBeginInformationalAlertSheet(_NS("Reset Preferences"), _NS("Cancel"),
227 _NS("Continue"), nil, o_prefs_window, self,
228 @selector(sheetDidEnd: returnCode: contextInfo:), NULL, nil,
229 _NS("Beware this will reset the VLC media player preferences.\n"
230 "Are you sure you want to continue?") );
233 - (void)sheetDidEnd:(NSWindow *)o_sheet returnCode:(int)i_return
234 contextInfo:(void *)o_context
236 if( i_return == NSAlertAlternateReturn )
238 config_ResetAll( p_intf );
239 [_rootTreeItem resetView];
243 - (IBAction)buttonAction: (id)sender
245 [o_prefs_window orderOut: self];
246 [[[VLCMain sharedInstance] simplePreferences] showSimplePrefs];
249 - (void)loadConfigTree
253 - (void)outlineViewSelectionIsChanging:(NSNotification *)o_notification
257 /* update the document view to the view of the selected tree item */
258 - (void)outlineViewSelectionDidChange:(NSNotification *)o_notification
260 [[o_tree itemAtRow:[o_tree selectedRow]] showView: o_prefs_view];
261 [o_tree expandItem:[o_tree itemAtRow:[o_tree selectedRow]]];
266 @implementation VLCPrefs (NSTableDataSource)
268 - (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
270 return (item == nil) ? [_rootTreeItem numberOfChildren] : [item numberOfChildren];
273 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
275 return (item == nil) ? [_rootTreeItem numberOfChildren] : [item numberOfChildren];
278 - (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item
280 return (item == nil) ? (id)[_rootTreeItem childAtIndex:index]: (id)[item childAtIndex:index];
283 - (id)outlineView:(NSOutlineView *)outlineView
284 objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
286 return (item == nil) ? @"" : [item name];
292 @implementation VLCTreeMainItem
294 - (VLCTreeCategoryItem *)itemRepresentingCategory:(int)category
296 NSUInteger childrenCount = [[self children] count];
297 for( int i = 0; i < childrenCount; 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 ) ) {
352 _configItems = module_config_get( p_module, &confsize );
353 _configSize = confsize;
355 pluginItem = [VLCTreePluginItem pluginTreeItemWithPlugin: p_module];
356 confsize = [pluginItem configSize];
358 p_configs = [pluginItem configItems];
361 for( j = 0; j < confsize; j++ )
363 int configType = p_configs[j].i_type;
364 if( configType == CONFIG_CATEGORY )
366 categoryItem = [self itemRepresentingCategory:p_configs[j].value.i];
369 categoryItem = [VLCTreeCategoryItem categoryTreeItemWithCategory:p_configs[j].value.i];
370 if(categoryItem) [[self children] addObject:categoryItem];
373 else if( configType == CONFIG_SUBCATEGORY )
375 lastsubcat = p_configs[j].value.i;
376 if( categoryItem && ![self isSubCategoryGeneral:lastsubcat] )
378 subCategoryItem = [categoryItem itemRepresentingSubCategory:lastsubcat];
381 subCategoryItem = [VLCTreeSubCategoryItem subCategoryTreeItemWithSubCategory:lastsubcat];
382 if(subCategoryItem) [[categoryItem children] addObject:subCategoryItem];
387 if( module_is_main( p_module) && CONFIG_ITEM(configType) )
389 if( categoryItem && [self isSubCategoryGeneral:lastsubcat] )
391 [[categoryItem options] addObject:[[VLCTreeLeafItem alloc] initWithConfigItem:&p_configs[j]]];
393 else if( subCategoryItem )
395 [[subCategoryItem options] addObject:[[VLCTreeLeafItem alloc] initWithConfigItem:&p_configs[j]]];
398 else if( !module_is_main( p_module) && CONFIG_ITEM(configType))
400 if( ![[subCategoryItem children] containsObject: pluginItem] )
402 [[subCategoryItem children] addObject:pluginItem];
405 [[pluginItem options] addObject:[[VLCTreeLeafItem alloc] initWithConfigItem:&p_configs[j]]];
409 module_list_free( modules );
415 @implementation VLCTreeCategoryItem
416 + (VLCTreeCategoryItem *)categoryTreeItemWithCategory:(int)category
418 if(!config_CategoryNameGet( category )) return nil;
419 return [[[[self class] alloc] initWithCategory:category] autorelease];
421 - (id)initWithCategory:(int)category
423 NSString * name = _NS(config_CategoryNameGet( category ));
424 if(self = [super initWithName:name])
426 _category = category;
427 //_help = [_NS(config_CategoryHelpGet( category )) retain];
432 - (VLCTreeSubCategoryItem *)itemRepresentingSubCategory:(int)subCategory
434 assert( [self isKindOfClass:[VLCTreeCategoryItem class]] );
435 NSUInteger childrenCount = [[self children] count];
436 for( NSUInteger i = 0; i < childrenCount; 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 = _NS(config_CategoryNameGet( subCategory ));
456 if(self = [super initWithName:name])
458 _subCategory = subCategory;
459 //_help = [_NS(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 = _NS( module_get_name( plugin, false )?:"" );
482 if(self = [super initWithName:name])
484 _configItems = module_config_get( plugin, &_configSize );
486 //_help = [_NS(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 = _NS(configItem->psz_name);
520 self = [super initWithName:name];
524 _configItem = configItem;
529 - (module_config_t *)configItem
536 #pragma mark (Root class for all TreeItems)
537 @implementation VLCTreeItem
539 - (id)initWithName:(NSString*)name
544 _name = [name retain];
558 - (VLCTreeItem *)childAtIndex:(NSInteger)i_index
560 return [[self children] objectAtIndex:i_index];
563 - (int)numberOfChildren
565 return [[self children] count];
570 return [[_name retain] autorelease];
573 - (void)showView:(NSScrollView *)prefsView
578 [[VLCPrefs sharedInstance] setTitle: [self name]];
579 s_vrc = [[prefsView contentView] bounds]; s_vrc.size.height -= 4;
580 view = [[VLCFlippedView alloc] initWithFrame: s_vrc];
581 [view setAutoresizingMask: NSViewWidthSizable | NSViewMinYMargin | NSViewMaxYMargin];
585 _subviews = [[NSMutableArray alloc] initWithCapacity:10];
587 NSUInteger count = [[self options] count];
588 for( NSUInteger i = 0; i < count; i++)
590 VLCTreeLeafItem * item = [[self options] objectAtIndex:i];
592 VLCConfigControl *control;
593 control = [VLCConfigControl newControl:[item configItem] withView:view];
596 [control setAutoresizingMask: NSViewMaxYMargin | NSViewWidthSizable];
597 [_subviews addObject: control];
608 NSEnumerator *enumerator = [_subviews objectEnumerator];
609 VLCConfigControl *widget;
612 while( ( widget = [enumerator nextObject] ) )
613 if( i_max_label < [widget labelSize] )
614 i_max_label = [widget labelSize];
616 enumerator = [_subviews objectEnumerator];
617 while( ( widget = [enumerator nextObject] ) )
621 i_widget = [widget viewType];
622 i_yPos += [VLCConfigControl calcVerticalMargin:i_widget lastItem:i_lastItem];
623 [widget setYPos:i_yPos];
624 frame = [widget frame];
625 frame.size.width = [view frame].size.width - LEFTMARGIN - RIGHTMARGIN;
626 [widget setFrame:frame];
627 [widget alignWithXPosition: i_max_label];
628 i_yPos += [widget frame].size.height;
629 i_lastItem = i_widget;
630 [view addSubview:widget];
633 frame = [view frame];
634 frame.size.height = i_yPos;
635 [view setFrame:frame];
636 [prefsView setDocumentView:view];
642 NSUInteger count = [_subviews count];
643 for( i = 0 ; i < count ; i++ )
644 [[_subviews objectAtIndex:i] applyChanges];
646 count = [_children count];
647 for( i = 0 ; i < count ; i++ )
648 [[_children objectAtIndex:i] applyChanges];
654 NSUInteger count = [_subviews count];
655 for( i = 0 ; i < count ; i++ )
656 [[_subviews objectAtIndex:i] resetValues];
658 count = [_options count];
659 for( i = 0 ; i < count ; i++ )
660 [[_options objectAtIndex:i] resetView];
662 count = [_children count];
663 for( i = 0 ; i < count ; i++ )
664 [[_children objectAtIndex:i] resetView];
668 - (NSMutableArray *)children
670 if(!_children) _children = [[NSMutableArray alloc] init];
674 - (NSMutableArray *)options
676 if(!_options) _options = [[NSMutableArray alloc] init];
682 @implementation VLCFlippedView