1 /*****************************************************************************
2 * misc.m: code not specific to vlc
3 *****************************************************************************
4 * Copyright (C) 2003-2008 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:(unsigned int)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
251 if (!animate || MACOS_VERSION < 10.4f)
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;
283 if (!animate || MACOS_VERSION < 10.4f)
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;
332 if (!animate || MACOS_VERSION < 10.4f)
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:(unsigned int)styleMask
407 backing:(NSBackingStoreType)backingType defer:(BOOL)flag
409 self = [super initWithContentRect:contentRect styleMask:styleMask //& ~NSTitledWindowMask
410 backing:backingType defer:flag];
412 [[VLCMain sharedInstance] updateTogglePlaylistState];
417 - (BOOL)performKeyEquivalent:(NSEvent *)o_event
419 /* We indeed want to prioritize Cocoa key equivalent against libvlc,
420 so we perform the menu equivalent now. */
421 if([[NSApp mainMenu] performKeyEquivalent:o_event])
424 return [[VLCMain sharedInstance] hasDefinedShortcutKey:o_event] ||
425 [(VLCControls *)[[VLCMain sharedInstance] getControls] keyEvent:o_event];
432 /*****************************************************************************
434 *****************************************************************************/
436 @implementation VLCControllerView
440 [self unregisterDraggedTypes];
446 [self registerForDraggedTypes:[NSArray arrayWithObjects:NSTIFFPboardType,
447 NSFilenamesPboardType, nil]];
450 - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
452 if ((NSDragOperationGeneric & [sender draggingSourceOperationMask])
453 == NSDragOperationGeneric)
455 return NSDragOperationGeneric;
459 return NSDragOperationNone;
463 - (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
468 - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
470 NSPasteboard *o_paste = [sender draggingPasteboard];
471 NSArray *o_types = [NSArray arrayWithObjects: NSFilenamesPboardType, nil];
472 NSString *o_desired_type = [o_paste availableTypeFromArray:o_types];
473 NSData *o_carried_data = [o_paste dataForType:o_desired_type];
477 if ([o_desired_type isEqualToString:NSFilenamesPboardType])
480 NSArray *o_array = [NSArray array];
481 NSArray *o_values = [[o_paste propertyListForType: NSFilenamesPboardType]
482 sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
484 for( i = 0; i < (int)[o_values count]; i++)
487 o_dic = [NSDictionary dictionaryWithObject:[o_values objectAtIndex:i] forKey:@"ITEM_URL"];
488 o_array = [o_array arrayByAddingObject: o_dic];
490 [(VLCPlaylist *)[[VLCMain sharedInstance] getPlaylist] appendArray: o_array atPos: -1 enqueue:NO];
494 [self setNeedsDisplay:YES];
498 - (void)concludeDragOperation:(id <NSDraggingInfo>)sender
500 [self setNeedsDisplay:YES];
505 /*****************************************************************************
506 * VLBrushedMetalImageView
507 *****************************************************************************/
509 @implementation VLBrushedMetalImageView
511 - (BOOL)mouseDownCanMoveWindow
518 [self unregisterDraggedTypes];
524 [self registerForDraggedTypes:[NSArray arrayWithObjects:NSTIFFPboardType,
525 NSFilenamesPboardType, nil]];
528 - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
530 if ((NSDragOperationGeneric & [sender draggingSourceOperationMask])
531 == NSDragOperationGeneric)
533 return NSDragOperationGeneric;
537 return NSDragOperationNone;
541 - (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
546 - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
548 NSPasteboard *o_paste = [sender draggingPasteboard];
549 NSArray *o_types = [NSArray arrayWithObjects: NSFilenamesPboardType, nil];
550 NSString *o_desired_type = [o_paste availableTypeFromArray:o_types];
551 NSData *o_carried_data = [o_paste dataForType:o_desired_type];
552 BOOL b_autoplay = config_GetInt( VLCIntf, "macosx-autoplay" );
556 if ([o_desired_type isEqualToString:NSFilenamesPboardType])
559 NSArray *o_array = [NSArray array];
560 NSArray *o_values = [[o_paste propertyListForType: NSFilenamesPboardType]
561 sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
563 for( i = 0; i < (int)[o_values count]; i++)
566 o_dic = [NSDictionary dictionaryWithObject:[o_values objectAtIndex:i] forKey:@"ITEM_URL"];
567 o_array = [o_array arrayByAddingObject: o_dic];
570 [[[VLCMain sharedInstance] getPlaylist] appendArray: o_array atPos: -1 enqueue:NO];
572 [[[VLCMain sharedInstance] getPlaylist] appendArray: o_array atPos: -1 enqueue:YES];
576 [self setNeedsDisplay:YES];
580 - (void)concludeDragOperation:(id <NSDraggingInfo>)sender
582 [self setNeedsDisplay:YES];
588 /*****************************************************************************
590 *****************************************************************************/
591 @implementation MPSlider
593 void _drawKnobInRect(NSRect knobRect)
595 // Center knob in given rect
596 knobRect.origin.x += (int)((float)(knobRect.size.width - 7)/2.0);
597 knobRect.origin.y += (int)((float)(knobRect.size.height - 7)/2.0);
600 NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 3, knobRect.origin.y + 6, 1, 1), NSCompositeSourceOver);
601 NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 2, knobRect.origin.y + 5, 3, 1), NSCompositeSourceOver);
602 NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 1, knobRect.origin.y + 4, 5, 1), NSCompositeSourceOver);
603 NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 0, knobRect.origin.y + 3, 7, 1), NSCompositeSourceOver);
604 NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 1, knobRect.origin.y + 2, 5, 1), NSCompositeSourceOver);
605 NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 2, knobRect.origin.y + 1, 3, 1), NSCompositeSourceOver);
606 NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 3, knobRect.origin.y + 0, 1, 1), NSCompositeSourceOver);
609 void _drawFrameInRect(NSRect frameRect)
612 NSRectFillUsingOperation(NSMakeRect(frameRect.origin.x, frameRect.origin.y, frameRect.size.width, 1), NSCompositeSourceOver);
613 NSRectFillUsingOperation(NSMakeRect(frameRect.origin.x, frameRect.origin.y + frameRect.size.height-1, frameRect.size.width, 1), NSCompositeSourceOver);
614 NSRectFillUsingOperation(NSMakeRect(frameRect.origin.x, frameRect.origin.y, 1, frameRect.size.height), NSCompositeSourceOver);
615 NSRectFillUsingOperation(NSMakeRect(frameRect.origin.x+frameRect.size.width-1, frameRect.origin.y, 1, frameRect.size.height), NSCompositeSourceOver);
618 - (void)drawRect:(NSRect)rect
620 // Draw default to make sure the slider behaves correctly
621 [[NSGraphicsContext currentContext] saveGraphicsState];
622 NSRectClip(NSZeroRect);
623 [super drawRect:rect];
624 [[NSGraphicsContext currentContext] restoreGraphicsState];
627 rect = [self bounds];
628 int diff = (int)(([[self cell] knobThickness] - 7.0)/2.0) - 1;
629 rect.origin.x += diff-1;
630 rect.origin.y += diff;
631 rect.size.width -= 2*diff-2;
632 rect.size.height -= 2*diff;
635 NSRect knobRect = [[self cell] knobRectFlipped:NO];
636 [[[NSColor blackColor] colorWithAlphaComponent:0.6] set];
637 _drawFrameInRect(rect);
638 _drawKnobInRect(knobRect);
641 [[[NSColor blackColor] colorWithAlphaComponent:0.1] set];
646 _drawFrameInRect(rect);
647 _drawKnobInRect(knobRect);
653 /*****************************************************************************
655 *****************************************************************************/
657 @implementation ITSlider
661 if ([[self cell] class] != [ITSliderCell class]) {
663 NSSliderCell *oldCell = [self cell];
664 NSSliderCell *newCell = [[[ITSliderCell alloc] init] autorelease];
665 [newCell setTag:[oldCell tag]];
666 [newCell setTarget:[oldCell target]];
667 [newCell setAction:[oldCell action]];
668 [newCell setControlSize:[oldCell controlSize]];
669 [newCell setType:[oldCell type]];
670 [newCell setState:[oldCell state]];
671 [newCell setAllowsTickMarkValuesOnly:[oldCell allowsTickMarkValuesOnly]];
672 [newCell setAltIncrementValue:[oldCell altIncrementValue]];
673 [newCell setControlTint:[oldCell controlTint]];
674 [newCell setKnobThickness:[oldCell knobThickness]];
675 [newCell setMaxValue:[oldCell maxValue]];
676 [newCell setMinValue:[oldCell minValue]];
677 [newCell setDoubleValue:[oldCell doubleValue]];
678 [newCell setNumberOfTickMarks:[oldCell numberOfTickMarks]];
679 [newCell setEditable:[oldCell isEditable]];
680 [newCell setEnabled:[oldCell isEnabled]];
681 [newCell setEntryType:[oldCell entryType]];
682 [newCell setHighlighted:[oldCell isHighlighted]];
683 [newCell setTickMarkPosition:[oldCell tickMarkPosition]];
684 [self setCell:newCell];
690 /*****************************************************************************
692 *****************************************************************************/
693 @implementation ITSliderCell
698 _knobOff = [NSImage imageNamed:@"volumeslider_normal"];
699 [self controlTintChanged];
700 [[NSNotificationCenter defaultCenter] addObserver: self
701 selector: @selector( controlTintChanged )
702 name: NSControlTintDidChangeNotification
704 b_mouse_down = FALSE;
708 - (void)controlTintChanged
710 if( [NSColor currentControlTint] == NSGraphiteControlTint )
711 _knobOn = [NSImage imageNamed:@"volumeslider_graphite"];
713 _knobOn = [NSImage imageNamed:@"volumeslider_blue"];
718 [[NSNotificationCenter defaultCenter] removeObserver: self];
724 - (void)drawKnob:(NSRect)knob_rect
733 [[self controlView] lockFocus];
734 [knob compositeToPoint:NSMakePoint( knob_rect.origin.x + 1,
735 knob_rect.origin.y + knob_rect.size.height -2 )
736 operation:NSCompositeSourceOver];
737 [[self controlView] unlockFocus];
740 - (void)stopTracking:(NSPoint)lastPoint at:(NSPoint)stopPoint inView:
741 (NSView *)controlView mouseIsUp:(BOOL)flag
745 [super stopTracking:lastPoint at:stopPoint inView:controlView mouseIsUp:flag];
748 - (BOOL)startTrackingAt:(NSPoint)startPoint inView:(NSView *)controlView
752 return [super startTrackingAt:startPoint inView:controlView];