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