]> git.sesse.net Git - vlc/blob - modules/gui/macosx/simple_prefs.m
macosx: protect against NULL string in simple_prefs.
[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 & OSD 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)resetControls
303 {
304     module_config_t *p_item;
305     vlc_list_t *p_list;
306     module_t *p_parser;
307     int i, y = 0;
308     char *psz_tmp;
309
310 #define SetupIntList( object, name ) \
311     [object removeAllItems]; \
312     p_item = config_FindConfig( VLC_OBJECT(p_intf), name ); \
313     for( i = 0; i < p_item->i_list; i++ ) \
314     { \
315         if( p_item->ppsz_list_text[i] != NULL) \
316             [object addItemWithTitle: _NS( p_item->ppsz_list_text[i] )]; \
317         else \
318             [object addItemWithTitle: [NSString stringWithUTF8String: p_item->ppsz_list[i] ?: ""]]; \
319     } \
320     if( p_item->value.i < [object numberOfItems] ) \
321         [object selectItemAtIndex: p_item->value.i]; \
322     else \
323         [object selectItemAtIndex: 0]; \
324     [object setToolTip: _NS( p_item->psz_longtext )]
325
326 #define SetupStringList( object, name ) \
327     [object removeAllItems]; \
328     y = 0; \
329     p_item = config_FindConfig( VLC_OBJECT(p_intf), name ); \
330     for( i = 0; p_item->ppsz_list[i] != nil; i++ ) \
331     { \
332         [object addItemWithTitle: _NS( p_item->ppsz_list_text[i] )]; \
333         if( p_item->value.psz && !strcmp( p_item->value.psz, p_item->ppsz_list[i] ) ) \
334             y = i; \
335     } \
336     [object selectItemAtIndex: y]; \
337     [object setToolTip: _NS( p_item->psz_longtext )]
338     
339 #define SetupModuleList( object, name ) \
340     p_item = config_FindConfig( VLC_OBJECT(p_intf), name ); \
341     p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE ); \
342     [object removeAllItems]; \
343     [object addItemWithTitle: _NS("Default")]; \
344     for( int i_index = 0; i_index < p_list->i_count; i_index++ ) \
345     { \
346         p_parser = (module_t *)p_list->p_values[i_index].p_object ; \
347         \
348         if( module_IsCapable( p_parser, p_item->psz_type ) ) \
349         { \
350             [object addItemWithTitle: [NSString stringWithUTF8String: module_GetLongName( p_parser ) ?: ""]]; \
351             \
352             if( p_item->value.psz && !strcmp( p_item->value.psz, module_GetObjName( p_parser ) ) ) \
353                 [object selectItem: [object lastItem]]; \
354         } \
355     } \
356     vlc_list_release( p_list ); \
357     [object setToolTip: _NS(p_item->psz_longtext)]
358
359     [[o_sprefs_basicFull_matrix cellAtRow:0 column:0] setState: NSOnState];
360     [[o_sprefs_basicFull_matrix cellAtRow:0 column:1] setState: NSOffState];
361     
362     /**********************
363      * interface settings *
364      **********************/
365     SetupStringList( o_intf_lang_pop, "language" );
366     SetupIntList( o_intf_art_pop, "album-art" );
367
368     [o_intf_fspanel_ckb setState: config_GetInt( p_intf, "macosx-fspanel" )];
369     [o_intf_embedded_ckb setState: config_GetInt( p_intf, "embedded-video" )];
370
371     /******************
372      * audio settings *
373      ******************/
374     [o_audio_enable_ckb setState: config_GetInt( p_intf, "audio" )];
375     [o_audio_vol_fld setIntValue: config_GetInt( p_intf, "volume" )];
376     [o_audio_vol_sld setIntValue: config_GetInt( p_intf, "volume" )];
377
378     [o_audio_spdif_ckb setState: config_GetInt( p_intf, "spdif" )];
379
380     SetupIntList( o_audio_dolby_pop, "force-dolby-surround" );
381
382     [o_audio_lang_fld setStringValue: [NSString stringWithUTF8String: config_GetPsz( p_intf, "audio-language" ) ?: ""]];
383
384     [o_audio_headphone_ckb setState: config_GetInt( p_intf, "headphone-dolby" )];
385     
386     psz_tmp = config_GetPsz( p_intf, "audio-filter" );
387     if( psz_tmp )
388         [o_audio_norm_ckb setState: (int)strstr( psz_tmp, "normvol" )];
389     [o_audio_norm_fld setFloatValue: config_GetFloat( p_intf, "norm-max-level" )];
390
391     SetupModuleList( o_audio_visual_pop, "audio-visual" );
392
393     /* Last.FM is optional */
394     if( module_Exists( p_intf, "audioscrobbler" ) )
395     {
396         [o_audio_lastuser_fld setStringValue: [NSString stringWithUTF8String: config_GetPsz( p_intf, "lastfm-username" ) ?: ""]];
397         [o_audio_lastpwd_sfld setStringValue: [NSString stringWithUTF8String: config_GetPsz( p_intf, "lastfm-password" ) ?: ""]];
398
399         if( config_ExistIntf( VLC_OBJECT( p_intf ), "audioscrobbler" ) )
400         {
401             [o_audio_last_ckb setState: NSOnState];
402             [o_audio_lastuser_fld setEnabled: YES];
403             [o_audio_lastpwd_sfld setEnabled: YES];
404         }
405         else
406         {
407             [o_audio_last_ckb setState: NSOffState];
408             [o_audio_lastuser_fld setEnabled: NO];
409             [o_audio_lastpwd_sfld setEnabled: NO];
410         }
411     }
412     else
413         [o_audio_last_ckb setEnabled: NO];
414
415     /******************
416      * video settings *
417      ******************/
418     [o_video_enable_ckb setState: config_GetInt( p_intf, "video" )];
419     [o_video_fullscreen_ckb setState: config_GetInt( p_intf, "fullscreen" )];
420     [o_video_onTop_ckb setState: config_GetInt( p_intf, "video-on-top" )];
421     [o_video_skipFrames_ckb setState: config_GetInt( p_intf, "skip-frames" )];
422     [o_video_black_ckb setState: config_GetInt( p_intf, "macosx-black" )];
423
424     SetupModuleList( o_video_output_pop, "vout" );
425
426     [o_video_device_pop removeAllItems];
427     i = 0;
428     y = [[NSScreen screens] count];
429     [o_video_device_pop addItemWithTitle: _NS("Default")];
430     [[o_video_device_pop lastItem] setTag: 0];
431     while( i < y )
432     {
433         NSRect s_rect = [[[NSScreen screens] objectAtIndex: i] frame];
434         [o_video_device_pop addItemWithTitle: 
435          [NSString stringWithFormat: @"%@ %i (%ix%i)", _NS("Screen"), i+1,
436                    (int)s_rect.size.width, (int)s_rect.size.height]];
437         [[o_video_device_pop lastItem] setTag: (int)[[[NSScreen screens] objectAtIndex: i] displayID]];
438         i++;
439     }
440     [o_video_device_pop selectItemAtIndex: 0];
441     [o_video_device_pop selectItemWithTag: config_GetInt( p_intf, "macosx-vdev" )];
442
443     [o_video_snap_folder_fld setStringValue: [NSString stringWithUTF8String: config_GetPsz( p_intf, "snapshot-path" ) ?: ""]];
444     [o_video_snap_prefix_fld setStringValue: [NSString stringWithUTF8String: config_GetPsz( p_intf, "snapshot-prefix" ) ?: ""]];
445     [o_video_snap_seqnum_ckb setState: config_GetInt( p_intf, "snapshot-sequential" )];
446     
447     p_item = config_FindConfig( VLC_OBJECT(p_intf), "snapshot-format" );
448     for( i = 0; p_item->ppsz_list[i] != nil; i++ )
449     {
450         [o_video_snap_format_pop addItemWithTitle: [NSString stringWithUTF8String: p_item->ppsz_list[i] ?: ""]];
451         if( p_item->value.psz && !strcmp( p_item->value.psz, p_item->ppsz_list[i] ) )
452             y = i;
453     }
454     [o_video_snap_format_pop selectItemAtIndex: y];
455
456     /***************************
457      * input & codecs settings *
458      ***************************/
459     [o_input_serverport_fld setIntValue: config_GetInt( p_intf, "server-port" )];
460     if( config_GetPsz( p_intf, "http-proxy" ) != NULL )
461         [o_input_httpproxy_fld setStringValue: [NSString stringWithUTF8String: config_GetPsz( p_intf, "http-proxy" ) ?: ""]];
462     if( config_GetPsz( p_intf, "http-proxy" ) != NULL )
463         [o_input_httpproxypwd_sfld setStringValue: [NSString stringWithUTF8String: config_GetPsz( p_intf, "http-proxy-pwd" ) ?: ""]];
464     [o_input_postproc_fld setIntValue: config_GetInt( p_intf, "ffmpeg-pp-q" )];
465
466     SetupIntList( o_input_avi_pop, "avi-index" );
467
468     [o_input_rtsp_ckb setState: config_GetInt( p_intf, "rtsp-tcp" )];
469
470     psz_tmp = config_GetPsz( p_intf, "access-filter" );
471     if( psz_tmp )
472     {
473         [o_input_record_ckb setState: (int)strstr( psz_tmp, "record" )];
474         [o_input_dump_ckb setState: (int)strstr( psz_tmp, "dump" )];
475         [o_input_bandwidth_ckb setState: (int)strstr( psz_tmp, "bandwidth" )];
476         [o_input_timeshift_ckb setState: (int)strstr( psz_tmp, "timeshift" )];
477     }
478
479     [o_input_cachelevel_pop removeAllItems];
480     [o_input_cachelevel_pop addItemsWithTitles: 
481         [NSArray arrayWithObjects: _NS("Custom"), _NS("Lowest latency"), _NS("Low latency"), _NS("Normal"),
482             _NS("High latency"), _NS("Higher latency"), nil]];
483     [[o_input_cachelevel_pop itemAtIndex: 0] setTag: 0];
484     [[o_input_cachelevel_pop itemAtIndex: 1] setTag: 100];
485     [[o_input_cachelevel_pop itemAtIndex: 2] setTag: 200];
486     [[o_input_cachelevel_pop itemAtIndex: 3] setTag: 300];
487     [[o_input_cachelevel_pop itemAtIndex: 4] setTag: 400];
488     [[o_input_cachelevel_pop itemAtIndex: 5] setTag: 500];
489     
490 #define TestCaC( name ) \
491     b_cache_equal =  b_cache_equal && \
492         ( i_cache == config_GetInt( p_intf, name ) )
493
494 #define TestCaCi( name, int ) \
495         b_cache_equal = b_cache_equal &&  \
496         ( ( i_cache * int ) == config_GetInt( p_intf, name ) )
497
498     /* Select the accurate value of the PopupButton */
499     bool b_cache_equal = true;
500     int i_cache = config_GetInt( p_intf, "file-caching");
501     
502     TestCaC( "udp-caching" );
503     if( module_Exists (p_intf, "dvdread") )
504         TestCaC( "dvdread-caching" );
505     if( module_Exists (p_intf, "dvdnav") )
506         TestCaC( "dvdnav-caching" );
507     TestCaC( "tcp-caching" );
508     TestCaC( "fake-caching" );
509     TestCaC( "cdda-caching" );
510     TestCaC( "screen-caching" );
511     TestCaC( "vcd-caching" );
512     TestCaCi( "rtsp-caching", 4 );
513     TestCaCi( "ftp-caching", 2 );
514     TestCaCi( "http-caching", 4 );
515     if(module_Exists (p_intf, "access_realrtsp"))
516         TestCaCi( "realrtsp-caching", 10 );
517     TestCaCi( "mms-caching", 19 );
518     if( b_cache_equal )
519     {
520         [o_input_cachelevel_pop selectItemWithTag: i_cache];
521         [o_input_cachelevel_custom_txt setHidden: YES];
522     }
523     else
524     {
525         [o_input_cachelevel_pop selectItemWithTitle: _NS("Custom")];
526         [o_input_cachelevel_custom_txt setHidden: NO];
527     }
528
529     /*********************
530      * subtitle settings *
531      *********************/
532     [o_osd_osd_ckb setState: config_GetInt( p_intf, "osd" )];
533     
534     [o_osd_encoding_pop removeAllItems];
535     y = 0;
536     p_item = config_FindConfig( VLC_OBJECT(p_intf), "subsdec-encoding" );
537     for( i = 0; p_item->ppsz_list[i] != nil; i++ )
538     {
539         if( p_item->ppsz_list[i] != "" )
540             [o_osd_encoding_pop addItemWithTitle: _NS( p_item->ppsz_list[i] )];
541         else
542             [o_osd_encoding_pop addItemWithTitle: @" "];
543
544         if( p_item->value.psz && !strcmp( p_item->value.psz, p_item->ppsz_list[i] ) )
545             y = i;
546     }
547     [o_osd_encoding_pop selectItemAtIndex: y];
548     
549     [o_osd_lang_fld setStringValue: [NSString stringWithUTF8String: config_GetPsz( p_intf, "sub-language" ) ?: ""]];
550     if( config_GetPsz( p_intf, "freetype-font" ) != NULL )
551         [o_osd_font_fld setStringValue: [NSString stringWithUTF8String: config_GetPsz( p_intf, "freetype-font" ) ?: ""]];
552
553     SetupIntList( o_osd_font_color_pop, "freetype-color" );
554     SetupIntList( o_osd_font_size_pop, "freetype-rel-fontsize" );
555     SetupIntList( o_osd_font_effect_pop, "freetype-effect" );
556
557     /********************
558      * hotkeys settings *
559      ********************/
560     struct hotkey *p_hotkeys = p_intf->p_libvlc->p_hotkeys;
561     o_hotkeySettings = [[NSMutableArray alloc] init];
562     NSMutableArray *o_tempArray_desc = [[NSMutableArray alloc] init];
563     i = 1;
564
565     while( i < 100 )
566     {
567         p_item = config_FindConfig( VLC_OBJECT(p_intf), p_hotkeys[i].psz_action );
568         if( !p_item )
569             break;
570
571         [o_tempArray_desc addObject: _NS( p_item->psz_text )];
572         [o_hotkeySettings addObject: [NSNumber numberWithInt: p_item->value.i]];
573
574         i++;
575     }
576     o_hotkeyDescriptions = [[NSArray alloc] initWithArray: o_tempArray_desc copyItems: YES];
577     [o_tempArray_desc release];
578     [o_hotkeys_listbox reloadData];
579 }
580
581 - (void)showSimplePrefs
582 {
583     /* we want to show the interface settings, if no category was chosen */
584     if( [o_sprefs_toolbar selectedItemIdentifier] == nil )
585     {
586         [o_sprefs_toolbar setSelectedItemIdentifier: VLCIntfSettingToolbarIdentifier];
587         [self showInterfaceSettings];
588     }
589     
590     [self resetControls];
591
592     [o_sprefs_win center];
593     [o_sprefs_win makeKeyAndOrderFront: self];
594 }
595
596 - (IBAction)buttonAction:(id)sender
597 {
598     if( sender == o_sprefs_cancel_btn )
599         [o_sprefs_win orderOut: sender];
600     else if( sender == o_sprefs_save_btn )
601     {
602         [self saveChangedSettings];
603         [o_sprefs_win orderOut: sender];
604     }
605     else if( sender == o_sprefs_reset_btn )
606         NSBeginInformationalAlertSheet( _NS("Reset Preferences"), _NS("Cancel"),
607                                         _NS("Continue"), nil, o_sprefs_win, self,
608                                         @selector(sheetDidEnd: returnCode: contextInfo:), NULL, nil,
609                                         _NS("Beware this will reset the VLC media player preferences.\n"
610                                             "Are you sure you want to continue?") );
611     else if( sender == o_sprefs_basicFull_matrix )
612     {
613         [o_sprefs_win orderOut: self];
614         [[o_sprefs_basicFull_matrix cellAtRow:0 column:0] setState: NSOffState];
615         [[o_sprefs_basicFull_matrix cellAtRow:0 column:1] setState: NSOnState];
616         [[[VLCMain sharedInstance] getPreferences] showPrefs];
617     }
618     else
619         msg_Warn( p_intf, "unknown buttonAction sender" );
620 }
621
622 - (void)sheetDidEnd:(NSWindow *)o_sheet 
623          returnCode:(int)i_return
624         contextInfo:(void *)o_context
625 {
626     if( i_return == NSAlertAlternateReturn )
627     {
628         config_ResetAll( p_intf );
629         b_intfSettingChanged = b_videoSettingChanged = b_audioSettingChanged = YES;
630         [self resetControls];
631     }
632 }
633
634 - (void)saveChangedSettings
635 {
636     module_config_t *p_item;
637     vlc_list_t *p_list;
638     module_t *p_parser;
639     char *psz_tmp;
640     int i;
641     
642 #define SaveIntList( object, name ) \
643     p_item = config_FindConfig( VLC_OBJECT(p_intf), name ); \
644     if( [object indexOfSelectedItem] >= 0 ) \
645         config_PutInt( p_intf, name, p_item->pi_list[[object indexOfSelectedItem]] ); \
646     else \
647         config_PutInt( p_intf, name, [object intValue] ) \
648                     
649 #define SaveStringList( object, name ) \
650     p_item = config_FindConfig( VLC_OBJECT(p_intf), name ); \
651     if( [object indexOfSelectedItem] >= 0 ) \
652         config_PutPsz( p_intf, name, strdup( p_item->ppsz_list[[object indexOfSelectedItem]] ) ); \
653     else \
654         config_PutPsz( p_intf, name, strdup( [[VLCMain sharedInstance] delocalizeString: [object stringValue]] ) )
655
656 #define SaveModuleList( object, name ) \
657     p_item = config_FindConfig( VLC_OBJECT(p_intf), name ); \
658     \
659     p_list = vlc_list_find( VLCIntf, VLC_OBJECT_MODULE, FIND_ANYWHERE ); \
660     for( int i_module_index = 0; i_module_index < p_list->i_count; i_module_index++ ) \
661     { \
662         p_parser = (module_t *)p_list->p_values[i_module_index].p_object; \
663         \
664         if( p_item->i_type == CONFIG_ITEM_MODULE && module_IsCapable( p_parser, p_item->psz_type ) ) \
665         { \
666             if( [[[object selectedItem] title] isEqualToString: _NS( module_GetLongName( p_parser ) )] ) \
667             { \
668                 config_PutPsz( p_intf, name, strdup( module_GetObjName( p_parser ))); \
669                 break; \
670             } \
671         } \
672     } \
673     vlc_list_release( p_list ); \
674     if( [[[object selectedItem] title] isEqualToString: _NS( "Default" )] ) \
675         config_PutPsz( p_intf, name, "" )
676
677     /**********************
678      * interface settings *
679      **********************/
680     if( b_intfSettingChanged )
681     {
682         SaveStringList( o_intf_lang_pop, "language" );
683         SaveIntList( o_intf_art_pop, "album-art" );
684
685         config_PutInt( p_intf, "macosx-fspanel", [o_intf_fspanel_ckb state] );
686         config_PutInt( p_intf, "embedded-video", [o_intf_embedded_ckb state] );
687
688         /* okay, let's save our changes to vlcrc */
689         i = config_SaveConfigFile( p_intf, "main" );
690         i = i + config_SaveConfigFile( p_intf, "macosx" );
691
692         if( i != 0 )
693         {
694             msg_Err( p_intf, "An error occurred while saving the Interface settings using SimplePrefs (%i)", i );
695             intf_UserFatal( p_intf, false, _("Interface Settings not saved"),
696                         _("An error occured while saving your settings via SimplePrefs (%i)."), i );
697             i = 0;
698         }
699
700         b_intfSettingChanged = NO;
701     }
702     
703     /******************
704      * audio settings *
705      ******************/
706     if( b_audioSettingChanged )
707     {
708         config_PutInt( p_intf, "audio", [o_audio_enable_ckb state] );
709         config_PutInt( p_intf, "volume", [o_audio_vol_sld intValue] );
710         config_PutInt( p_intf, "spdif", [o_audio_spdif_ckb state] );
711
712         SaveIntList( o_audio_dolby_pop, "force-dolby-surround" );
713
714         config_PutPsz( p_intf, "audio-language", [[o_audio_lang_fld stringValue] UTF8String] );
715         config_PutInt( p_intf, "headphone-dolby", [o_audio_headphone_ckb state] );
716
717         psz_tmp = config_GetPsz( p_intf, "audio-filter" );
718         if(! psz_tmp)
719             config_PutPsz( p_intf, "audio-filter", "volnorm" );
720         else if( (int)strstr( psz_tmp, "normvol" ) == NO )
721         {
722             /* work-around a GCC 4.0.1 bug */
723             psz_tmp = (char *)[[NSString stringWithFormat: @"%s:volnorm", psz_tmp] UTF8String];
724             config_PutPsz( p_intf, "audio-filter", psz_tmp );
725         }
726         else
727         {
728             psz_tmp = (char *)[[[NSString stringWithUTF8String: psz_tmp] stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@":volnorm"]] UTF8String];
729             psz_tmp = (char *)[[[NSString stringWithUTF8String: psz_tmp] stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@"volnorm:"]] UTF8String];
730             psz_tmp = (char *)[[[NSString stringWithUTF8String: psz_tmp] stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@"volnorm"]] UTF8String];
731             config_PutPsz( p_intf, "audio-filter", psz_tmp );
732         }
733         config_PutFloat( p_intf, "norm-max-level", [o_audio_norm_fld floatValue] );
734
735         SaveModuleList( o_audio_visual_pop, "audio-visual" );
736
737         /* Last.FM is optional */
738         if( module_Exists( p_intf, "audioscrobbler" ) )
739         {   
740             [o_audio_last_ckb setEnabled: YES];
741             if( [o_audio_last_ckb state] == NSOnState )
742                 config_AddIntf( p_intf, "audioscrobbler" );
743             else
744                 config_RemoveIntf( p_intf, "audioscrobbler" );
745
746             config_PutPsz( p_intf, "lastfm-username", [[o_audio_lastuser_fld stringValue] UTF8String] );
747             config_PutPsz( p_intf, "lastfm-password", [[o_audio_lastpwd_sfld stringValue] UTF8String] );
748         }
749         else
750             [o_audio_last_ckb setEnabled: NO];
751
752         /* okay, let's save our changes to vlcrc */
753         i = config_SaveConfigFile( p_intf, "main" );
754         i = i + config_SaveConfigFile( p_intf, "audioscrobbler" );
755         i = i + config_SaveConfigFile( p_intf, "volnorm" );
756
757         if( i != 0 )
758         {
759             msg_Err( p_intf, "An error occurred while saving the Audio settings using SimplePrefs (%i)", i );
760             intf_UserFatal( p_intf, false, _("Audio Settings not saved"),
761                         _("An error occured while saving your settings via SimplePrefs (%i)."), i );
762             
763             i = 0;
764         }
765         b_audioSettingChanged = NO;
766     }
767     
768     /******************
769      * video settings *
770      ******************/
771     if( b_videoSettingChanged )
772     {
773         config_PutInt( p_intf, "video", [o_video_enable_ckb state] );
774         config_PutInt( p_intf, "fullscreen", [o_video_fullscreen_ckb state] );
775         config_PutInt( p_intf, "video-on-top", [o_video_onTop_ckb state] );
776         config_PutInt( p_intf, "skip-frames", [o_video_skipFrames_ckb state] );
777         config_PutInt( p_intf, "macosx-black", [o_video_black_ckb state] );
778
779         SaveModuleList( o_video_output_pop, "vout" );
780         config_PutInt( p_intf, "macosx-vdev", [[o_video_device_pop selectedItem] tag] );
781
782         config_PutPsz( p_intf, "snapshot-path", [[o_video_snap_folder_fld stringValue] UTF8String] );
783         config_PutPsz( p_intf, "snapshot-prefix", [[o_video_snap_prefix_fld stringValue] UTF8String] );
784         config_PutInt( p_intf, "snapshot-sequential", [o_video_snap_seqnum_ckb state] );
785
786         if( [o_video_snap_format_pop indexOfSelectedItem] >= 0 )
787             config_PutPsz( p_intf, "snapshot-format", [[[o_video_snap_format_pop selectedItem] title] UTF8String] );
788
789         i = config_SaveConfigFile( p_intf, "main" );
790         i = i + config_SaveConfigFile( p_intf, "macosx" );
791
792         if( i != 0 )
793         {
794             msg_Err( p_intf, "An error occurred while saving the Video settings using SimplePrefs (%i)", i );
795             intf_UserFatal( p_intf, false, _("Video Settings not saved"),
796                         _("An error occured while saving your settings via SimplePrefs (%i)."), i );
797             i = 0;
798         }
799         b_videoSettingChanged = NO;
800     }
801     
802     /***************************
803      * input & codecs settings *
804      ***************************/
805     if( b_inputSettingChanged )
806     {
807         config_PutInt( p_intf, "server-port", [o_input_serverport_fld intValue] );
808         config_PutPsz( p_intf, "http-proxy", [[o_input_httpproxy_fld stringValue] UTF8String] );
809         config_PutPsz( p_intf, "http-proxy-pwd", [[o_input_httpproxypwd_sfld stringValue] UTF8String] );
810         config_PutInt( p_intf, "ffmpeg-pp-q", [o_input_postproc_fld intValue] );
811
812         SaveIntList( o_input_avi_pop, "avi-index" );
813
814         config_PutInt( p_intf, "rtsp-tcp", [o_input_rtsp_ckb state] );
815
816         #define CaCi( name, int ) config_PutInt( p_intf, name, int * [[o_input_cachelevel_pop selectedItem] tag] )
817         #define CaC( name ) CaCi( name, 1 )
818         msg_Dbg( p_intf, "Adjusting all cache values to: %i", [[o_input_cachelevel_pop selectedItem] tag] );
819         CaC( "udp-caching" );
820         if( module_Exists (p_intf, "dvdread" ) )
821         {
822             CaC( "dvdread-caching" );
823             i = i + config_SaveConfigFile( p_intf, "dvdread" );
824         }
825         if( module_Exists (p_intf, "dvdnav" ) )
826         {
827             CaC( "dvdnav-caching" );
828             i = i + config_SaveConfigFile( p_intf, "dvdnav" );
829         }
830         CaC( "tcp-caching" ); CaC( "vcd-caching" );
831         CaC( "fake-caching" ); CaC( "cdda-caching" ); CaC( "file-caching" );
832         CaC( "screen-caching" );
833         CaCi( "rtsp-caching", 4 ); CaCi( "ftp-caching", 2 );
834         CaCi( "http-caching", 4 );
835         if( module_Exists (p_intf, "access_realrtsp" ) )
836         {
837             CaCi( "realrtsp-caching", 10 );
838             i = i + config_SaveConfigFile( p_intf, "access_realrtsp" );
839         }
840         CaCi( "mms-caching", 19 );
841
842         #define SaveAccessFilter( object, name ) \
843         if( [object state] == NSOnState ) \
844         { \
845             if( b_first ) \
846             { \
847                 [o_temp appendString: name]; \
848                 b_first = NO; \
849             } \
850             else \
851                 [o_temp appendFormat: @":%@", name]; \
852         }
853
854         BOOL b_first = YES;
855         NSMutableString *o_temp = [[NSMutableString alloc] init];
856         SaveAccessFilter( o_input_record_ckb, @"record" );
857         SaveAccessFilter( o_input_dump_ckb, @"dump" );
858         SaveAccessFilter( o_input_bandwidth_ckb, @"bandwidth" );
859         SaveAccessFilter( o_input_timeshift_ckb, @"timeshift" );
860         config_PutPsz( p_intf, "access-filter", [o_temp UTF8String] );
861         [o_temp release];
862
863         i = config_SaveConfigFile( p_intf, "main" );
864         i = i + config_SaveConfigFile( p_intf, "ffmpeg" );
865         i = i + config_SaveConfigFile( p_intf, "access_http" );
866         i = i + config_SaveConfigFile( p_intf, "access_file" );
867         i = i + config_SaveConfigFile( p_intf, "access_tcp" );
868         i = i + config_SaveConfigFile( p_intf, "access_fake" );
869         i = i + config_SaveConfigFile( p_intf, "cdda" );
870         i = i + config_SaveConfigFile( p_intf, "screen" );
871         i = i + config_SaveConfigFile( p_intf, "vcd" );
872         i = i + config_SaveConfigFile( p_intf, "access_ftp" );
873         i = i + config_SaveConfigFile( p_intf, "access_mms" );
874         i = i + config_SaveConfigFile( p_intf, "live555" );
875
876         if( i != 0 )
877         {
878             msg_Err( p_intf, "An error occurred while saving the Input settings using SimplePrefs (%i)", i );
879             intf_UserFatal( p_intf, false, _("Input Settings not saved"),
880                         _("An error occured while saving your settings via SimplePrefs (%i)."), i );
881             i = 0;
882         }
883         b_inputSettingChanged = NO;
884     }
885     
886     /**********************
887      * subtitles settings *
888      **********************/
889     if( b_osdSettingChanged )
890     {
891         config_PutInt( p_intf, "osd", [o_osd_osd_ckb state] );
892
893         if( [o_osd_encoding_pop indexOfSelectedItem] >= 0 )
894             config_PutPsz( p_intf, "subsdec-encoding", [[[o_osd_encoding_pop selectedItem] title] UTF8String] );
895
896         config_PutPsz( p_intf, "sub-language", [[o_osd_lang_fld stringValue] UTF8String] );
897         config_PutPsz( p_intf, "freetype-font", [[o_osd_font_fld stringValue] UTF8String] );
898
899         SaveIntList( o_osd_font_color_pop, "freetype-color" );
900         SaveIntList( o_osd_font_size_pop, "freetype-rel-fontsize" );
901         SaveIntList( o_osd_font_effect_pop, "freetype-effect" );
902
903         i = config_SaveConfigFile( p_intf, NULL );
904
905         if( i != 0 )
906         {
907             msg_Err( p_intf, "An error occurred while saving the OSD/Subtitle settings using SimplePrefs (%i)", i );
908             intf_UserFatal( p_intf, false, _("OSD/Subtitle Settings not saved"),
909                         _("An error occured while saving your settings via SimplePrefs (%i)."), i );
910             i = 0;
911         }
912         b_osdSettingChanged = NO;
913     }
914     
915     /********************
916      * hotkeys settings *
917      ********************/
918     if( b_hotkeyChanged )
919     {
920         struct hotkey *p_hotkeys = p_intf->p_libvlc->p_hotkeys;
921         i = 1;
922         while( i < [o_hotkeySettings count] )
923         {
924             config_PutInt( p_intf, p_hotkeys[i].psz_action, [[o_hotkeySettings objectAtIndex: i-1] intValue] );
925             i++;
926         }        
927
928         i = config_SaveConfigFile( p_intf, "main" );
929
930         if( i != 0 )
931         {
932             msg_Err( p_intf, "An error occurred while saving the Hotkey settings using SimplePrefs (%i)", i );
933             intf_UserFatal( p_intf, false, _("Hotkeys not saved"),
934                         _("An error occured while saving your settings via SimplePrefs (%i)."), i );
935             i = 0;
936         }
937         b_hotkeyChanged = NO;
938     }
939 }
940
941 - (void)showSettingsForCategory: (id)o_new_category_view
942 {
943     NSRect o_win_rect, o_view_rect, o_old_view_rect;
944     o_win_rect = [o_sprefs_win frame];
945     o_view_rect = [o_new_category_view frame];
946     
947     if( o_currentlyShownCategoryView != nil )
948     {
949         /* restore our window's height, if we've shown another category previously */
950         o_old_view_rect = [o_currentlyShownCategoryView frame];
951         o_win_rect.size.height = o_win_rect.size.height - o_old_view_rect.size.height;
952         o_win_rect.origin.y = ( o_win_rect.origin.y + o_old_view_rect.size.height ) - o_view_rect.size.height;
953
954         /* remove our previous category view */
955         [o_currentlyShownCategoryView removeFromSuperviewWithoutNeedingDisplay];
956     }
957     
958     o_win_rect.size.height = o_win_rect.size.height + o_view_rect.size.height;
959     
960     [o_sprefs_win displayIfNeeded];
961     [o_sprefs_win setFrame: o_win_rect display:YES animate: YES];
962     
963     [o_new_category_view setFrame: NSMakeRect( 0, 
964                                                [o_sprefs_controls_box frame].size.height, 
965                                                o_view_rect.size.width, 
966                                                o_view_rect.size.height )];
967     [o_new_category_view setNeedsDisplay: YES];
968     [o_new_category_view setAutoresizesSubviews: YES];
969     [[o_sprefs_win contentView] addSubview: o_new_category_view];
970     
971     /* keep our current category for further reference */
972     [o_currentlyShownCategoryView release];
973     o_currentlyShownCategoryView = o_new_category_view;
974     [o_currentlyShownCategoryView retain];
975 }
976
977 - (IBAction)interfaceSettingChanged:(id)sender
978 {
979     b_intfSettingChanged = YES;
980 }
981
982 - (void)showInterfaceSettings
983 {
984     [self showSettingsForCategory: o_intf_view];
985 }
986
987 - (IBAction)audioSettingChanged:(id)sender
988 {
989     if( sender == o_audio_vol_sld )
990         [o_audio_vol_fld setIntValue: [o_audio_vol_sld intValue]];
991
992     if( sender == o_audio_vol_fld )
993         [o_audio_vol_sld setIntValue: [o_audio_vol_fld intValue]];
994
995     if( sender == o_audio_last_ckb )
996     {
997         if( [o_audio_last_ckb state] == NSOnState )
998         {
999             [o_audio_lastpwd_sfld setEnabled: YES];
1000             [o_audio_lastuser_fld setEnabled: YES];
1001         }
1002         else
1003         {
1004             [o_audio_lastpwd_sfld setEnabled: NO];
1005             [o_audio_lastuser_fld setEnabled: NO];
1006         }
1007     }
1008
1009     b_audioSettingChanged = YES;
1010 }
1011
1012 - (void)showAudioSettings
1013 {
1014     [self showSettingsForCategory: o_audio_view];
1015 }
1016
1017 - (IBAction)videoSettingChanged:(id)sender
1018 {
1019     if( sender == o_video_snap_folder_btn )
1020     {
1021         o_selectFolderPanel = [[NSOpenPanel alloc] init];
1022         [o_selectFolderPanel setCanChooseDirectories: YES];
1023         [o_selectFolderPanel setCanChooseFiles: NO];
1024         [o_selectFolderPanel setResolvesAliases: YES];
1025         [o_selectFolderPanel setAllowsMultipleSelection: NO];
1026         [o_selectFolderPanel setMessage: _NS("Choose the folder to save your video snapshots to.")];
1027         [o_selectFolderPanel setCanCreateDirectories: YES];
1028         [o_selectFolderPanel setPrompt: _NS("Choose")];
1029         [o_selectFolderPanel beginSheetForDirectory: nil file: nil modalForWindow: o_sprefs_win 
1030                                       modalDelegate: self 
1031                                      didEndSelector: @selector(savePanelDidEnd:returnCode:contextInfo:)
1032                                         contextInfo: o_video_snap_folder_btn];
1033     }
1034     else
1035         b_videoSettingChanged = YES;
1036 }
1037
1038 - (void)savePanelDidEnd:(NSOpenPanel * )panel returnCode: (int)returnCode contextInfo: (void *)contextInfo
1039 {
1040     if( returnCode == NSOKButton )
1041     {
1042         if( contextInfo == o_video_snap_folder_btn )
1043         {
1044             [o_video_snap_folder_fld setStringValue: [o_selectFolderPanel filename]];
1045             b_videoSettingChanged = YES;
1046         }
1047         else if( contextInfo == o_osd_font_btn )
1048         {
1049             [o_osd_font_fld setStringValue: [o_selectFolderPanel filename]];
1050             b_osdSettingChanged = YES;
1051         }
1052     }
1053
1054     [o_selectFolderPanel release];
1055 }
1056
1057 - (void)showVideoSettings
1058 {
1059     [self showSettingsForCategory: o_video_view];
1060 }
1061
1062 - (IBAction)osdSettingChanged:(id)sender
1063 {
1064     if( sender == o_osd_font_btn )
1065     {
1066         o_selectFolderPanel = [[NSOpenPanel alloc] init];
1067         [o_selectFolderPanel setCanChooseDirectories: NO];
1068         [o_selectFolderPanel setCanChooseFiles: YES];
1069         [o_selectFolderPanel setResolvesAliases: YES];
1070         [o_selectFolderPanel setAllowsMultipleSelection: NO];
1071         [o_selectFolderPanel setMessage: _NS("Choose the font to display your Subtitles with.")];
1072         [o_selectFolderPanel setCanCreateDirectories: NO];
1073         [o_selectFolderPanel setPrompt: _NS("Choose")];
1074         [o_selectFolderPanel setAllowedFileTypes: [NSArray arrayWithObjects: @"dfont", @"ttf", @"otf", @"FFIL", nil]];
1075         [o_selectFolderPanel beginSheetForDirectory: @"/System/Library/Fonts/" file: nil modalForWindow: o_sprefs_win 
1076                                       modalDelegate: self 
1077                                      didEndSelector: @selector(savePanelDidEnd:returnCode:contextInfo:)
1078                                         contextInfo: o_osd_font_btn];
1079     }
1080     else
1081         b_osdSettingChanged = YES;
1082 }
1083
1084 - (void)showOSDSettings
1085 {
1086     [self showSettingsForCategory: o_osd_view];
1087 }
1088
1089 - (IBAction)inputSettingChanged:(id)sender
1090 {
1091     if( sender == o_input_cachelevel_pop )
1092     {
1093         if( [[[o_input_cachelevel_pop selectedItem] title] isEqualToString: _NS("Custom")] )
1094             [o_input_cachelevel_custom_txt setHidden: NO];
1095         else
1096             [o_input_cachelevel_custom_txt setHidden: YES];
1097     }
1098
1099     b_inputSettingChanged = YES;
1100 }
1101
1102 - (void)showInputSettings
1103 {
1104     [self showSettingsForCategory: o_input_view];
1105 }
1106
1107 - (IBAction)hotkeySettingChanged:(id)sender
1108 {
1109     if( sender == o_hotkeys_change_btn || sender == o_hotkeys_listbox )
1110     {
1111         [o_hotkeys_change_lbl setStringValue: [NSString stringWithFormat: _NS("Press new keys for\n\"%@\""), 
1112                                                [o_hotkeyDescriptions objectAtIndex: [o_hotkeys_listbox selectedRow]]]];
1113         [o_hotkeys_change_keys_lbl setStringValue: [self OSXKeyToString:[[o_hotkeySettings objectAtIndex: [o_hotkeys_listbox selectedRow]] intValue]]];
1114         [o_hotkeys_change_taken_lbl setStringValue: @""];
1115         [o_hotkeys_change_win setInitialFirstResponder: [o_hotkeys_change_win contentView]];
1116         [o_hotkeys_change_win makeFirstResponder: [o_hotkeys_change_win contentView]];
1117         [NSApp runModalForWindow: o_hotkeys_change_win];
1118     }
1119     else if( sender == o_hotkeys_change_cancel_btn )
1120     {
1121         [NSApp stopModal];
1122         [o_hotkeys_change_win close];
1123     }
1124     else if( sender == o_hotkeys_change_ok_btn )
1125     {
1126         int i_returnValue;
1127         if(! o_keyInTransition )
1128         {
1129             [NSApp stopModal];
1130             [o_hotkeys_change_win close];
1131             msg_Err( p_intf, "internal error prevented the hotkey switch" );
1132             return;
1133         }
1134
1135         b_hotkeyChanged = YES;
1136
1137         i_returnValue = [o_hotkeySettings indexOfObject: o_keyInTransition];
1138         if( i_returnValue != NSNotFound )
1139             [o_hotkeySettings replaceObjectAtIndex: i_returnValue withObject: [[NSNumber numberWithInt: 0] retain]];
1140
1141         [o_hotkeySettings replaceObjectAtIndex: [o_hotkeys_listbox selectedRow] withObject: [o_keyInTransition retain]];
1142
1143         [NSApp stopModal];
1144         [o_hotkeys_change_win close];
1145
1146         [o_hotkeys_listbox reloadData];
1147     }
1148     else if( sender == o_hotkeys_clear_btn )
1149     {
1150         [o_hotkeySettings replaceObjectAtIndex: [o_hotkeys_listbox selectedRow] withObject: [NSNumber numberWithInt: 0]];
1151         [o_hotkeys_listbox reloadData];
1152         b_hotkeyChanged = YES;
1153     }
1154 }
1155
1156 - (void)showHotkeySettings
1157 {
1158     [self showSettingsForCategory: o_hotkeys_view];
1159 }
1160
1161 - (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
1162 {
1163     return [o_hotkeySettings count];
1164 }
1165
1166 - (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
1167 {
1168     if( [[aTableColumn identifier] isEqualToString: @"action"] )
1169         return [o_hotkeyDescriptions objectAtIndex: rowIndex];
1170     else if( [[aTableColumn identifier] isEqualToString: @"shortcut"] )
1171         return [self OSXKeyToString: [[o_hotkeySettings objectAtIndex: rowIndex] intValue]];
1172     else
1173     {
1174         msg_Err( p_intf, "unknown TableColumn identifier (%s)!", [[aTableColumn identifier] UTF8String] );
1175         return NULL;
1176     }
1177 }
1178
1179 - (void)changeHotkeyTo: (int)i_theNewKey
1180 {
1181     int i_returnValue;
1182     i_returnValue = [o_hotkeysNonUseableKeys indexOfObject: [NSNumber numberWithInt: i_theNewKey]];
1183     if( i_returnValue != NSNotFound || i_theNewKey == 0 )
1184     {
1185         [o_hotkeys_change_keys_lbl setStringValue: _NS("Invalid combination")];
1186         [o_hotkeys_change_taken_lbl setStringValue: _NS("Regrettably, these keys cannot be assigned as hotkey shortcuts.")];
1187         [o_hotkeys_change_ok_btn setEnabled: NO];
1188     }
1189     else
1190     {
1191         NSString *o_temp;
1192         if( o_keyInTransition )
1193             [o_keyInTransition release];
1194         o_keyInTransition = [[NSNumber numberWithInt: i_theNewKey] retain];
1195
1196         o_temp = [self OSXKeyToString: i_theNewKey];
1197
1198         [o_hotkeys_change_keys_lbl setStringValue: o_temp];
1199
1200         i_returnValue = [o_hotkeySettings indexOfObject: o_keyInTransition];
1201         if( i_returnValue != NSNotFound )
1202             [o_hotkeys_change_taken_lbl setStringValue: [NSString stringWithFormat:
1203                                                          _NS("This combination is already taken by \"%@\"."),
1204                                                          [o_hotkeyDescriptions objectAtIndex: i_returnValue]]];
1205         else
1206             [o_hotkeys_change_taken_lbl setStringValue: @""];
1207
1208         [o_hotkeys_change_ok_btn setEnabled: YES];
1209     }
1210 }
1211     
1212 @end
1213
1214 /********************
1215  * hotkeys settings *
1216  ********************/
1217
1218 @implementation VLCHotkeyChangeWindow
1219
1220 - (BOOL)resignFirstResponder
1221 {
1222     /* We need to stay the first responder or we'll miss the user's input */
1223     return NO;
1224 }
1225
1226 - (void)keyDown:(NSEvent *)o_theEvent
1227 {
1228     int i_nonReadableKey = 0;
1229
1230     if( [o_theEvent modifierFlags] & NSControlKeyMask )
1231         i_nonReadableKey = i_nonReadableKey | KEY_MODIFIER_CTRL;
1232
1233     if( [o_theEvent modifierFlags] & NSAlternateKeyMask  )
1234         i_nonReadableKey = i_nonReadableKey | KEY_MODIFIER_ALT;
1235
1236     if( [o_theEvent modifierFlags] & NSShiftKeyMask )
1237         i_nonReadableKey = i_nonReadableKey | KEY_MODIFIER_SHIFT;
1238
1239     if( [o_theEvent modifierFlags] & NSCommandKeyMask )
1240         i_nonReadableKey = i_nonReadableKey | KEY_MODIFIER_COMMAND;
1241
1242     if( [o_theEvent modifierFlags] & NSFunctionKeyMask  )
1243     {
1244         unichar key = 0;
1245         key = [[o_theEvent charactersIgnoringModifiers] characterAtIndex: 0];
1246         
1247         switch( key )
1248         {
1249             case 0x1b:
1250                 i_nonReadableKey = i_nonReadableKey | KEY_ESC;
1251                 break;
1252             case NSF1FunctionKey:
1253                 i_nonReadableKey = i_nonReadableKey | KEY_F1;
1254                 break;
1255             case NSF2FunctionKey:
1256                 i_nonReadableKey = i_nonReadableKey | KEY_F2;
1257                 break;
1258             case NSF3FunctionKey:
1259                 i_nonReadableKey = i_nonReadableKey | KEY_F3;
1260                 break;
1261             case NSF4FunctionKey:
1262                 i_nonReadableKey = i_nonReadableKey | KEY_F4;
1263                 break;
1264             case NSF5FunctionKey:
1265                 i_nonReadableKey = i_nonReadableKey | KEY_F5;
1266                 break;
1267             case NSF6FunctionKey:
1268                 i_nonReadableKey = i_nonReadableKey | KEY_F6;
1269                 break;
1270             case NSF7FunctionKey:
1271                 i_nonReadableKey = i_nonReadableKey | KEY_F7;
1272                 break;
1273             case NSF8FunctionKey:
1274                 i_nonReadableKey = i_nonReadableKey | KEY_F8;
1275                 break;
1276             case NSF9FunctionKey:
1277                 i_nonReadableKey = i_nonReadableKey | KEY_F9;
1278                 break;
1279             case NSF10FunctionKey:
1280                 i_nonReadableKey = i_nonReadableKey | KEY_F10;
1281                 break;
1282             case NSF11FunctionKey:
1283                 i_nonReadableKey = i_nonReadableKey | KEY_F11;
1284                 break;
1285             case NSF12FunctionKey:
1286                 i_nonReadableKey = i_nonReadableKey | KEY_F12;
1287                 break;
1288             case NSInsertFunctionKey:
1289                 i_nonReadableKey = i_nonReadableKey | KEY_INSERT;
1290                 break;
1291             case NSHomeFunctionKey:
1292                 i_nonReadableKey = i_nonReadableKey | KEY_HOME;
1293                 break;
1294             case NSEndFunctionKey:
1295                 i_nonReadableKey = i_nonReadableKey | KEY_END;
1296                 break;
1297             case NSPageUpFunctionKey:
1298                 i_nonReadableKey = i_nonReadableKey | KEY_PAGEUP;
1299                 break;
1300             case NSPageDownFunctionKey:
1301                 i_nonReadableKey = i_nonReadableKey | KEY_PAGEDOWN;
1302                 break;
1303             case NSMenuFunctionKey:
1304                 i_nonReadableKey = i_nonReadableKey | KEY_MENU;
1305                 break;
1306             case NSTabCharacter:
1307                 i_nonReadableKey = i_nonReadableKey | KEY_TAB;
1308                 break;
1309             case NSDeleteCharacter:
1310                 i_nonReadableKey = i_nonReadableKey | KEY_DELETE;
1311                 break;
1312             case NSBackspaceCharacter:
1313                 i_nonReadableKey = i_nonReadableKey | KEY_BACKSPACE;
1314                 break;
1315             case NSUpArrowFunctionKey:
1316                 i_nonReadableKey = i_nonReadableKey | KEY_UP;
1317                 break;
1318             case NSDownArrowFunctionKey:
1319                 i_nonReadableKey = i_nonReadableKey | KEY_DOWN;
1320                 break;
1321             case NSRightArrowFunctionKey:
1322                 i_nonReadableKey = i_nonReadableKey | KEY_RIGHT;
1323                 break;
1324             case NSLeftArrowFunctionKey:
1325                 i_nonReadableKey = i_nonReadableKey | KEY_LEFT;
1326                 break;
1327             case NSEnterCharacter:
1328                 i_nonReadableKey = i_nonReadableKey | KEY_ENTER;
1329                 break;
1330             default:
1331             {
1332                 msg_Warn( VLCIntf, "user pressed unknown function key" );
1333                 i_nonReadableKey = 0;
1334                 break;
1335             }
1336         }
1337     }
1338     else
1339     {
1340         if( [[o_theEvent charactersIgnoringModifiers] isEqualToString: @" "] )
1341             i_nonReadableKey = i_nonReadableKey | KEY_SPACE;
1342         else
1343             i_nonReadableKey = i_nonReadableKey | StringToKey( (char *)[[[o_theEvent charactersIgnoringModifiers] lowercaseString] UTF8String] );
1344     }
1345
1346     [[[VLCMain sharedInstance] getSimplePreferences] changeHotkeyTo: i_nonReadableKey];
1347 }
1348
1349 @end