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