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 /* /!\ Warning: Unreadable code :/ */
64 /*****************************************************************************
65 * VLCPrefs implementation
66 *****************************************************************************/
67 @implementation VLCPrefs
69 static VLCPrefs *_o_sharedMainInstance = nil;
71 + (VLCPrefs *)sharedInstance
73 return _o_sharedMainInstance ? _o_sharedMainInstance : [[self alloc] init];
78 if( _o_sharedMainInstance ) {
83 _o_sharedMainInstance = [super init];
85 o_empty_view = [[NSView alloc] init];
88 return _o_sharedMainInstance;
93 [o_empty_view release];
100 b_advanced = config_GetInt( p_intf, "advanced" );
103 [o_advanced_ckb setState: b_advanced];
104 [o_prefs_view setBorderType: NSGrooveBorder];
105 [o_prefs_view setHasVerticalScroller: YES];
106 [o_prefs_view setDrawsBackground: NO];
107 [o_prefs_view setDocumentView: o_empty_view];
108 [o_tree selectRow:0 byExtendingSelection:NO];
111 - (void)setTitle: (NSString *) o_title_name
113 [o_title setStringValue: o_title_name];
118 [[o_basicFull_matrix cellAtRow:0 column:0] setState: NSOffState];
119 [[o_basicFull_matrix cellAtRow:0 column:1] setState: NSOnState];
121 [o_prefs_window center];
122 [o_prefs_window makeKeyAndOrderFront:self];
127 [o_prefs_window setTitle: _NS("Preferences")];
128 [o_save_btn setTitle: _NS("Save")];
129 [o_cancel_btn setTitle: _NS("Cancel")];
130 [o_reset_btn setTitle: _NS("Reset All")];
131 [o_advanced_ckb setTitle: _NS("Advanced")];
132 [[o_basicFull_matrix cellAtRow: 0 column: 0] setStringValue: _NS("Basic")];
133 [[o_basicFull_matrix cellAtRow: 0 column: 1] setStringValue: _NS("All")];
136 - (IBAction)savePrefs: (id)sender
138 /* TODO: call savePrefs on Root item */
139 [[VLCTreeItem rootItem] applyChanges];
140 config_SaveConfigFile( p_intf, NULL );
141 [o_prefs_window orderOut:self];
144 - (IBAction)closePrefs: (id)sender
146 [o_prefs_window orderOut:self];
149 - (IBAction)resetAll: (id)sender
151 NSBeginInformationalAlertSheet(_NS("Reset Preferences"), _NS("Cancel"),
152 _NS("Continue"), nil, o_prefs_window, self,
153 @selector(sheetDidEnd: returnCode: contextInfo:), NULL, nil,
154 _NS("Beware this will reset the VLC media player preferences.\n"
155 "Are you sure you want to continue?") );
158 - (void)sheetDidEnd:(NSWindow *)o_sheet returnCode:(int)i_return
159 contextInfo:(void *)o_context
161 if( i_return == NSAlertAlternateReturn )
163 [o_prefs_view setDocumentView: o_empty_view];
164 config_ResetAll( p_intf );
165 [[VLCTreeItem rootItem] resetView];
166 [[o_tree itemAtRow:[o_tree selectedRow]]
167 showView:o_prefs_view advancedView:
168 ( [o_advanced_ckb state] == NSOnState ) ? true : false];
172 - (IBAction)advancedToggle: (id)sender
174 b_advanced = !b_advanced;
175 [o_advanced_ckb setState: b_advanced];
176 /* refresh the view of the current treeitem */
177 [[o_tree itemAtRow:[o_tree selectedRow]] showView:o_prefs_view advancedView:
178 ( [o_advanced_ckb state] == NSOnState ) ? true : false];
181 - (IBAction)buttonAction: (id)sender
183 [o_prefs_window orderOut: self];
184 [[o_basicFull_matrix cellAtRow:0 column:0] setState: NSOnState];
185 [[o_basicFull_matrix cellAtRow:0 column:1] setState: NSOffState];
186 [[[VLCMain sharedInstance] getSimplePreferences] showSimplePrefs];
189 - (void)loadConfigTree
193 - (void)outlineViewSelectionIsChanging:(NSNotification *)o_notification
197 /* update the document view to the view of the selected tree item */
198 - (void)outlineViewSelectionDidChange:(NSNotification *)o_notification
200 [[o_tree itemAtRow:[o_tree selectedRow]] showView: o_prefs_view
201 advancedView:( [o_advanced_ckb state] == NSOnState ) ?
207 @implementation VLCPrefs (NSTableDataSource)
209 - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
210 return (item == nil) ? [[VLCTreeItem rootItem] numberOfChildren] :
211 [item numberOfChildren];
214 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
216 return (item == nil) ? YES : ( ([item numberOfChildren] != -1) &&
217 ([item numberOfChildren] != 0));
220 - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item {
221 return (item == nil) ? [[VLCTreeItem rootItem] childAtIndex:index] :
222 (id)[item childAtIndex:index];
225 - (id)outlineView:(NSOutlineView *)outlineView
226 objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
228 return (item == nil) ? @"" : (id)[item getName];
233 @implementation VLCTreeItem
235 static VLCTreeItem *o_root_item = nil;
237 #define IsALeafNode ((id)-1)
239 - (id)initWithName: (NSString *)o_item_name
240 withTitle: (NSString *)o_item_title
241 withHelp: (NSString *)o_item_help
243 parent:(VLCTreeItem *)o_parent_item
244 children:(NSMutableArray *)o_children_array
245 whithCategory: (int) i_category
251 o_name = [o_item_name copy];
252 o_title= [o_item_title copy];
253 o_help= [o_item_help copy];
255 o_parent = o_parent_item;
256 o_children = o_children_array;
257 i_object_category = i_category;
263 + (VLCTreeItem *)rootItem
265 if (o_root_item == nil)
266 o_root_item = [[VLCTreeItem alloc] initWithName:@"main" withTitle:@"main" withHelp:@"" ID:0
267 parent:nil children:[[NSMutableArray alloc] initWithCapacity:10]
274 if (o_children != IsALeafNode) [o_children release];
281 /* Creates and returns the array of children
282 * Loads children incrementally */
283 - (NSArray *)children
285 if( o_children == IsALeafNode )
287 if( [ o_children count] == 0 )
289 intf_thread_t *p_intf = VLCIntf;
291 module_t *p_module = NULL;
292 module_t *p_main_module;
293 module_config_t *p_items;
295 if( [[self getName] isEqualToString: @"main"] )
297 p_main_module = module_GetMainModule( p_intf );
298 assert( p_main_module );
300 /* We found the main module */
301 /* Enumerate config categories and store a reference so we can
302 * generate their config panel them when it is asked by the user. */
303 VLCTreeItem *p_last_category = NULL;
304 unsigned int i_confsize;
305 p_items = module_GetConfig( p_main_module, &i_confsize );
306 o_children = [[NSMutableArray alloc] initWithCapacity:10];
307 for( int i = 0; i < i_confsize; i++ )
309 NSString *o_child_name;
310 NSString *o_child_title;
311 NSString *o_child_help;
312 switch( p_items[i].i_type )
314 case CONFIG_CATEGORY:
315 if( p_items[i].value.i == -1 ) break;
317 o_child_name = [[VLCMain sharedInstance]
318 localizedString: config_CategoryNameGet( p_items[i].value.i )];
319 o_child_title = o_child_name;
320 o_child_help = [[VLCMain sharedInstance]
321 localizedString: config_CategoryHelpGet( p_items[i].value.i )];
322 p_last_category = [VLCTreeItem alloc];
323 [o_children addObject:[p_last_category
324 initWithName: o_child_name
325 withTitle: o_child_title
326 withHelp: o_child_help
327 ID: ((vlc_object_t*)p_main_module)->i_object_id
329 children:[[NSMutableArray alloc]
331 whithCategory: p_items[i].value.i]];
333 case CONFIG_SUBCATEGORY:
334 if( p_items[i].value.i == -1 ) break;
336 if( p_items[i].value.i != SUBCAT_PLAYLIST_GENERAL &&
337 p_items[i].value.i != SUBCAT_VIDEO_GENERAL &&
338 p_items[i].value.i != SUBCAT_INPUT_GENERAL &&
339 p_items[i].value.i != SUBCAT_INTERFACE_GENERAL &&
340 p_items[i].value.i != SUBCAT_SOUT_GENERAL &&
341 p_items[i].value.i != SUBCAT_ADVANCED_MISC &&
342 p_items[i].value.i != SUBCAT_AUDIO_GENERAL )
344 o_child_name = [[VLCMain sharedInstance]
345 localizedString: config_CategoryNameGet( p_items[i].value.i ) ];
346 o_child_title = o_child_name;
347 o_child_help = [[VLCMain sharedInstance]
348 localizedString: config_CategoryHelpGet( p_items[i].value.i ) ];
350 [p_last_category->o_children
351 addObject:[[VLCTreeItem alloc]
352 initWithName: o_child_name
353 withTitle: o_child_title
354 withHelp: o_child_help
355 ID: ((vlc_object_t*)p_main_module)->i_object_id
356 parent:p_last_category
357 children:[[NSMutableArray alloc]
359 whithCategory: p_items[i].value.i]];
368 vlc_object_release( (vlc_object_t *)p_main_module );
370 /* List the modules */
371 p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
372 if( !p_list ) return nil;
374 /* Build a tree of the plugins */
375 /* Add the capabilities */
376 for( i = 0; i < p_list->i_count; i++ )
378 unsigned int confsize;
379 p_module = (module_t *)p_list->p_values[i].p_object;
381 /* Exclude the main module */
382 if( module_IsMainModule( p_module ) )
385 /* Exclude empty plugins (submodules don't have config */
386 /* options, they are stored in the parent module) */
387 p_items = module_GetConfig( p_module, &confsize );
392 int i_subcategory = -1;
395 for( j = 0; j < confsize; j++ )
397 if( p_items[j].i_type == CONFIG_CATEGORY )
398 i_category = p_items[j].value.i;
399 else if( p_items[j].i_type == CONFIG_SUBCATEGORY )
400 i_subcategory = p_items[j].value.i;
402 if( p_items[j].i_type & CONFIG_ITEM )
405 if( b_item && i_category >= 0 && i_subcategory >= 0 )
409 if( !b_item ) continue;
411 /* Find the right category item */
414 bool b_found = false;
416 VLCTreeItem* p_category_item, * p_subcategory_item;
417 for (j = 0 ; j < [o_children count] ; j++)
419 p_category_item = [o_children objectAtIndex: j];
420 if( p_category_item->i_object_category == i_category )
426 if( !b_found ) continue;
428 /* Find subcategory item */
431 for (j = 0 ; j < [p_category_item->o_children count] ; j++)
433 p_subcategory_item = [p_category_item->o_children
435 if( p_subcategory_item->i_object_category == i_subcategory )
442 p_subcategory_item = p_category_item;
444 [p_subcategory_item->o_children addObject:[[VLCTreeItem alloc]
445 initWithName:[[VLCMain sharedInstance]
446 localizedString: module_GetName( p_module, false ) ]
447 withTitle:[[VLCMain sharedInstance]
448 localizedString: module_GetLongName( p_module ) ]
450 ID: ((vlc_object_t*)p_module)->i_object_id
451 parent:p_subcategory_item
455 vlc_list_release( p_list );
466 - (NSString *)getName
471 - (NSString *)getTitle
476 - (NSString *)getHelp
481 - (VLCTreeItem *)childAtIndex:(int)i_index
483 return [[self children] objectAtIndex:i_index];
486 - (int)numberOfChildren {
487 id i_tmp = [self children];
488 return (i_tmp == IsALeafNode) ? (-1) : (int)[i_tmp count];
491 - (BOOL)hasPrefs:(NSString *)o_module_name
493 intf_thread_t *p_intf = VLCIntf;
496 char *psz_module_name;
499 psz_module_name = (char *)[o_module_name UTF8String];
501 /* look for module */
502 p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
504 for( i_index = 0; i_index < p_list->i_count; i_index++ )
506 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
508 if( !strcmp( module_GetObjName( p_parser ), psz_module_name ) )
510 unsigned int confsize;
511 module_GetConfig( p_parser, &confsize );
512 BOOL b_has_prefs = confsize != 0;
513 vlc_list_release( p_list );
514 return( b_has_prefs );
518 vlc_list_release( p_list );
523 - (NSView *)showView:(NSScrollView *)o_prefs_view
524 advancedView:(bool) b_advanced
529 [[VLCPrefs sharedInstance] setTitle: [self getTitle]];
530 /* NSLog( [self getHelp] ); */
531 s_vrc = [[o_prefs_view contentView] bounds]; s_vrc.size.height -= 4;
532 o_view = [[VLCFlippedView alloc] initWithFrame: s_vrc];
533 [o_view setAutoresizingMask: NSViewWidthSizable | NSViewMinYMargin |
536 /* Create all subviews if it isn't already done because we cannot use */
537 /* setHiden for MacOS < 10.3*/
538 if( o_subviews == nil )
540 intf_thread_t *p_intf = VLCIntf;
542 module_t *p_module = NULL;
543 module_t *p_main_module;
544 module_config_t *p_items;
545 unsigned int confsize;
547 o_subviews = [[NSMutableArray alloc] initWithCapacity:10];
548 /* Get a pointer to the module */
549 if( i_object_category == -1 )
551 p_module = (module_t *) vlc_object_get( i_object_id );
554 p_items = module_GetConfig( p_module, &confsize );
556 for( unsigned int i = 0; i < confsize; i++ )
558 switch( p_items[i].i_type )
560 case CONFIG_SUBCATEGORY:
561 case CONFIG_CATEGORY:
563 case CONFIG_HINT_USAGE:
567 VLCConfigControl *o_control = nil;
568 o_control = [VLCConfigControl newControl:&p_items[i]
572 [o_control setAutoresizingMask: NSViewMaxYMargin |
574 [o_subviews addObject: o_control];
580 vlc_object_release( (vlc_object_t*)p_module );
584 p_main_module = module_GetMainModule( p_intf );
585 assert( p_main_module );
586 module_config_t *p_items;
588 unsigned int i, confsize;
589 p_items = module_GetConfig( p_main_module, &confsize );
591 /* We need to first, find the right (sub)category,
592 * and then abort when we find a new (sub)category. Part of the Ugliness. */
593 bool in_right_category = false;
594 bool in_subcategory = false;
596 for( i = 0; i < confsize; i++ )
598 if( !p_items[i].i_type )
600 msg_Err( p_intf, "invalid preference item found" );
604 switch( p_items[i].i_type )
606 case CONFIG_CATEGORY:
607 if(!in_right_category && p_items[i].value.i == i_object_category)
608 in_right_category = true;
609 else if(in_right_category)
612 case CONFIG_SUBCATEGORY:
613 if(!in_right_category && p_items[i].value.i == i_object_category)
615 in_right_category = true;
616 in_subcategory = true;
618 else if(in_right_category && in_subcategory)
622 case CONFIG_HINT_USAGE:
626 if(!in_right_category) break;
628 VLCConfigControl *o_control = nil;
629 o_control = [VLCConfigControl newControl:&p_items[i]
631 if( o_control != nil )
633 [o_control setAutoresizingMask: NSViewMaxYMargin |
635 [o_subviews addObject: o_control];
642 vlc_object_release( (vlc_object_t*)p_main_module );
651 int i_show_advanced = 0;
653 NSEnumerator *enumerator = [o_subviews objectEnumerator];
654 VLCConfigControl *o_widget;
657 while( ( o_widget = [enumerator nextObject] ) )
658 if( ( [o_widget isAdvanced] ) && (! b_advanced) )
660 else if( i_max_label < [o_widget getLabelSize] )
661 i_max_label = [o_widget getLabelSize];
663 enumerator = [o_subviews objectEnumerator];
664 while( ( o_widget = [enumerator nextObject] ) )
667 if( ( [o_widget isAdvanced] ) && (! b_advanced) )
673 i_widget = [o_widget getViewType];
674 i_yPos += [VLCConfigControl calcVerticalMargin:i_widget
675 lastItem:i_lastItem];
676 [o_widget setYPos:i_yPos];
677 o_frame = [o_widget frame];
678 o_frame.size.width = [o_view frame].size.width -
679 LEFTMARGIN - RIGHTMARGIN;
680 [o_widget setFrame:o_frame];
681 [o_widget alignWithXPosition: i_max_label];
682 i_yPos += [o_widget frame].size.height;
683 i_lastItem = i_widget;
684 [o_view addSubview:o_widget];
686 if( i_show_advanced != 0 )
688 /* We add the advanced notice... */
689 NSRect s_rc = [o_view frame];
690 NSTextField *o_label;
691 s_rc.size.height = 17;
692 s_rc.origin.x = LEFTMARGIN;
693 s_rc.origin.y = i_yPos += [VLCConfigControl
694 calcVerticalMargin:CONFIG_ITEM_STRING
695 lastItem:i_lastItem];
696 o_label = [[[NSTextField alloc] initWithFrame: s_rc] retain];
697 [o_label setDrawsBackground: NO];
698 [o_label setBordered: NO];
699 [o_label setEditable: NO];
700 [o_label setSelectable: NO];
701 [o_label setStringValue: _NS("Some options are hidden. " \
702 "Check \"Advanced\" to display them.")];
703 [o_label setFont:[NSFont systemFontOfSize:10]];
705 [o_view addSubview:o_label];
706 i_yPos += [o_label frame].size.height;
708 o_frame = [o_view frame];
709 o_frame.size.height = i_yPos;
710 [o_view setFrame:o_frame];
711 [o_prefs_view setDocumentView:o_view];
720 if( o_subviews != nil )
721 //Item has been shown
722 for( i = 0 ; i < [o_subviews count] ; i++ )
723 [[o_subviews objectAtIndex:i] applyChanges];
725 if( o_children != IsALeafNode )
726 for( i = 0 ; i < [o_children count] ; i++ )
727 [[o_children objectAtIndex:i] applyChanges];
733 if( o_subviews != nil )
735 //Item has been shown
736 [o_subviews release];
740 if( o_children != IsALeafNode )
741 for( i = 0 ; i < [o_children count] ; i++ )
742 [[o_children objectAtIndex:i] resetView];
748 @implementation VLCFlippedView