]> git.sesse.net Git - vlc/blob - modules/gui/macosx/misc.m
* cleaning, reformatting, clarifying here and there...
[vlc] / modules / gui / macosx / misc.m
1 /*****************************************************************************
2  * misc.m: code not specific to vlc
3  *****************************************************************************
4  * Copyright (C) 2003-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 #include <Cocoa/Cocoa.h>
25
26 #include "intf.h"                                          /* VLCApplication */
27 #include "misc.h"
28 #include "playlist.h"
29 #include "controls.h"
30
31 /*****************************************************************************
32  * NSAnimation (VLCAdditions)
33  *
34  *  Missing extension to NSAnimation
35  *****************************************************************************/
36
37 @implementation NSAnimation (VLCAdditions)
38 /* fake class attributes  */
39 static NSMapTable *VLCAdditions_userInfo = NULL;
40
41 + (void)load
42 {
43     /* init our fake object attribute */
44     VLCAdditions_userInfo = NSCreateMapTable(NSNonRetainedObjectMapKeyCallBacks, NSObjectMapValueCallBacks, 16);
45 }
46
47 - (void)dealloc
48 {
49     NSMapRemove(VLCAdditions_userInfo, self);
50     [super dealloc];
51 }
52
53 - (void)setUserInfo: (void *)userInfo
54 {
55     NSMapInsert(VLCAdditions_userInfo, self, (void*)userInfo);
56 }
57
58 - (void *)userInfo
59 {
60     return NSMapGet(VLCAdditions_userInfo, self);
61 }
62 @end
63
64 /*****************************************************************************
65  * NSScreen (VLCAdditions)
66  *
67  *  Missing extension to NSScreen
68  *****************************************************************************/
69
70 @implementation NSScreen (VLCAdditions)
71
72 static NSMutableArray *blackoutWindows = NULL;
73
74 + (void)load
75 {
76     /* init our fake object attribute */
77     blackoutWindows = [[NSMutableArray alloc] initWithCapacity:1];
78 }
79
80 + (NSScreen *)screenWithDisplayID: (CGDirectDisplayID)displayID
81 {
82     int i;
83  
84     for( i = 0; i < [[NSScreen screens] count]; i++ )
85     {
86         NSScreen *screen = [[NSScreen screens] objectAtIndex: i];
87         if([screen displayID] == displayID)
88             return screen;
89     }
90     return nil;
91 }
92
93 - (BOOL)isMainScreen
94 {
95     return ([self displayID] == [[[NSScreen screens] objectAtIndex:0] displayID]);
96 }
97
98 - (BOOL)isScreen: (NSScreen*)screen
99 {
100     return ([self displayID] == [screen displayID]);
101 }
102
103 - (CGDirectDisplayID)displayID
104 {
105     return (CGDirectDisplayID)_screenNumber;
106 }
107
108 - (void)blackoutOtherScreens
109 {
110     unsigned int i;
111
112     /* Free our previous blackout window (follow blackoutWindow alloc strategy) */
113     [blackoutWindows makeObjectsPerformSelector:@selector(close)];
114     [blackoutWindows removeAllObjects];
115
116  
117     for(i = 0; i < [[NSScreen screens] count]; i++)
118     {
119         NSScreen *screen = [[NSScreen screens] objectAtIndex: i];
120         VLCWindow *blackoutWindow;
121         NSRect screen_rect;
122  
123         if([self isScreen: screen])
124             continue;
125
126         screen_rect = [screen frame];
127         screen_rect.origin.x = screen_rect.origin.y = 0.0f;
128
129         /* blackoutWindow alloc strategy
130             - The NSMutableArray blackoutWindows has the blackoutWindow references
131             - blackoutOtherDisplays is responsible for alloc/releasing its Windows
132         */
133         blackoutWindow = [[VLCWindow alloc] initWithContentRect: screen_rect styleMask: NSBorderlessWindowMask
134                 backing: NSBackingStoreBuffered defer: NO screen: screen];
135         [blackoutWindow setBackgroundColor:[NSColor blackColor]];
136         [blackoutWindow setLevel: NSFloatingWindowLevel]; /* Disappear when Expose is triggered */
137  
138         [blackoutWindow orderFront: self animate: YES];
139
140         [blackoutWindows addObject: blackoutWindow];
141         [blackoutWindow release];
142     }
143 }
144
145 + (void)unblackoutScreens
146 {
147     unsigned int i;
148
149     for(i = 0; i < [blackoutWindows count]; i++)
150     {
151         VLCWindow *blackoutWindow = [blackoutWindows objectAtIndex: i];
152         [blackoutWindow closeAndAnimate: YES];
153     }
154 }
155
156 @end
157
158 /*****************************************************************************
159  * VLCWindow
160  *
161  *  Missing extension to NSWindow
162  *****************************************************************************/
163
164 @implementation VLCWindow
165 - (id)initWithContentRect:(NSRect)contentRect styleMask:(unsigned int)styleMask
166     backing:(NSBackingStoreType)backingType defer:(BOOL)flag
167 {
168     self = [super initWithContentRect:contentRect styleMask:styleMask backing:backingType defer:flag];
169     if( self )
170         b_isset_canBecomeKeyWindow = NO;
171     return self;
172 }
173 - (void)setCanBecomeKeyWindow: (BOOL)canBecomeKey
174 {
175     b_isset_canBecomeKeyWindow = YES;
176     b_canBecomeKeyWindow = canBecomeKey;
177 }
178
179 - (BOOL)canBecomeKeyWindow
180 {
181     if(b_isset_canBecomeKeyWindow)
182         return b_canBecomeKeyWindow;
183
184     return [super canBecomeKeyWindow];
185 }
186
187 - (void)closeAndAnimate: (BOOL)animate
188 {
189     NSInvocation *invoc;
190  
191     if (!animate || MACOS_VERSION < 10.4f)
192     {
193         [super close];
194         return;
195     }
196
197     invoc = [NSInvocation invocationWithMethodSignature:[super methodSignatureForSelector:@selector(close)]];
198     [invoc setTarget: (id)super];
199
200     if (![self isVisible] || [self alphaValue] == 0.0)
201     {
202         [super close];
203         return;
204     }
205
206     [self orderOut: self animate: YES callback: invoc];
207 }
208
209 - (void)orderOut: (id)sender animate: (BOOL)animate
210 {
211     NSInvocation *invoc = [NSInvocation invocationWithMethodSignature:[super methodSignatureForSelector:@selector(orderOut:)]];
212     [invoc setTarget: (id)super];
213     [invoc setArgument: sender atIndex: 0];
214     [self orderOut: sender animate: animate callback: invoc];
215 }
216
217 - (void)orderOut: (id)sender animate: (BOOL)animate callback:(NSInvocation *)callback
218 {
219     NSViewAnimation *anim;
220     NSViewAnimation *current_anim;
221     NSMutableDictionary *dict;
222
223     if (!animate || MACOS_VERSION < 10.4f)
224     {
225         [self orderOut: sender];
226         return;
227     }
228
229     dict = [[NSMutableDictionary alloc] initWithCapacity:2];
230
231     [dict setObject:self forKey:NSViewAnimationTargetKey];
232
233     [dict setObject:NSViewAnimationFadeOutEffect forKey:NSViewAnimationEffectKey];
234     anim = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects:dict, nil]];
235     [dict release];
236
237     [anim setAnimationBlockingMode:NSAnimationNonblocking];
238     [anim setDuration:0.9];
239     [anim setFrameRate:30];
240     [anim setUserInfo: callback];
241
242     @synchronized(self) {
243         current_anim = self->animation;
244
245         if ([[[current_anim viewAnimations] objectAtIndex:0] objectForKey: NSViewAnimationEffectKey] == NSViewAnimationFadeOutEffect && [current_anim isAnimating])
246         {
247             [anim release];
248         }
249         else
250         {
251             if (current_anim)
252             {
253                 [current_anim stopAnimation];
254                 [anim setCurrentProgress:1.0-[current_anim currentProgress]];
255                 [current_anim release];
256             }
257             else
258                 [anim setCurrentProgress:1.0 - [self alphaValue]];
259             self->animation = anim;
260             [self setDelegate: self];
261             [anim startAnimation];
262         }
263     }
264 }
265
266 - (void)orderFront: (id)sender animate: (BOOL)animate
267 {
268     NSViewAnimation *anim;
269     NSViewAnimation *current_anim;
270     NSMutableDictionary *dict;
271  
272     if (!animate || MACOS_VERSION < 10.4f)
273     {
274         [super orderFront: sender];
275         [self setAlphaValue: 1.0];
276         return;
277     }
278
279     if (![self isVisible])
280     {
281         [self setAlphaValue: 0.0];
282         [super orderFront: sender];
283     }
284     else if ([self alphaValue] == 1.0)
285     {
286         [super orderFront: self];
287         return;
288     }
289
290     dict = [[NSMutableDictionary alloc] initWithCapacity:2];
291
292     [dict setObject:self forKey:NSViewAnimationTargetKey];
293  
294     [dict setObject:NSViewAnimationFadeInEffect forKey:NSViewAnimationEffectKey];
295     anim = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects:dict, nil]];
296     [dict release];
297  
298     [anim setAnimationBlockingMode:NSAnimationNonblocking];
299     [anim setDuration:0.5];
300     [anim setFrameRate:30];
301
302     @synchronized(self) {
303         current_anim = self->animation;
304
305         if ([[[current_anim viewAnimations] objectAtIndex:0] objectForKey: NSViewAnimationEffectKey] == NSViewAnimationFadeInEffect && [current_anim isAnimating])
306         {
307             [anim release];
308         }
309         else
310         {
311             if (current_anim)
312             {
313                 [current_anim stopAnimation];
314                 [anim setCurrentProgress:1.0 - [current_anim currentProgress]];
315                 [current_anim release];
316             }
317             else
318                 [anim setCurrentProgress:[self alphaValue]];
319             self->animation = anim;
320             [self setDelegate: self];
321             [self orderFront: sender];
322             [anim startAnimation];
323         }
324     }
325 }
326
327 - (void)animationDidEnd:(NSAnimation*)anim
328 {
329     if ([self alphaValue] <= 0.0)
330     {
331         NSInvocation * invoc;
332         [super orderOut: nil];
333         [self setAlphaValue: 1.0];
334         if ((invoc = [anim userInfo]))
335             [invoc invoke];
336     }
337 }
338 @end
339
340 /*****************************************************************************
341  * VLCControllerWindow
342  *****************************************************************************/
343
344 @implementation VLCControllerWindow
345
346 - (id)initWithContentRect:(NSRect)contentRect styleMask:(unsigned int)styleMask
347     backing:(NSBackingStoreType)backingType defer:(BOOL)flag
348 {
349     self = [super initWithContentRect:contentRect styleMask:styleMask //& ~NSTitledWindowMask
350     backing:backingType defer:flag];
351
352     [[VLCMain sharedInstance] updateTogglePlaylistState];
353
354     return( self );
355 }
356
357 - (BOOL)performKeyEquivalent:(NSEvent *)o_event
358 {
359     /* We indeed want to prioritize Cocoa key equivalent against libvlc,
360        so we perform the menu equivalent now. */
361     if([[NSApp mainMenu] performKeyEquivalent:o_event])
362         return TRUE;
363
364     return [[VLCMain sharedInstance] hasDefinedShortcutKey:o_event] ||
365            [(VLCControls *)[[VLCMain sharedInstance] getControls] keyEvent:o_event];
366 }
367
368 @end
369
370
371
372 /*****************************************************************************
373  * VLCControllerView
374  *****************************************************************************/
375
376 @implementation VLCControllerView
377
378 - (void)dealloc
379 {
380     [self unregisterDraggedTypes];
381     [super dealloc];
382 }
383
384 - (void)awakeFromNib
385 {
386     [self registerForDraggedTypes:[NSArray arrayWithObjects:NSTIFFPboardType,
387         NSFilenamesPboardType, nil]];
388 }
389
390 - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
391 {
392     if ((NSDragOperationGeneric & [sender draggingSourceOperationMask])
393                 == NSDragOperationGeneric)
394     {
395         return NSDragOperationGeneric;
396     }
397     else
398     {
399         return NSDragOperationNone;
400     }
401 }
402
403 - (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
404 {
405     return YES;
406 }
407
408 - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
409 {
410     NSPasteboard *o_paste = [sender draggingPasteboard];
411     NSArray *o_types = [NSArray arrayWithObjects: NSFilenamesPboardType, nil];
412     NSString *o_desired_type = [o_paste availableTypeFromArray:o_types];
413     NSData *o_carried_data = [o_paste dataForType:o_desired_type];
414
415     if( o_carried_data )
416     {
417         if ([o_desired_type isEqualToString:NSFilenamesPboardType])
418         {
419             int i;
420             NSArray *o_array = [NSArray array];
421             NSArray *o_values = [[o_paste propertyListForType: NSFilenamesPboardType]
422                         sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
423
424             for( i = 0; i < (int)[o_values count]; i++)
425             {
426                 NSDictionary *o_dic;
427                 o_dic = [NSDictionary dictionaryWithObject:[o_values objectAtIndex:i] forKey:@"ITEM_URL"];
428                 o_array = [o_array arrayByAddingObject: o_dic];
429             }
430             [(VLCPlaylist *)[[VLCMain sharedInstance] getPlaylist] appendArray: o_array atPos: -1 enqueue:NO];
431             return YES;
432         }
433     }
434     [self setNeedsDisplay:YES];
435     return YES;
436 }
437
438 - (void)concludeDragOperation:(id <NSDraggingInfo>)sender
439 {
440     [self setNeedsDisplay:YES];
441 }
442
443 @end
444
445 /*****************************************************************************
446  * VLBrushedMetalImageView
447  *****************************************************************************/
448
449 @implementation VLBrushedMetalImageView
450
451 - (BOOL)mouseDownCanMoveWindow
452 {
453     return YES;
454 }
455
456 - (void)dealloc
457 {
458     [self unregisterDraggedTypes];
459     [super dealloc];
460 }
461
462 - (void)awakeFromNib
463 {
464     [self registerForDraggedTypes:[NSArray arrayWithObjects:NSTIFFPboardType,
465         NSFilenamesPboardType, nil]];
466 }
467
468 - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
469 {
470     if ((NSDragOperationGeneric & [sender draggingSourceOperationMask])
471                 == NSDragOperationGeneric)
472     {
473         return NSDragOperationGeneric;
474     }
475     else
476     {
477         return NSDragOperationNone;
478     }
479 }
480
481 - (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
482 {
483     return YES;
484 }
485
486 - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
487 {
488     NSPasteboard *o_paste = [sender draggingPasteboard];
489     NSArray *o_types = [NSArray arrayWithObjects: NSFilenamesPboardType, nil];
490     NSString *o_desired_type = [o_paste availableTypeFromArray:o_types];
491     NSData *o_carried_data = [o_paste dataForType:o_desired_type];
492     BOOL b_autoplay = config_GetInt( VLCIntf, "macosx-autoplay" );
493
494     if( o_carried_data )
495     {
496         if ([o_desired_type isEqualToString:NSFilenamesPboardType])
497         {
498             int i;
499             NSArray *o_array = [NSArray array];
500             NSArray *o_values = [[o_paste propertyListForType: NSFilenamesPboardType]
501                         sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
502
503             for( i = 0; i < (int)[o_values count]; i++)
504             {
505                 NSDictionary *o_dic;
506                 o_dic = [NSDictionary dictionaryWithObject:[o_values objectAtIndex:i] forKey:@"ITEM_URL"];
507                 o_array = [o_array arrayByAddingObject: o_dic];
508             }
509             if( b_autoplay )
510                 [[[VLCMain sharedInstance] getPlaylist] appendArray: o_array atPos: -1 enqueue:NO];
511             else
512                 [[[VLCMain sharedInstance] getPlaylist] appendArray: o_array atPos: -1 enqueue:YES];
513             return YES;
514         }
515     }
516     [self setNeedsDisplay:YES];
517     return YES;
518 }
519
520 - (void)concludeDragOperation:(id <NSDraggingInfo>)sender
521 {
522     [self setNeedsDisplay:YES];
523 }
524
525 @end
526
527
528 /*****************************************************************************
529  * MPSlider
530  *****************************************************************************/
531 @implementation MPSlider
532
533 void _drawKnobInRect(NSRect knobRect)
534 {
535     // Center knob in given rect
536     knobRect.origin.x += (int)((float)(knobRect.size.width - 7)/2.0);
537     knobRect.origin.y += (int)((float)(knobRect.size.height - 7)/2.0);
538  
539     // Draw diamond
540     NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 3, knobRect.origin.y + 6, 1, 1), NSCompositeSourceOver);
541     NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 2, knobRect.origin.y + 5, 3, 1), NSCompositeSourceOver);
542     NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 1, knobRect.origin.y + 4, 5, 1), NSCompositeSourceOver);
543     NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 0, knobRect.origin.y + 3, 7, 1), NSCompositeSourceOver);
544     NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 1, knobRect.origin.y + 2, 5, 1), NSCompositeSourceOver);
545     NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 2, knobRect.origin.y + 1, 3, 1), NSCompositeSourceOver);
546     NSRectFillUsingOperation(NSMakeRect(knobRect.origin.x + 3, knobRect.origin.y + 0, 1, 1), NSCompositeSourceOver);
547 }
548
549 void _drawFrameInRect(NSRect frameRect)
550 {
551     // Draw frame
552     NSRectFillUsingOperation(NSMakeRect(frameRect.origin.x, frameRect.origin.y, frameRect.size.width, 1), NSCompositeSourceOver);
553     NSRectFillUsingOperation(NSMakeRect(frameRect.origin.x, frameRect.origin.y + frameRect.size.height-1, frameRect.size.width, 1), NSCompositeSourceOver);
554     NSRectFillUsingOperation(NSMakeRect(frameRect.origin.x, frameRect.origin.y, 1, frameRect.size.height), NSCompositeSourceOver);
555     NSRectFillUsingOperation(NSMakeRect(frameRect.origin.x+frameRect.size.width-1, frameRect.origin.y, 1, frameRect.size.height), NSCompositeSourceOver);
556 }
557
558 - (void)drawRect:(NSRect)rect
559 {
560     // Draw default to make sure the slider behaves correctly
561     [[NSGraphicsContext currentContext] saveGraphicsState];
562     NSRectClip(NSZeroRect);
563     [super drawRect:rect];
564     [[NSGraphicsContext currentContext] restoreGraphicsState];
565  
566     // Full size
567     rect = [self bounds];
568     int diff = (int)(([[self cell] knobThickness] - 7.0)/2.0) - 1;
569     rect.origin.x += diff-1;
570     rect.origin.y += diff;
571     rect.size.width -= 2*diff-2;
572     rect.size.height -= 2*diff;
573  
574     // Draw dark
575     NSRect knobRect = [[self cell] knobRectFlipped:NO];
576     [[[NSColor blackColor] colorWithAlphaComponent:0.6] set];
577     _drawFrameInRect(rect);
578     _drawKnobInRect(knobRect);
579  
580     // Draw shadow
581     [[[NSColor blackColor] colorWithAlphaComponent:0.1] set];
582     rect.origin.x++;
583     rect.origin.y++;
584     knobRect.origin.x++;
585     knobRect.origin.y++;
586     _drawFrameInRect(rect);
587     _drawKnobInRect(knobRect);
588 }
589
590 @end
591
592
593 /*****************************************************************************
594  * ITSlider
595  *****************************************************************************/
596
597 @implementation ITSlider
598
599 - (void)awakeFromNib
600 {
601     if ([[self cell] class] != [ITSliderCell class]) {
602         // replace cell
603         NSSliderCell *oldCell = [self cell];
604         NSSliderCell *newCell = [[[ITSliderCell alloc] init] autorelease];
605         [newCell setTag:[oldCell tag]];
606         [newCell setTarget:[oldCell target]];
607         [newCell setAction:[oldCell action]];
608         [newCell setControlSize:[oldCell controlSize]];
609         [newCell setType:[oldCell type]];
610         [newCell setState:[oldCell state]];
611         [newCell setAllowsTickMarkValuesOnly:[oldCell allowsTickMarkValuesOnly]];
612         [newCell setAltIncrementValue:[oldCell altIncrementValue]];
613         [newCell setControlTint:[oldCell controlTint]];
614         [newCell setKnobThickness:[oldCell knobThickness]];
615         [newCell setMaxValue:[oldCell maxValue]];
616         [newCell setMinValue:[oldCell minValue]];
617         [newCell setDoubleValue:[oldCell doubleValue]];
618         [newCell setNumberOfTickMarks:[oldCell numberOfTickMarks]];
619         [newCell setEditable:[oldCell isEditable]];
620         [newCell setEnabled:[oldCell isEnabled]];
621         [newCell setEntryType:[oldCell entryType]];
622         [newCell setHighlighted:[oldCell isHighlighted]];
623         [newCell setTickMarkPosition:[oldCell tickMarkPosition]];
624         [self setCell:newCell];
625     }
626 }
627
628 @end
629
630 /*****************************************************************************
631  * ITSliderCell
632  *****************************************************************************/
633 @implementation ITSliderCell
634
635 - (id)init
636 {
637     self = [super init];
638     _knobOff = [[NSImage imageNamed:@"volumeslider_normal"] retain];
639     _knobOn = [[NSImage imageNamed:@"volumeslider_blue"] retain];
640     b_mouse_down = FALSE;
641     return self;
642 }
643
644 - (void)dealloc
645 {
646     [_knobOff release];
647     [_knobOn release];
648     [super dealloc];
649 }
650
651 - (void)drawKnob:(NSRect)knob_rect
652 {
653     NSImage *knob;
654
655     if( b_mouse_down )
656         knob = _knobOn;
657     else
658         knob = _knobOff;
659
660     [[self controlView] lockFocus];
661     [knob compositeToPoint:NSMakePoint( knob_rect.origin.x + 1,
662         knob_rect.origin.y + knob_rect.size.height -2 )
663         operation:NSCompositeSourceOver];
664     [[self controlView] unlockFocus];
665 }
666
667 - (void)stopTracking:(NSPoint)lastPoint at:(NSPoint)stopPoint inView:
668         (NSView *)controlView mouseIsUp:(BOOL)flag
669 {
670     b_mouse_down = NO;
671     [self drawKnob];
672     [super stopTracking:lastPoint at:stopPoint inView:controlView mouseIsUp:flag];
673 }
674
675 - (BOOL)startTrackingAt:(NSPoint)startPoint inView:(NSView *)controlView
676 {
677     b_mouse_down = YES;
678     [self drawKnob];
679     return [super startTrackingAt:startPoint inView:controlView];
680 }
681
682 @end
683