]> git.sesse.net Git - vlc/blob - modules/gui/macosx/prefs.m
macosx: remove a NSLog message.
[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     module_config_t * _configItem;
70 }
71 - (id)initWithConfigItem:(module_config_t *)configItem;
72
73 - (id)initWithName:(NSString*)name andConfigItem:(module_config_t *)configItem;
74
75 - (int)numberOfChildren;
76 - (VLCTreeItem *)childAtIndex:(int)i_index;
77
78 - (module_config_t *)configItem;
79
80 - (NSString *)name;
81 - (NSMutableArray *)children;
82 - (NSMutableArray *)options;
83 - (void)showView:(NSScrollView *)o_prefs_view;
84 - (void)applyChanges;
85 - (void)resetView;
86
87 @end
88
89 /* CONFIG_SUBCAT */
90 @interface VLCTreeSubCategoryItem : VLCTreeItem
91 {
92     int _subCategory;
93 }
94 + (VLCTreeSubCategoryItem *)subCategoryTreeItemWithSubCategory:(int)subCategory;
95 - (id)initWithSubCategory:(int)subCategory;
96 - (int)subCategory;
97 @end
98
99 /* Plugin daughters */
100 @interface VLCTreePluginItem : VLCTreeItem
101 {
102 }
103 + (VLCTreePluginItem *)pluginTreeItemWithPlugin:(module_t *)plugin;
104 - (id)initWithPlugin:(module_t *)plugin;
105 @end
106
107 /* CONFIG_CAT */
108 @interface VLCTreeCategoryItem : VLCTreeItem
109 {
110     int _category;
111 }
112 + (VLCTreeCategoryItem *)categoryTreeItemWithCategory:(int)category;
113 - (id)initWithCategory:(int)category;
114 - (int)category;
115 - (VLCTreeSubCategoryItem *)itemRepresentingSubCategory:(int)category;
116 @end
117
118 /* individual options. */
119 @interface VLCTreeLeafItem : VLCTreeItem
120 { }
121 @end
122
123 @interface VLCTreeMainItem : VLCTreeItem
124 {
125     module_config_t * _configItems;
126 }
127 - (VLCTreeCategoryItem *)itemRepresentingCategory:(int)category;
128 @end
129
130 #pragma mark -
131
132 /*****************************************************************************
133  * VLCPrefs implementation
134  *****************************************************************************/
135 @implementation VLCPrefs
136
137 static VLCPrefs *_o_sharedMainInstance = nil;
138
139 + (VLCPrefs *)sharedInstance
140 {
141     return _o_sharedMainInstance ? _o_sharedMainInstance : [[self alloc] init];
142 }
143
144 - (id)init
145 {
146     if( _o_sharedMainInstance ) {
147         [self dealloc];
148     }
149     else
150     {
151         _o_sharedMainInstance = [super init];
152         p_intf = VLCIntf;
153         o_empty_view = [[NSView alloc] init];
154         _rootTreeItem = [[VLCTreeMainItem alloc] init];
155     }
156
157     return _o_sharedMainInstance;
158 }
159
160 - (void)dealloc
161 {
162     [o_empty_view release];
163     [_rootTreeItem release];
164     [super dealloc];
165 }
166
167 - (void)awakeFromNib
168 {
169     p_intf = VLCIntf;
170
171     [self initStrings];
172     [o_prefs_view setBorderType: NSGrooveBorder];
173     [o_prefs_view setHasVerticalScroller: YES];
174     [o_prefs_view setDrawsBackground: NO];
175     [o_prefs_view setDocumentView: o_empty_view];
176     [o_tree selectRow:0 byExtendingSelection:NO];
177 }
178
179 - (void)setTitle: (NSString *) o_title_name
180 {
181     [o_title setStringValue: o_title_name];
182 }
183
184 - (void)showPrefs
185 {
186     [[o_basicFull_matrix cellAtRow:0 column:0] setState: NSOffState];
187     [[o_basicFull_matrix cellAtRow:0 column:1] setState: NSOnState];
188     
189     [o_prefs_window center];
190     [o_prefs_window makeKeyAndOrderFront:self];
191 }
192
193 - (void)initStrings
194 {
195     [o_prefs_window setTitle: _NS("Preferences")];
196     [o_save_btn setTitle: _NS("Save")];
197     [o_cancel_btn setTitle: _NS("Cancel")];
198     [o_reset_btn setTitle: _NS("Reset All")];
199     [[o_basicFull_matrix cellAtRow: 0 column: 0] setStringValue: _NS("Basic")];
200     [[o_basicFull_matrix cellAtRow: 0 column: 1] setStringValue: _NS("All")];
201 }
202
203 - (IBAction)savePrefs: (id)sender
204 {
205     /* TODO: call savePrefs on Root item */
206     [_rootTreeItem applyChanges];
207     config_SaveConfigFile( p_intf, NULL );
208     [o_prefs_window orderOut:self];
209 }
210
211 - (IBAction)closePrefs: (id)sender
212 {
213     [o_prefs_window orderOut:self];
214 }
215
216 - (IBAction)resetAll: (id)sender
217 {
218     NSBeginInformationalAlertSheet(_NS("Reset Preferences"), _NS("Cancel"),
219         _NS("Continue"), nil, o_prefs_window, self,
220         @selector(sheetDidEnd: returnCode: contextInfo:), NULL, nil,
221         _NS("Beware this will reset the VLC media player preferences.\n"
222             "Are you sure you want to continue?") );
223 }
224
225 - (void)sheetDidEnd:(NSWindow *)o_sheet returnCode:(int)i_return
226     contextInfo:(void *)o_context
227 {
228     if( i_return == NSAlertAlternateReturn )
229     {
230         [o_prefs_view setDocumentView: o_empty_view];
231         config_ResetAll( p_intf );
232         [_rootTreeItem resetView];
233         [[o_tree itemAtRow:[o_tree selectedRow]]
234             showView:o_prefs_view];
235     }
236 }
237
238 - (IBAction)buttonAction: (id)sender
239 {
240     [o_prefs_window orderOut: self];
241     [[o_basicFull_matrix cellAtRow:0 column:0] setState: NSOnState];
242     [[o_basicFull_matrix cellAtRow:0 column:1] setState: NSOffState];
243     [[[VLCMain sharedInstance] simplePreferences] showSimplePrefs];
244 }
245
246 - (void)loadConfigTree
247 {
248 }
249
250 - (void)outlineViewSelectionIsChanging:(NSNotification *)o_notification
251 {
252 }
253
254 /* update the document view to the view of the selected tree item */
255 - (void)outlineViewSelectionDidChange:(NSNotification *)o_notification
256 {
257     [[o_tree itemAtRow:[o_tree selectedRow]] showView: o_prefs_view];
258 }
259
260 @end
261
262 @implementation VLCPrefs (NSTableDataSource)
263
264 - (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
265 {
266     return (item == nil) ? [_rootTreeItem numberOfChildren] : [item numberOfChildren];
267 }
268
269 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
270 {
271     return (item == nil) ? [_rootTreeItem numberOfChildren] : [item numberOfChildren];
272 }
273
274 - (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item
275 {
276     return (item == nil) ? (id)[_rootTreeItem childAtIndex:index]: (id)[item childAtIndex:index];
277 }
278
279 - (id)outlineView:(NSOutlineView *)outlineView
280     objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
281 {
282     return (item == nil) ? @"" : [item name];
283 }
284
285 @end
286
287 #pragma mark -
288 @implementation VLCTreeMainItem
289
290 - (VLCTreeCategoryItem *)itemRepresentingCategory:(int)category
291 {
292     for( int i = 0; i < [[self children] count]; i++ )
293     {
294         VLCTreeCategoryItem * categoryItem = [[self children] objectAtIndex:i];
295         if( [categoryItem category] == category )
296             return categoryItem;
297     }
298     return nil;
299 }
300
301 - (void)dealloc
302 {
303     module_config_free( _configItems );
304     [super dealloc];
305 }
306
307 - (bool)isSubCategoryGeneral:(int)category
308 {
309     if(category == SUBCAT_VIDEO_GENERAL ||
310           category == SUBCAT_ADVANCED_MISC ||
311           category == SUBCAT_INPUT_GENERAL ||
312           category == SUBCAT_INTERFACE_GENERAL ||
313           category == SUBCAT_SOUT_GENERAL||
314           category == SUBCAT_PLAYLIST_GENERAL||
315           category == SUBCAT_AUDIO_GENERAL )
316     {
317         return true;
318     }
319     return false;
320 }
321
322 /* Creates and returns the array of children
323  * Loads children incrementally */
324 - (NSMutableArray *)children
325 {
326     if( _children ) return _children;
327     _children = [[NSMutableArray alloc] init];
328
329     intf_thread_t   *p_intf = VLCIntf;
330
331     /* List the modules */
332     size_t count, i;
333     module_t ** modules = module_list_get( &count );
334     if( !modules ) return nil;
335
336     /* Build a tree of the plugins */
337     /* Add the capabilities */
338     for( i = 0; i < count; i++ )
339     {
340         module_t * p_module = modules[i];
341
342         /* Exclude empty plugins (submodules don't have config */
343         /* options, they are stored in the parent module) */
344         unsigned int confsize;
345         _configItems = module_config_get( p_module, &confsize );
346
347         VLCTreeCategoryItem * categoryItem = nil;
348         VLCTreeSubCategoryItem * subCategoryItem = nil;
349         VLCTreePluginItem * pluginItem = nil;
350         int lastsubcat = 0;
351
352         unsigned int j;
353         for( j = 0; j < confsize; j++ )
354         {
355             int configType = _configItems[j].i_type;
356             if( configType == CONFIG_CATEGORY )
357             {
358                 categoryItem = [self itemRepresentingCategory:_configItems[j].value.i];
359                 if(!categoryItem)
360                 {
361                     categoryItem = [VLCTreeCategoryItem categoryTreeItemWithCategory:_configItems[j].value.i];
362                     if(categoryItem) [[self children] addObject:categoryItem];
363                 }
364             }
365             else if( configType == CONFIG_SUBCATEGORY )
366             {
367                 lastsubcat = _configItems[j].value.i;
368                 if( categoryItem && ![self isSubCategoryGeneral:lastsubcat] )
369                 {
370                     subCategoryItem = [categoryItem itemRepresentingSubCategory:lastsubcat];
371                     if(!subCategoryItem)
372                     {
373                         subCategoryItem = [VLCTreeSubCategoryItem subCategoryTreeItemWithSubCategory:lastsubcat];
374                         if(subCategoryItem) [[categoryItem children] addObject:subCategoryItem];
375                     }
376                 }
377             }
378             
379             if( module_is_main( p_module) && (configType & CONFIG_ITEM) )
380             {
381                 if( categoryItem && [self isSubCategoryGeneral:lastsubcat] )
382                 {
383                     [[categoryItem options] addObject:[[VLCTreeLeafItem alloc] initWithConfigItem:&_configItems[j]]];
384                 }
385                 else if( subCategoryItem )
386                 {
387                     [[subCategoryItem options] addObject:[[VLCTreeLeafItem alloc] initWithConfigItem:&_configItems[j]]];
388                 }
389             }
390             else if( !module_is_main( p_module) && (configType & CONFIG_ITEM))
391             {
392                 if( !pluginItem )
393                 {
394                     pluginItem = [VLCTreePluginItem pluginTreeItemWithPlugin: p_module];
395                     if(pluginItem) [[subCategoryItem children] addObject:pluginItem];
396                 }
397                 if( pluginItem )
398                     [[pluginItem options] addObject:[[VLCTreeLeafItem alloc] initWithConfigItem:&_configItems[j]]];
399             }
400         }
401     }
402     module_list_free( modules );
403     return _children;
404 }
405 @end
406
407 #pragma mark -
408 @implementation VLCTreeCategoryItem
409 + (VLCTreeCategoryItem *)categoryTreeItemWithCategory:(int)category
410 {
411     return [[[[self class] alloc] initWithCategory:category] autorelease];
412 }
413 - (id)initWithCategory:(int)category
414 {
415     if(!config_CategoryNameGet( category )) return nil;
416     NSString * name = [[VLCMain sharedInstance] localizedString: config_CategoryNameGet( category )];
417     if(self = [super initWithName:name andConfigItem:nil])
418     {
419         _category = category;
420         //_help = [[[VLCMain sharedInstance] localizedString: config_CategoryHelpGet( category )] retain];
421     }
422     return self;
423 }
424
425 - (VLCTreeSubCategoryItem *)itemRepresentingSubCategory:(int)subCategory
426 {
427     assert( [self isKindOfClass:[VLCTreeCategoryItem class]] );
428     for( int i = 0; i < [[self children] count]; i++ )
429     {
430         VLCTreeSubCategoryItem * subCategoryItem = [[self children] objectAtIndex:i];
431         if( [subCategoryItem subCategory] == subCategory )
432             return subCategoryItem;
433     }
434     return nil;
435 }
436
437 - (int)category
438 {
439     return _category;
440 }
441 @end
442
443 #pragma mark -
444 @implementation VLCTreeSubCategoryItem
445 - (id)initWithSubCategory:(int)subCategory
446 {
447     if(!config_CategoryNameGet( subCategory )) return nil;
448     NSString * name = [[VLCMain sharedInstance] localizedString: config_CategoryNameGet( subCategory )];
449     if(self = [super initWithName:name andConfigItem:NULL])
450     {
451         _subCategory = subCategory;
452         //_help = [[[VLCMain sharedInstance] localizedString: config_CategoryHelpGet( subCategory )] retain];
453     }
454     return self;
455 }
456
457 + (VLCTreeSubCategoryItem *)subCategoryTreeItemWithSubCategory:(int)subCategory
458 {
459     return [[[[self class] alloc] initWithSubCategory:subCategory] autorelease];
460 }
461
462 - (int)subCategory
463 {
464     return _subCategory;
465 }
466
467 @end
468
469 #pragma mark -
470 @implementation VLCTreePluginItem
471 - (id)initWithPlugin:(module_t *)plugin
472 {
473     NSString * name = [[VLCMain sharedInstance] localizedString: module_get_name( plugin, false )];
474     if(self = [super initWithName:name andConfigItem:NULL])
475     {
476         //_plugin = plugin;
477         //_help = [[[VLCMain sharedInstance] localizedString: config_CategoryHelpGet( subCategory )] retain];
478     }
479     return self;
480 }
481
482 + (VLCTreePluginItem *)pluginTreeItemWithPlugin:(module_t *)plugin
483 {
484     return [[[[self class] alloc] initWithPlugin:plugin] autorelease];
485 }
486
487 @end
488
489 #pragma mark -
490 @implementation VLCTreeLeafItem
491 @end
492
493 #pragma mark -
494 #pragma mark (Root class for all TreeItems)
495 @implementation VLCTreeItem
496
497 - (id)initWithConfigItem: (module_config_t *) configItem
498 {
499     NSString * name = [[VLCMain sharedInstance] localizedString:configItem->psz_name];
500     return [self initWithName:name andConfigItem:configItem];
501 }
502
503 - (id)initWithName:(NSString*)name andConfigItem:(module_config_t *)configItem
504 {
505     self = [super init];
506     if( self != nil )
507     {
508         _name = [name retain];
509         _configItem = configItem;
510     }
511     return self;
512 }
513
514 - (void)dealloc
515 {
516     [_children release];
517     [_options release];
518     [_name release];
519     [_subviews release];
520     [super dealloc];
521 }
522
523 - (VLCTreeItem *)childAtIndex:(int)i_index
524 {
525     return [[self children] objectAtIndex:i_index];
526 }
527
528 - (int)numberOfChildren
529 {
530     return [[self children] count];
531 }
532
533 - (NSString *)name
534 {
535     return [[_name retain] autorelease];
536 }
537
538 - (void)showView:(NSScrollView *)prefsView
539 {
540     NSRect          s_vrc;
541     NSView          *view;
542
543     [[VLCPrefs sharedInstance] setTitle: [self name]];
544     s_vrc = [[prefsView contentView] bounds]; s_vrc.size.height -= 4;
545     view = [[VLCFlippedView alloc] initWithFrame: s_vrc];
546     [view setAutoresizingMask: NSViewWidthSizable | NSViewMinYMargin | NSViewMaxYMargin];
547
548     if(!_subviews)
549     {
550         _subviews = [[NSMutableArray alloc] initWithCapacity:10];
551
552         long i;
553         for( i = 0; i < [[self options] count]; i++)
554         {
555             VLCTreeItem * item = [[self options] objectAtIndex:i];
556
557             VLCConfigControl *control;
558             control = [VLCConfigControl newControl:[item configItem] withView:view];
559             if( control )
560             {
561                 [control setAutoresizingMask: NSViewMaxYMargin | NSViewWidthSizable];
562                 [_subviews addObject: control];
563             }
564         }
565     }
566
567     assert(view);
568     
569     int i_lastItem = 0;
570     int i_yPos = -2;
571     int i_max_label = 0;
572
573     NSEnumerator *enumerator = [_subviews objectEnumerator];
574     VLCConfigControl *widget;
575     NSRect frame;
576
577     while( ( widget = [enumerator nextObject] ) )
578         if( i_max_label < [widget labelSize] )
579             i_max_label = [widget labelSize];
580
581     enumerator = [_subviews objectEnumerator];
582     while( ( widget = [enumerator nextObject] ) )
583     {
584         int i_widget;
585
586         i_widget = [widget viewType];
587         i_yPos += [VLCConfigControl calcVerticalMargin:i_widget lastItem:i_lastItem];
588         [widget setYPos:i_yPos];
589         frame = [widget frame];
590         frame.size.width = [view frame].size.width - LEFTMARGIN - RIGHTMARGIN;
591         [widget setFrame:frame];
592         [widget alignWithXPosition: i_max_label];
593         i_yPos += [widget frame].size.height;
594         i_lastItem = i_widget;
595         [view addSubview:widget];
596     }
597
598     frame = [view frame];
599     frame.size.height = i_yPos;
600     [view setFrame:frame];
601     [prefsView setDocumentView:view];
602 }
603
604 - (void)applyChanges
605 {
606     unsigned int i;
607     for( i = 0 ; i < [_subviews count] ; i++ )
608         [[_subviews objectAtIndex:i] applyChanges];
609
610     for( i = 0 ; i < [_children count] ; i++ )
611         [[_children objectAtIndex:i] applyChanges];
612 }
613
614 - (void)resetView
615 {
616     [_subviews release];
617     _subviews = nil;
618
619     unsigned int i;
620     for( i = 0 ; i < [_children count] ; i++ )
621         [[_children objectAtIndex:i] resetView];
622 }
623
624 - (NSMutableArray *)children
625 {
626     if(!_children) _children = [[NSMutableArray alloc] init];
627     return _children;
628 }
629
630 - (NSMutableArray *)options
631 {
632     if(!_options) _options = [[NSMutableArray alloc] init];
633     return _options;
634 }
635
636 - (module_config_t *)configItem
637 {
638     return _configItem;
639 }
640 @end
641
642 #pragma mark -
643 @implementation VLCFlippedView
644
645 - (BOOL)isFlipped
646 {
647     return( YES );
648 }
649
650 @end