]> git.sesse.net Git - vlc/blob - modules/gui/macosx/embeddedwindow.m
legacy OS X intf: restored 1.0 look of the video output window
[vlc] / modules / gui / macosx / embeddedwindow.m
1 /*****************************************************************************
2  * embeddedwindow.m: MacOS X interface module
3  *****************************************************************************
4  * Copyright (C) 2005-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Benjamin Pracht <bigben at videolan dot org>
8  *          Felix Paul Kühne <fkuehne at videolan dot org>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #import "intf.h"
30 #import "controls.h"
31 #import "vout.h"
32 #import "embeddedwindow.h"
33 #import "fspanel.h"
34 #import "playlist.h"
35
36 /* SetSystemUIMode, ... */
37 #import <Carbon/Carbon.h>
38
39 /*****************************************************************************
40  * VLCEmbeddedWindow Implementation
41  *****************************************************************************/
42
43 @implementation VLCEmbeddedWindow
44
45 - (id)initWithContentRect:(NSRect)contentRect styleMask: (NSUInteger)windowStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)deferCreation
46 {
47     BOOL b_useTextured = YES;
48     if( [[NSWindow class] instancesRespondToSelector:@selector(setContentBorderThickness:forEdge:)] )
49     {
50         b_useTextured = NO;
51         windowStyle ^= NSTexturedBackgroundWindowMask;
52     }
53     self = [super initWithContentRect:contentRect styleMask:windowStyle backing:bufferingType defer:deferCreation];
54     if(! b_useTextured )
55     {
56         [self setContentBorderThickness:28.0 forEdge:NSMinYEdge];
57     }
58     return self;
59 }
60
61 - (void)awakeFromNib
62 {
63     [self setDelegate: self];
64
65     /* button strings */
66     [o_btn_backward setToolTip: _NS("Rewind")];
67     [o_btn_forward setToolTip: _NS("Fast Forward")];
68     [o_btn_fullscreen setToolTip: _NS("Fullscreen")];
69     [o_btn_play setToolTip: _NS("Play")];
70     [o_timeslider setToolTip: _NS("Position")];
71     [o_btn_prev setToolTip: _NS("Previous")];
72     [o_btn_stop setToolTip: _NS("Stop")];
73     [o_btn_next setToolTip: _NS("Next")];
74     [o_volumeslider setToolTip: _NS("Volume")];
75     [o_btn_playlist setToolTip: _NS("Playlist")];
76     [self setTitle: _NS("VLC media player")];
77
78     o_img_play = [NSImage imageNamed: @"play_embedded"];
79     o_img_pause = [NSImage imageNamed: @"pause_embedded"];
80
81     [self controlTintChanged];
82     [[NSNotificationCenter defaultCenter] addObserver: self
83                                              selector: @selector( controlTintChanged )
84                                                  name: NSControlTintDidChangeNotification
85                                                object: nil];
86
87     /* Set color of sidebar to Leopard's "Sidebar Blue" */
88     [o_sidebar_list setBackgroundColor: [NSColor colorWithCalibratedRed:0.820
89                                                                   green:0.843
90                                                                    blue:0.886
91                                                                   alpha:1.0]];
92
93     [self setMinSize:NSMakeSize([o_sidebar_list convertRect:[o_sidebar_list bounds]
94                                                      toView: nil].size.width + 551., 114.)];
95
96     /* Useful to save o_view frame in fullscreen mode */
97     o_temp_view = [[NSView alloc] init];
98     [o_temp_view setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
99
100     o_fullscreen_window = nil;
101     o_makekey_anim = o_fullscreen_anim1 = o_fullscreen_anim2 = nil;
102
103     /* Not fullscreen when we wake up */
104     [o_btn_fullscreen setState: NO];
105     b_fullscreen = NO;
106
107     [self setMovableByWindowBackground:YES];
108
109     [self setDelegate:self];
110
111     /* Make sure setVisible: returns NO */
112     [self orderOut:self];
113     b_window_is_invisible = YES;
114     videoRatio = NSMakeSize( 0., 0. );
115 }
116
117 - (void)controlTintChanged
118 {
119     BOOL b_playing = NO;
120     if( [o_btn_play alternateImage] == o_img_play_pressed )
121         b_playing = YES;
122
123     if( [NSColor currentControlTint] == NSGraphiteControlTint )
124     {
125         o_img_play_pressed = [NSImage imageNamed: @"play_embedded_graphite"];
126         o_img_pause_pressed = [NSImage imageNamed: @"pause_embedded_graphite"];
127         [o_btn_backward setAlternateImage: [NSImage imageNamed: @"skip_previous_embedded_graphite"]];
128         [o_btn_forward setAlternateImage: [NSImage imageNamed: @"skip_forward_embedded_graphite"]];
129         [o_btn_fullscreen setAlternateImage: [NSImage imageNamed: @"fullscreen_graphite"]];
130     } else {
131         o_img_play_pressed = [NSImage imageNamed: @"play_embedded_blue"];
132         o_img_pause_pressed = [NSImage imageNamed: @"pause_embedded_blue"];
133         [o_btn_backward setAlternateImage: [NSImage imageNamed: @"skip_previous_embedded_blue"]];
134         [o_btn_forward setAlternateImage: [NSImage imageNamed: @"skip_forward_embedded_blue"]];
135         [o_btn_fullscreen setAlternateImage: [NSImage imageNamed: @"fullscreen_blue"]];
136     }
137
138     if( b_playing )
139         [o_btn_play setAlternateImage: o_img_play_pressed];
140     else
141         [o_btn_play setAlternateImage: o_img_pause_pressed];
142 }
143
144 - (void)dealloc
145 {
146     [[NSNotificationCenter defaultCenter] removeObserver: self];
147     [o_img_play release];
148     [o_img_play_pressed release];
149     [o_img_pause release];
150     [o_img_pause_pressed release];
151
152     [super dealloc];
153 }
154
155 - (void)setTime:(NSString *)o_arg_time position:(float)f_position
156 {
157     [o_time setStringValue: o_arg_time];
158     [o_timeslider setFloatValue: f_position];
159 }
160
161 - (void)playStatusUpdated:(int)i_status
162 {
163     if( i_status == PLAYING_S )
164     {
165         [o_btn_play setImage: o_img_pause];
166         [o_btn_play setAlternateImage: o_img_pause_pressed];
167         [o_btn_play setToolTip: _NS("Pause")];
168     }
169     else
170     {
171         [o_btn_play setImage: o_img_play];
172         [o_btn_play setAlternateImage: o_img_play_pressed];
173         [o_btn_play setToolTip: _NS("Play")];
174     }
175 }
176
177 - (void)setSeekable:(BOOL)b_seekable
178 {
179     [o_btn_forward setEnabled: b_seekable];
180     [o_btn_backward setEnabled: b_seekable];
181     [o_timeslider setEnabled: b_seekable];
182 }
183
184 - (void)setScrollString:(NSString *)o_string
185 {
186     [o_scrollfield setStringValue: o_string];
187 }
188
189 - (id)getPgbar
190 {
191     if( o_main_pgbar )
192         return o_main_pgbar;
193
194     return nil;
195 }
196
197 - (void)setStop:(BOOL)b_input
198 {
199     [o_btn_stop setEnabled: b_input];
200 }
201
202 - (void)setNext:(BOOL)b_input
203 {
204     [o_btn_next setEnabled: b_input];
205 }
206
207 - (void)setPrev:(BOOL)b_input
208 {
209     [o_btn_prev setEnabled: b_input];
210 }
211
212 - (void)setVolumeEnabled:(BOOL)b_input
213 {
214     [o_volumeslider setEnabled: b_input];
215 }
216
217 - (void)setVolumeSlider:(float)f_level
218 {
219     [o_volumeslider setFloatValue: f_level];
220 }
221
222 - (BOOL)windowShouldZoom:(NSWindow *)sender toFrame:(NSRect)newFrame
223 {
224     [self setFrame: newFrame display: YES animate: YES];
225     return NO;
226 }
227
228 - (BOOL)windowShouldClose:(id)sender
229 {
230     playlist_t * p_playlist = pl_Get( VLCIntf );
231     playlist_Stop( p_playlist );
232
233     return YES;
234 }
235
236 - (NSView *)mainView
237 {
238     if (o_fullscreen_window)
239         return o_temp_view;
240     else
241         return o_view;
242 }
243
244 - (void)setVideoRatio:(NSSize)ratio
245 {
246     videoRatio = ratio;
247 }
248
249 - (NSSize)windowWillResize:(NSWindow *)window toSize:(NSSize)proposedFrameSize
250 {
251         NSView *playlist_area = [[o_vertical_split subviews] objectAtIndex:1];
252         NSRect newList = [playlist_area frame];
253         if( newList.size.height < 50 && newList.size.height > 0 ) {
254                 [self togglePlaylist:self];
255         }
256
257     /* With no video open or with the playlist open the behavior is odd */
258     if( newList.size.height > 50 )
259         return proposedFrameSize;
260
261     if( videoRatio.height == 0. || videoRatio.width == 0. )
262         return proposedFrameSize;
263
264     NSRect viewRect = [o_view convertRect:[o_view bounds] toView: nil];
265     NSRect contentRect = [self contentRectForFrameRect:[self frame]];
266     float marginy = viewRect.origin.y + [self frame].size.height - contentRect.size.height;
267     float marginx = contentRect.size.width - viewRect.size.width;
268
269     proposedFrameSize.height = (proposedFrameSize.width - marginx) * videoRatio.height / videoRatio.width + marginy;
270
271     return proposedFrameSize;
272 }
273
274 - (void)becomeMainWindow
275 {
276     [o_sidebar_list setBackgroundColor: [NSColor colorWithCalibratedRed:0.820
277                                                                   green:0.843
278                                                                    blue:0.886
279                                                                   alpha:1.0]];
280         [o_status becomeMainWindow];
281     [super becomeMainWindow];
282 }
283
284 - (void)resignMainWindow
285 {
286     [o_sidebar_list setBackgroundColor: [NSColor colorWithCalibratedWhite:0.91 alpha:1.0]];
287         [o_status resignMainWindow];
288     [super resignMainWindow];
289 }
290
291 - (CGFloat)splitView:(NSSplitView *) splitView constrainSplitPosition:(CGFloat) proposedPosition ofSubviewAt:(NSInteger) index
292 {
293         if([splitView isVertical])
294                 return proposedPosition;
295         else if ( splitView == o_vertical_split )
296                 return proposedPosition ;
297         else {
298                 float bottom = [splitView frame].size.height - [splitView dividerThickness];
299                 if(proposedPosition > bottom - 50) {
300                         [o_btn_playlist setState: NSOffState];
301                         [o_searchfield setHidden:YES];
302                         [o_playlist_view setHidden:YES];
303                         return bottom;
304                 }
305                 else {
306                         [o_btn_playlist setState: NSOnState];
307                         [o_searchfield setHidden:NO];
308                         [o_playlist_view setHidden:NO];
309                         [o_playlist swapPlaylists: o_playlist_table];
310                         [o_vlc_main togglePlaylist:self];
311                         return proposedPosition;
312                 }
313         }
314 }
315
316 - (void)splitViewWillResizeSubviews:(NSNotification *) notification
317 {
318
319 }
320
321 - (CGFloat)splitView:(NSSplitView *) splitView constrainMinCoordinate:(CGFloat) proposedMin ofSubviewAt:(NSInteger) offset
322 {
323         if([splitView isVertical])
324                 return 125.;
325         else
326                 return 0.;
327 }
328
329 - (CGFloat)splitView:(NSSplitView *) splitView constrainMaxCoordinate:(CGFloat) proposedMax ofSubviewAt:(NSInteger) offset
330 {
331     if([splitView isVertical])
332                 return MIN([self frame].size.width - 551, 300);
333         else
334                 return [splitView frame].size.height;
335 }
336
337 - (BOOL)splitView:(NSSplitView *) splitView canCollapseSubview:(NSView *) subview
338 {
339         if([splitView isVertical])
340                 return NO;
341         else
342                 return NO;
343 }
344
345 - (NSRect)splitView:(NSSplitView *)splitView effectiveRect:(NSRect)proposedEffectiveRect forDrawnRect:(NSRect)drawnRect
346    ofDividerAtIndex:(NSInteger)dividerIndex
347 {
348         if([splitView isVertical]) {
349                 drawnRect.origin.x -= 3;
350                 drawnRect.size.width += 5;
351                 return drawnRect;
352         }
353         else
354                 return drawnRect;
355 }
356
357 - (IBAction)togglePlaylist:(id)sender
358 {
359         NSView *playback_area = [[o_vertical_split subviews] objectAtIndex:0];
360         NSView *playlist_area = [[o_vertical_split subviews] objectAtIndex:1];
361         NSRect newVid = [playback_area frame];
362         NSRect newList = [playlist_area frame];
363         if(newList.size.height < 50 && sender != self && sender != o_vlc_main) {
364                 newList.size.height = newVid.size.height/2;
365                 newVid.size.height = newVid.size.height/2;
366                 newVid.origin.y = newVid.origin.y + newList.size.height;
367                 [o_btn_playlist setState: NSOnState];
368                 [o_searchfield setHidden:NO];
369                 [o_playlist_view setHidden:NO];
370                 [o_playlist swapPlaylists: o_playlist_table];
371                 [o_vlc_main togglePlaylist:self];
372         }
373         else {
374                 newVid.size.height = newVid.size.height + newList.size.height;
375                 newList.size.height = 0;
376                 newVid.origin.y = 0;
377                 [o_btn_playlist setState: NSOffState];
378                 [o_searchfield setHidden:YES];
379                 [o_playlist_view setHidden:YES];
380         }
381         [playback_area setFrame: newVid];
382         [playlist_area setFrame: newList];
383 }
384
385 /*****************************************************************************
386  * Fullscreen support
387  */
388
389 - (BOOL)isFullscreen
390 {
391     return b_fullscreen;
392 }
393
394 - (void)lockFullscreenAnimation
395 {
396     [o_animation_lock lock];
397 }
398
399 - (void)unlockFullscreenAnimation
400 {
401     [o_animation_lock unlock];
402 }
403
404 - (void)enterFullscreen
405 {
406     NSMutableDictionary *dict1, *dict2;
407     NSScreen *screen;
408     NSRect screen_rect;
409     NSRect rect;
410     vout_thread_t *p_vout = getVout();
411     BOOL blackout_other_displays = config_GetInt( VLCIntf, "macosx-black" );
412
413     screen = [NSScreen screenWithDisplayID:(CGDirectDisplayID)var_GetInteger( p_vout, "video-device" )];
414
415     [self lockFullscreenAnimation];
416
417     if (!screen)
418     {
419         msg_Dbg( p_vout, "chosen screen isn't present, using current screen for fullscreen mode" );
420         screen = [self screen];
421     }
422     if (!screen)
423     {
424         msg_Dbg( p_vout, "Using deepest screen" );
425         screen = [NSScreen deepestScreen];
426     }
427
428     vlc_object_release( p_vout );
429
430     screen_rect = [screen frame];
431
432     [o_btn_fullscreen setState: YES];
433
434     [NSCursor setHiddenUntilMouseMoves: YES];
435
436     if( blackout_other_displays )
437         [screen blackoutOtherScreens];
438
439     /* Make sure we don't see the window flashes in float-on-top mode */
440     originalLevel = [self level];
441     [self setLevel:NSNormalWindowLevel];
442
443     /* Only create the o_fullscreen_window if we are not in the middle of the zooming animation */
444     if (!o_fullscreen_window)
445     {
446         /* We can't change the styleMask of an already created NSWindow, so we create an other window, and do eye catching stuff */
447
448         rect = [[o_view superview] convertRect: [o_view frame] toView: nil]; /* Convert to Window base coord */
449         rect.origin.x += [self frame].origin.x;
450         rect.origin.y += [self frame].origin.y;
451         o_fullscreen_window = [[VLCWindow alloc] initWithContentRect:rect styleMask: NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:YES];
452         [o_fullscreen_window setBackgroundColor: [NSColor blackColor]];
453         [o_fullscreen_window setCanBecomeKeyWindow: YES];
454
455         if (![self isVisible] || [self alphaValue] == 0.0)
456         {
457             /* We don't animate if we are not visible, instead we
458              * simply fade the display */
459             CGDisplayFadeReservationToken token;
460
461             CGAcquireDisplayFadeReservation(kCGMaxDisplayReservationInterval, &token);
462             CGDisplayFade( token, 0.5, kCGDisplayBlendNormal, kCGDisplayBlendSolidColor, 0, 0, 0, YES );
463
464             if ([screen isMainScreen])
465                 SetSystemUIMode( kUIModeAllHidden, kUIOptionAutoShowMenuBar);
466
467             [[o_view superview] replaceSubview:o_view with:o_temp_view];
468             [o_temp_view setFrame:[o_view frame]];
469             [o_fullscreen_window setContentView:o_view];
470
471             [o_fullscreen_window makeKeyAndOrderFront:self];
472             [o_fullscreen_window orderFront:self animate:YES];
473
474             [o_fullscreen_window setFrame:screen_rect display:YES];
475
476             CGDisplayFade( token, 0.3, kCGDisplayBlendSolidColor, kCGDisplayBlendNormal, 0, 0, 0, NO );
477             CGReleaseDisplayFadeReservation( token);
478
479             /* Will release the lock */
480             [self hasBecomeFullscreen];
481
482             return;
483         }
484
485         /* Make sure we don't see the o_view disappearing of the screen during this operation */
486         NSDisableScreenUpdates();
487         [[o_view superview] replaceSubview:o_view with:o_temp_view];
488         [o_temp_view setFrame:[o_view frame]];
489         [o_fullscreen_window setContentView:o_view];
490         [o_fullscreen_window makeKeyAndOrderFront:self];
491         NSEnableScreenUpdates();
492     }
493
494     /* We are in fullscreen (and no animation is running) */
495     if (b_fullscreen)
496     {
497         /* Make sure we are hidden */
498         [super orderOut: self];
499         [self unlockFullscreenAnimation];
500         return;
501     }
502
503     if (o_fullscreen_anim1)
504     {
505         [o_fullscreen_anim1 stopAnimation];
506         [o_fullscreen_anim1 release];
507     }
508     if (o_fullscreen_anim2)
509     {
510         [o_fullscreen_anim2 stopAnimation];
511         [o_fullscreen_anim2 release];
512     }
513
514     if ([screen isMainScreen])
515         SetSystemUIMode( kUIModeAllHidden, kUIOptionAutoShowMenuBar);
516
517     dict1 = [[NSMutableDictionary alloc] initWithCapacity:2];
518     dict2 = [[NSMutableDictionary alloc] initWithCapacity:3];
519
520     [dict1 setObject:self forKey:NSViewAnimationTargetKey];
521     [dict1 setObject:NSViewAnimationFadeOutEffect forKey:NSViewAnimationEffectKey];
522
523     [dict2 setObject:o_fullscreen_window forKey:NSViewAnimationTargetKey];
524     [dict2 setObject:[NSValue valueWithRect:[o_fullscreen_window frame]] forKey:NSViewAnimationStartFrameKey];
525     [dict2 setObject:[NSValue valueWithRect:screen_rect] forKey:NSViewAnimationEndFrameKey];
526
527     /* Strategy with NSAnimation allocation:
528         - Keep at most 2 animation at a time
529         - leaveFullscreen/enterFullscreen are the only responsible for releasing and alloc-ing
530     */
531     o_fullscreen_anim1 = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObject:dict1]];
532     o_fullscreen_anim2 = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObject:dict2]];
533
534     [dict1 release];
535     [dict2 release];
536
537     [o_fullscreen_anim1 setAnimationBlockingMode: NSAnimationNonblocking];
538     [o_fullscreen_anim1 setDuration: 0.3];
539     [o_fullscreen_anim1 setFrameRate: 30];
540     [o_fullscreen_anim2 setAnimationBlockingMode: NSAnimationNonblocking];
541     [o_fullscreen_anim2 setDuration: 0.2];
542     [o_fullscreen_anim2 setFrameRate: 30];
543
544     [o_fullscreen_anim2 setDelegate: self];
545     [o_fullscreen_anim2 startWhenAnimation: o_fullscreen_anim1 reachesProgress: 1.0];
546
547     [o_fullscreen_anim1 startAnimation];
548     /* fullscreenAnimation will be unlocked when animation ends */
549 }
550
551 - (void)hasBecomeFullscreen
552 {
553     [o_fullscreen_window makeFirstResponder: [[[VLCMain sharedInstance] controls] voutView]];
554
555     [o_fullscreen_window makeKeyWindow];
556     [o_fullscreen_window setAcceptsMouseMovedEvents: TRUE];
557
558     /* tell the fspanel to move itself to front next time it's triggered */
559     [[[[VLCMain sharedInstance] controls] fspanel] setVoutWasUpdated: (int)[[o_fullscreen_window screen] displayID]];
560
561     if([self isVisible])
562         [super orderOut: self];
563
564     [[[[VLCMain sharedInstance] controls] fspanel] setActive: nil];
565
566     b_fullscreen = YES;
567     [self unlockFullscreenAnimation];
568 }
569
570 - (void)leaveFullscreen
571 {
572     [self leaveFullscreenAndFadeOut: NO];
573 }
574
575 - (void)leaveFullscreenAndFadeOut: (BOOL)fadeout
576 {
577     NSMutableDictionary *dict1, *dict2;
578     NSRect frame;
579
580     [self lockFullscreenAnimation];
581
582     b_fullscreen = NO;
583     [o_btn_fullscreen setState: NO];
584
585     /* We always try to do so */
586     [NSScreen unblackoutScreens];
587
588     /* Don't do anything if o_fullscreen_window is already closed */
589     if (!o_fullscreen_window)
590     {
591         [self unlockFullscreenAnimation];
592         return;
593     }
594
595     if (fadeout)
596     {
597         /* We don't animate if we are not visible, instead we
598         * simply fade the display */
599         CGDisplayFadeReservationToken token;
600
601         CGAcquireDisplayFadeReservation(kCGMaxDisplayReservationInterval, &token);
602         CGDisplayFade( token, 0.3, kCGDisplayBlendNormal, kCGDisplayBlendSolidColor, 0, 0, 0, YES );
603
604         [[[[VLCMain sharedInstance] controls] fspanel] setNonActive: nil];
605         SetSystemUIMode( kUIModeNormal, kUIOptionAutoShowMenuBar);
606
607         /* Will release the lock */
608         [self hasEndedFullscreen];
609
610         /* Our window is hidden, and might be faded. We need to workaround that, so note it
611          * here */
612         b_window_is_invisible = YES;
613
614         CGDisplayFade( token, 0.5, kCGDisplayBlendSolidColor, kCGDisplayBlendNormal, 0, 0, 0, NO );
615         CGReleaseDisplayFadeReservation( token);
616         return;
617     }
618
619     [self setAlphaValue: 0.0];
620     [self orderFront: self];
621
622     [[[[VLCMain sharedInstance] controls] fspanel] setNonActive: nil];
623     SetSystemUIMode( kUIModeNormal, kUIOptionAutoShowMenuBar);
624
625     if (o_fullscreen_anim1)
626     {
627         [o_fullscreen_anim1 stopAnimation];
628         [o_fullscreen_anim1 release];
629     }
630     if (o_fullscreen_anim2)
631     {
632         [o_fullscreen_anim2 stopAnimation];
633         [o_fullscreen_anim2 release];
634     }
635
636     frame = [[o_temp_view superview] convertRect: [o_temp_view frame] toView: nil]; /* Convert to Window base coord */
637     frame.origin.x += [self frame].origin.x;
638     frame.origin.y += [self frame].origin.y;
639
640     dict2 = [[NSMutableDictionary alloc] initWithCapacity:2];
641     [dict2 setObject:self forKey:NSViewAnimationTargetKey];
642     [dict2 setObject:NSViewAnimationFadeInEffect forKey:NSViewAnimationEffectKey];
643
644     o_fullscreen_anim2 = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects:dict2, nil]];
645     [dict2 release];
646
647     [o_fullscreen_anim2 setAnimationBlockingMode: NSAnimationNonblocking];
648     [o_fullscreen_anim2 setDuration: 0.3];
649     [o_fullscreen_anim2 setFrameRate: 30];
650
651     [o_fullscreen_anim2 setDelegate: self];
652
653     dict1 = [[NSMutableDictionary alloc] initWithCapacity:3];
654
655     [dict1 setObject:o_fullscreen_window forKey:NSViewAnimationTargetKey];
656     [dict1 setObject:[NSValue valueWithRect:[o_fullscreen_window frame]] forKey:NSViewAnimationStartFrameKey];
657     [dict1 setObject:[NSValue valueWithRect:frame] forKey:NSViewAnimationEndFrameKey];
658
659     o_fullscreen_anim1 = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects:dict1, nil]];
660     [dict1 release];
661
662     [o_fullscreen_anim1 setAnimationBlockingMode: NSAnimationNonblocking];
663     [o_fullscreen_anim1 setDuration: 0.2];
664     [o_fullscreen_anim1 setFrameRate: 30];
665     [o_fullscreen_anim2 startWhenAnimation: o_fullscreen_anim1 reachesProgress: 1.0];
666
667     /* Make sure o_fullscreen_window is the frontmost window */
668     [o_fullscreen_window orderFront: self];
669
670     [o_fullscreen_anim1 startAnimation];
671     /* fullscreenAnimation will be unlocked when animation ends */
672 }
673
674 - (void)hasEndedFullscreen
675 {
676     /* This function is private and should be only triggered at the end of the fullscreen change animation */
677     /* Make sure we don't see the o_view disappearing of the screen during this operation */
678     NSDisableScreenUpdates();
679     [o_view retain];
680     [o_view removeFromSuperviewWithoutNeedingDisplay];
681     [[o_temp_view superview] replaceSubview:o_temp_view with:o_view];
682     [o_view release];
683     [o_view setFrame:[o_temp_view frame]];
684     [self makeFirstResponder: o_view];
685     if ([self isVisible])
686         [super makeKeyAndOrderFront:self]; /* our version contains a workaround */
687     [o_fullscreen_window orderOut: self];
688     NSEnableScreenUpdates();
689
690     [o_fullscreen_window release];
691     o_fullscreen_window = nil;
692     [self setLevel:originalLevel];
693
694     [self unlockFullscreenAnimation];
695 }
696
697 - (void)animationDidEnd:(NSAnimation*)animation
698 {
699     NSArray *viewAnimations;
700     if( o_makekey_anim == animation )
701     {
702         [o_makekey_anim release];
703         return;
704     }
705     if ([animation currentValue] < 1.0)
706         return;
707
708     /* Fullscreen ended or started (we are a delegate only for leaveFullscreen's/enterFullscren's anim2) */
709     viewAnimations = [o_fullscreen_anim2 viewAnimations];
710     if ([viewAnimations count] >=1 &&
711         [[[viewAnimations objectAtIndex: 0] objectForKey: NSViewAnimationEffectKey] isEqualToString:NSViewAnimationFadeInEffect])
712     {
713         /* Fullscreen ended */
714         [self hasEndedFullscreen];
715     }
716     else
717     {
718         /* Fullscreen started */
719         [self hasBecomeFullscreen];
720     }
721 }
722
723 - (void)orderOut: (id)sender
724 {
725     [super orderOut: sender];
726
727     /* Make sure we leave fullscreen */
728     [self leaveFullscreenAndFadeOut: YES];
729 }
730
731 - (void)makeKeyAndOrderFront: (id)sender
732 {
733     /* Hack
734      * when we exit fullscreen and fade out, we may endup in
735      * having a window that is faded. We can't have it fade in unless we
736      * animate again. */
737
738     if(!b_window_is_invisible)
739     {
740         /* Make sure we don't do it too much */
741         [super makeKeyAndOrderFront: sender];
742         return;
743     }
744
745     [super setAlphaValue:0.0f];
746     [super makeKeyAndOrderFront: sender];
747
748     NSMutableDictionary * dict = [[NSMutableDictionary alloc] initWithCapacity:2];
749     [dict setObject:self forKey:NSViewAnimationTargetKey];
750     [dict setObject:NSViewAnimationFadeInEffect forKey:NSViewAnimationEffectKey];
751
752     o_makekey_anim = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObject:dict]];
753     [dict release];
754
755     [o_makekey_anim setAnimationBlockingMode: NSAnimationNonblocking];
756     [o_makekey_anim setDuration: 0.1];
757     [o_makekey_anim setFrameRate: 30];
758     [o_makekey_anim setDelegate: self];
759
760     [o_makekey_anim startAnimation];
761     b_window_is_invisible = NO;
762
763     /* fullscreenAnimation will be unlocked when animation ends */
764 }
765
766
767
768 /* Make sure setFrame gets executed on main thread especially if we are animating.
769  * (Thus we won't block the video output thread) */
770 - (void)setFrame:(NSRect)frame display:(BOOL)display animate:(BOOL)animate
771 {
772     struct { NSRect frame; BOOL display; BOOL animate;} args;
773     NSData *packedargs;
774
775     args.frame = frame;
776     args.display = display;
777     args.animate = animate;
778
779     packedargs = [NSData dataWithBytes:&args length:sizeof(args)];
780
781     [self performSelectorOnMainThread:@selector(setFrameOnMainThread:)
782                     withObject: packedargs waitUntilDone: YES];
783 }
784
785 - (void)setFrameOnMainThread:(NSData*)packedargs
786 {
787     struct args { NSRect frame; BOOL display; BOOL animate; } * args = (struct args*)[packedargs bytes];
788
789     if( args->animate )
790     {
791         /* Make sure we don't block too long and set up a non blocking animation */
792         NSDictionary * dict = [NSDictionary dictionaryWithObjectsAndKeys:
793             self, NSViewAnimationTargetKey,
794             [NSValue valueWithRect:[self frame]], NSViewAnimationStartFrameKey,
795             [NSValue valueWithRect:args->frame], NSViewAnimationEndFrameKey, nil];
796
797         NSViewAnimation * anim = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObject:dict]];
798         [dict release];
799
800         [anim setAnimationBlockingMode: NSAnimationNonblocking];
801         [anim setDuration: 0.4];
802         [anim setFrameRate: 30];
803         [anim startAnimation];
804     }
805     else {
806         [super setFrame:args->frame display:args->display animate:args->animate];
807     }
808
809 }
810 @end
811
812 /*****************************************************************************
813  * embeddedbackground
814  *****************************************************************************/
815
816
817 @implementation embeddedbackground
818
819 - (void)dealloc
820 {
821     [self unregisterDraggedTypes];
822     [super dealloc];
823 }
824
825 - (void)awakeFromNib
826 {
827     [self registerForDraggedTypes:[NSArray arrayWithObjects:NSTIFFPboardType,
828                                    NSFilenamesPboardType, nil]];
829     [self addSubview: o_timeslider];
830     [self addSubview: o_scrollfield];
831     [self addSubview: o_time];
832     [self addSubview: o_main_pgbar];
833     [self addSubview: o_btn_backward];
834     [self addSubview: o_btn_forward];
835     [self addSubview: o_btn_fullscreen];
836     [self addSubview: o_btn_equalizer];
837     [self addSubview: o_btn_playlist];
838     [self addSubview: o_btn_play];
839     [self addSubview: o_btn_prev];
840     [self addSubview: o_btn_stop];
841     [self addSubview: o_btn_next];
842     [self addSubview: o_btn_volume_down];
843     [self addSubview: o_volumeslider];
844     [self addSubview: o_btn_volume_up];
845     [self addSubview: o_searchfield];
846 }
847
848 - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
849 {
850     if ((NSDragOperationGeneric & [sender draggingSourceOperationMask])
851         == NSDragOperationGeneric)
852     {
853         return NSDragOperationGeneric;
854     }
855     else
856     {
857         return NSDragOperationNone;
858     }
859 }
860
861 - (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
862 {
863     return YES;
864 }
865
866 - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
867 {
868     NSPasteboard *o_paste = [sender draggingPasteboard];
869     NSArray *o_types = [NSArray arrayWithObjects: NSFilenamesPboardType, nil];
870     NSString *o_desired_type = [o_paste availableTypeFromArray:o_types];
871     NSData *o_carried_data = [o_paste dataForType:o_desired_type];
872     BOOL b_autoplay = config_GetInt( VLCIntf, "macosx-autoplay" );
873
874     if( o_carried_data )
875     {
876         if ([o_desired_type isEqualToString:NSFilenamesPboardType])
877         {
878             int i;
879             NSArray *o_array = [NSArray array];
880             NSArray *o_values = [[o_paste propertyListForType: NSFilenamesPboardType]
881                                  sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
882
883             for( i = 0; i < (int)[o_values count]; i++)
884             {
885                 NSDictionary *o_dic;
886                 o_dic = [NSDictionary dictionaryWithObject:[o_values objectAtIndex:i] forKey:@"ITEM_URL"];
887                 o_array = [o_array arrayByAddingObject: o_dic];
888             }
889             if( b_autoplay )
890                 [[[VLCMain sharedInstance] playlist] appendArray: o_array atPos: -1 enqueue:NO];
891             else
892                 [[[VLCMain sharedInstance] playlist] appendArray: o_array atPos: -1 enqueue:YES];
893             return YES;
894         }
895     }
896     [self setNeedsDisplay:YES];
897     return YES;
898 }
899
900 - (void)concludeDragOperation:(id <NSDraggingInfo>)sender
901 {
902     [self setNeedsDisplay:YES];
903 }
904
905 - (void)drawRect:(NSRect)rect
906 {
907     NSImage *leftImage = [NSImage imageNamed:@"display_left"];
908     NSImage *middleImage = [NSImage imageNamed:@"display_middle"];
909     NSImage *rightImage = [NSImage imageNamed:@"display_right"];
910     [middleImage setSize:NSMakeSize(NSWidth( [self bounds] ) - 134 - [leftImage size].width - [rightImage size].width, [middleImage size].height)];
911     [middleImage setScalesWhenResized:YES];
912     [leftImage compositeToPoint:NSMakePoint( 122., 40. ) operation:NSCompositeSourceOver];
913     [middleImage compositeToPoint:NSMakePoint( 122. + [leftImage size].width, 40. ) operation:NSCompositeSourceOver];
914     [rightImage compositeToPoint:NSMakePoint( NSWidth( [self bounds] ) - 12 - [rightImage size].width, 40. ) operation:NSCompositeSourceOver];
915 }
916
917 - (void)mouseDown:(NSEvent *)event
918 {
919     dragStart = [self convertPoint:[event locationInWindow] fromView:nil];
920 }
921
922 - (void)mouseDragged:(NSEvent *)event
923 {
924     NSPoint dragLocation = [self convertPoint:[event locationInWindow] fromView:nil];
925     NSPoint winOrigin = [o_window frame].origin;
926
927     NSPoint newOrigin = NSMakePoint(winOrigin.x + (dragLocation.x - dragStart.x),
928                                     winOrigin.y + (dragLocation.y - dragStart.y));
929     [o_window setFrameOrigin: newOrigin];
930 }
931
932 @end
933
934 /*****************************************************************************
935  * statusbar
936  *****************************************************************************/
937
938
939 @implementation statusbar
940 - (void)awakeFromNib
941 {
942     [self addSubview: o_text];
943         mainwindow = YES;
944 }
945
946 - (void)resignMainWindow
947 {
948         mainwindow = NO;
949         [self needsDisplay];
950 }
951
952 - (void)becomeMainWindow
953 {
954         mainwindow = YES;
955         [self needsDisplay];
956 }
957
958 - (void)drawRect:(NSRect)rect
959 {
960         if(mainwindow)
961                 [[NSColor colorWithCalibratedRed:0.820
962                                                                    green:0.843
963                                                                         blue:0.886
964                                                                    alpha:1.0] set];
965         else
966                 [[NSColor colorWithCalibratedWhite:0.91 alpha:1.0] set];
967         NSRectFill(rect);
968         /*NSRect divider = rect;
969         divider.origin.y += divider.size.height - 1;
970         divider.size.height = 1;
971         [[NSColor colorWithCalibratedWhite:0.65 alpha:1.] set];
972         NSRectFill(divider);*/
973 }
974 @end