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