]> git.sesse.net Git - vlc/blob - modules/gui/macosx/simple_prefs.m
Fix type
[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 = config_GetInt(p_intf, "volume");
476         i = i * 200.0 / AOUT_VOLUME_MAX + 0.5;
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     [self setupButton: o_input_avcodec_hw_ckb forBoolValue:"avcodec-hw"];
549
550     [self setupButton: o_input_avi_pop forIntList: "avi-index"];
551
552     [self setupButton: o_input_rtsp_ckb forBoolValue: "rtsp-tcp"];
553     [self setupButton: o_input_skipLoop_pop forIntList: "avcodec-skiploopfilter"];
554
555     [self setupButton: o_input_mkv_preload_dir_ckb forBoolValue: "mkv-preload-local-dir"];
556
557     [o_input_cachelevel_pop removeAllItems];
558     [o_input_cachelevel_pop addItemsWithTitles:
559         [NSArray arrayWithObjects: _NS("Custom"), _NS("Lowest latency"), _NS("Low latency"), _NS("Normal"),
560             _NS("High latency"), _NS("Higher latency"), nil]];
561     [[o_input_cachelevel_pop itemAtIndex: 0] setTag: 0];
562     [[o_input_cachelevel_pop itemAtIndex: 1] setTag: 100];
563     [[o_input_cachelevel_pop itemAtIndex: 2] setTag: 200];
564     [[o_input_cachelevel_pop itemAtIndex: 3] setTag: 300];
565     [[o_input_cachelevel_pop itemAtIndex: 4] setTag: 500];
566     [[o_input_cachelevel_pop itemAtIndex: 5] setTag: 1000];
567
568     #define TestCaC(name, factor) \
569     b_cache_equal =  b_cache_equal && \
570     (i_cache * factor == config_GetInt(p_intf, name));
571
572     /* Select the accurate value of the PopupButton */
573     bool b_cache_equal = true;
574     int i_cache = config_GetInt(p_intf, "file-caching");
575
576     TestCaC("network-caching", 10/3);
577     TestCaC("disc-caching", 1);
578     TestCaC("live-caching", 1);
579     if (b_cache_equal) {
580         [o_input_cachelevel_pop selectItemWithTag: i_cache];
581         [o_input_cachelevel_custom_txt setHidden: YES];
582     } else {
583         [o_input_cachelevel_pop selectItemWithTitle: _NS("Custom")];
584         [o_input_cachelevel_custom_txt setHidden: NO];
585     }
586     #undef TestCaC
587
588     /*********************
589      * subtitle settings *
590      *********************/
591     [self setupButton: o_osd_osd_ckb forBoolValue: "osd"];
592
593     [self setupButton: o_osd_encoding_pop forStringList: "subsdec-encoding"];
594     [self setupField: o_osd_lang_fld forOption: "sub-language" ];
595
596     [self setupField: o_osd_font_fld forOption: "freetype-font"];
597     [self setupButton: o_osd_font_color_pop forIntList: "freetype-color"];
598     [self setupButton: o_osd_font_size_pop forIntList: "freetype-rel-fontsize"];
599     i = config_GetInt(p_intf, "freetype-opacity") * 100.0 / 255.0 + 0.5;
600     [o_osd_opacity_fld setIntValue: i];
601     [o_osd_opacity_sld setIntValue: i];
602     [o_osd_opacity_sld setToolTip: _NS(config_GetLabel(p_intf, "freetype-opacity"))];
603     [o_osd_opacity_fld setToolTip: [o_osd_opacity_sld toolTip]];
604     [self setupButton: o_osd_forcebold_ckb forBoolValue: "freetype-bold"];
605     [self setupButton: o_osd_outline_color_pop forIntList: "freetype-outline-color"];
606     [self setupButton: o_osd_outline_thickness_pop forIntList: "freetype-outline-thickness"];
607
608     /********************
609      * hotkeys settings *
610      ********************/
611     const struct hotkey *p_hotkeys = p_intf->p_libvlc->p_hotkeys;
612     [o_hotkeySettings release];
613     o_hotkeySettings = [[NSMutableArray alloc] init];
614     NSMutableArray *o_tempArray_desc = [[NSMutableArray alloc] init];
615     NSMutableArray *o_tempArray_names = [[NSMutableArray alloc] init];
616
617     /* Get the main Module */
618     module_t *p_main = module_get_main();
619     assert(p_main);
620     unsigned confsize;
621     module_config_t *p_config;
622
623     p_config = module_config_get (p_main, &confsize);
624
625     for (size_t i = 0; i < confsize; i++) {
626         module_config_t *p_item = p_config + i;
627
628         if (CONFIG_ITEM(p_item->i_type) && p_item->psz_name != NULL
629            && !strncmp(p_item->psz_name , "key-", 4)
630            && !EMPTY_STR(p_item->psz_text)) {
631             [o_tempArray_desc addObject: _NS(p_item->psz_text)];
632             [o_tempArray_names addObject: [NSString stringWithUTF8String:p_item->psz_name]];
633             if (p_item->value.psz)
634                 [o_hotkeySettings addObject: [NSString stringWithUTF8String:p_item->value.psz]];
635             else
636                 [o_hotkeySettings addObject: [NSString string]];
637         }
638     }
639     module_config_free (p_config);
640
641     [o_hotkeyDescriptions release];
642     o_hotkeyDescriptions = [[NSArray alloc] initWithArray: o_tempArray_desc copyItems: YES];
643     [o_tempArray_desc release];
644     [o_hotkeyNames release];
645     o_hotkeyNames = [[NSArray alloc] initWithArray: o_tempArray_names copyItems: YES];
646     [o_tempArray_names release];
647     [o_hotkeys_listbox reloadData];
648 }
649
650 #pragma mark -
651 #pragma mark General actions
652
653 - (void)showSimplePrefs
654 {
655     /* we want to show the interface settings, if no category was chosen */
656     if ([[o_sprefs_win toolbar] selectedItemIdentifier] == nil) {
657         [[o_sprefs_win toolbar] setSelectedItemIdentifier: VLCIntfSettingToolbarIdentifier];
658         [self showInterfaceSettings];
659     }
660
661     [self resetControls];
662
663     [o_sprefs_win center];
664     [o_sprefs_win makeKeyAndOrderFront: self];
665 }
666
667 - (void)showSimplePrefsWithLevel:(NSInteger)i_window_level
668 {
669     [o_sprefs_win setLevel: i_window_level];
670     [self showSimplePrefs];
671 }
672
673 - (IBAction)buttonAction:(id)sender
674 {
675     if (sender == o_sprefs_cancel_btn) {
676         [[NSFontPanel sharedFontPanel] close];
677         [o_sprefs_win orderOut: sender];
678     } else if (sender == o_sprefs_save_btn) {
679         [self saveChangedSettings];
680         [[NSFontPanel sharedFontPanel] close];
681         [o_sprefs_win orderOut: sender];
682     } else if (sender == o_sprefs_reset_btn)
683         NSBeginInformationalAlertSheet(_NS("Reset Preferences"), _NS("Cancel"),
684                                         _NS("Continue"), nil, o_sprefs_win, self,
685                                         @selector(sheetDidEnd: returnCode: contextInfo:), NULL, nil,
686                                         _NS("Beware this will reset the VLC media player preferences.\n"
687                                             "Are you sure you want to continue?"));
688     else if (sender == o_sprefs_showAll_btn) {
689         [o_sprefs_win orderOut: self];
690         [[[VLCMain sharedInstance] preferences] showPrefsWithLevel:[o_sprefs_win level]];
691     } else
692         msg_Warn(p_intf, "unknown buttonAction sender");
693 }
694
695 - (void)sheetDidEnd:(NSWindow *)o_sheet
696          returnCode:(int)i_return
697         contextInfo:(void *)o_context
698 {
699     if (i_return == NSAlertAlternateReturn) {
700         /* reset VLC's config */
701         config_ResetAll(p_intf);
702         [self resetControls];
703         config_SaveConfigFile(p_intf);
704
705         /* reset OS X defaults */
706         [NSUserDefaults resetStandardUserDefaults];
707         [[NSUserDefaults standardUserDefaults] synchronize];
708     }
709 }
710
711 static inline void save_int_list(intf_thread_t * p_intf, id object, const char * name)
712 {
713     NSNumber *p_valueobject;
714     module_config_t *p_item;
715     p_item = config_FindConfig(VLC_OBJECT(p_intf), name);
716     p_valueobject = (NSNumber *)[[object selectedItem] representedObject];
717     assert([p_valueobject isKindOfClass:[NSNumber class]]);
718     if (p_valueobject) config_PutInt(p_intf, name, [p_valueobject intValue]);
719 }
720
721 static inline void save_string_list(intf_thread_t * p_intf, id object, const char * name)
722 {
723     NSString *p_stringobject;
724     module_config_t *p_item;
725     p_item = config_FindConfig(VLC_OBJECT(p_intf), name);
726     p_stringobject = (NSString *)[[object selectedItem] representedObject];
727     assert([p_stringobject isKindOfClass:[NSString class]]);
728     if (p_stringobject) {
729         config_PutPsz(p_intf, name, [p_stringobject UTF8String]);
730     }
731 }
732
733 static inline void save_module_list(intf_thread_t * p_intf, id object, const char * name)
734 {
735     module_config_t *p_item;
736     module_t *p_parser, **p_list;
737     NSString * objectTitle = [[object selectedItem] title];
738
739     p_item = config_FindConfig(VLC_OBJECT(p_intf), name);
740
741     size_t count;
742     p_list = module_list_get(&count);
743     for (size_t i_module_index = 0; i_module_index < count; i_module_index++) {
744         p_parser = p_list[i_module_index];
745
746         if (p_item->i_type == CONFIG_ITEM_MODULE && module_provides(p_parser, p_item->psz_type)) {
747             if ([objectTitle isEqualToString: _NS(module_GetLongName(p_parser))])
748             {
749                 config_PutPsz(p_intf, name, strdup(module_get_object(p_parser)));
750                 break;
751             }
752         }
753     }
754     module_list_free(p_list);
755     if ([objectTitle isEqualToString: _NS("Default")])
756         config_PutPsz(p_intf, name, "");
757 }
758
759 - (void)saveChangedSettings
760 {
761     NSString *tmpString;
762     NSRange tmpRange;
763
764 #define SaveIntList(object, name) save_int_list(p_intf, object, name)
765
766 #define SaveStringList(object, name) save_string_list(p_intf, object, name)
767
768 #define SaveModuleList(object, name) save_module_list(p_intf, object, name)
769
770 #define getString(name) [NSString stringWithFormat:@"%s", config_GetPsz(p_intf, name)]
771
772     /**********************
773      * interface settings *
774      **********************/
775     if (b_intfSettingChanged) {
776         SaveIntList(o_intf_art_pop, "album-art");
777
778         config_PutInt(p_intf, "macosx-fspanel", [o_intf_fspanel_ckb state]);
779         config_PutInt(p_intf, "embedded-video", [o_intf_embedded_ckb state]);
780
781         config_PutInt(p_intf, "macosx-appleremote", [o_intf_appleremote_ckb state]);
782         config_PutInt(p_intf, "macosx-appleremote-sysvol", [o_intf_appleremote_sysvol_ckb state]);
783         config_PutInt(p_intf, "macosx-mediakeys", [o_intf_mediakeys_ckb state]);
784         config_PutInt(p_intf, "macosx-interfacestyle", [o_intf_style_dark_bcell state]);
785         config_PutInt(p_intf, "macosx-nativefullscreenmode", [o_intf_nativefullscreen_ckb state]);
786         config_PutInt(p_intf, "macosx-pause-minimized", [o_intf_pauseminimized_ckb state]);
787         config_PutInt(p_intf, "macosx-video-autoresize", [o_intf_autoresize_ckb state]);
788         if ([o_intf_enableGrowl_ckb state] == NSOnState) {
789             tmpString = getString("control");
790             tmpRange = [tmpString rangeOfString:@"growl"];
791             if ([tmpString length] > 0 && tmpRange.location == NSNotFound)
792             {
793                 tmpString = [tmpString stringByAppendingString: @":growl"];
794                 config_PutPsz(p_intf, "control", [tmpString UTF8String]);
795             }
796             else
797                 config_PutPsz(p_intf, "control", "growl");
798         } else {
799             tmpString = getString("control");
800             if (! [tmpString isEqualToString:@""])
801             {
802                 tmpString = [tmpString stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@":growl"]];
803                 tmpString = [tmpString stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@"growl:"]];
804                 tmpString = [tmpString stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@"growl"]];
805                 config_PutPsz(p_intf, "control", [tmpString UTF8String]);
806             }
807         }
808
809         /* activate stuff without restart */
810         if ([o_intf_appleremote_ckb state] == YES)
811             [[[VLCMain sharedInstance] appleRemoteController] startListening: [VLCMain sharedInstance]];
812         else
813             [[[VLCMain sharedInstance] appleRemoteController] stopListening: [VLCMain sharedInstance]];
814         b_intfSettingChanged = NO;
815     }
816
817     /******************
818      * audio settings *
819      ******************/
820     if (b_audioSettingChanged) {
821         config_PutInt(p_intf, "audio", [o_audio_enable_ckb state]);
822         if ([o_audio_vol_fld isEnabled])
823             config_PutInt(p_intf, "volume", [o_audio_vol_fld intValue] * AOUT_VOLUME_MAX / 200.0 + 0.5);
824         config_PutInt(p_intf, "volume-save", [o_audio_autosavevol_yes_bcell state]);
825         config_PutInt(p_intf, "spdif", [o_audio_spdif_ckb state]);
826
827         SaveIntList(o_audio_dolby_pop, "force-dolby-surround");
828
829         config_PutPsz(p_intf, "audio-language", [[o_audio_lang_fld stringValue] UTF8String]);
830
831         SaveModuleList(o_audio_visual_pop, "audio-visual");
832
833         /* Last.FM is optional */
834         if (module_exists("audioscrobbler")) {
835             [o_audio_last_ckb setEnabled: YES];
836             if ([o_audio_last_ckb state] == NSOnState)
837                 config_AddIntf(p_intf, "audioscrobbler");
838             else
839                 config_RemoveIntf(p_intf, "audioscrobbler");
840
841             config_PutPsz(p_intf, "lastfm-username", [[o_audio_lastuser_fld stringValue] UTF8String]);
842             config_PutPsz(p_intf, "lastfm-password", [[o_audio_lastpwd_sfld stringValue] UTF8String]);
843         }
844         else
845             [o_audio_last_ckb setEnabled: NO];
846         b_audioSettingChanged = NO;
847     }
848
849     /******************
850      * video settings *
851      ******************/
852     if (b_videoSettingChanged) {
853         config_PutInt(p_intf, "video", [o_video_enable_ckb state]);
854         config_PutInt(p_intf, "fullscreen", [o_video_fullscreen_ckb state]);
855         config_PutInt(p_intf, "video-deco", [o_video_videodeco_ckb state]);
856         config_PutInt(p_intf, "video-on-top", [o_video_onTop_ckb state]);
857         config_PutInt(p_intf, "skip-frames", [o_video_skipFrames_ckb state]);
858         config_PutInt(p_intf, "macosx-black", [o_video_black_ckb state]);
859
860         SaveModuleList(o_video_output_pop, "vout");
861         config_PutInt(p_intf, "macosx-vdev", [[o_video_device_pop selectedItem] tag]);
862
863         config_PutPsz(p_intf, "snapshot-path", [[o_video_snap_folder_fld stringValue] UTF8String]);
864         config_PutPsz(p_intf, "snapshot-prefix", [[o_video_snap_prefix_fld stringValue] UTF8String]);
865         config_PutInt(p_intf, "snapshot-sequential", [o_video_snap_seqnum_ckb state]);
866         SaveStringList(o_video_snap_format_pop, "snapshot-format");
867         SaveIntList(o_video_deinterlace_pop, "deinterlace");
868         SaveStringList(o_video_deinterlace_mode_pop, "deinterlace-mode");
869         b_videoSettingChanged = NO;
870     }
871
872     /***************************
873      * input & codecs settings *
874      ***************************/
875     if (b_inputSettingChanged) {
876         config_PutPsz(p_intf, "input-record-path", [[o_input_record_fld stringValue] UTF8String]);
877         config_PutPsz(p_intf, "http-proxy", [[o_input_httpproxy_fld stringValue] UTF8String]);
878         config_PutPsz(p_intf, "http-proxy-pwd", [[o_input_httpproxypwd_sfld stringValue] UTF8String]);
879         config_PutInt(p_intf, "postproc-q", [o_input_postproc_fld intValue]);
880
881         SaveIntList(o_input_avi_pop, "avi-index");
882
883         config_PutInt(p_intf, "rtsp-tcp", [o_input_rtsp_ckb state]);
884         config_PutInt(p_intf, "avcodec-hw", [o_input_avcodec_hw_ckb state]);
885         SaveIntList(o_input_skipLoop_pop, "avcodec-skiploopfilter");
886
887         config_PutInt(p_intf, "mkv-preload-local-dir", [o_input_mkv_preload_dir_ckb state]);
888
889         #define CaC(name, factor) config_PutInt(p_intf, name, [[o_input_cachelevel_pop selectedItem] tag] * factor)
890         if ([[o_input_cachelevel_pop selectedItem] tag] == 0) {
891             msg_Dbg(p_intf, "Custom chosen, not adjusting cache values");
892         } else {
893             msg_Dbg(p_intf, "Adjusting all cache values to: %i", (int)[[o_input_cachelevel_pop selectedItem] tag]);
894             CaC("file-caching", 1);
895             CaC("network-caching", 10/3);
896             CaC("disc-caching", 1);
897             CaC("live-caching", 1);
898         }
899         #undef CaC
900         b_inputSettingChanged = NO;
901     }
902
903     /**********************
904      * subtitles settings *
905      **********************/
906     if (b_osdSettingChanged) {
907         config_PutInt(p_intf, "osd", [o_osd_osd_ckb state]);
908
909         if ([o_osd_encoding_pop indexOfSelectedItem] >= 0)
910             SaveStringList(o_osd_encoding_pop, "subsdec-encoding");
911         else
912             config_PutPsz(p_intf, "subsdec-encoding", "");
913
914         config_PutPsz(p_intf, "sub-language", [[o_osd_lang_fld stringValue] UTF8String]);
915
916         config_PutPsz(p_intf, "freetype-font", [[o_osd_font_fld stringValue] UTF8String]);
917         SaveIntList(o_osd_font_color_pop, "freetype-color");
918         SaveIntList(o_osd_font_size_pop, "freetype-rel-fontsize");
919         config_PutInt(p_intf, "freetype-opacity", [o_osd_opacity_fld intValue] * 255.0 / 100.0 + 0.5);
920         config_PutInt(p_intf, "freetype-bold", [o_osd_forcebold_ckb state]);
921         SaveIntList(o_osd_outline_color_pop, "freetype-outline-color");
922         SaveIntList(o_osd_outline_thickness_pop, "freetype-outline-thickness");
923         b_osdSettingChanged = NO;
924     }
925
926     /********************
927      * hotkeys settings *
928      ********************/
929     if (b_hotkeyChanged) {
930         NSUInteger hotKeyCount = [o_hotkeySettings count];
931         for (NSUInteger i = 0; i < hotKeyCount; i++)
932             config_PutPsz(p_intf, [[o_hotkeyNames objectAtIndex:i] UTF8String], [[o_hotkeySettings objectAtIndex:i]UTF8String]);
933         b_hotkeyChanged = NO;
934     }
935
936     [[VLCCoreInteraction sharedInstance] fixPreferences];
937
938     /* okay, let's save our changes to vlcrc */
939     config_SaveConfigFile(p_intf);
940
941     [[NSNotificationCenter defaultCenter] postNotificationName: @"VLCMediaKeySupportSettingChanged"
942                                                             object: nil
943                                                           userInfo: nil];
944 }
945
946 - (void)showSettingsForCategory: (id)o_new_category_view
947 {
948     NSRect o_win_rect, o_view_rect, o_old_view_rect;
949     o_win_rect = [o_sprefs_win frame];
950     o_view_rect = [o_new_category_view frame];
951
952     if (o_currentlyShownCategoryView != nil) {
953         /* restore our window's height, if we've shown another category previously */
954         o_old_view_rect = [o_currentlyShownCategoryView frame];
955         o_win_rect.size.height = o_win_rect.size.height - o_old_view_rect.size.height;
956         o_win_rect.origin.y = (o_win_rect.origin.y + o_old_view_rect.size.height) - o_view_rect.size.height;
957     }
958
959     o_win_rect.size.height = o_win_rect.size.height + o_view_rect.size.height;
960
961     [o_new_category_view setFrame: NSMakeRect(0,
962                                                [o_sprefs_controls_box frame].size.height,
963                                                o_view_rect.size.width,
964                                                o_view_rect.size.height)];
965     [o_new_category_view setAutoresizesSubviews: YES];
966     if (o_currentlyShownCategoryView) {
967         [[[o_sprefs_win contentView] animator] replaceSubview: o_currentlyShownCategoryView with: o_new_category_view];
968         [o_currentlyShownCategoryView release];
969         [[o_sprefs_win animator] setFrame: o_win_rect display:YES];
970     } else {
971         [[o_sprefs_win contentView] addSubview: o_new_category_view];
972         [o_sprefs_win setFrame: o_win_rect display:YES animate:NO];
973     }
974
975     /* keep our current category for further reference */
976     o_currentlyShownCategoryView = o_new_category_view;
977     [o_currentlyShownCategoryView retain];
978 }
979
980 #pragma mark -
981 #pragma mark Specific actions
982
983 - (IBAction)interfaceSettingChanged:(id)sender
984 {
985     if (sender == o_intf_embedded_ckb && [o_intf_embedded_ckb state] == NSOffState)
986         [o_intf_nativefullscreen_ckb setState: NSOffState];
987
988     if (sender == o_intf_nativefullscreen_ckb && [o_intf_nativefullscreen_ckb state] == NSOnState)
989         [o_intf_embedded_ckb setState: NSOnState];
990
991     b_intfSettingChanged = YES;
992 }
993
994 - (void)showInterfaceSettings
995 {
996     [self showSettingsForCategory: o_intf_view];
997 }
998
999 - (IBAction)audioSettingChanged:(id)sender
1000 {
1001     if (sender == o_audio_vol_sld)
1002         [o_audio_vol_fld setIntValue: [o_audio_vol_sld intValue]];
1003
1004     if (sender == o_audio_vol_fld)
1005         [o_audio_vol_sld setIntValue: [o_audio_vol_fld intValue]];
1006
1007     if (sender == o_audio_last_ckb) {
1008         if ([o_audio_last_ckb state] == NSOnState) {
1009             [o_audio_lastpwd_sfld setEnabled: YES];
1010             [o_audio_lastuser_fld setEnabled: YES];
1011         } else {
1012             [o_audio_lastpwd_sfld setEnabled: NO];
1013             [o_audio_lastuser_fld setEnabled: NO];
1014         }
1015     }
1016
1017     if (sender == o_audio_autosavevol_matrix) {
1018         BOOL enableVolumeSlider = [o_audio_autosavevol_matrix selectedTag] == 1;
1019         [o_audio_vol_fld setEnabled: enableVolumeSlider];
1020         [o_audio_vol_sld setEnabled: enableVolumeSlider];
1021     }
1022
1023     b_audioSettingChanged = YES;
1024 }
1025
1026 - (void)showAudioSettings
1027 {
1028     [self showSettingsForCategory: o_audio_view];
1029 }
1030
1031 - (IBAction)videoSettingChanged:(id)sender
1032 {
1033     if (sender == o_video_snap_folder_btn) {
1034         o_selectFolderPanel = [[NSOpenPanel alloc] init];
1035         [o_selectFolderPanel setCanChooseDirectories: YES];
1036         [o_selectFolderPanel setCanChooseFiles: NO];
1037         [o_selectFolderPanel setResolvesAliases: YES];
1038         [o_selectFolderPanel setAllowsMultipleSelection: NO];
1039         [o_selectFolderPanel setMessage: _NS("Choose the folder to save your video snapshots to.")];
1040         [o_selectFolderPanel setCanCreateDirectories: YES];
1041         [o_selectFolderPanel setPrompt: _NS("Choose")];
1042         [o_selectFolderPanel beginSheetModalForWindow: o_sprefs_win completionHandler: ^(NSInteger returnCode) {
1043             if (returnCode == NSOKButton)
1044             {
1045                 [o_video_snap_folder_fld setStringValue: [[o_selectFolderPanel URL] path]];
1046                 b_videoSettingChanged = YES;
1047             }
1048         }];
1049         [o_selectFolderPanel release];
1050     } else
1051         b_videoSettingChanged = YES;
1052 }
1053
1054 - (void)showVideoSettings
1055 {
1056     [self showSettingsForCategory: o_video_view];
1057 }
1058
1059 - (IBAction)osdSettingChanged:(id)sender
1060 {
1061     if (sender == o_osd_opacity_fld)
1062         [o_osd_opacity_sld setIntValue: [o_osd_opacity_fld intValue]];
1063
1064     if (sender == o_osd_opacity_sld)
1065         [o_osd_opacity_fld setIntValue: [o_osd_opacity_sld intValue]];
1066
1067     b_osdSettingChanged = YES;
1068 }
1069
1070 - (void)showOSDSettings
1071 {
1072     [self showSettingsForCategory: o_osd_view];
1073 }
1074
1075 - (void)controlTextDidChange:(NSNotification *)o_notification
1076 {
1077     id notificationObject = [o_notification object];
1078     if (notificationObject == o_audio_lang_fld ||
1079        notificationObject ==  o_audio_lastpwd_sfld ||
1080        notificationObject ==  o_audio_lastuser_fld ||
1081        notificationObject == o_audio_vol_fld)
1082         b_audioSettingChanged = YES;
1083     else if (notificationObject == o_input_record_fld ||
1084              notificationObject == o_input_httpproxy_fld ||
1085             notificationObject == o_input_httpproxypwd_sfld ||
1086             notificationObject == o_input_postproc_fld)
1087         b_inputSettingChanged = YES;
1088     else if (notificationObject == o_osd_font_fld ||
1089             notificationObject == o_osd_lang_fld ||
1090             notificationObject == o_osd_opacity_fld)
1091         b_osdSettingChanged = YES;
1092     else if (notificationObject == o_video_snap_folder_fld ||
1093             notificationObject == o_video_snap_prefix_fld)
1094         b_videoSettingChanged = YES;
1095 }
1096
1097 - (IBAction)showFontPicker:(id)sender
1098 {
1099     char * font = config_GetPsz(p_intf, "freetype-font");
1100     NSString * fontName = font ? [NSString stringWithUTF8String: font] : nil;
1101     free(font);
1102     if (fontName) {
1103         NSFont * font = [NSFont fontWithName:fontName size:0.0];
1104         [[NSFontManager sharedFontManager] setSelectedFont:font isMultiple:NO];
1105     }
1106     [[NSFontManager sharedFontManager] setTarget: self];
1107     [[NSFontPanel sharedFontPanel] orderFront:self];
1108 }
1109
1110 - (void)changeFont:(id)sender
1111 {
1112     NSFont * font = [sender convertFont:[[NSFontManager sharedFontManager] selectedFont]];
1113     [o_osd_font_fld setStringValue:[font fontName]];
1114     [self osdSettingChanged:self];
1115 }
1116
1117 - (IBAction)inputSettingChanged:(id)sender
1118 {
1119     if (sender == o_input_cachelevel_pop) {
1120         if ([[[o_input_cachelevel_pop selectedItem] title] isEqualToString: _NS("Custom")])
1121             [o_input_cachelevel_custom_txt setHidden: NO];
1122         else
1123             [o_input_cachelevel_custom_txt setHidden: YES];
1124     } else if (sender == o_input_record_btn) {
1125         o_selectFolderPanel = [[NSOpenPanel alloc] init];
1126         [o_selectFolderPanel setCanChooseDirectories: YES];
1127         [o_selectFolderPanel setCanChooseFiles: YES];
1128         [o_selectFolderPanel setResolvesAliases: YES];
1129         [o_selectFolderPanel setAllowsMultipleSelection: NO];
1130         [o_selectFolderPanel setMessage: _NS("Choose the directory or filename where the records will be stored.")];
1131         [o_selectFolderPanel setCanCreateDirectories: YES];
1132         [o_selectFolderPanel setPrompt: _NS("Choose")];
1133         [o_selectFolderPanel beginSheetModalForWindow: o_sprefs_win completionHandler: ^(NSInteger returnCode) {
1134             if (returnCode == NSOKButton)
1135             {
1136                 [o_input_record_fld setStringValue: [[o_selectFolderPanel URL] path]];
1137                 b_inputSettingChanged = YES;
1138             }
1139         }];
1140         [o_selectFolderPanel release];
1141
1142         return;
1143     }
1144
1145     b_inputSettingChanged = YES;
1146 }
1147
1148 - (void)showInputSettings
1149 {
1150     [self showSettingsForCategory: o_input_view];
1151 }
1152
1153 #pragma mark -
1154 #pragma mark Hotkey actions
1155
1156 - (void)hotkeyTableDoubleClick:(id)object
1157 {
1158     // -1 is header
1159     if ([o_hotkeys_listbox clickedRow] >= 0)
1160         [self hotkeySettingChanged:o_hotkeys_listbox];
1161 }
1162
1163 - (IBAction)hotkeySettingChanged:(id)sender
1164 {
1165     if (sender == o_hotkeys_change_btn || sender == o_hotkeys_listbox) {
1166         [o_hotkeys_change_lbl setStringValue: [NSString stringWithFormat: _NS("Press new keys for\n\"%@\""),
1167                                                [o_hotkeyDescriptions objectAtIndex: [o_hotkeys_listbox selectedRow]]]];
1168         [o_hotkeys_change_keys_lbl setStringValue: [[VLCStringUtility sharedInstance] OSXStringKeyToString:[o_hotkeySettings objectAtIndex: [o_hotkeys_listbox selectedRow]]]];
1169         [o_hotkeys_change_taken_lbl setStringValue: @""];
1170         [o_hotkeys_change_win setInitialFirstResponder: [o_hotkeys_change_win contentView]];
1171         [o_hotkeys_change_win makeFirstResponder: [o_hotkeys_change_win contentView]];
1172         [NSApp runModalForWindow: o_hotkeys_change_win];
1173     } else if (sender == o_hotkeys_change_cancel_btn) {
1174         [NSApp stopModal];
1175         [o_hotkeys_change_win close];
1176     } else if (sender == o_hotkeys_change_ok_btn) {
1177         NSInteger i_returnValue;
1178         if (! o_keyInTransition) {
1179             [NSApp stopModal];
1180             [o_hotkeys_change_win close];
1181             msg_Err(p_intf, "internal error prevented the hotkey switch");
1182             return;
1183         }
1184
1185         b_hotkeyChanged = YES;
1186
1187         i_returnValue = [o_hotkeySettings indexOfObject: o_keyInTransition];
1188         if (i_returnValue != NSNotFound)
1189             [o_hotkeySettings replaceObjectAtIndex: i_returnValue withObject: [NSString string]];
1190         NSString *tempString;
1191         tempString = [o_keyInTransition stringByReplacingOccurrencesOfString:@"-" withString:@"+"];
1192         i_returnValue = [o_hotkeySettings indexOfObject: tempString];
1193         if (i_returnValue != NSNotFound)
1194             [o_hotkeySettings replaceObjectAtIndex: i_returnValue withObject: [NSString string]];
1195
1196         [o_hotkeySettings replaceObjectAtIndex: [o_hotkeys_listbox selectedRow] withObject: [o_keyInTransition retain]];
1197
1198         [NSApp stopModal];
1199         [o_hotkeys_change_win close];
1200
1201         [o_hotkeys_listbox reloadData];
1202     } else if (sender == o_hotkeys_clear_btn) {
1203         [o_hotkeySettings replaceObjectAtIndex: [o_hotkeys_listbox selectedRow] withObject: [NSString string]];
1204         [o_hotkeys_listbox reloadData];
1205         b_hotkeyChanged = YES;
1206     }
1207
1208     [[NSNotificationCenter defaultCenter] postNotificationName: @"VLCMediaKeySupportSettingChanged"
1209                                                         object: nil
1210                                                       userInfo: nil];
1211 }
1212
1213 - (void)showHotkeySettings
1214 {
1215     [self showSettingsForCategory: o_hotkeys_view];
1216 }
1217
1218 - (int)numberOfRowsInTableView:(NSTableView *)aTableView
1219 {
1220     return [o_hotkeySettings count];
1221 }
1222
1223 - (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
1224 {
1225     NSString * identifier = [aTableColumn identifier];
1226
1227     if ([identifier isEqualToString: @"action"])
1228         return [o_hotkeyDescriptions objectAtIndex: rowIndex];
1229     else if ([identifier isEqualToString: @"shortcut"])
1230         return [[VLCStringUtility sharedInstance] OSXStringKeyToString:[o_hotkeySettings objectAtIndex: rowIndex]];
1231     else {
1232         msg_Err(p_intf, "unknown TableColumn identifier (%s)!", [identifier UTF8String]);
1233         return NULL;
1234     }
1235 }
1236
1237 - (BOOL)changeHotkeyTo: (NSString *)theKey
1238 {
1239     NSInteger i_returnValue, i_returnValue2;
1240     i_returnValue = [o_hotkeysNonUseableKeys indexOfObject: theKey];
1241
1242     if (i_returnValue != NSNotFound || [theKey isEqualToString:@""]) {
1243         [o_hotkeys_change_keys_lbl setStringValue: _NS("Invalid combination")];
1244         [o_hotkeys_change_taken_lbl setStringValue: _NS("Regrettably, these keys cannot be assigned as hotkey shortcuts.")];
1245         [o_hotkeys_change_ok_btn setEnabled: NO];
1246         return NO;
1247     } else {
1248         [o_hotkeys_change_keys_lbl setStringValue: [[VLCStringUtility sharedInstance] OSXStringKeyToString:theKey]];
1249
1250         i_returnValue = [o_hotkeySettings indexOfObject: theKey];
1251         i_returnValue2 = [o_hotkeySettings indexOfObject: [theKey stringByReplacingOccurrencesOfString:@"-" withString:@"+"]];
1252         if (i_returnValue != NSNotFound)
1253             [o_hotkeys_change_taken_lbl setStringValue: [NSString stringWithFormat:
1254                                                          _NS("This combination is already taken by \"%@\"."),
1255                                                          [o_hotkeyDescriptions objectAtIndex: i_returnValue]]];
1256         else if (i_returnValue2 != NSNotFound)
1257             [o_hotkeys_change_taken_lbl setStringValue: [NSString stringWithFormat:
1258                                                          _NS("This combination is already taken by \"%@\"."),
1259                                                          [o_hotkeyDescriptions objectAtIndex: i_returnValue2]]];
1260         else
1261             [o_hotkeys_change_taken_lbl setStringValue: @""];
1262
1263         [o_hotkeys_change_ok_btn setEnabled: YES];
1264         [o_keyInTransition release];
1265         o_keyInTransition = theKey;
1266         [o_keyInTransition retain];
1267         return YES;
1268     }
1269 }
1270
1271 @end
1272
1273 /********************
1274  * hotkeys settings *
1275  ********************/
1276
1277 @implementation VLCHotkeyChangeWindow
1278
1279 - (BOOL)acceptsFirstResponder
1280 {
1281     return YES;
1282 }
1283
1284 - (BOOL)becomeFirstResponder
1285 {
1286     return YES;
1287 }
1288
1289 - (BOOL)resignFirstResponder
1290 {
1291     /* We need to stay the first responder or we'll miss the user's input */
1292     return NO;
1293 }
1294
1295 - (BOOL)performKeyEquivalent:(NSEvent *)o_theEvent
1296 {
1297     NSMutableString *tempString = [[[NSMutableString alloc] init] autorelease];
1298     NSString *keyString = [o_theEvent characters];
1299
1300     unichar key = [keyString characterAtIndex:0];
1301     NSUInteger i_modifiers = [o_theEvent modifierFlags];
1302
1303     /* modifiers */
1304     if (i_modifiers & NSControlKeyMask)
1305         [tempString appendString:@"Ctrl-"];
1306     if (i_modifiers & NSAlternateKeyMask )
1307         [tempString appendString:@"Alt-"];
1308     if (i_modifiers & NSShiftKeyMask)
1309         [tempString appendString:@"Shift-"];
1310     if (i_modifiers & NSCommandKeyMask)
1311         [tempString appendString:@"Command-"];
1312
1313     /* non character keys */
1314     if (key == NSUpArrowFunctionKey)
1315         [tempString appendString:@"Up"];
1316     else if (key == NSDownArrowFunctionKey)
1317         [tempString appendString:@"Down"];
1318     else if (key == NSLeftArrowFunctionKey)
1319         [tempString appendString:@"Left"];
1320     else if (key == NSRightArrowFunctionKey)
1321         [tempString appendString:@"Right"];
1322     else if (key == NSF1FunctionKey)
1323         [tempString appendString:@"F1"];
1324     else if (key == NSF2FunctionKey)
1325         [tempString appendString:@"F2"];
1326     else if (key == NSF3FunctionKey)
1327         [tempString appendString:@"F3"];
1328     else if (key == NSF4FunctionKey)
1329         [tempString appendString:@"F4"];
1330     else if (key == NSF5FunctionKey)
1331         [tempString appendString:@"F5"];
1332     else if (key == NSF6FunctionKey)
1333         [tempString appendString:@"F6"];
1334     else if (key == NSF7FunctionKey)
1335         [tempString appendString:@"F7"];
1336     else if (key == NSF8FunctionKey)
1337         [tempString appendString:@"F8"];
1338     else if (key == NSF9FunctionKey)
1339         [tempString appendString:@"F9"];
1340     else if (key == NSF10FunctionKey)
1341         [tempString appendString:@"F10"];
1342     else if (key == NSF11FunctionKey)
1343         [tempString appendString:@"F11"];
1344     else if (key == NSF12FunctionKey)
1345         [tempString appendString:@"F12"];
1346     else if (key == NSInsertFunctionKey)
1347         [tempString appendString:@"Insert"];
1348     else if (key == NSHomeFunctionKey)
1349         [tempString appendString:@"Home"];
1350     else if (key == NSEndFunctionKey)
1351         [tempString appendString:@"End"];
1352     else if (key == NSPageUpFunctionKey)
1353         [tempString appendString:@"Pageup"];
1354     else if (key == NSPageDownFunctionKey)
1355         [tempString appendString:@"Pagedown"];
1356     else if (key == NSMenuFunctionKey)
1357         [tempString appendString:@"Menu"];
1358     else if (key == NSTabCharacter)
1359         [tempString appendString:@"Tab"];
1360     else if (key == NSCarriageReturnCharacter)
1361         [tempString appendString:@"Enter"];
1362     else if (key == NSEnterCharacter)
1363         [tempString appendString:@"Enter"];
1364     else if (key == NSDeleteCharacter)
1365         [tempString appendString:@"Delete"];
1366     else if (key == NSBackspaceCharacter)
1367         [tempString appendString:@"Backspace"];
1368     else if (![[[o_theEvent charactersIgnoringModifiers] lowercaseString] isEqualToString:@""]) //plain characters
1369         [tempString appendString:[[o_theEvent charactersIgnoringModifiers] lowercaseString]];
1370     else
1371         return NO;
1372
1373     return [[[VLCMain sharedInstance] simplePreferences] changeHotkeyTo: tempString];
1374 }
1375
1376 @end
1377
1378 @implementation VLCSimplePrefsWindow
1379
1380 - (BOOL)acceptsFirstResponder
1381 {
1382     return YES;
1383 }
1384
1385 - (void)changeFont:(id)sender
1386 {
1387     [[[VLCMain sharedInstance] simplePreferences] changeFont: sender];
1388 }
1389 @end