]> git.sesse.net Git - vlc/blob - modules/gui/macosx/interaction.m
82275cb5c0d57f5a35438cfa47fda118136ef02b
[vlc] / modules / gui / macosx / interaction.m
1 /*****************************************************************************
2  * interaction.h: Mac OS X interaction dialogs
3  *****************************************************************************
4  * Copyright (C) 2005-2007 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 /* for the icons in our custom error panel */
29 #import <ApplicationServices/ApplicationServices.h> 
30
31 /*****************************************************************************
32  * VLCInteractionList implementation
33  *****************************************************************************/
34 @implementation VLCInteractionList
35
36 -(id)init
37 {
38     [super init];
39     o_interaction_list = [[NSMutableArray alloc] initWithCapacity:1];
40     [[NSNotificationCenter defaultCenter] addObserver:self
41         selector:@selector(newInteractionEvent:)
42         name: @"VLCNewInteractionEventNotification"
43         object:self];
44
45     o_error_panel = [[VLCErrorInteractionPanel alloc] init];
46
47     return self;
48 }
49
50 -(void)newInteractionEvent: (NSNotification *)o_notification
51 {
52     VLCInteraction *o_interaction;
53     NSValue *o_value = [[o_notification userInfo] objectForKey:@"VLCDialogPointer"];
54     interaction_dialog_t *p_dialog = [o_value pointerValue];
55
56     switch( p_dialog->i_action )
57     {
58     case INTERACT_NEW:
59         [self addInteraction: p_dialog];
60         break;
61     case INTERACT_UPDATE:
62         o_interaction = (VLCInteraction *)p_dialog->p_private;
63         [o_interaction updateDialog];
64         break;
65     case INTERACT_HIDE:
66         o_interaction = (VLCInteraction *)p_dialog->p_private;
67         [o_interaction hideDialog];
68         break;
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;
74         break;
75     }
76 }
77
78 -(void)addInteraction: (interaction_dialog_t *)p_dialog
79 {
80     VLCInteraction *o_interaction = [[VLCInteraction alloc] initDialog: p_dialog];
81     
82     p_dialog->p_private = (void *)o_interaction;
83     [o_interaction_list addObject:[o_interaction autorelease]];
84     [o_interaction runDialog];
85 }
86
87 -(void)removeInteraction: (VLCInteraction *)o_interaction
88 {
89     [o_interaction_list removeObject:o_interaction];
90 }
91
92 -(id)getErrorPanel
93 {
94     return o_error_panel;
95 }
96
97 #if GC_ENABLED
98 -(void)finalize
99 {
100         /* dealloc doesn't get called on 10.5 if GC is enabled, so we need to provide the basic functionality here */
101         [[NSNotificationCenter defaultCenter] removeObserver:self];
102         [o_interaction_list removeAllObjects];
103     [super finalize];
104 }
105 #endif
106
107 -(void)dealloc
108 {
109     [[NSNotificationCenter defaultCenter] removeObserver:self];
110     [o_interaction_list removeAllObjects];
111     [o_interaction_list release];
112     [super dealloc];
113 }
114 @end
115
116 /*****************************************************************************
117  * VLCInteraction implementation
118  *****************************************************************************/
119 @implementation VLCInteraction
120
121 -(id)initDialog: (interaction_dialog_t *)_p_dialog
122 {
123     p_intf = VLCIntf;
124     [super init];
125     p_dialog = _p_dialog;
126     return self;
127 }
128
129 -(void)runDialog
130 {
131     id o_window = NULL;
132     if( !p_dialog )
133         msg_Err( p_intf, "no available interaction framework" );
134
135     if( !nib_interact_loaded )
136     {
137         nib_interact_loaded = [NSBundle loadNibNamed:@"Interaction" owner:self];
138         [o_prog_cancel_btn setTitle: _NS("Cancel")];
139         [o_prog_bar setUsesThreadedAnimation: YES];
140         [o_auth_login_txt setStringValue: _NS("Login:")];
141         [o_auth_pw_txt setStringValue: _NS("Password:")];
142         [o_auth_cancel_btn setTitle: _NS("Cancel")];
143         [o_auth_ok_btn setTitle: _NS("OK")];
144         [o_input_ok_btn setTitle: _NS("OK")];
145         [o_input_cancel_btn setTitle: _NS("Cancel")];
146         o_mainIntfPgbar = [[VLCMain sharedInstance] getMainIntfPgbar];
147     }
148
149     NSString *o_title = [NSString stringWithUTF8String:p_dialog->psz_title ? p_dialog->psz_title : _("Error")];
150     NSString *o_description = [NSString stringWithUTF8String:p_dialog->psz_description ? p_dialog->psz_description : ""];
151     NSString *o_defaultButton = p_dialog->psz_default_button ? [NSString stringWithUTF8String:p_dialog->psz_default_button] : nil;
152     NSString *o_alternateButton = p_dialog->psz_alternate_button ? [NSString stringWithUTF8String:p_dialog->psz_alternate_button] : nil;
153     NSString *o_otherButton = p_dialog->psz_other_button ? [NSString stringWithUTF8String:p_dialog->psz_other_button] : nil;
154
155     vout_thread_t *p_vout = vlc_object_find( VLCIntf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
156     if( p_vout != NULL )
157     {
158         NSEnumerator * o_enum = [[NSApp orderedWindows] objectEnumerator];
159
160         while( ( o_window = [o_enum nextObject] ) )
161         {
162             if( [[o_window className] isEqualToString: @"VLCVoutWindow"] )
163             {
164                 vlc_object_release( (vlc_object_t *)p_vout );
165                 break;
166             }
167         }
168         vlc_object_release( (vlc_object_t *)p_vout );
169     }
170     else
171     {
172         o_window = [NSApp mainWindow];
173     }
174
175 #if 0
176     msg_Dbg( p_intf, "Title: %s", [o_title UTF8String] );
177     msg_Dbg( p_intf, "Description: %s", [o_description UTF8String] );
178     msg_Dbg( p_intf, "Delivered flag: %i", p_dialog->i_flags );
179 #endif
180
181     if( p_dialog->i_flags & DIALOG_BLOCKING_ERROR )
182     {
183         msg_Dbg( p_intf, "error panel requested" );
184         NSBeginInformationalAlertSheet( o_title, _NS("OK"), nil, nil, 
185             o_window, self, @selector(sheetDidEnd: returnCode: contextInfo:), 
186             NULL, nil, o_description );
187     }
188     else if( p_dialog->i_flags & DIALOG_NONBLOCKING_ERROR )
189     {
190         msg_Dbg( p_intf, "addition to non-blocking error panel received" );
191         [[[[VLCMain sharedInstance] getInteractionList] getErrorPanel] 
192         addError: o_title withMsg: o_description];
193     }
194     else if( p_dialog->i_flags & DIALOG_WARNING )
195     {
196         msg_Dbg( p_intf, "addition to non-blocking warning panel received" );
197         [[[[VLCMain sharedInstance] getInteractionList] getErrorPanel] 
198             addWarning: o_title withMsg: o_description];
199     }
200     else if( p_dialog->i_flags & DIALOG_YES_NO_CANCEL )
201     {
202         msg_Dbg( p_intf, "yes-no-cancel-dialog requested" );
203         NSBeginInformationalAlertSheet( o_title, o_defaultButton, 
204             o_alternateButton, o_otherButton, o_window, self,
205             @selector(sheetDidEnd: returnCode: contextInfo:), NULL, nil, 
206             o_description );
207     }
208     else if( p_dialog->i_flags & DIALOG_LOGIN_PW_OK_CANCEL )
209     {
210         msg_Dbg( p_intf, "dialog for login and pw requested" );
211         [o_auth_title setStringValue: o_title];
212         [o_auth_description setStringValue: o_description];
213         [o_auth_login_fld setStringValue: @""];
214         [o_auth_pw_fld setStringValue: @""];
215         [NSApp beginSheet: o_auth_win modalForWindow: o_window
216             modalDelegate: self didEndSelector: nil contextInfo: nil];
217         [o_auth_win makeKeyWindow];
218     }
219     else if( p_dialog->i_flags & DIALOG_USER_PROGRESS )
220     {
221         msg_Dbg( p_intf, "user progress dialog requested" );
222         [o_prog_title setStringValue: o_title];
223         [o_prog_description setStringValue: o_description];
224         [o_prog_bar setDoubleValue: (double)p_dialog->val.f_float];
225         if( p_dialog->i_timeToGo < 1 )
226             [o_prog_timeToGo setStringValue: @""];
227         else
228             [o_prog_timeToGo setStringValue: [NSString stringWithFormat:
229                 _NS("Remaining time: %i seconds"), p_dialog->i_timeToGo]];
230         [NSApp beginSheet: o_prog_win modalForWindow: o_window
231             modalDelegate: self didEndSelector: nil contextInfo: nil];
232         [o_prog_win makeKeyWindow];
233     }
234     else if( p_dialog->i_flags & DIALOG_PSZ_INPUT_OK_CANCEL )
235     {
236         msg_Dbg( p_intf, "text input from user requested" );
237         [o_input_title setStringValue: o_title];
238         [o_input_description setStringValue: o_description];
239         [o_input_fld setStringValue: @""];
240         [NSApp beginSheet: o_input_win modalForWindow: o_window
241             modalDelegate: self didEndSelector: nil contextInfo: nil];
242         [o_input_win makeKeyWindow];
243     }
244     else if( p_dialog->i_flags & DIALOG_INTF_PROGRESS )
245     {
246         msg_Dbg( p_intf, "progress-bar in main intf requested" );
247         [[VLCMain sharedInstance] setScrollField: o_description stopAfter: -1];
248         [o_mainIntfPgbar setDoubleValue: (double)p_dialog->val.f_float];
249         [o_mainIntfPgbar setHidden: NO];
250         [[[VLCMain sharedInstance] getControllerWindow] makeKeyWindow];
251         [o_mainIntfPgbar setIndeterminate: NO];
252     }
253     else
254         msg_Err( p_intf, "requested dialog type unknown (%i)", p_dialog->i_flags );
255 }
256
257 - (void)sheetDidEnd:(NSWindow *)o_sheet returnCode:(int)i_return
258     contextInfo:(void *)o_context
259 {
260     vlc_mutex_lock( &p_dialog->p_interaction->object_lock );
261     if( i_return == NSAlertDefaultReturn )
262     {
263         p_dialog->i_return = DIALOG_OK_YES;
264     }
265     else if( i_return == NSAlertAlternateReturn )
266     {
267         p_dialog->i_return = DIALOG_NO;
268     }
269     else if( i_return == NSAlertOtherReturn )
270     {
271         p_dialog->i_return = DIALOG_CANCELLED;
272     }
273     p_dialog->i_status = ANSWERED_DIALOG;
274     vlc_mutex_unlock( &p_dialog->p_interaction->object_lock );
275 }
276
277 -(void)updateDialog
278 {
279     if( p_dialog->i_flags & DIALOG_USER_PROGRESS )
280     {
281         [o_prog_description setStringValue: \
282             [NSString stringWithUTF8String: p_dialog->psz_description]];
283         [o_prog_bar setDoubleValue: (double)p_dialog->val.f_float];
284
285         if( [o_prog_bar doubleValue] == 100.0 )
286         {
287             /* we are done, let's hide */
288             [self hideDialog];
289         }
290
291         if( p_dialog->i_timeToGo < 1 )
292             [o_prog_timeToGo setStringValue: @""];
293         else
294             [o_prog_timeToGo setStringValue: [NSString stringWithFormat:
295                     _NS("Remaining time: %i seconds"), p_dialog->i_timeToGo]];
296
297         return;
298     }
299     if( p_dialog->i_flags & DIALOG_INTF_PROGRESS )
300     {
301         [[VLCMain sharedInstance] setScrollField:
302             [NSString stringWithUTF8String: p_dialog->psz_description]
303             stopAfter: -1];
304         [o_mainIntfPgbar setDoubleValue: (double)p_dialog->val.f_float];
305
306         if( [o_mainIntfPgbar doubleValue] == 100.0 )
307         {
308             /* we are done, let's hide */
309             [self hideDialog];
310         }
311         return;
312     }
313 }
314
315 -(void)hideDialog
316 {
317     msg_Dbg( p_intf, "hide event" );
318     if( p_dialog->i_flags & DIALOG_USER_PROGRESS )
319     {
320         [NSApp endSheet: o_prog_win];
321         [o_prog_win close];
322     }
323     if( p_dialog->i_flags & DIALOG_LOGIN_PW_OK_CANCEL )
324     {
325         [NSApp endSheet: o_auth_win];
326         [o_auth_win close];
327     }
328     if( p_dialog->i_flags & DIALOG_PSZ_INPUT_OK_CANCEL )
329     {
330         [NSApp endSheet: o_input_win];
331         [o_input_win close];
332     }
333     if( p_dialog->i_flags & DIALOG_INTF_PROGRESS )
334     {
335         [o_mainIntfPgbar setIndeterminate: YES];
336         [o_mainIntfPgbar setHidden: YES];
337         [[VLCMain sharedInstance] resetScrollField];
338     }
339 }
340
341 -(void)destroyDialog
342 {
343     msg_Dbg( p_intf, "destroy event" );
344     if( o_mainIntfPgbar )
345         [o_mainIntfPgbar release];
346 }
347
348 - (IBAction)cancelAndClose:(id)sender
349 {
350     /* tell the core that the dialog was cancelled in a yes/no-style dialogue */
351     vlc_mutex_lock( &p_dialog->p_interaction->object_lock );
352     p_dialog->i_return = DIALOG_CANCELLED;
353     p_dialog->i_status = ANSWERED_DIALOG;
354     vlc_mutex_unlock( &p_dialog->p_interaction->object_lock );
355     msg_Dbg( p_intf, "dialog cancelled" );
356 }
357
358 - (IBAction)cancelDialog:(id)sender
359 {
360     /* tell core that the user wishes to cancel the dialogue
361      * Use this function if cancelling is optionally like in the progress-dialogue */
362     vlc_mutex_lock( &p_dialog->p_interaction->object_lock );
363     p_dialog->b_cancelled = VLC_TRUE;
364     vlc_mutex_unlock( &p_dialog->p_interaction->object_lock );
365     msg_Dbg( p_intf, "cancelling dialog, will close it later on" );
366 }
367
368 - (IBAction)okayAndClose:(id)sender
369 {
370     msg_Dbg( p_intf, "running okayAndClose" );
371     vlc_mutex_lock( &p_dialog->p_interaction->object_lock );
372     if( p_dialog->i_flags == DIALOG_LOGIN_PW_OK_CANCEL )
373     {
374         p_dialog->psz_returned[0] = strdup( [[o_auth_login_fld stringValue] UTF8String] );
375         p_dialog->psz_returned[1] = strdup( [[o_auth_pw_fld stringValue] UTF8String] );
376     }
377     else if( p_dialog->i_flags == DIALOG_PSZ_INPUT_OK_CANCEL )
378         p_dialog->psz_returned[0] = strdup( [[o_input_fld stringValue] UTF8String] );
379     p_dialog->i_return = DIALOG_OK_YES;
380     p_dialog->i_status = ANSWERED_DIALOG;
381     vlc_mutex_unlock( &p_dialog->p_interaction->object_lock );
382     msg_Dbg( p_intf, "dialog acknowledged" );
383 }
384
385 @end
386
387 /*****************************************************************************
388  * VLCErrorInteractionPanel implementation
389  *****************************************************************************/
390 @implementation VLCErrorInteractionPanel
391 -(id)init
392 {
393     [super init];
394
395     /* load the nib */
396     nib_interact_errpanel_loaded = [NSBundle loadNibNamed:@"InteractionErrorPanel" owner:self];
397
398     /* init strings */
399     [o_window setTitle: _NS("Errors and Warnings")];
400     [o_cleanup_button setTitle: _NS("Clean up")];
401     [o_messages_btn setTitle: _NS("Show Details")];
402
403     /* init data sources */
404     o_errors = [[NSMutableArray alloc] init];
405     o_icons = [[NSMutableArray alloc] init];
406
407     /* ugly Carbon stuff following...
408      * regrettably, you can't get the icons through clean Cocoa */
409
410     /* retrieve our error icon */
411     IconRef ourIconRef;
412     int returnValue;
413     returnValue = GetIconRef(kOnSystemDisk, 'macs', 'stop', &ourIconRef);
414     errorIcon = [[NSImage alloc] initWithSize:NSMakeSize(32,32)];
415     [errorIcon lockFocus];
416     CGRect rect = CGRectMake(0,0,32,32);
417     PlotIconRefInContext((CGContextRef)[[NSGraphicsContext currentContext] 
418         graphicsPort],
419         &rect,
420         kAlignNone,
421         kTransformNone,
422         NULL /*inLabelColor*/,
423         kPlotIconRefNormalFlags,
424         (IconRef)ourIconRef);
425     [errorIcon unlockFocus];
426     returnValue = ReleaseIconRef(ourIconRef);
427
428     /* retrieve our caution icon */
429     returnValue = GetIconRef(kOnSystemDisk, 'macs', 'caut', &ourIconRef);
430     warnIcon = [[NSImage alloc] initWithSize:NSMakeSize(32,32)];
431     [warnIcon lockFocus];
432     PlotIconRefInContext((CGContextRef)[[NSGraphicsContext currentContext] 
433         graphicsPort],
434         &rect,
435         kAlignNone,
436         kTransformNone,
437         NULL /*inLabelColor*/,
438         kPlotIconRefNormalFlags,
439         (IconRef)ourIconRef);
440     [warnIcon unlockFocus];
441     returnValue = ReleaseIconRef(ourIconRef);    
442
443     return self;
444 }
445
446 -(void)dealloc
447 {
448     [errorIcon release];
449     [warnIcon release];
450     [o_errors release];
451     [o_icons release];
452     [super dealloc];
453 }
454
455 -(void)showPanel
456 {
457     [o_window makeKeyAndOrderFront: self];
458 }
459
460 -(void)addError: (NSString *)o_error withMsg:(NSString *)o_msg
461 {
462     /* format our string as desired */
463     NSMutableAttributedString * ourError;
464     ourError = [[NSMutableAttributedString alloc] initWithString:
465         [NSString stringWithFormat:@"%@\n%@", o_error, o_msg]
466         attributes: 
467         [NSDictionary dictionaryWithObject: [NSFont systemFontOfSize:11] forKey: NSFontAttributeName]];
468     [ourError 
469         addAttribute: NSFontAttributeName
470         value: [NSFont boldSystemFontOfSize:11] 
471         range: NSMakeRange( 0, [o_error length])];
472     [o_errors addObject: ourError];
473     [ourError release];
474
475     [o_icons addObject: errorIcon];
476
477     [o_error_table reloadData];
478     [self showPanel];
479 }
480
481 -(void)addWarning: (NSString *)o_warning withMsg:(NSString *)o_msg
482 {
483     /* format our string as desired */
484     NSMutableAttributedString * ourWarning;
485     ourWarning = [[NSMutableAttributedString alloc] initWithString:
486         [NSString stringWithFormat:@"%@\n%@", o_warning, o_msg]
487         attributes: 
488         [NSDictionary dictionaryWithObject: [NSFont systemFontOfSize:11] forKey: NSFontAttributeName]];
489     [ourWarning 
490         addAttribute: NSFontAttributeName
491         value: [NSFont boldSystemFontOfSize:11] 
492         range: NSMakeRange( 0, [o_warning length])];
493     [o_errors addObject: ourWarning];
494     [ourWarning release];
495
496     [o_icons addObject: warnIcon];
497     
498     [o_error_table reloadData];
499
500     [self showPanel];
501 }
502
503 -(IBAction)cleanupTable:(id)sender
504 {
505     [o_errors removeAllObjects];
506     [o_icons removeAllObjects];
507     [o_error_table reloadData];
508 }
509
510 -(IBAction)showMessages:(id)sender
511 {
512     [[VLCMain sharedInstance] showMessagesPanel: sender];
513 }
514
515 /*----------------------------------------------------------------------------
516  * data source methods
517  *---------------------------------------------------------------------------*/
518 - (int)numberOfRowsInTableView:(NSTableView *)theDataTable
519 {
520     return [o_errors count];
521 }
522
523 - (id)tableView:(NSTableView *)theDataTable objectValueForTableColumn:
524     (NSTableColumn *)theTableColumn row: (int)row
525 {
526     if( [[theTableColumn identifier] isEqualToString: @"error_msg"] )
527         return [o_errors objectAtIndex: row];
528
529     if( [[theTableColumn identifier] isEqualToString: @"icon"] )
530         return [o_icons objectAtIndex: row];
531
532     return @"unknown identifier";
533 }
534
535 @end