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 - a redraw view action
34 - the children action should generate a list of the treeitems children (to be used by VLCPrefs datasource)
36 The class is sort of a mix of wxwindows intfs, PrefsTreeCtrl and ConfigTreeData
38 /* VLCConfigControl are subclassed NSView's containing and managing individual config items
39 the classes are VERY closely related to wxwindows ConfigControls */
41 /*****************************************************************************
43 *****************************************************************************/
44 #include <stdlib.h> /* malloc(), free() */
45 #include <sys/param.h> /* for MAXPATHLEN */
52 #include <vlc_common.h>
53 #include <vlc_config_cat.h>
57 #import "simple_prefs.h"
58 #import "prefs_widgets.h"
61 /* /!\ Warning: Unreadable code :/ */
63 @interface VLCTreeItem : NSObject
68 vlc_object_t * _vlc_object;
69 VLCTreeItem *o_parent;
70 NSMutableArray *o_children;
71 int i_object_category;
72 NSMutableArray *o_subviews;
75 - (id)initWithName: (NSString *)o_item_name
76 withTitle: (NSString *)o_item_title
77 withHelp: (NSString *)o_item_help
78 withObject: (vlc_object_t *)object
79 parent:(VLCTreeItem *)o_parent_item
80 children:(NSMutableArray *)o_children_array
81 whithCategory: (int) i_category;
83 + (VLCTreeItem *)rootItem;
84 - (int)numberOfChildren;
85 - (VLCTreeItem *)childAtIndex:(int)i_index;
86 - (vlc_object_t*)vlcObject;
90 - (BOOL)hasPrefs:(NSString *)o_module_name;
91 - (NSView *)showView:(NSScrollView *)o_prefs_view;
99 /*****************************************************************************
100 * VLCPrefs implementation
101 *****************************************************************************/
102 @implementation VLCPrefs
104 static VLCPrefs *_o_sharedMainInstance = nil;
106 + (VLCPrefs *)sharedInstance
108 return _o_sharedMainInstance ? _o_sharedMainInstance : [[self alloc] init];
113 if( _o_sharedMainInstance ) {
118 _o_sharedMainInstance = [super init];
120 o_empty_view = [[NSView alloc] init];
123 return _o_sharedMainInstance;
128 [o_empty_view release];
137 [o_prefs_view setBorderType: NSGrooveBorder];
138 [o_prefs_view setHasVerticalScroller: YES];
139 [o_prefs_view setDrawsBackground: NO];
140 [o_prefs_view setDocumentView: o_empty_view];
141 [o_tree selectRow:0 byExtendingSelection:NO];
144 - (void)setTitle: (NSString *) o_title_name
146 [o_title setStringValue: o_title_name];
151 [[o_basicFull_matrix cellAtRow:0 column:0] setState: NSOffState];
152 [[o_basicFull_matrix cellAtRow:0 column:1] setState: NSOnState];
154 [o_prefs_window center];
155 [o_prefs_window makeKeyAndOrderFront:self];
160 [o_prefs_window setTitle: _NS("Preferences")];
161 [o_save_btn setTitle: _NS("Save")];
162 [o_cancel_btn setTitle: _NS("Cancel")];
163 [o_reset_btn setTitle: _NS("Reset All")];
164 [[o_basicFull_matrix cellAtRow: 0 column: 0] setStringValue: _NS("Basic")];
165 [[o_basicFull_matrix cellAtRow: 0 column: 1] setStringValue: _NS("All")];
168 - (IBAction)savePrefs: (id)sender
170 /* TODO: call savePrefs on Root item */
171 [[VLCTreeItem rootItem] applyChanges];
172 config_SaveConfigFile( p_intf, NULL );
173 [o_prefs_window orderOut:self];
176 - (IBAction)closePrefs: (id)sender
178 [o_prefs_window orderOut:self];
181 - (IBAction)resetAll: (id)sender
183 NSBeginInformationalAlertSheet(_NS("Reset Preferences"), _NS("Cancel"),
184 _NS("Continue"), nil, o_prefs_window, self,
185 @selector(sheetDidEnd: returnCode: contextInfo:), NULL, nil,
186 _NS("Beware this will reset the VLC media player preferences.\n"
187 "Are you sure you want to continue?") );
190 - (void)sheetDidEnd:(NSWindow *)o_sheet returnCode:(int)i_return
191 contextInfo:(void *)o_context
193 if( i_return == NSAlertAlternateReturn )
195 [o_prefs_view setDocumentView: o_empty_view];
196 config_ResetAll( p_intf );
197 [[VLCTreeItem rootItem] resetView];
198 [[o_tree itemAtRow:[o_tree selectedRow]]
199 showView:o_prefs_view];
203 - (IBAction)buttonAction: (id)sender
205 [o_prefs_window orderOut: self];
206 [[o_basicFull_matrix cellAtRow:0 column:0] setState: NSOnState];
207 [[o_basicFull_matrix cellAtRow:0 column:1] setState: NSOffState];
208 [[[VLCMain sharedInstance] getSimplePreferences] showSimplePrefs];
211 - (void)loadConfigTree
215 - (void)outlineViewSelectionIsChanging:(NSNotification *)o_notification
219 /* update the document view to the view of the selected tree item */
220 - (void)outlineViewSelectionDidChange:(NSNotification *)o_notification
222 [[o_tree itemAtRow:[o_tree selectedRow]] showView: o_prefs_view];
227 @implementation VLCPrefs (NSTableDataSource)
229 - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
230 return (item == nil) ? [[VLCTreeItem rootItem] numberOfChildren] :
231 [item numberOfChildren];
234 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
236 return (item == nil) ? YES : ( ([item numberOfChildren] != -1) &&
237 ([item numberOfChildren] != 0));
240 - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item {
241 return (item == nil) ? [[VLCTreeItem rootItem] childAtIndex:index] :
242 (id)[item childAtIndex:index];
245 - (id)outlineView:(NSOutlineView *)outlineView
246 objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
248 return (item == nil) ? @"" : (id)[item name];
253 @implementation VLCTreeItem
255 static VLCTreeItem *o_root_item = nil;
257 #define IsALeafNode ((id)-1)
259 - (id)initWithName: (NSString *)o_item_name
260 withTitle: (NSString *)o_item_title
261 withHelp: (NSString *)o_item_help
262 withObject: (vlc_object_t *)object
263 parent:(VLCTreeItem *)o_parent_item
264 children:(NSMutableArray *)o_children_array
265 whithCategory: (int) i_category
271 o_name = [o_item_name copy];
272 o_title= [o_item_title copy];
273 o_help= [o_item_help copy];
274 _vlc_object = object ? vlc_object_hold( object ) : NULL;
275 o_parent = o_parent_item;
276 o_children = o_children_array;
277 i_object_category = i_category;
283 + (VLCTreeItem *)rootItem
285 if (o_root_item == nil)
286 o_root_item = [[VLCTreeItem alloc] initWithName:@"main" withTitle:@"main" withHelp:@"" withObject:NULL
287 parent:nil children:[[NSMutableArray alloc] initWithCapacity:10]
294 if(_vlc_object) vlc_object_release( _vlc_object );
295 if (o_children != IsALeafNode) [o_children release];
302 /* Creates and returns the array of children
303 * Loads children incrementally */
304 - (NSArray *)children
306 if( o_children == IsALeafNode )
308 if( [ o_children count] == 0 )
310 intf_thread_t *p_intf = VLCIntf;
312 module_t *p_module = NULL;
313 module_t *p_main_module;
314 module_config_t *p_items;
315 if( [[self name] isEqualToString: @"main"] )
317 p_main_module = module_get_main( p_intf );
318 assert( p_main_module );
320 /* We found the main module */
321 /* Enumerate config categories and store a reference so we can
322 * generate their config panel them when it is asked by the user. */
323 VLCTreeItem *p_last_category = NULL;
324 unsigned int i_confsize;
325 p_items = module_config_get( p_main_module, &i_confsize );
326 o_children = [[NSMutableArray alloc] initWithCapacity:10];
327 for( int i = 0; i < i_confsize; i++ )
329 NSString *o_child_name;
330 NSString *o_child_title;
331 NSString *o_child_help;
333 switch( p_items[i].i_type )
335 case CONFIG_CATEGORY:
336 if( p_items[i].value.i == -1 ) break;
338 o_child_name = [[VLCMain sharedInstance]
339 localizedString: config_CategoryNameGet( p_items[i].value.i )];
340 o_child_title = o_child_name;
341 o_child_help = [[VLCMain sharedInstance]
342 localizedString: config_CategoryHelpGet( p_items[i].value.i )];
343 p_last_category = [VLCTreeItem alloc];
344 [o_children addObject:[p_last_category
345 initWithName: o_child_name
346 withTitle: o_child_title
347 withHelp: o_child_help
348 withObject: (vlc_object_t*)p_main_module
350 children:[[NSMutableArray alloc]
352 whithCategory: p_items[i].value.i]];
354 case CONFIG_SUBCATEGORY:
355 if( p_items[i].value.i == -1 ) break;
357 if( p_items[i].value.i != SUBCAT_PLAYLIST_GENERAL &&
358 p_items[i].value.i != SUBCAT_VIDEO_GENERAL &&
359 p_items[i].value.i != SUBCAT_INPUT_GENERAL &&
360 p_items[i].value.i != SUBCAT_INTERFACE_GENERAL &&
361 p_items[i].value.i != SUBCAT_SOUT_GENERAL &&
362 p_items[i].value.i != SUBCAT_ADVANCED_MISC &&
363 p_items[i].value.i != SUBCAT_AUDIO_GENERAL )
365 o_child_name = [[VLCMain sharedInstance]
366 localizedString: config_CategoryNameGet( p_items[i].value.i ) ];
367 o_child_title = o_child_name;
368 o_child_help = [[VLCMain sharedInstance]
369 localizedString: config_CategoryHelpGet( p_items[i].value.i ) ];
371 [p_last_category->o_children
372 addObject:[[VLCTreeItem alloc]
373 initWithName: o_child_name
374 withTitle: o_child_title
375 withHelp: o_child_help
376 withObject: (vlc_object_t*)p_main_module
377 parent:p_last_category
378 children:[[NSMutableArray alloc]
380 whithCategory: p_items[i].value.i]];
389 vlc_object_release( (vlc_object_t *)p_main_module );
391 /* List the modules */
392 p_list = module_list_get( NULL );
393 if( !p_list ) return nil;
395 /* Build a tree of the plugins */
396 /* Add the capabilities */
397 for( size_t i = 0; p_list[i]; i++ )
399 unsigned int confsize;
400 p_module = p_list[i];
402 /* Exclude the main module */
403 if( module_is_main( p_module ) )
406 /* Exclude empty plugins (submodules don't have config */
407 /* options, they are stored in the parent module) */
408 p_items = module_config_get( p_module, &confsize );
413 int i_subcategory = -1;
416 for( j = 0; j < confsize; j++ )
418 if( p_items[j].i_type == CONFIG_CATEGORY )
419 i_category = p_items[j].value.i;
420 else if( p_items[j].i_type == CONFIG_SUBCATEGORY )
421 i_subcategory = p_items[j].value.i;
423 if( p_items[j].i_type & CONFIG_ITEM )
426 if( b_item && i_category >= 0 && i_subcategory >= 0 )
430 if( !b_item ) continue;
432 /* Find the right category item */
435 bool b_found = false;
437 VLCTreeItem* p_category_item, * p_subcategory_item;
438 for (j = 0 ; j < [o_children count] ; j++)
440 p_category_item = [o_children objectAtIndex: j];
441 if( p_category_item->i_object_category == i_category )
447 if( !b_found ) continue;
449 /* Find subcategory item */
452 for (j = 0 ; j < [p_category_item->o_children count] ; j++)
454 p_subcategory_item = [p_category_item->o_children
456 if( p_subcategory_item->i_object_category == i_subcategory )
463 p_subcategory_item = p_category_item;
465 [p_subcategory_item->o_children addObject:[[VLCTreeItem alloc]
466 initWithName:[[VLCMain sharedInstance]
467 localizedString: module_get_name( p_module, false ) ]
468 withTitle:[[VLCMain sharedInstance]
469 localizedString: module_GetLongName( p_module ) ]
471 withObject: (vlc_object_t*)p_main_module
472 parent:p_subcategory_item
476 module_list_free( p_list );
482 - (vlc_object_t *)vlcObject
484 return vlc_object_hold(_vlc_object);
489 return [[o_name retain] autorelease];
494 return [[o_title retain] autorelease];
499 return [[o_help retain] autorelease];
502 - (VLCTreeItem *)childAtIndex:(int)i_index
504 return [[self children] objectAtIndex:i_index];
507 - (int)numberOfChildren {
508 id i_tmp = [self children];
509 return (i_tmp == IsALeafNode) ? (-1) : (int)[i_tmp count];
512 - (BOOL)hasPrefs:(NSString *)o_module_name
514 unsigned int confsize;
516 intf_thread_t *p_intf = VLCIntf;
519 const char *psz_module_name = (char *)[o_module_name UTF8String];
521 /* look for module */
522 p_parser = module_find( p_intf, psz_module_name );
526 module_config_get( p_parser, &confsize );
527 BOOL b_has_prefs = confsize != 0;
528 module_release( p_parser );
529 return( b_has_prefs );
532 - (NSView *)showView:(NSScrollView *)o_prefs_view
537 [[VLCPrefs sharedInstance] setTitle: [self title]];
538 /* NSLog( [self getHelp] ); */
539 s_vrc = [[o_prefs_view contentView] bounds]; s_vrc.size.height -= 4;
540 o_view = [[VLCFlippedView alloc] initWithFrame: s_vrc];
541 [o_view setAutoresizingMask: NSViewWidthSizable | NSViewMinYMargin |
544 /* Create all subviews if it isn't already done because we cannot use */
545 /* setHiden for MacOS < 10.3*/
546 if( o_subviews == nil )
548 intf_thread_t *p_intf = VLCIntf;
550 module_t *p_module = NULL;
551 module_t *p_main_module;
552 module_config_t *p_items;
553 unsigned int confsize;
555 o_subviews = [[NSMutableArray alloc] initWithCapacity:10];
556 /* Get a pointer to the module */
557 if( i_object_category == -1 )
559 p_module = (module_t *) [self vlcObject];
562 p_items = module_config_get( p_module, &confsize );
564 for( unsigned int i = 0; i < confsize; i++ )
566 switch( p_items[i].i_type )
568 case CONFIG_SUBCATEGORY:
569 case CONFIG_CATEGORY:
571 case CONFIG_HINT_USAGE:
575 VLCConfigControl *o_control = nil;
576 o_control = [VLCConfigControl newControl:&p_items[i]
580 [o_control setAutoresizingMask: NSViewMaxYMargin |
582 [o_subviews addObject: o_control];
588 vlc_object_release( (vlc_object_t*)p_module );
592 p_main_module = module_get_main( p_intf );
593 assert( p_main_module );
594 module_config_t *p_items;
596 unsigned int i, confsize;
597 p_items = module_config_get( p_main_module, &confsize );
599 /* We need to first, find the right (sub)category,
600 * and then abort when we find a new (sub)category. Part of the Ugliness. */
601 bool in_right_category = false;
602 bool in_subcategory = false;
604 for( i = 0; i < confsize; i++ )
606 if( !p_items[i].i_type )
608 msg_Err( p_intf, "invalid preference item found" );
612 switch( p_items[i].i_type )
614 case CONFIG_CATEGORY:
615 if(!in_right_category && p_items[i].value.i == i_object_category)
616 in_right_category = true;
617 else if(in_right_category)
620 case CONFIG_SUBCATEGORY:
621 if(!in_right_category && p_items[i].value.i == i_object_category)
623 in_right_category = true;
624 in_subcategory = true;
626 else if(in_right_category && in_subcategory)
630 case CONFIG_HINT_USAGE:
634 if(!in_right_category) break;
636 VLCConfigControl *o_control = nil;
637 o_control = [VLCConfigControl newControl:&p_items[i]
639 if( o_control != nil )
641 [o_control setAutoresizingMask: NSViewMaxYMargin |
643 [o_subviews addObject: o_control];
650 vlc_object_release( (vlc_object_t*)p_main_module );
660 NSEnumerator *enumerator = [o_subviews objectEnumerator];
661 VLCConfigControl *o_widget;
664 while( ( o_widget = [enumerator nextObject] ) )
665 if( i_max_label < [o_widget getLabelSize] )
666 i_max_label = [o_widget getLabelSize];
668 enumerator = [o_subviews objectEnumerator];
669 while( ( o_widget = [enumerator nextObject] ) )
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];
687 o_frame = [o_view frame];
688 o_frame.size.height = i_yPos;
689 [o_view setFrame:o_frame];
690 [o_prefs_view setDocumentView:o_view];
699 if( o_subviews != nil )
700 //Item has been shown
701 for( i = 0 ; i < [o_subviews count] ; i++ )
702 [[o_subviews objectAtIndex:i] applyChanges];
704 if( o_children != IsALeafNode )
705 for( i = 0 ; i < [o_children count] ; i++ )
706 [[o_children objectAtIndex:i] applyChanges];
712 if( o_subviews != nil )
714 //Item has been shown
715 [o_subviews release];
719 if( o_children != IsALeafNode )
720 for( i = 0 ; i < [o_children count] ; i++ )
721 [[o_children objectAtIndex:i] resetView];
727 @implementation VLCFlippedView