]> git.sesse.net Git - vlc/blob - modules/gui/macosx/prefs.m
* VLCPrefs's delloc method got accidentally removed while splitting the nibs. Re...
[vlc] / modules / gui / macosx / prefs.m
1 /*****************************************************************************
2  * prefs.m: MacOS X module for vlc
3  *****************************************************************************
4  * Copyright (C) 2002-2004 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 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>                                      /* malloc(), free() */
29 #include <sys/param.h>                                    /* for MAXPATHLEN */
30 #include <string.h>
31
32 #include "intf.h"
33 #include "prefs.h"
34 #include "vlc_keys.h"
35
36 /*****************************************************************************
37  * VLCPrefs implementation
38  *****************************************************************************/
39 @implementation VLCPrefs
40
41 static VLCPrefs *_o_sharedMainInstance = nil;
42
43 + (VLCPrefs *)sharedInstance
44 {
45     return _o_sharedMainInstance ? _o_sharedMainInstance : [[self alloc] init];
46 }
47
48 - (id)init
49 {
50     if( _o_sharedMainInstance) {
51         [self dealloc];
52     } else {
53         _o_sharedMainInstance = [super init];
54     }
55     
56     return _o_sharedMainInstance;
57 }
58
59 - (void)dealloc
60 {
61     [o_empty_view release];
62     [o_save_prefs release];
63     [super dealloc];
64 }
65
66 - (void)showPrefs
67 {
68     /* load our nib */
69     [NSBundle loadNibNamed:@"Preferences" owner:self];
70     
71     /* from "init" =< r8571 */
72     o_empty_view = [[NSView alloc] init];
73     o_save_prefs = [[NSMutableDictionary alloc] init];
74
75     /* from "awakeFromNib" =< r8571 */
76     p_intf = VLCIntf;
77     b_advanced = config_GetInt( p_intf, "advanced" );
78
79     [self initStrings];
80     [o_advanced_ckb setState: b_advanced];
81     [o_prefs_view setBorderType: NSGrooveBorder];
82     [o_prefs_view setHasVerticalScroller: YES];
83     [o_prefs_view setDrawsBackground: NO];
84     [o_prefs_view setRulersVisible: NO];
85     [o_prefs_view setDocumentView: o_empty_view];
86     [o_tree selectRow:0 byExtendingSelection:NO];
87     /* end of the previous "awakeFromNib" method */
88
89     [o_save_prefs release];
90     o_save_prefs = [[NSMutableDictionary alloc] init];
91     [self showViewForID: [[o_tree itemAtRow:[o_tree selectedRow]] getObjectID]
92         andName: [[o_tree itemAtRow:[o_tree selectedRow]] getName]];
93     [o_prefs_window center];
94     [o_prefs_window makeKeyAndOrderFront:self];
95 }
96
97 - (void)initStrings
98 {
99     [o_prefs_window setTitle: _NS("Preferences")];
100     [o_save_btn setTitle: _NS("Save")];
101     [o_cancel_btn setTitle: _NS("Cancel")];
102     [o_reset_btn setTitle: _NS("Reset All")];
103     [o_advanced_ckb setTitle: _NS("Advanced")];
104 }
105
106 - (IBAction)savePrefs: (id)sender
107 {
108     id o_vlc_config;
109     NSEnumerator *o_enum;
110
111     o_enum = [o_save_prefs objectEnumerator];
112     while( ( o_vlc_config = [o_enum nextObject] ) )
113     {
114         int i_type = [o_vlc_config configType];
115         NSString *o_name = [o_vlc_config configName];
116         char *psz_name = (char *)[o_name UTF8String];
117
118         switch( i_type )
119         {
120     
121         case CONFIG_ITEM_MODULE:
122             {
123                 char *psz_value;
124                 module_t *p_a_module;
125                 int i_id = [[o_vlc_config selectedItem] tag];
126                 
127                 p_a_module = (module_t *)vlc_object_get( p_intf, i_id );
128                 if( p_a_module == NULL || p_a_module->i_object_type != VLC_OBJECT_MODULE )
129                 {
130                     i_id = -1;
131                 }
132                 
133                 psz_value = ( i_id == -1 ) ? "" :  p_a_module->psz_object_name ;
134                 config_PutPsz( p_intf, psz_name, strdup(psz_value) );
135             }
136             break;
137     
138         case CONFIG_ITEM_STRING:
139             {
140                 char *psz_value;
141                 NSString *o_value = [o_vlc_config stringValue];
142                 psz_value = (char *)[o_value UTF8String];
143     
144                 config_PutPsz( p_intf, psz_name, psz_value );
145             }
146             break;
147
148         case CONFIG_ITEM_FILE:
149         case CONFIG_ITEM_DIRECTORY:
150             {
151                 char *psz_value;
152                 NSString *o_value = [o_vlc_config stringValue];
153                 psz_value = (char *)[o_value fileSystemRepresentation];
154     
155                 config_PutPsz( p_intf, psz_name, psz_value );
156             }
157             break;
158     
159         case CONFIG_ITEM_INTEGER:
160         case CONFIG_ITEM_BOOL:
161             {
162                 int i_value = [o_vlc_config intValue];
163     
164                 config_PutInt( p_intf, psz_name, i_value );
165             }
166             break;
167     
168         case CONFIG_ITEM_FLOAT:
169             {
170                 float f_value = [o_vlc_config floatValue];
171     
172                 config_PutFloat( p_intf, psz_name, f_value );
173             }
174             break;
175
176         case CONFIG_ITEM_KEY:
177             {
178                 unsigned int i_key = config_GetInt( p_intf, psz_name );
179                 unsigned int i_new_key = 0;
180
181                 if( [o_vlc_config class] == [VLCMatrix class] )
182                 {
183                     int i;
184                     NSButtonCell *o_current_cell;
185                     NSArray *o_cells = [o_vlc_config cells];
186                     i_new_key = (i_key & ~KEY_MODIFIER);
187                     for( i = 0; i < [o_cells count]; i++ )
188                     {
189                         o_current_cell = [o_cells objectAtIndex:i];
190                         if( [[o_current_cell title] isEqualToString:_NS("Command")] && 
191                             [o_current_cell state] == NSOnState )
192                                 i_new_key |= KEY_MODIFIER_COMMAND;
193                         if( [[o_current_cell title] isEqualToString:_NS("Control")] && 
194                             [o_current_cell state] == NSOnState )
195                                 i_new_key |= KEY_MODIFIER_CTRL;
196                         if( [[o_current_cell title] isEqualToString:_NS("Option/Alt")] && 
197                             [o_current_cell state] == NSOnState )
198                                 i_new_key |= KEY_MODIFIER_ALT;
199                         if( [[o_current_cell title] isEqualToString:_NS("Shift")] && 
200                             [o_current_cell state] == NSOnState )
201                                 i_new_key |= KEY_MODIFIER_SHIFT;
202                     }
203                 }
204                 else
205                 {
206                     i_new_key = (i_key & KEY_MODIFIER);
207                     i_new_key |= StringToKey([[o_vlc_config stringValue] cString]);
208                 }
209                 config_PutInt( p_intf, psz_name, i_new_key );
210             }
211             break;
212         }
213     }
214     config_SaveConfigFile( p_intf, NULL );
215     [o_prefs_window orderOut:self];
216 }
217
218 - (IBAction)closePrefs: (id)sender
219 {
220     [o_prefs_window orderOut:self];
221 }
222
223 - (IBAction)resetAll: (id)sender
224 {
225     NSBeginInformationalAlertSheet(_NS("Reset Preferences"), _NS("Cancel"), _NS("Continue"), 
226         nil, o_prefs_window, self, @selector(sheetDidEnd: returnCode: contextInfo:), NULL, nil,
227         _NS("Beware this will reset your VLC media player preferences.\n"
228             "Are you sure you want to continue?") );
229 }
230
231 - (void)sheetDidEnd:(NSWindow *)o_sheet returnCode:(int)i_return contextInfo:(void *)o_context
232 {
233     if( i_return == NSAlertAlternateReturn )
234     {
235         config_ResetAll( p_intf );
236         [self showViewForID: [[o_tree itemAtRow:[o_tree selectedRow]] getObjectID]
237             andName: [[o_tree itemAtRow:[o_tree selectedRow]] getName]];
238     }
239 }
240
241 - (IBAction)advancedToggle: (id)sender
242 {
243     b_advanced = !b_advanced;
244     [o_advanced_ckb setState: b_advanced];
245     [self showViewForID: [[o_tree itemAtRow:[o_tree selectedRow]] getObjectID]
246         andName: [[o_tree itemAtRow:[o_tree selectedRow]] getName]];
247 }
248
249 - (IBAction)openFileDialog: (id)sender
250 {
251     NSOpenPanel *o_open_panel = [NSOpenPanel openPanel];
252     
253     [o_open_panel setTitle: _NS("Select file or directory")];
254     [o_open_panel setPrompt: _NS("Select")];
255     [o_open_panel setAllowsMultipleSelection: NO];
256     [o_open_panel setCanChooseFiles: YES];
257     [o_open_panel setCanChooseDirectories: YES];
258     [o_open_panel beginSheetForDirectory:nil
259         file:nil
260         types:nil
261         modalForWindow:[sender window]
262         modalDelegate: self
263         didEndSelector: @selector(pathChosenInPanel: 
264                         withReturn:
265                         contextInfo:)
266         contextInfo: sender];
267 }
268
269 - (void)pathChosenInPanel:(NSOpenPanel *)o_sheet withReturn:(int)i_return_code contextInfo:(void  *)o_context_info
270 {
271     if( i_return_code == NSOKButton )
272     {
273         NSString *o_path = [[o_sheet filenames] objectAtIndex: 0];
274         VLCTextField *o_field = (VLCTextField *)[(VLCButton *)o_context_info tag]; /* FIXME */
275         [o_field setStringValue: o_path];
276         [self configChanged: o_field];
277     }
278 }
279
280 - (void)loadConfigTree
281 {
282     
283 }
284
285 - (void)outlineViewSelectionIsChanging:(NSNotification *)o_notification
286 {
287 }
288
289 - (void)outlineViewSelectionDidChange:(NSNotification *)o_notification
290 {
291     [self showViewForID: [[o_tree itemAtRow:[o_tree selectedRow]] getObjectID]
292         andName: [[o_tree itemAtRow:[o_tree selectedRow]] getName]];
293 }
294
295 - (void)configChanged:(id)o_unknown
296 {
297     id o_vlc_config = [o_unknown isKindOfClass: [NSNotification class]] ?
298                       [o_unknown object] : o_unknown;
299
300     NSString *o_name = [o_vlc_config configName];
301     [o_save_prefs setObject: o_vlc_config forKey: o_name];
302 }
303
304 - (void)showViewForID: (int)i_id andName: (NSString *)o_item_name
305 {
306     vlc_list_t *p_list;
307     module_t *p_parser;
308     module_config_t *p_item;
309     
310     int i_pos, i_module_tag, i_index;
311     
312     NSString *o_module_name;
313     NSRect s_rc;                        /* rect                         */
314     NSView *o_view;                     /* view                         */
315     NSRect s_vrc;                       /* view rect                    */
316     VLCTextField *o_text_field;         /* input field / label          */
317     
318     p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
319
320     /* Get a pointer to the module */
321     p_parser = (module_t *)vlc_object_get( p_intf, i_id );
322     if( p_parser->i_object_type != VLC_OBJECT_MODULE )
323     {
324         /* 0OOoo something went really bad */
325         vlc_list_release( p_list );
326         return;
327     }
328     
329     /* Enumerate config options and add corresponding config boxes */
330     o_module_name = [NSString stringWithUTF8String: p_parser->psz_object_name];
331     p_item = p_parser->p_config;
332
333     i_pos = 0;
334     o_view = nil;
335     i_module_tag = 3;
336
337 #define X_ORIGIN 20
338 #define Y_ORIGIN (X_ORIGIN - 10)
339
340 #define CHECK_VIEW_HEIGHT \
341     { \
342         float f_new_pos = s_rc.origin.y + s_rc.size.height + X_ORIGIN; \
343         if( f_new_pos > s_vrc.size.height ) \
344         { \
345             s_vrc.size.height = f_new_pos; \
346             [o_view setFrame: s_vrc]; \
347         } \
348     }
349
350 #define CONTROL_LABEL( label ) \
351     { \
352         s_rc.origin.x += s_rc.size.width + 10; \
353         s_rc.size.width = s_vrc.size.width - s_rc.origin.x - X_ORIGIN - 20; \
354         o_text_field = [[NSTextField alloc] initWithFrame: s_rc]; \
355         [o_text_field setDrawsBackground: NO]; \
356         [o_text_field setBordered: NO]; \
357         [o_text_field setEditable: NO]; \
358         [o_text_field setSelectable: NO]; \
359         if ( label ) \
360         { \
361             [o_text_field setStringValue: \
362                 [[VLCMain sharedInstance] localizedString: label]]; \
363         } \
364         [o_text_field sizeToFit]; \
365         [o_view addSubview: [o_text_field autorelease]]; \
366     }
367
368 #define INPUT_FIELD( ctype, cname, label, w, msg, param, tip ) \
369     { \
370         char * psz_duptip = NULL; \
371         if ( p_item->psz_longtext != NULL ) \
372             psz_duptip = strdup( p_item->psz_longtext ); \
373         s_rc.size.height = 25; \
374         s_rc.size.width = w; \
375         s_rc.origin.y += 10; \
376         CHECK_VIEW_HEIGHT; \
377         o_text_field = [[VLCTextField alloc] initWithFrame: s_rc]; \
378         [o_text_field setAlignment: NSRightTextAlignment]; \
379         CONTROL_CONFIG( o_text_field, o_module_name, ctype, cname ); \
380         [o_text_field msg: param]; \
381         if ( psz_duptip != NULL ) \
382         { \
383             [o_text_field setToolTip: [[VLCMain sharedInstance] wrapString: [[VLCMain sharedInstance] localizedString: \
384                                        psz_duptip] toWidth: PREFS_WRAP ]]; \
385             free(psz_duptip);\
386         } \
387         [o_view addSubview: [o_text_field autorelease]]; \
388         [[NSNotificationCenter defaultCenter] addObserver: self \
389             selector: @selector(configChanged:) \
390             name: NSControlTextDidChangeNotification \
391             object: o_text_field]; \
392         CONTROL_LABEL( label ); \
393         s_rc.origin.y += s_rc.size.height; \
394         s_rc.origin.x = X_ORIGIN; \
395     }
396
397 #define INPUT_FIELD_INTEGER( name, label, w, param, tip ) \
398     INPUT_FIELD( CONFIG_ITEM_INTEGER, name, label, w, setIntValue, param, tip )
399 #define INPUT_FIELD_FLOAT( name, label, w, param, tip ) \
400     INPUT_FIELD( CONFIG_ITEM_FLOAT, name, label, w, setFloatValue, param, tip )
401 #define INPUT_FIELD_STRING( name, label, w, param, tip ) \
402     INPUT_FIELD( CONFIG_ITEM_STRING, name, label, w, setStringValue, param, tip )
403
404     /* Init View */
405     s_vrc = [[o_prefs_view contentView] bounds]; s_vrc.size.height -= 4;
406     o_view = [[VLCFlippedView alloc] initWithFrame: s_vrc];
407     [o_view setAutoresizingMask: NSViewWidthSizable];
408     s_rc.origin.x = X_ORIGIN;
409     s_rc.origin.y = Y_ORIGIN;
410     BOOL b_right_cat = TRUE;
411
412     if( p_item ) do
413     {
414         if( p_item->i_type == CONFIG_HINT_CATEGORY )
415         {
416             if( !strcmp( p_parser->psz_object_name, "main" ) &&
417                 [o_item_name isEqualToString: [[VLCMain sharedInstance] localizedString: p_item->psz_text]] )
418             {
419                 b_right_cat = TRUE;
420             } else if( strcmp( p_parser->psz_object_name, "main" ) )
421             {
422                  b_right_cat = TRUE;
423             } else b_right_cat = FALSE; 
424         } else if( p_item->i_type == CONFIG_HINT_END && !strcmp( p_parser->psz_object_name, "main" ) )
425         {
426             b_right_cat = FALSE;
427         }
428         
429         if( (p_item->b_advanced && !b_advanced ) || !b_right_cat )
430         {
431             continue;
432         }
433         switch( p_item->i_type )
434         {
435             case CONFIG_ITEM_MODULE:
436             {
437                 VLCPopUpButton *o_modules;
438                 module_t *p_a_module;
439                 char * psz_duptip = NULL;
440
441                 if ( p_item->psz_longtext != NULL )
442                     psz_duptip = strdup( p_item->psz_longtext );
443
444                 s_rc.size.height = 25;
445                 s_rc.size.width = 200;
446                 s_rc.origin.y += 10;
447                 
448                 CHECK_VIEW_HEIGHT;
449     
450                 o_modules = [[VLCPopUpButton alloc] initWithFrame: s_rc];
451                 CONTROL_CONFIG( o_modules, o_module_name,
452                                     CONFIG_ITEM_MODULE, p_item->psz_name );
453                 [o_modules setTarget: self];
454                 [o_modules setAction: @selector(configChanged:)];
455                 [o_modules sendActionOn:NSLeftMouseUpMask];
456                 
457                 if ( psz_duptip != NULL )
458                 {
459                     [o_modules setToolTip: [[VLCMain sharedInstance] wrapString: [[VLCMain sharedInstance] localizedString: psz_duptip] toWidth: PREFS_WRAP]];
460                     free( psz_duptip );
461                 }
462                 [o_view addSubview: [o_modules autorelease]];
463
464                 [o_modules addItemWithTitle: _NS("Default")];
465                 [[o_modules lastItem] setTag: -1];
466                 [o_modules selectItem: [o_modules lastItem]];
467
468                 /* build a list of available modules */
469                 {
470                     for( i_index = 0; i_index < p_list->i_count; i_index++ )
471                     {
472                         p_a_module = (module_t *)p_list->p_values[i_index].p_object ;
473     
474                         if( !strcmp( p_a_module->psz_capability,
475                                     p_item->psz_type ) )
476                         {
477                             NSString *o_description = [[VLCMain sharedInstance]
478                                 localizedString: p_a_module->psz_longname];
479                             [o_modules addItemWithTitle: o_description];
480                             [[o_modules lastItem] setTag: p_a_module->i_object_id];
481
482                             if( p_item->psz_value &&
483                                 !strcmp( p_item->psz_value, p_a_module->psz_object_name ) )
484                             {
485                                 [o_modules selectItem:[o_modules lastItem]];
486                             }
487                         }
488                     }
489                 }
490
491                 CONTROL_LABEL( p_item->psz_text );
492                 s_rc.origin.y += s_rc.size.height;
493                 s_rc.origin.x = X_ORIGIN;
494             }
495             break;
496
497             case CONFIG_ITEM_FILE:
498             case CONFIG_ITEM_DIRECTORY:
499             {
500                 char *psz_duptip = NULL;
501                 char *psz_value = p_item->psz_value ?
502                                     p_item->psz_value : "";
503
504                 if ( p_item->psz_longtext != NULL )
505                     psz_duptip = strdup( p_item->psz_longtext );
506
507                 s_rc.origin.y += 10;
508                 s_rc.size.width = - 10;
509                 s_rc.size.height = 25;
510                 CHECK_VIEW_HEIGHT;
511                 CONTROL_LABEL( p_item->psz_text );
512                 s_rc.origin.x = X_ORIGIN;
513                 s_rc.origin.y += s_rc.size.height;
514                 CHECK_VIEW_HEIGHT;
515
516                 VLCButton *button = [[VLCButton alloc] initWithFrame: s_rc];
517                 CONTROL_CONFIG( button, o_module_name, CONFIG_ITEM_STRING , p_item->psz_name );
518                 [button setButtonType: NSMomentaryPushInButton];
519                 [button setBezelStyle: NSRoundedBezelStyle];
520                 [button setTitle: _NS("Browse...")];
521                 [button sizeToFit];
522                 [button setAutoresizingMask:NSViewMinXMargin];
523                 [button setFrameOrigin: NSMakePoint( s_vrc.size.width - ( 10 + [button frame].size.width), s_rc.origin.y)];
524
525                 [button setTarget: self];
526                 [button setAction: @selector(openFileDialog:)];
527
528                 s_rc.size.height = 25;
529                 s_rc.size.width = s_vrc.size.width - ( 35 + [button frame].size.width);
530                 
531                 o_text_field = [[VLCTextField alloc] initWithFrame: s_rc];
532                 CONTROL_CONFIG( o_text_field, o_module_name, CONFIG_ITEM_STRING , p_item->psz_name );
533
534                 [o_text_field setStringValue: [[VLCMain sharedInstance] localizedString: psz_value]];
535                 if ( psz_duptip != NULL )
536                 {
537                     [o_text_field setToolTip: [[VLCMain sharedInstance] wrapString: [[VLCMain sharedInstance] localizedString:
538                                             psz_duptip] toWidth: PREFS_WRAP ]];
539                     free(psz_duptip);
540                 }
541                 
542                 [[NSNotificationCenter defaultCenter] addObserver: self
543                     selector: @selector(configChanged:)
544                     name: NSControlTextDidChangeNotification
545                     object: o_text_field];
546                 [o_text_field setAutoresizingMask:NSViewWidthSizable];
547                 [button setTag: (int) o_text_field ]; /* FIXME */
548                 
549                 [o_view addSubview: [o_text_field autorelease]];
550                 [o_view addSubview: [button autorelease]];
551                 s_rc.origin.y += s_rc.size.height;
552                 s_rc.origin.x = X_ORIGIN;
553             }
554             break;
555             
556             case CONFIG_ITEM_STRING:            
557             {
558                 if( !p_item->ppsz_list )
559                 {
560                     char *psz_value = p_item->psz_value ?
561                                     p_item->psz_value : "";
562     
563                     INPUT_FIELD_STRING( p_item->psz_name, p_item->psz_text, 200,
564                                         [[VLCMain sharedInstance] localizedString: psz_value],
565                                         p_item->psz_longtext );
566                 }
567                 else
568                 {
569                     int i;
570                     VLCComboBox *o_combo_box;
571                     char * psz_duptip = NULL;
572                     if ( p_item->psz_longtext != NULL )
573                         psz_duptip = strdup( p_item->psz_longtext );
574     
575                     s_rc.size.height = 25;
576                     s_rc.size.width = 200;
577                     s_rc.origin.y += 10;
578     
579                     CHECK_VIEW_HEIGHT;
580     
581                     o_combo_box = [[VLCComboBox alloc] initWithFrame: s_rc];
582                     CONTROL_CONFIG( o_combo_box, o_module_name,
583                                     CONFIG_ITEM_STRING, p_item->psz_name );
584                     [o_combo_box setTarget: self];
585                     [o_combo_box setAction: @selector(configChanged:)];
586                     [o_combo_box sendActionOn:NSLeftMouseUpMask];
587                     [[NSNotificationCenter defaultCenter] addObserver: self
588                         selector: @selector(configChanged:)
589                         name: NSControlTextDidChangeNotification
590                         object: o_combo_box];
591
592                     if ( psz_duptip != NULL )
593                     {
594                         [o_combo_box setToolTip: [[VLCMain sharedInstance] wrapString: [[VLCMain sharedInstance] localizedString: psz_duptip] toWidth: PREFS_WRAP]];
595                         free( psz_duptip );
596                     }
597                     [o_view addSubview: [o_combo_box autorelease]];
598                     
599                     for( i=0; p_item->ppsz_list[i]; i++ )
600                     {
601                         [o_combo_box addItemWithObjectValue:
602                             [[VLCMain sharedInstance] localizedString: p_item->ppsz_list[i]]];
603                     }
604                     [o_combo_box setStringValue: [[VLCMain sharedInstance] localizedString: 
605                         p_item->psz_value ? p_item->psz_value : ""]];
606     
607                     CONTROL_LABEL( p_item->psz_text );
608     
609                     s_rc.origin.y += s_rc.size.height;
610                     s_rc.origin.x = X_ORIGIN;
611                 }
612     
613             }
614             break;
615     
616             case CONFIG_ITEM_INTEGER:
617             {
618                 if( p_item->i_min == p_item->i_max )
619                 {
620                     INPUT_FIELD_INTEGER( p_item->psz_name, p_item->psz_text, 70,
621                         p_item->i_value, p_item->psz_longtext );
622                 }
623                 else
624                 {
625                     /*create a slider */
626                     VLCSlider *o_slider;
627                     char * psz_duptip = NULL;
628                     if ( p_item->psz_longtext != NULL )
629                         psz_duptip = strdup( p_item->psz_longtext );
630         
631                     s_rc.size.height = 27;
632                     s_rc.size.width = 200;
633                     s_rc.origin.y += 10;
634         
635                     CHECK_VIEW_HEIGHT;
636         
637                     o_slider = [[VLCSlider alloc] initWithFrame: s_rc];
638                     [o_slider setMinValue: p_item->i_min];
639                     [o_slider setMaxValue: p_item->i_max];
640                     [o_slider setIntValue: p_item->i_value];
641
642                     if ( psz_duptip != NULL )
643                     {
644                         [o_slider setToolTip: [[VLCMain sharedInstance] wrapString: [[VLCMain sharedInstance] localizedString: psz_duptip] toWidth: PREFS_WRAP]];
645                         free( psz_duptip );
646                     }
647                     [o_slider setTarget: self];
648                     [o_slider setAction: @selector(configChanged:)];
649                     [o_slider sendActionOn:NSLeftMouseUpMask];
650                     CONTROL_CONFIG( o_slider, o_module_name,
651                                     CONFIG_ITEM_INTEGER, p_item->psz_name );
652                     [o_view addSubview: [o_slider autorelease]];
653                     CONTROL_LABEL( p_item->psz_text );
654         
655                     s_rc.origin.y += s_rc.size.height;
656                     s_rc.origin.x = X_ORIGIN;
657                 }
658             }
659             break;
660     
661             case CONFIG_ITEM_FLOAT:
662             {
663                 if( p_item->f_min == p_item->f_max )
664                 {
665                     INPUT_FIELD_FLOAT( p_item->psz_name, p_item->psz_text, 70,
666                         p_item->f_value, p_item->psz_longtext );
667                 }
668                 else
669                 {
670                     /* create a slider */
671                     VLCSlider *o_slider;
672                     char * psz_duptip = NULL;
673                     if ( p_item->psz_longtext != NULL )
674                         psz_duptip = strdup( p_item->psz_longtext );
675         
676                     s_rc.size.height = 27;
677                     s_rc.size.width = 200;
678                     s_rc.origin.y += 10;
679         
680                     CHECK_VIEW_HEIGHT;
681         
682                     o_slider = [[VLCSlider alloc] initWithFrame: s_rc];
683                     [o_slider setMinValue: p_item->f_min];
684                     [o_slider setMaxValue: p_item->f_max];
685                     [o_slider setFloatValue: p_item->f_value];
686
687                     if ( psz_duptip != NULL )
688                     {
689                         [o_slider setToolTip: [[VLCMain sharedInstance] wrapString: [[VLCMain sharedInstance] localizedString: psz_duptip] toWidth: PREFS_WRAP]];
690                         free( psz_duptip );
691                     }
692                     [o_slider setTarget: self];
693                     [o_slider setAction: @selector(configChanged:)];
694                     [o_slider sendActionOn:NSLeftMouseUpMask];
695                     CONTROL_CONFIG( o_slider, o_module_name,
696                                     CONFIG_ITEM_FLOAT, p_item->psz_name );
697                     [o_view addSubview: [o_slider autorelease]];
698                     CONTROL_LABEL( p_item->psz_text );
699         
700                     s_rc.origin.y += s_rc.size.height;
701                     s_rc.origin.x = X_ORIGIN;
702                 }
703             }
704             break;
705     
706             case CONFIG_ITEM_BOOL:
707             {
708                 VLCButton *o_btn_bool;
709                 char * psz_duptip = NULL;
710
711                 if ( p_item->psz_longtext != NULL )
712                     psz_duptip = strdup( p_item->psz_longtext );
713     
714                 s_rc.size.height = 27;
715                 s_rc.size.width = s_vrc.size.width - X_ORIGIN * 2 - 20;
716                 s_rc.origin.y += 10;
717     
718                 CHECK_VIEW_HEIGHT;
719     
720                 o_btn_bool = [[VLCButton alloc] initWithFrame: s_rc];
721                 [o_btn_bool setButtonType: NSSwitchButton];
722                 [o_btn_bool setIntValue: p_item->i_value];
723                 [o_btn_bool setTitle: [[VLCMain sharedInstance] localizedString: p_item->psz_text]];
724                 if ( psz_duptip != NULL )
725                 {
726                     [o_btn_bool setToolTip: [[VLCMain sharedInstance] wrapString: [[VLCMain sharedInstance] localizedString: psz_duptip] toWidth: PREFS_WRAP]];
727                     free( psz_duptip );
728                 }
729                 [o_btn_bool setTarget: self];
730                 [o_btn_bool setAction: @selector(configChanged:)];
731                 CONTROL_CONFIG( o_btn_bool, o_module_name,
732                                 CONFIG_ITEM_BOOL, p_item->psz_name );
733                 [o_view addSubview: [o_btn_bool autorelease]];
734     
735                 s_rc.origin.y += s_rc.size.height;
736             }
737             break;
738
739             case CONFIG_ITEM_KEY:
740             {
741                 int i;
742                 char *psz_duptip = NULL;
743                 VLCComboBox *o_combo_box;
744
745                 if ( p_item->psz_longtext != NULL )
746                     psz_duptip = strdup( p_item->psz_longtext );
747
748                 s_rc.origin.y += 10;
749                 s_rc.size.width = - 10;
750                 s_rc.size.height = 20;
751                 CHECK_VIEW_HEIGHT;
752                 CONTROL_LABEL( p_item->psz_text );
753                 s_rc.origin.x = X_ORIGIN;
754                 s_rc.origin.y += s_rc.size.height;
755                 s_rc.size.width = s_vrc.size.width - X_ORIGIN * 2;
756                 CHECK_VIEW_HEIGHT;
757                 VLCMatrix *o_matrix = [[VLCMatrix alloc] initWithFrame: s_rc mode: NSHighlightModeMatrix cellClass: [NSButtonCell class] numberOfRows:2 numberOfColumns:2];
758                 NSArray *o_cells = [o_matrix cells];
759                 for( i=0; i < [o_cells count]; i++ )
760                 {
761                     NSButtonCell *o_current_cell = [o_cells objectAtIndex:i];
762                     [o_current_cell setButtonType: NSSwitchButton];
763                     [o_current_cell setControlSize: NSSmallControlSize];
764                     if( psz_duptip != NULL )
765                     {
766                         [o_matrix setToolTip: [[VLCMain sharedInstance] wrapString: [[VLCMain sharedInstance] localizedString: psz_duptip] toWidth: PREFS_WRAP] forCell: o_current_cell];
767                     }
768                     switch( i )
769                     {
770                         case 0:
771                             [o_current_cell setTitle:_NS("Command")];
772                             [o_current_cell setState: p_item->i_value & KEY_MODIFIER_COMMAND];
773                             break;
774                         case 1:
775                             [o_current_cell setTitle:_NS("Control")];
776                             [o_current_cell setState: p_item->i_value & KEY_MODIFIER_CTRL];
777                             break;
778                         case 2:
779                             [o_current_cell setTitle:_NS("Option/Alt")];
780                             [o_current_cell setState: p_item->i_value & KEY_MODIFIER_ALT];
781                             break;
782                         case 3:
783                             [o_current_cell setTitle:_NS("Shift")];
784                             [o_current_cell setState: p_item->i_value & KEY_MODIFIER_SHIFT];
785                             break;
786                     }
787                     [o_current_cell setTarget: self];
788                     [o_current_cell setAction: @selector(configChanged:)];
789                     [o_current_cell sendActionOn:NSLeftMouseUpMask];
790                 }
791                 CONTROL_CONFIG( o_matrix, o_module_name,
792                                 CONFIG_ITEM_KEY, p_item->psz_name );
793                 [o_matrix sizeToCells];
794                 [o_view addSubview: [o_matrix autorelease]];
795
796                 s_rc.origin.x += [o_matrix frame].size.width + 20;
797                 s_rc.size.height = 25;
798                 s_rc.size.width = 100;
799
800                 CHECK_VIEW_HEIGHT;
801
802                 o_combo_box = [[VLCComboBox alloc] initWithFrame: s_rc];
803                 CONTROL_CONFIG( o_combo_box, o_module_name,
804                                 CONFIG_ITEM_KEY, p_item->psz_name );
805                 [o_combo_box setTarget: self];
806                 [o_combo_box setAction: @selector(configChanged:)];
807                 [o_combo_box sendActionOn:NSLeftMouseUpMask];
808                 [[NSNotificationCenter defaultCenter] addObserver: self
809                         selector: @selector(configChanged:)
810                         name: NSControlTextDidChangeNotification
811                         object: o_combo_box];
812
813                 if ( psz_duptip != NULL )
814                 {
815                     [o_combo_box setToolTip: [[VLCMain sharedInstance] wrapString: [[VLCMain sharedInstance] localizedString: psz_duptip] toWidth: PREFS_WRAP]];
816                 }
817                 [o_view addSubview: [o_combo_box autorelease]];
818                 
819                 for( i = 0; i < sizeof(vlc_keys) / sizeof(key_descriptor_t); i++ )
820                 {
821                     
822                     if( vlc_keys[i].psz_key_string && *vlc_keys[i].psz_key_string )
823                     [o_combo_box addItemWithObjectValue: [[VLCMain sharedInstance] localizedString:vlc_keys[i].psz_key_string]];
824                 }
825                 
826                 [o_combo_box setStringValue: [[VLCMain sharedInstance] localizedString:KeyToString(( ((unsigned int)p_item->i_value) & ~KEY_MODIFIER ))]];
827                 
828                 s_rc.origin.y += s_rc.size.height;
829                 s_rc.origin.x = X_ORIGIN;
830                 if( psz_duptip ) free( psz_duptip );
831             }
832             break;
833     
834             }
835     
836     #undef INPUT_FIELD_INTEGER
837     #undef INPUT_FIELD_FLOAT
838     #undef INPUT_FIELD_STRING
839     #undef INPUT_FIELD
840     #undef CHECK_VIEW_HEIGHT
841     #undef CONTROL_LABEL
842     #undef Y_ORIGIN
843     #undef X_ORIGIN
844         }
845         while( p_item->i_type != CONFIG_HINT_END && p_item++ );
846         vlc_object_release( p_parser );
847         vlc_list_release( p_list );
848     
849     [o_prefs_view setDocumentView: o_view];
850     [o_prefs_view setNeedsDisplay: TRUE];
851 }
852
853
854 @end
855
856 @implementation VLCPrefs (NSTableDataSource)
857
858 - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
859     return (item == nil) ? [[VLCTreeItem rootItem] numberOfChildren] : [item numberOfChildren];
860 }
861
862 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
863     return (item == nil) ? YES : ([item numberOfChildren] != -1);
864 }
865
866 - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item {
867     return (item == nil) ? [[VLCTreeItem rootItem] childAtIndex:index] : [item childAtIndex:index];
868 }
869
870 - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item {
871     return (item == nil) ? @"" : (id)[item getName];
872 }
873
874 @end
875
876 @implementation VLCTreeItem
877
878 static VLCTreeItem *o_root_item = nil;
879
880 #define IsALeafNode ((id)-1)
881
882 - (id)initWithName: (NSString *)o_item_name ID: (int)i_id parent:(VLCTreeItem *)o_parent_item
883 {
884     self = [super init];
885
886     if( self != nil )
887     {
888         o_name = [o_item_name copy];
889         i_object_id = i_id;
890         o_parent = o_parent_item;
891     }
892     return( self );
893 }
894
895 + (VLCTreeItem *)rootItem {
896    if (o_root_item == nil) o_root_item = [[VLCTreeItem alloc] initWithName:@"main" ID: 0 parent:nil];
897    return o_root_item;       
898 }
899
900 - (void)dealloc
901 {
902     if (o_children != IsALeafNode) [o_children release];
903     [o_name release];
904     [super dealloc];
905 }
906
907 /* Creates and returns the array of children
908  * Loads children incrementally */
909 - (NSArray *)children {
910     if (o_children == NULL) {
911         intf_thread_t *p_intf = VLCIntf;
912         vlc_list_t      *p_list;
913         module_t        *p_module = NULL;
914         module_config_t *p_item;
915         int i_index,j;
916
917         /* List the modules */
918         p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
919         if( !p_list ) return nil;
920
921         if( [[self getName] isEqualToString: @"main"] )
922         {
923             /*
924             * Build a tree of the main options
925             */
926             for( i_index = 0; i_index < p_list->i_count; i_index++ )
927             {
928                 p_module = (module_t *)p_list->p_values[i_index].p_object;
929                 if( !strcmp( p_module->psz_object_name, "main" ) )
930                     break;
931             }
932             if( p_module == NULL )
933             {
934                 msg_Err( p_intf, "could not find the main module in our preferences" );
935                 return nil;
936             }
937             if( i_index < p_list->i_count )
938             {
939                 /* We found the main module */
940         
941                 /* Enumerate config categories and store a reference so we can
942                  * generate their config panel them when it is asked by the user. */
943                 p_item = p_module->p_config;
944                 o_children = [[NSMutableArray alloc] initWithCapacity:10];
945
946                 if( p_item ) do
947                 {
948                     NSString *o_child_name;
949                     
950                     switch( p_item->i_type )
951                     {
952                     case CONFIG_HINT_CATEGORY:
953                         o_child_name = [[VLCMain sharedInstance] localizedString: p_item->psz_text];
954                         [o_children addObject:[[VLCTreeItem alloc] initWithName: o_child_name
955                             ID: p_module->i_object_id parent:self]];
956                         break;
957                     }
958                 }
959                 while( p_item->i_type != CONFIG_HINT_END && p_item++ );
960                 
961                 /* Add the modules item */
962                 [o_children addObject:[[VLCTreeItem alloc] initWithName: _NS("Modules")
963                     ID: 0 parent:self]];
964             }
965             else
966             {
967                 o_children = IsALeafNode;
968             }
969         }
970         else if( [[self getName] isEqualToString: _NS("Modules")] )
971         {
972             /* Add the capabilities */
973             o_children = [[NSMutableArray alloc] initWithCapacity:10];
974             for( i_index = 0; i_index < p_list->i_count; i_index++ )
975             {
976                 p_module = (module_t *)p_list->p_values[i_index].p_object;
977         
978                 /* Exclude the main module */
979                 if( !strcmp( p_module->psz_object_name, "main" ) )
980                     continue;
981         
982                 /* Exclude empty modules */
983                 p_item = p_module->p_config;
984                 if( !p_item ) continue;
985                 do
986                 {
987                     if( p_item->i_type & CONFIG_ITEM )
988                         break;
989                 }
990                 while( p_item->i_type != CONFIG_HINT_END && p_item++ );
991                 if( p_item->i_type == CONFIG_HINT_END ) continue;
992         
993                 /* Create the capability tree if it doesn't already exist */
994                 NSString *o_capability;
995                 o_capability = [[VLCMain sharedInstance] localizedString: p_module->psz_capability];
996                 if( !p_module->psz_capability || !*p_module->psz_capability )
997                 {
998                     /* Empty capability ? Let's look at the submodules */
999                     module_t * p_submodule;
1000                     for( j = 0; j < p_module->i_children; j++ )
1001                     {
1002                         p_submodule = (module_t*)p_module->pp_children[ j ];
1003                         if( p_submodule->psz_capability && *p_submodule->psz_capability )
1004                         {
1005                             o_capability = [[VLCMain sharedInstance] localizedString: p_submodule->psz_capability];
1006                             BOOL b_found = FALSE;
1007                             for( j = 0; j < (int)[o_children count]; j++ )
1008                             {
1009                                 if( [[[o_children objectAtIndex:j] getName] isEqualToString: o_capability] )
1010                                 {
1011                                     b_found = TRUE;
1012                                     break;
1013                                 }
1014                             }
1015                             if( !b_found )
1016                             {
1017                                 [o_children addObject:[[VLCTreeItem alloc] initWithName: o_capability
1018                                 ID: 0 parent:self]];
1019                             }
1020                         }
1021                     }
1022                 }
1023
1024                 BOOL b_found = FALSE;
1025                 for( j = 0; j < (int)[o_children count]; j++ )
1026                 {
1027                     if( [[[o_children objectAtIndex:j] getName] isEqualToString: o_capability] )
1028                     {
1029                         b_found = TRUE;
1030                         break;
1031                     }
1032                 }
1033                 if( !b_found )
1034                 {
1035                     [o_children addObject:[[VLCTreeItem alloc] initWithName: o_capability
1036                     ID: 0 parent:self]];
1037                 }
1038             }
1039         }
1040         else if( [[o_parent getName] isEqualToString: _NS("Modules")] )
1041         {
1042             /* Now add the modules */
1043             o_children = [[NSMutableArray alloc] initWithCapacity:10];
1044             for( i_index = 0; i_index < p_list->i_count; i_index++ )
1045             {
1046                 p_module = (module_t *)p_list->p_values[i_index].p_object;
1047         
1048                 /* Exclude the main module */
1049                 if( !strcmp( p_module->psz_object_name, "main" ) )
1050                     continue;
1051         
1052                 /* Exclude empty modules */
1053                 p_item = p_module->p_config;
1054                 if( !p_item ) continue;
1055                 do
1056                 {
1057                     if( p_item->i_type & CONFIG_ITEM )
1058                         break;
1059                 }
1060                 while( p_item->i_type != CONFIG_HINT_END && p_item++ );
1061                 if( p_item->i_type == CONFIG_HINT_END ) continue;
1062         
1063                 /* Check the capability */
1064                 NSString *o_capability;
1065                 o_capability = [[VLCMain sharedInstance] localizedString: p_module->psz_capability];
1066                 if( !p_module->psz_capability || !*p_module->psz_capability )
1067                 {
1068                     /* Empty capability ? Let's look at the submodules */
1069                     module_t * p_submodule;
1070                     for( j = 0; j < p_module->i_children; j++ )
1071                     {
1072                         p_submodule = (module_t*)p_module->pp_children[ j ];
1073                         if( p_submodule->psz_capability && *p_submodule->psz_capability )
1074                         {
1075                             o_capability = [[VLCMain sharedInstance] localizedString: p_submodule->psz_capability];
1076                             if( [o_capability isEqualToString: [self getName]] )
1077                             {
1078                             [o_children addObject:[[VLCTreeItem alloc] initWithName:
1079                                 [[VLCMain sharedInstance] localizedString: p_module->psz_object_name ]
1080                                 ID: p_module->i_object_id parent:self]];
1081                             }
1082                         }
1083                     }
1084                 }
1085                 else if( [o_capability isEqualToString: [self getName]] )
1086                 {
1087                     [o_children addObject:[[VLCTreeItem alloc] initWithName:
1088                         [[VLCMain sharedInstance] localizedString: p_module->psz_object_name ]
1089                         ID: p_module->i_object_id parent:self]];
1090                 }
1091             }
1092         }
1093         else
1094         {
1095             /* all the other stuff are leafs */
1096             o_children = IsALeafNode;
1097         }
1098         vlc_list_release( p_list );
1099     }
1100     return o_children;
1101 }
1102
1103 - (int)getObjectID
1104 {
1105     return i_object_id;
1106 }
1107
1108 - (NSString *)getName
1109 {
1110     return o_name;
1111 }
1112
1113 - (VLCTreeItem *)childAtIndex:(int)i_index {
1114     return [[self children] objectAtIndex:i_index];
1115 }
1116
1117 - (int)numberOfChildren {
1118     id i_tmp = [self children];
1119     return (i_tmp == IsALeafNode) ? (-1) : (int)[i_tmp count];
1120 }
1121
1122 - (BOOL)hasPrefs:(NSString *)o_module_name
1123 {
1124     intf_thread_t *p_intf = VLCIntf;
1125     module_t *p_parser;
1126     vlc_list_t *p_list;
1127     char *psz_module_name;
1128     int i_index;
1129
1130     psz_module_name = (char *)[o_module_name UTF8String];
1131
1132     /* look for module */
1133     p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1134
1135     for( i_index = 0; i_index < p_list->i_count; i_index++ )
1136     {
1137         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1138
1139         if( !strcmp( p_parser->psz_object_name, psz_module_name ) )
1140         {
1141             BOOL b_has_prefs = p_parser->i_config_items != 0;
1142             vlc_list_release( p_list );
1143             return( b_has_prefs );
1144         }
1145     }
1146
1147     vlc_list_release( p_list );
1148
1149     return( NO );
1150 }
1151
1152 @end
1153
1154
1155 @implementation VLCFlippedView
1156
1157 - (BOOL)isFlipped
1158 {
1159     return( YES );
1160 }
1161
1162 @end
1163
1164 IMPL_CONTROL_CONFIG(Button);
1165 IMPL_CONTROL_CONFIG(PopUpButton);
1166 IMPL_CONTROL_CONFIG(ComboBox);
1167 IMPL_CONTROL_CONFIG(TextField);
1168 IMPL_CONTROL_CONFIG(Slider);
1169 IMPL_CONTROL_CONFIG(Matrix);