]> git.sesse.net Git - vlc/blob - modules/gui/macosx/coredialogs.m
Merge branch 'master' into lpcm_encoder
[vlc] / modules / gui / macosx / coredialogs.m
1 /*****************************************************************************
2  * coredialogs.m: Mac OS X Core Dialogs
3  *****************************************************************************
4  * Copyright (C) 2005-2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Derk-Jan Hartman <hartman at videolan dot org>
8  *          Felix Paul Kühne <fkuehne at videolan dot org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #import "intf.h"
26 #import "coredialogs.h"
27 #import "misc.h"
28
29 /* for the icon in our custom error panel */
30 #import <ApplicationServices/ApplicationServices.h>
31
32 /*****************************************************************************
33  * VLCCoreDialogProvider implementation
34  *****************************************************************************/
35 @implementation VLCCoreDialogProvider
36
37 static VLCCoreDialogProvider *_o_sharedInstance = nil;
38
39 + (VLCCoreDialogProvider *)sharedInstance
40 {
41     return _o_sharedInstance ? _o_sharedInstance : [[self alloc] init];
42 }
43
44 -(id)init
45 {
46     if( _o_sharedInstance )
47         [self dealloc];
48     else
49     {
50         _o_sharedInstance = [super init];
51         [[NSNotificationCenter defaultCenter] addObserver:self
52                                                  selector:@selector(performDialogEvent:)
53                                                      name: @"VLCNewCoreDialogEventNotification"
54                                                    object:self];
55         o_error_panel = [[VLCErrorPanel alloc] init];
56         b_progress_cancelled = NO;
57     }
58     
59     return _o_sharedInstance;
60 }
61
62 -(void)awakeFromNib
63 {
64     [o_auth_login_txt setStringValue: _NS("User name")];
65     [o_auth_pw_txt setStringValue: _NS("Password")];
66     [o_auth_cancel_btn setTitle: _NS("Cancel")];
67     [o_auth_ok_btn setTitle: _NS("OK")];
68     [o_prog_cancel_btn setTitle: _NS("Cancel")];
69     [o_prog_bar setUsesThreadedAnimation: YES];
70
71 }    
72
73 -(void)performDialogEvent: (NSNotification *)o_notification
74 {
75     NSValue *o_value = [[o_notification userInfo] objectForKey:@"VLCDialogPointer"];
76     NSString *o_type = [[o_notification userInfo] objectForKey:@"VLCDialogType"];
77
78     if( [o_type isEqualToString: @"dialog-error"] )
79         [self showFatalDialog: o_value];
80     else if( [o_type isEqualToString: @"dialog-critical"] )
81         [self showFatalWaitDialog: o_value];
82     else if( [o_type isEqualToString: @"dialog-question"] )
83         [self showQuestionDialog: o_value];
84     else if( [o_type isEqualToString: @"dialog-login"] )
85         [self showLoginDialog: o_value];
86     else if( [o_type isEqualToString: @"dialog-progress-bar"] )
87         [self showProgressDialog: o_value];
88     else
89         msg_Err( VLCIntf, "unhandled dialog type: '%s'", [o_type UTF8String] );
90 }
91
92 -(void)showFatalDialog: (NSValue *)o_value
93 {
94     dialog_fatal_t *p_dialog = [o_value pointerValue];
95
96     [o_error_panel addError: [NSString stringWithUTF8String: p_dialog->title] withMsg: [NSString stringWithUTF8String: p_dialog->message]];
97     [o_error_panel showPanel];
98 }
99
100 -(void)showFatalWaitDialog: (NSValue *)o_value
101 {
102     dialog_fatal_t *p_dialog = [o_value pointerValue];
103     NSAlert *o_alert;
104
105     o_alert = [NSAlert alertWithMessageText: [NSString stringWithUTF8String: p_dialog->title] defaultButton: _NS("OK") alternateButton: nil otherButton: nil informativeTextWithFormat: [NSString stringWithUTF8String: p_dialog->message]];
106     [o_alert setAlertStyle: NSCriticalAlertStyle];
107     [o_alert runModal];
108 }
109
110 -(void)showQuestionDialog: (NSValue *)o_value
111 {
112     dialog_question_t *p_dialog = [o_value pointerValue];
113     NSAlert *o_alert;
114     NSString *o_yes, *o_no, *o_cancel;
115     NSInteger i_returnValue = 0;
116     
117     if( p_dialog->yes != NULL )
118         o_yes = [NSString stringWithUTF8String: p_dialog->yes];
119     if( p_dialog->no != NULL )
120         o_no = [NSString stringWithUTF8String: p_dialog->no];
121     if( p_dialog->cancel != NULL )
122         o_cancel = [NSString stringWithUTF8String: p_dialog->cancel];
123
124     o_alert = [NSAlert alertWithMessageText: [NSString stringWithUTF8String: p_dialog->title] defaultButton: o_yes alternateButton:o_no otherButton: o_cancel informativeTextWithFormat: [NSString stringWithUTF8String: p_dialog->message]];
125     [o_alert setAlertStyle: NSInformationalAlertStyle];
126     i_returnValue = [o_alert runModal];
127
128     if( i_returnValue == NSAlertDefaultReturn )
129         p_dialog->answer = 1;
130     if( i_returnValue == NSAlertAlternateReturn )
131         p_dialog->answer = 2;
132     if( i_returnValue == NSAlertOtherReturn )
133         p_dialog->answer = 3;
134 }
135
136 -(void)showLoginDialog: (NSValue *)o_value
137 {
138     dialog_login_t *p_dialog = [o_value pointerValue];
139     NSInteger i_returnValue = 0;
140
141     [o_auth_title_txt setStringValue: [NSString stringWithUTF8String: p_dialog->title]];
142     [o_auth_win setTitle: [NSString stringWithUTF8String: p_dialog->title]];
143     [o_auth_description_txt setStringValue: [NSString stringWithUTF8String: p_dialog->message]];
144     [o_auth_login_fld setStringValue: @""];
145     [o_auth_pw_fld setStringValue: @""];
146
147     [o_auth_win center];
148     i_returnValue = [NSApp runModalForWindow: o_auth_win];
149     [o_auth_win close];
150     if( i_returnValue )
151     {
152         *p_dialog->username = strdup( [[o_auth_login_fld stringValue] UTF8String] );
153         *p_dialog->password = strdup( [[o_auth_pw_fld stringValue] UTF8String] );
154     }
155     else
156     {
157          *p_dialog->username = *p_dialog->password = NULL;
158     }
159 }
160
161 -(IBAction)loginDialogAction:(id)sender
162 {
163     if( [[sender title] isEqualToString: _NS("OK")] )
164         [NSApp stopModalWithCode: 1];
165     else
166         [NSApp stopModalWithCode: 0];
167 }
168
169 -(void)showProgressDialog: (NSValue *)o_value
170 {
171     dialog_progress_bar_t *p_dialog = [o_value pointerValue];
172
173     if( p_dialog->title != NULL )
174     {
175         [o_prog_win setTitle: [NSString stringWithUTF8String: p_dialog->title]];
176         [o_prog_title_txt setStringValue: [NSString stringWithUTF8String: p_dialog->title]];
177     }
178     else
179     {
180         [o_prog_win setTitle: @""];
181         [o_prog_title_txt setStringValue: @""];
182     }
183     if( p_dialog->cancel != NULL )
184         [o_prog_cancel_btn setTitle: [NSString stringWithUTF8String: p_dialog->cancel]];
185     else
186         [o_prog_cancel_btn setTitle: _NS("Cancel")];
187     if( p_dialog->message != NULL )
188         [o_prog_description_txt setStringValue: [NSString stringWithUTF8String: p_dialog->message]];
189     else
190         [o_prog_description_txt setStringValue: @""];
191     [o_prog_bar setDoubleValue: 0];
192     [o_prog_bar startAnimation: self];
193
194     [o_prog_win makeKeyAndOrderFront: self];
195 }
196
197 -(void)updateProgressPanelWithText: (NSString *)string andNumber: (double)d_number
198 {
199     [o_prog_description_txt setStringValue: string];
200     [o_prog_bar setDoubleValue: d_number];
201 }
202
203 -(void)destroyProgressPanel
204 {
205     [o_prog_bar stopAnimation: self];
206     [o_prog_win close];
207 }
208
209 -(IBAction)progDialogAction:(id)sender
210 {
211     b_progress_cancelled = YES;
212 }
213
214 -(BOOL)progressCancelled
215 {
216     return b_progress_cancelled;
217 }
218
219 -(id)errorPanel
220 {
221     return o_error_panel;
222 }
223
224 -(void)dealloc
225 {
226     [[NSNotificationCenter defaultCenter] removeObserver:self];
227     [super dealloc];
228 }
229 @end
230
231 /*****************************************************************************
232  * VLCErrorPanel implementation
233  *****************************************************************************/
234 @implementation VLCErrorPanel
235 -(id)init
236 {
237     [super init];
238
239     if( !b_nib_loaded )
240     {
241         b_nib_loaded = [NSBundle loadNibNamed:@"ErrorPanel" owner:self];
242     
243         /* init strings */
244         [o_window setTitle: _NS("Errors and Warnings")];
245         [o_cleanup_button setTitle: _NS("Clean up")];
246         [o_messages_btn setTitle: _NS("Show Details")];
247     }
248
249     /* init data sources */
250     o_errors = [[NSMutableArray alloc] init];
251     o_icons = [[NSMutableArray alloc] init];
252
253     return self;
254 }
255
256 -(void)dealloc
257 {
258     [o_errors release];
259     [o_icons release];
260     [super dealloc];
261 }
262
263 -(void)showPanel
264 {
265     [o_window makeKeyAndOrderFront: self];
266 }
267
268 -(void)addError: (NSString *)o_error withMsg:(NSString *)o_msg
269 {
270     /* format our string as desired */
271     NSMutableAttributedString * ourError;
272     ourError = [[NSMutableAttributedString alloc] initWithString:
273         [NSString stringWithFormat:@"%@\n%@", o_error, o_msg]
274         attributes:
275         [NSDictionary dictionaryWithObject: [NSFont systemFontOfSize:11] forKey: NSFontAttributeName]];
276     [ourError
277         addAttribute: NSFontAttributeName
278         value: [NSFont boldSystemFontOfSize:11]
279         range: NSMakeRange( 0, [o_error length])];
280     [o_errors addObject: ourError];
281     [ourError release];
282
283     [o_icons addObject: [NSImage imageWithErrorIcon]];
284
285     [o_error_table reloadData];
286 }
287
288 -(IBAction)cleanupTable:(id)sender
289 {
290     [o_errors removeAllObjects];
291     [o_icons removeAllObjects];
292     [o_error_table reloadData];
293 }
294
295 -(IBAction)showMessages:(id)sender
296 {
297     [[VLCMain sharedInstance] showMessagesPanel: sender];
298 }
299
300 /*----------------------------------------------------------------------------
301  * data source methods
302  *---------------------------------------------------------------------------*/
303 - (NSInteger)numberOfRowsInTableView:(NSTableView *)theDataTable
304 {
305     return [o_errors count];
306 }
307
308 - (id)tableView:(NSTableView *)theDataTable objectValueForTableColumn:
309     (NSTableColumn *)theTableColumn row: (NSInteger)row
310 {
311     if( [[theTableColumn identifier] isEqualToString: @"error_msg"] )
312         return [o_errors objectAtIndex: row];
313
314     if( [[theTableColumn identifier] isEqualToString: @"icon"] )
315         return [o_icons objectAtIndex: row];
316
317     return @"unknown identifier";
318 }
319
320 @end