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