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