1 /*****************************************************************************
2 * simple_prefs.m: Simple Preferences for Mac OS X
3 *****************************************************************************
4 * Copyright (C) 2008 the VideoLAN team
7 * Authors: Felix Paul Kühne <fkuehne at videolan dot org>
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.
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.
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 *****************************************************************************/
24 #import "simple_prefs.h"
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";
31 @implementation VLCSimplePrefs
33 static VLCSimplePrefs *_o_sharedInstance = nil;
35 + (VLCSimplePrefs *)sharedInstance
37 return _o_sharedInstance ? _o_sharedInstance : [[self alloc] init];
42 if (_o_sharedInstance) {
46 _o_sharedInstance = [super init];
49 return _o_sharedInstance;
54 [o_currentlyShownCategoryView release];
55 [o_sprefs_toolbar release];
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];
76 - (NSToolbarItem *) toolbar: (NSToolbar *)o_sprefs_toolbar
77 itemForItemIdentifier: (NSString *)o_itemIdent
78 willBeInsertedIntoToolbar: (BOOL)b_willBeInserted
80 NSToolbarItem *o_toolbarItem = nil;
82 if( [o_itemIdent isEqual: VLCIntfSettingToolbarIdentifier] )
84 o_toolbarItem = [[[NSToolbarItem alloc] initWithItemIdentifier: o_itemIdent] autorelease];
86 [o_toolbarItem setLabel: _NS("Interface")];
87 [o_toolbarItem setPaletteLabel: _NS("Interface settings")];
89 [o_toolbarItem setToolTip: _NS("Interface settings")];
90 [o_toolbarItem setImage: [NSImage imageNamed: @"spref_cone_Interface_64"]];
92 [o_toolbarItem setTarget: self];
93 [o_toolbarItem setAction: @selector(showInterfaceSettings)];
95 [o_toolbarItem setEnabled: YES];
96 [o_toolbarItem setAutovalidates: YES];
98 else if( [o_itemIdent isEqual: VLCAudioSettingToolbarIdentifier] )
100 o_toolbarItem = [[[NSToolbarItem alloc] initWithItemIdentifier: o_itemIdent] autorelease];
102 [o_toolbarItem setLabel: _NS("Audio")];
103 [o_toolbarItem setPaletteLabel: _NS("General Audio settings")];
105 [o_toolbarItem setToolTip: _NS("General Audio settings")];
106 [o_toolbarItem setImage: [NSImage imageNamed: @"spref_cone_Audio_64"]];
108 [o_toolbarItem setTarget: self];
109 [o_toolbarItem setAction: @selector(showAudioSettings)];
111 [o_toolbarItem setEnabled: YES];
112 [o_toolbarItem setAutovalidates: YES];
115 return o_toolbarItem;
118 - (NSArray *)toolbarDefaultItemIdentifiers: (NSToolbar *)toolbar
120 return [NSArray arrayWithObjects: VLCIntfSettingToolbarIdentifier, VLCAudioSettingToolbarIdentifier, NSToolbarFlexibleSpaceItemIdentifier, nil];
123 - (NSArray *)toolbarAllowedItemIdentifiers: (NSToolbar *)toolbar
125 return [NSArray arrayWithObjects: VLCIntfSettingToolbarIdentifier, VLCAudioSettingToolbarIdentifier, NSToolbarFlexibleSpaceItemIdentifier, nil];
128 - (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar
130 return [NSArray arrayWithObjects: VLCIntfSettingToolbarIdentifier, VLCAudioSettingToolbarIdentifier, nil];
135 [o_sprefs_reset_btn setEnabled: NO];
136 msg_Warn( p_intf, "localisation of the simple preferences not implemented!" );
139 - (void)resetControls
141 module_config_t *p_item;
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++ )
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] ) )
156 [o_intf_lang_pop selectItemAtIndex: y];
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];
165 [o_intf_meta_ckb setState: config_GetInt( p_intf, "fetch-meta" )];
166 [o_intf_fspanel_ckb setState: config_GetInt( p_intf, "macosx-fspanel" )];
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" )];
176 [o_audio_spdif_ckb setState: config_GetInt( p_intf, "spdif" )];
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];
185 [o_audio_lang_fld setStringValue: [NSString stringWithUTF8String: config_GetPsz( p_intf, "audio-language" )]];
187 [o_audio_headphone_ckb setState: config_GetInt( p_intf, "headphone-dolby" )];
189 psz_tmp = config_GetPsz( p_intf, "audio-filter" );
191 [o_audio_norm_ckb setState: (int)strstr( psz_tmp, "normvol" )];
192 [o_audio_norm_fld setFloatValue: config_GetFloat( p_intf, "norm-max-level" )];
195 msg_Warn( p_intf, "visualizer not implemented!" );
197 /* Last.FM is optional */
198 if( module_Exists( p_intf, "audioscrobbler" ) )
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" )]];
203 if( config_ExistIntf( VLC_OBJECT( p_intf ), "audioscrobbler" ) )
204 [o_audio_last_ckb setState: NSOnState];
206 [o_audio_last_ckb setState: NSOffState];
217 /*********************
218 * subtitle settings *
219 *********************/
221 /********************
223 ********************/
226 - (void)showSimplePrefs
228 /* we want to show the interface settings, if no category was chosen */
229 if( [o_sprefs_toolbar selectedItemIdentifier] == nil )
231 [o_sprefs_toolbar setSelectedItemIdentifier: VLCIntfSettingToolbarIdentifier];
232 [self showInterfaceSettings];
235 [o_sprefs_win makeKeyAndOrderFront: self];
238 - (IBAction)buttonAction:(id)sender
240 if( sender == o_sprefs_cancel_btn )
241 [o_sprefs_win orderOut: sender];
242 else if( sender == o_sprefs_save_btn )
244 [self saveChangedSettings];
245 [o_sprefs_win orderOut: sender];
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 )
255 [o_sprefs_win orderOut: self];
256 [[[VLCMain sharedInstance] getPreferences] showPrefs];
257 [self resetControls];
260 msg_Err( p_intf, "unknown buttonAction sender" );
263 - (void)sheetDidEnd:(NSWindow *)o_sheet
264 returnCode:(int)i_return
265 contextInfo:(void *)o_context
267 if( i_return == NSAlertAlternateReturn )
269 config_ResetAll( p_intf );
270 [self resetControls];
274 - (void)saveChangedSettings
276 module_config_t *p_item;
280 /**********************
281 * interface settings *
282 **********************/
283 if( b_intfSettingChanged )
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]] ) );
289 config_PutPsz( p_intf, "language", strdup( [[VLCMain sharedInstance] delocalizeString: [o_intf_lang_pop stringValue]] ) );
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]] );
295 config_PutInt( p_intf, "album-art", [o_intf_art_pop intValue] );
297 config_PutInt( p_intf, "fetch-meta", [o_intf_meta_ckb state] );
298 config_PutInt( p_intf, "macosx-fspanel", [o_intf_fspanel_ckb state] );
300 /* okay, let's save our changes to vlcrc */
301 i = config_SaveConfigFile( p_intf, "main" );
302 i = config_SaveConfigFile( p_intf, "macosx" );
305 msg_Err( p_intf, "An error occured while saving the Audio settings using SimplePrefs" );
307 b_intfSettingChanged = NO;
313 if( b_audioSettingChanged )
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] );
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]] );
323 config_PutInt( p_intf, "force-dolby-surround", [o_audio_dolby_pop intValue] );
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] );
328 psz_tmp = config_GetPsz( p_intf, "audio-filter" );
330 config_PutPsz( p_intf, "audio-filter", "volnorm" );
331 else if( (int)strstr( psz_tmp, "normvol" ) == NO )
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 );
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 );
344 config_PutFloat( p_intf, "norm-max-level", [o_audio_norm_fld floatValue] );
346 msg_Warn( p_intf, "visualizer not implemented!" );
348 if( [o_audio_last_ckb state] == NSOnState )
349 config_AddIntf( VLC_OBJECT( p_intf ), "audioscrobbler" );
351 config_RemoveIntf( VLC_OBJECT( p_intf ), "audioscrobbler" );
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] );
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" );
362 msg_Err( p_intf, "An error occured while saving the Audio settings using SimplePrefs" );
363 b_audioSettingChanged = NO;
367 - (void)showSettingsForCategory: (id)o_new_category_view
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];
374 if( o_currentlyShownCategoryView != nil )
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;
380 /* remove our previous category view */
381 [o_currentlyShownCategoryView removeFromSuperviewWithoutNeedingDisplay];
384 o_win_rect.size.height = o_win_rect.size.height + o_view_rect.size.height;
386 [o_sprefs_win displayIfNeeded];
387 [o_sprefs_win setFrame: o_win_rect display:YES animate: YES];
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];
397 /* keep our current category for further reference */
398 [o_currentlyShownCategoryView release];
399 o_currentlyShownCategoryView = o_new_category_view;
400 [o_currentlyShownCategoryView retain];
403 - (IBAction)interfaceSettingChanged:(id)sender
405 b_intfSettingChanged = YES;
408 - (void)showInterfaceSettings
410 msg_Dbg( p_intf, "showing interface settings" );
411 [self showSettingsForCategory: o_intf_view];
414 - (IBAction)audioSettingChanged:(id)sender
416 if( sender == o_audio_vol_sld )
417 [o_audio_vol_fld setIntValue: [o_audio_vol_sld intValue]];
419 if( sender == o_audio_vol_fld )
420 [o_audio_vol_sld setIntValue: [o_audio_vol_fld intValue]];
422 b_audioSettingChanged = YES;
425 - (void)showAudioSettings
427 msg_Dbg( p_intf, "showing audio settings" );
428 [self showSettingsForCategory: o_audio_view];