1 /*****************************************************************************
2 * misc.m: code not specific to vlc
3 *****************************************************************************
4 * Copyright (C) 2003-2011 the VideoLAN team
7 * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
8 * Felix Paul Kühne <fkuehne at videolan dot org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 #import <Cocoa/Cocoa.h>
26 #import <Carbon/Carbon.h>
28 #import "CompatibilityFixes.h"
29 #import "intf.h" /* VLCApplication */
30 #import "MainWindow.h"
36 /*****************************************************************************
37 * NSAnimation (VLCAdditions)
39 * Missing extension to NSAnimation
40 *****************************************************************************/
42 @implementation NSAnimation (VLCAdditions)
43 /* fake class attributes */
44 static NSMapTable *VLCAdditions_userInfo = NULL;
48 /* init our fake object attribute */
49 VLCAdditions_userInfo = NSCreateMapTable(NSNonRetainedObjectMapKeyCallBacks, NSObjectMapValueCallBacks, 16);
54 NSMapRemove(VLCAdditions_userInfo, self);
58 - (void)setUserInfo: (void *)userInfo
60 NSMapInsert(VLCAdditions_userInfo, self, (void*)userInfo);
65 return NSMapGet(VLCAdditions_userInfo, self);
69 /*****************************************************************************
70 * NSScreen (VLCAdditions)
72 * Missing extension to NSScreen
73 *****************************************************************************/
75 @implementation NSScreen (VLCAdditions)
77 static NSMutableArray *blackoutWindows = NULL;
81 /* init our fake object attribute */
82 blackoutWindows = [[NSMutableArray alloc] initWithCapacity:1];
85 + (NSScreen *)screenWithDisplayID: (CGDirectDisplayID)displayID
87 NSUInteger count = [[NSScreen screens] count];
89 for( NSUInteger i = 0; i < count; i++ )
91 NSScreen *screen = [[NSScreen screens] objectAtIndex: i];
92 if([screen displayID] == displayID)
100 return ([self displayID] == [[[NSScreen screens] objectAtIndex:0] displayID]);
103 - (BOOL)isScreen: (NSScreen*)screen
105 return ([self displayID] == [screen displayID]);
108 - (CGDirectDisplayID)displayID
110 return (CGDirectDisplayID)[[[self deviceDescription] objectForKey: @"NSScreenNumber"] intValue];
113 - (void)blackoutOtherScreens
115 /* Free our previous blackout window (follow blackoutWindow alloc strategy) */
116 [blackoutWindows makeObjectsPerformSelector:@selector(close)];
117 [blackoutWindows removeAllObjects];
119 NSUInteger screenCount = [[NSScreen screens] count];
120 for(NSUInteger i = 0; i < screenCount; i++)
122 NSScreen *screen = [[NSScreen screens] objectAtIndex: i];
123 VLCWindow *blackoutWindow;
126 if([self isScreen: screen])
129 screen_rect = [screen frame];
130 screen_rect.origin.x = screen_rect.origin.y = 0;
132 /* blackoutWindow alloc strategy
133 - The NSMutableArray blackoutWindows has the blackoutWindow references
134 - blackoutOtherDisplays is responsible for alloc/releasing its Windows
136 blackoutWindow = [[VLCWindow alloc] initWithContentRect: screen_rect styleMask: NSBorderlessWindowMask
137 backing: NSBackingStoreBuffered defer: NO screen: screen];
138 [blackoutWindow setBackgroundColor:[NSColor blackColor]];
139 [blackoutWindow setLevel: NSFloatingWindowLevel]; /* Disappear when Expose is triggered */
141 [blackoutWindow displayIfNeeded];
142 [blackoutWindow orderFront: self animate: YES];
144 [blackoutWindows addObject: blackoutWindow];
145 [blackoutWindow release];
147 if( [screen isMainScreen ] )
149 if ([screen isMainScreen])
152 SetSystemUIMode( kUIModeAllHidden, kUIOptionAutoShowMenuBar);
154 [NSApp setPresentationOptions:(NSApplicationPresentationAutoHideDock | NSApplicationPresentationAutoHideMenuBar)];
160 + (void)unblackoutScreens
162 NSUInteger blackoutWindowCount = [blackoutWindows count];
164 for(NSUInteger i = 0; i < blackoutWindowCount; i++)
166 VLCWindow *blackoutWindow = [blackoutWindows objectAtIndex: i];
167 [blackoutWindow closeAndAnimate: YES];
171 SetSystemUIMode( kUIModeNormal, kUIOptionAutoShowMenuBar);
173 [NSApp setPresentationOptions:(NSApplicationPresentationDefault)];
178 /*****************************************************************************
181 * Missing extension to NSWindow
182 *****************************************************************************/
184 @implementation VLCWindow
185 - (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)styleMask
186 backing:(NSBackingStoreType)backingType defer:(BOOL)flag
188 self = [super initWithContentRect:contentRect styleMask:styleMask backing:backingType defer:flag];
191 b_isset_canBecomeKeyWindow = NO;
192 /* we don't want this window to be restored on relaunch */
193 if ([self respondsToSelector:@selector(setRestorable:)])
194 [self setRestorable:NO];
198 - (void)setCanBecomeKeyWindow: (BOOL)canBecomeKey
200 b_isset_canBecomeKeyWindow = YES;
201 b_canBecomeKeyWindow = canBecomeKey;
204 - (BOOL)canBecomeKeyWindow
206 if(b_isset_canBecomeKeyWindow)
207 return b_canBecomeKeyWindow;
209 return [super canBecomeKeyWindow];
212 - (void)closeAndAnimate: (BOOL)animate
222 invoc = [NSInvocation invocationWithMethodSignature:[super methodSignatureForSelector:@selector(close)]];
223 [invoc setTarget: self];
225 if (![self isVisible] || [self alphaValue] == 0.0)
231 [self orderOut: self animate: YES callback: invoc];
234 - (void)orderOut: (id)sender animate: (BOOL)animate
236 NSInvocation *invoc = [NSInvocation invocationWithMethodSignature:[super methodSignatureForSelector:@selector(orderOut:)]];
237 [invoc setTarget: self];
238 [invoc setArgument: sender atIndex: 0];
239 [self orderOut: sender animate: animate callback: invoc];
242 - (void)orderOut: (id)sender animate: (BOOL)animate callback:(NSInvocation *)callback
244 NSViewAnimation *anim;
245 NSViewAnimation *current_anim;
246 NSMutableDictionary *dict;
250 [self orderOut: sender];
254 dict = [[NSMutableDictionary alloc] initWithCapacity:2];
256 [dict setObject:self forKey:NSViewAnimationTargetKey];
258 [dict setObject:NSViewAnimationFadeOutEffect forKey:NSViewAnimationEffectKey];
259 anim = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects:dict, nil]];
262 [anim setAnimationBlockingMode:NSAnimationNonblocking];
263 [anim setDuration:0.9];
264 [anim setFrameRate:30];
265 [anim setUserInfo: callback];
267 @synchronized(self) {
268 current_anim = self->animation;
270 if ([[[current_anim viewAnimations] objectAtIndex:0] objectForKey: NSViewAnimationEffectKey] == NSViewAnimationFadeOutEffect && [current_anim isAnimating])
278 [current_anim stopAnimation];
279 [anim setCurrentProgress:1.0-[current_anim currentProgress]];
280 [current_anim release];
283 [anim setCurrentProgress:1.0 - [self alphaValue]];
284 self->animation = anim;
285 [self setDelegate: self];
286 [anim startAnimation];
291 - (void)orderFront: (id)sender animate: (BOOL)animate
293 NSViewAnimation *anim;
294 NSViewAnimation *current_anim;
295 NSMutableDictionary *dict;
299 [super orderFront: sender];
300 [self setAlphaValue: 1.0];
304 if (![self isVisible])
306 [self setAlphaValue: 0.0];
307 [super orderFront: sender];
309 else if ([self alphaValue] == 1.0)
311 [super orderFront: self];
315 dict = [[NSMutableDictionary alloc] initWithCapacity:2];
317 [dict setObject:self forKey:NSViewAnimationTargetKey];
319 [dict setObject:NSViewAnimationFadeInEffect forKey:NSViewAnimationEffectKey];
320 anim = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects:dict, nil]];
323 [anim setAnimationBlockingMode:NSAnimationNonblocking];
324 [anim setDuration:0.5];
325 [anim setFrameRate:30];
327 @synchronized(self) {
328 current_anim = self->animation;
330 if ([[[current_anim viewAnimations] objectAtIndex:0] objectForKey: NSViewAnimationEffectKey] == NSViewAnimationFadeInEffect && [current_anim isAnimating])
338 [current_anim stopAnimation];
339 [anim setCurrentProgress:1.0 - [current_anim currentProgress]];
340 [current_anim release];
343 [anim setCurrentProgress:[self alphaValue]];
344 self->animation = anim;
345 [self setDelegate: self];
346 [self orderFront: sender];
347 [anim startAnimation];
352 - (void)animationDidEnd:(NSAnimation*)anim
354 if ([self alphaValue] <= 0.0)
356 NSInvocation * invoc;
357 [super orderOut: nil];
358 [self setAlphaValue: 1.0];
359 if ((invoc = [anim userInfo]))
365 /*****************************************************************************
367 *****************************************************************************/
369 @implementation VLCControllerView
373 [self unregisterDraggedTypes];
379 [self registerForDraggedTypes:[NSArray arrayWithObjects:NSTIFFPboardType,
380 NSFilenamesPboardType, nil]];
383 - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
385 if ((NSDragOperationGeneric & [sender draggingSourceOperationMask])
386 == NSDragOperationGeneric)
388 return NSDragOperationGeneric;
392 return NSDragOperationNone;
396 - (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
401 - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
403 NSPasteboard *o_paste = [sender draggingPasteboard];
404 NSArray *o_types = [NSArray arrayWithObjects: NSFilenamesPboardType, nil];
405 NSString *o_desired_type = [o_paste availableTypeFromArray:o_types];
406 NSData *o_carried_data = [o_paste dataForType:o_desired_type];
410 if ([o_desired_type isEqualToString:NSFilenamesPboardType])
412 NSArray *o_array = [NSArray array];
413 NSArray *o_values = [[o_paste propertyListForType: NSFilenamesPboardType] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
414 NSUInteger count = [o_values count];
416 for( NSUInteger i = 0; i < count; i++)
419 char *psz_uri = make_URI([[o_values objectAtIndex:i] UTF8String], NULL);
423 o_dic = [NSDictionary dictionaryWithObject:[NSString stringWithCString:psz_uri encoding:NSUTF8StringEncoding] forKey:@"ITEM_URL"];
426 o_array = [o_array arrayByAddingObject: o_dic];
428 [(VLCPlaylist *)[[VLCMain sharedInstance] playlist] appendArray: o_array atPos: -1 enqueue:NO];
432 [self setNeedsDisplay:YES];
436 - (void)concludeDragOperation:(id <NSDraggingInfo>)sender
438 [self setNeedsDisplay:YES];
443 /*****************************************************************************
444 * VLBrushedMetalImageView
445 *****************************************************************************/
447 @implementation VLBrushedMetalImageView
449 - (BOOL)mouseDownCanMoveWindow
456 [self unregisterDraggedTypes];
462 [self registerForDraggedTypes:[NSArray arrayWithObjects:NSTIFFPboardType,
463 NSFilenamesPboardType, nil]];
466 - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
468 if ((NSDragOperationGeneric & [sender draggingSourceOperationMask])
469 == NSDragOperationGeneric)
471 return NSDragOperationGeneric;
475 return NSDragOperationNone;
479 - (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
484 - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
486 NSPasteboard *o_paste = [sender draggingPasteboard];
487 NSArray *o_types = [NSArray arrayWithObjects: NSFilenamesPboardType, nil];
488 NSString *o_desired_type = [o_paste availableTypeFromArray:o_types];
489 NSData *o_carried_data = [o_paste dataForType:o_desired_type];
490 BOOL b_autoplay = config_GetInt( VLCIntf, "macosx-autoplay" );
494 if ([o_desired_type isEqualToString:NSFilenamesPboardType])
496 NSArray *o_array = [NSArray array];
497 NSArray *o_values = [[o_paste propertyListForType: NSFilenamesPboardType] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
498 NSUInteger count = [o_values count];
500 for( NSUInteger i = 0; i < count; i++)
503 char *psz_uri = make_URI([[o_values objectAtIndex:i] UTF8String], NULL);
507 o_dic = [NSDictionary dictionaryWithObject:[NSString stringWithCString:psz_uri encoding:NSUTF8StringEncoding] forKey:@"ITEM_URL"];
510 o_array = [o_array arrayByAddingObject: o_dic];
513 [[[VLCMain sharedInstance] playlist] appendArray: o_array atPos: -1 enqueue:NO];
515 [[[VLCMain sharedInstance] playlist] appendArray: o_array atPos: -1 enqueue:YES];
519 [self setNeedsDisplay:YES];
523 - (void)concludeDragOperation:(id <NSDraggingInfo>)sender
525 [self setNeedsDisplay:YES];
531 /*****************************************************************************
533 *****************************************************************************/
534 @implementation MPSlider
536 void _drawKnobInRect(NSRect knobRect)
538 // Center knob in given rect
539 knobRect.origin.x += (int)((float)(knobRect.size.width - 7)/2.0);
540 knobRect.origin.y += (int)((float)(knobRect.size.height - 7)/2.0);
543 NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 3, knobRect.origin.y + 6, 1, 1), NSCompositeSourceOver);
544 NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 2, knobRect.origin.y + 5, 3, 1), NSCompositeSourceOver);
545 NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 1, knobRect.origin.y + 4, 5, 1), NSCompositeSourceOver);
546 NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 0, knobRect.origin.y + 3, 7, 1), NSCompositeSourceOver);
547 NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 1, knobRect.origin.y + 2, 5, 1), NSCompositeSourceOver);
548 NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 2, knobRect.origin.y + 1, 3, 1), NSCompositeSourceOver);
549 NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 3, knobRect.origin.y + 0, 1, 1), NSCompositeSourceOver);
552 void _drawFrameInRect(NSRect frameRect)
555 NSRectFillUsingOperation(NSMakeRect(frameRect.origin.x, frameRect.origin.y, frameRect.size.width, 1), NSCompositeSourceOver);
556 NSRectFillUsingOperation(NSMakeRect(frameRect.origin.x, frameRect.origin.y + frameRect.size.height-1, frameRect.size.width, 1), NSCompositeSourceOver);
557 NSRectFillUsingOperation(NSMakeRect(frameRect.origin.x, frameRect.origin.y, 1, frameRect.size.height), NSCompositeSourceOver);
558 NSRectFillUsingOperation(NSMakeRect(frameRect.origin.x+frameRect.size.width-1, frameRect.origin.y, 1, frameRect.size.height), NSCompositeSourceOver);
561 - (void)drawRect:(NSRect)rect
563 // Draw default to make sure the slider behaves correctly
564 [[NSGraphicsContext currentContext] saveGraphicsState];
565 NSRectClip(NSZeroRect);
566 [super drawRect:rect];
567 [[NSGraphicsContext currentContext] restoreGraphicsState];
570 rect = [self bounds];
571 int diff = (int)(([[self cell] knobThickness] - 7.0)/2.0) - 1;
572 rect.origin.x += diff-1;
573 rect.origin.y += diff;
574 rect.size.width -= 2*diff-2;
575 rect.size.height -= 2*diff;
578 NSRect knobRect = [[self cell] knobRectFlipped:NO];
579 [[[NSColor blackColor] colorWithAlphaComponent:0.6] set];
580 _drawFrameInRect(rect);
581 _drawKnobInRect(knobRect);
584 [[[NSColor blackColor] colorWithAlphaComponent:0.1] set];
589 _drawFrameInRect(rect);
590 _drawKnobInRect(knobRect);
595 /*****************************************************************************
597 *****************************************************************************/
599 @implementation TimeLineSlider
601 - (void)drawKnobInRect:(NSRect)knobRect
604 NSImage *img = [NSImage imageNamed:@"progression-knob"];
605 image_rect.size = [img size];
606 image_rect.origin.x = 0;
607 image_rect.origin.y = 0;
608 knobRect.origin.x += (knobRect.size.width - image_rect.size.width) / 2;
609 knobRect.size.width = image_rect.size.width;
610 knobRect.size.height = image_rect.size.height;
611 [img drawInRect:knobRect fromRect:image_rect operation:NSCompositeSourceOver fraction:1];
614 - (void)drawRect:(NSRect)rect
616 /* Draw default to make sure the slider behaves correctly */
617 [[NSGraphicsContext currentContext] saveGraphicsState];
618 NSRectClip(NSZeroRect);
619 [super drawRect:rect];
620 [[NSGraphicsContext currentContext] restoreGraphicsState];
622 NSRect knobRect = [[self cell] knobRectFlipped:NO];
623 knobRect.origin.y+=1;
624 [self drawKnobInRect: knobRect];
629 /*****************************************************************************
631 *****************************************************************************/
633 @implementation ITSlider
635 - (void)drawKnobInRect:(NSRect)knobRect
638 NSImage *img = [NSImage imageNamed:@"volume-slider-knob"];
639 image_rect.size = [img size];
640 image_rect.origin.x = 0;
641 image_rect.origin.y = 0;
642 knobRect.origin.x += (knobRect.size.width - image_rect.size.width) / 2;
643 knobRect.size.width = image_rect.size.width;
644 knobRect.size.height = image_rect.size.height;
645 [img drawInRect:knobRect fromRect:image_rect operation:NSCompositeSourceOver fraction:1];
648 - (void)drawRect:(NSRect)rect
650 /* Draw default to make sure the slider behaves correctly */
651 [[NSGraphicsContext currentContext] saveGraphicsState];
652 NSRectClip(NSZeroRect);
653 [super drawRect:rect];
654 [[NSGraphicsContext currentContext] restoreGraphicsState];
656 NSRect knobRect = [[self cell] knobRectFlipped:NO];
657 knobRect.origin.y+=2;
658 [self drawKnobInRect: knobRect];
663 /*****************************************************************************
664 * VLCTimeField implementation
665 *****************************************************************************
666 * we need this to catch our click-event in the controller window
667 *****************************************************************************/
669 @implementation VLCTimeField
671 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
672 NSDictionary *appDefaults = [NSDictionary dictionaryWithObject:@"NO" forKey:@"DisplayTimeAsTimeRemaining"];
674 [defaults registerDefaults:appDefaults];
677 - (void)mouseDown: (NSEvent *)ourEvent
679 if( [ourEvent clickCount] > 1 )
680 [[[VLCMain sharedInstance] controls] goToSpecificTime: nil];
683 if ([[NSUserDefaults standardUserDefaults] boolForKey:@"DisplayTimeAsTimeRemaining"])
684 [[NSUserDefaults standardUserDefaults] setObject:@"NO" forKey:@"DisplayTimeAsTimeRemaining"];
686 [[NSUserDefaults standardUserDefaults] setObject:@"YES" forKey:@"DisplayTimeAsTimeRemaining"];
690 - (BOOL)timeRemaining
692 return [[NSUserDefaults standardUserDefaults] boolForKey:@"DisplayTimeAsTimeRemaining"];
696 /*****************************************************************************
697 * VLCMainWindowSplitView implementation
698 * comments taken from NSSplitView.h (10.7 SDK)
699 *****************************************************************************/
700 @implementation VLCMainWindowSplitView : NSSplitView
701 /* Return the color of the dividers that the split view is drawing between subviews. The default implementation of this method returns [NSColor clearColor] for the thick divider style. It will also return [NSColor clearColor] for the thin divider style when the split view is in a textured window. All other thin dividers are drawn with a color that looks good between two white panes. You can override this method to change the color of dividers.
703 - (NSColor *)dividerColor
705 return [NSColor blackColor];
708 /* Return the thickness of the dividers that the split view is drawing between subviews. The default implementation returns a value that depends on the divider style. You can override this method to change the size of dividers.
710 - (CGFloat)dividerThickness