]> git.sesse.net Git - vlc/blob - modules/gui/macosx/prefs.m
Work on MacOS preferences. Need to be completed, improved, and debugged...
[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 <vlc/vlc.h>
50 #include <vlc_config_cat.h>
51
52 #include "intf.h"
53 #include "prefs.h"
54 #include "prefs_widgets.h"
55 #include "vlc_keys.h"
56
57 /*****************************************************************************
58  * VLCPrefs implementation
59  *****************************************************************************/
60 @implementation VLCPrefs
61
62 static VLCPrefs *_o_sharedMainInstance = nil;
63
64 + (VLCPrefs *)sharedInstance
65 {
66     return _o_sharedMainInstance ? _o_sharedMainInstance : [[self alloc] init];
67 }
68
69 - (id)init
70 {
71     if( _o_sharedMainInstance ) {
72         [self dealloc];
73     }
74     else
75     {
76         _o_sharedMainInstance = [super init];
77         p_intf = VLCIntf;
78         o_empty_view = [[NSView alloc] init];
79     }
80     
81     return _o_sharedMainInstance;
82 }
83
84 - (void)dealloc
85 {
86     [o_empty_view release];
87     [super dealloc];
88 }
89
90 - (void)awakeFromNib
91 {
92     p_intf = VLCIntf;
93     b_advanced = config_GetInt( p_intf, "advanced" );
94
95     [self initStrings];
96     [o_advanced_ckb setState: b_advanced];
97     [o_prefs_view setBorderType: NSGrooveBorder];
98     [o_prefs_view setHasVerticalScroller: YES];
99     [o_prefs_view setDrawsBackground: NO];
100     [o_prefs_view setRulersVisible: NO];
101     [o_prefs_view setDocumentView: o_empty_view];
102     [o_tree selectRow:0 byExtendingSelection:NO];
103 }
104
105 - (void)showPrefs
106 {
107     /* load our nib (if not already loaded) */
108     [NSBundle loadNibNamed:@"Preferences" owner:self];
109     
110     /* Show View for the currently select treeitem */
111     /* [self showViewForID: [[o_tree itemAtRow:[o_tree selectedRow]] getObjectID]
112         andName: [[o_tree itemAtRow:[o_tree selectedRow]] getName]]; */
113     [o_prefs_window center];
114     [o_prefs_window makeKeyAndOrderFront:self];
115 }
116
117 - (void)initStrings
118 {
119     [o_prefs_window setTitle: _NS("Preferences")];
120     [o_save_btn setTitle: _NS("Save")];
121     [o_cancel_btn setTitle: _NS("Cancel")];
122     [o_reset_btn setTitle: _NS("Reset All")];
123     [o_advanced_ckb setTitle: _NS("Advanced")];
124 }
125
126 - (IBAction)savePrefs: (id)sender
127 {
128     /* TODO: call savePrefs on Root item */
129     config_SaveConfigFile( p_intf, NULL );
130     [o_prefs_window orderOut:self];
131 }
132
133 - (IBAction)closePrefs: (id)sender
134 {
135     [o_prefs_window orderOut:self];
136 }
137
138 - (IBAction)resetAll: (id)sender
139 {
140     NSBeginInformationalAlertSheet(_NS("Reset Preferences"), _NS("Cancel"), _NS("Continue"), 
141         nil, o_prefs_window, self, @selector(sheetDidEnd: returnCode: contextInfo:), NULL, nil,
142         _NS("Beware this will reset your VLC media player preferences.\n"
143             "Are you sure you want to continue?") );
144 }
145
146 - (void)sheetDidEnd:(NSWindow *)o_sheet returnCode:(int)i_return contextInfo:(void *)o_context
147 {
148     if( i_return == NSAlertAlternateReturn )
149     {
150         config_ResetAll( p_intf );
151         [[o_tree itemAtRow:[o_tree selectedRow]] showView:o_prefs_view];
152 /*        [self showViewForID: [[o_tree itemAtRow:[o_tree selectedRow]] getObjectID]
153             andName: [[o_tree itemAtRow:[o_tree selectedRow]] getName]];
154 */
155     }
156 }
157
158 - (IBAction)advancedToggle: (id)sender
159 {
160     b_advanced = !b_advanced;
161     [o_advanced_ckb setState: b_advanced];
162     /* refresh the view of the current treeitem */
163     [[o_tree itemAtRow:[o_tree selectedRow]] showView:o_prefs_view];
164 /*    [self showViewForID: [[o_tree itemAtRow:[o_tree selectedRow]] getObjectID]
165         andName: [[o_tree itemAtRow:[o_tree selectedRow]] getName]];
166 */
167 }
168
169 - (void)loadConfigTree
170 {
171 }
172
173 - (void)outlineViewSelectionIsChanging:(NSNotification *)o_notification
174 {
175 }
176
177 /* update the document view to the view of the selected tree item */
178 - (void)outlineViewSelectionDidChange:(NSNotification *)o_notification
179 {
180     [[o_tree itemAtRow:[o_tree selectedRow]] showView: o_prefs_view];
181 }
182
183 @end
184
185 @implementation VLCPrefs (NSTableDataSource)
186
187 - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
188     return (item == nil) ? [[VLCTreeItem rootItem] numberOfChildren] : [item numberOfChildren];
189 }
190
191 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
192     return (item == nil) ? YES : ([item numberOfChildren] != -1);
193 }
194
195 - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item {
196     return (item == nil) ? [[VLCTreeItem rootItem] childAtIndex:index] : [item childAtIndex:index];
197 }
198
199 - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item {
200     return (item == nil) ? @"" : (id)[item getName];
201 }
202
203 @end
204
205 @implementation VLCTreeItem
206
207 static VLCTreeItem *o_root_item = nil;
208
209 #define IsALeafNode ((id)-1)
210
211 - (id)initWithName: (NSString *)o_item_name ID: (int)i_id parent:(VLCTreeItem *)o_parent_item children:(NSMutableArray *)o_children_array whithCategory: (int) i_category
212 {
213     self = [super init];
214
215     if( self != nil )
216     {
217         o_name = [o_item_name copy];
218         i_object_id = i_id;
219         o_parent = o_parent_item;
220         o_children = o_children_array;
221         i_object_category = i_category;
222     }
223     return( self );
224 }
225
226 + (VLCTreeItem *)rootItem {
227    if (o_root_item == nil) o_root_item = [[VLCTreeItem alloc] initWithName:@"main" ID: 0 parent:nil children:[[NSMutableArray alloc] initWithCapacity:10] whithCategory: -1];
228    return o_root_item;       
229 }
230
231 - (void)dealloc
232 {
233     if (o_children != IsALeafNode) [o_children release];
234     [o_name release];
235     [super dealloc];
236 }
237
238 /* Creates and returns the array of children
239  * Loads children incrementally */
240 - (NSArray *)children
241 {
242     if( o_children == IsALeafNode )
243         return o_children;
244     if( [ o_children count] == 0 )
245     {
246         intf_thread_t *p_intf = VLCIntf;
247         vlc_list_t      *p_list;
248         module_t        *p_module = NULL;
249         module_config_t *p_item;
250         int i_index;
251
252         /* List the modules */
253         p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
254         if( !p_list ) return nil;
255
256         if( [[self getName] isEqualToString: @"main"] )
257         {
258             /*
259             * Find the main module
260             */
261             for( i_index = 0; i_index < p_list->i_count; i_index++ )
262             {
263                 p_module = (module_t *)p_list->p_values[i_index].p_object;
264                 if( !strcmp( p_module->psz_object_name, "main" ) )
265                     break;
266             }
267             if( p_module == NULL )
268             {
269                 msg_Err( p_intf, "could not find the main module in our preferences" );
270                 return nil;
271             }
272             if( i_index < p_list->i_count )
273             {
274                 /* We found the main module */
275                 /* Enumerate config categories and store a reference so we can
276                  * generate their config panel them when it is asked by the user. */
277                 VLCTreeItem *p_last_category = NULL;
278                 p_item = p_module->p_config;
279                 o_children = [[NSMutableArray alloc] initWithCapacity:10];
280                 if( p_item ) do
281                 {
282                     NSString *o_child_name;
283                     switch( p_item->i_type )
284                     {
285                     case CONFIG_CATEGORY:
286                         o_child_name = [[VLCMain sharedInstance] localizedString: config_CategoryNameGet(p_item->i_value ) ];
287                         p_last_category = [VLCTreeItem alloc];
288                         [o_children addObject:[p_last_category initWithName: o_child_name
289                             ID: p_item->i_value parent:self children:[[NSMutableArray alloc] initWithCapacity:10] whithCategory: p_item - p_module->p_config]];
290                         break;
291                     case CONFIG_SUBCATEGORY:
292                         o_child_name = [[VLCMain sharedInstance] localizedString: config_CategoryNameGet(p_item->i_value ) ];
293                         [p_last_category->o_children addObject:[[VLCTreeItem alloc] initWithName: o_child_name
294                             ID: p_item->i_value parent:p_last_category children:[[NSMutableArray alloc] initWithCapacity:10] whithCategory: p_item - p_module->p_config]];
295                         break;
296                     default:
297                         break;
298                     }
299                 } while( p_item->i_type != CONFIG_HINT_END && p_item++ );
300             }
301
302             /* Build a tree of the plugins */
303             /* Add the capabilities */
304             for( i_index = 0; i_index < p_list->i_count; i_index++ )
305             {
306                 p_module = (module_t *)p_list->p_values[i_index].p_object;
307
308                 /* Exclude the main module */
309                 if( !strcmp( p_module->psz_object_name, "main" ) )
310                     continue;
311
312                 /* Exclude empty plugins (submodules don't have config options, they
313                  * are stored in the parent module) */
314                 if( p_module->b_submodule )
315                     continue;
316                 else
317                     p_item = p_module->p_config;
318
319                 if( !p_item ) continue;
320                 int i_category = -1;
321                 int i_subcategory = -1;
322                 int i_options = 0;
323                 do
324                 {
325                     if( p_item->i_type == CONFIG_CATEGORY )
326                     {
327                         i_category = p_item->i_value;
328                     }
329                     else if( p_item->i_type == CONFIG_SUBCATEGORY )
330                     {
331                         i_subcategory = p_item->i_value;
332                     }
333                     if( p_item->i_type & CONFIG_ITEM )
334                         i_options ++;
335                     if( i_options > 0 && i_category >= 0 && i_subcategory >= 0 )
336                     {
337                         break;
338                     }
339                 } while( p_item->i_type != CONFIG_HINT_END && p_item++ );
340                 if( !i_options ) continue;
341
342                 /* Find the right category item */
343
344                 long cookie;
345                 vlc_bool_t b_found = VLC_FALSE;
346                 int i;
347                 VLCTreeItem* p_category_item, * p_subcategory_item;
348                 for (i = 0 ; i < [o_children count] ; i++)
349                 {
350                     p_category_item = [o_children objectAtIndex: i];
351                     if( p_category_item->i_object_id == i_category )
352                     {
353                         b_found = VLC_TRUE;
354                         break;
355                     }
356                 }
357                 if( !b_found ) continue;
358                 
359                 /* Find subcategory item */
360                 b_found = VLC_FALSE;
361                 cookie = -1;
362                 for (i = 0 ; i < [p_category_item->o_children count] ; i++)
363                 {
364                     p_subcategory_item = [p_category_item->o_children objectAtIndex: i];
365                     if( p_subcategory_item->i_object_id == i_subcategory )
366                     {
367                         b_found = VLC_TRUE;
368                         break;
369                     }
370                 }
371                 if( !b_found )
372                     p_subcategory_item = p_category_item;
373
374                 [p_subcategory_item->o_children addObject:[[VLCTreeItem alloc] initWithName:
375                     [[VLCMain sharedInstance] localizedString: p_module->psz_object_name ]
376                     ID: p_module->i_object_id parent:p_subcategory_item children:IsALeafNode whithCategory: -1]];
377
378             }
379         }
380         vlc_list_release( p_list );
381     }
382     return o_children;
383 }
384
385 - (int)getObjectID
386 {
387     return i_object_id;
388 }
389
390 - (NSString *)getName
391 {
392     return o_name;
393 }
394
395 - (VLCTreeItem *)childAtIndex:(int)i_index {
396     return [[self children] objectAtIndex:i_index];
397 }
398
399 - (int)numberOfChildren {
400     id i_tmp = [self children];
401     return (i_tmp == IsALeafNode) ? (-1) : (int)[i_tmp count];
402 }
403
404 - (BOOL)hasPrefs:(NSString *)o_module_name
405 {
406     intf_thread_t *p_intf = VLCIntf;
407     module_t *p_parser;
408     vlc_list_t *p_list;
409     char *psz_module_name;
410     int i_index;
411
412     psz_module_name = (char *)[o_module_name UTF8String];
413
414     /* look for module */
415     p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
416
417     for( i_index = 0; i_index < p_list->i_count; i_index++ )
418     {
419         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
420
421         if( !strcmp( p_parser->psz_object_name, psz_module_name ) )
422         {
423             BOOL b_has_prefs = p_parser->i_config_items != 0;
424             vlc_list_release( p_list );
425             return( b_has_prefs );
426         }
427     }
428
429     vlc_list_release( p_list );
430
431     return( NO );
432 }
433
434 - (NSView *)showView:(NSScrollView *)o_prefs_view
435 {
436 fprintf( stderr, "[%s] showView\n", [o_name UTF8String] );
437     vlc_list_t *p_list;
438     intf_thread_t *p_intf = VLCIntf;
439     module_t *p_parser;
440     module_config_t *p_item, *p_first_item;
441     NSView *o_view;
442     NSRect s_rc;                        /* rect                         */
443     NSTextField *o_text_field;         /* input field / label          */
444     NSRect s_vrc;                       /* view rect                    */
445     NSString *o_module_name;
446     int i_pos, i_module_tag;
447     NSPoint o_pos;
448     vlc_bool_t b_advanced = VLC_TRUE;
449     s_vrc = [[o_prefs_view contentView] bounds];
450     s_vrc.size.height -= 4;
451     
452     o_view = [[NSView alloc] initWithFrame: s_vrc];
453
454     p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
455     
456     /* Get a pointer to the module */
457     if( i_object_category == -1 )
458     {
459         p_parser = (module_t *) vlc_object_get( p_intf, i_object_id );
460         if( !p_parser || p_parser->i_object_type != VLC_OBJECT_MODULE )
461         {
462             /* 0OOoo something went really bad */
463             vlc_list_release( p_list );
464             return o_view;
465         }
466         o_module_name = [NSString stringWithUTF8String: p_parser->psz_object_name];    
467 fprintf( stderr, "showView: going to show [%d] %s\n", i_object_id, p_parser->psz_object_name );
468         p_first_item = p_item = p_parser->p_config;
469     }
470     else
471     {
472         int i_index;
473         if( !p_list ) return o_view;
474
475         /*
476         * Find the main module
477         */
478         for( i_index = 0; i_index < p_list->i_count; i_index++ )
479         {
480             p_parser = (module_t *)p_list->p_values[i_index].p_object;
481             if( !strcmp( p_parser->psz_object_name, "main" ) )
482                 break;
483         }
484         if( p_parser == NULL )
485         {
486             msg_Err( p_intf, "could not find the main module in our preferences" );
487             return o_view;
488         }
489         p_first_item = p_item = (p_parser->p_config + i_object_category);
490     }
491     o_view = nil;
492     i_module_tag = 3;
493
494 /* These defines should come from "Apple Human Interface Guidelines" */
495 #define X_ORIGIN 20
496 #define Y_ORIGIN 17
497 #define Y_INTER 8
498     /* Init View */
499     s_vrc = [[o_prefs_view contentView] bounds]; s_vrc.size.height -= 4;
500     o_view = [[VLCFlippedView alloc] initWithFrame: s_vrc];
501     [o_view setAutoresizingMask: NSViewWidthSizable];
502
503     o_pos.x = X_ORIGIN;
504     o_pos.y = Y_ORIGIN;
505     BOOL b_right_cat = TRUE;
506
507     if( p_item ) do ; while ( ( p_item->i_type == CONFIG_CATEGORY || p_item->i_type == CONFIG_SUBCATEGORY ) && p_item++ ); 
508
509     if( p_item ) do
510     {
511 fprintf( stderr, "Category: %d\n", p_item->i_type );
512         if( p_item->i_type == CONFIG_HINT_CATEGORY )
513         {
514             if( !strcmp( p_parser->psz_object_name, "main" ) &&
515                 [o_name isEqualToString: [[VLCMain sharedInstance] localizedString: p_item->psz_text]] )
516                 b_right_cat = TRUE;
517             else if( strcmp( p_parser->psz_object_name, "main" ) )
518                  b_right_cat = TRUE;
519             else b_right_cat = FALSE;
520         }
521         else if( p_item->i_type == CONFIG_HINT_END && !strcmp( p_parser->psz_object_name, "main" ) )
522             b_right_cat = FALSE;
523
524         if( (p_item->b_advanced && !b_advanced ) || !b_right_cat )
525         {
526             continue;
527         }
528 fprintf( stderr, "Creating view for: %s\n", p_item->psz_name );
529         VLCConfigControl *o_control = nil;
530         switch( p_item->i_type )
531         {
532             case CONFIG_ITEM_STRING:
533             {
534                 if( !p_item->i_list )
535                     o_control = [StringConfigControl newControl:p_item
536                                                       withView:o_view withObject:p_intf offset: o_pos];
537                 else
538                     o_control = [StringListConfigControl newControl:p_item
539                                                                 withView:o_view withObject:p_intf offset: o_pos];
540             }
541             break;
542             case CONFIG_ITEM_FILE:
543             case CONFIG_ITEM_DIRECTORY:
544             {
545                 o_control = [FileConfigControl newControl: p_item withView: o_view
546                                                     withObject: p_intf offset: o_pos];
547             }
548             break;
549             case CONFIG_ITEM_INTEGER:
550             {
551                 if( !p_item->i_list )
552                     o_control = [IntegerConfigControl newControl:p_item
553                                                             withView:o_view withObject:p_intf offset: o_pos];
554
555                 else if( p_item->i_min != 0 || p_item->i_max != 0 )
556                     o_control = [RangedIntegerConfigControl newControl:p_item
557                                                             withView:o_view withObject:p_intf offset: o_pos];
558                 else
559                     o_control = [IntegerListConfigControl newControl:p_item
560                                                                 withView:o_view withObject:p_intf offset: o_pos];
561             }
562             break;
563             case CONFIG_ITEM_KEY:
564             {
565                 o_control = [KeyConfigControl newControl:p_item withView:o_view withObject:p_intf offset: o_pos];
566             }
567             break;
568         }
569         if( o_control != nil )
570         {
571             [o_view addSubview: o_control];
572             o_pos.y += [o_control frame].size.height + Y_INTER;
573         }
574     #undef Y_ORIGIN
575     #undef X_ORIGIN
576     }
577     while( p_item->i_type != CONFIG_HINT_END && p_item->i_type != CONFIG_CATEGORY && p_item->i_type != CONFIG_SUBCATEGORY && p_item++ );
578
579     vlc_object_release( p_parser );
580     vlc_list_release( p_list );
581
582     [o_prefs_view setDocumentView: o_view];
583     [o_prefs_view setNeedsDisplay: TRUE];
584     return o_view;
585 }
586
587 @end
588
589
590 @implementation VLCFlippedView
591
592 - (BOOL)isFlipped
593 {
594     return( YES );
595 }
596
597 @end