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 - an advanced action (to hide/show advanced options)
34 - a redraw view action
35 - the children action should generate a list of the treeitems children (to be used by VLCPrefs datasource)
37 The class is sort of a mix of wxwindows intfs, PrefsTreeCtrl and ConfigTreeData
39 /* VLCConfigControl are subclassed NSView's containing and managing individual config items
40 the classes are VERY closely related to wxwindows ConfigControls */
42 /*****************************************************************************
44 *****************************************************************************/
45 #include <stdlib.h> /* malloc(), free() */
46 #include <sys/param.h> /* for MAXPATHLEN */
53 #include <vlc_common.h>
54 #include <vlc_config_cat.h>
58 #import "simple_prefs.h"
59 #import "prefs_widgets.h"
62 /*****************************************************************************
63 * VLCPrefs implementation
64 *****************************************************************************/
65 @implementation VLCPrefs
67 static VLCPrefs *_o_sharedMainInstance = nil;
69 + (VLCPrefs *)sharedInstance
71 return _o_sharedMainInstance ? _o_sharedMainInstance : [[self alloc] init];
76 if( _o_sharedMainInstance ) {
81 _o_sharedMainInstance = [super init];
83 o_empty_view = [[NSView alloc] init];
86 return _o_sharedMainInstance;
91 [o_empty_view release];
98 b_advanced = config_GetInt( p_intf, "advanced" );
101 [o_advanced_ckb setState: b_advanced];
102 [o_prefs_view setBorderType: NSGrooveBorder];
103 [o_prefs_view setHasVerticalScroller: YES];
104 [o_prefs_view setDrawsBackground: NO];
105 [o_prefs_view setDocumentView: o_empty_view];
106 [o_tree selectRow:0 byExtendingSelection:NO];
109 - (void)setTitle: (NSString *) o_title_name
111 [o_title setStringValue: o_title_name];
116 [[o_basicFull_matrix cellAtRow:0 column:0] setState: NSOffState];
117 [[o_basicFull_matrix cellAtRow:0 column:1] setState: NSOnState];
119 [o_prefs_window center];
120 [o_prefs_window makeKeyAndOrderFront:self];
125 [o_prefs_window setTitle: _NS("Preferences")];
126 [o_save_btn setTitle: _NS("Save")];
127 [o_cancel_btn setTitle: _NS("Cancel")];
128 [o_reset_btn setTitle: _NS("Reset All")];
129 [o_advanced_ckb setTitle: _NS("Advanced")];
130 [[o_basicFull_matrix cellAtRow: 0 column: 0] setStringValue: _NS("Basic")];
131 [[o_basicFull_matrix cellAtRow: 0 column: 1] setStringValue: _NS("All")];
134 - (IBAction)savePrefs: (id)sender
136 /* TODO: call savePrefs on Root item */
137 [[VLCTreeItem rootItem] applyChanges];
138 config_SaveConfigFile( p_intf, NULL );
139 [o_prefs_window orderOut:self];
142 - (IBAction)closePrefs: (id)sender
144 [o_prefs_window orderOut:self];
147 - (IBAction)resetAll: (id)sender
149 NSBeginInformationalAlertSheet(_NS("Reset Preferences"), _NS("Cancel"),
150 _NS("Continue"), nil, o_prefs_window, self,
151 @selector(sheetDidEnd: returnCode: contextInfo:), NULL, nil,
152 _NS("Beware this will reset the VLC media player preferences.\n"
153 "Are you sure you want to continue?") );
156 - (void)sheetDidEnd:(NSWindow *)o_sheet returnCode:(int)i_return
157 contextInfo:(void *)o_context
159 if( i_return == NSAlertAlternateReturn )
161 [o_prefs_view setDocumentView: o_empty_view];
162 config_ResetAll( p_intf );
163 [[VLCTreeItem rootItem] resetView];
164 [[o_tree itemAtRow:[o_tree selectedRow]]
165 showView:o_prefs_view advancedView:
166 ( [o_advanced_ckb state] == NSOnState ) ? true : false];
170 - (IBAction)advancedToggle: (id)sender
172 b_advanced = !b_advanced;
173 [o_advanced_ckb setState: b_advanced];
174 /* refresh the view of the current treeitem */
175 [[o_tree itemAtRow:[o_tree selectedRow]] showView:o_prefs_view advancedView:
176 ( [o_advanced_ckb state] == NSOnState ) ? true : false];
179 - (IBAction)buttonAction: (id)sender
181 [o_prefs_window orderOut: self];
182 [[o_basicFull_matrix cellAtRow:0 column:0] setState: NSOnState];
183 [[o_basicFull_matrix cellAtRow:0 column:1] setState: NSOffState];
184 [[[VLCMain sharedInstance] getSimplePreferences] showSimplePrefs];
187 - (void)loadConfigTree
191 - (void)outlineViewSelectionIsChanging:(NSNotification *)o_notification
195 /* update the document view to the view of the selected tree item */
196 - (void)outlineViewSelectionDidChange:(NSNotification *)o_notification
198 [[o_tree itemAtRow:[o_tree selectedRow]] showView: o_prefs_view
199 advancedView:( [o_advanced_ckb state] == NSOnState ) ?
205 @implementation VLCPrefs (NSTableDataSource)
207 - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
208 return (item == nil) ? [[VLCTreeItem rootItem] numberOfChildren] :
209 [item numberOfChildren];
212 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
214 return (item == nil) ? YES : ( ([item numberOfChildren] != -1) &&
215 ([item numberOfChildren] != 0));
218 - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item {
219 return (item == nil) ? [[VLCTreeItem rootItem] childAtIndex:index] :
220 (id)[item childAtIndex:index];
223 - (id)outlineView:(NSOutlineView *)outlineView
224 objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
226 return (item == nil) ? @"" : (id)[item getName];
231 @implementation VLCTreeItem
233 static VLCTreeItem *o_root_item = nil;
235 #define IsALeafNode ((id)-1)
237 - (id)initWithName: (NSString *)o_item_name
238 withTitle: (NSString *)o_item_title
239 withHelp: (NSString *)o_item_help
241 parent:(VLCTreeItem *)o_parent_item
242 children:(NSMutableArray *)o_children_array
243 whithCategory: (int) i_category
249 o_name = [o_item_name copy];
250 o_title= [o_item_title copy];
251 o_help= [o_item_help copy];
253 o_parent = o_parent_item;
254 o_children = o_children_array;
255 i_object_category = i_category;
261 + (VLCTreeItem *)rootItem
263 if (o_root_item == nil)
264 o_root_item = [[VLCTreeItem alloc] initWithName:@"main" withTitle:@"main" withHelp:@"" ID:0
265 parent:nil children:[[NSMutableArray alloc] initWithCapacity:10]
272 if (o_children != IsALeafNode) [o_children release];
279 /* Creates and returns the array of children
280 * Loads children incrementally */
281 - (NSArray *)children
283 if( o_children == IsALeafNode )
285 if( [ o_children count] == 0 )
287 intf_thread_t *p_intf = VLCIntf;
289 module_t *p_module = NULL;
291 module_config_t *p_item,
295 /* List the modules */
296 p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
297 if( !p_list ) return nil;
300 p_parser = (module_t *)p_list->p_values[i_index].p_object;
302 if( [[self getName] isEqualToString: @"main"] )
305 * Find the main module
307 for( i_index = 0; i_index < p_list->i_count; i_index++ )
309 p_module = (module_t *)p_list->p_values[i_index].p_object;
310 if( !strcmp( module_GetObjName( p_module ), "main" ) )
313 if( p_module == NULL )
316 "could not load the preferences" );
319 if( i_index < p_list->i_count )
321 /* We found the main module */
322 /* Enumerate config categories and store a reference so we can
323 * generate their config panel them when it is asked by the user. */
324 VLCTreeItem *p_last_category = NULL;
325 unsigned int i_confsize;
326 p_item = module_GetConfig( p_parser, &i_confsize );
327 p_end = p_item + i_confsize;
328 o_children = [[NSMutableArray alloc] initWithCapacity:10];
331 NSString *o_child_name;
332 NSString *o_child_title;
333 NSString *o_child_help;
334 switch( p_item->i_type )
336 case CONFIG_CATEGORY:
337 if( p_item->value.i == -1 ) break;
339 o_child_name = [[VLCMain sharedInstance]
340 localizedString: config_CategoryNameGet( p_item->value.i )];
341 o_child_title = o_child_name;
342 o_child_help = [[VLCMain sharedInstance]
343 localizedString: config_CategoryHelpGet( p_item->value.i )];
344 p_last_category = [VLCTreeItem alloc];
345 [o_children addObject:[p_last_category
346 initWithName: o_child_name
347 withTitle: o_child_title
348 withHelp: o_child_help
351 children:[[NSMutableArray alloc]
353 whithCategory: p_item - module_GetConfig( p_module, &i_confsize )]];
355 case CONFIG_SUBCATEGORY:
356 if( p_item->value.i == -1 ) break;
358 if( p_item->value.i != SUBCAT_PLAYLIST_GENERAL &&
359 p_item->value.i != SUBCAT_VIDEO_GENERAL &&
360 p_item->value.i != SUBCAT_INPUT_GENERAL &&
361 p_item->value.i != SUBCAT_INTERFACE_GENERAL &&
362 p_item->value.i != SUBCAT_SOUT_GENERAL &&
363 p_item->value.i != SUBCAT_ADVANCED_MISC &&
364 p_item->value.i != SUBCAT_AUDIO_GENERAL )
366 o_child_name = [[VLCMain sharedInstance]
367 localizedString: config_CategoryNameGet( p_item->value.i ) ];
368 o_child_title = o_child_name;
369 o_child_help = [[VLCMain sharedInstance]
370 localizedString: config_CategoryHelpGet( p_item->value.i ) ];
372 [p_last_category->o_children
373 addObject:[[VLCTreeItem alloc]
374 initWithName: o_child_name
375 withTitle: o_child_title
376 withHelp: o_child_help
378 parent:p_last_category
379 children:[[NSMutableArray alloc]
381 whithCategory: p_item - module_GetConfig( p_parser, &i_confsize )]];
388 } while( p_item < p_end && p_item++ );
391 /* Build a tree of the plugins */
392 /* Add the capabilities */
393 for( i_index = 0; i_index < p_list->i_count; i_index++ )
395 unsigned int confsize;
396 p_module = (module_t *)p_list->p_values[i_index].p_object;
398 /* Exclude the main module */
399 if( !strcmp( module_GetObjName( p_module ), "main" ) )
402 /* Exclude empty plugins (submodules don't have config */
403 /* options, they are stored in the parent module) */
405 // if( modules_IsSubModule( p_module ) )
407 p_item = module_GetConfig( p_module, &confsize );
409 if( !p_item ) continue;
410 if( !p_item->i_type ) break;
412 int i_subcategory = -1;
416 if( p_item->i_type == CONFIG_CATEGORY )
417 i_category = p_item->value.i;
418 else if( p_item->i_type == CONFIG_SUBCATEGORY )
419 i_subcategory = p_item->value.i;
421 if( p_item->i_type & CONFIG_ITEM )
423 if( i_options > 0 && i_category >= 0 && i_subcategory >= 0 )
425 } while( p_item < p_end && p_item++ );
426 if( !i_options ) continue;
428 /* Find the right category item */
431 bool b_found = false;
433 VLCTreeItem* p_category_item, * p_subcategory_item;
434 for (i = 0 ; i < [o_children count] ; i++)
436 p_category_item = [o_children objectAtIndex: i];
437 if( p_category_item->i_object_id == i_category )
443 if( !b_found ) continue;
445 /* Find subcategory item */
448 for (i = 0 ; i < [p_category_item->o_children count] ; i++)
450 p_subcategory_item = [p_category_item->o_children
452 if( p_subcategory_item->i_object_id == i_subcategory )
459 p_subcategory_item = p_category_item;
461 [p_subcategory_item->o_children addObject:[[VLCTreeItem alloc]
462 initWithName:[[VLCMain sharedInstance]
463 localizedString: module_GetName( p_module, false ) ]
464 withTitle:[[VLCMain sharedInstance]
465 localizedString: module_GetLongName( p_module ) ]
467 ID: ((vlc_object_t*)p_module)->i_object_id
468 parent:p_subcategory_item
473 vlc_list_release( p_list );
483 - (NSString *)getName
488 - (NSString *)getTitle
493 - (NSString *)getHelp
498 - (VLCTreeItem *)childAtIndex:(int)i_index
500 return [[self children] objectAtIndex:i_index];
503 - (int)numberOfChildren {
504 id i_tmp = [self children];
505 return (i_tmp == IsALeafNode) ? (-1) : (int)[i_tmp count];
508 - (BOOL)hasPrefs:(NSString *)o_module_name
510 intf_thread_t *p_intf = VLCIntf;
513 char *psz_module_name;
516 psz_module_name = (char *)[o_module_name UTF8String];
518 /* look for module */
519 p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
521 for( i_index = 0; i_index < p_list->i_count; i_index++ )
523 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
525 if( !strcmp( module_GetObjName( p_parser ), psz_module_name ) )
527 unsigned int confsize;
528 module_GetConfig( p_parser, &confsize );
529 BOOL b_has_prefs = confsize != 0;
530 vlc_list_release( p_list );
531 return( b_has_prefs );
535 vlc_list_release( p_list );
540 - (NSView *)showView:(NSScrollView *)o_prefs_view
541 advancedView:(bool) b_advanced
546 [[VLCPrefs sharedInstance] setTitle: [self getTitle]];
547 /* NSLog( [self getHelp] ); */
548 s_vrc = [[o_prefs_view contentView] bounds]; s_vrc.size.height -= 4;
549 o_view = [[VLCFlippedView alloc] initWithFrame: s_vrc];
550 [o_view setAutoresizingMask: NSViewWidthSizable | NSViewMinYMargin |
553 /* Create all subviews if it isn't already done because we cannot use */
554 /* setHiden for MacOS < 10.3*/
555 if( o_subviews == nil )
557 intf_thread_t *p_intf = VLCIntf;
559 module_t *p_parser = NULL;
560 module_config_t *p_item,
562 unsigned int confsize;
564 o_subviews = [[NSMutableArray alloc] initWithCapacity:10];
565 /* Get a pointer to the module */
566 if( i_object_category == -1 )
568 p_parser = (module_t *) vlc_object_get( i_object_id );
571 /* 0OOoo something went really bad */
574 p_item = module_GetConfig( p_parser, &confsize );
575 p_end = p_item + confsize;
581 msg_Err( p_intf, "invalid preference item found" );
586 switch(p_item->i_type)
588 case CONFIG_SUBCATEGORY:
590 case CONFIG_CATEGORY:
594 case CONFIG_HINT_USAGE:
598 VLCConfigControl *o_control = nil;
599 o_control = [VLCConfigControl newControl:p_item
601 if( o_control != nil )
603 [o_control setAutoresizingMask: NSViewMaxYMargin |
605 [o_subviews addObject: o_control];
610 } while( p_item < p_end && p_item++ );
612 vlc_object_release( (vlc_object_t*)p_parser );
617 p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
618 if( !p_list ) return o_view;
621 * Find the main module
623 for( i_index = 0; i_index < p_list->i_count; i_index++ )
625 p_parser = (module_t *)p_list->p_values[i_index].p_object;
626 if( !strcmp( module_GetObjName( p_parser ), "main" ) )
629 if( p_parser == NULL )
631 msg_Err( p_intf, "could not load preferences" );
634 unsigned int confsize;
635 p_item = module_GetConfig( p_parser, &confsize );
636 p_end = p_item + confsize;
637 p_item += i_object_category;
639 if( ( p_item->i_type == CONFIG_CATEGORY ) &&
640 ( ( p_item->value.i == CAT_PLAYLIST ) ||
641 ( p_item->value.i == CAT_AUDIO ) ||
642 ( p_item->value.i == CAT_VIDEO ) ||
643 ( p_item->value.i == CAT_INTERFACE ) ||
644 ( p_item->value.i == CAT_INPUT ) ||
645 ( p_item->value.i == CAT_SOUT ) ) )
651 if( !p_item || !p_item->i_type )
653 msg_Err( p_intf, "invalid preference item found" );
658 switch( p_item->i_type )
660 case CONFIG_SUBCATEGORY:
662 case CONFIG_CATEGORY:
666 case CONFIG_HINT_USAGE:
670 VLCConfigControl *o_control = nil;
671 o_control = [VLCConfigControl newControl:p_item
673 if( o_control != nil )
675 [o_control setAutoresizingMask: NSViewMaxYMargin |
677 [o_subviews addObject: o_control];
682 } while ( ( p_item < p_end ) &&
683 ( p_item->i_type != CONFIG_SUBCATEGORY ) );
685 vlc_list_release( p_list );
694 int i_show_advanced = 0;
696 NSEnumerator *enumerator = [o_subviews objectEnumerator];
697 VLCConfigControl *o_widget;
700 while( ( o_widget = [enumerator nextObject] ) )
701 if( ( [o_widget isAdvanced] ) && (! b_advanced) )
703 else if( i_max_label < [o_widget getLabelSize] )
704 i_max_label = [o_widget getLabelSize];
706 enumerator = [o_subviews objectEnumerator];
707 while( ( o_widget = [enumerator nextObject] ) )
710 if( ( [o_widget isAdvanced] ) && (! b_advanced) )
716 i_widget = [o_widget getViewType];
717 i_yPos += [VLCConfigControl calcVerticalMargin:i_widget
718 lastItem:i_lastItem];
719 [o_widget setYPos:i_yPos];
720 o_frame = [o_widget frame];
721 o_frame.size.width = [o_view frame].size.width -
722 LEFTMARGIN - RIGHTMARGIN;
723 [o_widget setFrame:o_frame];
724 [o_widget alignWithXPosition: i_max_label];
725 i_yPos += [o_widget frame].size.height;
726 i_lastItem = i_widget;
727 [o_view addSubview:o_widget];
729 if( i_show_advanced != 0 )
731 /* We add the advanced notice... */
732 NSRect s_rc = [o_view frame];
733 NSTextField *o_label;
734 s_rc.size.height = 17;
735 s_rc.origin.x = LEFTMARGIN;
736 s_rc.origin.y = i_yPos += [VLCConfigControl
737 calcVerticalMargin:CONFIG_ITEM_STRING
738 lastItem:i_lastItem];
739 o_label = [[[NSTextField alloc] initWithFrame: s_rc] retain];
740 [o_label setDrawsBackground: NO];
741 [o_label setBordered: NO];
742 [o_label setEditable: NO];
743 [o_label setSelectable: NO];
744 [o_label setStringValue: _NS("Some options are hidden. " \
745 "Check \"Advanced\" to display them.")];
746 [o_label setFont:[NSFont systemFontOfSize:10]];
748 [o_view addSubview:o_label];
749 i_yPos += [o_label frame].size.height;
751 o_frame = [o_view frame];
752 o_frame.size.height = i_yPos;
753 [o_view setFrame:o_frame];
754 [o_prefs_view setDocumentView:o_view];
763 if( o_subviews != nil )
764 //Item has been shown
765 for( i = 0 ; i < [o_subviews count] ; i++ )
766 [[o_subviews objectAtIndex:i] applyChanges];
768 if( o_children != IsALeafNode )
769 for( i = 0 ; i < [o_children count] ; i++ )
770 [[o_children objectAtIndex:i] applyChanges];
776 if( o_subviews != nil )
778 //Item has been shown
779 [o_subviews release];
783 if( o_children != IsALeafNode )
784 for( i = 0 ; i < [o_children count] ; i++ )
785 [[o_children objectAtIndex:i] resetView];
791 @implementation VLCFlippedView