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)
93 - (CGDirectDisplayID)displayID
95 return (CGDirectDisplayID)_screenNumber;
98 - (void)blackoutOtherScreens
102 /* Free our previous blackout window (follow blackoutWindow alloc strategy) */
103 [blackoutWindows makeObjectsPerformSelector:@selector(close)];
104 [blackoutWindows removeAllObjects];
107 for(i = 0; i < [[NSScreen screens] count]; i++)
109 VLCWindow *blackoutWindow;
110 NSScreen *screen = [[NSScreen screens] objectAtIndex: i];
113 /* blackoutWindow alloc strategy
114 - The NSMutableArray blackoutWindows has the blackoutWindow references
115 - blackoutOtherDisplays is responsible for alloc/releasing its Windows
117 blackoutWindow = [[VLCWindow alloc] initWithContentRect: [screen frame] styleMask: NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO];
118 [blackoutWindow setBackgroundColor:[NSColor blackColor]];
119 [blackoutWindow setLevel: NSFloatingWindowLevel]; /* Disappear when Expose is triggered */
121 [blackoutWindow orderFront: self animate: YES];
123 [blackoutWindows addObject: blackoutWindow];
124 [blackoutWindow release];
128 + (void)unblackoutScreens
132 for(i = 0; i < [blackoutWindows count]; i++)
134 VLCWindow *blackoutWindow = [blackoutWindows objectAtIndex: i];
135 [blackoutWindow closeAndAnimate: YES];
141 /*****************************************************************************
144 * Missing extension to NSWindow
145 *****************************************************************************/
147 @implementation VLCWindow
148 - (id)initWithContentRect:(NSRect)contentRect styleMask:(unsigned int)styleMask
149 backing:(NSBackingStoreType)backingType defer:(BOOL)flag
151 self = [super initWithContentRect:contentRect styleMask:styleMask backing:backingType defer:flag];
153 b_isset_canBecomeKeyWindow = NO;
156 - (void)setCanBecomeKeyWindow: (BOOL)canBecomeKey
158 b_isset_canBecomeKeyWindow = YES;
159 b_canBecomeKeyWindow = canBecomeKey;
162 - (BOOL)canBecomeKeyWindow
164 if(b_isset_canBecomeKeyWindow)
165 return b_canBecomeKeyWindow;
167 return [super canBecomeKeyWindow];
170 - (void)closeAndAnimate: (BOOL)animate
174 if (!animate || MACOS_VERSION < 10.4f)
180 invoc = [NSInvocation invocationWithMethodSignature:[super methodSignatureForSelector:@selector(close)]];
181 [invoc setTarget: (id)super];
183 if (![self isVisible] || [self alphaValue] == 0.0)
189 [self orderOut: self animate: YES callback: invoc];
192 - (void)orderOut: (id)sender animate: (BOOL)animate
194 NSInvocation *invoc = [NSInvocation invocationWithMethodSignature:[super methodSignatureForSelector:@selector(orderOut:)]];
195 [invoc setTarget: (id)super];
196 [invoc setArgument: sender atIndex: 0];
197 [self orderOut: sender animate: animate callback: invoc];
200 - (void)orderOut: (id)sender animate: (BOOL)animate callback:(NSInvocation *)callback
202 NSViewAnimation *anim;
203 NSViewAnimation *current_anim;
204 NSMutableDictionary *dict;
206 if (!animate || MACOS_VERSION < 10.4f)
208 [self orderOut: sender];
212 dict = [[NSMutableDictionary alloc] initWithCapacity:2];
214 [dict setObject:self forKey:NSViewAnimationTargetKey];
216 [dict setObject:NSViewAnimationFadeOutEffect forKey:NSViewAnimationEffectKey];
217 anim = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects:dict, nil]];
220 [anim setAnimationBlockingMode:NSAnimationNonblocking];
221 [anim setDuration:0.9];
222 [anim setFrameRate:30];
223 [anim setUserInfo: callback];
225 @synchronized(self) {
226 current_anim = self->animation;
228 if ([[[current_anim viewAnimations] objectAtIndex:0] objectForKey: NSViewAnimationEffectKey] == NSViewAnimationFadeOutEffect && [current_anim isAnimating])
236 [current_anim stopAnimation];
237 [anim setCurrentProgress:1.0-[current_anim currentProgress]];
238 [current_anim release];
241 [anim setCurrentProgress:1.0 - [self alphaValue]];
242 self->animation = anim;
243 [self setDelegate: self];
244 [anim startAnimation];
249 - (void)orderFront: (id)sender animate: (BOOL)animate
251 NSViewAnimation *anim;
252 NSViewAnimation *current_anim;
253 NSMutableDictionary *dict;
255 if (!animate || MACOS_VERSION < 10.4f)
257 [super orderFront: sender];
258 [self setAlphaValue: 1.0];
262 if (![self isVisible])
264 [self setAlphaValue: 0.0];
265 [super orderFront: sender];
267 else if ([self alphaValue] == 1.0)
269 [super orderFront: self];
273 dict = [[NSMutableDictionary alloc] initWithCapacity:2];
275 [dict setObject:self forKey:NSViewAnimationTargetKey];
277 [dict setObject:NSViewAnimationFadeInEffect forKey:NSViewAnimationEffectKey];
278 anim = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects:dict, nil]];
281 [anim setAnimationBlockingMode:NSAnimationNonblocking];
282 [anim setDuration:0.5];
283 [anim setFrameRate:30];
285 @synchronized(self) {
286 current_anim = self->animation;
288 if ([[[current_anim viewAnimations] objectAtIndex:0] objectForKey: NSViewAnimationEffectKey] == NSViewAnimationFadeInEffect && [current_anim isAnimating])
296 [current_anim stopAnimation];
297 [anim setCurrentProgress:1.0 - [current_anim currentProgress]];
298 [current_anim release];
301 [anim setCurrentProgress:[self alphaValue]];
302 self->animation = anim;
303 [self setDelegate: self];
304 [self orderFront: sender];
305 [anim startAnimation];
310 - (void)animationDidEnd:(NSAnimation*)anim
312 if ([self alphaValue] <= 0.0)
314 NSInvocation * invoc;
315 [super orderOut: nil];
316 [self setAlphaValue: 1.0];
317 if ((invoc = [anim userInfo]))
323 /*****************************************************************************
324 * VLCControllerWindow
325 *****************************************************************************/
327 @implementation VLCControllerWindow
329 - (id)initWithContentRect:(NSRect)contentRect styleMask:(unsigned int)styleMask
330 backing:(NSBackingStoreType)backingType defer:(BOOL)flag
332 self = [super initWithContentRect:contentRect styleMask:styleMask //& ~NSTitledWindowMask
333 backing:backingType defer:flag];
335 [[VLCMain sharedInstance] updateTogglePlaylistState];
340 - (BOOL)performKeyEquivalent:(NSEvent *)o_event
342 /* We indeed want to prioritize Cocoa key equivalent against libvlc,
343 so we perform the menu equivalent now. */
344 if([[NSApp mainMenu] performKeyEquivalent:o_event])
347 return [[VLCMain sharedInstance] hasDefinedShortcutKey:o_event] ||
348 [(VLCControls *)[[VLCMain sharedInstance] getControls] keyEvent:o_event];
355 /*****************************************************************************
357 *****************************************************************************/
359 @implementation VLCControllerView
363 [self unregisterDraggedTypes];
370 /* dealloc doesn't get called on 10.5 if GC is enabled, so we need to provide the basic functionality here */
371 [self unregisterDraggedTypes];
378 [self registerForDraggedTypes:[NSArray arrayWithObjects:NSTIFFPboardType,
379 NSFilenamesPboardType, nil]];
382 - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
384 if ((NSDragOperationGeneric & [sender draggingSourceOperationMask])
385 == NSDragOperationGeneric)
387 return NSDragOperationGeneric;
391 return NSDragOperationNone;
395 - (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
400 - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
402 NSPasteboard *o_paste = [sender draggingPasteboard];
403 NSArray *o_types = [NSArray arrayWithObjects: NSFilenamesPboardType, nil];
404 NSString *o_desired_type = [o_paste availableTypeFromArray:o_types];
405 NSData *o_carried_data = [o_paste dataForType:o_desired_type];
409 if ([o_desired_type isEqualToString:NSFilenamesPboardType])
412 NSArray *o_array = [NSArray array];
413 NSArray *o_values = [[o_paste propertyListForType: NSFilenamesPboardType]
414 sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
416 for( i = 0; i < (int)[o_values count]; i++)
419 o_dic = [NSDictionary dictionaryWithObject:[o_values objectAtIndex:i] forKey:@"ITEM_URL"];
420 o_array = [o_array arrayByAddingObject: o_dic];
422 [(VLCPlaylist *)[[VLCMain sharedInstance] getPlaylist] appendArray: o_array atPos: -1 enqueue:NO];
426 [self setNeedsDisplay:YES];
430 - (void)concludeDragOperation:(id <NSDraggingInfo>)sender
432 [self setNeedsDisplay:YES];
437 /*****************************************************************************
438 * VLBrushedMetalImageView
439 *****************************************************************************/
441 @implementation VLBrushedMetalImageView
443 - (BOOL)mouseDownCanMoveWindow
450 [self unregisterDraggedTypes];
457 /* dealloc doesn't get called on 10.5 if GC is enabled, so we need to provide the basic functionality here */
458 [self unregisterDraggedTypes];
465 [self registerForDraggedTypes:[NSArray arrayWithObjects:NSTIFFPboardType,
466 NSFilenamesPboardType, nil]];
469 - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
471 if ((NSDragOperationGeneric & [sender draggingSourceOperationMask])
472 == NSDragOperationGeneric)
474 return NSDragOperationGeneric;
478 return NSDragOperationNone;
482 - (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
487 - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
489 NSPasteboard *o_paste = [sender draggingPasteboard];
490 NSArray *o_types = [NSArray arrayWithObjects: NSFilenamesPboardType, nil];
491 NSString *o_desired_type = [o_paste availableTypeFromArray:o_types];
492 NSData *o_carried_data = [o_paste dataForType:o_desired_type];
493 BOOL b_autoplay = config_GetInt( VLCIntf, "macosx-autoplay" );
497 if ([o_desired_type isEqualToString:NSFilenamesPboardType])
500 NSArray *o_array = [NSArray array];
501 NSArray *o_values = [[o_paste propertyListForType: NSFilenamesPboardType]
502 sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
504 for( i = 0; i < (int)[o_values count]; i++)
507 o_dic = [NSDictionary dictionaryWithObject:[o_values objectAtIndex:i] forKey:@"ITEM_URL"];
508 o_array = [o_array arrayByAddingObject: o_dic];
511 [[[VLCMain sharedInstance] getPlaylist] appendArray: o_array atPos: -1 enqueue:NO];
513 [[[VLCMain sharedInstance] getPlaylist] appendArray: o_array atPos: -1 enqueue:YES];
517 [self setNeedsDisplay:YES];
521 - (void)concludeDragOperation:(id <NSDraggingInfo>)sender
523 [self setNeedsDisplay:YES];
529 /*****************************************************************************
531 *****************************************************************************/
532 @implementation MPSlider
534 void _drawKnobInRect(NSRect knobRect)
536 // Center knob in given rect
537 knobRect.origin.x += (int)((float)(knobRect.size.width - 7)/2.0);
538 knobRect.origin.y += (int)((float)(knobRect.size.height - 7)/2.0);
541 NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 3, knobRect.origin.y + 6, 1, 1), NSCompositeSourceOver);
542 NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 2, knobRect.origin.y + 5, 3, 1), NSCompositeSourceOver);
543 NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 1, knobRect.origin.y + 4, 5, 1), NSCompositeSourceOver);
544 NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 0, knobRect.origin.y + 3, 7, 1), NSCompositeSourceOver);
545 NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 1, knobRect.origin.y + 2, 5, 1), NSCompositeSourceOver);
546 NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 2, knobRect.origin.y + 1, 3, 1), NSCompositeSourceOver);
547 NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 3, knobRect.origin.y + 0, 1, 1), NSCompositeSourceOver);
550 void _drawFrameInRect(NSRect frameRect)
553 NSRectFillUsingOperation(NSMakeRect(frameRect.origin.x, frameRect.origin.y, frameRect.size.width, 1), NSCompositeSourceOver);
554 NSRectFillUsingOperation(NSMakeRect(frameRect.origin.x, frameRect.origin.y + frameRect.size.height-1, frameRect.size.width, 1), NSCompositeSourceOver);
555 NSRectFillUsingOperation(NSMakeRect(frameRect.origin.x, frameRect.origin.y, 1, frameRect.size.height), NSCompositeSourceOver);
556 NSRectFillUsingOperation(NSMakeRect(frameRect.origin.x+frameRect.size.width-1, frameRect.origin.y, 1, frameRect.size.height), NSCompositeSourceOver);
559 - (void)drawRect:(NSRect)rect
561 // Draw default to make sure the slider behaves correctly
562 [[NSGraphicsContext currentContext] saveGraphicsState];
563 NSRectClip(NSZeroRect);
564 [super drawRect:rect];
565 [[NSGraphicsContext currentContext] restoreGraphicsState];
568 rect = [self bounds];
569 int diff = (int)(([[self cell] knobThickness] - 7.0)/2.0) - 1;
570 rect.origin.x += diff-1;
571 rect.origin.y += diff;
572 rect.size.width -= 2*diff-2;
573 rect.size.height -= 2*diff;
576 NSRect knobRect = [[self cell] knobRectFlipped:NO];
577 [[[NSColor blackColor] colorWithAlphaComponent:0.6] set];
578 _drawFrameInRect(rect);
579 _drawKnobInRect(knobRect);
582 [[[NSColor blackColor] colorWithAlphaComponent:0.1] set];
587 _drawFrameInRect(rect);
588 _drawKnobInRect(knobRect);
594 /*****************************************************************************
596 *****************************************************************************/
598 @implementation ITSlider
602 if ([[self cell] class] != [ITSliderCell class]) {
604 NSSliderCell *oldCell = [self cell];
605 NSSliderCell *newCell = [[[ITSliderCell alloc] init] autorelease];
606 [newCell setTag:[oldCell tag]];
607 [newCell setTarget:[oldCell target]];
608 [newCell setAction:[oldCell action]];
609 [newCell setControlSize:[oldCell controlSize]];
610 [newCell setType:[oldCell type]];
611 [newCell setState:[oldCell state]];
612 [newCell setAllowsTickMarkValuesOnly:[oldCell allowsTickMarkValuesOnly]];
613 [newCell setAltIncrementValue:[oldCell altIncrementValue]];
614 [newCell setControlTint:[oldCell controlTint]];
615 [newCell setKnobThickness:[oldCell knobThickness]];
616 [newCell setMaxValue:[oldCell maxValue]];
617 [newCell setMinValue:[oldCell minValue]];
618 [newCell setDoubleValue:[oldCell doubleValue]];
619 [newCell setNumberOfTickMarks:[oldCell numberOfTickMarks]];
620 [newCell setEditable:[oldCell isEditable]];
621 [newCell setEnabled:[oldCell isEnabled]];
622 [newCell setEntryType:[oldCell entryType]];
623 [newCell setHighlighted:[oldCell isHighlighted]];
624 [newCell setTickMarkPosition:[oldCell tickMarkPosition]];
625 [self setCell:newCell];
631 /*****************************************************************************
633 *****************************************************************************/
634 @implementation ITSliderCell
639 _knobOff = [[NSImage imageNamed:@"volumeslider_normal"] retain];
640 _knobOn = [[NSImage imageNamed:@"volumeslider_blue"] retain];
641 b_mouse_down = FALSE;
652 - (void)drawKnob:(NSRect)knob_rect
661 [[self controlView] lockFocus];
662 [knob compositeToPoint:NSMakePoint( knob_rect.origin.x + 1,
663 knob_rect.origin.y + knob_rect.size.height -2 )
664 operation:NSCompositeSourceOver];
665 [[self controlView] unlockFocus];
668 - (void)stopTracking:(NSPoint)lastPoint at:(NSPoint)stopPoint inView:
669 (NSView *)controlView mouseIsUp:(BOOL)flag
673 [super stopTracking:lastPoint at:stopPoint inView:controlView mouseIsUp:flag];
676 - (BOOL)startTrackingAt:(NSPoint)startPoint inView:(NSView *)controlView
680 return [super startTrackingAt:startPoint inView:controlView];