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>
40 #import "dialogProvider.h"
41 #import "VLCUIWidgets.h"
43 /*****************************************************************************
45 *****************************************************************************/
46 static int OpenIntf(vlc_object_t *);
47 static void CloseIntf(vlc_object_t *);
48 static void Run(intf_thread_t * );
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 * );
57 static void updateProgressPanel (void *, const char *, float);
58 static bool checkProgressPanel (void *);
59 static void destroyProgressPanel (void *);
62 static inline NSDictionary *DictFromDialogFatal(dialog_fatal_t *dialog) {
63 return [VLCDialogDisplayer dictionaryForDialog:dialog->title :dialog->message :NULL :NULL :NULL];
65 static inline NSDictionary *DictFromDialogLogin(dialog_login_t *dialog) {
66 return [VLCDialogDisplayer dictionaryForDialog:dialog->title :dialog->message :NULL :NULL :NULL];
68 static inline NSDictionary *DictFromDialogQuestion(dialog_question_t *dialog) {
69 return [VLCDialogDisplayer dictionaryForDialog:dialog->title :dialog->message :dialog->yes :dialog->no :dialog->cancel];
71 static inline NSDictionary *DictFromDialogProgressBar(dialog_progress_bar_t *dialog) {
72 return [VLCDialogDisplayer dictionaryForDialog:dialog->title :dialog->message :NULL :NULL :dialog->cancel];
77 VLCDialogDisplayer *displayer;
81 bool is_hidding_noaction_dialogs;
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).")
88 #define prefix "macosx-dialog-provider"
89 /*****************************************************************************
91 *****************************************************************************/
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)
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)
106 set_callbacks(OpenIntf, CloseIntf)
107 set_category(CAT_INTERFACE)
108 set_subcategory(SUBCAT_INTERFACE_MAIN)
111 /*****************************************************************************
112 * OpenIntf: initialize interface
113 *****************************************************************************/
114 int OpenIntf(vlc_object_t *p_this)
116 intf_thread_t *p_intf = (intf_thread_t*) p_this;
118 p_intf->p_sys = malloc(sizeof(intf_sys_t));
122 memset(p_intf->p_sys,0,sizeof(*p_intf->p_sys));
124 p_intf->p_sys->displayer = [[VLCDialogDisplayer alloc] init];
125 [p_intf->p_sys->displayer setIntf:p_intf];
127 bool hide = var_CreateGetBool(p_intf, prefix "hide-no-user-action-dialogs");
128 p_intf->p_sys->is_hidding_noaction_dialogs = hide;
130 /* subscribe to various interactive dialogues */
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);
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);
149 /* subscribe to our last.fm announcements */
150 [[NSDistributedNotificationCenter defaultCenter] addObserver:p_intf->p_sys->displayer
151 selector:@selector(globalNotificationReceived:)
153 object:@"VLCLastFMSupport"
154 suspensionBehavior:NSNotificationSuspensionBehaviorDeliverImmediately];
156 msg_Dbg(p_intf,"Mac OS X dialog provider initialised");
161 /*****************************************************************************
162 * CloseIntf: destroy interface
163 *****************************************************************************/
164 void CloseIntf(vlc_object_t *p_this)
166 intf_thread_t *p_intf = (intf_thread_t*) p_this;
168 /* unsubscribe from the interactive dialogues */
169 dialog_Unregister(p_intf);
171 if (!p_intf->p_sys->is_hidding_noaction_dialogs)
173 var_DelCallback(p_intf,"dialog-error",DisplayError,p_intf);
174 var_DelCallback(p_intf,"dialog-critical",DisplayCritical,p_intf);
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);
181 [p_intf->p_sys->displayer release];
183 msg_Dbg(p_intf,"Mac OS X dialog provider closed");
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)
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];
202 static int DisplayCritical(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
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];
213 static int DisplayQuestion(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
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];
224 static int DisplayLogin(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
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)];
232 *dialog->username = strdup([[dict objectForKey:@"username"] UTF8String]);
233 *dialog->password = strdup([[dict objectForKey:@"password"] UTF8String]);
239 static int DisplayProgressPanelAction(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
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;
246 [sys->displayer performSelectorOnMainThread:@selector(displayProgressBar:) withObject:DictFromDialogProgressBar(dialog) waitUntilDone:YES];
248 dialog->pf_update = updateProgressPanel;
249 dialog->pf_check = checkProgressPanel;
250 dialog->pf_destroy = destroyProgressPanel;
251 dialog->p_sys = p_intf->p_sys;
257 static int DisplayExtension(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
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;
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];
271 void updateProgressPanel (void *priv, const char *text, float value)
273 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
274 intf_sys_t *sys = (intf_sys_t *)priv;
276 NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
277 [NSNumber numberWithFloat:value], @"value",
278 text ? [NSString stringWithUTF8String:text] : nil, @"text",
281 [sys->displayer performSelectorOnMainThread:@selector(updateProgressPanel:) withObject:dict waitUntilDone:YES];
286 void destroyProgressPanel (void *priv)
288 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
289 intf_sys_t *sys = (intf_sys_t *)priv;
291 [sys->displayer performSelectorOnMainThread:@selector(destroyProgressPanel) withObject:nil waitUntilDone:YES];
296 bool checkProgressPanel (void *priv)
298 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
299 intf_sys_t *sys = (intf_sys_t *)priv;
302 ret = [[sys->displayer resultFromSelectorOnMainThread:@selector(checkProgressPanel) withObject:nil] boolValue];
309 @implementation VLCDialogDisplayer
312 [[NSDistributedNotificationCenter defaultCenter] removeObserver:self];
313 assert(!_currentProgressBarPanel); // This has to be closed on main thread.
317 - (void)setIntf: (intf_thread_t *)p_mainintf
322 - (intf_thread_t *)intf
327 + (NSDictionary *)dictionaryForDialog:(const char *)title :(const char *)message :(const char *)yes :(const char *)no :(const char *)cancel
329 NSMutableDictionary *dict = [NSMutableDictionary dictionary];
331 [dict setObject:[NSString stringWithUTF8String:title] forKey:@"title"];
333 [dict setObject:[NSString stringWithUTF8String:message] forKey:@"message"];
335 [dict setObject:[NSString stringWithUTF8String:yes] forKey:@"yes"];
337 [dict setObject:[NSString stringWithUTF8String:no] forKey:@"no"];
339 [dict setObject:[NSString stringWithUTF8String:cancel] forKey:@"cancel"];
343 #define VLCAssertIsMainThread() assert([NSThread isMainThread])
345 - (void)displayError:(NSDictionary *)dialog
347 VLCAssertIsMainThread();
349 NSRunInformationalAlertPanel([dialog objectForKey:@"title"],
350 [dialog objectForKey:@"message"],
354 - (void)displayCritical:(NSDictionary *)dialog
356 VLCAssertIsMainThread();
358 NSRunCriticalAlertPanel([dialog objectForKey:@"title"],
359 [dialog objectForKey:@"message"],
363 - (NSNumber *)displayQuestion:(NSDictionary *)dialog
365 VLCAssertIsMainThread();
367 NSInteger alertRet = 0;
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];
379 case NSAlertDefaultReturn:
382 case NSAlertAlternateReturn:
385 case NSAlertOtherReturn:
394 return [NSNumber numberWithInt:ret];
397 - (NSDictionary *)displayLogin:(NSDictionary *)dialog
399 VLCAssertIsMainThread();
401 VLCLoginPanel *panel = [[VLCLoginPanel alloc] init];
402 [panel createContentView];
403 [panel setDialogTitle:[dialog objectForKey:@"title"]];
404 [panel setDialogMessage:[dialog objectForKey:@"message"]];
406 NSInteger ret = [NSApp runModalForWindow:panel];
412 return [NSDictionary dictionaryWithObjectsAndKeys:
413 [panel userName], @"username",
414 [panel password], @"password",
418 - (void)displayProgressBar:(NSDictionary *)dialog
420 VLCAssertIsMainThread();
422 if(_currentProgressBarPanel)
423 [self destroyProgressPanel];
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"]];
432 [_currentProgressBarPanel center];
433 [_currentProgressBarPanel makeKeyAndOrderFront:nil];
436 - (void)updateProgressPanel:(NSDictionary *)dict
438 VLCAssertIsMainThread();
440 assert(_currentProgressBarPanel);
441 [_currentProgressBarPanel setDialogMessage:[dict objectForKey:@"text"] ?: @""];
442 [_currentProgressBarPanel setProgressAsDouble:[[dict objectForKey:@"value"] doubleValue] * 1000.];
445 - (void)destroyProgressPanel
447 VLCAssertIsMainThread();
449 [_currentProgressBarPanel close];
450 [_currentProgressBarPanel release];
451 _currentProgressBarPanel = nil;
454 - (NSNumber *)checkProgressPanel
456 VLCAssertIsMainThread();
458 return [NSNumber numberWithBool:[_currentProgressBarPanel isCancelled]];
462 #pragma mark Last.FM support
463 - (void)globalNotificationReceived: (NSNotification *)theNotification
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"];
471 if (module_exists("audioscrobbler")) {
473 config_AddIntf(p_intf, "audioscrobbler");
475 config_RemoveIntf(p_intf, "audioscrobbler");
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");
483 msg_Err(p_intf,"Last.FM module not found, no action");
487 #pragma mark Extensions Dialog
489 - (void)triggerClick:(id)sender
491 assert([sender isKindOfClass:[VLCDialogButton class]]);
492 VLCDialogButton *button = sender;
493 extension_widget_t *widget = [button widget];
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);
501 - (void)syncTextField:(NSNotification *)notifcation
503 id sender = [notifcation object];
504 assert([sender isKindOfClass:[VLCDialogTextField class]]);
505 VLCDialogTextField *field = sender;
506 extension_widget_t *widget = [field widget];
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);
514 - (void)tableViewSelectionDidChange:(NSNotification *)notifcation
516 id sender = [notifcation object];
517 assert(sender && [sender isKindOfClass:[VLCDialogList class]]);
518 VLCDialogList *list = sender;
520 struct extension_widget_value_t *value;
522 for(value = [list widget]->p_values; value != NULL; value = value->p_next, i++)
523 value->b_selected = (i == [list selectedRow]);
526 - (void)popUpSelectionChanged:(id)sender
528 assert([sender isKindOfClass:[VLCDialogPopUpButton class]]);
529 VLCDialogPopUpButton *popup = sender;
530 struct extension_widget_value_t *value;
532 for(value = [popup widget]->p_values; value != NULL; value = value->p_next, i++)
533 value->b_selected = (i == [popup indexOfSelectedItem]);
537 - (NSSize)windowWillResize:(NSWindow *)sender toSize:(NSSize)frameSize
539 NSView *contentView = [sender contentView];
540 assert([contentView isKindOfClass:[VLCDialogGridView class]]);
541 VLCDialogGridView *gridView = (VLCDialogGridView *)contentView;
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];
551 - (BOOL)windowShouldClose:(id)sender
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;
561 static NSView *createControlFromWidget(extension_widget_t *widget, id self)
563 assert(!widget->p_sys_intf);
565 switch (widget->type)
567 case EXTENSION_WIDGET_HTML:
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];
576 // return scrollView;
577 NSTextView *field = [[NSTextView alloc] init];
578 [field setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
579 [field setDrawsBackground:NO];
582 case EXTENSION_WIDGET_LABEL:
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];
593 case EXTENSION_WIDGET_TEXT_FIELD:
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];
603 case EXTENSION_WIDGET_BUTTON:
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];
614 case EXTENSION_WIDGET_DROPDOWN:
616 VLCDialogPopUpButton *popup = [[VLCDialogPopUpButton alloc] init];
617 [popup setAction:@selector(popUpSelectionChanged:)];
618 [popup setTarget:self];
619 [popup setWidget:widget];
622 case EXTENSION_WIDGET_LIST:
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];
632 NSTableColumn *column = [[NSTableColumn alloc] init];
633 [list addTableColumn:column];
635 [list setDataSource:list];
636 [list setDelegate:self];
637 [list setWidget:widget];
641 case EXTENSION_WIDGET_IMAGE:
643 NSImageView *imageView = [[NSImageView alloc] init];
644 [imageView setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
645 [imageView setImageFrameStyle:NSImageFramePhoto];
646 [imageView setImageScaling:NSImageScaleProportionallyUpOrDown];
656 static void updateControlFromWidget(NSView *control, extension_widget_t *widget, id self)
658 switch (widget->type)
660 case EXTENSION_WIDGET_HTML:
662 // // Get the scroll view
663 // assert([control isKindOfClass:[NSScrollView class]]);
664 // NSScrollView *scrollView = (NSScrollView *)control;
665 // control = [scrollView documentView];
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];
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];
691 case EXTENSION_WIDGET_LABEL:
692 case EXTENSION_WIDGET_PASSWORD:
693 case EXTENSION_WIDGET_TEXT_FIELD:
695 if (!widget->psz_text)
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];
705 case EXTENSION_WIDGET_BUTTON:
707 assert([control isKindOfClass:[NSButton class]]);
708 NSButton *button = (NSButton *)control;
709 if (!widget->psz_text)
711 [button setTitle:[NSString stringWithUTF8String:widget->psz_text]];
714 case EXTENSION_WIDGET_DROPDOWN:
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)
722 [popup addItemWithTitle:[NSString stringWithUTF8String:value->psz_text]];
724 [popup synchronizeTitleAndSelectedItem];
725 [self popUpSelectionChanged:popup];
729 case EXTENSION_WIDGET_LIST:
731 assert([control isKindOfClass:[NSScrollView class]]);
732 NSScrollView *scrollView = (NSScrollView *)control;
733 assert([[scrollView documentView] isKindOfClass:[VLCDialogList class]]);
734 VLCDialogList *list = (VLCDialogList *)[scrollView documentView];
736 NSMutableArray *contentArray = [NSMutableArray array];
737 struct extension_widget_value_t *value;
738 for(value = widget->p_values; value != NULL; value = value->p_next)
740 NSDictionary *entry = [NSDictionary dictionaryWithObjectsAndKeys:
741 [NSNumber numberWithInt:value->i_id], @"id",
742 [NSString stringWithUTF8String:value->psz_text], @"text",
744 [contentArray addObject:entry];
746 list.contentArray = contentArray;
750 case EXTENSION_WIDGET_IMAGE:
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);
758 image = [[NSImage alloc] initWithContentsOfURL:[NSURL fileURLWithPath:string]];
759 [imageView setImage:image];
767 - (void)updateWidgets:(extension_dialog_t *)dialog
769 extension_widget_t *widget;
770 NSWindow *window = dialog->p_sys_intf;
771 FOREACH_ARRAY(widget, dialog->widgets)
774 continue; /* Some widgets may be NULL at this point */
776 BOOL shouldDestroy = widget->b_kill;
777 NSView *control = widget->p_sys_intf;
778 BOOL update = widget->b_update;
781 if (!control && !shouldDestroy)
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];
790 if (update && !shouldDestroy)
792 updateControlFromWidget(control, widget, self);
793 [control setHidden:widget->b_hide];
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 );
805 VLCDialogGridView *gridView = (VLCDialogGridView *)[window contentView];
806 [gridView addSubview:control atRow:row column:col rowSpan:vsp colSpan:hsp];
808 //this->resize( sizeHint() );
809 widget->b_update = false;
814 VLCDialogGridView *gridView = (VLCDialogGridView *)[window contentView];
815 [gridView removeSubview:control];
817 widget->p_sys_intf = NULL;
823 - (void)updateExtensionDialog:(NSValue *)extensionDialog
825 extension_dialog_t *dialog = [extensionDialog pointerValue];
827 vlc_mutex_lock(&dialog->lock);
829 NSSize size = NSMakeSize(dialog->i_width, dialog->i_height);
831 BOOL shouldDestroy = dialog->b_kill;
833 if (!dialog->i_width || !dialog->i_height)
834 size = NSMakeSize(640, 480);
836 VLCDialogWindow *window = dialog->p_sys_intf;
837 if (!window && !shouldDestroy)
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];
848 dialog->p_sys_intf = window;
851 [self updateWidgets:dialog];
855 [window setDelegate:nil];
857 dialog->p_sys_intf = NULL;
861 if (![window isVisible]) {
863 [window makeKeyAndOrderFront:self];
866 vlc_cond_signal(&dialog->cond);
867 vlc_mutex_unlock(&dialog->lock);
872 * Helper to execute a function on main thread and get its return value.
874 - (void)execute:(NSDictionary *)dict
876 SEL sel = [[dict objectForKey:@"sel"] pointerValue];
877 id *result = [[dict objectForKey:@"result"] pointerValue];
878 id object = [dict objectForKey:@"object"];
880 NSAssert(sel, @"Try to execute a NULL selector");
882 *result = [self performSelector:sel withObject:object];
883 [*result retain]; // Balanced in -resultFromSelectorOnMainThread
886 - (id)resultFromSelectorOnMainThread:(SEL)sel withObject:(id)object
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];