]> git.sesse.net Git - vlc/blob - modules/gui/macosx_dialog_provider/dialogProvider.m
macosx dialog provider: moved UI widgets out of the interface implementation
[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")
97     add_shortcut("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, NULL, 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_hidding_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_hidding_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     NSLog(@"globalNotificationReceived");
467     NSDictionary *userData = [theNotification userInfo];
468     BOOL lastFMEnabled = [[userData objectForKey:@"enabled"] intValue];
469     NSString *lastFMUsername = [userData objectForKey:@"username"];
470     NSString *lastFMPassword = [userData objectForKey:@"password"];
471
472     if (module_exists("audioscrobbler")) {
473         if (lastFMEnabled)
474             config_AddIntf(p_intf, "audioscrobbler");
475         else
476             config_RemoveIntf(p_intf, "audioscrobbler");
477
478         config_PutPsz(p_intf, "lastfm-username", [lastFMUsername UTF8String]);
479         config_PutPsz(p_intf, "lastfm-password", [lastFMPassword UTF8String]);
480         config_SaveConfigFile(p_intf, "main");
481         config_SaveConfigFile(p_intf, "audioscrobbler");
482     }
483     else
484         msg_Err(p_intf,"Last.FM module not found, no action");
485 }
486
487 #pragma mark -
488 #pragma mark Extensions Dialog
489
490 - (void)triggerClick:(id)sender
491 {
492     assert([sender isKindOfClass:[VLCDialogButton class]]);
493     VLCDialogButton *button = sender;
494     extension_widget_t *widget = [button widget];
495
496     NSLog(@"(triggerClick)");
497     vlc_mutex_lock(&widget->p_dialog->lock);
498     extension_WidgetClicked(widget->p_dialog, widget);
499     vlc_mutex_unlock(&widget->p_dialog->lock);
500 }
501
502 - (void)syncTextField:(NSNotification *)notifcation
503 {
504     id sender = [notifcation object];
505     assert([sender isKindOfClass:[VLCDialogTextField class]]);
506     VLCDialogTextField *field = sender;
507     extension_widget_t *widget = [field widget];
508
509     vlc_mutex_lock(&widget->p_dialog->lock);
510     free(widget->psz_text);
511     widget->psz_text = strdup([[field stringValue] UTF8String]);
512     vlc_mutex_unlock(&widget->p_dialog->lock);
513 }
514
515 - (void)tableViewSelectionDidChange:(NSNotification *)notifcation
516 {
517     id sender = [notifcation object];
518     assert(sender && [sender isKindOfClass:[VLCDialogList class]]);
519     VLCDialogList *list = sender;
520
521     struct extension_widget_value_t *value;
522     unsigned i = 0;
523     for(value = [list widget]->p_values; value != NULL; value = value->p_next, i++)
524         value->b_selected = (i == [list selectedRow]);
525 }
526
527 - (void)popUpSelectionChanged:(id)sender
528 {
529     assert([sender isKindOfClass:[VLCDialogPopUpButton class]]);
530     VLCDialogPopUpButton *popup = sender;
531     struct extension_widget_value_t *value;
532     unsigned i = 0;
533     for(value = [popup widget]->p_values; value != NULL; value = value->p_next, i++)
534         value->b_selected = (i == [popup indexOfSelectedItem]);
535
536 }
537
538 - (NSSize)windowWillResize:(NSWindow *)sender toSize:(NSSize)frameSize
539 {
540     NSView *contentView = [sender contentView];
541     assert([contentView isKindOfClass:[VLCDialogGridView class]]);
542     VLCDialogGridView *gridView = (VLCDialogGridView *)contentView;
543
544     NSRect rect = NSMakeRect(0, 0, 0, 0);
545     rect.size = frameSize;
546     rect = [sender contentRectForFrameRect:rect];
547     rect.size = [gridView flexSize:rect.size];
548     rect = [sender frameRectForContentRect:rect];
549     return rect.size;
550 }
551
552 - (BOOL)windowShouldClose:(id)sender
553 {
554     assert([sender isKindOfClass:[VLCDialogWindow class]]);
555     VLCDialogWindow *window = sender;
556     extension_dialog_t *dialog = [window dialog];
557     extension_DialogClosed(dialog);
558     dialog->p_sys_intf = NULL;
559     return YES;
560 }
561
562 static NSView *createControlFromWidget(extension_widget_t *widget, id self)
563 {
564     assert(!widget->p_sys_intf);
565
566     switch (widget->type)
567     {
568         case EXTENSION_WIDGET_HTML:
569         {
570 //            NSScrollView *scrollView = [[NSScrollView alloc] init];
571 //            [scrollView setHasVerticalScroller:YES];
572 //            NSTextView *field = [[NSTextView alloc] init];
573 //            [scrollView setDocumentView:field];
574 //            [scrollView setAutoresizesSubviews:YES];
575 //            [scrollView setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
576 //            [field release];
577 //            return scrollView;
578             NSTextView *field = [[NSTextView alloc] init];
579             [field setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
580             [field setDrawsBackground:NO];
581             return field;
582         }
583         case EXTENSION_WIDGET_LABEL:
584         {
585             NSTextField *field = [[NSTextField alloc] init];
586             [field setEditable:NO];
587             [field setBordered:NO];
588             [field setDrawsBackground:NO];
589             [field setFont:[NSFont systemFontOfSize:0]];
590             [[field cell] setControlSize:NSRegularControlSize];
591             [field setAutoresizingMask:NSViewNotSizable];
592             return field;
593         }
594         case EXTENSION_WIDGET_TEXT_FIELD:
595         {
596             VLCDialogTextField *field = [[VLCDialogTextField alloc] init];
597             [field setWidget:widget];
598             [field setAutoresizingMask:NSViewWidthSizable];
599             [field setFont:[NSFont systemFontOfSize:0]];
600             [[field cell] setControlSize:NSRegularControlSize];
601             [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(syncTextField:)  name:NSControlTextDidChangeNotification object:field];
602             return field;
603         }
604         case EXTENSION_WIDGET_BUTTON:
605         {
606             VLCDialogButton *button = [[VLCDialogButton alloc] init];
607             [button setBezelStyle:NSRoundedBezelStyle];
608             [button setWidget:widget];
609             [button setAction:@selector(triggerClick:)];
610             [button setTarget:self];
611             [[button cell] setControlSize:NSRegularControlSize];
612             [button setAutoresizingMask:NSViewNotSizable];
613             return button;
614         }
615         case EXTENSION_WIDGET_DROPDOWN:
616         {
617             VLCDialogPopUpButton *popup = [[VLCDialogPopUpButton alloc] init];
618             [popup setAction:@selector(popUpSelectionChanged:)];
619             [popup setTarget:self];
620             [popup setWidget:widget];
621             return popup;
622         }
623         case EXTENSION_WIDGET_LIST:
624         {
625             NSScrollView *scrollView = [[NSScrollView alloc] init];
626             [scrollView setHasVerticalScroller:YES];
627             VLCDialogList *list = [[VLCDialogList alloc] init];
628             [list setUsesAlternatingRowBackgroundColors:YES];
629             [list setHeaderView:nil];
630             [scrollView setDocumentView:list];
631             [scrollView setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
632
633             NSTableColumn *column = [[NSTableColumn alloc] init];
634             [list addTableColumn:column];
635             [column release];
636             [list setDataSource:list];
637             [list setDelegate:self];
638             [list setWidget:widget];
639             [list release];
640             return scrollView;
641         }
642         case EXTENSION_WIDGET_IMAGE:
643         {
644             NSImageView *imageView = [[NSImageView alloc] init];
645             [imageView setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
646             [imageView setImageFrameStyle:NSImageFramePhoto];
647             [imageView setImageScaling:NSImageScaleProportionallyUpOrDown];
648             return imageView;
649         }
650         default:
651             assert(0);
652             return nil;
653     }
654
655 }
656
657 static void updateControlFromWidget(NSView *control, extension_widget_t *widget, id self)
658 {
659     switch (widget->type)
660     {
661         case EXTENSION_WIDGET_HTML:
662 //        {
663 //            // Get the scroll view
664 //            assert([control isKindOfClass:[NSScrollView class]]);
665 //            NSScrollView *scrollView = (NSScrollView *)control;
666 //            control = [scrollView documentView];
667 //
668 //            assert([control isKindOfClass:[NSTextView class]]);
669 //            NSTextView *textView = (NSTextView *)control;
670 //            NSString *string = [NSString stringWithUTF8String:widget->psz_text];
671 //            NSAttributedString *attrString = [[NSAttributedString alloc] initWithHTML:[string dataUsingEncoding:NSUTF8StringEncoding] documentAttributes:NULL];
672 //            [[textView textStorage] setAttributedString:[[NSAttributedString alloc] initWithString:@"Hello"]];
673 //            NSLog(@"%@", string);
674 //            [textView setNeedsDisplay:YES];
675 //            [textView scrollRangeToVisible:NSMakeRange(0, 0)];
676 //            [attrString release];
677 //            break;
678 //
679 //        }
680         {
681             assert([control isKindOfClass:[NSTextView class]]);
682             NSTextView *textView = (NSTextView *)control;
683             NSString *string = [NSString stringWithUTF8String:widget->psz_text];
684             NSAttributedString *attrString = [[NSAttributedString alloc] initWithHTML:[string dataUsingEncoding:NSUTF8StringEncoding] documentAttributes:NULL];
685             [[textView textStorage] setAttributedString:attrString];
686             [textView setNeedsDisplay:YES];
687             [textView scrollRangeToVisible:NSMakeRange(0, 0)];
688             [attrString release];
689             break;
690
691         }
692         case EXTENSION_WIDGET_LABEL:
693         case EXTENSION_WIDGET_PASSWORD:
694         case EXTENSION_WIDGET_TEXT_FIELD:
695         {
696             if (!widget->psz_text)
697                 break;
698             assert([control isKindOfClass:[NSControl class]]);
699             NSControl *field = (NSControl *)control;
700             NSString *string = [NSString stringWithUTF8String:widget->psz_text];
701             NSAttributedString *attrString = [[NSAttributedString alloc] initWithHTML:[string dataUsingEncoding:NSUTF8StringEncoding] documentAttributes:NULL];
702             [field setAttributedStringValue:attrString];
703             [attrString release];
704             break;
705         }
706         case EXTENSION_WIDGET_BUTTON:
707         {
708             assert([control isKindOfClass:[NSButton class]]);
709             NSButton *button = (NSButton *)control;
710             if (!widget->psz_text)
711                 break;
712             [button setTitle:[NSString stringWithUTF8String:widget->psz_text]];
713             break;
714         }
715         case EXTENSION_WIDGET_DROPDOWN:
716         {
717             assert([control isKindOfClass:[NSPopUpButton class]]);
718             NSPopUpButton *popup = (NSPopUpButton *)control;
719             [popup removeAllItems];
720             struct extension_widget_value_t *value;
721             for(value = widget->p_values; value != NULL; value = value->p_next)
722             {
723                 [popup addItemWithTitle:[NSString stringWithUTF8String:value->psz_text]];
724             }
725             [popup synchronizeTitleAndSelectedItem];
726             [self popUpSelectionChanged:popup];
727             break;
728         }
729
730         case EXTENSION_WIDGET_LIST:
731         {
732             assert([control isKindOfClass:[NSScrollView class]]);
733             NSScrollView *scrollView = (NSScrollView *)control;
734             assert([[scrollView documentView] isKindOfClass:[VLCDialogList class]]);
735             VLCDialogList *list = (VLCDialogList *)[scrollView documentView];
736
737             NSMutableArray *contentArray = [NSMutableArray array];
738             struct extension_widget_value_t *value;
739             for(value = widget->p_values; value != NULL; value = value->p_next)
740             {
741                 NSDictionary *entry = [NSDictionary dictionaryWithObjectsAndKeys:
742                                        [NSNumber numberWithInt:value->i_id], @"id",
743                                        [NSString stringWithUTF8String:value->psz_text], @"text",
744                                        nil];
745                 [contentArray addObject:entry];
746             }
747             list.contentArray = contentArray;
748             [list reloadData];
749             break;
750         }
751         case EXTENSION_WIDGET_IMAGE:
752         {
753             assert([control isKindOfClass:[NSImageView class]]);
754             NSImageView *imageView = (NSImageView *)control;
755             NSString *string = widget->psz_text ? [NSString stringWithUTF8String:widget->psz_text] : nil;
756             NSImage *image = nil;
757             NSLog(@"Setting image to %@", string);
758             if (string)
759                 image = [[NSImage alloc] initWithContentsOfURL:[NSURL fileURLWithPath:string]];
760             [imageView setImage:image];
761             [image release];
762             break;
763         }
764     }
765
766 }
767
768 - (void)updateWidgets:(extension_dialog_t *)dialog
769 {
770     extension_widget_t *widget;
771     NSWindow *window = dialog->p_sys_intf;
772     FOREACH_ARRAY(widget, dialog->widgets)
773     {
774         if (!widget)
775             continue; /* Some widgets may be NULL at this point */
776
777         BOOL shouldDestroy = widget->b_kill;
778         NSView *control = widget->p_sys_intf;
779         BOOL update = widget->b_update;
780
781
782         if (!control && !shouldDestroy)
783         {
784             control = createControlFromWidget(widget, self);
785             updateControlFromWidget(control, widget, self);
786             widget->p_sys_intf = control;
787             update = YES; // Force update and repositionning
788             [control setHidden:widget->b_hide];
789         }
790
791         if (update && !shouldDestroy)
792         {
793             updateControlFromWidget(control, widget, self);
794             [control setHidden:widget->b_hide];
795
796             int row = widget->i_row - 1;
797             int col = widget->i_column - 1;
798             int hsp = __MAX( 1, widget->i_horiz_span );
799             int vsp = __MAX( 1, widget->i_vert_span );
800             if( row < 0 )
801             {
802                 row = 4;
803                 col = 0;
804             }
805
806             VLCDialogGridView *gridView = (VLCDialogGridView *)[window contentView];
807             [gridView addSubview:control atRow:row column:col rowSpan:vsp colSpan:hsp];
808
809             //this->resize( sizeHint() );
810             widget->b_update = false;
811         }
812
813         if (shouldDestroy)
814         {
815             VLCDialogGridView *gridView = (VLCDialogGridView *)[window contentView];
816             [gridView removeSubview:control];
817             [control release];
818             widget->p_sys_intf = NULL;
819         }
820     }
821     FOREACH_END()
822 }
823
824 - (void)updateExtensionDialog:(NSValue *)extensionDialog
825 {
826     extension_dialog_t *dialog = [extensionDialog pointerValue];
827
828     vlc_mutex_lock(&dialog->lock);
829
830     NSSize size = NSMakeSize(dialog->i_width, dialog->i_height);
831
832     BOOL shouldDestroy = dialog->b_kill;
833
834     if (!dialog->i_width || !dialog->i_height)
835         size = NSMakeSize(640, 480);
836
837     VLCDialogWindow *window = dialog->p_sys_intf;
838     if (!window && !shouldDestroy)
839     {
840         NSRect content = NSMakeRect(0, 0, 1, 1);
841         window = [[VLCDialogWindow alloc] initWithContentRect:content styleMask:NSTitledWindowMask | NSClosableWindowMask | NSResizableWindowMask backing:NSBackingStoreBuffered defer:NO];
842         [window setDelegate:self];
843         [window setDialog:dialog];
844         [window setTitle:[NSString stringWithUTF8String:dialog->psz_title]];
845         VLCDialogGridView *gridView = [[VLCDialogGridView alloc] init];
846         [gridView setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
847         [window setContentView:gridView];
848         [gridView release];
849         dialog->p_sys_intf = window;
850     }
851
852     [self updateWidgets:dialog];
853
854     if (shouldDestroy)
855     {
856         [window setDelegate:nil];
857         [window close];
858         dialog->p_sys_intf = NULL;
859         window = nil;
860     }
861
862     if (![window isVisible]) {
863         [window center];
864         [window makeKeyAndOrderFront:self];
865     }
866
867     vlc_cond_signal(&dialog->cond);
868     vlc_mutex_unlock(&dialog->lock);
869 }
870
871
872 /**
873  * Helper to execute a function on main thread and get its return value.
874  */
875 - (void)execute:(NSDictionary *)dict
876 {
877     SEL sel = [[dict objectForKey:@"sel"] pointerValue];
878     id *result = [[dict objectForKey:@"result"] pointerValue];
879     id object = [dict objectForKey:@"object"];
880
881     NSAssert(sel, @"Try to execute a NULL selector");
882
883     *result = [self performSelector:sel withObject:object];
884     [*result retain]; // Balanced in -resultFromSelectorOnMainThread
885 }
886
887 - (id)resultFromSelectorOnMainThread:(SEL)sel withObject:(id)object
888 {
889     id result = nil;
890     NSAssert(sel, @"Try to execute a NULL selector");
891     NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
892      [NSValue valueWithPointer:sel], @"sel",
893      [NSValue valueWithPointer:&result], @"result",
894      object, @"object", nil];
895     [self performSelectorOnMainThread:@selector(execute:) withObject:dict waitUntilDone:YES];
896     return [result autorelease];
897 }
898 @end
899
900
901