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