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;
69 module_config_t * _configItem;
71 - (id)initWithConfigItem:(module_config_t *)configItem;
73 - (id)initWithName:(NSString*)name andConfigItem:(module_config_t *)configItem;
75 - (int)numberOfChildren;
76 - (VLCTreeItem *)childAtIndex:(int)i_index;
78 - (module_config_t *)configItem;
81 - (NSMutableArray *)children;
82 - (NSMutableArray *)options;
83 - (void)showView:(NSScrollView *)o_prefs_view;
90 @interface VLCTreeSubCategoryItem : VLCTreeItem
94 + (VLCTreeSubCategoryItem *)subCategoryTreeItemWithSubCategory:(int)subCategory;
95 - (id)initWithSubCategory:(int)subCategory;
99 /* Plugin daughters */
100 @interface VLCTreePluginItem : VLCTreeItem
103 + (VLCTreePluginItem *)pluginTreeItemWithPlugin:(module_t *)plugin;
104 - (id)initWithPlugin:(module_t *)plugin;
108 @interface VLCTreeCategoryItem : VLCTreeItem
112 + (VLCTreeCategoryItem *)categoryTreeItemWithCategory:(int)category;
113 - (id)initWithCategory:(int)category;
115 - (VLCTreeSubCategoryItem *)itemRepresentingSubCategory:(int)category;
118 /* individual options. */
119 @interface VLCTreeLeafItem : VLCTreeItem
123 @interface VLCTreeMainItem : VLCTreeItem
125 module_config_t * _configItems;
127 - (VLCTreeCategoryItem *)itemRepresentingCategory:(int)category;
132 /*****************************************************************************
133 * VLCPrefs implementation
134 *****************************************************************************/
135 @implementation VLCPrefs
137 static VLCPrefs *_o_sharedMainInstance = nil;
139 + (VLCPrefs *)sharedInstance
141 return _o_sharedMainInstance ? _o_sharedMainInstance : [[self alloc] init];
146 if( _o_sharedMainInstance ) {
151 _o_sharedMainInstance = [super init];
153 o_empty_view = [[NSView alloc] init];
154 _rootTreeItem = [[VLCTreeMainItem alloc] init];
157 return _o_sharedMainInstance;
162 [o_empty_view release];
163 [_rootTreeItem release];
172 [o_prefs_view setBorderType: NSGrooveBorder];
173 [o_prefs_view setHasVerticalScroller: YES];
174 [o_prefs_view setDrawsBackground: NO];
175 [o_prefs_view setDocumentView: o_empty_view];
176 [o_tree selectRow:0 byExtendingSelection:NO];
179 - (void)setTitle: (NSString *) o_title_name
181 [o_title setStringValue: o_title_name];
186 [[o_basicFull_matrix cellAtRow:0 column:0] setState: NSOffState];
187 [[o_basicFull_matrix cellAtRow:0 column:1] setState: NSOnState];
189 [o_prefs_window center];
190 [o_prefs_window makeKeyAndOrderFront:self];
195 [o_prefs_window setTitle: _NS("Preferences")];
196 [o_save_btn setTitle: _NS("Save")];
197 [o_cancel_btn setTitle: _NS("Cancel")];
198 [o_reset_btn setTitle: _NS("Reset All")];
199 [[o_basicFull_matrix cellAtRow: 0 column: 0] setStringValue: _NS("Basic")];
200 [[o_basicFull_matrix cellAtRow: 0 column: 1] setStringValue: _NS("All")];
203 - (IBAction)savePrefs: (id)sender
205 /* TODO: call savePrefs on Root item */
206 [_rootTreeItem applyChanges];
207 config_SaveConfigFile( p_intf, NULL );
208 [o_prefs_window orderOut:self];
211 - (IBAction)closePrefs: (id)sender
213 [o_prefs_window orderOut:self];
216 - (IBAction)resetAll: (id)sender
218 NSBeginInformationalAlertSheet(_NS("Reset Preferences"), _NS("Cancel"),
219 _NS("Continue"), nil, o_prefs_window, self,
220 @selector(sheetDidEnd: returnCode: contextInfo:), NULL, nil,
221 _NS("Beware this will reset the VLC media player preferences.\n"
222 "Are you sure you want to continue?") );
225 - (void)sheetDidEnd:(NSWindow *)o_sheet returnCode:(int)i_return
226 contextInfo:(void *)o_context
228 if( i_return == NSAlertAlternateReturn )
230 [o_prefs_view setDocumentView: o_empty_view];
231 config_ResetAll( p_intf );
232 [_rootTreeItem resetView];
233 [[o_tree itemAtRow:[o_tree selectedRow]]
234 showView:o_prefs_view];
238 - (IBAction)buttonAction: (id)sender
240 [o_prefs_window orderOut: self];
241 [[o_basicFull_matrix cellAtRow:0 column:0] setState: NSOnState];
242 [[o_basicFull_matrix cellAtRow:0 column:1] setState: NSOffState];
243 [[[VLCMain sharedInstance] simplePreferences] showSimplePrefs];
246 - (void)loadConfigTree
250 - (void)outlineViewSelectionIsChanging:(NSNotification *)o_notification
254 /* update the document view to the view of the selected tree item */
255 - (void)outlineViewSelectionDidChange:(NSNotification *)o_notification
257 [[o_tree itemAtRow:[o_tree selectedRow]] showView: o_prefs_view];
262 @implementation VLCPrefs (NSTableDataSource)
264 - (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
266 return (item == nil) ? [_rootTreeItem numberOfChildren] : [item numberOfChildren];
269 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
271 return (item == nil) ? [_rootTreeItem numberOfChildren] : [item numberOfChildren];
274 - (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item
276 return (item == nil) ? (id)[_rootTreeItem childAtIndex:index]: (id)[item childAtIndex:index];
279 - (id)outlineView:(NSOutlineView *)outlineView
280 objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
282 return (item == nil) ? @"" : [item name];
288 @implementation VLCTreeMainItem
290 - (VLCTreeCategoryItem *)itemRepresentingCategory:(int)category
292 for( int i = 0; i < [[self children] count]; i++ )
294 VLCTreeCategoryItem * categoryItem = [[self children] objectAtIndex:i];
295 if( [categoryItem category] == category )
303 module_config_free( _configItems );
307 - (bool)isSubCategoryGeneral:(int)category
309 if(category == SUBCAT_VIDEO_GENERAL ||
310 category == SUBCAT_ADVANCED_MISC ||
311 category == SUBCAT_INPUT_GENERAL ||
312 category == SUBCAT_INTERFACE_GENERAL ||
313 category == SUBCAT_SOUT_GENERAL||
314 category == SUBCAT_PLAYLIST_GENERAL||
315 category == SUBCAT_AUDIO_GENERAL )
322 /* Creates and returns the array of children
323 * Loads children incrementally */
324 - (NSMutableArray *)children
326 if( _children ) return _children;
327 _children = [[NSMutableArray alloc] init];
329 intf_thread_t *p_intf = VLCIntf;
331 /* List the modules */
333 module_t ** modules = module_list_get( &count );
334 if( !modules ) return nil;
336 /* Build a tree of the plugins */
337 /* Add the capabilities */
338 for( i = 0; i < count; i++ )
340 module_t * p_module = modules[i];
342 /* Exclude empty plugins (submodules don't have config */
343 /* options, they are stored in the parent module) */
344 unsigned int confsize;
345 _configItems = module_config_get( p_module, &confsize );
347 VLCTreeCategoryItem * categoryItem = nil;
348 VLCTreeSubCategoryItem * subCategoryItem = nil;
349 VLCTreePluginItem * pluginItem = nil;
353 for( j = 0; j < confsize; j++ )
355 int configType = _configItems[j].i_type;
356 if( configType == CONFIG_CATEGORY )
358 categoryItem = [self itemRepresentingCategory:_configItems[j].value.i];
361 categoryItem = [VLCTreeCategoryItem categoryTreeItemWithCategory:_configItems[j].value.i];
362 if(categoryItem) [[self children] addObject:categoryItem];
365 else if( configType == CONFIG_SUBCATEGORY )
367 lastsubcat = _configItems[j].value.i;
368 if( categoryItem && ![self isSubCategoryGeneral:lastsubcat] )
370 subCategoryItem = [categoryItem itemRepresentingSubCategory:lastsubcat];
373 subCategoryItem = [VLCTreeSubCategoryItem subCategoryTreeItemWithSubCategory:lastsubcat];
374 if(subCategoryItem) [[categoryItem children] addObject:subCategoryItem];
379 if( module_is_main( p_module) && (configType & CONFIG_ITEM) )
381 if( categoryItem && [self isSubCategoryGeneral:lastsubcat] )
383 [[categoryItem options] addObject:[[VLCTreeLeafItem alloc] initWithConfigItem:&_configItems[j]]];
385 else if( subCategoryItem )
387 [[subCategoryItem options] addObject:[[VLCTreeLeafItem alloc] initWithConfigItem:&_configItems[j]]];
390 else if( !module_is_main( p_module) && (configType & CONFIG_ITEM))
394 pluginItem = [VLCTreePluginItem pluginTreeItemWithPlugin: p_module];
395 if(pluginItem) [[subCategoryItem children] addObject:pluginItem];
398 [[pluginItem options] addObject:[[VLCTreeLeafItem alloc] initWithConfigItem:&_configItems[j]]];
402 module_list_free( modules );
408 @implementation VLCTreeCategoryItem
409 + (VLCTreeCategoryItem *)categoryTreeItemWithCategory:(int)category
411 return [[[[self class] alloc] initWithCategory:category] autorelease];
413 - (id)initWithCategory:(int)category
415 if(!config_CategoryNameGet( category )) return nil;
416 NSString * name = [[VLCMain sharedInstance] localizedString: config_CategoryNameGet( category )];
417 if(self = [super initWithName:name andConfigItem:nil])
419 _category = category;
420 //_help = [[[VLCMain sharedInstance] localizedString: config_CategoryHelpGet( category )] retain];
425 - (VLCTreeSubCategoryItem *)itemRepresentingSubCategory:(int)subCategory
427 assert( [self isKindOfClass:[VLCTreeCategoryItem class]] );
428 for( int i = 0; i < [[self children] count]; i++ )
430 VLCTreeSubCategoryItem * subCategoryItem = [[self children] objectAtIndex:i];
431 if( [subCategoryItem subCategory] == subCategory )
432 return subCategoryItem;
444 @implementation VLCTreeSubCategoryItem
445 - (id)initWithSubCategory:(int)subCategory
447 if(!config_CategoryNameGet( subCategory )) return nil;
448 NSString * name = [[VLCMain sharedInstance] localizedString: config_CategoryNameGet( subCategory )];
449 if(self = [super initWithName:name andConfigItem:NULL])
451 _subCategory = subCategory;
452 //_help = [[[VLCMain sharedInstance] localizedString: config_CategoryHelpGet( subCategory )] retain];
457 + (VLCTreeSubCategoryItem *)subCategoryTreeItemWithSubCategory:(int)subCategory
459 return [[[[self class] alloc] initWithSubCategory:subCategory] autorelease];
470 @implementation VLCTreePluginItem
471 - (id)initWithPlugin:(module_t *)plugin
473 NSString * name = [[VLCMain sharedInstance] localizedString: module_get_name( plugin, false )];
474 if(self = [super initWithName:name andConfigItem:NULL])
477 //_help = [[[VLCMain sharedInstance] localizedString: config_CategoryHelpGet( subCategory )] retain];
482 + (VLCTreePluginItem *)pluginTreeItemWithPlugin:(module_t *)plugin
484 return [[[[self class] alloc] initWithPlugin:plugin] autorelease];
490 @implementation VLCTreeLeafItem
494 #pragma mark (Root class for all TreeItems)
495 @implementation VLCTreeItem
497 - (id)initWithConfigItem: (module_config_t *) configItem
499 NSString * name = [[VLCMain sharedInstance] localizedString:configItem->psz_name];
500 return [self initWithName:name andConfigItem:configItem];
503 - (id)initWithName:(NSString*)name andConfigItem:(module_config_t *)configItem
508 _name = [name retain];
509 _configItem = configItem;
523 - (VLCTreeItem *)childAtIndex:(int)i_index
525 return [[self children] objectAtIndex:i_index];
528 - (int)numberOfChildren
530 return [[self children] count];
535 return [[_name retain] autorelease];
538 - (void)showView:(NSScrollView *)prefsView
543 [[VLCPrefs sharedInstance] setTitle: [self name]];
544 s_vrc = [[prefsView contentView] bounds]; s_vrc.size.height -= 4;
545 view = [[VLCFlippedView alloc] initWithFrame: s_vrc];
546 [view setAutoresizingMask: NSViewWidthSizable | NSViewMinYMargin | NSViewMaxYMargin];
550 _subviews = [[NSMutableArray alloc] initWithCapacity:10];
553 for( i = 0; i < [[self options] count]; i++)
555 VLCTreeItem * item = [[self options] objectAtIndex:i];
557 VLCConfigControl *control;
558 control = [VLCConfigControl newControl:[item configItem] withView:view];
561 [control setAutoresizingMask: NSViewMaxYMargin | NSViewWidthSizable];
562 [_subviews addObject: control];
573 NSEnumerator *enumerator = [_subviews objectEnumerator];
574 VLCConfigControl *widget;
577 while( ( widget = [enumerator nextObject] ) )
578 if( i_max_label < [widget labelSize] )
579 i_max_label = [widget labelSize];
581 enumerator = [_subviews objectEnumerator];
582 while( ( widget = [enumerator nextObject] ) )
586 i_widget = [widget viewType];
587 i_yPos += [VLCConfigControl calcVerticalMargin:i_widget lastItem:i_lastItem];
588 [widget setYPos:i_yPos];
589 frame = [widget frame];
590 frame.size.width = [view frame].size.width - LEFTMARGIN - RIGHTMARGIN;
591 [widget setFrame:frame];
592 [widget alignWithXPosition: i_max_label];
593 i_yPos += [widget frame].size.height;
594 i_lastItem = i_widget;
595 [view addSubview:widget];
598 frame = [view frame];
599 frame.size.height = i_yPos;
600 [view setFrame:frame];
601 [prefsView setDocumentView:view];
607 for( i = 0 ; i < [_subviews count] ; i++ )
608 [[_subviews objectAtIndex:i] applyChanges];
610 for( i = 0 ; i < [_children count] ; i++ )
611 [[_children objectAtIndex:i] applyChanges];
620 for( i = 0 ; i < [_children count] ; i++ )
621 [[_children objectAtIndex:i] resetView];
624 - (NSMutableArray *)children
626 if(!_children) _children = [[NSMutableArray alloc] init];
630 - (NSMutableArray *)options
632 if(!_options) _options = [[NSMutableArray alloc] init];
636 - (module_config_t *)configItem
643 @implementation VLCFlippedView