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