]> git.sesse.net Git - vlc/blob - modules/gui/macosx/simple_prefs.m
macosx: fully implemented the interface and audio categories (excepting the visualise...
[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
27 static NSString* VLCSPrefsToolbarIdentifier = @"Our Simple Preferences Toolbar Identifier";
28 static NSString* VLCIntfSettingToolbarIdentifier = @"Intf Settings Item Identifier";
29 static NSString* VLCAudioSettingToolbarIdentifier = @"Audio Settings Item Identifier";
30
31 @implementation VLCSimplePrefs
32
33 static VLCSimplePrefs *_o_sharedInstance = nil;
34
35 + (VLCSimplePrefs *)sharedInstance
36 {
37     return _o_sharedInstance ? _o_sharedInstance : [[self alloc] init];
38 }
39
40 - (id)init
41 {
42     if (_o_sharedInstance) {
43         [self dealloc];
44     } else {
45         p_intf = VLCIntf;
46         _o_sharedInstance = [super init];
47     }
48     
49     return _o_sharedInstance;
50 }
51
52 - (void)dealloc
53 {
54     [o_currentlyShownCategoryView release];
55     [o_sprefs_toolbar release];
56     
57     [super dealloc];
58 }
59
60 - (void)awakeFromNib
61 {
62     [self initStrings];
63
64     [self resetControls];
65     
66     /* setup the toolbar */
67     o_sprefs_toolbar = [[[NSToolbar alloc] initWithIdentifier: VLCSPrefsToolbarIdentifier] autorelease];
68     [o_sprefs_toolbar setAllowsUserCustomization: NO];
69     [o_sprefs_toolbar setAutosavesConfiguration: NO];
70     [o_sprefs_toolbar setDisplayMode: NSToolbarDisplayModeIconAndLabel];
71     [o_sprefs_toolbar setSizeMode: NSToolbarSizeModeRegular];
72     [o_sprefs_toolbar setDelegate: self];
73     [o_sprefs_win setToolbar: o_sprefs_toolbar];
74 }
75
76 - (NSToolbarItem *) toolbar: (NSToolbar *)o_sprefs_toolbar 
77       itemForItemIdentifier: (NSString *)o_itemIdent 
78   willBeInsertedIntoToolbar: (BOOL)b_willBeInserted
79 {
80     NSToolbarItem *o_toolbarItem = nil;
81     
82     if( [o_itemIdent isEqual: VLCIntfSettingToolbarIdentifier] )
83     {
84         o_toolbarItem = [[[NSToolbarItem alloc] initWithItemIdentifier: o_itemIdent] autorelease];
85
86         [o_toolbarItem setLabel: _NS("Interface")];
87         [o_toolbarItem setPaletteLabel: _NS("Interface settings")];
88
89         [o_toolbarItem setToolTip: _NS("Interface settings")];
90         [o_toolbarItem setImage: [NSImage imageNamed: @"spref_cone_Interface_64"]];
91
92         [o_toolbarItem setTarget: self];
93         [o_toolbarItem setAction: @selector(showInterfaceSettings)];
94
95         [o_toolbarItem setEnabled: YES];
96         [o_toolbarItem setAutovalidates: YES];
97     }
98     else if( [o_itemIdent isEqual: VLCAudioSettingToolbarIdentifier] )
99     {
100         o_toolbarItem = [[[NSToolbarItem alloc] initWithItemIdentifier: o_itemIdent] autorelease];
101
102         [o_toolbarItem setLabel: _NS("Audio")];
103         [o_toolbarItem setPaletteLabel: _NS("General Audio settings")];
104
105         [o_toolbarItem setToolTip: _NS("General Audio settings")];
106         [o_toolbarItem setImage: [NSImage imageNamed: @"spref_cone_Audio_64"]];
107
108         [o_toolbarItem setTarget: self];
109         [o_toolbarItem setAction: @selector(showAudioSettings)];
110
111         [o_toolbarItem setEnabled: YES];
112         [o_toolbarItem setAutovalidates: YES];
113     }
114     
115     return o_toolbarItem;
116 }
117
118 - (NSArray *)toolbarDefaultItemIdentifiers: (NSToolbar *)toolbar
119 {
120     return [NSArray arrayWithObjects: VLCIntfSettingToolbarIdentifier, VLCAudioSettingToolbarIdentifier, NSToolbarFlexibleSpaceItemIdentifier, nil];
121 }
122
123 - (NSArray *)toolbarAllowedItemIdentifiers: (NSToolbar *)toolbar
124 {
125     return [NSArray arrayWithObjects: VLCIntfSettingToolbarIdentifier, VLCAudioSettingToolbarIdentifier, NSToolbarFlexibleSpaceItemIdentifier, nil];
126 }
127
128 - (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar
129 {
130     return [NSArray arrayWithObjects: VLCIntfSettingToolbarIdentifier, VLCAudioSettingToolbarIdentifier, nil];
131 }
132
133 - (void)initStrings
134 {
135     [o_sprefs_reset_btn setEnabled: NO];
136     msg_Warn( p_intf, "localisation of the simple preferences not implemented!" );
137 }
138
139 - (void)resetControls
140 {
141     module_config_t *p_item;
142     int i, y;
143     char *psz_tmp;
144
145     /**********************
146      * interface settings *
147      **********************/
148     [o_intf_lang_pop removeAllItems];
149     p_item = config_FindConfig( VLC_OBJECT(p_intf), "language" );
150     for( i = 0; p_item->ppsz_list[i] != nil; i++ )
151     {
152         [o_intf_lang_pop addItemWithTitle: _NS( p_item->ppsz_list_text[i] )];
153         if( p_item->value.psz && !strcmp( p_item->value.psz, p_item->ppsz_list[i] ) )
154             y = i;
155     }
156     [o_intf_lang_pop selectItemAtIndex: y];
157
158     [o_intf_art_pop removeAllItems];
159     p_item = config_FindConfig( VLC_OBJECT(p_intf), "album-art" );
160     for( i = 0; i < p_item->i_list; i++ )
161         [o_intf_art_pop addItemWithTitle: _NS( p_item->ppsz_list_text[i] )];
162     [o_intf_art_pop selectItemAtIndex: 0];
163     [o_intf_art_pop selectItemAtIndex: p_item->value.i];
164
165     [o_intf_meta_ckb setState: config_GetInt( p_intf, "fetch-meta" )];
166     [o_intf_fspanel_ckb setState: config_GetInt( p_intf, "macosx-fspanel" )];
167
168
169     /******************
170      * audio settings *
171      ******************/
172     [o_audio_enable_ckb setState: config_GetInt( p_intf, "audio" )];
173     [o_audio_vol_fld setIntValue: config_GetInt( p_intf, "volume" )];
174     [o_audio_vol_sld setIntValue: config_GetInt( p_intf, "volume" )];
175
176     [o_audio_spdif_ckb setState: config_GetInt( p_intf, "spdif" )];
177
178     [o_audio_dolby_pop removeAllItems];
179     p_item = config_FindConfig( VLC_OBJECT(p_intf), "force-dolby-surround" );
180     for( i = 0; i < p_item->i_list; i++ )
181         [o_audio_dolby_pop addItemWithTitle: _NS( p_item->ppsz_list_text[i] )];
182     [o_audio_dolby_pop selectItemAtIndex: 0];
183     [o_audio_dolby_pop selectItemAtIndex: p_item->value.i];
184     
185     [o_audio_lang_fld setStringValue: [NSString stringWithUTF8String: config_GetPsz( p_intf, "audio-language" )]];
186
187     [o_audio_headphone_ckb setState: config_GetInt( p_intf, "headphone-dolby" )];
188     
189     psz_tmp = config_GetPsz( p_intf, "audio-filter" );
190     if( psz_tmp )
191         [o_audio_norm_ckb setState: (int)strstr( psz_tmp, "normvol" )];
192     [o_audio_norm_fld setFloatValue: config_GetFloat( p_intf, "norm-max-level" )];
193     
194     // visualizer
195     msg_Warn( p_intf, "visualizer not implemented!" );
196     
197     /* Last.FM is optional */
198     if( module_Exists( p_intf, "audioscrobbler" ) )
199     {
200         [o_audio_lastuser_fld setStringValue: [NSString stringWithUTF8String: config_GetPsz( p_intf, "lastfm-username" )]];
201         [o_audio_lastpwd_fld setStringValue: [NSString stringWithUTF8String: config_GetPsz( p_intf, "lastfm-password" )]];
202         
203         if( config_ExistIntf( VLC_OBJECT( p_intf ), "audioscrobbler" ) )
204             [o_audio_last_ckb setState: NSOnState];
205         else
206             [o_audio_last_ckb setState: NSOffState];
207     }
208
209     /******************
210      * video settings *
211      ******************/
212     
213     /*******************
214      * codecs settings *
215      *******************/
216
217     /*********************
218      * subtitle settings *
219      *********************/
220     
221     /********************
222      * hotkeys settings *
223      ********************/
224 }
225
226 - (void)showSimplePrefs
227 {
228     /* we want to show the interface settings, if no category was chosen */
229     if( [o_sprefs_toolbar selectedItemIdentifier] == nil )
230     {
231         [o_sprefs_toolbar setSelectedItemIdentifier: VLCIntfSettingToolbarIdentifier];
232         [self showInterfaceSettings];
233     }
234
235     [o_sprefs_win makeKeyAndOrderFront: self];
236 }
237
238 - (IBAction)buttonAction:(id)sender
239 {
240     if( sender == o_sprefs_cancel_btn )
241         [o_sprefs_win orderOut: sender];
242     else if( sender == o_sprefs_save_btn )
243     {
244         [self saveChangedSettings];
245         [o_sprefs_win orderOut: sender];
246     }
247     else if( sender == o_sprefs_reset_btn )
248         NSBeginInformationalAlertSheet( _NS("Reset Preferences"), _NS("Cancel"),
249                                         _NS("Continue"), nil, o_sprefs_win, self,
250                                         @selector(sheetDidEnd: returnCode: contextInfo:), NULL, nil,
251                                         _NS("Beware this will reset the VLC media player preferences.\n"
252                                             "Are you sure you want to continue?") );
253     else if( sender == o_sprefs_basicFull_matrix )
254     {
255         [o_sprefs_win orderOut: self];
256         [[[VLCMain sharedInstance] getPreferences] showPrefs];
257         [self resetControls];
258     }
259     else
260         msg_Err( p_intf, "unknown buttonAction sender" );
261 }
262
263 - (void)sheetDidEnd:(NSWindow *)o_sheet 
264          returnCode:(int)i_return
265         contextInfo:(void *)o_context
266 {
267     if( i_return == NSAlertAlternateReturn )
268     {
269         config_ResetAll( p_intf );
270         [self resetControls];
271     }
272 }
273
274 - (void)saveChangedSettings
275 {
276     module_config_t *p_item;
277     char *psz_tmp;
278     int i;
279     
280     /**********************
281      * interface settings *
282      **********************/
283     if( b_intfSettingChanged )
284     {
285         p_item = config_FindConfig( VLC_OBJECT(p_intf), "language" );
286         if( [o_intf_lang_pop indexOfSelectedItem] >= 0 )
287             config_PutPsz( p_intf, "language", strdup( p_item->ppsz_list[[o_intf_lang_pop indexOfSelectedItem]] ) );
288         else
289             config_PutPsz( p_intf, "language", strdup( [[VLCMain sharedInstance] delocalizeString: [o_intf_lang_pop stringValue]] ) );
290
291         p_item = config_FindConfig( VLC_OBJECT(p_intf), "album-art" );
292         if( [o_intf_art_pop indexOfSelectedItem] >= 0 )
293             config_PutInt( p_intf, "album-art", p_item->pi_list[[o_intf_art_pop indexOfSelectedItem]] );
294         else
295             config_PutInt( p_intf, "album-art", [o_intf_art_pop intValue] );
296
297         config_PutInt( p_intf, "fetch-meta", [o_intf_meta_ckb state] );
298         config_PutInt( p_intf, "macosx-fspanel", [o_intf_fspanel_ckb state] );
299
300         /* okay, let's save our changes to vlcrc */
301         i = config_SaveConfigFile( p_intf, "main" );
302         i = config_SaveConfigFile( p_intf, "macosx" );
303
304         if( i != 0 )
305             msg_Err( p_intf, "An error occured while saving the Audio settings using SimplePrefs" );
306
307         b_intfSettingChanged = NO;
308     }
309     
310     /******************
311      * audio settings *
312      ******************/
313     if( b_audioSettingChanged )
314     {
315         config_PutInt( p_intf, "audio", [o_audio_enable_ckb state] );
316         config_PutInt( p_intf, "volume", [o_audio_vol_sld intValue] );
317         config_PutInt( p_intf, "spdif", [o_audio_spdif_ckb state] );
318
319         p_item = config_FindConfig( VLC_OBJECT(p_intf), "force-dolby-surround" );
320         if( [o_audio_dolby_pop indexOfSelectedItem] >= 0 )
321             config_PutInt( p_intf, "force-dolby-surround", p_item->pi_list[[o_audio_dolby_pop indexOfSelectedItem]] );
322         else
323             config_PutInt( p_intf, "force-dolby-surround", [o_audio_dolby_pop intValue] );
324
325         config_PutPsz( p_intf, "audio-language", [[o_audio_lang_fld stringValue] UTF8String] );
326         config_PutInt( p_intf, "headphone-dolby", [o_audio_headphone_ckb state] );
327
328         psz_tmp = config_GetPsz( p_intf, "audio-filter" );
329         if(! psz_tmp)
330             config_PutPsz( p_intf, "audio-filter", "volnorm" );
331         else if( (int)strstr( psz_tmp, "normvol" ) == NO )
332         {
333             /* work-around a GCC 4.0.1 bug */
334             psz_tmp = (char *)[[NSString stringWithFormat: @"%s:volnorm", psz_tmp] UTF8String];
335             config_PutPsz( p_intf, "audio-filter", psz_tmp );
336         }
337         else
338         {
339             psz_tmp = (char *)[[[NSString stringWithUTF8String: psz_tmp] stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@":volnorm"]] UTF8String];
340             psz_tmp = (char *)[[[NSString stringWithUTF8String: psz_tmp] stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@"volnorm:"]] UTF8String];
341             psz_tmp = (char *)[[[NSString stringWithUTF8String: psz_tmp] stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@"volnorm"]] UTF8String];
342             config_PutPsz( p_intf, "audio-filter", psz_tmp );
343         }
344         config_PutFloat( p_intf, "norm-max-level", [o_audio_norm_fld floatValue] );
345
346         msg_Warn( p_intf, "visualizer not implemented!" );
347
348         if( [o_audio_last_ckb state] == NSOnState )
349             config_AddIntf( VLC_OBJECT( p_intf ), "audioscrobbler" );
350         else
351             config_RemoveIntf( VLC_OBJECT( p_intf ), "audioscrobbler" );
352
353         config_PutPsz( p_intf, "lastfm-username", [[o_audio_lastuser_fld stringValue] UTF8String] );
354         config_PutPsz( p_intf, "lastfm-password", [[o_audio_lastuser_fld stringValue] UTF8String] );
355
356         /* okay, let's save our changes to vlcrc */
357         i = config_SaveConfigFile( p_intf, "main" );
358         i = i + config_SaveConfigFile( p_intf, "audioscrobbler" );
359         i = i + config_SaveConfigFile( p_intf, "volnorm" );
360
361         if( i != 0 )
362             msg_Err( p_intf, "An error occured while saving the Audio settings using SimplePrefs" );
363         b_audioSettingChanged = NO;
364     }
365 }
366
367 - (void)showSettingsForCategory: (id)o_new_category_view
368 {
369     msg_Dbg( p_intf, "switching to another category" );
370     NSRect o_win_rect, o_view_rect, o_old_view_rect;
371     o_win_rect = [o_sprefs_win frame];
372     o_view_rect = [o_new_category_view frame];
373     
374     if( o_currentlyShownCategoryView != nil )
375     {
376         /* restore our window's height, if we've shown another category previously */
377         o_old_view_rect = [o_currentlyShownCategoryView frame];
378         o_win_rect.size.height = o_win_rect.size.height - o_old_view_rect.size.height;
379         
380         /* remove our previous category view */
381         [o_currentlyShownCategoryView removeFromSuperviewWithoutNeedingDisplay];
382     }
383     
384     o_win_rect.size.height = o_win_rect.size.height + o_view_rect.size.height;
385     
386     [o_sprefs_win displayIfNeeded];
387     [o_sprefs_win setFrame: o_win_rect display:YES animate: YES];
388     
389     [o_new_category_view setFrame: NSMakeRect( 0, 
390                                                [o_sprefs_controls_box frame].size.height, 
391                                                o_view_rect.size.width, 
392                                                o_view_rect.size.height )];
393     [o_new_category_view setNeedsDisplay: YES];
394     [o_new_category_view setAutoresizesSubviews: YES];
395     [[o_sprefs_win contentView] addSubview: o_new_category_view];
396     
397     /* keep our current category for further reference */
398     [o_currentlyShownCategoryView release];
399     o_currentlyShownCategoryView = o_new_category_view;
400     [o_currentlyShownCategoryView retain];
401 }
402
403 - (IBAction)interfaceSettingChanged:(id)sender
404 {
405     b_intfSettingChanged = YES;
406 }
407
408 - (void)showInterfaceSettings
409 {
410     msg_Dbg( p_intf, "showing interface settings" );
411     [self showSettingsForCategory: o_intf_view];
412 }
413
414 - (IBAction)audioSettingChanged:(id)sender
415 {
416     if( sender == o_audio_vol_sld )
417         [o_audio_vol_fld setIntValue: [o_audio_vol_sld intValue]];
418     
419     if( sender == o_audio_vol_fld )
420         [o_audio_vol_sld setIntValue: [o_audio_vol_fld intValue]];
421     
422     b_audioSettingChanged = YES;
423 }
424
425 - (void)showAudioSettings
426 {
427     msg_Dbg( p_intf, "showing audio settings" );
428     [self showSettingsForCategory: o_audio_view];
429 }
430 @end