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