]> git.sesse.net Git - vlc/blob - modules/gui/macosx/prefs.m
macosx: prevent ugly artifacts when playing a video
[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 "CompatibilityFixes.h"
56 #import "intf.h"
57 #import "prefs.h"
58 #import "simple_prefs.h"
59 #import "prefs_widgets.h"
60 #import <vlc_keys.h>
61 #import <vlc_modules.h>
62
63 /* /!\ Warning: Unreadable code :/ */
64
65 @interface VLCTreeItem : NSObject
66 {
67     NSString *_name;
68     NSMutableArray *_children;
69     NSMutableArray *_options;
70     NSMutableArray *_subviews;
71 }
72
73 - (id)initWithName:(NSString*)name;
74
75 - (int)numberOfChildren;
76 - (VLCTreeItem *)childAtIndex:(NSInteger)i_index;
77
78 - (NSString *)name;
79 - (NSMutableArray *)children;
80 - (NSMutableArray *)options;
81 - (void)showView:(NSScrollView *)o_prefs_view;
82 - (void)applyChanges;
83 - (void)resetView;
84
85 @end
86
87 /* CONFIG_SUBCAT */
88 @interface VLCTreeSubCategoryItem : VLCTreeItem
89 {
90     int _subCategory;
91 }
92 + (VLCTreeSubCategoryItem *)subCategoryTreeItemWithSubCategory:(int)subCategory;
93 - (id)initWithSubCategory:(int)subCategory;
94 - (int)subCategory;
95 @end
96
97 /* Plugin daughters */
98 @interface VLCTreePluginItem : VLCTreeItem
99 {
100     module_config_t * _configItems;
101     unsigned int _configSize;
102 }
103 + (VLCTreePluginItem *)pluginTreeItemWithPlugin:(module_t *)plugin;
104 - (id)initWithPlugin:(module_t *)plugin;
105
106 - (module_config_t *)configItems;
107 - (unsigned int)configSize;
108 @end
109
110 /* CONFIG_CAT */
111 @interface VLCTreeCategoryItem : VLCTreeItem
112 {
113     int _category;
114 }
115 + (VLCTreeCategoryItem *)categoryTreeItemWithCategory:(int)category;
116 - (id)initWithCategory:(int)category;
117
118 - (int)category;
119 - (VLCTreeSubCategoryItem *)itemRepresentingSubCategory:(int)category;
120 @end
121
122 /* individual options. */
123 @interface VLCTreeLeafItem : VLCTreeItem
124 {
125     module_config_t * _configItem;
126 }
127 - (id)initWithConfigItem:(module_config_t *)configItem;
128
129 - (module_config_t *)configItem;
130 @end
131
132 @interface VLCTreeMainItem : VLCTreePluginItem
133 {
134 }
135 - (VLCTreeCategoryItem *)itemRepresentingCategory:(int)category;
136 @end
137
138 #pragma mark -
139
140 /*****************************************************************************
141  * VLCPrefs implementation
142  *****************************************************************************/
143 @implementation VLCPrefs
144
145 static VLCPrefs *_o_sharedMainInstance = nil;
146
147 + (VLCPrefs *)sharedInstance
148 {
149     return _o_sharedMainInstance ? _o_sharedMainInstance : [[self alloc] init];
150 }
151
152 - (id)init
153 {
154     if( _o_sharedMainInstance ) {
155         [self dealloc];
156     }
157     else
158     {
159         _o_sharedMainInstance = [super init];
160         p_intf = VLCIntf;
161         o_empty_view = [[NSView alloc] init];
162         _rootTreeItem = [[VLCTreeMainItem alloc] init];
163     }
164
165     return _o_sharedMainInstance;
166 }
167
168 - (void)dealloc
169 {
170     [o_empty_view release];
171     [_rootTreeItem release];
172     [super dealloc];
173 }
174
175 - (void)awakeFromNib
176 {
177     p_intf = VLCIntf;
178
179     if (OSX_LION)
180         [o_prefs_window setCollectionBehavior: NSWindowCollectionBehaviorFullScreenAuxiliary];
181
182     [self initStrings];
183     [o_prefs_view setBorderType: NSGrooveBorder];
184     [o_prefs_view setHasVerticalScroller: YES];
185     [o_prefs_view setDrawsBackground: NO];
186     [o_prefs_view setDocumentView: o_empty_view];
187         [o_tree selectRowIndexes: [NSIndexSet indexSetWithIndex: 0] byExtendingSelection: NO];
188 }
189
190 - (void)setTitle: (NSString *) o_title_name
191 {
192     [o_title setStringValue: o_title_name];
193 }
194
195 - (void)showPrefs
196 {
197     [o_prefs_window center];
198     [o_prefs_window makeKeyAndOrderFront:self];
199     [_rootTreeItem resetView];
200 }
201
202 - (void)initStrings
203 {
204     [o_prefs_window setTitle: _NS("Preferences")];
205     [o_save_btn setTitle: _NS("Save")];
206     [o_cancel_btn setTitle: _NS("Cancel")];
207     [o_reset_btn setTitle: _NS("Reset All")];
208     [o_showBasic_btn setTitle: _NS("Show Basic")];
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     [[[VLCMain sharedInstance] simplePreferences] showSimplePrefs];
247 }
248
249 - (void)loadConfigTree
250 {
251 }
252
253 - (void)outlineViewSelectionIsChanging:(NSNotification *)o_notification
254 {
255 }
256
257 /* update the document view to the view of the selected tree item */
258 - (void)outlineViewSelectionDidChange:(NSNotification *)o_notification
259 {
260     [[o_tree itemAtRow:[o_tree selectedRow]] showView: o_prefs_view];
261     [o_tree expandItem:[o_tree itemAtRow:[o_tree selectedRow]]];
262 }
263
264 @end
265
266 @implementation VLCPrefs (NSTableDataSource)
267
268 - (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
269 {
270     return (item == nil) ? [_rootTreeItem numberOfChildren] : [item numberOfChildren];
271 }
272
273 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
274 {
275     return (item == nil) ? [_rootTreeItem numberOfChildren] : [item numberOfChildren];
276 }
277
278 - (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item
279 {
280     return (item == nil) ? (id)[_rootTreeItem childAtIndex:index]: (id)[item childAtIndex:index];
281 }
282
283 - (id)outlineView:(NSOutlineView *)outlineView
284     objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
285 {
286     return (item == nil) ? @"" : [item name];
287 }
288
289 @end
290
291 #pragma mark -
292 @implementation VLCTreeMainItem
293
294 - (VLCTreeCategoryItem *)itemRepresentingCategory:(int)category
295 {
296     NSUInteger childrenCount = [[self children] count];
297     for( int i = 0; i < childrenCount; 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             pluginItem = self;
352             _configItems = module_config_get( p_module, &confsize );
353             _configSize = confsize;
354         } else {
355             pluginItem = [VLCTreePluginItem pluginTreeItemWithPlugin: p_module];
356             confsize = [pluginItem configSize];
357         }
358         p_configs = [pluginItem configItems];
359
360         unsigned int j;
361         for( j = 0; j < confsize; j++ )
362         {
363             int configType = p_configs[j].i_type;
364             if( configType == CONFIG_CATEGORY )
365             {
366                 categoryItem = [self itemRepresentingCategory:p_configs[j].value.i];
367                 if(!categoryItem)
368                 {
369                     categoryItem = [VLCTreeCategoryItem categoryTreeItemWithCategory:p_configs[j].value.i];
370                     if(categoryItem) [[self children] addObject:categoryItem];
371                 }
372             }
373             else if( configType == CONFIG_SUBCATEGORY )
374             {
375                 lastsubcat = p_configs[j].value.i;
376                 if( categoryItem && ![self isSubCategoryGeneral:lastsubcat] )
377                 {
378                     subCategoryItem = [categoryItem itemRepresentingSubCategory:lastsubcat];
379                     if(!subCategoryItem)
380                     {
381                         subCategoryItem = [VLCTreeSubCategoryItem subCategoryTreeItemWithSubCategory:lastsubcat];
382                         if(subCategoryItem) [[categoryItem children] addObject:subCategoryItem];
383                     }
384                 }
385             }
386             
387             if( module_is_main( p_module) && CONFIG_ITEM(configType) )
388             {
389                 if( categoryItem && [self isSubCategoryGeneral:lastsubcat] )
390                 {
391                     [[categoryItem options] addObject:[[VLCTreeLeafItem alloc] initWithConfigItem:&p_configs[j]]];
392                 }
393                 else if( subCategoryItem )
394                 {
395                     [[subCategoryItem options] addObject:[[VLCTreeLeafItem alloc] initWithConfigItem:&p_configs[j]]];
396                 }
397             }
398             else if( !module_is_main( p_module) && CONFIG_ITEM(configType))
399             {
400                 if( ![[subCategoryItem children] containsObject: pluginItem] )
401                 {
402                     [[subCategoryItem children] addObject:pluginItem];
403                 }
404                 if( pluginItem )
405                     [[pluginItem options] addObject:[[VLCTreeLeafItem alloc] initWithConfigItem:&p_configs[j]]];
406             }
407         }
408     }
409     module_list_free( modules );
410     return _children;
411 }
412 @end
413
414 #pragma mark -
415 @implementation VLCTreeCategoryItem
416 + (VLCTreeCategoryItem *)categoryTreeItemWithCategory:(int)category
417 {
418     if(!config_CategoryNameGet( category )) return nil;
419     return [[[[self class] alloc] initWithCategory:category] autorelease];
420 }
421 - (id)initWithCategory:(int)category
422 {
423     NSString * name = [[VLCMain sharedInstance] localizedString: config_CategoryNameGet( category )];
424     if(self = [super initWithName:name])
425     {
426         _category = category;
427         //_help = [[[VLCMain sharedInstance] localizedString: config_CategoryHelpGet( category )] retain];
428     }
429     return self;
430 }
431
432 - (VLCTreeSubCategoryItem *)itemRepresentingSubCategory:(int)subCategory
433 {
434     assert( [self isKindOfClass:[VLCTreeCategoryItem class]] );
435     NSUInteger childrenCount = [[self children] count];
436     for( NSUInteger i = 0; i < childrenCount; 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         NSUInteger count = [[self options] count];
588         for( NSUInteger i = 0; i < 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     NSUInteger i;
642     NSUInteger count = [_subviews count];
643     for( i = 0 ; i < count ; i++ )
644         [[_subviews objectAtIndex:i] applyChanges];
645
646     count = [_children count];
647     for( i = 0 ; i < count ; i++ )
648         [[_children objectAtIndex:i] applyChanges];
649 }
650
651 - (void)resetView
652 {
653     NSUInteger i;
654     NSUInteger count = [_subviews count];
655     for( i = 0 ; i < count ; i++ )
656         [[_subviews objectAtIndex:i] resetValues];
657
658     count = [_options count];
659     for( i = 0 ; i < count ; i++ )
660         [[_options objectAtIndex:i] resetView];
661
662     count = [_children count];
663     for( i = 0 ; i < count ; i++ )
664         [[_children objectAtIndex:i] resetView];
665
666 }
667
668 - (NSMutableArray *)children
669 {
670     if(!_children) _children = [[NSMutableArray alloc] init];
671     return _children;
672 }
673
674 - (NSMutableArray *)options
675 {
676     if(!_options) _options = [[NSMutableArray alloc] init];
677     return _options;
678 }
679 @end
680
681 #pragma mark -
682 @implementation VLCFlippedView
683
684 - (BOOL)isFlipped
685 {
686     return( YES );
687 }
688
689 @end