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