]> git.sesse.net Git - vlc/blob - modules/gui/macosx_dialog_provider/dialogProvider.m
Qt: make win7 buttons smaller to avoid graphic bug
[vlc] / modules / gui / macosx_dialog_provider / dialogProvider.m
1 /*****************************************************************************
2  * dialogProvider.m: Minimal Dialog Provider for Mac OS X
3  *****************************************************************************
4  * Copyright (C) 2009-2011 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Felix Paul Kühne <fkuehne at videolan dot org>
8  *          Pierre d'Herbemont <pdherbemont # videolan dot>
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 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #import <stdlib.h>                                      /* malloc(), free() */
29 #import <string.h>
30
31 #ifdef HAVE_CONFIG_H
32 # import "config.h"
33 #endif
34
35 #import <vlc_common.h>
36 #import <vlc_plugin.h>
37 #import <vlc_dialog.h>
38 #import <vlc_extensions.h>
39 #import <vlc_modules.h>
40
41 #import "dialogProvider.h"
42 #import "VLCUIWidgets.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
63 static inline NSDictionary *DictFromDialogFatal(dialog_fatal_t *dialog) {
64     return [VLCDialogDisplayer dictionaryForDialog:dialog->title :dialog->message :NULL :NULL :NULL];
65 }
66 static inline NSDictionary *DictFromDialogLogin(dialog_login_t *dialog) {
67     return [VLCDialogDisplayer dictionaryForDialog:dialog->title :dialog->message :NULL :NULL :NULL];
68 }
69 static inline NSDictionary *DictFromDialogQuestion(dialog_question_t *dialog) {
70     return [VLCDialogDisplayer dictionaryForDialog:dialog->title :dialog->message :dialog->yes :dialog->no :dialog->cancel];
71 }
72 static inline NSDictionary *DictFromDialogProgressBar(dialog_progress_bar_t *dialog) {
73     return [VLCDialogDisplayer dictionaryForDialog:dialog->title :dialog->message :NULL :NULL :dialog->cancel];
74 }
75
76 struct intf_sys_t
77 {
78     VLCDialogDisplayer *displayer;
79
80     vlc_mutex_t lock;
81     vlc_cond_t wait;
82     bool is_hiding_noaction_dialogs;
83 };
84
85
86 #define T_HIDE_NOACTION N_("Hide no user action dialogs")
87 #define LT_HIDE_NOACTION N_("Don't display dialogs that don't require user action (Critical and error panel).")
88
89 #define prefix "macosx-dialog-provider"
90 /*****************************************************************************
91  * Module descriptor
92  *****************************************************************************/
93
94 vlc_module_begin()
95     /* Minimal interface. see intf.m */
96     set_shortname("Mac OS X Dialogs")
97     add_shortcut("macosx_dialog_provider", "miosx")
98     set_description("Minimal Mac OS X Dialog Provider")
99     set_capability("interface", 0)
100
101     /* This setting is interesting, because when used with a libvlc app
102      * it's almost certain that the client program will display error by
103      * itself. Moreover certain action might end up in an error, but
104      * the client wants to ignored them completely. */
105     add_bool(prefix "hide-no-user-action-dialogs", true, T_HIDE_NOACTION, LT_HIDE_NOACTION, false)
106
107     set_callbacks(OpenIntf, CloseIntf)
108     set_category(CAT_INTERFACE)
109     set_subcategory(SUBCAT_INTERFACE_MAIN)
110 vlc_module_end()
111
112 /*****************************************************************************
113  * OpenIntf: initialize interface
114  *****************************************************************************/
115 int OpenIntf(vlc_object_t *p_this)
116 {
117     intf_thread_t *p_intf = (intf_thread_t*) p_this;
118
119     p_intf->p_sys = malloc(sizeof(intf_sys_t));
120     if(!p_intf->p_sys)
121         return VLC_ENOMEM;
122
123     memset(p_intf->p_sys,0,sizeof(*p_intf->p_sys));
124
125     p_intf->p_sys->displayer = [[VLCDialogDisplayer alloc] init];
126     [p_intf->p_sys->displayer setIntf:p_intf];
127
128     bool hide = var_CreateGetBool(p_intf, prefix "hide-no-user-action-dialogs");
129     p_intf->p_sys->is_hiding_noaction_dialogs = hide;
130
131     /* subscribe to various interactive dialogues */
132
133     if (!hide)
134     {
135         var_Create(p_intf,"dialog-error",VLC_VAR_ADDRESS);
136         var_AddCallback(p_intf,"dialog-error",DisplayError,p_intf);
137         var_Create(p_intf,"dialog-critical",VLC_VAR_ADDRESS);
138         var_AddCallback(p_intf,"dialog-critical",DisplayCritical,p_intf);
139     }
140     var_Create(p_intf,"dialog-login",VLC_VAR_ADDRESS);
141     var_AddCallback(p_intf,"dialog-login",DisplayLogin,p_intf);
142     var_Create(p_intf,"dialog-question",VLC_VAR_ADDRESS);
143     var_AddCallback(p_intf,"dialog-question",DisplayQuestion,p_intf);
144     var_Create(p_intf,"dialog-progress-bar",VLC_VAR_ADDRESS);
145     var_AddCallback(p_intf,"dialog-progress-bar",DisplayProgressPanelAction,p_intf);
146     var_Create(p_intf,"dialog-extension",VLC_VAR_ADDRESS);
147     var_AddCallback(p_intf,"dialog-extension",DisplayExtension,p_intf);
148     dialog_Register(p_intf);
149
150     /* subscribe to our last.fm announcements */
151     [[NSDistributedNotificationCenter defaultCenter] addObserver:p_intf->p_sys->displayer
152                                                         selector:@selector(globalNotificationReceived:)
153                                                             name:NULL
154                                                           object:@"VLCLastFMSupport"
155                                               suspensionBehavior:NSNotificationSuspensionBehaviorDeliverImmediately];
156
157     msg_Dbg(p_intf,"Mac OS X dialog provider initialised");
158
159     return VLC_SUCCESS;
160 }
161
162 /*****************************************************************************
163  * CloseIntf: destroy interface
164  *****************************************************************************/
165 void CloseIntf(vlc_object_t *p_this)
166 {
167     intf_thread_t *p_intf = (intf_thread_t*) p_this;
168
169     /* unsubscribe from the interactive dialogues */
170     dialog_Unregister(p_intf);
171
172     if (!p_intf->p_sys->is_hiding_noaction_dialogs)
173     {
174         var_DelCallback(p_intf,"dialog-error",DisplayError,p_intf);
175         var_DelCallback(p_intf,"dialog-critical",DisplayCritical,p_intf);
176     }
177     var_DelCallback(p_intf,"dialog-login",DisplayLogin,p_intf);
178     var_DelCallback(p_intf,"dialog-question",DisplayQuestion,p_intf);
179     var_DelCallback(p_intf,"dialog-progress-bar",DisplayProgressPanelAction,p_intf);
180     var_DelCallback(p_intf,"dialog-extension",DisplayExtension,p_intf);
181
182     [p_intf->p_sys->displayer release];
183
184     msg_Dbg(p_intf,"Mac OS X dialog provider closed");
185     free(p_intf->p_sys);
186 }
187
188
189 /*****************************************************************************
190  * Callbacks triggered by the "dialog-*" variables
191  *****************************************************************************/
192 static int DisplayError(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
193 {
194     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
195     dialog_fatal_t *dialog = value.p_address;
196     intf_thread_t *p_intf = (intf_thread_t*) p_this;
197     intf_sys_t *sys = p_intf->p_sys;
198     [sys->displayer performSelectorOnMainThread:@selector(displayError:) withObject:DictFromDialogFatal(dialog) waitUntilDone:NO];
199     [pool release];
200     return VLC_SUCCESS;
201 }
202
203 static int DisplayCritical(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
204 {
205     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
206     dialog_fatal_t *dialog = value.p_address;
207     intf_thread_t *p_intf = (intf_thread_t*) p_this;
208     intf_sys_t *sys = p_intf->p_sys;
209     [sys->displayer performSelectorOnMainThread:@selector(displayCritical:) withObject:DictFromDialogFatal(dialog) waitUntilDone:NO];
210     [pool release];
211     return VLC_SUCCESS;
212 }
213
214 static int DisplayQuestion(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
215 {
216     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
217     dialog_question_t *dialog = value.p_address;
218     intf_thread_t *p_intf = (intf_thread_t*) p_this;
219     intf_sys_t *sys = p_intf->p_sys;
220     dialog->answer = [[sys->displayer resultFromSelectorOnMainThread:@selector(displayQuestion:) withObject:DictFromDialogQuestion(dialog)] intValue];
221     [pool release];
222     return VLC_SUCCESS;
223 }
224
225 static int DisplayLogin(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
226 {
227     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
228     dialog_login_t *dialog = value.p_address;
229     intf_thread_t *p_intf = (intf_thread_t*) p_this;
230     intf_sys_t *sys = p_intf->p_sys;
231     NSDictionary *dict = [sys->displayer resultFromSelectorOnMainThread:@selector(displayLogin:) withObject:DictFromDialogLogin(dialog)];
232     if (dict) {
233         *dialog->username = strdup([[dict objectForKey:@"username"] UTF8String]);
234         *dialog->password = strdup([[dict objectForKey:@"password"] UTF8String]);
235     }
236     [pool release];
237     return VLC_SUCCESS;
238 }
239
240 static int DisplayProgressPanelAction(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
241 {
242     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
243     dialog_progress_bar_t *dialog = value.p_address;
244     intf_thread_t *p_intf = (intf_thread_t*) p_this;
245     intf_sys_t *sys = p_intf->p_sys;
246
247     [sys->displayer performSelectorOnMainThread:@selector(displayProgressBar:) withObject:DictFromDialogProgressBar(dialog) waitUntilDone:YES];
248
249     dialog->pf_update = updateProgressPanel;
250     dialog->pf_check = checkProgressPanel;
251     dialog->pf_destroy = destroyProgressPanel;
252     dialog->p_sys = p_intf->p_sys;
253
254     [pool release];
255     return VLC_SUCCESS;
256 }
257
258 static int DisplayExtension(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
259 {
260     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
261     intf_thread_t *p_intf = (intf_thread_t*) p_this;
262     intf_sys_t *sys = p_intf->p_sys;
263     extension_dialog_t *dialog = value.p_address;
264
265     // -updateExtensionDialog: Open its own runloop, so be sure to run on DefaultRunLoop.
266     [sys->displayer performSelectorOnMainThread:@selector(updateExtensionDialog:) withObject:[NSValue valueWithPointer:dialog] waitUntilDone:YES];
267     [pool release];
268     return VLC_SUCCESS;
269 }
270
271
272 void updateProgressPanel (void *priv, const char *text, float value)
273 {
274     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
275     intf_sys_t *sys = (intf_sys_t *)priv;
276
277     NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
278                           [NSNumber numberWithFloat:value], @"value",
279                           text ? [NSString stringWithUTF8String:text] : nil, @"text",
280                           nil];
281
282     [sys->displayer performSelectorOnMainThread:@selector(updateProgressPanel:) withObject:dict waitUntilDone:YES];
283
284     [pool release];
285 }
286
287 void destroyProgressPanel (void *priv)
288 {
289     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
290     intf_sys_t *sys = (intf_sys_t *)priv;
291
292     [sys->displayer performSelectorOnMainThread:@selector(destroyProgressPanel) withObject:nil waitUntilDone:YES];
293
294     [pool release];
295 }
296
297 bool checkProgressPanel (void *priv)
298 {
299     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
300     intf_sys_t *sys = (intf_sys_t *)priv;
301     BOOL ret;
302
303     ret = [[sys->displayer resultFromSelectorOnMainThread:@selector(checkProgressPanel) withObject:nil] boolValue];
304
305     [pool release];
306     return ret;
307 }
308
309
310 @implementation VLCDialogDisplayer
311 - (void)dealloc
312 {
313     [[NSDistributedNotificationCenter defaultCenter] removeObserver:self];
314     assert(!_currentProgressBarPanel); // This has to be closed on main thread.
315     [super dealloc];
316 }
317
318 - (void)setIntf: (intf_thread_t *)p_mainintf
319 {
320     p_intf = p_mainintf;
321 }
322
323 - (intf_thread_t *)intf
324 {
325     return p_intf;
326 }
327
328 + (NSDictionary *)dictionaryForDialog:(const char *)title :(const char *)message :(const char *)yes :(const char *)no :(const char *)cancel
329 {
330     NSMutableDictionary *dict = [NSMutableDictionary dictionary];
331     if (title)
332         [dict setObject:[NSString stringWithUTF8String:title] forKey:@"title"];
333     if (message)
334         [dict setObject:[NSString stringWithUTF8String:message] forKey:@"message"];
335     if (yes)
336         [dict setObject:[NSString stringWithUTF8String:yes] forKey:@"yes"];
337     if (no)
338         [dict setObject:[NSString stringWithUTF8String:no] forKey:@"no"];
339     if (cancel)
340         [dict setObject:[NSString stringWithUTF8String:cancel] forKey:@"cancel"];
341
342     return dict;
343 }
344 #define VLCAssertIsMainThread() assert([NSThread isMainThread])
345
346 - (void)displayError:(NSDictionary *)dialog
347 {
348     VLCAssertIsMainThread();
349
350     NSRunInformationalAlertPanel([dialog objectForKey:@"title"],
351                                  [dialog objectForKey:@"message"],
352                                  @"OK", nil, nil);
353 }
354
355 - (void)displayCritical:(NSDictionary *)dialog
356 {
357     VLCAssertIsMainThread();
358
359     NSRunCriticalAlertPanel([dialog objectForKey:@"title"],
360                                  [dialog objectForKey:@"message"],
361                                  @"OK", nil, nil);
362 }
363
364 - (NSNumber *)displayQuestion:(NSDictionary *)dialog
365 {
366     VLCAssertIsMainThread();
367
368     NSInteger alertRet = 0;
369
370     NSAlert *alert = [NSAlert alertWithMessageText:[dialog objectForKey:@"title"]
371                               defaultButton:[dialog objectForKey:@"yes"]
372                             alternateButton:[dialog objectForKey:@"no"]
373                                 otherButton:[dialog objectForKey:@"cancel"]
374                   informativeTextWithFormat:[dialog objectForKey:@"message"]];
375     [alert setAlertStyle:NSInformationalAlertStyle];
376     alertRet = [alert runModal];
377
378     int ret;
379     switch (alertRet) {
380         case NSAlertDefaultReturn:
381             ret = 1;
382             break;
383         case NSAlertAlternateReturn:
384             ret = 2;
385             break;
386         case NSAlertOtherReturn:
387             ret = 3;
388             break;
389         default:
390             assert(0);
391             ret = 0;
392             break;
393     }
394
395     return [NSNumber numberWithInt:ret];
396 }
397
398 - (NSDictionary *)displayLogin:(NSDictionary *)dialog
399 {
400     VLCAssertIsMainThread();
401
402     VLCLoginPanel *panel = [[VLCLoginPanel alloc] init];
403     [panel createContentView];
404     [panel setDialogTitle:[dialog objectForKey:@"title"]];
405     [panel setDialogMessage:[dialog objectForKey:@"message"]];
406     [panel center];
407     NSInteger ret = [NSApp runModalForWindow:panel];
408     [panel close];
409
410     if (!ret)
411         return nil;
412
413     return [NSDictionary dictionaryWithObjectsAndKeys:
414             [panel userName], @"username",
415             [panel password], @"password",
416             nil];
417 }
418
419 - (void)displayProgressBar:(NSDictionary *)dialog
420 {
421     VLCAssertIsMainThread();
422
423     if(_currentProgressBarPanel)
424         [self destroyProgressPanel];
425
426     assert(!_currentProgressBarPanel);
427     _currentProgressBarPanel = [[VLCProgressPanel alloc] init];
428     [_currentProgressBarPanel createContentView];
429     [_currentProgressBarPanel setDialogTitle:[dialog objectForKey:@"title"]];
430     [_currentProgressBarPanel setDialogMessage:[dialog objectForKey:@"message"] ?: @""];
431     [_currentProgressBarPanel setCancelButtonLabel:[dialog objectForKey:@"cancel"]];
432
433     [_currentProgressBarPanel center];
434     [_currentProgressBarPanel makeKeyAndOrderFront:nil];
435 }
436
437 - (void)updateProgressPanel:(NSDictionary *)dict
438 {
439     VLCAssertIsMainThread();
440
441     assert(_currentProgressBarPanel);
442     [_currentProgressBarPanel setDialogMessage:[dict objectForKey:@"text"] ?: @""];
443     [_currentProgressBarPanel setProgressAsDouble:[[dict objectForKey:@"value"] doubleValue] * 1000.];
444 }
445
446 - (void)destroyProgressPanel
447 {
448     VLCAssertIsMainThread();
449
450     [_currentProgressBarPanel close];
451     [_currentProgressBarPanel release];
452     _currentProgressBarPanel = nil;
453 }
454
455 - (NSNumber *)checkProgressPanel
456 {
457     VLCAssertIsMainThread();
458
459     return [NSNumber numberWithBool:[_currentProgressBarPanel isCancelled]];
460 }
461
462 #pragma mark -
463 #pragma mark Last.FM support
464 - (void)globalNotificationReceived: (NSNotification *)theNotification
465 {
466     NSDictionary *userData = [theNotification userInfo];
467     BOOL lastFMEnabled = [[userData objectForKey:@"enabled"] intValue];
468     NSString *lastFMUsername = [userData objectForKey:@"username"];
469     NSString *lastFMPassword = [userData objectForKey:@"password"];
470
471     if (module_exists("audioscrobbler")) {
472         if (lastFMEnabled)
473             config_AddIntf(p_intf, "audioscrobbler");
474         else
475             config_RemoveIntf(p_intf, "audioscrobbler");
476
477         config_PutPsz(p_intf, "lastfm-username", [lastFMUsername UTF8String]);
478         config_PutPsz(p_intf, "lastfm-password", [lastFMPassword UTF8String]);
479     }
480     else
481         msg_Err(p_intf,"Last.FM module not found, no action");
482 }
483
484 #pragma mark -
485 #pragma mark Extensions Dialog
486
487 - (void)triggerClick:(id)sender
488 {
489     assert([sender isKindOfClass:[VLCDialogButton class]]);
490     VLCDialogButton *button = sender;
491     extension_widget_t *widget = [button widget];
492
493     vlc_mutex_lock(&widget->p_dialog->lock);
494     extension_WidgetClicked(widget->p_dialog, widget);
495     vlc_mutex_unlock(&widget->p_dialog->lock);
496 }
497
498 - (void)syncTextField:(NSNotification *)notifcation
499 {
500     id sender = [notifcation object];
501     assert([sender isKindOfClass:[VLCDialogTextField class]]);
502     VLCDialogTextField *field = sender;
503     extension_widget_t *widget = [field widget];
504
505     vlc_mutex_lock(&widget->p_dialog->lock);
506     free(widget->psz_text);
507     widget->psz_text = strdup([[field stringValue] UTF8String]);
508     vlc_mutex_unlock(&widget->p_dialog->lock);
509 }
510
511 - (void)tableViewSelectionDidChange:(NSNotification *)notifcation
512 {
513     id sender = [notifcation object];
514     assert(sender && [sender isKindOfClass:[VLCDialogList class]]);
515     VLCDialogList *list = sender;
516
517     struct extension_widget_value_t *value;
518     unsigned i = 0;
519     for(value = [list widget]->p_values; value != NULL; value = value->p_next, i++)
520         value->b_selected = (i == [list selectedRow]);
521 }
522
523 - (void)popUpSelectionChanged:(id)sender
524 {
525     assert([sender isKindOfClass:[VLCDialogPopUpButton class]]);
526     VLCDialogPopUpButton *popup = sender;
527     struct extension_widget_value_t *value;
528     unsigned i = 0;
529     for(value = [popup widget]->p_values; value != NULL; value = value->p_next, i++)
530         value->b_selected = (i == [popup indexOfSelectedItem]);
531
532 }
533
534 - (NSSize)windowWillResize:(NSWindow *)sender toSize:(NSSize)frameSize
535 {
536     NSView *contentView = [sender contentView];
537     assert([contentView isKindOfClass:[VLCDialogGridView class]]);
538     VLCDialogGridView *gridView = (VLCDialogGridView *)contentView;
539
540     NSRect rect = NSMakeRect(0, 0, 0, 0);
541     rect.size = frameSize;
542     rect = [sender contentRectForFrameRect:rect];
543     rect.size = [gridView flexSize:rect.size];
544     rect = [sender frameRectForContentRect:rect];
545     return rect.size;
546 }
547
548 - (BOOL)windowShouldClose:(id)sender
549 {
550     assert([sender isKindOfClass:[VLCDialogWindow class]]);
551     VLCDialogWindow *window = sender;
552     extension_dialog_t *dialog = [window dialog];
553     extension_DialogClosed(dialog);
554     dialog->p_sys_intf = NULL;
555     return YES;
556 }
557
558 static NSView *createControlFromWidget(extension_widget_t *widget, id self)
559 {
560     assert(!widget->p_sys_intf);
561
562     switch (widget->type)
563     {
564         case EXTENSION_WIDGET_HTML:
565         {
566 //            NSScrollView *scrollView = [[NSScrollView alloc] init];
567 //            [scrollView setHasVerticalScroller:YES];
568 //            NSTextView *field = [[NSTextView alloc] init];
569 //            [scrollView setDocumentView:field];
570 //            [scrollView setAutoresizesSubviews:YES];
571 //            [scrollView setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
572 //            [field release];
573 //            return scrollView;
574             NSTextView *field = [[NSTextView alloc] init];
575             [field setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
576             [field setDrawsBackground:NO];
577             return field;
578         }
579         case EXTENSION_WIDGET_LABEL:
580         {
581             NSTextField *field = [[NSTextField alloc] init];
582             [field setEditable:NO];
583             [field setBordered:NO];
584             [field setDrawsBackground:NO];
585             [field setFont:[NSFont systemFontOfSize:0]];
586             [[field cell] setControlSize:NSRegularControlSize];
587             [field setAutoresizingMask:NSViewNotSizable];
588             return field;
589         }
590         case EXTENSION_WIDGET_TEXT_FIELD:
591         {
592             VLCDialogTextField *field = [[VLCDialogTextField alloc] init];
593             [field setWidget:widget];
594             [field setAutoresizingMask:NSViewWidthSizable];
595             [field setFont:[NSFont systemFontOfSize:0]];
596             [[field cell] setControlSize:NSRegularControlSize];
597             [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(syncTextField:)  name:NSControlTextDidChangeNotification object:field];
598             return field;
599         }
600         case EXTENSION_WIDGET_BUTTON:
601         {
602             VLCDialogButton *button = [[VLCDialogButton alloc] init];
603             [button setBezelStyle:NSRoundedBezelStyle];
604             [button setWidget:widget];
605             [button setAction:@selector(triggerClick:)];
606             [button setTarget:self];
607             [[button cell] setControlSize:NSRegularControlSize];
608             [button setAutoresizingMask:NSViewNotSizable];
609             return button;
610         }
611         case EXTENSION_WIDGET_DROPDOWN:
612         {
613             VLCDialogPopUpButton *popup = [[VLCDialogPopUpButton alloc] init];
614             [popup setAction:@selector(popUpSelectionChanged:)];
615             [popup setTarget:self];
616             [popup setWidget:widget];
617             return popup;
618         }
619         case EXTENSION_WIDGET_LIST:
620         {
621             NSScrollView *scrollView = [[NSScrollView alloc] init];
622             [scrollView setHasVerticalScroller:YES];
623             VLCDialogList *list = [[VLCDialogList alloc] init];
624             [list setUsesAlternatingRowBackgroundColors:YES];
625             [list setHeaderView:nil];
626             [scrollView setDocumentView:list];
627             [scrollView setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
628
629             NSTableColumn *column = [[NSTableColumn alloc] init];
630             [list addTableColumn:column];
631             [column release];
632             [list setDataSource:list];
633             [list setDelegate:self];
634             [list setWidget:widget];
635             [list release];
636             return scrollView;
637         }
638         case EXTENSION_WIDGET_IMAGE:
639         {
640             NSImageView *imageView = [[NSImageView alloc] init];
641             [imageView setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
642             [imageView setImageFrameStyle:NSImageFramePhoto];
643             [imageView setImageScaling:NSImageScaleProportionallyUpOrDown];
644             return imageView;
645         }
646         case EXTENSION_WIDGET_SPIN_ICON:
647         {
648             NSProgressIndicator *spinner = [[NSProgressIndicator alloc] init];
649             [spinner setUsesThreadedAnimation:YES];
650             [spinner setStyle:NSProgressIndicatorSpinningStyle];
651             [spinner setDisplayedWhenStopped:YES];
652             [spinner startAnimation:self];
653             return spinner;
654         }
655         default:
656             assert(0);
657             return nil;
658     }
659
660 }
661
662 static void updateControlFromWidget(NSView *control, extension_widget_t *widget, id self)
663 {
664     switch (widget->type)
665     {
666         case EXTENSION_WIDGET_HTML:
667 //        {
668 //            // Get the scroll view
669 //            assert([control isKindOfClass:[NSScrollView class]]);
670 //            NSScrollView *scrollView = (NSScrollView *)control;
671 //            control = [scrollView documentView];
672 //
673 //            assert([control isKindOfClass:[NSTextView class]]);
674 //            NSTextView *textView = (NSTextView *)control;
675 //            NSString *string = [NSString stringWithUTF8String:widget->psz_text];
676 //            NSAttributedString *attrString = [[NSAttributedString alloc] initWithHTML:[string dataUsingEncoding:NSISOLatin1StringEncoding] documentAttributes:NULL];
677 //            [[textView textStorage] setAttributedString:[[NSAttributedString alloc] initWithString:@"Hello"]];
678 //            [textView setNeedsDisplay:YES];
679 //            [textView scrollRangeToVisible:NSMakeRange(0, 0)];
680 //            [attrString release];
681 //            break;
682 //
683 //        }
684         {
685             assert([control isKindOfClass:[NSTextView class]]);
686             NSTextView *textView = (NSTextView *)control;
687             NSString *string = [NSString stringWithUTF8String:widget->psz_text];
688             NSAttributedString *attrString = [[NSAttributedString alloc] initWithHTML:[string dataUsingEncoding:NSISOLatin1StringEncoding] documentAttributes:NULL];
689             [[textView textStorage] setAttributedString:attrString];
690             [textView setNeedsDisplay:YES];
691             [textView scrollRangeToVisible:NSMakeRange(0, 0)];
692             [attrString release];
693             break;
694
695         }
696         case EXTENSION_WIDGET_LABEL:
697         case EXTENSION_WIDGET_PASSWORD:
698         case EXTENSION_WIDGET_TEXT_FIELD:
699         {
700             if (!widget->psz_text)
701                 break;
702             assert([control isKindOfClass:[NSControl class]]);
703             NSControl *field = (NSControl *)control;
704             NSString *string = [NSString stringWithUTF8String:widget->psz_text];
705             NSAttributedString *attrString = [[NSAttributedString alloc] initWithHTML:[string dataUsingEncoding:NSISOLatin1StringEncoding] documentAttributes:NULL];
706             [field setAttributedStringValue:attrString];
707             [attrString release];
708             break;
709         }
710         case EXTENSION_WIDGET_CHECK_BOX:
711         case EXTENSION_WIDGET_BUTTON:
712         {
713             assert([control isKindOfClass:[NSButton class]]);
714             NSButton *button = (NSButton *)control;
715             if (!widget->psz_text)
716                 break;
717             [button setTitle:[NSString stringWithUTF8String:widget->psz_text]];
718             break;
719         }
720         case EXTENSION_WIDGET_DROPDOWN:
721         {
722             assert([control isKindOfClass:[NSPopUpButton class]]);
723             NSPopUpButton *popup = (NSPopUpButton *)control;
724             [popup removeAllItems];
725             struct extension_widget_value_t *value;
726             for(value = widget->p_values; value != NULL; value = value->p_next)
727             {
728                 [popup addItemWithTitle:[NSString stringWithUTF8String:value->psz_text]];
729             }
730             [popup synchronizeTitleAndSelectedItem];
731             [self popUpSelectionChanged:popup];
732             break;
733         }
734
735         case EXTENSION_WIDGET_LIST:
736         {
737             assert([control isKindOfClass:[NSScrollView class]]);
738             NSScrollView *scrollView = (NSScrollView *)control;
739             assert([[scrollView documentView] isKindOfClass:[VLCDialogList class]]);
740             VLCDialogList *list = (VLCDialogList *)[scrollView documentView];
741
742             NSMutableArray *contentArray = [NSMutableArray array];
743             struct extension_widget_value_t *value;
744             for(value = widget->p_values; value != NULL; value = value->p_next)
745             {
746                 NSDictionary *entry = [NSDictionary dictionaryWithObjectsAndKeys:
747                                        [NSNumber numberWithInt:value->i_id], @"id",
748                                        [NSString stringWithUTF8String:value->psz_text], @"text",
749                                        nil];
750                 [contentArray addObject:entry];
751             }
752             list.contentArray = contentArray;
753             [list reloadData];
754             break;
755         }
756         case EXTENSION_WIDGET_IMAGE:
757         {
758             assert([control isKindOfClass:[NSImageView class]]);
759             NSImageView *imageView = (NSImageView *)control;
760             NSString *string = widget->psz_text ? [NSString stringWithUTF8String:widget->psz_text] : nil;
761             NSImage *image = nil;
762             if (string)
763                 image = [[NSImage alloc] initWithContentsOfURL:[NSURL fileURLWithPath:string]];
764             [imageView setImage:image];
765             [image release];
766             break;
767         }
768         case EXTENSION_WIDGET_SPIN_ICON:
769         {
770             assert([control isKindOfClass:[NSProgressIndicator class]]);
771             NSProgressIndicator *progressIndicator = (NSProgressIndicator *)control;
772             if( widget->i_spin_loops != 0 )
773                 [progressIndicator startAnimation:self];
774             else
775                 [progressIndicator stopAnimation:self];
776             break;
777         }
778     }
779
780 }
781
782 - (void)updateWidgets:(extension_dialog_t *)dialog
783 {
784     extension_widget_t *widget;
785     NSWindow *window = dialog->p_sys_intf;
786     FOREACH_ARRAY(widget, dialog->widgets)
787     {
788         if (!widget)
789             continue; /* Some widgets may be NULL at this point */
790
791         BOOL shouldDestroy = widget->b_kill;
792         NSView *control = widget->p_sys_intf;
793         BOOL update = widget->b_update;
794
795
796         if (!control && !shouldDestroy)
797         {
798             control = createControlFromWidget(widget, self);
799             updateControlFromWidget(control, widget, self);
800             widget->p_sys_intf = control;
801             update = YES; // Force update and repositionning
802             [control setHidden:widget->b_hide];
803         }
804
805         if (update && !shouldDestroy)
806         {
807             updateControlFromWidget(control, widget, self);
808             [control setHidden:widget->b_hide];
809
810             int row = widget->i_row - 1;
811             int col = widget->i_column - 1;
812             int hsp = __MAX( 1, widget->i_horiz_span );
813             int vsp = __MAX( 1, widget->i_vert_span );
814             if( row < 0 )
815             {
816                 row = 4;
817                 col = 0;
818             }
819
820             VLCDialogGridView *gridView = (VLCDialogGridView *)[window contentView];
821             [gridView addSubview:control atRow:row column:col rowSpan:vsp colSpan:hsp];
822
823             //this->resize( sizeHint() );
824             widget->b_update = false;
825         }
826
827         if (shouldDestroy)
828         {
829             VLCDialogGridView *gridView = (VLCDialogGridView *)[window contentView];
830             [gridView removeSubview:control];
831             [control release];
832             widget->p_sys_intf = NULL;
833         }
834     }
835     FOREACH_END()
836 }
837
838 - (void)updateExtensionDialog:(NSValue *)extensionDialog
839 {
840     extension_dialog_t *dialog = [extensionDialog pointerValue];
841
842     vlc_mutex_lock(&dialog->lock);
843
844     NSSize size = NSMakeSize(dialog->i_width, dialog->i_height);
845
846     BOOL shouldDestroy = dialog->b_kill;
847
848     if (!dialog->i_width || !dialog->i_height)
849         size = NSMakeSize(640, 480);
850
851     VLCDialogWindow *window = dialog->p_sys_intf;
852     if (!window && !shouldDestroy)
853     {
854         NSRect content = NSMakeRect(0, 0, 1, 1);
855         window = [[VLCDialogWindow alloc] initWithContentRect:content styleMask:NSTitledWindowMask | NSClosableWindowMask | NSResizableWindowMask backing:NSBackingStoreBuffered defer:NO];
856         [window setDelegate:self];
857         [window setDialog:dialog];
858         [window setTitle:[NSString stringWithUTF8String:dialog->psz_title]];
859         VLCDialogGridView *gridView = [[VLCDialogGridView alloc] init];
860         [gridView setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
861         [window setContentView:gridView];
862         [gridView release];
863         dialog->p_sys_intf = window;
864     }
865
866     [self updateWidgets:dialog];
867
868     if (shouldDestroy)
869     {
870         [window setDelegate:nil];
871         [window close];
872         dialog->p_sys_intf = NULL;
873         window = nil;
874     }
875
876     if (![window isVisible]) {
877         [window center];
878         [window makeKeyAndOrderFront:self];
879     }
880
881     vlc_cond_signal(&dialog->cond);
882     vlc_mutex_unlock(&dialog->lock);
883 }
884
885
886 /**
887  * Helper to execute a function on main thread and get its return value.
888  */
889 - (void)execute:(NSDictionary *)dict
890 {
891     SEL sel = [[dict objectForKey:@"sel"] pointerValue];
892     id *result = [[dict objectForKey:@"result"] pointerValue];
893     id object = [dict objectForKey:@"object"];
894
895     NSAssert(sel, @"Try to execute a NULL selector");
896
897     *result = [self performSelector:sel withObject:object];
898     [*result retain]; // Balanced in -resultFromSelectorOnMainThread
899 }
900
901 - (id)resultFromSelectorOnMainThread:(SEL)sel withObject:(id)object
902 {
903     id result = nil;
904     NSAssert(sel, @"Try to execute a NULL selector");
905     NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
906      [NSValue valueWithPointer:sel], @"sel",
907      [NSValue valueWithPointer:&result], @"result",
908      object, @"object", nil];
909     [self performSelectorOnMainThread:@selector(execute:) withObject:dict waitUntilDone:YES];
910     return [result autorelease];
911 }
912 @end
913
914
915