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