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 - (BOOL)isScreen: (NSScreen*)screen
100 return ([self displayID] == [screen displayID]);
103 - (CGDirectDisplayID)displayID
105 return (CGDirectDisplayID)_screenNumber;
108 - (void)blackoutOtherScreens
112 /* Free our previous blackout window (follow blackoutWindow alloc strategy) */
113 [blackoutWindows makeObjectsPerformSelector:@selector(close)];
114 [blackoutWindows removeAllObjects];
117 for(i = 0; i < [[NSScreen screens] count]; i++)
119 VLCWindow *blackoutWindow;
120 NSScreen *screen = [[NSScreen screens] objectAtIndex: i];
121 if([self isScreen: screen])
123 /* blackoutWindow alloc strategy
124 - The NSMutableArray blackoutWindows has the blackoutWindow references
125 - blackoutOtherDisplays is responsible for alloc/releasing its Windows
127 blackoutWindow = [[VLCWindow alloc] initWithContentRect: [screen frame] styleMask: NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO];
128 [blackoutWindow setBackgroundColor:[NSColor blackColor]];
129 [blackoutWindow setLevel: NSFloatingWindowLevel]; /* Disappear when Expose is triggered */
131 [blackoutWindow orderFront: self animate: YES];
133 [blackoutWindows addObject: blackoutWindow];
134 [blackoutWindow release];
138 + (void)unblackoutScreens
142 for(i = 0; i < [blackoutWindows count]; i++)
144 VLCWindow *blackoutWindow = [blackoutWindows objectAtIndex: i];
145 [blackoutWindow closeAndAnimate: YES];
151 /*****************************************************************************
154 * Missing extension to NSWindow
155 *****************************************************************************/
157 @implementation VLCWindow
158 - (id)initWithContentRect:(NSRect)contentRect styleMask:(unsigned int)styleMask
159 backing:(NSBackingStoreType)backingType defer:(BOOL)flag
161 self = [super initWithContentRect:contentRect styleMask:styleMask backing:backingType defer:flag];
163 b_isset_canBecomeKeyWindow = NO;
166 - (void)setCanBecomeKeyWindow: (BOOL)canBecomeKey
168 b_isset_canBecomeKeyWindow = YES;
169 b_canBecomeKeyWindow = canBecomeKey;
172 - (BOOL)canBecomeKeyWindow
174 if(b_isset_canBecomeKeyWindow)
175 return b_canBecomeKeyWindow;
177 return [super canBecomeKeyWindow];
180 - (void)closeAndAnimate: (BOOL)animate
184 if (!animate || MACOS_VERSION < 10.4f)
190 invoc = [NSInvocation invocationWithMethodSignature:[super methodSignatureForSelector:@selector(close)]];
191 [invoc setTarget: (id)super];
193 if (![self isVisible] || [self alphaValue] == 0.0)
199 [self orderOut: self animate: YES callback: invoc];
202 - (void)orderOut: (id)sender animate: (BOOL)animate
204 NSInvocation *invoc = [NSInvocation invocationWithMethodSignature:[super methodSignatureForSelector:@selector(orderOut:)]];
205 [invoc setTarget: (id)super];
206 [invoc setArgument: sender atIndex: 0];
207 [self orderOut: sender animate: animate callback: invoc];
210 - (void)orderOut: (id)sender animate: (BOOL)animate callback:(NSInvocation *)callback
212 NSViewAnimation *anim;
213 NSViewAnimation *current_anim;
214 NSMutableDictionary *dict;
216 if (!animate || MACOS_VERSION < 10.4f)
218 [self orderOut: sender];
222 dict = [[NSMutableDictionary alloc] initWithCapacity:2];
224 [dict setObject:self forKey:NSViewAnimationTargetKey];
226 [dict setObject:NSViewAnimationFadeOutEffect forKey:NSViewAnimationEffectKey];
227 anim = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects:dict, nil]];
230 [anim setAnimationBlockingMode:NSAnimationNonblocking];
231 [anim setDuration:0.9];
232 [anim setFrameRate:30];
233 [anim setUserInfo: callback];
235 @synchronized(self) {
236 current_anim = self->animation;
238 if ([[[current_anim viewAnimations] objectAtIndex:0] objectForKey: NSViewAnimationEffectKey] == NSViewAnimationFadeOutEffect && [current_anim isAnimating])
246 [current_anim stopAnimation];
247 [anim setCurrentProgress:1.0-[current_anim currentProgress]];
248 [current_anim release];
251 [anim setCurrentProgress:1.0 - [self alphaValue]];
252 self->animation = anim;
253 [self setDelegate: self];
254 [anim startAnimation];
259 - (void)orderFront: (id)sender animate: (BOOL)animate
261 NSViewAnimation *anim;
262 NSViewAnimation *current_anim;
263 NSMutableDictionary *dict;
265 if (!animate || MACOS_VERSION < 10.4f)
267 [super orderFront: sender];
268 [self setAlphaValue: 1.0];
272 if (![self isVisible])
274 [self setAlphaValue: 0.0];
275 [super orderFront: sender];
277 else if ([self alphaValue] == 1.0)
279 [super orderFront: self];
283 dict = [[NSMutableDictionary alloc] initWithCapacity:2];
285 [dict setObject:self forKey:NSViewAnimationTargetKey];
287 [dict setObject:NSViewAnimationFadeInEffect forKey:NSViewAnimationEffectKey];
288 anim = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects:dict, nil]];
291 [anim setAnimationBlockingMode:NSAnimationNonblocking];
292 [anim setDuration:0.5];
293 [anim setFrameRate:30];
295 @synchronized(self) {
296 current_anim = self->animation;
298 if ([[[current_anim viewAnimations] objectAtIndex:0] objectForKey: NSViewAnimationEffectKey] == NSViewAnimationFadeInEffect && [current_anim isAnimating])
306 [current_anim stopAnimation];
307 [anim setCurrentProgress:1.0 - [current_anim currentProgress]];
308 [current_anim release];
311 [anim setCurrentProgress:[self alphaValue]];
312 self->animation = anim;
313 [self setDelegate: self];
314 [self orderFront: sender];
315 [anim startAnimation];
320 - (void)animationDidEnd:(NSAnimation*)anim
322 if ([self alphaValue] <= 0.0)
324 NSInvocation * invoc;
325 [super orderOut: nil];
326 [self setAlphaValue: 1.0];
327 if ((invoc = [anim userInfo]))
333 /*****************************************************************************
334 * VLCControllerWindow
335 *****************************************************************************/
337 @implementation VLCControllerWindow
339 - (id)initWithContentRect:(NSRect)contentRect styleMask:(unsigned int)styleMask
340 backing:(NSBackingStoreType)backingType defer:(BOOL)flag
342 self = [super initWithContentRect:contentRect styleMask:styleMask //& ~NSTitledWindowMask
343 backing:backingType defer:flag];
345 [[VLCMain sharedInstance] updateTogglePlaylistState];
350 - (BOOL)performKeyEquivalent:(NSEvent *)o_event
352 /* We indeed want to prioritize Cocoa key equivalent against libvlc,
353 so we perform the menu equivalent now. */
354 if([[NSApp mainMenu] performKeyEquivalent:o_event])
357 return [[VLCMain sharedInstance] hasDefinedShortcutKey:o_event] ||
358 [(VLCControls *)[[VLCMain sharedInstance] getControls] keyEvent:o_event];
365 /*****************************************************************************
367 *****************************************************************************/
369 @implementation VLCControllerView
373 [self unregisterDraggedTypes];
380 /* dealloc doesn't get called on 10.5 if GC is enabled, so we need to provide the basic functionality here */
381 [self unregisterDraggedTypes];
388 [self registerForDraggedTypes:[NSArray arrayWithObjects:NSTIFFPboardType,
389 NSFilenamesPboardType, nil]];
392 - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
394 if ((NSDragOperationGeneric & [sender draggingSourceOperationMask])
395 == NSDragOperationGeneric)
397 return NSDragOperationGeneric;
401 return NSDragOperationNone;
405 - (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
410 - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
412 NSPasteboard *o_paste = [sender draggingPasteboard];
413 NSArray *o_types = [NSArray arrayWithObjects: NSFilenamesPboardType, nil];
414 NSString *o_desired_type = [o_paste availableTypeFromArray:o_types];
415 NSData *o_carried_data = [o_paste dataForType:o_desired_type];
419 if ([o_desired_type isEqualToString:NSFilenamesPboardType])
422 NSArray *o_array = [NSArray array];
423 NSArray *o_values = [[o_paste propertyListForType: NSFilenamesPboardType]
424 sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
426 for( i = 0; i < (int)[o_values count]; i++)
429 o_dic = [NSDictionary dictionaryWithObject:[o_values objectAtIndex:i] forKey:@"ITEM_URL"];
430 o_array = [o_array arrayByAddingObject: o_dic];
432 [(VLCPlaylist *)[[VLCMain sharedInstance] getPlaylist] appendArray: o_array atPos: -1 enqueue:NO];
436 [self setNeedsDisplay:YES];
440 - (void)concludeDragOperation:(id <NSDraggingInfo>)sender
442 [self setNeedsDisplay:YES];
447 /*****************************************************************************
448 * VLBrushedMetalImageView
449 *****************************************************************************/
451 @implementation VLBrushedMetalImageView
453 - (BOOL)mouseDownCanMoveWindow
460 [self unregisterDraggedTypes];
467 /* dealloc doesn't get called on 10.5 if GC is enabled, so we need to provide the basic functionality here */
468 [self unregisterDraggedTypes];
475 [self registerForDraggedTypes:[NSArray arrayWithObjects:NSTIFFPboardType,
476 NSFilenamesPboardType, nil]];
479 - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
481 if ((NSDragOperationGeneric & [sender draggingSourceOperationMask])
482 == NSDragOperationGeneric)
484 return NSDragOperationGeneric;
488 return NSDragOperationNone;
492 - (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
497 - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
499 NSPasteboard *o_paste = [sender draggingPasteboard];
500 NSArray *o_types = [NSArray arrayWithObjects: NSFilenamesPboardType, nil];
501 NSString *o_desired_type = [o_paste availableTypeFromArray:o_types];
502 NSData *o_carried_data = [o_paste dataForType:o_desired_type];
503 BOOL b_autoplay = config_GetInt( VLCIntf, "macosx-autoplay" );
507 if ([o_desired_type isEqualToString:NSFilenamesPboardType])
510 NSArray *o_array = [NSArray array];
511 NSArray *o_values = [[o_paste propertyListForType: NSFilenamesPboardType]
512 sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
514 for( i = 0; i < (int)[o_values count]; i++)
517 o_dic = [NSDictionary dictionaryWithObject:[o_values objectAtIndex:i] forKey:@"ITEM_URL"];
518 o_array = [o_array arrayByAddingObject: o_dic];
521 [[[VLCMain sharedInstance] getPlaylist] appendArray: o_array atPos: -1 enqueue:NO];
523 [[[VLCMain sharedInstance] getPlaylist] appendArray: o_array atPos: -1 enqueue:YES];
527 [self setNeedsDisplay:YES];
531 - (void)concludeDragOperation:(id <NSDraggingInfo>)sender
533 [self setNeedsDisplay:YES];
539 /*****************************************************************************
541 *****************************************************************************/
542 @implementation MPSlider
544 void _drawKnobInRect(NSRect knobRect)
546 // Center knob in given rect
547 knobRect.origin.x += (int)((float)(knobRect.size.width - 7)/2.0);
548 knobRect.origin.y += (int)((float)(knobRect.size.height - 7)/2.0);
551 NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 3, knobRect.origin.y + 6, 1, 1), NSCompositeSourceOver);
552 NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 2, knobRect.origin.y + 5, 3, 1), NSCompositeSourceOver);
553 NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 1, knobRect.origin.y + 4, 5, 1), NSCompositeSourceOver);
554 NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 0, knobRect.origin.y + 3, 7, 1), NSCompositeSourceOver);
555 NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 1, knobRect.origin.y + 2, 5, 1), NSCompositeSourceOver);
556 NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 2, knobRect.origin.y + 1, 3, 1), NSCompositeSourceOver);
557 NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 3, knobRect.origin.y + 0, 1, 1), NSCompositeSourceOver);
560 void _drawFrameInRect(NSRect frameRect)
563 NSRectFillUsingOperation(NSMakeRect(frameRect.origin.x, frameRect.origin.y, frameRect.size.width, 1), NSCompositeSourceOver);
564 NSRectFillUsingOperation(NSMakeRect(frameRect.origin.x, frameRect.origin.y + frameRect.size.height-1, frameRect.size.width, 1), NSCompositeSourceOver);
565 NSRectFillUsingOperation(NSMakeRect(frameRect.origin.x, frameRect.origin.y, 1, frameRect.size.height), NSCompositeSourceOver);
566 NSRectFillUsingOperation(NSMakeRect(frameRect.origin.x+frameRect.size.width-1, frameRect.origin.y, 1, frameRect.size.height), NSCompositeSourceOver);
569 - (void)drawRect:(NSRect)rect
571 // Draw default to make sure the slider behaves correctly
572 [[NSGraphicsContext currentContext] saveGraphicsState];
573 NSRectClip(NSZeroRect);
574 [super drawRect:rect];
575 [[NSGraphicsContext currentContext] restoreGraphicsState];
578 rect = [self bounds];
579 int diff = (int)(([[self cell] knobThickness] - 7.0)/2.0) - 1;
580 rect.origin.x += diff-1;
581 rect.origin.y += diff;
582 rect.size.width -= 2*diff-2;
583 rect.size.height -= 2*diff;
586 NSRect knobRect = [[self cell] knobRectFlipped:NO];
587 [[[NSColor blackColor] colorWithAlphaComponent:0.6] set];
588 _drawFrameInRect(rect);
589 _drawKnobInRect(knobRect);
592 [[[NSColor blackColor] colorWithAlphaComponent:0.1] set];
597 _drawFrameInRect(rect);
598 _drawKnobInRect(knobRect);
604 /*****************************************************************************
606 *****************************************************************************/
608 @implementation ITSlider
612 if ([[self cell] class] != [ITSliderCell class]) {
614 NSSliderCell *oldCell = [self cell];
615 NSSliderCell *newCell = [[[ITSliderCell alloc] init] autorelease];
616 [newCell setTag:[oldCell tag]];
617 [newCell setTarget:[oldCell target]];
618 [newCell setAction:[oldCell action]];
619 [newCell setControlSize:[oldCell controlSize]];
620 [newCell setType:[oldCell type]];
621 [newCell setState:[oldCell state]];
622 [newCell setAllowsTickMarkValuesOnly:[oldCell allowsTickMarkValuesOnly]];
623 [newCell setAltIncrementValue:[oldCell altIncrementValue]];
624 [newCell setControlTint:[oldCell controlTint]];
625 [newCell setKnobThickness:[oldCell knobThickness]];
626 [newCell setMaxValue:[oldCell maxValue]];
627 [newCell setMinValue:[oldCell minValue]];
628 [newCell setDoubleValue:[oldCell doubleValue]];
629 [newCell setNumberOfTickMarks:[oldCell numberOfTickMarks]];
630 [newCell setEditable:[oldCell isEditable]];
631 [newCell setEnabled:[oldCell isEnabled]];
632 [newCell setEntryType:[oldCell entryType]];
633 [newCell setHighlighted:[oldCell isHighlighted]];
634 [newCell setTickMarkPosition:[oldCell tickMarkPosition]];
635 [self setCell:newCell];
641 /*****************************************************************************
643 *****************************************************************************/
644 @implementation ITSliderCell
649 _knobOff = [[NSImage imageNamed:@"volumeslider_normal"] retain];
650 _knobOn = [[NSImage imageNamed:@"volumeslider_blue"] retain];
651 b_mouse_down = FALSE;
662 - (void)drawKnob:(NSRect)knob_rect
671 [[self controlView] lockFocus];
672 [knob compositeToPoint:NSMakePoint( knob_rect.origin.x + 1,
673 knob_rect.origin.y + knob_rect.size.height -2 )
674 operation:NSCompositeSourceOver];
675 [[self controlView] unlockFocus];
678 - (void)stopTracking:(NSPoint)lastPoint at:(NSPoint)stopPoint inView:
679 (NSView *)controlView mouseIsUp:(BOOL)flag
683 [super stopTracking:lastPoint at:stopPoint inView:controlView mouseIsUp:flag];
686 - (BOOL)startTrackingAt:(NSPoint)startPoint inView:(NSView *)controlView
690 return [super startTrackingAt:startPoint inView:controlView];