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