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