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