]> git.sesse.net Git - vlc/blob - modules/gui/macosx/MainWindow.m
macosx: make sure we can't access the split view while showing a video
[vlc] / 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 "CompatibilityFixes.h"
28 #import "MainWindow.h"
29 #import "intf.h"
30 #import "CoreInteraction.h"
31 #import "AudioEffects.h"
32 #import "MainMenu.h"
33 #import "open.h"
34 #import "controls.h" // TODO: remove me
35 #import "SideBarItem.h"
36 #import <vlc_playlist.h>
37 #import <vlc_aout_intf.h>
38 #import <vlc_url.h>
39 #import <vlc_strings.h>
40 #import <vlc_services_discovery.h>
41
42 @implementation VLCMainWindow
43 static VLCMainWindow *_o_sharedInstance = nil;
44
45 + (VLCMainWindow *)sharedInstance
46 {
47     return _o_sharedInstance ? _o_sharedInstance : [[self alloc] init];
48 }
49
50 #pragma mark -
51 #pragma mark Initialization
52
53 - (id)init
54 {
55     if( _o_sharedInstance)
56     {
57         [self dealloc];
58         return _o_sharedInstance;
59     }
60     else
61     {
62         o_fspanel = [[VLCFSPanel alloc] init];
63         _o_sharedInstance = [super init];
64     }
65
66     return _o_sharedInstance;
67 }
68
69 - (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)styleMask
70                   backing:(NSBackingStoreType)backingType defer:(BOOL)flag
71 {
72 //     styleMask ^= NSTexturedBackgroundWindowMask;
73
74     self = [super initWithContentRect:contentRect styleMask:styleMask //& ~NSTitledWindowMask
75                               backing:backingType defer:flag];
76
77     [[VLCMain sharedInstance] updateTogglePlaylistState];
78
79     /* we want to be moveable regardless of our style */
80     [self setMovableByWindowBackground: YES];
81
82     /* we don't want this window to be restored on relaunch */
83     if ([self respondsToSelector:@selector(setRestorable:)])
84         [self setRestorable:NO];
85
86     return self;
87 }
88
89 - (BOOL)performKeyEquivalent:(NSEvent *)o_event
90 {
91     /* We indeed want to prioritize Cocoa key equivalent against libvlc,
92      so we perform the menu equivalent now. */
93     if([[NSApp mainMenu] performKeyEquivalent:o_event])
94         return TRUE;
95
96     return [[VLCMain sharedInstance] hasDefinedShortcutKey:o_event] || [(VLCControls *)[[VLCMain sharedInstance] controls] keyEvent:o_event];
97 }
98
99 - (void)dealloc
100 {
101     config_PutInt( VLCIntf->p_libvlc, "volume", i_lastShownVolume );
102     [o_sidebaritems release];
103     [super dealloc];
104 }
105
106 - (void)awakeFromNib
107 {
108     /* setup the styled interface */
109     b_dark_interface = config_GetInt( VLCIntf, "macosx-interfacestyle" );
110     i_lastShownVolume = -1;
111
112     [o_play_btn setToolTip: _NS("Play/Pause")];
113     [o_bwd_btn setToolTip: _NS("Backward")];
114     [o_fwd_btn setToolTip: _NS("Forward")];
115     [o_stop_btn setToolTip: _NS("Stop")];
116     [o_playlist_btn setToolTip: _NS("Show/Hide Playlist")];
117     [o_repeat_btn setToolTip: _NS("Repeat")];
118     [o_shuffle_btn setToolTip: _NS("Shuffle")];
119     [o_effects_btn setToolTip: _NS("Effects")];
120     [o_fullscreen_btn setToolTip: _NS("Toggle Fullscreen mode")];
121     [[o_search_fld cell] setPlaceholderString: _NS("Search")];
122     [o_volume_sld setToolTip: _NS("Volume")];
123     [o_volume_down_btn setToolTip: _NS("Mute")];
124     [o_volume_up_btn setToolTip: _NS("Full Volume")];
125     [o_time_sld setToolTip: _NS("Position")];
126     [o_dropzone_btn setTitle: _NS("Open media...")];
127     [o_dropzone_lbl setStringValue: _NS("Drop media here")];
128
129     if (!b_dark_interface) {
130         [o_bottombar_view setImage: [NSImage imageNamed:@"bottom-background"]];
131         [o_bwd_btn setImage: [NSImage imageNamed:@"back"]];
132         [o_bwd_btn setAlternateImage: [NSImage imageNamed:@"back-pressed"]];
133         o_play_img = [[NSImage imageNamed:@"play"] retain];
134         o_play_pressed_img = [[NSImage imageNamed:@"play-pressed"] retain];
135         o_pause_img = [[NSImage imageNamed:@"pause"] retain];
136         o_pause_pressed_img = [[NSImage imageNamed:@"pause-pressed"] retain];
137         [o_fwd_btn setImage: [NSImage imageNamed:@"forward"]];
138         [o_fwd_btn setAlternateImage: [NSImage imageNamed:@"forward-pressed"]];
139         [o_stop_btn setImage: [NSImage imageNamed:@"stop"]];
140         [o_stop_btn setAlternateImage: [NSImage imageNamed:@"stop-pressed"]];
141         [o_playlist_btn setImage: [NSImage imageNamed:@"playlist"]];
142         [o_playlist_btn setAlternateImage: [NSImage imageNamed:@"playlist-pressed"]];
143         o_repeat_img = [[NSImage imageNamed:@"repeat"] retain];
144         o_repeat_pressed_img = [[NSImage imageNamed:@"repeat-pressed"] retain];
145         o_repeat_all_img  = [[NSImage imageNamed:@"repeat-all"] retain];
146         o_repeat_all_pressed_img = [[NSImage imageNamed:@"repeat-all-pressed"] retain];
147         o_repeat_one_img = [[NSImage imageNamed:@"repeat-one"] retain];
148         o_repeat_one_pressed_img = [[NSImage imageNamed:@"repeat-one-pressed"] retain];
149         o_shuffle_img = [[NSImage imageNamed:@"shuffle"] retain];
150         o_shuffle_pressed_img = [[NSImage imageNamed:@"shuffle-pressed"] retain];
151         o_shuffle_on_img = [[NSImage imageNamed:@"shuffle-blue"] retain];
152         o_shuffle_on_pressed_img = [[NSImage imageNamed:@"shuffle-blue-pressed"] retain];
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:@"volume-slider-track"]];
158         [o_volume_up_btn setImage: [NSImage imageNamed:@"volume-high"]];
159         if (OSX_LION)
160         {
161             [o_effects_btn setImage: [NSImage imageNamed:@"effects-one-button"]];
162             [o_effects_btn setAlternateImage: [NSImage imageNamed:@"effects-one-button-blue"]];
163         }
164         else
165         {
166             [o_effects_btn setImage: [NSImage imageNamed:@"effects-double-buttons"]];
167             [o_effects_btn setAlternateImage: [NSImage imageNamed:@"effects-double-buttons-pressed"]];
168         }
169         [o_fullscreen_btn setImage: [NSImage imageNamed:@"fullscreen-double-buttons"]];
170         [o_fullscreen_btn setAlternateImage: [NSImage imageNamed:@"fullscreen-double-buttons-pressed"]];
171         [o_time_sld_fancygradient_view loadImagesInDarkStyle:NO];
172     }
173     else
174     {
175         /* TODO: we also need to change the window style here... */
176         [o_bottombar_view setImage: [NSImage imageNamed:@"bottom-background_dark"]];
177         [o_bwd_btn setImage: [NSImage imageNamed:@"back_dark"]];
178         [o_bwd_btn setAlternateImage: [NSImage imageNamed:@"back-pressed_dark"]];
179         o_play_img = [[NSImage imageNamed:@"play_dark"] retain];
180         o_play_pressed_img = [[NSImage imageNamed:@"play-pressed_dark"] retain];
181         o_pause_img = [[NSImage imageNamed:@"pause_dark"] retain];
182         o_pause_pressed_img = [[NSImage imageNamed:@"pause-pressed_dark"] retain];
183         [o_fwd_btn setImage: [NSImage imageNamed:@"forward_dark"]];
184         [o_fwd_btn setAlternateImage: [NSImage imageNamed:@"forward-pressed_dark"]];
185         [o_stop_btn setImage: [NSImage imageNamed:@"stop_dark"]];
186         [o_stop_btn setAlternateImage: [NSImage imageNamed:@"stop-pressed_dark"]];
187         [o_playlist_btn setImage: [NSImage imageNamed:@"playlist_dark"]];
188         [o_playlist_btn setAlternateImage: [NSImage imageNamed:@"playlist-pressed_dark"]];
189         o_repeat_img = [[NSImage imageNamed:@"repeat_dark"] retain];
190         o_repeat_pressed_img = [[NSImage imageNamed:@"repeat-pressed_dark"] retain];
191         o_repeat_all_img  = [[NSImage imageNamed:@"repeat-all-blue_dark"] retain];
192         o_repeat_all_pressed_img = [[NSImage imageNamed:@"repeat-all-blue-pressed_dark"] retain];
193         o_repeat_one_img = [[NSImage imageNamed:@"repeat-one-blue_dark"] retain];
194         o_repeat_one_pressed_img = [[NSImage imageNamed:@"repeat-one-blue-pressed_dark"] retain];
195         o_shuffle_img = [[NSImage imageNamed:@"shuffle_dark"] retain];
196         o_shuffle_pressed_img = [[NSImage imageNamed:@"shuffle-pressed_dark"] retain];
197         o_shuffle_on_img = [[NSImage imageNamed:@"shuffle-blue_dark"] retain];
198         o_shuffle_on_pressed_img = [[NSImage imageNamed:@"shuffle-blue-pressed_dark"] retain];
199         [o_time_fld setTextColor: [NSColor colorWithCalibratedRed:229.0 green:229.0 blue:229.0 alpha:100.0]];
200         [o_time_sld_left_view setImage: [NSImage imageNamed:@"progression-track-wrapper-left_dark"]];
201         [o_time_sld_middle_view setImage: [NSImage imageNamed:@"progression-track-wrapper-middle_dark"]];
202         [o_time_sld_right_view setImage: [NSImage imageNamed:@"progression-track-wrapper-right_dark"]];
203         [o_volume_down_btn setImage: [NSImage imageNamed:@"volume-low_dark"]];
204         [o_volume_track_view setImage: [NSImage imageNamed:@"volume-slider-track_dark"]];
205         [o_volume_up_btn setImage: [NSImage imageNamed:@"volume-high_dark"]];
206         if (OSX_LION)
207         {
208             [o_effects_btn setImage: [NSImage imageNamed:@"effects-one-button_dark"]];
209             [o_effects_btn setAlternateImage: [NSImage imageNamed:@"effects-one-button-blue_dark"]];
210         }
211         else
212         {
213             [o_effects_btn setImage: [NSImage imageNamed:@"effects-double-buttons_dark"]];
214             [o_effects_btn setAlternateImage: [NSImage imageNamed:@"effects-double-buttons-pressed_dark"]];            
215         }
216         [o_fullscreen_btn setImage: [NSImage imageNamed:@"fullscreen-double-buttons_dark"]];
217         [o_fullscreen_btn setAlternateImage: [NSImage imageNamed:@"fullscreen-double-buttons-pressed_dark"]];
218         [o_time_sld_fancygradient_view loadImagesInDarkStyle:YES];
219     }
220     [o_repeat_btn setImage: o_repeat_img];
221     [o_repeat_btn setAlternateImage: o_repeat_pressed_img];
222     [o_shuffle_btn setImage: o_shuffle_img];
223     [o_shuffle_btn setAlternateImage: o_shuffle_pressed_img];
224     [o_play_btn setImage: o_play_img];
225     [o_play_btn setAlternateImage: o_play_pressed_img];
226
227     /* interface builder action */
228     [self setDelegate: self];
229     [self setExcludedFromWindowsMenu: YES];
230     // Set that here as IB seems to be buggy
231     [self setContentMinSize:NSMakeSize(500., 288.)];
232     [self setTitle: _NS("VLC media player")];
233     [o_playlist_btn setEnabled:NO];
234     [o_video_view setFrame: [o_split_view frame]];
235     o_temp_view = [[NSView alloc] init];
236     [o_temp_view setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
237     [o_dropzone_view setFrame: [o_playlist_table frame]];
238     [o_left_split_view setFrame: [o_sidebar_view frame]];
239     if (OSX_LION)
240     {
241         [self setCollectionBehavior: NSWindowCollectionBehaviorFullScreenPrimary];
242         NSRect frame;
243         float f_width = [o_fullscreen_btn frame].size.width;
244
245         #define moveItem( item ) \
246         frame = [item frame]; \
247         frame.origin.x = f_width + frame.origin.x; \
248         [item setFrame: frame]
249
250         moveItem( o_effects_btn );
251         moveItem( o_volume_up_btn );
252         moveItem( o_volume_sld );
253         moveItem( o_volume_track_view );
254         moveItem( o_volume_down_btn );
255         moveItem( o_time_fld );
256         moveItem( o_time_sld_right_view );
257         #undef moveItem
258
259         #define enlargeItem( item ) \
260         frame = [item frame]; \
261         frame.size.width = f_width + frame.size.width; \
262         [item setFrame: frame]
263
264         enlargeItem( o_time_sld );
265         enlargeItem( o_progress_bar );
266         enlargeItem( o_time_sld_middle_view );
267         enlargeItem( o_time_sld_fancygradient_view );
268         #undef enlargeItem
269
270         [o_fullscreen_btn removeFromSuperviewWithoutNeedingDisplay];
271     }
272
273     /* create the sidebar */
274     o_sidebaritems = [[NSMutableArray alloc] init];
275     SideBarItem *libraryItem = [SideBarItem itemWithTitle:_NS("LIBRARY") identifier:@"library"];
276     SideBarItem *playlistItem = [SideBarItem itemWithTitle:_NS("Playlist") identifier:@"playlist"];
277     [playlistItem setIcon: [NSImage imageNamed:@"document-music-playlist"]];
278     SideBarItem *mycompItem = [SideBarItem itemWithTitle:_NS("MY COMPUTER") identifier:@"mycomputer"];
279     SideBarItem *devicesItem = [SideBarItem itemWithTitle:_NS("DEVICES") identifier:@"devices"];
280     SideBarItem *lanItem = [SideBarItem itemWithTitle:_NS("LOCAL NETWORK") identifier:@"localnetwork"];
281     SideBarItem *internetItem = [SideBarItem itemWithTitle:_NS("INTERNET") identifier:@"internet"];
282
283     /* SD subnodes, inspired by the Qt4 intf */
284     char **ppsz_longnames;
285     int *p_categories;
286     char **ppsz_names = vlc_sd_GetNames( pl_Get( VLCIntf ), &ppsz_longnames, &p_categories );
287     if (!ppsz_names)
288         msg_Err( VLCIntf, "no sd item found" ); //TODO
289     char **ppsz_name = ppsz_names, **ppsz_longname = ppsz_longnames;
290     int *p_category = p_categories;
291     NSMutableArray *internetItems = [[NSMutableArray alloc] init];
292     NSMutableArray *devicesItems = [[NSMutableArray alloc] init];
293     NSMutableArray *lanItems = [[NSMutableArray alloc] init];
294     NSMutableArray *mycompItems = [[NSMutableArray alloc] init];
295     NSString *o_identifier;
296     for (; *ppsz_name; ppsz_name++, ppsz_longname++, p_category++)
297     {
298         o_identifier = [NSString stringWithCString: *ppsz_name encoding: NSUTF8StringEncoding];
299         o_identifier = [[o_identifier componentsSeparatedByString:@"{"] objectAtIndex:0];
300         switch (*p_category) {
301             case SD_CAT_INTERNET:
302                 {
303                     [internetItems addObject: [SideBarItem itemWithTitle: [NSString stringWithCString: *ppsz_longname encoding: NSUTF8StringEncoding] identifier: o_identifier]];
304                     if (!strncmp( *ppsz_name, "podcast", 7 ))
305                         [[internetItems lastObject] setIcon: [NSImage imageNamed:@"film-cast"]];
306                     else
307                         [[internetItems lastObject] setIcon: [NSImage imageNamed:@"NSApplicationIcon"]];
308                 }
309                 break;
310             case SD_CAT_DEVICES:
311                 {
312                     [devicesItems addObject: [SideBarItem itemWithTitle: [NSString stringWithCString: *ppsz_longname encoding: NSUTF8StringEncoding] identifier: o_identifier]];
313                     [[devicesItems lastObject] setIcon: [NSImage imageNamed:@"NSApplicationIcon"]];
314                 }
315                 break;
316             case SD_CAT_LAN:
317                 {
318                     [lanItems addObject: [SideBarItem itemWithTitle: [NSString stringWithCString: *ppsz_longname encoding: NSUTF8StringEncoding] identifier: o_identifier]];
319                     [[lanItems lastObject] setIcon: [NSImage imageNamed:@"network-cloud"]];
320                 }
321                 break;
322             case SD_CAT_MYCOMPUTER:
323                 {
324                     [mycompItems addObject: [SideBarItem itemWithTitle: [NSString stringWithCString: *ppsz_longname encoding: NSUTF8StringEncoding] identifier: o_identifier]];
325                     if (!strncmp( *ppsz_name, "video_dir", 9 ))
326                         [[mycompItems lastObject] setIcon: [NSImage imageNamed:@"film"]];
327                     else if (!strncmp( *ppsz_name, "audio_dir", 9 ))
328                         [[mycompItems lastObject] setIcon: [NSImage imageNamed:@"music-beam"]];
329                     else if (!strncmp( *ppsz_name, "picture_dir", 11 ))
330                         [[mycompItems lastObject] setIcon: [NSImage imageNamed:@"picture"]];
331                     else
332                         [[mycompItems lastObject] setIcon: [NSImage imageNamed:@"NSApplicationIcon"]];
333                 }
334                 break;
335             default:
336                 msg_Warn( VLCIntf, "unknown SD type found, skipping (%s)", *ppsz_name );
337                 break;
338         }
339
340         free( *ppsz_name );
341         free( *ppsz_longname );
342     }
343     [mycompItem setChildren: [NSArray arrayWithArray: mycompItems]];
344     [devicesItem setChildren: [NSArray arrayWithArray: devicesItems]];
345     [lanItem setChildren: [NSArray arrayWithArray: lanItems]];
346     [internetItem setChildren: [NSArray arrayWithArray: internetItems]];
347     [mycompItems release];
348     [devicesItems release];
349     [lanItems release];
350     [internetItems release];
351     free( ppsz_names );
352     free( ppsz_longnames );
353     free( p_categories );
354
355     [libraryItem setChildren: [NSArray arrayWithObject: playlistItem]];
356     [o_sidebaritems addObject: libraryItem];
357     if ([mycompItem hasChildren])
358         [o_sidebaritems addObject: mycompItem];
359     if ([devicesItem hasChildren])
360         [o_sidebaritems addObject: devicesItem];
361     if ([lanItem hasChildren])
362         [o_sidebaritems addObject: lanItem];
363     if ([internetItem hasChildren])
364         [o_sidebaritems addObject: internetItem];
365
366     [o_sidebar_view reloadData];
367     [o_sidebar_view selectRowIndexes:[NSIndexSet indexSetWithIndex:0] byExtendingSelection:YES];
368 }
369
370 #pragma mark -
371 #pragma mark Button Actions
372
373 - (IBAction)play:(id)sender
374 {
375     [[VLCCoreInteraction sharedInstance] play];
376 }
377
378 - (void)resetPreviousButton
379 {
380     if (([NSDate timeIntervalSinceReferenceDate] - last_bwd_event) >= 0.35) {
381         // seems like no further event occured, so let's switch the playback item
382         [[VLCCoreInteraction sharedInstance] previous];
383         just_triggered_previous = NO;
384     }
385 }
386
387 - (void)resetBackwardSkip
388 {
389     // the user stopped skipping, so let's allow him to change the item
390     if (([NSDate timeIntervalSinceReferenceDate] - last_bwd_event) >= 0.35)
391         just_triggered_previous = NO;
392 }
393
394 - (IBAction)bwd:(id)sender
395 {
396     if(!just_triggered_previous)
397     {
398         just_triggered_previous = YES;
399         [self performSelector:@selector(resetPreviousButton)
400                    withObject: NULL
401                    afterDelay:0.40];
402     }
403     else
404     {
405         if (([NSDate timeIntervalSinceReferenceDate] - last_fwd_event) > 0.12 )
406         {
407             // we just skipped 3 "continous" events, otherwise we are too fast
408             [[VLCCoreInteraction sharedInstance] backward];
409             last_bwd_event = [NSDate timeIntervalSinceReferenceDate];
410             [self performSelector:@selector(resetBackwardSkip)
411                        withObject: NULL
412                        afterDelay:0.40];
413         }
414     }
415 }
416
417 - (void)resetNextButton
418 {
419     if (([NSDate timeIntervalSinceReferenceDate] - last_fwd_event) >= 0.35) {
420         // seems like no further event occured, so let's switch the playback item
421         [[VLCCoreInteraction sharedInstance] next];
422         just_triggered_next = NO;
423     }
424 }
425
426 - (void)resetForwardSkip
427 {
428     // the user stopped skipping, so let's allow him to change the item
429     if (([NSDate timeIntervalSinceReferenceDate] - last_fwd_event) >= 0.35)
430         just_triggered_next = NO;
431 }
432
433 - (IBAction)fwd:(id)sender
434 {
435    if(!just_triggered_next)
436     {
437         just_triggered_next = YES;
438         [self performSelector:@selector(resetNextButton)
439                    withObject: NULL
440                    afterDelay:0.40];
441     }
442     else
443     {
444         if (([NSDate timeIntervalSinceReferenceDate] - last_fwd_event) > 0.12 )
445         {
446             // we just skipped 3 "continous" events, otherwise we are too fast
447             [[VLCCoreInteraction sharedInstance] forward];
448             last_fwd_event = [NSDate timeIntervalSinceReferenceDate];
449             [self performSelector:@selector(resetForwardSkip)
450                        withObject: NULL
451                        afterDelay:0.40];
452         }
453     }
454 }
455
456 - (IBAction)stop:(id)sender
457 {
458     [[VLCCoreInteraction sharedInstance] stop];
459 }
460
461 - (IBAction)togglePlaylist:(id)sender
462 {
463     if (!b_nonembedded)
464     {
465         if ([o_video_view isHidden] && [o_playlist_btn isEnabled]) {
466             [o_split_view setHidden: YES];
467             [o_video_view setHidden: NO];
468         }
469         else
470         {
471             [o_video_view setHidden: YES];
472             [o_split_view setHidden: NO];
473         }
474     }
475     else
476     {
477         [o_split_view setHidden: NO];
478         [o_playlist_table setHidden: NO];
479         [o_video_view setHidden: ![[VLCMain sharedInstance] activeVideoPlayback]];
480     }
481 }
482
483 - (void)setRepeatOne
484 {
485     [o_repeat_btn setImage: o_repeat_one_img];
486     [o_repeat_btn setAlternateImage: o_repeat_one_pressed_img];   
487 }
488
489 - (void)setRepeatAll
490 {
491     [o_repeat_btn setImage: o_repeat_all_img];
492     [o_repeat_btn setAlternateImage: o_repeat_all_pressed_img];
493 }
494
495 - (void)setRepeatOff
496 {
497     [o_repeat_btn setImage: o_repeat_img];
498     [o_repeat_btn setAlternateImage: o_repeat_pressed_img];
499 }
500
501 - (IBAction)repeat:(id)sender
502 {
503     vlc_value_t looping,repeating;
504     intf_thread_t * p_intf = VLCIntf;
505     playlist_t * p_playlist = pl_Get( p_intf );
506
507     var_Get( p_playlist, "repeat", &repeating );
508     var_Get( p_playlist, "loop", &looping );
509
510     if( !repeating.b_bool && !looping.b_bool )
511     {
512         /* was: no repeating at all, switching to Repeat One */
513         [[VLCCoreInteraction sharedInstance] repeatOne];
514         [self setRepeatOne];
515     }
516     else if( repeating.b_bool && !looping.b_bool )
517     {
518         /* was: Repeat One, switching to Repeat All */
519         [[VLCCoreInteraction sharedInstance] repeatAll];
520         [self setRepeatAll];
521     }
522     else
523     {
524         /* was: Repeat All or bug in VLC, switching to Repeat Off */
525         [[VLCCoreInteraction sharedInstance] repeatOff];
526         [self setRepeatOff];
527     }
528 }
529
530 - (void)setShuffle
531 {
532     bool b_value;
533     playlist_t *p_playlist = pl_Get( VLCIntf );
534     b_value = var_GetBool( p_playlist, "random" );
535         if(b_value) {
536         [o_shuffle_btn setImage: o_shuffle_on_img];
537         [o_shuffle_btn setAlternateImage: o_shuffle_on_pressed_img];
538     }
539     else
540     {
541         [o_shuffle_btn setImage: o_shuffle_img];
542         [o_shuffle_btn setAlternateImage: o_shuffle_pressed_img];
543     }
544 }
545
546 - (IBAction)shuffle:(id)sender
547 {
548     [[VLCCoreInteraction sharedInstance] shuffle];
549     [self setShuffle];
550 }
551
552 - (IBAction)timeSliderAction:(id)sender
553 {
554     float f_updated;
555     input_thread_t * p_input;
556
557     switch( [[NSApp currentEvent] type] )
558     {
559         case NSLeftMouseUp:
560         case NSLeftMouseDown:
561         case NSLeftMouseDragged:
562             f_updated = [sender floatValue];
563             break;
564
565         default:
566             return;
567     }
568     p_input = pl_CurrentInput( VLCIntf );
569     if( p_input != NULL )
570     {
571         vlc_value_t time;
572         vlc_value_t pos;
573         NSString * o_time;
574         char psz_time[MSTRTIME_MAX_SIZE];
575
576         pos.f_float = f_updated / 10000.;
577         var_Set( p_input, "position", pos );
578         [o_time_sld setFloatValue: f_updated];
579
580         var_Get( p_input, "time", &time );
581
582         mtime_t dur = input_item_GetDuration( input_GetItem( p_input ) );
583         if( [o_time_fld timeRemaining] && dur != -1 )
584         {
585             o_time = [NSString stringWithFormat: @"-%s", secstotimestr( psz_time, ((dur - time.i_time) / 1000000) )];
586         }
587         else
588             o_time = [NSString stringWithUTF8String: secstotimestr( psz_time, (time.i_time / 1000000) )];
589
590         [o_time_fld setStringValue: o_time];
591         [o_fspanel setStreamPos: f_updated andTime: o_time];
592         vlc_object_release( p_input );
593     }
594     [self drawFancyGradientEffectForTimeSlider];
595 }
596
597 - (IBAction)volumeAction:(id)sender
598 {
599     if (sender == o_volume_sld)
600         [[VLCCoreInteraction sharedInstance] setVolume: [sender intValue]];
601     else if (sender == o_volume_down_btn)
602         [[VLCCoreInteraction sharedInstance] mute];
603     else
604         [[VLCCoreInteraction sharedInstance] setVolume: 400];
605 }
606
607 - (IBAction)effects:(id)sender
608 {
609     [[VLCMainMenu sharedInstance] showAudioEffects: sender];
610 }
611
612 - (IBAction)fullscreen:(id)sender
613 {
614     [[VLCCoreInteraction sharedInstance] toggleFullscreen];
615 }
616
617 - (IBAction)dropzoneButtonAction:(id)sender
618 {
619     [[[VLCMain sharedInstance] open] openFileGeneric];
620 }
621
622 #pragma mark -
623 #pragma mark Update interface and respond to foreign events
624 - (void)showDropZone
625 {
626     [o_right_split_view addSubview: o_dropzone_view];
627     [o_dropzone_view setFrame: [o_playlist_table frame]];
628     [[o_playlist_table animator] setHidden:YES];
629 }
630
631 - (void)hideDropZone
632 {
633     [o_dropzone_view removeFromSuperview];
634     [[o_playlist_table animator] setHidden: NO];
635 }
636
637 - (void)updateTimeSlider
638 {
639     input_thread_t * p_input;
640     p_input = pl_CurrentInput( VLCIntf );
641     if( p_input )
642     {
643         vlc_value_t time;
644         NSString * o_time;
645         vlc_value_t pos;
646         char psz_time[MSTRTIME_MAX_SIZE];
647         float f_updated;
648
649         var_Get( p_input, "position", &pos );
650         f_updated = 10000. * pos.f_float;
651         [o_time_sld setFloatValue: f_updated];
652
653         var_Get( p_input, "time", &time );
654
655         mtime_t dur = input_item_GetDuration( input_GetItem( p_input ) );
656         if( [o_time_fld timeRemaining] && dur != -1 )
657         {
658             o_time = [NSString stringWithFormat: @"-%s", secstotimestr( psz_time, ((dur - time.i_time) / 1000000))];
659         }
660         else
661             o_time = [NSString stringWithUTF8String: secstotimestr( psz_time, (time.i_time / 1000000) )];
662
663         if (dur == -1) {
664             [o_time_sld setEnabled: NO];
665             [o_time_sld setHidden: YES];
666         } else {
667             [o_time_sld setEnabled: YES];
668             [o_time_sld setHidden: NO];
669         }
670
671         [o_time_fld setStringValue: o_time];
672         [o_time_fld setNeedsDisplay:YES];
673         [o_fspanel setStreamPos: f_updated andTime: o_time];
674         vlc_object_release( p_input );
675     }
676     else
677     {
678         [o_time_sld setFloatValue: 0.0];
679         [o_time_fld setStringValue: @"00:00"];
680         [o_time_sld setEnabled: NO];
681         [o_time_sld setHidden: YES];
682     }
683         
684     [self performSelectorOnMainThread:@selector(drawFancyGradientEffectForTimeSlider) withObject:nil waitUntilDone:NO];
685 }
686
687 - (void)updateVolumeSlider
688 {
689     audio_volume_t i_volume;
690     playlist_t * p_playlist = pl_Get( VLCIntf );
691
692     i_volume = aout_VolumeGet( p_playlist );
693
694     if( i_volume != i_lastShownVolume )
695     {
696         i_lastShownVolume = i_volume;
697         int i_volume_step = 0;
698         i_volume_step = config_GetInt( VLCIntf->p_libvlc, "volume-step" );
699         [o_volume_sld setFloatValue: (float)i_lastShownVolume / i_volume_step];
700         [o_fspanel setVolumeLevel: (float)i_lastShownVolume / i_volume_step];
701     }
702 }
703
704 - (void)updateName
705 {
706     NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
707     input_thread_t * p_input;
708     p_input = pl_CurrentInput( VLCIntf );
709     if( p_input )
710     {
711         NSString *aString;
712         char *format = var_InheritString( VLCIntf, "input-title-format" );
713         char *formated = str_format_meta( p_input, format );
714         free( format );
715         aString = [NSString stringWithUTF8String:formated];
716         free( formated );
717
718         char *uri = input_item_GetURI( input_GetItem( p_input ) );
719
720         NSURL * o_url = [NSURL URLWithString: [NSString stringWithUTF8String: uri]];
721         if ([o_url isFileURL])
722             [self setRepresentedURL: o_url];
723         else
724             [self setRepresentedURL: nil];
725         free( uri );
726
727         if ([aString isEqualToString:@""])
728         {
729             if ([o_url isFileURL])
730                 aString = [[NSFileManager defaultManager] displayNameAtPath: [o_url path]];
731             else
732                 aString = [o_url absoluteString];
733         }
734
735         [self setTitle: aString];
736         [o_fspanel setStreamTitle: aString];
737     }
738     else
739     {
740         [self setTitle: _NS("VLC media player")];
741         [self setRepresentedURL: nil];
742     }
743     [o_pool release];
744 }
745
746 - (void)updateWindow
747 {
748     bool b_input = false;
749     bool b_plmul = false;
750     bool b_control = false;
751     bool b_seekable = false;
752     bool b_chapters = false;
753
754     playlist_t * p_playlist = pl_Get( VLCIntf );
755
756     PL_LOCK;
757     b_plmul = playlist_CurrentSize( p_playlist ) > 1;
758     PL_UNLOCK;
759
760     [o_sidebar_view reloadData];
761
762     input_thread_t * p_input = playlist_CurrentInput( p_playlist );
763
764     bool b_buffering = NO;
765
766     if( ( b_input = ( p_input != NULL ) ) )
767     {
768         /* seekable streams */
769         cachedInputState = input_GetState( p_input );
770         if ( cachedInputState == INIT_S || cachedInputState == OPENING_S )
771             b_buffering = YES;
772
773         /* seekable streams */
774         b_seekable = var_GetBool( p_input, "can-seek" );
775
776         /* check whether slow/fast motion is possible */
777         b_control = var_GetBool( p_input, "can-rate" );
778
779         /* chapters & titles */
780         //FIXME! b_chapters = p_input->stream.i_area_nb > 1;
781         vlc_object_release( p_input );
782     }
783
784     if( b_buffering )
785     {
786         [o_progress_bar startAnimation:self];
787         [o_progress_bar setIndeterminate:YES];
788         [o_progress_bar setHidden:NO];
789     } else {
790         [o_progress_bar stopAnimation:self];
791         [o_progress_bar setHidden:YES];
792     }
793
794     [o_stop_btn setEnabled: b_input];
795     [o_fwd_btn setEnabled: (b_seekable || b_plmul || b_chapters)];
796     [o_bwd_btn setEnabled: (b_seekable || b_plmul || b_chapters)];
797     [[VLCMainMenu sharedInstance] setRateControlsEnabled: b_control];
798
799     [o_time_sld setEnabled: b_seekable];
800     [self updateTimeSlider];
801     [o_fspanel setSeekable: b_seekable];
802
803     PL_LOCK;
804     if (playlist_CurrentSize( p_playlist ) >= 1)
805         [self hideDropZone];
806     else
807         [self showDropZone];
808     PL_UNLOCK;
809 }
810
811 - (void)setPause
812 {
813     [o_play_btn setImage: o_pause_img];
814     [o_play_btn setAlternateImage: o_pause_pressed_img];
815     [o_play_btn setToolTip: _NS("Pause")];
816     [o_fspanel setPause];
817 }
818
819 - (void)setPlay
820 {
821     [o_play_btn setImage: o_play_img];
822     [o_play_btn setAlternateImage: o_play_pressed_img];
823     [o_play_btn setToolTip: _NS("Play")];
824     [o_fspanel setPlay];
825 }
826
827 - (void)drawFancyGradientEffectForTimeSlider
828 {
829     NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
830     float f_value = ([o_time_sld_middle_view frame].size.width -5) * ([o_time_sld intValue] / [o_time_sld maxValue]);
831     if (f_value > 5.0)
832     {
833         if (f_value != [o_time_sld_fancygradient_view frame].size.width)
834         {
835             [o_time_sld_fancygradient_view setHidden: NO];
836             [o_time_sld_fancygradient_view setFrame: NSMakeRect( [o_time_sld_fancygradient_view frame].origin.x, [o_time_sld_fancygradient_view frame].origin.y, f_value, [o_time_sld_fancygradient_view frame].size.height )];
837             [o_time_sld_fancygradient_view setNeedsDisplay:YES];
838             [o_time_sld_middle_view setNeedsDisplay:YES];
839         }
840     }
841     else
842     {
843         [o_time_sld_fancygradient_view setHidden: YES];
844     }
845     [o_pool release];
846 }
847
848 #pragma mark -
849 #pragma mark Video Output handling
850
851 - (id)videoView
852 {
853     vout_thread_t *p_vout = getVout();
854     if (config_GetInt( VLCIntf, "embedded-video" ))
855     {
856         if ([o_video_view window] != self)
857         {
858             [o_video_view removeFromSuperviewWithoutNeedingDisplay];
859             [o_video_view setFrame: [o_split_view frame]];
860             [[self contentView] addSubview:o_video_view positioned:NSWindowAbove relativeTo:nil];
861         }
862         b_nonembedded = NO;
863     }
864     else
865     {
866         [o_video_view removeFromSuperviewWithoutNeedingDisplay];
867         if (o_nonembedded_window)
868             [o_nonembedded_window release];
869
870         o_nonembedded_window = [[VLCWindow alloc] initWithContentRect:[o_video_view frame] styleMask: NSBorderlessWindowMask|NSResizableWindowMask backing:NSBackingStoreBuffered defer:YES];
871         [o_nonembedded_window setFrame:[o_video_view frame] display:NO];
872         [o_nonembedded_window setBackgroundColor: [NSColor blackColor]];
873         [o_nonembedded_window setMovableByWindowBackground: YES];
874         [o_nonembedded_window setCanBecomeKeyWindow: YES];
875         [o_nonembedded_window setHasShadow:YES];
876         [o_nonembedded_window setContentView: o_video_view];
877         [o_nonembedded_window setLevel:NSNormalWindowLevel];
878         [o_nonembedded_window useOptimizedDrawing: YES];
879         [o_nonembedded_window center];
880         [o_nonembedded_window makeKeyAndOrderFront:self];
881         [o_nonembedded_window orderFront:self animate:YES];
882         [o_nonembedded_window setReleasedWhenClosed:NO];
883         b_nonembedded = YES;
884     }
885
886     if (p_vout)
887     {
888         if( var_GetBool( p_vout, "video-on-top" ) )
889             [[o_video_view window] setLevel: NSStatusWindowLevel];
890         else
891             [[o_video_view window] setLevel: NSNormalWindowLevel];
892         vlc_object_release( p_vout );
893     }
894     return o_video_view;
895 }
896
897 - (void)setVideoplayEnabled
898 {
899     if (!b_nonembedded)
900         [o_playlist_btn setEnabled: [[VLCMain sharedInstance] activeVideoPlayback]];
901     else
902     {
903         [o_playlist_btn setEnabled: NO];
904         if (![[VLCMain sharedInstance] activeVideoPlayback])
905             [o_nonembedded_window orderOut: nil];
906     }
907 }
908
909 - (void)resizeWindow
910 {
911     if ( !b_fullscreen && !(OSX_LION && [NSApp currentSystemPresentationOptions] == NSApplicationPresentationFullScreen) )
912     {
913         NSPoint topleftbase;
914         NSPoint topleftscreen;
915         NSRect new_frame;
916         topleftbase.x = 0;
917         topleftbase.y = [self frame].size.height;
918         topleftscreen = [self convertBaseToScreen: topleftbase];
919
920         /* Calculate the window's new size */
921         new_frame.size.width = [self frame].size.width - [o_video_view frame].size.width + nativeVideoSize.width;
922         new_frame.size.height = [self frame].size.height - [o_video_view frame].size.height + nativeVideoSize.height;
923
924         new_frame.origin.x = topleftscreen.x;
925         new_frame.origin.y = topleftscreen.y - new_frame.size.height;
926
927         [[self animator] setFrame:new_frame display:YES];
928     }
929 }
930
931 - (void)setNativeVideoSize:(NSSize)size
932 {
933     if (size.width != nativeVideoSize.width || size.height != nativeVideoSize.height )
934     {
935         nativeVideoSize = size;
936         [self resizeWindow];
937     }
938 }
939
940 #pragma mark -
941 #pragma mark Fullscreen support
942 - (void)showFullscreenController
943 {
944     if (b_fullscreen)
945         [o_fspanel fadeIn];
946 }
947
948 - (BOOL)isFullscreen
949 {
950     return b_fullscreen;
951 }
952
953 - (void)lockFullscreenAnimation
954 {
955     [o_animation_lock lock];
956 }
957
958 - (void)unlockFullscreenAnimation
959 {
960     [o_animation_lock unlock];
961 }
962
963 - (void)enterFullscreen
964 {
965     NSMutableDictionary *dict1, *dict2;
966     NSScreen *screen;
967     NSRect screen_rect;
968     NSRect rect;
969     vout_thread_t *p_vout = getVout();
970     BOOL blackout_other_displays = config_GetInt( VLCIntf, "macosx-black" );
971
972     if( p_vout )
973         screen = [NSScreen screenWithDisplayID:(CGDirectDisplayID)var_GetInteger( p_vout, "video-device" )];
974
975     [self lockFullscreenAnimation];
976
977     if (!screen)
978     {
979         msg_Dbg( VLCIntf, "chosen screen isn't present, using current screen for fullscreen mode" );
980         screen = [self screen];
981     }
982     if (!screen)
983     {
984         msg_Dbg( VLCIntf, "Using deepest screen" );
985         screen = [NSScreen deepestScreen];
986     }
987
988     if( p_vout )
989         vlc_object_release( p_vout );
990
991     screen_rect = [screen frame];
992
993     [o_fullscreen_btn setState: YES];
994
995     [NSCursor setHiddenUntilMouseMoves: YES];
996
997     if( blackout_other_displays )
998         [screen blackoutOtherScreens];
999
1000     /* Make sure we don't see the window flashes in float-on-top mode */
1001     i_originalLevel = [self level];
1002     [self setLevel:NSNormalWindowLevel];
1003
1004     /* Only create the o_fullscreen_window if we are not in the middle of the zooming animation */
1005     if (!o_fullscreen_window)
1006     {
1007         /* We can't change the styleMask of an already created NSWindow, so we create another window, and do eye catching stuff */
1008
1009         rect = [[o_video_view superview] convertRect: [o_video_view frame] toView: nil]; /* Convert to Window base coord */
1010         rect.origin.x += [self frame].origin.x;
1011         rect.origin.y += [self frame].origin.y;
1012         o_fullscreen_window = [[VLCWindow alloc] initWithContentRect:rect styleMask: NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:YES];
1013         [o_fullscreen_window setBackgroundColor: [NSColor blackColor]];
1014         [o_fullscreen_window setCanBecomeKeyWindow: YES];
1015
1016         if (![self isVisible] || [self alphaValue] == 0.0)
1017         {
1018             /* We don't animate if we are not visible, instead we
1019              * simply fade the display */
1020             CGDisplayFadeReservationToken token;
1021
1022             if( blackout_other_displays )
1023             {
1024                 CGAcquireDisplayFadeReservation( kCGMaxDisplayReservationInterval, &token );
1025                 CGDisplayFade( token, 0.5, kCGDisplayBlendNormal, kCGDisplayBlendSolidColor, 0, 0, 0, YES );
1026             }
1027
1028             if ([screen isMainScreen])
1029             {
1030                 if (OSX_LEOPARD)
1031                     SetSystemUIMode( kUIModeAllHidden, kUIOptionAutoShowMenuBar);
1032                 else
1033                     [NSApp setPresentationOptions:(NSApplicationPresentationAutoHideDock | NSApplicationPresentationAutoHideMenuBar)];
1034             }
1035
1036             [[o_video_view superview] replaceSubview:o_video_view with:o_temp_view];
1037             [o_temp_view setFrame:[o_video_view frame]];
1038             [o_fullscreen_window setContentView:o_video_view];
1039
1040             [o_fullscreen_window makeKeyAndOrderFront:self];
1041             [o_fullscreen_window orderFront:self animate:YES];
1042
1043             [o_fullscreen_window setFrame:screen_rect display:YES animate:YES];
1044             [o_fullscreen_window setLevel:NSNormalWindowLevel];
1045
1046             if( blackout_other_displays )
1047             {
1048                 CGDisplayFade( token, 0.3, kCGDisplayBlendSolidColor, kCGDisplayBlendNormal, 0, 0, 0, NO );
1049                 CGReleaseDisplayFadeReservation( token );
1050             }
1051
1052             /* Will release the lock */
1053             [self hasBecomeFullscreen];
1054
1055             return;
1056         }
1057
1058         /* Make sure we don't see the o_video_view disappearing of the screen during this operation */
1059         NSDisableScreenUpdates();
1060         [[o_video_view superview] replaceSubview:o_video_view with:o_temp_view];
1061         [o_temp_view setFrame:[o_video_view frame]];
1062         [o_fullscreen_window setContentView:o_video_view];
1063         [o_fullscreen_window makeKeyAndOrderFront:self];
1064         NSEnableScreenUpdates();
1065     }
1066
1067     /* We are in fullscreen (and no animation is running) */
1068     if (b_fullscreen)
1069     {
1070         /* Make sure we are hidden */
1071         [super orderOut: self];
1072         [self unlockFullscreenAnimation];
1073         return;
1074     }
1075
1076     if (o_fullscreen_anim1)
1077     {
1078         [o_fullscreen_anim1 stopAnimation];
1079         [o_fullscreen_anim1 release];
1080     }
1081     if (o_fullscreen_anim2)
1082     {
1083         [o_fullscreen_anim2 stopAnimation];
1084         [o_fullscreen_anim2 release];
1085     }
1086
1087     if ([screen isMainScreen])
1088     {
1089         if (OSX_LEOPARD)
1090             SetSystemUIMode( kUIModeAllHidden, kUIOptionAutoShowMenuBar);
1091         else
1092             [NSApp setPresentationOptions:(NSApplicationPresentationAutoHideDock | NSApplicationPresentationAutoHideMenuBar)];
1093     }
1094
1095     dict1 = [[NSMutableDictionary alloc] initWithCapacity:2];
1096     dict2 = [[NSMutableDictionary alloc] initWithCapacity:3];
1097
1098     [dict1 setObject:self forKey:NSViewAnimationTargetKey];
1099     [dict1 setObject:NSViewAnimationFadeOutEffect forKey:NSViewAnimationEffectKey];
1100
1101     [dict2 setObject:o_fullscreen_window forKey:NSViewAnimationTargetKey];
1102     [dict2 setObject:[NSValue valueWithRect:[o_fullscreen_window frame]] forKey:NSViewAnimationStartFrameKey];
1103     [dict2 setObject:[NSValue valueWithRect:screen_rect] forKey:NSViewAnimationEndFrameKey];
1104
1105     /* Strategy with NSAnimation allocation:
1106      - Keep at most 2 animation at a time
1107      - leaveFullscreen/enterFullscreen are the only responsible for releasing and alloc-ing
1108      */
1109     o_fullscreen_anim1 = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObject:dict1]];
1110     o_fullscreen_anim2 = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObject:dict2]];
1111
1112     [dict1 release];
1113     [dict2 release];
1114
1115     [o_fullscreen_anim1 setAnimationBlockingMode: NSAnimationNonblocking];
1116     [o_fullscreen_anim1 setDuration: 0.3];
1117     [o_fullscreen_anim1 setFrameRate: 30];
1118     [o_fullscreen_anim2 setAnimationBlockingMode: NSAnimationNonblocking];
1119     [o_fullscreen_anim2 setDuration: 0.2];
1120     [o_fullscreen_anim2 setFrameRate: 30];
1121
1122     [o_fullscreen_anim2 setDelegate: self];
1123     [o_fullscreen_anim2 startWhenAnimation: o_fullscreen_anim1 reachesProgress: 1.0];
1124
1125     [o_fullscreen_anim1 startAnimation];
1126     /* fullscreenAnimation will be unlocked when animation ends */
1127 }
1128
1129 - (void)hasBecomeFullscreen
1130 {
1131     [o_fullscreen_window makeFirstResponder: o_video_view];
1132
1133     [o_fullscreen_window makeKeyWindow];
1134     [o_fullscreen_window setAcceptsMouseMovedEvents: TRUE];
1135
1136     /* tell the fspanel to move itself to front next time it's triggered */
1137     [o_fspanel setVoutWasUpdated: (int)[[o_fullscreen_window screen] displayID]];
1138     [o_fspanel setActive: nil];
1139
1140     if([self isVisible])
1141         [super orderOut: self];
1142
1143     [o_fspanel setActive: nil];
1144
1145     b_fullscreen = YES;
1146     [self unlockFullscreenAnimation];
1147 }
1148
1149 - (void)leaveFullscreen
1150 {
1151     [self leaveFullscreenAndFadeOut: NO];
1152 }
1153
1154 - (void)leaveFullscreenAndFadeOut: (BOOL)fadeout
1155 {
1156     NSMutableDictionary *dict1, *dict2;
1157     NSRect frame;
1158     BOOL blackout_other_displays = config_GetInt( VLCIntf, "macosx-black" );
1159
1160     [self lockFullscreenAnimation];
1161
1162     b_fullscreen = NO;
1163     [o_fullscreen_btn setState: NO];
1164
1165     /* We always try to do so */
1166     [NSScreen unblackoutScreens];
1167     vout_thread_t *p_vout = getVout();
1168     if (p_vout)
1169     {
1170         if( var_GetBool( p_vout, "video-on-top" ) )
1171             [[o_video_view window] setLevel: NSStatusWindowLevel];
1172         else
1173             [[o_video_view window] setLevel: NSNormalWindowLevel];
1174         vlc_object_release( p_vout );
1175     }
1176     [[o_video_view window] makeKeyAndOrderFront: nil];
1177
1178     /* Don't do anything if o_fullscreen_window is already closed */
1179     if (!o_fullscreen_window)
1180     {
1181         [self unlockFullscreenAnimation];
1182         return;
1183     }
1184
1185     if (fadeout)
1186     {
1187         /* We don't animate if we are not visible, instead we
1188          * simply fade the display */
1189         CGDisplayFadeReservationToken token;
1190
1191         if( blackout_other_displays )
1192         {
1193             CGAcquireDisplayFadeReservation( kCGMaxDisplayReservationInterval, &token );
1194             CGDisplayFade( token, 0.3, kCGDisplayBlendNormal, kCGDisplayBlendSolidColor, 0, 0, 0, YES );
1195         }
1196
1197         [o_fspanel setNonActive: nil];
1198         if (OSX_LEOPARD)
1199             SetSystemUIMode( kUIModeNormal, kUIOptionAutoShowMenuBar);
1200         else
1201             [NSApp setPresentationOptions:(NSApplicationPresentationDefault)];
1202
1203         /* Will release the lock */
1204         [self hasEndedFullscreen];
1205
1206         /* Our window is hidden, and might be faded. We need to workaround that, so note it
1207          * here */
1208         b_window_is_invisible = YES;
1209
1210         if( blackout_other_displays )
1211         {
1212             CGDisplayFade( token, 0.5, kCGDisplayBlendSolidColor, kCGDisplayBlendNormal, 0, 0, 0, NO );
1213             CGReleaseDisplayFadeReservation( token );
1214         }
1215
1216         return;
1217     }
1218
1219     [self setAlphaValue: 0.0];
1220     [self orderFront: self];
1221     [[o_video_view window] orderFront: self];
1222
1223     [o_fspanel setNonActive: nil];
1224     if (OSX_LEOPARD)
1225         SetSystemUIMode( kUIModeNormal, kUIOptionAutoShowMenuBar);
1226     else
1227         [NSApp setPresentationOptions:(NSApplicationPresentationDefault)];
1228
1229     if (o_fullscreen_anim1)
1230     {
1231         [o_fullscreen_anim1 stopAnimation];
1232         [o_fullscreen_anim1 release];
1233     }
1234     if (o_fullscreen_anim2)
1235     {
1236         [o_fullscreen_anim2 stopAnimation];
1237         [o_fullscreen_anim2 release];
1238     }
1239
1240     frame = [[o_temp_view superview] convertRect: [o_temp_view frame] toView: nil]; /* Convert to Window base coord */
1241     frame.origin.x += [self frame].origin.x;
1242     frame.origin.y += [self frame].origin.y;
1243
1244     dict2 = [[NSMutableDictionary alloc] initWithCapacity:2];
1245     [dict2 setObject:self forKey:NSViewAnimationTargetKey];
1246     [dict2 setObject:NSViewAnimationFadeInEffect forKey:NSViewAnimationEffectKey];
1247
1248     o_fullscreen_anim2 = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects:dict2, nil]];
1249     [dict2 release];
1250
1251     [o_fullscreen_anim2 setAnimationBlockingMode: NSAnimationNonblocking];
1252     [o_fullscreen_anim2 setDuration: 0.3];
1253     [o_fullscreen_anim2 setFrameRate: 30];
1254
1255     [o_fullscreen_anim2 setDelegate: self];
1256
1257     dict1 = [[NSMutableDictionary alloc] initWithCapacity:3];
1258
1259     [dict1 setObject:o_fullscreen_window forKey:NSViewAnimationTargetKey];
1260     [dict1 setObject:[NSValue valueWithRect:[o_fullscreen_window frame]] forKey:NSViewAnimationStartFrameKey];
1261     [dict1 setObject:[NSValue valueWithRect:frame] forKey:NSViewAnimationEndFrameKey];
1262
1263     o_fullscreen_anim1 = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects:dict1, nil]];
1264     [dict1 release];
1265
1266     [o_fullscreen_anim1 setAnimationBlockingMode: NSAnimationNonblocking];
1267     [o_fullscreen_anim1 setDuration: 0.2];
1268     [o_fullscreen_anim1 setFrameRate: 30];
1269     [o_fullscreen_anim2 startWhenAnimation: o_fullscreen_anim1 reachesProgress: 1.0];
1270
1271     /* Make sure o_fullscreen_window is the frontmost window */
1272     [o_fullscreen_window orderFront: self];
1273
1274     [o_fullscreen_anim1 startAnimation];
1275     /* fullscreenAnimation will be unlocked when animation ends */
1276 }
1277
1278 - (void)hasEndedFullscreen
1279 {
1280     /* This function is private and should be only triggered at the end of the fullscreen change animation */
1281     /* Make sure we don't see the o_video_view disappearing of the screen during this operation */
1282     NSDisableScreenUpdates();
1283     [o_video_view retain];
1284     [o_video_view removeFromSuperviewWithoutNeedingDisplay];
1285     [[o_temp_view superview] replaceSubview:o_temp_view with:o_video_view];
1286     [o_video_view release];
1287     [o_video_view setFrame:[o_temp_view frame]];
1288     [self makeFirstResponder: o_video_view];
1289     if ([self isVisible])
1290         [super makeKeyAndOrderFront:self]; /* our version contains a workaround */
1291     [o_fullscreen_window orderOut: self];
1292     NSEnableScreenUpdates();
1293
1294     [o_fullscreen_window release];
1295     o_fullscreen_window = nil;
1296     [self setLevel:i_originalLevel];
1297
1298     [self unlockFullscreenAnimation];
1299 }
1300
1301 - (void)animationDidEnd:(NSAnimation*)animation
1302 {
1303     NSArray *viewAnimations;
1304     if( o_makekey_anim == animation )
1305     {
1306         [o_makekey_anim release];
1307         return;
1308     }
1309     if ([animation currentValue] < 1.0)
1310         return;
1311
1312     /* Fullscreen ended or started (we are a delegate only for leaveFullscreen's/enterFullscren's anim2) */
1313     viewAnimations = [o_fullscreen_anim2 viewAnimations];
1314     if ([viewAnimations count] >=1 &&
1315         [[[viewAnimations objectAtIndex: 0] objectForKey: NSViewAnimationEffectKey] isEqualToString:NSViewAnimationFadeInEffect])
1316     {
1317         /* Fullscreen ended */
1318         [self hasEndedFullscreen];
1319     }
1320     else
1321     {
1322         /* Fullscreen started */
1323         [self hasBecomeFullscreen];
1324     }
1325 }
1326
1327 - (void)orderOut: (id)sender
1328 {
1329     [super orderOut: sender];
1330
1331     /* Make sure we leave fullscreen */
1332     [self leaveFullscreenAndFadeOut: YES];
1333 }
1334
1335 - (void)makeKeyAndOrderFront: (id)sender
1336 {
1337     /* Hack
1338      * when we exit fullscreen and fade out, we may endup in
1339      * having a window that is faded. We can't have it fade in unless we
1340      * animate again. */
1341
1342     if(!b_window_is_invisible)
1343     {
1344         /* Make sure we don't do it too much */
1345         [super makeKeyAndOrderFront: sender];
1346         return;
1347     }
1348
1349     [super setAlphaValue:0.0f];
1350     [super makeKeyAndOrderFront: sender];
1351
1352     NSMutableDictionary * dict = [[NSMutableDictionary alloc] initWithCapacity:2];
1353     [dict setObject:self forKey:NSViewAnimationTargetKey];
1354     [dict setObject:NSViewAnimationFadeInEffect forKey:NSViewAnimationEffectKey];
1355
1356     o_makekey_anim = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObject:dict]];
1357     [dict release];
1358
1359     [o_makekey_anim setAnimationBlockingMode: NSAnimationNonblocking];
1360     [o_makekey_anim setDuration: 0.1];
1361     [o_makekey_anim setFrameRate: 30];
1362     [o_makekey_anim setDelegate: self];
1363
1364     [o_makekey_anim startAnimation];
1365     b_window_is_invisible = NO;
1366
1367     /* fullscreenAnimation will be unlocked when animation ends */
1368 }
1369
1370 /* Make sure setFrame gets executed on main thread especially if we are animating.
1371  * (Thus we won't block the video output thread) */
1372 - (void)setFrame:(NSRect)frame display:(BOOL)display animate:(BOOL)animate
1373 {
1374     struct { NSRect frame; BOOL display; BOOL animate;} args;
1375     NSData *packedargs;
1376
1377     args.frame = frame;
1378     args.display = display;
1379     args.animate = animate;
1380
1381     packedargs = [NSData dataWithBytes:&args length:sizeof(args)];
1382
1383     [self performSelectorOnMainThread:@selector(setFrameOnMainThread:)
1384                            withObject: packedargs waitUntilDone: YES];
1385 }
1386
1387 - (void)setFrameOnMainThread:(NSData*)packedargs
1388 {
1389     struct args { NSRect frame; BOOL display; BOOL animate; } * args = (struct args*)[packedargs bytes];
1390
1391     if( args->animate )
1392     {
1393         /* Make sure we don't block too long and set up a non blocking animation */
1394         NSDictionary * dict = [NSDictionary dictionaryWithObjectsAndKeys:
1395                                self, NSViewAnimationTargetKey,
1396                                [NSValue valueWithRect:[self frame]], NSViewAnimationStartFrameKey,
1397                                [NSValue valueWithRect:args->frame], NSViewAnimationEndFrameKey, nil];
1398
1399         NSViewAnimation * anim = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObject:dict]];
1400         [dict release];
1401
1402         [anim setAnimationBlockingMode: NSAnimationNonblocking];
1403         [anim setDuration: 0.4];
1404         [anim setFrameRate: 30];
1405         [anim startAnimation];
1406     }
1407     else {
1408         [super setFrame:args->frame display:args->display animate:args->animate];
1409     }
1410 }
1411
1412 #pragma mark -
1413 #pragma mark Side Bar Data handling
1414 /* taken under BSD-new from the PXSourceList sample project, adapted for VLC */
1415 - (NSUInteger)sourceList:(PXSourceList*)sourceList numberOfChildrenOfItem:(id)item
1416 {
1417         //Works the same way as the NSOutlineView data source: `nil` means a parent item
1418         if(item==nil) {
1419                 return [o_sidebaritems count];
1420         }
1421         else {
1422                 return [[item children] count];
1423         }
1424 }
1425
1426
1427 - (id)sourceList:(PXSourceList*)aSourceList child:(NSUInteger)index ofItem:(id)item
1428 {
1429     //Works the same way as the NSOutlineView data source: `nil` means a parent item
1430         if(item==nil) {
1431                 return [o_sidebaritems objectAtIndex:index];
1432         }
1433         else {
1434                 return [[item children] objectAtIndex:index];
1435         }
1436 }
1437
1438
1439 - (id)sourceList:(PXSourceList*)aSourceList objectValueForItem:(id)item
1440 {
1441         return [item title];
1442 }
1443
1444 - (void)sourceList:(PXSourceList*)aSourceList setObjectValue:(id)object forItem:(id)item
1445 {
1446         [item setTitle:object];
1447 }
1448
1449 - (BOOL)sourceList:(PXSourceList*)aSourceList isItemExpandable:(id)item
1450 {
1451         return [item hasChildren];
1452 }
1453
1454
1455 - (BOOL)sourceList:(PXSourceList*)aSourceList itemHasBadge:(id)item
1456 {
1457     if ([[item identifier] isEqualToString: @"playlist"])
1458         return YES;
1459
1460         return [item hasBadge];
1461 }
1462
1463
1464 - (NSInteger)sourceList:(PXSourceList*)aSourceList badgeValueForItem:(id)item
1465 {
1466     if ([[item identifier] isEqualToString: @"playlist"]) {
1467         playlist_t * p_playlist = pl_Get( VLCIntf );
1468         NSInteger i_playlist_size;
1469
1470         PL_LOCK;
1471         i_playlist_size = playlist_CurrentSize( p_playlist );
1472         PL_UNLOCK;
1473
1474         return i_playlist_size;
1475     }
1476         return [item badgeValue];
1477 }
1478
1479
1480 - (BOOL)sourceList:(PXSourceList*)aSourceList itemHasIcon:(id)item
1481 {
1482         return [item hasIcon];
1483 }
1484
1485
1486 - (NSImage*)sourceList:(PXSourceList*)aSourceList iconForItem:(id)item
1487 {
1488         return [item icon];
1489 }
1490
1491 - (NSMenu*)sourceList:(PXSourceList*)aSourceList menuForEvent:(NSEvent*)theEvent item:(id)item
1492 {
1493         if ([theEvent type] == NSRightMouseDown || ([theEvent type] == NSLeftMouseDown && ([theEvent modifierFlags] & NSControlKeyMask) == NSControlKeyMask)) {
1494                 NSMenu * m = [[NSMenu alloc] init];
1495                 if (item != nil)
1496                         [m addItemWithTitle:[item title] action:nil keyEquivalent:@""];
1497                 return [m autorelease];
1498         }
1499         return nil;
1500 }
1501
1502 #pragma mark -
1503 #pragma mark Side Bar Delegate Methods
1504 /* taken under BSD-new from the PXSourceList sample project, adapted for VLC */
1505 - (BOOL)sourceList:(PXSourceList*)aSourceList isGroupAlwaysExpanded:(id)group
1506 {
1507         if([[group identifier] isEqualToString:@"library"])
1508                 return YES;
1509
1510         return NO;
1511 }
1512
1513 - (void)sourceListSelectionDidChange:(NSNotification *)notification
1514 {
1515         NSIndexSet *selectedIndexes = [o_sidebar_view selectedRowIndexes];
1516
1517         //Set the label text to represent the new selection
1518     if([selectedIndexes count]==1) {
1519                 NSString *title = [[o_sidebar_view itemAtRow:[selectedIndexes firstIndex]] title];
1520
1521                 [o_chosen_category_lbl setStringValue:title];
1522         }
1523         else {
1524                 [o_chosen_category_lbl setStringValue:@"(none)"];
1525         }
1526 }
1527
1528 @end
1529
1530 @implementation VLCProgressBarGradientEffect
1531 - (void)dealloc
1532 {
1533     [o_time_sld_gradient_left_img release];
1534     [o_time_sld_gradient_middle_img release];
1535     [o_time_sld_gradient_right_img release];
1536     [super dealloc];
1537 }
1538
1539 - (void)loadImagesInDarkStyle: (BOOL)b_value
1540 {
1541     if (b_value)
1542     {
1543         o_time_sld_gradient_left_img = [[NSImage imageNamed:@"progressbar-fill-left_dark"] retain];
1544         o_time_sld_gradient_middle_img = [[NSImage imageNamed:@"progressbar-fill-middle_dark"] retain];
1545         o_time_sld_gradient_right_img = [[NSImage imageNamed:@"progressbar-fill-right_dark"] retain];
1546     }
1547     else
1548     {
1549         o_time_sld_gradient_left_img = [[NSImage imageNamed:@"progression-fill-left"] retain];
1550         o_time_sld_gradient_middle_img = [[NSImage imageNamed:@"progression-fill-middle"] retain];
1551         o_time_sld_gradient_right_img = [[NSImage imageNamed:@"progression-fill-right"] retain];
1552     }
1553 }
1554
1555 - (void)drawRect:(NSRect)rect
1556 {
1557     NSRect bnds = [self bounds];
1558     NSDrawThreePartImage( bnds, o_time_sld_gradient_left_img, o_time_sld_gradient_middle_img, o_time_sld_gradient_right_img, NO, NSCompositeSourceOver, 1, NO );
1559 }
1560 @end