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 */
50 #include <vlc_config_cat.h>
54 #include "prefs_widgets.h"
57 /*****************************************************************************
58 * VLCPrefs implementation
59 *****************************************************************************/
60 @implementation VLCPrefs
62 static VLCPrefs *_o_sharedMainInstance = nil;
64 + (VLCPrefs *)sharedInstance
66 return _o_sharedMainInstance ? _o_sharedMainInstance : [[self alloc] init];
71 if( _o_sharedMainInstance ) {
76 _o_sharedMainInstance = [super init];
78 o_empty_view = [[NSView alloc] init];
81 return _o_sharedMainInstance;
86 [o_empty_view release];
93 b_advanced = config_GetInt( p_intf, "advanced" );
96 [o_advanced_ckb setState: b_advanced];
97 [o_prefs_view setBorderType: NSGrooveBorder];
98 [o_prefs_view setHasVerticalScroller: YES];
99 [o_prefs_view setDrawsBackground: NO];
100 [o_prefs_view setDocumentView: o_empty_view];
101 [o_tree selectRow:0 byExtendingSelection:NO];
104 - (void)setTitle: (NSString *) o_title_name
106 [o_title setStringValue: o_title_name];
111 /* load our nib (if not already loaded) */
112 [NSBundle loadNibNamed:@"Preferences" owner:self];
114 [o_prefs_window center];
115 [o_prefs_window makeKeyAndOrderFront:self];
120 [o_prefs_window setTitle: _NS("Preferences")];
121 [o_save_btn setTitle: _NS("Save")];
122 [o_cancel_btn setTitle: _NS("Cancel")];
123 [o_reset_btn setTitle: _NS("Reset All")];
124 [o_advanced_ckb setTitle: _NS("Advanced")];
127 - (IBAction)savePrefs: (id)sender
129 /* TODO: call savePrefs on Root item */
130 [[VLCTreeItem rootItem] applyChanges];
131 config_SaveConfigFile( p_intf, NULL );
132 [o_prefs_window orderOut:self];
135 - (IBAction)closePrefs: (id)sender
137 [o_prefs_window orderOut:self];
140 - (IBAction)resetAll: (id)sender
142 NSBeginInformationalAlertSheet(_NS("Reset Preferences"), _NS("Cancel"),
143 _NS("Continue"), nil, o_prefs_window, self,
144 @selector(sheetDidEnd: returnCode: contextInfo:), NULL, nil,
145 _NS("Beware this will reset the VLC media player preferences.\n"
146 "Are you sure you want to continue?") );
149 - (void)sheetDidEnd:(NSWindow *)o_sheet returnCode:(int)i_return
150 contextInfo:(void *)o_context
152 if( i_return == NSAlertAlternateReturn )
154 [o_prefs_view setDocumentView: o_empty_view];
155 config_ResetAll( p_intf );
156 [[VLCTreeItem rootItem] resetView];
157 [[o_tree itemAtRow:[o_tree selectedRow]]
158 showView:o_prefs_view advancedView:
159 ( [o_advanced_ckb state] == NSOnState ) ? VLC_TRUE : VLC_FALSE];
163 - (IBAction)advancedToggle: (id)sender
165 b_advanced = !b_advanced;
166 [o_advanced_ckb setState: b_advanced];
167 /* refresh the view of the current treeitem */
168 [[o_tree itemAtRow:[o_tree selectedRow]] showView:o_prefs_view advancedView:
169 ( [o_advanced_ckb state] == NSOnState ) ? VLC_TRUE : VLC_FALSE];
172 - (void)loadConfigTree
176 - (void)outlineViewSelectionIsChanging:(NSNotification *)o_notification
180 /* update the document view to the view of the selected tree item */
181 - (void)outlineViewSelectionDidChange:(NSNotification *)o_notification
183 [[o_tree itemAtRow:[o_tree selectedRow]] showView: o_prefs_view
184 advancedView:( [o_advanced_ckb state] == NSOnState ) ?
185 VLC_TRUE : VLC_FALSE];
190 @implementation VLCPrefs (NSTableDataSource)
192 - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
193 return (item == nil) ? [[VLCTreeItem rootItem] numberOfChildren] :
194 [item numberOfChildren];
197 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
199 return (item == nil) ? YES : ( ([item numberOfChildren] != -1) &&
200 ([item numberOfChildren] != 0));
203 - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item {
204 return (item == nil) ? [[VLCTreeItem rootItem] childAtIndex:index] :
205 (id)[item childAtIndex:index];
208 - (id)outlineView:(NSOutlineView *)outlineView
209 objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
211 return (item == nil) ? @"" : (id)[item getName];
216 @implementation VLCTreeItem
218 static VLCTreeItem *o_root_item = nil;
220 #define IsALeafNode ((id)-1)
222 - (id)initWithName: (NSString *)o_item_name
223 withTitle: (NSString *)o_item_title
224 withHelp: (NSString *)o_item_help
226 parent:(VLCTreeItem *)o_parent_item
227 children:(NSMutableArray *)o_children_array
228 whithCategory: (int) i_category
234 o_name = [o_item_name copy];
235 o_title= [o_item_title copy];
236 o_help= [o_item_help copy];
238 o_parent = o_parent_item;
239 o_children = o_children_array;
240 i_object_category = i_category;
246 + (VLCTreeItem *)rootItem
248 if (o_root_item == nil)
249 o_root_item = [[VLCTreeItem alloc] initWithName:@"main" withTitle:@"main" withHelp:@"" ID:0
250 parent:nil children:[[NSMutableArray alloc] initWithCapacity:10]
257 if (o_children != IsALeafNode) [o_children release];
264 /* Creates and returns the array of children
265 * Loads children incrementally */
266 - (NSArray *)children
268 if( o_children == IsALeafNode )
270 if( [ o_children count] == 0 )
272 intf_thread_t *p_intf = VLCIntf;
274 module_t *p_module = NULL;
276 module_config_t *p_item,
280 /* List the modules */
281 p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
282 if( !p_list ) return nil;
285 p_parser = (module_t *)p_list->p_values[i_index].p_object;
287 if( [[self getName] isEqualToString: @"main"] )
290 * Find the main module
292 for( i_index = 0; i_index < p_list->i_count; i_index++ )
294 p_module = (module_t *)p_list->p_values[i_index].p_object;
295 if( !strcmp( module_GetObjName( p_module ), "main" ) )
298 if( p_module == NULL )
301 "could not load the preferences" );
304 if( i_index < p_list->i_count )
306 /* We found the main module */
307 /* Enumerate config categories and store a reference so we can
308 * generate their config panel them when it is asked by the user. */
309 VLCTreeItem *p_last_category = NULL;
310 unsigned int i_confsize;
311 p_item = module_GetConfig( p_parser, &i_confsize );
312 p_end = p_item + i_confsize;
313 o_children = [[NSMutableArray alloc] initWithCapacity:10];
316 NSString *o_child_name;
317 NSString *o_child_title;
318 NSString *o_child_help;
319 switch( p_item->i_type )
321 case CONFIG_CATEGORY:
322 if( p_item->value.i == -1 ) break;
324 o_child_name = [[VLCMain sharedInstance]
325 localizedString: config_CategoryNameGet( p_item->value.i )];
326 o_child_title = o_child_name;
327 o_child_help = [[VLCMain sharedInstance]
328 localizedString: config_CategoryHelpGet( p_item->value.i )];
329 p_last_category = [VLCTreeItem alloc];
330 [o_children addObject:[p_last_category
331 initWithName: o_child_name
332 withTitle: o_child_title
333 withHelp: o_child_help
336 children:[[NSMutableArray alloc]
338 whithCategory: p_item - module_GetConfig( p_module, &i_confsize )]];
340 case CONFIG_SUBCATEGORY:
341 if( p_item->value.i == -1 ) break;
343 if( p_item->value.i != SUBCAT_PLAYLIST_GENERAL &&
344 p_item->value.i != SUBCAT_VIDEO_GENERAL &&
345 p_item->value.i != SUBCAT_INPUT_GENERAL &&
346 p_item->value.i != SUBCAT_INTERFACE_GENERAL &&
347 p_item->value.i != SUBCAT_SOUT_GENERAL &&
348 p_item->value.i != SUBCAT_ADVANCED_MISC &&
349 p_item->value.i != SUBCAT_AUDIO_GENERAL )
351 o_child_name = [[VLCMain sharedInstance]
352 localizedString: config_CategoryNameGet( p_item->value.i ) ];
353 o_child_title = o_child_name;
354 o_child_help = [[VLCMain sharedInstance]
355 localizedString: config_CategoryHelpGet( p_item->value.i ) ];
357 [p_last_category->o_children
358 addObject:[[VLCTreeItem alloc]
359 initWithName: o_child_name
360 withTitle: o_child_title
361 withHelp: o_child_help
363 parent:p_last_category
364 children:[[NSMutableArray alloc]
366 whithCategory: p_item - module_GetConfig( p_parser, &i_confsize )]];
373 } while( p_item < p_end && p_item++ );
376 /* Build a tree of the plugins */
377 /* Add the capabilities */
378 for( i_index = 0; i_index < p_list->i_count; i_index++ )
380 unsigned int confsize;
381 p_module = (module_t *)p_list->p_values[i_index].p_object;
383 /* Exclude the main module */
384 if( !strcmp( module_GetObjName( p_module ), "main" ) )
387 /* Exclude empty plugins (submodules don't have config */
388 /* options, they are stored in the parent module) */
390 // if( modules_IsSubModule( p_module ) )
392 p_item = module_GetConfig( p_module, &confsize );
394 if( !p_item ) continue;
396 int i_subcategory = -1;
400 if( p_item->i_type == CONFIG_CATEGORY )
401 i_category = p_item->value.i;
402 else if( p_item->i_type == CONFIG_SUBCATEGORY )
403 i_subcategory = p_item->value.i;
405 if( p_item->i_type & CONFIG_ITEM )
407 if( i_options > 0 && i_category >= 0 && i_subcategory >= 0 )
409 } while( p_item < p_end && p_item++ );
410 if( !i_options ) continue;
412 /* Find the right category item */
415 vlc_bool_t b_found = VLC_FALSE;
417 VLCTreeItem* p_category_item, * p_subcategory_item;
418 for (i = 0 ; i < [o_children count] ; i++)
420 p_category_item = [o_children objectAtIndex: i];
421 if( p_category_item->i_object_id == i_category )
427 if( !b_found ) continue;
429 /* Find subcategory item */
432 for (i = 0 ; i < [p_category_item->o_children count] ; i++)
434 p_subcategory_item = [p_category_item->o_children
436 if( p_subcategory_item->i_object_id == i_subcategory )
443 p_subcategory_item = p_category_item;
445 [p_subcategory_item->o_children addObject:[[VLCTreeItem alloc]
446 initWithName:[[VLCMain sharedInstance]
447 localizedString: module_GetName( p_module, VLC_FALSE ) ]
448 withTitle:[[VLCMain sharedInstance]
449 localizedString: module_GetLongName( p_module ) ]
451 ID: ((vlc_object_t*)p_module)->i_object_id
452 parent:p_subcategory_item
457 vlc_list_release( p_list );
467 - (NSString *)getName
472 - (NSString *)getTitle
477 - (NSString *)getHelp
482 - (VLCTreeItem *)childAtIndex:(int)i_index
484 return [[self children] objectAtIndex:i_index];
487 - (int)numberOfChildren {
488 id i_tmp = [self children];
489 return (i_tmp == IsALeafNode) ? (-1) : (int)[i_tmp count];
492 - (BOOL)hasPrefs:(NSString *)o_module_name
494 intf_thread_t *p_intf = VLCIntf;
497 char *psz_module_name;
500 psz_module_name = (char *)[o_module_name UTF8String];
502 /* look for module */
503 p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
505 for( i_index = 0; i_index < p_list->i_count; i_index++ )
507 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
509 if( !strcmp( module_GetObjName( p_parser ), psz_module_name ) )
511 unsigned int confsize;
512 module_GetConfig( p_parser, &confsize );
513 BOOL b_has_prefs = confsize != 0;
514 vlc_list_release( p_list );
515 return( b_has_prefs );
519 vlc_list_release( p_list );
524 - (NSView *)showView:(NSScrollView *)o_prefs_view
525 advancedView:(vlc_bool_t) b_advanced
530 [[VLCPrefs sharedInstance] setTitle: [self getTitle]];
531 /* NSLog( [self getHelp] ); */
532 s_vrc = [[o_prefs_view contentView] bounds]; s_vrc.size.height -= 4;
533 o_view = [[VLCFlippedView alloc] initWithFrame: s_vrc];
534 [o_view setAutoresizingMask: NSViewWidthSizable | NSViewMinYMargin |
537 /* Create all subviews if it isn't already done because we cannot use */
538 /* setHiden for MacOS < 10.3*/
539 if( o_subviews == nil )
541 intf_thread_t *p_intf = VLCIntf;
543 module_t *p_parser = NULL;
544 module_config_t *p_item,
546 unsigned int confsize;
548 o_subviews = [[NSMutableArray alloc] initWithCapacity:10];
549 /* Get a pointer to the module */
550 if( i_object_category == -1 )
552 p_parser = (module_t *) vlc_object_get( p_intf, i_object_id );
553 if( !p_parser || ((vlc_object_t*)p_parser)->i_object_type != VLC_OBJECT_MODULE )
555 /* 0OOoo something went really bad */
558 p_item = module_GetConfig( p_parser, &confsize );
559 p_end = p_item + confsize;
565 msg_Err( p_intf, "invalid preference item found" );
570 switch(p_item->i_type)
572 case CONFIG_SUBCATEGORY:
574 case CONFIG_CATEGORY:
578 case CONFIG_HINT_USAGE:
582 VLCConfigControl *o_control = nil;
583 o_control = [VLCConfigControl newControl:p_item
585 if( o_control != nil )
587 [o_control setAutoresizingMask: NSViewMaxYMargin |
589 [o_subviews addObject: o_control];
594 } while( p_item < p_end && p_item++ );
596 vlc_object_release( (vlc_object_t*)p_parser );
601 p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
602 if( !p_list ) return o_view;
605 * Find the main module
607 for( i_index = 0; i_index < p_list->i_count; i_index++ )
609 p_parser = (module_t *)p_list->p_values[i_index].p_object;
610 if( !strcmp( module_GetObjName( p_parser ), "main" ) )
613 if( p_parser == NULL )
615 msg_Err( p_intf, "could not load preferences" );
618 unsigned int confsize;
619 p_item = module_GetConfig( p_parser, &confsize );
620 p_end = p_item + confsize;
621 p_item += i_object_category;
623 if( ( p_item->i_type == CONFIG_CATEGORY ) &&
624 ( ( p_item->value.i == CAT_PLAYLIST ) ||
625 ( p_item->value.i == CAT_AUDIO ) ||
626 ( p_item->value.i == CAT_VIDEO ) ||
627 ( p_item->value.i == CAT_INTERFACE ) ||
628 ( p_item->value.i == CAT_INPUT ) ||
629 ( p_item->value.i == CAT_SOUT ) ) )
637 msg_Err( p_intf, "invalid preference item found" );
642 switch( p_item->i_type )
644 case CONFIG_SUBCATEGORY:
646 case CONFIG_CATEGORY:
650 case CONFIG_HINT_USAGE:
654 VLCConfigControl *o_control = nil;
655 o_control = [VLCConfigControl newControl:p_item
657 if( o_control != nil )
659 [o_control setAutoresizingMask: NSViewMaxYMargin |
661 [o_subviews addObject: o_control];
666 } while ( ( p_item < p_end ) &&
667 ( p_item->i_type != CONFIG_SUBCATEGORY ) );
669 vlc_list_release( p_list );
678 int i_show_advanced = 0;
680 NSEnumerator *enumerator = [o_subviews objectEnumerator];
681 VLCConfigControl *o_widget;
684 while( ( o_widget = [enumerator nextObject] ) )
685 if( ( [o_widget isAdvanced] ) && (! b_advanced) )
687 else if( i_max_label < [o_widget getLabelSize] )
688 i_max_label = [o_widget getLabelSize];
690 enumerator = [o_subviews objectEnumerator];
691 while( ( o_widget = [enumerator nextObject] ) )
694 if( ( [o_widget isAdvanced] ) && (! b_advanced) )
700 i_widget = [o_widget getViewType];
701 i_yPos += [VLCConfigControl calcVerticalMargin:i_widget
702 lastItem:i_lastItem];
703 [o_widget setYPos:i_yPos];
704 o_frame = [o_widget frame];
705 o_frame.size.width = [o_view frame].size.width -
706 LEFTMARGIN - RIGHTMARGIN;
707 [o_widget setFrame:o_frame];
708 [o_widget alignWithXPosition: i_max_label];
709 i_yPos += [o_widget frame].size.height;
710 i_lastItem = i_widget;
711 [o_view addSubview:o_widget];
713 if( i_show_advanced != 0 )
715 /* We add the advanced notice... */
716 NSRect s_rc = [o_view frame];
717 NSTextField *o_label;
718 s_rc.size.height = 17;
719 s_rc.origin.x = LEFTMARGIN;
720 s_rc.origin.y = i_yPos += [VLCConfigControl
721 calcVerticalMargin:CONFIG_ITEM_STRING
722 lastItem:i_lastItem];
723 o_label = [[[NSTextField alloc] initWithFrame: s_rc] retain];
724 [o_label setDrawsBackground: NO];
725 [o_label setBordered: NO];
726 [o_label setEditable: NO];
727 [o_label setSelectable: NO];
728 [o_label setStringValue: _NS("Some options are hidden. " \
729 "Check \"Advanced\" to display them.")];
730 [o_label setFont:[NSFont systemFontOfSize:10]];
732 [o_view addSubview:o_label];
733 i_yPos += [o_label frame].size.height;
735 o_frame = [o_view frame];
736 o_frame.size.height = i_yPos;
737 [o_view setFrame:o_frame];
738 [o_prefs_view setDocumentView:o_view];
747 if( o_subviews != nil )
748 //Item has been shown
749 for( i = 0 ; i < [o_subviews count] ; i++ )
750 [[o_subviews objectAtIndex:i] applyChanges];
752 if( o_children != IsALeafNode )
753 for( i = 0 ; i < [o_children count] ; i++ )
754 [[o_children objectAtIndex:i] applyChanges];
760 if( o_subviews != nil )
762 //Item has been shown
763 [o_subviews release];
767 if( o_children != IsALeafNode )
768 for( i = 0 ; i < [o_children count] ; i++ )
769 [[o_children objectAtIndex:i] resetView];
775 @implementation VLCFlippedView