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";
30 static NSString* VLCVideoSettingToolbarIdentifier = @"Video Settings Item Identifier";
32 @implementation VLCSimplePrefs
34 static VLCSimplePrefs *_o_sharedInstance = nil;
36 + (VLCSimplePrefs *)sharedInstance
38 return _o_sharedInstance ? _o_sharedInstance : [[self alloc] init];
43 if (_o_sharedInstance) {
47 _o_sharedInstance = [super init];
50 return _o_sharedInstance;
55 [o_currentlyShownCategoryView release];
56 [o_sprefs_toolbar release];
67 /* setup the toolbar */
68 o_sprefs_toolbar = [[[NSToolbar alloc] initWithIdentifier: VLCSPrefsToolbarIdentifier] autorelease];
69 [o_sprefs_toolbar setAllowsUserCustomization: NO];
70 [o_sprefs_toolbar setAutosavesConfiguration: NO];
71 [o_sprefs_toolbar setDisplayMode: NSToolbarDisplayModeIconAndLabel];
72 [o_sprefs_toolbar setSizeMode: NSToolbarSizeModeRegular];
73 [o_sprefs_toolbar setDelegate: self];
74 [o_sprefs_win setToolbar: o_sprefs_toolbar];
77 - (NSToolbarItem *) toolbar: (NSToolbar *)o_sprefs_toolbar
78 itemForItemIdentifier: (NSString *)o_itemIdent
79 willBeInsertedIntoToolbar: (BOOL)b_willBeInserted
81 NSToolbarItem *o_toolbarItem = nil;
83 if( [o_itemIdent isEqual: VLCIntfSettingToolbarIdentifier] )
85 o_toolbarItem = [[[NSToolbarItem alloc] initWithItemIdentifier: o_itemIdent] autorelease];
87 [o_toolbarItem setLabel: _NS("Interface")];
88 [o_toolbarItem setPaletteLabel: _NS("Interface settings")];
90 [o_toolbarItem setToolTip: _NS("Interface settings")];
91 [o_toolbarItem setImage: [NSImage imageNamed: @"spref_cone_Interface_64"]];
93 [o_toolbarItem setTarget: self];
94 [o_toolbarItem setAction: @selector(showInterfaceSettings)];
96 [o_toolbarItem setEnabled: YES];
97 [o_toolbarItem setAutovalidates: YES];
99 else if( [o_itemIdent isEqual: VLCAudioSettingToolbarIdentifier] )
101 o_toolbarItem = [[[NSToolbarItem alloc] initWithItemIdentifier: o_itemIdent] autorelease];
103 [o_toolbarItem setLabel: _NS("Audio")];
104 [o_toolbarItem setPaletteLabel: _NS("General Audio settings")];
106 [o_toolbarItem setToolTip: _NS("General Audio settings")];
107 [o_toolbarItem setImage: [NSImage imageNamed: @"spref_cone_Audio_64"]];
109 [o_toolbarItem setTarget: self];
110 [o_toolbarItem setAction: @selector(showAudioSettings)];
112 [o_toolbarItem setEnabled: YES];
113 [o_toolbarItem setAutovalidates: YES];
115 else if( [o_itemIdent isEqual: VLCVideoSettingToolbarIdentifier] )
117 o_toolbarItem = [[[NSToolbarItem alloc] initWithItemIdentifier: o_itemIdent] autorelease];
119 [o_toolbarItem setLabel: _NS("Video")];
120 [o_toolbarItem setPaletteLabel: _NS("General Video settings")];
122 [o_toolbarItem setToolTip: _NS("General Video settings")];
123 [o_toolbarItem setImage: [NSImage imageNamed: @"spref_cone_Video_64"]];
125 [o_toolbarItem setTarget: self];
126 [o_toolbarItem setAction: @selector(showVideoSettings)];
128 [o_toolbarItem setEnabled: YES];
129 [o_toolbarItem setAutovalidates: YES];
132 return o_toolbarItem;
135 - (NSArray *)toolbarDefaultItemIdentifiers: (NSToolbar *)toolbar
137 return [NSArray arrayWithObjects: VLCIntfSettingToolbarIdentifier, VLCAudioSettingToolbarIdentifier, VLCVideoSettingToolbarIdentifier, NSToolbarFlexibleSpaceItemIdentifier, nil];
140 - (NSArray *)toolbarAllowedItemIdentifiers: (NSToolbar *)toolbar
142 return [NSArray arrayWithObjects: VLCIntfSettingToolbarIdentifier, VLCAudioSettingToolbarIdentifier, VLCVideoSettingToolbarIdentifier, NSToolbarFlexibleSpaceItemIdentifier, nil];
145 - (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar
147 return [NSArray arrayWithObjects: VLCIntfSettingToolbarIdentifier, VLCAudioSettingToolbarIdentifier, VLCVideoSettingToolbarIdentifier, nil];
152 msg_Warn( p_intf, "localisation of the simple preferences not implemented!" );
155 - (void)resetControls
157 module_config_t *p_item;
161 /**********************
162 * interface settings *
163 **********************/
164 [o_intf_lang_pop removeAllItems];
165 p_item = config_FindConfig( VLC_OBJECT(p_intf), "language" );
166 for( i = 0; p_item->ppsz_list[i] != nil; i++ )
168 [o_intf_lang_pop addItemWithTitle: _NS( p_item->ppsz_list_text[i] )];
169 if( p_item->value.psz && !strcmp( p_item->value.psz, p_item->ppsz_list[i] ) )
172 [o_intf_lang_pop selectItemAtIndex: y];
174 [o_intf_art_pop removeAllItems];
175 p_item = config_FindConfig( VLC_OBJECT(p_intf), "album-art" );
176 for( i = 0; i < p_item->i_list; i++ )
177 [o_intf_art_pop addItemWithTitle: _NS( p_item->ppsz_list_text[i] )];
178 [o_intf_art_pop selectItemAtIndex: 0];
179 [o_intf_art_pop selectItemAtIndex: p_item->value.i];
181 [o_intf_meta_ckb setState: config_GetInt( p_intf, "fetch-meta" )];
182 [o_intf_fspanel_ckb setState: config_GetInt( p_intf, "macosx-fspanel" )];
183 [o_intf_embedded_ckb setState: config_GetInt( p_intf, "embeded-video" )];
189 [o_audio_enable_ckb setState: config_GetInt( p_intf, "audio" )];
190 [o_audio_vol_fld setIntValue: config_GetInt( p_intf, "volume" )];
191 [o_audio_vol_sld setIntValue: config_GetInt( p_intf, "volume" )];
193 [o_audio_spdif_ckb setState: config_GetInt( p_intf, "spdif" )];
195 [o_audio_dolby_pop removeAllItems];
196 p_item = config_FindConfig( VLC_OBJECT(p_intf), "force-dolby-surround" );
197 for( i = 0; i < p_item->i_list; i++ )
198 [o_audio_dolby_pop addItemWithTitle: _NS( p_item->ppsz_list_text[i] )];
199 [o_audio_dolby_pop selectItemAtIndex: 0];
200 [o_audio_dolby_pop selectItemAtIndex: p_item->value.i];
202 [o_audio_lang_fld setStringValue: [NSString stringWithUTF8String: config_GetPsz( p_intf, "audio-language" )]];
204 [o_audio_headphone_ckb setState: config_GetInt( p_intf, "headphone-dolby" )];
206 psz_tmp = config_GetPsz( p_intf, "audio-filter" );
208 [o_audio_norm_ckb setState: (int)strstr( psz_tmp, "normvol" )];
209 [o_audio_norm_fld setFloatValue: config_GetFloat( p_intf, "norm-max-level" )];
212 msg_Warn( p_intf, "visualizer not implemented!" );
214 /* Last.FM is optional */
215 if( module_Exists( p_intf, "audioscrobbler" ) )
217 [o_audio_lastuser_fld setStringValue: [NSString stringWithUTF8String: config_GetPsz( p_intf, "lastfm-username" )]];
218 [o_audio_lastpwd_fld setStringValue: [NSString stringWithUTF8String: config_GetPsz( p_intf, "lastfm-password" )]];
220 if( config_ExistIntf( VLC_OBJECT( p_intf ), "audioscrobbler" ) )
221 [o_audio_last_ckb setState: NSOnState];
223 [o_audio_last_ckb setState: NSOffState];
229 [o_video_enable_ckb setState: config_GetInt( p_intf, "video" )];
230 [o_video_fullscreen_ckb setState: config_GetInt( p_intf, "fullscreen" )];
231 [o_video_onTop_ckb setState: config_GetInt( p_intf, "video-on-top" )];
232 [o_video_skipFrames_ckb setState: config_GetInt( p_intf, "skip-frames" )];
233 [o_video_black_ckb setState: config_GetInt( p_intf, "macosx-black" )];
235 msg_Warn( p_intf, "vout module and display device selectors not implemented!" );
237 if( config_GetPsz( p_intf, "snapshot-path" ) != NULL )
238 [o_video_snap_folder_fld setStringValue: [NSString stringWithUTF8String: config_GetPsz( p_intf, "snapshot-path" )]];
239 [o_video_snap_prefix_fld setStringValue: [NSString stringWithUTF8String: config_GetPsz( p_intf, "snapshot-prefix" )]];
240 [o_video_snap_seqnum_ckb setState: config_GetInt( p_intf, "snapshot-sequential" )];
241 [o_video_snap_format_pop removeAllItems];
242 p_item = config_FindConfig( VLC_OBJECT(p_intf), "snapshot-format" );
243 for( i = 0; p_item->ppsz_list[i] != nil; i++ )
245 [o_video_snap_format_pop addItemWithTitle: [NSString stringWithUTF8String: p_item->ppsz_list[i]]];
246 if( p_item->value.psz && !strcmp( p_item->value.psz, p_item->ppsz_list[i] ) )
249 [o_video_snap_format_pop selectItemAtIndex: y];
255 /*********************
256 * subtitle settings *
257 *********************/
259 /********************
261 ********************/
264 - (void)showSimplePrefs
266 /* we want to show the interface settings, if no category was chosen */
267 if( [o_sprefs_toolbar selectedItemIdentifier] == nil )
269 [o_sprefs_toolbar setSelectedItemIdentifier: VLCIntfSettingToolbarIdentifier];
270 [self showInterfaceSettings];
273 [o_sprefs_win makeKeyAndOrderFront: self];
276 - (IBAction)buttonAction:(id)sender
278 if( sender == o_sprefs_cancel_btn )
279 [o_sprefs_win orderOut: sender];
280 else if( sender == o_sprefs_save_btn )
282 [self saveChangedSettings];
283 [o_sprefs_win orderOut: sender];
285 else if( sender == o_sprefs_reset_btn )
286 NSBeginInformationalAlertSheet( _NS("Reset Preferences"), _NS("Cancel"),
287 _NS("Continue"), nil, o_sprefs_win, self,
288 @selector(sheetDidEnd: returnCode: contextInfo:), NULL, nil,
289 _NS("Beware this will reset the VLC media player preferences.\n"
290 "Are you sure you want to continue?") );
291 else if( sender == o_sprefs_basicFull_matrix )
293 [o_sprefs_win orderOut: self];
294 [[[VLCMain sharedInstance] getPreferences] showPrefs];
295 [self resetControls];
298 msg_Err( p_intf, "unknown buttonAction sender" );
301 - (void)sheetDidEnd:(NSWindow *)o_sheet
302 returnCode:(int)i_return
303 contextInfo:(void *)o_context
305 if( i_return == NSAlertAlternateReturn )
307 config_ResetAll( p_intf );
308 [self resetControls];
312 - (void)saveChangedSettings
314 module_config_t *p_item;
318 /**********************
319 * interface settings *
320 **********************/
321 if( b_intfSettingChanged )
323 p_item = config_FindConfig( VLC_OBJECT(p_intf), "language" );
324 if( [o_intf_lang_pop indexOfSelectedItem] >= 0 )
325 config_PutPsz( p_intf, "language", strdup( p_item->ppsz_list[[o_intf_lang_pop indexOfSelectedItem]] ) );
327 config_PutPsz( p_intf, "language", strdup( [[VLCMain sharedInstance] delocalizeString: [o_intf_lang_pop stringValue]] ) );
329 p_item = config_FindConfig( VLC_OBJECT(p_intf), "album-art" );
330 if( [o_intf_art_pop indexOfSelectedItem] >= 0 )
331 config_PutInt( p_intf, "album-art", p_item->pi_list[[o_intf_art_pop indexOfSelectedItem]] );
333 config_PutInt( p_intf, "album-art", [o_intf_art_pop intValue] );
335 config_PutInt( p_intf, "fetch-meta", [o_intf_meta_ckb state] );
336 config_PutInt( p_intf, "macosx-fspanel", [o_intf_fspanel_ckb state] );
337 config_PutInt( p_intf, "video-embeded", [o_intf_embedded_ckb state] );
339 /* okay, let's save our changes to vlcrc */
340 i = config_SaveConfigFile( p_intf, "main" );
341 i = config_SaveConfigFile( p_intf, "macosx" );
345 msg_Err( p_intf, "An error occured while saving the Interface settings using SimplePrefs" );
349 b_intfSettingChanged = NO;
355 if( b_audioSettingChanged )
357 config_PutInt( p_intf, "audio", [o_audio_enable_ckb state] );
358 config_PutInt( p_intf, "volume", [o_audio_vol_sld intValue] );
359 config_PutInt( p_intf, "spdif", [o_audio_spdif_ckb state] );
361 p_item = config_FindConfig( VLC_OBJECT(p_intf), "force-dolby-surround" );
362 if( [o_audio_dolby_pop indexOfSelectedItem] >= 0 )
363 config_PutInt( p_intf, "force-dolby-surround", p_item->pi_list[[o_audio_dolby_pop indexOfSelectedItem]] );
365 config_PutInt( p_intf, "force-dolby-surround", [o_audio_dolby_pop intValue] );
367 config_PutPsz( p_intf, "audio-language", [[o_audio_lang_fld stringValue] UTF8String] );
368 config_PutInt( p_intf, "headphone-dolby", [o_audio_headphone_ckb state] );
370 psz_tmp = config_GetPsz( p_intf, "audio-filter" );
372 config_PutPsz( p_intf, "audio-filter", "volnorm" );
373 else if( (int)strstr( psz_tmp, "normvol" ) == NO )
375 /* work-around a GCC 4.0.1 bug */
376 psz_tmp = (char *)[[NSString stringWithFormat: @"%s:volnorm", psz_tmp] UTF8String];
377 config_PutPsz( p_intf, "audio-filter", psz_tmp );
381 psz_tmp = (char *)[[[NSString stringWithUTF8String: psz_tmp] stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@":volnorm"]] UTF8String];
382 psz_tmp = (char *)[[[NSString stringWithUTF8String: psz_tmp] stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@"volnorm:"]] UTF8String];
383 psz_tmp = (char *)[[[NSString stringWithUTF8String: psz_tmp] stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@"volnorm"]] UTF8String];
384 config_PutPsz( p_intf, "audio-filter", psz_tmp );
386 config_PutFloat( p_intf, "norm-max-level", [o_audio_norm_fld floatValue] );
388 msg_Warn( p_intf, "visualizer not implemented!" );
390 /* Last.FM is optional */
391 if( module_Exists( p_intf, "audioscrobbler" ) )
393 if( [o_audio_last_ckb state] == NSOnState )
394 config_AddIntf( VLC_OBJECT( p_intf ), "audioscrobbler" );
396 config_RemoveIntf( VLC_OBJECT( p_intf ), "audioscrobbler" );
398 config_PutPsz( p_intf, "lastfm-username", [[o_audio_lastuser_fld stringValue] UTF8String] );
399 config_PutPsz( p_intf, "lastfm-password", [[o_audio_lastuser_fld stringValue] UTF8String] );
402 /* okay, let's save our changes to vlcrc */
403 i = config_SaveConfigFile( p_intf, "main" );
404 i = i + config_SaveConfigFile( p_intf, "audioscrobbler" );
405 i = i + config_SaveConfigFile( p_intf, "volnorm" );
409 msg_Err( p_intf, "An error occured while saving the Audio settings using SimplePrefs" );
412 b_audioSettingChanged = NO;
418 if( b_videoSettingChanged )
420 config_PutInt( p_intf, "video", [o_video_enable_ckb state] );
421 config_PutInt( p_intf, "fullscreen", [o_video_fullscreen_ckb state] );
422 config_PutInt( p_intf, "video-on-top", [o_video_onTop_ckb state] );
423 config_PutInt( p_intf, "skip-frames", [o_video_skipFrames_ckb state] );
424 config_PutInt( p_intf, "macosx-black", [o_video_black_ckb state] );
426 msg_Warn( p_intf, "vout module and display device selectors not implemented!" );
428 config_PutPsz( p_intf, "snapshot-path", [[o_video_snap_folder_fld stringValue] UTF8String] );
429 config_PutPsz( p_intf, "snapshot-prefix", [[o_video_snap_prefix_fld stringValue] UTF8String] );
430 config_PutInt( p_intf, "snapshot-sequential", [o_video_snap_seqnum_ckb state] );
432 if( [o_video_snap_format_pop indexOfSelectedItem] >= 0 )
433 config_PutPsz( p_intf, "snapshot-format", [[[o_video_snap_format_pop selectedItem] title] UTF8String] );
435 i = config_SaveConfigFile( p_intf, "main" );
436 i = i + config_SaveConfigFile( p_intf, "macosx" );
440 msg_Err( p_intf, "An error occured while saving the Video settings using SimplePrefs" );
443 b_videoSettingChanged = NO;
447 - (void)showSettingsForCategory: (id)o_new_category_view
449 NSRect o_win_rect, o_view_rect, o_old_view_rect;
450 o_win_rect = [o_sprefs_win frame];
451 o_view_rect = [o_new_category_view frame];
453 if( o_currentlyShownCategoryView != nil )
455 /* restore our window's height, if we've shown another category previously */
456 o_old_view_rect = [o_currentlyShownCategoryView frame];
457 o_win_rect.size.height = o_win_rect.size.height - o_old_view_rect.size.height;
459 /* remove our previous category view */
460 [o_currentlyShownCategoryView removeFromSuperviewWithoutNeedingDisplay];
463 o_win_rect.size.height = o_win_rect.size.height + o_view_rect.size.height;
465 [o_sprefs_win displayIfNeeded];
466 [o_sprefs_win setFrame: o_win_rect display:YES animate: YES];
468 [o_new_category_view setFrame: NSMakeRect( 0,
469 [o_sprefs_controls_box frame].size.height,
470 o_view_rect.size.width,
471 o_view_rect.size.height )];
472 [o_new_category_view setNeedsDisplay: YES];
473 [o_new_category_view setAutoresizesSubviews: YES];
474 [[o_sprefs_win contentView] addSubview: o_new_category_view];
476 /* keep our current category for further reference */
477 [o_currentlyShownCategoryView release];
478 o_currentlyShownCategoryView = o_new_category_view;
479 [o_currentlyShownCategoryView retain];
482 - (IBAction)interfaceSettingChanged:(id)sender
484 b_intfSettingChanged = YES;
487 - (void)showInterfaceSettings
489 msg_Dbg( p_intf, "showing interface settings" );
490 [self showSettingsForCategory: o_intf_view];
493 - (IBAction)audioSettingChanged:(id)sender
495 if( sender == o_audio_vol_sld )
496 [o_audio_vol_fld setIntValue: [o_audio_vol_sld intValue]];
498 if( sender == o_audio_vol_fld )
499 [o_audio_vol_sld setIntValue: [o_audio_vol_fld intValue]];
501 b_audioSettingChanged = YES;
504 - (void)showAudioSettings
506 msg_Dbg( p_intf, "showing audio settings" );
507 [self showSettingsForCategory: o_audio_view];
510 - (IBAction)videoSettingChanged:(id)sender
512 if( sender == o_video_snap_folder_btn )
514 o_selectFolderPanel = [[NSOpenPanel alloc] init];
515 [o_selectFolderPanel setCanChooseDirectories: YES];
516 [o_selectFolderPanel setCanChooseFiles: NO];
517 [o_selectFolderPanel setResolvesAliases: YES];
518 [o_selectFolderPanel setAllowsMultipleSelection: NO];
519 [o_selectFolderPanel setMessage: _NS("Choose the Folder to save your video snapshots to.")];
520 [o_selectFolderPanel setCanCreateDirectories: YES];
521 [o_selectFolderPanel setPrompt: _NS("Choose")];
522 [o_selectFolderPanel beginSheetForDirectory: nil file: nil modalForWindow: o_sprefs_win
524 didEndSelector: @selector(savePanelDidEnd:returnCode:contextInfo:)
528 b_videoSettingChanged = YES;
531 - (void)savePanelDidEnd:(NSOpenPanel * )panel returnCode: (int)returnCode contextInfo: (void *)contextInfo
533 if( returnCode == NSOKButton )
535 [o_video_snap_folder_fld setStringValue: [o_selectFolderPanel filename]];
536 b_videoSettingChanged = YES;
539 [o_selectFolderPanel release];
542 - (void)showVideoSettings
544 msg_Dbg( p_intf, "showing video settings" );
545 [self showSettingsForCategory: o_video_view];