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