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