ExtensionsDialogProvider.m 21 KB
Newer Older
1 2 3
/*****************************************************************************
 * ExtensionsDialogProvider.m: Mac OS X Extensions Dialogs
 *****************************************************************************
4
 * Copyright (C) 2010-2013 VLC authors and VideoLAN
5 6
 * $Id$
 *
7 8
 * Authors: Pierre d'Herbemont <pdherbemont # videolan org>
 *          Brendon Justin <brendonjustin@gmail.com>,
9 10
 *          Derk-Jan Hartman <hartman@videolan dot org>,
 *          Felix Paul Kühne <fkuehne@videolan dot org>
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#import "ExtensionsDialogProvider.h"

#import "intf.h"
#import "ExtensionsManager.h"
#import "misc.h"
#import "VLCUIWidgets.h"

#import <WebKit/WebKit.h>
#import <stdlib.h>

/*****************************************************************************
 * VLCExtensionsDialogProvider implementation
 *****************************************************************************/

41
static int dialogCallback(vlc_object_t *p_this, const char *psz_variable,
42
                           vlc_value_t old_val, vlc_value_t new_val,
43
                           void *param);
44 45 46 47

static NSView *createControlFromWidget(extension_widget_t *widget, id self)
{
    assert(!widget->p_sys_intf);
48
    switch (widget->type) {
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
        case EXTENSION_WIDGET_HTML:
        {
            WebView *webView = [[WebView alloc] initWithFrame:NSMakeRect (0,0,1,1)];
            [webView setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
            [webView setDrawsBackground:NO];
            return webView;
        }
        case EXTENSION_WIDGET_LABEL:
        {
            NSTextField *field = [[NSTextField alloc] init];
            [field setEditable:NO];
            [field setBordered:NO];
            [field setDrawsBackground:NO];
            [field setFont:[NSFont systemFontOfSize:0]];
            [[field cell] setControlSize:NSRegularControlSize];
            [field setAutoresizingMask:NSViewNotSizable];
            return field;
        }
        case EXTENSION_WIDGET_TEXT_FIELD:
        {
            VLCDialogTextField *field = [[VLCDialogTextField alloc] init];
            [field setWidget:widget];
            [field setAutoresizingMask:NSViewWidthSizable];
            [field setFont:[NSFont systemFontOfSize:0]];
            [[field cell] setControlSize:NSRegularControlSize];
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(syncTextField:)  name:NSControlTextDidChangeNotification object:field];
            return field;
        }
77 78 79 80 81 82 83 84 85 86 87
        case EXTENSION_WIDGET_CHECK_BOX:
        {
            VLCDialogButton *button = [[VLCDialogButton alloc] init];
            [button setButtonType:NSSwitchButton];
            [button setWidget:widget];
            [button setAction:@selector(triggerClick:)];
            [button setTarget:self];
            [[button cell] setControlSize:NSRegularControlSize];
            [button setAutoresizingMask:NSViewWidthSizable];
            return button;
        }
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
        case EXTENSION_WIDGET_BUTTON:
        {
            VLCDialogButton *button = [[VLCDialogButton alloc] init];
            [button setBezelStyle:NSRoundedBezelStyle];
            [button setWidget:widget];
            [button setAction:@selector(triggerClick:)];
            [button setTarget:self];
            [[button cell] setControlSize:NSRegularControlSize];
            [button setAutoresizingMask:NSViewNotSizable];
            return button;
        }
        case EXTENSION_WIDGET_DROPDOWN:
        {
            VLCDialogPopUpButton *popup = [[VLCDialogPopUpButton alloc] init];
            [popup setAction:@selector(popUpSelectionChanged:)];
            [popup setTarget:self];
            [popup setWidget:widget];
            return popup;
        }
        case EXTENSION_WIDGET_LIST:
        {
            NSScrollView *scrollView = [[NSScrollView alloc] init];
            [scrollView setHasVerticalScroller:YES];
            VLCDialogList *list = [[VLCDialogList alloc] init];
            [list setUsesAlternatingRowBackgroundColors:YES];
            [list setHeaderView:nil];
114
            [list setAllowsMultipleSelection:YES];
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
            [scrollView setDocumentView:list];
            [scrollView setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];

            NSTableColumn *column = [[NSTableColumn alloc] init];
            [list addTableColumn:column];
            [column release];
            [list setDataSource:list];
            [list setDelegate:self];
            [list setWidget:widget];
            [list release];
            return scrollView;
        }
        case EXTENSION_WIDGET_IMAGE:
        {
            NSImageView *imageView = [[NSImageView alloc] init];
            [imageView setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
            [imageView setImageFrameStyle:NSImageFramePhoto];
            [imageView setImageScaling:NSImageScaleProportionallyUpOrDown];
            return imageView;
        }
        case EXTENSION_WIDGET_SPIN_ICON:
        {
            NSProgressIndicator *spinner = [[NSProgressIndicator alloc] init];
            [spinner setUsesThreadedAnimation:YES];
            [spinner setStyle:NSProgressIndicatorSpinningStyle];
            [spinner setDisplayedWhenStopped:YES];
            [spinner startAnimation:self];
            return spinner;
        }
        default:
            return nil;
    }

}

static void updateControlFromWidget(NSView *control, extension_widget_t *widget, id self)
{
152
    switch (widget->type) {
153 154 155 156 157
        case EXTENSION_WIDGET_HTML:
        {
            // Get the web view
            assert([control isKindOfClass:[WebView class]]);
            WebView *webView = (WebView *)control;
158
            NSString *string = [NSString stringWithUTF8String:widget->psz_text];
159 160 161 162 163 164 165 166
            [[webView mainFrame] loadHTMLString:string baseURL:[NSURL URLWithString:@""]];
            [webView setNeedsDisplay:YES];
            break;

        }
        {
            assert([control isKindOfClass:[NSTextView class]]);
            NSTextView *textView = (NSTextView *)control;
167
            NSString *string = [NSString stringWithUTF8String:widget->psz_text];
168
            NSAttributedString *attrString = [[NSAttributedString alloc] initWithHTML:[string dataUsingEncoding: NSISOLatin1StringEncoding] documentAttributes:NULL];
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
            [[textView textStorage] setAttributedString:attrString];
            [textView setNeedsDisplay:YES];
            [textView scrollRangeToVisible:NSMakeRange(0, 0)];
            [attrString release];
            break;

        }
        case EXTENSION_WIDGET_LABEL:
        case EXTENSION_WIDGET_PASSWORD:
        case EXTENSION_WIDGET_TEXT_FIELD:
        {
            if (!widget->psz_text)
                break;
            assert([control isKindOfClass:[NSControl class]]);
            NSControl *field = (NSControl *)control;
184 185
            NSString *string = [NSString stringWithCString:widget->psz_text encoding:NSUTF8StringEncoding];
            NSAttributedString *attrString = [[NSAttributedString alloc] initWithHTML:[string dataUsingEncoding: NSISOLatin1StringEncoding] documentAttributes:NULL];
186 187 188 189 190 191 192 193 194
            [field setAttributedStringValue:attrString];
            [attrString release];
            break;
        }
        case EXTENSION_WIDGET_CHECK_BOX:
        case EXTENSION_WIDGET_BUTTON:
        {
            assert([control isKindOfClass:[NSButton class]]);
            NSButton *button = (NSButton *)control;
195 196 197
            [button setTitle:toNSStr(widget->psz_text)];
            if (widget->type == EXTENSION_WIDGET_CHECK_BOX)
                [button setState:widget->b_checked ? NSOnState : NSOffState];
198 199 200 201 202 203 204 205
            break;
        }
        case EXTENSION_WIDGET_DROPDOWN:
        {
            assert([control isKindOfClass:[NSPopUpButton class]]);
            NSPopUpButton *popup = (NSPopUpButton *)control;
            [popup removeAllItems];
            struct extension_widget_value_t *value;
206
            for (value = widget->p_values; value != NULL; value = value->p_next)
207 208
                [[popup menu] addItemWithTitle:toNSStr(value->psz_text) action:nil keyEquivalent:@""];

209 210 211 212 213 214 215 216 217 218 219 220 221
            [popup synchronizeTitleAndSelectedItem];
            [self popUpSelectionChanged:popup];
            break;
        }
        case EXTENSION_WIDGET_LIST:
        {
            assert([control isKindOfClass:[NSScrollView class]]);
            NSScrollView *scrollView = (NSScrollView *)control;
            assert([[scrollView documentView] isKindOfClass:[VLCDialogList class]]);
            VLCDialogList *list = (VLCDialogList *)[scrollView documentView];

            NSMutableArray *contentArray = [NSMutableArray array];
            struct extension_widget_value_t *value;
222
            for (value = widget->p_values; value != NULL; value = value->p_next)
223 224
            {
                NSDictionary *entry = [NSDictionary dictionaryWithObjectsAndKeys:
225 226
                                       [NSNumber numberWithInt:value->i_id], @"id",
                                       [NSString stringWithUTF8String:value->psz_text], @"text",
227 228 229 230 231 232 233 234 235 236 237
                                       nil];
                [contentArray addObject:entry];
            }
            list.contentArray = contentArray;
            [list reloadData];
            break;
        }
        case EXTENSION_WIDGET_IMAGE:
        {
            assert([control isKindOfClass:[NSImageView class]]);
            NSImageView *imageView = (NSImageView *)control;
238
            NSString *string = widget->psz_text ? [NSString stringWithUTF8String:widget->psz_text] : nil;
239 240 241 242 243 244 245 246 247 248 249
            NSImage *image = nil;
            if (string)
                image = [[NSImage alloc] initWithContentsOfURL:[NSURL fileURLWithPath:string]];
            [imageView setImage:image];
            [image release];
            break;
        }
        case EXTENSION_WIDGET_SPIN_ICON:
        {
            assert([control isKindOfClass:[NSProgressIndicator class]]);
            NSProgressIndicator *progressIndicator = (NSProgressIndicator *)control;
250
            if (widget->i_spin_loops != 0)
251 252 253 254 255 256 257 258 259 260 261 262
                [progressIndicator startAnimation:self];
            else
                [progressIndicator stopAnimation:self];
            break;
        }
    }

}

/**
 * Ask the dialogs provider to create a new dialog
 **/
263
static int dialogCallback(vlc_object_t *p_this, const char *psz_variable,
264
                           vlc_value_t old_val, vlc_value_t new_val,
265
                           void *param)
266 267 268 269 270 271 272
{
    (void) p_this;
    (void) psz_variable;
    (void) old_val;
    (void) param;

    ExtensionsDialogProvider *p_edp = [ExtensionsDialogProvider sharedInstance:(intf_thread_t *)p_this];
273
    if (!p_edp)
274
        return VLC_EGENERIC;
275
    if (!new_val.p_address)
276 277
        return VLC_EGENERIC;

278
    extension_dialog_t *p_dialog = (extension_dialog_t*) new_val.p_address;
279 280 281 282
    [p_edp manageDialog:p_dialog];
    return VLC_SUCCESS;
}

283
@implementation ExtensionsDialogProvider
284 285 286 287 288 289 290 291 292 293

static ExtensionsDialogProvider *_o_sharedInstance = nil;

+ (ExtensionsDialogProvider *)sharedInstance:(intf_thread_t *)_p_intf
{
    return _o_sharedInstance ? _o_sharedInstance : [[self alloc] initWithIntf:_p_intf];
}

+ (void)killInstance
{
294
    if (_o_sharedInstance) {
295 296 297 298 299 300
        [_o_sharedInstance release];
    }
}

- (id)initWithIntf:(intf_thread_t *)_p_intf
{
301
    if (_o_sharedInstance)
302 303
        [self dealloc];

304
    if ((self = [super init])) {
305 306 307 308
        _o_sharedInstance = self;
        p_intf = _p_intf;

        // The Cocoa interface already called dialog_Register()
309 310
        var_Create(p_intf, "dialog-extension", VLC_VAR_ADDRESS);
        var_AddCallback(p_intf, "dialog-extension", dialogCallback, NULL);
311 312 313 314 315 316 317
    }

    return _o_sharedInstance;
}

- (void)dealloc
{
318 319
    msg_Dbg(p_intf, "ExtensionsDialogProvider is quitting...");
    var_DelCallback(p_intf, "dialog-extension", dialogCallback, NULL);
320 321 322 323 324 325

    [super dealloc];
}

- (void)performEventWithObject: (NSValue *)o_value ofType: (const char*)type
{
326
    NSString *o_type = [NSString stringWithUTF8String:type];
327

328
    if ([o_type isEqualToString: @"dialog-extension"]) {
329 330 331 332 333 334
        [self performSelectorOnMainThread:@selector(updateExtensionDialog:)
                               withObject:o_value
                            waitUntilDone:YES];

    }
    else
335
        msg_Err(VLCIntf, "unhandled dialog type: '%s'", type);
336 337 338 339 340 341 342 343 344
}

- (void)triggerClick:(id)sender
{
    assert([sender isKindOfClass:[VLCDialogButton class]]);
    VLCDialogButton *button = sender;
    extension_widget_t *widget = [button widget];

    vlc_mutex_lock(&widget->p_dialog->lock);
345 346 347 348
    if (widget->type == EXTENSION_WIDGET_BUTTON)
        extension_WidgetClicked(widget->p_dialog, widget);
    else
        widget->b_checked = [button state] == NSOnState;
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
    vlc_mutex_unlock(&widget->p_dialog->lock);
}

- (void)syncTextField:(NSNotification *)notifcation
{
    id sender = [notifcation object];
    assert([sender isKindOfClass:[VLCDialogTextField class]]);
    VLCDialogTextField *field = sender;
    extension_widget_t *widget = [field widget];

    vlc_mutex_lock(&widget->p_dialog->lock);
    free(widget->psz_text);
    widget->psz_text = strdup([[field stringValue] UTF8String]);
    vlc_mutex_unlock(&widget->p_dialog->lock);
}

- (void)tableViewSelectionDidChange:(NSNotification *)notifcation
{
    id sender = [notifcation object];
    assert(sender && [sender isKindOfClass:[VLCDialogList class]]);
    VLCDialogList *list = sender;

    struct extension_widget_value_t *value;
    unsigned i = 0;
373
    NSIndexSet *selectedIndexes = [list selectedRowIndexes];
374
    for (value = [list widget]->p_values; value != NULL; value = value->p_next, i++)
375
        value->b_selected = (YES == [selectedIndexes containsIndex:i]);
376 377 378 379 380 381 382 383
}

- (void)popUpSelectionChanged:(id)sender
{
    assert([sender isKindOfClass:[VLCDialogPopUpButton class]]);
    VLCDialogPopUpButton *popup = sender;
    struct extension_widget_value_t *value;
    unsigned i = 0;
384
    for (value = [popup widget]->p_values; value != NULL; value = value->p_next, i++)
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
        value->b_selected = (i == [popup indexOfSelectedItem]);

}

- (NSSize)windowWillResize:(NSWindow *)sender toSize:(NSSize)frameSize
{
    NSView *contentView = [sender contentView];
    assert([contentView isKindOfClass:[VLCDialogGridView class]]);
    VLCDialogGridView *gridView = (VLCDialogGridView *)contentView;

    NSRect rect = NSMakeRect(0, 0, 0, 0);
    rect.size = frameSize;
    rect = [sender contentRectForFrameRect:rect];
    rect.size = [gridView flexSize:rect.size];
    rect = [sender frameRectForContentRect:rect];
    return rect.size;
}

- (BOOL)windowShouldClose:(id)sender
{
    assert([sender isKindOfClass:[VLCDialogWindow class]]);
    VLCDialogWindow *window = sender;
    extension_dialog_t *dialog = [window dialog];
    extension_DialogClosed(dialog);
    dialog->p_sys_intf = NULL;
    return YES;
}

- (void)updateWidgets:(extension_dialog_t *)dialog
{
    extension_widget_t *widget;
    VLCDialogWindow *dialogWindow = dialog->p_sys_intf;

418
    FOREACH_ARRAY(widget, dialog->widgets) {
419
        if (!widget)
420
            continue; /* Some widgets may be NULL@this point */
421 422 423 424 425

        BOOL shouldDestroy = widget->b_kill;
        NSView *control = widget->p_sys_intf;
        BOOL update = widget->b_update;

426
        if (!control && !shouldDestroy) {
427 428 429 430 431 432 433
            control = createControlFromWidget(widget, self);
            updateControlFromWidget(control, widget, self);
            widget->p_sys_intf = control;
            update = YES; // Force update and repositionning
            [control setHidden:widget->b_hide];
        }

434
        if (update && !shouldDestroy) {
435 436 437 438 439
            updateControlFromWidget(control, widget, self);
            [control setHidden:widget->b_hide];

            int row = widget->i_row - 1;
            int col = widget->i_column - 1;
440 441 442
            int hsp = __MAX(1, widget->i_horiz_span);
            int vsp = __MAX(1, widget->i_vert_span);
            if (row < 0) {
443 444 445 446 447 448 449 450 451 452
                row = 4;
                col = 0;
            }

            VLCDialogGridView *gridView = (VLCDialogGridView *)[dialogWindow contentView];
            [gridView addSubview:control atRow:row column:col rowSpan:vsp colSpan:hsp];

            widget->b_update = false;
        }

453
        if (shouldDestroy) {
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
            VLCDialogGridView *gridView = (VLCDialogGridView *)[dialogWindow contentView];
            [gridView removeSubview:control];
            [control release];
            widget->p_sys_intf = NULL;
        }
    }
    FOREACH_END()
}

/** Create a dialog
 * Note: Lock on p_dialog->lock must be held. */
- (VLCDialogWindow *)createExtensionDialog:(extension_dialog_t *)p_dialog
{
    VLCDialogWindow *dialogWindow = nil;

    BOOL shouldDestroy = p_dialog->b_kill;
470
    if (!shouldDestroy) {
471
        NSRect content = NSMakeRect(0, 0, 1, 1);
472
        dialogWindow = [[VLCDialogWindow alloc] initWithContentRect:content
473 474 475 476 477
                                                          styleMask:NSTitledWindowMask | NSClosableWindowMask | NSResizableWindowMask
                                                            backing:NSBackingStoreBuffered
                                                              defer:NO];
        [dialogWindow setDelegate:self];
        [dialogWindow setDialog:p_dialog];
478
        [dialogWindow setTitle:[NSString stringWithUTF8String:p_dialog->psz_title]];
479 480 481 482 483 484 485 486 487 488 489

        VLCDialogGridView *gridView = [[VLCDialogGridView alloc] init];
        [gridView setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
        [dialogWindow setContentView:gridView];
        [gridView release];

        p_dialog->p_sys_intf = (void *)dialogWindow;
    }

    [self updateWidgets:p_dialog];

490
    if (shouldDestroy) {
491 492 493 494 495 496 497 498 499 500 501 502 503
        [dialogWindow setDelegate:nil];
        [dialogWindow close];
        p_dialog->p_sys_intf = NULL;
        dialogWindow = nil;
    }

    return dialogWindow;
}

/** Destroy a dialog
 * Note: Lock on p_dialog->lock must be held. */
- (int)destroyExtensionDialog:(extension_dialog_t *)p_dialog
{
504
    assert(p_dialog);
505

506
    VLCDialogWindow *dialogWindow = (VLCDialogWindow*) p_dialog->p_sys_intf;
507 508
    if (!dialogWindow) {
        msg_Warn(VLCIntf, "dialog window not found");
509
        return VLC_EGENERIC;
510
    }
511

512 513 514
    [dialogWindow setDelegate:nil];
    [dialogWindow close];
    dialogWindow = nil;
515 516

    p_dialog->p_sys_intf = NULL;
517
    vlc_cond_signal(&p_dialog->cond);
518 519 520 521 522 523 524 525 526 527
    return VLC_SUCCESS;
}

/**
 * Update/Create/Destroy a dialog
 **/
- (VLCDialogWindow *)updateExtensionDialog:(NSValue *)o_value
{
    extension_dialog_t *p_dialog = [o_value pointerValue];

528 529
    VLCDialogWindow *dialogWindow = (VLCDialogWindow*) p_dialog->p_sys_intf;
    if (p_dialog->b_kill && !dialogWindow) {
530 531 532 533 534 535
        /* This extension could not be activated properly but tried
           to create a dialog. We must ignore it. */
        return NULL;
    }

    vlc_mutex_lock(&p_dialog->lock);
536
    if (!p_dialog->b_kill && !dialogWindow) {
537 538 539
        dialogWindow = [self createExtensionDialog:p_dialog];

        BOOL visible = !p_dialog->b_hide;
540
        if (visible) {
541 542
            [dialogWindow center];
            [dialogWindow makeKeyAndOrderFront:self];
543
        } else
544 545 546 547
            [dialogWindow orderOut:nil];

        [dialogWindow setHas_lock:NO];
    }
548
    else if (!p_dialog->b_kill && dialogWindow) {
549 550
        [dialogWindow setHas_lock:YES];
        [self updateWidgets:p_dialog];
551 552
        if (strcmp([[dialogWindow title] UTF8String],
                    p_dialog->psz_title) != 0) {
553
            NSString *titleString = [NSString stringWithCString:p_dialog->psz_title
554
                                                       encoding:NSUTF8StringEncoding];
555 556 557 558 559 560 561

            [dialogWindow setTitle:titleString];
        }

        [dialogWindow setHas_lock:NO];

        BOOL visible = !p_dialog->b_hide;
562
        if (visible)
563 564 565 566
            [dialogWindow makeKeyAndOrderFront:self];
        else
            [dialogWindow orderOut:nil];
    }
567
    else if (p_dialog->b_kill) {
568 569
        [self destroyExtensionDialog:p_dialog];
    }
570 571
    vlc_cond_signal(&p_dialog->cond);
    vlc_mutex_unlock(&p_dialog->lock);
572 573 574 575 576 577
    return dialogWindow;
}

/**
 * Ask the dialog manager to create/update/kill the dialog. Thread-safe.
 **/
578
- (void)manageDialog:(extension_dialog_t *)p_dialog
579
{
Felix Paul Kühne's avatar
Felix Paul Kühne committed
580
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
581
    assert(p_dialog);
582
    ExtensionsManager *extMgr = [ExtensionsManager getInstance:p_intf];
583
    assert(extMgr != NULL);
584 585 586 587 588

    NSValue *o_value = [NSValue valueWithPointer:p_dialog];
    [self performSelectorOnMainThread:@selector(updateExtensionDialog:)
                           withObject:o_value
                        waitUntilDone:YES];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
589
    [pool release];
590 591
}

592
@end