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 "intf.h" /* VLCApplication */
34 /*****************************************************************************
35 * NSImage (VLCAdditions)
38 *****************************************************************************/
39 @implementation NSImage (VLCAdditions)
40 + (id)imageWithSystemName:(int)name
42 /* ugly Carbon stuff following...
43 * regrettably, you can't get the icons through clean Cocoa */
45 /* retrieve our error icon */
49 returnValue = GetIconRef(kOnSystemDisk, 'macs', name, &ourIconRef);
50 icon = [[[NSImage alloc] initWithSize:NSMakeSize(32,32)] autorelease];
52 CGRect rect = CGRectMake(0,0,32,32);
53 PlotIconRefInContext((CGContextRef)[[NSGraphicsContext currentContext]
58 NULL /*inLabelColor*/,
59 kPlotIconRefNormalFlags,
62 returnValue = ReleaseIconRef(ourIconRef);
66 + (id)imageWithWarningIcon
68 static NSImage * imageWithWarningIcon = nil;
69 if( !imageWithWarningIcon )
71 imageWithWarningIcon = [[[self class] imageWithSystemName:'caut'] retain];
73 return imageWithWarningIcon;
76 + (id)imageWithErrorIcon
78 static NSImage * imageWithErrorIcon = nil;
79 if( !imageWithErrorIcon )
81 imageWithErrorIcon = [[[self class] imageWithSystemName:'stop'] retain];
83 return imageWithErrorIcon;
87 /*****************************************************************************
88 * NSAnimation (VLCAdditions)
90 * Missing extension to NSAnimation
91 *****************************************************************************/
93 @implementation NSAnimation (VLCAdditions)
94 /* fake class attributes */
95 static NSMapTable *VLCAdditions_userInfo = NULL;
99 /* init our fake object attribute */
100 VLCAdditions_userInfo = NSCreateMapTable(NSNonRetainedObjectMapKeyCallBacks, NSObjectMapValueCallBacks, 16);
105 NSMapRemove(VLCAdditions_userInfo, self);
109 - (void)setUserInfo: (void *)userInfo
111 NSMapInsert(VLCAdditions_userInfo, self, (void*)userInfo);
116 return NSMapGet(VLCAdditions_userInfo, self);
120 /*****************************************************************************
121 * NSScreen (VLCAdditions)
123 * Missing extension to NSScreen
124 *****************************************************************************/
126 @implementation NSScreen (VLCAdditions)
128 static NSMutableArray *blackoutWindows = NULL;
132 /* init our fake object attribute */
133 blackoutWindows = [[NSMutableArray alloc] initWithCapacity:1];
136 + (NSScreen *)screenWithDisplayID: (CGDirectDisplayID)displayID
140 for( i = 0; i < [[NSScreen screens] count]; i++ )
142 NSScreen *screen = [[NSScreen screens] objectAtIndex: i];
143 if([screen displayID] == displayID)
151 return ([self displayID] == [[[NSScreen screens] objectAtIndex:0] displayID]);
154 - (BOOL)isScreen: (NSScreen*)screen
156 return ([self displayID] == [screen displayID]);
159 - (CGDirectDisplayID)displayID
161 return (CGDirectDisplayID)[[[self deviceDescription] objectForKey: @"NSScreenNumber"] intValue];
164 - (void)blackoutOtherScreens
168 /* Free our previous blackout window (follow blackoutWindow alloc strategy) */
169 [blackoutWindows makeObjectsPerformSelector:@selector(close)];
170 [blackoutWindows removeAllObjects];
172 for(i = 0; i < [[NSScreen screens] count]; i++)
174 NSScreen *screen = [[NSScreen screens] objectAtIndex: i];
175 VLCWindow *blackoutWindow;
178 if([self isScreen: screen])
181 screen_rect = [screen frame];
182 screen_rect.origin.x = screen_rect.origin.y = 0;
184 /* blackoutWindow alloc strategy
185 - The NSMutableArray blackoutWindows has the blackoutWindow references
186 - blackoutOtherDisplays is responsible for alloc/releasing its Windows
188 blackoutWindow = [[VLCWindow alloc] initWithContentRect: screen_rect styleMask: NSBorderlessWindowMask
189 backing: NSBackingStoreBuffered defer: NO screen: screen];
190 [blackoutWindow setBackgroundColor:[NSColor blackColor]];
191 [blackoutWindow setLevel: NSFloatingWindowLevel]; /* Disappear when Expose is triggered */
193 [blackoutWindow displayIfNeeded];
194 [blackoutWindow orderFront: self animate: YES];
196 [blackoutWindows addObject: blackoutWindow];
197 [blackoutWindow release];
199 if( [screen isMainScreen ] )
200 SetSystemUIMode( kUIModeAllHidden, kUIOptionAutoShowMenuBar);
204 + (void)unblackoutScreens
208 for(i = 0; i < [blackoutWindows count]; i++)
210 VLCWindow *blackoutWindow = [blackoutWindows objectAtIndex: i];
211 [blackoutWindow closeAndAnimate: YES];
214 SetSystemUIMode( kUIModeNormal, 0);
219 /*****************************************************************************
222 * Missing extension to NSWindow
223 *****************************************************************************/
225 @implementation VLCWindow
226 - (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)styleMask
227 backing:(NSBackingStoreType)backingType defer:(BOOL)flag
229 self = [super initWithContentRect:contentRect styleMask:styleMask backing:backingType defer:flag];
232 b_isset_canBecomeKeyWindow = NO;
233 /* we don't want this window to be restored on relaunch */
234 if ([self respondsToSelector:@selector(setRestorable:)])
235 [self setRestorable:NO];
239 - (void)setCanBecomeKeyWindow: (BOOL)canBecomeKey
241 b_isset_canBecomeKeyWindow = YES;
242 b_canBecomeKeyWindow = canBecomeKey;
245 - (BOOL)canBecomeKeyWindow
247 if(b_isset_canBecomeKeyWindow)
248 return b_canBecomeKeyWindow;
250 return [super canBecomeKeyWindow];
253 - (void)closeAndAnimate: (BOOL)animate
263 invoc = [NSInvocation invocationWithMethodSignature:[super methodSignatureForSelector:@selector(close)]];
264 [invoc setTarget: self];
266 if (![self isVisible] || [self alphaValue] == 0.0)
272 [self orderOut: self animate: YES callback: invoc];
275 - (void)orderOut: (id)sender animate: (BOOL)animate
277 NSInvocation *invoc = [NSInvocation invocationWithMethodSignature:[super methodSignatureForSelector:@selector(orderOut:)]];
278 [invoc setTarget: self];
279 [invoc setArgument: sender atIndex: 0];
280 [self orderOut: sender animate: animate callback: invoc];
283 - (void)orderOut: (id)sender animate: (BOOL)animate callback:(NSInvocation *)callback
285 NSViewAnimation *anim;
286 NSViewAnimation *current_anim;
287 NSMutableDictionary *dict;
291 [self orderOut: sender];
295 dict = [[NSMutableDictionary alloc] initWithCapacity:2];
297 [dict setObject:self forKey:NSViewAnimationTargetKey];
299 [dict setObject:NSViewAnimationFadeOutEffect forKey:NSViewAnimationEffectKey];
300 anim = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects:dict, nil]];
303 [anim setAnimationBlockingMode:NSAnimationNonblocking];
304 [anim setDuration:0.9];
305 [anim setFrameRate:30];
306 [anim setUserInfo: callback];
308 @synchronized(self) {
309 current_anim = self->animation;
311 if ([[[current_anim viewAnimations] objectAtIndex:0] objectForKey: NSViewAnimationEffectKey] == NSViewAnimationFadeOutEffect && [current_anim isAnimating])
319 [current_anim stopAnimation];
320 [anim setCurrentProgress:1.0-[current_anim currentProgress]];
321 [current_anim release];
324 [anim setCurrentProgress:1.0 - [self alphaValue]];
325 self->animation = anim;
326 [self setDelegate: self];
327 [anim startAnimation];
332 - (void)orderFront: (id)sender animate: (BOOL)animate
334 NSViewAnimation *anim;
335 NSViewAnimation *current_anim;
336 NSMutableDictionary *dict;
340 [super orderFront: sender];
341 [self setAlphaValue: 1.0];
345 if (![self isVisible])
347 [self setAlphaValue: 0.0];
348 [super orderFront: sender];
350 else if ([self alphaValue] == 1.0)
352 [super orderFront: self];
356 dict = [[NSMutableDictionary alloc] initWithCapacity:2];
358 [dict setObject:self forKey:NSViewAnimationTargetKey];
360 [dict setObject:NSViewAnimationFadeInEffect forKey:NSViewAnimationEffectKey];
361 anim = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects:dict, nil]];
364 [anim setAnimationBlockingMode:NSAnimationNonblocking];
365 [anim setDuration:0.5];
366 [anim setFrameRate:30];
368 @synchronized(self) {
369 current_anim = self->animation;
371 if ([[[current_anim viewAnimations] objectAtIndex:0] objectForKey: NSViewAnimationEffectKey] == NSViewAnimationFadeInEffect && [current_anim isAnimating])
379 [current_anim stopAnimation];
380 [anim setCurrentProgress:1.0 - [current_anim currentProgress]];
381 [current_anim release];
384 [anim setCurrentProgress:[self alphaValue]];
385 self->animation = anim;
386 [self setDelegate: self];
387 [self orderFront: sender];
388 [anim startAnimation];
393 - (void)animationDidEnd:(NSAnimation*)anim
395 if ([self alphaValue] <= 0.0)
397 NSInvocation * invoc;
398 [super orderOut: nil];
399 [self setAlphaValue: 1.0];
400 if ((invoc = [anim userInfo]))
406 /*****************************************************************************
407 * VLCControllerWindow
408 *****************************************************************************/
410 @implementation VLCControllerWindow
412 - (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)styleMask
413 backing:(NSBackingStoreType)backingType defer:(BOOL)flag
415 /* FIXME: this should enable the SnowLeopard window style, however, it leads to ugly artifacts
416 * needs some further investigation! -- feepk
417 BOOL b_useTextured = YES;
419 if( [[NSWindow class] instancesRespondToSelector:@selector(setContentBorderThickness:forEdge:)] )
422 styleMask ^= NSTexturedBackgroundWindowMask;
425 self = [super initWithContentRect:contentRect styleMask:styleMask //& ~NSTitledWindowMask
426 backing:backingType defer:flag];
428 [[VLCMain sharedInstance] updateTogglePlaylistState];
430 /* FIXME: see above...
433 [self setContentBorderThickness:28.0 forEdge:NSMinYEdge];
437 /* we don't want this window to be restored on relaunch */
438 if ([self respondsToSelector:@selector(setRestorable:)])
439 [self setRestorable:NO];
444 - (BOOL)performKeyEquivalent:(NSEvent *)o_event
446 /* We indeed want to prioritize Cocoa key equivalent against libvlc,
447 so we perform the menu equivalent now. */
448 if([[NSApp mainMenu] performKeyEquivalent:o_event])
451 return [[VLCMain sharedInstance] hasDefinedShortcutKey:o_event] ||
452 [(VLCControls *)[[VLCMain sharedInstance] controls] keyEvent:o_event];
459 /*****************************************************************************
461 *****************************************************************************/
463 @implementation VLCControllerView
467 [self unregisterDraggedTypes];
473 [self registerForDraggedTypes:[NSArray arrayWithObjects:NSTIFFPboardType,
474 NSFilenamesPboardType, nil]];
477 - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
479 if ((NSDragOperationGeneric & [sender draggingSourceOperationMask])
480 == NSDragOperationGeneric)
482 return NSDragOperationGeneric;
486 return NSDragOperationNone;
490 - (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
495 - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
497 NSPasteboard *o_paste = [sender draggingPasteboard];
498 NSArray *o_types = [NSArray arrayWithObjects: NSFilenamesPboardType, nil];
499 NSString *o_desired_type = [o_paste availableTypeFromArray:o_types];
500 NSData *o_carried_data = [o_paste dataForType:o_desired_type];
504 if ([o_desired_type isEqualToString:NSFilenamesPboardType])
507 NSArray *o_array = [NSArray array];
508 NSArray *o_values = [[o_paste propertyListForType: NSFilenamesPboardType]
509 sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
511 for( i = 0; i < (int)[o_values count]; i++)
514 char *psz_uri = make_URI([[o_values objectAtIndex:i] UTF8String], NULL);
518 o_dic = [NSDictionary dictionaryWithObject:[NSString stringWithCString:psz_uri encoding:NSUTF8StringEncoding] forKey:@"ITEM_URL"];
521 o_array = [o_array arrayByAddingObject: o_dic];
523 [(VLCPlaylist *)[[VLCMain sharedInstance] playlist] appendArray: o_array atPos: -1 enqueue:NO];
527 [self setNeedsDisplay:YES];
531 - (void)concludeDragOperation:(id <NSDraggingInfo>)sender
533 [self setNeedsDisplay:YES];
538 /*****************************************************************************
539 * VLBrushedMetalImageView
540 *****************************************************************************/
542 @implementation VLBrushedMetalImageView
544 - (BOOL)mouseDownCanMoveWindow
551 [self unregisterDraggedTypes];
557 [self registerForDraggedTypes:[NSArray arrayWithObjects:NSTIFFPboardType,
558 NSFilenamesPboardType, nil]];
561 - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
563 if ((NSDragOperationGeneric & [sender draggingSourceOperationMask])
564 == NSDragOperationGeneric)
566 return NSDragOperationGeneric;
570 return NSDragOperationNone;
574 - (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
579 - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
581 NSPasteboard *o_paste = [sender draggingPasteboard];
582 NSArray *o_types = [NSArray arrayWithObjects: NSFilenamesPboardType, nil];
583 NSString *o_desired_type = [o_paste availableTypeFromArray:o_types];
584 NSData *o_carried_data = [o_paste dataForType:o_desired_type];
585 BOOL b_autoplay = config_GetInt( VLCIntf, "macosx-autoplay" );
589 if ([o_desired_type isEqualToString:NSFilenamesPboardType])
592 NSArray *o_array = [NSArray array];
593 NSArray *o_values = [[o_paste propertyListForType: NSFilenamesPboardType]
594 sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
596 for( i = 0; i < (int)[o_values count]; i++)
599 char *psz_uri = make_URI([[o_values objectAtIndex:i] UTF8String], NULL);
603 o_dic = [NSDictionary dictionaryWithObject:[NSString stringWithCString:psz_uri encoding:NSUTF8StringEncoding] forKey:@"ITEM_URL"];
606 o_array = [o_array arrayByAddingObject: o_dic];
609 [[[VLCMain sharedInstance] playlist] appendArray: o_array atPos: -1 enqueue:NO];
611 [[[VLCMain sharedInstance] playlist] appendArray: o_array atPos: -1 enqueue:YES];
615 [self setNeedsDisplay:YES];
619 - (void)concludeDragOperation:(id <NSDraggingInfo>)sender
621 [self setNeedsDisplay:YES];
627 /*****************************************************************************
629 *****************************************************************************/
630 @implementation MPSlider
632 void _drawKnobInRect(NSRect knobRect)
634 // Center knob in given rect
635 knobRect.origin.x += (int)((float)(knobRect.size.width - 7)/2.0);
636 knobRect.origin.y += (int)((float)(knobRect.size.height - 7)/2.0);
639 NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 3, knobRect.origin.y + 6, 1, 1), NSCompositeSourceOver);
640 NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 2, knobRect.origin.y + 5, 3, 1), NSCompositeSourceOver);
641 NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 1, knobRect.origin.y + 4, 5, 1), NSCompositeSourceOver);
642 NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 0, knobRect.origin.y + 3, 7, 1), NSCompositeSourceOver);
643 NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 1, knobRect.origin.y + 2, 5, 1), NSCompositeSourceOver);
644 NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 2, knobRect.origin.y + 1, 3, 1), NSCompositeSourceOver);
645 NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 3, knobRect.origin.y + 0, 1, 1), NSCompositeSourceOver);
648 void _drawFrameInRect(NSRect frameRect)
651 NSRectFillUsingOperation(NSMakeRect(frameRect.origin.x, frameRect.origin.y, frameRect.size.width, 1), NSCompositeSourceOver);
652 NSRectFillUsingOperation(NSMakeRect(frameRect.origin.x, frameRect.origin.y + frameRect.size.height-1, frameRect.size.width, 1), NSCompositeSourceOver);
653 NSRectFillUsingOperation(NSMakeRect(frameRect.origin.x, frameRect.origin.y, 1, frameRect.size.height), NSCompositeSourceOver);
654 NSRectFillUsingOperation(NSMakeRect(frameRect.origin.x+frameRect.size.width-1, frameRect.origin.y, 1, frameRect.size.height), NSCompositeSourceOver);
657 - (void)drawRect:(NSRect)rect
659 // Draw default to make sure the slider behaves correctly
660 [[NSGraphicsContext currentContext] saveGraphicsState];
661 NSRectClip(NSZeroRect);
662 [super drawRect:rect];
663 [[NSGraphicsContext currentContext] restoreGraphicsState];
666 rect = [self bounds];
667 int diff = (int)(([[self cell] knobThickness] - 7.0)/2.0) - 1;
668 rect.origin.x += diff-1;
669 rect.origin.y += diff;
670 rect.size.width -= 2*diff-2;
671 rect.size.height -= 2*diff;
674 NSRect knobRect = [[self cell] knobRectFlipped:NO];
675 [[[NSColor blackColor] colorWithAlphaComponent:0.6] set];
676 _drawFrameInRect(rect);
677 _drawKnobInRect(knobRect);
680 [[[NSColor blackColor] colorWithAlphaComponent:0.1] set];
685 _drawFrameInRect(rect);
686 _drawKnobInRect(knobRect);
692 /*****************************************************************************
694 *****************************************************************************/
696 @implementation ITSlider
700 if ([[self cell] class] != [ITSliderCell class]) {
702 NSSliderCell *oldCell = [self cell];
703 NSSliderCell *newCell = [[[ITSliderCell alloc] init] autorelease];
704 [newCell setTag:[oldCell tag]];
705 [newCell setTarget:[oldCell target]];
706 [newCell setAction:[oldCell action]];
707 [newCell setControlSize:[oldCell controlSize]];
708 [newCell setType:[oldCell type]];
709 [newCell setState:[oldCell state]];
710 [newCell setAllowsTickMarkValuesOnly:[oldCell allowsTickMarkValuesOnly]];
711 [newCell setAltIncrementValue:[oldCell altIncrementValue]];
712 [newCell setControlTint:[oldCell controlTint]];
713 [newCell setKnobThickness:[oldCell knobThickness]];
714 [newCell setMaxValue:[oldCell maxValue]];
715 [newCell setMinValue:[oldCell minValue]];
716 [newCell setDoubleValue:[oldCell doubleValue]];
717 [newCell setNumberOfTickMarks:[oldCell numberOfTickMarks]];
718 [newCell setEditable:[oldCell isEditable]];
719 [newCell setEnabled:[oldCell isEnabled]];
720 [newCell setFormatter:[oldCell formatter]];
721 [newCell setHighlighted:[oldCell isHighlighted]];
722 [newCell setTickMarkPosition:[oldCell tickMarkPosition]];
723 [self setCell:newCell];
729 /*************************************************************************** **
731 *****************************************************************************/
732 @implementation ITSliderCell
737 _knobOff = [NSImage imageNamed:@"volumeslider_normal"];
738 _knobOn = [NSImage imageNamed:@"volumeslider_graphite"];
739 b_mouse_down = FALSE;
747 [[NSNotificationCenter defaultCenter] removeObserver: self];
753 - (void)drawKnob:(NSRect)knob_rect
762 [[self controlView] lockFocus];
763 [knob compositeToPoint:NSMakePoint( knob_rect.origin.x + 1,
764 knob_rect.origin.y + knob_rect.size.height -2 )
765 operation:NSCompositeSourceOver];
766 [[self controlView] unlockFocus];
769 - (void)stopTracking:(NSPoint)lastPoint at:(NSPoint)stopPoint inView:
770 (NSView *)controlView mouseIsUp:(BOOL)flag
774 [super stopTracking:lastPoint at:stopPoint inView:controlView mouseIsUp:flag];
777 - (BOOL)startTrackingAt:(NSPoint)startPoint inView:(NSView *)controlView
781 return [super startTrackingAt:startPoint inView:controlView];