demux: mkv: handle WAVE_FORMAT_MPEG_ADTS_AAC
[vlc.git] / modules / gui / macosx / VLCMainWindowTitleView.m
bloba30d81c6eca038f2f65d9fddde56508396e4cca4
1 /*****************************************************************************
2  * MainWindowTitle.m: MacOS X interface module
3  *****************************************************************************
4  * Copyright (C) 2011-2012 Felix Paul Kühne
5  * $Id$
6  *
7  * Authors: Felix Paul Kühne <fkuehne -at- videolan -dot- org>
8  *
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.
13  *
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.
18  *
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>
25 #import "VLCMain.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
33  *
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()
41     NSImage *_redImage;
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
65     BOOL b_alt_pressed;
66     BOOL b_mouse_over;
68 @end
70 @implementation VLCMainWindowTitleView
72 - (id)init
74     self = [super init];
76     if (self) {
77         _windowTitleAttributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys: [NSColor whiteColor], NSForegroundColorAttributeName, [NSFont titleBarFontOfSize:12.0], NSFontAttributeName, nil];
78     }
80     return self;
83 - (void)dealloc
85     [[NSNotificationCenter defaultCenter] removeObserver: self];
88 - (void)awakeFromNib
90     b_nativeFullscreenMode = var_InheritBool(getIntf(), "macosx-nativefullscreenmode");
92     if (!b_nativeFullscreenMode || OSX_YOSEMITE_AND_HIGHER) {
93         [_fullscreenButton setHidden: YES];
94     }
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];
120     }
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
129         o_name = @"lion-";
130     }
132     o_name = [o_name stringByAppendingString:o_id];
134     if ([NSColor currentControlTint] != NSBlueControlTint) {
135         o_name = [o_name stringByAppendingString:@"-graphite"];
136     }
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"];
158     }
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) {
191         if (b_mouse_over) {
192             [_greenButton setImage: _greenHoverImage];
193             [_greenButton setAlternateImage: _greenOnClickImage];
194         } else {
195             [_greenButton setImage: _greenImage];
196             [_greenButton setAlternateImage: _greenOnClickImage];
197         }
198     } else {
200         if (b_mouse_over) {
201             [_greenButton setImage: _fullscreenHoverImage];
202             [_greenButton setAlternateImage: _fullscreenOnClickImage];
203         } else {
204             [_greenButton setImage: _fullscreenImage];
205             [_greenButton setAlternateImage: _fullscreenOnClickImage];
206         }
207     }
210 - (BOOL)mouseDownCanMoveWindow
212     return YES;
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];
224         } else {
225             [[self window] performZoom: sender];
226         }
227     } else if (sender == _fullscreenButton) {
228         // same action as native fs button
229         [[self window] toggleFullScreen:self];
231     } else
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];
245     }
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;
258     if (b_value) {
259         [_redButton setImage: _redHoverImage];
260         [_yellowButton setImage: _yellowHoverImage];
261     } else {
262         [_redButton setImage: _redImage];
263         [_yellowButton setImage: _yellowImage];
264     }
266     [self updateGreenButton];
269 - (void)setWindowFullscreenButtonOver:(BOOL)b_value
271     if (b_value)
272         [_fullscreenButton setImage: _oldFullscreenHoverImage];
273     else
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];
288     }
290     [super mouseDown: event];
293 - (NSButton*)closeButton
295     return _redButton;
298 - (NSButton*)minimizeButton
300     return _yellowButton;
303 - (NSButton*)zoomButton
305     return _greenButton;
308 @end
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
322     if ([self tag] == 3)
323         [(VLCMainWindowTitleView *)[[self controlView] superview] setWindowFullscreenButtonOver: YES];
324     else
325         [(VLCMainWindowTitleView *)[[self controlView] superview] setWindowButtonOver: YES];
328 - (void)mouseExited:(NSEvent *)theEvent
330     if ([self tag] == 3)
331         [(VLCMainWindowTitleView *)[[self controlView] superview] setWindowFullscreenButtonOver: NO];
332     else
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];
347         if (theValue) {
348             return theValue; // if this is an extended attribute value we added, return that -- otherwise, fall back to super's implementation
349         }
350     }
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];
358         if (theValue)
359             return [theValue boolValue]; // same basic strategy we use in -accessibilityAttributeValue:
360     }
361     return [super accessibilityIsAttributeSettable: theAttributeName];
364 @end
367 /*****************************************************************************
368  * VLCColorView
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];
378     NSRectFill(rect);
381 @end
383 /*****************************************************************************
384  * custom window buttons to support the accessibility stuff
385  *****************************************************************************/
387 @implementation VLCCustomWindowButtonPrototype
388 + (Class)cellClass {
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 {
397     return nil;
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];
408     } else
409         [super accessibilityPerformAction: theActionName];
412 @end
414 @implementation VLCCustomWindowCloseButton
415 - (id)extendedAccessibilityAttributeValue: (NSString*)theAttributeName {
416     return ([theAttributeName isEqualToString: NSAccessibilitySubroleAttribute] ? NSAccessibilityCloseButtonAttribute : nil);
419 @end
422 @implementation VLCCustomWindowMinimizeButton
423 - (id)extendedAccessibilityAttributeValue: (NSString*)theAttributeName {
424     return ([theAttributeName isEqualToString: NSAccessibilitySubroleAttribute] ? NSAccessibilityMinimizeButtonAttribute : nil);
427 @end
430 @implementation VLCCustomWindowZoomButton
431 - (id)extendedAccessibilityAttributeValue: (NSString*)theAttributeName {
432     return ([theAttributeName isEqualToString: NSAccessibilitySubroleAttribute] ? NSAccessibilityZoomButtonAttribute : nil);
435 @end
438 @implementation VLCCustomWindowFullscreenButton
439 - (id)extendedAccessibilityAttributeValue: (NSString*)theAttributeName {
440     return ([theAttributeName isEqualToString: NSAccessibilitySubroleAttribute] ? NSAccessibilityFullScreenButtonAttribute : nil);
442 @end
445 @interface VLCWindowTitleTextField()
447     NSMenu *_contextMenu;
449 @end
451 @implementation VLCWindowTitleTextField
453 - (void)showRightClickMenuWithEvent:(NSEvent *)o_event
455     NSURL * representedURL = [[self window] representedURL];
456     if (!representedURL)
457         return;
459     NSArray * pathComponents;
460     pathComponents = [representedURL pathComponents];
462     if (!pathComponents)
463         return;
465     _contextMenu = [[NSMenu alloc] initWithTitle: [[NSFileManager defaultManager] displayNameAtPath: [representedURL path]]];
467     NSUInteger count = [pathComponents count];
468     NSImage * icon;
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];
484     }
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"]];
489         if (currentItem)
490             [_contextMenu removeItem: currentItem];
491     } else {
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];
499     }
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];
512     NSPoint point;
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.;
521     if (point.x < 0)
522         point.x = 10;
524     NSEvent *fakeMouseEvent = [NSEvent mouseEventWithType:NSRightMouseDown
525                                                  location:point
526                                             modifierFlags:0
527                                                 timestamp:0
528                                              windowNumber:[[self window] windowNumber]
529                                                   context:nil
530                                               eventNumber:0
531                                                clickCount:0
532                                                  pressure:0];
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: @""];
543         return;
544     }
546     NSURL * representedURL = [[self window] representedURL];
547     if (! representedURL)
548         return;
550     if (selectedItem == 0) { // the actual file, let's save time
551         [[NSWorkspace sharedWorkspace] selectFile: [representedURL path] inFileViewerRootedAtPath: [representedURL path]];
552         return;
553     }
555     NSArray * pathComponents;
556     pathComponents = [representedURL pathComponents];
557     if (!pathComponents)
558         return;
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"])
566         selectedItem += 1;
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];
582 @end