1 /*****************************************************************************
2 * dialogProvider.m: Minimal Dialog Provider for Mac OS X
3 *****************************************************************************
4 * Copyright (C) 2009-2010 the VideoLAN team
7 * Authors: Felix Paul Kühne <fkuehne at videolan dot org>
8 * Pierre d'Herbemont <pdherbemont # videolan dot>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
28 #import <stdlib.h> /* malloc(), free() */
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>
41 #import "dialogProvider.h"
42 #import "VLCUIWidgets.h"
44 /*****************************************************************************
46 *****************************************************************************/
47 static int OpenIntf(vlc_object_t *);
48 static void CloseIntf(vlc_object_t *);
49 static void Run(intf_thread_t * );
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 * );
58 static void updateProgressPanel (void *, const char *, float);
59 static bool checkProgressPanel (void *);
60 static void destroyProgressPanel (void *);
63 static inline NSDictionary *DictFromDialogFatal(dialog_fatal_t *dialog) {
64 return [VLCDialogDisplayer dictionaryForDialog:dialog->title :dialog->message :NULL :NULL :NULL];
66 static inline NSDictionary *DictFromDialogLogin(dialog_login_t *dialog) {
67 return [VLCDialogDisplayer dictionaryForDialog:dialog->title :dialog->message :NULL :NULL :NULL];
69 static inline NSDictionary *DictFromDialogQuestion(dialog_question_t *dialog) {
70 return [VLCDialogDisplayer dictionaryForDialog:dialog->title :dialog->message :dialog->yes :dialog->no :dialog->cancel];
72 static inline NSDictionary *DictFromDialogProgressBar(dialog_progress_bar_t *dialog) {
73 return [VLCDialogDisplayer dictionaryForDialog:dialog->title :dialog->message :NULL :NULL :dialog->cancel];
78 VLCDialogDisplayer *displayer;
82 bool is_hidding_noaction_dialogs;
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).")
89 #define prefix "macosx-dialog-provider"
90 /*****************************************************************************
92 *****************************************************************************/
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)
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)
107 set_callbacks(OpenIntf, CloseIntf)
108 set_category(CAT_INTERFACE)
109 set_subcategory(SUBCAT_INTERFACE_MAIN)
112 /*****************************************************************************
113 * OpenIntf: initialize interface
114 *****************************************************************************/
115 int OpenIntf(vlc_object_t *p_this)
117 intf_thread_t *p_intf = (intf_thread_t*) p_this;
119 p_intf->p_sys = malloc(sizeof(intf_sys_t));
123 memset(p_intf->p_sys,0,sizeof(*p_intf->p_sys));
125 p_intf->p_sys->displayer = [[VLCDialogDisplayer alloc] init];
126 [p_intf->p_sys->displayer setIntf:p_intf];
128 bool hide = var_CreateGetBool(p_intf, prefix "hide-no-user-action-dialogs");
129 p_intf->p_sys->is_hidding_noaction_dialogs = hide;
131 /* subscribe to various interactive dialogues */
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);
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);
150 /* subscribe to our last.fm announcements */
151 [[NSDistributedNotificationCenter defaultCenter] addObserver:p_intf->p_sys->displayer
152 selector:@selector(globalNotificationReceived:)
154 object:@"VLCLastFMSupport"
155 suspensionBehavior:NSNotificationSuspensionBehaviorDeliverImmediately];
157 msg_Dbg(p_intf,"Mac OS X dialog provider initialised");
162 /*****************************************************************************
163 * CloseIntf: destroy interface
164 *****************************************************************************/
165 void CloseIntf(vlc_object_t *p_this)
167 intf_thread_t *p_intf = (intf_thread_t*) p_this;
169 /* unsubscribe from the interactive dialogues */
170 dialog_Unregister(p_intf);
172 if (!p_intf->p_sys->is_hidding_noaction_dialogs)
174 var_DelCallback(p_intf,"dialog-error",DisplayError,p_intf);
175 var_DelCallback(p_intf,"dialog-critical",DisplayCritical,p_intf);
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);
182 [p_intf->p_sys->displayer release];
184 msg_Dbg(p_intf,"Mac OS X dialog provider closed");
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)
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];
203 static int DisplayCritical(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
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];
214 static int DisplayQuestion(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
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];
225 static int DisplayLogin(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
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)];
233 *dialog->username = strdup([[dict objectForKey:@"username"] UTF8String]);
234 *dialog->password = strdup([[dict objectForKey:@"password"] UTF8String]);
240 static int DisplayProgressPanelAction(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
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;
247 [sys->displayer performSelectorOnMainThread:@selector(displayProgressBar:) withObject:DictFromDialogProgressBar(dialog) waitUntilDone:YES];
249 dialog->pf_update = updateProgressPanel;
250 dialog->pf_check = checkProgressPanel;
251 dialog->pf_destroy = destroyProgressPanel;
252 dialog->p_sys = p_intf->p_sys;
258 static int DisplayExtension(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
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;
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];
272 void updateProgressPanel (void *priv, const char *text, float value)
274 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
275 intf_sys_t *sys = (intf_sys_t *)priv;
277 NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
278 [NSNumber numberWithFloat:value], @"value",
279 text ? [NSString stringWithUTF8String:text] : nil, @"text",
282 [sys->displayer performSelectorOnMainThread:@selector(updateProgressPanel:) withObject:dict waitUntilDone:YES];
287 void destroyProgressPanel (void *priv)
289 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
290 intf_sys_t *sys = (intf_sys_t *)priv;
292 [sys->displayer performSelectorOnMainThread:@selector(destroyProgressPanel) withObject:nil waitUntilDone:YES];
297 bool checkProgressPanel (void *priv)
299 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
300 intf_sys_t *sys = (intf_sys_t *)priv;
303 ret = [[sys->displayer resultFromSelectorOnMainThread:@selector(checkProgressPanel) withObject:nil] boolValue];
310 @implementation VLCDialogDisplayer
313 [[NSDistributedNotificationCenter defaultCenter] removeObserver:self];
314 assert(!_currentProgressBarPanel); // This has to be closed on main thread.
318 - (void)setIntf: (intf_thread_t *)p_mainintf
323 - (intf_thread_t *)intf
328 + (NSDictionary *)dictionaryForDialog:(const char *)title :(const char *)message :(const char *)yes :(const char *)no :(const char *)cancel
330 NSMutableDictionary *dict = [NSMutableDictionary dictionary];
332 [dict setObject:[NSString stringWithUTF8String:title] forKey:@"title"];
334 [dict setObject:[NSString stringWithUTF8String:message] forKey:@"message"];
336 [dict setObject:[NSString stringWithUTF8String:yes] forKey:@"yes"];
338 [dict setObject:[NSString stringWithUTF8String:no] forKey:@"no"];
340 [dict setObject:[NSString stringWithUTF8String:cancel] forKey:@"cancel"];
344 #define VLCAssertIsMainThread() assert([NSThread isMainThread])
346 - (void)displayError:(NSDictionary *)dialog
348 VLCAssertIsMainThread();
350 NSRunInformationalAlertPanel([dialog objectForKey:@"title"],
351 [dialog objectForKey:@"message"],
355 - (void)displayCritical:(NSDictionary *)dialog
357 VLCAssertIsMainThread();
359 NSRunCriticalAlertPanel([dialog objectForKey:@"title"],
360 [dialog objectForKey:@"message"],
364 - (NSNumber *)displayQuestion:(NSDictionary *)dialog
366 VLCAssertIsMainThread();
368 NSInteger alertRet = 0;
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];
380 case NSAlertDefaultReturn:
383 case NSAlertAlternateReturn:
386 case NSAlertOtherReturn:
395 return [NSNumber numberWithInt:ret];
398 - (NSDictionary *)displayLogin:(NSDictionary *)dialog
400 VLCAssertIsMainThread();
402 VLCLoginPanel *panel = [[VLCLoginPanel alloc] init];
403 [panel createContentView];
404 [panel setDialogTitle:[dialog objectForKey:@"title"]];
405 [panel setDialogMessage:[dialog objectForKey:@"message"]];
407 NSInteger ret = [NSApp runModalForWindow:panel];
413 return [NSDictionary dictionaryWithObjectsAndKeys:
414 [panel userName], @"username",
415 [panel password], @"password",
419 - (void)displayProgressBar:(NSDictionary *)dialog
421 VLCAssertIsMainThread();
423 if(_currentProgressBarPanel)
424 [self destroyProgressPanel];
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"]];
433 [_currentProgressBarPanel center];
434 [_currentProgressBarPanel makeKeyAndOrderFront:nil];
437 - (void)updateProgressPanel:(NSDictionary *)dict
439 VLCAssertIsMainThread();
441 assert(_currentProgressBarPanel);
442 [_currentProgressBarPanel setDialogMessage:[dict objectForKey:@"text"] ?: @""];
443 [_currentProgressBarPanel setProgressAsDouble:[[dict objectForKey:@"value"] doubleValue] * 1000.];
446 - (void)destroyProgressPanel
448 VLCAssertIsMainThread();
450 [_currentProgressBarPanel close];
451 [_currentProgressBarPanel release];
452 _currentProgressBarPanel = nil;
455 - (NSNumber *)checkProgressPanel
457 VLCAssertIsMainThread();
459 return [NSNumber numberWithBool:[_currentProgressBarPanel isCancelled]];
463 #pragma mark Last.FM support
464 - (void)globalNotificationReceived: (NSNotification *)theNotification
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"];
472 if (module_exists("audioscrobbler")) {
474 config_AddIntf(p_intf, "audioscrobbler");
476 config_RemoveIntf(p_intf, "audioscrobbler");
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");
484 msg_Err(p_intf,"Last.FM module not found, no action");
488 #pragma mark Extensions Dialog
490 - (void)triggerClick:(id)sender
492 assert([sender isKindOfClass:[VLCDialogButton class]]);
493 VLCDialogButton *button = sender;
494 extension_widget_t *widget = [button widget];
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);
502 - (void)syncTextField:(NSNotification *)notifcation
504 id sender = [notifcation object];
505 assert([sender isKindOfClass:[VLCDialogTextField class]]);
506 VLCDialogTextField *field = sender;
507 extension_widget_t *widget = [field widget];
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);
515 - (void)tableViewSelectionDidChange:(NSNotification *)notifcation
517 id sender = [notifcation object];
518 assert(sender && [sender isKindOfClass:[VLCDialogList class]]);
519 VLCDialogList *list = sender;
521 struct extension_widget_value_t *value;
523 for(value = [list widget]->p_values; value != NULL; value = value->p_next, i++)
524 value->b_selected = (i == [list selectedRow]);
527 - (void)popUpSelectionChanged:(id)sender
529 assert([sender isKindOfClass:[VLCDialogPopUpButton class]]);
530 VLCDialogPopUpButton *popup = sender;
531 struct extension_widget_value_t *value;
533 for(value = [popup widget]->p_values; value != NULL; value = value->p_next, i++)
534 value->b_selected = (i == [popup indexOfSelectedItem]);
538 - (NSSize)windowWillResize:(NSWindow *)sender toSize:(NSSize)frameSize
540 NSView *contentView = [sender contentView];
541 assert([contentView isKindOfClass:[VLCDialogGridView class]]);
542 VLCDialogGridView *gridView = (VLCDialogGridView *)contentView;
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];
552 - (BOOL)windowShouldClose:(id)sender
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;
562 static NSView *createControlFromWidget(extension_widget_t *widget, id self)
564 assert(!widget->p_sys_intf);
566 switch (widget->type)
568 case EXTENSION_WIDGET_HTML:
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];
577 // return scrollView;
578 NSTextView *field = [[NSTextView alloc] init];
579 [field setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
580 [field setDrawsBackground:NO];
583 case EXTENSION_WIDGET_LABEL:
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];
594 case EXTENSION_WIDGET_TEXT_FIELD:
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];
604 case EXTENSION_WIDGET_BUTTON:
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];
615 case EXTENSION_WIDGET_DROPDOWN:
617 VLCDialogPopUpButton *popup = [[VLCDialogPopUpButton alloc] init];
618 [popup setAction:@selector(popUpSelectionChanged:)];
619 [popup setTarget:self];
620 [popup setWidget:widget];
623 case EXTENSION_WIDGET_LIST:
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];
633 NSTableColumn *column = [[NSTableColumn alloc] init];
634 [list addTableColumn:column];
636 [list setDataSource:list];
637 [list setDelegate:self];
638 [list setWidget:widget];
642 case EXTENSION_WIDGET_IMAGE:
644 NSImageView *imageView = [[NSImageView alloc] init];
645 [imageView setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
646 [imageView setImageFrameStyle:NSImageFramePhoto];
647 [imageView setImageScaling:NSImageScaleProportionallyUpOrDown];
657 static void updateControlFromWidget(NSView *control, extension_widget_t *widget, id self)
659 switch (widget->type)
661 case EXTENSION_WIDGET_HTML:
663 // // Get the scroll view
664 // assert([control isKindOfClass:[NSScrollView class]]);
665 // NSScrollView *scrollView = (NSScrollView *)control;
666 // control = [scrollView documentView];
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];
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];
692 case EXTENSION_WIDGET_LABEL:
693 case EXTENSION_WIDGET_PASSWORD:
694 case EXTENSION_WIDGET_TEXT_FIELD:
696 if (!widget->psz_text)
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];
706 case EXTENSION_WIDGET_CHECK_BOX:
707 case EXTENSION_WIDGET_BUTTON:
709 assert([control isKindOfClass:[NSButton class]]);
710 NSButton *button = (NSButton *)control;
711 if (!widget->psz_text)
713 [button setTitle:[NSString stringWithUTF8String:widget->psz_text]];
716 case EXTENSION_WIDGET_DROPDOWN:
718 assert([control isKindOfClass:[NSPopUpButton class]]);
719 NSPopUpButton *popup = (NSPopUpButton *)control;
720 [popup removeAllItems];
721 struct extension_widget_value_t *value;
722 for(value = widget->p_values; value != NULL; value = value->p_next)
724 [popup addItemWithTitle:[NSString stringWithUTF8String:value->psz_text]];
726 [popup synchronizeTitleAndSelectedItem];
727 [self popUpSelectionChanged:popup];
731 case EXTENSION_WIDGET_LIST:
733 assert([control isKindOfClass:[NSScrollView class]]);
734 NSScrollView *scrollView = (NSScrollView *)control;
735 assert([[scrollView documentView] isKindOfClass:[VLCDialogList class]]);
736 VLCDialogList *list = (VLCDialogList *)[scrollView documentView];
738 NSMutableArray *contentArray = [NSMutableArray array];
739 struct extension_widget_value_t *value;
740 for(value = widget->p_values; value != NULL; value = value->p_next)
742 NSDictionary *entry = [NSDictionary dictionaryWithObjectsAndKeys:
743 [NSNumber numberWithInt:value->i_id], @"id",
744 [NSString stringWithUTF8String:value->psz_text], @"text",
746 [contentArray addObject:entry];
748 list.contentArray = contentArray;
752 case EXTENSION_WIDGET_IMAGE:
754 assert([control isKindOfClass:[NSImageView class]]);
755 NSImageView *imageView = (NSImageView *)control;
756 NSString *string = widget->psz_text ? [NSString stringWithUTF8String:widget->psz_text] : nil;
757 NSImage *image = nil;
758 NSLog(@"Setting image to %@", string);
760 image = [[NSImage alloc] initWithContentsOfURL:[NSURL fileURLWithPath:string]];
761 [imageView setImage:image];
769 - (void)updateWidgets:(extension_dialog_t *)dialog
771 extension_widget_t *widget;
772 NSWindow *window = dialog->p_sys_intf;
773 FOREACH_ARRAY(widget, dialog->widgets)
776 continue; /* Some widgets may be NULL at this point */
778 BOOL shouldDestroy = widget->b_kill;
779 NSView *control = widget->p_sys_intf;
780 BOOL update = widget->b_update;
783 if (!control && !shouldDestroy)
785 control = createControlFromWidget(widget, self);
786 updateControlFromWidget(control, widget, self);
787 widget->p_sys_intf = control;
788 update = YES; // Force update and repositionning
789 [control setHidden:widget->b_hide];
792 if (update && !shouldDestroy)
794 updateControlFromWidget(control, widget, self);
795 [control setHidden:widget->b_hide];
797 int row = widget->i_row - 1;
798 int col = widget->i_column - 1;
799 int hsp = __MAX( 1, widget->i_horiz_span );
800 int vsp = __MAX( 1, widget->i_vert_span );
807 VLCDialogGridView *gridView = (VLCDialogGridView *)[window contentView];
808 [gridView addSubview:control atRow:row column:col rowSpan:vsp colSpan:hsp];
810 //this->resize( sizeHint() );
811 widget->b_update = false;
816 VLCDialogGridView *gridView = (VLCDialogGridView *)[window contentView];
817 [gridView removeSubview:control];
819 widget->p_sys_intf = NULL;
825 - (void)updateExtensionDialog:(NSValue *)extensionDialog
827 extension_dialog_t *dialog = [extensionDialog pointerValue];
829 vlc_mutex_lock(&dialog->lock);
831 NSSize size = NSMakeSize(dialog->i_width, dialog->i_height);
833 BOOL shouldDestroy = dialog->b_kill;
835 if (!dialog->i_width || !dialog->i_height)
836 size = NSMakeSize(640, 480);
838 VLCDialogWindow *window = dialog->p_sys_intf;
839 if (!window && !shouldDestroy)
841 NSRect content = NSMakeRect(0, 0, 1, 1);
842 window = [[VLCDialogWindow alloc] initWithContentRect:content styleMask:NSTitledWindowMask | NSClosableWindowMask | NSResizableWindowMask backing:NSBackingStoreBuffered defer:NO];
843 [window setDelegate:self];
844 [window setDialog:dialog];
845 [window setTitle:[NSString stringWithUTF8String:dialog->psz_title]];
846 VLCDialogGridView *gridView = [[VLCDialogGridView alloc] init];
847 [gridView setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
848 [window setContentView:gridView];
850 dialog->p_sys_intf = window;
853 [self updateWidgets:dialog];
857 [window setDelegate:nil];
859 dialog->p_sys_intf = NULL;
863 if (![window isVisible]) {
865 [window makeKeyAndOrderFront:self];
868 vlc_cond_signal(&dialog->cond);
869 vlc_mutex_unlock(&dialog->lock);
874 * Helper to execute a function on main thread and get its return value.
876 - (void)execute:(NSDictionary *)dict
878 SEL sel = [[dict objectForKey:@"sel"] pointerValue];
879 id *result = [[dict objectForKey:@"result"] pointerValue];
880 id object = [dict objectForKey:@"object"];
882 NSAssert(sel, @"Try to execute a NULL selector");
884 *result = [self performSelector:sel withObject:object];
885 [*result retain]; // Balanced in -resultFromSelectorOnMainThread
888 - (id)resultFromSelectorOnMainThread:(SEL)sel withObject:(id)object
891 NSAssert(sel, @"Try to execute a NULL selector");
892 NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
893 [NSValue valueWithPointer:sel], @"sel",
894 [NSValue valueWithPointer:&result], @"result",
895 object, @"object", nil];
896 [self performSelectorOnMainThread:@selector(execute:) withObject:dict waitUntilDone:YES];
897 return [result autorelease];