MainWindowTitle.m 16.8 KB
Newer Older
1 2 3
/*****************************************************************************
 * MainWindowTitle.m: MacOS X interface module
 *****************************************************************************
4
 * Copyright (C) 2011-2012 Felix Paul Kühne
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 * $Id$
 *
 * Authors: Felix Paul Kühne <fkuehne -at- videolan -dot- org>
 *
 * 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 <vlc_common.h>
#import "intf.h"
#import "MainWindowTitle.h"
#import "CoreInteraction.h"
28
#import "CompatibilityFixes.h"
29 30 31

/*****************************************************************************
 * VLCMainWindowTitleView
32 33 34 35
 *
 * this is our title bar, which can do anything a title should do
 * it relies on the VLCWindowButtonCell to display the correct traffic light
 * states, since we can't capture the mouse-moved events here correctly
36 37 38
 *****************************************************************************/

@implementation VLCMainWindowTitleView
39 40 41 42 43 44 45
- (id)init
{
    o_window_title_attributes_dict = [[NSDictionary dictionaryWithObjectsAndKeys: [NSColor whiteColor], NSForegroundColorAttributeName, [NSFont titleBarFontOfSize:12.0], NSFontAttributeName, nil] retain];

    return [super init];
}

46 47 48 49 50 51 52 53 54 55 56 57 58 59
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver: self];

    [o_red_img release];
    [o_red_over_img release];
    [o_red_on_img release];
    [o_yellow_img release];
    [o_yellow_over_img release];
    [o_yellow_on_img release];
    [o_green_img release];
    [o_green_over_img release];
    [o_green_on_img release];

60 61 62
    [o_window_title_shadow release];
    [o_window_title_attributes_dict release];

63 64
    [super dealloc];
}
65 66 67 68

- (void)awakeFromNib
{
    [self setAutoresizesSubviews: YES];
69
    [self setImagesLeft:[NSImage imageNamed:@"topbar-dark-left"] middle: [NSImage imageNamed:@"topbar-dark-center-fill"] right:[NSImage imageNamed:@"topbar-dark-right"]];
70

71 72 73 74 75
    [self loadButtonIcons];
    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(controlTintChanged:) name: NSControlTintDidChangeNotification object: nil];

    [o_red_btn setImage: o_red_img];
    [o_red_btn setAlternateImage: o_red_on_img];
76
    [[o_red_btn cell] setShowsBorderOnlyWhileMouseInside: YES];
77
    [[o_red_btn cell] setTag: 0];
78 79
    [o_yellow_btn setImage: o_yellow_img];
    [o_yellow_btn setAlternateImage: o_yellow_on_img];
80
    [[o_yellow_btn cell] setShowsBorderOnlyWhileMouseInside: YES];
81
    [[o_yellow_btn cell] setTag: 1];
82 83
    [o_green_btn setImage: o_green_img];
    [o_green_btn setAlternateImage: o_green_on_img];
84
    [[o_green_btn cell] setShowsBorderOnlyWhileMouseInside: YES];
85 86 87 88 89
    [[o_green_btn cell] setTag: 2];
    [o_fullscreen_btn setImage: [NSImage imageNamed:@"window-fullscreen"]];
    [o_fullscreen_btn setAlternateImage: [NSImage imageNamed:@"window-fullscreen-on"]];
    [[o_fullscreen_btn cell] setShowsBorderOnlyWhileMouseInside: YES];
    [[o_fullscreen_btn cell] setTag: 3];
90 91
}

92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
- (void)controlTintChanged:(NSNotification *)notification
{
    [o_red_img release];
    [o_red_over_img release];
    [o_red_on_img release];
    [o_yellow_img release];
    [o_yellow_over_img release];
    [o_yellow_on_img release];
    [o_green_img release];
    [o_green_over_img release];
    [o_green_on_img release];

    [self loadButtonIcons];

    [o_red_btn setNeedsDisplay];
    [o_yellow_btn setNeedsDisplay];
    [o_green_btn setNeedsDisplay];
}

