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