]> git.sesse.net Git - vlc/blob - modules/gui/macosx/prefs.m
String Review round one, Mac OS X interface.
[vlc] / modules / gui / macosx / prefs.m
1 /*****************************************************************************
2  * prefs.m: MacOS X module for vlc
3  *****************************************************************************
4  * Copyright (C) 2002-2003 VideoLAN
5  * $Id: prefs.m,v 1.37 2004/01/25 17:01:57 murray Exp $
6  *
7  * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
8  *          Derk-Jan Hartman <thedj at users.sf.net>
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 = [NSApp getIntf];
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         return;
306     }
307     
308     /* Enumerate config options and add corresponding config boxes */
309     o_module_name = [NSString stringWithUTF8String: p_parser->psz_object_name];
310     p_item = p_parser->p_config;
311
312     i_pos = 0;
313     o_view = nil;
314     i_module_tag = 3;
315
316 #define X_ORIGIN 20
317 #define Y_ORIGIN (X_ORIGIN - 10)
318
319 #define CHECK_VIEW_HEIGHT \
320     { \
321         float f_new_pos = s_rc.origin.y + s_rc.size.height + X_ORIGIN; \
322         if( f_new_pos > s_vrc.size.height ) \
323         { \
324             s_vrc.size.height = f_new_pos; \
325             [o_view setFrame: s_vrc]; \
326         } \
327     }
328
329 #define CONTROL_LABEL( label ) \
330     { \
331         s_rc.origin.x += s_rc.size.width + 10; \
332         s_rc.size.width = s_vrc.size.width - s_rc.origin.x - X_ORIGIN - 20; \
333         o_text_field = [[NSTextField alloc] initWithFrame: s_rc]; \
334         [o_text_field setDrawsBackground: NO]; \
335         [o_text_field setBordered: NO]; \
336         [o_text_field setEditable: NO]; \
337         [o_text_field setSelectable: NO]; \
338         if ( label ) \
339         { \
340             [o_text_field setStringValue: \
341                 [NSApp localizedString: label]]; \
342         } \
343         [o_text_field sizeToFit]; \
344         [o_view addSubview: [o_text_field autorelease]]; \
345     }
346
347 #define INPUT_FIELD( ctype, cname, label, w, msg, param, tip ) \
348     { \
349         char * psz_duptip = NULL; \
350         if ( p_item->psz_longtext != NULL ) \
351             psz_duptip = strdup( p_item->psz_longtext ); \
352         s_rc.size.height = 25; \
353         s_rc.size.width = w; \
354         s_rc.origin.y += 10; \
355         CHECK_VIEW_HEIGHT; \
356         o_text_field = [[VLCTextField alloc] initWithFrame: s_rc]; \
357         [o_text_field setAlignment: NSRightTextAlignment]; \
358         CONTROL_CONFIG( o_text_field, o_module_name, ctype, cname ); \
359         [o_text_field msg: param]; \
360         if ( psz_duptip != NULL ) \
361         { \
362             [o_text_field setToolTip: [NSApp wrapString: [NSApp localizedString: \
363                                        psz_duptip] toWidth: PREFS_WRAP ]]; \
364             free(psz_duptip);\
365         } \
366         [o_view addSubview: [o_text_field autorelease]]; \
367         [[NSNotificationCenter defaultCenter] addObserver: self \
368             selector: @selector(configChanged:) \
369             name: NSControlTextDidChangeNotification \
370             object: o_text_field]; \
371         CONTROL_LABEL( label ); \
372         s_rc.origin.y += s_rc.size.height; \
373         s_rc.origin.x = X_ORIGIN; \
374     }
375
376 #define INPUT_FIELD_INTEGER( name, label, w, param, tip ) \
377     INPUT_FIELD( CONFIG_ITEM_INTEGER, name, label, w, setIntValue, param, tip )
378 #define INPUT_FIELD_FLOAT( name, label, w, param, tip ) \
379     INPUT_FIELD( CONFIG_ITEM_FLOAT, name, label, w, setFloatValue, param, tip )
380 #define INPUT_FIELD_STRING( name, label, w, param, tip ) \
381     INPUT_FIELD( CONFIG_ITEM_STRING, name, label, w, setStringValue, param, tip )
382
383     /* Init View */
384     s_vrc = [[o_prefs_view contentView] bounds]; s_vrc.size.height -= 4;
385     o_view = [[VLCFlippedView alloc] initWithFrame: s_vrc];
386     [o_view setAutoresizingMask: NSViewWidthSizable];
387     s_rc.origin.x = X_ORIGIN;
388     s_rc.origin.y = Y_ORIGIN;
389     BOOL b_right_cat = FALSE;
390
391     if( p_item ) do
392     {
393         if( p_item->i_type == CONFIG_HINT_CATEGORY )
394         {
395             if( !strcmp( p_parser->psz_object_name, "main" ) &&
396                 [o_item_name isEqualToString: [NSApp localizedString: p_item->psz_text]] )
397             {
398                 b_right_cat = TRUE;
399             } else if( strcmp( p_parser->psz_object_name, "main" ) )
400             {
401                  b_right_cat = TRUE;
402             } else b_right_cat = FALSE; 
403         } else if( p_item->i_type == CONFIG_HINT_END && !strcmp( p_parser->psz_object_name, "main" ) )
404         {
405             b_right_cat = FALSE;
406         }
407         
408         if( (p_item->b_advanced && !b_advanced ) || !b_right_cat )
409         {
410             continue;
411         }
412         switch( p_item->i_type )
413         {
414             case CONFIG_ITEM_MODULE:
415             {
416                 VLCPopUpButton *o_modules;
417                 module_t *p_a_module;
418                 char * psz_duptip = NULL;
419
420                 if ( p_item->psz_longtext != NULL )
421                     psz_duptip = strdup( p_item->psz_longtext );
422
423                 s_rc.size.height = 25;
424                 s_rc.size.width = 200;
425                 s_rc.origin.y += 10;
426                 
427                 CHECK_VIEW_HEIGHT;
428     
429                 o_modules = [[VLCPopUpButton alloc] initWithFrame: s_rc];
430                 CONTROL_CONFIG( o_modules, o_module_name,
431                                     CONFIG_ITEM_MODULE, p_item->psz_name );
432                 [o_modules setTarget: self];
433                 [o_modules setAction: @selector(configChanged:)];
434                 [o_modules sendActionOn:NSLeftMouseUpMask];
435                 
436                 if ( psz_duptip != NULL )
437                 {
438                     [o_modules setToolTip: [NSApp wrapString: [NSApp localizedString: psz_duptip] toWidth: PREFS_WRAP]];
439                     free( psz_duptip );
440                 }
441                 [o_view addSubview: [o_modules autorelease]];
442
443                 [o_modules addItemWithTitle: _NS("Default")];
444                 [[o_modules lastItem] setTag: -1];
445                 [o_modules selectItem: [o_modules lastItem]];
446
447                 /* build a list of available modules */
448                 {
449                     for( i_index = 0; i_index < p_list->i_count; i_index++ )
450                     {
451                         p_a_module = (module_t *)p_list->p_values[i_index].p_object ;
452     
453                         if( !strcmp( p_a_module->psz_capability,
454                                     p_item->psz_type ) )
455                         {
456                             NSString *o_description = [NSApp
457                                 localizedString: p_a_module->psz_longname];
458                             [o_modules addItemWithTitle: o_description];
459                             [[o_modules lastItem] setTag: p_a_module->i_object_id];
460
461                             if( p_item->psz_value &&
462                                 !strcmp( p_item->psz_value, p_a_module->psz_object_name ) )
463                             {
464                                 [o_modules selectItem:[o_modules lastItem]];
465                             }
466                         }
467                     }
468                 }
469
470                 CONTROL_LABEL( p_item->psz_text );
471                 s_rc.origin.y += s_rc.size.height;
472                 s_rc.origin.x = X_ORIGIN;
473             }
474             break;
475
476             case CONFIG_ITEM_FILE:
477             case CONFIG_ITEM_DIRECTORY:
478             {
479                 char *psz_duptip = NULL;
480                 char *psz_value = p_item->psz_value ?
481                                     p_item->psz_value : "";
482
483                 if ( p_item->psz_longtext != NULL )
484                     psz_duptip = strdup( p_item->psz_longtext );
485
486                 s_rc.origin.y += 10;
487                 s_rc.size.width = - 10;
488                 s_rc.size.height = 25;
489                 CHECK_VIEW_HEIGHT;
490                 CONTROL_LABEL( p_item->psz_text );
491                 s_rc.origin.x = X_ORIGIN;
492                 s_rc.origin.y += s_rc.size.height;
493                 CHECK_VIEW_HEIGHT;
494
495                 VLCButton *button = [[VLCButton alloc] initWithFrame: s_rc];
496                 CONTROL_CONFIG( button, o_module_name, CONFIG_ITEM_STRING , p_item->psz_name );
497                 [button setButtonType: NSMomentaryPushInButton];
498                 [button setBezelStyle: NSRoundedBezelStyle];
499                 [button setTitle: _NS("Browse...")];
500                 [button sizeToFit];
501                 [button setAutoresizingMask:NSViewMinXMargin];
502                 [button setFrameOrigin: NSMakePoint( s_vrc.size.width - ( 10 + [button frame].size.width), s_rc.origin.y)];
503
504                 [button setTarget: self];
505                 [button setAction: @selector(openFileDialog:)];
506
507                 s_rc.size.height = 25;
508                 s_rc.size.width = s_vrc.size.width - ( 35 + [button frame].size.width);
509                 
510                 o_text_field = [[VLCTextField alloc] initWithFrame: s_rc];
511                 CONTROL_CONFIG( o_text_field, o_module_name, CONFIG_ITEM_STRING , p_item->psz_name );
512
513                 [o_text_field setStringValue: [NSApp localizedString: psz_value]];
514                 if ( psz_duptip != NULL )
515                 {
516                     [o_text_field setToolTip: [NSApp wrapString: [NSApp localizedString:
517                                             psz_duptip] toWidth: PREFS_WRAP ]];
518                     free(psz_duptip);
519                 }
520                 
521                 [[NSNotificationCenter defaultCenter] addObserver: self
522                     selector: @selector(configChanged:)
523                     name: NSControlTextDidChangeNotification
524                     object: o_text_field];
525                 [o_text_field setAutoresizingMask:NSViewWidthSizable];
526                 [button setTag: (int) o_text_field ]; /* FIXME */
527                 
528                 [o_view addSubview: [o_text_field autorelease]];
529                 [o_view addSubview: [button autorelease]];
530                 s_rc.origin.y += s_rc.size.height;
531                 s_rc.origin.x = X_ORIGIN;
532             }
533             break;
534             
535             case CONFIG_ITEM_STRING:            
536             {
537                 if( !p_item->ppsz_list )
538                 {
539                     char *psz_value = p_item->psz_value ?
540                                     p_item->psz_value : "";
541     
542                     INPUT_FIELD_STRING( p_item->psz_name, p_item->psz_text, 200,
543                                         [NSApp localizedString: psz_value],
544                                         p_item->psz_longtext );
545                 }
546                 else
547                 {
548                     int i;
549                     VLCComboBox *o_combo_box;
550                     char * psz_duptip = NULL;
551                     if ( p_item->psz_longtext != NULL )
552                         psz_duptip = strdup( p_item->psz_longtext );
553     
554                     s_rc.size.height = 25;
555                     s_rc.size.width = 200;
556                     s_rc.origin.y += 10;
557     
558                     CHECK_VIEW_HEIGHT;
559     
560                     o_combo_box = [[VLCComboBox alloc] initWithFrame: s_rc];
561                     CONTROL_CONFIG( o_combo_box, o_module_name,
562                                     CONFIG_ITEM_STRING, p_item->psz_name );
563                     [o_combo_box setTarget: self];
564                     [o_combo_box setAction: @selector(configChanged:)];
565                     [o_combo_box sendActionOn:NSLeftMouseUpMask];
566                     [[NSNotificationCenter defaultCenter] addObserver: self
567                         selector: @selector(configChanged:)
568                         name: NSControlTextDidChangeNotification
569                         object: o_combo_box];
570
571                     if ( psz_duptip != NULL )
572                     {
573                         [o_combo_box setToolTip: [NSApp wrapString: [NSApp localizedString: psz_duptip] toWidth: PREFS_WRAP]];
574                         free( psz_duptip );
575                     }
576                     [o_view addSubview: [o_combo_box autorelease]];
577                     
578                     for( i=0; p_item->ppsz_list[i]; i++ )
579                     {
580                         [o_combo_box addItemWithObjectValue:
581                             [NSApp localizedString: p_item->ppsz_list[i]]];
582                     }
583                     [o_combo_box setStringValue: [NSApp localizedString: 
584                         p_item->psz_value ? p_item->psz_value : ""]];
585     
586                     CONTROL_LABEL( p_item->psz_text );
587     
588                     s_rc.origin.y += s_rc.size.height;
589                     s_rc.origin.x = X_ORIGIN;
590                 }
591     
592             }
593             break;
594     
595             case CONFIG_ITEM_INTEGER:
596             {
597                 if( p_item->i_min == p_item->i_max )
598                 {
599                     INPUT_FIELD_INTEGER( p_item->psz_name, p_item->psz_text, 70,
600                         p_item->i_value, p_item->psz_longtext );
601                 }
602                 else
603                 {
604                     /*create a slider */
605                     VLCSlider *o_slider;
606                     char * psz_duptip = NULL;
607                     if ( p_item->psz_longtext != NULL )
608                         psz_duptip = strdup( p_item->psz_longtext );
609         
610                     s_rc.size.height = 27;
611                     s_rc.size.width = 200;
612                     s_rc.origin.y += 10;
613         
614                     CHECK_VIEW_HEIGHT;
615         
616                     o_slider = [[VLCSlider alloc] initWithFrame: s_rc];
617                     [o_slider setMinValue: p_item->i_min];
618                     [o_slider setMaxValue: p_item->i_max];
619                     [o_slider setIntValue: p_item->i_value];
620
621                     if ( psz_duptip != NULL )
622                     {
623                         [o_slider setToolTip: [NSApp wrapString: [NSApp localizedString: psz_duptip] toWidth: PREFS_WRAP]];
624                         free( psz_duptip );
625                     }
626                     [o_slider setTarget: self];
627                     [o_slider setAction: @selector(configChanged:)];
628                     [o_slider sendActionOn:NSLeftMouseUpMask];
629                     CONTROL_CONFIG( o_slider, o_module_name,
630                                     CONFIG_ITEM_INTEGER, p_item->psz_name );
631                     [o_view addSubview: [o_slider autorelease]];
632                     CONTROL_LABEL( p_item->psz_text );
633         
634                     s_rc.origin.y += s_rc.size.height;
635                     s_rc.origin.x = X_ORIGIN;
636                 }
637             }
638             break;
639     
640             case CONFIG_ITEM_FLOAT:
641             {
642                 if( p_item->f_min == p_item->f_max )
643                 {
644                     INPUT_FIELD_FLOAT( p_item->psz_name, p_item->psz_text, 70,
645                         p_item->f_value, p_item->psz_longtext );
646                 }
647                 else
648                 {
649                     /* create a slider */
650                     VLCSlider *o_slider;
651                     char * psz_duptip = NULL;
652                     if ( p_item->psz_longtext != NULL )
653                         psz_duptip = strdup( p_item->psz_longtext );
654         
655                     s_rc.size.height = 27;
656                     s_rc.size.width = 200;
657                     s_rc.origin.y += 10;
658         
659                     CHECK_VIEW_HEIGHT;
660         
661                     o_slider = [[VLCSlider alloc] initWithFrame: s_rc];
662                     [o_slider setMinValue: p_item->f_min];
663                     [o_slider setMaxValue: p_item->f_max];
664                     [o_slider setFloatValue: p_item->f_value];
665
666                     if ( psz_duptip != NULL )
667                     {
668                         [o_slider setToolTip: [NSApp wrapString: [NSApp localizedString: psz_duptip] toWidth: PREFS_WRAP]];
669                         free( psz_duptip );
670                     }
671                     [o_slider setTarget: self];
672                     [o_slider setAction: @selector(configChanged:)];
673                     [o_slider sendActionOn:NSLeftMouseUpMask];
674                     CONTROL_CONFIG( o_slider, o_module_name,
675                                     CONFIG_ITEM_FLOAT, p_item->psz_name );
676                     [o_view addSubview: [o_slider autorelease]];
677                     CONTROL_LABEL( p_item->psz_text );
678         
679                     s_rc.origin.y += s_rc.size.height;
680                     s_rc.origin.x = X_ORIGIN;
681                 }
682             }
683             break;
684     
685             case CONFIG_ITEM_BOOL:
686             {
687                 VLCButton *o_btn_bool;
688                 char * psz_duptip = NULL;
689
690                 if ( p_item->psz_longtext != NULL )
691                     psz_duptip = strdup( p_item->psz_longtext );
692     
693                 s_rc.size.height = 27;
694                 s_rc.size.width = s_vrc.size.width - X_ORIGIN * 2 - 20;
695                 s_rc.origin.y += 10;
696     
697                 CHECK_VIEW_HEIGHT;
698     
699                 o_btn_bool = [[VLCButton alloc] initWithFrame: s_rc];
700                 [o_btn_bool setButtonType: NSSwitchButton];
701                 [o_btn_bool setIntValue: p_item->i_value];
702                 [o_btn_bool setTitle: [NSApp localizedString: p_item->psz_text]];
703                 if ( psz_duptip != NULL )
704                 {
705                     [o_btn_bool setToolTip: [NSApp wrapString: [NSApp localizedString: psz_duptip] toWidth: PREFS_WRAP]];
706                     free( psz_duptip );
707                 }
708                 [o_btn_bool setTarget: self];
709                 [o_btn_bool setAction: @selector(configChanged:)];
710                 CONTROL_CONFIG( o_btn_bool, o_module_name,
711                                 CONFIG_ITEM_BOOL, p_item->psz_name );
712                 [o_view addSubview: [o_btn_bool autorelease]];
713     
714                 s_rc.origin.y += s_rc.size.height;
715             }
716             break;
717
718             case CONFIG_ITEM_KEY:
719             {
720                 int i;
721                 char *psz_duptip = NULL;
722                 VLCComboBox *o_combo_box;
723
724                 if ( p_item->psz_longtext != NULL )
725                     psz_duptip = strdup( p_item->psz_longtext );
726
727                 s_rc.origin.y += 10;
728                 s_rc.size.width = - 10;
729                 s_rc.size.height = 20;
730                 CHECK_VIEW_HEIGHT;
731                 CONTROL_LABEL( p_item->psz_text );
732                 s_rc.origin.x = X_ORIGIN;
733                 s_rc.origin.y += s_rc.size.height;
734                 s_rc.size.width = s_vrc.size.width - X_ORIGIN * 2;
735                 CHECK_VIEW_HEIGHT;
736                 VLCMatrix *o_matrix = [[VLCMatrix alloc] initWithFrame: s_rc mode: NSHighlightModeMatrix cellClass: [NSButtonCell class] numberOfRows:2 numberOfColumns:2];
737                 NSArray *o_cells = [o_matrix cells];
738                 for( i=0; i < [o_cells count]; i++ )
739                 {
740                     NSButtonCell *o_current_cell = [o_cells objectAtIndex:i];
741                     [o_current_cell setButtonType: NSSwitchButton];
742                     [o_current_cell setControlSize: NSSmallControlSize];
743                     if( psz_duptip != NULL )
744                     {
745                         [o_matrix setToolTip: [NSApp wrapString: [NSApp localizedString: psz_duptip] toWidth: PREFS_WRAP] forCell: o_current_cell];
746                     }
747                     switch( i )
748                     {
749                         case 0:
750                             [o_current_cell setTitle:_NS("Command")];
751                             [o_current_cell setState: p_item->i_value & KEY_MODIFIER_COMMAND];
752                             break;
753                         case 1:
754                             [o_current_cell setTitle:_NS("Control")];
755                             [o_current_cell setState: p_item->i_value & KEY_MODIFIER_CTRL];
756                             break;
757                         case 2:
758                             [o_current_cell setTitle:_NS("Option/Alt")];
759                             [o_current_cell setState: p_item->i_value & KEY_MODIFIER_ALT];
760                             break;
761                         case 3:
762                             [o_current_cell setTitle:_NS("Shift")];
763                             [o_current_cell setState: p_item->i_value & KEY_MODIFIER_SHIFT];
764                             break;
765                     }
766                     [o_current_cell setTarget: self];
767                     [o_current_cell setAction: @selector(configChanged:)];
768                     [o_current_cell sendActionOn:NSLeftMouseUpMask];
769                 }
770                 CONTROL_CONFIG( o_matrix, o_module_name,
771                                 CONFIG_ITEM_KEY, p_item->psz_name );
772                 [o_matrix sizeToCells];
773                 [o_view addSubview: [o_matrix autorelease]];
774
775                 s_rc.origin.x += [o_matrix frame].size.width + 20;
776                 s_rc.size.height = 25;
777                 s_rc.size.width = 100;
778
779                 CHECK_VIEW_HEIGHT;
780
781                 o_combo_box = [[VLCComboBox alloc] initWithFrame: s_rc];
782                 CONTROL_CONFIG( o_combo_box, o_module_name,
783                                 CONFIG_ITEM_KEY, p_item->psz_name );
784                 [o_combo_box setTarget: self];
785                 [o_combo_box setAction: @selector(configChanged:)];
786                 [o_combo_box sendActionOn:NSLeftMouseUpMask];
787                 [[NSNotificationCenter defaultCenter] addObserver: self
788                         selector: @selector(configChanged:)
789                         name: NSControlTextDidChangeNotification
790                         object: o_combo_box];
791
792                 if ( psz_duptip != NULL )
793                 {
794                     [o_combo_box setToolTip: [NSApp wrapString: [NSApp localizedString: psz_duptip] toWidth: PREFS_WRAP]];
795                 }
796                 [o_view addSubview: [o_combo_box autorelease]];
797                 
798                 for( i = 0; i < sizeof(vlc_keys) / sizeof(key_descriptor_t); i++ )
799                 {
800                     
801                     if( vlc_keys[i].psz_key_string && *vlc_keys[i].psz_key_string )
802                     [o_combo_box addItemWithObjectValue: [NSApp localizedString:vlc_keys[i].psz_key_string]];
803                 }
804                 
805                 [o_combo_box setStringValue: [NSApp localizedString:KeyToString(( ((unsigned int)p_item->i_value) & ~KEY_MODIFIER ))]];
806                 
807                 s_rc.origin.y += s_rc.size.height;
808                 s_rc.origin.x = X_ORIGIN;
809                 if( psz_duptip ) free( psz_duptip );
810             }
811             break;
812     
813             }
814     
815     #undef INPUT_FIELD_INTEGER
816     #undef INPUT_FIELD_FLOAT
817     #undef INPUT_FIELD_STRING
818     #undef INPUT_FIELD
819     #undef CHECK_VIEW_HEIGHT
820     #undef CONTROL_LABEL
821     #undef Y_ORIGIN
822     #undef X_ORIGIN
823         }
824         while( p_item->i_type != CONFIG_HINT_END && p_item++ );
825         vlc_list_release( p_list );
826     
827     [o_prefs_view setDocumentView: o_view];
828     [o_prefs_view setNeedsDisplay: TRUE];
829 }
830
831
832 @end
833
834 @implementation VLCPrefs (NSTableDataSource)
835
836 - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
837     return (item == nil) ? [[VLCTreeItem rootItem] numberOfChildren] : [item numberOfChildren];
838 }
839
840 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
841     return (item == nil) ? YES : ([item numberOfChildren] != -1);
842 }
843
844 - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item {
845     return (item == nil) ? [[VLCTreeItem rootItem] childAtIndex:index] : [item childAtIndex:index];
846 }
847
848 - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item {
849     return (item == nil) ? @"" : (id)[item getName];
850 }
851
852 @end
853
854 @implementation VLCTreeItem
855
856 static VLCTreeItem *o_root_item = nil;
857
858 #define IsALeafNode ((id)-1)
859
860 - (id)initWithName: (NSString *)o_item_name ID: (int)i_id parent:(VLCTreeItem *)o_parent_item
861 {
862     self = [super init];
863
864     if( self != nil )
865     {
866         o_name = [o_item_name copy];
867         i_object_id = i_id;
868         o_parent = o_parent_item;
869     }
870     return( self );
871 }
872
873 + (VLCTreeItem *)rootItem {
874    if (o_root_item == nil) o_root_item = [[VLCTreeItem alloc] initWithName:@"main" ID: 0 parent:nil];
875    return o_root_item;       
876 }
877
878 - (void)dealloc
879 {
880     if (o_children != IsALeafNode) [o_children release];
881     [o_name release];
882     [super dealloc];
883 }
884
885 /* Creates and returns the array of children
886  * Loads children incrementally */
887 - (NSArray *)children {
888     if (o_children == NULL) {
889         intf_thread_t *p_intf = [NSApp getIntf];
890         vlc_list_t      *p_list;
891         module_t        *p_module = NULL;
892         module_config_t *p_item;
893         int i_index,j;
894
895         /* List the modules */
896         p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
897         if( !p_list ) return nil;
898
899         if( [[self getName] isEqualToString: @"main"] )
900         {
901             /*
902             * Build a tree of the main options
903             */
904             for( i_index = 0; i_index < p_list->i_count; i_index++ )
905             {
906                 p_module = (module_t *)p_list->p_values[i_index].p_object;
907                 if( !strcmp( p_module->psz_object_name, "main" ) )
908                     break;
909             }
910             if( p_module == NULL )
911             {
912                 msg_Err( p_intf, "could not find the main module in our preferences" );
913                 return nil;
914             }
915             if( i_index < p_list->i_count )
916             {
917                 /* We found the main module */
918         
919                 /* Enumerate config categories and store a reference so we can
920                  * generate their config panel them when it is asked by the user. */
921                 p_item = p_module->p_config;
922                 o_children = [[NSMutableArray alloc] initWithCapacity:10];
923
924                 if( p_item ) do
925                 {
926                     NSString *o_child_name;
927                     
928                     switch( p_item->i_type )
929                     {
930                     case CONFIG_HINT_CATEGORY:
931                         o_child_name = [NSApp localizedString: p_item->psz_text];
932                         [o_children addObject:[[VLCTreeItem alloc] initWithName: o_child_name
933                             ID: p_module->i_object_id parent:self]];
934                         break;
935                     }
936                 }
937                 while( p_item->i_type != CONFIG_HINT_END && p_item++ );
938                 
939                 /* Add the modules item */
940                 [o_children addObject:[[VLCTreeItem alloc] initWithName: _NS("Modules")
941                     ID: 0 parent:self]];
942             }
943             else
944             {
945                 o_children = IsALeafNode;
946             }
947         }
948         else if( [[self getName] isEqualToString: _NS("Modules")] )
949         {
950             /* Add the capabilities */
951             o_children = [[NSMutableArray alloc] initWithCapacity:10];
952             for( i_index = 0; i_index < p_list->i_count; i_index++ )
953             {
954                 p_module = (module_t *)p_list->p_values[i_index].p_object;
955         
956                 /* Exclude the main module */
957                 if( !strcmp( p_module->psz_object_name, "main" ) )
958                     continue;
959         
960                 /* Exclude empty modules */
961                 p_item = p_module->p_config;
962                 if( !p_item ) continue;
963                 do
964                 {
965                     if( p_item->i_type & CONFIG_ITEM )
966                         break;
967                 }
968                 while( p_item->i_type != CONFIG_HINT_END && p_item++ );
969                 if( p_item->i_type == CONFIG_HINT_END ) continue;
970         
971                 /* Create the capability tree if it doesn't already exist */
972                 NSString *o_capability;
973                 o_capability = [NSApp localizedString: p_module->psz_capability];
974                 if( !p_module->psz_capability || !*p_module->psz_capability )
975                 {
976                     /* Empty capability ? Let's look at the submodules */
977                     module_t * p_submodule;
978                     for( j = 0; j < p_module->i_children; j++ )
979                     {
980                         p_submodule = (module_t*)p_module->pp_children[ j ];
981                         if( p_submodule->psz_capability && *p_submodule->psz_capability )
982                         {
983                             o_capability = [NSApp localizedString: p_submodule->psz_capability];
984                             BOOL b_found = FALSE;
985                             for( j = 0; j < (int)[o_children count]; j++ )
986                             {
987                                 if( [[[o_children objectAtIndex:j] getName] isEqualToString: o_capability] )
988                                 {
989                                     b_found = TRUE;
990                                     break;
991                                 }
992                             }
993                             if( !b_found )
994                             {
995                                 [o_children addObject:[[VLCTreeItem alloc] initWithName: o_capability
996                                 ID: 0 parent:self]];
997                             }
998                         }
999                     }
1000                 }
1001
1002                 BOOL b_found = FALSE;
1003                 for( j = 0; j < (int)[o_children count]; j++ )
1004                 {
1005                     if( [[[o_children objectAtIndex:j] getName] isEqualToString: o_capability] )
1006                     {
1007                         b_found = TRUE;
1008                         break;
1009                     }
1010                 }
1011                 if( !b_found )
1012                 {
1013                     [o_children addObject:[[VLCTreeItem alloc] initWithName: o_capability
1014                     ID: 0 parent:self]];
1015                 }
1016             }
1017         }
1018         else if( [[o_parent getName] isEqualToString: _NS("Modules")] )
1019         {
1020             /* Now add the modules */
1021             o_children = [[NSMutableArray alloc] initWithCapacity:10];
1022             for( i_index = 0; i_index < p_list->i_count; i_index++ )
1023             {
1024                 p_module = (module_t *)p_list->p_values[i_index].p_object;
1025         
1026                 /* Exclude the main module */
1027                 if( !strcmp( p_module->psz_object_name, "main" ) )
1028                     continue;
1029         
1030                 /* Exclude empty modules */
1031                 p_item = p_module->p_config;
1032                 if( !p_item ) continue;
1033                 do
1034                 {
1035                     if( p_item->i_type & CONFIG_ITEM )
1036                         break;
1037                 }
1038                 while( p_item->i_type != CONFIG_HINT_END && p_item++ );
1039                 if( p_item->i_type == CONFIG_HINT_END ) continue;
1040         
1041                 /* Check the capability */
1042                 NSString *o_capability;
1043                 o_capability = [NSApp localizedString: p_module->psz_capability];
1044                 if( !p_module->psz_capability || !*p_module->psz_capability )
1045                 {
1046                     /* Empty capability ? Let's look at the submodules */
1047                     module_t * p_submodule;
1048                     for( j = 0; j < p_module->i_children; j++ )
1049                     {
1050                         p_submodule = (module_t*)p_module->pp_children[ j ];
1051                         if( p_submodule->psz_capability && *p_submodule->psz_capability )
1052                         {
1053                             o_capability = [NSApp localizedString: p_submodule->psz_capability];
1054                             if( [o_capability isEqualToString: [self getName]] )
1055                             {
1056                             [o_children addObject:[[VLCTreeItem alloc] initWithName:
1057                                 [NSApp localizedString: p_module->psz_object_name ]
1058                                 ID: p_module->i_object_id parent:self]];
1059                             }
1060                         }
1061                     }
1062                 }
1063                 else if( [o_capability isEqualToString: [self getName]] )
1064                 {
1065                     [o_children addObject:[[VLCTreeItem alloc] initWithName:
1066                         [NSApp localizedString: p_module->psz_object_name ]
1067                         ID: p_module->i_object_id parent:self]];
1068                 }
1069             }
1070         }
1071         else
1072         {
1073             /* all the other stuff are leafs */
1074             o_children = IsALeafNode;
1075         }
1076     }
1077     return o_children;
1078 }
1079
1080 - (int)getObjectID
1081 {
1082     return i_object_id;
1083 }
1084
1085 - (NSString *)getName
1086 {
1087     return o_name;
1088 }
1089
1090 - (VLCTreeItem *)childAtIndex:(int)i_index {
1091     return [[self children] objectAtIndex:i_index];
1092 }
1093
1094 - (int)numberOfChildren {
1095     id i_tmp = [self children];
1096     return (i_tmp == IsALeafNode) ? (-1) : (int)[i_tmp count];
1097 }
1098
1099 - (BOOL)hasPrefs:(NSString *)o_module_name
1100 {
1101     intf_thread_t *p_intf = [NSApp getIntf];
1102     module_t *p_parser;
1103     vlc_list_t *p_list;
1104     char *psz_module_name;
1105     int i_index;
1106
1107     psz_module_name = (char *)[o_module_name UTF8String];
1108
1109     /* look for module */
1110     p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
1111
1112     for( i_index = 0; i_index < p_list->i_count; i_index++ )
1113     {
1114         p_parser = (module_t *)p_list->p_values[i_index].p_object ;
1115
1116         if( !strcmp( p_parser->psz_object_name, psz_module_name ) )
1117         {
1118             BOOL b_has_prefs = p_parser->i_config_items != 0;
1119             vlc_list_release( p_list );
1120             return( b_has_prefs );
1121         }
1122     }
1123
1124     vlc_list_release( p_list );
1125
1126     return( NO );
1127 }
1128
1129 @end
1130
1131
1132 @implementation VLCFlippedView
1133
1134 - (BOOL)isFlipped
1135 {
1136     return( YES );
1137 }
1138
1139 @end
1140
1141 IMPL_CONTROL_CONFIG(Button);
1142 IMPL_CONTROL_CONFIG(PopUpButton);
1143 IMPL_CONTROL_CONFIG(ComboBox);
1144 IMPL_CONTROL_CONFIG(TextField);
1145 IMPL_CONTROL_CONFIG(Slider);
1146 IMPL_CONTROL_CONFIG(Matrix);