]> git.sesse.net Git - vlc/blob - modules/gui/macosx/prefs.m
* Let's break the prefs again for OSX
[vlc] / modules / gui / macosx / prefs.m
1 /*****************************************************************************
2  * prefs.m: MacOS X module for vlc
3  *****************************************************************************
4  * Copyright (C) 2002-2005 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA  02111, 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    - an advanced action (to hide/show advanced options)
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 #include "intf.h"
50 #include "prefs.h"
51 #include "vlc_keys.h"
52
53 /*****************************************************************************
54  * VLCPrefs implementation
55  *****************************************************************************/
56 @implementation VLCPrefs
57
58 static VLCPrefs *_o_sharedMainInstance = nil;
59
60 + (VLCPrefs *)sharedInstance
61 {
62     return _o_sharedMainInstance ? _o_sharedMainInstance : [[self alloc] init];
63 }
64
65 - (id)init
66 {
67     if( _o_sharedMainInstance ) {
68         [self dealloc];
69     }
70     else
71     {
72         _o_sharedMainInstance = [super init];
73         p_intf = VLCIntf;
74         o_empty_view = [[NSView alloc] init];
75     }
76     
77     return _o_sharedMainInstance;
78 }
79
80 - (void)dealloc
81 {
82     [o_empty_view release];
83     [super dealloc];
84 }
85
86 - (void)awakeFromNib
87 {
88     p_intf = VLCIntf;
89     b_advanced = config_GetInt( p_intf, "advanced" );
90
91     [self initStrings];
92     [o_advanced_ckb setState: b_advanced];
93     [o_prefs_view setBorderType: NSGrooveBorder];
94     [o_prefs_view setHasVerticalScroller: YES];
95     [o_prefs_view setDrawsBackground: NO];
96     [o_prefs_view setRulersVisible: NO];
97     [o_prefs_view setDocumentView: o_empty_view];
98     [o_tree selectRow:0 byExtendingSelection:NO];
99 }
100
101 - (void)showPrefs
102 {
103     /* load our nib (if not already loaded) */
104     [NSBundle loadNibNamed:@"Preferences" owner:self];
105     
106     /* Show View for the currently select treeitem */
107     /* [self showViewForID: [[o_tree itemAtRow:[o_tree selectedRow]] getObjectID]
108         andName: [[o_tree itemAtRow:[o_tree selectedRow]] getName]]; */
109     [o_prefs_window center];
110     [o_prefs_window makeKeyAndOrderFront:self];
111 }
112
113 - (void)initStrings
114 {
115     [o_prefs_window setTitle: _NS("Preferences")];
116     [o_save_btn setTitle: _NS("Save")];
117     [o_cancel_btn setTitle: _NS("Cancel")];
118     [o_reset_btn setTitle: _NS("Reset All")];
119     [o_advanced_ckb setTitle: _NS("Advanced")];
120 }
121
122 - (IBAction)savePrefs: (id)sender
123 {
124     /* TODO: call savePrefs on Root item */
125     config_SaveConfigFile( p_intf, NULL );
126     [o_prefs_window orderOut:self];
127 }
128
129 - (IBAction)closePrefs: (id)sender
130 {
131     [o_prefs_window orderOut:self];
132 }
133
134 - (IBAction)resetAll: (id)sender
135 {
136     NSBeginInformationalAlertSheet(_NS("Reset Preferences"), _NS("Cancel"), _NS("Continue"), 
137         nil, o_prefs_window, self, @selector(sheetDidEnd: returnCode: contextInfo:), NULL, nil,
138         _NS("Beware this will reset your VLC media player preferences.\n"
139             "Are you sure you want to continue?") );
140 }
141
142 - (void)sheetDidEnd:(NSWindow *)o_sheet returnCode:(int)i_return contextInfo:(void *)o_context
143 {
144     if( i_return == NSAlertAlternateReturn )
145     {
146         config_ResetAll( p_intf );
147         [self showViewForID: [[o_tree itemAtRow:[o_tree selectedRow]] getObjectID]
148             andName: [[o_tree itemAtRow:[o_tree selectedRow]] getName]];
149     }
150 }
151
152 - (IBAction)advancedToggle: (id)sender
153 {
154     b_advanced = !b_advanced;
155     [o_advanced_ckb setState: b_advanced];
156     /* refresh the view of the current treeitem */
157     /* [self showViewForID: [[o_tree itemAtRow:[o_tree selectedRow]] getObjectID]
158         andName: [[o_tree itemAtRow:[o_tree selectedRow]] getName]]; */
159 }
160
161 - (void)loadConfigTree
162 {
163 }
164
165 - (void)outlineViewSelectionIsChanging:(NSNotification *)o_notification
166 {
167 }
168
169 /* update the document view to the view of the selected tree item */
170 - (void)outlineViewSelectionDidChange:(NSNotification *)o_notification
171 {
172     /*
173     [self showViewForID: [[o_tree itemAtRow:[o_tree selectedRow]] getObjectID]
174         andName: [[o_tree itemAtRow:[o_tree selectedRow]] getName]];*/
175 }
176
177 @end
178
179 @implementation VLCPrefs (NSTableDataSource)
180
181 - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
182     return (item == nil) ? [[VLCTreeItem rootItem] numberOfChildren] : [item numberOfChildren];
183 }
184
185 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
186     return (item == nil) ? YES : ([item numberOfChildren] != -1);
187 }
188
189 - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item {
190     return (item == nil) ? [[VLCTreeItem rootItem] childAtIndex:index] : [item childAtIndex:index];
191 }
192
193 - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item {
194     return (item == nil) ? @"" : (id)[item getName];
195 }
196
197 @end
198
199 @implementation VLCTreeItem
200
201 static VLCTreeItem *o_root_item = nil;
202
203 #define IsALeafNode ((id)-1)
204
205 - (id)initWithName: (NSString *)o_item_name ID: (int)i_id parent:(VLCTreeItem *)o_parent_item
206 {
207     self = [super init];
208
209     if( self != nil )
210     {
211         o_name = [o_item_name copy];
212         i_object_id = i_id;
213         o_parent = o_parent_item;
214     }
215     return( self );
216 }
217
218 + (VLCTreeItem *)rootItem {
219    if (o_root_item == nil) o_root_item = [[VLCTreeItem alloc] initWithName:@"main" ID: 0 parent:nil];
220    return o_root_item;       
221 }
222
223 - (void)dealloc
224 {
225     if (o_children != IsALeafNode) [o_children release];
226     [o_name release];
227     [super dealloc];
228 }
229
230 /* Creates and returns the array of children
231  * Loads children incrementally */
232 - (NSArray *)children
233 {
234     if( o_children == NULL )
235     {
236         intf_thread_t *p_intf = VLCIntf;
237         vlc_list_t      *p_list;
238         module_t        *p_module = NULL;
239         module_config_t *p_item;
240         int i_index,j;
241
242         /* List the modules */
243         p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
244         if( !p_list ) return nil;
245
246         if( [[self getName] isEqualToString: @"main"] )
247         {
248             /*
249             * Find the main module
250             */
251             for( i_index = 0; i_index < p_list->i_count; i_index++ )
252             {
253                 p_module = (module_t *)p_list->p_values[i_index].p_object;
254                 if( !strcmp( p_module->psz_object_name, "main" ) )
255                     break;
256             }
257             if( p_module == NULL )
258             {
259                 msg_Err( p_intf, "could not find the main module in our preferences" );
260                 return nil;
261             }
262             if( i_index < p_list->i_count )
263             {
264                 /* We found the main module */
265         
266                 /* Enumerate config categories and store a reference so we can
267                  * generate their config panel them when it is asked by the user. */
268                 p_item = p_module->p_config;
269                 o_children = [[NSMutableArray alloc] initWithCapacity:10];
270
271                 if( p_item ) do
272                 {
273                     NSString *o_child_name;
274                     
275                     switch( p_item->i_type )
276                     {
277                     case CONFIG_HINT_CATEGORY:
278                         o_child_name = [[VLCMain sharedInstance] localizedString: p_item->psz_text];
279                         [o_children addObject:[[VLCTreeItem alloc] initWithName: o_child_name
280                             ID: p_module->i_object_id parent:self]];
281                         break;
282                     }
283                 }
284                 while( p_item->i_type != CONFIG_HINT_END && p_item++ );
285                 
286                 /* Add the modules item */
287                 [o_children addObject:[[VLCTreeItem alloc] initWithName: _NS("Modules")
288                     ID: 0 parent:self]];
289             }
290             else
291             {
292                 o_children = IsALeafNode;
293             }
294         }
295         else if( [[self getName] isEqualToString: _NS("Modules")] )
296         {
297             /* Add the capabilities */
298             o_children = [[NSMutableArray alloc] initWithCapacity:10];
299             for( i_index = 0; i_index < p_list->i_count; i_index++ )
300             {
301                 p_module = (module_t *)p_list->p_values[i_index].p_object;
302         
303                 /* Exclude the main module */
304                 if( !strcmp( p_module->psz_object_name, "main" ) )
305                     continue;
306         
307                 /* Exclude empty modules */
308                 p_item = p_module->p_config;
309                 if( !p_item ) continue;
310                 do
311                 {
312                     if( p_item->i_type & CONFIG_ITEM )
313                         break;
314                 }
315                 while( p_item->i_type != CONFIG_HINT_END && p_item++ );
316                 if( p_item->i_type == CONFIG_HINT_END ) continue;
317         
318                 /* Create the capability tree if it doesn't already exist */
319                 NSString *o_capability;
320                 o_capability = [[VLCMain sharedInstance] localizedString: p_module->psz_capability];
321                 if( !p_module->psz_capability || !*p_module->psz_capability )
322                 {
323                     /* Empty capability ? Let's look at the submodules */
324                     module_t * p_submodule;
325                     for( j = 0; j < p_module->i_children; j++ )
326                     {
327                         p_submodule = (module_t*)p_module->pp_children[ j ];
328                         if( p_submodule->psz_capability && *p_submodule->psz_capability )
329                         {
330                             o_capability = [[VLCMain sharedInstance] localizedString: p_submodule->psz_capability];
331                             BOOL b_found = FALSE;
332                             for( j = 0; j < (int)[o_children count]; j++ )
333                             {
334                                 if( [[[o_children objectAtIndex:j] getName] isEqualToString: o_capability] )
335                                 {
336                                     b_found = TRUE;
337                                     break;
338                                 }
339                             }
340                             if( !b_found )
341                             {
342                                 [o_children addObject:[[VLCTreeItem alloc] initWithName: o_capability
343                                 ID: 0 parent:self]];
344                             }
345                         }
346                     }
347                 }
348
349                 BOOL b_found = FALSE;
350                 for( j = 0; j < (int)[o_children count]; j++ )
351                 {
352                     if( [[[o_children objectAtIndex:j] getName] isEqualToString: o_capability] )
353                     {
354                         b_found = TRUE;
355                         break;
356                     }
357                 }
358                 if( !b_found )
359                 {
360                     [o_children addObject:[[VLCTreeItem alloc] initWithName: o_capability
361                     ID: 0 parent:self]];
362                 }
363             }
364         }
365         else if( [[o_parent getName] isEqualToString: _NS("Modules")] )
366         {
367             /* Now add the modules */
368             o_children = [[NSMutableArray alloc] initWithCapacity:10];
369             for( i_index = 0; i_index < p_list->i_count; i_index++ )
370             {
371                 p_module = (module_t *)p_list->p_values[i_index].p_object;
372         
373                 /* Exclude the main module */
374                 if( !strcmp( p_module->psz_object_name, "main" ) )
375                     continue;
376         
377                 /* Exclude empty modules */
378                 p_item = p_module->p_config;
379                 if( !p_item ) continue;
380                 do
381                 {
382                     if( p_item->i_type & CONFIG_ITEM )
383                         break;
384                 }
385                 while( p_item->i_type != CONFIG_HINT_END && p_item++ );
386                 if( p_item->i_type == CONFIG_HINT_END ) continue;
387         
388                 /* Check the capability */
389                 NSString *o_capability;
390                 o_capability = [[VLCMain sharedInstance] localizedString: p_module->psz_capability];
391                 if( !p_module->psz_capability || !*p_module->psz_capability )
392                 {
393                     /* Empty capability ? Let's look at the submodules */
394                     module_t * p_submodule;
395                     for( j = 0; j < p_module->i_children; j++ )
396                     {
397                         p_submodule = (module_t*)p_module->pp_children[ j ];
398                         if( p_submodule->psz_capability && *p_submodule->psz_capability )
399                         {
400                             o_capability = [[VLCMain sharedInstance] localizedString: p_submodule->psz_capability];
401                             if( [o_capability isEqualToString: [self getName]] )
402                             {
403                             [o_children addObject:[[VLCTreeItem alloc] initWithName:
404                                 [[VLCMain sharedInstance] localizedString: p_module->psz_object_name ]
405                                 ID: p_module->i_object_id parent:self]];
406                             }
407                         }
408                     }
409                 }
410                 else if( [o_capability isEqualToString: [self getName]] )
411                 {
412                     [o_children addObject:[[VLCTreeItem alloc] initWithName:
413                         [[VLCMain sharedInstance] localizedString: p_module->psz_object_name ]
414                         ID: p_module->i_object_id parent:self]];
415                 }
416             }
417         }
418         else
419         {
420             /* all the other stuff are leafs */
421             o_children = IsALeafNode;
422         }
423         vlc_list_release( p_list );
424     }
425     return o_children;
426 }
427
428 - (int)getObjectID
429 {
430     return i_object_id;
431 }
432
433 - (NSString *)getName
434 {
435     return o_name;
436 }
437
438 - (VLCTreeItem *)childAtIndex:(int)i_index {
439     return [[self children] objectAtIndex:i_index];
440 }
441
442 - (int)numberOfChildren {
443     id i_tmp = [self children];
444     return (i_tmp == IsALeafNode) ? (-1) : (int)[i_tmp count];
445 }
446
447 - (BOOL)hasPrefs:(NSString *)o_module_name
448 {
449     intf_thread_t *p_intf = VLCIntf;
450     module_t *p_parser;
451     vlc_list_t *p_list;
452     char *psz_module_name;
453     int i_index;
454
455     psz_module_name = (char *)[o_module_name UTF8String];
456
457     /* look for module */
458     p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
459
460     for( i_index = 0; i_index < p_list->i_count; i_index++ )
461     {
462         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
463
464         if( !strcmp( p_parser->psz_object_name, psz_module_name ) )
465         {
466             BOOL b_has_prefs = p_parser->i_config_items != 0;
467             vlc_list_release( p_list );
468             return( b_has_prefs );
469         }
470     }
471
472     vlc_list_release( p_list );
473
474     return( NO );
475 }
476
477 @end
478
479
480 @implementation VLCFlippedView
481
482 - (BOOL)isFlipped
483 {
484     return( YES );
485 }
486
487 @end