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 "VLCMainWindowTitleView.h"
27 #import "VLCCoreInteraction.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 @interface VLCMainWindowTitleView()
42 NSImage *_redHoverImage;
43 NSImage *_redOnClickImage;
44 NSImage * _yellowImage;
45 NSImage * _yellowHoverImage;
46 NSImage * _yellowOnClickImage;
47 NSImage * _greenImage;
48 NSImage * _greenHoverImage;
49 NSImage * _greenOnClickImage;
50 // yosemite fullscreen images
51 NSImage * _fullscreenImage;
52 NSImage * _fullscreenHoverImage;
53 NSImage * _fullscreenOnClickImage;
54 // old native fullscreen images
55 NSImage * _oldFullscreenImage;
56 NSImage * _oldFullscreenHoverImage;
57 NSImage * _oldFullscreenOnClickImage;
59 NSShadow * _windowTitleShadow;
60 NSDictionary * _windowTitleAttributesDictionary;
62 BOOL b_nativeFullscreenMode;
64 // state to determine correct image for green bubble
70 @implementation VLCMainWindowTitleView
77 _windowTitleAttributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys: [NSColor whiteColor], NSForegroundColorAttributeName, [NSFont titleBarFontOfSize:12.0], NSFontAttributeName, nil];
85 [[NSNotificationCenter defaultCenter] removeObserver: self];
90 b_nativeFullscreenMode = var_InheritBool(getIntf(), "macosx-nativefullscreenmode");
92 if (!b_nativeFullscreenMode || OSX_YOSEMITE_AND_HIGHER) {
93 [_fullscreenButton setHidden: YES];
96 [self setAutoresizesSubviews: YES];
97 [self setImagesLeft:imageFromRes(@"topbar-dark-left") middle: imageFromRes(@"topbar-dark-center-fill") right:imageFromRes(@"topbar-dark-right")];
99 [self loadButtonIcons];
100 [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(controlTintChanged:) name: NSControlTintDidChangeNotification object: nil];
103 - (void)controlTintChanged:(NSNotification *)notification
105 [self loadButtonIcons];
107 [_redButton setNeedsDisplay];
108 [_yellowButton setNeedsDisplay];
109 [_greenButton setNeedsDisplay];
112 - (void)informModifierPressed:(BOOL)b_is_altkey;
114 BOOL b_state_changed = b_alt_pressed != b_is_altkey;
116 b_alt_pressed = b_is_altkey;
118 if (b_state_changed) {
119 [self updateGreenButton];
123 - (NSImage *)getButtonImage:(NSString *)o_id
125 NSString *o_name = @"";
126 if (OSX_YOSEMITE_AND_HIGHER) {
127 o_name = @"yosemite-";
128 } else { // OSX_LION_AND_HIGHER, OSX_MOUNTAIN_LION_AND_HIGHER, OSX_MAVERICKS_AND_HIGHER
132 o_name = [o_name stringByAppendingString:o_id];
134 if ([NSColor currentControlTint] != NSBlueControlTint) {
135 o_name = [o_name stringByAppendingString:@"-graphite"];
138 return [NSImage imageNamed:o_name];
141 - (void)loadButtonIcons
143 _redImage = [self getButtonImage:@"window-close"];
144 _redHoverImage = [self getButtonImage:@"window-close-over"];
145 _redOnClickImage = [self getButtonImage:@"window-close-on"];
146 _yellowImage = [self getButtonImage:@"window-minimize"];
147 _yellowHoverImage = [self getButtonImage:@"window-minimize-over"];
148 _yellowOnClickImage = [self getButtonImage:@"window-minimize-on"];
149 _greenImage = [self getButtonImage:@"window-zoom"];
150 _greenHoverImage = [self getButtonImage:@"window-zoom-over"];
151 _greenOnClickImage = [self getButtonImage:@"window-zoom-on"];
153 // these files are only available in the yosemite variant
154 if (OSX_YOSEMITE_AND_HIGHER) {
155 _fullscreenImage = [self getButtonImage:@"window-fullscreen"];
156 _fullscreenHoverImage = [self getButtonImage:@"window-fullscreen-over"];
157 _fullscreenOnClickImage = [self getButtonImage:@"window-fullscreen-on"];
160 // old native fullscreen images are not available in graphite style
161 // thus they are loaded directly here
162 _oldFullscreenImage = [NSImage imageNamed:@"lion-window-fullscreen"];
163 _oldFullscreenOnClickImage = [NSImage imageNamed:@"lion-window-fullscreen-on"];
164 _oldFullscreenHoverImage = [NSImage imageNamed:@"lion-window-fullscreen-over"];
166 [_redButton setImage: _redImage];
167 [_redButton setAlternateImage: _redHoverImage];
168 [[_redButton cell] setShowsBorderOnlyWhileMouseInside: YES];
169 [[_redButton cell] setTag: 0];
170 [_yellowButton setImage: _yellowImage];
171 [_yellowButton setAlternateImage: _yellowHoverImage];
172 [[_yellowButton cell] setShowsBorderOnlyWhileMouseInside: YES];
173 [[_yellowButton cell] setTag: 1];
175 [self updateGreenButton];
176 [[_greenButton cell] setShowsBorderOnlyWhileMouseInside: YES];
177 [[_greenButton cell] setTag: 2];
179 [_fullscreenButton setImage: _oldFullscreenImage];
180 [_fullscreenButton setAlternateImage: _oldFullscreenHoverImage];
181 [[_fullscreenButton cell] setShowsBorderOnlyWhileMouseInside: YES];
182 [[_fullscreenButton cell] setTag: 3];
185 - (void)updateGreenButton
187 // default image for old version, or if native fullscreen is
188 // disabled on yosemite, or if alt key is pressed
189 if (!OSX_YOSEMITE_AND_HIGHER || !b_nativeFullscreenMode || b_alt_pressed) {
192 [_greenButton setImage: _greenHoverImage];
193 [_greenButton setAlternateImage: _greenOnClickImage];
195 [_greenButton setImage: _greenImage];
196 [_greenButton setAlternateImage: _greenOnClickImage];
201 [_greenButton setImage: _fullscreenHoverImage];
202 [_greenButton setAlternateImage: _fullscreenOnClickImage];
204 [_greenButton setImage: _fullscreenImage];
205 [_greenButton setAlternateImage: _fullscreenOnClickImage];
210 - (BOOL)mouseDownCanMoveWindow
215 - (IBAction)buttonAction:(id)sender
217 if (sender == _redButton)
218 [[self window] performClose: sender];
219 else if (sender == _yellowButton)
220 [[self window] miniaturize: sender];
221 else if (sender == _greenButton) {
222 if (OSX_YOSEMITE_AND_HIGHER && b_nativeFullscreenMode && !b_alt_pressed) {
223 [[self window] toggleFullScreen:self];
225 [[self window] performZoom: sender];
227 } else if (sender == _fullscreenButton) {
228 // same action as native fs button
229 [[self window] toggleFullScreen:self];
232 msg_Err(getIntf(), "unknown button action sender");
234 [self setWindowButtonOver: NO];
235 [self setWindowFullscreenButtonOver: NO];
238 - (void)setWindowTitle:(NSString *)title
240 if (!_windowTitleShadow) {
241 _windowTitleShadow = [[NSShadow alloc] init];
242 [_windowTitleShadow setShadowColor:[NSColor colorWithCalibratedWhite:1.0 alpha:0.5]];
243 [_windowTitleShadow setShadowOffset:NSMakeSize(0.0, -1.5)];
244 [_windowTitleShadow setShadowBlurRadius:0.5];
247 NSMutableAttributedString *attributedTitleString = [[NSMutableAttributedString alloc] initWithString:title attributes: _windowTitleAttributesDictionary];
248 NSUInteger i_titleLength = [title length];
250 [attributedTitleString addAttribute:NSShadowAttributeName value:_windowTitleShadow range:NSMakeRange(0, i_titleLength)];
251 [attributedTitleString setAlignment: NSCenterTextAlignment range:NSMakeRange(0, i_titleLength)];
252 [_titleLabel setAttributedStringValue:attributedTitleString];
255 - (void)setWindowButtonOver:(BOOL)b_value
257 b_mouse_over = b_value;
259 [_redButton setImage: _redHoverImage];
260 [_yellowButton setImage: _yellowHoverImage];
262 [_redButton setImage: _redImage];
263 [_yellowButton setImage: _yellowImage];
266 [self updateGreenButton];
269 - (void)setWindowFullscreenButtonOver:(BOOL)b_value
272 [_fullscreenButton setImage: _oldFullscreenHoverImage];
274 [_fullscreenButton setImage: _oldFullscreenImage];
277 - (void)mouseDown:(NSEvent *)event
279 NSPoint ml = [self convertPoint: [event locationInWindow] fromView: self];
280 if (([[self window] frame].size.height - ml.y) <= 22. && [event clickCount] == 2) {
281 //Get settings from "System Preferences" > "Appearance" > "Double-click on windows title bar to minimize"
282 NSString *const MDAppleMiniaturizeOnDoubleClickKey = @"AppleMiniaturizeOnDoubleClick";
283 NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
284 [userDefaults addSuiteNamed:NSGlobalDomain];
286 if ([[userDefaults objectForKey:MDAppleMiniaturizeOnDoubleClickKey] boolValue])
287 [[self window] miniaturize:self];
290 [super mouseDown: event];
293 - (NSButton*)closeButton
298 - (NSButton*)minimizeButton
300 return _yellowButton;
303 - (NSButton*)zoomButton
310 /*****************************************************************************
311 * VLCWindowButtonCell
313 * since the title bar cannot fetch these mouse events (the more top-level
314 * NSButton is unable fetch them as well), we are using a subclass of the
315 * button cell to do so. It's set in the nib for the respective objects.
316 *****************************************************************************/
318 @implementation VLCWindowButtonCell
320 - (void)mouseEntered:(NSEvent *)theEvent
323 [(VLCMainWindowTitleView *)[[self controlView] superview] setWindowFullscreenButtonOver: YES];
325 [(VLCMainWindowTitleView *)[[self controlView] superview] setWindowButtonOver: YES];
328 - (void)mouseExited:(NSEvent *)theEvent
331 [(VLCMainWindowTitleView *)[[self controlView] superview] setWindowFullscreenButtonOver: NO];
333 [(VLCMainWindowTitleView *)[[self controlView] superview] setWindowButtonOver: NO];
336 /* accessibility stuff */
337 - (NSArray*)accessibilityAttributeNames {
338 NSArray *theAttributeNames = [super accessibilityAttributeNames];
339 id theControlView = [self controlView];
340 return ([theControlView respondsToSelector: @selector(extendedAccessibilityAttributeNames:)] ? [theControlView extendedAccessibilityAttributeNames: theAttributeNames] : theAttributeNames); // ask the cell's control view (i.e., the button) for additional attribute values
343 - (id)accessibilityAttributeValue: (NSString*)theAttributeName {
344 id theControlView = [self controlView];
345 if ([theControlView respondsToSelector: @selector(extendedAccessibilityAttributeValue:)]) {
346 id theValue = [theControlView extendedAccessibilityAttributeValue: theAttributeName];
348 return theValue; // if this is an extended attribute value we added, return that -- otherwise, fall back to super's implementation
351 return [super accessibilityAttributeValue: theAttributeName];
354 - (BOOL)accessibilityIsAttributeSettable: (NSString*)theAttributeName {
355 id theControlView = [self controlView];
356 if ([theControlView respondsToSelector: @selector(extendedAccessibilityIsAttributeSettable:)]) {
357 NSNumber *theValue = [theControlView extendedAccessibilityIsAttributeSettable: theAttributeName];
359 return [theValue boolValue]; // same basic strategy we use in -accessibilityAttributeValue:
361 return [super accessibilityIsAttributeSettable: theAttributeName];
367 /*****************************************************************************
370 * since we are using a clear window color when using the black window
371 * style, some filling is needed behind the video and some other elements
372 *****************************************************************************/
374 @implementation VLCColorView
376 - (void)drawRect:(NSRect)rect {
377 [[NSColor blackColor] setFill];
383 /*****************************************************************************
384 * custom window buttons to support the accessibility stuff
385 *****************************************************************************/
387 @implementation VLCCustomWindowButtonPrototype
389 return [VLCWindowButtonCell class];
392 - (NSArray*)extendedAccessibilityAttributeNames: (NSArray*)theAttributeNames {
393 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
396 - (id)extendedAccessibilityAttributeValue: (NSString*)theAttributeName {
400 - (NSNumber*)extendedAccessibilityIsAttributeSettable: (NSString*)theAttributeName {
401 return ([theAttributeName isEqualToString: NSAccessibilitySubroleAttribute] ? [NSNumber numberWithBool:NO] : nil); // make the Subrole attribute we added non-settable
404 - (void)accessibilityPerformAction: (NSString*)theActionName {
405 if ([theActionName isEqualToString: NSAccessibilityPressAction]) {
406 if ([self isEnabled])
407 [self performClick: nil];
409 [super accessibilityPerformAction: theActionName];
414 @implementation VLCCustomWindowCloseButton
415 - (id)extendedAccessibilityAttributeValue: (NSString*)theAttributeName {
416 return ([theAttributeName isEqualToString: NSAccessibilitySubroleAttribute] ? NSAccessibilityCloseButtonAttribute : nil);
422 @implementation VLCCustomWindowMinimizeButton
423 - (id)extendedAccessibilityAttributeValue: (NSString*)theAttributeName {
424 return ([theAttributeName isEqualToString: NSAccessibilitySubroleAttribute] ? NSAccessibilityMinimizeButtonAttribute : nil);
430 @implementation VLCCustomWindowZoomButton
431 - (id)extendedAccessibilityAttributeValue: (NSString*)theAttributeName {
432 return ([theAttributeName isEqualToString: NSAccessibilitySubroleAttribute] ? NSAccessibilityZoomButtonAttribute : nil);
438 @implementation VLCCustomWindowFullscreenButton
439 - (id)extendedAccessibilityAttributeValue: (NSString*)theAttributeName {
440 return ([theAttributeName isEqualToString: NSAccessibilitySubroleAttribute] ? NSAccessibilityFullScreenButtonAttribute : nil);
445 @interface VLCWindowTitleTextField()
447 NSMenu *_contextMenu;
451 @implementation VLCWindowTitleTextField
453 - (void)showRightClickMenuWithEvent:(NSEvent *)o_event
455 NSURL * representedURL = [[self window] representedURL];
459 NSArray * pathComponents;
460 pathComponents = [representedURL pathComponents];
465 _contextMenu = [[NSMenu alloc] initWithTitle: [[NSFileManager defaultManager] displayNameAtPath: [representedURL path]]];
467 NSUInteger count = [pathComponents count];
469 NSMenuItem * currentItem;
470 NSMutableString * currentPath;
471 NSSize iconSize = NSMakeSize(16., 16.);
472 for (NSUInteger i = count - 1; i > 0; i--) {
473 currentPath = [NSMutableString stringWithCapacity:1024];
474 for (NSUInteger y = 0; y < i; y++)
475 [currentPath appendFormat: @"/%@", [pathComponents objectAtIndex:y + 1]];
477 [_contextMenu addItemWithTitle: [[NSFileManager defaultManager] displayNameAtPath: currentPath] action:@selector(revealInFinder:) keyEquivalent:@""];
478 currentItem = [_contextMenu itemAtIndex:[_contextMenu numberOfItems] - 1];
479 [currentItem setTarget: self];
481 icon = [[NSWorkspace sharedWorkspace] iconForFile:currentPath];
482 [icon setSize: iconSize];
483 [currentItem setImage: icon];
486 if ([[pathComponents objectAtIndex:1] isEqualToString:@"Volumes"]) {
487 /* we don't want to show the Volumes item, since the Cocoa does it neither */
488 currentItem = [_contextMenu itemWithTitle:[[NSFileManager defaultManager] displayNameAtPath: @"/Volumes"]];
490 [_contextMenu removeItem: currentItem];
492 /* we're on the boot drive, so add it since it isn't part of the components */
493 [_contextMenu addItemWithTitle: [[NSFileManager defaultManager] displayNameAtPath:@"/"] action:@selector(revealInFinder:) keyEquivalent:@""];
494 currentItem = [_contextMenu itemAtIndex: [_contextMenu numberOfItems] - 1];
495 icon = [[NSWorkspace sharedWorkspace] iconForFile:@"/"];
496 [icon setSize: iconSize];
497 [currentItem setImage: icon];
498 [currentItem setTarget: self];
501 /* add the computer item */
502 [_contextMenu addItemWithTitle:(NSString*)CFBridgingRelease(SCDynamicStoreCopyComputerName(NULL, NULL)) action:@selector(revealInFinder:) keyEquivalent:@""];
503 currentItem = [_contextMenu itemAtIndex: [_contextMenu numberOfItems] - 1];
504 icon = [NSImage imageNamed: NSImageNameComputer];
505 [icon setSize: iconSize];
506 [currentItem setImage: icon];
507 [currentItem setTarget: self];
509 // center the context menu similar to the white interface
510 CGFloat menuWidth = [_contextMenu size].width;
511 NSRect windowFrame = [[self window] frame];
514 CGFloat fullButtonWidth = 0.;
515 if([[VLCMain sharedInstance] nativeFullscreenMode])
516 fullButtonWidth = 20.;
518 // assumes 60 px for the window buttons
519 point.x = (windowFrame.size.width - 60. - fullButtonWidth) / 2. - menuWidth / 2. + 60. - 20.;
520 point.y = windowFrame.size.height + 1.;
524 NSEvent *fakeMouseEvent = [NSEvent mouseEventWithType:NSRightMouseDown
528 windowNumber:[[self window] windowNumber]
533 [NSMenu popUpContextMenu: _contextMenu withEvent: fakeMouseEvent forView: [self superview]];
536 - (IBAction)revealInFinder:(id)sender
538 NSUInteger count = [_contextMenu numberOfItems];
539 NSUInteger selectedItem = [_contextMenu indexOfItem: sender];
541 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
551 [[NSWorkspace sharedWorkspace] selectFile: [representedURL path] inFileViewerRootedAtPath: [representedURL path]];
555 NSArray * pathComponents;
556 pathComponents = [representedURL pathComponents];
560 NSMutableString * currentPath;
561 currentPath = [NSMutableString stringWithCapacity:1024];
562 selectedItem = count - selectedItem;
564 /* fix for non-startup volumes */
565 if ([[pathComponents objectAtIndex:1] isEqualToString:@"Volumes"])
568 for (NSUInteger y = 1; y < selectedItem; y++)
569 [currentPath appendFormat: @"/%@", [pathComponents objectAtIndex:y]];
571 [[NSWorkspace sharedWorkspace] selectFile: currentPath inFileViewerRootedAtPath: currentPath];
574 - (void)rightMouseDown:(NSEvent *)o_event
576 if ([o_event type] == NSRightMouseDown)
577 [self showRightClickMenuWithEvent:o_event];
579 [super mouseDown: o_event];