1 /*****************************************************************************
2 * interaction.h: Mac OS X interaction dialogs
3 *****************************************************************************
4 * Copyright (C) 2005-2006 the VideoLAN team
7 * Authors: Derk-Jan Hartman <hartman at videolan dot org>
8 * Felix Kühne <fkuehne at videolan dot org>
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.
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.
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 *****************************************************************************/
26 #import "interaction.h"
28 /*****************************************************************************
29 * VLCInteractionList implementation
30 *****************************************************************************/
31 @implementation VLCInteractionList
36 o_interaction_list = [[NSMutableArray alloc] initWithCapacity:1];
37 [[NSNotificationCenter defaultCenter] addObserver:self
38 selector:@selector(newInteractionEvent:)
39 name: @"VLCNewInteractionEventNotification"
45 -(void)newInteractionEvent: (NSNotification *)o_notification
47 VLCInteraction *o_interaction;
48 NSValue *o_value = [[o_notification userInfo] objectForKey:@"VLCDialogPointer"];
49 interaction_dialog_t *p_dialog = [o_value pointerValue];
51 switch( p_dialog->i_action )
54 [self addInteraction: p_dialog];
57 o_interaction = (VLCInteraction *)p_dialog->p_private;
58 [o_interaction updateDialog];
61 o_interaction = (VLCInteraction *)p_dialog->p_private;
62 [o_interaction hideDialog];
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;
73 -(void)addInteraction: (interaction_dialog_t *)p_dialog
76 VLCInteraction *o_interaction = [[VLCInteraction alloc] initDialog: p_dialog];
78 p_dialog->p_private = (void *)o_interaction;
79 [o_interaction_list addObject:[o_interaction autorelease]];
80 [o_interaction runDialog];
83 -(void)removeInteraction: (VLCInteraction *)o_interaction
85 [o_interaction_list removeObject:o_interaction];
90 [[NSNotificationCenter defaultCenter] removeObserver:self];
91 [o_interaction_list removeAllObjects];
92 [o_interaction_list release];
98 /*****************************************************************************
99 * VLCInteraction implementation
100 *****************************************************************************/
101 @implementation VLCInteraction
103 -(id)initDialog: (interaction_dialog_t *)_p_dialog
107 p_dialog = _p_dialog;
115 msg_Err( p_intf, "no available interaction framework" );
117 if( !nib_interact_loaded )
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];
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 NSString *o_defaultButton = [NSString stringWithUTF8String:p_dialog->psz_defaultButton];
134 NSString *o_alternateButton = [NSString stringWithUTF8String:p_dialog->psz_alternateButton];
135 NSString *o_otherButton = p_dialog->psz_otherButton ? [NSString stringWithUTF8String:p_dialog->psz_otherButton] : nil;
137 vout_thread_t *p_vout = vlc_object_find( VLCIntf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
140 NSEnumerator * o_enum = [[NSApp orderedWindows] objectEnumerator];
142 while( ( o_window = [o_enum nextObject] ) )
144 if( [[o_window className] isEqualToString: @"VLCWindow"] )
146 vlc_object_release( (vlc_object_t *)p_vout );
150 vlc_object_release( (vlc_object_t *)p_vout );
154 o_window = [NSApp mainWindow];
158 msg_Dbg( p_intf, "Title: %s", [o_title UTF8String] );
159 msg_Dbg( p_intf, "Description: %s", [o_description UTF8String] );
162 if( p_dialog->i_id == DIALOG_ERRORS )
164 msg_Dbg( p_intf, "error panel requested" );
165 NSAlert * ourAlert = [NSAlert alertWithMessageText:
166 [NSString stringWithUTF8String:p_dialog->psz_title ? p_dialog->psz_title : _("Error")]
167 defaultButton: _NS("OK") alternateButton: nil otherButton: nil
168 informativeTextWithFormat:
169 [NSString stringWithUTF8String:p_dialog->psz_description]];
170 [ourAlert setAlertStyle: NSWarningAlertStyle];
175 if( p_dialog->i_flags & DIALOG_YES_NO_CANCEL )
177 msg_Dbg( p_intf, "yes-no-cancel-dialog requested" );
178 NSBeginInformationalAlertSheet( o_title, o_defaultButton,
179 o_alternateButton, o_otherButton, o_window, self,
180 @selector(sheetDidEnd: returnCode: contextInfo:), NULL, nil,
183 else if( p_dialog->i_flags & DIALOG_LOGIN_PW_OK_CANCEL )
185 msg_Dbg( p_intf, "dialog for login and pw requested" );
186 [o_auth_title setStringValue: o_title];
187 [o_auth_description setStringValue: o_description];
188 [o_auth_login_fld setStringValue: @""];
189 [o_auth_pw_fld setStringValue: @""];
190 [NSApp beginSheet: o_auth_win modalForWindow: o_window
191 modalDelegate: self didEndSelector: nil contextInfo: nil];
192 [o_auth_win makeKeyWindow];
194 else if( p_dialog->i_flags & DIALOG_USER_PROGRESS )
196 msg_Dbg( p_intf, "user progress dialog requested" );
197 [o_prog_title setStringValue: o_title];
198 [o_prog_description setStringValue: o_description];
199 [o_prog_bar setDoubleValue: (double)p_dialog->val.f_float];
200 if( p_dialog->i_timeToGo < 1 )
201 [o_prog_timeToGo setStringValue: @""];
203 [o_prog_timeToGo setStringValue: [NSString stringWithFormat:
204 _NS("Remaining time: %i seconds"), p_dialog->i_timeToGo]];
205 [NSApp beginSheet: o_prog_win modalForWindow: o_window
206 modalDelegate: self didEndSelector: nil contextInfo: nil];
207 [o_prog_win makeKeyWindow];
209 else if( p_dialog->i_flags & DIALOG_PSZ_INPUT_OK_CANCEL )
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];
219 else if( p_dialog->i_flags & DIALOG_INTF_PROGRESS )
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];
229 msg_Err( p_intf, "requested dialog type unknown (%i)",
234 - (void)sheetDidEnd:(NSWindow *)o_sheet returnCode:(int)i_return
235 contextInfo:(void *)o_context
237 vlc_mutex_lock( &p_dialog->p_interaction->object_lock );
238 if( i_return == NSAlertDefaultReturn )
240 p_dialog->i_return = DIALOG_OK_YES;
242 else if( i_return == NSAlertAlternateReturn )
244 p_dialog->i_return = DIALOG_NO;
246 else if( i_return == NSAlertOtherReturn )
248 p_dialog->i_return = DIALOG_CANCELLED;
250 p_dialog->i_status = ANSWERED_DIALOG;
251 vlc_mutex_unlock( &p_dialog->p_interaction->object_lock );
256 if( p_dialog->i_flags & DIALOG_USER_PROGRESS )
258 [o_prog_description setStringValue: \
259 [NSString stringWithUTF8String: p_dialog->psz_description]];
260 [o_prog_bar setDoubleValue: (double)p_dialog->val.f_float];
262 if( [o_prog_bar doubleValue] == 100.0 )
264 /* we are done, let's hide */
268 if( p_dialog->i_timeToGo < 1 )
269 [o_prog_timeToGo setStringValue: @""];
271 [o_prog_timeToGo setStringValue: [NSString stringWithFormat:
272 _NS("Remaining time: %i seconds"), p_dialog->i_timeToGo]];
276 if( p_dialog->i_flags & DIALOG_INTF_PROGRESS )
278 [[VLCMain sharedInstance] setScrollField:
279 [NSString stringWithUTF8String: p_dialog->psz_description]
281 [o_mainIntfPgbar setDoubleValue: (double)p_dialog->val.f_float];
283 if( [o_mainIntfPgbar doubleValue] == 100.0 )
285 /* we are done, let's hide */
294 msg_Dbg( p_intf, "hide event" );
295 if( p_dialog->i_flags & DIALOG_USER_PROGRESS )
297 [NSApp endSheet: o_prog_win];
300 if( p_dialog->i_flags & DIALOG_LOGIN_PW_OK_CANCEL )
302 [NSApp endSheet: o_auth_win];
305 if( p_dialog->i_flags & DIALOG_PSZ_INPUT_OK_CANCEL )
307 [NSApp endSheet: o_input_win];
310 if( p_dialog->i_flags & DIALOG_INTF_PROGRESS )
312 [o_mainIntfPgbar setIndeterminate: YES];
313 [o_mainIntfPgbar setHidden: YES];
314 [[VLCMain sharedInstance] resetScrollField];
320 msg_Dbg( p_intf, "destroy event" );
321 if( o_mainIntfPgbar )
322 [o_mainIntfPgbar release];
325 - (IBAction)cancelAndClose:(id)sender
327 /* tell the core that the dialog was cancelled in a yes/no-style dialogue */
328 vlc_mutex_lock( &p_dialog->p_interaction->object_lock );
329 p_dialog->i_return = DIALOG_CANCELLED;
330 p_dialog->i_status = ANSWERED_DIALOG;
331 vlc_mutex_unlock( &p_dialog->p_interaction->object_lock );
332 msg_Dbg( p_intf, "dialog cancelled" );
335 - (IBAction)cancelDialog:(id)sender
337 /* tell core that the user wishes to cancel the dialogue
338 * Use this function if cancelling is optionally like in the progress-dialogue */
339 vlc_mutex_lock( &p_dialog->p_interaction->object_lock );
340 p_dialog->b_cancelled = VLC_TRUE;
341 vlc_mutex_unlock( &p_dialog->p_interaction->object_lock );
342 msg_Dbg( p_intf, "cancelling dialog, will close it later on" );
345 - (IBAction)okayAndClose:(id)sender
347 vlc_mutex_lock( &p_dialog->p_interaction->object_lock );
348 if( p_dialog->i_flags == DIALOG_LOGIN_PW_OK_CANCEL )
350 p_dialog->psz_returned[0] = strdup( [[o_auth_login_fld stringValue] UTF8String] );
351 p_dialog->psz_returned[1] = strdup( [[o_auth_pw_fld stringValue] UTF8String] );
353 else if( p_dialog->i_flags == DIALOG_PSZ_INPUT_OK_CANCEL )
354 p_dialog->psz_returned[0] = strdup( [[o_input_fld stringValue] UTF8String] );
355 p_dialog->i_return = DIALOG_OK_YES;
356 p_dialog->i_status = ANSWERED_DIALOG;
357 vlc_mutex_unlock( &p_dialog->p_interaction->object_lock );
358 msg_Dbg( p_intf, "dialog acknowledged" );