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