]> git.sesse.net Git - vlc/blob - modules/gui/macosx/simple_prefs.m
macosx: GUI demo of Simple Preferences showing the Interface and Audio categories...
[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 "intf.h"
25 #import <vlc/vlc.h>
26 #import "simple_prefs.h"
27 #import "prefs.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
33 @implementation VLCSimplePrefs
34
35 static VLCSimplePrefs *_o_sharedInstance = nil;
36
37 + (VLCSimplePrefs *)sharedInstance
38 {
39     return _o_sharedInstance ? _o_sharedInstance : [[self alloc] init];
40 }
41
42 - (id)init
43 {
44     if (_o_sharedInstance) {
45         [self dealloc];
46     } else {
47         p_intf = VLCIntf;
48         _o_sharedInstance = [super init];
49     }
50     
51     return _o_sharedInstance;
52 }
53
54 - (void)dealloc
55 {
56     [o_currentlyShownCategoryView release];
57     [o_sprefs_toolbar release];
58     
59     [super dealloc];
60 }
61
62 - (void)awakeFromNib
63 {
64     [self initStrings];
65
66     o_sprefs_toolbar = [[[NSToolbar alloc] initWithIdentifier: VLCSPrefsToolbarIdentifier] autorelease];
67
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
73     [o_sprefs_toolbar setDelegate: self];
74
75     [o_sprefs_win setToolbar: o_sprefs_toolbar];    
76 }
77
78 - (NSToolbarItem *) toolbar: (NSToolbar *)o_sprefs_toolbar 
79       itemForItemIdentifier: (NSString *)o_itemIdent 
80   willBeInsertedIntoToolbar: (BOOL)b_willBeInserted
81 {
82     NSToolbarItem *o_toolbarItem = nil;
83     
84     if( [o_itemIdent isEqual: VLCIntfSettingToolbarIdentifier] )
85     {
86         o_toolbarItem = [[[NSToolbarItem alloc] initWithItemIdentifier: o_itemIdent] autorelease];
87
88         [o_toolbarItem setLabel: _NS("Interface")];
89         [o_toolbarItem setPaletteLabel: _NS("Interface settings")];
90
91         [o_toolbarItem setToolTip: _NS("Interface settings")];
92         [o_toolbarItem setImage: [NSImage imageNamed: @"spref_cone_Interface_64"]];
93
94         [o_toolbarItem setTarget: self];
95         [o_toolbarItem setAction: @selector(showInterfaceSettings)];
96
97         [o_toolbarItem setEnabled: YES];
98         [o_toolbarItem setAutovalidates: YES];
99     }
100     else if( [o_itemIdent isEqual: VLCAudioSettingToolbarIdentifier] )
101     {
102         o_toolbarItem = [[[NSToolbarItem alloc] initWithItemIdentifier: o_itemIdent] autorelease];
103
104         [o_toolbarItem setLabel: _NS("Audio")];
105         [o_toolbarItem setPaletteLabel: _NS("General Audio settings")];
106
107         [o_toolbarItem setToolTip: _NS("General Audio settings")];
108         [o_toolbarItem setImage: [NSImage imageNamed: @"spref_cone_Audio_64"]];
109
110         [o_toolbarItem setTarget: self];
111         [o_toolbarItem setAction: @selector(showAudioSettings)];
112
113         [o_toolbarItem setEnabled: YES];
114         [o_toolbarItem setAutovalidates: YES];
115     }
116     
117     return o_toolbarItem;
118 }
119
120 - (NSArray *)toolbarDefaultItemIdentifiers: (NSToolbar *)toolbar
121 {
122     return [NSArray arrayWithObjects: VLCIntfSettingToolbarIdentifier, VLCAudioSettingToolbarIdentifier, NSToolbarFlexibleSpaceItemIdentifier, nil];
123 }
124
125 - (NSArray *)toolbarAllowedItemIdentifiers: (NSToolbar *)toolbar
126 {
127     return [NSArray arrayWithObjects: VLCIntfSettingToolbarIdentifier, VLCAudioSettingToolbarIdentifier, NSToolbarFlexibleSpaceItemIdentifier, nil];
128 }
129
130 - (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar
131 {
132     return [NSArray arrayWithObjects: VLCIntfSettingToolbarIdentifier, VLCAudioSettingToolbarIdentifier, NSToolbarFlexibleSpaceItemIdentifier, nil];
133 }
134
135 - (void)initStrings
136 {
137     [o_sprefs_reset_btn setEnabled: NO];
138     msg_Warn( p_intf, "localisation of the simple preferences not implemented!" );
139 }
140
141 - (void)showSimplePrefs
142 {
143     /* we want to show the interface settings, if no category was chosen */
144     if( [o_sprefs_toolbar selectedItemIdentifier] == nil )
145     {
146         [o_sprefs_toolbar setSelectedItemIdentifier: VLCIntfSettingToolbarIdentifier];
147         [self showInterfaceSettings];
148     }
149
150     [o_sprefs_win makeKeyAndOrderFront: self];
151 }
152
153 - (IBAction)buttonAction:(id)sender
154 {
155     if( sender == o_sprefs_cancel_btn )
156         [o_sprefs_win orderOut: sender];
157     else if( sender == o_sprefs_save_btn )
158     {
159         msg_Warn( p_intf, "sprefs saving not implemented, your changes have no effect!" );
160         [o_sprefs_win orderOut: sender];
161     }
162     else if( sender == o_sprefs_reset_btn )
163         NSBeginInformationalAlertSheet( _NS("Reset Preferences"), _NS("Cancel"),
164                                         _NS("Continue"), nil, o_sprefs_win, self,
165                                         @selector(sheetDidEnd: returnCode: contextInfo:), NULL, nil,
166                                         _NS("Beware this will reset the VLC media player preferences.\n"
167                                             "Are you sure you want to continue?") );
168     else if( sender == o_sprefs_basicFull_matrix )
169     {
170         [o_sprefs_win orderOut: self];
171         [[[VLCMain sharedInstance] getPreferences] showPrefs];
172         /* TODO: reset our selector controls here */
173     }
174     else
175         msg_Err( p_intf, "unknown buttonAction sender" );
176 }
177
178 - (void)sheetDidEnd:(NSWindow *)o_sheet 
179          returnCode:(int)i_return
180         contextInfo:(void *)o_context
181 {
182     if( i_return == NSAlertAlternateReturn )
183     {
184         config_ResetAll( p_intf );
185         /* TODO: we need to reset our views here */
186     }
187 }
188
189 - (void)showSettingsForCategory: (id)o_new_category_view
190 {
191     msg_Dbg( p_intf, "switching to another category" );
192     NSRect o_win_rect, o_view_rect, o_old_view_rect;
193     o_win_rect = [o_sprefs_win frame];
194     o_view_rect = [o_new_category_view frame];
195     
196     if( o_currentlyShownCategoryView != nil )
197     {
198         /* restore our window's height, if we've shown another category previously */
199         o_old_view_rect = [o_currentlyShownCategoryView frame];
200         o_win_rect.size.height = o_win_rect.size.height - o_old_view_rect.size.height;
201         
202         /* remove our previous category view */
203         [o_currentlyShownCategoryView removeFromSuperviewWithoutNeedingDisplay];
204     }
205     
206     o_win_rect.size.height = o_win_rect.size.height + o_view_rect.size.height;
207     
208     [o_sprefs_win displayIfNeeded];
209     [o_sprefs_win setFrame: o_win_rect display:YES animate: YES];
210     
211     [o_new_category_view setFrame: NSMakeRect( 0, 
212                                                [o_sprefs_controls_box frame].size.height, 
213                                                o_view_rect.size.width, 
214                                                o_view_rect.size.height )];
215     [o_new_category_view setNeedsDisplay: YES];
216     [o_new_category_view setAutoresizesSubviews: YES];
217     [[o_sprefs_win contentView] addSubview: o_new_category_view];
218     
219     /* keep our current category for further reference */
220     [o_currentlyShownCategoryView release];
221     o_currentlyShownCategoryView = o_new_category_view;
222     [o_currentlyShownCategoryView retain];
223 }
224
225 - (IBAction)interfaceSettingChanged:(id)sender
226 {
227 }
228
229 - (void)showInterfaceSettings
230 {
231     msg_Dbg( p_intf, "showing interface settings" );
232     [self showSettingsForCategory: o_intf_view];
233 }
234
235 - (IBAction)audioSettingChanged:(id)sender
236 {
237 }
238
239 - (void)showAudioSettings
240 {
241     msg_Dbg( p_intf, "showing audio settings" );
242     [self showSettingsForCategory: o_audio_view];
243 }
244 @end