- (void)loadButtonIcons
{
    if (OSX_LION)
    {
        if( [NSColor currentControlTint] == NSBlueControlTint )
        {
            o_red_img = [[NSImage imageNamed:@"lion-window-close"] retain];
            o_red_over_img = [[NSImage imageNamed:@"lion-window-close-over"] retain];
            o_red_on_img = [[NSImage imageNamed:@"lion-window-close-on"] retain];
            o_yellow_img = [[NSImage imageNamed:@"lion-window-minimize"] retain];
            o_yellow_over_img = [[NSImage imageNamed:@"lion-window-minimize-over"] retain];
            o_yellow_on_img = [[NSImage imageNamed:@"lion-window-minimize-on"] retain];
            o_green_img = [[NSImage imageNamed:@"lion-window-zoom"] retain];
            o_green_over_img = [[NSImage imageNamed:@"lion-window-zoom-over"] retain];
            o_green_on_img = [[NSImage imageNamed:@"lion-window-zoom-on"] retain];
        } else {
            o_red_img = [[NSImage imageNamed:@"lion-window-close-graphite"] retain];
            o_red_over_img = [[NSImage imageNamed:@"lion-window-close-over-graphite"] retain];
            o_red_on_img = [[NSImage imageNamed:@"lion-window-close-on-graphite"] retain];
            o_yellow_img = [[NSImage imageNamed:@"lion-window-minimize-graphite"] retain];
            o_yellow_over_img = [[NSImage imageNamed:@"lion-window-minimize-over-graphite"] retain];
            o_yellow_on_img = [[NSImage imageNamed:@"lion-window-minimize-on-graphite"] retain];
            o_green_img = [[NSImage imageNamed:@"lion-window-zoom-graphite"] retain];
            o_green_over_img = [[NSImage imageNamed:@"lion-window-zoom-over-graphite"] retain];
135
            o_green_on_img = [[NSImage imageNamed:@"lion-window-zoom-on-graphite"] retain];
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
        }
    } else {
        if( [NSColor currentControlTint] == NSBlueControlTint )
        {
            o_red_img = [[NSImage imageNamed:@"snowleo-window-close"] retain];
            o_red_over_img = [[NSImage imageNamed:@"snowleo-window-close-over"] retain];
            o_red_on_img = [[NSImage imageNamed:@"snowleo-window-close-on"] retain];
            o_yellow_img = [[NSImage imageNamed:@"snowleo-window-minimize"] retain];
            o_yellow_over_img = [[NSImage imageNamed:@"snowleo-window-minimize-over"] retain];
            o_yellow_on_img = [[NSImage imageNamed:@"snowleo-window-minimize-on"] retain];
            o_green_img = [[NSImage imageNamed:@"snowleo-window-zoom"] retain];
            o_green_over_img = [[NSImage imageNamed:@"snowleo-window-zoom-over"] retain];
            o_green_on_img = [[NSImage imageNamed:@"snowleo-window-zoom-on"] retain];
        } else {
            o_red_img = [[NSImage imageNamed:@"snowleo-window-close-graphite"] retain];
            o_red_over_img = [[NSImage imageNamed:@"snowleo-window-close-over-graphite"] retain];
            o_red_on_img = [[NSImage imageNamed:@"snowleo-window-close-on-graphite"] retain];
            o_yellow_img = [[NSImage imageNamed:@"snowleo-window-minimize-graphite"] retain];
            o_yellow_over_img = [[NSImage imageNamed:@"snowleo-window-minimize-over-graphite"] retain];
            o_yellow_on_img = [[NSImage imageNamed:@"snowleo-window-minimize-on-graphite"] retain];
            o_green_img = [[NSImage imageNamed:@"snowleo-window-zoom-graphite"] retain];
            o_green_over_img = [[NSImage imageNamed:@"snowleo-window-zoom-over-graphite"] retain];
158
            o_green_on_img = [[NSImage imageNamed:@"snowleo-window-zoom-on-graphite"] retain];
159 160 161 162
        }
    }
}

163 164 165 166 167 168 169 170
- (BOOL)mouseDownCanMoveWindow
{
    return YES;
}

- (IBAction)buttonAction:(id)sender
{
    if (sender == o_red_btn)
171
        [[self window] performClose: sender];
172 173 174 175 176 177 178 179
    else if (sender == o_yellow_btn)
        [[self window] miniaturize: sender];
    else if (sender == o_green_btn)
        [[self window] performZoom: sender];
    else if (sender == o_fullscreen_btn)
        [[VLCCoreInteraction sharedInstance] toggleFullscreen];
    else
        msg_Err( VLCIntf, "unknown button action sender" );
180 181 182

    [self setWindowButtonOver: NO];
    [self setWindowFullscreenButtonOver: NO];
183 184 185 186
}

- (void)setWindowTitle:(NSString *)title
{
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
    if (!o_window_title_shadow)
    {
        o_window_title_shadow = [[NSShadow alloc] init];
        [o_window_title_shadow setShadowColor:[NSColor colorWithCalibratedWhite:1.0 alpha:0.5]];
        [o_window_title_shadow setShadowOffset:NSMakeSize(0.0, -1.5)];
        [o_window_title_shadow setShadowBlurRadius:0.5];
        [o_window_title_shadow retain];
    }

    NSMutableAttributedString *o_attributed_title = [[NSMutableAttributedString alloc] initWithString:title attributes: o_window_title_attributes_dict];
    NSUInteger i_titleLength = [title length];

    [o_attributed_title addAttribute:NSShadowAttributeName value:o_window_title_shadow range:NSMakeRange(0, i_titleLength)];
    [o_attributed_title setAlignment: NSCenterTextAlignment range:NSMakeRange(0, i_titleLength)];
    [o_title_lbl setAttributedStringValue:o_attributed_title];
    [o_attributed_title release];
203 204
}

