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