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 VLCDialogWindow : NSWindow
120 extension_dialog_t *dialog;
122 @property (readwrite) extension_dialog_t *dialog;
125 @implementation VLCDialogWindow
130 @interface VLCDialogList : NSTableView
132 extension_widget_t *widget;
133 NSMutableArray *contentArray;
135 @property (readwrite) extension_widget_t *widget;
136 @property (readwrite, retain) NSMutableArray *contentArray;
139 @implementation VLCDialogList
141 @synthesize contentArray;
143 - (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
145 return [contentArray count];
148 - (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
150 return [[contentArray objectAtIndex:rowIndex] objectForKey:@"text"];
154 @interface VLCDialogGridView : NSView {
155 NSUInteger _rowCount, _colCount;
156 NSMutableArray *_gridedViews;
159 - (NSSize)flexSize:(NSSize)size;
160 - (void)removeSubview:(NSView *)view;
164 // Move this to separate file
165 @implementation VLCDialogGridView
169 [_gridedViews release];
173 - (void)recomputeCount
177 for (NSDictionary *obj in _gridedViews)
179 NSUInteger row = [[obj objectForKey:@"row"] intValue];
180 NSUInteger col = [[obj objectForKey:@"col"] intValue];
181 if (col + 1 > _colCount)
183 if (row + 1 > _rowCount)
188 - (void)recomputeWindowSize
190 [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(recomputeWindowSize) object:nil];
192 NSWindow *window = [self window];
193 NSRect frame = [window frame];
194 NSRect contentRect = [window contentRectForFrameRect:frame];
195 contentRect.size = [self flexSize:frame.size];
196 NSRect newFrame = [window frameRectForContentRect:contentRect];
197 newFrame.origin.y -= newFrame.size.height - frame.size.height;
198 newFrame.origin.x -= (newFrame.size.width - frame.size.width) / 2;
199 [window setFrame:newFrame display:YES animate:YES];
202 - (NSSize)objectSizeToFit:(NSView *)view
204 if ([view isKindOfClass:[NSControl class]]) {
205 NSControl *control = (NSControl *)view;
206 return [[control cell] cellSize];
208 return [view frame].size;
220 - (CGFloat)constrainedHeightOfRow:(NSUInteger)targetRow
223 for(NSDictionary *obj in _gridedViews) {
224 NSUInteger row = [[obj objectForKey:@"row"] intValue];
225 if (row != targetRow)
227 NSUInteger rowSpan = [[obj objectForKey:@"rowSpan"] intValue];
230 NSView *view = [obj objectForKey:@"view"];
231 if ([view autoresizingMask] & NSViewHeightSizable)
233 NSSize sizeToFit = [self objectSizeToFit:view];
234 if (height < sizeToFit.height)
235 height = sizeToFit.height;
240 - (CGFloat)remainingRowsHeight
242 NSUInteger height = [self marginY];
245 NSUInteger autosizedRows = 0;
246 for (NSUInteger i = 0; i < _rowCount; i++) {
247 CGFloat constrainedHeight = [self constrainedHeightOfRow:i];
248 if (!constrainedHeight)
250 height += constrainedHeight + [self marginY];
252 CGFloat remaining = 0;
253 if (height < self.bounds.size.height && autosizedRows)
254 remaining = (self.bounds.size.height - height) / autosizedRows;
261 - (CGFloat)heightOfRow:(NSUInteger)targetRow
263 NSAssert(targetRow < _rowCount, @"accessing a non existing row");
264 CGFloat height = [self constrainedHeightOfRow:targetRow];
266 height = [self remainingRowsHeight];
271 - (CGFloat)topOfRow:(NSUInteger)targetRow
273 CGFloat top = [self marginY];
274 for (NSUInteger i = 1; i < _rowCount - targetRow; i++)
276 top += [self heightOfRow:_rowCount - i] + [self marginY];
281 - (CGFloat)constrainedWidthOfColumn:(NSUInteger)targetColumn
284 for(NSDictionary *obj in _gridedViews) {
285 NSUInteger col = [[obj objectForKey:@"col"] intValue];
286 if (col != targetColumn)
288 NSUInteger colSpan = [[obj objectForKey:@"colSpan"] intValue];
291 NSView *view = [obj objectForKey:@"view"];
292 if ([view autoresizingMask] & NSViewWidthSizable)
294 NSSize sizeToFit = [self objectSizeToFit:view];
295 if (width < sizeToFit.width)
296 width = sizeToFit.width;
301 - (CGFloat)remainingColumnWidth
303 NSUInteger width = [self marginX];
306 NSUInteger autosizedCol = 0;
307 for (NSUInteger i = 0; i < _colCount; i++) {
308 CGFloat constrainedWidth = [self constrainedWidthOfColumn:i];
309 if (!constrainedWidth)
311 width += constrainedWidth + [self marginX];
314 CGFloat remaining = 0;
315 if (width < self.bounds.size.width && autosizedCol)
316 remaining = (self.bounds.size.width - width) / autosizedCol;
322 - (CGFloat)widthOfColumn:(NSUInteger)targetColumn
324 CGFloat width = [self constrainedWidthOfColumn:targetColumn];
326 width = [self remainingColumnWidth];
331 - (CGFloat)leftOfColumn:(NSUInteger)targetColumn
333 CGFloat left = [self marginX];
334 for (NSUInteger i = 0; i < targetColumn; i++)
336 left += [self widthOfColumn:i] + [self marginX];
344 for(NSDictionary *obj in _gridedViews) {
345 NSUInteger row = [[obj objectForKey:@"row"] intValue];
346 NSUInteger col = [[obj objectForKey:@"col"] intValue];
347 NSUInteger rowSpan = [[obj objectForKey:@"rowSpan"] intValue];
348 NSUInteger colSpan = [[obj objectForKey:@"colSpan"] intValue];
349 NSView *view = [obj objectForKey:@"view"];
353 if ([view autoresizingMask] & NSViewHeightSizable || rowSpan > 1) {
355 for (NSUInteger r = 0; r < rowSpan; r++) {
356 if (row + r >= _rowCount)
358 height += [self heightOfRow:row + r] + [self marginY];
360 rect.size.height = height - [self marginY];
363 rect.size.height = [self objectSizeToFit:view].height;
366 if ([view autoresizingMask] & NSViewWidthSizable) {
368 for (NSUInteger c = 0; c < colSpan; c++)
369 width += [self widthOfColumn:col + c] + [self marginX];
370 rect.size.width = width - [self marginX];
373 rect.size.width = [self objectSizeToFit:view].width;
376 rect.origin.y = [self topOfRow:row] + ([self heightOfRow:row] - rect.size.height) / 2;
377 rect.origin.x = [self leftOfColumn:col];
379 [view setFrame:rect];
380 [view setNeedsDisplay:YES];
384 - (NSMutableDictionary *)objectForView:(NSView *)view
386 for (NSMutableDictionary *dict in _gridedViews)
388 if ([dict objectForKey:@"view"] == view)
394 - (void)addSubview:(NSView *)view atRow:(NSUInteger)row column:(NSUInteger)column rowSpan:(NSUInteger)rowSpan colSpan:(NSUInteger)colSpan
396 if (row + 1 > _rowCount)
398 if (column + 1 > _colCount)
399 _colCount = column + 1;
402 _gridedViews = [[NSMutableArray alloc] init];
404 NSMutableDictionary *dict = [self objectForView:view];
406 dict = [NSMutableDictionary dictionary];
407 [dict setObject:view forKey:@"view"];
408 [_gridedViews addObject:dict];
410 [dict setObject:[NSNumber numberWithInt:rowSpan] forKey:@"rowSpan"];
411 [dict setObject:[NSNumber numberWithInt:colSpan] forKey:@"colSpan"];
412 [dict setObject:[NSNumber numberWithInt:row] forKey:@"row"];
413 [dict setObject:[NSNumber numberWithInt:column] forKey:@"col"];
416 [self addSubview:view];
419 // Recompute the size of the window after making sure we won't see anymore update
420 [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(recomputeWindowSize) object:nil];
421 [self performSelector:@selector(recomputeWindowSize) withObject:nil afterDelay:0.1];
424 - (void)removeSubview:(NSView *)view
426 NSDictionary *dict = [self objectForView:view];
428 [_gridedViews removeObject:dict];
429 [view removeFromSuperview];
431 [self recomputeCount];
432 [self recomputeWindowSize];
435 [self setNeedsDisplay:YES];
438 - (void)setFrame:(NSRect)frameRect
440 [super setFrame:frameRect];
444 - (NSSize)flexSize:(NSSize)size
446 if (!_rowCount || !_colCount)
449 CGFloat minHeight = [self marginY];
450 BOOL canFlexHeight = NO;
451 for (NSUInteger i = 0; i < _rowCount; i++) {
452 CGFloat constrained = [self constrainedHeightOfRow:i];
457 minHeight += constrained + [self marginY];
460 CGFloat minWidth = [self marginX];
461 BOOL canFlexWidth = NO;
462 for (NSUInteger i = 0; i < _colCount; i++) {
463 CGFloat constrained = [self constrainedWidthOfColumn:i];
468 minWidth += constrained + [self marginX];
470 if (size.width < minWidth)
471 size.width = minWidth;
472 if (size.height < minHeight)
473 size.height = minHeight;
475 size.height = minHeight;
477 size.width = minWidth;
484 static inline NSDictionary *DictFromDialogFatal(dialog_fatal_t *dialog) {
485 return [VLCDialogDisplayer dictionaryForDialog:dialog->title :dialog->message :NULL :NULL :NULL];
487 static inline NSDictionary *DictFromDialogLogin(dialog_login_t *dialog) {
488 return [VLCDialogDisplayer dictionaryForDialog:dialog->title :dialog->message :NULL :NULL :NULL];
490 static inline NSDictionary *DictFromDialogQuestion(dialog_question_t *dialog) {
491 return [VLCDialogDisplayer dictionaryForDialog:dialog->title :dialog->message :dialog->yes :dialog->no :dialog->cancel];
493 static inline NSDictionary *DictFromDialogProgressBar(dialog_progress_bar_t *dialog) {
494 return [VLCDialogDisplayer dictionaryForDialog:dialog->title :dialog->message :NULL :NULL :dialog->cancel];
499 VLCDialogDisplayer *displayer;
503 bool is_hidding_noaction_dialogs;
507 #define T_HIDE_NOACTION N_("Hide no user action dialogs")
508 #define LT_HIDE_NOACTION N_("Don't display dialogs that don't require user action (Critical and error panel).")
510 #define prefix "macosx-dialog-provider"
511 /*****************************************************************************
513 *****************************************************************************/
516 /* Minimal interface. see intf.m */
517 set_shortname("Mac OS X Dialogs")
518 add_shortcut("macosx_dialog_provider")
519 add_shortcut("miosx")
520 set_description("Minimal Mac OS X Dialog Provider")
521 set_capability("interface", 0)
523 /* This setting is interesting, because when used with a libvlc app
524 * it's almost certain that the client program will display error by
525 * itself. Moreover certain action might end up in an error, but
526 * the client wants to ignored them completely. */
527 add_bool(prefix "hide-no-user-action-dialogs", true, NULL, T_HIDE_NOACTION, LT_HIDE_NOACTION, false)
529 set_callbacks(OpenIntf, CloseIntf)
530 set_category(CAT_INTERFACE)
531 set_subcategory(SUBCAT_INTERFACE_MAIN)
534 /*****************************************************************************
535 * OpenIntf: initialize interface
536 *****************************************************************************/
537 int OpenIntf(vlc_object_t *p_this)
539 intf_thread_t *p_intf = (intf_thread_t*) p_this;
541 p_intf->p_sys = malloc(sizeof(intf_sys_t));
545 memset(p_intf->p_sys,0,sizeof(*p_intf->p_sys));
547 p_intf->p_sys->displayer = [[VLCDialogDisplayer alloc] init];
549 bool hide = var_CreateGetBool(p_intf, prefix "hide-no-user-action-dialogs");
550 p_intf->p_sys->is_hidding_noaction_dialogs = hide;
552 /* subscribe to various interactive dialogues */
556 var_Create(p_intf,"dialog-error",VLC_VAR_ADDRESS);
557 var_AddCallback(p_intf,"dialog-error",DisplayError,p_intf);
558 var_Create(p_intf,"dialog-critical",VLC_VAR_ADDRESS);
559 var_AddCallback(p_intf,"dialog-critical",DisplayCritical,p_intf);
561 var_Create(p_intf,"dialog-login",VLC_VAR_ADDRESS);
562 var_AddCallback(p_intf,"dialog-login",DisplayLogin,p_intf);
563 var_Create(p_intf,"dialog-question",VLC_VAR_ADDRESS);
564 var_AddCallback(p_intf,"dialog-question",DisplayQuestion,p_intf);
565 var_Create(p_intf,"dialog-progress-bar",VLC_VAR_ADDRESS);
566 var_AddCallback(p_intf,"dialog-progress-bar",DisplayProgressPanelAction,p_intf);
567 var_Create(p_intf,"dialog-extension",VLC_VAR_ADDRESS);
568 var_AddCallback(p_intf,"dialog-extension",DisplayExtension,p_intf);
569 dialog_Register(p_intf);
571 msg_Dbg(p_intf,"Mac OS X dialog provider initialised");
576 /*****************************************************************************
577 * CloseIntf: destroy interface
578 *****************************************************************************/
579 void CloseIntf(vlc_object_t *p_this)
581 intf_thread_t *p_intf = (intf_thread_t*) p_this;
583 /* unsubscribe from the interactive dialogues */
584 dialog_Unregister(p_intf);
586 if (!p_intf->p_sys->is_hidding_noaction_dialogs)
588 var_DelCallback(p_intf,"dialog-error",DisplayError,p_intf);
589 var_DelCallback(p_intf,"dialog-critical",DisplayCritical,p_intf);
591 var_DelCallback(p_intf,"dialog-login",DisplayLogin,p_intf);
592 var_DelCallback(p_intf,"dialog-question",DisplayQuestion,p_intf);
593 var_DelCallback(p_intf,"dialog-progress-bar",DisplayProgressPanelAction,p_intf);
594 var_DelCallback(p_intf,"dialog-extension",DisplayExtension,p_intf);
596 [p_intf->p_sys->displayer release];
598 msg_Dbg(p_intf,"Mac OS X dialog provider closed");
603 /*****************************************************************************
604 * Callbacks triggered by the "dialog-*" variables
605 *****************************************************************************/
606 static int DisplayError(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
608 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
609 dialog_fatal_t *dialog = value.p_address;
610 intf_thread_t *p_intf = (intf_thread_t*) p_this;
611 intf_sys_t *sys = p_intf->p_sys;
612 [sys->displayer performSelectorOnMainThread:@selector(displayError:) withObject:DictFromDialogFatal(dialog) waitUntilDone:NO];
617 static int DisplayCritical(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
619 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
620 dialog_fatal_t *dialog = value.p_address;
621 intf_thread_t *p_intf = (intf_thread_t*) p_this;
622 intf_sys_t *sys = p_intf->p_sys;
623 [sys->displayer performSelectorOnMainThread:@selector(displayCritical:) withObject:DictFromDialogFatal(dialog) waitUntilDone:NO];
628 static int DisplayQuestion(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
630 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
631 dialog_question_t *dialog = value.p_address;
632 intf_thread_t *p_intf = (intf_thread_t*) p_this;
633 intf_sys_t *sys = p_intf->p_sys;
634 dialog->answer = [[sys->displayer resultFromSelectorOnMainThread:@selector(displayQuestion:) withObject:DictFromDialogQuestion(dialog)] intValue];
639 static int DisplayLogin(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
641 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
642 dialog_login_t *dialog = value.p_address;
643 intf_thread_t *p_intf = (intf_thread_t*) p_this;
644 intf_sys_t *sys = p_intf->p_sys;
645 NSDictionary *dict = [sys->displayer resultFromSelectorOnMainThread:@selector(displayLogin:) withObject:DictFromDialogLogin(dialog)];
647 *dialog->username = strdup([[dict objectForKey:@"username"] UTF8String]);
648 *dialog->password = strdup([[dict objectForKey:@"password"] UTF8String]);
654 static int DisplayProgressPanelAction(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
656 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
657 dialog_progress_bar_t *dialog = value.p_address;
658 intf_thread_t *p_intf = (intf_thread_t*) p_this;
659 intf_sys_t *sys = p_intf->p_sys;
661 [sys->displayer performSelectorOnMainThread:@selector(displayProgressBar:) withObject:DictFromDialogProgressBar(dialog) waitUntilDone:YES];
663 dialog->pf_update = updateProgressPanel;
664 dialog->pf_check = checkProgressPanel;
665 dialog->pf_destroy = destroyProgressPanel;
666 dialog->p_sys = p_intf->p_sys;
672 static int DisplayExtension(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
674 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
675 intf_thread_t *p_intf = (intf_thread_t*) p_this;
676 intf_sys_t *sys = p_intf->p_sys;
677 extension_dialog_t *dialog = value.p_address;
679 // -updateExtensionDialog: Open its own runloop, so be sure to run on DefaultRunLoop.
680 [sys->displayer performSelectorOnMainThread:@selector(updateExtensionDialog:) withObject:[NSValue valueWithPointer:dialog] waitUntilDone:YES];
686 void updateProgressPanel (void *priv, const char *text, float value)
688 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
689 intf_sys_t *sys = (intf_sys_t *)priv;
691 NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
692 [NSNumber numberWithFloat:value], @"value",
693 text ? [NSString stringWithUTF8String:text] : nil, @"text",
696 [sys->displayer performSelectorOnMainThread:@selector(updateProgressPanel:) withObject:dict waitUntilDone:YES];
701 void destroyProgressPanel (void *priv)
703 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
704 intf_sys_t *sys = (intf_sys_t *)priv;
706 [sys->displayer performSelectorOnMainThread:@selector(destroyProgressPanel) withObject:nil waitUntilDone:YES];
711 bool checkProgressPanel (void *priv)
713 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
714 intf_sys_t *sys = (intf_sys_t *)priv;
717 ret = [[sys->displayer resultFromSelectorOnMainThread:@selector(checkProgressPanel) withObject:nil] boolValue];
724 @implementation VLCDialogDisplayer
727 assert(!_currentProgressBarPanel); // This has to be closed on main thread.
731 + (NSDictionary *)dictionaryForDialog:(const char *)title :(const char *)message :(const char *)yes :(const char *)no :(const char *)cancel
733 NSMutableDictionary *dict = [NSMutableDictionary dictionary];
735 [dict setObject:[NSString stringWithUTF8String:title] forKey:@"title"];
737 [dict setObject:[NSString stringWithUTF8String:message] forKey:@"message"];
739 [dict setObject:[NSString stringWithUTF8String:yes] forKey:@"yes"];
741 [dict setObject:[NSString stringWithUTF8String:no] forKey:@"no"];
743 [dict setObject:[NSString stringWithUTF8String:cancel] forKey:@"cancel"];
747 #define VLCAssertIsMainThread() assert([NSThread isMainThread])
749 - (void)displayError:(NSDictionary *)dialog
751 VLCAssertIsMainThread();
753 NSRunInformationalAlertPanel([dialog objectForKey:@"title"],
754 [dialog objectForKey:@"message"],
758 - (void)displayCritical:(NSDictionary *)dialog
760 VLCAssertIsMainThread();
762 NSRunCriticalAlertPanel([dialog objectForKey:@"title"],
763 [dialog objectForKey:@"message"],
767 - (NSNumber *)displayQuestion:(NSDictionary *)dialog
769 VLCAssertIsMainThread();
771 NSInteger alertRet = 0;
773 NSAlert *alert = [NSAlert alertWithMessageText:[dialog objectForKey:@"title"]
774 defaultButton:[dialog objectForKey:@"yes"]
775 alternateButton:[dialog objectForKey:@"no"]
776 otherButton:[dialog objectForKey:@"cancel"]
777 informativeTextWithFormat:[dialog objectForKey:@"message"]];
778 [alert setAlertStyle:NSInformationalAlertStyle];
779 alertRet = [alert runModal];
783 case NSAlertDefaultReturn:
786 case NSAlertAlternateReturn:
789 case NSAlertOtherReturn:
798 return [NSNumber numberWithInt:ret];
801 - (NSDictionary *)displayLogin:(NSDictionary *)dialog
803 VLCAssertIsMainThread();
805 VLCLoginPanel *panel = [[VLCLoginPanel alloc] init];
806 [panel createContentView];
807 [panel setDialogTitle:[dialog objectForKey:@"title"]];
808 [panel setDialogMessage:[dialog objectForKey:@"message"]];
810 NSInteger ret = [NSApp runModalForWindow:panel];
816 return [NSDictionary dictionaryWithObjectsAndKeys:
817 [panel userName], @"username",
818 [panel password], @"password",
822 - (void)displayProgressBar:(NSDictionary *)dialog
824 VLCAssertIsMainThread();
826 if(_currentProgressBarPanel)
827 [self destroyProgressPanel];
829 assert(!_currentProgressBarPanel);
830 _currentProgressBarPanel = [[VLCProgressPanel alloc] init];
831 [_currentProgressBarPanel createContentView];
832 [_currentProgressBarPanel setDialogTitle:[dialog objectForKey:@"title"]];
833 [_currentProgressBarPanel setDialogMessage:[dialog objectForKey:@"message"] ?: @""];
834 [_currentProgressBarPanel setCancelButtonLabel:[dialog objectForKey:@"cancel"]];
836 [_currentProgressBarPanel center];
837 [_currentProgressBarPanel makeKeyAndOrderFront:nil];
840 - (void)updateProgressPanel:(NSDictionary *)dict
842 VLCAssertIsMainThread();
844 assert(_currentProgressBarPanel);
845 [_currentProgressBarPanel setDialogMessage:[dict objectForKey:@"text"] ?: @""];
846 [_currentProgressBarPanel setProgressAsDouble:[[dict objectForKey:@"value"] doubleValue] * 1000.];
849 - (void)destroyProgressPanel
851 VLCAssertIsMainThread();
853 [_currentProgressBarPanel close];
854 [_currentProgressBarPanel release];
855 _currentProgressBarPanel = nil;
858 - (NSNumber *)checkProgressPanel
860 VLCAssertIsMainThread();
862 return [NSNumber numberWithBool:[_currentProgressBarPanel isCancelled]];
866 #pragma mark Extensions Dialog
868 - (void)triggerClick:(id)sender
870 assert([sender isKindOfClass:[VLCDialogButton class]]);
871 VLCDialogButton *button = sender;
872 extension_widget_t *widget = [button widget];
874 NSLog(@"(triggerClick)");
875 vlc_mutex_lock(&widget->p_dialog->lock);
876 extension_WidgetClicked(widget->p_dialog, widget);
877 vlc_mutex_unlock(&widget->p_dialog->lock);
880 - (void)syncTextField:(NSNotification *)notifcation
882 id sender = [notifcation object];
883 assert([sender isKindOfClass:[VLCDialogTextField class]]);
884 VLCDialogTextField *field = sender;
885 extension_widget_t *widget = [field widget];
887 vlc_mutex_lock(&widget->p_dialog->lock);
888 free(widget->psz_text);
889 widget->psz_text = strdup([[field stringValue] UTF8String]);
890 vlc_mutex_unlock(&widget->p_dialog->lock);
893 - (void)tableViewSelectionDidChange:(NSNotification *)notifcation
895 id sender = [notifcation object];
896 assert(sender && [sender isKindOfClass:[VLCDialogList class]]);
897 VLCDialogList *list = sender;
899 struct extension_widget_value_t *value;
901 for(value = [list widget]->p_values; value != NULL; value = value->p_next, i++)
902 value->b_selected = (i == [list selectedRow]);
905 - (void)popUpSelectionChanged:(id)sender
907 assert([sender isKindOfClass:[VLCDialogPopUpButton class]]);
908 VLCDialogPopUpButton *popup = sender;
909 struct extension_widget_value_t *value;
911 for(value = [popup widget]->p_values; value != NULL; value = value->p_next, i++)
912 value->b_selected = (i == [popup indexOfSelectedItem]);
916 - (NSSize)windowWillResize:(NSWindow *)sender toSize:(NSSize)frameSize
918 NSView *contentView = [sender contentView];
919 assert([contentView isKindOfClass:[VLCDialogGridView class]]);
920 VLCDialogGridView *gridView = (VLCDialogGridView *)contentView;
922 NSRect rect = NSMakeRect(0, 0, 0, 0);
923 rect.size = frameSize;
924 rect = [sender contentRectForFrameRect:rect];
925 rect.size = [gridView flexSize:rect.size];
926 rect = [sender frameRectForContentRect:rect];
930 - (BOOL)windowShouldClose:(id)sender
932 assert([sender isKindOfClass:[VLCDialogWindow class]]);
933 VLCDialogWindow *window = sender;
934 extension_dialog_t *dialog = [window dialog];
935 extension_DialogClosed(dialog);
936 dialog->p_sys_intf = NULL;
940 static NSView *createControlFromWidget(extension_widget_t *widget, id self)
942 assert(!widget->p_sys_intf);
944 switch (widget->type)
946 case EXTENSION_WIDGET_HTML:
948 // NSScrollView *scrollView = [[NSScrollView alloc] init];
949 // [scrollView setHasVerticalScroller:YES];
950 // NSTextView *field = [[NSTextView alloc] init];
951 // [scrollView setDocumentView:field];
952 // [scrollView setAutoresizesSubviews:YES];
953 // [scrollView setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
955 // return scrollView;
956 NSTextView *field = [[NSTextView alloc] init];
957 [field setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
958 [field setDrawsBackground:NO];
961 case EXTENSION_WIDGET_LABEL:
963 NSTextField *field = [[NSTextField alloc] init];
964 [field setEditable:NO];
965 [field setBordered:NO];
966 [field setDrawsBackground:NO];
967 [field setFont:[NSFont systemFontOfSize:0]];
968 [[field cell] setControlSize:NSRegularControlSize];
969 [field setAutoresizingMask:NSViewNotSizable];
972 case EXTENSION_WIDGET_TEXT_FIELD:
974 VLCDialogTextField *field = [[VLCDialogTextField alloc] init];
975 [field setWidget:widget];
976 [field setAutoresizingMask:NSViewWidthSizable];
977 [field setFont:[NSFont systemFontOfSize:0]];
978 [[field cell] setControlSize:NSRegularControlSize];
979 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(syncTextField:) name:NSControlTextDidChangeNotification object:field];
982 case EXTENSION_WIDGET_BUTTON:
984 VLCDialogButton *button = [[VLCDialogButton alloc] init];
985 [button setBezelStyle:NSRoundedBezelStyle];
986 [button setWidget:widget];
987 [button setAction:@selector(triggerClick:)];
988 [button setTarget:self];
989 [[button cell] setControlSize:NSRegularControlSize];
990 [button setAutoresizingMask:NSViewNotSizable];
993 case EXTENSION_WIDGET_DROPDOWN:
995 VLCDialogPopUpButton *popup = [[VLCDialogPopUpButton alloc] init];
996 [popup setAction:@selector(popUpSelectionChanged:)];
997 [popup setTarget:self];
998 [popup setWidget:widget];
1001 case EXTENSION_WIDGET_LIST:
1003 NSScrollView *scrollView = [[NSScrollView alloc] init];
1004 [scrollView setHasVerticalScroller:YES];
1005 VLCDialogList *list = [[VLCDialogList alloc] init];
1006 [list setUsesAlternatingRowBackgroundColors:YES];
1007 [list setHeaderView:nil];
1008 [scrollView setDocumentView:list];
1009 [scrollView setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
1011 NSTableColumn *column = [[NSTableColumn alloc] init];
1012 [list addTableColumn:column];
1014 [list setDataSource:list];
1015 [list setDelegate:self];
1016 [list setWidget:widget];
1020 case EXTENSION_WIDGET_IMAGE:
1022 NSImageView *imageView = [[NSImageView alloc] init];
1023 [imageView setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
1024 [imageView setImageFrameStyle:NSImageFramePhoto];
1025 [imageView setImageScaling:NSImageScaleProportionallyUpOrDown];
1035 static void updateControlFromWidget(NSView *control, extension_widget_t *widget, id self)
1037 switch (widget->type)
1039 case EXTENSION_WIDGET_HTML:
1041 // // Get the scroll view
1042 // assert([control isKindOfClass:[NSScrollView class]]);
1043 // NSScrollView *scrollView = (NSScrollView *)control;
1044 // control = [scrollView documentView];
1046 // assert([control isKindOfClass:[NSTextView class]]);
1047 // NSTextView *textView = (NSTextView *)control;
1048 // NSString *string = [NSString stringWithUTF8String:widget->psz_text];
1049 // NSAttributedString *attrString = [[NSAttributedString alloc] initWithHTML:[string dataUsingEncoding:NSUTF8StringEncoding] documentAttributes:NULL];
1050 // [[textView textStorage] setAttributedString:[[NSAttributedString alloc] initWithString:@"Hello"]];
1051 // NSLog(@"%@", string);
1052 // [textView setNeedsDisplay:YES];
1053 // [textView scrollRangeToVisible:NSMakeRange(0, 0)];
1054 // [attrString release];
1059 assert([control isKindOfClass:[NSTextView class]]);
1060 NSTextView *textView = (NSTextView *)control;
1061 NSString *string = [NSString stringWithUTF8String:widget->psz_text];
1062 NSAttributedString *attrString = [[NSAttributedString alloc] initWithHTML:[string dataUsingEncoding:NSUTF8StringEncoding] documentAttributes:NULL];
1063 [[textView textStorage] setAttributedString:attrString];
1064 [textView setNeedsDisplay:YES];
1065 [textView scrollRangeToVisible:NSMakeRange(0, 0)];
1066 [attrString release];
1070 case EXTENSION_WIDGET_LABEL:
1071 case EXTENSION_WIDGET_PASSWORD:
1072 case EXTENSION_WIDGET_TEXT_FIELD:
1074 if (!widget->psz_text)
1076 assert([control isKindOfClass:[NSControl class]]);
1077 NSControl *field = (NSControl *)control;
1078 NSString *string = [NSString stringWithUTF8String:widget->psz_text];
1079 NSAttributedString *attrString = [[NSAttributedString alloc] initWithHTML:[string dataUsingEncoding:NSUTF8StringEncoding] documentAttributes:NULL];
1080 [field setAttributedStringValue:attrString];
1081 [attrString release];
1084 case EXTENSION_WIDGET_BUTTON:
1086 assert([control isKindOfClass:[NSButton class]]);
1087 NSButton *button = (NSButton *)control;
1088 if (!widget->psz_text)
1090 [button setTitle:[NSString stringWithUTF8String:widget->psz_text]];
1093 case EXTENSION_WIDGET_DROPDOWN:
1095 assert([control isKindOfClass:[NSPopUpButton class]]);
1096 NSPopUpButton *popup = (NSPopUpButton *)control;
1097 [popup removeAllItems];
1098 struct extension_widget_value_t *value;
1099 for(value = widget->p_values; value != NULL; value = value->p_next)
1101 [popup addItemWithTitle:[NSString stringWithUTF8String:value->psz_text]];
1103 [popup synchronizeTitleAndSelectedItem];
1104 [self popUpSelectionChanged:popup];
1108 case EXTENSION_WIDGET_LIST:
1110 assert([control isKindOfClass:[NSScrollView class]]);
1111 NSScrollView *scrollView = (NSScrollView *)control;
1112 assert([[scrollView documentView] isKindOfClass:[VLCDialogList class]]);
1113 VLCDialogList *list = (VLCDialogList *)[scrollView documentView];
1115 NSMutableArray *contentArray = [NSMutableArray array];
1116 struct extension_widget_value_t *value;
1117 for(value = widget->p_values; value != NULL; value = value->p_next)
1119 NSDictionary *entry = [NSDictionary dictionaryWithObjectsAndKeys:
1120 [NSNumber numberWithInt:value->i_id], @"id",
1121 [NSString stringWithUTF8String:value->psz_text], @"text",
1123 [contentArray addObject:entry];
1125 list.contentArray = contentArray;
1129 case EXTENSION_WIDGET_IMAGE:
1131 assert([control isKindOfClass:[NSImageView class]]);
1132 NSImageView *imageView = (NSImageView *)control;
1133 NSString *string = widget->psz_text ? [NSString stringWithUTF8String:widget->psz_text] : nil;
1134 NSImage *image = nil;
1135 NSLog(@"Setting image to %@", string);
1137 image = [[NSImage alloc] initWithContentsOfURL:[NSURL fileURLWithPath:string]];
1138 [imageView setImage:image];
1146 - (void)updateWidgets:(extension_dialog_t *)dialog
1148 extension_widget_t *widget;
1149 NSWindow *window = dialog->p_sys_intf;
1150 FOREACH_ARRAY(widget, dialog->widgets)
1153 continue; /* Some widgets may be NULL at this point */
1155 BOOL shouldDestroy = widget->b_kill;
1156 NSView *control = widget->p_sys_intf;
1157 BOOL update = widget->b_update;
1160 if (!control && !shouldDestroy)
1162 control = createControlFromWidget(widget, self);
1163 updateControlFromWidget(control, widget, self);
1164 widget->p_sys_intf = control;
1165 update = YES; // Force update and repositionning
1166 [control setHidden:widget->b_hide];
1169 if (update && !shouldDestroy)
1171 updateControlFromWidget(control, widget, self);
1172 [control setHidden:widget->b_hide];
1174 int row = widget->i_row - 1;
1175 int col = widget->i_column - 1;
1176 int hsp = __MAX( 1, widget->i_horiz_span );
1177 int vsp = __MAX( 1, widget->i_vert_span );
1184 VLCDialogGridView *gridView = (VLCDialogGridView *)[window contentView];
1185 [gridView addSubview:control atRow:row column:col rowSpan:vsp colSpan:hsp];
1187 //this->resize( sizeHint() );
1188 widget->b_update = false;
1193 VLCDialogGridView *gridView = (VLCDialogGridView *)[window contentView];
1194 [gridView removeSubview:control];
1196 widget->p_sys_intf = NULL;
1202 - (void)updateExtensionDialog:(NSValue *)extensionDialog
1204 extension_dialog_t *dialog = [extensionDialog pointerValue];
1206 vlc_mutex_lock(&dialog->lock);
1208 NSSize size = NSMakeSize(dialog->i_width, dialog->i_height);
1210 BOOL shouldDestroy = dialog->b_kill;
1212 if (!dialog->i_width || !dialog->i_height)
1213 size = NSMakeSize(640, 480);
1215 VLCDialogWindow *window = dialog->p_sys_intf;
1216 if (!window && !shouldDestroy)
1218 NSRect content = NSMakeRect(0, 0, 1, 1);
1219 window = [[VLCDialogWindow alloc] initWithContentRect:content styleMask:NSTitledWindowMask | NSClosableWindowMask | NSResizableWindowMask backing:NSBackingStoreBuffered defer:NO];
1220 [window setDelegate:self];
1221 [window setDialog:dialog];
1222 [window setTitle:[NSString stringWithUTF8String:dialog->psz_title]];
1223 VLCDialogGridView *gridView = [[VLCDialogGridView alloc] init];
1224 [gridView setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
1225 [window setContentView:gridView];
1227 dialog->p_sys_intf = window;
1230 [self updateWidgets:dialog];
1234 [window setDelegate:nil];
1236 dialog->p_sys_intf = NULL;
1240 if (![window isVisible]) {
1242 [window makeKeyAndOrderFront:self];
1245 vlc_cond_signal(&dialog->cond);
1246 vlc_mutex_unlock(&dialog->lock);
1251 * Helper to execute a function on main thread and get its return value.
1253 - (void)execute:(NSDictionary *)dict
1255 SEL sel = [[dict objectForKey:@"sel"] pointerValue];
1256 id *result = [[dict objectForKey:@"result"] pointerValue];
1257 id object = [dict objectForKey:@"object"];
1259 NSAssert(sel, @"Try to execute a NULL selector");
1261 *result = [self performSelector:sel withObject:object];
1262 [*result retain]; // Balanced in -resultFromSelectorOnMainThread
1265 - (id)resultFromSelectorOnMainThread:(SEL)sel withObject:(id)object
1268 NSAssert(sel, @"Try to execute a NULL selector");
1269 NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
1270 [NSValue valueWithPointer:sel], @"sel",
1271 [NSValue valueWithPointer:&result], @"result",
1272 object, @"object", nil];
1273 [self performSelectorOnMainThread:@selector(execute:) withObject:dict waitUntilDone:YES];
1274 return [result autorelease];