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 <QuickTime/QuickTime.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];
135 + (NSScreen *)screenWithDisplayID: (CGDirectDisplayID)displayID
139 for( i = 0; i < [[NSScreen screens] count]; i++ )
141 NSScreen *screen = [[NSScreen screens] objectAtIndex: i];
142 if([screen displayID] == displayID)
150 return ([self displayID] == [[[NSScreen screens] objectAtIndex:0] displayID]);
153 - (BOOL)isScreen: (NSScreen*)screen
155 return ([self displayID] == [screen displayID]);
158 - (CGDirectDisplayID)displayID
160 return (CGDirectDisplayID)_screenNumber;
163 - (void)blackoutOtherScreens
167 /* Free our previous blackout window (follow blackoutWindow alloc strategy) */
168 [blackoutWindows makeObjectsPerformSelector:@selector(close)];
169 [blackoutWindows removeAllObjects];
171 for(i = 0; i < [[NSScreen screens] count]; i++)
173 NSScreen *screen = [[NSScreen screens] objectAtIndex: i];
174 VLCWindow *blackoutWindow;
177 if([self isScreen: screen])
180 screen_rect = [screen frame];
181 screen_rect.origin.x = screen_rect.origin.y = 0;
183 /* blackoutWindow alloc strategy
184 - The NSMutableArray blackoutWindows has the blackoutWindow references
185 - blackoutOtherDisplays is responsible for alloc/releasing its Windows
187 blackoutWindow = [[VLCWindow alloc] initWithContentRect: screen_rect styleMask: NSBorderlessWindowMask
188 backing: NSBackingStoreBuffered defer: NO screen: screen];
189 [blackoutWindow setBackgroundColor:[NSColor blackColor]];
190 [blackoutWindow setLevel: NSFloatingWindowLevel]; /* Disappear when Expose is triggered */
192 [blackoutWindow displayIfNeeded];
193 [blackoutWindow orderFront: self animate: YES];
195 [blackoutWindows addObject: blackoutWindow];
196 [blackoutWindow release];
198 if( [screen isMainScreen ] )
199 SetSystemUIMode( kUIModeAllHidden, kUIOptionAutoShowMenuBar);
203 + (void)unblackoutScreens
207 for(i = 0; i < [blackoutWindows count]; i++)
209 VLCWindow *blackoutWindow = [blackoutWindows objectAtIndex: i];
210 [blackoutWindow closeAndAnimate: YES];
213 SetSystemUIMode( kUIModeNormal, 0);
218 /*****************************************************************************
221 * Missing extension to NSWindow
222 *****************************************************************************/
224 @implementation VLCWindow
225 - (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)styleMask
226 backing:(NSBackingStoreType)backingType defer:(BOOL)flag
228 self = [super initWithContentRect:contentRect styleMask:styleMask backing:backingType defer:flag];
230 b_isset_canBecomeKeyWindow = NO;
233 - (void)setCanBecomeKeyWindow: (BOOL)canBecomeKey
235 b_isset_canBecomeKeyWindow = YES;
236 b_canBecomeKeyWindow = canBecomeKey;
239 - (BOOL)canBecomeKeyWindow
241 if(b_isset_canBecomeKeyWindow)
242 return b_canBecomeKeyWindow;
244 return [super canBecomeKeyWindow];
247 - (void)closeAndAnimate: (BOOL)animate
257 invoc = [NSInvocation invocationWithMethodSignature:[super methodSignatureForSelector:@selector(close)]];
258 [invoc setTarget: (id)super];
260 if (![self isVisible] || [self alphaValue] == 0.0)
266 [self orderOut: self animate: YES callback: invoc];
269 - (void)orderOut: (id)sender animate: (BOOL)animate
271 NSInvocation *invoc = [NSInvocation invocationWithMethodSignature:[super methodSignatureForSelector:@selector(orderOut:)]];
272 [invoc setTarget: (id)super];
273 [invoc setArgument: sender atIndex: 0];
274 [self orderOut: sender animate: animate callback: invoc];
277 - (void)orderOut: (id)sender animate: (BOOL)animate callback:(NSInvocation *)callback
279 NSViewAnimation *anim;
280 NSViewAnimation *current_anim;
281 NSMutableDictionary *dict;
285 [self orderOut: sender];
289 dict = [[NSMutableDictionary alloc] initWithCapacity:2];
291 [dict setObject:self forKey:NSViewAnimationTargetKey];
293 [dict setObject:NSViewAnimationFadeOutEffect forKey:NSViewAnimationEffectKey];
294 anim = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects:dict, nil]];
297 [anim setAnimationBlockingMode:NSAnimationNonblocking];
298 [anim setDuration:0.9];
299 [anim setFrameRate:30];
300 [anim setUserInfo: callback];
302 @synchronized(self) {
303 current_anim = self->animation;
305 if ([[[current_anim viewAnimations] objectAtIndex:0] objectForKey: NSViewAnimationEffectKey] == NSViewAnimationFadeOutEffect && [current_anim isAnimating])
313 [current_anim stopAnimation];
314 [anim setCurrentProgress:1.0-[current_anim currentProgress]];
315 [current_anim release];
318 [anim setCurrentProgress:1.0 - [self alphaValue]];
319 self->animation = anim;
320 [self setDelegate: self];
321 [anim startAnimation];
326 - (void)orderFront: (id)sender animate: (BOOL)animate
328 NSViewAnimation *anim;
329 NSViewAnimation *current_anim;
330 NSMutableDictionary *dict;
334 [super orderFront: sender];
335 [self setAlphaValue: 1.0];
339 if (![self isVisible])
341 [self setAlphaValue: 0.0];
342 [super orderFront: sender];
344 else if ([self alphaValue] == 1.0)
346 [super orderFront: self];
350 dict = [[NSMutableDictionary alloc] initWithCapacity:2];
352 [dict setObject:self forKey:NSViewAnimationTargetKey];
354 [dict setObject:NSViewAnimationFadeInEffect forKey:NSViewAnimationEffectKey];
355 anim = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects:dict, nil]];
358 [anim setAnimationBlockingMode:NSAnimationNonblocking];
359 [anim setDuration:0.5];
360 [anim setFrameRate:30];
362 @synchronized(self) {
363 current_anim = self->animation;
365 if ([[[current_anim viewAnimations] objectAtIndex:0] objectForKey: NSViewAnimationEffectKey] == NSViewAnimationFadeInEffect && [current_anim isAnimating])
373 [current_anim stopAnimation];
374 [anim setCurrentProgress:1.0 - [current_anim currentProgress]];
375 [current_anim release];
378 [anim setCurrentProgress:[self alphaValue]];
379 self->animation = anim;
380 [self setDelegate: self];
381 [self orderFront: sender];
382 [anim startAnimation];
387 - (void)animationDidEnd:(NSAnimation*)anim
389 if ([self alphaValue] <= 0.0)
391 NSInvocation * invoc;
392 [super orderOut: nil];
393 [self setAlphaValue: 1.0];
394 if ((invoc = [anim userInfo]))
400 /*****************************************************************************
401 * VLCControllerWindow
402 *****************************************************************************/
404 @implementation VLCControllerWindow
406 - (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)styleMask
407 backing:(NSBackingStoreType)backingType defer:(BOOL)flag
409 BOOL b_useTextured = YES;
410 if( [[NSWindow class] instancesRespondToSelector:@selector(setContentBorderThickness:forEdge:)] )
413 styleMask ^= NSTexturedBackgroundWindowMask;
416 self = [super initWithContentRect:contentRect styleMask:styleMask //& ~NSTitledWindowMask
417 backing:backingType defer:flag];
419 [[VLCMain sharedInstance] updateTogglePlaylistState];
423 [self setContentBorderThickness:28.0 forEdge:NSMinYEdge];
428 - (BOOL)performKeyEquivalent:(NSEvent *)o_event
430 /* We indeed want to prioritize Cocoa key equivalent against libvlc,
431 so we perform the menu equivalent now. */
432 if([[NSApp mainMenu] performKeyEquivalent:o_event])
435 return [[VLCMain sharedInstance] hasDefinedShortcutKey:o_event] ||
436 [(VLCControls *)[[VLCMain sharedInstance] getControls] keyEvent:o_event];
443 /*****************************************************************************
445 *****************************************************************************/
447 @implementation VLCControllerView
451 [self unregisterDraggedTypes];
457 [self registerForDraggedTypes:[NSArray arrayWithObjects:NSTIFFPboardType,
458 NSFilenamesPboardType, nil]];
461 - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
463 if ((NSDragOperationGeneric & [sender draggingSourceOperationMask])
464 == NSDragOperationGeneric)
466 return NSDragOperationGeneric;
470 return NSDragOperationNone;
474 - (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
479 - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
481 NSPasteboard *o_paste = [sender draggingPasteboard];
482 NSArray *o_types = [NSArray arrayWithObjects: NSFilenamesPboardType, nil];
483 NSString *o_desired_type = [o_paste availableTypeFromArray:o_types];
484 NSData *o_carried_data = [o_paste dataForType:o_desired_type];
488 if ([o_desired_type isEqualToString:NSFilenamesPboardType])
491 NSArray *o_array = [NSArray array];
492 NSArray *o_values = [[o_paste propertyListForType: NSFilenamesPboardType]
493 sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
495 for( i = 0; i < (int)[o_values count]; i++)
498 o_dic = [NSDictionary dictionaryWithObject:[o_values objectAtIndex:i] forKey:@"ITEM_URL"];
499 o_array = [o_array arrayByAddingObject: o_dic];
501 [(VLCPlaylist *)[[VLCMain sharedInstance] getPlaylist] appendArray: o_array atPos: -1 enqueue:NO];
505 [self setNeedsDisplay:YES];
509 - (void)concludeDragOperation:(id <NSDraggingInfo>)sender
511 [self setNeedsDisplay:YES];
516 /*****************************************************************************
517 * VLBrushedMetalImageView
518 *****************************************************************************/
520 @implementation VLBrushedMetalImageView
522 - (BOOL)mouseDownCanMoveWindow
529 [self unregisterDraggedTypes];
535 [self registerForDraggedTypes:[NSArray arrayWithObjects:NSTIFFPboardType,
536 NSFilenamesPboardType, nil]];
539 - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
541 if ((NSDragOperationGeneric & [sender draggingSourceOperationMask])
542 == NSDragOperationGeneric)
544 return NSDragOperationGeneric;
548 return NSDragOperationNone;
552 - (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
557 - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
559 NSPasteboard *o_paste = [sender draggingPasteboard];
560 NSArray *o_types = [NSArray arrayWithObjects: NSFilenamesPboardType, nil];
561 NSString *o_desired_type = [o_paste availableTypeFromArray:o_types];
562 NSData *o_carried_data = [o_paste dataForType:o_desired_type];
563 BOOL b_autoplay = config_GetInt( VLCIntf, "macosx-autoplay" );
567 if ([o_desired_type isEqualToString:NSFilenamesPboardType])
570 NSArray *o_array = [NSArray array];
571 NSArray *o_values = [[o_paste propertyListForType: NSFilenamesPboardType]
572 sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
574 for( i = 0; i < (int)[o_values count]; i++)
577 o_dic = [NSDictionary dictionaryWithObject:[o_values objectAtIndex:i] forKey:@"ITEM_URL"];
578 o_array = [o_array arrayByAddingObject: o_dic];
581 [[[VLCMain sharedInstance] getPlaylist] appendArray: o_array atPos: -1 enqueue:NO];
583 [[[VLCMain sharedInstance] getPlaylist] appendArray: o_array atPos: -1 enqueue:YES];
587 [self setNeedsDisplay:YES];
591 - (void)concludeDragOperation:(id <NSDraggingInfo>)sender
593 [self setNeedsDisplay:YES];
599 /*****************************************************************************
601 *****************************************************************************/
602 @implementation MPSlider
604 void _drawKnobInRect(NSRect knobRect)
606 // Center knob in given rect
607 knobRect.origin.x += (int)((float)(knobRect.size.width - 7)/2.0);
608 knobRect.origin.y += (int)((float)(knobRect.size.height - 7)/2.0);
611 NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 3, knobRect.origin.y + 6, 1, 1), NSCompositeSourceOver);
612 NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 2, knobRect.origin.y + 5, 3, 1), NSCompositeSourceOver);
613 NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 1, knobRect.origin.y + 4, 5, 1), NSCompositeSourceOver);
614 NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 0, knobRect.origin.y + 3, 7, 1), NSCompositeSourceOver);
615 NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 1, knobRect.origin.y + 2, 5, 1), NSCompositeSourceOver);
616 NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 2, knobRect.origin.y + 1, 3, 1), NSCompositeSourceOver);
617 NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 3, knobRect.origin.y + 0, 1, 1), NSCompositeSourceOver);
620 void _drawFrameInRect(NSRect frameRect)
623 NSRectFillUsingOperation(NSMakeRect(frameRect.origin.x, frameRect.origin.y, frameRect.size.width, 1), NSCompositeSourceOver);
624 NSRectFillUsingOperation(NSMakeRect(frameRect.origin.x, frameRect.origin.y + frameRect.size.height-1, frameRect.size.width, 1), NSCompositeSourceOver);
625 NSRectFillUsingOperation(NSMakeRect(frameRect.origin.x, frameRect.origin.y, 1, frameRect.size.height), NSCompositeSourceOver);
626 NSRectFillUsingOperation(NSMakeRect(frameRect.origin.x+frameRect.size.width-1, frameRect.origin.y, 1, frameRect.size.height), NSCompositeSourceOver);
629 - (void)drawRect:(NSRect)rect
631 // Draw default to make sure the slider behaves correctly
632 [[NSGraphicsContext currentContext] saveGraphicsState];
633 NSRectClip(NSZeroRect);
634 [super drawRect:rect];
635 [[NSGraphicsContext currentContext] restoreGraphicsState];
638 rect = [self bounds];
639 int diff = (int)(([[self cell] knobThickness] - 7.0)/2.0) - 1;
640 rect.origin.x += diff-1;
641 rect.origin.y += diff;
642 rect.size.width -= 2*diff-2;
643 rect.size.height -= 2*diff;
646 NSRect knobRect = [[self cell] knobRectFlipped:NO];
647 [[[NSColor blackColor] colorWithAlphaComponent:0.6] set];
648 _drawFrameInRect(rect);
649 _drawKnobInRect(knobRect);
652 [[[NSColor blackColor] colorWithAlphaComponent:0.1] set];
657 _drawFrameInRect(rect);
658 _drawKnobInRect(knobRect);
664 /*****************************************************************************
666 *****************************************************************************/
668 @implementation ITSlider
672 if ([[self cell] class] != [ITSliderCell class]) {
674 NSSliderCell *oldCell = [self cell];
675 NSSliderCell *newCell = [[[ITSliderCell alloc] init] autorelease];
676 [newCell setTag:[oldCell tag]];
677 [newCell setTarget:[oldCell target]];
678 [newCell setAction:[oldCell action]];
679 [newCell setControlSize:[oldCell controlSize]];
680 [newCell setType:[oldCell type]];
681 [newCell setState:[oldCell state]];
682 [newCell setAllowsTickMarkValuesOnly:[oldCell allowsTickMarkValuesOnly]];
683 [newCell setAltIncrementValue:[oldCell altIncrementValue]];
684 [newCell setControlTint:[oldCell controlTint]];
685 [newCell setKnobThickness:[oldCell knobThickness]];
686 [newCell setMaxValue:[oldCell maxValue]];
687 [newCell setMinValue:[oldCell minValue]];
688 [newCell setDoubleValue:[oldCell doubleValue]];
689 [newCell setNumberOfTickMarks:[oldCell numberOfTickMarks]];
690 [newCell setEditable:[oldCell isEditable]];
691 [newCell setEnabled:[oldCell isEnabled]];
692 [newCell setEntryType:[oldCell entryType]];
693 [newCell setHighlighted:[oldCell isHighlighted]];
694 [newCell setTickMarkPosition:[oldCell tickMarkPosition]];
695 [self setCell:newCell];
701 /*****************************************************************************
703 *****************************************************************************/
704 @implementation ITSliderCell
709 _knobOff = [NSImage imageNamed:@"volumeslider_normal"];
710 [self controlTintChanged];
711 [[NSNotificationCenter defaultCenter] addObserver: self
712 selector: @selector( controlTintChanged )
713 name: NSControlTintDidChangeNotification
715 b_mouse_down = FALSE;
719 - (void)controlTintChanged
721 if( [NSColor currentControlTint] == NSGraphiteControlTint )
722 _knobOn = [NSImage imageNamed:@"volumeslider_graphite"];
724 _knobOn = [NSImage imageNamed:@"volumeslider_blue"];
729 [[NSNotificationCenter defaultCenter] removeObserver: self];
735 - (void)drawKnob:(NSRect)knob_rect
744 [[self controlView] lockFocus];
745 [knob compositeToPoint:NSMakePoint( knob_rect.origin.x + 1,
746 knob_rect.origin.y + knob_rect.size.height -2 )
747 operation:NSCompositeSourceOver];
748 [[self controlView] unlockFocus];
751 - (void)stopTracking:(NSPoint)lastPoint at:(NSPoint)stopPoint inView:
752 (NSView *)controlView mouseIsUp:(BOOL)flag
756 [super stopTracking:lastPoint at:stopPoint inView:controlView mouseIsUp:flag];
759 - (BOOL)startTrackingAt:(NSPoint)startPoint inView:(NSView *)controlView
763 return [super startTrackingAt:startPoint inView:controlView];