1 /*****************************************************************************
2 * dialogProvider.m: Minimal Dialog Provider for Mac OS X
3 *****************************************************************************
4 * Copyright (C) 2009-2011 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_hiding_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, 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_hiding_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_hiding_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 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]);
481 msg_Err(p_intf,"Last.FM module not found, no action");
485 #pragma mark Extensions Dialog
487 - (void)triggerClick:(id)sender
489 assert([sender isKindOfClass:[VLCDialogButton class]]);
490 VLCDialogButton *button = sender;
491 extension_widget_t *widget = [button widget];
493 vlc_mutex_lock(&widget->p_dialog->lock);
494 extension_WidgetClicked(widget->p_dialog, widget);
495 vlc_mutex_unlock(&widget->p_dialog->lock);
498 - (void)syncTextField:(NSNotification *)notifcation
500 id sender = [notifcation object];
501 assert([sender isKindOfClass:[VLCDialogTextField class]]);
502 VLCDialogTextField *field = sender;
503 extension_widget_t *widget = [field widget];
505 vlc_mutex_lock(&widget->p_dialog->lock);
506 free(widget->psz_text);
507 widget->psz_text = strdup([[field stringValue] UTF8String]);
508 vlc_mutex_unlock(&widget->p_dialog->lock);
511 - (void)tableViewSelectionDidChange:(NSNotification *)notifcation
513 id sender = [notifcation object];
514 assert(sender && [sender isKindOfClass:[VLCDialogList class]]);
515 VLCDialogList *list = sender;
517 struct extension_widget_value_t *value;
519 for(value = [list widget]->p_values; value != NULL; value = value->p_next, i++)
520 value->b_selected = (i == [list selectedRow]);
523 - (void)popUpSelectionChanged:(id)sender
525 assert([sender isKindOfClass:[VLCDialogPopUpButton class]]);
526 VLCDialogPopUpButton *popup = sender;
527 struct extension_widget_value_t *value;
529 for(value = [popup widget]->p_values; value != NULL; value = value->p_next, i++)
530 value->b_selected = (i == [popup indexOfSelectedItem]);
534 - (NSSize)windowWillResize:(NSWindow *)sender toSize:(NSSize)frameSize
536 NSView *contentView = [sender contentView];
537 assert([contentView isKindOfClass:[VLCDialogGridView class]]);
538 VLCDialogGridView *gridView = (VLCDialogGridView *)contentView;
540 NSRect rect = NSMakeRect(0, 0, 0, 0);
541 rect.size = frameSize;
542 rect = [sender contentRectForFrameRect:rect];
543 rect.size = [gridView flexSize:rect.size];
544 rect = [sender frameRectForContentRect:rect];
548 - (BOOL)windowShouldClose:(id)sender
550 assert([sender isKindOfClass:[VLCDialogWindow class]]);
551 VLCDialogWindow *window = sender;
552 extension_dialog_t *dialog = [window dialog];
553 extension_DialogClosed(dialog);
554 dialog->p_sys_intf = NULL;
558 static NSView *createControlFromWidget(extension_widget_t *widget, id self)
560 assert(!widget->p_sys_intf);
562 switch (widget->type)
564 case EXTENSION_WIDGET_HTML:
566 // NSScrollView *scrollView = [[NSScrollView alloc] init];
567 // [scrollView setHasVerticalScroller:YES];
568 // NSTextView *field = [[NSTextView alloc] init];
569 // [scrollView setDocumentView:field];
570 // [scrollView setAutoresizesSubviews:YES];
571 // [scrollView setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
573 // return scrollView;
574 NSTextView *field = [[NSTextView alloc] init];
575 [field setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
576 [field setDrawsBackground:NO];
579 case EXTENSION_WIDGET_LABEL:
581 NSTextField *field = [[NSTextField alloc] init];
582 [field setEditable:NO];
583 [field setBordered:NO];
584 [field setDrawsBackground:NO];
585 [field setFont:[NSFont systemFontOfSize:0]];
586 [[field cell] setControlSize:NSRegularControlSize];
587 [field setAutoresizingMask:NSViewNotSizable];
590 case EXTENSION_WIDGET_TEXT_FIELD:
592 VLCDialogTextField *field = [[VLCDialogTextField alloc] init];
593 [field setWidget:widget];
594 [field setAutoresizingMask:NSViewWidthSizable];
595 [field setFont:[NSFont systemFontOfSize:0]];
596 [[field cell] setControlSize:NSRegularControlSize];
597 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(syncTextField:) name:NSControlTextDidChangeNotification object:field];
600 case EXTENSION_WIDGET_BUTTON:
602 VLCDialogButton *button = [[VLCDialogButton alloc] init];
603 [button setBezelStyle:NSRoundedBezelStyle];
604 [button setWidget:widget];
605 [button setAction:@selector(triggerClick:)];
606 [button setTarget:self];
607 [[button cell] setControlSize:NSRegularControlSize];
608 [button setAutoresizingMask:NSViewNotSizable];
611 case EXTENSION_WIDGET_DROPDOWN:
613 VLCDialogPopUpButton *popup = [[VLCDialogPopUpButton alloc] init];
614 [popup setAction:@selector(popUpSelectionChanged:)];
615 [popup setTarget:self];
616 [popup setWidget:widget];
619 case EXTENSION_WIDGET_LIST:
621 NSScrollView *scrollView = [[NSScrollView alloc] init];
622 [scrollView setHasVerticalScroller:YES];
623 VLCDialogList *list = [[VLCDialogList alloc] init];
624 [list setUsesAlternatingRowBackgroundColors:YES];
625 [list setHeaderView:nil];
626 [scrollView setDocumentView:list];
627 [scrollView setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
629 NSTableColumn *column = [[NSTableColumn alloc] init];
630 [list addTableColumn:column];
632 [list setDataSource:list];
633 [list setDelegate:self];
634 [list setWidget:widget];
638 case EXTENSION_WIDGET_IMAGE:
640 NSImageView *imageView = [[NSImageView alloc] init];
641 [imageView setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
642 [imageView setImageFrameStyle:NSImageFramePhoto];
643 [imageView setImageScaling:NSImageScaleProportionallyUpOrDown];
646 case EXTENSION_WIDGET_SPIN_ICON:
648 NSProgressIndicator *spinner = [[NSProgressIndicator alloc] init];
649 [spinner setUsesThreadedAnimation:YES];
650 [spinner setStyle:NSProgressIndicatorSpinningStyle];
651 [spinner setDisplayedWhenStopped:YES];
652 [spinner startAnimation:self];
662 static void updateControlFromWidget(NSView *control, extension_widget_t *widget, id self)
664 switch (widget->type)
666 case EXTENSION_WIDGET_HTML:
668 // // Get the scroll view
669 // assert([control isKindOfClass:[NSScrollView class]]);
670 // NSScrollView *scrollView = (NSScrollView *)control;
671 // control = [scrollView documentView];
673 // assert([control isKindOfClass:[NSTextView class]]);
674 // NSTextView *textView = (NSTextView *)control;
675 // NSString *string = [NSString stringWithUTF8String:widget->psz_text];
676 // NSAttributedString *attrString = [[NSAttributedString alloc] initWithHTML:[string dataUsingEncoding:NSISOLatin1StringEncoding] documentAttributes:NULL];
677 // [[textView textStorage] setAttributedString:[[NSAttributedString alloc] initWithString:@"Hello"]];
678 // [textView setNeedsDisplay:YES];
679 // [textView scrollRangeToVisible:NSMakeRange(0, 0)];
680 // [attrString release];
685 assert([control isKindOfClass:[NSTextView class]]);
686 NSTextView *textView = (NSTextView *)control;
687 NSString *string = [NSString stringWithUTF8String:widget->psz_text];
688 NSAttributedString *attrString = [[NSAttributedString alloc] initWithHTML:[string dataUsingEncoding:NSISOLatin1StringEncoding] documentAttributes:NULL];
689 [[textView textStorage] setAttributedString:attrString];
690 [textView setNeedsDisplay:YES];
691 [textView scrollRangeToVisible:NSMakeRange(0, 0)];
692 [attrString release];
696 case EXTENSION_WIDGET_LABEL:
697 case EXTENSION_WIDGET_PASSWORD:
698 case EXTENSION_WIDGET_TEXT_FIELD:
700 if (!widget->psz_text)
702 assert([control isKindOfClass:[NSControl class]]);
703 NSControl *field = (NSControl *)control;
704 NSString *string = [NSString stringWithUTF8String:widget->psz_text];
705 NSAttributedString *attrString = [[NSAttributedString alloc] initWithHTML:[string dataUsingEncoding:NSISOLatin1StringEncoding] documentAttributes:NULL];
706 [field setAttributedStringValue:attrString];
707 [attrString release];
710 case EXTENSION_WIDGET_CHECK_BOX:
711 case EXTENSION_WIDGET_BUTTON:
713 assert([control isKindOfClass:[NSButton class]]);
714 NSButton *button = (NSButton *)control;
715 if (!widget->psz_text)
717 [button setTitle:[NSString stringWithUTF8String:widget->psz_text]];
720 case EXTENSION_WIDGET_DROPDOWN:
722 assert([control isKindOfClass:[NSPopUpButton class]]);
723 NSPopUpButton *popup = (NSPopUpButton *)control;
724 [popup removeAllItems];
725 struct extension_widget_value_t *value;
726 for(value = widget->p_values; value != NULL; value = value->p_next)
728 [popup addItemWithTitle:[NSString stringWithUTF8String:value->psz_text]];
730 [popup synchronizeTitleAndSelectedItem];
731 [self popUpSelectionChanged:popup];
735 case EXTENSION_WIDGET_LIST:
737 assert([control isKindOfClass:[NSScrollView class]]);
738 NSScrollView *scrollView = (NSScrollView *)control;
739 assert([[scrollView documentView] isKindOfClass:[VLCDialogList class]]);
740 VLCDialogList *list = (VLCDialogList *)[scrollView documentView];
742 NSMutableArray *contentArray = [NSMutableArray array];
743 struct extension_widget_value_t *value;
744 for(value = widget->p_values; value != NULL; value = value->p_next)
746 NSDictionary *entry = [NSDictionary dictionaryWithObjectsAndKeys:
747 [NSNumber numberWithInt:value->i_id], @"id",
748 [NSString stringWithUTF8String:value->psz_text], @"text",
750 [contentArray addObject:entry];
752 list.contentArray = contentArray;
756 case EXTENSION_WIDGET_IMAGE:
758 assert([control isKindOfClass:[NSImageView class]]);
759 NSImageView *imageView = (NSImageView *)control;
760 NSString *string = widget->psz_text ? [NSString stringWithUTF8String:widget->psz_text] : nil;
761 NSImage *image = nil;
763 image = [[NSImage alloc] initWithContentsOfURL:[NSURL fileURLWithPath:string]];
764 [imageView setImage:image];
768 case EXTENSION_WIDGET_SPIN_ICON:
770 assert([control isKindOfClass:[NSProgressIndicator class]]);
771 NSProgressIndicator *progressIndicator = (NSProgressIndicator *)control;
772 if( widget->i_spin_loops != 0 )
773 [progressIndicator startAnimation:self];
775 [progressIndicator stopAnimation:self];
782 - (void)updateWidgets:(extension_dialog_t *)dialog
784 extension_widget_t *widget;
785 NSWindow *window = dialog->p_sys_intf;
786 FOREACH_ARRAY(widget, dialog->widgets)
789 continue; /* Some widgets may be NULL at this point */
791 BOOL shouldDestroy = widget->b_kill;
792 NSView *control = widget->p_sys_intf;
793 BOOL update = widget->b_update;
796 if (!control && !shouldDestroy)
798 control = createControlFromWidget(widget, self);
799 updateControlFromWidget(control, widget, self);
800 widget->p_sys_intf = control;
801 update = YES; // Force update and repositionning
802 [control setHidden:widget->b_hide];
805 if (update && !shouldDestroy)
807 updateControlFromWidget(control, widget, self);
808 [control setHidden:widget->b_hide];
810 int row = widget->i_row - 1;
811 int col = widget->i_column - 1;
812 int hsp = __MAX( 1, widget->i_horiz_span );
813 int vsp = __MAX( 1, widget->i_vert_span );
820 VLCDialogGridView *gridView = (VLCDialogGridView *)[window contentView];
821 [gridView addSubview:control atRow:row column:col rowSpan:vsp colSpan:hsp];
823 //this->resize( sizeHint() );
824 widget->b_update = false;
829 VLCDialogGridView *gridView = (VLCDialogGridView *)[window contentView];
830 [gridView removeSubview:control];
832 widget->p_sys_intf = NULL;
838 - (void)updateExtensionDialog:(NSValue *)extensionDialog
840 extension_dialog_t *dialog = [extensionDialog pointerValue];
842 vlc_mutex_lock(&dialog->lock);
844 NSSize size = NSMakeSize(dialog->i_width, dialog->i_height);
846 BOOL shouldDestroy = dialog->b_kill;
848 if (!dialog->i_width || !dialog->i_height)
849 size = NSMakeSize(640, 480);
851 VLCDialogWindow *window = dialog->p_sys_intf;
852 if (!window && !shouldDestroy)
854 NSRect content = NSMakeRect(0, 0, 1, 1);
855 window = [[VLCDialogWindow alloc] initWithContentRect:content styleMask:NSTitledWindowMask | NSClosableWindowMask | NSResizableWindowMask backing:NSBackingStoreBuffered defer:NO];
856 [window setDelegate:self];
857 [window setDialog:dialog];
858 [window setTitle:[NSString stringWithUTF8String:dialog->psz_title]];
859 VLCDialogGridView *gridView = [[VLCDialogGridView alloc] init];
860 [gridView setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
861 [window setContentView:gridView];
863 dialog->p_sys_intf = window;
866 [self updateWidgets:dialog];
870 [window setDelegate:nil];
872 dialog->p_sys_intf = NULL;
876 if (![window isVisible]) {
878 [window makeKeyAndOrderFront:self];
881 vlc_cond_signal(&dialog->cond);
882 vlc_mutex_unlock(&dialog->lock);
887 * Helper to execute a function on main thread and get its return value.
889 - (void)execute:(NSDictionary *)dict
891 SEL sel = [[dict objectForKey:@"sel"] pointerValue];
892 id *result = [[dict objectForKey:@"result"] pointerValue];
893 id object = [dict objectForKey:@"object"];
895 NSAssert(sel, @"Try to execute a NULL selector");
897 *result = [self performSelector:sel withObject:object];
898 [*result retain]; // Balanced in -resultFromSelectorOnMainThread
901 - (id)resultFromSelectorOnMainThread:(SEL)sel withObject:(id)object
904 NSAssert(sel, @"Try to execute a NULL selector");
905 NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
906 [NSValue valueWithPointer:sel], @"sel",
907 [NSValue valueWithPointer:&result], @"result",
908 object, @"object", nil];
909 [self performSelectorOnMainThread:@selector(execute:) withObject:dict waitUntilDone:YES];
910 return [result autorelease];