]> git.sesse.net Git - vlc/blob - modules/gui/macosx/simple_prefs.m
macosx: forward-port recent preferences and media keys enhancements
[vlc] / modules / gui / macosx / simple_prefs.m
1 /*****************************************************************************
2 * simple_prefs.m: Simple Preferences for Mac OS X
3 *****************************************************************************
4 * Copyright (C) 2008-2009 the VideoLAN team
5 * $Id$
6 *
7 * Authors: Felix Paul Kühne <fkuehne at videolan dot org>
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
23
24 #import "simple_prefs.h"
25 #import "prefs.h"
26 #import <vlc_keys.h>
27 #import <vlc_interface.h>
28 #import <vlc_dialog.h>
29 #import "misc.h"
30 #import "intf.h"
31 #import "AppleRemote.h"
32
33 static NSString* VLCSPrefsToolbarIdentifier = @"Our Simple Preferences Toolbar Identifier";
34 static NSString* VLCIntfSettingToolbarIdentifier = @"Intf Settings Item Identifier";
35 static NSString* VLCAudioSettingToolbarIdentifier = @"Audio Settings Item Identifier";
36 static NSString* VLCVideoSettingToolbarIdentifier = @"Video Settings Item Identifier";
37 static NSString* VLCOSDSettingToolbarIdentifier = @"Subtitles Settings Item Identifier";
38 static NSString* VLCInputSettingToolbarIdentifier = @"Input Settings Item Identifier";
39 static NSString* VLCHotkeysSettingToolbarIdentifier = @"Hotkeys Settings Item Identifier";
40
41 @implementation VLCSimplePrefs
42
43 static VLCSimplePrefs *_o_sharedInstance = nil;
44
45 + (VLCSimplePrefs *)sharedInstance
46 {
47     return _o_sharedInstance ? _o_sharedInstance : [[self alloc] init];
48 }
49
50 - (id)init
51 {
52     if (_o_sharedInstance) {
53         [self dealloc];
54     } else {
55         _o_sharedInstance = [super init];
56         p_intf = VLCIntf;
57     }
58
59     return _o_sharedInstance;
60 }
61
62 - (void)dealloc
63 {
64     [o_currentlyShownCategoryView release];
65
66     [o_hotkeySettings release];
67     [o_hotkeyDescriptions release];
68     [o_hotkeysNonUseableKeys release];
69
70     [o_keyInTransition release];
71
72     [super dealloc];
73 }
74
75
76 - (NSString *)OSXKeyToString:(int)val
77 {
78     NSMutableString *o_temp_str = [[[NSMutableString alloc] init] autorelease];
79     if( val & KEY_MODIFIER_CTRL )
80         [o_temp_str appendString: [NSString stringWithUTF8String: "\xE2\x8C\x83"]];
81     if( val & KEY_MODIFIER_ALT )
82         [o_temp_str appendString: [NSString stringWithUTF8String: "\xE2\x8C\xA5"]];
83     if( val & KEY_MODIFIER_SHIFT )
84         [o_temp_str appendString: [NSString stringWithUTF8String: "\xE2\x87\xA7"]];
85     if( val & KEY_MODIFIER_COMMAND )
86         [o_temp_str appendString: [NSString stringWithUTF8String: "\xE2\x8C\x98"]];
87
88     const char *base = KeyToString( val & ~KEY_MODIFIER );
89     if( base )
90         [o_temp_str appendString: [NSString stringWithUTF8String: base]];
91     else
92         o_temp_str = [NSMutableString stringWithString:_NS("Not Set")];
93     return o_temp_str;
94 }
95
96 - (void)awakeFromNib
97 {
98     [self initStrings];
99     
100     /* setup the toolbar */
101     NSToolbar * o_sprefs_toolbar = [[[NSToolbar alloc] initWithIdentifier: VLCSPrefsToolbarIdentifier] autorelease];
102     [o_sprefs_toolbar setAllowsUserCustomization: NO];
103     [o_sprefs_toolbar setAutosavesConfiguration: NO];
104     [o_sprefs_toolbar setDisplayMode: NSToolbarDisplayModeIconAndLabel];
105     [o_sprefs_toolbar setSizeMode: NSToolbarSizeModeRegular];
106     [o_sprefs_toolbar setDelegate: self];
107     [o_sprefs_win setToolbar: o_sprefs_toolbar];
108     
109     /* setup useful stuff */
110     o_hotkeysNonUseableKeys = [[NSArray arrayWithObjects:
111                                 [NSNumber numberWithInt: KEY_MODIFIER_COMMAND|'c'],
112                                 [NSNumber numberWithInt: KEY_MODIFIER_COMMAND|'x'],
113                                 [NSNumber numberWithInt: KEY_MODIFIER_COMMAND|'v'],
114                                 [NSNumber numberWithInt: KEY_MODIFIER_COMMAND|'a'],
115                                 [NSNumber numberWithInt: KEY_MODIFIER_COMMAND|','],
116                                 [NSNumber numberWithInt: KEY_MODIFIER_COMMAND|'h'],
117                                 [NSNumber numberWithInt: KEY_MODIFIER_COMMAND|KEY_MODIFIER_ALT|'h'],
118                                 [NSNumber numberWithInt: KEY_MODIFIER_COMMAND|KEY_MODIFIER_SHIFT|'o'],
119                                 [NSNumber numberWithInt: KEY_MODIFIER_COMMAND|'o'],
120                                 [NSNumber numberWithInt: KEY_MODIFIER_COMMAND|'d'],
121                                 [NSNumber numberWithInt: KEY_MODIFIER_COMMAND|'n'],
122                                 [NSNumber numberWithInt: KEY_MODIFIER_COMMAND|'s'],
123                                 [NSNumber numberWithInt: KEY_MODIFIER_COMMAND|'z'],
124                                 [NSNumber numberWithInt: KEY_MODIFIER_COMMAND|'l'],
125                                 [NSNumber numberWithInt: KEY_MODIFIER_COMMAND|'r'],
126                                 [NSNumber numberWithInt: KEY_MODIFIER_COMMAND|'0'],
127                                 [NSNumber numberWithInt: KEY_MODIFIER_COMMAND|'1'],
128                                 [NSNumber numberWithInt: KEY_MODIFIER_COMMAND|'2'],
129                                 [NSNumber numberWithInt: KEY_MODIFIER_COMMAND|'3'],
130                                 [NSNumber numberWithInt: KEY_MODIFIER_COMMAND|'m'],
131                                 [NSNumber numberWithInt: KEY_MODIFIER_COMMAND|'w'],
132                                 [NSNumber numberWithInt: KEY_MODIFIER_COMMAND|KEY_MODIFIER_SHIFT|'w'],
133                                 [NSNumber numberWithInt: KEY_MODIFIER_COMMAND|KEY_MODIFIER_SHIFT|'c'],
134                                 [NSNumber numberWithInt: KEY_MODIFIER_COMMAND|KEY_MODIFIER_SHIFT|'p'],
135                                 [NSNumber numberWithInt: KEY_MODIFIER_COMMAND|'i'],
136                                 [NSNumber numberWithInt: KEY_MODIFIER_COMMAND|'e'],
137                                 [NSNumber numberWithInt: KEY_MODIFIER_COMMAND|KEY_MODIFIER_SHIFT|'e'],
138                                 [NSNumber numberWithInt: KEY_MODIFIER_COMMAND|'b'],
139                                 [NSNumber numberWithInt: KEY_MODIFIER_COMMAND|KEY_MODIFIER_SHIFT|'m'],
140                                 [NSNumber numberWithInt: KEY_MODIFIER_COMMAND|KEY_MODIFIER_CTRL|'m'],
141                                 [NSNumber numberWithInt: KEY_MODIFIER_COMMAND|'?'],
142                                 [NSNumber numberWithInt: KEY_MODIFIER_COMMAND|KEY_MODIFIER_ALT|'?'],
143                                 nil] retain];
144 }
145
146 #define CreateToolbarItem( o_name, o_desc, o_img, sel ) \
147     o_toolbarItem = create_toolbar_item(o_itemIdent, o_name, o_desc, o_img, self, @selector(sel));
148 static inline NSToolbarItem *
149 create_toolbar_item( NSString * o_itemIdent, NSString * o_name, NSString * o_desc, NSString * o_img, id target, SEL selector )
150 {
151     NSToolbarItem *o_toolbarItem = [[[NSToolbarItem alloc] initWithItemIdentifier: o_itemIdent] autorelease]; \
152
153     [o_toolbarItem setLabel: o_name];
154     [o_toolbarItem setPaletteLabel: o_desc];
155
156     [o_toolbarItem setToolTip: o_desc];
157     [o_toolbarItem setImage: [NSImage imageNamed: o_img]];
158
159     [o_toolbarItem setTarget: target];
160     [o_toolbarItem setAction: selector];
161
162     [o_toolbarItem setEnabled: YES];
163     [o_toolbarItem setAutovalidates: YES];
164
165     return o_toolbarItem;
166 }
167
168 - (NSToolbarItem *) toolbar: (NSToolbar *)o_sprefs_toolbar 
169       itemForItemIdentifier: (NSString *)o_itemIdent 
170   willBeInsertedIntoToolbar: (BOOL)b_willBeInserted
171 {
172     NSToolbarItem *o_toolbarItem = nil;
173
174     if( [o_itemIdent isEqual: VLCIntfSettingToolbarIdentifier] )
175     {
176         CreateToolbarItem( _NS("Interface"), _NS("Interface Settings"), @"spref_cone_Interface_64", showInterfaceSettings );
177     }
178     else if( [o_itemIdent isEqual: VLCAudioSettingToolbarIdentifier] )
179     {
180         CreateToolbarItem( _NS("Audio"), _NS("General Audio Settings"), @"spref_cone_Audio_64", showAudioSettings );
181     }
182     else if( [o_itemIdent isEqual: VLCVideoSettingToolbarIdentifier] )
183     {
184         CreateToolbarItem( _NS("Video"), _NS("General Video Settings"), @"spref_cone_Video_64", showVideoSettings );
185     }
186     else if( [o_itemIdent isEqual: VLCOSDSettingToolbarIdentifier] )
187     {
188         CreateToolbarItem( _NS("Subtitles & OSD"), _NS("Subtitles & On Screen Display Settings"), @"spref_cone_Subtitles_64", showOSDSettings );
189     }
190     else if( [o_itemIdent isEqual: VLCInputSettingToolbarIdentifier] )
191     {
192         CreateToolbarItem( _NS("Input & Codecs"), _NS("Input & Codec settings"), @"spref_cone_Input_64", showInputSettings );
193     }
194     else if( [o_itemIdent isEqual: VLCHotkeysSettingToolbarIdentifier] )
195     {
196         CreateToolbarItem( _NS("Hotkeys"), _NS("Hotkeys settings"), @"spref_cone_Hotkeys_64", showHotkeySettings );
197     }
198
199     return o_toolbarItem;
200 }
201
202 - (NSArray *)toolbarDefaultItemIdentifiers: (NSToolbar *)toolbar
203 {
204     return [NSArray arrayWithObjects: VLCIntfSettingToolbarIdentifier, VLCAudioSettingToolbarIdentifier, VLCVideoSettingToolbarIdentifier, 
205         VLCOSDSettingToolbarIdentifier, VLCInputSettingToolbarIdentifier, VLCHotkeysSettingToolbarIdentifier, NSToolbarFlexibleSpaceItemIdentifier, nil];
206 }
207
208 - (NSArray *)toolbarAllowedItemIdentifiers: (NSToolbar *)toolbar
209 {
210     return [NSArray arrayWithObjects: VLCIntfSettingToolbarIdentifier, VLCAudioSettingToolbarIdentifier, VLCVideoSettingToolbarIdentifier, 
211         VLCOSDSettingToolbarIdentifier, VLCInputSettingToolbarIdentifier, VLCHotkeysSettingToolbarIdentifier, NSToolbarFlexibleSpaceItemIdentifier, nil];
212 }
213
214 - (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar
215 {
216     return [NSArray arrayWithObjects: VLCIntfSettingToolbarIdentifier, VLCAudioSettingToolbarIdentifier, VLCVideoSettingToolbarIdentifier, 
217         VLCOSDSettingToolbarIdentifier, VLCInputSettingToolbarIdentifier, VLCHotkeysSettingToolbarIdentifier, nil];
218 }
219
220 - (void)initStrings
221 {
222     /* audio */
223     [o_audio_dolby_txt setStringValue: _NS("Force detection of Dolby Surround")];
224     [o_audio_effects_box setTitle: _NS("Effects")];
225     [o_audio_enable_ckb setTitle: _NS("Enable Audio")];
226     [o_audio_general_box setTitle: _NS("General Audio")];
227     [o_audio_headphone_ckb setTitle: _NS("Headphone surround effect")];
228     [o_audio_lang_txt setStringValue: _NS("Preferred Audio language")];
229     [o_audio_last_ckb setTitle: _NS("Enable Last.fm submissions")];
230     [o_audio_lastpwd_txt setStringValue: _NS("Password")];
231     [o_audio_lastuser_txt setStringValue: _NS("User name")];
232     [o_audio_norm_ckb setTitle: _NS("Volume normalizer")];
233     [o_audio_spdif_ckb setTitle: _NS("Use S/PDIF when available")];
234     [o_audio_visual_txt setStringValue: _NS("Visualization")];
235     [o_audio_vol_txt setStringValue: _NS("Default Volume")];
236
237     /* hotkeys */
238     [o_hotkeys_change_btn setTitle: _NS("Change")];
239     [o_hotkeys_change_win setTitle: _NS("Change Hotkey")];
240     [o_hotkeys_change_cancel_btn setTitle: _NS("Cancel")];
241     [o_hotkeys_change_ok_btn setTitle: _NS("OK")];
242     [o_hotkeys_clear_btn setTitle: _NS("Clear")];
243     [o_hotkeys_lbl setStringValue: _NS("Select an action to change the associated hotkey:")];
244     [[[o_hotkeys_listbox tableColumnWithIdentifier: @"action"] headerCell] setStringValue: _NS("Action")];
245     [[[o_hotkeys_listbox tableColumnWithIdentifier: @"shortcut"] headerCell] setStringValue: _NS("Shortcut")];
246
247     /* input */
248     [o_input_avi_txt setStringValue: _NS("Repair AVI Files")];
249     [o_input_cachelevel_txt setStringValue: _NS("Default Caching Level")];
250     [o_input_caching_box setTitle: _NS("Caching")];
251     [o_input_cachelevel_custom_txt setStringValue: _NS("Use the complete preferences to configure custom caching values for each access module.")];
252     [o_input_httpproxy_txt setStringValue: _NS("HTTP Proxy")];
253     [o_input_httpproxypwd_txt setStringValue: _NS("Password for HTTP Proxy")];
254     [o_input_mux_box setTitle: _NS("Codecs / Muxers")];
255     [o_input_net_box setTitle: _NS("Network")];
256     [o_input_postproc_txt setStringValue: _NS("Post-Processing Quality")];
257     [o_input_rtsp_ckb setTitle: _NS("Use RTP over RTSP (TCP)")];
258     [o_input_skipLoop_txt setStringValue: _NS("Skip the loop filter for H.264 decoding")];
259     [o_input_serverport_txt setStringValue: _NS("Default Server Port")];
260
261     /* interface */
262     [o_intf_art_txt setStringValue: _NS("Album art download policy")];
263     [o_intf_embedded_ckb setTitle: _NS("Add controls to the video window")];
264     [o_intf_fspanel_ckb setTitle: _NS("Show Fullscreen Controller")];
265     [o_intf_lang_txt setStringValue: _NS("Language")];
266     [o_intf_network_box setTitle: _NS("Privacy / Network Interaction")];
267         [o_intf_appleremote_ckb setTitle: _NS("Control playback with the Apple Remote")];
268         [o_intf_mediakeys_ckb setTitle: _NS("Control playback with media keys")];
269     [o_intf_mediakeys_bg_ckb setTitle: _NS("...when VLC is in background")];
270
271     /* Subtitles and OSD */
272     [o_osd_encoding_txt setStringValue: _NS("Default Encoding")];
273     [o_osd_font_box setTitle: _NS("Display Settings")];
274     [o_osd_font_btn setTitle: _NS("Choose...")];
275     [o_osd_font_color_txt setStringValue: _NS("Font Color")];
276     [o_osd_font_size_txt setStringValue: _NS("Font Size")];
277     [o_osd_font_txt setStringValue: _NS("Font")];
278     [o_osd_lang_box setTitle: _NS("Subtitle Languages")];
279     [o_osd_lang_txt setStringValue: _NS("Preferred Subtitle Language")];
280     [o_osd_osd_box setTitle: _NS("On Screen Display")];
281     [o_osd_osd_ckb setTitle: _NS("Enable OSD")];
282
283     /* video */
284     [o_video_black_ckb setTitle: _NS("Black screens in Fullscreen mode")];
285     [o_video_device_txt setStringValue: _NS("Fullscreen Video Device")];
286     [o_video_display_box setTitle: _NS("Display")];
287     [o_video_enable_ckb setTitle: _NS("Enable Video")];
288     [o_video_fullscreen_ckb setTitle: _NS("Fullscreen")];
289     [o_video_onTop_ckb setTitle: _NS("Always on top")];
290     [o_video_output_txt setStringValue: _NS("Output module")];
291     [o_video_skipFrames_ckb setTitle: _NS("Skip frames")];
292     [o_video_snap_box setTitle: _NS("Video snapshots")];
293     [o_video_snap_folder_btn setTitle: _NS("Browse...")];
294     [o_video_snap_folder_txt setStringValue: _NS("Folder")];
295     [o_video_snap_format_txt setStringValue: _NS("Format")];
296     [o_video_snap_prefix_txt setStringValue: _NS("Prefix")];
297     [o_video_snap_seqnum_ckb setTitle: _NS("Sequential numbering")];
298     
299     /* generic stuff */
300     [[o_sprefs_basicFull_matrix cellAtRow: 0 column: 0] setStringValue: _NS("Basic")];
301     [[o_sprefs_basicFull_matrix cellAtRow: 0 column: 1] setStringValue: _NS("All")];
302     [o_sprefs_cancel_btn setTitle: _NS("Cancel")];
303     [o_sprefs_reset_btn setTitle: _NS("Reset All")];
304     [o_sprefs_save_btn setTitle: _NS("Save")];
305     [o_sprefs_win setTitle: _NS("Preferences")];
306 }
307
308 /* TODO: move this part to core */
309 #define config_GetLabel(a,b) __config_GetLabel(VLC_OBJECT(a),b)
310 static inline char * __config_GetLabel( vlc_object_t *p_this, const char *psz_name )
311 {
312     module_config_t *p_config;
313
314     p_config = config_FindConfig( p_this, psz_name );
315
316     /* sanity checks */
317     if( !p_config )
318     {
319         msg_Err( p_this, "option %s does not exist", psz_name );
320         return NULL;
321     }
322
323     if ( p_config->psz_longtext )
324         return p_config->psz_longtext;
325     else if( p_config->psz_text )
326         return p_config->psz_text;
327     else
328         msg_Warn( p_this, "option %s does not include any help" );
329
330     return NULL;
331 }
332
333 - (void)setupButton: (NSPopUpButton *)object forStringList: (const char *)name
334 {
335     module_config_t *p_item;
336
337     [object removeAllItems];
338     p_item = config_FindConfig( VLC_OBJECT(p_intf), name );
339
340     /* serious problem, if no item found */
341     assert( p_item );
342
343     for( int i = 0; i < p_item->i_list; i++ )
344     {
345         NSMenuItem *mi;
346         if( p_item->ppsz_list_text != NULL )
347             mi = [[NSMenuItem alloc] initWithTitle: _NS( p_item->ppsz_list_text[i] ) action:NULL keyEquivalent: @""];
348         else if( p_item->ppsz_list[i] && strcmp(p_item->ppsz_list[i],"") == 0 )
349         {
350             [[object menu] addItem: [NSMenuItem separatorItem]];
351             continue;
352         }
353         else if( p_item->ppsz_list[i] )
354             mi = [[NSMenuItem alloc] initWithTitle: [NSString stringWithUTF8String: p_item->ppsz_list[i]] action:NULL keyEquivalent: @""];
355         else NSLog( @"item %d of pref %s failed to be created", i, name);
356         [mi setRepresentedObject:[NSString stringWithUTF8String: p_item->ppsz_list[i]]];
357         [[object menu] addItem: [mi autorelease]];
358         if( p_item->value.psz && !strcmp( p_item->value.psz, p_item->ppsz_list[i] ) )
359             [object selectItem:[object lastItem]];
360     }
361     [object setToolTip: _NS( p_item->psz_longtext )];
362 }
363
364 - (void)setupButton: (NSPopUpButton *)object forIntList: (const char *)name
365 {
366     module_config_t *p_item;
367
368     [object removeAllItems];
369     p_item = config_FindConfig( VLC_OBJECT(p_intf), name );
370
371     /* serious problem, if no item found */
372     assert( p_item );
373
374     for( int i = 0; i < p_item->i_list; i++ )
375     {
376         NSMenuItem *mi;
377         if( p_item->ppsz_list_text != NULL)
378             mi = [[NSMenuItem alloc] initWithTitle: _NS( p_item->ppsz_list_text[i] ) action:NULL keyEquivalent: @""];
379         else if( p_item->pi_list[i] )
380             mi = [[NSMenuItem alloc] initWithTitle: [NSString stringWithFormat: @"%d", p_item->pi_list[i]] action:NULL keyEquivalent: @""];
381         else NSLog( @"item %d of pref %s failed to be created", i, name);
382         [mi setRepresentedObject:[NSNumber numberWithInt: p_item->pi_list[i]]];
383         [[object menu] addItem: [mi autorelease]];
384         if( p_item->value.i == p_item->pi_list[i] )
385             [object selectItem:[object lastItem]];
386     }
387     [object setToolTip: _NS( p_item->psz_longtext )];
388 }
389
390 - (void)setupButton: (NSPopUpButton *)object forModuleList: (const char *)name
391 {
392     module_config_t *p_item;
393     module_t *p_parser, **p_list;
394     int y = 0;
395     
396     [object removeAllItems];
397     
398     p_item = config_FindConfig( VLC_OBJECT(p_intf), name );
399     p_list = module_list_get( NULL );
400     if( !p_item ||!p_list )
401     {
402         if( p_list ) module_list_free(p_list);
403         NSLog( @"serious problem, item or list not found" );
404         return;
405     }
406
407     [object addItemWithTitle: _NS("Default")];
408     for( size_t i_index = 0; p_list[i_index]; i_index++ )
409     {
410         p_parser = p_list[i_index];
411         if( module_provides( p_parser, p_item->psz_type ) )
412         {
413             [object addItemWithTitle: [NSString stringWithUTF8String: module_GetLongName( p_parser ) ?: ""]];
414             if( p_item->value.psz && !strcmp( p_item->value.psz, module_get_object( p_parser ) ) )
415                 [object selectItem: [object lastItem]];
416         }
417     }
418     module_list_free( p_list );
419     [object setToolTip: _NS(p_item->psz_longtext)];
420 }
421
422 - (void)setupButton: (NSButton *)object forBoolValue: (const char *)name
423 {
424     [object setState: config_GetInt( p_intf, name )];
425     [object setToolTip: [NSString stringWithUTF8String: config_GetLabel( p_intf, name )]];
426 }
427
428 - (void)setupField:(NSTextField *)o_object forOption:(const char *)psz_option
429 {
430     char *psz_tmp = config_GetPsz( p_intf, psz_option );
431     [o_object setStringValue: [NSString stringWithUTF8String: psz_tmp ?: ""]];
432     [o_object setToolTip: [NSString stringWithUTF8String: config_GetLabel( p_intf, psz_option )]];
433     free( psz_tmp );
434 }
435
436 - (void)resetControls
437 {
438     module_config_t *p_item;
439     int i, y = 0;
440     char *psz_tmp;
441
442     [[o_sprefs_basicFull_matrix cellAtRow:0 column:0] setState: NSOnState];
443     [[o_sprefs_basicFull_matrix cellAtRow:0 column:1] setState: NSOffState];
444     
445     /**********************
446      * interface settings *
447      **********************/
448     [self setupButton: o_intf_lang_pop forStringList: "language"];
449     [self setupButton: o_intf_art_pop forIntList: "album-art"];
450
451     [self setupButton: o_intf_fspanel_ckb forBoolValue: "macosx-fspanel"];
452     [self setupButton: o_intf_embedded_ckb forBoolValue: "embedded-video"];
453         [self setupButton: o_intf_appleremote_ckb forBoolValue: "macosx-appleremote"];
454         [self setupButton: o_intf_mediakeys_ckb forBoolValue: "macosx-mediakeys"];
455     [self setupButton: o_intf_mediakeys_bg_ckb forBoolValue: "macosx-mediakeys-background"];
456     [o_intf_mediakeys_bg_ckb setEnabled: [o_intf_mediakeys_ckb state]];
457
458     /******************
459      * audio settings *
460      ******************/
461     [self setupButton: o_audio_enable_ckb forBoolValue: "audio"];
462     i = (config_GetInt( p_intf, "volume" ) * 0.390625);
463     [o_audio_vol_fld setToolTip: [NSString stringWithUTF8String: config_GetLabel( p_intf, "volume")]];
464     [o_audio_vol_fld setIntValue: i];
465     [o_audio_vol_sld setToolTip: [o_audio_vol_fld toolTip]];
466     [o_audio_vol_sld setIntValue: i];
467
468     [self setupButton: o_audio_spdif_ckb forBoolValue: "spdif"];
469
470     [self setupButton: o_audio_dolby_pop forIntList: "force-dolby-surround"];
471     [self setupField: o_audio_lang_fld forOption: "audio-language"];
472
473     [self setupButton: o_audio_headphone_ckb forBoolValue: "headphone-dolby"];
474
475     psz_tmp = config_GetPsz( p_intf, "audio-filter" );
476     if( psz_tmp )
477     {
478         [o_audio_norm_ckb setState: (NSInteger)strstr( psz_tmp, "volnorm" )];
479         [o_audio_norm_fld setEnabled: [o_audio_norm_ckb state]];
480         [o_audio_norm_stepper setEnabled: [o_audio_norm_ckb state]];
481         free( psz_tmp );
482     }
483     [o_audio_norm_fld setFloatValue: config_GetFloat( p_intf, "norm-max-level" )];
484     [o_audio_norm_fld setToolTip: [NSString stringWithUTF8String: config_GetLabel( p_intf, "norm-max-level")]];
485
486     [self setupButton: o_audio_visual_pop forModuleList: "audio-visual"];
487
488     /* Last.FM is optional */
489     if( module_exists( "audioscrobbler" ) )
490     {
491         [self setupField: o_audio_lastuser_fld forOption:"lastfm-username"];
492         [self setupField: o_audio_lastpwd_sfld forOption:"lastfm-password"];
493
494         if( config_ExistIntf( VLC_OBJECT( p_intf ), "audioscrobbler" ) )
495         {
496             [o_audio_last_ckb setState: NSOnState];
497             [o_audio_lastuser_fld setEnabled: YES];
498             [o_audio_lastpwd_sfld setEnabled: YES];
499         }
500         else
501         {
502             [o_audio_last_ckb setState: NSOffState];
503             [o_audio_lastuser_fld setEnabled: NO];
504             [o_audio_lastpwd_sfld setEnabled: NO];
505         }
506     }
507     else
508         [o_audio_last_ckb setEnabled: NO];
509
510     /******************
511      * video settings *
512      ******************/
513     [self setupButton: o_video_enable_ckb forBoolValue: "video"];
514     [self setupButton: o_video_fullscreen_ckb forBoolValue: "fullscreen"];
515     [self setupButton: o_video_onTop_ckb forBoolValue: "video-on-top"];
516     [self setupButton: o_video_skipFrames_ckb forBoolValue: "skip-frames"];
517     [self setupButton: o_video_black_ckb forBoolValue: "macosx-black"];
518
519     [self setupButton: o_video_output_pop forModuleList: "vout"];
520
521     [o_video_device_pop removeAllItems];
522     i = 0;
523     y = [[NSScreen screens] count];
524     [o_video_device_pop addItemWithTitle: _NS("Default")];
525     [[o_video_device_pop lastItem] setTag: 0];
526     while( i < y )
527     {
528         NSRect s_rect = [[[NSScreen screens] objectAtIndex: i] frame];
529         [o_video_device_pop addItemWithTitle: 
530          [NSString stringWithFormat: @"%@ %i (%ix%i)", _NS("Screen"), i+1,
531                    (int)s_rect.size.width, (int)s_rect.size.height]];
532         [[o_video_device_pop lastItem] setTag: (int)[[[NSScreen screens] objectAtIndex: i] displayID]];
533         i++;
534     }
535     [o_video_device_pop selectItemAtIndex: 0];
536     [o_video_device_pop selectItemWithTag: config_GetInt( p_intf, "macosx-vdev" )];
537
538     [self setupField: o_video_snap_folder_fld forOption:"snapshot-path"];
539     [self setupField: o_video_snap_prefix_fld forOption:"snapshot-prefix"];
540     [self setupButton: o_video_snap_seqnum_ckb forBoolValue: "snapshot-sequential"];
541     [self setupButton: o_video_snap_format_pop forStringList: "snapshot-format"];
542
543     /***************************
544      * input & codecs settings *
545      ***************************/
546     [o_input_serverport_fld setIntValue: config_GetInt( p_intf, "server-port")];
547     [o_input_serverport_fld setToolTip: [NSString stringWithUTF8String: config_GetLabel( p_intf, "server-port")]];
548     [self setupField: o_input_httpproxy_fld forOption:"http-proxy"];
549     [self setupField: o_input_httpproxypwd_sfld forOption:"http-proxy-pwd"];
550     [o_input_postproc_fld setIntValue: config_GetInt( p_intf, "postproc-q")];
551     [o_input_postproc_fld setToolTip: [NSString stringWithUTF8String: config_GetLabel( p_intf, "postproc-q")]];
552
553     [self setupButton: o_input_avi_pop forIntList: "avi-index"];
554
555     [self setupButton: o_input_rtsp_ckb forBoolValue: "rtsp-tcp"];
556     [self setupButton: o_input_skipLoop_pop forIntList: "ffmpeg-skiploopfilter"];
557
558     [o_input_cachelevel_pop removeAllItems];
559     [o_input_cachelevel_pop addItemsWithTitles: 
560         [NSArray arrayWithObjects: _NS("Custom"), _NS("Lowest latency"), _NS("Low latency"), _NS("Normal"),
561             _NS("High latency"), _NS("Higher latency"), nil]];
562     [[o_input_cachelevel_pop itemAtIndex: 0] setTag: 0];
563     [[o_input_cachelevel_pop itemAtIndex: 1] setTag: 100];
564     [[o_input_cachelevel_pop itemAtIndex: 2] setTag: 200];
565     [[o_input_cachelevel_pop itemAtIndex: 3] setTag: 300];
566     [[o_input_cachelevel_pop itemAtIndex: 4] setTag: 400];
567     [[o_input_cachelevel_pop itemAtIndex: 5] setTag: 500];
568     
569 #define TestCaC( name ) \
570     b_cache_equal =  b_cache_equal && \
571         ( i_cache == config_GetInt( p_intf, name ) )
572
573 #define TestCaCi( name, int ) \
574         b_cache_equal = b_cache_equal &&  \
575         ( ( i_cache * int ) == config_GetInt( p_intf, name ) )
576
577     /* Select the accurate value of the PopupButton */
578     bool b_cache_equal = true;
579     int i_cache = config_GetInt( p_intf, "file-caching");
580     
581     TestCaC( "udp-caching" );
582     if( module_exists ("dvdread") )
583         TestCaC( "dvdread-caching" );
584     if( module_exists ("dvdnav") )
585         TestCaC( "dvdnav-caching" );
586     TestCaC( "tcp-caching" );
587     TestCaC( "fake-caching" );
588     TestCaC( "cdda-caching" );
589     TestCaC( "screen-caching" );
590     TestCaC( "vcd-caching" );
591     TestCaCi( "rtsp-caching", 4 );
592     TestCaCi( "ftp-caching", 2 );
593     TestCaCi( "http-caching", 4 );
594     if(module_exists ("access_realrtsp"))
595         TestCaCi( "realrtsp-caching", 10 );
596     TestCaCi( "mms-caching", 19 );
597     if( b_cache_equal )
598     {
599         [o_input_cachelevel_pop selectItemWithTag: i_cache];
600         [o_input_cachelevel_custom_txt setHidden: YES];
601     }
602     else
603     {
604         [o_input_cachelevel_pop selectItemWithTitle: _NS("Custom")];
605         [o_input_cachelevel_custom_txt setHidden: NO];
606     }
607
608     /*********************
609      * subtitle settings *
610      *********************/
611     [self setupButton: o_osd_osd_ckb forBoolValue: "osd"];
612     
613     [self setupButton: o_osd_encoding_pop forStringList: "subsdec-encoding"];
614     [self setupField: o_osd_lang_fld forOption: "sub-language" ];
615         
616         if( module_exists( "quartztext" ) )
617         {
618                 [self setupField: o_osd_font_fld forOption: "quartztext-font"];
619                 [self setupButton: o_osd_font_color_pop forIntList: "quartztext-color"];
620                 [self setupButton: o_osd_font_size_pop forIntList: "quartztext-rel-fontsize"];
621         }
622         else 
623         {
624         /* fallback on freetype */
625                 [self setupField: o_osd_font_fld forOption: "freetype-font"];
626                 [self setupButton: o_osd_font_color_pop forIntList: "freetype-color"];
627                 [self setupButton: o_osd_font_size_pop forIntList: "freetype-rel-fontsize"];
628                 /* selector button is useless in this case */
629                 [o_osd_font_btn setEnabled: NO];
630         }
631
632
633     /********************
634      * hotkeys settings *
635      ********************/
636     const struct hotkey *p_hotkeys = p_intf->p_libvlc->p_hotkeys;
637     [o_hotkeySettings release];
638     o_hotkeySettings = [[NSMutableArray alloc] init];
639     NSMutableArray *o_tempArray_desc = [[NSMutableArray alloc] init];
640     i = 1;
641
642     while( i < 100 )
643     {
644         p_item = config_FindConfig( VLC_OBJECT(p_intf), p_hotkeys[i].psz_action );
645         if( !p_item )
646             break;
647
648         [o_tempArray_desc addObject: _NS( p_item->psz_text )];
649         [o_hotkeySettings addObject: [NSNumber numberWithInt: p_item->value.i]];
650
651         i++;
652     }
653     [o_hotkeyDescriptions release];
654     o_hotkeyDescriptions = [[NSArray alloc] initWithArray: o_tempArray_desc copyItems: YES];
655     [o_tempArray_desc release];
656     [o_hotkeys_listbox reloadData];
657 }
658
659 - (void)showSimplePrefs
660 {
661     /* we want to show the interface settings, if no category was chosen */
662     if( [[o_sprefs_win toolbar] selectedItemIdentifier] == nil )
663     {
664         [[o_sprefs_win toolbar] setSelectedItemIdentifier: VLCIntfSettingToolbarIdentifier];
665         [self showInterfaceSettings];
666     }
667     
668     [self resetControls];
669
670     [o_sprefs_win center];
671     [o_sprefs_win makeKeyAndOrderFront: self];
672 }
673
674 - (IBAction)buttonAction:(id)sender
675 {
676     if( sender == o_sprefs_cancel_btn )
677         [o_sprefs_win orderOut: sender];
678     else if( sender == o_sprefs_save_btn )
679     {
680         [self saveChangedSettings];
681         [o_sprefs_win orderOut: sender];
682     }
683     else if( sender == o_sprefs_reset_btn )
684         NSBeginInformationalAlertSheet( _NS("Reset Preferences"), _NS("Cancel"),
685                                         _NS("Continue"), nil, o_sprefs_win, self,
686                                         @selector(sheetDidEnd: returnCode: contextInfo:), NULL, nil,
687                                         _NS("Beware this will reset the VLC media player preferences.\n"
688                                             "Are you sure you want to continue?") );
689     else if( sender == o_sprefs_basicFull_matrix )
690     {
691         [o_sprefs_win orderOut: self];
692         [[o_sprefs_basicFull_matrix cellAtRow:0 column:0] setState: NSOffState];
693         [[o_sprefs_basicFull_matrix cellAtRow:0 column:1] setState: NSOnState];
694         [[[VLCMain sharedInstance] preferences] showPrefs];
695     }
696     else
697         msg_Warn( p_intf, "unknown buttonAction sender" );
698 }
699
700 - (void)sheetDidEnd:(NSWindow *)o_sheet 
701          returnCode:(int)i_return
702         contextInfo:(void *)o_context
703 {
704     if( i_return == NSAlertAlternateReturn )
705     {
706         config_ResetAll( p_intf );
707         [self resetControls];
708         config_SaveConfigFile( p_intf, NULL );
709     }
710 }
711
712 static inline void save_int_list( intf_thread_t * p_intf, id object, const char * name )
713 {
714     NSNumber *p_valueobject;
715     module_config_t *p_item;
716     p_item = config_FindConfig( VLC_OBJECT(p_intf), name );
717     p_valueobject = (NSNumber *)[[object selectedItem] representedObject];
718     assert([p_valueobject isKindOfClass:[NSNumber class]]);
719     if( p_valueobject) config_PutInt( p_intf, name, [p_valueobject intValue] );
720 }
721
722 static inline void save_string_list( intf_thread_t * p_intf, id object, const char * name )
723 {
724     NSString *p_stringobject;
725     module_config_t *p_item;
726     p_item = config_FindConfig( VLC_OBJECT(p_intf), name );
727     p_stringobject = (NSString *)[[object selectedItem] representedObject];
728     assert([p_stringobject isKindOfClass:[NSString class]]);
729     if( p_stringobject ) 
730     {
731         config_PutPsz( p_intf, name, [p_stringobject UTF8String] );
732         [p_stringobject release];
733     }
734 }
735
736 static inline void save_module_list( intf_thread_t * p_intf, id object, const char * name )
737 {
738     module_config_t *p_item;
739     module_t *p_parser, **p_list;
740
741     p_item = config_FindConfig( VLC_OBJECT(p_intf), name );
742
743     p_list = module_list_get( NULL );
744     for( size_t i_module_index = 0; p_list[i_module_index]; i_module_index++ )
745     {
746         p_parser = p_list[i_module_index];
747
748         if( p_item->i_type == CONFIG_ITEM_MODULE && module_provides( p_parser, p_item->psz_type ) )
749         {
750             if( [[[object selectedItem] title] isEqualToString: _NS( module_GetLongName( p_parser ) )] )
751             {
752                 config_PutPsz( p_intf, name, strdup( module_get_object( p_parser )));
753                 break;
754             }
755         }
756     }
757     module_list_free( p_list );
758     if( [[[object selectedItem] title] isEqualToString: _NS( "Default" )] )
759         config_PutPsz( p_intf, name, "" );
760 }
761
762 - (void)saveChangedSettings
763 {
764     char *psz_tmp;
765     int i;
766     
767 #define SaveIntList( object, name ) save_int_list( p_intf, object, name )
768                     
769 #define SaveStringList( object, name ) save_string_list( p_intf, object, name )
770
771 #define SaveModuleList( object, name ) save_module_list( p_intf, object, name )
772
773     /**********************
774      * interface settings *
775      **********************/
776     if( b_intfSettingChanged )
777     {
778         SaveStringList( o_intf_lang_pop, "language" );
779         SaveIntList( o_intf_art_pop, "album-art" );
780
781         config_PutInt( p_intf, "macosx-fspanel", [o_intf_fspanel_ckb state] );
782         config_PutInt( p_intf, "embedded-video", [o_intf_embedded_ckb state] );
783                 config_PutInt( p_intf, "macosx-appleremote", [o_intf_appleremote_ckb state] );
784                 config_PutInt( p_intf, "macosx-mediakeys", [o_intf_mediakeys_ckb state] );
785         config_PutInt( p_intf, "macosx-mediakeys-background", [o_intf_mediakeys_bg_ckb state] );
786
787                 /* activate stuff without restart */
788                 if( [o_intf_appleremote_ckb state] == YES )
789                         [[[VLCMain sharedInstance] appleRemoteController] startListening: [VLCMain sharedInstance]];
790                 else
791                         [[[VLCMain sharedInstance] appleRemoteController] stopListening: [VLCMain sharedInstance]];
792         [[NSNotificationCenter defaultCenter] postNotificationName: @"VLCMediaKeySupportSettingChanged" 
793                                                             object: nil 
794                                                           userInfo: nil];
795
796         /* okay, let's save our changes to vlcrc */
797         i = config_SaveConfigFile( p_intf, "main" );
798         i = i + config_SaveConfigFile( p_intf, "macosx" );
799
800         if( i != 0 )
801         {
802             msg_Err( p_intf, "An error occurred while saving the Interface settings using SimplePrefs (%i)", i );
803             dialog_Fatal( p_intf, _("Interface Settings not saved"),
804                         _("An error occured while saving your settings via SimplePrefs (%i)."), i );
805             i = 0;
806         }
807
808         b_intfSettingChanged = NO;
809     }
810     
811     /******************
812      * audio settings *
813      ******************/
814     if( b_audioSettingChanged )
815     {
816         config_PutInt( p_intf, "audio", [o_audio_enable_ckb state] );
817         config_PutInt( p_intf, "volume", ([o_audio_vol_sld intValue] * 2.56));
818         NSLog( @"slider=%i, pref=%i", [o_audio_vol_sld intValue], config_GetInt( p_intf, "volume" ));
819         config_PutInt( p_intf, "spdif", [o_audio_spdif_ckb state] );
820
821         SaveIntList( o_audio_dolby_pop, "force-dolby-surround" );
822
823         config_PutPsz( p_intf, "audio-language", [[o_audio_lang_fld stringValue] UTF8String] );
824         config_PutInt( p_intf, "headphone-dolby", [o_audio_headphone_ckb state] );
825
826         if( [o_audio_norm_ckb state] == NSOnState )
827         {
828             psz_tmp = config_GetPsz( p_intf, "audio-filter" );
829             if(! psz_tmp)
830                 config_PutPsz( p_intf, "audio-filter", "volnorm" );
831             else if( (NSInteger)strstr( psz_tmp, "normvol" ) == NO )
832             {
833                 /* work-around a GCC 4.0.1 bug */
834                 psz_tmp = (char *)[[NSString stringWithFormat: @"%s:volnorm", psz_tmp] UTF8String];
835                 config_PutPsz( p_intf, "audio-filter", psz_tmp );
836                 free( psz_tmp );
837             }
838         }
839         else
840         {
841             psz_tmp = config_GetPsz( p_intf, "audio-filter" );
842             if( psz_tmp )
843             {
844                 char *psz_tmp2 = (char *)[[[NSString stringWithUTF8String: psz_tmp] stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@":volnorm"]] UTF8String];
845                 psz_tmp2 = (char *)[[[NSString stringWithUTF8String: psz_tmp2] stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@"volnorm:"]] UTF8String];
846                 psz_tmp2 = (char *)[[[NSString stringWithUTF8String: psz_tmp2] stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@"volnorm"]] UTF8String];
847                 config_PutPsz( p_intf, "audio-filter", psz_tmp );
848                 free( psz_tmp );
849             }
850         }
851         config_PutFloat( p_intf, "norm-max-level", [o_audio_norm_fld floatValue] );
852
853         SaveModuleList( o_audio_visual_pop, "audio-visual" );
854
855         /* Last.FM is optional */
856         if( module_exists( "audioscrobbler" ) )
857         {   
858             [o_audio_last_ckb setEnabled: YES];
859             if( [o_audio_last_ckb state] == NSOnState )
860                 config_AddIntf( p_intf, "audioscrobbler" );
861             else
862                 config_RemoveIntf( p_intf, "audioscrobbler" );
863
864             config_PutPsz( p_intf, "lastfm-username", [[o_audio_lastuser_fld stringValue] UTF8String] );
865             config_PutPsz( p_intf, "lastfm-password", [[o_audio_lastpwd_sfld stringValue] UTF8String] );
866         }
867         else
868             [o_audio_last_ckb setEnabled: NO];
869
870         /* okay, let's save our changes to vlcrc */
871         i = config_SaveConfigFile( p_intf, "main" );
872         i = i + config_SaveConfigFile( p_intf, "audioscrobbler" );
873         i = i + config_SaveConfigFile( p_intf, "volnorm" );
874
875         if( i != 0 )
876         {
877             msg_Err( p_intf, "An error occurred while saving the Audio settings using SimplePrefs (%i)", i );
878             dialog_Fatal( p_intf, _("Audio Settings not saved"),
879                         _("An error occured while saving your settings via SimplePrefs (%i)."), i );
880             
881             i = 0;
882         }
883         b_audioSettingChanged = NO;
884     }
885     
886     /******************
887      * video settings *
888      ******************/
889     if( b_videoSettingChanged )
890     {
891         config_PutInt( p_intf, "video", [o_video_enable_ckb state] );
892         config_PutInt( p_intf, "fullscreen", [o_video_fullscreen_ckb state] );
893         config_PutInt( p_intf, "video-on-top", [o_video_onTop_ckb state] );
894         config_PutInt( p_intf, "skip-frames", [o_video_skipFrames_ckb state] );
895         config_PutInt( p_intf, "macosx-black", [o_video_black_ckb state] );
896
897         SaveModuleList( o_video_output_pop, "vout" );
898         config_PutInt( p_intf, "macosx-vdev", [[o_video_device_pop selectedItem] tag] );
899
900         config_PutPsz( p_intf, "snapshot-path", [[o_video_snap_folder_fld stringValue] UTF8String] );
901         config_PutPsz( p_intf, "snapshot-prefix", [[o_video_snap_prefix_fld stringValue] UTF8String] );
902         config_PutInt( p_intf, "snapshot-sequential", [o_video_snap_seqnum_ckb state] );
903         SaveStringList( o_video_snap_format_pop, "snapshot-format" );
904
905         i = config_SaveConfigFile( p_intf, "main" );
906         i = i + config_SaveConfigFile( p_intf, "macosx" );
907
908         if( i != 0 )
909         {
910             msg_Err( p_intf, "An error occurred while saving the Video settings using SimplePrefs (%i)", i );
911             dialog_Fatal( p_intf, _("Video Settings not saved"),
912                         _("An error occured while saving your settings via SimplePrefs (%i)."), i );
913             i = 0;
914         }
915         b_videoSettingChanged = NO;
916     }
917     
918     /***************************
919      * input & codecs settings *
920      ***************************/
921     if( b_inputSettingChanged )
922     {
923         config_PutInt( p_intf, "server-port", [o_input_serverport_fld intValue] );
924         config_PutPsz( p_intf, "http-proxy", [[o_input_httpproxy_fld stringValue] UTF8String] );
925         config_PutPsz( p_intf, "http-proxy-pwd", [[o_input_httpproxypwd_sfld stringValue] UTF8String] );
926         config_PutInt( p_intf, "postproc-q", [o_input_postproc_fld intValue] );
927
928         SaveIntList( o_input_avi_pop, "avi-index" );
929
930         config_PutInt( p_intf, "rtsp-tcp", [o_input_rtsp_ckb state] );
931         SaveIntList( o_input_skipLoop_pop, "ffmpeg-skiploopfilter" );
932
933         #define CaCi( name, int ) config_PutInt( p_intf, name, int * [[o_input_cachelevel_pop selectedItem] tag] )
934         #define CaC( name ) CaCi( name, 1 )
935         msg_Dbg( p_intf, "Adjusting all cache values to: %i", (int)[[o_input_cachelevel_pop selectedItem] tag] );
936         CaC( "udp-caching" );
937         if( module_exists ( "dvdread" ) )
938         {
939             CaC( "dvdread-caching" );
940             i = i + config_SaveConfigFile( p_intf, "dvdread" );
941         }
942         if( module_exists ( "dvdnav" ) )
943         {
944             CaC( "dvdnav-caching" );
945             i = i + config_SaveConfigFile( p_intf, "dvdnav" );
946         }
947         CaC( "tcp-caching" ); CaC( "vcd-caching" );
948         CaC( "fake-caching" ); CaC( "cdda-caching" ); CaC( "file-caching" );
949         CaC( "screen-caching" );
950         CaCi( "rtsp-caching", 4 ); CaCi( "ftp-caching", 2 );
951         CaCi( "http-caching", 4 );
952         if( module_exists ( "access_realrtsp" ) )
953         {
954             CaCi( "realrtsp-caching", 10 );
955             i = i + config_SaveConfigFile( p_intf, "access_realrtsp" );
956         }
957         CaCi( "mms-caching", 19 );
958
959         i = config_SaveConfigFile( p_intf, "main" );
960         i = i + config_SaveConfigFile( p_intf, "avcodec" );
961         i = i + config_SaveConfigFile( p_intf, "postproc" );
962         i = i + config_SaveConfigFile( p_intf, "access_http" );
963         i = i + config_SaveConfigFile( p_intf, "access_file" );
964         i = i + config_SaveConfigFile( p_intf, "access_tcp" );
965         i = i + config_SaveConfigFile( p_intf, "access_fake" );
966         i = i + config_SaveConfigFile( p_intf, "cdda" );
967         i = i + config_SaveConfigFile( p_intf, "screen" );
968         i = i + config_SaveConfigFile( p_intf, "vcd" );
969         i = i + config_SaveConfigFile( p_intf, "access_ftp" );
970         i = i + config_SaveConfigFile( p_intf, "access_mms" );
971         i = i + config_SaveConfigFile( p_intf, "live555" );
972         i = i + config_SaveConfigFile( p_intf, "avi" );
973
974         if( i != 0 )
975         {
976             msg_Err( p_intf, "An error occurred while saving the Input settings using SimplePrefs (%i)", i );
977             dialog_Fatal( p_intf, _("Input Settings not saved"),
978                         _("An error occured while saving your settings via SimplePrefs (%i)."), i );
979             i = 0;
980         }
981         b_inputSettingChanged = NO;
982     }
983     
984     /**********************
985      * subtitles settings *
986      **********************/
987     if( b_osdSettingChanged )
988     {
989         config_PutInt( p_intf, "osd", [o_osd_osd_ckb state] );
990
991         if( [o_osd_encoding_pop indexOfSelectedItem] >= 0 )
992             SaveStringList( o_osd_encoding_pop, "subsdec-encoding" );
993         else
994             config_PutPsz( p_intf, "subsdec-encoding", "" );
995
996         config_PutPsz( p_intf, "sub-language", [[o_osd_lang_fld stringValue] UTF8String] );
997         
998                 if( module_exists( "quartztext" ) )
999                 {
1000                         config_PutPsz( p_intf, "quartztext-font", [[o_osd_font_fld stringValue] UTF8String] );
1001                         SaveIntList( o_osd_font_color_pop, "quartztext-color" );
1002                         SaveIntList( o_osd_font_size_pop, "quartztext-rel-fontsize" );
1003                 }
1004                 else
1005                 {
1006             /* fallback on freetype */
1007                         config_PutPsz( p_intf, "freetype-font", [[o_osd_font_fld stringValue] UTF8String] );
1008                         SaveIntList( o_osd_font_color_pop, "freetype-color" );
1009                         SaveIntList( o_osd_font_size_pop, "freetype-rel-fontsize" );                
1010                 }
1011
1012         i = config_SaveConfigFile( p_intf, NULL );
1013
1014         if( i != 0 )
1015         {
1016             msg_Err( p_intf, "An error occurred while saving the OSD/Subtitle settings using SimplePrefs (%i)", i );
1017             dialog_Fatal( p_intf, _("On Screen Display/Subtitle Settings not saved"),
1018                         _("An error occured while saving your settings via SimplePrefs (%i)."), i );
1019             i = 0;
1020         }
1021         b_osdSettingChanged = NO;
1022     }
1023     
1024     /********************
1025      * hotkeys settings *
1026      ********************/
1027     if( b_hotkeyChanged )
1028     {
1029         const struct hotkey *p_hotkeys = p_intf->p_libvlc->p_hotkeys;
1030         i = 1;
1031         while( i < [o_hotkeySettings count] )
1032         {
1033             config_PutInt( p_intf, p_hotkeys[i].psz_action, [[o_hotkeySettings objectAtIndex: i-1] intValue] );
1034             i++;
1035         }        
1036
1037         i = config_SaveConfigFile( p_intf, "main" );
1038
1039         if( i != 0 )
1040         {
1041             msg_Err( p_intf, "An error occurred while saving the Hotkey settings using SimplePrefs (%i)", i );
1042             dialog_Fatal( p_intf, _("Hotkeys not saved"),
1043                         _("An error occured while saving your settings via SimplePrefs (%i)."), i );
1044             i = 0;
1045         }
1046         b_hotkeyChanged = NO;
1047     }
1048 }
1049
1050 - (void)showSettingsForCategory: (id)o_new_category_view
1051 {
1052     NSRect o_win_rect, o_view_rect, o_old_view_rect;
1053     o_win_rect = [o_sprefs_win frame];
1054     o_view_rect = [o_new_category_view frame];
1055     
1056     if( o_currentlyShownCategoryView != nil )
1057     {
1058         /* restore our window's height, if we've shown another category previously */
1059         o_old_view_rect = [o_currentlyShownCategoryView frame];
1060         o_win_rect.size.height = o_win_rect.size.height - o_old_view_rect.size.height;
1061         o_win_rect.origin.y = ( o_win_rect.origin.y + o_old_view_rect.size.height ) - o_view_rect.size.height;
1062
1063         /* remove our previous category view */
1064         [o_currentlyShownCategoryView removeFromSuperviewWithoutNeedingDisplay];
1065     }
1066     
1067     o_win_rect.size.height = o_win_rect.size.height + o_view_rect.size.height;
1068     
1069     [o_sprefs_win displayIfNeeded];
1070     [o_sprefs_win setFrame: o_win_rect display:YES animate: YES];
1071     
1072     [o_new_category_view setFrame: NSMakeRect( 0, 
1073                                                [o_sprefs_controls_box frame].size.height, 
1074                                                o_view_rect.size.width, 
1075                                                o_view_rect.size.height )];
1076     [o_new_category_view setNeedsDisplay: YES];
1077     [o_new_category_view setAutoresizesSubviews: YES];
1078     [[o_sprefs_win contentView] addSubview: o_new_category_view];
1079     
1080     /* keep our current category for further reference */
1081     [o_currentlyShownCategoryView release];
1082     o_currentlyShownCategoryView = o_new_category_view;
1083     [o_currentlyShownCategoryView retain];
1084 }
1085
1086 - (IBAction)interfaceSettingChanged:(id)sender
1087 {
1088     if( sender == o_intf_mediakeys_ckb )
1089         [o_intf_mediakeys_bg_ckb setEnabled: [o_intf_mediakeys_ckb state]];
1090     b_intfSettingChanged = YES;
1091 }
1092
1093 - (void)showInterfaceSettings
1094 {
1095     [self showSettingsForCategory: o_intf_view];
1096 }
1097
1098 - (IBAction)audioSettingChanged:(id)sender
1099 {
1100     if( sender == o_audio_vol_sld )
1101         [o_audio_vol_fld setIntValue: [o_audio_vol_sld intValue]];
1102
1103     if( sender == o_audio_vol_fld )
1104         [o_audio_vol_sld setIntValue: [o_audio_vol_fld intValue]];
1105
1106     if( sender == o_audio_norm_ckb )
1107     {
1108         [o_audio_norm_stepper setEnabled: [o_audio_norm_ckb state]];
1109         [o_audio_norm_fld setEnabled: [o_audio_norm_ckb state]];
1110     }    
1111     
1112     if( sender == o_audio_last_ckb )
1113     {
1114         if( [o_audio_last_ckb state] == NSOnState )
1115         {
1116             [o_audio_lastpwd_sfld setEnabled: YES];
1117             [o_audio_lastuser_fld setEnabled: YES];
1118         }
1119         else
1120         {
1121             [o_audio_lastpwd_sfld setEnabled: NO];
1122             [o_audio_lastuser_fld setEnabled: NO];
1123         }
1124     }
1125
1126     b_audioSettingChanged = YES;
1127 }
1128
1129 - (void)showAudioSettings
1130 {
1131     [self showSettingsForCategory: o_audio_view];
1132 }
1133
1134 - (IBAction)videoSettingChanged:(id)sender
1135 {
1136     if( sender == o_video_snap_folder_btn )
1137     {
1138         o_selectFolderPanel = [[NSOpenPanel alloc] init];
1139         [o_selectFolderPanel setCanChooseDirectories: YES];
1140         [o_selectFolderPanel setCanChooseFiles: NO];
1141         [o_selectFolderPanel setResolvesAliases: YES];
1142         [o_selectFolderPanel setAllowsMultipleSelection: NO];
1143         [o_selectFolderPanel setMessage: _NS("Choose the folder to save your video snapshots to.")];
1144         [o_selectFolderPanel setCanCreateDirectories: YES];
1145         [o_selectFolderPanel setPrompt: _NS("Choose")];
1146         [o_selectFolderPanel beginSheetForDirectory: nil file: nil modalForWindow: o_sprefs_win 
1147                                       modalDelegate: self 
1148                                      didEndSelector: @selector(savePanelDidEnd:returnCode:contextInfo:)
1149                                         contextInfo: o_video_snap_folder_btn];
1150     }
1151     else
1152         b_videoSettingChanged = YES;
1153 }
1154
1155 - (void)savePanelDidEnd:(NSOpenPanel * )panel returnCode: (int)returnCode contextInfo: (void *)contextInfo
1156 {
1157     if( returnCode == NSOKButton )
1158     {
1159         if( contextInfo == o_video_snap_folder_btn )
1160         {
1161             [o_video_snap_folder_fld setStringValue: [o_selectFolderPanel filename]];
1162             b_videoSettingChanged = YES;
1163         }
1164     }
1165
1166     [o_selectFolderPanel release];
1167 }
1168
1169 - (void)showVideoSettings
1170 {
1171     [self showSettingsForCategory: o_video_view];
1172 }
1173
1174 - (IBAction)osdSettingChanged:(id)sender
1175 {
1176     b_osdSettingChanged = YES;
1177 }
1178
1179 - (void)showOSDSettings
1180 {
1181     [self showSettingsForCategory: o_osd_view];
1182 }
1183
1184 - (IBAction)showFontPicker:(id)sender
1185 {
1186         if( module_exists( "quartztext" ) )
1187         {
1188                 char * font = config_GetPsz( p_intf, "quartztext-font" );
1189                 NSString * fontFamilyName = font ? [NSString stringWithUTF8String: font] : nil;
1190                 free(font);
1191                 if( fontFamilyName )
1192                 {
1193                         NSFontDescriptor * fd = [NSFontDescriptor fontDescriptorWithFontAttributes:nil];
1194                         NSFont * font = [NSFont fontWithDescriptor:[fd fontDescriptorWithFamily:fontFamilyName] textTransform:nil];
1195                         [[NSFontManager sharedFontManager] setSelectedFont:font isMultiple:NO];
1196                 }
1197                 [[NSFontManager sharedFontManager] setTarget: self];
1198                 [[NSFontPanel sharedFontPanel] orderFront:self];
1199         }
1200 }
1201
1202 - (void)changeFont:(id)sender
1203 {
1204     NSFont * font = [sender convertFont:[[NSFontManager sharedFontManager] selectedFont]];
1205     [o_osd_font_fld setStringValue:[font familyName]];
1206     [self osdSettingChanged:self];
1207 }
1208
1209 - (IBAction)inputSettingChanged:(id)sender
1210 {
1211     if( sender == o_input_cachelevel_pop )
1212     {
1213         if( [[[o_input_cachelevel_pop selectedItem] title] isEqualToString: _NS("Custom")] )
1214             [o_input_cachelevel_custom_txt setHidden: NO];
1215         else
1216             [o_input_cachelevel_custom_txt setHidden: YES];
1217     }
1218
1219     b_inputSettingChanged = YES;
1220 }
1221
1222 - (void)showInputSettings
1223 {
1224     [self showSettingsForCategory: o_input_view];
1225 }
1226
1227 - (IBAction)hotkeySettingChanged:(id)sender
1228 {
1229     if( sender == o_hotkeys_change_btn || sender == o_hotkeys_listbox )
1230     {
1231         [o_hotkeys_change_lbl setStringValue: [NSString stringWithFormat: _NS("Press new keys for\n\"%@\""), 
1232                                                [o_hotkeyDescriptions objectAtIndex: [o_hotkeys_listbox selectedRow]]]];
1233         [o_hotkeys_change_keys_lbl setStringValue: [self OSXKeyToString:[[o_hotkeySettings objectAtIndex: [o_hotkeys_listbox selectedRow]] intValue]]];
1234         [o_hotkeys_change_taken_lbl setStringValue: @""];
1235         [o_hotkeys_change_win setInitialFirstResponder: [o_hotkeys_change_win contentView]];
1236         [o_hotkeys_change_win makeFirstResponder: [o_hotkeys_change_win contentView]];
1237         [NSApp runModalForWindow: o_hotkeys_change_win];
1238     }
1239     else if( sender == o_hotkeys_change_cancel_btn )
1240     {
1241         [NSApp stopModal];
1242         [o_hotkeys_change_win close];
1243     }
1244     else if( sender == o_hotkeys_change_ok_btn )
1245     {
1246         NSInteger i_returnValue;
1247         if(! o_keyInTransition )
1248         {
1249             [NSApp stopModal];
1250             [o_hotkeys_change_win close];
1251             msg_Err( p_intf, "internal error prevented the hotkey switch" );
1252             return;
1253         }
1254
1255         b_hotkeyChanged = YES;
1256
1257         i_returnValue = [o_hotkeySettings indexOfObject: o_keyInTransition];
1258         if( i_returnValue != NSNotFound )
1259             [o_hotkeySettings replaceObjectAtIndex: i_returnValue withObject: [[NSNumber numberWithInt: 0] retain]];
1260
1261         [o_hotkeySettings replaceObjectAtIndex: [o_hotkeys_listbox selectedRow] withObject: [o_keyInTransition retain]];
1262
1263         [NSApp stopModal];
1264         [o_hotkeys_change_win close];
1265
1266         [o_hotkeys_listbox reloadData];
1267     }
1268     else if( sender == o_hotkeys_clear_btn )
1269     {
1270         [o_hotkeySettings replaceObjectAtIndex: [o_hotkeys_listbox selectedRow] withObject: [NSNumber numberWithInt: 0]];
1271         [o_hotkeys_listbox reloadData];
1272         b_hotkeyChanged = YES;
1273     }
1274 }
1275
1276 - (void)showHotkeySettings
1277 {
1278     [self showSettingsForCategory: o_hotkeys_view];
1279 }
1280
1281 - (int)numberOfRowsInTableView:(NSTableView *)aTableView
1282 {
1283     return [o_hotkeySettings count];
1284 }
1285
1286 - (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
1287 {
1288     if( [[aTableColumn identifier] isEqualToString: @"action"] )
1289         return [o_hotkeyDescriptions objectAtIndex: rowIndex];
1290     else if( [[aTableColumn identifier] isEqualToString: @"shortcut"] )
1291         return [self OSXKeyToString: [[o_hotkeySettings objectAtIndex: rowIndex] intValue]];
1292     else
1293     {
1294         msg_Err( p_intf, "unknown TableColumn identifier (%s)!", [[aTableColumn identifier] UTF8String] );
1295         return NULL;
1296     }
1297 }
1298
1299 - (BOOL)changeHotkeyTo: (int)i_theNewKey
1300 {
1301     NSInteger i_returnValue;
1302     i_returnValue = [o_hotkeysNonUseableKeys indexOfObject: [NSNumber numberWithInt: i_theNewKey]];
1303     if( i_returnValue != NSNotFound || i_theNewKey == 0 )
1304     {
1305         [o_hotkeys_change_keys_lbl setStringValue: _NS("Invalid combination")];
1306         [o_hotkeys_change_taken_lbl setStringValue: _NS("Regrettably, these keys cannot be assigned as hotkey shortcuts.")];
1307         [o_hotkeys_change_ok_btn setEnabled: NO];
1308         return NO;
1309     }
1310     else
1311     {
1312         NSString *o_temp;
1313         if( o_keyInTransition )
1314             [o_keyInTransition release];
1315         o_keyInTransition = [[NSNumber numberWithInt: i_theNewKey] retain];
1316
1317         o_temp = [self OSXKeyToString: i_theNewKey];
1318
1319         [o_hotkeys_change_keys_lbl setStringValue: o_temp];
1320
1321         i_returnValue = [o_hotkeySettings indexOfObject: o_keyInTransition];
1322         if( i_returnValue != NSNotFound )
1323             [o_hotkeys_change_taken_lbl setStringValue: [NSString stringWithFormat:
1324                                                          _NS("This combination is already taken by \"%@\"."),
1325                                                          [o_hotkeyDescriptions objectAtIndex: i_returnValue]]];
1326         else
1327             [o_hotkeys_change_taken_lbl setStringValue: @""];
1328
1329         [o_hotkeys_change_ok_btn setEnabled: YES];
1330         return YES;
1331     }
1332 }
1333     
1334 @end
1335
1336 /********************
1337  * hotkeys settings *
1338  ********************/
1339
1340 @implementation VLCHotkeyChangeWindow
1341
1342 - (BOOL)acceptsFirstResponder
1343 {
1344     return YES;
1345 }
1346
1347 - (BOOL)becomeFirstResponder
1348 {
1349     return YES;
1350 }
1351
1352 - (BOOL)resignFirstResponder
1353 {
1354     /* We need to stay the first responder or we'll miss the user's input */
1355     return NO;
1356 }
1357
1358 - (BOOL)performKeyEquivalent:(NSEvent *)o_theEvent
1359 {
1360     unichar key;
1361     int i_key = 0;
1362
1363     if( [o_theEvent modifierFlags] & NSControlKeyMask )
1364         i_key |= KEY_MODIFIER_CTRL;
1365
1366     if( [o_theEvent modifierFlags] & NSAlternateKeyMask  )
1367         i_key |= KEY_MODIFIER_ALT;
1368
1369     if( [o_theEvent modifierFlags] & NSShiftKeyMask )
1370         i_key |= KEY_MODIFIER_SHIFT;
1371
1372     if( [o_theEvent modifierFlags] & NSCommandKeyMask )
1373         i_key |= KEY_MODIFIER_COMMAND;
1374
1375     key = [[[o_theEvent charactersIgnoringModifiers] lowercaseString] characterAtIndex: 0];
1376     if( key )
1377     {
1378         i_key |= CocoaKeyToVLC( key );
1379         return [[[VLCMain sharedInstance] simplePreferences] changeHotkeyTo: i_key];
1380     }
1381     return FALSE;
1382 }
1383
1384 @end
1385
1386 @implementation VLCSimplePrefsWindow
1387
1388 - (BOOL)acceptsFirstResponder
1389 {
1390     return YES;
1391 }
1392
1393 - (void)changeFont:(id)sender
1394 {
1395     [[[VLCMain sharedInstance] simplePreferences] changeFont: sender];
1396 }
1397 @end