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