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