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