]> git.sesse.net Git - vlc/blob - modules/gui/macosx/prefs.m
macosx: video effects panel: synchronize text field and stepper values properly by...
[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     else {
160         _o_sharedMainInstance = [super init];
161         p_intf = VLCIntf;
162         o_empty_view = [[NSView alloc] init];
163         _rootTreeItem = [[VLCTreeMainItem alloc] init];
164     }
165
166     return _o_sharedMainInstance;
167 }
168
169 - (void)dealloc
170 {
171     [o_empty_view release];
172     [_rootTreeItem release];
173     [super dealloc];
174 }
175
176 - (void)awakeFromNib
177 {
178     p_intf = VLCIntf;
179
180     if (!OSX_SNOW_LEOPARD)
181         [o_prefs_window setCollectionBehavior: NSWindowCollectionBehaviorFullScreenAuxiliary];
182
183     [self initStrings];
184     [o_prefs_view setBorderType: NSGrooveBorder];
185     [o_prefs_view setHasVerticalScroller: YES];
186     [o_prefs_view setDrawsBackground: NO];
187     [o_prefs_view setDocumentView: o_empty_view];
188     [o_tree selectRowIndexes: [NSIndexSet indexSetWithIndex: 0] byExtendingSelection: NO];
189 }
190
191 - (void)setTitle: (NSString *) o_title_name
192 {
193     [o_title setStringValue: o_title_name];
194 }
195
196 - (void)showPrefsWithLevel:(NSInteger)i_window_level
197 {
198     [o_prefs_window setLevel: i_window_level];
199     [o_prefs_window center];
200     [o_prefs_window makeKeyAndOrderFront:self];
201     [_rootTreeItem resetView];
202 }
203
204 - (void)initStrings
205 {
206     [o_prefs_window setTitle: _NS("Preferences")];
207     [o_save_btn setTitle: _NS("Save")];
208     [o_cancel_btn setTitle: _NS("Cancel")];
209     [o_reset_btn setTitle: _NS("Reset All")];
210     [o_showBasic_btn setTitle: _NS("Show Basic")];
211 }
212
213 - (IBAction)savePrefs: (id)sender
214 {
215     /* TODO: call savePrefs on Root item */
216     [_rootTreeItem applyChanges];
217     [[VLCCoreInteraction sharedInstance] fixPreferences];
218     config_SaveConfigFile(p_intf);
219     [o_prefs_window orderOut:self];
220 }
221
222 - (IBAction)closePrefs: (id)sender
223 {
224     [o_prefs_window orderOut:self];
225 }
226
227 - (IBAction)resetAll: (id)sender
228 {
229     NSBeginInformationalAlertSheet(_NS("Reset Preferences"), _NS("Cancel"),
230         _NS("Continue"), nil, o_prefs_window, self,
231         @selector(sheetDidEnd: returnCode: contextInfo:), NULL, nil, @"%@",
232         _NS("Beware this will reset the VLC media player preferences.\n"
233             "Are you sure you want to continue?"));
234 }
235
236 - (void)sheetDidEnd:(NSWindow *)o_sheet returnCode:(int)i_return
237     contextInfo:(void *)o_context
238 {
239     if (i_return == NSAlertAlternateReturn) {
240         /* reset VLC's config */
241         config_ResetAll(p_intf);
242         [_rootTreeItem resetView];
243         config_SaveConfigFile(p_intf);
244
245         /* reset OS X defaults */
246         [NSUserDefaults resetStandardUserDefaults];
247         [[NSUserDefaults standardUserDefaults] synchronize];
248     }
249 }
250
251 - (IBAction)buttonAction: (id)sender
252 {
253     [o_prefs_window orderOut: self];
254     [[[VLCMain sharedInstance] simplePreferences] showSimplePrefs];
255 }
256
257 - (void)loadConfigTree
258 {
259 }
260
261 - (void)outlineViewSelectionIsChanging:(NSNotification *)o_notification
262 {
263 }
264
265 /* update the document view to the view of the selected tree item */
266 - (void)outlineViewSelectionDidChange:(NSNotification *)o_notification
267 {
268     [[o_tree itemAtRow:[o_tree selectedRow]] showView: o_prefs_view];
269     [o_tree expandItem:[o_tree itemAtRow:[o_tree selectedRow]]];
270 }
271
272 @end
273
274 @implementation VLCPrefs (NSTableDataSource)
275
276 - (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
277 {
278     return (item == nil) ? [_rootTreeItem numberOfChildren] : [item numberOfChildren];
279 }
280
281 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
282 {
283     return (item == nil) ? [_rootTreeItem numberOfChildren] : [item numberOfChildren];
284 }
285
286 - (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item
287 {
288     return (item == nil) ? (id)[_rootTreeItem childAtIndex:index]: (id)[item childAtIndex:index];
289 }
290
291 - (id)outlineView:(NSOutlineView *)outlineView
292     objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
293 {
294     return (item == nil) ? @"" : [item name];
295 }
296
297 @end
298
299 #pragma mark -
300 @implementation VLCTreeMainItem
301
302 - (VLCTreeCategoryItem *)itemRepresentingCategory:(int)category
303 {
304     NSUInteger childrenCount = [[self children] count];
305     for (int i = 0; i < childrenCount; i++) {
306         VLCTreeCategoryItem * categoryItem = [[self children] objectAtIndex:i];
307         if ([categoryItem category] == category)
308             return categoryItem;
309     }
310     return nil;
311 }
312
313 - (bool)isSubCategoryGeneral:(int)category
314 {
315     if (category == SUBCAT_VIDEO_GENERAL ||
316           category == SUBCAT_ADVANCED_MISC ||
317           category == SUBCAT_INPUT_GENERAL ||
318           category == SUBCAT_INTERFACE_GENERAL ||
319           category == SUBCAT_SOUT_GENERAL||
320           category == SUBCAT_PLAYLIST_GENERAL||
321           category == SUBCAT_AUDIO_GENERAL) {
322         return true;
323     }
324     return false;
325 }
326
327 /* Creates and returns the array of children
328  * Loads children incrementally */
329 - (NSMutableArray *)children
330 {
331     if (_children) return _children;
332     _children = [[NSMutableArray alloc] init];
333
334     intf_thread_t   *p_intf = VLCIntf;
335
336     /* List the modules */
337     size_t count, i;
338     module_t ** modules = module_list_get(&count);
339     if (!modules) return nil;
340
341     /* Build a tree of the plugins */
342     /* Add the capabilities */
343     for (i = 0; i < count; i++) {
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             int configType = p_configs[j].i_type;
368             if (configType == CONFIG_CATEGORY) {
369                 categoryItem = [self itemRepresentingCategory:p_configs[j].value.i];
370                 if (!categoryItem) {
371                     categoryItem = [VLCTreeCategoryItem categoryTreeItemWithCategory:p_configs[j].value.i];
372                     if (categoryItem)
373                         [[self children] addObject:categoryItem];
374                 }
375             }
376             else if (configType == CONFIG_SUBCATEGORY) {
377                 lastsubcat = p_configs[j].value.i;
378                 if (categoryItem && ![self isSubCategoryGeneral:lastsubcat]) {
379                     subCategoryItem = [categoryItem itemRepresentingSubCategory:lastsubcat];
380                     if (!subCategoryItem) {
381                         subCategoryItem = [VLCTreeSubCategoryItem subCategoryTreeItemWithSubCategory:lastsubcat];
382                         if (subCategoryItem)
383                             [[categoryItem children] addObject:subCategoryItem];
384                     }
385                 }
386             }
387
388             if (module_is_main(p_module) && CONFIG_ITEM(configType)) {
389                 if (categoryItem && [self isSubCategoryGeneral:lastsubcat])
390                     [[categoryItem options] addObject:[[VLCTreeLeafItem alloc] initWithConfigItem:&p_configs[j]]];
391                 else if (subCategoryItem)
392                     [[subCategoryItem options] addObject:[[VLCTreeLeafItem alloc] initWithConfigItem:&p_configs[j]]];
393             }
394             else if (!module_is_main(p_module) && CONFIG_ITEM(configType)) {
395                 if (![[subCategoryItem children] containsObject: pluginItem])
396                     [[subCategoryItem children] addObject:pluginItem];
397
398                 if (pluginItem)
399                     [[pluginItem options] addObject:[[VLCTreeLeafItem alloc] initWithConfigItem:&p_configs[j]]];
400             }
401         }
402     }
403     module_list_free(modules);
404     return _children;
405 }
406 @end
407
408 #pragma mark -
409 @implementation VLCTreeCategoryItem
410 + (VLCTreeCategoryItem *)categoryTreeItemWithCategory:(int)category
411 {
412     if (!config_CategoryNameGet(category)) return nil;
413     return [[[[self class] alloc] initWithCategory:category] autorelease];
414 }
415
416 - (id)initWithCategory:(int)category
417 {
418     NSString * name = _NS(config_CategoryNameGet(category));
419     if (self = [super initWithName:name]) {
420         _category = category;
421         //_help = [_NS(config_CategoryHelpGet(category)) retain];
422     }
423     return self;
424 }
425
426 - (VLCTreeSubCategoryItem *)itemRepresentingSubCategory:(int)subCategory
427 {
428     assert([self isKindOfClass:[VLCTreeCategoryItem class]]);
429     NSUInteger childrenCount = [[self children] count];
430     for (NSUInteger i = 0; i < childrenCount; i++) {
431         VLCTreeSubCategoryItem * subCategoryItem = [[self children] objectAtIndex:i];
432         if ([subCategoryItem subCategory] == subCategory)
433             return subCategoryItem;
434     }
435     return nil;
436 }
437
438 - (int)category
439 {
440     return _category;
441 }
442 @end
443
444 #pragma mark -
445 @implementation VLCTreeSubCategoryItem
446 - (id)initWithSubCategory:(int)subCategory
447 {
448     NSString * name = _NS(config_CategoryNameGet(subCategory));
449     if (self = [super initWithName:name]) {
450         _subCategory = subCategory;
451         //_help = [_NS(config_CategoryHelpGet(subCategory)) retain];
452     }
453     return self;
454 }
455
456 + (VLCTreeSubCategoryItem *)subCategoryTreeItemWithSubCategory:(int)subCategory
457 {
458     if (!config_CategoryNameGet(subCategory))
459         return nil;
460     return [[[[self class] alloc] initWithSubCategory:subCategory] autorelease];
461 }
462
463 - (int)subCategory
464 {
465     return _subCategory;
466 }
467
468 @end
469
470 #pragma mark -
471 @implementation VLCTreePluginItem
472 - (id)initWithPlugin:(module_t *)plugin
473 {
474     NSString * name = _NS(module_get_name(plugin, false)?:"");
475     if (self = [super initWithName:name]) {
476         _configItems = module_config_get(plugin, &_configSize);
477         //_plugin = plugin;
478         //_help = [_NS(config_CategoryHelpGet(subCategory)) retain];
479     }
480     return self;
481 }
482
483 + (VLCTreePluginItem *)pluginTreeItemWithPlugin:(module_t *)plugin
484 {
485     return [[[[self class] alloc] initWithPlugin:plugin] autorelease];
486 }
487
488 - (void)dealloc
489 {
490     module_config_free(_configItems);
491     [super dealloc];
492 }
493
494 - (module_config_t *)configItems
495 {
496     return _configItems;
497 }
498
499 - (unsigned int)configSize
500 {
501     return _configSize;
502 }
503
504 @end
505
506 #pragma mark -
507 @implementation VLCTreeLeafItem
508
509 - (id)initWithConfigItem: (module_config_t *) configItem
510 {
511     NSString * name = _NS(configItem->psz_name);
512     self = [super initWithName:name];
513     [name release];
514     if (self != nil)
515         _configItem = configItem;
516
517     return self;
518 }
519
520 - (module_config_t *)configItem
521 {
522     return _configItem;
523 }
524 @end
525
526 #pragma mark -
527 #pragma mark (Root class for all TreeItems)
528 @implementation VLCTreeItem
529
530 - (id)initWithName:(NSString*)name
531 {
532     self = [super init];
533     if (self != nil)
534         _name = [name retain];
535
536     return self;
537 }
538
539 - (void)dealloc
540 {
541     [_children release];
542     [_options release];
543     [_name release];
544     [_subviews release];
545     [super dealloc];
546 }
547
548 - (VLCTreeItem *)childAtIndex:(NSInteger)i_index
549 {
550     return [[self children] objectAtIndex:i_index];
551 }
552
553 - (int)numberOfChildren
554 {
555     return [[self children] count];
556 }
557
558 - (NSString *)name
559 {
560     return [[_name retain] autorelease];
561 }
562
563 - (void)showView:(NSScrollView *)prefsView
564 {
565     NSRect          s_vrc;
566     NSView          *view;
567
568     [[VLCPrefs sharedInstance] setTitle: [self name]];
569     s_vrc = [[prefsView contentView] bounds]; s_vrc.size.height -= 4;
570     view = [[VLCFlippedView alloc] initWithFrame: s_vrc];
571     [view setAutoresizingMask: NSViewWidthSizable | NSViewMinYMargin | NSViewMaxYMargin];
572
573     if (!_subviews) {
574         _subviews = [[NSMutableArray alloc] initWithCapacity:10];
575
576         NSUInteger count = [[self options] count];
577         for (NSUInteger i = 0; i < count; i++) {
578             VLCTreeLeafItem * item = [[self options] objectAtIndex:i];
579
580             VLCConfigControl *control;
581             control = [VLCConfigControl newControl:[item configItem] withView:view];
582             if (control) {
583                 [control setAutoresizingMask: NSViewMaxYMargin | NSViewWidthSizable];
584                 [_subviews addObject: control];
585             }
586         }
587     }
588
589     assert(view);
590
591     int i_lastItem = 0;
592     int i_yPos = -2;
593     int i_max_label = 0;
594
595     NSEnumerator *enumerator = [_subviews objectEnumerator];
596     VLCConfigControl *widget;
597     NSRect frame;
598
599     while((widget = [enumerator nextObject])) {
600         if (i_max_label < [widget labelSize])
601             i_max_label = [widget labelSize];
602     }
603
604     enumerator = [_subviews objectEnumerator];
605     while((widget = [enumerator nextObject])) {
606         int i_widget;
607
608         i_widget = [widget viewType];
609         i_yPos += [VLCConfigControl calcVerticalMargin:i_widget lastItem:i_lastItem];
610         [widget setYPos:i_yPos];
611         frame = [widget frame];
612         frame.size.width = [view frame].size.width - LEFTMARGIN - RIGHTMARGIN;
613         [widget setFrame:frame];
614         [widget alignWithXPosition: i_max_label];
615         i_yPos += [widget frame].size.height;
616         i_lastItem = i_widget;
617         [view addSubview:widget];
618     }
619
620     frame = [view frame];
621     frame.size.height = i_yPos;
622     [view setFrame:frame];
623     [prefsView setDocumentView:view];
624 }
625
626 - (void)applyChanges
627 {
628     NSUInteger i;
629     NSUInteger count = [_subviews count];
630     for (i = 0 ; i < count ; i++)
631         [[_subviews objectAtIndex:i] applyChanges];
632
633     count = [_children count];
634     for (i = 0 ; i < count ; i++)
635         [[_children objectAtIndex:i] applyChanges];
636 }
637
638 - (void)resetView
639 {
640     NSUInteger count = [_subviews count];
641     for (NSUInteger i = 0 ; i < count ; i++)
642         [[_subviews objectAtIndex:i] resetValues];
643
644     count = [_options count];
645     for (NSUInteger i = 0 ; i < count ; i++)
646         [[_options objectAtIndex:i] resetView];
647
648     count = [_children count];
649     for (NSUInteger i = 0 ; i < count ; i++)
650         [[_children objectAtIndex:i] resetView];
651
652 }
653
654 - (NSMutableArray *)children
655 {
656     if (!_children)
657         _children = [[NSMutableArray alloc] init];
658     return _children;
659 }
660
661 - (NSMutableArray *)options
662 {
663     if (!_options)
664         _options = [[NSMutableArray alloc] init];
665     return _options;
666 }
667 @end
668
669 #pragma mark -
670 @implementation VLCFlippedView
671
672 - (BOOL)isFlipped
673 {
674     return(YES);
675 }
676
677 @end