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, "embedded-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 b_intfSettingChanged, b_videoSettingChanged, b_audioSettingChanged = YES;
309 [self resetControls];
313 - (void)saveChangedSettings
315 module_config_t *p_item;
319 /**********************
320 * interface settings *
321 **********************/
322 if( b_intfSettingChanged )
324 p_item = config_FindConfig( VLC_OBJECT(p_intf), "language" );
325 if( [o_intf_lang_pop indexOfSelectedItem] >= 0 )
326 config_PutPsz( p_intf, "language", strdup( p_item->ppsz_list[[o_intf_lang_pop indexOfSelectedItem]] ) );
328 config_PutPsz( p_intf, "language", strdup( [[VLCMain sharedInstance] delocalizeString: [o_intf_lang_pop stringValue]] ) );
330 p_item = config_FindConfig( VLC_OBJECT(p_intf), "album-art" );
331 if( [o_intf_art_pop indexOfSelectedItem] >= 0 )
332 config_PutInt( p_intf, "album-art", p_item->pi_list[[o_intf_art_pop indexOfSelectedItem]] );
334 config_PutInt( p_intf, "album-art", [o_intf_art_pop intValue] );
336 config_PutInt( p_intf, "fetch-meta", [o_intf_meta_ckb state] );
337 config_PutInt( p_intf, "macosx-fspanel", [o_intf_fspanel_ckb state] );
338 config_PutInt( p_intf, "video-embeded", [o_intf_embedded_ckb state] );
340 /* okay, let's save our changes to vlcrc */
341 i = config_SaveConfigFile( p_intf, "main" );
342 i = config_SaveConfigFile( p_intf, "macosx" );
346 msg_Err( p_intf, "An error occured while saving the Interface settings using SimplePrefs" );
350 b_intfSettingChanged = NO;
356 if( b_audioSettingChanged )
358 config_PutInt( p_intf, "audio", [o_audio_enable_ckb state] );
359 config_PutInt( p_intf, "volume", [o_audio_vol_sld intValue] );
360 config_PutInt( p_intf, "spdif", [o_audio_spdif_ckb state] );
362 p_item = config_FindConfig( VLC_OBJECT(p_intf), "force-dolby-surround" );
363 if( [o_audio_dolby_pop indexOfSelectedItem] >= 0 )
364 config_PutInt( p_intf, "force-dolby-surround", p_item->pi_list[[o_audio_dolby_pop indexOfSelectedItem]] );
366 config_PutInt( p_intf, "force-dolby-surround", [o_audio_dolby_pop intValue] );
368 config_PutPsz( p_intf, "audio-language", [[o_audio_lang_fld stringValue] UTF8String] );
369 config_PutInt( p_intf, "headphone-dolby", [o_audio_headphone_ckb state] );
371 psz_tmp = config_GetPsz( p_intf, "audio-filter" );
373 config_PutPsz( p_intf, "audio-filter", "volnorm" );
374 else if( (int)strstr( psz_tmp, "normvol" ) == NO )
376 /* work-around a GCC 4.0.1 bug */
377 psz_tmp = (char *)[[NSString stringWithFormat: @"%s:volnorm", psz_tmp] UTF8String];
378 config_PutPsz( p_intf, "audio-filter", psz_tmp );
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 psz_tmp = (char *)[[[NSString stringWithUTF8String: psz_tmp] stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@"volnorm"]] UTF8String];
385 config_PutPsz( p_intf, "audio-filter", psz_tmp );
387 config_PutFloat( p_intf, "norm-max-level", [o_audio_norm_fld floatValue] );
389 msg_Warn( p_intf, "visualizer not implemented!" );
391 /* Last.FM is optional */
392 if( module_Exists( p_intf, "audioscrobbler" ) )
394 if( [o_audio_last_ckb state] == NSOnState )
395 config_AddIntf( VLC_OBJECT( p_intf ), "audioscrobbler" );
397 config_RemoveIntf( VLC_OBJECT( p_intf ), "audioscrobbler" );
399 config_PutPsz( p_intf, "lastfm-username", [[o_audio_lastuser_fld stringValue] UTF8String] );
400 config_PutPsz( p_intf, "lastfm-password", [[o_audio_lastuser_fld stringValue] UTF8String] );
403 /* okay, let's save our changes to vlcrc */
404 i = config_SaveConfigFile( p_intf, "main" );
405 i = i + config_SaveConfigFile( p_intf, "audioscrobbler" );
406 i = i + config_SaveConfigFile( p_intf, "volnorm" );
410 msg_Err( p_intf, "An error occured while saving the Audio settings using SimplePrefs" );
413 b_audioSettingChanged = NO;
419 if( b_videoSettingChanged )
421 config_PutInt( p_intf, "video", [o_video_enable_ckb state] );
422 config_PutInt( p_intf, "fullscreen", [o_video_fullscreen_ckb state] );
423 config_PutInt( p_intf, "video-on-top", [o_video_onTop_ckb state] );
424 config_PutInt( p_intf, "skip-frames", [o_video_skipFrames_ckb state] );
425 config_PutInt( p_intf, "macosx-black", [o_video_black_ckb state] );
427 msg_Warn( p_intf, "vout module and display device selectors not implemented!" );
429 config_PutPsz( p_intf, "snapshot-path", [[o_video_snap_folder_fld stringValue] UTF8String] );
430 config_PutPsz( p_intf, "snapshot-prefix", [[o_video_snap_prefix_fld stringValue] UTF8String] );
431 config_PutInt( p_intf, "snapshot-sequential", [o_video_snap_seqnum_ckb state] );
433 if( [o_video_snap_format_pop indexOfSelectedItem] >= 0 )
434 config_PutPsz( p_intf, "snapshot-format", [[[o_video_snap_format_pop selectedItem] title] UTF8String] );
436 i = config_SaveConfigFile( p_intf, "main" );
437 i = i + config_SaveConfigFile( p_intf, "macosx" );
441 msg_Err( p_intf, "An error occured while saving the Video settings using SimplePrefs" );
444 b_videoSettingChanged = NO;
448 - (void)showSettingsForCategory: (id)o_new_category_view
450 NSRect o_win_rect, o_view_rect, o_old_view_rect;
451 o_win_rect = [o_sprefs_win frame];
452 o_view_rect = [o_new_category_view frame];
454 if( o_currentlyShownCategoryView != nil )
456 /* restore our window's height, if we've shown another category previously */
457 o_old_view_rect = [o_currentlyShownCategoryView frame];
458 o_win_rect.size.height = o_win_rect.size.height - o_old_view_rect.size.height;
460 /* remove our previous category view */
461 [o_currentlyShownCategoryView removeFromSuperviewWithoutNeedingDisplay];
464 o_win_rect.size.height = o_win_rect.size.height + o_view_rect.size.height;
466 [o_sprefs_win displayIfNeeded];
467 [o_sprefs_win setFrame: o_win_rect display:YES animate: YES];
469 [o_new_category_view setFrame: NSMakeRect( 0,
470 [o_sprefs_controls_box frame].size.height,
471 o_view_rect.size.width,
472 o_view_rect.size.height )];
473 [o_new_category_view setNeedsDisplay: YES];
474 [o_new_category_view setAutoresizesSubviews: YES];
475 [[o_sprefs_win contentView] addSubview: o_new_category_view];
477 /* keep our current category for further reference */
478 [o_currentlyShownCategoryView release];
479 o_currentlyShownCategoryView = o_new_category_view;
480 [o_currentlyShownCategoryView retain];
483 - (IBAction)interfaceSettingChanged:(id)sender
485 b_intfSettingChanged = YES;
488 - (void)showInterfaceSettings
490 msg_Dbg( p_intf, "showing interface settings" );
491 [self showSettingsForCategory: o_intf_view];
494 - (IBAction)audioSettingChanged:(id)sender
496 if( sender == o_audio_vol_sld )
497 [o_audio_vol_fld setIntValue: [o_audio_vol_sld intValue]];
499 if( sender == o_audio_vol_fld )
500 [o_audio_vol_sld setIntValue: [o_audio_vol_fld intValue]];
502 b_audioSettingChanged = YES;
505 - (void)showAudioSettings
507 msg_Dbg( p_intf, "showing audio settings" );
508 [self showSettingsForCategory: o_audio_view];
511 - (IBAction)videoSettingChanged:(id)sender
513 if( sender == o_video_snap_folder_btn )
515 o_selectFolderPanel = [[NSOpenPanel alloc] init];
516 [o_selectFolderPanel setCanChooseDirectories: YES];
517 [o_selectFolderPanel setCanChooseFiles: NO];
518 [o_selectFolderPanel setResolvesAliases: YES];
519 [o_selectFolderPanel setAllowsMultipleSelection: NO];
520 [o_selectFolderPanel setMessage: _NS("Choose the Folder to save your video snapshots to.")];
521 [o_selectFolderPanel setCanCreateDirectories: YES];
522 [o_selectFolderPanel setPrompt: _NS("Choose")];
523 [o_selectFolderPanel beginSheetForDirectory: nil file: nil modalForWindow: o_sprefs_win
525 didEndSelector: @selector(savePanelDidEnd:returnCode:contextInfo:)
529 b_videoSettingChanged = YES;
532 - (void)savePanelDidEnd:(NSOpenPanel * )panel returnCode: (int)returnCode contextInfo: (void *)contextInfo
534 if( returnCode == NSOKButton )
536 [o_video_snap_folder_fld setStringValue: [o_selectFolderPanel filename]];
537 b_videoSettingChanged = YES;
540 [o_selectFolderPanel release];
543 - (void)showVideoSettings
545 msg_Dbg( p_intf, "showing video settings" );
546 [self showSettingsForCategory: o_video_view];