1 /*****************************************************************************
2 * misc.m: code not specific to vlc
3 *****************************************************************************
4 * Copyright (C) 2003-2009 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 */
33 /*****************************************************************************
34 * NSImage (VLCAdditions)
37 *****************************************************************************/
38 @implementation NSImage (VLCAdditions)
39 + (id)imageWithSystemName:(int)name
41 /* ugly Carbon stuff following...
42 * regrettably, you can't get the icons through clean Cocoa */
44 /* retrieve our error icon */
48 returnValue = GetIconRef(kOnSystemDisk, 'macs', name, &ourIconRef);
49 icon = [[[NSImage alloc] initWithSize:NSMakeSize(32,32)] autorelease];
51 CGRect rect = CGRectMake(0,0,32,32);
52 PlotIconRefInContext((CGContextRef)[[NSGraphicsContext currentContext]
57 NULL /*inLabelColor*/,
58 kPlotIconRefNormalFlags,
61 returnValue = ReleaseIconRef(ourIconRef);
65 + (id)imageWithWarningIcon
67 static NSImage * imageWithWarningIcon = nil;
68 if( !imageWithWarningIcon )
70 imageWithWarningIcon = [[[self class] imageWithSystemName:'caut'] retain];
72 return imageWithWarningIcon;
75 + (id)imageWithErrorIcon
77 static NSImage * imageWithErrorIcon = nil;
78 if( !imageWithErrorIcon )
80 imageWithErrorIcon = [[[self class] imageWithSystemName:'stop'] retain];
82 return imageWithErrorIcon;
86 /*****************************************************************************
87 * NSAnimation (VLCAdditions)
89 * Missing extension to NSAnimation
90 *****************************************************************************/
92 @implementation NSAnimation (VLCAdditions)
93 /* fake class attributes */
94 static NSMapTable *VLCAdditions_userInfo = NULL;
98 /* init our fake object attribute */
99 VLCAdditions_userInfo = NSCreateMapTable(NSNonRetainedObjectMapKeyCallBacks, NSObjectMapValueCallBacks, 16);
104 NSMapRemove(VLCAdditions_userInfo, self);
108 - (void)setUserInfo: (void *)userInfo
110 NSMapInsert(VLCAdditions_userInfo, self, (void*)userInfo);
115 return NSMapGet(VLCAdditions_userInfo, self);
119 /*****************************************************************************
120 * NSScreen (VLCAdditions)
122 * Missing extension to NSScreen
123 *****************************************************************************/
125 @implementation NSScreen (VLCAdditions)
127 static NSMutableArray *blackoutWindows = NULL;
131 /* init our fake object attribute */
132 blackoutWindows = [[NSMutableArray alloc] initWithCapacity:1];
137 [blackoutWindows release];
141 + (NSScreen *)screenWithDisplayID: (CGDirectDisplayID)displayID
145 for( i = 0; i < [[NSScreen screens] count]; i++ )
147 NSScreen *screen = [[NSScreen screens] objectAtIndex: i];
148 if([screen displayID] == displayID)
156 return ([self displayID] == [[[NSScreen screens] objectAtIndex:0] displayID]);
159 - (BOOL)isScreen: (NSScreen*)screen
161 return ([self displayID] == [screen displayID]);
164 - (CGDirectDisplayID)displayID
166 return (CGDirectDisplayID)[[[self deviceDescription] objectForKey: @"NSScreenNumber"] intValue];
169 - (void)blackoutOtherScreens
173 /* Free our previous blackout window (follow blackoutWindow alloc strategy) */
174 [blackoutWindows makeObjectsPerformSelector:@selector(close)];
175 [blackoutWindows removeAllObjects];
177 for(i = 0; i < [[NSScreen screens] count]; i++)
179 NSScreen *screen = [[NSScreen screens] objectAtIndex: i];
180 VLCWindow *blackoutWindow;
183 if([self isScreen: screen])
186 screen_rect = [screen frame];
187 screen_rect.origin.x = screen_rect.origin.y = 0;
189 /* blackoutWindow alloc strategy
190 - The NSMutableArray blackoutWindows has the blackoutWindow references
191 - blackoutOtherDisplays is responsible for alloc/releasing its Windows
193 blackoutWindow = [[VLCWindow alloc] initWithContentRect: screen_rect styleMask: NSBorderlessWindowMask
194 backing: NSBackingStoreBuffered defer: NO screen: screen];
195 [blackoutWindow setBackgroundColor:[NSColor blackColor]];
196 [blackoutWindow setLevel: NSFloatingWindowLevel]; /* Disappear when Expose is triggered */
198 [blackoutWindow displayIfNeeded];
199 [blackoutWindow orderFront: self animate: YES];
201 [blackoutWindows addObject: blackoutWindow];
202 [blackoutWindow release];
204 if( [screen isMainScreen ] )
205 SetSystemUIMode( kUIModeAllHidden, kUIOptionAutoShowMenuBar);
209 + (void)unblackoutScreens
213 for(i = 0; i < [blackoutWindows count]; i++)
215 VLCWindow *blackoutWindow = [blackoutWindows objectAtIndex: i];
216 [blackoutWindow closeAndAnimate: YES];
219 SetSystemUIMode( kUIModeNormal, 0);
224 /*****************************************************************************
227 * Missing extension to NSWindow
228 *****************************************************************************/
230 @implementation VLCWindow
231 - (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)styleMask
232 backing:(NSBackingStoreType)backingType defer:(BOOL)flag
234 self = [super initWithContentRect:contentRect styleMask:styleMask backing:backingType defer:flag];
236 b_isset_canBecomeKeyWindow = 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: (id)super];
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: (id)super];
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];
439 - (BOOL)performKeyEquivalent:(NSEvent *)o_event
441 /* We indeed want to prioritize Cocoa key equivalent against libvlc,
442 so we perform the menu equivalent now. */
443 if([[NSApp mainMenu] performKeyEquivalent:o_event])
446 return [[VLCMain sharedInstance] hasDefinedShortcutKey:o_event] ||
447 [(VLCControls *)[[VLCMain sharedInstance] controls] keyEvent:o_event];
454 /*****************************************************************************
456 *****************************************************************************/
458 @implementation VLCControllerView
462 [self unregisterDraggedTypes];
468 [self registerForDraggedTypes:[NSArray arrayWithObjects:NSTIFFPboardType,
469 NSFilenamesPboardType, nil]];
472 - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
474 if ((NSDragOperationGeneric & [sender draggingSourceOperationMask])
475 == NSDragOperationGeneric)
477 return NSDragOperationGeneric;
481 return NSDragOperationNone;
485 - (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
490 - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
492 NSPasteboard *o_paste = [sender draggingPasteboard];
493 NSArray *o_types = [NSArray arrayWithObjects: NSFilenamesPboardType, nil];
494 NSString *o_desired_type = [o_paste availableTypeFromArray:o_types];
495 NSData *o_carried_data = [o_paste dataForType:o_desired_type];
499 if ([o_desired_type isEqualToString:NSFilenamesPboardType])
502 NSArray *o_array = [NSArray array];
503 NSArray *o_values = [[o_paste propertyListForType: NSFilenamesPboardType]
504 sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
506 for( i = 0; i < (int)[o_values count]; i++)
509 o_dic = [NSDictionary dictionaryWithObject:[o_values objectAtIndex:i] forKey:@"ITEM_URL"];
510 o_array = [o_array arrayByAddingObject: o_dic];
512 [(VLCPlaylist *)[[VLCMain sharedInstance] playlist] appendArray: o_array atPos: -1 enqueue:NO];
516 [self setNeedsDisplay:YES];
520 - (void)concludeDragOperation:(id <NSDraggingInfo>)sender
522 [self setNeedsDisplay:YES];
527 /*****************************************************************************
528 * VLBrushedMetalImageView
529 *****************************************************************************/
531 @implementation VLBrushedMetalImageView
533 - (BOOL)mouseDownCanMoveWindow
540 [self unregisterDraggedTypes];
546 [self registerForDraggedTypes:[NSArray arrayWithObjects:NSTIFFPboardType,
547 NSFilenamesPboardType, nil]];
550 - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
552 if ((NSDragOperationGeneric & [sender draggingSourceOperationMask])
553 == NSDragOperationGeneric)
555 return NSDragOperationGeneric;
559 return NSDragOperationNone;
563 - (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
568 - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
570 NSPasteboard *o_paste = [sender draggingPasteboard];
571 NSArray *o_types = [NSArray arrayWithObjects: NSFilenamesPboardType, nil];
572 NSString *o_desired_type = [o_paste availableTypeFromArray:o_types];
573 NSData *o_carried_data = [o_paste dataForType:o_desired_type];
574 BOOL b_autoplay = config_GetInt( VLCIntf, "macosx-autoplay" );
578 if ([o_desired_type isEqualToString:NSFilenamesPboardType])
581 NSArray *o_array = [NSArray array];
582 NSArray *o_values = [[o_paste propertyListForType: NSFilenamesPboardType]
583 sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
585 for( i = 0; i < (int)[o_values count]; i++)
588 o_dic = [NSDictionary dictionaryWithObject:[o_values objectAtIndex:i] forKey:@"ITEM_URL"];
589 o_array = [o_array arrayByAddingObject: o_dic];
592 [[[VLCMain sharedInstance] playlist] appendArray: o_array atPos: -1 enqueue:NO];
594 [[[VLCMain sharedInstance] playlist] appendArray: o_array atPos: -1 enqueue:YES];
598 [self setNeedsDisplay:YES];
602 - (void)concludeDragOperation:(id <NSDraggingInfo>)sender
604 [self setNeedsDisplay:YES];
610 /*****************************************************************************
612 *****************************************************************************/
613 @implementation MPSlider
615 void _drawKnobInRect(NSRect knobRect)
617 // Center knob in given rect
618 knobRect.origin.x += (int)((float)(knobRect.size.width - 7)/2.0);
619 knobRect.origin.y += (int)((float)(knobRect.size.height - 7)/2.0);
622 NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 3, knobRect.origin.y + 6, 1, 1), NSCompositeSourceOver);
623 NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 2, knobRect.origin.y + 5, 3, 1), NSCompositeSourceOver);
624 NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 1, knobRect.origin.y + 4, 5, 1), NSCompositeSourceOver);
625 NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 0, knobRect.origin.y + 3, 7, 1), NSCompositeSourceOver);
626 NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 1, knobRect.origin.y + 2, 5, 1), NSCompositeSourceOver);
627 NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 2, knobRect.origin.y + 1, 3, 1), NSCompositeSourceOver);
628 NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 3, knobRect.origin.y + 0, 1, 1), NSCompositeSourceOver);
631 void _drawFrameInRect(NSRect frameRect)
634 NSRectFillUsingOperation(NSMakeRect(frameRect.origin.x, frameRect.origin.y, frameRect.size.width, 1), NSCompositeSourceOver);
635 NSRectFillUsingOperation(NSMakeRect(frameRect.origin.x, frameRect.origin.y + frameRect.size.height-1, frameRect.size.width, 1), NSCompositeSourceOver);
636 NSRectFillUsingOperation(NSMakeRect(frameRect.origin.x, frameRect.origin.y, 1, frameRect.size.height), NSCompositeSourceOver);
637 NSRectFillUsingOperation(NSMakeRect(frameRect.origin.x+frameRect.size.width-1, frameRect.origin.y, 1, frameRect.size.height), NSCompositeSourceOver);
640 - (void)drawRect:(NSRect)rect
642 // Draw default to make sure the slider behaves correctly
643 [[NSGraphicsContext currentContext] saveGraphicsState];
644 NSRectClip(NSZeroRect);
645 [super drawRect:rect];
646 [[NSGraphicsContext currentContext] restoreGraphicsState];
649 rect = [self bounds];
650 int diff = (int)(([[self cell] knobThickness] - 7.0)/2.0) - 1;
651 rect.origin.x += diff-1;
652 rect.origin.y += diff;
653 rect.size.width -= 2*diff-2;
654 rect.size.height -= 2*diff;
657 NSRect knobRect = [[self cell] knobRectFlipped:NO];
658 [[[NSColor blackColor] colorWithAlphaComponent:0.6] set];
659 _drawFrameInRect(rect);
660 _drawKnobInRect(knobRect);
663 [[[NSColor blackColor] colorWithAlphaComponent:0.1] set];
668 _drawFrameInRect(rect);
669 _drawKnobInRect(knobRect);
675 /*****************************************************************************
677 *****************************************************************************/
679 @implementation ITSlider
683 if ([[self cell] class] != [ITSliderCell class]) {
685 NSSliderCell *oldCell = [self cell];
686 NSSliderCell *newCell = [[[ITSliderCell alloc] init] autorelease];
687 [newCell setTag:[oldCell tag]];
688 [newCell setTarget:[oldCell target]];
689 [newCell setAction:[oldCell action]];
690 [newCell setControlSize:[oldCell controlSize]];
691 [newCell setType:[oldCell type]];
692 [newCell setState:[oldCell state]];
693 [newCell setAllowsTickMarkValuesOnly:[oldCell allowsTickMarkValuesOnly]];
694 [newCell setAltIncrementValue:[oldCell altIncrementValue]];
695 [newCell setControlTint:[oldCell controlTint]];
696 [newCell setKnobThickness:[oldCell knobThickness]];
697 [newCell setMaxValue:[oldCell maxValue]];
698 [newCell setMinValue:[oldCell minValue]];
699 [newCell setDoubleValue:[oldCell doubleValue]];
700 [newCell setNumberOfTickMarks:[oldCell numberOfTickMarks]];
701 [newCell setEditable:[oldCell isEditable]];
702 [newCell setEnabled:[oldCell isEnabled]];
703 [newCell setFormatter:[oldCell formatter]];
704 [newCell setHighlighted:[oldCell isHighlighted]];
705 [newCell setTickMarkPosition:[oldCell tickMarkPosition]];
706 [self setCell:newCell];
712 /*****************************************************************************
714 *****************************************************************************/
715 @implementation ITSliderCell
720 _knobOff = [NSImage imageNamed:@"volumeslider_normal"];
721 [self controlTintChanged];
722 [[NSNotificationCenter defaultCenter] addObserver: self
723 selector: @selector( controlTintChanged )
724 name: NSControlTintDidChangeNotification
726 b_mouse_down = FALSE;
730 - (void)controlTintChanged
732 if( [NSColor currentControlTint] == NSGraphiteControlTint )
733 _knobOn = [NSImage imageNamed:@"volumeslider_graphite"];
735 _knobOn = [NSImage imageNamed:@"volumeslider_blue"];
740 [[NSNotificationCenter defaultCenter] removeObserver: self];
746 - (void)drawKnob:(NSRect)knob_rect
755 [[self controlView] lockFocus];
756 [knob compositeToPoint:NSMakePoint( knob_rect.origin.x + 1,
757 knob_rect.origin.y + knob_rect.size.height -2 )
758 operation:NSCompositeSourceOver];
759 [[self controlView] unlockFocus];
762 - (void)stopTracking:(NSPoint)lastPoint at:(NSPoint)stopPoint inView:
763 (NSView *)controlView mouseIsUp:(BOOL)flag
767 [super stopTracking:lastPoint at:stopPoint inView:controlView mouseIsUp:flag];
770 - (BOOL)startTrackingAt:(NSPoint)startPoint inView:(NSView *)controlView
774 return [super startTrackingAt:startPoint inView:controlView];