]> git.sesse.net Git - vlc/blob - modules/gui/macosx/prefs.m
macosx: don't use Carbon to retrieve icons from the OS
[vlc] / modules / gui / macosx / prefs.m
1 /*****************************************************************************
2  * prefs.m: MacOS X module for vlc
3  *****************************************************************************
4  * Copyright (C) 2002-2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
8  *          Derk-Jan Hartman <hartman at videolan dot org>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
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)
35
36    The class is sort of a mix of wxwindows intfs, PrefsTreeCtrl and ConfigTreeData
37 */
38 /* VLCConfigControl are subclassed NSView's containing and managing individual config items
39    the classes are VERY closely related to wxwindows ConfigControls */
40
41 /*****************************************************************************
42  * Preamble
43  *****************************************************************************/
44 #include <stdlib.h>                                      /* malloc(), free() */
45 #include <sys/param.h>                                    /* for MAXPATHLEN */
46 #include <string.h>
47
48 #ifdef HAVE_CONFIG_H
49 # include "config.h"
50 #endif
51
52 #include <vlc_common.h>
53 #include <vlc_config_cat.h>
54
55 #import "intf.h"
56 #import "prefs.h"
57 #import "simple_prefs.h"
58 #import "prefs_widgets.h"
59 #import <vlc_keys.h>
60 #import <vlc_modules.h>
61
62 /* /!\ Warning: Unreadable code :/ */
63
64 @interface VLCTreeItem : NSObject
65 {
66     NSString *_name;
67     NSMutableArray *_children;
68     NSMutableArray *_options;
69     NSMutableArray *_subviews;
70 }
71
72 - (id)initWithName:(NSString*)name;
73
74 - (int)numberOfChildren;
75 - (VLCTreeItem *)childAtIndex:(NSInteger)i_index;
76
77 - (NSString *)name;
78 - (NSMutableArray *)children;
79 - (NSMutableArray *)options;
80 - (void)showView:(NSScrollView *)o_prefs_view;
81 - (void)applyChanges;
82 - (void)resetView;
83
84 @end
85
86 /* CONFIG_SUBCAT */
87 @interface VLCTreeSubCategoryItem : VLCTreeItem
88 {
89     int _subCategory;
90 }
91 + (VLCTreeSubCategoryItem *)subCategoryTreeItemWithSubCategory:(int)subCategory;
92 - (id)initWithSubCategory:(int)subCategory;
93 - (int)subCategory;
94 @end
95
96 /* Plugin daughters */
97 @interface VLCTreePluginItem : VLCTreeItem
98 {
99     module_config_t * _configItems;
100     unsigned int _configSize;
101 }
102 + (VLCTreePluginItem *)pluginTreeItemWithPlugin:(module_t *)plugin;
103 - (id)initWithPlugin:(module_t *)plugin;
104
105 - (module_config_t *)configItems;
106 - (unsigned int)configSize;
107 @end
108
109 /* CONFIG_CAT */
110 @interface VLCTreeCategoryItem : VLCTreeItem
111 {
112     int _category;
113 }
114 + (VLCTreeCategoryItem *)categoryTreeItemWithCategory:(int)category;
115 - (id)initWithCategory:(int)category;
116
117 - (int)category;
118 - (VLCTreeSubCategoryItem *)itemRepresentingSubCategory:(int)category;
119 @end
120
121 /* individual options. */
122 @interface VLCTreeLeafItem : VLCTreeItem
123 {
124     module_config_t * _configItem;
125 }
126 - (id)initWithConfigItem:(module_config_t *)configItem;
127
128 - (module_config_t *)configItem;
129 @end
130
131 @interface VLCTreeMainItem : VLCTreePluginItem
132 {
133 }
134 - (VLCTreeCategoryItem *)itemRepresentingCategory:(int)category;
135 @end
136
137 #pragma mark -
138
139 /*****************************************************************************
140  * VLCPrefs implementation
141  *****************************************************************************/
142 @implementation VLCPrefs
143
144 static VLCPrefs *_o_sharedMainInstance = nil;
145
146 + (VLCPrefs *)sharedInstance
147 {
148     return _o_sharedMainInstance ? _o_sharedMainInstance : [[self alloc] init];
149 }
150
151 - (id)init
152 {
153     if( _o_sharedMainInstance ) {
154         [self dealloc];
155     }
156     else
157     {
158         _o_sharedMainInstance = [super init];
159         p_intf = VLCIntf;
160         o_empty_view = [[NSView alloc] init];
161         _rootTreeItem = [[VLCTreeMainItem alloc] init];
162     }
163
164     return _o_sharedMainInstance;
165 }
166
167 - (void)dealloc
168 {
169     [o_empty_view release];
170     [_rootTreeItem release];
171     [super dealloc];
172 }
173
174 - (void)awakeFromNib
175 {
176     p_intf = VLCIntf;
177
178     [self initStrings];
179     [o_prefs_view setBorderType: NSGrooveBorder];
180     [o_prefs_view setHasVerticalScroller: YES];
181     [o_prefs_view setDrawsBackground: NO];
182     [o_prefs_view setDocumentView: o_empty_view];
183         [o_tree selectRowIndexes: [NSIndexSet indexSetWithIndex: 0] byExtendingSelection: NO];
184 }
185
186 - (void)setTitle: (NSString *) o_title_name
187 {
188     [o_title setStringValue: o_title_name];
189 }
190
191 - (void)showPrefs
192 {
193     [[o_basicFull_matrix cellAtRow:0 column:0] setState: NSOffState];
194     [[o_basicFull_matrix cellAtRow:0 column:1] setState: NSOnState];
195     
196     [o_prefs_window center];
197     [o_prefs_window makeKeyAndOrderFront:self];
198     [_rootTreeItem resetView];
199 }
200
201 - (void)initStrings
202 {
203     [o_prefs_window setTitle: _NS("Preferences")];
204     [o_save_btn setTitle: _NS("Save")];
205     [o_cancel_btn setTitle: _NS("Cancel")];
206     [o_reset_btn setTitle: _NS("Reset All")];
207     [[o_basicFull_matrix cellAtRow: 0 column: 0] setStringValue: _NS("Basic")];
208     [[o_basicFull_matrix cellAtRow: 0 column: 1] setStringValue: _NS("All")];
209 }
210
211 - (IBAction)savePrefs: (id)sender
212 {
213     /* TODO: call savePrefs on Root item */
214     [_rootTreeItem applyChanges];
215     config_SaveConfigFile( p_intf );
216     [o_prefs_window orderOut:self];
217 }
218
219 - (IBAction)closePrefs: (id)sender
220 {
221     [o_prefs_window orderOut:self];
222 }
223
224 - (IBAction)resetAll: (id)sender
225 {
226     NSBeginInformationalAlertSheet(_NS("Reset Preferences"), _NS("Cancel"),
227         _NS("Continue"), nil, o_prefs_window, self,
228         @selector(sheetDidEnd: returnCode: contextInfo:), NULL, nil,
229         _NS("Beware this will reset the VLC media player preferences.\n"
230             "Are you sure you want to continue?") );
231 }
232
233 - (void)sheetDidEnd:(NSWindow *)o_sheet returnCode:(int)i_return
234     contextInfo:(void *)o_context
235 {
236     if( i_return == NSAlertAlternateReturn )
237     {
238         config_ResetAll( p_intf );
239         [_rootTreeItem resetView];
240     }
241 }
242
243 - (IBAction)buttonAction: (id)sender
244 {
245     [o_prefs_window orderOut: self];
246     [[o_basicFull_matrix cellAtRow:0 column:0] setState: NSOnState];
247     [[o_basicFull_matrix cellAtRow:0 column:1] setState: NSOffState];
248     [[[VLCMain sharedInstance] simplePreferences] showSimplePrefs];
249 }
250
251 - (void)loadConfigTree
252 {
253 }
254
255 - (void)outlineViewSelectionIsChanging:(NSNotification *)o_notification
256 {
257 }
258
259 /* update the document view to the view of the selected tree item */
260 - (void)outlineViewSelectionDidChange:(NSNotification *)o_notification
261 {
262     [[o_tree itemAtRow:[o_tree selectedRow]] showView: o_prefs_view];
263     [o_tree expandItem:[o_tree itemAtRow:[o_tree selectedRow]]];
264 }
265
266 @end
267
268 @implementation VLCPrefs (NSTableDataSource)
269
270 - (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
271 {
272     return (item == nil) ? [_rootTreeItem numberOfChildren] : [item numberOfChildren];
273 }
274
275 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
276 {
277     return (item == nil) ? [_rootTreeItem numberOfChildren] : [item numberOfChildren];
278 }
279
280 - (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item
281 {
282     return (item == nil) ? (id)[_rootTreeItem childAtIndex:index]: (id)[item childAtIndex:index];
283 }
284
285 - (id)outlineView:(NSOutlineView *)outlineView
286     objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
287 {
288     return (item == nil) ? @"" : [item name];
289 }
290
291 @end
292
293 #pragma mark -
294 @implementation VLCTreeMainItem
295
296 - (VLCTreeCategoryItem *)itemRepresentingCategory:(int)category
297 {
298     for( int i = 0; i < [[self children] count]; i++ )
299     {
300         VLCTreeCategoryItem * categoryItem = [[self children] objectAtIndex:i];
301         if( [categoryItem category] == category )
302             return categoryItem;
303     }
304     return nil;
305 }
306
307 - (bool)isSubCategoryGeneral:(int)category
308 {
309     if(category == SUBCAT_VIDEO_GENERAL ||
310           category == SUBCAT_ADVANCED_MISC ||
311           category == SUBCAT_INPUT_GENERAL ||
312           category == SUBCAT_INTERFACE_GENERAL ||
313           category == SUBCAT_SOUT_GENERAL||
314           category == SUBCAT_PLAYLIST_GENERAL||
315           category == SUBCAT_AUDIO_GENERAL )
316     {
317         return true;
318     }
319     return false;
320 }
321
322 /* Creates and returns the array of children
323  * Loads children incrementally */
324 - (NSMutableArray *)children
325 {
326     if( _children ) return _children;
327     _children = [[NSMutableArray alloc] init];
328
329     intf_thread_t   *p_intf = VLCIntf;
330
331     /* List the modules */
332     size_t count, i;
333     module_t ** modules = module_list_get( &count );
334     if( !modules ) return nil;
335
336     /* Build a tree of the plugins */
337     /* Add the capabilities */
338     for( i = 0; i < count; i++ )
339     {
340         VLCTreeCategoryItem * categoryItem = nil;
341         VLCTreeSubCategoryItem * subCategoryItem = nil;
342         VLCTreePluginItem * pluginItem = nil;
343         module_config_t *p_configs = NULL;
344         int lastsubcat = 0;
345         unsigned int confsize;
346
347         module_t * p_module = modules[i];
348
349         /* Exclude empty plugins (submodules don't have config */
350         /* options, they are stored in the parent module) */
351         if( module_is_main( p_module ) ) {
352             pluginItem = self;
353             _configItems = module_config_get( p_module, &confsize );
354             _configSize = confsize;
355         } else {
356             pluginItem = [VLCTreePluginItem pluginTreeItemWithPlugin: p_module];
357             confsize = [pluginItem configSize];
358         }
359         p_configs = [pluginItem configItems];
360
361         unsigned int j;
362         for( j = 0; j < confsize; j++ )
363         {
364             int configType = p_configs[j].i_type;
365             if( configType == CONFIG_CATEGORY )
366             {
367                 categoryItem = [self itemRepresentingCategory:p_configs[j].value.i];
368                 if(!categoryItem)
369                 {
370                     categoryItem = [VLCTreeCategoryItem categoryTreeItemWithCategory:p_configs[j].value.i];
371                     if(categoryItem) [[self children] addObject:categoryItem];
372                 }
373             }
374             else if( configType == CONFIG_SUBCATEGORY )
375             {
376                 lastsubcat = p_configs[j].value.i;
377                 if( categoryItem && ![self isSubCategoryGeneral:lastsubcat] )
378                 {
379                     subCategoryItem = [categoryItem itemRepresentingSubCategory:lastsubcat];
380                     if(!subCategoryItem)
381                     {
382                         subCategoryItem = [VLCTreeSubCategoryItem subCategoryTreeItemWithSubCategory:lastsubcat];
383                         if(subCategoryItem) [[categoryItem children] addObject:subCategoryItem];
384                     }
385                 }
386             }
387             
388             if( module_is_main( p_module) && CONFIG_ITEM(configType) )
389             {
390                 if( categoryItem && [self isSubCategoryGeneral:lastsubcat] )
391                 {
392                     [[categoryItem options] addObject:[[VLCTreeLeafItem alloc] initWithConfigItem:&p_configs[j]]];
393                 }
394                 else if( subCategoryItem )
395                 {
396                     [[subCategoryItem options] addObject:[[VLCTreeLeafItem alloc] initWithConfigItem:&p_configs[j]]];
397                 }
398             }
399             else if( !module_is_main( p_module) && CONFIG_ITEM(configType))
400             {
401                 if( ![[subCategoryItem children] containsObject: pluginItem] )
402                 {
403                     [[subCategoryItem children] addObject:pluginItem];
404                 }
405                 if( pluginItem )
406                     [[pluginItem options] addObject:[[VLCTreeLeafItem alloc] initWithConfigItem:&p_configs[j]]];
407             }
408         }
409     }
410     module_list_free( modules );
411     return _children;
412 }
413 @end
414
415 #pragma mark -
416 @implementation VLCTreeCategoryItem
417 + (VLCTreeCategoryItem *)categoryTreeItemWithCategory:(int)category
418 {
419     if(!config_CategoryNameGet( category )) return nil;
420     return [[[[self class] alloc] initWithCategory:category] autorelease];
421 }
422 - (id)initWithCategory:(int)category
423 {
424     NSString * name = [[VLCMain sharedInstance] localizedString: config_CategoryNameGet( category )];
425     if(self = [super initWithName:name])
426     {
427         _category = category;
428         //_help = [[[VLCMain sharedInstance] localizedString: config_CategoryHelpGet( category )] retain];
429     }
430     return self;
431 }
432
433 - (VLCTreeSubCategoryItem *)itemRepresentingSubCategory:(int)subCategory
434 {
435     assert( [self isKindOfClass:[VLCTreeCategoryItem class]] );
436     for( int i = 0; i < [[self children] count]; i++ )
437     {
438         VLCTreeSubCategoryItem * subCategoryItem = [[self children] objectAtIndex:i];
439         if( [subCategoryItem subCategory] == subCategory )
440             return subCategoryItem;
441     }
442     return nil;
443 }
444
445 - (int)category
446 {
447     return _category;
448 }
449 @end
450
451 #pragma mark -
452 @implementation VLCTreeSubCategoryItem
453 - (id)initWithSubCategory:(int)subCategory
454 {
455     NSString * name = [[VLCMain sharedInstance] localizedString: config_CategoryNameGet( subCategory )];
456     if(self = [super initWithName:name])
457     {
458         _subCategory = subCategory;
459         //_help = [[[VLCMain sharedInstance] localizedString: config_CategoryHelpGet( subCategory )] retain];
460     }
461     return self;
462 }
463
464 + (VLCTreeSubCategoryItem *)subCategoryTreeItemWithSubCategory:(int)subCategory
465 {
466     if(!config_CategoryNameGet( subCategory )) return nil;
467     return [[[[self class] alloc] initWithSubCategory:subCategory] autorelease];
468 }
469
470 - (int)subCategory
471 {
472     return _subCategory;
473 }
474
475 @end
476
477 #pragma mark -
478 @implementation VLCTreePluginItem
479 - (id)initWithPlugin:(module_t *)plugin
480 {
481     NSString * name = [[VLCMain sharedInstance] localizedString: module_get_name( plugin, false )?:""];
482     if(self = [super initWithName:name])
483     {
484         _configItems = module_config_get( plugin, &_configSize );
485         //_plugin = plugin;
486         //_help = [[[VLCMain sharedInstance] localizedString: config_CategoryHelpGet( subCategory )] retain];
487     }
488     return self;
489 }
490
491 + (VLCTreePluginItem *)pluginTreeItemWithPlugin:(module_t *)plugin
492 {
493     return [[[[self class] alloc] initWithPlugin:plugin] autorelease];
494 }
495
496 - (void)dealloc
497 {
498     module_config_free( _configItems );
499     [super dealloc];
500 }
501
502 - (module_config_t *)configItems
503 {
504     return _configItems;
505 }
506
507 - (unsigned int)configSize
508 {
509     return _configSize;
510 }
511
512 @end
513
514 #pragma mark -
515 @implementation VLCTreeLeafItem
516
517 - (id)initWithConfigItem: (module_config_t *) configItem
518 {
519     NSString * name = [[VLCMain sharedInstance] localizedString:configItem->psz_name];
520     self = [super initWithName:name];
521     [name release];
522     if( self != nil )
523     {
524         _configItem = configItem;
525     }
526     return self;
527 }
528
529 - (module_config_t *)configItem
530 {
531     return _configItem;
532 }
533 @end
534
535 #pragma mark -
536 #pragma mark (Root class for all TreeItems)
537 @implementation VLCTreeItem
538
539 - (id)initWithName:(NSString*)name
540 {
541     self = [super init];
542     if( self != nil )
543     {
544         _name = [name retain];
545     }
546     return self;
547 }
548
549 - (void)dealloc
550 {
551     [_children release];
552     [_options release];
553     [_name release];
554     [_subviews release];
555     [super dealloc];
556 }
557
558 - (VLCTreeItem *)childAtIndex:(NSInteger)i_index
559 {
560     return [[self children] objectAtIndex:i_index];
561 }
562
563 - (int)numberOfChildren
564 {
565     return [[self children] count];
566 }
567
568 - (NSString *)name
569 {
570     return [[_name retain] autorelease];
571 }
572
573 - (void)showView:(NSScrollView *)prefsView
574 {
575     NSRect          s_vrc;
576     NSView          *view;
577
578     [[VLCPrefs sharedInstance] setTitle: [self name]];
579     s_vrc = [[prefsView contentView] bounds]; s_vrc.size.height -= 4;
580     view = [[VLCFlippedView alloc] initWithFrame: s_vrc];
581     [view setAutoresizingMask: NSViewWidthSizable | NSViewMinYMargin | NSViewMaxYMargin];
582
583     if(!_subviews)
584     {
585         _subviews = [[NSMutableArray alloc] initWithCapacity:10];
586
587         long i;
588         for( i = 0; i < [[self options] count]; i++)
589         {
590             VLCTreeLeafItem * item = [[self options] objectAtIndex:i];
591
592             VLCConfigControl *control;
593             control = [VLCConfigControl newControl:[item configItem] withView:view];
594             if( control )
595             {
596                 [control setAutoresizingMask: NSViewMaxYMargin | NSViewWidthSizable];
597                 [_subviews addObject: control];
598             }
599         }
600     }
601
602     assert(view);
603     
604     int i_lastItem = 0;
605     int i_yPos = -2;
606     int i_max_label = 0;
607
608     NSEnumerator *enumerator = [_subviews objectEnumerator];
609     VLCConfigControl *widget;
610     NSRect frame;
611
612     while( ( widget = [enumerator nextObject] ) )
613         if( i_max_label < [widget labelSize] )
614             i_max_label = [widget labelSize];
615
616     enumerator = [_subviews objectEnumerator];
617     while( ( widget = [enumerator nextObject] ) )
618     {
619         int i_widget;
620
621         i_widget = [widget viewType];
622         i_yPos += [VLCConfigControl calcVerticalMargin:i_widget lastItem:i_lastItem];
623         [widget setYPos:i_yPos];
624         frame = [widget frame];
625         frame.size.width = [view frame].size.width - LEFTMARGIN - RIGHTMARGIN;
626         [widget setFrame:frame];
627         [widget alignWithXPosition: i_max_label];
628         i_yPos += [widget frame].size.height;
629         i_lastItem = i_widget;
630         [view addSubview:widget];
631     }
632
633     frame = [view frame];
634     frame.size.height = i_yPos;
635     [view setFrame:frame];
636     [prefsView setDocumentView:view];
637 }
638
639 - (void)applyChanges
640 {
641     unsigned int i;
642     for( i = 0 ; i < [_subviews count] ; i++ )
643         [[_subviews objectAtIndex:i] applyChanges];
644
645     for( i = 0 ; i < [_children count] ; i++ )
646         [[_children objectAtIndex:i] applyChanges];
647 }
648
649 - (void)resetView
650 {
651     unsigned int i;
652     for( i = 0 ; i < [_subviews count] ; i++ )
653         [[_subviews objectAtIndex:i] resetValues];
654
655     for( i = 0 ; i < [_options count] ; i++ )
656         [[_options objectAtIndex:i] resetView];
657
658     for( i = 0 ; i < [_children count] ; i++ )
659         [[_children objectAtIndex:i] resetView];
660
661 }
662
663 - (NSMutableArray *)children
664 {
665     if(!_children) _children = [[NSMutableArray alloc] init];
666     return _children;
667 }
668
669 - (NSMutableArray *)options
670 {
671     if(!_options) _options = [[NSMutableArray alloc] init];
672     return _options;
673 }
674 @end
675
676 #pragma mark -
677 @implementation VLCFlippedView
678
679 - (BOOL)isFlipped
680 {
681     return( YES );
682 }
683
684 @end