]> git.sesse.net Git - vlc/blob - modules/gui/macosx/prefs.m
Use KiB instead of kB for input statistics
[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
61 /* /!\ Warning: Unreadable code :/ */
62
63 @interface VLCTreeItem : NSObject
64 {
65     NSString *_name;
66     NSMutableArray *_children;
67     NSMutableArray *_options;
68     NSMutableArray *_subviews;
69 }
70
71 - (id)initWithName:(NSString*)name;
72
73 - (int)numberOfChildren;
74 - (VLCTreeItem *)childAtIndex:(NSInteger)i_index;
75
76 - (NSString *)name;
77 - (NSMutableArray *)children;
78 - (NSMutableArray *)options;
79 - (void)showView:(NSScrollView *)o_prefs_view;
80 - (void)applyChanges;
81 - (void)resetView;
82
83 @end
84
85 /* CONFIG_SUBCAT */
86 @interface VLCTreeSubCategoryItem : VLCTreeItem
87 {
88     int _subCategory;
89 }
90 + (VLCTreeSubCategoryItem *)subCategoryTreeItemWithSubCategory:(int)subCategory;
91 - (id)initWithSubCategory:(int)subCategory;
92 - (int)subCategory;
93 @end
94
95 /* Plugin daughters */
96 @interface VLCTreePluginItem : VLCTreeItem
97 {
98     module_config_t * _configItems;
99     unsigned int _configSize;
100 }
101 + (VLCTreePluginItem *)pluginTreeItemWithPlugin:(module_t *)plugin;
102 - (id)initWithPlugin:(module_t *)plugin;
103
104 - (module_config_t *)configItems;
105 - (unsigned int)configSize;
106 @end
107
108 /* CONFIG_CAT */
109 @interface VLCTreeCategoryItem : VLCTreeItem
110 {
111     int _category;
112 }
113 + (VLCTreeCategoryItem *)categoryTreeItemWithCategory:(int)category;
114 - (id)initWithCategory:(int)category;
115
116 - (int)category;
117 - (VLCTreeSubCategoryItem *)itemRepresentingSubCategory:(int)category;
118 @end
119
120 /* individual options. */
121 @interface VLCTreeLeafItem : VLCTreeItem
122 {
123     module_config_t * _configItem;
124 }
125 - (id)initWithConfigItem:(module_config_t *)configItem;
126
127 - (module_config_t *)configItem;
128 @end
129
130 @interface VLCTreeMainItem : VLCTreePluginItem
131 {
132 }
133 - (VLCTreeCategoryItem *)itemRepresentingCategory:(int)category;
134 @end
135
136 #pragma mark -
137
138 /*****************************************************************************
139  * VLCPrefs implementation
140  *****************************************************************************/
141 @implementation VLCPrefs
142
143 static VLCPrefs *_o_sharedMainInstance = nil;
144
145 + (VLCPrefs *)sharedInstance
146 {
147     return _o_sharedMainInstance ? _o_sharedMainInstance : [[self alloc] init];
148 }
149
150 - (id)init
151 {
152     if( _o_sharedMainInstance ) {
153         [self dealloc];
154     }
155     else
156     {
157         _o_sharedMainInstance = [super init];
158         p_intf = VLCIntf;
159         o_empty_view = [[NSView alloc] init];
160         _rootTreeItem = [[VLCTreeMainItem alloc] init];
161     }
162
163     return _o_sharedMainInstance;
164 }
165
166 - (void)dealloc
167 {
168     [o_empty_view release];
169     [_rootTreeItem release];
170     [super dealloc];
171 }
172
173 - (void)awakeFromNib
174 {
175     p_intf = VLCIntf;
176
177     [self initStrings];
178     [o_prefs_view setBorderType: NSGrooveBorder];
179     [o_prefs_view setHasVerticalScroller: YES];
180     [o_prefs_view setDrawsBackground: NO];
181     [o_prefs_view setDocumentView: o_empty_view];
182         [o_tree selectRowIndexes: [NSIndexSet indexSetWithIndex: 0] byExtendingSelection: NO];
183 }
184
185 - (void)setTitle: (NSString *) o_title_name
186 {
187     [o_title setStringValue: o_title_name];
188 }
189
190 - (void)showPrefs
191 {
192     [[o_basicFull_matrix cellAtRow:0 column:0] setState: NSOffState];
193     [[o_basicFull_matrix cellAtRow:0 column:1] setState: NSOnState];
194     
195     [o_prefs_window center];
196     [o_prefs_window makeKeyAndOrderFront:self];
197     [_rootTreeItem resetView];
198 }
199
200 - (void)initStrings
201 {
202     [o_prefs_window setTitle: _NS("Preferences")];
203     [o_save_btn setTitle: _NS("Save")];
204     [o_cancel_btn setTitle: _NS("Cancel")];
205     [o_reset_btn setTitle: _NS("Reset All")];
206     [[o_basicFull_matrix cellAtRow: 0 column: 0] setStringValue: _NS("Basic")];
207     [[o_basicFull_matrix cellAtRow: 0 column: 1] setStringValue: _NS("All")];
208 }
209
210 - (IBAction)savePrefs: (id)sender
211 {
212     /* TODO: call savePrefs on Root item */
213     [_rootTreeItem applyChanges];
214     config_SaveConfigFile( p_intf, NULL );
215     [o_prefs_window orderOut:self];
216 }
217
218 - (IBAction)closePrefs: (id)sender
219 {
220     [o_prefs_window orderOut:self];
221 }
222
223 - (IBAction)resetAll: (id)sender
224 {
225     NSBeginInformationalAlertSheet(_NS("Reset Preferences"), _NS("Cancel"),
226         _NS("Continue"), nil, o_prefs_window, self,
227         @selector(sheetDidEnd: returnCode: contextInfo:), NULL, nil,
228         _NS("Beware this will reset the VLC media player preferences.\n"
229             "Are you sure you want to continue?") );
230 }
231
232 - (void)sheetDidEnd:(NSWindow *)o_sheet returnCode:(int)i_return
233     contextInfo:(void *)o_context
234 {
235     if( i_return == NSAlertAlternateReturn )
236     {
237         config_ResetAll( p_intf );
238         [_rootTreeItem resetView];
239     }
240 }
241
242 - (IBAction)buttonAction: (id)sender
243 {
244     [o_prefs_window orderOut: self];
245     [[o_basicFull_matrix cellAtRow:0 column:0] setState: NSOnState];
246     [[o_basicFull_matrix cellAtRow:0 column:1] setState: NSOffState];
247     [[[VLCMain sharedInstance] simplePreferences] showSimplePrefs];
248 }
249
250 - (void)loadConfigTree
251 {
252 }
253
254 - (void)outlineViewSelectionIsChanging:(NSNotification *)o_notification
255 {
256 }
257
258 /* update the document view to the view of the selected tree item */
259 - (void)outlineViewSelectionDidChange:(NSNotification *)o_notification
260 {
261     [[o_tree itemAtRow:[o_tree selectedRow]] showView: o_prefs_view];
262     [o_tree expandItem:[o_tree itemAtRow:[o_tree selectedRow]]];
263 }
264
265 @end
266
267 @implementation VLCPrefs (NSTableDataSource)
268
269 - (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
270 {
271     return (item == nil) ? [_rootTreeItem numberOfChildren] : [item numberOfChildren];
272 }
273
274 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
275 {
276     return (item == nil) ? [_rootTreeItem numberOfChildren] : [item numberOfChildren];
277 }
278
279 - (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item
280 {
281     return (item == nil) ? (id)[_rootTreeItem childAtIndex:index]: (id)[item childAtIndex:index];
282 }
283
284 - (id)outlineView:(NSOutlineView *)outlineView
285     objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
286 {
287     return (item == nil) ? @"" : [item name];
288 }
289
290 @end
291
292 #pragma mark -
293 @implementation VLCTreeMainItem
294
295 - (VLCTreeCategoryItem *)itemRepresentingCategory:(int)category
296 {
297     for( int i = 0; i < [[self children] count]; i++ )
298     {
299         VLCTreeCategoryItem * categoryItem = [[self children] objectAtIndex:i];
300         if( [categoryItem category] == category )
301             return categoryItem;
302     }
303     return nil;
304 }
305
306 - (bool)isSubCategoryGeneral:(int)category
307 {
308     if(category == SUBCAT_VIDEO_GENERAL ||
309           category == SUBCAT_ADVANCED_MISC ||
310           category == SUBCAT_INPUT_GENERAL ||
311           category == SUBCAT_INTERFACE_GENERAL ||
312           category == SUBCAT_SOUT_GENERAL||
313           category == SUBCAT_PLAYLIST_GENERAL||
314           category == SUBCAT_AUDIO_GENERAL )
315     {
316         return true;
317     }
318     return false;
319 }
320
321 /* Creates and returns the array of children
322  * Loads children incrementally */
323 - (NSMutableArray *)children
324 {
325     if( _children ) return _children;
326     _children = [[NSMutableArray alloc] init];
327
328     intf_thread_t   *p_intf = VLCIntf;
329
330     /* List the modules */
331     size_t count, i;
332     module_t ** modules = module_list_get( &count );
333     if( !modules ) return nil;
334
335     /* Build a tree of the plugins */
336     /* Add the capabilities */
337     for( i = 0; i < count; i++ )
338     {
339         VLCTreeCategoryItem * categoryItem = nil;
340         VLCTreeSubCategoryItem * subCategoryItem = nil;
341         VLCTreePluginItem * pluginItem = nil;
342         module_config_t *p_configs = NULL;
343         int lastsubcat = 0;
344         unsigned int confsize;
345
346         module_t * p_module = modules[i];
347
348         /* Exclude empty plugins (submodules don't have config */
349         /* options, they are stored in the parent module) */
350         if( module_is_main( p_module) )
351         {
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) && (configType & CONFIG_ITEM) )
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) && (configType & CONFIG_ITEM))
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] autorelease];
520     self = [super initWithName:name];
521     if( self != nil )
522     {
523         _configItem = configItem;
524     }
525     return self;
526 }
527
528 - (module_config_t *)configItem
529 {
530     return _configItem;
531 }
532 @end
533
534 #pragma mark -
535 #pragma mark (Root class for all TreeItems)
536 @implementation VLCTreeItem
537
538 - (id)initWithName:(NSString*)name
539 {
540     self = [super init];
541     if( self != nil )
542     {
543         _name = [name retain];
544     }
545     return self;
546 }
547
548 - (void)dealloc
549 {
550     [_children release];
551     [_options release];
552     [_name release];
553     [_subviews release];
554     [super dealloc];
555 }
556
557 - (VLCTreeItem *)childAtIndex:(NSInteger)i_index
558 {
559     return [[self children] objectAtIndex:i_index];
560 }
561
562 - (int)numberOfChildren
563 {
564     return [[self children] count];
565 }
566
567 - (NSString *)name
568 {
569     return [[_name retain] autorelease];
570 }
571
572 - (void)showView:(NSScrollView *)prefsView
573 {
574     NSRect          s_vrc;
575     NSView          *view;
576
577     [[VLCPrefs sharedInstance] setTitle: [self name]];
578     s_vrc = [[prefsView contentView] bounds]; s_vrc.size.height -= 4;
579     view = [[VLCFlippedView alloc] initWithFrame: s_vrc];
580     [view setAutoresizingMask: NSViewWidthSizable | NSViewMinYMargin | NSViewMaxYMargin];
581
582     if(!_subviews)
583     {
584         _subviews = [[NSMutableArray alloc] initWithCapacity:10];
585
586         long i;
587         for( i = 0; i < [[self options] count]; i++)
588         {
589             VLCTreeLeafItem * item = [[self options] objectAtIndex:i];
590
591             VLCConfigControl *control;
592             control = [VLCConfigControl newControl:[item configItem] withView:view];
593             if( control )
594             {
595                 [control setAutoresizingMask: NSViewMaxYMargin | NSViewWidthSizable];
596                 [_subviews addObject: control];
597             }
598         }
599     }
600
601     assert(view);
602     
603     int i_lastItem = 0;
604     int i_yPos = -2;
605     int i_max_label = 0;
606
607     NSEnumerator *enumerator = [_subviews objectEnumerator];
608     VLCConfigControl *widget;
609     NSRect frame;
610
611     while( ( widget = [enumerator nextObject] ) )
612         if( i_max_label < [widget labelSize] )
613             i_max_label = [widget labelSize];
614
615     enumerator = [_subviews objectEnumerator];
616     while( ( widget = [enumerator nextObject] ) )
617     {
618         int i_widget;
619
620         i_widget = [widget viewType];
621         i_yPos += [VLCConfigControl calcVerticalMargin:i_widget lastItem:i_lastItem];
622         [widget setYPos:i_yPos];
623         frame = [widget frame];
624         frame.size.width = [view frame].size.width - LEFTMARGIN - RIGHTMARGIN;
625         [widget setFrame:frame];
626         [widget alignWithXPosition: i_max_label];
627         i_yPos += [widget frame].size.height;
628         i_lastItem = i_widget;
629         [view addSubview:widget];
630     }
631
632     frame = [view frame];
633     frame.size.height = i_yPos;
634     [view setFrame:frame];
635     [prefsView setDocumentView:view];
636 }
637
638 - (void)applyChanges
639 {
640     unsigned int i;
641     for( i = 0 ; i < [_subviews count] ; i++ )
642         [[_subviews objectAtIndex:i] applyChanges];
643
644     for( i = 0 ; i < [_children count] ; i++ )
645         [[_children objectAtIndex:i] applyChanges];
646 }
647
648 - (void)resetView
649 {
650     unsigned int i;
651     for( i = 0 ; i < [_subviews count] ; i++ )
652         [[_subviews objectAtIndex:i] resetValues];
653
654     for( i = 0 ; i < [_options count] ; i++ )
655         [[_options objectAtIndex:i] resetView];
656
657     for( i = 0 ; i < [_children count] ; i++ )
658         [[_children objectAtIndex:i] resetView];
659
660 }
661
662 - (NSMutableArray *)children
663 {
664     if(!_children) _children = [[NSMutableArray alloc] init];
665     return _children;
666 }
667
668 - (NSMutableArray *)options
669 {
670     if(!_options) _options = [[NSMutableArray alloc] init];
671     return _options;
672 }
673 @end
674
675 #pragma mark -
676 @implementation VLCFlippedView
677
678 - (BOOL)isFlipped
679 {
680     return( YES );
681 }
682
683 @end