205 206 207 208 209
- (void)setFullscreenButtonHidden:(BOOL)b_value
{
    [o_fullscreen_btn setHidden: b_value];
}

210 211 212 213
- (void)setWindowButtonOver:(BOOL)b_value
{
    if( b_value )
    {
214 215 216
        [o_red_btn setImage: o_red_over_img];
        [o_yellow_btn setImage: o_yellow_over_img];
        [o_green_btn setImage: o_green_over_img];
217 218 219
    }
    else
    {
220 221 222
        [o_red_btn setImage: o_red_img];
        [o_yellow_btn setImage: o_yellow_img];
        [o_green_btn setImage: o_green_img];
223 224 225
    }
}

226 227 228 229 230 231 232
- (void)setWindowFullscreenButtonOver:(BOOL)b_value
{
    if (b_value)
        [o_fullscreen_btn setImage: [NSImage imageNamed:@"window-fullscreen-over"]];
    else
        [o_fullscreen_btn setImage: [NSImage imageNamed:@"window-fullscreen"]];
}
233 234
@end

235 236 237 238 239 240 241 242
/*****************************************************************************
 * VLCWindowButtonCell
 *
 * since the title bar cannot fetch these mouse events (the more top-level
 * NSButton is unable fetch them as well), we are using a subclass of the
 * button cell to do so. It's set in the nib for the respective objects.
 *****************************************************************************/

243 244 245 246
@implementation VLCWindowButtonCell

- (void)mouseEntered:(NSEvent *)theEvent
{
247 248 249 250
    if ([self tag] == 3)
        [(VLCMainWindowTitleView *)[[self controlView] superview] setWindowFullscreenButtonOver: YES];
    else
        [(VLCMainWindowTitleView *)[[self controlView] superview] setWindowButtonOver: YES];
251 252 253 254
}

- (void)mouseExited:(NSEvent *)theEvent
{
255 256 257 258
    if ([self tag] == 3)
        [(VLCMainWindowTitleView *)[[self controlView] superview] setWindowFullscreenButtonOver: NO];
    else
        [(VLCMainWindowTitleView *)[[self controlView] superview] setWindowButtonOver: NO];
259 260
}

261 262 263 264
/* accessibility stuff */
- (NSArray*)accessibilityAttributeNames {
    NSArray *theAttributeNames = [super accessibilityAttributeNames];
    id theControlView = [self controlView];
265
    return ([theControlView respondsToSelector: @selector(extendedAccessibilityAttributeNames:)] ? [theControlView extendedAccessibilityAttributeNames: theAttributeNames] : theAttributeNames); // ask the cell's control view (i.e., the button) for additional attribute values
266 267 268 269 270 271 272
}

- (id)accessibilityAttributeValue: (NSString*)theAttributeName {
    id theControlView = [self controlView];
    if ([theControlView respondsToSelector: @selector(extendedAccessibilityAttributeValue:)]) {
        id theValue = [theControlView extendedAccessibilityAttributeValue: theAttributeName];
        if (theValue) {
273
            return theValue; // if this is an extended attribute value we added, return that -- otherwise, fall back to super's implementation
274 275 276 277 278 279 280 281 282 283
        }
    }
    return [super accessibilityAttributeValue: theAttributeName];
}

- (BOOL)accessibilityIsAttributeSettable: (NSString*)theAttributeName {
    id theControlView = [self controlView];
    if ([theControlView respondsToSelector: @selector(extendedAccessibilityIsAttributeSettable:)]) {
        NSNumber *theValue = [theControlView extendedAccessibilityIsAttributeSettable: theAttributeName];
        if (theValue) {
284
            return [theValue boolValue]; // same basic strategy we use in -accessibilityAttributeValue:
285 286 287 288 289
        }
    }
    return [super accessibilityIsAttributeSettable: theAttributeName];
}

290
@end
291

292 293 294 295 296 297 298 299 300

/*****************************************************************************
 * VLCResizeControl
 *
 * For Leopard and Snow Leopard, we need to emulate the resize control on the
 * bottom right of the window, since it is gone by using the borderless window
 * mask. A proper fix would be Lion-only.
 *****************************************************************************/

