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