]> git.sesse.net Git - vlc/blob - modules/gui/macosx/interaction.m
* implement the error panel correctly (for the records: not < 10.3 compatible)
[vlc] / modules / gui / macosx / interaction.m
1 /*****************************************************************************
2  * interaction.h: Mac OS X interaction dialogs
3  *****************************************************************************
4  * Copyright (C) 2005-2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Derk-Jan Hartman <hartman at videolan dot org>
8  *          Felix 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 "interaction.h"
27
28 /*****************************************************************************
29  * VLCInteractionList implementation
30  *****************************************************************************/
31 @implementation VLCInteractionList
32
33 -(id)init
34 {
35     [super init];
36     o_interaction_list = [[NSMutableArray alloc] initWithCapacity:1];
37     [[NSNotificationCenter defaultCenter] addObserver:self
38         selector:@selector(newInteractionEvent:)
39         name: @"VLCNewInteractionEventNotification"
40         object:self];
41
42     return self;
43 }
44
45 -(void)newInteractionEvent: (NSNotification *)o_notification
46 {
47     VLCInteraction *o_interaction;
48     NSValue *o_value = [[o_notification userInfo] objectForKey:@"VLCDialogPointer"];
49     interaction_dialog_t *p_dialog = [o_value pointerValue];
50
51     switch( p_dialog->i_action )
52     {
53     case INTERACT_NEW:
54         [self addInteraction: p_dialog];
55         break;
56     case INTERACT_UPDATE:
57         o_interaction = (VLCInteraction *)p_dialog->p_private;
58         [o_interaction updateDialog];
59         break;
60     case INTERACT_HIDE:
61         o_interaction = (VLCInteraction *)p_dialog->p_private;
62         [o_interaction hideDialog];
63         break;
64     case INTERACT_DESTROY:
65         o_interaction = (VLCInteraction *)p_dialog->p_private;
66         [o_interaction destroyDialog];
67         [self removeInteraction:o_interaction];
68         p_dialog->i_status = DESTROYED_DIALOG;
69         break;
70     }
71 }
72
73 -(void)addInteraction: (interaction_dialog_t *)p_dialog
74 {
75
76     VLCInteraction *o_interaction = [[VLCInteraction alloc] initDialog: p_dialog];
77     
78     p_dialog->p_private = (void *)o_interaction;
79     [o_interaction_list addObject:[o_interaction autorelease]];
80     [o_interaction runDialog];
81 }
82
83 -(void)removeInteraction: (VLCInteraction *)o_interaction
84 {
85     [o_interaction_list removeObject:o_interaction];
86 }
87
88 -(void)dealloc
89 {
90     [[NSNotificationCenter defaultCenter] removeObserver:self];
91     [o_interaction_list removeAllObjects];
92     [o_interaction_list release];
93     [super dealloc];
94 }
95
96 @end
97
98 /*****************************************************************************
99  * VLCInteraction implementation
100  *****************************************************************************/
101 @implementation VLCInteraction
102
103 -(id)initDialog: (interaction_dialog_t *)_p_dialog
104 {
105     p_intf = VLCIntf;
106     [super init];
107     p_dialog = _p_dialog;
108     return self;
109 }
110
111 -(void)runDialog
112 {
113     id o_window = NULL;
114     if( !p_dialog )
115         msg_Err( p_intf, "no available interaction framework" );
116
117     if( !nib_interact_loaded )
118     {
119         nib_interact_loaded = [NSBundle loadNibNamed:@"Interaction" owner:self];
120         [o_prog_cancel_btn setTitle: _NS("Cancel")];
121         [o_prog_bar setUsesThreadedAnimation: YES];
122         [o_auth_login_txt setStringValue: _NS("Login:")];
123         [o_auth_pw_txt setStringValue: _NS("Password:")];
124         [o_auth_cancel_btn setTitle: _NS("Cancel")];
125         [o_auth_ok_btn setTitle: _NS("OK")];
126         [o_input_ok_btn setTitle: _NS("OK")];
127         [o_input_cancel_btn setTitle: _NS("Cancel")];
128         o_mainIntfPgbar = [[VLCMain sharedInstance] getMainIntfPgbar];
129     }
130
131     NSString *o_title = [NSString stringWithUTF8String:p_dialog->psz_title ? p_dialog->psz_title : "title"];
132     NSString *o_description = [NSString stringWithUTF8String:p_dialog->psz_description ? p_dialog->psz_description : ""];
133     
134     vout_thread_t *p_vout = vlc_object_find( VLCIntf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
135     if( p_vout != NULL )
136     {
137         NSEnumerator * o_enum = [[NSApp orderedWindows] objectEnumerator];
138
139         while( ( o_window = [o_enum nextObject] ) )
140         {
141             if( [[o_window className] isEqualToString: @"VLCWindow"] )
142             {
143                 vlc_object_release( (vlc_object_t *)p_vout );
144                 break;
145             }
146         }
147         vlc_object_release( (vlc_object_t *)p_vout );
148     }
149     else
150     {
151         o_window = [NSApp mainWindow];
152     }
153
154     #if 0
155     msg_Dbg( p_intf, "Title: %s", [o_title UTF8String] );
156     msg_Dbg( p_intf, "Description: %s", [o_description UTF8String] );
157     #endif
158
159     if( p_dialog->i_id == DIALOG_ERRORS )
160     {
161         msg_Dbg( p_intf, "error panel requested" );
162         NSAlert * ourAlert = [NSAlert alertWithMessageText:
163             [NSString stringWithUTF8String:p_dialog->psz_title ? p_dialog->psz_title : _("Error")]
164             defaultButton: _NS("OK") alternateButton: nil otherButton: nil 
165             informativeTextWithFormat: 
166             [NSString stringWithUTF8String:p_dialog->psz_description]];
167         [ourAlert setAlertStyle: NSWarningAlertStyle];
168         [ourAlert runModal];
169     }
170     else
171     {
172         if( p_dialog->i_flags & DIALOG_OK_CANCEL )
173         {
174             msg_Dbg( p_intf, "OK-Cancel-dialog requested" );
175             NSBeginInformationalAlertSheet( o_title, _NS("OK") , _NS("Cancel"),
176                 nil, o_window, self,
177                 @selector(sheetDidEnd: returnCode: contextInfo:), NULL, nil, 
178                 o_description );
179         }
180         else if( p_dialog->i_flags & DIALOG_YES_NO_CANCEL )
181         {
182             msg_Dbg( p_intf, "yes-no-cancel-dialog requested" );
183             NSBeginInformationalAlertSheet( o_title, _NS("Yes"), _NS("No"),
184                 _NS("Cancel"), o_window, self,
185                 @selector(sheetDidEnd: returnCode: contextInfo:), NULL, nil, 
186                 o_description );
187         }
188         else if( p_dialog->i_flags & DIALOG_LOGIN_PW_OK_CANCEL )
189         {
190             msg_Dbg( p_intf, "dialog for login and pw requested" );
191             [o_auth_title setStringValue: o_title];
192             [o_auth_description setStringValue: o_description];
193             [o_auth_login_fld setStringValue: @""];
194             [o_auth_pw_fld setStringValue: @""];
195             [NSApp beginSheet: o_auth_win modalForWindow: o_window
196                 modalDelegate: self didEndSelector: nil contextInfo: nil];
197             [o_auth_win makeKeyWindow];
198         }
199         else if( p_dialog->i_flags & DIALOG_USER_PROGRESS )
200         {
201             msg_Dbg( p_intf, "user progress dialog requested" );
202             [o_prog_title setStringValue: o_title];
203             [o_prog_description setStringValue: o_description];
204             [o_prog_bar setDoubleValue: (double)p_dialog->val.f_float];
205             [NSApp beginSheet: o_prog_win modalForWindow: o_window
206                 modalDelegate: self didEndSelector: nil contextInfo: nil];
207             [o_prog_win makeKeyWindow];
208         }
209         else if( p_dialog->i_flags & DIALOG_PSZ_INPUT_OK_CANCEL )
210         {
211             msg_Dbg( p_intf, "text input from user requested" );
212             [o_input_title setStringValue: o_title];
213             [o_input_description setStringValue: o_description];
214             [o_input_fld setStringValue: @""];
215             [NSApp beginSheet: o_input_win modalForWindow: o_window
216                 modalDelegate: self didEndSelector: nil contextInfo: nil];
217             [o_input_win makeKeyWindow];
218         }
219         else if( p_dialog->i_flags & DIALOG_INTF_PROGRESS )
220         {
221             msg_Dbg( p_intf, "progress-bar in main intf requested" );
222             [[VLCMain sharedInstance] setScrollField: o_description stopAfter: -1];
223             [o_mainIntfPgbar setDoubleValue: (double)p_dialog->val.f_float];
224             [o_mainIntfPgbar setHidden: NO];
225             [[[VLCMain sharedInstance] getControllerWindow] makeKeyWindow];
226             [o_mainIntfPgbar setIndeterminate: NO];
227         }
228         else
229             msg_Err( p_intf, "requested dialog type unknown (%i)", 
230                 p_dialog->i_flags );
231     }
232 }
233
234 - (void)sheetDidEnd:(NSWindow *)o_sheet returnCode:(int)i_return
235     contextInfo:(void *)o_context
236 {
237     vlc_mutex_lock( &p_dialog->p_interaction->object_lock );
238     if( i_return == NSAlertDefaultReturn )
239     {
240         p_dialog->i_return = DIALOG_OK_YES;
241     }
242     else if( i_return == NSAlertAlternateReturn && ( p_dialog->i_flags & DIALOG_OK_CANCEL ) )
243     {
244         p_dialog->i_return = DIALOG_CANCELLED;
245     }
246     else if( i_return == NSAlertAlternateReturn )
247     {
248         p_dialog->i_return = DIALOG_NO;
249     }
250     else if( i_return == NSAlertOtherReturn )
251     {
252         p_dialog->i_return = DIALOG_CANCELLED;
253     }
254     p_dialog->i_status = ANSWERED_DIALOG;
255     vlc_mutex_unlock( &p_dialog->p_interaction->object_lock );
256 }
257
258 -(void)updateDialog
259 {
260     if( p_dialog->i_flags & DIALOG_USER_PROGRESS )
261     {
262         [o_prog_description setStringValue: \
263             [NSString stringWithUTF8String: p_dialog->psz_description]];
264         [o_prog_bar setDoubleValue: (double)p_dialog->val.f_float];
265
266         if( [o_prog_bar doubleValue] == 100.0 )
267         {
268             /* we are done, let's hide */
269             [self hideDialog];
270         }
271         return;
272     }
273     if( p_dialog->i_flags & DIALOG_INTF_PROGRESS )
274     {
275         [[VLCMain sharedInstance] setScrollField:
276             [NSString stringWithUTF8String: p_dialog->psz_description]
277             stopAfter: -1];
278         [o_mainIntfPgbar setDoubleValue: (double)p_dialog->val.f_float];
279
280         if( [o_mainIntfPgbar doubleValue] == 100.0 )
281         {
282             /* we are done, let's hide */
283             [self hideDialog];
284         }
285         return;
286     }
287 }
288
289 -(void)hideDialog
290 {
291     msg_Dbg( p_intf, "hide event" );
292     if( p_dialog->i_flags & DIALOG_USER_PROGRESS )
293     {
294         [NSApp endSheet: o_prog_win];
295         [o_prog_win close];
296     }
297     if( p_dialog->i_flags & DIALOG_LOGIN_PW_OK_CANCEL )
298     {
299         [NSApp endSheet: o_auth_win];
300         [o_auth_win close];
301     }
302     if( p_dialog->i_flags & DIALOG_PSZ_INPUT_OK_CANCEL )
303     {
304         [NSApp endSheet: o_input_win];
305         [o_input_win close];
306     }
307     if( p_dialog->i_flags & DIALOG_INTF_PROGRESS )
308     {
309         [o_mainIntfPgbar setIndeterminate: YES];
310         [o_mainIntfPgbar setHidden: YES];
311         [[VLCMain sharedInstance] resetScrollField];
312     }
313 }
314
315 -(void)destroyDialog
316 {
317     msg_Dbg( p_intf, "destroy event" );
318     if( o_mainIntfPgbar )
319         [o_mainIntfPgbar release];
320 }
321
322 - (IBAction)cancelAndClose:(id)sender
323 {
324     /* tell the core that the dialog was cancelled */
325     vlc_mutex_lock( &p_dialog->p_interaction->object_lock );
326     p_dialog->i_return = DIALOG_CANCELLED;
327     p_dialog->i_status = ANSWERED_DIALOG;
328     vlc_mutex_unlock( &p_dialog->p_interaction->object_lock );
329     msg_Dbg( p_intf, "dialog cancelled" );
330 }
331
332 - (IBAction)okayAndClose:(id)sender
333 {
334     vlc_mutex_lock( &p_dialog->p_interaction->object_lock );
335     if( p_dialog->i_flags == DIALOG_LOGIN_PW_OK_CANCEL )
336     {
337         p_dialog->psz_returned[0] = strdup( [[o_auth_login_fld stringValue] UTF8String] );
338         p_dialog->psz_returned[1] = strdup( [[o_auth_pw_fld stringValue] UTF8String] );
339     }
340     else if( p_dialog->i_flags == DIALOG_PSZ_INPUT_OK_CANCEL )
341         p_dialog->psz_returned[0] = strdup( [[o_input_fld stringValue] UTF8String] );
342     p_dialog->i_return = DIALOG_OK_YES;
343     p_dialog->i_status = ANSWERED_DIALOG;
344     vlc_mutex_unlock( &p_dialog->p_interaction->object_lock );
345     msg_Dbg( p_intf, "dialog acknowledged" );
346 }
347
348 @end