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 */
54 #include <vlc_config_cat.h>
58 #include "prefs_widgets.h"
61 /*****************************************************************************
62 * VLCPrefs implementation
63 *****************************************************************************/
64 @implementation VLCPrefs
66 static VLCPrefs *_o_sharedMainInstance = nil;
68 + (VLCPrefs *)sharedInstance
70 return _o_sharedMainInstance ? _o_sharedMainInstance : [[self alloc] init];
75 if( _o_sharedMainInstance ) {
80 _o_sharedMainInstance = [super init];
82 o_empty_view = [[NSView alloc] init];
85 return _o_sharedMainInstance;
90 [o_empty_view release];
97 b_advanced = config_GetInt( p_intf, "advanced" );
100 [o_advanced_ckb setState: b_advanced];
101 [o_prefs_view setBorderType: NSGrooveBorder];
102 [o_prefs_view setHasVerticalScroller: YES];
103 [o_prefs_view setDrawsBackground: NO];
104 [o_prefs_view setDocumentView: o_empty_view];
105 [o_tree selectRow:0 byExtendingSelection:NO];
108 - (void)setTitle: (NSString *) o_title_name
110 [o_title setStringValue: o_title_name];
115 [o_prefs_window center];
116 [o_prefs_window makeKeyAndOrderFront:self];
121 [o_prefs_window setTitle: _NS("Preferences")];
122 [o_save_btn setTitle: _NS("Save")];
123 [o_cancel_btn setTitle: _NS("Cancel")];
124 [o_reset_btn setTitle: _NS("Reset All")];
125 [o_advanced_ckb setTitle: _NS("Advanced")];
128 - (IBAction)savePrefs: (id)sender
130 /* TODO: call savePrefs on Root item */
131 [[VLCTreeItem rootItem] applyChanges];
132 config_SaveConfigFile( p_intf, NULL );
133 [o_prefs_window orderOut:self];
136 - (IBAction)closePrefs: (id)sender
138 [o_prefs_window orderOut:self];
141 - (IBAction)resetAll: (id)sender
143 NSBeginInformationalAlertSheet(_NS("Reset Preferences"), _NS("Cancel"),
144 _NS("Continue"), nil, o_prefs_window, self,
145 @selector(sheetDidEnd: returnCode: contextInfo:), NULL, nil,
146 _NS("Beware this will reset the VLC media player preferences.\n"
147 "Are you sure you want to continue?") );
150 - (void)sheetDidEnd:(NSWindow *)o_sheet returnCode:(int)i_return
151 contextInfo:(void *)o_context
153 if( i_return == NSAlertAlternateReturn )
155 [o_prefs_view setDocumentView: o_empty_view];
156 config_ResetAll( p_intf );
157 [[VLCTreeItem rootItem] resetView];
158 [[o_tree itemAtRow:[o_tree selectedRow]]
159 showView:o_prefs_view advancedView:
160 ( [o_advanced_ckb state] == NSOnState ) ? VLC_TRUE : VLC_FALSE];
164 - (IBAction)advancedToggle: (id)sender
166 b_advanced = !b_advanced;
167 [o_advanced_ckb setState: b_advanced];
168 /* refresh the view of the current treeitem */
169 [[o_tree itemAtRow:[o_tree selectedRow]] showView:o_prefs_view advancedView:
170 ( [o_advanced_ckb state] == NSOnState ) ? VLC_TRUE : VLC_FALSE];
173 - (void)loadConfigTree
177 - (void)outlineViewSelectionIsChanging:(NSNotification *)o_notification
181 /* update the document view to the view of the selected tree item */
182 - (void)outlineViewSelectionDidChange:(NSNotification *)o_notification
184 [[o_tree itemAtRow:[o_tree selectedRow]] showView: o_prefs_view
185 advancedView:( [o_advanced_ckb state] == NSOnState ) ?
186 VLC_TRUE : VLC_FALSE];
191 @implementation VLCPrefs (NSTableDataSource)
193 - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
194 return (item == nil) ? [[VLCTreeItem rootItem] numberOfChildren] :
195 [item numberOfChildren];
198 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
200 return (item == nil) ? YES : ( ([item numberOfChildren] != -1) &&
201 ([item numberOfChildren] != 0));
204 - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item {
205 return (item == nil) ? [[VLCTreeItem rootItem] childAtIndex:index] :
206 (id)[item childAtIndex:index];
209 - (id)outlineView:(NSOutlineView *)outlineView
210 objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
212 return (item == nil) ? @"" : (id)[item getName];
217 @implementation VLCTreeItem
219 static VLCTreeItem *o_root_item = nil;
221 #define IsALeafNode ((id)-1)
223 - (id)initWithName: (NSString *)o_item_name
224 withTitle: (NSString *)o_item_title
225 withHelp: (NSString *)o_item_help
227 parent:(VLCTreeItem *)o_parent_item
228 children:(NSMutableArray *)o_children_array
229 whithCategory: (int) i_category
235 o_name = [o_item_name copy];
236 o_title= [o_item_title copy];
237 o_help= [o_item_help copy];
239 o_parent = o_parent_item;
240 o_children = o_children_array;
241 i_object_category = i_category;
247 + (VLCTreeItem *)rootItem
249 if (o_root_item == nil)
250 o_root_item = [[VLCTreeItem alloc] initWithName:@"main" withTitle:@"main" withHelp:@"" ID:0
251 parent:nil children:[[NSMutableArray alloc] initWithCapacity:10]
258 if (o_children != IsALeafNode) [o_children release];
265 /* Creates and returns the array of children
266 * Loads children incrementally */
267 - (NSArray *)children
269 if( o_children == IsALeafNode )
271 if( [ o_children count] == 0 )
273 intf_thread_t *p_intf = VLCIntf;
275 module_t *p_module = NULL;
277 module_config_t *p_item,
281 /* List the modules */
282 p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
283 if( !p_list ) return nil;
286 p_parser = (module_t *)p_list->p_values[i_index].p_object;
288 if( [[self getName] isEqualToString: @"main"] )
291 * Find the main module
293 for( i_index = 0; i_index < p_list->i_count; i_index++ )
295 p_module = (module_t *)p_list->p_values[i_index].p_object;
296 if( !strcmp( module_GetObjName( p_module ), "main" ) )
299 if( p_module == NULL )
302 "could not load the preferences" );
305 if( i_index < p_list->i_count )
307 /* We found the main module */
308 /* Enumerate config categories and store a reference so we can
309 * generate their config panel them when it is asked by the user. */
310 VLCTreeItem *p_last_category = NULL;
311 unsigned int i_confsize;
312 p_item = module_GetConfig( p_parser, &i_confsize );
313 p_end = p_item + i_confsize;
314 o_children = [[NSMutableArray alloc] initWithCapacity:10];
317 NSString *o_child_name;
318 NSString *o_child_title;
319 NSString *o_child_help;
320 switch( p_item->i_type )
322 case CONFIG_CATEGORY:
323 if( p_item->value.i == -1 ) break;
325 o_child_name = [[VLCMain sharedInstance]
326 localizedString: config_CategoryNameGet( p_item->value.i )];
327 o_child_title = o_child_name;
328 o_child_help = [[VLCMain sharedInstance]
329 localizedString: config_CategoryHelpGet( p_item->value.i )];
330 p_last_category = [VLCTreeItem alloc];
331 [o_children addObject:[p_last_category
332 initWithName: o_child_name
333 withTitle: o_child_title
334 withHelp: o_child_help
337 children:[[NSMutableArray alloc]
339 whithCategory: p_item - module_GetConfig( p_module, &i_confsize )]];
341 case CONFIG_SUBCATEGORY:
342 if( p_item->value.i == -1 ) break;
344 if( p_item->value.i != SUBCAT_PLAYLIST_GENERAL &&
345 p_item->value.i != SUBCAT_VIDEO_GENERAL &&
346 p_item->value.i != SUBCAT_INPUT_GENERAL &&
347 p_item->value.i != SUBCAT_INTERFACE_GENERAL &&
348 p_item->value.i != SUBCAT_SOUT_GENERAL &&
349 p_item->value.i != SUBCAT_ADVANCED_MISC &&
350 p_item->value.i != SUBCAT_AUDIO_GENERAL )
352 o_child_name = [[VLCMain sharedInstance]
353 localizedString: config_CategoryNameGet( p_item->value.i ) ];
354 o_child_title = o_child_name;
355 o_child_help = [[VLCMain sharedInstance]
356 localizedString: config_CategoryHelpGet( p_item->value.i ) ];
358 [p_last_category->o_children
359 addObject:[[VLCTreeItem alloc]
360 initWithName: o_child_name
361 withTitle: o_child_title
362 withHelp: o_child_help
364 parent:p_last_category
365 children:[[NSMutableArray alloc]
367 whithCategory: p_item - module_GetConfig( p_parser, &i_confsize )]];
374 } while( p_item < p_end && p_item++ );
377 /* Build a tree of the plugins */
378 /* Add the capabilities */
379 for( i_index = 0; i_index < p_list->i_count; i_index++ )
381 unsigned int confsize;
382 p_module = (module_t *)p_list->p_values[i_index].p_object;
384 /* Exclude the main module */
385 if( !strcmp( module_GetObjName( p_module ), "main" ) )
388 /* Exclude empty plugins (submodules don't have config */
389 /* options, they are stored in the parent module) */
391 // if( modules_IsSubModule( p_module ) )
393 p_item = module_GetConfig( p_module, &confsize );
395 if( !p_item ) continue;
396 if( !p_item->i_type ) break;
398 int i_subcategory = -1;
402 if( p_item->i_type == CONFIG_CATEGORY )
403 i_category = p_item->value.i;
404 else if( p_item->i_type == CONFIG_SUBCATEGORY )
405 i_subcategory = p_item->value.i;
407 if( p_item->i_type & CONFIG_ITEM )
409 if( i_options > 0 && i_category >= 0 && i_subcategory >= 0 )
411 } while( p_item < p_end && p_item++ );
412 if( !i_options ) continue;
414 /* Find the right category item */
417 vlc_bool_t b_found = VLC_FALSE;
419 VLCTreeItem* p_category_item, * p_subcategory_item;
420 for (i = 0 ; i < [o_children count] ; i++)
422 p_category_item = [o_children objectAtIndex: i];
423 if( p_category_item->i_object_id == i_category )
429 if( !b_found ) continue;
431 /* Find subcategory item */
434 for (i = 0 ; i < [p_category_item->o_children count] ; i++)
436 p_subcategory_item = [p_category_item->o_children
438 if( p_subcategory_item->i_object_id == i_subcategory )
445 p_subcategory_item = p_category_item;
447 [p_subcategory_item->o_children addObject:[[VLCTreeItem alloc]
448 initWithName:[[VLCMain sharedInstance]
449 localizedString: module_GetName( p_module, VLC_FALSE ) ]
450 withTitle:[[VLCMain sharedInstance]
451 localizedString: module_GetLongName( p_module ) ]
453 ID: ((vlc_object_t*)p_module)->i_object_id
454 parent:p_subcategory_item
459 vlc_list_release( p_list );
469 - (NSString *)getName
474 - (NSString *)getTitle
479 - (NSString *)getHelp
484 - (VLCTreeItem *)childAtIndex:(int)i_index
486 return [[self children] objectAtIndex:i_index];
489 - (int)numberOfChildren {
490 id i_tmp = [self children];
491 return (i_tmp == IsALeafNode) ? (-1) : (int)[i_tmp count];
494 - (BOOL)hasPrefs:(NSString *)o_module_name
496 intf_thread_t *p_intf = VLCIntf;
499 char *psz_module_name;
502 psz_module_name = (char *)[o_module_name UTF8String];
504 /* look for module */
505 p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
507 for( i_index = 0; i_index < p_list->i_count; i_index++ )
509 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
511 if( !strcmp( module_GetObjName( p_parser ), psz_module_name ) )
513 unsigned int confsize;
514 module_GetConfig( p_parser, &confsize );
515 BOOL b_has_prefs = confsize != 0;
516 vlc_list_release( p_list );
517 return( b_has_prefs );
521 vlc_list_release( p_list );
526 - (NSView *)showView:(NSScrollView *)o_prefs_view
527 advancedView:(vlc_bool_t) b_advanced
532 [[VLCPrefs sharedInstance] setTitle: [self getTitle]];
533 /* NSLog( [self getHelp] ); */
534 s_vrc = [[o_prefs_view contentView] bounds]; s_vrc.size.height -= 4;
535 o_view = [[VLCFlippedView alloc] initWithFrame: s_vrc];
536 [o_view setAutoresizingMask: NSViewWidthSizable | NSViewMinYMargin |
539 /* Create all subviews if it isn't already done because we cannot use */
540 /* setHiden for MacOS < 10.3*/
541 if( o_subviews == nil )
543 intf_thread_t *p_intf = VLCIntf;
545 module_t *p_parser = NULL;
546 module_config_t *p_item,
548 unsigned int confsize;
550 o_subviews = [[NSMutableArray alloc] initWithCapacity:10];
551 /* Get a pointer to the module */
552 if( i_object_category == -1 )
554 p_parser = (module_t *) vlc_object_get( i_object_id );
555 if( !p_parser || ((vlc_object_t*)p_parser)->i_object_type != VLC_OBJECT_MODULE )
557 /* 0OOoo something went really bad */
560 p_item = module_GetConfig( p_parser, &confsize );
561 p_end = p_item + confsize;
567 msg_Err( p_intf, "invalid preference item found" );
572 switch(p_item->i_type)
574 case CONFIG_SUBCATEGORY:
576 case CONFIG_CATEGORY:
580 case CONFIG_HINT_USAGE:
584 VLCConfigControl *o_control = nil;
585 o_control = [VLCConfigControl newControl:p_item
587 if( o_control != nil )
589 [o_control setAutoresizingMask: NSViewMaxYMargin |
591 [o_subviews addObject: o_control];
596 } while( p_item < p_end && p_item++ );
598 vlc_object_release( (vlc_object_t*)p_parser );
603 p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
604 if( !p_list ) return o_view;
607 * Find the main module
609 for( i_index = 0; i_index < p_list->i_count; i_index++ )
611 p_parser = (module_t *)p_list->p_values[i_index].p_object;
612 if( !strcmp( module_GetObjName( p_parser ), "main" ) )
615 if( p_parser == NULL )
617 msg_Err( p_intf, "could not load preferences" );
620 unsigned int confsize;
621 p_item = module_GetConfig( p_parser, &confsize );
622 p_end = p_item + confsize;
623 p_item += i_object_category;
625 if( ( p_item->i_type == CONFIG_CATEGORY ) &&
626 ( ( p_item->value.i == CAT_PLAYLIST ) ||
627 ( p_item->value.i == CAT_AUDIO ) ||
628 ( p_item->value.i == CAT_VIDEO ) ||
629 ( p_item->value.i == CAT_INTERFACE ) ||
630 ( p_item->value.i == CAT_INPUT ) ||
631 ( p_item->value.i == CAT_SOUT ) ) )
637 if( !p_item || !p_item->i_type )
639 msg_Err( p_intf, "invalid preference item found" );
644 switch( p_item->i_type )
646 case CONFIG_SUBCATEGORY:
648 case CONFIG_CATEGORY:
652 case CONFIG_HINT_USAGE:
656 VLCConfigControl *o_control = nil;
657 o_control = [VLCConfigControl newControl:p_item
659 if( o_control != nil )
661 [o_control setAutoresizingMask: NSViewMaxYMargin |
663 [o_subviews addObject: o_control];
668 } while ( ( p_item < p_end ) &&
669 ( p_item->i_type != CONFIG_SUBCATEGORY ) );
671 vlc_list_release( p_list );
680 int i_show_advanced = 0;
682 NSEnumerator *enumerator = [o_subviews objectEnumerator];
683 VLCConfigControl *o_widget;
686 while( ( o_widget = [enumerator nextObject] ) )
687 if( ( [o_widget isAdvanced] ) && (! b_advanced) )
689 else if( i_max_label < [o_widget getLabelSize] )
690 i_max_label = [o_widget getLabelSize];
692 enumerator = [o_subviews objectEnumerator];
693 while( ( o_widget = [enumerator nextObject] ) )
696 if( ( [o_widget isAdvanced] ) && (! b_advanced) )
702 i_widget = [o_widget getViewType];
703 i_yPos += [VLCConfigControl calcVerticalMargin:i_widget
704 lastItem:i_lastItem];
705 [o_widget setYPos:i_yPos];
706 o_frame = [o_widget frame];
707 o_frame.size.width = [o_view frame].size.width -
708 LEFTMARGIN - RIGHTMARGIN;
709 [o_widget setFrame:o_frame];
710 [o_widget alignWithXPosition: i_max_label];
711 i_yPos += [o_widget frame].size.height;
712 i_lastItem = i_widget;
713 [o_view addSubview:o_widget];
715 if( i_show_advanced != 0 )
717 /* We add the advanced notice... */
718 NSRect s_rc = [o_view frame];
719 NSTextField *o_label;
720 s_rc.size.height = 17;
721 s_rc.origin.x = LEFTMARGIN;
722 s_rc.origin.y = i_yPos += [VLCConfigControl
723 calcVerticalMargin:CONFIG_ITEM_STRING
724 lastItem:i_lastItem];
725 o_label = [[[NSTextField alloc] initWithFrame: s_rc] retain];
726 [o_label setDrawsBackground: NO];
727 [o_label setBordered: NO];
728 [o_label setEditable: NO];
729 [o_label setSelectable: NO];
730 [o_label setStringValue: _NS("Some options are hidden. " \
731 "Check \"Advanced\" to display them.")];
732 [o_label setFont:[NSFont systemFontOfSize:10]];
734 [o_view addSubview:o_label];
735 i_yPos += [o_label frame].size.height;
737 o_frame = [o_view frame];
738 o_frame.size.height = i_yPos;
739 [o_view setFrame:o_frame];
740 [o_prefs_view setDocumentView:o_view];
749 if( o_subviews != nil )
750 //Item has been shown
751 for( i = 0 ; i < [o_subviews count] ; i++ )
752 [[o_subviews objectAtIndex:i] applyChanges];
754 if( o_children != IsALeafNode )
755 for( i = 0 ; i < [o_children count] ; i++ )
756 [[o_children objectAtIndex:i] applyChanges];
762 if( o_subviews != nil )
764 //Item has been shown
765 [o_subviews release];
769 if( o_children != IsALeafNode )
770 for( i = 0 ; i < [o_children count] ; i++ )
771 [[o_children objectAtIndex:i] resetView];
777 @implementation VLCFlippedView