]> git.sesse.net Git - vlc/blob - modules/gui/macosx/interaction.m
* new dialogue type to prompt the user for a string
[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 #include "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     }
129
130     NSString *o_title = [NSString stringWithUTF8String:p_dialog->psz_title ? p_dialog->psz_title : "title"];
131     NSString *o_description = [NSString stringWithUTF8String:p_dialog->psz_description ? p_dialog->psz_description : ""];
132     
133     vout_thread_t *p_vout = vlc_object_find( VLCIntf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
134     if( p_vout != NULL )
135     {
136         NSEnumerator * o_enum = [[NSApp orderedWindows] objectEnumerator];
137
138         while( ( o_window = [o_enum nextObject] ) )
139         {
140             if( [[o_window className] isEqualToString: @"VLCWindow"] )
141             {
142                 vlc_object_release( (vlc_object_t *)p_vout );
143                 break;
144             }
145         }
146         vlc_object_release( (vlc_object_t *)p_vout );
147     }
148     else
149     {
150         o_window = [NSApp mainWindow];
151     }
152     
153     msg_Dbg( p_intf, "Title: %s", [o_title UTF8String] );
154     msg_Dbg( p_intf, "Description: %s", [o_description UTF8String] );
155     if( p_dialog->i_id == DIALOG_ERRORS )
156     {
157         msg_Err( p_intf, "Error: %s", p_dialog->psz_description );
158     }
159     else
160     {
161         if( p_dialog->i_flags & DIALOG_OK_CANCEL )
162         {
163             msg_Dbg( p_intf, "requested flag: DIALOG_OK_CANCEL" );
164             NSBeginInformationalAlertSheet( o_title, _NS("OK") , _NS("Cancel"),
165                 nil, o_window, self,
166                 @selector(sheetDidEnd: returnCode: contextInfo:), NULL, nil, 
167                 o_description );
168         }
169         else if( p_dialog->i_flags & DIALOG_YES_NO_CANCEL )
170         {
171             msg_Dbg( p_intf, "requested flag: DIALOG_YES_NO_CANCEL" );
172             NSBeginInformationalAlertSheet( o_title, _NS("Yes"), _NS("No"),
173                 _NS("Cancel"), o_window, self,
174                 @selector(sheetDidEnd: returnCode: contextInfo:), NULL, nil, 
175                 o_description );
176         }
177         else if( p_dialog->i_flags & DIALOG_LOGIN_PW_OK_CANCEL )
178         {
179             msg_Dbg( p_intf, "requested flag: DIALOG_LOGIN_PW_OK_CANCEL" );
180             [o_auth_title setStringValue: o_title];
181             [o_auth_description setStringValue: o_description];
182             [o_auth_login_fld setStringValue: @""];
183             [o_auth_pw_fld setStringValue: @""];
184             [NSApp beginSheet: o_auth_win modalForWindow: o_window
185                 modalDelegate: self didEndSelector: nil contextInfo: nil];
186             [o_auth_win makeKeyWindow];
187         }
188         else if( p_dialog->i_flags & DIALOG_USER_PROGRESS )
189         {
190             msg_Dbg( p_intf, "requested flag: DIALOG_USER_PROGRESS" );
191             [o_prog_title setStringValue: o_title];
192             [o_prog_description setStringValue: o_description];
193             [o_prog_bar setDoubleValue: 0];
194             [NSApp beginSheet: o_prog_win modalForWindow: o_window
195                 modalDelegate: self didEndSelector: nil contextInfo: nil];
196             [o_prog_win makeKeyWindow];
197         }
198         else if( p_dialog->i_flags & DIALOG_PSZ_INPUT_OK_CANCEL )
199         {
200             msg_Dbg( p_intf, "requested flag: DIALOG_STRING_INPUT_OK" );
201             [o_input_title setStringValue: o_title];
202             [o_input_description setStringValue: o_description];
203             [o_input_fld setStringValue: @""];
204             [NSApp beginSheet: o_input_win modalForWindow: o_window
205                 modalDelegate: self didEndSelector: nil contextInfo: nil];
206             [o_input_win makeKeyWindow];
207         }
208         else
209             msg_Warn( p_intf, "requested dialog type unknown" );
210     }
211 }
212
213 - (void)sheetDidEnd:(NSWindow *)o_sheet returnCode:(int)i_return
214     contextInfo:(void *)o_context
215 {
216     vlc_mutex_lock( &p_dialog->p_interaction->object_lock );
217     if( i_return == NSAlertDefaultReturn )
218     {
219         p_dialog->i_return = DIALOG_OK_YES;
220     }
221     else if( i_return == NSAlertAlternateReturn && ( p_dialog->i_flags & DIALOG_OK_CANCEL ) )
222     {
223         p_dialog->i_return = DIALOG_CANCELLED;
224     }
225     else if( i_return == NSAlertAlternateReturn )
226     {
227         p_dialog->i_return = DIALOG_NO;
228     }
229     else if( i_return == NSAlertOtherReturn )
230     {
231         p_dialog->i_return = DIALOG_CANCELLED;
232     }
233     p_dialog->i_status = ANSWERED_DIALOG;
234     vlc_mutex_unlock( &p_dialog->p_interaction->object_lock );
235 }
236
237 -(void)updateDialog
238 {
239     if( p_dialog->i_flags & DIALOG_USER_PROGRESS )
240     {
241         [o_prog_description setStringValue: \
242             [NSString stringWithUTF8String: p_dialog->psz_description]];
243         [o_prog_bar setDoubleValue: \
244             (double)(p_dialog->val.f_float)];
245
246         if( [o_prog_bar doubleValue] == 100.0 )
247         {
248             /* we are done, let's hide */
249             [self hideDialog];
250             return;
251         }
252     }
253 }
254
255 -(void)hideDialog
256 {
257     msg_Dbg( p_intf, "hide event" );
258     if( p_dialog->i_flags & DIALOG_USER_PROGRESS )
259     {
260         [NSApp endSheet: o_prog_win];
261         [o_prog_win close];
262     }
263     if( p_dialog->i_flags & DIALOG_LOGIN_PW_OK_CANCEL )
264     {
265         [NSApp endSheet: o_auth_win];
266         [o_auth_win close];
267     }
268     if( p_dialog->i_flags & DIALOG_PSZ_INPUT_OK_CANCEL )
269     {
270         [NSApp endSheet: o_input_win];
271         [o_input_win close];
272     }
273 }
274
275 -(void)destroyDialog
276 {
277     msg_Dbg( p_intf, "destroy event" );
278 }
279
280 - (IBAction)cancelAndClose:(id)sender
281 {
282     /* tell the core that the dialog was cancelled */
283     vlc_mutex_lock( &p_dialog->p_interaction->object_lock );
284     p_dialog->i_return = DIALOG_CANCELLED;
285     p_dialog->i_status = ANSWERED_DIALOG;
286     vlc_mutex_unlock( &p_dialog->p_interaction->object_lock );
287     msg_Dbg( p_intf, "dialog cancelled" );
288 }
289
290 - (IBAction)okayAndClose:(id)sender
291 {
292     msg_Dbg( p_intf, "dialog's okay btn pressed, returning values" );
293     vlc_mutex_lock( &p_dialog->p_interaction->object_lock );
294     if( p_dialog->i_flags == DIALOG_LOGIN_PW_OK_CANCEL )
295     {
296         p_dialog->psz_returned[0] = strdup( [[o_auth_login_fld stringValue] UTF8String] );
297         p_dialog->psz_returned[1] = strdup( [[o_auth_pw_fld stringValue] UTF8String] );
298     }
299     else if( p_dialog->i_flags == DIALOG_PSZ_INPUT_OK_CANCEL )
300         p_dialog->psz_returned[0] = strdup( [[o_input_fld stringValue] UTF8String] );
301     p_dialog->i_return = DIALOG_OK_YES;
302     p_dialog->i_status = ANSWERED_DIALOG;
303     vlc_mutex_unlock( &p_dialog->p_interaction->object_lock );
304 }
305
306 @end