]> git.sesse.net Git - vlc/blob - modules/gui/macosx/prefs.m
* ./modules/gui/macosx/prefs.m: new configuration interface
[vlc] / modules / gui / macosx / prefs.m
1 /*****************************************************************************
2  * prefs.m: MacOS X plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id: prefs.m,v 1.1 2002/11/05 03:57:16 jlj Exp $
6  *
7  * Authors: Jon Lech Johansen <jon-vl@nanocrew.net> 
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  * 
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <sys/param.h>                                    /* for MAXPATHLEN */
29 #include <string.h>
30
31 #import <Cocoa/Cocoa.h>
32
33 #include <vlc/vlc.h>
34 #include <vlc/intf.h>
35
36 #import "intf.h"
37 #import "prefs.h"
38
39 /*****************************************************************************
40  * VLCPrefs implementation 
41  *****************************************************************************/
42 @implementation VLCPrefs
43
44 - (id)init
45 {
46     self = [super init];
47
48     if( self != nil )
49     {
50         p_intf = [NSApp getIntf];
51
52         o_pref_panels = [[NSMutableDictionary alloc] init];  
53         o_toolbars = [[NSMutableDictionary alloc] init];
54         o_scroll_views = [[NSMutableDictionary alloc] init];
55         o_panel_views = [[NSMutableDictionary alloc] init];
56         o_save_prefs = [[NSMutableDictionary alloc] init];
57     }
58
59     return( self );
60 }
61
62 - (void)dealloc
63 {
64     id v1, v2;
65     NSEnumerator *o_e1;
66     NSEnumerator *o_e2;
67
68 #define DIC_REL1(o_dic) \
69     { \
70     o_e1 = [o_dic objectEnumerator]; \
71     while( (v1 = [o_e1 nextObject]) ) \
72     { \
73         [v1 release]; \
74     } \
75     [o_dic removeAllObjects]; \
76     [o_dic release]; \
77     }
78
79 #define DIC_REL2(o_dic) \
80     { \
81         o_e2 = [o_dic objectEnumerator]; \
82         while( (v2 = [o_e2 nextObject]) ) \
83         { \
84             DIC_REL1(v2); \
85         } \
86         [o_dic removeAllObjects]; \
87     }
88
89     DIC_REL1(o_pref_panels);
90     DIC_REL2(o_toolbars);
91     DIC_REL1(o_scroll_views);
92     DIC_REL2(o_panel_views);
93     DIC_REL1(o_save_prefs);
94
95 #undef DIC_REL1
96 #undef DIC_REL2
97
98     [super dealloc];
99 }
100
101 - (BOOL)hasPrefs:(NSString *)o_module_name
102 {
103     module_t **pp_parser;
104     vlc_list_t *p_list;
105     char *psz_module_name;
106
107     psz_module_name = (char *)[o_module_name lossyCString];
108
109     /* look for module */
110     p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
111
112     for( pp_parser = (module_t **)p_list->pp_objects ;
113          *pp_parser ;
114          pp_parser++ )
115     {
116         if( !strcmp( (*pp_parser)->psz_object_name, psz_module_name ) )
117         {
118             BOOL b_has_prefs = (*pp_parser)->i_config_items != 0;
119             vlc_list_release( p_list );
120             return( b_has_prefs );
121         }
122     }
123
124     vlc_list_release( p_list );
125
126     return( NO );
127 }
128
129 - (void)createPrefPanel:(NSString *)o_module_name
130 {
131     int i_pos;
132     int i_module_tag;
133
134     module_t **pp_parser;
135     vlc_list_t *p_list;
136     module_config_t *p_item;
137     char *psz_module_name;
138
139     NSPanel *o_panel;                   /* panel                        */
140     NSRect s_panel_rc;                  /* panel rect                   */
141     NSView *o_panel_view;               /* panel view                   */
142     NSToolbar *o_toolbar;               /* panel toolbar                */
143     NSMutableDictionary *o_tb_items;    /* panel toolbar items          */
144     NSScrollView *o_scroll_view;        /* panel scroll view            */ 
145     NSRect s_scroll_rc;                 /* panel scroll view rect       */
146     NSMutableDictionary *o_views;       /* panel scroll view docviews   */
147
148     NSRect s_rc;                        /* rect                         */
149     NSView *o_view;                     /* view                         */
150     NSRect s_vrc;                       /* view rect                    */
151     NSButton *o_button;                 /* button                       */
152     NSRect s_brc;                       /* button rect                  */
153     VLCTextField *o_text_field;         /* input field / label          */
154
155     o_panel = [o_pref_panels objectForKey: o_module_name];
156     if( o_panel != nil )
157     {
158         [o_panel center];
159         [o_panel makeKeyAndOrderFront: nil];
160         return;
161     } 
162
163     psz_module_name = (char *)[o_module_name lossyCString];
164
165     /* Look for the selected module */
166     p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
167
168     for( pp_parser = (module_t **)p_list->pp_objects ;
169          *pp_parser ;
170          pp_parser++ )
171     {
172         if( psz_module_name
173             && !strcmp( psz_module_name, (*pp_parser)->psz_object_name ) )
174         {
175             break;
176         }
177     }
178
179     if( !(*pp_parser) )
180     {
181         vlc_list_release( p_list );
182         return;
183     }
184
185     /* We found it, now we can start building its configuration interface */
186
187     s_panel_rc = NSMakeRect( 0, 0, 450, 450 );
188     o_panel = [[NSPanel alloc] initWithContentRect: s_panel_rc
189                                styleMask: NSTitledWindowMask
190                                backing: NSBackingStoreBuffered
191                                defer: YES];
192     o_toolbar = [[NSToolbar alloc] initWithIdentifier: o_module_name];
193     [o_panel setTitle: [NSString stringWithFormat: @"%@ (%@)", 
194                                  _NS("Preferences"), o_module_name]];
195     o_panel_view = [o_panel contentView];
196
197     s_scroll_rc = s_panel_rc; 
198     s_scroll_rc.size.height -= 55; s_scroll_rc.origin.y += 55;
199     o_scroll_view = [[NSScrollView alloc] initWithFrame: s_scroll_rc];
200     [o_scroll_views setObject: o_scroll_view forKey: o_module_name];
201     [o_scroll_view setBorderType: NSGrooveBorder]; 
202     [o_scroll_view setHasVerticalScroller: YES];
203     [o_scroll_view setDrawsBackground: NO];
204     [o_scroll_view setRulersVisible: YES];
205     [o_panel_view addSubview: o_scroll_view];
206
207     o_tb_items = [[NSMutableDictionary alloc] init];
208     o_views = [[NSMutableDictionary alloc] init];
209
210     [o_save_prefs setObject: [[NSMutableArray alloc] init]
211                   forKey: o_module_name];
212
213     /* Enumerate config options and add corresponding config boxes */
214     p_item = (*pp_parser)->p_config;
215
216     i_pos = 0;
217     o_view = nil;
218     i_module_tag = 3;
219
220 #define X_ORIGIN 20
221 #define Y_ORIGIN (X_ORIGIN - 10) 
222
223 #define CHECK_VIEW_HEIGHT \
224     { \
225         float f_new_pos = s_rc.origin.y + s_rc.size.height + X_ORIGIN; \
226         if( f_new_pos > s_vrc.size.height ) \
227         { \
228             s_vrc.size.height = f_new_pos; \
229             [o_view setFrame: s_vrc]; \
230         } \
231     }
232
233 #define CONTROL_LABEL( label ) \
234     { \
235         s_rc.origin.x += s_rc.size.width + 10; \
236         s_rc.size.width = s_vrc.size.width - s_rc.origin.x - X_ORIGIN - 20; \
237         o_text_field = [[NSTextField alloc] initWithFrame: s_rc]; \
238         [o_text_field setDrawsBackground: NO]; \
239         [o_text_field setBordered: NO]; \
240         [o_text_field setEditable: NO]; \
241         [o_text_field setSelectable: NO]; \
242         [o_text_field setStringValue: \
243                 [NSString stringWithCString: label]]; \
244         [o_view addSubview: [o_text_field autorelease]]; \
245     }
246
247 #define INPUT_FIELD( ctype, cname, label, w, msg, param ) \
248     { \
249         s_rc.size.height = 23; \
250         s_rc.size.width = w; \
251         s_rc.origin.y += 10; \
252         CHECK_VIEW_HEIGHT; \
253         o_text_field = [[VLCTextField alloc] initWithFrame: s_rc]; \
254         [o_text_field setAlignment: NSRightTextAlignment]; \
255         CONTROL_CONFIG( o_text_field, o_module_name, ctype, cname ); \
256         [o_text_field msg: param]; \
257         [o_view addSubview: [o_text_field autorelease]]; \
258         [[NSNotificationCenter defaultCenter] addObserver: self \
259             selector: @selector(configChanged:) \
260             name: NSControlTextDidChangeNotification \
261             object: o_text_field]; \
262         CONTROL_LABEL( label ); \
263         s_rc.origin.y += s_rc.size.height; \
264         s_rc.origin.x = X_ORIGIN; \
265     }
266
267 #define INPUT_FIELD_INTEGER( name, label, w, param ) \
268     INPUT_FIELD( CONFIG_ITEM_INTEGER, name, label, w, setIntValue, param )
269 #define INPUT_FIELD_FLOAT( name, label, w, param ) \
270     INPUT_FIELD( CONFIG_ITEM_FLOAT, name, label, w, setFloatValue, param )
271 #define INPUT_FIELD_STRING( name, label, w, param ) \
272     INPUT_FIELD( CONFIG_ITEM_STRING, name, label, w, setStringValue, param )
273
274     if( p_item ) do
275     {
276
277         switch( p_item->i_type )
278         {
279
280         case CONFIG_HINT_CATEGORY:
281         {
282             NSString *o_key;
283             NSString *o_label;
284             NSToolbarItem *o_tbi;
285
286             o_label = [NSString stringWithCString: p_item->psz_text];
287             o_tbi = [[NSToolbarItem alloc] initWithItemIdentifier: o_label]; 
288             [o_tbi setImage: [NSImage imageNamed: @"NSApplicationIcon"]];
289             [o_tbi setLabel: o_label];
290             [o_tbi setTarget: self];
291             [o_tbi setAction: @selector(selectPrefView:)];
292
293             o_key = [NSString stringWithFormat: @"%02d %@",
294                                                 i_pos, o_label]; 
295             [o_tb_items setObject: o_tbi forKey: o_key];
296
297             s_vrc = s_scroll_rc; s_vrc.size.height -= 4;
298             o_view = [[VLCFlippedView alloc] initWithFrame: s_vrc];
299             [o_views setObject: o_view forKey: o_label];
300
301             s_rc.origin.x = X_ORIGIN;
302             s_rc.origin.y = Y_ORIGIN;
303
304             i_module_tag = 3;
305
306             if( i_pos == 0 )
307             {
308                 [o_scroll_view setDocumentView: o_view]; 
309             }
310
311             i_pos++;
312         }
313         break;
314
315         case CONFIG_ITEM_MODULE:
316         {
317             NSBox *o_box;
318             NSRect s_crc;
319             NSView *o_cview;
320             NSPopUpButton *o_modules;
321             NSButton *o_btn_select;
322             NSButton *o_btn_configure;
323
324 #define MODULE_BUTTON( button, title, sel ) \
325     { \
326         s_brc.size.height = 25; \
327         s_brc.origin.x += s_brc.size.width + 10; \
328         s_brc.size.width = s_crc.size.width - s_brc.origin.x - 10; \
329         button = [[NSButton alloc] initWithFrame: s_brc]; \
330         [button setButtonType: NSMomentaryPushInButton]; \
331         [button setBezelStyle: NSRoundedBezelStyle]; \
332         [button setTitle: title]; \
333         [button setTag: i_module_tag++]; \
334         [button setTarget: self]; \
335         [button setAction: @selector(sel)]; \
336         [o_cview addSubview: [button autorelease]]; \
337     }
338
339             s_rc.size.height = 100;
340             s_rc.size.width = s_vrc.size.width - X_ORIGIN * 2 - 20;
341             s_rc.origin.y += i_module_tag == 3 ? Y_ORIGIN : 20;
342
343             CHECK_VIEW_HEIGHT;
344
345             o_box = [[NSBox alloc] initWithFrame: s_rc];
346             [o_box setTitle: [NSString stringWithCString: p_item->psz_text]];
347             [o_view addSubview: [o_box autorelease]];
348             s_rc.origin.y += s_rc.size.height + 10;
349             o_cview = [[VLCFlippedView alloc] initWithFrame: s_rc];
350             [o_box setContentView: [o_cview autorelease]];
351             s_crc = [o_cview bounds];
352
353             s_brc = NSMakeRect( 5, 10, 200, 23 ); 
354             o_modules = [[NSPopUpButton alloc] initWithFrame: s_brc];
355             [o_modules setTag: i_module_tag++];
356             [o_modules setTarget: self];
357             [o_modules setAction: @selector(moduleSelected:)];
358             [o_cview addSubview: [o_modules autorelease]]; 
359
360             MODULE_BUTTON( o_btn_configure, _NS("Configure"), 
361                            configureModule: );
362
363             s_brc = NSMakeRect( 8, s_brc.origin.y + s_brc.size.height + 10, 
364                                 194, 23 ); 
365             o_text_field = [[VLCTextField alloc] initWithFrame: s_brc];
366             [o_text_field setTag: i_module_tag++];
367             [o_text_field setAlignment: NSLeftTextAlignment];
368             CONTROL_CONFIG( o_text_field, o_module_name, 
369                             CONFIG_ITEM_MODULE, p_item->psz_name );
370             [[NSNotificationCenter defaultCenter] addObserver: self
371                 selector: @selector(configChanged:)
372                 name: NSControlTextDidChangeNotification
373                 object: o_text_field];
374             [o_cview addSubview: [o_text_field autorelease]];
375
376             s_brc.origin.x += 3;
377             MODULE_BUTTON( o_btn_select, _NS("Select"), 
378                            selectModule: );
379
380             [o_modules addItemWithTitle: _NS("None")];
381
382             /* build a list of available modules */
383             {
384                 pp_parser = (module_t **)p_list->pp_objects;
385
386                 for( ; *pp_parser ; pp_parser++ )
387                 {
388                     if( !strcmp( (*pp_parser)->psz_capability,
389                                  p_item->psz_type ) )
390                     {
391                         NSString *o_object_name = [NSString 
392                             stringWithCString: (*pp_parser)->psz_object_name];
393                         [o_modules addItemWithTitle: o_object_name];
394                     }
395                 }
396             }
397
398             if( p_item->psz_value != NULL )
399             {
400                 NSString *o_value =
401                     [NSString stringWithCString: p_item->psz_value];
402
403                 [o_text_field setStringValue: o_value]; 
404                 [o_modules selectItemWithTitle: o_value]; 
405                 [o_btn_configure setEnabled: [self hasPrefs: o_value]]; 
406             }
407             else
408             {
409                 [o_modules selectItemWithTitle: _NS("None")];
410                 [o_btn_configure setEnabled: NO];
411             }
412
413 #undef MODULE_BUTTON
414         }
415         break;
416
417         case CONFIG_ITEM_STRING:
418         case CONFIG_ITEM_FILE:
419         {
420
421             if( !p_item->ppsz_list )
422             {
423                 char *psz_value = p_item->psz_value ?
424                                   p_item->psz_value : "";
425
426                 INPUT_FIELD_STRING( p_item->psz_name, p_item->psz_text, 150,
427                                     [NSString stringWithCString: psz_value] );
428             }
429             else
430             {
431                 int i;
432                 VLCComboBox *o_combo_box;
433
434                 s_rc.size.height = 25;
435                 s_rc.size.width = 150;
436                 s_rc.origin.y += 10;
437
438                 CHECK_VIEW_HEIGHT;
439
440                 o_combo_box = [[VLCComboBox alloc] initWithFrame: s_rc];
441                 CONTROL_CONFIG( o_combo_box, o_module_name, 
442                                 CONFIG_ITEM_STRING, p_item->psz_name );
443                 [o_view addSubview: [o_combo_box autorelease]];
444                 [[NSNotificationCenter defaultCenter] addObserver: self
445                     selector: @selector(configChanged:)
446                     name: NSControlTextDidChangeNotification
447                     object: o_combo_box];
448                 [[NSNotificationCenter defaultCenter] addObserver: self
449                     selector: @selector(configChanged:)
450                     name: NSComboBoxSelectionDidChangeNotification 
451                     object: o_combo_box];
452
453                 for( i=0; p_item->ppsz_list[i]; i++ )
454                 {
455                     [o_combo_box addItemWithObjectValue:
456                         [NSString stringWithCString: p_item->ppsz_list[i]]]; 
457                 }
458
459                 CONTROL_LABEL( p_item->psz_text ); 
460
461                 s_rc.origin.y += s_rc.size.height;
462                 s_rc.origin.x = X_ORIGIN;
463             }
464
465         }
466         break;
467
468         case CONFIG_ITEM_INTEGER:
469         {
470             INPUT_FIELD_INTEGER( p_item->psz_name, p_item->psz_text, 70, 
471                                  p_item->i_value );
472         }
473         break;
474
475         case CONFIG_ITEM_FLOAT:
476         {
477             INPUT_FIELD_FLOAT( p_item->psz_name, p_item->psz_text, 70,
478                                p_item->f_value );
479         }
480         break;
481
482         case CONFIG_ITEM_BOOL:
483         {
484             VLCButton *o_btn_bool;
485
486             s_rc.size.height = 20;
487             s_rc.size.width = s_vrc.size.width - X_ORIGIN * 2 - 20;
488             s_rc.origin.y += 10;
489
490             CHECK_VIEW_HEIGHT;
491
492             o_btn_bool = [[VLCButton alloc] initWithFrame: s_rc];
493             [o_btn_bool setButtonType: NSSwitchButton];
494             [o_btn_bool setIntValue: p_item->i_value];
495             [o_btn_bool setTitle: 
496                 [NSString stringWithCString: p_item->psz_text]]; 
497             [o_btn_bool setTarget: self];
498             [o_btn_bool setAction: @selector(configChanged:)];
499             CONTROL_CONFIG( o_btn_bool, o_module_name, 
500                             CONFIG_ITEM_BOOL, p_item->psz_name ); 
501             [o_view addSubview: [o_btn_bool autorelease]];
502
503             s_rc.origin.y += s_rc.size.height;
504         }
505         break;
506
507         }
508
509 #undef INPUT_FIELD_INTEGER
510 #undef INPUT_FIELD_FLOAT
511 #undef INPUT_FIELD_STRING
512 #undef INPUT_FIELD
513 #undef CHECK_VIEW_HEIGHT
514 #undef CONTROL_LABEL
515 #undef Y_ORIGIN
516 #undef X_ORIGIN
517     }
518     while( p_item->i_type != CONFIG_HINT_END && p_item++ );
519
520     vlc_list_release( p_list );
521
522     [o_toolbars setObject: o_tb_items forKey: o_module_name];
523     [o_toolbar setDelegate: self];
524     [o_panel setToolbar: [o_toolbar autorelease]];
525
526 #define DEF_PANEL_BUTTON( tag, title, sel ) \
527     { \
528         o_button = [[NSButton alloc] initWithFrame: s_rc]; \
529         [o_button setButtonType: NSMomentaryPushInButton]; \
530         [o_button setBezelStyle: NSRoundedBezelStyle]; \
531         [o_button setAction: @selector(sel)]; \
532         [o_button setTarget: self]; \
533         [o_button setTitle: title]; \
534         [o_button setTag: tag]; \
535         [o_panel_view addSubview: [o_button autorelease]]; \
536     }
537
538     s_rc.origin.y = s_panel_rc.origin.y + 14;
539     s_rc.size.height = 25; s_rc.size.width = 100;
540     s_rc.origin.x = s_panel_rc.size.width - s_rc.size.width - 14;
541     DEF_PANEL_BUTTON( 0, _NS("OK"), clickedCancelOK: ); 
542     [o_panel setDefaultButtonCell: [o_button cell]];
543
544     s_rc.origin.x -= s_rc.size.width;
545     DEF_PANEL_BUTTON( 1, _NS("Cancel"), clickedCancelOK: );
546
547     s_rc.origin.x -= s_rc.size.width;
548     DEF_PANEL_BUTTON( 2, _NS("Apply"), clickedApply: );
549     [o_button setEnabled: NO];
550
551 #undef DEF_PANEL_BUTTON
552
553     [o_pref_panels setObject: o_panel forKey: o_module_name];
554     [o_panel_views setObject: o_views forKey: o_module_name];
555
556     [o_panel center];
557     [o_panel makeKeyAndOrderFront: nil];
558 }
559
560 - (void)destroyPrefPanel:(id)o_unknown
561 {
562     id v1;
563     NSPanel *o_panel;
564     NSEnumerator *o_e1;
565     NSMutableArray *o_prefs;
566     NSMutableDictionary *o_dic;
567     NSScrollView *o_scroll_view;
568     NSString *o_module_name;
569
570     o_module_name = (NSString *)([o_unknown isKindOfClass: [NSTimer class]] ?
571                                  [o_unknown userInfo] : o_unknown);
572
573 #define DIC_REL(dic) \
574     { \
575     o_dic = [dic objectForKey: o_module_name]; \
576     [dic removeObjectForKey: o_module_name]; \
577     o_e1 = [o_dic objectEnumerator]; \
578     while( (v1 = [o_e1 nextObject]) ) \
579     { \
580         [v1 release]; \
581     } \
582     [o_dic removeAllObjects]; \
583     [o_dic release]; \
584     }
585
586     o_panel = [o_pref_panels objectForKey: o_module_name];
587     [o_pref_panels removeObjectForKey: o_module_name];
588     [o_panel release];
589
590     DIC_REL(o_toolbars);
591
592     o_scroll_view = [o_scroll_views objectForKey: o_module_name];
593     [o_scroll_views removeObjectForKey: o_module_name];
594     [o_scroll_view release];
595
596     DIC_REL(o_panel_views);
597
598     o_prefs = [o_save_prefs objectForKey: o_module_name];
599     [o_save_prefs removeObjectForKey: o_module_name];
600     [o_prefs removeAllObjects];
601     [o_prefs release];
602
603 #undef DIC_REL
604
605 }
606
607 - (void)selectPrefView:(id)sender
608 {
609     NSView *o_view;
610     NSString *o_module_name;
611     NSScrollView *o_scroll_view;
612     NSMutableDictionary *o_views;
613
614     o_module_name = [[sender toolbar] identifier];
615     o_views = [o_panel_views objectForKey: o_module_name];
616     o_view = [o_views objectForKey: [sender label]];
617
618     o_scroll_view = [o_scroll_views objectForKey: o_module_name];
619     [o_scroll_view setDocumentView: o_view]; 
620 }
621
622 - (void)moduleSelected:(id)sender
623 {
624     NSButton *o_btn_config;
625     NSString *o_module_name;
626     BOOL b_has_prefs = NO;
627
628     o_module_name = [sender titleOfSelectedItem];
629     o_btn_config = [[sender superview] viewWithTag: [sender tag] + 1];
630
631     if( ![o_module_name isEqualToString: _NS("None")] )
632     {
633         b_has_prefs = [self hasPrefs: o_module_name];
634     }
635
636     [o_btn_config setEnabled: b_has_prefs];
637 }
638
639 - (void)configureModule:(id)sender
640 {
641     NSString *o_module_name;
642     NSPopUpButton *o_modules;
643
644     o_modules = [[sender superview] viewWithTag: [sender tag] - 1]; 
645     o_module_name = [o_modules titleOfSelectedItem];
646
647     [self createPrefPanel: o_module_name];
648 }
649
650 - (void)selectModule:(id)sender
651 {
652     NSString *o_module_name;
653     NSPopUpButton *o_modules;
654     NSTextField *o_module;
655
656     o_module = [[sender superview] viewWithTag: [sender tag] - 1];
657     o_modules = [[sender superview] viewWithTag: [sender tag] - 3];
658     o_module_name = [o_modules titleOfSelectedItem];
659
660     if( [o_module_name isEqualToString: _NS("None")] )
661     {
662         o_module_name = [NSString string];
663     }
664
665     [o_module setStringValue: o_module_name];
666     [self configChanged: o_module];
667 }
668
669 - (void)configChanged:(id)o_unknown
670 {
671     id o_vlc_config = [o_unknown isKindOfClass: [NSNotification class]] ?
672                       [o_unknown object] : o_unknown;
673
674     NSString *o_module_name = [o_vlc_config moduleName]; 
675     NSPanel *o_pref_panel = [o_pref_panels objectForKey: o_module_name]; 
676     NSMutableArray *o_prefs = [o_save_prefs objectForKey: o_module_name];
677
678     if( [o_prefs indexOfObjectIdenticalTo: o_vlc_config] == NSNotFound )
679     {
680         NSView *o_pref_view = [o_pref_panel contentView];
681         NSButton *o_btn_apply = [o_pref_view viewWithTag: 2]; 
682
683         [o_prefs addObject: o_vlc_config];
684         [o_btn_apply setEnabled: YES];
685     }
686 }
687
688 - (void)clickedApply:(id)sender
689 {
690     id o_vlc_control;
691     NSEnumerator *o_enum;
692
693     NSView *o_config_view = [sender superview];
694     NSWindow *o_config_panel = [o_config_view window];    
695     NSButton *o_btn_apply = [o_config_view viewWithTag: 2]; 
696     NSString *o_module_name = [[o_config_panel toolbar] identifier];
697     NSMutableArray *o_prefs = [o_save_prefs objectForKey: o_module_name];
698
699     o_enum = [o_prefs objectEnumerator];
700     while( ( o_vlc_control = [o_enum nextObject] ) )
701     {
702         int i_type = [o_vlc_control configType];
703         NSString *o_name = [o_vlc_control configName];
704         char *psz_name = (char *)[o_name lossyCString];
705
706         switch( i_type )
707         {
708
709         case CONFIG_ITEM_MODULE:
710         case CONFIG_ITEM_STRING:
711         case CONFIG_ITEM_FILE:
712             {
713                 char *psz_value;
714                 NSString *o_value;
715
716                 o_value = [o_vlc_control stringValue];
717                 psz_value = (char *)[o_value lossyCString];
718
719                 config_PutPsz( p_intf, psz_name, 
720                                *psz_value ? psz_value : NULL );
721             }
722             break;
723
724         case CONFIG_ITEM_INTEGER:
725         case CONFIG_ITEM_BOOL:
726             {
727                 int i_value = [o_vlc_control intValue];
728
729                 config_PutInt( p_intf, psz_name, i_value );
730             }
731             break;
732
733         case CONFIG_ITEM_FLOAT:
734             {
735                 float f_value = [o_vlc_control floatValue];
736
737                 config_PutFloat( p_intf, psz_name, f_value );
738             }
739             break;
740
741         }
742     }
743
744     [o_btn_apply setEnabled: NO];
745     [o_prefs removeAllObjects];
746
747     config_SaveConfigFile( p_intf, NULL );
748 }
749
750 - (void)clickedCancelOK:(id)sender
751 {
752     NSWindow *o_pref_panel = [[sender superview] window];
753     NSString *o_module_name = [[o_pref_panel toolbar] identifier];
754
755     if( [[sender title] isEqualToString: _NS("OK")] )
756     {
757         [self clickedApply: sender];
758     }
759
760     [o_pref_panel close];
761
762     if( [self respondsToSelector: @selector(performSelectorOnMainThread:
763                                             withObject:waitUntilDone:)] ) 
764     {
765         [self performSelectorOnMainThread: @selector(destroyPrefPanel:)
766                                            withObject: o_module_name
767                                            waitUntilDone: NO];
768     }
769     else
770     {
771         [NSTimer scheduledTimerWithTimeInterval: 0.1
772                  target: self selector: @selector(destroyPrefPanel:)
773                  userInfo: o_module_name repeats: NO];
774     }
775 }
776
777 @end
778
779 @implementation VLCPrefs (NSToolbarDelegate)
780
781 - (NSToolbarItem *)toolbar:(NSToolbar *)o_toolbar 
782                    itemForItemIdentifier:(NSString *)o_item_id 
783                    willBeInsertedIntoToolbar:(BOOL)b_flag
784 {
785     NSMutableDictionary *o_toolbar_items;
786     NSString *o_module_name = [o_toolbar identifier];
787
788     o_toolbar_items = [o_toolbars objectForKey: o_module_name];
789     if( o_toolbar_items == nil )
790     {
791         return( nil );
792     }
793
794     return( [o_toolbar_items objectForKey: o_item_id] );
795 }
796
797 - (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)o_toolbar
798 {
799     return( [self toolbarDefaultItemIdentifiers: o_toolbar] );
800 }
801
802 - (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)o_toolbar
803 {
804     NSArray *o_ids;
805     NSMutableDictionary *o_toolbar_items;
806     NSString *o_module_name = [o_toolbar identifier];
807
808     o_toolbar_items = [o_toolbars objectForKey: o_module_name];
809     if( o_toolbar_items == nil )
810     {
811         return( nil );
812     }  
813
814     o_ids = [[o_toolbar_items allKeys] 
815         sortedArrayUsingSelector: @selector(compare:)];
816
817     return( o_ids );
818 }
819
820 @end
821
822 @implementation VLCFlippedView
823
824 - (BOOL)isFlipped
825 {
826     return( YES );
827 }
828
829 @end
830
831 IMPL_CONTROL_CONFIG(Button);
832 IMPL_CONTROL_CONFIG(ComboBox);
833 IMPL_CONTROL_CONFIG(TextField);