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 /* for the icons in our custom error panel */
29 #import <ApplicationServices/ApplicationServices.h>
31 /*****************************************************************************
32 * VLCInteractionList implementation
33 *****************************************************************************/
34 @implementation VLCInteractionList
39 o_interaction_list = [[NSMutableArray alloc] initWithCapacity:1];
40 [[NSNotificationCenter defaultCenter] addObserver:self
41 selector:@selector(newInteractionEvent:)
42 name: @"VLCNewInteractionEventNotification"
45 o_error_panel = [[VLCErrorInteractionPanel alloc] init];
50 -(void)newInteractionEvent: (NSNotification *)o_notification
52 VLCInteraction *o_interaction;
53 NSValue *o_value = [[o_notification userInfo] objectForKey:@"VLCDialogPointer"];
54 interaction_dialog_t *p_dialog = [o_value pointerValue];
56 switch( p_dialog->i_action )
59 [self addInteraction: p_dialog];
62 o_interaction = (VLCInteraction *)p_dialog->p_private;
63 [o_interaction updateDialog];
66 o_interaction = (VLCInteraction *)p_dialog->p_private;
67 [o_interaction hideDialog];
69 case INTERACT_DESTROY:
70 o_interaction = (VLCInteraction *)p_dialog->p_private;
71 [o_interaction destroyDialog];
72 [self removeInteraction:o_interaction];
73 p_dialog->i_status = DESTROYED_DIALOG;
78 -(void)addInteraction: (interaction_dialog_t *)p_dialog
80 VLCInteraction *o_interaction = [[VLCInteraction alloc] initDialog: p_dialog];
82 p_dialog->p_private = (void *)o_interaction;
83 [o_interaction_list addObject:[o_interaction autorelease]];
84 [o_interaction runDialog];
87 -(void)removeInteraction: (VLCInteraction *)o_interaction
89 [o_interaction_list removeObject:o_interaction];
99 [[NSNotificationCenter defaultCenter] removeObserver:self];
100 [o_interaction_list removeAllObjects];
101 [o_interaction_list release];
106 /*****************************************************************************
107 * VLCInteraction implementation
108 *****************************************************************************/
109 @implementation VLCInteraction
111 -(id)initDialog: (interaction_dialog_t *)_p_dialog
115 p_dialog = _p_dialog;
123 msg_Err( p_intf, "no available interaction framework" );
125 if( !nib_interact_loaded )
127 nib_interact_loaded = [NSBundle loadNibNamed:@"Interaction" owner:self];
128 [o_prog_cancel_btn setTitle: _NS("Cancel")];
129 [o_prog_bar setUsesThreadedAnimation: YES];
130 [o_auth_login_txt setStringValue: _NS("Login:")];
131 [o_auth_pw_txt setStringValue: _NS("Password:")];
132 [o_auth_cancel_btn setTitle: _NS("Cancel")];
133 [o_auth_ok_btn setTitle: _NS("OK")];
134 [o_input_ok_btn setTitle: _NS("OK")];
135 [o_input_cancel_btn setTitle: _NS("Cancel")];
136 o_mainIntfPgbar = [[VLCMain sharedInstance] getMainIntfPgbar];
139 NSString *o_title = [NSString stringWithUTF8String:p_dialog->psz_title ? p_dialog->psz_title : _("Error")];
140 NSString *o_description = [NSString stringWithUTF8String:p_dialog->psz_description ? p_dialog->psz_description : ""];
141 NSString *o_defaultButton = p_dialog->psz_default_button ? [NSString stringWithUTF8String:p_dialog->psz_default_button] : nil;
142 NSString *o_alternateButton = p_dialog->psz_alternate_button ? [NSString stringWithUTF8String:p_dialog->psz_alternate_button] : nil;
143 NSString *o_otherButton = p_dialog->psz_other_button ? [NSString stringWithUTF8String:p_dialog->psz_other_button] : nil;
145 vout_thread_t *p_vout = vlc_object_find( VLCIntf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
148 NSEnumerator * o_enum = [[NSApp orderedWindows] objectEnumerator];
150 while( ( o_window = [o_enum nextObject] ) )
152 if( [[o_window className] isEqualToString: @"VLCWindow"] )
154 vlc_object_release( (vlc_object_t *)p_vout );
158 vlc_object_release( (vlc_object_t *)p_vout );
162 o_window = [NSApp mainWindow];
166 msg_Dbg( p_intf, "Title: %s", [o_title UTF8String] );
167 msg_Dbg( p_intf, "Description: %s", [o_description UTF8String] );
168 msg_Dbg( p_intf, "Delivered flag: %i", p_dialog->i_flags );
171 if( p_dialog->i_flags & DIALOG_BLOCKING_ERROR )
173 msg_Dbg( p_intf, "error panel requested" );
174 NSBeginInformationalAlertSheet( o_title, _NS("OK"), nil, nil,
175 o_window, self, @selector(sheetDidEnd: returnCode: contextInfo:),
176 NULL, nil, o_description );
178 else if( p_dialog->i_flags & DIALOG_NONBLOCKING_ERROR )
180 msg_Dbg( p_intf, "addition to non-blocking error panel received" );
181 [[[[VLCMain sharedInstance] getInteractionList] getErrorPanel]
182 addError: o_title withMsg: o_description];
184 else if( p_dialog->i_flags & DIALOG_WARNING )
186 msg_Dbg( p_intf, "addition to non-blocking warning panel received" );
187 [[[[VLCMain sharedInstance] getInteractionList] getErrorPanel]
188 addWarning: o_title withMsg: o_description];
190 else if( p_dialog->i_flags & DIALOG_YES_NO_CANCEL )
192 msg_Dbg( p_intf, "yes-no-cancel-dialog requested" );
193 NSBeginInformationalAlertSheet( o_title, o_defaultButton,
194 o_alternateButton, o_otherButton, o_window, self,
195 @selector(sheetDidEnd: returnCode: contextInfo:), NULL, nil,
198 else if( p_dialog->i_flags & DIALOG_LOGIN_PW_OK_CANCEL )
200 msg_Dbg( p_intf, "dialog for login and pw requested" );
201 [o_auth_title setStringValue: o_title];
202 [o_auth_description setStringValue: o_description];
203 [o_auth_login_fld setStringValue: @""];
204 [o_auth_pw_fld setStringValue: @""];
205 [NSApp beginSheet: o_auth_win modalForWindow: o_window
206 modalDelegate: self didEndSelector: nil contextInfo: nil];
207 [o_auth_win makeKeyWindow];
209 else if( p_dialog->i_flags & DIALOG_USER_PROGRESS )
211 msg_Dbg( p_intf, "user progress dialog requested" );
212 [o_prog_title setStringValue: o_title];
213 [o_prog_description setStringValue: o_description];
214 [o_prog_bar setDoubleValue: (double)p_dialog->val.f_float];
215 if( p_dialog->i_timeToGo < 1 )
216 [o_prog_timeToGo setStringValue: @""];
218 [o_prog_timeToGo setStringValue: [NSString stringWithFormat:
219 _NS("Remaining time: %i seconds"), p_dialog->i_timeToGo]];
220 [NSApp beginSheet: o_prog_win modalForWindow: o_window
221 modalDelegate: self didEndSelector: nil contextInfo: nil];
222 [o_prog_win makeKeyWindow];
224 else if( p_dialog->i_flags & DIALOG_PSZ_INPUT_OK_CANCEL )
226 msg_Dbg( p_intf, "text input from user requested" );
227 [o_input_title setStringValue: o_title];
228 [o_input_description setStringValue: o_description];
229 [o_input_fld setStringValue: @""];
230 [NSApp beginSheet: o_input_win modalForWindow: o_window
231 modalDelegate: self didEndSelector: nil contextInfo: nil];
232 [o_input_win makeKeyWindow];
234 else if( p_dialog->i_flags & DIALOG_INTF_PROGRESS )
236 msg_Dbg( p_intf, "progress-bar in main intf requested" );
237 [[VLCMain sharedInstance] setScrollField: o_description stopAfter: -1];
238 [o_mainIntfPgbar setDoubleValue: (double)p_dialog->val.f_float];
239 [o_mainIntfPgbar setHidden: NO];
240 [[[VLCMain sharedInstance] getControllerWindow] makeKeyWindow];
241 [o_mainIntfPgbar setIndeterminate: NO];
244 msg_Err( p_intf, "requested dialog type unknown (%i)", p_dialog->i_flags );
247 - (void)sheetDidEnd:(NSWindow *)o_sheet returnCode:(int)i_return
248 contextInfo:(void *)o_context
250 vlc_mutex_lock( &p_dialog->p_interaction->object_lock );
251 if( i_return == NSAlertDefaultReturn )
253 p_dialog->i_return = DIALOG_OK_YES;
255 else if( i_return == NSAlertAlternateReturn )
257 p_dialog->i_return = DIALOG_NO;
259 else if( i_return == NSAlertOtherReturn )
261 p_dialog->i_return = DIALOG_CANCELLED;
263 p_dialog->i_status = ANSWERED_DIALOG;
264 vlc_mutex_unlock( &p_dialog->p_interaction->object_lock );
269 if( p_dialog->i_flags & DIALOG_USER_PROGRESS )
271 [o_prog_description setStringValue: \
272 [NSString stringWithUTF8String: p_dialog->psz_description]];
273 [o_prog_bar setDoubleValue: (double)p_dialog->val.f_float];
275 if( [o_prog_bar doubleValue] == 100.0 )
277 /* we are done, let's hide */
281 if( p_dialog->i_timeToGo < 1 )
282 [o_prog_timeToGo setStringValue: @""];
284 [o_prog_timeToGo setStringValue: [NSString stringWithFormat:
285 _NS("Remaining time: %i seconds"), p_dialog->i_timeToGo]];
289 if( p_dialog->i_flags & DIALOG_INTF_PROGRESS )
291 [[VLCMain sharedInstance] setScrollField:
292 [NSString stringWithUTF8String: p_dialog->psz_description]
294 [o_mainIntfPgbar setDoubleValue: (double)p_dialog->val.f_float];
296 if( [o_mainIntfPgbar doubleValue] == 100.0 )
298 /* we are done, let's hide */
307 msg_Dbg( p_intf, "hide event" );
308 if( p_dialog->i_flags & DIALOG_USER_PROGRESS )
310 [NSApp endSheet: o_prog_win];
313 if( p_dialog->i_flags & DIALOG_LOGIN_PW_OK_CANCEL )
315 [NSApp endSheet: o_auth_win];
318 if( p_dialog->i_flags & DIALOG_PSZ_INPUT_OK_CANCEL )
320 [NSApp endSheet: o_input_win];
323 if( p_dialog->i_flags & DIALOG_INTF_PROGRESS )
325 [o_mainIntfPgbar setIndeterminate: YES];
326 [o_mainIntfPgbar setHidden: YES];
327 [[VLCMain sharedInstance] resetScrollField];
333 msg_Dbg( p_intf, "destroy event" );
334 if( o_mainIntfPgbar )
335 [o_mainIntfPgbar release];
338 - (IBAction)cancelAndClose:(id)sender
340 /* tell the core that the dialog was cancelled in a yes/no-style dialogue */
341 vlc_mutex_lock( &p_dialog->p_interaction->object_lock );
342 p_dialog->i_return = DIALOG_CANCELLED;
343 p_dialog->i_status = ANSWERED_DIALOG;
344 vlc_mutex_unlock( &p_dialog->p_interaction->object_lock );
345 msg_Dbg( p_intf, "dialog cancelled" );
348 - (IBAction)cancelDialog:(id)sender
350 /* tell core that the user wishes to cancel the dialogue
351 * Use this function if cancelling is optionally like in the progress-dialogue */
352 vlc_mutex_lock( &p_dialog->p_interaction->object_lock );
353 p_dialog->b_cancelled = VLC_TRUE;
354 vlc_mutex_unlock( &p_dialog->p_interaction->object_lock );
355 msg_Dbg( p_intf, "cancelling dialog, will close it later on" );
358 - (IBAction)okayAndClose:(id)sender
360 msg_Dbg( p_intf, "running okayAndClose" );
361 vlc_mutex_lock( &p_dialog->p_interaction->object_lock );
362 if( p_dialog->i_flags == DIALOG_LOGIN_PW_OK_CANCEL )
364 p_dialog->psz_returned[0] = strdup( [[o_auth_login_fld stringValue] UTF8String] );
365 p_dialog->psz_returned[1] = strdup( [[o_auth_pw_fld stringValue] UTF8String] );
367 else if( p_dialog->i_flags == DIALOG_PSZ_INPUT_OK_CANCEL )
368 p_dialog->psz_returned[0] = strdup( [[o_input_fld stringValue] UTF8String] );
369 p_dialog->i_return = DIALOG_OK_YES;
370 p_dialog->i_status = ANSWERED_DIALOG;
371 vlc_mutex_unlock( &p_dialog->p_interaction->object_lock );
372 msg_Dbg( p_intf, "dialog acknowledged" );
377 /*****************************************************************************
378 * VLCErrorInteractionPanel implementation
379 *****************************************************************************/
380 @implementation VLCErrorInteractionPanel
386 nib_interact_errpanel_loaded = [NSBundle loadNibNamed:@"InteractionErrorPanel" owner:self];
389 [o_window setTitle: _NS("Errors and Warnings")];
390 [o_cleanup_button setTitle: _NS("Clean up")];
391 [o_messages_btn setTitle: _NS("Show Details")];
393 /* init data sources */
394 o_errors = [[NSMutableArray alloc] init];
395 o_icons = [[NSMutableArray alloc] init];
397 /* ugly Carbon stuff following...
398 * regrettably, you can't get the icons through clean Cocoa */
400 /* retrieve our error icon */
403 returnValue = GetIconRef(kOnSystemDisk, 'macs', 'stop', &ourIconRef);
404 errorIcon = [[NSImage alloc] initWithSize:NSMakeSize(32,32)];
405 [errorIcon lockFocus];
406 CGRect rect = CGRectMake(0,0,32,32);
407 PlotIconRefInContext((CGContextRef)[[NSGraphicsContext currentContext]
412 NULL /*inLabelColor*/,
413 kPlotIconRefNormalFlags,
414 (IconRef)ourIconRef);
415 [errorIcon unlockFocus];
416 returnValue = ReleaseIconRef(ourIconRef);
418 /* retrieve our caution icon */
419 returnValue = GetIconRef(kOnSystemDisk, 'macs', 'caut', &ourIconRef);
420 warnIcon = [[NSImage alloc] initWithSize:NSMakeSize(32,32)];
421 [warnIcon lockFocus];
422 PlotIconRefInContext((CGContextRef)[[NSGraphicsContext currentContext]
427 NULL /*inLabelColor*/,
428 kPlotIconRefNormalFlags,
429 (IconRef)ourIconRef);
430 [warnIcon unlockFocus];
431 returnValue = ReleaseIconRef(ourIconRef);
447 [o_window makeKeyAndOrderFront: self];
450 -(void)addError: (NSString *)o_error withMsg:(NSString *)o_msg
452 /* format our string as desired */
453 NSMutableAttributedString * ourError;
454 ourError = [[NSMutableAttributedString alloc] initWithString:
455 [NSString stringWithFormat:@"%@\n%@", o_error, o_msg]
457 [NSDictionary dictionaryWithObject: [NSFont systemFontOfSize:11] forKey: NSFontAttributeName]];
459 addAttribute: NSFontAttributeName
460 value: [NSFont boldSystemFontOfSize:11]
461 range: NSMakeRange( 0, [o_error length])];
462 [o_errors addObject: ourError];
465 [o_icons addObject: errorIcon];
467 [o_error_table reloadData];
471 -(void)addWarning: (NSString *)o_warning withMsg:(NSString *)o_msg
473 /* format our string as desired */
474 NSMutableAttributedString * ourWarning;
475 ourWarning = [[NSMutableAttributedString alloc] initWithString:
476 [NSString stringWithFormat:@"%@\n%@", o_warning, o_msg]
478 [NSDictionary dictionaryWithObject: [NSFont systemFontOfSize:11] forKey: NSFontAttributeName]];
480 addAttribute: NSFontAttributeName
481 value: [NSFont boldSystemFontOfSize:11]
482 range: NSMakeRange( 0, [o_warning length])];
483 [o_errors addObject: ourWarning];
484 [ourWarning release];
486 [o_icons addObject: warnIcon];
488 [o_error_table reloadData];
493 -(IBAction)cleanupTable:(id)sender
495 [o_errors removeAllObjects];
496 [o_icons removeAllObjects];
497 [o_error_table reloadData];
500 -(IBAction)showMessages:(id)sender
502 [[VLCMain sharedInstance] showMessagesPanel: sender];
505 /*----------------------------------------------------------------------------
506 * data source methods
507 *---------------------------------------------------------------------------*/
508 - (int)numberOfRowsInTableView:(NSTableView *)theDataTable
510 return [o_errors count];
513 - (id)tableView:(NSTableView *)theDataTable objectValueForTableColumn:
514 (NSTableColumn *)theTableColumn row: (int)row
516 if( [[theTableColumn identifier] isEqualToString: @"error_msg"] )
517 return [o_errors objectAtIndex: row];
519 if( [[theTableColumn identifier] isEqualToString: @"icon"] )
520 return [o_icons objectAtIndex: row];
522 return @"unknown identifier";