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 /* Show View for the currently select treeitem */
111 /* [self showViewForID: [[o_tree itemAtRow:[o_tree selectedRow]] getObjectID]
112 andName: [[o_tree itemAtRow:[o_tree selectedRow]] getName]]; */
113 [o_prefs_window center];
114 [o_prefs_window makeKeyAndOrderFront:self];
119 [o_prefs_window setTitle: _NS("Preferences")];
120 [o_save_btn setTitle: _NS("Save")];
121 [o_cancel_btn setTitle: _NS("Cancel")];
122 [o_reset_btn setTitle: _NS("Reset All")];
123 [o_advanced_ckb setTitle: _NS("Advanced")];
126 - (IBAction)savePrefs: (id)sender
128 /* TODO: call savePrefs on Root item */
129 [[VLCTreeItem rootItem] applyChanges];
130 config_SaveConfigFile( p_intf, NULL );
131 [o_prefs_window orderOut:self];
134 - (IBAction)closePrefs: (id)sender
136 [o_prefs_window orderOut:self];
139 - (IBAction)resetAll: (id)sender
141 NSBeginInformationalAlertSheet(_NS("Reset Preferences"), _NS("Cancel"), _NS("Continue"),
142 nil, o_prefs_window, self, @selector(sheetDidEnd: returnCode: contextInfo:), NULL, nil,
143 _NS("Beware this will reset your VLC media player preferences.\n"
144 "Are you sure you want to continue?") );
147 - (void)sheetDidEnd:(NSWindow *)o_sheet returnCode:(int)i_return contextInfo:(void *)o_context
149 if( i_return == NSAlertAlternateReturn )
151 config_ResetAll( p_intf );
152 [[o_tree itemAtRow:[o_tree selectedRow]] 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 advancedView:
178 ([o_advanced_ckb state] == NSOnState)?VLC_TRUE:VLC_FALSE];
183 @implementation VLCPrefs (NSTableDataSource)
185 - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
186 return (item == nil) ? [[VLCTreeItem rootItem] numberOfChildren] : [item numberOfChildren];
189 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
190 return (item == nil) ? YES : (([item numberOfChildren] != -1) && ([item numberOfChildren] != 0));
193 - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item {
194 return (item == nil) ? [[VLCTreeItem rootItem] childAtIndex:index] : [item childAtIndex:index];
197 - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item {
198 return (item == nil) ? @"" : (id)[item getName];
203 @implementation VLCTreeItem
205 static VLCTreeItem *o_root_item = nil;
207 #define IsALeafNode ((id)-1)
209 - (id)initWithName: (NSString *)o_item_name ID: (int)i_id parent:(VLCTreeItem *)o_parent_item children:(NSMutableArray *)o_children_array whithCategory: (int) i_category
215 o_name = [o_item_name copy];
217 o_parent = o_parent_item;
218 o_children = o_children_array;
219 i_object_category = i_category;
225 + (VLCTreeItem *)rootItem {
226 if (o_root_item == nil) o_root_item = [[VLCTreeItem alloc] initWithName:@"main" ID: 0 parent:nil children:[[NSMutableArray alloc] initWithCapacity:10] whithCategory: -1];
232 if (o_children != IsALeafNode) [o_children release];
237 /* Creates and returns the array of children
238 * Loads children incrementally */
239 - (NSArray *)children
241 if( o_children == IsALeafNode )
243 if( [ o_children count] == 0 )
245 intf_thread_t *p_intf = VLCIntf;
247 module_t *p_module = NULL;
248 module_config_t *p_item;
251 /* List the modules */
252 p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
253 if( !p_list ) return nil;
255 if( [[self getName] isEqualToString: @"main"] )
258 * Find the main module
260 for( i_index = 0; i_index < p_list->i_count; i_index++ )
262 p_module = (module_t *)p_list->p_values[i_index].p_object;
263 if( !strcmp( p_module->psz_object_name, "main" ) )
266 if( p_module == NULL )
268 msg_Err( p_intf, "could not find the main module in our preferences" );
271 if( i_index < p_list->i_count )
273 /* We found the main module */
274 /* Enumerate config categories and store a reference so we can
275 * generate their config panel them when it is asked by the user. */
276 VLCTreeItem *p_last_category = NULL;
277 p_item = p_module->p_config;
278 o_children = [[NSMutableArray alloc] initWithCapacity:10];
281 NSString *o_child_name;
282 switch( p_item->i_type )
284 case CONFIG_CATEGORY:
285 o_child_name = [[VLCMain sharedInstance] localizedString: config_CategoryNameGet(p_item->i_value ) ];
286 p_last_category = [VLCTreeItem alloc];
287 [o_children addObject:[p_last_category initWithName: o_child_name
288 ID: p_item->i_value parent:self children:[[NSMutableArray alloc] initWithCapacity:10] whithCategory: p_item - p_module->p_config]];
290 case CONFIG_SUBCATEGORY:
291 o_child_name = [[VLCMain sharedInstance] localizedString: config_CategoryNameGet(p_item->i_value ) ];
292 if( p_item->i_value != SUBCAT_VIDEO_GENERAL &&
293 p_item->i_value != SUBCAT_AUDIO_GENERAL )
294 [p_last_category->o_children addObject:[[VLCTreeItem alloc] initWithName: o_child_name
295 ID: p_item->i_value parent:p_last_category children:[[NSMutableArray alloc] initWithCapacity:10] whithCategory: p_item - p_module->p_config]];
300 } while( p_item->i_type != CONFIG_HINT_END && p_item++ );
303 /* Build a tree of the plugins */
304 /* Add the capabilities */
305 for( i_index = 0; i_index < p_list->i_count; i_index++ )
307 p_module = (module_t *)p_list->p_values[i_index].p_object;
309 /* Exclude the main module */
310 if( !strcmp( p_module->psz_object_name, "main" ) )
313 /* Exclude empty plugins (submodules don't have config options, they
314 * are stored in the parent module) */
315 if( p_module->b_submodule )
318 p_item = p_module->p_config;
320 if( !p_item ) continue;
322 int i_subcategory = -1;
326 if( p_item->i_type == CONFIG_CATEGORY )
328 i_category = p_item->i_value;
330 else if( p_item->i_type == CONFIG_SUBCATEGORY )
332 i_subcategory = p_item->i_value;
334 if( p_item->i_type & CONFIG_ITEM )
336 if( i_options > 0 && i_category >= 0 && i_subcategory >= 0 )
340 } while( p_item->i_type != CONFIG_HINT_END && p_item++ );
341 if( !i_options ) continue;
343 /* Find the right category item */
346 vlc_bool_t b_found = VLC_FALSE;
348 VLCTreeItem* p_category_item, * p_subcategory_item;
349 for (i = 0 ; i < [o_children count] ; i++)
351 p_category_item = [o_children objectAtIndex: i];
352 if( p_category_item->i_object_id == i_category )
358 if( !b_found ) continue;
360 /* Find subcategory item */
363 for (i = 0 ; i < [p_category_item->o_children count] ; i++)
365 p_subcategory_item = [p_category_item->o_children objectAtIndex: i];
366 if( p_subcategory_item->i_object_id == i_subcategory )
373 p_subcategory_item = p_category_item;
375 [p_subcategory_item->o_children addObject:[[VLCTreeItem alloc] initWithName:
376 [[VLCMain sharedInstance] localizedString: p_module->psz_object_name ]
377 ID: p_module->i_object_id parent:p_subcategory_item children:IsALeafNode whithCategory: -1]];
381 vlc_list_release( p_list );
391 - (NSString *)getName
396 - (VLCTreeItem *)childAtIndex:(int)i_index {
397 return [[self children] objectAtIndex:i_index];
400 - (int)numberOfChildren {
401 id i_tmp = [self children];
402 return (i_tmp == IsALeafNode) ? (-1) : (int)[i_tmp count];
405 - (BOOL)hasPrefs:(NSString *)o_module_name
407 intf_thread_t *p_intf = VLCIntf;
410 char *psz_module_name;
413 psz_module_name = (char *)[o_module_name UTF8String];
415 /* look for module */
416 p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
418 for( i_index = 0; i_index < p_list->i_count; i_index++ )
420 p_parser = (module_t *)p_list->p_values[i_index].p_object ;
422 if( !strcmp( p_parser->psz_object_name, psz_module_name ) )
424 BOOL b_has_prefs = p_parser->i_config_items != 0;
425 vlc_list_release( p_list );
426 return( b_has_prefs );
430 vlc_list_release( p_list );
435 - (NSView *)showView:(NSScrollView *)o_prefs_view advancedView:(vlc_bool_t) b_advanced
437 fprintf( stderr, "[%s] showView\n", [o_name UTF8String] );
440 intf_thread_t *p_intf = VLCIntf;
442 module_t *p_parser = NULL;
443 module_config_t *p_item;
446 s_vrc = [[o_prefs_view contentView] bounds]; s_vrc.size.height -= 4;
447 o_view = [[VLCFlippedView alloc] initWithFrame: s_vrc];
448 [o_view setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable];
450 /* Get a pointer to the module */
451 if( i_object_category == -1 )
453 p_parser = (module_t *) vlc_object_get( p_intf, i_object_id );
454 if( !p_parser || p_parser->i_object_type != VLC_OBJECT_MODULE )
456 /* 0OOoo something went really bad */
459 p_item = p_parser->p_config;
464 p_item = p_parser->p_config + 1;
470 fprintf( stderr, "Something is going very bad... skipping null item\n" );
473 switch(p_item->i_type)
475 case CONFIG_SUBCATEGORY:
476 fprintf( stderr, "drawing subcategory %s\n", [o_name UTF8String] );
479 fprintf( stderr, "drawing section %s\n", p_item->psz_text );
481 case CONFIG_CATEGORY:
482 fprintf( stderr, "drawing category %s\n", [o_name UTF8String] );
484 case CONFIG_HINT_END:
485 fprintf( stderr, "end of (sub)category\n" );
487 case CONFIG_HINT_USAGE:
488 fprintf( stderr, "skipping hint usage\n" );
491 fprintf( stderr, "%s (%d) is ", p_item->psz_name, p_item->i_type );
493 VLCConfigControl *o_control = nil;
495 if( p_item->b_advanced && (! b_advanced) )
497 switch( p_item->i_type )
499 case CONFIG_ITEM_STRING:
500 fprintf( stderr, "CONFIG_ITEM_STRING" );
501 if( !p_item->i_list )
502 i_widget = CONFIG_ITEM_STRING;
504 i_widget = CONFIG_ITEM_STRING_LIST;
506 case CONFIG_ITEM_FILE:
507 case CONFIG_ITEM_DIRECTORY:
508 fprintf( stderr, "CONFIG_ITEM_FILE" );
509 i_widget = CONFIG_ITEM_FILE;
511 case CONFIG_ITEM_MODULE:
512 case CONFIG_ITEM_MODULE_CAT:
513 fprintf( stderr, "CONFIG_ITEM_MODULE" );
514 i_widget = CONFIG_ITEM_MODULE;
516 case CONFIG_ITEM_INTEGER:
517 fprintf( stderr, "CONFIG_ITEM_INTEGER" );
519 i_widget = CONFIG_ITEM_STRING_LIST;
520 else if( p_item->i_min != 0 || p_item->i_max != 0 )
521 i_widget = CONFIG_ITEM_RANGED_INTEGER;
523 i_widget = CONFIG_ITEM_INTEGER;
525 case CONFIG_ITEM_FLOAT:
526 fprintf( stderr, "CONFIG_ITEM_FLOAT" );
527 if( p_item->f_min != 0 || p_item->f_max != 0 )
528 i_widget = CONFIG_ITEM_RANGED_INTEGER;
530 i_widget = CONFIG_ITEM_INTEGER;
532 case CONFIG_ITEM_BOOL:
533 fprintf( stderr, "CONFIG_ITEM_BOOL" );
534 i_widget = CONFIG_ITEM_BOOL;
536 case CONFIG_ITEM_KEY:
537 fprintf( stderr, "CONFIG_ITEM_KEY" );
538 #define MACOS_VERSION 4
539 if( MACOS_VERSION < 3 )
540 i_widget = CONFIG_ITEM_KEY_BEFORE_10_3;
542 i_widget = CONFIG_ITEM_KEY_AFTER_10_3;
544 case CONFIG_ITEM_MODULE_LIST:
545 case CONFIG_ITEM_MODULE_LIST_CAT:
546 fprintf( stderr, "CONFIG_ITEM_MODULE_LIST" );
547 i_widget = CONFIG_ITEM_MODULE_LIST;
550 fprintf( stderr, "***UNKNOWN***" );
554 i_yPos += [VLCConfigControl calcVerticalMargin:i_widget lastItem:i_lastItem];
555 o_control = [VLCConfigControl newControl:p_item
558 lastItem: i_lastItem];
559 if( o_control != nil )
561 i_yPos += [o_control frame].size.height;
562 i_lastItem = i_widget;
563 [o_control setAutoresizingMask: NSViewMaxYMargin | NSViewWidthSizable];
564 [o_view addSubview: o_control];
567 fprintf( stderr, "\n" );
571 } while( p_item++->i_type != CONFIG_HINT_END );
573 vlc_object_release( p_parser );
578 p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
579 if( !p_list ) return o_view;
582 * Find the main module
584 for( i_index = 0; i_index < p_list->i_count; i_index++ )
586 p_parser = (module_t *)p_list->p_values[i_index].p_object;
587 if( !strcmp( p_parser->psz_object_name, "main" ) )
590 if( p_parser == NULL )
592 msg_Err( p_intf, "could not find the main module in our preferences" );
595 p_item = (p_parser->p_config + i_object_category);
596 if( ( p_item->i_type == CONFIG_CATEGORY ) &&
597 ( ( p_item->i_value == CAT_AUDIO ) || ( p_item->i_value == CAT_VIDEO ) ) )
609 fprintf( stderr, "Something is going very bad... skipping null item\n" );
612 switch(p_item->i_type)
614 case CONFIG_SUBCATEGORY:
615 fprintf( stderr, "drawing subcategory %s\n", [o_name UTF8String] );
618 fprintf( stderr, "drawing section %s\n", p_item->psz_text );
620 case CONFIG_CATEGORY:
621 fprintf( stderr, "drawing category %s\n", [o_name UTF8String] );
623 case CONFIG_HINT_END:
624 fprintf( stderr, "end of (sub)category\n" );
626 case CONFIG_HINT_USAGE:
627 fprintf( stderr, "skipping hint usage\n" );
630 fprintf( stderr, "%s (%d) is ", p_item->psz_name, p_item->i_type );
632 VLCConfigControl *o_control = nil;
634 switch( p_item->i_type )
636 case CONFIG_ITEM_STRING:
637 fprintf( stderr, "CONFIG_ITEM_STRING" );
638 if( !p_item->i_list )
639 i_widget = CONFIG_ITEM_STRING;
641 i_widget = CONFIG_ITEM_STRING_LIST;
643 case CONFIG_ITEM_FILE:
644 case CONFIG_ITEM_DIRECTORY:
645 fprintf( stderr, "CONFIG_ITEM_FILE" );
646 i_widget = CONFIG_ITEM_FILE;
648 case CONFIG_ITEM_MODULE:
649 case CONFIG_ITEM_MODULE_CAT:
650 fprintf( stderr, "CONFIG_ITEM_MODULE" );
651 i_widget = CONFIG_ITEM_MODULE;
653 case CONFIG_ITEM_INTEGER:
654 fprintf( stderr, "CONFIG_ITEM_INTEGER" );
656 i_widget = CONFIG_ITEM_STRING_LIST;
657 else if( p_item->i_min != 0 || p_item->i_max != 0 )
658 i_widget = CONFIG_ITEM_RANGED_INTEGER;
660 i_widget = CONFIG_ITEM_INTEGER;
662 case CONFIG_ITEM_FLOAT:
663 fprintf( stderr, "CONFIG_ITEM_FLOAT" );
664 if( p_item->f_min != 0 || p_item->f_max != 0 )
665 i_widget = CONFIG_ITEM_RANGED_INTEGER;
667 i_widget = CONFIG_ITEM_INTEGER;
669 case CONFIG_ITEM_BOOL:
670 fprintf( stderr, "CONFIG_ITEM_BOOL" );
671 i_widget = CONFIG_ITEM_BOOL;
673 case CONFIG_ITEM_KEY:
674 fprintf( stderr, "CONFIG_ITEM_KEY" );
675 #define MACOS_VERSION 4
676 if( MACOS_VERSION < 3 )
677 i_widget = CONFIG_ITEM_KEY_BEFORE_10_3;
679 i_widget = CONFIG_ITEM_KEY_AFTER_10_3;
681 case CONFIG_ITEM_MODULE_LIST:
682 case CONFIG_ITEM_MODULE_LIST_CAT:
683 fprintf( stderr, "CONFIG_ITEM_MODULE_LIST" );
684 i_widget = CONFIG_ITEM_MODULE_LIST;
687 fprintf( stderr, "***UNKNOWN***" );
691 i_yPos += [VLCConfigControl calcVerticalMargin:i_widget lastItem:i_lastItem];
692 o_control = [VLCConfigControl newControl:p_item
695 lastItem: i_lastItem];
696 if( o_control != nil )
698 i_yPos += [o_control frame].size.height;
699 i_lastItem = i_widget;
700 [o_control setAutoresizingMask: NSViewMaxYMargin | NSViewWidthSizable];
701 [o_view addSubview: o_control];
704 fprintf( stderr, "\n" );
708 } while ( ( p_item->i_type != CONFIG_HINT_END ) && ( p_item->i_type != CONFIG_SUBCATEGORY ) );
710 vlc_object_release( p_parser );
711 vlc_list_release( p_list );
717 s_vrc = [[o_prefs_view contentView] bounds]; s_vrc.size.height -= 4;
718 [o_view setFrame: s_vrc];
721 [o_prefs_view setDocumentView:o_view];
730 //Item has been shown
731 fprintf( stderr, "[%s] applying changes\n", [o_name cString]);
732 NSArray *o_subviews = [o_view subviews];
733 for( i = 0 ; i < [o_subviews count] ; i++ )
734 [[o_subviews objectAtIndex:i] applyChanges];
736 if( o_children != IsALeafNode )
737 for( i = 0 ; i < [o_children count] ; i++ )
738 [[o_children objectAtIndex:i] applyChanges];
744 @implementation VLCFlippedView