301 302
@implementation VLCResizeControl

303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
- (void)mouseDown:(NSEvent *)theEvent {
    BOOL keepOn = YES;

    while (keepOn) {
        theEvent = [[self window] nextEventMatchingMask: NSLeftMouseUpMask |
                    NSLeftMouseDraggedMask];

        switch ([theEvent type]) {
            case NSLeftMouseDragged:
            {
                NSRect windowFrame = [[self window] frame];
                CGFloat deltaX, deltaY, oldOriginY;
                deltaX = [theEvent deltaX];
                deltaY = [theEvent deltaY];
                oldOriginY = windowFrame.origin.y;

                windowFrame.origin.y = (oldOriginY + windowFrame.size.height) - (windowFrame.size.height + deltaY);
                windowFrame.size.width += deltaX;
                windowFrame.size.height += deltaY;

                NSSize winMinSize = [self window].minSize;
                if (windowFrame.size.width < winMinSize.width)
                    windowFrame.size.width = winMinSize.width;

                if (windowFrame.size.height < winMinSize.height)
                {
                    windowFrame.size.height = winMinSize.height;
                    windowFrame.origin.y = oldOriginY;
                }

                [[self window] setFrame: windowFrame display: YES animate: NO];
                break;
            }
                break;
            case NSLeftMouseUp:
                keepOn = NO;
                break;
            default:
                /* Ignore any other kind of event. */
                break;
        }
344

345
    };
346

347
    return;
348 349 350
}

@end
351 352 353 354

/*****************************************************************************
 * VLCColorView
 *
355
 * since we are using a clear window color when using the black window
356 357 358 359 360 361 362 363 364 365 366
 * style, some filling is needed behind the video and some other elements
 *****************************************************************************/

@implementation VLCColorView

- (void)drawRect:(NSRect)rect {
    [[NSColor blackColor] setFill];
    NSRectFill(rect);
}

@end
367 368 369 370 371 372 373 374 375 376 377

/*****************************************************************************
 * custom window buttons to support the accessibility stuff
 *****************************************************************************/

@implementation VLCCustomWindowButtonPrototype
+ (Class)cellClass {
    return [VLCWindowButtonCell class];
}

- (NSArray*)extendedAccessibilityAttributeNames: (NSArray*)theAttributeNames {
378
    return ([theAttributeNames containsObject: NSAccessibilitySubroleAttribute] ? theAttributeNames : [theAttributeNames arrayByAddingObject: NSAccessibilitySubroleAttribute]); // run-of-the-mill button cells don't usually have a Subrole attribute, so we add that attribute
379 380 381 382 383 384 385
}

- (id)extendedAccessibilityAttributeValue: (NSString*)theAttributeName {
    return nil;
}

- (NSNumber*)extendedAccessibilityIsAttributeSettable: (NSString*)theAttributeName {
386
    return ([theAttributeName isEqualToString: NSAccessibilitySubroleAttribute] ? [NSNumber numberWithBool: NO] : nil); // make the Subrole attribute we added non-settable
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 418 419 420 421 422 423
}

- (void)accessibilityPerformAction: (NSString*)theActionName {
    if ([theActionName isEqualToString: NSAccessibilityPressAction]) {
        if ([self isEnabled]) {
            [self performClick: nil];
        }
    } else {
        [super accessibilityPerformAction: theActionName];
    }
}

@end

@implementation VLCCustomWindowCloseButton
- (id)extendedAccessibilityAttributeValue: (NSString*)theAttributeName {
    return ([theAttributeName isEqualToString: NSAccessibilitySubroleAttribute] ? NSAccessibilityCloseButtonAttribute : nil);
}

@end


@implementation VLCCustomWindowMinimizeButton
- (id)extendedAccessibilityAttributeValue: (NSString*)theAttributeName {
    return ([theAttributeName isEqualToString: NSAccessibilitySubroleAttribute] ? NSAccessibilityMinimizeButtonAttribute : nil);
}

@end


@implementation VLCCustomWindowZoomButton
- (id)extendedAccessibilityAttributeValue: (NSString*)theAttributeName {
    return ([theAttributeName isEqualToString: NSAccessibilitySubroleAttribute] ? NSAccessibilityZoomButtonAttribute : nil);
}

@end

424

425
@implementation VLCCustomWindowFullscreenButton
426
#ifdef MAC_OS_X_VERSION_10_7
427 428 429
- (id)extendedAccessibilityAttributeValue: (NSString*)theAttributeName {
    return ([theAttributeName isEqualToString: NSAccessibilitySubroleAttribute] ? NSAccessibilityFullScreenButtonAttribute : nil);
}
430
#endif
431 432

@end