1 /*****************************************************************************
2 * prefs.m: MacOS X module for vlc
3 *****************************************************************************
4 * Copyright (C) 2002-2005 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA 02111, 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 setRulersVisible: NO];
101 [o_prefs_view setDocumentView: o_empty_view];
102 [o_tree selectRow:0 byExtendingSelection:NO];
107 /* load our nib (if not already loaded) */
108 [NSBundle loadNibNamed:@"Preferences" owner:self];
110 [o_prefs_window center];
111 [o_prefs_window makeKeyAndOrderFront:self];
116 [o_prefs_window setTitle: _NS("Preferences")];
117 [o_save_btn setTitle: _NS("Save")];
118 [o_cancel_btn setTitle: _NS("Cancel")];
119 [o_reset_btn setTitle: _NS("Reset All")];
120 [o_advanced_ckb setTitle: _NS("Advanced")];
123 - (IBAction)savePrefs: (id)sender
125 /* TODO: call savePrefs on Root item */
126 [[VLCTreeItem rootItem] applyChanges];
127 config_SaveConfigFile( p_intf, NULL );
128 [o_prefs_window orderOut:self];
131 - (IBAction)closePrefs: (id)sender
133 [o_prefs_window orderOut:self];
136 - (IBAction)resetAll: (id)sender
138 NSBeginInformationalAlertSheet(_NS("Reset Preferences"), _NS("Cancel"),
139 _NS("Continue"), nil, o_prefs_window, self,
140 @selector(sheetDidEnd: returnCode: contextInfo:), NULL, nil,
141 _NS("Beware this will reset your VLC media player preferences.\n"
142 "Are you sure you want to continue?") );
145 - (void)sheetDidEnd:(NSWindow *)o_sheet returnCode:(int)i_return
146 contextInfo:(void *)o_context
148 if( i_return == NSAlertAlternateReturn )
150 config_ResetAll( p_intf );
151 [[o_tree itemAtRow:[o_tree selectedRow]]
152 showView:o_prefs_view advancedView:
153 ( [o_advanced_ckb state] == NSOnState ) ? VLC_TRUE : VLC_FALSE];
157 - (IBAction)advancedToggle: (id)sender
159 b_advanced = !b_advanced;
160 [o_advanced_ckb setState: b_advanced];
161 /* refresh the view of the current treeitem */
162 [[o_tree itemAtRow:[o_tree selectedRow]] showView:o_prefs_view advancedView:
163 ( [o_advanced_ckb state] == NSOnState ) ? VLC_TRUE : VLC_FALSE];
166 - (void)loadConfigTree
170 - (void)outlineViewSelectionIsChanging:(NSNotification *)o_notification
174 /* update the document view to the view of the selected tree item */
175 - (void)outlineViewSelectionDidChange:(NSNotification *)o_notification
177 [[o_tree itemAtRow:[o_tree selectedRow]] showView: o_prefs_view
178 advancedView:( [o_advanced_ckb state] == NSOnState ) ?
179 VLC_TRUE : VLC_FALSE];
184 @implementation VLCPrefs (NSTableDataSource)
186 - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
187 return (item == nil) ? [[VLCTreeItem rootItem] numberOfChildren] :
188 [item numberOfChildren];
191 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
193 return (item == nil) ? YES : ( ([item numberOfChildren] != -1) &&
194 ([item numberOfChildren] != 0));
197 - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item {
198 return (item == nil) ? [[VLCTreeItem rootItem] childAtIndex:index] :
199 [item childAtIndex:index];
202 - (id)outlineView:(NSOutlineView *)outlineView
203 objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
205 return (item == nil) ? @"" : (id)[item getName];
210 @implementation VLCTreeItem
212 static VLCTreeItem *o_root_item = nil;
214 #define IsALeafNode ((id)-1)
216 - (id)initWithName: (NSString *)o_item_name ID: (int)i_id
217 parent:(VLCTreeItem *)o_parent_item
218 children:(NSMutableArray *)o_children_array
219 whithCategory: (int) i_category
225 o_name = [o_item_name copy];
227 o_parent = o_parent_item;
228 o_children = o_children_array;
229 i_object_category = i_category;
235 + (VLCTreeItem *)rootItem
237 if (o_root_item == nil)
238 o_root_item = [[VLCTreeItem alloc] initWithName:@"main" ID:0
239 parent:nil children:[[NSMutableArray alloc] initWithCapacity:10]
246 if (o_children != IsALeafNode) [o_children release];
251 /* Creates and returns the array of children
252 * Loads children incrementally */
253 - (NSArray *)children
255 if( o_children == IsALeafNode )
257 if( [ o_children count] == 0 )
259 intf_thread_t *p_intf = VLCIntf;
261 module_t *p_module = NULL;
262 module_config_t *p_item;
265 /* List the modules */
266 p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
267 if( !p_list ) return nil;
269 if( [[self getName] isEqualToString: @"main"] )
272 * Find the main module
274 for( i_index = 0; i_index < p_list->i_count; i_index++ )
276 p_module = (module_t *)p_list->p_values[i_index].p_object;
277 if( !strcmp( p_module->psz_object_name, "main" ) )
280 if( p_module == NULL )
283 "could not find the main module in our preferences" );
286 if( i_index < p_list->i_count )
288 /* We found the main module */
289 /* Enumerate config categories and store a reference so we can
290 * generate their config panel them when it is asked by the user. */
291 VLCTreeItem *p_last_category = NULL;
292 p_item = p_module->p_config;
293 o_children = [[NSMutableArray alloc] initWithCapacity:10];
296 NSString *o_child_name;
297 switch( p_item->i_type )
299 case CONFIG_CATEGORY:
300 o_child_name = [[VLCMain sharedInstance]
301 localizedString: config_CategoryNameGet(p_item->i_value ) ];
302 p_last_category = [VLCTreeItem alloc];
303 [o_children addObject:[p_last_category
304 initWithName: o_child_name
307 children:[[NSMutableArray alloc]
309 whithCategory: p_item - p_module->p_config]];
311 case CONFIG_SUBCATEGORY:
312 o_child_name = [[VLCMain sharedInstance]
313 localizedString: config_CategoryNameGet(p_item->i_value ) ];
314 if( p_item->i_value != SUBCAT_VIDEO_GENERAL &&
315 p_item->i_value != SUBCAT_AUDIO_GENERAL )
316 [p_last_category->o_children
317 addObject:[[VLCTreeItem alloc]
318 initWithName: o_child_name
320 parent:p_last_category
321 children:[[NSMutableArray alloc]
323 whithCategory: p_item - p_module->p_config]];
328 } while( p_item->i_type != CONFIG_HINT_END && p_item++ );
331 /* Build a tree of the plugins */
332 /* Add the capabilities */
333 for( i_index = 0; i_index < p_list->i_count; i_index++ )
335 p_module = (module_t *)p_list->p_values[i_index].p_object;
337 /* Exclude the main module */
338 if( !strcmp( p_module->psz_object_name, "main" ) )
341 /* Exclude empty plugins (submodules don't have config */
342 /* options, they are stored in the parent module) */
343 if( p_module->b_submodule )
346 p_item = p_module->p_config;
348 if( !p_item ) continue;
350 int i_subcategory = -1;
354 if( p_item->i_type == CONFIG_CATEGORY )
355 i_category = p_item->i_value;
356 else if( p_item->i_type == CONFIG_SUBCATEGORY )
357 i_subcategory = p_item->i_value;
359 if( p_item->i_type & CONFIG_ITEM )
361 if( i_options > 0 && i_category >= 0 && i_subcategory >= 0 )
363 } while( p_item->i_type != CONFIG_HINT_END && p_item++ );
364 if( !i_options ) continue;
366 /* Find the right category item */
369 vlc_bool_t b_found = VLC_FALSE;
371 VLCTreeItem* p_category_item, * p_subcategory_item;
372 for (i = 0 ; i < [o_children count] ; i++)
374 p_category_item = [o_children objectAtIndex: i];
375 if( p_category_item->i_object_id == i_category )
381 if( !b_found ) continue;
383 /* Find subcategory item */
386 for (i = 0 ; i < [p_category_item->o_children count] ; i++)
388 p_subcategory_item = [p_category_item->o_children
390 if( p_subcategory_item->i_object_id == i_subcategory )
397 p_subcategory_item = p_category_item;
399 [p_subcategory_item->o_children addObject:[[VLCTreeItem alloc]
400 initWithName:[[VLCMain sharedInstance]
401 localizedString: p_module->psz_object_name ]
402 ID: p_module->i_object_id
403 parent:p_subcategory_item
408 vlc_list_release( p_list );
418 - (NSString *)getName
423 - (VLCTreeItem *)childAtIndex:(int)i_index
425 return [[self children] objectAtIndex:i_index];
428 - (int)numberOfChildren {
429 id i_tmp = [self children];
430 return (i_tmp == IsALeafNode) ? (-1) : (int)[i_tmp count];
433 - (BOOL)hasPrefs:(NSString *)o_module_name
435 intf_thread_t *p_intf = VLCIntf;
438 char *psz_module_name;
441 psz_module_name = (char *)[o_module_name UTF8String];
443 /* look for module */
444 p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
446 for( i_index = 0; i_index < p_list->i_count; i_index++ )
448 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
450 if( !strcmp( p_parser->psz_object_name, psz_module_name ) )
452 BOOL b_has_prefs = p_parser->i_config_items != 0;
453 vlc_list_release( p_list );
454 return( b_has_prefs );
458 vlc_list_release( p_list );
463 - (NSView *)showView:(NSScrollView *)o_prefs_view
464 advancedView:(vlc_bool_t) b_advanced
466 fprintf( stderr, "[%s] showView\n", [o_name UTF8String] );
469 intf_thread_t *p_intf = VLCIntf;
471 module_t *p_parser = NULL;
472 module_config_t *p_item;
475 s_vrc = [[o_prefs_view contentView] bounds]; s_vrc.size.height -= 4;
476 o_view = [[VLCFlippedView alloc] initWithFrame: s_vrc];
477 [o_view setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable];
479 /* Get a pointer to the module */
480 if( i_object_category == -1 )
482 p_parser = (module_t *) vlc_object_get( p_intf, i_object_id );
483 if( !p_parser || p_parser->i_object_type != VLC_OBJECT_MODULE )
485 /* 0OOoo something went really bad */
488 p_item = p_parser->p_config;
493 p_item = p_parser->p_config + 1;
499 msg_Err( p_intf, "null item found" );
502 switch(p_item->i_type)
504 case CONFIG_SUBCATEGORY:
505 fprintf( stderr, "drawing subcategory %s\n", [o_name UTF8String] );
508 fprintf( stderr, "drawing section %s\n", p_item->psz_text );
510 case CONFIG_CATEGORY:
511 fprintf( stderr, "drawing category %s\n", [o_name UTF8String] );
513 case CONFIG_HINT_END:
514 fprintf( stderr, "end of (sub)category\n" );
516 case CONFIG_HINT_USAGE:
517 fprintf( stderr, "skipping hint usage\n" );
520 fprintf( stderr, "%s (%d) is ", p_item->psz_name, p_item->i_type );
522 VLCConfigControl *o_control = nil;
524 if( p_item->b_advanced && (! b_advanced) )
526 switch( p_item->i_type )
528 case CONFIG_ITEM_STRING:
529 fprintf( stderr, "CONFIG_ITEM_STRING" );
530 if( !p_item->i_list )
531 i_widget = CONFIG_ITEM_STRING;
533 i_widget = CONFIG_ITEM_STRING_LIST;
535 case CONFIG_ITEM_FILE:
536 case CONFIG_ITEM_DIRECTORY:
537 fprintf( stderr, "CONFIG_ITEM_FILE" );
538 i_widget = CONFIG_ITEM_FILE;
540 case CONFIG_ITEM_MODULE:
541 case CONFIG_ITEM_MODULE_CAT:
542 fprintf( stderr, "CONFIG_ITEM_MODULE" );
543 i_widget = CONFIG_ITEM_MODULE;
545 case CONFIG_ITEM_INTEGER:
546 fprintf( stderr, "CONFIG_ITEM_INTEGER" );
548 i_widget = CONFIG_ITEM_STRING_LIST;
549 else if( p_item->i_min != 0 || p_item->i_max != 0 )
550 i_widget = CONFIG_ITEM_RANGED_INTEGER;
552 i_widget = CONFIG_ITEM_INTEGER;
554 case CONFIG_ITEM_FLOAT:
555 fprintf( stderr, "CONFIG_ITEM_FLOAT" );
556 if( p_item->f_min != 0 || p_item->f_max != 0 )
557 i_widget = CONFIG_ITEM_RANGED_INTEGER;
559 i_widget = CONFIG_ITEM_INTEGER;
561 case CONFIG_ITEM_BOOL:
562 fprintf( stderr, "CONFIG_ITEM_BOOL" );
563 i_widget = CONFIG_ITEM_BOOL;
565 case CONFIG_ITEM_KEY:
566 fprintf( stderr, "CONFIG_ITEM_KEY" );
567 if( MACOS_VERSION < 10.3 )
568 i_widget = CONFIG_ITEM_KEY_BEFORE_10_3;
570 i_widget = CONFIG_ITEM_KEY_AFTER_10_3;
572 case CONFIG_ITEM_MODULE_LIST:
573 case CONFIG_ITEM_MODULE_LIST_CAT:
574 fprintf( stderr, "CONFIG_ITEM_MODULE_LIST" );
575 i_widget = CONFIG_ITEM_MODULE_LIST;
578 fprintf( stderr, "***UNKNOWN***" );
582 i_yPos += [VLCConfigControl
583 calcVerticalMargin:i_widget lastItem:i_lastItem];
584 o_control = [VLCConfigControl newControl:p_item
587 lastItem: i_lastItem];
588 if( o_control != nil )
590 i_yPos += [o_control frame].size.height;
591 i_lastItem = i_widget;
592 [o_control setAutoresizingMask: NSViewMaxYMargin |
594 [o_view addSubview: o_control];
597 fprintf( stderr, "\n" );
601 } while( p_item++->i_type != CONFIG_HINT_END );
603 vlc_object_release( p_parser );
611 p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
612 if( !p_list ) return o_view;
615 * Find the main module
617 for( i_index = 0; i_index < p_list->i_count; i_index++ )
619 p_parser = (module_t *)p_list->p_values[i_index].p_object;
620 if( !strcmp( p_parser->psz_object_name, "main" ) )
623 if( p_parser == NULL )
625 msg_Err( p_intf, "could not find the main module in our "
629 p_item = (p_parser->p_config + i_object_category);
630 if( ( p_item->i_type == CONFIG_CATEGORY ) &&
631 ( ( p_item->i_value == CAT_AUDIO ) ||
632 ( p_item->i_value == CAT_VIDEO ) ) )
640 msg_Err( p_intf, "null item found" );
643 switch(p_item->i_type)
645 case CONFIG_SUBCATEGORY:
646 fprintf( stderr, "drawing subcategory %s\n", [o_name UTF8String] );
649 fprintf( stderr, "drawing section %s\n", p_item->psz_text );
651 case CONFIG_CATEGORY:
652 fprintf( stderr, "drawing category %s\n", [o_name UTF8String] );
654 case CONFIG_HINT_END:
655 fprintf( stderr, "end of (sub)category\n" );
657 case CONFIG_HINT_USAGE:
658 fprintf( stderr, "skipping hint usage\n" );
661 fprintf( stderr, "%s (%d) is ", p_item->psz_name, p_item->i_type );
663 VLCConfigControl *o_control = nil;
665 switch( p_item->i_type )
667 case CONFIG_ITEM_STRING:
668 fprintf( stderr, "CONFIG_ITEM_STRING" );
669 if( !p_item->i_list )
670 i_widget = CONFIG_ITEM_STRING;
672 i_widget = CONFIG_ITEM_STRING_LIST;
674 case CONFIG_ITEM_FILE:
675 case CONFIG_ITEM_DIRECTORY:
676 fprintf( stderr, "CONFIG_ITEM_FILE" );
677 i_widget = CONFIG_ITEM_FILE;
679 case CONFIG_ITEM_MODULE:
680 case CONFIG_ITEM_MODULE_CAT:
681 fprintf( stderr, "CONFIG_ITEM_MODULE" );
682 i_widget = CONFIG_ITEM_MODULE;
684 case CONFIG_ITEM_INTEGER:
685 fprintf( stderr, "CONFIG_ITEM_INTEGER" );
687 i_widget = CONFIG_ITEM_STRING_LIST;
688 else if( p_item->i_min != 0 || p_item->i_max != 0 )
689 i_widget = CONFIG_ITEM_RANGED_INTEGER;
691 i_widget = CONFIG_ITEM_INTEGER;
693 case CONFIG_ITEM_FLOAT:
694 fprintf( stderr, "CONFIG_ITEM_FLOAT" );
695 if( p_item->f_min != 0 || p_item->f_max != 0 )
696 i_widget = CONFIG_ITEM_RANGED_INTEGER;
698 i_widget = CONFIG_ITEM_INTEGER;
700 case CONFIG_ITEM_BOOL:
701 fprintf( stderr, "CONFIG_ITEM_BOOL" );
702 i_widget = CONFIG_ITEM_BOOL;
704 case CONFIG_ITEM_KEY:
705 fprintf( stderr, "CONFIG_ITEM_KEY" );
706 if( MACOS_VERSION < 10.3 )
707 i_widget = CONFIG_ITEM_KEY_BEFORE_10_3;
709 i_widget = CONFIG_ITEM_KEY_AFTER_10_3;
711 case CONFIG_ITEM_MODULE_LIST:
712 case CONFIG_ITEM_MODULE_LIST_CAT:
713 fprintf( stderr, "CONFIG_ITEM_MODULE_LIST" );
714 i_widget = CONFIG_ITEM_MODULE_LIST;
717 fprintf( stderr, "***UNKNOWN***" );
721 i_yPos += [VLCConfigControl
722 calcVerticalMargin:i_widget lastItem:i_lastItem];
723 o_control = [VLCConfigControl newControl:p_item
726 lastItem: i_lastItem];
727 if( o_control != nil )
729 i_yPos += [o_control frame].size.height;
730 i_lastItem = i_widget;
731 [o_control setAutoresizingMask: NSViewMaxYMargin |
733 [o_view addSubview: o_control];
736 fprintf( stderr, "\n" );
740 } while ( ( p_item->i_type != CONFIG_HINT_END ) &&
741 ( p_item->i_type != CONFIG_SUBCATEGORY ) );
743 vlc_object_release( p_parser );
744 vlc_list_release( p_list );
750 s_vrc = [[o_prefs_view contentView] bounds]; s_vrc.size.height -= 4;
751 [o_view setFrame: s_vrc];
754 [o_prefs_view setDocumentView:o_view];
763 //Item has been shown
764 fprintf( stderr, "[%s] applying changes\n", [o_name cString]);
765 NSArray *o_subviews = [o_view subviews];
766 for( i = 0 ; i < [o_subviews count] ; i++ )
767 [[o_subviews objectAtIndex:i] applyChanges];
769 if( o_children != IsALeafNode )
770 for( i = 0 ; i < [o_children count] ; i++ )
771 [[o_children objectAtIndex:i] applyChanges];
777 @implementation VLCFlippedView