1 /*****************************************************************************
2 * misc.m: code not specific to vlc
3 *****************************************************************************
4 * Copyright (C) 2003-2007 the VideoLAN team
7 * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 #include <Cocoa/Cocoa.h>
26 #include "intf.h" /* VLCApplication */
31 /*****************************************************************************
32 * NSAnimation (VLCAdditions)
34 * Missing extension to NSAnimation
35 *****************************************************************************/
37 @implementation NSAnimation (VLCAdditions)
38 /* fake class attributes */
39 static NSMapTable *VLCAdditions_userInfo = NULL;
43 /* init our fake object attribute */
44 VLCAdditions_userInfo = NSCreateMapTable(NSNonRetainedObjectMapKeyCallBacks, NSObjectMapValueCallBacks, 16);
49 NSMapRemove(VLCAdditions_userInfo, self);
53 - (void)setUserInfo: (void *)userInfo
55 NSMapInsert(VLCAdditions_userInfo, self, (void*)userInfo);
60 return NSMapGet(VLCAdditions_userInfo, self);
64 /*****************************************************************************
65 * NSScreen (VLCAdditions)
67 * Missing extension to NSScreen
68 *****************************************************************************/
70 @implementation NSScreen (VLCAdditions)
72 static NSMutableArray *blackoutWindows = NULL;
76 /* init our fake object attribute */
77 blackoutWindows = [[NSMutableArray alloc] initWithCapacity:1];
80 + (NSScreen *)screenWithDisplayID: (CGDirectDisplayID)displayID
84 for( i = 0; i < [[NSScreen screens] count]; i++ )
86 NSScreen *screen = [[NSScreen screens] objectAtIndex: i];
87 if([screen displayID] == displayID)
95 return ([self displayID] == [[[NSScreen screens] objectAtIndex:0] displayID]);
98 - (CGDirectDisplayID)displayID
100 return (CGDirectDisplayID)_screenNumber;
103 - (void)blackoutOtherScreens
107 /* Free our previous blackout window (follow blackoutWindow alloc strategy) */
108 [blackoutWindows makeObjectsPerformSelector:@selector(close)];
109 [blackoutWindows removeAllObjects];
112 for(i = 0; i < [[NSScreen screens] count]; i++)
114 VLCWindow *blackoutWindow;
115 NSScreen *screen = [[NSScreen screens] objectAtIndex: i];
118 /* blackoutWindow alloc strategy
119 - The NSMutableArray blackoutWindows has the blackoutWindow references
120 - blackoutOtherDisplays is responsible for alloc/releasing its Windows
122 blackoutWindow = [[VLCWindow alloc] initWithContentRect: [screen frame] styleMask: NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO];
123 [blackoutWindow setBackgroundColor:[NSColor blackColor]];
124 [blackoutWindow setLevel: NSFloatingWindowLevel]; /* Disappear when Expose is triggered */
126 [blackoutWindow orderFront: self animate: YES];
128 [blackoutWindows addObject: blackoutWindow];
129 [blackoutWindow release];
133 + (void)unblackoutScreens
137 for(i = 0; i < [blackoutWindows count]; i++)
139 VLCWindow *blackoutWindow = [blackoutWindows objectAtIndex: i];
140 [blackoutWindow closeAndAnimate: YES];
146 /*****************************************************************************
149 * Missing extension to NSWindow
150 *****************************************************************************/
152 @implementation VLCWindow
153 - (id)initWithContentRect:(NSRect)contentRect styleMask:(unsigned int)styleMask
154 backing:(NSBackingStoreType)backingType defer:(BOOL)flag
156 self = [super initWithContentRect:contentRect styleMask:styleMask backing:backingType defer:flag];
158 b_isset_canBecomeKeyWindow = NO;
161 - (void)setCanBecomeKeyWindow: (BOOL)canBecomeKey
163 b_isset_canBecomeKeyWindow = YES;
164 b_canBecomeKeyWindow = canBecomeKey;
167 - (BOOL)canBecomeKeyWindow
169 if(b_isset_canBecomeKeyWindow)
170 return b_canBecomeKeyWindow;
172 return [super canBecomeKeyWindow];
175 - (void)closeAndAnimate: (BOOL)animate
179 if (!animate || MACOS_VERSION < 10.4f)
185 invoc = [NSInvocation invocationWithMethodSignature:[super methodSignatureForSelector:@selector(close)]];
186 [invoc setTarget: (id)super];
188 if (![self isVisible] || [self alphaValue] == 0.0)
194 [self orderOut: self animate: YES callback: invoc];
197 - (void)orderOut: (id)sender animate: (BOOL)animate
199 NSInvocation *invoc = [NSInvocation invocationWithMethodSignature:[super methodSignatureForSelector:@selector(orderOut:)]];
200 [invoc setTarget: (id)super];
201 [invoc setArgument: sender atIndex: 0];
202 [self orderOut: sender animate: animate callback: invoc];
205 - (void)orderOut: (id)sender animate: (BOOL)animate callback:(NSInvocation *)callback
207 NSViewAnimation *anim;
208 NSViewAnimation *current_anim;
209 NSMutableDictionary *dict;
211 if (!animate || MACOS_VERSION < 10.4f)
213 [self orderOut: sender];
217 dict = [[NSMutableDictionary alloc] initWithCapacity:2];
219 [dict setObject:self forKey:NSViewAnimationTargetKey];
221 [dict setObject:NSViewAnimationFadeOutEffect forKey:NSViewAnimationEffectKey];
222 anim = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects:dict, nil]];
225 [anim setAnimationBlockingMode:NSAnimationNonblocking];
226 [anim setDuration:0.9];
227 [anim setFrameRate:30];
228 [anim setUserInfo: callback];
230 @synchronized(self) {
231 current_anim = self->animation;
233 if ([[[current_anim viewAnimations] objectAtIndex:0] objectForKey: NSViewAnimationEffectKey] == NSViewAnimationFadeOutEffect && [current_anim isAnimating])
241 [current_anim stopAnimation];
242 [anim setCurrentProgress:1.0-[current_anim currentProgress]];
243 [current_anim release];
246 [anim setCurrentProgress:1.0 - [self alphaValue]];
247 self->animation = anim;
248 [self setDelegate: self];
249 [anim startAnimation];
254 - (void)orderFront: (id)sender animate: (BOOL)animate
256 NSViewAnimation *anim;
257 NSViewAnimation *current_anim;
258 NSMutableDictionary *dict;
260 if (!animate || MACOS_VERSION < 10.4f)
262 [super orderFront: sender];
263 [self setAlphaValue: 1.0];
267 if (![self isVisible])
269 [self setAlphaValue: 0.0];
270 [super orderFront: sender];
272 else if ([self alphaValue] == 1.0)
274 [super orderFront: self];
278 dict = [[NSMutableDictionary alloc] initWithCapacity:2];
280 [dict setObject:self forKey:NSViewAnimationTargetKey];
282 [dict setObject:NSViewAnimationFadeInEffect forKey:NSViewAnimationEffectKey];
283 anim = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects:dict, nil]];
286 [anim setAnimationBlockingMode:NSAnimationNonblocking];
287 [anim setDuration:0.5];
288 [anim setFrameRate:30];
290 @synchronized(self) {
291 current_anim = self->animation;
293 if ([[[current_anim viewAnimations] objectAtIndex:0] objectForKey: NSViewAnimationEffectKey] == NSViewAnimationFadeInEffect && [current_anim isAnimating])
301 [current_anim stopAnimation];
302 [anim setCurrentProgress:1.0 - [current_anim currentProgress]];
303 [current_anim release];
306 [anim setCurrentProgress:[self alphaValue]];
307 self->animation = anim;
308 [self setDelegate: self];
309 [self orderFront: sender];
310 [anim startAnimation];
315 - (void)animationDidEnd:(NSAnimation*)anim
317 if ([self alphaValue] <= 0.0)
319 NSInvocation * invoc;
320 [super orderOut: nil];
321 [self setAlphaValue: 1.0];
322 if ((invoc = [anim userInfo]))
328 /*****************************************************************************
329 * VLCControllerWindow
330 *****************************************************************************/
332 @implementation VLCControllerWindow
334 - (id)initWithContentRect:(NSRect)contentRect styleMask:(unsigned int)styleMask
335 backing:(NSBackingStoreType)backingType defer:(BOOL)flag
337 self = [super initWithContentRect:contentRect styleMask:styleMask //& ~NSTitledWindowMask
338 backing:backingType defer:flag];
340 [[VLCMain sharedInstance] updateTogglePlaylistState];
345 - (BOOL)performKeyEquivalent:(NSEvent *)o_event
347 /* We indeed want to prioritize Cocoa key equivalent against libvlc,
348 so we perform the menu equivalent now. */
349 if([[NSApp mainMenu] performKeyEquivalent:o_event])
352 return [[VLCMain sharedInstance] hasDefinedShortcutKey:o_event] ||
353 [(VLCControls *)[[VLCMain sharedInstance] getControls] keyEvent:o_event];
360 /*****************************************************************************
362 *****************************************************************************/
364 @implementation VLCControllerView
368 [self unregisterDraggedTypes];
375 /* dealloc doesn't get called on 10.5 if GC is enabled, so we need to provide the basic functionality here */
376 [self unregisterDraggedTypes];
383 [self registerForDraggedTypes:[NSArray arrayWithObjects:NSTIFFPboardType,
384 NSFilenamesPboardType, nil]];
387 - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
389 if ((NSDragOperationGeneric & [sender draggingSourceOperationMask])
390 == NSDragOperationGeneric)
392 return NSDragOperationGeneric;
396 return NSDragOperationNone;
400 - (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
405 - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
407 NSPasteboard *o_paste = [sender draggingPasteboard];
408 NSArray *o_types = [NSArray arrayWithObjects: NSFilenamesPboardType, nil];
409 NSString *o_desired_type = [o_paste availableTypeFromArray:o_types];
410 NSData *o_carried_data = [o_paste dataForType:o_desired_type];
414 if ([o_desired_type isEqualToString:NSFilenamesPboardType])
417 NSArray *o_array = [NSArray array];
418 NSArray *o_values = [[o_paste propertyListForType: NSFilenamesPboardType]
419 sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
421 for( i = 0; i < (int)[o_values count]; i++)
424 o_dic = [NSDictionary dictionaryWithObject:[o_values objectAtIndex:i] forKey:@"ITEM_URL"];
425 o_array = [o_array arrayByAddingObject: o_dic];
427 [(VLCPlaylist *)[[VLCMain sharedInstance] getPlaylist] appendArray: o_array atPos: -1 enqueue:NO];
431 [self setNeedsDisplay:YES];
435 - (void)concludeDragOperation:(id <NSDraggingInfo>)sender
437 [self setNeedsDisplay:YES];
442 /*****************************************************************************
443 * VLBrushedMetalImageView
444 *****************************************************************************/
446 @implementation VLBrushedMetalImageView
448 - (BOOL)mouseDownCanMoveWindow
455 [self unregisterDraggedTypes];
462 /* dealloc doesn't get called on 10.5 if GC is enabled, so we need to provide the basic functionality here */
463 [self unregisterDraggedTypes];
470 [self registerForDraggedTypes:[NSArray arrayWithObjects:NSTIFFPboardType,
471 NSFilenamesPboardType, nil]];
474 - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
476 if ((NSDragOperationGeneric & [sender draggingSourceOperationMask])
477 == NSDragOperationGeneric)
479 return NSDragOperationGeneric;
483 return NSDragOperationNone;
487 - (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
492 - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
494 NSPasteboard *o_paste = [sender draggingPasteboard];
495 NSArray *o_types = [NSArray arrayWithObjects: NSFilenamesPboardType, nil];
496 NSString *o_desired_type = [o_paste availableTypeFromArray:o_types];
497 NSData *o_carried_data = [o_paste dataForType:o_desired_type];
498 BOOL b_autoplay = config_GetInt( VLCIntf, "macosx-autoplay" );
502 if ([o_desired_type isEqualToString:NSFilenamesPboardType])
505 NSArray *o_array = [NSArray array];
506 NSArray *o_values = [[o_paste propertyListForType: NSFilenamesPboardType]
507 sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
509 for( i = 0; i < (int)[o_values count]; i++)
512 o_dic = [NSDictionary dictionaryWithObject:[o_values objectAtIndex:i] forKey:@"ITEM_URL"];
513 o_array = [o_array arrayByAddingObject: o_dic];
516 [[[VLCMain sharedInstance] getPlaylist] appendArray: o_array atPos: -1 enqueue:NO];
518 [[[VLCMain sharedInstance] getPlaylist] appendArray: o_array atPos: -1 enqueue:YES];
522 [self setNeedsDisplay:YES];
526 - (void)concludeDragOperation:(id <NSDraggingInfo>)sender
528 [self setNeedsDisplay:YES];
534 /*****************************************************************************
536 *****************************************************************************/
537 @implementation MPSlider
539 void _drawKnobInRect(NSRect knobRect)
541 // Center knob in given rect
542 knobRect.origin.x += (int)((float)(knobRect.size.width - 7)/2.0);
543 knobRect.origin.y += (int)((float)(knobRect.size.height - 7)/2.0);
546 NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 3, knobRect.origin.y + 6, 1, 1), NSCompositeSourceOver);
547 NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 2, knobRect.origin.y + 5, 3, 1), NSCompositeSourceOver);
548 NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 1, knobRect.origin.y + 4, 5, 1), NSCompositeSourceOver);
549 NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 0, knobRect.origin.y + 3, 7, 1), NSCompositeSourceOver);
550 NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 1, knobRect.origin.y + 2, 5, 1), NSCompositeSourceOver);
551 NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 2, knobRect.origin.y + 1, 3, 1), NSCompositeSourceOver);
552 NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 3, knobRect.origin.y + 0, 1, 1), NSCompositeSourceOver);
555 void _drawFrameInRect(NSRect frameRect)
558 NSRectFillUsingOperation(NSMakeRect(frameRect.origin.x, frameRect.origin.y, frameRect.size.width, 1), NSCompositeSourceOver);
559 NSRectFillUsingOperation(NSMakeRect(frameRect.origin.x, frameRect.origin.y + frameRect.size.height-1, frameRect.size.width, 1), NSCompositeSourceOver);
560 NSRectFillUsingOperation(NSMakeRect(frameRect.origin.x, frameRect.origin.y, 1, frameRect.size.height), NSCompositeSourceOver);
561 NSRectFillUsingOperation(NSMakeRect(frameRect.origin.x+frameRect.size.width-1, frameRect.origin.y, 1, frameRect.size.height), NSCompositeSourceOver);
564 - (void)drawRect:(NSRect)rect
566 // Draw default to make sure the slider behaves correctly
567 [[NSGraphicsContext currentContext] saveGraphicsState];
568 NSRectClip(NSZeroRect);
569 [super drawRect:rect];
570 [[NSGraphicsContext currentContext] restoreGraphicsState];
573 rect = [self bounds];
574 int diff = (int)(([[self cell] knobThickness] - 7.0)/2.0) - 1;
575 rect.origin.x += diff-1;
576 rect.origin.y += diff;
577 rect.size.width -= 2*diff-2;
578 rect.size.height -= 2*diff;
581 NSRect knobRect = [[self cell] knobRectFlipped:NO];
582 [[[NSColor blackColor] colorWithAlphaComponent:0.6] set];
583 _drawFrameInRect(rect);
584 _drawKnobInRect(knobRect);
587 [[[NSColor blackColor] colorWithAlphaComponent:0.1] set];
592 _drawFrameInRect(rect);
593 _drawKnobInRect(knobRect);
599 /*****************************************************************************
601 *****************************************************************************/
603 @implementation ITSlider
607 if ([[self cell] class] != [ITSliderCell class]) {
609 NSSliderCell *oldCell = [self cell];
610 NSSliderCell *newCell = [[[ITSliderCell alloc] init] autorelease];
611 [newCell setTag:[oldCell tag]];
612 [newCell setTarget:[oldCell target]];
613 [newCell setAction:[oldCell action]];
614 [newCell setControlSize:[oldCell controlSize]];
615 [newCell setType:[oldCell type]];
616 [newCell setState:[oldCell state]];
617 [newCell setAllowsTickMarkValuesOnly:[oldCell allowsTickMarkValuesOnly]];
618 [newCell setAltIncrementValue:[oldCell altIncrementValue]];
619 [newCell setControlTint:[oldCell controlTint]];
620 [newCell setKnobThickness:[oldCell knobThickness]];
621 [newCell setMaxValue:[oldCell maxValue]];
622 [newCell setMinValue:[oldCell minValue]];
623 [newCell setDoubleValue:[oldCell doubleValue]];
624 [newCell setNumberOfTickMarks:[oldCell numberOfTickMarks]];
625 [newCell setEditable:[oldCell isEditable]];
626 [newCell setEnabled:[oldCell isEnabled]];
627 [newCell setEntryType:[oldCell entryType]];
628 [newCell setHighlighted:[oldCell isHighlighted]];
629 [newCell setTickMarkPosition:[oldCell tickMarkPosition]];
630 [self setCell:newCell];
636 /*****************************************************************************
638 *****************************************************************************/
639 @implementation ITSliderCell
644 _knobOff = [[NSImage imageNamed:@"volumeslider_normal"] retain];
645 _knobOn = [[NSImage imageNamed:@"volumeslider_blue"] retain];
646 b_mouse_down = FALSE;
657 - (void)drawKnob:(NSRect)knob_rect
666 [[self controlView] lockFocus];
667 [knob compositeToPoint:NSMakePoint( knob_rect.origin.x + 1,
668 knob_rect.origin.y + knob_rect.size.height -2 )
669 operation:NSCompositeSourceOver];
670 [[self controlView] unlockFocus];
673 - (void)stopTracking:(NSPoint)lastPoint at:(NSPoint)stopPoint inView:
674 (NSView *)controlView mouseIsUp:(BOOL)flag
678 [super stopTracking:lastPoint at:stopPoint inView:controlView mouseIsUp:flag];
681 - (BOOL)startTrackingAt:(NSPoint)startPoint inView:(NSView *)controlView
685 return [super startTrackingAt:startPoint inView:controlView];