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