]> git.sesse.net Git - vlc/blob - modules/gui/macosx/fspanel.m
Merge branch 'master' into lpcm_encoder
[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:(NSUInteger)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] controls] voutView] != nil )
182     {
183         if( [[[[VLCMain sharedInstance] controls] voutView] isFullscreen] )
184         {
185             b_nonActive = NO;
186             [self fadeIn];
187         }
188     }
189 }
190
191 /* This routine is called repeatedly to fade in the window */
192 - (void)focus:(NSTimer *)timer
193 {
194     /* we need to push ourselves to front if the vout window was closed since our last display */
195     if( b_voutWasUpdated )
196     {
197         [self orderFront: self];
198         b_voutWasUpdated = NO;
199     }
200
201     if( [self alphaValue] < 1.0 )
202         [self setAlphaValue:[self alphaValue]+0.1];
203     if( [self alphaValue] >= 1.0 )
204     {
205         b_displayed = YES;
206         [self setAlphaValue: 1.0];
207         [self setFadeTimer:nil];
208         if( b_fadeQueued )
209         {
210             b_fadeQueued=NO;
211             [self setFadeTimer:[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(unfocus:) userInfo:NULL repeats:YES]];
212         }
213     }
214 }
215
216 /* This routine is called repeatedly to hide the window */
217 - (void)unfocus:(NSTimer *)timer
218 {
219     if( b_keptVisible )
220     {
221         b_keptVisible = NO;
222         b_fadeQueued = NO;
223         [self setFadeTimer: NULL];
224         [self fadeIn];
225         return;
226     }
227     if( [self alphaValue] > 0.0 )
228         [self setAlphaValue:[self alphaValue]-0.05];
229     if( [self alphaValue] <= 0.05 )
230     {
231         b_displayed = NO;
232         [self setAlphaValue:0.0];
233         [self setFadeTimer:nil];
234         if( b_fadeQueued )
235         {
236             b_fadeQueued=NO;
237             [self setFadeTimer:
238                 [NSTimer scheduledTimerWithTimeInterval:0.1 
239                                                  target:self 
240                                                selector:@selector(focus:) 
241                                                userInfo:NULL 
242                                                 repeats:YES]];
243         }
244     }
245 }
246
247 - (void)mouseExited:(NSEvent *)theEvent
248 {
249     /* give up our focus, so the vout may show us again without letting the user clicking it */
250     if( [[[[VLCMain sharedInstance] controls] voutView] isFullscreen] )
251         [[[[[VLCMain sharedInstance] controls] voutView] window] makeKeyWindow];
252 }
253
254 - (void)hideMouse
255 {
256     [NSCursor setHiddenUntilMouseMoves: YES];
257 }
258
259 - (void)fadeIn
260 {
261     /* in case that the user don't want us to appear, make sure we hide the mouse */
262
263     if( !config_GetInt( VLCIntf, "macosx-fspanel" ) )
264     {
265         float time = (float)var_CreateGetInteger( VLCIntf, "mouse-hide-timeout" ) / 1000.;
266         [self setFadeTimer:[NSTimer scheduledTimerWithTimeInterval:time target:self selector:@selector(hideMouse) userInfo:nil repeats:NO]];
267         return;
268     }
269
270     if( b_nonActive )
271         return;
272
273     [self orderFront: nil];
274     
275     if( [self alphaValue] < 1.0 || b_displayed != YES )
276     {
277         if (![self fadeTimer])
278             [self setFadeTimer:[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(focus:) userInfo:[NSNumber numberWithShort:1] repeats:YES]];
279         else if ([[[self fadeTimer] userInfo] shortValue]==0)
280             b_fadeQueued=YES;
281     }
282     [self autoHide];
283 }
284
285 - (void)fadeOut
286 {
287     if( NSPointInRect([NSEvent mouseLocation],[self frame]))
288         return;
289
290     if( ( [self alphaValue] > 0.0 ) )
291     {
292         if (![self fadeTimer])
293             [self setFadeTimer:[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(unfocus:) userInfo:[NSNumber numberWithShort:0] repeats:YES]];
294         else if ([[[self fadeTimer] userInfo] shortValue]==1)
295             b_fadeQueued=YES;
296     }
297 }
298
299 /* triggers a timer to autoHide us again after some seconds of no activity */
300 - (void)autoHide
301 {
302     /* this will tell the timer to start over again or to start at all */
303     b_keptVisible = YES;
304     
305     /* get us a valid timer */
306     if(! b_alreadyCounting )
307     {
308         i_timeToKeepVisibleInSec = var_CreateGetInteger( VLCIntf, "mouse-hide-timeout" ) / 500;
309         if( hideAgainTimer )
310         {
311             [hideAgainTimer invalidate];
312             [hideAgainTimer autorelease];
313         }
314         /* released in -autoHide and -dealloc */
315         hideAgainTimer = [[NSTimer scheduledTimerWithTimeInterval: 0.5
316                                                           target: self 
317                                                         selector: @selector(keepVisible:)
318                                                         userInfo: nil 
319                                                          repeats: YES] retain];
320         b_alreadyCounting = YES;
321     }
322 }
323
324 - (void)keepVisible:(NSTimer *)timer
325 {
326     /* if the user triggered an action, start over again */
327     if( b_keptVisible )
328         b_keptVisible = NO;
329
330     /* count down until we hide ourselfes again and do so if necessary */
331     if( --i_timeToKeepVisibleInSec < 1 )
332     {
333         [self hideMouse];
334         [self fadeOut];
335         [hideAgainTimer invalidate]; /* released in -autoHide and -dealloc */
336         b_alreadyCounting = NO;
337     }
338 }
339
340 /* A getter and setter for our main timer that handles window fading */
341 - (NSTimer *)fadeTimer
342 {
343     return fadeTimer;
344 }
345
346 - (void)setFadeTimer:(NSTimer *)timer
347 {
348     [timer retain];
349     [fadeTimer invalidate];
350     [fadeTimer autorelease];
351     fadeTimer=timer;
352 }
353
354 - (void)mouseDown:(NSEvent *)theEvent
355 {
356     mouseClic = [theEvent locationInWindow];
357 }
358
359 - (void)mouseDragged:(NSEvent *)theEvent
360 {
361     NSPoint point = [NSEvent mouseLocation];
362     point.x -= mouseClic.x;
363     point.y -= mouseClic.y;
364     [self setFrameOrigin:point];
365 }
366
367 - (BOOL)isDisplayed
368 {
369     return b_displayed;
370 }
371
372 - (void)setVoutWasUpdated: (int)i_newdevice;
373 {
374     b_voutWasUpdated = YES;
375     if( i_newdevice != i_device )
376     {
377         i_device = i_newdevice;
378         [self center];
379     }
380 }
381 @end
382
383 /*****************************************************************************
384  * FSPanelView
385  *****************************************************************************/
386 @implementation VLCFSPanelView
387
388 #define addButton( o_button, imageOff, imageOn, _x, _y, action )                                \
389     s_rc.origin.x = _x;                                                                         \
390     s_rc.origin.y = _y;                                                                         \
391     o_button = [[NSButton alloc] initWithFrame: s_rc];                                 \
392     [o_button setButtonType: NSMomentaryChangeButton];                                          \
393     [o_button setBezelStyle: NSRegularSquareBezelStyle];                                        \
394     [o_button setBordered: NO];                                                                 \
395     [o_button setFont:[NSFont systemFontOfSize:0]];                                             \
396     [o_button setImage:[NSImage imageNamed:imageOff]];                                 \
397     [o_button setAlternateImage:[NSImage imageNamed:imageOn]];                         \
398     [o_button sizeToFit];                                                                       \
399     [o_button setTarget: self];                                                                 \
400     [o_button setAction: @selector(action:)];                                                   \
401     [self addSubview:o_button];
402
403 #define addTextfield( o_text, align, font, color, size )                                    \
404     o_text = [[NSTextField alloc] initWithFrame: s_rc];                            \
405     [o_text setDrawsBackground: NO];                                                        \
406     [o_text setBordered: NO];                                                               \
407     [o_text setEditable: NO];                                                               \
408     [o_text setSelectable: NO];                                                             \
409     [o_text setStringValue: _NS("(no item is being played)")];                                                    \
410     [o_text setAlignment: align];                                                           \
411     [o_text setTextColor: [NSColor color]];                                                 \
412     [o_text setFont:[NSFont font:[NSFont smallSystemFontSize] - size]];                     \
413     [self addSubview:o_text];
414
415 - (id)initWithFrame:(NSRect)frameRect
416 {
417     id view = [super initWithFrame:frameRect];
418     fillColor = [[NSColor clearColor] retain];
419     NSRect s_rc = [self frame];
420     addButton( o_prev, @"fs_skip_previous" , @"fs_skip_previous_highlight", 174, 15, prev );
421     addButton( o_bwd, @"fs_rewind"        , @"fs_rewind_highlight"       , 211, 14, backward );
422     addButton( o_play, @"fs_play"          , @"fs_play_highlight"         , 267, 10, play );
423     addButton( o_fwd, @"fs_forward"       , @"fs_forward_highlight"      , 313, 14, forward );
424     addButton( o_next, @"fs_skip_next"     , @"fs_skip_next_highlight"    , 365, 15, next );
425     addButton( o_fullscreen, @"fs_exit_fullscreen", @"fs_exit_fullscreen_hightlight", 507, 13, windowAction );
426 /*
427     addButton( o_button, @"image (off state)", @"image (on state)", 38, 51, something );
428  */
429
430     /* time slider */
431     s_rc = [self frame];
432     s_rc.origin.x = 15;
433     s_rc.origin.y = 53;
434     s_rc.size.width = 518;
435     s_rc.size.height = 9;
436     o_fs_timeSlider = [[VLCFSTimeSlider alloc] initWithFrame: s_rc];
437     [o_fs_timeSlider setMinValue:0];
438     [o_fs_timeSlider setMaxValue:10000];
439     [o_fs_timeSlider setFloatValue: 0];
440     [o_fs_timeSlider setContinuous: YES];
441     [o_fs_timeSlider setTarget: self];
442     [o_fs_timeSlider setAction: @selector(fsTimeSliderUpdate:)];
443     [self addSubview: o_fs_timeSlider];
444
445     /* volume slider */
446     s_rc = [self frame];
447     s_rc.origin.x = 26;
448     s_rc.origin.y = 17.5;
449     s_rc.size.width = 95;
450     s_rc.size.height = 10;
451     o_fs_volumeSlider = [[VLCFSVolumeSlider alloc] initWithFrame: s_rc];
452     [o_fs_volumeSlider setMinValue:0];
453     [o_fs_volumeSlider setMaxValue:32];
454     [o_fs_volumeSlider setFloatValue: 0];
455     [o_fs_volumeSlider setContinuous: YES];
456     [o_fs_volumeSlider setTarget: self];
457     [o_fs_volumeSlider setAction: @selector(fsVolumeSliderUpdate:)];
458     [self addSubview: o_fs_volumeSlider];
459     
460     /* time counter and stream title output fields */
461     s_rc = [self frame];
462     s_rc.origin.x = 98;
463     s_rc.origin.y = 64;
464     s_rc.size.width = 352;
465     s_rc.size.height = 14;
466     addTextfield( o_streamTitle_txt, NSCenterTextAlignment, systemFontOfSize, whiteColor, 0 );
467     s_rc.origin.x = 486;
468     s_rc.origin.y = 64;
469     s_rc.size.width = 50;
470     addTextfield( o_streamPosition_txt, NSRightTextAlignment, systemFontOfSize, whiteColor, 0 );
471
472     return view;
473 }
474
475 - (void)dealloc
476 {
477     [o_fs_timeSlider release];
478     [o_fs_volumeSlider release];
479     [o_prev release];
480     [o_next release];
481     [o_bwd release];
482     [o_play release];
483     [o_fwd release];
484     [o_fullscreen release];
485     [o_streamTitle_txt release];
486     [o_streamPosition_txt release];
487     [super dealloc];
488 }
489
490 - (void)setPlay
491 {
492     [o_play setImage:[NSImage imageNamed:@"fs_play"]];
493     [o_play setAlternateImage: [NSImage imageNamed:@"fs_play_highlight"]];
494 }
495
496 - (void)setPause
497 {
498     [o_play setImage: [NSImage imageNamed:@"fs_pause"]];
499     [o_play setAlternateImage: [NSImage imageNamed:@"fs_pause_highlight"]];
500 }
501
502 - (void)setStreamTitle:(NSString *)o_title
503 {
504     [o_streamTitle_txt setStringValue: o_title];
505 }
506
507 - (void)setStreamPos:(float) f_pos andTime:(NSString *)o_time
508 {
509     [o_streamPosition_txt setStringValue: o_time];
510     [o_fs_timeSlider setFloatValue: f_pos];
511 }
512
513 - (void)setSeekable:(BOOL)b_seekable
514 {
515     [o_bwd setEnabled: b_seekable];
516     [o_fwd setEnabled: b_seekable];
517     [o_fs_timeSlider setEnabled: b_seekable];
518 }
519
520 - (void)setVolumeLevel: (float)f_volumeLevel
521 {
522     [o_fs_volumeSlider setFloatValue: f_volumeLevel];
523 }
524
525 - (IBAction)play:(id)sender
526 {
527     [[[VLCMain sharedInstance] controls] play: sender];
528 }
529
530 - (IBAction)forward:(id)sender
531 {
532     [[[VLCMain sharedInstance] controls] forward: sender];
533 }
534
535 - (IBAction)backward:(id)sender
536 {
537     [[[VLCMain sharedInstance] controls] backward: sender];
538 }
539
540 - (IBAction)prev:(id)sender
541 {
542     [[[VLCMain sharedInstance] controls] prev: sender];
543 }
544
545 - (IBAction)next:(id)sender
546 {
547     [[[VLCMain sharedInstance] controls] next: sender];
548 }
549
550 - (IBAction)windowAction:(id)sender
551 {
552     [[[VLCMain sharedInstance] controls] windowAction: sender];
553 }
554
555 - (IBAction)fsTimeSliderUpdate:(id)sender
556 {
557     [[VLCMain sharedInstance] timesliderUpdate: sender];
558 }
559
560 - (IBAction)fsVolumeSliderUpdate:(id)sender
561 {
562     [[[VLCMain sharedInstance] controls] volumeSliderUpdated: sender];
563 }
564
565 #define addImage(image, _x, _y, mode, _width)                                               \
566     img = [NSImage imageNamed:image];                                              \
567     image_rect.size = [img size];                                                           \
568     image_rect.origin.x = 0;                                                                \
569     image_rect.origin.y = 0;                                                                \
570     frame.origin.x = _x;                                                                    \
571     frame.origin.y = _y;                                                                    \
572     frame.size = [img size];                                                                \
573     if( _width ) frame.size.width = _width;                                                 \
574     [img drawInRect:frame fromRect:image_rect operation:mode fraction:1];
575
576 - (void)drawRect:(NSRect)rect
577 {
578     NSRect frame = [self frame];
579     NSRect image_rect;
580     NSImage *img;
581     addImage( @"fs_background", 0, 0, NSCompositeCopy, 0 );
582     addImage( @"fs_volume_slider_bar", 26, 22, NSCompositeSourceOver, 0 );
583     addImage( @"fs_volume_mute", 16, 18, NSCompositeSourceOver, 0 );
584     addImage( @"fs_volume_max", 124, 17, NSCompositeSourceOver, 0 );
585     addImage( @"fs_time_slider", 15, 53, NSCompositeSourceOver, 0);
586 }
587
588 @end
589
590 /*****************************************************************************
591  * VLCFSTimeSlider
592  *****************************************************************************/
593 @implementation VLCFSTimeSlider
594 - (void)drawKnobInRect:(NSRect)knobRect
595 {
596     NSRect image_rect;
597     NSImage *img = [NSImage imageNamed:@"fs_time_slider_knob_highlight"];
598     image_rect.size = [img size];
599     image_rect.origin.x = 0;
600     image_rect.origin.y = 0;
601     knobRect.origin.x += (knobRect.size.width - image_rect.size.width) / 2;
602     knobRect.size.width = image_rect.size.width;
603     knobRect.size.height = image_rect.size.height;
604     [img drawInRect:knobRect fromRect:image_rect operation:NSCompositeSourceOver fraction:1];
605 }
606
607 - (void)drawRect:(NSRect)rect
608 {
609     /* Draw default to make sure the slider behaves correctly */
610     [[NSGraphicsContext currentContext] saveGraphicsState];
611     NSRectClip(NSZeroRect);
612     [super drawRect:rect];
613     [[NSGraphicsContext currentContext] restoreGraphicsState];
614     
615     NSRect knobRect = [[self cell] knobRectFlipped:NO];
616     knobRect.origin.y+=7.5;
617     [[[NSColor blackColor] colorWithAlphaComponent:0.6] set];
618     [self drawKnobInRect: knobRect];
619 }
620
621 @end
622
623 /*****************************************************************************
624 * VLCFSVolumeSlider
625 *****************************************************************************/
626 @implementation VLCFSVolumeSlider
627 - (void)drawKnobInRect:(NSRect) knobRect
628 {
629     NSRect image_rect;
630     NSImage *img = [NSImage imageNamed:@"fs_volume_slider_knob"];
631     image_rect.size = [img size];
632     image_rect.origin.x = 0;
633     image_rect.origin.y = 0;
634     knobRect.origin.x += (knobRect.size.width - image_rect.size.width) / 2;
635     knobRect.size.width = image_rect.size.width;
636     knobRect.size.height = image_rect.size.height;
637     [img drawInRect:knobRect fromRect:image_rect operation:NSCompositeSourceOver fraction:1];
638 }
639
640 - (void)drawRect:(NSRect)rect
641 {
642     /* Draw default to make sure the slider behaves correctly */
643     [[NSGraphicsContext currentContext] saveGraphicsState];
644     NSRectClip(NSZeroRect);
645     [super drawRect:rect];
646     [[NSGraphicsContext currentContext] restoreGraphicsState];
647     
648     NSRect knobRect = [[self cell] knobRectFlipped:NO];
649     knobRect.origin.y+=6;
650     [[[NSColor blackColor] colorWithAlphaComponent:0.6] set];
651     [self drawKnobInRect: knobRect];
652 }
653
654 @end
655