1 /*****************************************************************************
2 * MainWindowTitle.m: MacOS X interface module
3 *****************************************************************************
4 * Copyright (C) 2011-2012 Felix Paul Kühne
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 #import <vlc_common.h>
26 #import "MainWindowTitle.h"
27 #import "CoreInteraction.h"
28 #import "CompatibilityFixes.h"
29 #import <SystemConfiguration/SystemConfiguration.h> // for the revealInFinder clone
31 /*****************************************************************************
32 * VLCMainWindowTitleView
34 * this is our title bar, which can do anything a title should do
35 * it relies on the VLCWindowButtonCell to display the correct traffic light
36 * states, since we can't capture the mouse-moved events here correctly
37 *****************************************************************************/
39 @implementation VLCMainWindowTitleView
42 o_window_title_attributes_dict = [[NSDictionary dictionaryWithObjectsAndKeys: [NSColor whiteColor], NSForegroundColorAttributeName, [NSFont titleBarFontOfSize:12.0], NSFontAttributeName, nil] retain];
49 [[NSNotificationCenter defaultCenter] removeObserver: self];
52 [o_red_over_img release];
53 [o_red_on_img release];
54 [o_yellow_img release];
55 [o_yellow_over_img release];
56 [o_yellow_on_img release];
57 [o_green_img release];
58 [o_green_over_img release];
59 [o_green_on_img release];
61 [o_window_title_shadow release];
62 [o_window_title_attributes_dict release];
69 [self setAutoresizesSubviews: YES];
70 [self setImagesLeft:[NSImage imageNamed:@"topbar-dark-left"] middle: [NSImage imageNamed:@"topbar-dark-center-fill"] right:[NSImage imageNamed:@"topbar-dark-right"]];
72 [self loadButtonIcons];
73 [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(controlTintChanged:) name: NSControlTintDidChangeNotification object: nil];
75 [o_red_btn setImage: o_red_img];
76 [o_red_btn setAlternateImage: o_red_on_img];
77 [[o_red_btn cell] setShowsBorderOnlyWhileMouseInside: YES];
78 [[o_red_btn cell] setTag: 0];
79 [o_yellow_btn setImage: o_yellow_img];
80 [o_yellow_btn setAlternateImage: o_yellow_on_img];
81 [[o_yellow_btn cell] setShowsBorderOnlyWhileMouseInside: YES];
82 [[o_yellow_btn cell] setTag: 1];
83 [o_green_btn setImage: o_green_img];
84 [o_green_btn setAlternateImage: o_green_on_img];
85 [[o_green_btn cell] setShowsBorderOnlyWhileMouseInside: YES];
86 [[o_green_btn cell] setTag: 2];
87 [o_fullscreen_btn setImage: [NSImage imageNamed:@"window-fullscreen"]];
88 [o_fullscreen_btn setAlternateImage: [NSImage imageNamed:@"window-fullscreen-on"]];
89 [[o_fullscreen_btn cell] setShowsBorderOnlyWhileMouseInside: YES];
90 [[o_fullscreen_btn cell] setTag: 3];
93 - (void)controlTintChanged:(NSNotification *)notification
96 [o_red_over_img release];
97 [o_red_on_img release];
98 [o_yellow_img release];
99 [o_yellow_over_img release];
100 [o_yellow_on_img release];
101 [o_green_img release];
102 [o_green_over_img release];
103 [o_green_on_img release];
105 [self loadButtonIcons];
107 [o_red_btn setNeedsDisplay];
108 [o_yellow_btn setNeedsDisplay];
109 [o_green_btn setNeedsDisplay];
112 - (void)loadButtonIcons
116 if( [NSColor currentControlTint] == NSBlueControlTint )
118 o_red_img = [[NSImage imageNamed:@"lion-window-close"] retain];
119 o_red_over_img = [[NSImage imageNamed:@"lion-window-close-over"] retain];
120 o_red_on_img = [[NSImage imageNamed:@"lion-window-close-on"] retain];
121 o_yellow_img = [[NSImage imageNamed:@"lion-window-minimize"] retain];
122 o_yellow_over_img = [[NSImage imageNamed:@"lion-window-minimize-over"] retain];
123 o_yellow_on_img = [[NSImage imageNamed:@"lion-window-minimize-on"] retain];
124 o_green_img = [[NSImage imageNamed:@"lion-window-zoom"] retain];
125 o_green_over_img = [[NSImage imageNamed:@"lion-window-zoom-over"] retain];
126 o_green_on_img = [[NSImage imageNamed:@"lion-window-zoom-on"] retain];
128 o_red_img = [[NSImage imageNamed:@"lion-window-close-graphite"] retain];
129 o_red_over_img = [[NSImage imageNamed:@"lion-window-close-over-graphite"] retain];
130 o_red_on_img = [[NSImage imageNamed:@"lion-window-close-on-graphite"] retain];
131 o_yellow_img = [[NSImage imageNamed:@"lion-window-minimize-graphite"] retain];
132 o_yellow_over_img = [[NSImage imageNamed:@"lion-window-minimize-over-graphite"] retain];
133 o_yellow_on_img = [[NSImage imageNamed:@"lion-window-minimize-on-graphite"] retain];
134 o_green_img = [[NSImage imageNamed:@"lion-window-zoom-graphite"] retain];
135 o_green_over_img = [[NSImage imageNamed:@"lion-window-zoom-over-graphite"] retain];
136 o_green_on_img = [[NSImage imageNamed:@"lion-window-zoom-on-graphite"] retain];
139 if( [NSColor currentControlTint] == NSBlueControlTint )
141 o_red_img = [[NSImage imageNamed:@"snowleo-window-close"] retain];
142 o_red_over_img = [[NSImage imageNamed:@"snowleo-window-close-over"] retain];
143 o_red_on_img = [[NSImage imageNamed:@"snowleo-window-close-on"] retain];
144 o_yellow_img = [[NSImage imageNamed:@"snowleo-window-minimize"] retain];
145 o_yellow_over_img = [[NSImage imageNamed:@"snowleo-window-minimize-over"] retain];
146 o_yellow_on_img = [[NSImage imageNamed:@"snowleo-window-minimize-on"] retain];
147 o_green_img = [[NSImage imageNamed:@"snowleo-window-zoom"] retain];
148 o_green_over_img = [[NSImage imageNamed:@"snowleo-window-zoom-over"] retain];
149 o_green_on_img = [[NSImage imageNamed:@"snowleo-window-zoom-on"] retain];
151 o_red_img = [[NSImage imageNamed:@"snowleo-window-close-graphite"] retain];
152 o_red_over_img = [[NSImage imageNamed:@"snowleo-window-close-over-graphite"] retain];
153 o_red_on_img = [[NSImage imageNamed:@"snowleo-window-close-on-graphite"] retain];
154 o_yellow_img = [[NSImage imageNamed:@"snowleo-window-minimize-graphite"] retain];
155 o_yellow_over_img = [[NSImage imageNamed:@"snowleo-window-minimize-over-graphite"] retain];
156 o_yellow_on_img = [[NSImage imageNamed:@"snowleo-window-minimize-on-graphite"] retain];
157 o_green_img = [[NSImage imageNamed:@"snowleo-window-zoom-graphite"] retain];
158 o_green_over_img = [[NSImage imageNamed:@"snowleo-window-zoom-over-graphite"] retain];
159 o_green_on_img = [[NSImage imageNamed:@"snowleo-window-zoom-on-graphite"] retain];
164 - (BOOL)mouseDownCanMoveWindow
169 - (IBAction)buttonAction:(id)sender
171 if (sender == o_red_btn)
172 [[self window] performClose: sender];
173 else if (sender == o_yellow_btn)
174 [[self window] miniaturize: sender];
175 else if (sender == o_green_btn)
176 [[self window] performZoom: sender];
177 else if (sender == o_fullscreen_btn)
179 // set fs directly to true, as the vars can be already true in some configs
180 var_SetBool( pl_Get( VLCIntf ), "fullscreen", true );
182 vout_thread_t *p_vout = getVout();
185 var_SetBool( p_vout, "fullscreen", true );
186 vlc_object_release( p_vout );
190 msg_Err( VLCIntf, "unknown button action sender" );
192 [self setWindowButtonOver: NO];
193 [self setWindowFullscreenButtonOver: NO];
196 - (void)setWindowTitle:(NSString *)title
198 if (!o_window_title_shadow)
200 o_window_title_shadow = [[NSShadow alloc] init];
201 [o_window_title_shadow setShadowColor:[NSColor colorWithCalibratedWhite:1.0 alpha:0.5]];
202 [o_window_title_shadow setShadowOffset:NSMakeSize(0.0, -1.5)];
203 [o_window_title_shadow setShadowBlurRadius:0.5];
204 [o_window_title_shadow retain];
207 NSMutableAttributedString *o_attributed_title = [[NSMutableAttributedString alloc] initWithString:title attributes: o_window_title_attributes_dict];
208 NSUInteger i_titleLength = [title length];
210 [o_attributed_title addAttribute:NSShadowAttributeName value:o_window_title_shadow range:NSMakeRange(0, i_titleLength)];
211 [o_attributed_title setAlignment: NSCenterTextAlignment range:NSMakeRange(0, i_titleLength)];
212 [o_title_lbl setAttributedStringValue:o_attributed_title];
213 [o_attributed_title release];
216 - (void)setFullscreenButtonHidden:(BOOL)b_value
218 [o_fullscreen_btn setHidden: b_value];
221 - (void)setWindowButtonOver:(BOOL)b_value
225 [o_red_btn setImage: o_red_over_img];
226 [o_yellow_btn setImage: o_yellow_over_img];
227 [o_green_btn setImage: o_green_over_img];
231 [o_red_btn setImage: o_red_img];
232 [o_yellow_btn setImage: o_yellow_img];
233 [o_green_btn setImage: o_green_img];
237 - (void)setWindowFullscreenButtonOver:(BOOL)b_value
240 [o_fullscreen_btn setImage: [NSImage imageNamed:@"window-fullscreen-over"]];
242 [o_fullscreen_btn setImage: [NSImage imageNamed:@"window-fullscreen"]];
245 - (void)mouseUp:(NSEvent *)event
247 if ([event clickCount] == 2) {
248 //Get settings from "System Preferences" > "Appearance" > "Double-click on windows title bar to minimize"
249 NSString *const MDAppleMiniaturizeOnDoubleClickKey = @"AppleMiniaturizeOnDoubleClick";
250 NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
251 [userDefaults addSuiteNamed:NSGlobalDomain];
253 if ([[userDefaults objectForKey:MDAppleMiniaturizeOnDoubleClickKey] boolValue])
254 [[self window] miniaturize:self];
259 /*****************************************************************************
260 * VLCWindowButtonCell
262 * since the title bar cannot fetch these mouse events (the more top-level
263 * NSButton is unable fetch them as well), we are using a subclass of the
264 * button cell to do so. It's set in the nib for the respective objects.
265 *****************************************************************************/
267 @implementation VLCWindowButtonCell
269 - (void)mouseEntered:(NSEvent *)theEvent
272 [(VLCMainWindowTitleView *)[[self controlView] superview] setWindowFullscreenButtonOver: YES];
274 [(VLCMainWindowTitleView *)[[self controlView] superview] setWindowButtonOver: YES];
277 - (void)mouseExited:(NSEvent *)theEvent
280 [(VLCMainWindowTitleView *)[[self controlView] superview] setWindowFullscreenButtonOver: NO];
282 [(VLCMainWindowTitleView *)[[self controlView] superview] setWindowButtonOver: NO];
285 /* accessibility stuff */
286 - (NSArray*)accessibilityAttributeNames {
287 NSArray *theAttributeNames = [super accessibilityAttributeNames];
288 id theControlView = [self controlView];
289 return ([theControlView respondsToSelector: @selector(extendedAccessibilityAttributeNames:)] ? [theControlView extendedAccessibilityAttributeNames: theAttributeNames] : theAttributeNames); // ask the cell's control view (i.e., the button) for additional attribute values
292 - (id)accessibilityAttributeValue: (NSString*)theAttributeName {
293 id theControlView = [self controlView];
294 if ([theControlView respondsToSelector: @selector(extendedAccessibilityAttributeValue:)]) {
295 id theValue = [theControlView extendedAccessibilityAttributeValue: theAttributeName];
297 return theValue; // if this is an extended attribute value we added, return that -- otherwise, fall back to super's implementation
300 return [super accessibilityAttributeValue: theAttributeName];
303 - (BOOL)accessibilityIsAttributeSettable: (NSString*)theAttributeName {
304 id theControlView = [self controlView];
305 if ([theControlView respondsToSelector: @selector(extendedAccessibilityIsAttributeSettable:)]) {
306 NSNumber *theValue = [theControlView extendedAccessibilityIsAttributeSettable: theAttributeName];
308 return [theValue boolValue]; // same basic strategy we use in -accessibilityAttributeValue:
311 return [super accessibilityIsAttributeSettable: theAttributeName];
317 /*****************************************************************************
320 * For Leopard and Snow Leopard, we need to emulate the resize control on the
321 * bottom right of the window, since it is gone by using the borderless window
322 * mask. A proper fix would be Lion-only.
323 *****************************************************************************/
325 @implementation VLCResizeControl
327 - (void)mouseDown:(NSEvent *)theEvent {
331 theEvent = [[self window] nextEventMatchingMask: NSLeftMouseUpMask |
332 NSLeftMouseDraggedMask];
334 switch ([theEvent type]) {
335 case NSLeftMouseDragged:
337 NSRect windowFrame = [[self window] frame];
338 CGFloat deltaX, deltaY, oldOriginY;
339 deltaX = [theEvent deltaX];
340 deltaY = [theEvent deltaY];
341 oldOriginY = windowFrame.origin.y;
343 windowFrame.origin.y = (oldOriginY + windowFrame.size.height) - (windowFrame.size.height + deltaY);
344 windowFrame.size.width += deltaX;
345 windowFrame.size.height += deltaY;
347 NSSize winMinSize = [self window].minSize;
348 if (windowFrame.size.width < winMinSize.width)
349 windowFrame.size.width = winMinSize.width;
351 if (windowFrame.size.height < winMinSize.height)
353 windowFrame.size.height = winMinSize.height;
354 windowFrame.origin.y = oldOriginY;
357 [[self window] setFrame: windowFrame display: YES animate: NO];
365 /* Ignore any other kind of event. */
376 /*****************************************************************************
379 * since we are using a clear window color when using the black window
380 * style, some filling is needed behind the video and some other elements
381 *****************************************************************************/
383 @implementation VLCColorView
385 - (void)drawRect:(NSRect)rect {
386 [[NSColor blackColor] setFill];
392 /*****************************************************************************
393 * custom window buttons to support the accessibility stuff
394 *****************************************************************************/
396 @implementation VLCCustomWindowButtonPrototype
398 return [VLCWindowButtonCell class];
401 - (NSArray*)extendedAccessibilityAttributeNames: (NSArray*)theAttributeNames {
402 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
405 - (id)extendedAccessibilityAttributeValue: (NSString*)theAttributeName {
409 - (NSNumber*)extendedAccessibilityIsAttributeSettable: (NSString*)theAttributeName {
410 return ([theAttributeName isEqualToString: NSAccessibilitySubroleAttribute] ? [NSNumber numberWithBool: NO] : nil); // make the Subrole attribute we added non-settable
413 - (void)accessibilityPerformAction: (NSString*)theActionName {
414 if ([theActionName isEqualToString: NSAccessibilityPressAction]) {
415 if ([self isEnabled]) {
416 [self performClick: nil];
419 [super accessibilityPerformAction: theActionName];
425 @implementation VLCCustomWindowCloseButton
426 - (id)extendedAccessibilityAttributeValue: (NSString*)theAttributeName {
427 return ([theAttributeName isEqualToString: NSAccessibilitySubroleAttribute] ? NSAccessibilityCloseButtonAttribute : nil);
433 @implementation VLCCustomWindowMinimizeButton
434 - (id)extendedAccessibilityAttributeValue: (NSString*)theAttributeName {
435 return ([theAttributeName isEqualToString: NSAccessibilitySubroleAttribute] ? NSAccessibilityMinimizeButtonAttribute : nil);
441 @implementation VLCCustomWindowZoomButton
442 - (id)extendedAccessibilityAttributeValue: (NSString*)theAttributeName {
443 return ([theAttributeName isEqualToString: NSAccessibilitySubroleAttribute] ? NSAccessibilityZoomButtonAttribute : nil);
449 @implementation VLCCustomWindowFullscreenButton
450 #ifdef MAC_OS_X_VERSION_10_7
451 - (id)extendedAccessibilityAttributeValue: (NSString*)theAttributeName {
452 return ([theAttributeName isEqualToString: NSAccessibilitySubroleAttribute] ? NSAccessibilityFullScreenButtonAttribute : nil);
459 @implementation VLCWindowTitleTextField
464 [contextMenu release];
469 - (void)showRightClickMenuWithEvent:(NSEvent *)o_event
472 [contextMenu release];
474 NSURL * representedURL = [[self window] representedURL];
475 if (! representedURL)
478 NSArray * pathComponents;
480 if (OSX_SNOW_LEOPARD || OSX_LION)
481 pathComponents = [representedURL pathComponents];
483 pathComponents = [[representedURL path] pathComponents];
488 contextMenu = [[NSMenu alloc] initWithTitle: [[NSFileManager defaultManager] displayNameAtPath: [representedURL path]]];
490 NSUInteger count = [pathComponents count];
492 NSMenuItem * currentItem;
493 NSMutableString * currentPath;
494 NSSize iconSize = NSMakeSize( 16., 16. );
495 for (NSUInteger i = count - 1; i > 0; i--) {
496 currentPath = [NSMutableString stringWithCapacity:1024];
497 for (NSUInteger y = 0; y < i; y++)
498 [currentPath appendFormat: @"/%@", [pathComponents objectAtIndex:y + 1]];
500 [contextMenu addItemWithTitle: [[NSFileManager defaultManager] displayNameAtPath: currentPath] action:@selector(revealInFinder:) keyEquivalent:@""];
501 currentItem = [contextMenu itemAtIndex:[contextMenu numberOfItems] - 1];
502 [currentItem setTarget: self];
504 icon = [[NSWorkspace sharedWorkspace] iconForFile:currentPath];
505 [icon setSize: iconSize];
506 [currentItem setImage: icon];
509 if ([[pathComponents objectAtIndex: 1] isEqualToString:@"Volumes"]) {
510 /* we don't want to show the Volumes item, since the Cocoa does it neither */
511 currentItem = [contextMenu itemWithTitle:[[NSFileManager defaultManager] displayNameAtPath: @"/Volumes"]];
513 [contextMenu removeItem: currentItem];
515 /* we're on the boot drive, so add it since it isn't part of the components */
516 [contextMenu addItemWithTitle: [[NSFileManager defaultManager] displayNameAtPath:@"/"] action:@selector(revealInFinder:) keyEquivalent:@""];
517 currentItem = [contextMenu itemAtIndex: [contextMenu numberOfItems] - 1];
518 icon = [[NSWorkspace sharedWorkspace] iconForFile:@"/"];
519 [icon setSize: iconSize];
520 [currentItem setImage: icon];
521 [currentItem setTarget: self];
524 /* add the computer item */
525 [contextMenu addItemWithTitle: [(NSString*)SCDynamicStoreCopyComputerName(NULL, NULL) autorelease] action:@selector(revealInFinder:) keyEquivalent:@""];
526 currentItem = [contextMenu itemAtIndex: [contextMenu numberOfItems] - 1];
527 icon = [NSImage imageNamed: NSImageNameComputer];
528 [icon setSize: iconSize];
529 [currentItem setImage: icon];
530 [currentItem setTarget: self];
532 [NSMenu popUpContextMenu: contextMenu withEvent: o_event forView: [self superview]];
535 - (IBAction)revealInFinder:(id)sender
537 NSUInteger count = [contextMenu numberOfItems];
538 NSUInteger selectedItem = [contextMenu indexOfItem: sender];
540 if (selectedItem == count - 1) // the fake computer item
542 [[NSWorkspace sharedWorkspace] selectFile: @"/" inFileViewerRootedAtPath: @""];
546 NSURL * representedURL = [[self window] representedURL];
547 if (! representedURL)
550 if (selectedItem == 0) // the actual file, let's save time
552 [[NSWorkspace sharedWorkspace] selectFile: [representedURL path] inFileViewerRootedAtPath: [representedURL path]];
556 NSArray * pathComponents;
557 if (OSX_SNOW_LEOPARD || OSX_LION)
558 pathComponents = [representedURL pathComponents];
560 pathComponents = [[representedURL path] pathComponents];
564 NSMutableString * currentPath;
565 currentPath = [NSMutableString stringWithCapacity:1024];
566 selectedItem = count - selectedItem;
568 /* fix for non-startup volumes */
569 if ([[pathComponents objectAtIndex:1] isEqualToString:@"Volumes"])
572 for (NSUInteger y = 1; y < selectedItem; y++)
573 [currentPath appendFormat: @"/%@", [pathComponents objectAtIndex:y]];
575 [[NSWorkspace sharedWorkspace] selectFile: currentPath inFileViewerRootedAtPath: currentPath];
578 - (void)rightMouseDown:(NSEvent *)o_event
580 if( [o_event type] == NSRightMouseDown )
581 [self showRightClickMenuWithEvent:o_event];
583 [super mouseDown: o_event];