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