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