]> git.sesse.net Git - vlc/blob - modules/gui/macosx/prefs.m
* Bumped up revision number to 0.5.0-test3,
[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.7 2003/01/16 23:00:47 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, 550, 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, tip ) \
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_text_field setToolTip: [NSApp localizedString: tip]]; \
260         [o_view addSubview: [o_text_field autorelease]]; \
261         [[NSNotificationCenter defaultCenter] addObserver: self \
262             selector: @selector(configChanged:) \
263             name: NSControlTextDidChangeNotification \
264             object: o_text_field]; \
265         CONTROL_LABEL( label ); \
266         s_rc.origin.y += s_rc.size.height; \
267         s_rc.origin.x = X_ORIGIN; \
268     }
269
270 #define INPUT_FIELD_INTEGER( name, label, w, param, tip ) \
271     INPUT_FIELD( CONFIG_ITEM_INTEGER, name, label, w, setIntValue, param, tip )
272 #define INPUT_FIELD_FLOAT( name, label, w, param, tip ) \
273     INPUT_FIELD( CONFIG_ITEM_FLOAT, name, label, w, setFloatValue, param, tip )
274 #define INPUT_FIELD_STRING( name, label, w, param, tip ) \
275     INPUT_FIELD( CONFIG_ITEM_STRING, name, label, w, setStringValue, param, tip )
276
277     if( p_item ) do
278     {
279
280         switch( p_item->i_type )
281         {
282
283         case CONFIG_HINT_CATEGORY:
284         {
285             NSString *o_key;
286             NSString *o_label;
287             NSToolbarItem *o_tbi;
288
289             o_label = [NSApp localizedString: p_item->psz_text];
290             o_tbi = [[NSToolbarItem alloc] initWithItemIdentifier: o_label]; 
291             [o_tbi setImage: [NSImage imageNamed: @"NSApplicationIcon"]];
292             [o_tbi setLabel: o_label];
293             [o_tbi setTarget: self];
294             [o_tbi setAction: @selector(selectPrefView:)];
295
296             o_key = [NSString stringWithFormat: @"%02d %@",
297                                                 i_pos, o_label]; 
298             [o_tb_items setObject: o_tbi forKey: o_key];
299
300             s_vrc = s_scroll_rc; s_vrc.size.height -= 4;
301             o_view = [[VLCFlippedView alloc] initWithFrame: s_vrc];
302             [o_views setObject: o_view forKey: o_label];
303
304             s_rc.origin.x = X_ORIGIN;
305             s_rc.origin.y = Y_ORIGIN;
306
307             i_module_tag = 3;
308
309             if( i_pos == 0 )
310             {
311                 [o_scroll_view setDocumentView: o_view]; 
312             }
313
314             i_pos++;
315         }
316         break;
317
318         case CONFIG_ITEM_MODULE:
319         {
320             NSBox *o_box;
321             NSRect s_crc;
322             NSView *o_cview;
323             NSPopUpButton *o_modules;
324             NSButton *o_btn_select;
325             NSButton *o_btn_configure;
326
327 #define MODULE_BUTTON( button, title, sel ) \
328     { \
329         s_brc.size.height = 32; \
330         s_brc.origin.x += s_brc.size.width + 10; \
331         s_brc.size.width = s_crc.size.width - s_brc.origin.x - 10; \
332         button = [[NSButton alloc] initWithFrame: s_brc]; \
333         [button setButtonType: NSMomentaryPushInButton]; \
334         [button setBezelStyle: NSRoundedBezelStyle]; \
335         [button setTitle: title]; \
336         [button setTag: i_module_tag++]; \
337         [button setTarget: self]; \
338         [button setAction: @selector(sel)]; \
339         [o_cview addSubview: [button autorelease]]; \
340     }
341
342             s_rc.size.height = 107;
343             s_rc.size.width = s_vrc.size.width - X_ORIGIN * 2 - 20;
344             s_rc.origin.y += i_module_tag == 3 ? Y_ORIGIN : 20;
345
346             CHECK_VIEW_HEIGHT;
347
348             o_box = [[NSBox alloc] initWithFrame: s_rc];
349             [o_box setTitle: [NSApp localizedString: p_item->psz_text]];
350             [o_view addSubview: [o_box autorelease]];
351             s_rc.origin.y += s_rc.size.height + 10;
352             o_cview = [[VLCFlippedView alloc] initWithFrame: s_rc];
353             [o_box setContentView: [o_cview autorelease]];
354             s_crc = [o_cview bounds];
355
356             s_brc = NSMakeRect( 5, 10, 200, 30 ); 
357             o_modules = [[NSPopUpButton alloc] initWithFrame: s_brc];
358             [o_modules setTag: i_module_tag++];
359             [o_modules setTarget: self];
360             [o_modules setAction: @selector(moduleSelected:)];
361             [o_modules setToolTip: [NSApp localizedString: p_item->psz_longtext]];
362             [o_cview addSubview: [o_modules autorelease]]; 
363
364             MODULE_BUTTON( o_btn_configure, _NS("Configure"), 
365                            configureModule: );
366
367             s_brc = NSMakeRect( 8, s_brc.origin.y + s_brc.size.height + 10, 
368                                 194, 25 ); 
369             o_text_field = [[VLCTextField alloc] initWithFrame: s_brc];
370             [o_text_field setTag: i_module_tag++];
371             [o_text_field setAlignment: NSLeftTextAlignment];
372             CONTROL_CONFIG( o_text_field, o_module_name, 
373                             CONFIG_ITEM_MODULE, p_item->psz_name );
374             [[NSNotificationCenter defaultCenter] addObserver: self
375                 selector: @selector(configChanged:)
376                 name: NSControlTextDidChangeNotification
377                 object: o_text_field];
378             [o_cview addSubview: [o_text_field autorelease]];
379
380             s_brc.origin.x += 3;
381             MODULE_BUTTON( o_btn_select, _NS("Select"), 
382                            selectModule: );
383
384             [o_modules addItemWithTitle: _NS("None")];
385
386             /* build a list of available modules */
387             {
388                 for( i_index = 0; i_index < list.i_count; i_index++ )
389                 {
390                     p_parser = (module_t *)list.p_values[i_index].p_object ;
391
392                     if( !strcmp( p_parser->psz_capability,
393                                  p_item->psz_type ) )
394                     {
395                         NSString *o_object_name = [NSString 
396                             stringWithCString: p_parser->psz_object_name];
397                         [o_modules addItemWithTitle: o_object_name];
398                     }
399                 }
400             }
401
402             if( p_item->psz_value != NULL )
403             {
404                 NSString *o_value =
405                     [NSString stringWithCString: p_item->psz_value];
406
407                 [o_text_field setStringValue: o_value]; 
408                 [o_modules selectItemWithTitle: o_value]; 
409                 [o_btn_configure setEnabled: [self hasPrefs: o_value]]; 
410             }
411             else
412             {
413                 [o_modules selectItemWithTitle: _NS("None")];
414                 [o_btn_configure setEnabled: NO];
415             }
416
417 #undef MODULE_BUTTON
418         }
419         break;
420
421         case CONFIG_ITEM_STRING:
422         case CONFIG_ITEM_FILE:
423         {
424
425             if( !p_item->ppsz_list )
426             {
427                 char *psz_value = p_item->psz_value ?
428                                   p_item->psz_value : "";
429
430                 INPUT_FIELD_STRING( p_item->psz_name, p_item->psz_text, 150,
431                                     [NSString stringWithCString: psz_value],
432                                     p_item->psz_longtext );
433             }
434             else
435             {
436                 int i;
437                 VLCComboBox *o_combo_box;
438
439                 s_rc.size.height = 27;
440                 s_rc.size.width = 150;
441                 s_rc.origin.y += 10;
442
443                 CHECK_VIEW_HEIGHT;
444
445                 o_combo_box = [[VLCComboBox alloc] initWithFrame: s_rc];
446                 CONTROL_CONFIG( o_combo_box, o_module_name, 
447                                 CONFIG_ITEM_STRING, p_item->psz_name );
448                 [o_combo_box setToolTip:
449                     [NSApp localizedString: p_item->psz_longtext]];
450                 [o_view addSubview: [o_combo_box autorelease]];
451                 [[NSNotificationCenter defaultCenter] addObserver: self
452                     selector: @selector(configChanged:)
453                     name: NSControlTextDidChangeNotification
454                     object: o_combo_box];
455                 [[NSNotificationCenter defaultCenter] addObserver: self
456                     selector: @selector(configChanged:)
457                     name: NSComboBoxSelectionDidChangeNotification 
458                     object: o_combo_box];
459
460                 for( i=0; p_item->ppsz_list[i]; i++ )
461                 {
462                     [o_combo_box addItemWithObjectValue:
463                         [NSString stringWithCString: p_item->ppsz_list[i]]]; 
464                 }
465
466                 CONTROL_LABEL( p_item->psz_text ); 
467
468                 s_rc.origin.y += s_rc.size.height;
469                 s_rc.origin.x = X_ORIGIN;
470             }
471
472         }
473         break;
474
475         case CONFIG_ITEM_INTEGER:
476         {
477             INPUT_FIELD_INTEGER( p_item->psz_name, p_item->psz_text, 70, 
478                                  p_item->i_value, p_item->psz_longtext );
479         }
480         break;
481
482         case CONFIG_ITEM_FLOAT:
483         {
484             INPUT_FIELD_FLOAT( p_item->psz_name, p_item->psz_text, 70,
485                                p_item->f_value, p_item->psz_longtext );
486         }
487         break;
488
489         case CONFIG_ITEM_BOOL:
490         {
491             VLCButton *o_btn_bool;
492
493             s_rc.size.height = 27;
494             s_rc.size.width = s_vrc.size.width - X_ORIGIN * 2 - 20;
495             s_rc.origin.y += 10;
496
497             CHECK_VIEW_HEIGHT;
498
499             o_btn_bool = [[VLCButton alloc] initWithFrame: s_rc];
500             [o_btn_bool setButtonType: NSSwitchButton];
501             [o_btn_bool setIntValue: p_item->i_value];
502             [o_btn_bool setTitle: 
503                 [NSApp localizedString: p_item->psz_text]];
504             [o_btn_bool setToolTip:
505                 [NSApp localizedString: p_item->psz_longtext]];
506             [o_btn_bool setTarget: self];
507             [o_btn_bool setAction: @selector(configChanged:)];
508             CONTROL_CONFIG( o_btn_bool, o_module_name, 
509                             CONFIG_ITEM_BOOL, p_item->psz_name ); 
510             [o_view addSubview: [o_btn_bool autorelease]];
511
512             s_rc.origin.y += s_rc.size.height;
513         }
514         break;
515
516         }
517
518 #undef INPUT_FIELD_INTEGER
519 #undef INPUT_FIELD_FLOAT
520 #undef INPUT_FIELD_STRING
521 #undef INPUT_FIELD
522 #undef CHECK_VIEW_HEIGHT
523 #undef CONTROL_LABEL
524 #undef Y_ORIGIN
525 #undef X_ORIGIN
526     }
527     while( p_item->i_type != CONFIG_HINT_END && p_item++ );
528
529     vlc_list_release( &list );
530
531     [o_toolbars setObject: o_tb_items forKey: o_module_name];
532     [o_toolbar setDelegate: self];
533     [o_panel setToolbar: [o_toolbar autorelease]];
534
535 #define DEF_PANEL_BUTTON( tag, title, sel ) \
536     { \
537         o_button = [[NSButton alloc] initWithFrame: s_rc]; \
538         [o_button setButtonType: NSMomentaryPushInButton]; \
539         [o_button setBezelStyle: NSRoundedBezelStyle]; \
540         [o_button setAction: @selector(sel)]; \
541         [o_button setTarget: self]; \
542         [o_button setTitle: title]; \
543         [o_button setTag: tag]; \
544         [o_panel_view addSubview: [o_button autorelease]]; \
545     }
546
547     s_rc.origin.y = s_panel_rc.origin.y + 14;
548     s_rc.size.height = 25; s_rc.size.width = 100;
549     s_rc.origin.x = s_panel_rc.size.width - s_rc.size.width - 14;
550     DEF_PANEL_BUTTON( 0, _NS("OK"), clickedCancelOK: ); 
551     [o_panel setDefaultButtonCell: [o_button cell]];
552
553     s_rc.origin.x -= s_rc.size.width;
554     DEF_PANEL_BUTTON( 1, _NS("Cancel"), clickedCancelOK: );
555     [o_button setKeyEquivalent: @"\E"];
556
557     s_rc.origin.x -= s_rc.size.width;
558     DEF_PANEL_BUTTON( 2, _NS("Apply"), clickedApply: );
559     [o_button setEnabled: NO];
560
561 #undef DEF_PANEL_BUTTON
562
563     [o_pref_panels setObject: o_panel forKey: o_module_name];
564     [o_panel_views setObject: o_views forKey: o_module_name];
565
566     [o_panel center];
567     [o_panel makeKeyAndOrderFront: nil];
568 }
569
570 - (void)destroyPrefPanel:(id)o_unknown
571 {
572     id v1;
573     NSPanel *o_panel;
574     NSEnumerator *o_e1;
575     NSMutableArray *o_prefs;
576     NSMutableDictionary *o_dic;
577     NSScrollView *o_scroll_view;
578     NSString *o_module_name;
579
580     o_module_name = (NSString *)([o_unknown isKindOfClass: [NSTimer class]] ?
581                                  [o_unknown userInfo] : o_unknown);
582
583 #define DIC_REL(dic) \
584     { \
585     o_dic = [dic objectForKey: o_module_name]; \
586     [dic removeObjectForKey: o_module_name]; \
587     o_e1 = [o_dic objectEnumerator]; \
588     while( (v1 = [o_e1 nextObject]) ) \
589     { \
590         [v1 release]; \
591     } \
592     [o_dic removeAllObjects]; \
593     [o_dic release]; \
594     }
595
596     o_panel = [o_pref_panels objectForKey: o_module_name];
597     [o_pref_panels removeObjectForKey: o_module_name];
598     [o_panel release];
599
600     DIC_REL(o_toolbars);
601
602     o_scroll_view = [o_scroll_views objectForKey: o_module_name];
603     [o_scroll_views removeObjectForKey: o_module_name];
604     [o_scroll_view release];
605
606     DIC_REL(o_panel_views);
607
608     o_prefs = [o_save_prefs objectForKey: o_module_name];
609     [o_save_prefs removeObjectForKey: o_module_name];
610     [o_prefs removeAllObjects];
611     [o_prefs release];
612
613 #undef DIC_REL
614
615 }
616
617 - (void)selectPrefView:(id)sender
618 {
619     NSView *o_view;
620     NSString *o_module_name;
621     NSScrollView *o_scroll_view;
622     NSMutableDictionary *o_views;
623
624     o_module_name = [[sender toolbar] identifier];
625     o_views = [o_panel_views objectForKey: o_module_name];
626     o_view = [o_views objectForKey: [sender label]];
627
628     o_scroll_view = [o_scroll_views objectForKey: o_module_name];
629     [o_scroll_view setDocumentView: o_view]; 
630 }
631
632 - (void)moduleSelected:(id)sender
633 {
634     NSButton *o_btn_config;
635     NSString *o_module_name;
636     BOOL b_has_prefs = NO;
637
638     o_module_name = [sender titleOfSelectedItem];
639     o_btn_config = [[sender superview] viewWithTag: [sender tag] + 1];
640
641     if( ![o_module_name isEqualToString: _NS("None")] )
642     {
643         b_has_prefs = [self hasPrefs: o_module_name];
644     }
645
646     [o_btn_config setEnabled: b_has_prefs];
647 }
648
649 - (void)configureModule:(id)sender
650 {
651     NSString *o_module_name;
652     NSPopUpButton *o_modules;
653
654     o_modules = [[sender superview] viewWithTag: [sender tag] - 1]; 
655     o_module_name = [o_modules titleOfSelectedItem];
656
657     [self createPrefPanel: o_module_name];
658 }
659
660 - (void)selectModule:(id)sender
661 {
662     NSString *o_module_name;
663     NSPopUpButton *o_modules;
664     NSTextField *o_module;
665
666     o_module = [[sender superview] viewWithTag: [sender tag] - 1];
667     o_modules = [[sender superview] viewWithTag: [sender tag] - 3];
668     o_module_name = [o_modules titleOfSelectedItem];
669
670     if( [o_module_name isEqualToString: _NS("None")] )
671     {
672         o_module_name = [NSString string];
673     }
674
675     [o_module setStringValue: o_module_name];
676     [self configChanged: o_module];
677 }
678
679 - (void)configChanged:(id)o_unknown
680 {
681     id o_vlc_config = [o_unknown isKindOfClass: [NSNotification class]] ?
682                       [o_unknown object] : o_unknown;
683
684     NSString *o_module_name = [o_vlc_config moduleName]; 
685     NSPanel *o_pref_panel = [o_pref_panels objectForKey: o_module_name]; 
686     NSMutableArray *o_prefs = [o_save_prefs objectForKey: o_module_name];
687
688     if( [o_prefs indexOfObjectIdenticalTo: o_vlc_config] == NSNotFound )
689     {
690         NSView *o_pref_view = [o_pref_panel contentView];
691         NSButton *o_btn_apply = [o_pref_view viewWithTag: 2]; 
692
693         [o_prefs addObject: o_vlc_config];
694         [o_btn_apply setEnabled: YES];
695     }
696 }
697
698 - (void)clickedApply:(id)sender
699 {
700     id o_vlc_control;
701     NSEnumerator *o_enum;
702
703     NSView *o_config_view = [sender superview];
704     NSWindow *o_config_panel = [o_config_view window];    
705     NSButton *o_btn_apply = [o_config_view viewWithTag: 2]; 
706     NSString *o_module_name = [[o_config_panel toolbar] identifier];
707     NSMutableArray *o_prefs = [o_save_prefs objectForKey: o_module_name];
708
709     o_enum = [o_prefs objectEnumerator];
710     while( ( o_vlc_control = [o_enum nextObject] ) )
711     {
712         int i_type = [o_vlc_control configType];
713         NSString *o_name = [o_vlc_control configName];
714         char *psz_name = (char *)[o_name lossyCString];
715
716         switch( i_type )
717         {
718
719         case CONFIG_ITEM_MODULE:
720         case CONFIG_ITEM_STRING:
721         case CONFIG_ITEM_FILE:
722             {
723                 char *psz_value;
724                 NSString *o_value;
725
726                 o_value = [o_vlc_control stringValue];
727                 psz_value = (char *)[o_value lossyCString];
728
729                 config_PutPsz( p_intf, psz_name, 
730                                *psz_value ? psz_value : NULL );
731             }
732             break;
733
734         case CONFIG_ITEM_INTEGER:
735         case CONFIG_ITEM_BOOL:
736             {
737                 int i_value = [o_vlc_control intValue];
738
739                 config_PutInt( p_intf, psz_name, i_value );
740             }
741             break;
742
743         case CONFIG_ITEM_FLOAT:
744             {
745                 float f_value = [o_vlc_control floatValue];
746
747                 config_PutFloat( p_intf, psz_name, f_value );
748             }
749             break;
750
751         }
752     }
753
754     [o_btn_apply setEnabled: NO];
755     [o_prefs removeAllObjects];
756
757     config_SaveConfigFile( p_intf, NULL );
758 }
759
760 - (void)clickedCancelOK:(id)sender
761 {
762     NSWindow *o_pref_panel = [[sender superview] window];
763     NSString *o_module_name = [[o_pref_panel toolbar] identifier];
764
765     if( [[sender title] isEqualToString: _NS("OK")] )
766     {
767         [self clickedApply: sender];
768     }
769
770     [o_pref_panel close];
771
772     if( [self respondsToSelector: @selector(performSelectorOnMainThread:
773                                             withObject:waitUntilDone:)] ) 
774     {
775         [self performSelectorOnMainThread: @selector(destroyPrefPanel:)
776                                            withObject: o_module_name
777                                            waitUntilDone: NO];
778     }
779     else
780     {
781         [NSTimer scheduledTimerWithTimeInterval: 0.1
782                  target: self selector: @selector(destroyPrefPanel:)
783                  userInfo: o_module_name repeats: NO];
784     }
785 }
786
787 @end
788
789 @implementation VLCPrefs (NSToolbarDelegate)
790
791 - (NSToolbarItem *)toolbar:(NSToolbar *)o_toolbar 
792                    itemForItemIdentifier:(NSString *)o_item_id 
793                    willBeInsertedIntoToolbar:(BOOL)b_flag
794 {
795     NSMutableDictionary *o_toolbar_items;
796     NSString *o_module_name = [o_toolbar identifier];
797
798     o_toolbar_items = [o_toolbars objectForKey: o_module_name];
799     if( o_toolbar_items == nil )
800     {
801         return( nil );
802     }
803
804     return( [o_toolbar_items objectForKey: o_item_id] );
805 }
806
807 - (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)o_toolbar
808 {
809     return( [self toolbarDefaultItemIdentifiers: o_toolbar] );
810 }
811
812 - (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)o_toolbar
813 {
814     NSArray *o_ids;
815     NSMutableDictionary *o_toolbar_items;
816     NSString *o_module_name = [o_toolbar identifier];
817
818     o_toolbar_items = [o_toolbars objectForKey: o_module_name];
819     if( o_toolbar_items == nil )
820     {
821         return( nil );
822     }  
823
824     o_ids = [[o_toolbar_items allKeys] 
825         sortedArrayUsingSelector: @selector(compare:)];
826
827     return( o_ids );
828 }
829
830 @end
831
832 @implementation VLCFlippedView
833
834 - (BOOL)isFlipped
835 {
836     return( YES );
837 }
838
839 @end
840
841 IMPL_CONTROL_CONFIG(Button);
842 IMPL_CONTROL_CONFIG(ComboBox);
843 IMPL_CONTROL_CONFIG(TextField);