macosx: fixed playlist/video toggling and some memory leaks
[vlc.git] / modules / gui / macosx / fspanel.m
1 /*****************************************************************************
2  * fspanel.m: MacOS X full screen panel
3  *****************************************************************************
4  * Copyright (C) 2006-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Jérôme Decoodt <djc 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 #import "intf.h"
29 #import "CoreInteraction.h"
30 #import "vout.h"
31 #import "misc.h"
32 #import "fspanel.h"
33 #import "MainWindow.h"
34
35 @interface VLCFSPanel ()
36 - (void)hideMouse;
37 @end
38
39 /*****************************************************************************
40  * VLCFSPanel
41  *****************************************************************************/
42 @implementation VLCFSPanel
43 /* We override this initializer so we can set the NSBorderlessWindowMask styleMask, and set a few other important settings */
44 - (id)initWithContentRect:(NSRect)contentRect 
45                 styleMask:(NSUInteger)aStyle 
46                   backing:(NSBackingStoreType)bufferingType 
47                     defer:(BOOL)flag
48 {
49     id win = [super initWithContentRect:contentRect styleMask:NSTexturedBackgroundWindowMask backing:bufferingType defer:flag];
50     [win setOpaque:NO];
51     [win setHasShadow: NO];
52     [win setBackgroundColor:[NSColor clearColor]];
53     
54     /* let the window sit on top of everything else and start out completely transparent */
55     [win setLevel:NSModalPanelWindowLevel];
56     i_device = 0;
57     [win center];
58     hideAgainTimer = fadeTimer = nil;
59     [self setNonActive:nil];
60     return win;
61 }
62
63 - (void)awakeFromNib
64 {
65     [self setContentView:[[VLCFSPanelView alloc] initWithFrame: [self frame]]];
66     BOOL isInside = (NSPointInRect([NSEvent mouseLocation],[self frame]));
67     [[self contentView] addTrackingRect:[[self contentView] bounds] owner:self userData:nil assumeInside:isInside];
68     if (isInside)
69         [self mouseEntered:NULL];
70     if (!isInside)
71         [self mouseExited:NULL];
72     
73     /* get a notification if VLC isn't the active app anymore */
74     [[NSNotificationCenter defaultCenter]
75     addObserver: self
76        selector: @selector(setNonActive:)
77            name: NSApplicationDidResignActiveNotification
78          object: NSApp];
79     
80     /* get a notification if VLC is the active app again */
81     [[NSNotificationCenter defaultCenter]
82     addObserver: self
83        selector: @selector(setActive:)
84            name: NSApplicationDidBecomeActiveNotification
85          object: NSApp];
86 }
87
88 /* make sure that we don't become key, since we can't handle hotkeys */
89 - (BOOL)canBecomeKeyWindow
90 {
91     return NO;
92 }
93
94 - (BOOL)mouseDownCanMoveWindow
95 {
96     return YES;
97 }
98
99 -(void)dealloc
100 {
101     [[NSNotificationCenter defaultCenter] removeObserver: self];
102
103     if( hideAgainTimer )
104     {
105         [hideAgainTimer invalidate];
106         [hideAgainTimer release];
107     }
108     [self setFadeTimer:nil];
109     [super dealloc];
110 }
111
112 -(void)center
113 {
114     /* centre the panel in the lower third of the screen */
115     NSPoint theCoordinate;
116     NSRect theScreensFrame;
117     NSRect theWindowsFrame;
118     NSScreen *screen;
119     
120     /* user-defined screen */
121     screen = [NSScreen screenWithDisplayID: (CGDirectDisplayID)i_device];
122     
123     if (!screen)
124     {
125         /* invalid preferences or none specified, using main screen */
126         screen = [NSScreen mainScreen];
127     }
128
129     theScreensFrame = [screen frame];
130
131     theWindowsFrame = [self frame];
132     
133     theCoordinate.x = (theScreensFrame.size.width - theWindowsFrame.size.width) / 2 + theScreensFrame.origin.x;
134     theCoordinate.y = (theScreensFrame.size.height / 3) - theWindowsFrame.size.height + theScreensFrame.origin.y;
135     [self setFrameTopLeftPoint: theCoordinate];
136 }
137
138 - (void)setPlay
139 {
140     [[self contentView] setPlay];
141 }
142
143 - (void)setPause
144 {
145     [[self contentView] setPause];
146 }
147
148 - (void)setStreamTitle:(NSString *)o_title
149 {
150     [[self contentView] setStreamTitle: o_title];
151 }
152
153 - (void)setStreamPos:(float) f_pos andTime:(NSString *)o_time
154 {
155     [[self contentView] setStreamPos:f_pos andTime: o_time];
156 }
157
158 - (void)setSeekable:(BOOL) b_seekable
159 {
160     [[self contentView] setSeekable: b_seekable];
161 }
162
163 - (void)setVolumeLevel: (float)f_volumeLevel
164 {
165     [[self contentView] setVolumeLevel: f_volumeLevel];
166 }
167
168 - (void)setNonActive:(id)noData
169 {
170     b_nonActive = YES;
171     [self orderOut: self];
172     
173     /* here's fadeOut, just without visibly fading */
174     b_displayed = NO;
175     [self setAlphaValue:0.0];
176     [self setFadeTimer:nil];
177     b_fadeQueued = NO;
178 }
179
180 - (void)setActive:(id)noData
181 {
182     if( [[VLCCoreInteraction sharedInstance] voutView] != nil )
183     {
184         if( [[[VLCCoreInteraction sharedInstance] voutView] isFullscreen] )
185         {
186             b_nonActive = NO;
187             [self fadeIn];
188         }
189     }
190 }
191
192 /* This routine is called repeatedly to fade in the window */
193 - (void)focus:(NSTimer *)timer
194 {
195     /* we need to push ourselves to front if the vout window was closed since our last display */
196     if( b_voutWasUpdated )
197     {
198         [self orderFront: self];
199         b_voutWasUpdated = NO;
200     }
201
202     if( [self alphaValue] < 1.0 )
203         [self setAlphaValue:[self alphaValue]+0.1];
204     if( [self alphaValue] >= 1.0 )
205     {
206         b_displayed = YES;
207         [self setAlphaValue: 1.0];
208         [self setFadeTimer:nil];
209         if( b_fadeQueued )
210         {
211             b_fadeQueued=NO;
212             [self setFadeTimer:[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(unfocus:) userInfo:NULL repeats:YES]];
213         }
214     }
215 }
216
217 /* This routine is called repeatedly to hide the window */
218 - (void)unfocus:(NSTimer *)timer
219 {
220     if( b_keptVisible )
221     {
222         b_keptVisible = NO;
223         b_fadeQueued = NO;
224         [self setFadeTimer: NULL];
225         [self fadeIn];
226         return;
227     }
228     if( [self alphaValue] > 0.0 )
229         [self setAlphaValue:[self alphaValue]-0.05];
230     if( [self alphaValue] <= 0.05 )
231     {
232         b_displayed = NO;
233         [self setAlphaValue:0.0];
234         [self setFadeTimer:nil];
235         if( b_fadeQueued )
236         {
237             b_fadeQueued=NO;
238             [self setFadeTimer:
239                 [NSTimer scheduledTimerWithTimeInterval:0.1 
240                                                  target:self 
241                                                selector:@selector(focus:) 
242                                                userInfo:NULL 
243                                                 repeats:YES]];
244         }
245     }
246 }
247
248 - (void)mouseExited:(NSEvent *)theEvent
249 {
250     /* give up our focus, so the vout may show us again without letting the user clicking it */
251     if( [[[VLCCoreInteraction sharedInstance] voutView] isFullscreen] )
252         [[[[VLCCoreInteraction sharedInstance] voutView] window] makeKeyWindow];
253 }
254
255 - (void)hideMouse
256 {
257     [NSCursor setHiddenUntilMouseMoves: YES];
258 }
259
260 - (void)fadeIn
261 {
262     /* in case that the user don't want us to appear, make sure we hide the mouse */
263
264     if( !config_GetInt( VLCIntf, "macosx-fspanel" ) )
265     {
266         float time = (float)var_CreateGetInteger( VLCIntf, "mouse-hide-timeout" ) / 1000.;
267         [self setFadeTimer:[NSTimer scheduledTimerWithTimeInterval:time target:self selector:@selector(hideMouse) userInfo:nil repeats:NO]];
268         return;
269     }
270
271     if( b_nonActive )
272         return;
273
274     [self orderFront: nil];
275     
276     if( [self alphaValue] < 1.0 || b_displayed != YES )
277     {
278         if (![self fadeTimer])
279             [self setFadeTimer:[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(focus:) userInfo:[NSNumber numberWithShort:1] repeats:YES]];
280         else if ([[[self fadeTimer] userInfo] shortValue]==0)
281             b_fadeQueued=YES;
282     }
283     [self autoHide];
284 }
285
286 - (void)fadeOut
287 {
288     if( NSPointInRect([NSEvent mouseLocation],[self frame]))
289         return;
290
291     if( ( [self alphaValue] > 0.0 ) )
292     {
293         if (![self fadeTimer])
294             [self setFadeTimer:[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(unfocus:) userInfo:[NSNumber numberWithShort:0] repeats:YES]];
295         else if ([[[self fadeTimer] userInfo] shortValue]==1)
296             b_fadeQueued=YES;
297     }
298 }
299
300 /* triggers a timer to autoHide us again after some seconds of no activity */
301 - (void)autoHide
302 {
303     /* this will tell the timer to start over again or to start at all */
304     b_keptVisible = YES;
305     
306     /* get us a valid timer */
307     if(! b_alreadyCounting )
308     {
309         i_timeToKeepVisibleInSec = var_CreateGetInteger( VLCIntf, "mouse-hide-timeout" ) / 500;
310         if( hideAgainTimer )
311         {
312             [hideAgainTimer invalidate];
313             [hideAgainTimer autorelease];
314         }
315         /* released in -autoHide and -dealloc */
316         hideAgainTimer = [[NSTimer scheduledTimerWithTimeInterval: 0.5
317                                                           target: self 
318                                                         selector: @selector(keepVisible:)
319                                                         userInfo: nil 
320                                                          repeats: YES] retain];
321         b_alreadyCounting = YES;
322     }
323 }
324
325 - (void)keepVisible:(NSTimer *)timer
326 {
327     /* if the user triggered an action, start over again */
328     if( b_keptVisible )
329         b_keptVisible = NO;
330
331     /* count down until we hide ourselfes again and do so if necessary */
332     if( --i_timeToKeepVisibleInSec < 1 )
333     {
334         [self hideMouse];
335         [self fadeOut];
336         [hideAgainTimer invalidate]; /* released in -autoHide and -dealloc */
337         b_alreadyCounting = NO;
338     }
339 }
340
341 /* A getter and setter for our main timer that handles window fading */
342 - (NSTimer *)fadeTimer
343 {
344     return fadeTimer;
345 }
346
347 - (void)setFadeTimer:(NSTimer *)timer
348 {
349     [timer retain];
350     [fadeTimer invalidate];
351     [fadeTimer autorelease];
352     fadeTimer=timer;
353 }
354
355 - (void)mouseDown:(NSEvent *)theEvent
356 {
357     mouseClic = [theEvent locationInWindow];
358 }
359
360 - (void)mouseDragged:(NSEvent *)theEvent
361 {
362     NSPoint point = [NSEvent mouseLocation];
363     point.x -= mouseClic.x;
364     point.y -= mouseClic.y;
365     [self setFrameOrigin:point];
366 }
367
368 - (BOOL)isDisplayed
369 {
370     return b_displayed;
371 }
372
373 - (void)setVoutWasUpdated: (int)i_newdevice;
374 {
375     b_voutWasUpdated = YES;
376     if( i_newdevice != i_device )
377     {
378         i_device = i_newdevice;
379         [self center];
380     }
381 }
382 @end
383
384 /*****************************************************************************
385  * FSPanelView
386  *****************************************************************************/
387 @implementation VLCFSPanelView
388
389 #define addButton( o_button, imageOff, imageOn, _x, _y, action )                                \
390     s_rc.origin.x = _x;                                                                         \
391     s_rc.origin.y = _y;                                                                         \
392     o_button = [[NSButton alloc] initWithFrame: s_rc];                                 \
393     [o_button setButtonType: NSMomentaryChangeButton];                                          \
394     [o_button setBezelStyle: NSRegularSquareBezelStyle];                                        \
395     [o_button setBordered: NO];                                                                 \
396     [o_button setFont:[NSFont systemFontOfSize:0]];                                             \
397     [o_button setImage:[NSImage imageNamed:imageOff]];                                 \
398     [o_button setAlternateImage:[NSImage imageNamed:imageOn]];                         \
399     [o_button sizeToFit];                                                                       \
400     [o_button setTarget: self];                                                                 \
401     [o_button setAction: @selector(action:)];                                                   \
402     [self addSubview:o_button];
403
404 #define addTextfield( class, o_text, align, font, color, size )                                    \
405     o_text = [[class alloc] initWithFrame: s_rc];                            \
406     [o_text setDrawsBackground: NO];                                                        \
407     [o_text setBordered: NO];                                                               \
408     [o_text setEditable: NO];                                                               \
409     [o_text setSelectable: NO];                                                             \
410     [o_text setStringValue: _NS("(no item is being played)")];                                                    \
411     [o_text setAlignment: align];                                                           \
412     [o_text setTextColor: [NSColor color]];                                                 \
413     [o_text setFont:[NSFont font:[NSFont smallSystemFontSize] - size]];                     \
414     [self addSubview:o_text];
415
416 - (id)initWithFrame:(NSRect)frameRect
417 {
418     id view = [super initWithFrame:frameRect];
419     fillColor = [[NSColor clearColor] retain];
420     NSRect s_rc = [self frame];
421     addButton( o_prev, @"fs_skip_previous" , @"fs_skip_previous_highlight", 174, 15, prev );
422     addButton( o_bwd, @"fs_rewind"        , @"fs_rewind_highlight"       , 211, 14, backward );
423     addButton( o_play, @"fs_play"          , @"fs_play_highlight"         , 267, 10, play );
424     addButton( o_fwd, @"fs_forward"       , @"fs_forward_highlight"      , 313, 14, forward );
425     addButton( o_next, @"fs_skip_next"     , @"fs_skip_next_highlight"    , 365, 15, next );
426     addButton( o_fullscreen, @"fs_exit_fullscreen", @"fs_exit_fullscreen_hightlight", 507, 13, windowAction );
427 /*
428     addButton( o_button, @"image (off state)", @"image (on state)", 38, 51, something );
429  */
430
431     /* time slider */
432     s_rc = [self frame];
433     s_rc.origin.x = 15;
434     s_rc.origin.y = 53;
435     s_rc.size.width = 518;
436     s_rc.size.height = 9;
437     o_fs_timeSlider = [[VLCFSTimeSlider alloc] initWithFrame: s_rc];
438     [o_fs_timeSlider setMinValue:0];
439     [o_fs_timeSlider setMaxValue:10000];
440     [o_fs_timeSlider setFloatValue: 0];
441     [o_fs_timeSlider setContinuous: YES];
442     [o_fs_timeSlider setTarget: self];
443     [o_fs_timeSlider setAction: @selector(fsTimeSliderUpdate:)];
444     [self addSubview: o_fs_timeSlider];
445
446     /* volume slider */
447     s_rc = [self frame];
448     s_rc.origin.x = 26;
449     s_rc.origin.y = 17.5;
450     s_rc.size.width = 95;
451     s_rc.size.height = 10;
452     o_fs_volumeSlider = [[VLCFSVolumeSlider alloc] initWithFrame: s_rc];
453     [o_fs_volumeSlider setMinValue:0];
454     [o_fs_volumeSlider setMaxValue:32];
455     [o_fs_volumeSlider setFloatValue: 0];
456     [o_fs_volumeSlider setContinuous: YES];
457     [o_fs_volumeSlider setTarget: self];
458     [o_fs_volumeSlider setAction: @selector(fsVolumeSliderUpdate:)];
459     [self addSubview: o_fs_volumeSlider];
460     
461     /* time counter and stream title output fields */
462     s_rc = [self frame];
463     s_rc.origin.x = 98;
464     s_rc.origin.y = 64;
465     s_rc.size.width = 352;
466     s_rc.size.height = 14;
467     addTextfield( NSTextField, o_streamTitle_txt, NSCenterTextAlignment, systemFontOfSize, whiteColor, 0 );
468     s_rc.origin.x = 481;
469     s_rc.origin.y = 64;
470     s_rc.size.width = 55;
471     addTextfield( VLCTimeField, o_streamPosition_txt, NSRightTextAlignment, systemFontOfSize, whiteColor, 0 );
472
473     return view;
474 }
475
476 - (void)dealloc
477 {
478     [o_fs_timeSlider release];
479     [o_fs_volumeSlider release];
480     [o_prev release];
481     [o_next release];
482     [o_bwd release];
483     [o_play release];
484     [o_fwd release];
485     [o_fullscreen release];
486     [o_streamTitle_txt release];
487     [o_streamPosition_txt release];
488     [super dealloc];
489 }
490
491 - (void)setPlay
492 {
493     [o_play setImage:[NSImage imageNamed:@"fs_play"]];
494     [o_play setAlternateImage: [NSImage imageNamed:@"fs_play_highlight"]];
495 }
496
497 - (void)setPause
498 {
499     [o_play setImage: [NSImage imageNamed:@"fs_pause"]];
500     [o_play setAlternateImage: [NSImage imageNamed:@"fs_pause_highlight"]];
501 }
502
503 - (void)setStreamTitle:(NSString *)o_title
504 {
505     [o_streamTitle_txt setStringValue: o_title];
506 }
507
508 - (void)setStreamPos:(float) f_pos andTime:(NSString *)o_time
509 {
510     [o_streamPosition_txt setStringValue: o_time];
511     [o_fs_timeSlider setFloatValue: f_pos];
512 }
513
514 - (void)setSeekable:(BOOL)b_seekable
515 {
516     [o_bwd setEnabled: b_seekable];
517     [o_fwd setEnabled: b_seekable];
518     [o_fs_timeSlider setEnabled: b_seekable];
519 }
520
521 - (void)setVolumeLevel: (float)f_volumeLevel
522 {
523     [o_fs_volumeSlider setFloatValue: f_volumeLevel];
524 }
525
526 - (IBAction)play:(id)sender
527 {
528     [[[VLCMain sharedInstance] controls] play: sender];
529 }
530
531 - (IBAction)forward:(id)sender
532 {
533     [[[VLCMain sharedInstance] controls] forward: sender];
534 }
535
536 - (IBAction)backward:(id)sender
537 {
538     [[[VLCMain sharedInstance] controls] backward: sender];
539 }
540
541 - (IBAction)prev:(id)sender
542 {
543     [[[VLCMain sharedInstance] controls] prev: sender];
544 }
545
546 - (IBAction)next:(id)sender
547 {
548     [[[VLCMain sharedInstance] controls] next: sender];
549 }
550
551 - (IBAction)windowAction:(id)sender
552 {
553     [[[VLCMain sharedInstance] controls] windowAction: sender];
554 }
555
556 - (IBAction)fsTimeSliderUpdate:(id)sender
557 {
558     [[VLCMainWindow sharedInstance] updateTimeSlider];
559 }
560
561 - (IBAction)fsVolumeSliderUpdate:(id)sender
562 {
563     [[VLCCoreInteraction sharedInstance] setVolume: [sender intValue]];
564 }
565
566 #define addImage(image, _x, _y, mode, _width)                                               \
567     img = [NSImage imageNamed:image];                                              \
568     image_rect.size = [img size];                                                           \
569     image_rect.origin.x = 0;                                                                \
570     image_rect.origin.y = 0;                                                                \
571     frame.origin.x = _x;                                                                    \
572     frame.origin.y = _y;                                                                    \
573     frame.size = [img size];                                                                \
574     if( _width ) frame.size.width = _width;                                                 \
575     [img drawInRect:frame fromRect:image_rect operation:mode fraction:1];
576
577 - (void)drawRect:(NSRect)rect
578 {
579     NSRect frame = [self frame];
580     NSRect image_rect;
581     NSImage *img;
582     addImage( @"fs_background", 0, 0, NSCompositeCopy, 0 );
583     addImage( @"fs_volume_slider_bar", 26, 22, NSCompositeSourceOver, 0 );
584     addImage( @"fs_volume_mute", 16, 18, NSCompositeSourceOver, 0 );
585     addImage( @"fs_volume_max", 124, 17, NSCompositeSourceOver, 0 );
586     addImage( @"fs_time_slider", 15, 53, NSCompositeSourceOver, 0);
587 }
588
589 @end
590
591 /*****************************************************************************
592  * VLCFSTimeSlider
593  *****************************************************************************/
594 @implementation VLCFSTimeSlider
595 - (void)drawKnobInRect:(NSRect)knobRect
596 {
597     NSRect image_rect;
598     NSImage *img = [NSImage imageNamed:@"fs_time_slider_knob_highlight"];
599     image_rect.size = [img size];
600     image_rect.origin.x = 0;
601     image_rect.origin.y = 0;
602     knobRect.origin.x += (knobRect.size.width - image_rect.size.width) / 2;
603     knobRect.size.width = image_rect.size.width;
604     knobRect.size.height = image_rect.size.height;
605     [img drawInRect:knobRect fromRect:image_rect operation:NSCompositeSourceOver fraction:1];
606 }
607
608 - (void)drawRect:(NSRect)rect
609 {
610     /* Draw default to make sure the slider behaves correctly */
611     [[NSGraphicsContext currentContext] saveGraphicsState];
612     NSRectClip(NSZeroRect);
613     [super drawRect:rect];
614     [[NSGraphicsContext currentContext] restoreGraphicsState];
615     
616     NSRect knobRect = [[self cell] knobRectFlipped:NO];
617     knobRect.origin.y+=7.5;
618     [[[NSColor blackColor] colorWithAlphaComponent:0.6] set];
619     [self drawKnobInRect: knobRect];
620 }
621
622 @end
623
624 /*****************************************************************************
625 * VLCFSVolumeSlider
626 *****************************************************************************/
627 @implementation VLCFSVolumeSlider
628 - (void)drawKnobInRect:(NSRect) knobRect
629 {
630     NSRect image_rect;
631     NSImage *img = [NSImage imageNamed:@"fs_volume_slider_knob"];
632     image_rect.size = [img size];
633     image_rect.origin.x = 0;
634     image_rect.origin.y = 0;
635     knobRect.origin.x += (knobRect.size.width - image_rect.size.width) / 2;
636     knobRect.size.width = image_rect.size.width;
637     knobRect.size.height = image_rect.size.height;
638     [img drawInRect:knobRect fromRect:image_rect operation:NSCompositeSourceOver fraction:1];
639 }
640
641 - (void)drawRect:(NSRect)rect
642 {
643     /* Draw default to make sure the slider behaves correctly */
644     [[NSGraphicsContext currentContext] saveGraphicsState];
645     NSRectClip(NSZeroRect);
646     [super drawRect:rect];
647     [[NSGraphicsContext currentContext] restoreGraphicsState];
648     
649     NSRect knobRect = [[self cell] knobRectFlipped:NO];
650     knobRect.origin.y+=6;
651     [[[NSColor blackColor] colorWithAlphaComponent:0.6] set];
652     [self drawKnobInRect: knobRect];
653 }
654
655 @end
656