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>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #import <stdlib.h> /* malloc(), free() */
34 #import <vlc_common.h>
35 #import <vlc_plugin.h>
36 #import <vlc_dialog.h>
37 #import <vlc_interface.h>
38 #import <vlc_extensions.h>
40 #import <Cocoa/Cocoa.h>
41 #import "VLCLoginPanel.h"
42 #import "VLCProgressPanel.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 *);
62 @interface VLCDialogDisplayer : NSObject
64 VLCProgressPanel *_currentProgressBarPanel;
67 + (NSDictionary *)dictionaryForDialog:(const char *)title :(const char *)message :(const char *)yes :(const char *)no :(const char *)cancel;
69 - (void)displayError:(NSDictionary *)dialog;
70 - (void)displayCritical:(NSDictionary *)dialog;
71 - (NSNumber *)displayQuestion:(NSDictionary *)dialog;
72 - (NSDictionary *)displayLogin:(NSDictionary *)dialog;
74 - (void)displayProgressBar:(NSDictionary *)dict;
75 - (void)updateProgressPanel:(NSDictionary *)dict;
76 - (void)destroyProgressPanel;
77 - (NSNumber *)checkProgressPanel;
79 - (void)updateExtensionDialog:(NSValue *)extensionDialog;
81 - (id)resultFromSelectorOnMainThread:(SEL)sel withObject:(id)object;
84 @interface VLCDialogButton : NSButton
86 extension_widget_t *widget;
88 @property (readwrite) extension_widget_t *widget;
91 @implementation VLCDialogButton
95 @interface VLCDialogPopUpButton : NSPopUpButton
97 extension_widget_t *widget;
99 @property (readwrite) extension_widget_t *widget;
102 @implementation VLCDialogPopUpButton
107 @interface VLCDialogTextField : NSTextField
109 extension_widget_t *widget;
111 @property (readwrite) extension_widget_t *widget;
114 @implementation VLCDialogTextField
118 @interface VLCDialogList : NSTableView
120 extension_widget_t *widget;
121 NSMutableArray *contentArray;
123 @property (readwrite) extension_widget_t *widget;
124 @property (readwrite, retain) NSMutableArray *contentArray;
127 @implementation VLCDialogList
129 @synthesize contentArray;
131 - (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
133 return [contentArray count];
136 - (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
138 return [[contentArray objectAtIndex:rowIndex] objectForKey:@"text"];
142 @interface VLCDialogGridView : NSView {
143 NSUInteger _rowCount, _colCount;
144 NSMutableArray *_gridedViews;
147 - (NSSize)flexSize:(NSSize)size;
148 - (void)removeSubview:(NSView *)view;
152 // Move this to separate file
153 @implementation VLCDialogGridView
157 [_gridedViews release];
161 - (void)recomputeCount
165 for (NSDictionary *obj in _gridedViews)
167 NSUInteger row = [[obj objectForKey:@"row"] intValue];
168 NSUInteger col = [[obj objectForKey:@"col"] intValue];
169 if (col + 1 > _colCount)
171 if (row + 1 > _rowCount)
176 - (void)recomputeWindowSize
178 [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(recomputeWindowSize) object:nil];
180 NSWindow *window = [self window];
181 NSRect frame = [window frame];
182 NSRect contentRect = [window contentRectForFrameRect:frame];
183 contentRect.size = [self flexSize:frame.size];
184 NSRect newFrame = [window frameRectForContentRect:contentRect];
185 newFrame.origin.y -= newFrame.size.height - frame.size.height;
186 newFrame.origin.x -= (newFrame.size.width - frame.size.width) / 2;
187 [window setFrame:newFrame display:YES animate:YES];
190 - (NSSize)objectSizeToFit:(NSView *)view
192 if ([view isKindOfClass:[NSControl class]]) {
193 NSControl *control = (NSControl *)view;
194 return [[control cell] cellSize];
196 return [view frame].size;
208 - (CGFloat)constrainedHeightOfRow:(NSUInteger)targetRow
211 for(NSDictionary *obj in _gridedViews) {
212 NSUInteger row = [[obj objectForKey:@"row"] intValue];
213 if (row != targetRow)
215 NSUInteger rowSpan = [[obj objectForKey:@"rowSpan"] intValue];
218 NSView *view = [obj objectForKey:@"view"];
219 if ([view autoresizingMask] & NSViewHeightSizable)
221 NSSize sizeToFit = [self objectSizeToFit:view];
222 if (height < sizeToFit.height)
223 height = sizeToFit.height;
228 - (CGFloat)remainingRowsHeight
230 NSUInteger height = [self marginY];
233 NSUInteger autosizedRows = 0;
234 for (NSUInteger i = 0; i < _rowCount; i++) {
235 CGFloat constrainedHeight = [self constrainedHeightOfRow:i];
236 if (!constrainedHeight)
238 height += constrainedHeight + [self marginY];
240 CGFloat remaining = 0;
241 if (height < self.bounds.size.height && autosizedRows)
242 remaining = (self.bounds.size.height - height) / autosizedRows;
249 - (CGFloat)heightOfRow:(NSUInteger)targetRow
251 NSAssert(targetRow < _rowCount, @"accessing a non existing row");
252 CGFloat height = [self constrainedHeightOfRow:targetRow];
254 height = [self remainingRowsHeight];
259 - (CGFloat)topOfRow:(NSUInteger)targetRow
261 CGFloat top = [self marginY];
262 for (NSUInteger i = 1; i < _rowCount - targetRow; i++)
264 top += [self heightOfRow:_rowCount - i] + [self marginY];
269 - (CGFloat)constrainedWidthOfColumn:(NSUInteger)targetColumn
272 for(NSDictionary *obj in _gridedViews) {
273 NSUInteger col = [[obj objectForKey:@"col"] intValue];
274 if (col != targetColumn)
276 NSUInteger colSpan = [[obj objectForKey:@"colSpan"] intValue];
279 NSView *view = [obj objectForKey:@"view"];
280 if ([view autoresizingMask] & NSViewWidthSizable)
282 NSSize sizeToFit = [self objectSizeToFit:view];
283 if (width < sizeToFit.width)
284 width = sizeToFit.width;
289 - (CGFloat)remainingColumnWidth
291 NSUInteger width = [self marginX];
294 NSUInteger autosizedCol = 0;
295 for (NSUInteger i = 0; i < _colCount; i++) {
296 CGFloat constrainedWidth = [self constrainedWidthOfColumn:i];
297 if (!constrainedWidth)
299 width += constrainedWidth + [self marginX];
302 CGFloat remaining = 0;
303 if (width < self.bounds.size.width && autosizedCol)
304 remaining = (self.bounds.size.width - width) / autosizedCol;
310 - (CGFloat)widthOfColumn:(NSUInteger)targetColumn
312 CGFloat width = [self constrainedWidthOfColumn:targetColumn];
314 width = [self remainingColumnWidth];
319 - (CGFloat)leftOfColumn:(NSUInteger)targetColumn
321 CGFloat left = [self marginX];
322 for (NSUInteger i = 0; i < targetColumn; i++)
324 left += [self widthOfColumn:i] + [self marginX];
332 for(NSDictionary *obj in _gridedViews) {
333 NSUInteger row = [[obj objectForKey:@"row"] intValue];
334 NSUInteger col = [[obj objectForKey:@"col"] intValue];
335 NSUInteger rowSpan = [[obj objectForKey:@"rowSpan"] intValue];
336 NSUInteger colSpan = [[obj objectForKey:@"colSpan"] intValue];
337 NSView *view = [obj objectForKey:@"view"];
341 if ([view autoresizingMask] & NSViewHeightSizable || rowSpan > 1) {
343 for (NSUInteger r = 0; r < rowSpan; r++) {
344 if (row + r >= _rowCount)
346 height += [self heightOfRow:row + r] + [self marginY];
348 rect.size.height = height - [self marginY];
351 rect.size.height = [self objectSizeToFit:view].height;
354 if ([view autoresizingMask] & NSViewWidthSizable) {
356 for (NSUInteger c = 0; c < colSpan; c++)
357 width += [self widthOfColumn:col + c] + [self marginX];
358 rect.size.width = width - [self marginX];
361 rect.size.width = [self objectSizeToFit:view].width;
364 rect.origin.y = [self topOfRow:row] + ([self heightOfRow:row] - rect.size.height) / 2;
365 rect.origin.x = [self leftOfColumn:col];
367 [view setFrame:rect];
368 [view setNeedsDisplay:YES];
372 - (NSMutableDictionary *)objectForView:(NSView *)view
374 for (NSMutableDictionary *dict in _gridedViews)
376 if ([dict objectForKey:@"view"] == view)
382 - (void)addSubview:(NSView *)view atRow:(NSUInteger)row column:(NSUInteger)column rowSpan:(NSUInteger)rowSpan colSpan:(NSUInteger)colSpan
384 if (row + 1 > _rowCount)
386 if (column + 1 > _colCount)
387 _colCount = column + 1;
390 _gridedViews = [[NSMutableArray alloc] init];
392 NSMutableDictionary *dict = [self objectForView:view];
394 dict = [NSMutableDictionary dictionary];
395 [dict setObject:view forKey:@"view"];
396 [_gridedViews addObject:dict];
398 [dict setObject:[NSNumber numberWithInt:rowSpan] forKey:@"rowSpan"];
399 [dict setObject:[NSNumber numberWithInt:colSpan] forKey:@"colSpan"];
400 [dict setObject:[NSNumber numberWithInt:row] forKey:@"row"];
401 [dict setObject:[NSNumber numberWithInt:column] forKey:@"col"];
404 [self addSubview:view];
407 // Recompute the size of the window after making sure we won't see anymore update
408 [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(recomputeWindowSize) object:nil];
409 [self performSelector:@selector(recomputeWindowSize) withObject:nil afterDelay:0.1];
412 - (void)removeSubview:(NSView *)view
414 NSDictionary *dict = [self objectForView:view];
416 [_gridedViews removeObject:dict];
417 [view removeFromSuperview];
419 [self recomputeCount];
420 [self recomputeWindowSize];
423 [self setNeedsDisplay:YES];
426 - (void)setFrame:(NSRect)frameRect
428 [super setFrame:frameRect];
432 - (NSSize)flexSize:(NSSize)size
434 if (!_rowCount || !_colCount)
437 CGFloat minHeight = [self marginY];
438 BOOL canFlexHeight = NO;
439 for (NSUInteger i = 0; i < _rowCount; i++) {
440 CGFloat constrained = [self constrainedHeightOfRow:i];
445 minHeight += constrained + [self marginY];
448 CGFloat minWidth = [self marginX];
449 BOOL canFlexWidth = NO;
450 for (NSUInteger i = 0; i < _colCount; i++) {
451 CGFloat constrained = [self constrainedWidthOfColumn:i];
456 minWidth += constrained + [self marginX];
458 if (size.width < minWidth)
459 size.width = minWidth;
460 if (size.height < minHeight)
461 size.height = minHeight;
463 size.height = minHeight;
465 size.width = minWidth;
469 - (NSSize)windowWillResize:(NSWindow *)sender toSize:(NSSize)frameSize
471 NSRect rect = NSMakeRect(0, 0, 0, 0);
472 rect.size = frameSize;
473 rect = [sender contentRectForFrameRect:rect];
474 rect.size = [self flexSize:rect.size];
475 rect = [sender frameRectForContentRect:rect];
482 static inline NSDictionary *DictFromDialogFatal(dialog_fatal_t *dialog) {
483 return [VLCDialogDisplayer dictionaryForDialog:dialog->title :dialog->message :NULL :NULL :NULL];
485 static inline NSDictionary *DictFromDialogLogin(dialog_login_t *dialog) {
486 return [VLCDialogDisplayer dictionaryForDialog:dialog->title :dialog->message :NULL :NULL :NULL];
488 static inline NSDictionary *DictFromDialogQuestion(dialog_question_t *dialog) {
489 return [VLCDialogDisplayer dictionaryForDialog:dialog->title :dialog->message :dialog->yes :dialog->no :dialog->cancel];
491 static inline NSDictionary *DictFromDialogProgressBar(dialog_progress_bar_t *dialog) {
492 return [VLCDialogDisplayer dictionaryForDialog:dialog->title :dialog->message :NULL :NULL :dialog->cancel];
497 VLCDialogDisplayer *displayer;
501 bool is_hidding_noaction_dialogs;
505 #define T_HIDE_NOACTION N_("Hide no user action dialogs")
506 #define LT_HIDE_NOACTION N_("Don't display dialogs that don't require user action (Critical and error panel).")
508 #define prefix "macosx-dialog-provider"
509 /*****************************************************************************
511 *****************************************************************************/
514 /* Minimal interface. see intf.m */
515 set_shortname("Mac OS X Dialogs")
516 add_shortcut("macosx_dialog_provider")
517 add_shortcut("miosx")
518 set_description("Minimal Mac OS X Dialog Provider")
519 set_capability("interface", 0)
521 /* This setting is interesting, because when used with a libvlc app
522 * it's almost certain that the client program will display error by
523 * itself. Moreover certain action might end up in an error, but
524 * the client wants to ignored them completely. */
525 add_bool(prefix "hide-no-user-action-dialogs", true, NULL, T_HIDE_NOACTION, LT_HIDE_NOACTION, false)
527 set_callbacks(OpenIntf, CloseIntf)
528 set_category(CAT_INTERFACE)
529 set_subcategory(SUBCAT_INTERFACE_MAIN)
532 /*****************************************************************************
533 * OpenIntf: initialize interface
534 *****************************************************************************/
535 int OpenIntf(vlc_object_t *p_this)
537 intf_thread_t *p_intf = (intf_thread_t*) p_this;
539 p_intf->p_sys = malloc(sizeof(intf_sys_t));
543 memset(p_intf->p_sys,0,sizeof(*p_intf->p_sys));
545 p_intf->p_sys->displayer = [[VLCDialogDisplayer alloc] init];
547 bool hide = var_CreateGetBool(p_intf, prefix "hide-no-user-action-dialogs");
548 p_intf->p_sys->is_hidding_noaction_dialogs = hide;
550 /* subscribe to various interactive dialogues */
554 var_Create(p_intf,"dialog-error",VLC_VAR_ADDRESS);
555 var_AddCallback(p_intf,"dialog-error",DisplayError,p_intf);
556 var_Create(p_intf,"dialog-critical",VLC_VAR_ADDRESS);
557 var_AddCallback(p_intf,"dialog-critical",DisplayCritical,p_intf);
559 var_Create(p_intf,"dialog-login",VLC_VAR_ADDRESS);
560 var_AddCallback(p_intf,"dialog-login",DisplayLogin,p_intf);
561 var_Create(p_intf,"dialog-question",VLC_VAR_ADDRESS);
562 var_AddCallback(p_intf,"dialog-question",DisplayQuestion,p_intf);
563 var_Create(p_intf,"dialog-progress-bar",VLC_VAR_ADDRESS);
564 var_AddCallback(p_intf,"dialog-progress-bar",DisplayProgressPanelAction,p_intf);
565 var_Create(p_intf,"dialog-extension",VLC_VAR_ADDRESS);
566 var_AddCallback(p_intf,"dialog-extension",DisplayExtension,p_intf);
567 dialog_Register(p_intf);
569 msg_Dbg(p_intf,"Mac OS X dialog provider initialised");
574 /*****************************************************************************
575 * CloseIntf: destroy interface
576 *****************************************************************************/
577 void CloseIntf(vlc_object_t *p_this)
579 intf_thread_t *p_intf = (intf_thread_t*) p_this;
581 /* unsubscribe from the interactive dialogues */
582 dialog_Unregister(p_intf);
584 if (!p_intf->p_sys->is_hidding_noaction_dialogs)
586 var_DelCallback(p_intf,"dialog-error",DisplayError,p_intf);
587 var_DelCallback(p_intf,"dialog-critical",DisplayCritical,p_intf);
589 var_DelCallback(p_intf,"dialog-login",DisplayLogin,p_intf);
590 var_DelCallback(p_intf,"dialog-question",DisplayQuestion,p_intf);
591 var_DelCallback(p_intf,"dialog-progress-bar",DisplayProgressPanelAction,p_intf);
592 var_DelCallback(p_intf,"dialog-extension",DisplayExtension,p_intf);
594 [p_intf->p_sys->displayer release];
596 msg_Dbg(p_intf,"Mac OS X dialog provider closed");
601 /*****************************************************************************
602 * Callbacks triggered by the "dialog-*" variables
603 *****************************************************************************/
604 static int DisplayError(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
606 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
607 dialog_fatal_t *dialog = value.p_address;
608 intf_thread_t *p_intf = (intf_thread_t*) p_this;
609 intf_sys_t *sys = p_intf->p_sys;
610 [sys->displayer performSelectorOnMainThread:@selector(displayError:) withObject:DictFromDialogFatal(dialog) waitUntilDone:NO];
615 static int DisplayCritical(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
617 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
618 dialog_fatal_t *dialog = value.p_address;
619 intf_thread_t *p_intf = (intf_thread_t*) p_this;
620 intf_sys_t *sys = p_intf->p_sys;
621 [sys->displayer performSelectorOnMainThread:@selector(displayCritical:) withObject:DictFromDialogFatal(dialog) waitUntilDone:NO];
626 static int DisplayQuestion(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
628 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
629 dialog_question_t *dialog = value.p_address;
630 intf_thread_t *p_intf = (intf_thread_t*) p_this;
631 intf_sys_t *sys = p_intf->p_sys;
632 dialog->answer = [[sys->displayer resultFromSelectorOnMainThread:@selector(displayQuestion:) withObject:DictFromDialogQuestion(dialog)] intValue];
637 static int DisplayLogin(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
639 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
640 dialog_login_t *dialog = value.p_address;
641 intf_thread_t *p_intf = (intf_thread_t*) p_this;
642 intf_sys_t *sys = p_intf->p_sys;
643 NSDictionary *dict = [sys->displayer resultFromSelectorOnMainThread:@selector(displayLogin:) withObject:DictFromDialogLogin(dialog)];
645 *dialog->username = strdup([[dict objectForKey:@"username"] UTF8String]);
646 *dialog->password = strdup([[dict objectForKey:@"password"] UTF8String]);
652 static int DisplayProgressPanelAction(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
654 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
655 dialog_progress_bar_t *dialog = value.p_address;
656 intf_thread_t *p_intf = (intf_thread_t*) p_this;
657 intf_sys_t *sys = p_intf->p_sys;
659 [sys->displayer performSelectorOnMainThread:@selector(displayProgressBar:) withObject:DictFromDialogProgressBar(dialog) waitUntilDone:YES];
661 dialog->pf_update = updateProgressPanel;
662 dialog->pf_check = checkProgressPanel;
663 dialog->pf_destroy = destroyProgressPanel;
664 dialog->p_sys = p_intf->p_sys;
670 static int DisplayExtension(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
672 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
673 intf_thread_t *p_intf = (intf_thread_t*) p_this;
674 intf_sys_t *sys = p_intf->p_sys;
675 extension_dialog_t *dialog = value.p_address;
677 // -updateExtensionDialog: Open its own runloop, so be sure to run on DefaultRunLoop.
678 [sys->displayer performSelectorOnMainThread:@selector(updateExtensionDialog:) withObject:[NSValue valueWithPointer:dialog] waitUntilDone:YES];
684 void updateProgressPanel (void *priv, const char *text, float value)
686 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
687 intf_sys_t *sys = (intf_sys_t *)priv;
689 NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
690 [NSNumber numberWithFloat:value], @"value",
691 text ? [NSString stringWithUTF8String:text] : nil, @"text",
694 [sys->displayer performSelectorOnMainThread:@selector(updateProgressPanel:) withObject:dict waitUntilDone:YES];
699 void destroyProgressPanel (void *priv)
701 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
702 intf_sys_t *sys = (intf_sys_t *)priv;
704 [sys->displayer performSelectorOnMainThread:@selector(destroyProgressPanel) withObject:nil waitUntilDone:YES];
709 bool checkProgressPanel (void *priv)
711 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
712 intf_sys_t *sys = (intf_sys_t *)priv;
715 ret = [[sys->displayer resultFromSelectorOnMainThread:@selector(checkProgressPanel) withObject:nil] boolValue];
722 @implementation VLCDialogDisplayer
725 assert(!_currentProgressBarPanel); // This has to be closed on main thread.
729 + (NSDictionary *)dictionaryForDialog:(const char *)title :(const char *)message :(const char *)yes :(const char *)no :(const char *)cancel
731 NSMutableDictionary *dict = [NSMutableDictionary dictionary];
733 [dict setObject:[NSString stringWithUTF8String:title] forKey:@"title"];
735 [dict setObject:[NSString stringWithUTF8String:message] forKey:@"message"];
737 [dict setObject:[NSString stringWithUTF8String:yes] forKey:@"yes"];
739 [dict setObject:[NSString stringWithUTF8String:no] forKey:@"no"];
741 [dict setObject:[NSString stringWithUTF8String:cancel] forKey:@"cancel"];
745 #define VLCAssertIsMainThread() assert([NSThread isMainThread])
747 - (void)displayError:(NSDictionary *)dialog
749 VLCAssertIsMainThread();
751 NSRunInformationalAlertPanel([dialog objectForKey:@"title"],
752 [dialog objectForKey:@"message"],
756 - (void)displayCritical:(NSDictionary *)dialog
758 VLCAssertIsMainThread();
760 NSRunCriticalAlertPanel([dialog objectForKey:@"title"],
761 [dialog objectForKey:@"message"],
765 - (NSNumber *)displayQuestion:(NSDictionary *)dialog
767 VLCAssertIsMainThread();
769 NSInteger alertRet = 0;
771 NSAlert *alert = [NSAlert alertWithMessageText:[dialog objectForKey:@"title"]
772 defaultButton:[dialog objectForKey:@"yes"]
773 alternateButton:[dialog objectForKey:@"no"]
774 otherButton:[dialog objectForKey:@"cancel"]
775 informativeTextWithFormat:[dialog objectForKey:@"message"]];
776 [alert setAlertStyle:NSInformationalAlertStyle];
777 alertRet = [alert runModal];
781 case NSAlertDefaultReturn:
784 case NSAlertAlternateReturn:
787 case NSAlertOtherReturn:
796 return [NSNumber numberWithInt:ret];
799 - (NSDictionary *)displayLogin:(NSDictionary *)dialog
801 VLCAssertIsMainThread();
803 VLCLoginPanel *panel = [[VLCLoginPanel alloc] init];
804 [panel createContentView];
805 [panel setDialogTitle:[dialog objectForKey:@"title"]];
806 [panel setDialogMessage:[dialog objectForKey:@"message"]];
808 NSInteger ret = [NSApp runModalForWindow:panel];
814 return [NSDictionary dictionaryWithObjectsAndKeys:
815 [panel userName], @"username",
816 [panel password], @"password",
820 - (void)displayProgressBar:(NSDictionary *)dialog
822 VLCAssertIsMainThread();
824 if(_currentProgressBarPanel)
825 [self destroyProgressPanel];
827 assert(!_currentProgressBarPanel);
828 _currentProgressBarPanel = [[VLCProgressPanel alloc] init];
829 [_currentProgressBarPanel createContentView];
830 [_currentProgressBarPanel setDialogTitle:[dialog objectForKey:@"title"]];
831 [_currentProgressBarPanel setDialogMessage:[dialog objectForKey:@"message"] ?: @""];
832 [_currentProgressBarPanel setCancelButtonLabel:[dialog objectForKey:@"cancel"]];
834 [_currentProgressBarPanel center];
835 [_currentProgressBarPanel makeKeyAndOrderFront:nil];
838 - (void)updateProgressPanel:(NSDictionary *)dict
840 VLCAssertIsMainThread();
842 assert(_currentProgressBarPanel);
843 [_currentProgressBarPanel setDialogMessage:[dict objectForKey:@"text"] ?: @""];
844 [_currentProgressBarPanel setProgressAsDouble:[[dict objectForKey:@"value"] doubleValue] * 1000.];
847 - (void)destroyProgressPanel
849 VLCAssertIsMainThread();
851 [_currentProgressBarPanel close];
852 [_currentProgressBarPanel release];
853 _currentProgressBarPanel = nil;
856 - (NSNumber *)checkProgressPanel
858 VLCAssertIsMainThread();
860 return [NSNumber numberWithBool:[_currentProgressBarPanel isCancelled]];
864 #pragma mark Extensions Dialog
866 - (void)triggerClick:(id)sender
868 assert([sender isKindOfClass:[VLCDialogButton class]]);
869 VLCDialogButton *button = sender;
870 extension_widget_t *widget = [button widget];
872 NSLog(@"(triggerClick)");
873 vlc_mutex_lock(&widget->p_dialog->lock);
874 extension_WidgetClicked(widget->p_dialog, widget);
875 vlc_mutex_unlock(&widget->p_dialog->lock);
878 - (void)syncTextField:(NSNotification *)notifcation
880 id sender = [notifcation object];
881 assert([sender isKindOfClass:[VLCDialogTextField class]]);
882 VLCDialogTextField *field = sender;
883 extension_widget_t *widget = [field widget];
885 vlc_mutex_lock(&widget->p_dialog->lock);
886 free(widget->psz_text);
887 widget->psz_text = strdup([[field stringValue] UTF8String]);
888 vlc_mutex_unlock(&widget->p_dialog->lock);
891 - (void)tableViewSelectionDidChange:(NSNotification *)notifcation
893 id sender = [notifcation object];
894 assert(sender && [sender isKindOfClass:[VLCDialogList class]]);
895 VLCDialogList *list = sender;
897 struct extension_widget_value_t *value;
899 for(value = [list widget]->p_values; value != NULL; value = value->p_next, i++)
900 value->b_selected = (i == [list selectedRow]);
903 - (void)popUpSelectionChanged:(id)sender
905 assert([sender isKindOfClass:[VLCDialogPopUpButton class]]);
906 VLCDialogPopUpButton *popup = sender;
907 struct extension_widget_value_t *value;
909 for(value = [popup widget]->p_values; value != NULL; value = value->p_next, i++)
910 value->b_selected = (i == [popup indexOfSelectedItem]);
914 static NSView *createControlFromWidget(extension_widget_t *widget, id self)
916 assert(!widget->p_sys_intf);
918 switch (widget->type)
920 case EXTENSION_WIDGET_HTML:
922 // NSScrollView *scrollView = [[NSScrollView alloc] init];
923 // [scrollView setHasVerticalScroller:YES];
924 // NSTextView *field = [[NSTextView alloc] init];
925 // [scrollView setDocumentView:field];
926 // [scrollView setAutoresizesSubviews:YES];
927 // [scrollView setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
929 // return scrollView;
930 NSTextView *field = [[NSTextView alloc] init];
931 [field setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
932 [field setDrawsBackground:NO];
935 case EXTENSION_WIDGET_LABEL:
937 NSTextField *field = [[NSTextField alloc] init];
938 [field setEditable:NO];
939 [field setBordered:NO];
940 [field setDrawsBackground:NO];
941 [[field cell] setControlSize:NSRegularControlSize];
942 [field setAutoresizingMask:NSViewNotSizable];
945 case EXTENSION_WIDGET_TEXT_FIELD:
947 VLCDialogTextField *field = [[VLCDialogTextField alloc] init];
948 [field setWidget:widget];
949 [field setAutoresizingMask:NSViewWidthSizable];
950 [[field cell] setControlSize:NSRegularControlSize];
951 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(syncTextField:) name:NSControlTextDidChangeNotification object:field];
954 case EXTENSION_WIDGET_BUTTON:
956 VLCDialogButton *button = [[VLCDialogButton alloc] init];
957 [button setBezelStyle:NSRoundedBezelStyle];
958 [button setWidget:widget];
959 [button setAction:@selector(triggerClick:)];
960 [button setTarget:self];
961 [[button cell] setControlSize:NSRegularControlSize];
962 [button setAutoresizingMask:NSViewNotSizable];
965 case EXTENSION_WIDGET_DROPDOWN:
967 VLCDialogPopUpButton *popup = [[VLCDialogPopUpButton alloc] init];
968 [popup setAction:@selector(popUpSelectionChanged:)];
969 [popup setTarget:self];
970 [popup setWidget:widget];
973 case EXTENSION_WIDGET_LIST:
975 NSScrollView *scrollView = [[NSScrollView alloc] init];
976 [scrollView setHasVerticalScroller:YES];
977 VLCDialogList *list = [[VLCDialogList alloc] init];
978 [list setUsesAlternatingRowBackgroundColors:YES];
979 [list setHeaderView:nil];
980 [scrollView setDocumentView:list];
981 [scrollView setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
983 NSTableColumn *column = [[NSTableColumn alloc] init];
984 [list addTableColumn:column];
986 [list setDataSource:list];
987 [list setDelegate:self];
988 [list setWidget:widget];
992 case EXTENSION_WIDGET_IMAGE:
994 NSImageView *imageView = [[NSImageView alloc] init];
995 [imageView setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
996 [imageView setImageFrameStyle:NSImageFramePhoto];
997 [imageView setImageScaling:NSImageScaleProportionallyUpOrDown];
1007 static void updateControlFromWidget(NSView *control, extension_widget_t *widget)
1009 switch (widget->type)
1011 case EXTENSION_WIDGET_HTML:
1013 // // Get the scroll view
1014 // assert([control isKindOfClass:[NSScrollView class]]);
1015 // NSScrollView *scrollView = (NSScrollView *)control;
1016 // control = [scrollView documentView];
1018 // assert([control isKindOfClass:[NSTextView class]]);
1019 // NSTextView *textView = (NSTextView *)control;
1020 // NSString *string = [NSString stringWithUTF8String:widget->psz_text];
1021 // NSAttributedString *attrString = [[NSAttributedString alloc] initWithHTML:[string dataUsingEncoding:NSUTF8StringEncoding] documentAttributes:NULL];
1022 // [[textView textStorage] setAttributedString:[[NSAttributedString alloc] initWithString:@"Hello"]];
1023 // NSLog(@"%@", string);
1024 // [textView setNeedsDisplay:YES];
1025 // [textView scrollRangeToVisible:NSMakeRange(0, 0)];
1026 // [attrString release];
1031 assert([control isKindOfClass:[NSTextView class]]);
1032 NSTextView *textView = (NSTextView *)control;
1033 NSString *string = [NSString stringWithUTF8String:widget->psz_text];
1034 NSAttributedString *attrString = [[NSAttributedString alloc] initWithHTML:[string dataUsingEncoding:NSUTF8StringEncoding] documentAttributes:NULL];
1035 [[textView textStorage] setAttributedString:attrString];
1036 [textView setNeedsDisplay:YES];
1037 [textView scrollRangeToVisible:NSMakeRange(0, 0)];
1038 [attrString release];
1042 case EXTENSION_WIDGET_LABEL:
1043 case EXTENSION_WIDGET_PASSWORD:
1044 case EXTENSION_WIDGET_TEXT_FIELD:
1046 if (!widget->psz_text)
1048 assert([control isKindOfClass:[NSControl class]]);
1049 NSControl *field = (NSControl *)control;
1050 NSString *string = [NSString stringWithUTF8String:widget->psz_text];
1051 NSAttributedString *attrString = [[NSAttributedString alloc] initWithHTML:[string dataUsingEncoding:NSUTF8StringEncoding] documentAttributes:NULL];
1052 [field setAttributedStringValue:attrString];
1053 [attrString release];
1056 case EXTENSION_WIDGET_BUTTON:
1058 assert([control isKindOfClass:[NSButton class]]);
1059 NSButton *button = (NSButton *)control;
1060 if (!widget->psz_text)
1062 [button setTitle:[NSString stringWithUTF8String:widget->psz_text]];
1065 case EXTENSION_WIDGET_DROPDOWN:
1067 assert([control isKindOfClass:[NSPopUpButton class]]);
1068 NSPopUpButton *popup = (NSPopUpButton *)control;
1069 [popup removeAllItems];
1070 struct extension_widget_value_t *value;
1071 for(value = widget->p_values; value != NULL; value = value->p_next)
1073 [popup addItemWithTitle:[NSString stringWithUTF8String:value->psz_text]];
1075 [popup synchronizeTitleAndSelectedItem];
1079 case EXTENSION_WIDGET_LIST:
1081 assert([control isKindOfClass:[NSScrollView class]]);
1082 NSScrollView *scrollView = (NSScrollView *)control;
1083 assert([[scrollView documentView] isKindOfClass:[VLCDialogList class]]);
1084 VLCDialogList *list = (VLCDialogList *)[scrollView documentView];
1086 NSMutableArray *contentArray = [NSMutableArray array];
1087 struct extension_widget_value_t *value;
1088 for(value = widget->p_values; value != NULL; value = value->p_next)
1090 NSDictionary *entry = [NSDictionary dictionaryWithObjectsAndKeys:
1091 [NSNumber numberWithInt:value->i_id], @"id",
1092 [NSString stringWithUTF8String:value->psz_text], @"text",
1094 [contentArray addObject:entry];
1096 list.contentArray = contentArray;
1100 case EXTENSION_WIDGET_IMAGE:
1102 assert([control isKindOfClass:[NSImageView class]]);
1103 NSImageView *imageView = (NSImageView *)control;
1104 NSString *string = widget->psz_text ? [NSString stringWithUTF8String:widget->psz_text] : nil;
1105 NSImage *image = nil;
1106 NSLog(@"Setting image to %@", string);
1108 image = [[NSImage alloc] initWithContentsOfURL:[NSURL fileURLWithPath:string]];
1109 [imageView setImage:image];
1117 - (void)updateWidgets:(extension_dialog_t *)dialog
1119 extension_widget_t *widget;
1120 NSWindow *window = dialog->p_sys_intf;
1121 FOREACH_ARRAY(widget, dialog->widgets)
1124 continue; /* Some widgets may be NULL at this point */
1126 BOOL shouldDestroy = widget->b_kill;
1127 NSView *control = widget->p_sys_intf;
1128 BOOL update = widget->b_update;
1131 if (!control && !shouldDestroy)
1133 control = createControlFromWidget(widget, self);
1134 updateControlFromWidget(control, widget);
1135 widget->p_sys_intf = control;
1136 update = YES; // Force update and repositionning
1137 [control setHidden:widget->b_hide];
1140 if (update && !shouldDestroy)
1142 updateControlFromWidget(control, widget);
1143 [control setHidden:widget->b_hide];
1145 int row = widget->i_row - 1;
1146 int col = widget->i_column - 1;
1147 int hsp = __MAX( 1, widget->i_horiz_span );
1148 int vsp = __MAX( 1, widget->i_vert_span );
1155 VLCDialogGridView *gridView = (VLCDialogGridView *)[window contentView];
1156 [gridView addSubview:control atRow:row column:col rowSpan:vsp colSpan:hsp];
1158 //this->resize( sizeHint() );
1159 widget->b_update = false;
1164 VLCDialogGridView *gridView = (VLCDialogGridView *)[window contentView];
1165 [gridView removeSubview:control];
1167 widget->p_sys_intf = NULL;
1173 - (void)updateExtensionDialog:(NSValue *)extensionDialog
1175 extension_dialog_t *dialog = [extensionDialog pointerValue];
1177 vlc_mutex_lock(&dialog->lock);
1179 NSSize size = NSMakeSize(dialog->i_width, dialog->i_height);
1181 BOOL shouldDestroy = dialog->b_kill;
1183 if (!dialog->i_width || !dialog->i_height)
1184 size = NSMakeSize(640, 480);
1186 NSWindow *window = dialog->p_sys_intf;
1187 if (!window && !shouldDestroy)
1189 NSRect content = NSMakeRect(0, 0, 1, 1);
1190 window = [[NSWindow alloc] initWithContentRect:content styleMask:NSTitledWindowMask | NSClosableWindowMask | NSResizableWindowMask backing:NSBackingStoreBuffered defer:NO];
1191 [window setTitle:[NSString stringWithUTF8String:dialog->psz_title]];
1192 VLCDialogGridView *gridView = [[VLCDialogGridView alloc] init];
1193 [gridView setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
1194 [window setContentView:gridView];
1195 [window setDelegate:gridView];
1197 dialog->p_sys_intf = window;
1200 [self updateWidgets:dialog];
1204 [window setDelegate:nil];
1206 dialog->p_sys_intf = NULL;
1209 if (!dialog->b_hide && ![window isVisible]) {
1211 [window makeKeyAndOrderFront:self];
1214 vlc_cond_signal(&dialog->cond);
1215 vlc_mutex_unlock(&dialog->lock);
1220 * Helper to execute a function on main thread and get its return value.
1222 - (void)execute:(NSDictionary *)dict
1224 SEL sel = [[dict objectForKey:@"sel"] pointerValue];
1225 id *result = [[dict objectForKey:@"result"] pointerValue];
1226 id object = [dict objectForKey:@"object"];
1228 NSAssert(sel, @"Try to execute a NULL selector");
1230 *result = [self performSelector:sel withObject:object];
1231 [*result retain]; // Balanced in -resultFromSelectorOnMainThread
1234 - (id)resultFromSelectorOnMainThread:(SEL)sel withObject:(id)object
1237 NSAssert(sel, @"Try to execute a NULL selector");
1238 NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
1239 [NSValue valueWithPointer:sel], @"sel",
1240 [NSValue valueWithPointer:&result], @"result",
1241 object, @"object", nil];
1242 [self performSelectorOnMainThread:@selector(execute:) withObject:dict waitUntilDone:YES];
1243 return [result autorelease];