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