]> git.sesse.net Git - vlc/blob - modules/gui/macosx/coredialogs.m
macosx: fix compilation on 10.6 with clang 3.0
[vlc] / modules / gui / macosx / coredialogs.m
1 /*****************************************************************************
2  * coredialogs.m: Mac OS X Core Dialogs
3  *****************************************************************************
4  * Copyright (C) 2005-2012 VLC authors and VideoLAN
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 NSString *toNSStr(const char *str) {
33     return str != NULL ? [NSString stringWithUTF8String:str] : @"";
34 }
35
36 /*****************************************************************************
37  * VLCCoreDialogProvider implementation
38  *****************************************************************************/
39 @implementation VLCCoreDialogProvider
40
41 static VLCCoreDialogProvider *_o_sharedInstance = nil;
42
43 + (VLCCoreDialogProvider *)sharedInstance
44 {
45     return _o_sharedInstance ? _o_sharedInstance : [[self alloc] init];
46 }
47
48 -(id)init
49 {
50     if (_o_sharedInstance)
51         [self dealloc];
52     else {
53         _o_sharedInstance = [super init];
54         o_error_panel = [[VLCErrorPanel alloc] init];
55         b_progress_cancelled = NO;
56     }
57
58     return _o_sharedInstance;
59 }
60
61 -(void)awakeFromNib
62 {
63     [o_auth_login_txt setStringValue: _NS("User name")];
64     [o_auth_pw_txt setStringValue: _NS("Password")];
65     [o_auth_cancel_btn setTitle: _NS("Cancel")];
66     [o_auth_ok_btn setTitle: _NS("OK")];
67     [o_prog_cancel_btn setTitle: _NS("Cancel")];
68     [o_prog_bar setUsesThreadedAnimation: YES];
69
70 }
71
72 -(void)performEventWithObject: (NSValue *)o_value ofType: (const char*)type
73 {
74     NSString *o_type = [NSString stringWithUTF8String:type];
75
76     if ([o_type isEqualToString: @"dialog-error"])
77         [self performSelectorOnMainThread:@selector(showFatalDialog:) withObject:o_value waitUntilDone:YES];
78     else if ([o_type isEqualToString: @"dialog-critical"])
79         [self performSelectorOnMainThread:@selector(showFatalWaitDialog:) withObject:o_value waitUntilDone:YES];
80     else if ([o_type isEqualToString: @"dialog-question"])
81         [self performSelectorOnMainThread:@selector(showQuestionDialog:) withObject:o_value waitUntilDone:YES];
82     else if ([o_type isEqualToString: @"dialog-login"])
83         [self performSelectorOnMainThread:@selector(showLoginDialog:) withObject:o_value waitUntilDone:YES];
84     else if ([o_type isEqualToString: @"dialog-progress-bar"])
85         [self performSelectorOnMainThread:@selector(showProgressDialogOnMainThread:) withObject: o_value waitUntilDone:YES];
86     else
87         msg_Err(VLCIntf, "unhandled dialog type: '%s'", type);
88 }
89
90 -(void)showFatalDialog: (NSValue *)o_value
91 {
92     dialog_fatal_t *p_dialog = [o_value pointerValue];
93
94     [o_error_panel addError: toNSStr(p_dialog->title) withMsg: toNSStr(p_dialog->message)];
95     [o_error_panel showPanel];
96 }
97
98 -(void)showFatalWaitDialog: (NSValue *)o_value
99 {
100     dialog_fatal_t *p_dialog = [o_value pointerValue];
101     NSAlert *o_alert;
102
103     o_alert = [NSAlert alertWithMessageText: toNSStr(p_dialog->title) defaultButton: _NS("OK") alternateButton: nil otherButton: nil informativeTextWithFormat: @"%s", p_dialog->message];
104     [o_alert setAlertStyle: NSCriticalAlertStyle];
105     [o_alert runModal];
106 }
107
108 -(void)showQuestionDialog: (NSValue *)o_value
109 {
110     dialog_question_t *p_dialog = [o_value pointerValue];
111     NSAlert *o_alert;
112     NSInteger i_returnValue = 0;
113   
114     o_alert = [NSAlert alertWithMessageText: toNSStr(p_dialog->title) defaultButton: toNSStr(p_dialog->yes) alternateButton: toNSStr(p_dialog->no) otherButton: toNSStr(p_dialog->cancel) informativeTextWithFormat: @"%s", p_dialog->message];
115     [o_alert setAlertStyle: NSInformationalAlertStyle];
116     i_returnValue = [o_alert runModal];
117
118     if (i_returnValue == NSAlertDefaultReturn)
119         p_dialog->answer = 1;
120     if (i_returnValue == NSAlertAlternateReturn)
121         p_dialog->answer = 2;
122     if (i_returnValue == NSAlertOtherReturn)
123         p_dialog->answer = 3;
124 }
125
126 -(void)showLoginDialog: (NSValue *)o_value
127 {
128     dialog_login_t *p_dialog = [o_value pointerValue];
129     NSInteger i_returnValue = 0;
130
131     [o_auth_title_txt setStringValue: toNSStr(p_dialog->title)];
132     [o_auth_win setTitle: toNSStr(p_dialog->title)];
133     [o_auth_description_txt setStringValue: toNSStr(p_dialog->message)];
134     [o_auth_login_fld setStringValue: @""];
135     [o_auth_pw_fld setStringValue: @""];
136
137     [o_auth_win center];
138     i_returnValue = [NSApp runModalForWindow: o_auth_win];
139     [o_auth_win close];
140     if (i_returnValue)
141     {
142         *p_dialog->username = strdup([[o_auth_login_fld stringValue] UTF8String]);
143         *p_dialog->password = strdup([[o_auth_pw_fld stringValue] UTF8String]);
144     } else
145         *p_dialog->username = *p_dialog->password = NULL;
146 }
147
148 -(IBAction)loginDialogAction:(id)sender
149 {
150     if ([[sender title] isEqualToString: _NS("OK")])
151         [NSApp stopModalWithCode: 1];
152     else
153         [NSApp stopModalWithCode: 0];
154 }
155
156 -(void)showProgressDialogOnMainThread: (NSValue *)o_value
157 {
158     /* we work-around a Cocoa limitation here, since you cannot delay an execution
159      * on the main thread within a single call */
160     b_progress_cancelled = NO;
161
162     dialog_progress_bar_t *p_dialog = [o_value pointerValue];
163     if (!p_dialog || b_progress_cancelled)
164         return;
165
166     [o_prog_win setTitle: toNSStr(p_dialog->title)];
167     [o_prog_title_txt setStringValue: toNSStr(p_dialog->title)];
168
169     if (p_dialog->cancel != NULL)
170         [o_prog_cancel_btn setTitle: [NSString stringWithUTF8String:p_dialog->cancel]];
171     else
172         [o_prog_cancel_btn setTitle: _NS("Cancel")];
173
174     [o_prog_description_txt setStringValue: toNSStr(p_dialog->message)];
175
176     if (VLCIntf)
177         [self performSelector:@selector(showProgressDialog:) withObject: o_value afterDelay:3.00];
178 }
179
180 -(void)showProgressDialog: (NSValue *)o_value
181 {
182     dialog_progress_bar_t *p_dialog = [o_value pointerValue];
183
184     if (!p_dialog || b_progress_cancelled)
185         return;
186
187     [o_prog_bar setDoubleValue: 0];
188     [o_prog_bar setIndeterminate: YES];
189     [o_prog_bar startAnimation: self];
190
191     [o_prog_win makeKeyAndOrderFront: self];
192 }
193
194 -(void)updateProgressPanelWithText: (NSString *)string andNumber: (double)d_number
195 {
196     [o_prog_description_txt setStringValue: string];
197     if (d_number > 0)
198         [o_prog_bar setIndeterminate: NO];
199     [o_prog_bar setDoubleValue: d_number];
200 }
201
202 -(void)destroyProgressPanel
203 {
204     b_progress_cancelled = YES;
205     [o_prog_bar performSelectorOnMainThread:@selector(stopAnimation:) withObject:self waitUntilDone:YES];
206     [o_prog_win performSelectorOnMainThread:@selector(close) withObject:nil waitUntilDone:YES];
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 @end
225
226 /*****************************************************************************
227  * VLCErrorPanel implementation
228  *****************************************************************************/
229 @implementation VLCErrorPanel
230 -(id)init
231 {
232     [super init];
233
234     if (!b_nib_loaded)
235         b_nib_loaded = [NSBundle loadNibNamed:@"ErrorPanel" owner:self];
236
237     /* init data sources */
238     o_errors = [[NSMutableArray alloc] init];
239     o_icons = [[NSMutableArray alloc] init];
240
241     return self;
242 }
243
244 - (void)awakeFromNib
245 {
246     /* init strings */
247     [o_window setTitle: _NS("Errors and Warnings")];
248     [o_cleanup_button setTitle: _NS("Clean up")];
249     [o_messages_btn setTitle: _NS("Show Details")];
250 }
251
252 -(void)dealloc
253 {
254     [o_errors release];
255     [o_icons release];
256     [super dealloc];
257 }
258
259 -(void)showPanel
260 {
261     [o_window makeKeyAndOrderFront: self];
262 }
263
264 -(void)addError: (NSString *)o_error withMsg:(NSString *)o_msg
265 {
266     /* format our string as desired */
267     NSMutableAttributedString * ourError;
268     ourError = [[NSMutableAttributedString alloc] initWithString:
269         [NSString stringWithFormat:@"%@\n%@", o_error, o_msg]
270         attributes:
271         [NSDictionary dictionaryWithObject: [NSFont systemFontOfSize:11] forKey: NSFontAttributeName]];
272     [ourError
273         addAttribute: NSFontAttributeName
274         value: [NSFont boldSystemFontOfSize:11]
275         range: NSMakeRange(0, [o_error length])];
276     [o_errors addObject: ourError];
277     [ourError release];
278
279     [o_icons addObject: [[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(kAlertStopIcon)]];
280
281     [o_error_table reloadData];
282 }
283
284 -(IBAction)cleanupTable:(id)sender
285 {
286     [o_errors removeAllObjects];
287     [o_icons removeAllObjects];
288     [o_error_table reloadData];
289 }
290
291 -(IBAction)showMessages:(id)sender
292 {
293     [[VLCMain sharedInstance] showMessagesPanel: sender];
294 }
295
296 /*----------------------------------------------------------------------------
297  * data source methods
298  *---------------------------------------------------------------------------*/
299 - (NSInteger)numberOfRowsInTableView:(NSTableView *)theDataTable
300 {
301     return [o_errors count];
302 }
303
304 - (id)tableView:(NSTableView *)theDataTable objectValueForTableColumn:
305     (NSTableColumn *)theTableColumn row: (NSInteger)row
306 {
307     if ([[theTableColumn identifier] isEqualToString: @"error_msg"])
308         return [o_errors objectAtIndex:row];
309
310     if ([[theTableColumn identifier] isEqualToString: @"icon"])
311         return [o_icons objectAtIndex:row];
312
313     return @"unknown identifier";
314 }
315
316 @end