8116be5a398d03c1af75411b3b66a17df256a6e1
[vlc.git] / modules / gui / macosx / MainWindow.m
1 /*****************************************************************************
2  * MainWindow.h: MacOS X interface module
3  *****************************************************************************
4  * Copyright (C) 2002-2011 VideoLAN
5  * $Id$
6  *
7  * Authors: Felix Paul K├╝hne <fkuehne -at- videolan -dot- org>
8  *          Jon Lech Johansen <jon-vl@nanocrew.net>
9  *          Christophe Massiot <massiot@via.ecp.fr>
10  *          Derk-Jan Hartman <hartman at videolan.org>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *****************************************************************************/
26
27 #import "MainWindow.h"
28 #import "intf.h"
29 #import "CoreInteraction.h"
30 #import "AudioEffects.h"
31 #import "MainMenu.h"
32 #import "controls.h" // TODO: remove me
33 #import <vlc_playlist.h>
34 #import <vlc_aout_intf.h>
35
36 @implementation VLCMainWindow
37 static VLCMainWindow *_o_sharedInstance = nil;
38
39 + (VLCMainWindow *)sharedInstance
40 {
41     return _o_sharedInstance ? _o_sharedInstance : [[self alloc] init];
42 }
43
44 #pragma mark -
45 #pragma mark Initialization
46
47 - (id)init
48 {
49     if( _o_sharedInstance)
50     {
51         [self dealloc];
52         return _o_sharedInstance;
53     }
54     else
55         _o_sharedInstance = [super init];
56
57     return _o_sharedInstance;
58 }
59
60 - (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)styleMask
61                   backing:(NSBackingStoreType)backingType defer:(BOOL)flag
62 {
63     /* FIXME: this should enable the SnowLeopard window style, however, it leads to ugly artifacts
64      *        needs some further investigation! -- feepk */
65      BOOL b_useTextured = YES;
66
67      if( [[NSWindow class] instancesRespondToSelector:@selector(setContentBorderThickness:forEdge:)] )
68      {
69      b_useTextured = NO;
70      styleMask ^= NSTexturedBackgroundWindowMask;
71      }
72
73     self = [super initWithContentRect:contentRect styleMask:styleMask //& ~NSTitledWindowMask
74                               backing:backingType defer:flag];
75
76     [[VLCMain sharedInstance] updateTogglePlaylistState];
77
78     // FIXME: see above...
79     if(! b_useTextured )
80      [self setContentBorderThickness:28.0 forEdge:NSMinYEdge];
81
82     /* we want to be moveable regardless of our style */
83     [self setMovableByWindowBackground: YES];
84
85     /* we don't want this window to be restored on relaunch */
86     if ([self respondsToSelector:@selector(setRestorable:)])
87         [self setRestorable:NO];
88
89     return self;
90 }
91
92 - (BOOL)performKeyEquivalent:(NSEvent *)o_event
93 {
94     /* We indeed want to prioritize Cocoa key equivalent against libvlc,
95      so we perform the menu equivalent now. */
96     if([[NSApp mainMenu] performKeyEquivalent:o_event])
97         return TRUE;
98
99     return [[VLCMain sharedInstance] hasDefinedShortcutKey:o_event] || [(VLCControls *)[[VLCMain sharedInstance] controls] keyEvent:o_event];
100 }
101
102 - (void)dealloc
103 {
104     config_PutInt( VLCIntf->p_libvlc, "volume", i_lastShownVolume );
105     [super dealloc];
106 }
107
108 - (void)awakeFromNib
109 {
110     b_dark_interface = config_GetInt( VLCIntf, "macosx-interfacestyle" );
111     i_lastShownVolume = -1;
112
113     [o_play_btn setToolTip: _NS("Play/Pause")];
114     [o_bwd_btn setToolTip: _NS("Backward")];
115     [o_fwd_btn setToolTip: _NS("Forward")];
116     [o_stop_btn setToolTip: _NS("Stop")];
117     [o_playlist_btn setToolTip: _NS("Show/Hide Playlist")];
118     [o_repeat_btn setToolTip: _NS("Repeat")];
119     [o_shuffle_btn setToolTip: _NS("Shuffle")];
120     [o_effects_btn setToolTip: _NS("Effects")];
121     [o_fullscreen_btn setToolTip: _NS("Toggle Fullscreen mode")];
122     [[o_search_fld cell] setPlaceholderString: _NS("Search")];
123     [o_volume_sld setToolTip: _NS("Volume")];
124     [o_volume_down_btn setToolTip: _NS("Mute")];
125     [o_volume_up_btn setToolTip: _NS("Full Volume")];
126     [o_time_sld setToolTip: _NS("Position")];
127
128     if (!b_dark_interface) {
129         [o_bottombar_view setImage: [NSImage imageNamed:@"bottom-background"]];
130         [o_bwd_btn setImage: [NSImage imageNamed:@"back"]];
131         [o_bwd_btn setAlternateImage: [NSImage imageNamed:@"back-pressed"]];
132         o_play_img = [[NSImage imageNamed:@"play"] retain];
133         o_play_pressed_img = [[NSImage imageNamed:@"play-pressed"] retain];
134         o_pause_img = [[NSImage imageNamed:@"pause"] retain];
135         o_pause_pressed_img = [[NSImage imageNamed:@"pause-pressed"] retain];
136         [o_fwd_btn setImage: [NSImage imageNamed:@"forward"]];
137         [o_fwd_btn setAlternateImage: [NSImage imageNamed:@"forward-pressed"]];
138         [o_stop_btn setImage: [NSImage imageNamed:@"stop"]];
139         [o_stop_btn setAlternateImage: [NSImage imageNamed:@"stop-pressed"]];
140         [o_playlist_btn setImage: [NSImage imageNamed:@"playlist"]];
141         [o_playlist_btn setAlternateImage: [NSImage imageNamed:@"playlist-pressed"]];
142         o_repeat_img = [[NSImage imageNamed:@"repeat"] retain];
143         o_repeat_pressed_img = [[NSImage imageNamed:@"repeat-pressed"] retain];
144         o_repeat_all_img  = [[NSImage imageNamed:@"repeat-all"] retain];
145         o_repeat_all_pressed_img = [[NSImage imageNamed:@"repeat-all-pressed"] retain];
146         o_repeat_one_img = [[NSImage imageNamed:@"repeat-one"] retain];
147         o_repeat_one_pressed_img = [[NSImage imageNamed:@"repeat-one-pressed"] retain];
148         o_shuffle_img = [[NSImage imageNamed:@"shuffle"] retain];
149         o_shuffle_pressed_img = [[NSImage imageNamed:@"shuffle-pressed"] retain];
150         o_shuffle_on_img = [[NSImage imageNamed:@"shuffle-blue"] retain];
151         o_shuffle_on_pressed_img = [[NSImage imageNamed:@"shuffle-blue-pressed"] retain];
152         [o_time_fld setTextColor: [NSColor colorWithCalibratedRed:64.0 green:64.0 blue:64.0 alpha:100.0]];
153         [o_time_sld_left_view setImage: [NSImage imageNamed:@"progression-track-wrapper-left"]];
154         [o_time_sld_middle_view setImage: [NSImage imageNamed:@"progression-track-wrapper-middle"]];
155         [o_time_sld_right_view setImage: [NSImage imageNamed:@"progression-track-wrapper-right"]];
156         [o_volume_down_btn setImage: [NSImage imageNamed:@"volume-low"]];
157         [o_volume_track_view setImage: [NSImage imageNamed:@"volumetrack"]];
158         [o_volume_up_btn setImage: [NSImage imageNamed:@"volume-high"]];
159         [o_effects_btn setImage: [NSImage imageNamed:@"effects-double-buttons"]];
160         [o_effects_btn setAlternateImage: [NSImage imageNamed:@"effects-double-buttons-pressed"]];
161         [o_fullscreen_btn setImage: [NSImage imageNamed:@"fullscreen-double-buttons"]];
162         [o_fullscreen_btn setAlternateImage: [NSImage imageNamed:@"fullscreen-double-buttons-pressed"]];
163     }
164     else
165     {
166         /* TODO: we also need to change the window style here... */
167         [o_bottombar_view setImage: [NSImage imageNamed:@"bottom-background_dark"]];
168         [o_bwd_btn setImage: [NSImage imageNamed:@"back_dark"]];
169         [o_bwd_btn setAlternateImage: [NSImage imageNamed:@"back-pressed_dark"]];
170         o_play_img = [[NSImage imageNamed:@"play_dark"] retain];
171         o_play_pressed_img = [[NSImage imageNamed:@"play-pressed_dark"] retain];
172         o_pause_img = [[NSImage imageNamed:@"pause_dark"] retain];
173         o_pause_pressed_img = [[NSImage imageNamed:@"pause-pressed_dark"] retain];
174         [o_fwd_btn setImage: [NSImage imageNamed:@"forward_dark"]];
175         [o_fwd_btn setAlternateImage: [NSImage imageNamed:@"forward-pressed_dark"]];
176         [o_stop_btn setImage: [NSImage imageNamed:@"stop_dark"]];
177         [o_stop_btn setAlternateImage: [NSImage imageNamed:@"stop-pressed_dark"]];
178         [o_playlist_btn setImage: [NSImage imageNamed:@"playlist_dark"]];
179         [o_playlist_btn setAlternateImage: [NSImage imageNamed:@"playlist-pressed_dark"]];
180         o_repeat_img = [[NSImage imageNamed:@"repeat_dark"] retain];
181         o_repeat_pressed_img = [[NSImage imageNamed:@"repeat-pressed_dark"] retain];
182         o_repeat_all_img  = [[NSImage imageNamed:@"repeat-all-blue_dark"] retain];
183         o_repeat_all_pressed_img = [[NSImage imageNamed:@"repeat-all-blue-pressed_dark"] retain];
184         o_repeat_one_img = [[NSImage imageNamed:@"repeat-one-blue_dark"] retain];
185         o_repeat_one_pressed_img = [[NSImage imageNamed:@"repeat-one-blue-pressed_dark"] retain];
186         o_shuffle_img = [[NSImage imageNamed:@"shuffle_dark"] retain];
187         o_shuffle_pressed_img = [[NSImage imageNamed:@"shuffle-pressed_dark"] retain];
188         o_shuffle_on_img = [[NSImage imageNamed:@"shuffle-blue_dark"] retain];
189         o_shuffle_on_pressed_img = [[NSImage imageNamed:@"shuffle-blue-pressed_dark"] retain];
190         [o_time_fld setTextColor: [NSColor colorWithCalibratedRed:229.0 green:229.0 blue:229.0 alpha:100.0]];
191         [o_time_sld_left_view setImage: [NSImage imageNamed:@"progression-track-wrapper-left_dark"]];
192         [o_time_sld_middle_view setImage: [NSImage imageNamed:@"progression-track-wrapper-middle_dark"]];
193         [o_time_sld_right_view setImage: [NSImage imageNamed:@"progression-track-wrapper-right_dark"]];
194         [o_volume_down_btn setImage: [NSImage imageNamed:@"volume-low_dark"]];
195         [o_volume_track_view setImage: [NSImage imageNamed:@"volume-slider-track_dark"]];
196         [o_volume_up_btn setImage: [NSImage imageNamed:@"volume-high_dark"]];
197         [o_effects_btn setImage: [NSImage imageNamed:@"effects-double-buttons_dark"]];
198         [o_effects_btn setAlternateImage: [NSImage imageNamed:@"effects-double-buttons-pressed_dark"]];
199         [o_fullscreen_btn setImage: [NSImage imageNamed:@"fullscreen-double-buttons_dark"]];
200         [o_fullscreen_btn setAlternateImage: [NSImage imageNamed:@"fullscreen-double-buttons-pressed_dark"]];        
201     }
202     [o_repeat_btn setImage: o_repeat_img];
203     [o_repeat_btn setAlternateImage: o_repeat_pressed_img];
204     [o_shuffle_btn setImage: o_shuffle_img];
205     [o_shuffle_btn setAlternateImage: o_shuffle_pressed_img];
206
207     [o_play_btn setImage: o_play_img];
208     [o_play_btn setAlternateImage: o_play_pressed_img];
209
210     [o_video_view setFrame: [o_playlist_table frame]];
211     [self setDelegate: self];
212     [self setExcludedFromWindowsMenu: YES];
213     // Set that here as IB seems to be buggy
214     [self setContentMinSize:NSMakeSize(500., 200.)];
215     [self setTitle: _NS("VLC media player")];
216     [o_playlist_btn setEnabled:NO];
217
218     [self updateVolumeSlider];
219 }
220
221 #pragma mark -
222 #pragma mark Button Actions
223
224 - (IBAction)play:(id)sender
225 {
226     [[VLCCoreInteraction sharedInstance] play];
227 }
228
229 // TODO: we need more advanced handling for the next 2 actions to handling skipping as well
230 - (IBAction)bwd:(id)sender
231 {
232     [[VLCCoreInteraction sharedInstance] previous];
233 }
234
235 - (IBAction)fwd:(id)sender
236 {
237     [[VLCCoreInteraction sharedInstance] next];
238 }
239
240 - (IBAction)stop:(id)sender
241 {
242     [[VLCCoreInteraction sharedInstance] stop];
243 }
244
245 - (IBAction)togglePlaylist:(id)sender
246 {
247     NSLog( @"b_videoplayen %i", b_video_playback_enabled );
248     if (b_video_playback_enabled && [o_video_view isHidden]) {
249         [o_video_view setHidden: NO];
250         [o_playlist_table setHidden: YES];
251     }
252     else
253     {
254         [o_playlist_table setHidden: NO];
255         [o_video_view setHidden: YES];
256     }
257 }
258
259 - (IBAction)repeat:(id)sender
260 {
261     vlc_value_t looping,repeating;
262     intf_thread_t * p_intf = VLCIntf;
263     playlist_t * p_playlist = pl_Get( p_intf );
264
265     var_Get( p_playlist, "repeat", &repeating );
266     var_Get( p_playlist, "loop", &looping );
267
268     if( !repeating.b_bool && !looping.b_bool )
269     {
270         /* was: no repeating at all, switching to Repeat One */
271         [[VLCCoreInteraction sharedInstance] repeatOne];
272
273         [o_repeat_btn setImage: o_repeat_one_img];
274         [o_repeat_btn setAlternateImage: o_repeat_one_pressed_img];
275     }
276     else if( repeating.b_bool && !looping.b_bool )
277     {
278         /* was: Repeat One, switching to Repeat All */
279         [[VLCCoreInteraction sharedInstance] repeatAll];
280
281         [o_repeat_btn setImage: o_repeat_all_img];
282         [o_repeat_btn setAlternateImage: o_repeat_all_pressed_img];
283     }
284     else
285     {
286         /* was: Repeat All or bug in VLC, switching to Repeat Off */
287         [[VLCCoreInteraction sharedInstance] repeatOff];
288
289         [o_repeat_btn setImage: o_repeat_img];
290         [o_repeat_btn setAlternateImage: o_repeat_pressed_img];
291     }
292 }
293
294 - (IBAction)shuffle:(id)sender
295 {
296     [[VLCCoreInteraction sharedInstance] shuffle];
297
298     vlc_value_t val;
299     playlist_t *p_playlist = pl_Get( VLCIntf );
300     var_Get( p_playlist, "random", &val );
301         if(val.b_bool) {
302         [o_shuffle_btn setImage: o_shuffle_on_img];
303         [o_shuffle_btn setAlternateImage: o_shuffle_on_pressed_img];
304     }
305     else
306     {
307         [o_shuffle_btn setImage: o_shuffle_img];
308         [o_shuffle_btn setAlternateImage: o_shuffle_pressed_img];
309     }
310 }
311
312 - (IBAction)timeSliderAction:(id)sender
313 {
314     float f_updated;
315     input_thread_t * p_input;
316
317     switch( [[NSApp currentEvent] type] )
318     {
319         case NSLeftMouseUp:
320         case NSLeftMouseDown:
321         case NSLeftMouseDragged:
322             f_updated = [sender floatValue];
323             break;
324
325         default:
326             return;
327     }
328     p_input = pl_CurrentInput( VLCIntf );
329     if( p_input != NULL )
330     {
331         vlc_value_t time;
332         vlc_value_t pos;
333         NSString * o_time;
334         char psz_time[MSTRTIME_MAX_SIZE];
335
336         pos.f_float = f_updated / 10000.;
337         var_Set( p_input, "position", pos );
338         [o_time_sld setFloatValue: f_updated];
339
340         var_Get( p_input, "time", &time );
341
342         mtime_t dur = input_item_GetDuration( input_GetItem( p_input ) );
343         if( b_time_remaining && dur != -1 )
344         {
345             o_time = [NSString stringWithFormat: @"-%s", secstotimestr( psz_time, ((dur - time.i_time) / 1000000) )];
346         }
347         else
348             o_time = [NSString stringWithUTF8String: secstotimestr( psz_time, (time.i_time / 1000000) )];
349
350         [o_time_fld setStringValue: o_time];
351         [[[[VLCMain sharedInstance] controls] fspanel] setStreamPos: f_updated andTime: o_time];
352         vlc_object_release( p_input );
353     }
354 }
355
356 - (IBAction)timeFieldWasClicked:(id)sender
357 {
358     b_time_remaining = !b_time_remaining;
359     NSLog( @"b_time_remaining %i", b_time_remaining );
360 }
361
362 - (IBAction)volumeAction:(id)sender
363 {
364     if (sender == o_volume_sld)
365         [[VLCCoreInteraction sharedInstance] setVolume: [sender intValue]];
366     else if (sender == o_volume_down_btn)
367         [[VLCCoreInteraction sharedInstance] mute];
368     else
369         [[VLCCoreInteraction sharedInstance] setVolume: 400];
370 }
371
372 - (IBAction)effects:(id)sender
373 {
374     [[VLCMainMenu sharedInstance] showAudioEffects: sender];
375 }
376
377 - (IBAction)fullscreen:(id)sender
378 {
379     NSLog( @"fullscreen mode not yet implemented" );
380     [[VLCCoreInteraction sharedInstance] toggleFullscreen];
381 }
382
383 #pragma mark -
384 #pragma mark Update interface and respond to foreign events
385 - (void)updateTimeSlider
386 {
387     if ([o_time_sld isEnabled])
388     {
389         input_thread_t * p_input;
390         p_input = pl_CurrentInput( VLCIntf );
391         if( p_input )
392         {
393             vlc_value_t time;
394             NSString * o_time;
395             vlc_value_t pos;
396             char psz_time[MSTRTIME_MAX_SIZE];
397             float f_updated;
398
399             var_Get( p_input, "position", &pos );
400             f_updated = 10000. * pos.f_float;
401             [o_time_sld setFloatValue: f_updated];
402
403             var_Get( p_input, "time", &time );
404
405             mtime_t dur = input_item_GetDuration( input_GetItem( p_input ) );
406             if( b_time_remaining && dur != -1 )
407             {
408                 o_time = [NSString stringWithFormat: @"-%s", secstotimestr( psz_time, ((dur - time.i_time) / 1000000))];
409             }
410             else
411                 o_time = [NSString stringWithUTF8String: secstotimestr( psz_time, (time.i_time / 1000000) )];
412
413             [o_time_fld setStringValue: o_time];
414     //        [[[[VLCMain sharedInstance] controls] fspanel] setStreamPos: f_updated andTime: o_time];
415         }
416     }
417 }
418
419 - (void)updateVolumeSlider
420 {
421     audio_volume_t i_volume;
422     playlist_t * p_playlist = pl_Get( VLCIntf );
423
424     i_volume = aout_VolumeGet( p_playlist );
425
426     if( i_volume != i_lastShownVolume )
427     {
428         i_lastShownVolume = i_volume;
429         int i_volume_step = 0;
430         i_volume_step = config_GetInt( VLCIntf->p_libvlc, "volume-step" );
431         [o_volume_sld setFloatValue: (float)i_lastShownVolume / i_volume_step];
432 //        [[[[VLCMain sharedInstance] controls] fspanel] setVolumeLevel: (float)i_lastShownVolume / i_volume_step];
433     }
434 }
435
436 - (void)updateWindow
437 {
438     bool b_input = false;
439     bool b_plmul = false;
440     bool b_control = false;
441     bool b_seekable = false;
442     bool b_chapters = false;
443
444     playlist_t * p_playlist = pl_Get( VLCIntf );
445
446     PL_LOCK;
447     b_plmul = playlist_CurrentSize( p_playlist ) > 1;
448     PL_UNLOCK;
449
450     input_thread_t * p_input = playlist_CurrentInput( p_playlist );
451
452     bool b_buffering = NO;
453
454     if( ( b_input = ( p_input != NULL ) ) )
455     {
456         /* seekable streams */
457         cachedInputState = input_GetState( p_input );
458         if ( cachedInputState == INIT_S || cachedInputState == OPENING_S )
459             b_buffering = YES;
460
461         /* seekable streams */
462         b_seekable = var_GetBool( p_input, "can-seek" );
463
464         /* check whether slow/fast motion is possible */
465         b_control = var_GetBool( p_input, "can-rate" );
466
467         /* chapters & titles */
468         //FIXME! b_chapters = p_input->stream.i_area_nb > 1;
469         vlc_object_release( p_input );
470     }
471
472     if( b_buffering )
473     {
474         [o_progress_bar startAnimation:self];
475         [o_progress_bar setIndeterminate:YES];
476         [o_progress_bar setHidden:NO];
477     } else {
478         [o_progress_bar stopAnimation:self];
479         [o_progress_bar setHidden:YES];
480     }
481
482     [o_stop_btn setEnabled: b_input];
483     [o_fwd_btn setEnabled: (b_seekable || b_plmul || b_chapters)];
484     [o_bwd_btn setEnabled: (b_seekable || b_plmul || b_chapters)];
485     [[VLCMainMenu sharedInstance] setRateControlsEnabled: b_control];
486
487     [o_time_sld setFloatValue: 0.0];
488     [o_time_sld setEnabled: b_seekable];
489     [o_time_fld setStringValue: @"00:00"];
490     [[[[VLCMain sharedInstance] controls] fspanel] setStreamPos: 0 andTime: @"00:00"];
491     [[[[VLCMain sharedInstance] controls] fspanel] setSeekable: b_seekable];
492 }
493
494 - (void)setPause
495 {
496     [o_play_btn setImage: o_pause_img];
497     [o_play_btn setAlternateImage: o_pause_pressed_img];
498     [o_play_btn setToolTip: _NS("Pause")];
499 }
500
501 - (void)setPlay
502 {
503     [o_play_btn setImage: o_play_img];
504     [o_play_btn setAlternateImage: o_play_pressed_img];
505     [o_play_btn setToolTip: _NS("Play")];
506 }
507
508 #pragma mark -
509 #pragma mark Video Output handling
510
511 - (id)videoView
512 {
513     return o_video_view;
514 }
515
516 - (void)setVideoplayEnabled:(BOOL)b_value
517 {
518     NSLog( @"setVideoplayEnabled:%i", b_value );
519     if (b_value) {
520         b_video_playback_enabled = YES;
521         [o_playlist_btn setEnabled: YES];
522     }
523     else
524     {
525         b_video_playback_enabled = NO;
526         [o_playlist_btn setEnabled: NO];
527     }
528 }
529
530 @end