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 *_subviews;
68 module_config_t * _configItem;
70 - (id)initWithConfigItem:(module_config_t *)configItem;
72 - (id)initWithName:(NSString*)name andConfigItem:(module_config_t *)configItem;
74 - (int)numberOfChildren;
75 - (VLCTreeItem *)childAtIndex:(int)i_index;
77 - (module_config_t *)configItem;
80 - (NSMutableArray *)children;
81 - (void)showView:(NSScrollView *)o_prefs_view;
87 @interface VLCTreeSubCategoryItem : VLCTreeItem
91 + (VLCTreeSubCategoryItem *)subCategoryTreeItemWithSubCategory:(int)category;
92 - (id)initWithSubCategory:(int)subCategory;
96 @interface VLCTreeCategoryItem : VLCTreeItem
100 + (VLCTreeCategoryItem *)categoryTreeItemWithCategory:(int)category;
101 - (id)initWithCategory:(int)category;
103 - (VLCTreeSubCategoryItem *)itemRepresentingSubCategory:(int)category;
107 @interface VLCTreeLeafItem : VLCTreeItem
111 @interface VLCTreeMainItem : VLCTreeItem
113 module_config_t * _configItems;
115 - (VLCTreeCategoryItem *)itemRepresentingCategory:(int)category;
120 /*****************************************************************************
121 * VLCPrefs implementation
122 *****************************************************************************/
123 @implementation VLCPrefs
125 static VLCPrefs *_o_sharedMainInstance = nil;
127 + (VLCPrefs *)sharedInstance
129 return _o_sharedMainInstance ? _o_sharedMainInstance : [[self alloc] init];
134 if( _o_sharedMainInstance ) {
139 _o_sharedMainInstance = [super init];
141 o_empty_view = [[NSView alloc] init];
142 _rootTreeItem = [[VLCTreeMainItem alloc] init];
145 return _o_sharedMainInstance;
150 [o_empty_view release];
151 [_rootTreeItem release];
160 [o_prefs_view setBorderType: NSGrooveBorder];
161 [o_prefs_view setHasVerticalScroller: YES];
162 [o_prefs_view setDrawsBackground: NO];
163 [o_prefs_view setDocumentView: o_empty_view];
164 [o_tree selectRow:0 byExtendingSelection:NO];
167 - (void)setTitle: (NSString *) o_title_name
169 [o_title setStringValue: o_title_name];
174 [[o_basicFull_matrix cellAtRow:0 column:0] setState: NSOffState];
175 [[o_basicFull_matrix cellAtRow:0 column:1] setState: NSOnState];
177 [o_prefs_window center];
178 [o_prefs_window makeKeyAndOrderFront:self];
183 [o_prefs_window setTitle: _NS("Preferences")];
184 [o_save_btn setTitle: _NS("Save")];
185 [o_cancel_btn setTitle: _NS("Cancel")];
186 [o_reset_btn setTitle: _NS("Reset All")];
187 [[o_basicFull_matrix cellAtRow: 0 column: 0] setStringValue: _NS("Basic")];
188 [[o_basicFull_matrix cellAtRow: 0 column: 1] setStringValue: _NS("All")];
191 - (IBAction)savePrefs: (id)sender
193 /* TODO: call savePrefs on Root item */
194 [_rootTreeItem applyChanges];
195 config_SaveConfigFile( p_intf, NULL );
196 [o_prefs_window orderOut:self];
199 - (IBAction)closePrefs: (id)sender
201 [o_prefs_window orderOut:self];
204 - (IBAction)resetAll: (id)sender
206 NSBeginInformationalAlertSheet(_NS("Reset Preferences"), _NS("Cancel"),
207 _NS("Continue"), nil, o_prefs_window, self,
208 @selector(sheetDidEnd: returnCode: contextInfo:), NULL, nil,
209 _NS("Beware this will reset the VLC media player preferences.\n"
210 "Are you sure you want to continue?") );
213 - (void)sheetDidEnd:(NSWindow *)o_sheet returnCode:(int)i_return
214 contextInfo:(void *)o_context
216 if( i_return == NSAlertAlternateReturn )
218 [o_prefs_view setDocumentView: o_empty_view];
219 config_ResetAll( p_intf );
220 [_rootTreeItem resetView];
221 [[o_tree itemAtRow:[o_tree selectedRow]]
222 showView:o_prefs_view];
226 - (IBAction)buttonAction: (id)sender
228 [o_prefs_window orderOut: self];
229 [[o_basicFull_matrix cellAtRow:0 column:0] setState: NSOnState];
230 [[o_basicFull_matrix cellAtRow:0 column:1] setState: NSOffState];
231 [[[VLCMain sharedInstance] getSimplePreferences] showSimplePrefs];
234 - (void)loadConfigTree
238 - (void)outlineViewSelectionIsChanging:(NSNotification *)o_notification
242 /* update the document view to the view of the selected tree item */
243 - (void)outlineViewSelectionDidChange:(NSNotification *)o_notification
245 [[o_tree itemAtRow:[o_tree selectedRow]] showView: o_prefs_view];
250 @implementation VLCPrefs (NSTableDataSource)
252 - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
254 return (item == nil) ? [_rootTreeItem numberOfChildren] : [item numberOfChildren];
257 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
259 return ![item isKindOfClass:[VLCTreeSubCategoryItem class]];
262 - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item
264 return (item == nil) ? (id)[_rootTreeItem childAtIndex:index]: (id)[item childAtIndex:index];
267 - (id)outlineView:(NSOutlineView *)outlineView
268 objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
270 return (item == nil) ? @"" : [item name];
276 @implementation VLCTreeMainItem
278 - (VLCTreeCategoryItem *)itemRepresentingCategory:(int)category
280 for( int i = 0; i < [[self children] count]; i++ )
282 VLCTreeCategoryItem * categoryItem = [[self children] objectAtIndex:i];
283 if( [categoryItem category] == category )
291 module_config_free( _configItems );
295 /* Creates and returns the array of children
296 * Loads children incrementally */
297 - (NSMutableArray *)children
299 if( _children ) return _children;
300 _children = [[NSMutableArray alloc] init];
302 intf_thread_t *p_intf = VLCIntf;
304 /* List the modules */
306 module_t ** modules = module_list_get( &count );
307 if( !modules ) return nil;
309 /* Build a tree of the plugins */
310 /* Add the capabilities */
311 for( i = 0; i < count; i++ )
313 module_t * p_module = modules[i];
315 /* Exclude empty plugins (submodules don't have config */
316 /* options, they are stored in the parent module) */
317 unsigned int confsize;
318 _configItems = module_config_get( p_module, &confsize );
320 VLCTreeCategoryItem * categoryItem = nil;
321 VLCTreeSubCategoryItem * subCategoryItem = nil;
324 for( j = 0; j < confsize; j++ )
326 int configType = _configItems[j].i_type;
327 if( configType == CONFIG_CATEGORY )
329 categoryItem = [self itemRepresentingCategory:_configItems[j].value.i];
332 categoryItem = [VLCTreeCategoryItem categoryTreeItemWithCategory:_configItems[j].value.i];
333 if(categoryItem) [[self children] addObject:categoryItem];
336 else if( configType == CONFIG_SUBCATEGORY && categoryItem )
338 subCategoryItem = [categoryItem itemRepresentingSubCategory:_configItems[j].value.i];
341 subCategoryItem = [VLCTreeSubCategoryItem subCategoryTreeItemWithSubCategory:_configItems[j].value.i];
342 if(subCategoryItem) [[categoryItem children] addObject:subCategoryItem];
345 else if( (configType & CONFIG_ITEM) && subCategoryItem )
347 [[subCategoryItem children] addObject:[[VLCTreeLeafItem alloc] initWithConfigItem:&_configItems[j]]];
352 module_list_free( modules );
358 @implementation VLCTreeCategoryItem
359 + (VLCTreeCategoryItem *)categoryTreeItemWithCategory:(int)category
361 return [[[[self class] alloc] initWithCategory:category] autorelease];
363 - (id)initWithCategory:(int)category
365 if(!config_CategoryNameGet( category )) return nil;
366 NSString * name = [[VLCMain sharedInstance] localizedString: config_CategoryNameGet( category )];
367 if(self = [super initWithName:name andConfigItem:nil])
369 _category = category;
370 //_help = [[[VLCMain sharedInstance] localizedString: config_CategoryHelpGet( category )] retain];
375 - (VLCTreeSubCategoryItem *)itemRepresentingSubCategory:(int)subCategory
377 assert( [self isKindOfClass:[VLCTreeCategoryItem class]] );
378 for( int i = 0; i < [[self children] count]; i++ )
380 VLCTreeSubCategoryItem * subCategoryItem = [[self children] objectAtIndex:i];
381 if( [subCategoryItem subCategory] == subCategory )
382 return subCategoryItem;
394 @implementation VLCTreeSubCategoryItem
395 - (id)initWithSubCategory:(int)subCategory
397 if(!config_CategoryNameGet( subCategory )) return nil;
398 NSString * name = [[VLCMain sharedInstance] localizedString: config_CategoryNameGet( subCategory )];
399 if(self = [super initWithName:name andConfigItem:NULL])
401 _subCategory = subCategory;
402 //_help = [[[VLCMain sharedInstance] localizedString: config_CategoryHelpGet( subCategory )] retain];
407 + (VLCTreeSubCategoryItem *)subCategoryTreeItemWithSubCategory:(int)subCategory
409 return [[[[self class] alloc] initWithSubCategory:subCategory] autorelease];
420 @implementation VLCTreeLeafItem
424 #pragma mark (Root class for all TreeItems)
425 @implementation VLCTreeItem
427 - (id)initWithConfigItem: (module_config_t *) configItem
429 NSString * name = [[VLCMain sharedInstance] localizedString:configItem->psz_name];
430 return [self initWithName:name andConfigItem:configItem];
433 - (id)initWithName:(NSString*)name andConfigItem:(module_config_t *)configItem
438 _name = [name retain];
439 _configItem = configItem;
452 - (VLCTreeItem *)childAtIndex:(int)i_index
454 return [[self children] objectAtIndex:i_index];
457 - (int)numberOfChildren
459 return [[self children] count];
464 return [[_name retain] autorelease];
467 - (void)showView:(NSScrollView *)prefsView
472 [[VLCPrefs sharedInstance] setTitle: [self name]];
473 s_vrc = [[prefsView contentView] bounds]; s_vrc.size.height -= 4;
474 view = [[VLCFlippedView alloc] initWithFrame: s_vrc];
475 [view setAutoresizingMask: NSViewWidthSizable | NSViewMinYMargin | NSViewMaxYMargin];
479 _subviews = [[NSMutableArray alloc] initWithCapacity:10];
482 for( i = 0; i < [[self children] count]; i++)
484 VLCTreeItem * item = [[self children] objectAtIndex:i];
485 if(![item isKindOfClass:[VLCTreeLeafItem class]]) continue;
487 VLCConfigControl *control;
488 control = [VLCConfigControl newControl:[item configItem] withView:view];
491 [control setAutoresizingMask: NSViewMaxYMargin | NSViewWidthSizable];
492 [_subviews addObject: control];
503 NSEnumerator *enumerator = [_subviews objectEnumerator];
504 VLCConfigControl *widget;
507 while( ( widget = [enumerator nextObject] ) )
508 if( i_max_label < [widget getLabelSize] )
509 i_max_label = [widget getLabelSize];
511 enumerator = [_subviews objectEnumerator];
512 while( ( widget = [enumerator nextObject] ) )
516 i_widget = [widget getViewType];
517 i_yPos += [VLCConfigControl calcVerticalMargin:i_widget lastItem:i_lastItem];
518 [widget setYPos:i_yPos];
519 frame = [widget frame];
520 frame.size.width = [view frame].size.width - LEFTMARGIN - RIGHTMARGIN;
521 [widget setFrame:frame];
522 [widget alignWithXPosition: i_max_label];
523 i_yPos += [widget frame].size.height;
524 i_lastItem = i_widget;
525 [view addSubview:widget];
528 frame = [view frame];
529 frame.size.height = i_yPos;
530 [view setFrame:frame];
531 [prefsView setDocumentView:view];
537 for( i = 0 ; i < [_subviews count] ; i++ )
538 [[_subviews objectAtIndex:i] applyChanges];
540 for( i = 0 ; i < [_children count] ; i++ )
541 [[_children objectAtIndex:i] applyChanges];
550 for( i = 0 ; i < [_children count] ; i++ )
551 [[_children objectAtIndex:i] resetView];
554 - (NSMutableArray *)children
556 if(!_children) _children = [[NSMutableArray alloc] init];
560 - (module_config_t *)configItem
567 @implementation VLCFlippedView