]> git.sesse.net Git - vlc/blob - modules/gui/macosx_dialog_provider/dialogProvider.m
33e8b07db4f62c1d40a7435661c38456427c6af6
[vlc] / modules / gui / macosx_dialog_provider / dialogProvider.m
1 /*****************************************************************************
2  * dialogProvider.m: Minimal Dialog Provider for Mac OS X
3  *****************************************************************************
4  * Copyright (C) 2009-2010 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Felix Paul Kühne <fkuehne at videolan dot org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #import <stdlib.h>                                      /* malloc(), free() */
28 #import <string.h>
29
30 #ifdef HAVE_CONFIG_H
31 # import "config.h"
32 #endif
33
34 #import <vlc_common.h>
35 #import <vlc_plugin.h>
36 #import <vlc_dialog.h>
37 #import <vlc_interface.h>
38 #import <vlc_extensions.h>
39
40 #import <Cocoa/Cocoa.h>
41 #import "VLCLoginPanel.h"
42 #import "VLCProgressPanel.h"
43
44 /*****************************************************************************
45  * Prototypes
46  *****************************************************************************/
47 static int  OpenIntf(vlc_object_t *);
48 static void CloseIntf(vlc_object_t *);
49 static void Run(intf_thread_t * );
50
51 static int DisplayError(vlc_object_t *,const char *,vlc_value_t,vlc_value_t,void * );
52 static int DisplayCritical(vlc_object_t *,const char *,vlc_value_t,vlc_value_t,void * );
53 static int DisplayQuestion(vlc_object_t *,const char *,vlc_value_t,vlc_value_t,void * );
54 static int DisplayLogin(vlc_object_t *,const char *,vlc_value_t,vlc_value_t,void * );
55 static int DisplayProgressPanelAction(vlc_object_t *,const char *,vlc_value_t,vlc_value_t,void * );
56 static int DisplayExtension(vlc_object_t *,const char *,vlc_value_t,vlc_value_t,void * );
57
58 static void updateProgressPanel (void *, const char *, float);
59 static bool checkProgressPanel (void *);
60 static void destroyProgressPanel (void *);
61
62 @interface VLCDialogDisplayer : NSObject
63 {
64     VLCProgressPanel *_currentProgressBarPanel;
65 }
66
67 + (NSDictionary *)dictionaryForDialog:(const char *)title :(const char *)message :(const char *)yes :(const char *)no :(const char *)cancel;
68
69 - (void)displayError:(NSDictionary *)dialog;
70 - (void)displayCritical:(NSDictionary *)dialog;
71 - (NSNumber *)displayQuestion:(NSDictionary *)dialog;
72 - (NSDictionary *)displayLogin:(NSDictionary *)dialog;
73
74 - (void)displayProgressBar:(NSDictionary *)dict;
75 - (void)updateProgressPanel:(NSDictionary *)dict;
76 - (void)destroyProgressPanel;
77 - (NSNumber *)checkProgressPanel;
78
79 - (void)updateExtensionDialog:(NSValue *)extensionDialog;
80
81 - (id)resultFromSelectorOnMainThread:(SEL)sel withObject:(id)object;
82 @end
83
84 @interface VLCDialogButton : NSButton
85 {
86     extension_widget_t *widget;
87 }
88 @property (readwrite) extension_widget_t *widget;
89 @end
90
91 @implementation VLCDialogButton
92 @synthesize widget;
93 @end
94
95 @interface VLCDialogPopUpButton : NSPopUpButton
96 {
97     extension_widget_t *widget;
98 }
99 @property (readwrite) extension_widget_t *widget;
100 @end
101
102 @implementation VLCDialogPopUpButton
103 @synthesize widget;
104 @end
105
106
107 @interface VLCDialogTextField : NSTextField
108 {
109     extension_widget_t *widget;
110 }
111 @property (readwrite) extension_widget_t *widget;
112 @end
113
114 @implementation VLCDialogTextField
115 @synthesize widget;
116 @end
117
118 @interface VLCDialogList : NSTableView
119 {
120     extension_widget_t *widget;
121     NSMutableArray *contentArray;
122 }
123 @property (readwrite) extension_widget_t *widget;
124 @property (readwrite, retain) NSMutableArray *contentArray;
125 @end
126
127 @implementation VLCDialogList
128 @synthesize widget;
129 @synthesize contentArray;
130
131 - (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
132 {
133     return [contentArray count];
134 }
135
136 - (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
137 {
138     return [[contentArray objectAtIndex:rowIndex] objectForKey:@"text"];
139 }
140 @end
141
142 @interface VLCDialogGridView : NSView {
143     NSUInteger _rowCount, _colCount;
144     NSMutableArray *_gridedViews;
145 }
146
147 - (NSSize)flexSize:(NSSize)size;
148 - (void)removeSubview:(NSView *)view;
149 @end
150
151
152 // Move this to separate file
153 @implementation VLCDialogGridView
154
155 - (void)dealloc
156 {
157     [_gridedViews release];
158     [super dealloc];
159 }
160
161 - (void)recomputeCount
162 {
163     _colCount = 0;
164     _rowCount = 0;
165     for (NSDictionary *obj in _gridedViews)
166     {
167         NSUInteger row = [[obj objectForKey:@"row"] intValue];
168         NSUInteger col = [[obj objectForKey:@"col"] intValue];
169         if (col + 1 > _colCount)
170             _colCount = col + 1;
171         if (row + 1 > _rowCount)
172             _rowCount = row + 1;
173     }
174 }
175
176 - (void)recomputeWindowSize
177 {
178     [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(recomputeWindowSize) object:nil];
179
180     NSWindow *window = [self window];
181     NSRect frame = [window frame];
182     NSRect contentRect = [window contentRectForFrameRect:frame];
183     contentRect.size = [self flexSize:frame.size];
184     NSRect newFrame = [window frameRectForContentRect:contentRect];
185     newFrame.origin.y -= newFrame.size.height - frame.size.height;
186     newFrame.origin.x -= (newFrame.size.width - frame.size.width) / 2;
187     [window setFrame:newFrame display:YES animate:YES];
188 }
189
190 - (NSSize)objectSizeToFit:(NSView *)view
191 {
192     if ([view isKindOfClass:[NSControl class]]) {
193         NSControl *control = (NSControl *)view;
194         return [[control cell] cellSize];
195     }
196     return [view frame].size;
197 }
198
199 - (CGFloat)marginX
200 {
201     return 16;
202 }
203 - (CGFloat)marginY
204 {
205     return 8;
206 }
207
208 - (CGFloat)constrainedHeightOfRow:(NSUInteger)targetRow
209 {
210     CGFloat height = 0;
211     for(NSDictionary *obj in _gridedViews) {
212         NSUInteger row = [[obj objectForKey:@"row"] intValue];
213         if (row != targetRow)
214             continue;
215         NSUInteger rowSpan = [[obj objectForKey:@"rowSpan"] intValue];
216         if (rowSpan != 1)
217             continue;
218         NSView *view = [obj objectForKey:@"view"];
219         if ([view autoresizingMask] & NSViewHeightSizable)
220             continue;
221         NSSize sizeToFit = [self objectSizeToFit:view];
222         if (height < sizeToFit.height)
223             height = sizeToFit.height;
224     }
225     return height;
226 }
227
228 - (CGFloat)remainingRowsHeight
229 {
230     NSUInteger height = [self marginY];
231     if (!_rowCount)
232         return 0;
233     NSUInteger autosizedRows = 0;
234     for (NSUInteger i = 0; i < _rowCount; i++) {
235         CGFloat constrainedHeight = [self constrainedHeightOfRow:i];
236         if (!constrainedHeight)
237             autosizedRows++;
238         height += constrainedHeight + [self marginY];
239     }
240     CGFloat remaining = 0;
241     if (height < self.bounds.size.height && autosizedRows)
242         remaining = (self.bounds.size.height - height) / autosizedRows;
243     if (remaining < 0)
244         remaining = 0;
245
246     return remaining;
247 }
248
249 - (CGFloat)heightOfRow:(NSUInteger)targetRow
250 {
251     NSAssert(targetRow < _rowCount, @"accessing a non existing row");
252     CGFloat height = [self constrainedHeightOfRow:targetRow];
253     if (!height)
254         height = [self remainingRowsHeight];
255     return height;
256 }
257
258
259 - (CGFloat)topOfRow:(NSUInteger)targetRow
260 {
261     CGFloat top = [self marginY];
262     for (NSUInteger i = 1; i < _rowCount - targetRow; i++)
263     {
264         top += [self heightOfRow:_rowCount - i] + [self marginY];
265     }
266     return top;
267 }
268
269 - (CGFloat)constrainedWidthOfColumn:(NSUInteger)targetColumn
270 {
271     CGFloat width = 0;
272     for(NSDictionary *obj in _gridedViews) {
273         NSUInteger col = [[obj objectForKey:@"col"] intValue];
274         if (col != targetColumn)
275             continue;
276         NSUInteger colSpan = [[obj objectForKey:@"colSpan"] intValue];
277         if (colSpan != 1)
278             continue;
279         NSView *view = [obj objectForKey:@"view"];
280         if ([view autoresizingMask] & NSViewWidthSizable)
281             return 0;
282         NSSize sizeToFit = [self objectSizeToFit:view];
283         if (width < sizeToFit.width)
284             width = sizeToFit.width;
285     }
286     return width;
287 }
288
289 - (CGFloat)remainingColumnWidth
290 {
291     NSUInteger width = [self marginX];
292     if (!_colCount)
293         return 0;
294     NSUInteger autosizedCol = 0;
295     for (NSUInteger i = 0; i < _colCount; i++) {
296         CGFloat constrainedWidth = [self constrainedWidthOfColumn:i];
297         if (!constrainedWidth)
298             autosizedCol++;
299         width += constrainedWidth + [self marginX];
300
301     }
302     CGFloat remaining = 0;
303     if (width < self.bounds.size.width && autosizedCol)
304         remaining = (self.bounds.size.width - width) / autosizedCol;
305     if (remaining < 0)
306         remaining = 0;
307     return remaining;
308 }
309
310 - (CGFloat)widthOfColumn:(NSUInteger)targetColumn
311 {
312     CGFloat width = [self constrainedWidthOfColumn:targetColumn];
313     if (!width)
314         width = [self remainingColumnWidth];
315     return width;
316 }
317
318
319 - (CGFloat)leftOfColumn:(NSUInteger)targetColumn
320 {
321     CGFloat left = [self marginX];
322     for (NSUInteger i = 0; i < targetColumn; i++)
323     {
324         left += [self widthOfColumn:i] + [self marginX];
325
326     }
327     return left;
328 }
329
330 - (void)relayout
331 {
332     for(NSDictionary *obj in _gridedViews) {
333         NSUInteger row = [[obj objectForKey:@"row"] intValue];
334         NSUInteger col = [[obj objectForKey:@"col"] intValue];
335         NSUInteger rowSpan = [[obj objectForKey:@"rowSpan"] intValue];
336         NSUInteger colSpan = [[obj objectForKey:@"colSpan"] intValue];
337         NSView *view = [obj objectForKey:@"view"];
338         NSRect rect;
339
340         // Get the height
341         if ([view autoresizingMask] & NSViewHeightSizable || rowSpan > 1) {
342             CGFloat height = 0;
343             for (NSUInteger r = 0; r < rowSpan; r++) {
344                 if (row + r >= _rowCount)
345                     break;
346                 height += [self heightOfRow:row + r] + [self marginY];
347             }
348             rect.size.height = height - [self marginY];
349         }
350         else
351             rect.size.height = [self objectSizeToFit:view].height;
352
353         // Get the width
354         if ([view autoresizingMask] & NSViewWidthSizable) {
355             CGFloat width = 0;
356             for (NSUInteger c = 0; c < colSpan; c++)
357                 width += [self widthOfColumn:col + c] + [self marginX];
358             rect.size.width = width - [self marginX];
359         }
360         else
361             rect.size.width = [self objectSizeToFit:view].width;
362
363         // Top corner
364         rect.origin.y = [self topOfRow:row] + ([self heightOfRow:row] - rect.size.height) / 2;
365         rect.origin.x = [self leftOfColumn:col];
366
367         [view setFrame:rect];
368         [view setNeedsDisplay:YES];
369     }
370 }
371
372 - (NSMutableDictionary *)objectForView:(NSView *)view
373 {
374     for (NSMutableDictionary *dict in _gridedViews)
375     {
376         if ([dict objectForKey:@"view"] == view)
377             return dict;
378     }
379     return nil;
380 }
381
382 - (void)addSubview:(NSView *)view atRow:(NSUInteger)row column:(NSUInteger)column rowSpan:(NSUInteger)rowSpan colSpan:(NSUInteger)colSpan
383 {
384     if (row + 1 > _rowCount)
385         _rowCount = row + 1;
386     if (column + 1 > _colCount)
387         _colCount = column + 1;
388
389     if (!_gridedViews)
390         _gridedViews = [[NSMutableArray alloc] init];
391
392     NSMutableDictionary *dict = [self objectForView:view];
393     if (!dict) {
394         dict = [NSMutableDictionary dictionary];
395         [dict setObject:view forKey:@"view"];
396         [_gridedViews addObject:dict];
397     }
398     [dict setObject:[NSNumber numberWithInt:rowSpan] forKey:@"rowSpan"];
399     [dict setObject:[NSNumber numberWithInt:colSpan] forKey:@"colSpan"];
400     [dict setObject:[NSNumber numberWithInt:row] forKey:@"row"];
401     [dict setObject:[NSNumber numberWithInt:column] forKey:@"col"];
402
403
404     [self addSubview:view];
405     [self relayout];
406
407     // Recompute the size of the window after making sure we won't see anymore update
408     [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(recomputeWindowSize) object:nil];
409     [self performSelector:@selector(recomputeWindowSize) withObject:nil afterDelay:0.1];
410 }
411
412 - (void)removeSubview:(NSView *)view
413 {
414     NSDictionary *dict = [self objectForView:view];
415     if (dict)
416         [_gridedViews removeObject:dict];
417     [view removeFromSuperview];
418
419     [self recomputeCount];
420     [self recomputeWindowSize];
421
422     [self relayout];
423     [self setNeedsDisplay:YES];
424 }
425
426 - (void)setFrame:(NSRect)frameRect
427 {
428     [super setFrame:frameRect];
429     [self relayout];
430 }
431
432 - (NSSize)flexSize:(NSSize)size
433 {
434     if (!_rowCount || !_colCount)
435         return size;
436
437     CGFloat minHeight = [self marginY];
438     BOOL canFlexHeight = NO;
439     for (NSUInteger i = 0; i < _rowCount; i++) {
440         CGFloat constrained = [self constrainedHeightOfRow:i];
441         if (!constrained) {
442             canFlexHeight = YES;
443             constrained = 128;
444         }
445         minHeight += constrained + [self marginY];
446     }
447
448     CGFloat minWidth = [self marginX];
449     BOOL canFlexWidth = NO;
450     for (NSUInteger i = 0; i < _colCount; i++) {
451         CGFloat constrained = [self constrainedWidthOfColumn:i];
452         if (!constrained) {
453             canFlexWidth = YES;
454             constrained = 128;
455         }
456         minWidth += constrained + [self marginX];
457     }
458     if (size.width < minWidth)
459         size.width = minWidth;
460     if (size.height < minHeight)
461         size.height = minHeight;
462     if (!canFlexHeight)
463         size.height = minHeight;
464     if (!canFlexWidth)
465         size.width = minWidth;
466     return size;
467 }
468
469 - (NSSize)windowWillResize:(NSWindow *)sender toSize:(NSSize)frameSize
470 {
471     NSRect rect = NSMakeRect(0, 0, 0, 0);
472     rect.size = frameSize;
473     rect = [sender contentRectForFrameRect:rect];
474     rect.size = [self flexSize:rect.size];
475     rect = [sender frameRectForContentRect:rect];
476     return rect.size;
477 }
478
479 @end
480
481
482 static inline NSDictionary *DictFromDialogFatal(dialog_fatal_t *dialog) {
483     return [VLCDialogDisplayer dictionaryForDialog:dialog->title :dialog->message :NULL :NULL :NULL];
484 }
485 static inline NSDictionary *DictFromDialogLogin(dialog_login_t *dialog) {
486     return [VLCDialogDisplayer dictionaryForDialog:dialog->title :dialog->message :NULL :NULL :NULL];
487 }
488 static inline NSDictionary *DictFromDialogQuestion(dialog_question_t *dialog) {
489     return [VLCDialogDisplayer dictionaryForDialog:dialog->title :dialog->message :dialog->yes :dialog->no :dialog->cancel];
490 }
491 static inline NSDictionary *DictFromDialogProgressBar(dialog_progress_bar_t *dialog) {
492     return [VLCDialogDisplayer dictionaryForDialog:dialog->title :dialog->message :NULL :NULL :dialog->cancel];
493 }
494
495 struct intf_sys_t
496 {
497     VLCDialogDisplayer *displayer;
498
499     vlc_mutex_t lock;
500     vlc_cond_t wait;
501     bool is_hidding_noaction_dialogs;
502 };
503
504
505 #define T_HIDE_NOACTION N_("Hide no user action dialogs")
506 #define LT_HIDE_NOACTION N_("Don't display dialogs that don't require user action (Critical and error panel).")
507
508 #define prefix "macosx-dialog-provider"
509 /*****************************************************************************
510  * Module descriptor
511  *****************************************************************************/
512
513 vlc_module_begin()
514     /* Minimal interface. see intf.m */
515     set_shortname("Mac OS X Dialogs")
516     add_shortcut("macosx_dialog_provider")
517     add_shortcut("miosx")
518     set_description("Minimal Mac OS X Dialog Provider")
519     set_capability("interface", 0)
520
521     /* This setting is interesting, because when used with a libvlc app
522      * it's almost certain that the client program will display error by
523      * itself. Moreover certain action might end up in an error, but
524      * the client wants to ignored them completely. */
525     add_bool(prefix "hide-no-user-action-dialogs", true, NULL, T_HIDE_NOACTION, LT_HIDE_NOACTION, false)
526
527     set_callbacks(OpenIntf, CloseIntf)
528     set_category(CAT_INTERFACE)
529     set_subcategory(SUBCAT_INTERFACE_MAIN)
530 vlc_module_end()
531
532 /*****************************************************************************
533  * OpenIntf: initialize interface
534  *****************************************************************************/
535 int OpenIntf(vlc_object_t *p_this)
536 {
537     intf_thread_t *p_intf = (intf_thread_t*) p_this;
538
539     p_intf->p_sys = malloc(sizeof(intf_sys_t));
540     if(!p_intf->p_sys)
541         return VLC_ENOMEM;
542
543     memset(p_intf->p_sys,0,sizeof(*p_intf->p_sys));
544
545     p_intf->p_sys->displayer = [[VLCDialogDisplayer alloc] init];
546
547     bool hide = var_CreateGetBool(p_intf, prefix "hide-no-user-action-dialogs");
548     p_intf->p_sys->is_hidding_noaction_dialogs = hide;
549
550     /* subscribe to various interactive dialogues */
551
552     if (!hide)
553     {
554         var_Create(p_intf,"dialog-error",VLC_VAR_ADDRESS);
555         var_AddCallback(p_intf,"dialog-error",DisplayError,p_intf);
556         var_Create(p_intf,"dialog-critical",VLC_VAR_ADDRESS);
557         var_AddCallback(p_intf,"dialog-critical",DisplayCritical,p_intf);
558     }
559     var_Create(p_intf,"dialog-login",VLC_VAR_ADDRESS);
560     var_AddCallback(p_intf,"dialog-login",DisplayLogin,p_intf);
561     var_Create(p_intf,"dialog-question",VLC_VAR_ADDRESS);
562     var_AddCallback(p_intf,"dialog-question",DisplayQuestion,p_intf);
563     var_Create(p_intf,"dialog-progress-bar",VLC_VAR_ADDRESS);
564     var_AddCallback(p_intf,"dialog-progress-bar",DisplayProgressPanelAction,p_intf);
565     var_Create(p_intf,"dialog-extension",VLC_VAR_ADDRESS);
566     var_AddCallback(p_intf,"dialog-extension",DisplayExtension,p_intf);
567     dialog_Register(p_intf);
568
569     msg_Dbg(p_intf,"Mac OS X dialog provider initialised");
570
571     return VLC_SUCCESS;
572 }
573
574 /*****************************************************************************
575  * CloseIntf: destroy interface
576  *****************************************************************************/
577 void CloseIntf(vlc_object_t *p_this)
578 {
579     intf_thread_t *p_intf = (intf_thread_t*) p_this;
580
581     /* unsubscribe from the interactive dialogues */
582     dialog_Unregister(p_intf);
583
584     if (!p_intf->p_sys->is_hidding_noaction_dialogs)
585     {
586         var_DelCallback(p_intf,"dialog-error",DisplayError,p_intf);
587         var_DelCallback(p_intf,"dialog-critical",DisplayCritical,p_intf);
588     }
589     var_DelCallback(p_intf,"dialog-login",DisplayLogin,p_intf);
590     var_DelCallback(p_intf,"dialog-question",DisplayQuestion,p_intf);
591     var_DelCallback(p_intf,"dialog-progress-bar",DisplayProgressPanelAction,p_intf);
592     var_DelCallback(p_intf,"dialog-extension",DisplayExtension,p_intf);
593
594     [p_intf->p_sys->displayer release];
595
596     msg_Dbg(p_intf,"Mac OS X dialog provider closed");
597     free(p_intf->p_sys);
598 }
599
600
601 /*****************************************************************************
602  * Callbacks triggered by the "dialog-*" variables
603  *****************************************************************************/
604 static int DisplayError(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
605 {
606     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
607     dialog_fatal_t *dialog = value.p_address;
608     intf_thread_t *p_intf = (intf_thread_t*) p_this;
609     intf_sys_t *sys = p_intf->p_sys;
610     [sys->displayer performSelectorOnMainThread:@selector(displayError:) withObject:DictFromDialogFatal(dialog) waitUntilDone:NO];
611     [pool release];
612     return VLC_SUCCESS;
613 }
614
615 static int DisplayCritical(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
616 {
617     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
618     dialog_fatal_t *dialog = value.p_address;
619     intf_thread_t *p_intf = (intf_thread_t*) p_this;
620     intf_sys_t *sys = p_intf->p_sys;
621     [sys->displayer performSelectorOnMainThread:@selector(displayCritical:) withObject:DictFromDialogFatal(dialog) waitUntilDone:NO];
622     [pool release];
623     return VLC_SUCCESS;
624 }
625
626 static int DisplayQuestion(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
627 {
628     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
629     dialog_question_t *dialog = value.p_address;
630     intf_thread_t *p_intf = (intf_thread_t*) p_this;
631     intf_sys_t *sys = p_intf->p_sys;
632     dialog->answer = [[sys->displayer resultFromSelectorOnMainThread:@selector(displayQuestion:) withObject:DictFromDialogQuestion(dialog)] intValue];
633     [pool release];
634     return VLC_SUCCESS;
635 }
636
637 static int DisplayLogin(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
638 {
639     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
640     dialog_login_t *dialog = value.p_address;
641     intf_thread_t *p_intf = (intf_thread_t*) p_this;
642     intf_sys_t *sys = p_intf->p_sys;
643     NSDictionary *dict = [sys->displayer resultFromSelectorOnMainThread:@selector(displayLogin:) withObject:DictFromDialogLogin(dialog)];
644     if (dict) {
645         *dialog->username = strdup([[dict objectForKey:@"username"] UTF8String]);
646         *dialog->password = strdup([[dict objectForKey:@"password"] UTF8String]);
647     }
648     [pool release];
649     return VLC_SUCCESS;
650 }
651
652 static int DisplayProgressPanelAction(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
653 {
654     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
655     dialog_progress_bar_t *dialog = value.p_address;
656     intf_thread_t *p_intf = (intf_thread_t*) p_this;
657     intf_sys_t *sys = p_intf->p_sys;
658
659     [sys->displayer performSelectorOnMainThread:@selector(displayProgressBar:) withObject:DictFromDialogProgressBar(dialog) waitUntilDone:YES];
660
661     dialog->pf_update = updateProgressPanel;
662     dialog->pf_check = checkProgressPanel;
663     dialog->pf_destroy = destroyProgressPanel;
664     dialog->p_sys = p_intf->p_sys;
665
666     [pool release];
667     return VLC_SUCCESS;
668 }
669
670 static int DisplayExtension(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
671 {
672     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
673     intf_thread_t *p_intf = (intf_thread_t*) p_this;
674     intf_sys_t *sys = p_intf->p_sys;
675     extension_dialog_t *dialog = value.p_address;
676
677     // -updateExtensionDialog: Open its own runloop, so be sure to run on DefaultRunLoop.
678     [sys->displayer performSelectorOnMainThread:@selector(updateExtensionDialog:) withObject:[NSValue valueWithPointer:dialog] waitUntilDone:YES];
679     [pool release];
680     return VLC_SUCCESS;
681 }
682
683
684 void updateProgressPanel (void *priv, const char *text, float value)
685 {
686     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
687     intf_sys_t *sys = (intf_sys_t *)priv;
688
689     NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
690                           [NSNumber numberWithFloat:value], @"value",
691                           text ? [NSString stringWithUTF8String:text] : nil, @"text",
692                           nil];
693
694     [sys->displayer performSelectorOnMainThread:@selector(updateProgressPanel:) withObject:dict waitUntilDone:YES];
695
696     [pool release];
697 }
698
699 void destroyProgressPanel (void *priv)
700 {
701     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
702     intf_sys_t *sys = (intf_sys_t *)priv;
703
704     [sys->displayer performSelectorOnMainThread:@selector(destroyProgressPanel) withObject:nil waitUntilDone:YES];
705
706     [pool release];
707 }
708
709 bool checkProgressPanel (void *priv)
710 {
711     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
712     intf_sys_t *sys = (intf_sys_t *)priv;
713     BOOL ret;
714
715     ret = [[sys->displayer resultFromSelectorOnMainThread:@selector(checkProgressPanel) withObject:nil] boolValue];
716
717     [pool release];
718     return ret;
719 }
720
721
722 @implementation VLCDialogDisplayer
723 - (void)dealloc
724 {
725     assert(!_currentProgressBarPanel); // This has to be closed on main thread.
726     [super dealloc];
727 }
728
729 + (NSDictionary *)dictionaryForDialog:(const char *)title :(const char *)message :(const char *)yes :(const char *)no :(const char *)cancel
730 {
731     NSMutableDictionary *dict = [NSMutableDictionary dictionary];
732     if (title)
733         [dict setObject:[NSString stringWithUTF8String:title] forKey:@"title"];
734     if (message)
735         [dict setObject:[NSString stringWithUTF8String:message] forKey:@"message"];
736     if (yes)
737         [dict setObject:[NSString stringWithUTF8String:yes] forKey:@"yes"];
738     if (no)
739         [dict setObject:[NSString stringWithUTF8String:no] forKey:@"no"];
740     if (cancel)
741         [dict setObject:[NSString stringWithUTF8String:cancel] forKey:@"cancel"];
742
743     return dict;
744 }
745 #define VLCAssertIsMainThread() assert([NSThread isMainThread])
746
747 - (void)displayError:(NSDictionary *)dialog
748 {
749     VLCAssertIsMainThread();
750
751     NSRunInformationalAlertPanel([dialog objectForKey:@"title"],
752                                  [dialog objectForKey:@"message"],
753                                  @"OK", nil, nil);
754 }
755
756 - (void)displayCritical:(NSDictionary *)dialog
757 {
758     VLCAssertIsMainThread();
759
760     NSRunCriticalAlertPanel([dialog objectForKey:@"title"],
761                                  [dialog objectForKey:@"message"],
762                                  @"OK", nil, nil);
763 }
764
765 - (NSNumber *)displayQuestion:(NSDictionary *)dialog
766 {
767     VLCAssertIsMainThread();
768
769     NSInteger alertRet = 0;
770
771     NSAlert *alert = [NSAlert alertWithMessageText:[dialog objectForKey:@"title"]
772                               defaultButton:[dialog objectForKey:@"yes"]
773                             alternateButton:[dialog objectForKey:@"no"]
774                                 otherButton:[dialog objectForKey:@"cancel"]
775                   informativeTextWithFormat:[dialog objectForKey:@"message"]];
776     [alert setAlertStyle:NSInformationalAlertStyle];
777     alertRet = [alert runModal];
778
779     int ret;
780     switch (alertRet) {
781         case NSAlertDefaultReturn:
782             ret = 1;
783             break;
784         case NSAlertAlternateReturn:
785             ret = 2;
786             break;
787         case NSAlertOtherReturn:
788             ret = 3;
789             break;
790         default:
791             assert(0);
792             ret = 0;
793             break;
794     }
795
796     return [NSNumber numberWithInt:ret];
797 }
798
799 - (NSDictionary *)displayLogin:(NSDictionary *)dialog
800 {
801     VLCAssertIsMainThread();
802
803     VLCLoginPanel *panel = [[VLCLoginPanel alloc] init];
804     [panel createContentView];
805     [panel setDialogTitle:[dialog objectForKey:@"title"]];
806     [panel setDialogMessage:[dialog objectForKey:@"message"]];
807     [panel center];
808     NSInteger ret = [NSApp runModalForWindow:panel];
809     [panel close];
810
811     if (!ret)
812         return nil;
813
814     return [NSDictionary dictionaryWithObjectsAndKeys:
815             [panel userName], @"username",
816             [panel password], @"password",
817             nil];
818 }
819
820 - (void)displayProgressBar:(NSDictionary *)dialog
821 {
822     VLCAssertIsMainThread();
823
824     if(_currentProgressBarPanel)
825         [self destroyProgressPanel];
826
827     assert(!_currentProgressBarPanel);
828     _currentProgressBarPanel = [[VLCProgressPanel alloc] init];
829     [_currentProgressBarPanel createContentView];
830     [_currentProgressBarPanel setDialogTitle:[dialog objectForKey:@"title"]];
831     [_currentProgressBarPanel setDialogMessage:[dialog objectForKey:@"message"] ?: @""];
832     [_currentProgressBarPanel setCancelButtonLabel:[dialog objectForKey:@"cancel"]];
833
834     [_currentProgressBarPanel center];
835     [_currentProgressBarPanel makeKeyAndOrderFront:nil];
836 }
837
838 - (void)updateProgressPanel:(NSDictionary *)dict
839 {
840     VLCAssertIsMainThread();
841
842     assert(_currentProgressBarPanel);
843     [_currentProgressBarPanel setDialogMessage:[dict objectForKey:@"text"] ?: @""];
844     [_currentProgressBarPanel setProgressAsDouble:[[dict objectForKey:@"value"] doubleValue] * 1000.];
845 }
846
847 - (void)destroyProgressPanel
848 {
849     VLCAssertIsMainThread();
850
851     [_currentProgressBarPanel close];
852     [_currentProgressBarPanel release];
853     _currentProgressBarPanel = nil;
854 }
855
856 - (NSNumber *)checkProgressPanel
857 {
858     VLCAssertIsMainThread();
859
860     return [NSNumber numberWithBool:[_currentProgressBarPanel isCancelled]];
861 }
862
863 #pragma mark -
864 #pragma mark Extensions Dialog
865
866 - (void)triggerClick:(id)sender
867 {
868     assert([sender isKindOfClass:[VLCDialogButton class]]);
869     VLCDialogButton *button = sender;
870     extension_widget_t *widget = [button widget];
871
872     NSLog(@"(triggerClick)");
873     vlc_mutex_lock(&widget->p_dialog->lock);
874     extension_WidgetClicked(widget->p_dialog, widget);
875     vlc_mutex_unlock(&widget->p_dialog->lock);
876 }
877
878 - (void)syncTextField:(NSNotification *)notifcation
879 {
880     id sender = [notifcation object];
881     assert([sender isKindOfClass:[VLCDialogTextField class]]);
882     VLCDialogTextField *field = sender;
883     extension_widget_t *widget = [field widget];
884
885     vlc_mutex_lock(&widget->p_dialog->lock);
886     free(widget->psz_text);
887     widget->psz_text = strdup([[field stringValue] UTF8String]);
888     vlc_mutex_unlock(&widget->p_dialog->lock);
889 }
890
891 - (void)tableViewSelectionDidChange:(NSNotification *)notifcation
892 {
893     id sender = [notifcation object];
894     assert(sender && [sender isKindOfClass:[VLCDialogList class]]);
895     VLCDialogList *list = sender;
896
897     struct extension_widget_value_t *value;
898     unsigned i = 0;
899     for(value = [list widget]->p_values; value != NULL; value = value->p_next, i++)
900         value->b_selected = (i == [list selectedRow]);
901 }
902
903 - (void)popUpSelectionChanged:(id)sender
904 {
905     assert([sender isKindOfClass:[VLCDialogPopUpButton class]]);
906     VLCDialogPopUpButton *popup = sender;
907     struct extension_widget_value_t *value;
908     unsigned i = 0;
909     for(value = [popup widget]->p_values; value != NULL; value = value->p_next, i++)
910         value->b_selected = (i == [popup indexOfSelectedItem]);
911
912 }
913
914 static NSView *createControlFromWidget(extension_widget_t *widget, id self)
915 {
916     assert(!widget->p_sys_intf);
917
918     switch (widget->type)
919     {
920         case EXTENSION_WIDGET_HTML:
921         {
922 //            NSScrollView *scrollView = [[NSScrollView alloc] init];
923 //            [scrollView setHasVerticalScroller:YES];
924 //            NSTextView *field = [[NSTextView alloc] init];
925 //            [scrollView setDocumentView:field];
926 //            [scrollView setAutoresizesSubviews:YES];
927 //            [scrollView setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
928 //            [field release];
929 //            return scrollView;
930             NSTextView *field = [[NSTextView alloc] init];
931             [field setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
932             [field setDrawsBackground:NO];
933             return field;
934         }
935         case EXTENSION_WIDGET_LABEL:
936         {
937             NSTextField *field = [[NSTextField alloc] init];
938             [field setEditable:NO];
939             [field setBordered:NO];
940             [field setDrawsBackground:NO];
941             [[field cell] setControlSize:NSRegularControlSize];
942             [field setAutoresizingMask:NSViewNotSizable];
943             return field;
944         }
945         case EXTENSION_WIDGET_TEXT_FIELD:
946         {
947             VLCDialogTextField *field = [[VLCDialogTextField alloc] init];
948             [field setWidget:widget];
949             [field setAutoresizingMask:NSViewWidthSizable];
950             [[field cell] setControlSize:NSRegularControlSize];
951             [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(syncTextField:)  name:NSControlTextDidChangeNotification object:field];
952             return field;
953         }
954         case EXTENSION_WIDGET_BUTTON:
955         {
956             VLCDialogButton *button = [[VLCDialogButton alloc] init];
957             [button setBezelStyle:NSRoundedBezelStyle];
958             [button setWidget:widget];
959             [button setAction:@selector(triggerClick:)];
960             [button setTarget:self];
961             [[button cell] setControlSize:NSRegularControlSize];
962             [button setAutoresizingMask:NSViewNotSizable];
963             return button;
964         }
965         case EXTENSION_WIDGET_DROPDOWN:
966         {
967             VLCDialogPopUpButton *popup = [[VLCDialogPopUpButton alloc] init];
968             [popup setAction:@selector(popUpSelectionChanged:)];
969             [popup setTarget:self];
970             [popup setWidget:widget];
971             return popup;
972         }
973         case EXTENSION_WIDGET_LIST:
974         {
975             NSScrollView *scrollView = [[NSScrollView alloc] init];
976             [scrollView setHasVerticalScroller:YES];
977             VLCDialogList *list = [[VLCDialogList alloc] init];
978             [list setUsesAlternatingRowBackgroundColors:YES];
979             [list setHeaderView:nil];
980             [scrollView setDocumentView:list];
981             [scrollView setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
982
983             NSTableColumn *column = [[NSTableColumn alloc] init];
984             [list addTableColumn:column];
985             [column release];
986             [list setDataSource:list];
987             [list setDelegate:self];
988             [list setWidget:widget];
989             [list release];
990             return scrollView;
991         }
992         case EXTENSION_WIDGET_IMAGE:
993         {
994             NSImageView *imageView = [[NSImageView alloc] init];
995             [imageView setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
996             [imageView setImageFrameStyle:NSImageFramePhoto];
997             [imageView setImageScaling:NSImageScaleProportionallyUpOrDown];
998             return imageView;
999         }
1000         default:
1001             assert(0);
1002             return nil;
1003     }
1004
1005 }
1006
1007 static void updateControlFromWidget(NSView *control, extension_widget_t *widget)
1008 {
1009     switch (widget->type)
1010     {
1011         case EXTENSION_WIDGET_HTML:
1012 //        {
1013 //            // Get the scroll view
1014 //            assert([control isKindOfClass:[NSScrollView class]]);
1015 //            NSScrollView *scrollView = (NSScrollView *)control;
1016 //            control = [scrollView documentView];
1017 //
1018 //            assert([control isKindOfClass:[NSTextView class]]);
1019 //            NSTextView *textView = (NSTextView *)control;
1020 //            NSString *string = [NSString stringWithUTF8String:widget->psz_text];
1021 //            NSAttributedString *attrString = [[NSAttributedString alloc] initWithHTML:[string dataUsingEncoding:NSUTF8StringEncoding] documentAttributes:NULL];
1022 //            [[textView textStorage] setAttributedString:[[NSAttributedString alloc] initWithString:@"Hello"]];
1023 //            NSLog(@"%@", string);
1024 //            [textView setNeedsDisplay:YES];
1025 //            [textView scrollRangeToVisible:NSMakeRange(0, 0)];
1026 //            [attrString release];
1027 //            break;
1028 //
1029 //        }
1030         {
1031             assert([control isKindOfClass:[NSTextView class]]);
1032             NSTextView *textView = (NSTextView *)control;
1033             NSString *string = [NSString stringWithUTF8String:widget->psz_text];
1034             NSAttributedString *attrString = [[NSAttributedString alloc] initWithHTML:[string dataUsingEncoding:NSUTF8StringEncoding] documentAttributes:NULL];
1035             [[textView textStorage] setAttributedString:attrString];
1036             [textView setNeedsDisplay:YES];
1037             [textView scrollRangeToVisible:NSMakeRange(0, 0)];
1038             [attrString release];
1039             break;
1040
1041         }
1042         case EXTENSION_WIDGET_LABEL:
1043         case EXTENSION_WIDGET_PASSWORD:
1044         case EXTENSION_WIDGET_TEXT_FIELD:
1045         {
1046             if (!widget->psz_text)
1047                 break;
1048             assert([control isKindOfClass:[NSControl class]]);
1049             NSControl *field = (NSControl *)control;
1050             NSString *string = [NSString stringWithUTF8String:widget->psz_text];
1051             NSAttributedString *attrString = [[NSAttributedString alloc] initWithHTML:[string dataUsingEncoding:NSUTF8StringEncoding] documentAttributes:NULL];
1052             [field setAttributedStringValue:attrString];
1053             [attrString release];
1054             break;
1055         }
1056         case EXTENSION_WIDGET_BUTTON:
1057         {
1058             assert([control isKindOfClass:[NSButton class]]);
1059             NSButton *button = (NSButton *)control;
1060             if (!widget->psz_text)
1061                 break;
1062             [button setTitle:[NSString stringWithUTF8String:widget->psz_text]];
1063             break;
1064         }
1065         case EXTENSION_WIDGET_DROPDOWN:
1066         {
1067             assert([control isKindOfClass:[NSPopUpButton class]]);
1068             NSPopUpButton *popup = (NSPopUpButton *)control;
1069             [popup removeAllItems];
1070             struct extension_widget_value_t *value;
1071             for(value = widget->p_values; value != NULL; value = value->p_next)
1072             {
1073                 [popup addItemWithTitle:[NSString stringWithUTF8String:value->psz_text]];
1074             }
1075             [popup synchronizeTitleAndSelectedItem];
1076             break;
1077         }
1078
1079         case EXTENSION_WIDGET_LIST:
1080         {
1081             assert([control isKindOfClass:[NSScrollView class]]);
1082             NSScrollView *scrollView = (NSScrollView *)control;
1083             assert([[scrollView documentView] isKindOfClass:[VLCDialogList class]]);
1084             VLCDialogList *list = (VLCDialogList *)[scrollView documentView];
1085
1086             NSMutableArray *contentArray = [NSMutableArray array];
1087             struct extension_widget_value_t *value;
1088             for(value = widget->p_values; value != NULL; value = value->p_next)
1089             {
1090                 NSDictionary *entry = [NSDictionary dictionaryWithObjectsAndKeys:
1091                                        [NSNumber numberWithInt:value->i_id], @"id",
1092                                        [NSString stringWithUTF8String:value->psz_text], @"text",
1093                                        nil];
1094                 [contentArray addObject:entry];
1095             }
1096             list.contentArray = contentArray;
1097             [list reloadData];
1098             break;
1099         }
1100         case EXTENSION_WIDGET_IMAGE:
1101         {
1102             assert([control isKindOfClass:[NSImageView class]]);
1103             NSImageView *imageView = (NSImageView *)control;
1104             NSString *string = widget->psz_text ? [NSString stringWithUTF8String:widget->psz_text] : nil;
1105             NSImage *image = nil;
1106             NSLog(@"Setting image to %@", string);
1107             if (string)
1108                 image = [[NSImage alloc] initWithContentsOfURL:[NSURL fileURLWithPath:string]];
1109             [imageView setImage:image];
1110             [image release];
1111             break;
1112         }
1113     }
1114
1115 }
1116
1117 - (void)updateWidgets:(extension_dialog_t *)dialog
1118 {
1119     extension_widget_t *widget;
1120     NSWindow *window = dialog->p_sys_intf;
1121     FOREACH_ARRAY(widget, dialog->widgets)
1122     {
1123         if (!widget)
1124             continue; /* Some widgets may be NULL at this point */
1125
1126         BOOL shouldDestroy = widget->b_kill;
1127         NSView *control = widget->p_sys_intf;
1128         BOOL update = widget->b_update;
1129
1130
1131         if (!control && !shouldDestroy)
1132         {
1133             control = createControlFromWidget(widget, self);
1134             updateControlFromWidget(control, widget);
1135             widget->p_sys_intf = control;
1136             update = YES; // Force update and repositionning
1137             [control setHidden:widget->b_hide];
1138         }
1139
1140         if (update && !shouldDestroy)
1141         {
1142             updateControlFromWidget(control, widget);
1143             [control setHidden:widget->b_hide];
1144
1145             int row = widget->i_row - 1;
1146             int col = widget->i_column - 1;
1147             int hsp = __MAX( 1, widget->i_horiz_span );
1148             int vsp = __MAX( 1, widget->i_vert_span );
1149             if( row < 0 )
1150             {
1151                 row = 4;
1152                 col = 0;
1153             }
1154
1155             VLCDialogGridView *gridView = (VLCDialogGridView *)[window contentView];
1156             [gridView addSubview:control atRow:row column:col rowSpan:vsp colSpan:hsp];
1157
1158             //this->resize( sizeHint() );
1159             widget->b_update = false;
1160         }
1161
1162         if (shouldDestroy)
1163         {
1164             VLCDialogGridView *gridView = (VLCDialogGridView *)[window contentView];
1165             [gridView removeSubview:control];
1166             [control release];
1167             widget->p_sys_intf = NULL;
1168         }
1169     }
1170     FOREACH_END()
1171 }
1172
1173 - (void)updateExtensionDialog:(NSValue *)extensionDialog
1174 {
1175     extension_dialog_t *dialog = [extensionDialog pointerValue];
1176
1177     vlc_mutex_lock(&dialog->lock);
1178
1179     NSSize size = NSMakeSize(dialog->i_width, dialog->i_height);
1180
1181     BOOL shouldDestroy = dialog->b_kill;
1182
1183     if (!dialog->i_width || !dialog->i_height)
1184         size = NSMakeSize(640, 480);
1185
1186     NSWindow *window = dialog->p_sys_intf;
1187     if (!window && !shouldDestroy)
1188     {
1189         NSRect content = NSMakeRect(0, 0, 1, 1);
1190         window = [[NSWindow alloc] initWithContentRect:content styleMask:NSTitledWindowMask | NSClosableWindowMask | NSResizableWindowMask backing:NSBackingStoreBuffered defer:NO];
1191         [window setTitle:[NSString stringWithUTF8String:dialog->psz_title]];
1192         VLCDialogGridView *gridView = [[VLCDialogGridView alloc] init];
1193         [gridView setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
1194         [window setContentView:gridView];
1195         [window setDelegate:gridView];
1196         [gridView release];
1197         dialog->p_sys_intf = window;
1198     }
1199
1200     [self updateWidgets:dialog];
1201
1202     if (shouldDestroy)
1203     {
1204         [window setDelegate:nil];
1205         [window close];
1206         dialog->p_sys_intf = NULL;
1207     }
1208
1209     if (!dialog->b_hide && ![window isVisible]) {
1210         [window center];
1211         [window makeKeyAndOrderFront:self];
1212     }
1213
1214     vlc_cond_signal(&dialog->cond);
1215     vlc_mutex_unlock(&dialog->lock);
1216 }
1217
1218
1219 /**
1220  * Helper to execute a function on main thread and get its return value.
1221  */
1222 - (void)execute:(NSDictionary *)dict
1223 {
1224     SEL sel = [[dict objectForKey:@"sel"] pointerValue];
1225     id *result = [[dict objectForKey:@"result"] pointerValue];
1226     id object = [dict objectForKey:@"object"];
1227
1228     NSAssert(sel, @"Try to execute a NULL selector");
1229
1230     *result = [self performSelector:sel withObject:object];
1231     [*result retain]; // Balanced in -resultFromSelectorOnMainThread
1232 }
1233
1234 - (id)resultFromSelectorOnMainThread:(SEL)sel withObject:(id)object
1235 {
1236     id result = nil;
1237     NSAssert(sel, @"Try to execute a NULL selector");
1238     NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
1239      [NSValue valueWithPointer:sel], @"sel",
1240      [NSValue valueWithPointer:&result], @"result",
1241      object, @"object", nil];
1242     [self performSelectorOnMainThread:@selector(execute:) withObject:dict waitUntilDone:YES];
1243     return [result autorelease];
1244 }
1245 @end
1246
1247
1248