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