]> git.sesse.net Git - vlc/blob - modules/gui/macosx/prefs.m
macosx: use position formatter for start and stop time in open panel
[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)buttonAction: (id)sender
228 {
229     [o_prefs_window orderOut: self];
230     [[[VLCMain sharedInstance] simplePreferences] showSimplePrefs];
231 }
232
233 - (void)loadConfigTree
234 {
235 }
236
237 - (void)outlineViewSelectionIsChanging:(NSNotification *)o_notification
238 {
239 }
240
241 /* update the document view to the view of the selected tree item */
242 - (void)outlineViewSelectionDidChange:(NSNotification *)o_notification
243 {
244     [[o_tree itemAtRow:[o_tree selectedRow]] showView: o_prefs_view];
245     [o_tree expandItem:[o_tree itemAtRow:[o_tree selectedRow]]];
246 }
247
248 @end
249
250 @implementation VLCPrefs (NSTableDataSource)
251
252 - (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
253 {
254     return (item == nil) ? [_rootTreeItem numberOfChildren] : [item numberOfChildren];
255 }
256
257 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
258 {
259     return (item == nil) ? [_rootTreeItem numberOfChildren] : [item numberOfChildren];
260 }
261
262 - (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item
263 {
264     return (item == nil) ? (id)[_rootTreeItem childAtIndex:index]: (id)[item childAtIndex:index];
265 }
266
267 - (id)outlineView:(NSOutlineView *)outlineView
268     objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
269 {
270     return (item == nil) ? @"" : [item name];
271 }
272
273 @end
274
275 #pragma mark -
276 @implementation VLCTreeMainItem
277
278 - (VLCTreeCategoryItem *)itemRepresentingCategory:(int)category
279 {
280     NSUInteger childrenCount = [[self children] count];
281     for (int i = 0; i < childrenCount; i++) {
282         VLCTreeCategoryItem * categoryItem = [self children][i];
283         if ([categoryItem category] == category)
284             return categoryItem;
285     }
286     return nil;
287 }
288
289 - (bool)isSubCategoryGeneral:(int)category
290 {
291     if (category == SUBCAT_VIDEO_GENERAL ||
292           category == SUBCAT_ADVANCED_MISC ||
293           category == SUBCAT_INPUT_GENERAL ||
294           category == SUBCAT_INTERFACE_GENERAL ||
295           category == SUBCAT_SOUT_GENERAL||
296           category == SUBCAT_PLAYLIST_GENERAL||
297           category == SUBCAT_AUDIO_GENERAL) {
298         return true;
299     }
300     return false;
301 }
302
303 /* Creates and returns the array of children
304  * Loads children incrementally */
305 - (NSMutableArray *)children
306 {
307     if (_children) return _children;
308     _children = [[NSMutableArray alloc] init];
309
310     intf_thread_t   *p_intf = VLCIntf;
311
312     /* List the modules */
313     size_t count, i;
314     module_t ** modules = module_list_get(&count);
315     if (!modules) return nil;
316
317     /* Build a tree of the plugins */
318     /* Add the capabilities */
319     for (i = 0; i < count; i++) {
320         VLCTreeCategoryItem * categoryItem = nil;
321         VLCTreeSubCategoryItem * subCategoryItem = nil;
322         VLCTreePluginItem * pluginItem = nil;
323         module_config_t *p_configs = NULL;
324         int lastsubcat = 0;
325         unsigned int confsize;
326
327         module_t * p_module = modules[i];
328
329         /* Exclude empty plugins (submodules don't have config */
330         /* options, they are stored in the parent module) */
331         if (module_is_main(p_module)) {
332             pluginItem = self;
333             _configItems = module_config_get(p_module, &confsize);
334             _configSize = confsize;
335         } else {
336             pluginItem = [VLCTreePluginItem pluginTreeItemWithPlugin: p_module];
337             confsize = [pluginItem configSize];
338         }
339         p_configs = [pluginItem configItems];
340
341         unsigned int j;
342         for (j = 0; j < confsize; j++) {
343             int configType = p_configs[j].i_type;
344             if (configType == CONFIG_CATEGORY) {
345                 categoryItem = [self itemRepresentingCategory:p_configs[j].value.i];
346                 if (!categoryItem) {
347                     categoryItem = [VLCTreeCategoryItem categoryTreeItemWithCategory:p_configs[j].value.i];
348                     if (categoryItem)
349                         [[self children] addObject:categoryItem];
350                 }
351             }
352             else if (configType == CONFIG_SUBCATEGORY) {
353                 lastsubcat = p_configs[j].value.i;
354                 if (categoryItem && ![self isSubCategoryGeneral:lastsubcat]) {
355                     subCategoryItem = [categoryItem itemRepresentingSubCategory:lastsubcat];
356                     if (!subCategoryItem) {
357                         subCategoryItem = [VLCTreeSubCategoryItem subCategoryTreeItemWithSubCategory:lastsubcat];
358                         if (subCategoryItem)
359                             [[categoryItem children] addObject:subCategoryItem];
360                     }
361                 }
362             }
363
364             if (module_is_main(p_module) && (CONFIG_ITEM(configType) || configType == CONFIG_SECTION)) {
365                 if (categoryItem && [self isSubCategoryGeneral:lastsubcat])
366                     [[categoryItem options] addObject:[[VLCTreeLeafItem alloc] initWithConfigItem:&p_configs[j]]];
367                 else if (subCategoryItem)
368                     [[subCategoryItem options] addObject:[[VLCTreeLeafItem alloc] initWithConfigItem:&p_configs[j]]];
369             }
370             else if (!module_is_main(p_module) && (CONFIG_ITEM(configType) || configType == CONFIG_SECTION)) {
371                 if (![[subCategoryItem children] containsObject: pluginItem])
372                     [[subCategoryItem children] addObject:pluginItem];
373
374                 if (pluginItem)
375                     [[pluginItem options] addObject:[[VLCTreeLeafItem alloc] initWithConfigItem:&p_configs[j]]];
376             }
377         }
378     }
379     module_list_free(modules);
380     return _children;
381 }
382 @end
383
384 #pragma mark -
385 @implementation VLCTreeCategoryItem
386 + (VLCTreeCategoryItem *)categoryTreeItemWithCategory:(int)category
387 {
388     if (!config_CategoryNameGet(category)) return nil;
389     return [[[[self class] alloc] initWithCategory:category] autorelease];
390 }
391
392 - (id)initWithCategory:(int)category
393 {
394     NSString * name = _NS(config_CategoryNameGet(category));
395     if (self = [super initWithName:name]) {
396         _category = category;
397         //_help = [_NS(config_CategoryHelpGet(category)) retain];
398     }
399     return self;
400 }
401
402 - (VLCTreeSubCategoryItem *)itemRepresentingSubCategory:(int)subCategory
403 {
404     assert([self isKindOfClass:[VLCTreeCategoryItem class]]);
405     NSUInteger childrenCount = [[self children] count];
406     for (NSUInteger i = 0; i < childrenCount; i++) {
407         VLCTreeSubCategoryItem * subCategoryItem = [self children][i];
408         if ([subCategoryItem subCategory] == subCategory)
409             return subCategoryItem;
410     }
411     return nil;
412 }
413
414 - (int)category
415 {
416     return _category;
417 }
418 @end
419
420 #pragma mark -
421 @implementation VLCTreeSubCategoryItem
422 - (id)initWithSubCategory:(int)subCategory
423 {
424     NSString * name = _NS(config_CategoryNameGet(subCategory));
425     if (self = [super initWithName:name]) {
426         _subCategory = subCategory;
427         //_help = [_NS(config_CategoryHelpGet(subCategory)) retain];
428     }
429     return self;
430 }
431
432 + (VLCTreeSubCategoryItem *)subCategoryTreeItemWithSubCategory:(int)subCategory
433 {
434     if (!config_CategoryNameGet(subCategory))
435         return nil;
436     return [[[[self class] alloc] initWithSubCategory:subCategory] autorelease];
437 }
438
439 - (int)subCategory
440 {
441     return _subCategory;
442 }
443
444 @end
445
446 #pragma mark -
447 @implementation VLCTreePluginItem
448 - (id)initWithPlugin:(module_t *)plugin
449 {
450     NSString * name = _NS(module_get_name(plugin, false));
451     if (self = [super initWithName:name]) {
452         _configItems = module_config_get(plugin, &_configSize);
453         //_plugin = plugin;
454         //_help = [_NS(config_CategoryHelpGet(subCategory)) retain];
455     }
456     return self;
457 }
458
459 + (VLCTreePluginItem *)pluginTreeItemWithPlugin:(module_t *)plugin
460 {
461     return [[[[self class] alloc] initWithPlugin:plugin] autorelease];
462 }
463
464 - (void)dealloc
465 {
466     module_config_free(_configItems);
467     [super dealloc];
468 }
469
470 - (module_config_t *)configItems
471 {
472     return _configItems;
473 }
474
475 - (unsigned int)configSize
476 {
477     return _configSize;
478 }
479
480 @end
481
482 #pragma mark -
483 @implementation VLCTreeLeafItem
484
485 - (id)initWithConfigItem: (module_config_t *) configItem
486 {
487     NSString * name = _NS(configItem->psz_name);
488     self = [super initWithName:name];
489     [name release];
490     if (self != nil)
491         _configItem = configItem;
492
493     return self;
494 }
495
496 - (module_config_t *)configItem
497 {
498     return _configItem;
499 }
500 @end
501
502 #pragma mark -
503 #pragma mark (Root class for all TreeItems)
504 @implementation VLCTreeItem
505
506 - (id)initWithName:(NSString*)name
507 {
508     self = [super init];
509     if (self != nil)
510         _name = [name retain];
511
512     return self;
513 }
514
515 - (void)dealloc
516 {
517     [_children release];
518     [_options release];
519     [_name release];
520     [_subviews release];
521     [super dealloc];
522 }
523
524 - (VLCTreeItem *)childAtIndex:(NSInteger)i_index
525 {
526     return [self children][i_index];
527 }
528
529 - (int)numberOfChildren
530 {
531     return [[self children] count];
532 }
533
534 - (NSString *)name
535 {
536     return [[_name retain] autorelease];
537 }
538
539 - (void)showView:(NSScrollView *)prefsView
540 {
541     NSRect          s_vrc;
542     NSView          *view;
543
544     [[VLCPrefs sharedInstance] setTitle: [self name]];
545     s_vrc = [[prefsView contentView] bounds]; s_vrc.size.height -= 4;
546     view = [[VLCFlippedView alloc] initWithFrame: s_vrc];
547     [view setAutoresizingMask: NSViewWidthSizable | NSViewMinYMargin | NSViewMaxYMargin];
548
549     if (!_subviews) {
550         _subviews = [[NSMutableArray alloc] initWithCapacity:10];
551
552         NSUInteger count = [[self options] count];
553         for (NSUInteger i = 0; i < count; i++) {
554             VLCTreeLeafItem * item = [self options][i];
555
556             VLCConfigControl *control;
557             control = [VLCConfigControl newControl:[item configItem] withView:view];
558             if (control) {
559                 [control setAutoresizingMask: NSViewMaxYMargin | NSViewWidthSizable];
560                 [_subviews addObject: control];
561             }
562         }
563     }
564
565     assert(view);
566
567     int i_lastItem = 0;
568     int i_yPos = -2;
569     int i_max_label = 0;
570
571     NSEnumerator *enumerator = [_subviews objectEnumerator];
572     VLCConfigControl *widget;
573     NSRect frame;
574
575     while((widget = [enumerator nextObject])) {
576         if (i_max_label < [widget labelSize])
577             i_max_label = [widget labelSize];
578     }
579
580     enumerator = [_subviews objectEnumerator];
581     while((widget = [enumerator nextObject])) {
582         int i_widget;
583
584         i_widget = [widget viewType];
585         i_yPos += [VLCConfigControl calcVerticalMargin:i_widget lastItem:i_lastItem];
586         [widget setYPos:i_yPos];
587         frame = [widget frame];
588         frame.size.width = [view frame].size.width - LEFTMARGIN - RIGHTMARGIN;
589         [widget setFrame:frame];
590         [widget alignWithXPosition: i_max_label];
591         i_yPos += [widget frame].size.height;
592         i_lastItem = i_widget;
593         [view addSubview:widget];
594     }
595
596     frame = [view frame];
597     frame.size.height = i_yPos;
598     [view setFrame:frame];
599     [prefsView setDocumentView:view];
600 }
601
602 - (void)applyChanges
603 {
604     NSUInteger i;
605     NSUInteger count = [_subviews count];
606     for (i = 0 ; i < count ; i++)
607         [_subviews[i] applyChanges];
608
609     count = [_children count];
610     for (i = 0 ; i < count ; i++)
611         [_children[i] applyChanges];
612 }
613
614 - (void)resetView
615 {
616     NSUInteger count = [_subviews count];
617     for (NSUInteger i = 0 ; i < count ; i++)
618         [_subviews[i] resetValues];
619
620     count = [_options count];
621     for (NSUInteger i = 0 ; i < count ; i++)
622         [_options[i] resetView];
623
624     count = [_children count];
625     for (NSUInteger i = 0 ; i < count ; i++)
626         [_children[i] resetView];
627
628 }
629
630 - (NSMutableArray *)children
631 {
632     if (!_children)
633         _children = [[NSMutableArray alloc] init];
634     return _children;
635 }
636
637 - (NSMutableArray *)options
638 {
639     if (!_options)
640         _options = [[NSMutableArray alloc] init];
641     return _options;
642 }
643 @end
644
645 #pragma mark -
646 @implementation VLCFlippedView
647
648 - (BOOL)isFlipped
649 {
650     return(YES);
651 }
652
653 @end