]> git.sesse.net Git - vlc/blob - modules/gui/macosx/MainWindow.m
macosx: fixed crash due to race condition on launch
[vlc] / modules / gui / macosx / MainWindow.m
1 /*****************************************************************************
2  * MainWindow.m: MacOS X interface module
3  *****************************************************************************
4  * Copyright (C) 2002-2012 VLC authors and 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 "playlist.h"
36 #import "SideBarItem.h"
37 #import <math.h>
38 #import <vlc_playlist.h>
39 #import <vlc_aout_intf.h>
40 #import <vlc_url.h>
41 #import <vlc_strings.h>
42 #import <vlc_services_discovery.h>
43 #import <vlc_aout_intf.h>
44
45 #import "ControlsBar.h"
46 #import "VideoView.h"
47
48
49 @interface VLCMainWindow ()
50 - (void)resizePlaylistAfterCollapse;
51 - (void)makeSplitViewVisible;
52 - (void)makeSplitViewHidden;
53
54 @end
55
56 @implementation VLCMainWindow
57 static const float f_min_video_height = 70.0;
58
59 static VLCMainWindow *_o_sharedInstance = nil;
60
61 + (VLCMainWindow *)sharedInstance
62 {
63     return _o_sharedInstance ? _o_sharedInstance : [[self alloc] init];
64 }
65
66 #pragma mark -
67 #pragma mark Initialization
68
69 - (id)init
70 {
71     if (_o_sharedInstance) {
72         [self dealloc];
73         return _o_sharedInstance;
74     } else
75         _o_sharedInstance = [super init];
76
77     return _o_sharedInstance;
78 }
79
80 - (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)styleMask
81                   backing:(NSBackingStoreType)backingType defer:(BOOL)flag
82 {
83     self = [super initWithContentRect:contentRect styleMask:styleMask
84                               backing:backingType defer:flag];
85     _o_sharedInstance = self;
86
87     [[VLCMain sharedInstance] updateTogglePlaylistState];
88
89     return self;
90 }
91
92 - (BOOL)isEvent:(NSEvent *)o_event forKey:(const char *)keyString
93 {
94     char *key;
95     NSString *o_key;
96
97     key = config_GetPsz(VLCIntf, keyString);
98     o_key = [NSString stringWithFormat:@"%s", key];
99     FREENULL(key);
100
101     unsigned int i_keyModifiers = [[VLCStringUtility sharedInstance] VLCModifiersToCocoa:o_key];
102
103     NSString * characters = [o_event charactersIgnoringModifiers];
104     if ([characters length] > 0) {
105         return [[characters lowercaseString] isEqualToString: [[VLCStringUtility sharedInstance] VLCKeyToString: o_key]] &&
106                 (i_keyModifiers & NSShiftKeyMask)     == ([o_event modifierFlags] & NSShiftKeyMask) &&
107                 (i_keyModifiers & NSControlKeyMask)   == ([o_event modifierFlags] & NSControlKeyMask) &&
108                 (i_keyModifiers & NSAlternateKeyMask) == ([o_event modifierFlags] & NSAlternateKeyMask) &&
109                 (i_keyModifiers & NSCommandKeyMask)   == ([o_event modifierFlags] & NSCommandKeyMask);
110     }
111     return NO;
112 }
113
114 - (BOOL)performKeyEquivalent:(NSEvent *)o_event
115 {
116     BOOL b_force = NO;
117     // these are key events which should be handled by vlc core, but are attached to a main menu item
118     if (![self isEvent: o_event forKey: "key-vol-up"] &&
119         ![self isEvent: o_event forKey: "key-vol-down"] &&
120         ![self isEvent: o_event forKey: "key-vol-mute"]) {
121         /* We indeed want to prioritize some Cocoa key equivalent against libvlc,
122          so we perform the menu equivalent now. */
123         if ([[NSApp mainMenu] performKeyEquivalent:o_event])
124             return TRUE;
125     }
126     else
127         b_force = YES;
128
129     return [[VLCMain sharedInstance] hasDefinedShortcutKey:o_event force:b_force] ||
130            [(VLCControls *)[[VLCMain sharedInstance] controls] keyEvent:o_event];
131 }
132
133 - (void)dealloc
134 {
135     if (b_dark_interface)
136         [o_color_backdrop release];
137
138     [[NSNotificationCenter defaultCenter] removeObserver: self];
139     [o_sidebaritems release];
140
141     if (o_extra_video_window) {
142         [o_extra_video_window release];
143         o_extra_video_window = nil;
144     }
145
146     if (o_detached_video_window) {
147         [o_detached_video_window release];
148         o_detached_video_window = nil;
149     }
150
151     [super dealloc];
152 }
153
154 - (void)awakeFromNib
155 {
156     BOOL b_splitviewShouldBeHidden = NO;
157
158     /* setup the styled interface */
159     b_nativeFullscreenMode = NO;
160 #ifdef MAC_OS_X_VERSION_10_7
161     if (!OSX_SNOW_LEOPARD)
162         b_nativeFullscreenMode = var_InheritBool(VLCIntf, "macosx-nativefullscreenmode");
163 #endif
164     t_hide_mouse_timer = nil;
165     [self useOptimizedDrawing: YES];
166     
167     [[o_search_fld cell] setPlaceholderString: _NS("Search")];
168     [[o_search_fld cell] accessibilitySetOverrideValue:_NS("Enter a term to search the playlist. Results will be selected in the table.") forAttribute:NSAccessibilityDescriptionAttribute];
169
170     [o_dropzone_btn setTitle: _NS("Open media...")];
171     [[o_dropzone_btn cell] accessibilitySetOverrideValue:_NS("Click to open an advanced dialog to select the media to play. You can also drop files here to play.") forAttribute:NSAccessibilityDescriptionAttribute];
172     [o_dropzone_lbl setStringValue: _NS("Drop media here")];
173
174     [o_podcast_add_btn setTitle: _NS("Subscribe")];
175     [o_podcast_remove_btn setTitle: _NS("Unsubscribe")];
176     [o_podcast_subscribe_title_lbl setStringValue: _NS("Subscribe to a podcast")];
177     [o_podcast_subscribe_subtitle_lbl setStringValue: _NS("Enter URL of the podcast to subscribe to:")];
178     [o_podcast_subscribe_cancel_btn setTitle: _NS("Cancel")];
179     [o_podcast_subscribe_ok_btn setTitle: _NS("Subscribe")];
180     [o_podcast_unsubscribe_title_lbl setStringValue: _NS("Unsubscribe from a podcast")];
181     [o_podcast_unsubscribe_subtitle_lbl setStringValue: _NS("Select the podcast you would like to unsubscribe from:")];
182     [o_podcast_unsubscribe_ok_btn setTitle: _NS("Unsubscribe")];
183     [o_podcast_unsubscribe_cancel_btn setTitle: _NS("Cancel")];
184
185     /* interface builder action */
186     float f_threshold_height = f_min_video_height + [[o_controls_bar bottomBarView] frame].size.height;
187     if (b_dark_interface)
188         f_threshold_height += [o_titlebar_view frame].size.height;
189     if ([[self contentView] frame].size.height < f_threshold_height)
190         b_splitviewShouldBeHidden = YES;
191
192     [self setDelegate: self];
193     [self setExcludedFromWindowsMenu: YES];
194     [self setAcceptsMouseMovedEvents: YES];
195     // Set that here as IB seems to be buggy
196     if (b_dark_interface) {
197         [self setContentMinSize:NSMakeSize(604., 288. + [o_titlebar_view frame].size.height)];
198     } else {
199         [self setContentMinSize:NSMakeSize(604., 288.)];
200     }
201
202     [self setTitle: _NS("VLC media player")];
203
204     b_dropzone_active = YES;
205     o_temp_view = [[NSView alloc] init];
206     [o_temp_view setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
207     [o_dropzone_view setFrame: [o_playlist_table frame]];
208     [o_left_split_view setFrame: [o_sidebar_view frame]];
209     
210     if (b_nativeFullscreenMode) {
211         [self setCollectionBehavior: NSWindowCollectionBehaviorFullScreenPrimary];
212     } else {
213         [o_titlebar_view setFullscreenButtonHidden: YES];
214     }
215
216     if (!OSX_SNOW_LEOPARD) {
217         /* the default small size of the search field is slightly different on Lion, let's work-around that */
218         NSRect frame;
219         frame = [o_search_fld frame];
220         frame.origin.y = frame.origin.y + 2.0;
221         frame.size.height = frame.size.height - 1.0;
222         [o_search_fld setFrame: frame];
223     }
224
225     /* create the sidebar */
226     o_sidebaritems = [[NSMutableArray alloc] init];
227     SideBarItem *libraryItem = [SideBarItem itemWithTitle:_NS("LIBRARY") identifier:@"library"];
228     SideBarItem *playlistItem = [SideBarItem itemWithTitle:_NS("Playlist") identifier:@"playlist"];
229     [playlistItem setIcon: [NSImage imageNamed:@"sidebar-playlist"]];
230     SideBarItem *medialibraryItem = [SideBarItem itemWithTitle:_NS("Media Library") identifier:@"medialibrary"];
231     [medialibraryItem setIcon: [NSImage imageNamed:@"sidebar-playlist"]];
232     SideBarItem *mycompItem = [SideBarItem itemWithTitle:_NS("MY COMPUTER") identifier:@"mycomputer"];
233     SideBarItem *devicesItem = [SideBarItem itemWithTitle:_NS("DEVICES") identifier:@"devices"];
234     SideBarItem *lanItem = [SideBarItem itemWithTitle:_NS("LOCAL NETWORK") identifier:@"localnetwork"];
235     SideBarItem *internetItem = [SideBarItem itemWithTitle:_NS("INTERNET") identifier:@"internet"];
236
237     /* SD subnodes, inspired by the Qt4 intf */
238     char **ppsz_longnames;
239     int *p_categories;
240     char **ppsz_names = vlc_sd_GetNames(pl_Get(VLCIntf), &ppsz_longnames, &p_categories);
241     if (!ppsz_names)
242         msg_Err(VLCIntf, "no sd item found"); //TODO
243     char **ppsz_name = ppsz_names, **ppsz_longname = ppsz_longnames;
244     int *p_category = p_categories;
245     NSMutableArray *internetItems = [[NSMutableArray alloc] init];
246     NSMutableArray *devicesItems = [[NSMutableArray alloc] init];
247     NSMutableArray *lanItems = [[NSMutableArray alloc] init];
248     NSMutableArray *mycompItems = [[NSMutableArray alloc] init];
249     NSString *o_identifier;
250     for (; *ppsz_name; ppsz_name++, ppsz_longname++, p_category++) {
251         o_identifier = [NSString stringWithCString: *ppsz_name encoding: NSUTF8StringEncoding];
252         switch (*p_category) {
253             case SD_CAT_INTERNET:
254                     [internetItems addObject: [SideBarItem itemWithTitle: _NS(*ppsz_longname) identifier: o_identifier]];
255                     if (!strncmp(*ppsz_name, "podcast", 7))
256                         [[internetItems lastObject] setIcon: [NSImage imageNamed:@"sidebar-podcast"]];
257                     else
258                         [[internetItems lastObject] setIcon: [NSImage imageNamed:@"NSApplicationIcon"]];
259                     [[internetItems lastObject] setSdtype: SD_CAT_INTERNET];
260                     [[internetItems lastObject] setUntranslatedTitle: [NSString stringWithUTF8String: *ppsz_longname]];
261                 break;
262             case SD_CAT_DEVICES:
263                     [devicesItems addObject: [SideBarItem itemWithTitle: _NS(*ppsz_longname) identifier: o_identifier]];
264                     [[devicesItems lastObject] setIcon: [NSImage imageNamed:@"NSApplicationIcon"]];
265                     [[devicesItems lastObject] setSdtype: SD_CAT_DEVICES];
266                     [[devicesItems lastObject] setUntranslatedTitle: [NSString stringWithUTF8String: *ppsz_longname]];
267                 break;
268             case SD_CAT_LAN:
269                     [lanItems addObject: [SideBarItem itemWithTitle: _NS(*ppsz_longname) identifier: o_identifier]];
270                     [[lanItems lastObject] setIcon: [NSImage imageNamed:@"sidebar-local"]];
271                     [[lanItems lastObject] setSdtype: SD_CAT_LAN];
272                     [[lanItems lastObject] setUntranslatedTitle: [NSString stringWithUTF8String: *ppsz_longname]];
273                 break;
274             case SD_CAT_MYCOMPUTER:
275                     [mycompItems addObject: [SideBarItem itemWithTitle: _NS(*ppsz_longname) identifier: o_identifier]];
276                     if (!strncmp(*ppsz_name, "video_dir", 9))
277                         [[mycompItems lastObject] setIcon: [NSImage imageNamed:@"sidebar-movie"]];
278                     else if (!strncmp(*ppsz_name, "audio_dir", 9))
279                         [[mycompItems lastObject] setIcon: [NSImage imageNamed:@"sidebar-music"]];
280                     else if (!strncmp(*ppsz_name, "picture_dir", 11))
281                         [[mycompItems lastObject] setIcon: [NSImage imageNamed:@"sidebar-pictures"]];
282                     else
283                         [[mycompItems lastObject] setIcon: [NSImage imageNamed:@"NSApplicationIcon"]];
284                     [[mycompItems lastObject] setUntranslatedTitle: [NSString stringWithUTF8String: *ppsz_longname]];
285                     [[mycompItems lastObject] setSdtype: SD_CAT_MYCOMPUTER];
286                 break;
287             default:
288                 msg_Warn(VLCIntf, "unknown SD type found, skipping (%s)", *ppsz_name);
289                 break;
290         }
291
292         free(*ppsz_name);
293         free(*ppsz_longname);
294     }
295     [mycompItem setChildren: [NSArray arrayWithArray: mycompItems]];
296     [devicesItem setChildren: [NSArray arrayWithArray: devicesItems]];
297     [lanItem setChildren: [NSArray arrayWithArray: lanItems]];
298     [internetItem setChildren: [NSArray arrayWithArray: internetItems]];
299     [mycompItems release];
300     [devicesItems release];
301     [lanItems release];
302     [internetItems release];
303     free(ppsz_names);
304     free(ppsz_longnames);
305     free(p_categories);
306
307     [libraryItem setChildren: [NSArray arrayWithObjects: playlistItem, medialibraryItem, nil]];
308     [o_sidebaritems addObject: libraryItem];
309     if ([mycompItem hasChildren])
310         [o_sidebaritems addObject: mycompItem];
311     if ([devicesItem hasChildren])
312         [o_sidebaritems addObject: devicesItem];
313     if ([lanItem hasChildren])
314         [o_sidebaritems addObject: lanItem];
315     if ([internetItem hasChildren])
316         [o_sidebaritems addObject: internetItem];
317
318     [o_sidebar_view reloadData];
319     [o_sidebar_view selectRowIndexes:[NSIndexSet indexSetWithIndex:1] byExtendingSelection:NO];
320     [o_sidebar_view setDropItem:playlistItem dropChildIndex:NSOutlineViewDropOnItemIndex];
321     [o_sidebar_view registerForDraggedTypes:[NSArray arrayWithObjects: NSFilenamesPboardType, @"VLCPlaylistItemPboardType", nil]];
322
323     [o_sidebar_view setAutosaveName:@"mainwindow-sidebar"];
324     [(PXSourceList *)o_sidebar_view setDataSource:self];
325     [o_sidebar_view setDelegate:self];
326     [o_sidebar_view setAutosaveExpandedItems:YES];
327
328     [o_sidebar_view expandItem: libraryItem expandChildren: YES];
329
330     /* make sure we display the desired default appearance when VLC launches for the first time */
331     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
332     if (![defaults objectForKey:@"VLCFirstRun"]) {
333         [defaults setObject:[NSDate date] forKey:@"VLCFirstRun"];
334
335         NSUInteger i_sidebaritem_count = [o_sidebaritems count];
336         for (NSUInteger x = 0; x < i_sidebaritem_count; x++)
337             [o_sidebar_view expandItem: [o_sidebaritems objectAtIndex: x] expandChildren: YES];
338     }
339
340     if (b_dark_interface) {
341         [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(windowResizedOrMoved:) name: NSWindowDidResizeNotification object: nil];
342         [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(windowResizedOrMoved:) name: NSWindowDidMoveNotification object: nil];
343
344         [self setBackgroundColor: [NSColor clearColor]];
345         [self setOpaque: NO];
346         [self display];
347         [self setHasShadow:NO];
348         [self setHasShadow:YES];
349
350         NSRect winrect = [self frame];
351         CGFloat f_titleBarHeight = [o_titlebar_view frame].size.height;
352
353         [o_titlebar_view setFrame: NSMakeRect(0, winrect.size.height - f_titleBarHeight,
354                                               winrect.size.width, f_titleBarHeight)];
355         [[self contentView] addSubview: o_titlebar_view positioned: NSWindowAbove relativeTo: o_split_view];
356
357         if (winrect.size.height > 100) {
358             [self setFrame: winrect display:YES animate:YES];
359             previousSavedFrame = winrect;
360         }
361
362         winrect = [o_split_view frame];
363         winrect.size.height = winrect.size.height - f_titleBarHeight;
364         [o_split_view setFrame: winrect];
365         [o_video_view setFrame: winrect];
366
367         o_color_backdrop = [[VLCColorView alloc] initWithFrame: [o_split_view frame]];
368         [[self contentView] addSubview: o_color_backdrop positioned: NSWindowBelow relativeTo: o_split_view];
369         [o_color_backdrop setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
370         
371     } else {
372         [o_video_view setFrame: [o_split_view frame]];
373         [o_playlist_table setBorderType: NSNoBorder];
374         [o_sidebar_scrollview setBorderType: NSNoBorder];
375     }
376
377     [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(someWindowWillClose:) name: NSWindowWillCloseNotification object: nil];
378     [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(someWindowWillMiniaturize:) name: NSWindowWillMiniaturizeNotification object:nil];
379     [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(applicationWillTerminate:) name: NSApplicationWillTerminateNotification object: nil];
380
381     [o_split_view setAutosaveName:@"10thanniversary-splitview"];
382     if (b_splitviewShouldBeHidden) {
383         [self hideSplitView];
384         i_lastSplitViewHeight = 300;
385     }
386
387     /* sanity check for the window size */
388     NSRect frame = [self frame];
389     NSSize screenSize = [[self screen] frame].size;
390     if (screenSize.width <= frame.size.width || screenSize.height <= frame.size.height) {
391         nativeVideoSize = screenSize;
392         [self resizeWindow];
393     }
394 }
395
396 #pragma mark -
397
398 - (VLCMainWindowControlsBar *)controlsBar;
399 {
400     return (VLCMainWindowControlsBar *)o_controls_bar;
401 }
402
403 - (void)resizePlaylistAfterCollapse
404 {
405     NSRect plrect;
406     plrect = [o_playlist_table frame];
407     plrect.size.height = i_lastSplitViewHeight - 20.0; // actual pl top bar height, which differs from its frame
408     [[o_playlist_table animator] setFrame: plrect];
409
410     NSRect rightSplitRect;
411     rightSplitRect = [o_right_split_view frame];
412     plrect = [o_dropzone_box frame];
413     plrect.origin.x = (rightSplitRect.size.width - plrect.size.width) / 2;
414     plrect.origin.y = (rightSplitRect.size.height - plrect.size.height) / 2;
415     [[o_dropzone_box animator] setFrame: plrect];
416 }
417
418 - (void)makeSplitViewVisible
419 {
420     if (b_dark_interface)
421         [self setContentMinSize: NSMakeSize(604., 288. + [o_titlebar_view frame].size.height)];
422     else
423         [self setContentMinSize: NSMakeSize(604., 288.)];
424
425     NSRect old_frame = [self frame];
426     float newHeight = [self minSize].height;
427     if (old_frame.size.height < newHeight) {
428         NSRect new_frame = old_frame;
429         new_frame.origin.y = old_frame.origin.y + old_frame.size.height - newHeight;
430         new_frame.size.height = newHeight;
431
432         [[self animator] setFrame: new_frame display: YES animate: YES];
433     }
434
435     [o_video_view setHidden: YES];
436     [o_split_view setHidden: NO];
437     [self makeFirstResponder: nil];
438
439 }
440
441 - (void)makeSplitViewHidden
442 {
443     if (b_dark_interface)
444         [self setContentMinSize: NSMakeSize(604., f_min_video_height + [o_titlebar_view frame].size.height)];
445     else
446         [self setContentMinSize: NSMakeSize(604., f_min_video_height)];
447
448     [o_split_view setHidden: YES];
449     [o_video_view setHidden: NO];
450
451     if ([[o_video_view subviews] count] > 0)
452         [self makeFirstResponder: [[o_video_view subviews] objectAtIndex:0]];
453 }
454
455 // only exception for an controls bar button action
456 - (IBAction)togglePlaylist:(id)sender
457 {
458     if (![self isVisible] && sender != nil) {
459         [self makeKeyAndOrderFront: sender];
460         return;
461     }
462
463     BOOL b_activeVideo = [[VLCMain sharedInstance] activeVideoPlayback];
464     BOOL b_restored = NO;
465
466     // TODO: implement toggle playlist in this situation (triggerd via menu item).
467     // but for now we block this case, to avoid displaying only the half
468     if (b_nativeFullscreenMode && b_fullscreen && b_activeVideo && sender != nil)
469         return;
470
471     if (b_dropzone_active && ([[NSApp currentEvent] modifierFlags] & NSAlternateKeyMask) != 0) {
472         [self hideDropZone];
473         return;
474     }
475
476     if (!(b_nativeFullscreenMode && b_fullscreen) && !b_splitview_removed && ((([[NSApp currentEvent] modifierFlags] & NSAlternateKeyMask) != 0 && b_activeVideo)
477                                                                               || (b_nonembedded && sender != nil)
478                                                                               || (!b_activeVideo && sender != nil)
479                                                                               || b_minimized_view))
480         [self hideSplitView];
481     else {
482         if (b_splitview_removed) {
483             if (!b_nonembedded || (sender != nil && b_nonembedded))
484                 [self showSplitView];
485
486             if (sender == nil)
487                 b_minimized_view = YES;
488             else
489                 b_minimized_view = NO;
490
491             if (b_activeVideo)
492                 b_restored = YES;
493         }
494
495         if (!b_nonembedded) {
496             if (([o_video_view isHidden] && b_activeVideo) || b_restored || (b_activeVideo && sender == nil))
497                 [self makeSplitViewHidden];
498             else
499                 [self makeSplitViewVisible];
500         } else {
501             [o_split_view setHidden: NO];
502             [o_playlist_table setHidden: NO];
503             [o_video_view setHidden: !b_activeVideo];
504             if (b_activeVideo && [[o_video_view subviews] count] > 0)
505                 [[o_video_view window] makeFirstResponder: [[o_video_view subviews] objectAtIndex:0]];
506         }
507     }
508 }
509
510 - (IBAction)dropzoneButtonAction:(id)sender
511 {
512     [[[VLCMain sharedInstance] open] openFileGeneric];
513 }
514
515 #pragma mark -
516 #pragma mark overwritten default functionality
517
518 - (void)windowResizedOrMoved:(NSNotification *)notification
519 {
520     [self saveFrameUsingName: [self frameAutosaveName]];
521 }
522
523 - (void)applicationWillTerminate:(NSNotification *)notification
524 {
525     [self saveFrameUsingName: [self frameAutosaveName]];
526 }
527
528
529 - (void)someWindowWillClose:(NSNotification *)notification
530 {
531     id obj = [notification object];
532     if (obj == o_detached_video_window || obj == o_extra_video_window || (obj == self && !b_nonembedded)) {
533         if ([[VLCMain sharedInstance] activeVideoPlayback])
534             [[VLCCoreInteraction sharedInstance] stop];
535     }
536 }
537
538 - (void)someWindowWillMiniaturize:(NSNotification *)notification
539 {
540     if (config_GetInt(VLCIntf, "macosx-pause-minimized")) {
541         id obj = [notification object];
542         if (obj == o_detached_video_window || obj == o_extra_video_window || (obj == self && !b_nonembedded)) {
543             if ([[VLCMain sharedInstance] activeVideoPlayback])
544                 [[VLCCoreInteraction sharedInstance] pause];
545         }
546     }
547 }
548
549 - (NSSize)windowWillResize:(NSWindow *)window toSize:(NSSize)proposedFrameSize
550 {
551     id videoWindow = [o_video_view window];
552     if (![[VLCMain sharedInstance] activeVideoPlayback] || nativeVideoSize.width == 0. || nativeVideoSize.height == 0. || window != videoWindow)
553         return proposedFrameSize;
554
555     // needed when entering lion fullscreen mode
556     if (b_fullscreen)
557         return proposedFrameSize;
558
559     if ([[VLCCoreInteraction sharedInstance] aspectRatioIsLocked]) {
560         NSRect videoWindowFrame = [videoWindow frame];
561         NSRect viewRect = [o_video_view convertRect:[o_video_view bounds] toView: nil];
562         NSRect contentRect = [videoWindow contentRectForFrameRect:videoWindowFrame];
563         float marginy = viewRect.origin.y + videoWindowFrame.size.height - contentRect.size.height;
564         float marginx = contentRect.size.width - viewRect.size.width;
565         if (b_dark_interface)// && b_video_deco)
566             marginy += [o_titlebar_view frame].size.height;
567
568         proposedFrameSize.height = (proposedFrameSize.width - marginx) * nativeVideoSize.height / nativeVideoSize.width + marginy;
569     }
570
571     return proposedFrameSize;
572 }
573
574 #pragma mark -
575 #pragma mark Update interface and respond to foreign events
576 - (void)showDropZone
577 {
578     b_dropzone_active = YES;
579     [o_right_split_view addSubview: o_dropzone_view positioned:NSWindowAbove relativeTo:o_playlist_table];
580     [o_dropzone_view setFrame: [o_playlist_table frame]];
581     [[o_playlist_table animator] setHidden:YES];
582 }
583
584 - (void)hideDropZone
585 {
586     b_dropzone_active = NO;
587     [o_dropzone_view removeFromSuperview];
588     [[o_playlist_table animator] setHidden: NO];
589 }
590
591 - (void)hideSplitView
592 {
593     NSRect winrect = [self frame];
594     i_lastSplitViewHeight = [o_split_view frame].size.height;
595     winrect.size.height = winrect.size.height - i_lastSplitViewHeight;
596     winrect.origin.y = winrect.origin.y + i_lastSplitViewHeight;
597     [self setFrame: winrect display: YES animate: YES];
598     [self performSelector:@selector(hideDropZone) withObject:nil afterDelay:0.1];
599     if (b_dark_interface) {
600         [self setContentMinSize: NSMakeSize(604., [[o_controls_bar bottomBarView] frame].size.height + [o_titlebar_view frame].size.height)];
601         [self setContentMaxSize: NSMakeSize(FLT_MAX, [[o_controls_bar bottomBarView] frame].size.height + [o_titlebar_view frame].size.height)];
602     } else {
603         [self setContentMinSize: NSMakeSize(604., [[o_controls_bar bottomBarView] frame].size.height)];
604         [self setContentMaxSize: NSMakeSize(FLT_MAX, [[o_controls_bar bottomBarView] frame].size.height)];
605     }
606
607     b_splitview_removed = YES;
608 }
609
610 - (void)showSplitView
611 {
612     [self updateWindow];
613     if (b_dark_interface)
614         [self setContentMinSize:NSMakeSize(604., 288. + [o_titlebar_view frame].size.height)];
615     else
616         [self setContentMinSize:NSMakeSize(604., 288.)];
617     [self setContentMaxSize: NSMakeSize(FLT_MAX, FLT_MAX)];
618
619     NSRect winrect;
620     winrect = [self frame];
621     winrect.size.height = winrect.size.height + i_lastSplitViewHeight;
622     winrect.origin.y = winrect.origin.y - i_lastSplitViewHeight;
623     [self setFrame: winrect display: YES animate: YES];
624
625     [self performSelector:@selector(resizePlaylistAfterCollapse) withObject: nil afterDelay:0.75];
626
627     b_splitview_removed = NO;
628 }
629
630 - (void)updateTimeSlider
631 {
632     [o_controls_bar updateTimeSlider];
633     [[self controlsBar] updatePosAndTimeInFSPanel:o_fspanel];
634     if (o_detached_video_window)
635         [[o_detached_video_window controlsBar] updateTimeSlider];
636 }
637
638 - (void)updateName
639 {
640     input_thread_t * p_input;
641     p_input = pl_CurrentInput(VLCIntf);
642     if (p_input) {
643         NSString *aString;
644         char *format = var_InheritString(VLCIntf, "input-title-format");
645         char *formated = str_format_meta(pl_Get(VLCIntf), format);
646         free(format);
647         aString = [NSString stringWithUTF8String:formated];
648         free(formated);
649
650         char *uri = input_item_GetURI(input_GetItem(p_input));
651
652         NSURL * o_url = [NSURL URLWithString: [NSString stringWithUTF8String: uri]];
653         if ([o_url isFileURL]) {
654             [self setRepresentedURL: o_url];
655             if (o_detached_video_window)
656                 [o_detached_video_window setRepresentedURL: o_url];
657         } else {
658             [self setRepresentedURL: nil];
659             if (o_detached_video_window)
660                 [o_detached_video_window setRepresentedURL: nil];
661         }
662         free(uri);
663
664         if ([aString isEqualToString:@""]) {
665             if ([o_url isFileURL])
666                 aString = [[NSFileManager defaultManager] displayNameAtPath: [o_url path]];
667             else
668                 aString = [o_url absoluteString];
669         }
670
671         [self setTitle: aString];
672         if (b_nonembedded && o_detached_video_window && [[VLCMain sharedInstance] activeVideoPlayback])
673             [o_detached_video_window setTitle: aString];
674
675         [o_fspanel setStreamTitle: aString];
676         vlc_object_release(p_input);
677     } else {
678         [self setTitle: _NS("VLC media player")];
679         [self setRepresentedURL: nil];
680     }
681 }
682
683 - (void)updateWindow
684 {
685     [o_controls_bar updateControls];
686     if (o_detached_video_window)
687         [[o_detached_video_window controlsBar] updateControls];
688
689     bool b_seekable = false;
690
691     playlist_t * p_playlist = pl_Get(VLCIntf);
692     input_thread_t * p_input = playlist_CurrentInput(p_playlist);
693     if (p_input) {
694         /* seekable streams */
695         b_seekable = var_GetBool(p_input, "can-seek");
696
697         vlc_object_release(p_input);
698     }
699
700     [self updateTimeSlider];
701     if ([o_fspanel respondsToSelector:@selector(setSeekable:)])
702         [o_fspanel setSeekable: b_seekable];
703
704     PL_LOCK;
705     if ([[[VLCMain sharedInstance] playlist] currentPlaylistRoot] != p_playlist->p_local_category || p_playlist->p_local_category->i_children > 0)
706         [self hideDropZone];
707     else
708         [self showDropZone];
709     PL_UNLOCK;
710     [o_sidebar_view setNeedsDisplay:YES];
711 }
712
713 - (void)setPause
714 {
715     [o_controls_bar setPause];
716     if (o_detached_video_window)
717         [[o_detached_video_window controlsBar] setPause];
718     [o_fspanel setPause];
719 }
720
721 - (void)setPlay
722 {
723     [o_controls_bar setPlay];
724     if (o_detached_video_window)
725         [[o_detached_video_window controlsBar] setPlay];
726     [o_fspanel setPlay];
727 }
728
729 - (void)updateVolumeSlider
730 {
731     [[self controlsBar] updateVolumeSlider];
732     [o_fspanel setVolumeLevel: [[VLCCoreInteraction sharedInstance] volume]];
733 }
734
735 #pragma mark -
736 #pragma mark Video Output handling
737
738 - (VLCVoutView *)setupVout:(vout_window_t *)p_wnd
739 {
740     BOOL b_video_deco = var_InheritBool(VLCIntf, "video-deco");
741     BOOL b_video_wallpaper = var_InheritBool(VLCIntf, "video-wallpaper");
742     VLCVoutView *o_vout_view;
743     VLCVideoWindowCommon *o_new_video_window;
744
745     // TODO: make lion fullscreen compatible with video-wallpaper and !embedded-video
746     if ((b_video_wallpaper || !b_video_deco) && !b_nativeFullscreenMode) {
747         // b_video_wallpaper is priorized over !b_video_deco
748
749         msg_Dbg(VLCIntf, "Creating background / blank window");
750         NSScreen *screen = [NSScreen screenWithDisplayID:(CGDirectDisplayID)var_InheritInteger(VLCIntf, "macosx-vdev")];
751         if (!screen)
752             screen = [self screen];
753
754         NSRect window_rect;
755         if (b_video_wallpaper)
756             window_rect = [screen frame];
757         else
758             window_rect = [self frame];
759
760         if (o_extra_video_window)
761             [o_extra_video_window release];
762
763         NSUInteger mask = NSBorderlessWindowMask;
764         if (!OSX_SNOW_LEOPARD && !b_video_deco)
765             mask |= NSResizableWindowMask;
766
767         BOOL b_no_video_deco_only = !b_video_wallpaper;
768         o_extra_video_window = [[VLCVideoWindowCommon alloc] initWithContentRect:window_rect styleMask:mask backing:NSBackingStoreBuffered defer:YES];
769         [o_extra_video_window setDelegate:self];
770
771         if (b_video_wallpaper)
772             [o_extra_video_window setLevel:CGWindowLevelForKey(kCGDesktopWindowLevelKey) + 1];
773
774         [o_extra_video_window setBackgroundColor: [NSColor blackColor]];
775         [o_extra_video_window setCanBecomeKeyWindow: !b_video_wallpaper];
776         [o_extra_video_window setCanBecomeMainWindow: !b_video_wallpaper];
777         [o_extra_video_window setAcceptsMouseMovedEvents: !b_video_wallpaper];
778         [o_extra_video_window setMovableByWindowBackground: !b_video_wallpaper];
779         [o_extra_video_window useOptimizedDrawing: YES];
780
781         o_vout_view = [[VLCVoutView alloc] initWithFrame:[[o_extra_video_window contentView] bounds]];
782         [o_vout_view setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
783         [[o_extra_video_window contentView] addSubview:o_vout_view positioned:NSWindowAbove relativeTo:nil];
784         [o_extra_video_window setVideoView:o_vout_view];
785
786         o_new_video_window = o_extra_video_window;
787
788         if (b_video_wallpaper)
789             [o_extra_video_window orderBack:nil];
790         else {
791             [o_extra_video_window center];
792             [o_extra_video_window setFrameAutosaveName:@"extra-videowindow"];
793             [o_extra_video_window setContentMinSize: NSMakeSize(f_min_video_height, f_min_video_height)];
794         }
795
796         b_nonembedded = YES;
797     } else {
798         if (var_InheritBool(VLCIntf, "embedded-video") || b_nativeFullscreenMode) {
799             o_vout_view = [o_video_view retain];
800             o_new_video_window = self;
801             b_nonembedded = NO;
802         } else {
803             if (!o_detached_video_window) {
804                 NSWindowController *o_controller = [[NSWindowController alloc] initWithWindowNibName:@"DetachedVideoWindow"];
805                 [o_controller loadWindow];
806                 o_detached_video_window = [(VLCDetachedVideoWindow *)[o_controller window] retain];
807                 [o_controller release];
808
809                 // event occurs before window is created, so call again
810                 [[VLCMain sharedInstance] playbackStatusUpdated];
811             }
812
813             [o_detached_video_window setDelegate: self];
814             if (b_dark_interface) {
815                 [o_detached_video_window setContentMinSize: NSMakeSize(363., f_min_video_height + [[[o_detached_video_window controlsBar] bottomBarView] frame].size.height + [o_titlebar_view frame].size.height)];
816             } else {
817                 [o_detached_video_window setContentMinSize: NSMakeSize(363., f_min_video_height + [[[o_detached_video_window controlsBar] bottomBarView] frame].size.height)];
818             }
819
820             [o_detached_video_window setLevel:NSNormalWindowLevel];
821             [o_detached_video_window useOptimizedDrawing: YES];
822
823             o_vout_view = [[o_detached_video_window videoView] retain];
824             o_new_video_window = o_detached_video_window;
825             b_nonembedded = YES;
826         }
827     }
828
829     if (!b_video_wallpaper) {
830         [o_new_video_window makeKeyAndOrderFront: self];
831
832         vout_thread_t *p_vout = getVout();
833         if (p_vout) {
834             if (var_GetBool(p_vout, "video-on-top"))
835                 [o_new_video_window setLevel: NSStatusWindowLevel];
836             else
837                 [o_new_video_window setLevel: NSNormalWindowLevel];
838             vlc_object_release(p_vout);
839         }
840     }
841
842     [o_new_video_window setAlphaValue: config_GetFloat(VLCIntf, "macosx-opaqueness")];
843
844     return [o_vout_view autorelease];
845 }
846
847 - (void)setVideoplayEnabled
848 {
849     BOOL b_videoPlayback = [[VLCMain sharedInstance] activeVideoPlayback];
850
851     if (b_videoPlayback) {
852         frameBeforePlayback = [self frame];
853
854         // look for 'start at fullscreen'
855         [[VLCMain sharedInstance] fullscreenChanged];
856     } else {
857         if (!b_nonembedded)
858             [[self animator] setFrame:frameBeforePlayback display:YES];
859
860         [self makeFirstResponder: nil];
861         if (o_detached_video_window)
862             [o_detached_video_window orderOut: nil];
863         if (o_extra_video_window)
864             [o_extra_video_window orderOut: nil];
865
866         if ([self level] != NSNormalWindowLevel)
867             [self setLevel: NSNormalWindowLevel];
868         if (o_detached_video_window && [o_detached_video_window level] != NSNormalWindowLevel)
869             [o_detached_video_window setLevel: NSNormalWindowLevel];
870
871         // restore alpha value to 1 for the case that macosx-opaqueness is set to < 1
872         [self setAlphaValue:1.0];
873     }
874
875     if (b_nativeFullscreenMode) {
876         if ([NSApp presentationOptions] & NSApplicationPresentationFullScreen)
877             [[o_controls_bar bottomBarView] setHidden: b_videoPlayback];
878         else
879             [[o_controls_bar bottomBarView] setHidden: NO];
880         if (b_videoPlayback && b_fullscreen)
881             [o_fspanel setActive: nil];
882         if (!b_videoPlayback)
883             [o_fspanel setNonActive: nil];
884     }
885
886     if (!b_videoPlayback && b_fullscreen) {
887         if (!b_nativeFullscreenMode)
888             [[VLCCoreInteraction sharedInstance] toggleFullscreen];
889     }
890 }
891
892 - (void)resizeWindow
893 {
894     if (b_fullscreen || (b_nativeFullscreenMode && [NSApp presentationOptions] & NSApplicationPresentationFullScreen))
895         return;
896
897     id o_videoWindow = [o_video_view window];
898     NSSize windowMinSize = [o_videoWindow minSize];
899     NSRect screenFrame = [[o_videoWindow screen] visibleFrame];
900
901     NSPoint topleftbase = NSMakePoint(0, [o_videoWindow frame].size.height);
902     NSPoint topleftscreen = [o_videoWindow convertBaseToScreen: topleftbase];
903
904     unsigned int i_width = nativeVideoSize.width;
905     unsigned int i_height = nativeVideoSize.height;
906     if (i_width < windowMinSize.width)
907         i_width = windowMinSize.width;
908     if (i_height < f_min_video_height)
909         i_height = f_min_video_height;
910
911     /* Calculate the window's new size */
912     NSRect new_frame;
913     new_frame.size.width = [o_videoWindow frame].size.width - [o_video_view frame].size.width + i_width;
914     new_frame.size.height = [o_videoWindow frame].size.height - [o_video_view frame].size.height + i_height;
915     new_frame.origin.x = topleftscreen.x;
916     new_frame.origin.y = topleftscreen.y - new_frame.size.height;
917
918     /* make sure the window doesn't exceed the screen size the window is on */
919     if (new_frame.size.width > screenFrame.size.width) {
920         new_frame.size.width = screenFrame.size.width;
921         new_frame.origin.x = screenFrame.origin.x;
922     }
923     if (new_frame.size.height > screenFrame.size.height) {
924         new_frame.size.height = screenFrame.size.height;
925         new_frame.origin.y = screenFrame.origin.y;
926     }
927     if (new_frame.origin.y < screenFrame.origin.y)
928         new_frame.origin.y = screenFrame.origin.y;
929
930     CGFloat right_screen_point = screenFrame.origin.x + screenFrame.size.width;
931     CGFloat right_window_point = new_frame.origin.x + new_frame.size.width;
932     if (right_window_point > right_screen_point)
933         new_frame.origin.x -= (right_window_point - right_screen_point);
934
935     [[o_videoWindow animator] setFrame:new_frame display:YES];
936 }
937
938 - (void)setNativeVideoSize:(NSSize)size
939 {
940     nativeVideoSize = size;
941
942     if (var_InheritBool(VLCIntf, "macosx-video-autoresize") && !b_fullscreen && !var_InheritBool(VLCIntf, "video-wallpaper"))
943         [self performSelectorOnMainThread:@selector(resizeWindow) withObject:nil waitUntilDone:NO];
944 }
945
946 //  Called automatically if window's acceptsMouseMovedEvents property is true
947 - (void)mouseMoved:(NSEvent *)theEvent
948 {
949     if (b_fullscreen)
950         [self recreateHideMouseTimer];
951
952     [super mouseMoved: theEvent];
953 }
954
955 - (void)recreateHideMouseTimer
956 {
957     if (t_hide_mouse_timer != nil) {
958         [t_hide_mouse_timer invalidate];
959         [t_hide_mouse_timer release];
960     }
961
962     t_hide_mouse_timer = [NSTimer scheduledTimerWithTimeInterval:2
963                                                           target:self
964                                                         selector:@selector(hideMouseCursor:)
965                                                         userInfo:nil
966                                                          repeats:NO];
967     [t_hide_mouse_timer retain];
968 }
969
970 //  NSTimer selectors require this function signature as per Apple's docs
971 - (void)hideMouseCursor:(NSTimer *)timer
972 {
973     [NSCursor setHiddenUntilMouseMoves: YES];
974 }
975
976 #pragma mark -
977 #pragma mark Fullscreen support
978 - (void)showFullscreenController
979 {
980      if (b_fullscreen && [[VLCMain sharedInstance] activeVideoPlayback])
981         [o_fspanel fadeIn];
982 }
983
984 - (BOOL)fullscreen
985 {
986     return b_fullscreen;
987 }
988
989 - (void)lockFullscreenAnimation
990 {
991     [o_animation_lock lock];
992 }
993
994 - (void)unlockFullscreenAnimation
995 {
996     [o_animation_lock unlock];
997 }
998
999 - (void)enterFullscreen
1000 {
1001     NSMutableDictionary *dict1, *dict2;
1002     NSScreen *screen;
1003     NSRect screen_rect;
1004     NSRect rect;
1005     BOOL blackout_other_displays = var_InheritBool(VLCIntf, "macosx-black");
1006     o_current_video_window = [o_video_view window];
1007
1008     screen = [NSScreen screenWithDisplayID:(CGDirectDisplayID)var_InheritInteger(VLCIntf, "macosx-vdev")];
1009     [self lockFullscreenAnimation];
1010
1011     if (!screen) {
1012         msg_Dbg(VLCIntf, "chosen screen isn't present, using current screen for fullscreen mode");
1013         screen = [o_current_video_window screen];
1014     }
1015     if (!screen) {
1016         msg_Dbg(VLCIntf, "Using deepest screen");
1017         screen = [NSScreen deepestScreen];
1018     }
1019
1020     screen_rect = [screen frame];
1021
1022     [o_controls_bar setFullscreenState:YES];
1023     if (o_detached_video_window)
1024         [[o_detached_video_window controlsBar] setFullscreenState:YES];
1025
1026     [self recreateHideMouseTimer];
1027
1028     if (blackout_other_displays)
1029         [screen blackoutOtherScreens];
1030
1031     /* Make sure we don't see the window flashes in float-on-top mode */
1032     i_originalLevel = [o_current_video_window level];
1033     [o_current_video_window setLevel:NSNormalWindowLevel];
1034
1035     /* Only create the o_fullscreen_window if we are not in the middle of the zooming animation */
1036     if (!o_fullscreen_window) {
1037         /* We can't change the styleMask of an already created NSWindow, so we create another window, and do eye catching stuff */
1038
1039         rect = [[o_video_view superview] convertRect: [o_video_view frame] toView: nil]; /* Convert to Window base coord */
1040         rect.origin.x += [o_current_video_window frame].origin.x;
1041         rect.origin.y += [o_current_video_window frame].origin.y;
1042         o_fullscreen_window = [[VLCWindow alloc] initWithContentRect:rect styleMask: NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:YES];
1043         [o_fullscreen_window setBackgroundColor: [NSColor blackColor]];
1044         [o_fullscreen_window setCanBecomeKeyWindow: YES];
1045         [o_fullscreen_window setCanBecomeMainWindow: YES];
1046
1047         if (![o_current_video_window isVisible] || [o_current_video_window alphaValue] == 0.0) {
1048             /* We don't animate if we are not visible, instead we
1049              * simply fade the display */
1050             CGDisplayFadeReservationToken token;
1051
1052             if (blackout_other_displays) {
1053                 CGAcquireDisplayFadeReservation(kCGMaxDisplayReservationInterval, &token);
1054                 CGDisplayFade(token, 0.5, kCGDisplayBlendNormal, kCGDisplayBlendSolidColor, 0, 0, 0, YES);
1055             }
1056
1057             if ([screen mainScreen])
1058                 [NSApp setPresentationOptions:(NSApplicationPresentationAutoHideDock | NSApplicationPresentationAutoHideMenuBar)];
1059
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
1064             [o_fullscreen_window makeKeyAndOrderFront:self];
1065             [o_fullscreen_window orderFront:self animate:YES];
1066
1067             [o_fullscreen_window setFrame:screen_rect display:YES animate:YES];
1068             [o_fullscreen_window setLevel:NSNormalWindowLevel];
1069
1070             if (blackout_other_displays) {
1071                 CGDisplayFade(token, 0.3, kCGDisplayBlendSolidColor, kCGDisplayBlendNormal, 0, 0, 0, NO);
1072                 CGReleaseDisplayFadeReservation(token);
1073             }
1074
1075             /* Will release the lock */
1076             [self hasBecomeFullscreen];
1077
1078             return;
1079         }
1080
1081         /* Make sure we don't see the o_video_view disappearing of the screen during this operation */
1082         NSDisableScreenUpdates();
1083         [[o_video_view superview] replaceSubview:o_video_view with:o_temp_view];
1084         [o_temp_view setFrame:[o_video_view frame]];
1085         [o_fullscreen_window setContentView:o_video_view];
1086         [o_fullscreen_window makeKeyAndOrderFront:self];
1087         NSEnableScreenUpdates();
1088     }
1089
1090     /* We are in fullscreen (and no animation is running) */
1091     if (b_fullscreen) {
1092         /* Make sure we are hidden */
1093         [o_current_video_window orderOut: self];
1094
1095         [self unlockFullscreenAnimation];
1096         return;
1097     }
1098
1099     if (o_fullscreen_anim1) {
1100         [o_fullscreen_anim1 stopAnimation];
1101         [o_fullscreen_anim1 release];
1102     }
1103     if (o_fullscreen_anim2) {
1104         [o_fullscreen_anim2 stopAnimation];
1105         [o_fullscreen_anim2 release];
1106     }
1107
1108     if ([screen mainScreen])
1109         [NSApp setPresentationOptions:(NSApplicationPresentationAutoHideDock | NSApplicationPresentationAutoHideMenuBar)];
1110
1111     dict1 = [[NSMutableDictionary alloc] initWithCapacity:2];
1112     dict2 = [[NSMutableDictionary alloc] initWithCapacity:3];
1113
1114     [dict1 setObject:o_current_video_window forKey:NSViewAnimationTargetKey];
1115     [dict1 setObject:NSViewAnimationFadeOutEffect forKey:NSViewAnimationEffectKey];
1116
1117     [dict2 setObject:o_fullscreen_window forKey:NSViewAnimationTargetKey];
1118     [dict2 setObject:[NSValue valueWithRect:[o_fullscreen_window frame]] forKey:NSViewAnimationStartFrameKey];
1119     [dict2 setObject:[NSValue valueWithRect:screen_rect] forKey:NSViewAnimationEndFrameKey];
1120
1121     /* Strategy with NSAnimation allocation:
1122      - Keep at most 2 animation at a time
1123      - leaveFullscreen/enterFullscreen are the only responsible for releasing and alloc-ing
1124      */
1125     o_fullscreen_anim1 = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObject:dict1]];
1126     o_fullscreen_anim2 = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObject:dict2]];
1127
1128     [dict1 release];
1129     [dict2 release];
1130
1131     [o_fullscreen_anim1 setAnimationBlockingMode: NSAnimationNonblocking];
1132     [o_fullscreen_anim1 setDuration: 0.3];
1133     [o_fullscreen_anim1 setFrameRate: 30];
1134     [o_fullscreen_anim2 setAnimationBlockingMode: NSAnimationNonblocking];
1135     [o_fullscreen_anim2 setDuration: 0.2];
1136     [o_fullscreen_anim2 setFrameRate: 30];
1137
1138     [o_fullscreen_anim2 setDelegate: self];
1139     [o_fullscreen_anim2 startWhenAnimation: o_fullscreen_anim1 reachesProgress: 1.0];
1140
1141     [o_fullscreen_anim1 startAnimation];
1142     /* fullscreenAnimation will be unlocked when animation ends */
1143 }
1144
1145 - (void)hasBecomeFullscreen
1146 {
1147     if ([[o_video_view subviews] count] > 0)
1148         [o_fullscreen_window makeFirstResponder: [[o_video_view subviews] objectAtIndex:0]];
1149
1150     [o_fullscreen_window makeKeyWindow];
1151     [o_fullscreen_window setAcceptsMouseMovedEvents: YES];
1152
1153     /* tell the fspanel to move itself to front next time it's triggered */
1154     [o_fspanel setVoutWasUpdated: (int)[[o_fullscreen_window screen] displayID]];
1155     [o_fspanel setActive: nil];
1156
1157     if ([o_current_video_window isVisible])
1158         [o_current_video_window orderOut: self];
1159
1160     b_fullscreen = YES;
1161     [self unlockFullscreenAnimation];
1162 }
1163
1164 - (void)leaveFullscreen
1165 {
1166     [self leaveFullscreenAndFadeOut: NO];
1167 }
1168
1169 - (void)leaveFullscreenAndFadeOut: (BOOL)fadeout
1170 {
1171     NSMutableDictionary *dict1, *dict2;
1172     NSRect frame;
1173     BOOL blackout_other_displays = var_InheritBool(VLCIntf, "macosx-black");
1174
1175     if (!o_current_video_window)
1176         return;
1177
1178     [self lockFullscreenAnimation];
1179
1180     [o_controls_bar setFullscreenState:NO];
1181     if (o_detached_video_window)
1182         [[o_detached_video_window controlsBar] setFullscreenState:NO];
1183
1184     /* We always try to do so */
1185     [NSScreen unblackoutScreens];
1186
1187     vout_thread_t *p_vout = getVout();
1188     if (p_vout) {
1189         if (var_GetBool(p_vout, "video-on-top"))
1190             [[o_video_view window] setLevel: NSStatusWindowLevel];
1191         else
1192             [[o_video_view window] setLevel: NSNormalWindowLevel];
1193         vlc_object_release(p_vout);
1194     }
1195     [[o_video_view window] makeKeyAndOrderFront: nil];
1196
1197     /* Don't do anything if o_fullscreen_window is already closed */
1198     if (!o_fullscreen_window) {
1199         [self unlockFullscreenAnimation];
1200         return;
1201     }
1202
1203     if (fadeout) {
1204         /* We don't animate if we are not visible, instead we
1205          * simply fade the display */
1206         CGDisplayFadeReservationToken token;
1207
1208         if (blackout_other_displays) {
1209             CGAcquireDisplayFadeReservation(kCGMaxDisplayReservationInterval, &token);
1210             CGDisplayFade(token, 0.3, kCGDisplayBlendNormal, kCGDisplayBlendSolidColor, 0, 0, 0, YES);
1211         }
1212
1213         [o_fspanel setNonActive: nil];
1214         [NSApp setPresentationOptions: NSApplicationPresentationDefault];
1215
1216         /* Will release the lock */
1217         [self hasEndedFullscreen];
1218
1219         /* Our window is hidden, and might be faded. We need to workaround that, so note it
1220          * here */
1221         b_window_is_invisible = YES;
1222
1223         if (blackout_other_displays) {
1224             CGDisplayFade(token, 0.5, kCGDisplayBlendSolidColor, kCGDisplayBlendNormal, 0, 0, 0, NO);
1225             CGReleaseDisplayFadeReservation(token);
1226         }
1227
1228         return;
1229     }
1230
1231     [o_current_video_window setAlphaValue: 0.0];
1232     [o_current_video_window orderFront: self];
1233     [[o_video_view window] orderFront: self];
1234
1235     [o_fspanel setNonActive: nil];
1236     [NSApp setPresentationOptions:(NSApplicationPresentationDefault)];
1237
1238     if (o_fullscreen_anim1) {
1239         [o_fullscreen_anim1 stopAnimation];
1240         [o_fullscreen_anim1 release];
1241     }
1242     if (o_fullscreen_anim2) {
1243         [o_fullscreen_anim2 stopAnimation];
1244         [o_fullscreen_anim2 release];
1245     }
1246
1247     frame = [[o_temp_view superview] convertRect: [o_temp_view frame] toView: nil]; /* Convert to Window base coord */
1248     frame.origin.x += [o_current_video_window frame].origin.x;
1249     frame.origin.y += [o_current_video_window frame].origin.y;
1250
1251     dict2 = [[NSMutableDictionary alloc] initWithCapacity:2];
1252     [dict2 setObject:o_current_video_window forKey:NSViewAnimationTargetKey];
1253     [dict2 setObject:NSViewAnimationFadeInEffect forKey:NSViewAnimationEffectKey];
1254
1255     o_fullscreen_anim2 = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects:dict2, nil]];
1256     [dict2 release];
1257
1258     [o_fullscreen_anim2 setAnimationBlockingMode: NSAnimationNonblocking];
1259     [o_fullscreen_anim2 setDuration: 0.3];
1260     [o_fullscreen_anim2 setFrameRate: 30];
1261
1262     [o_fullscreen_anim2 setDelegate: self];
1263
1264     dict1 = [[NSMutableDictionary alloc] initWithCapacity:3];
1265
1266     [dict1 setObject:o_fullscreen_window forKey:NSViewAnimationTargetKey];
1267     [dict1 setObject:[NSValue valueWithRect:[o_fullscreen_window frame]] forKey:NSViewAnimationStartFrameKey];
1268     [dict1 setObject:[NSValue valueWithRect:frame] forKey:NSViewAnimationEndFrameKey];
1269
1270     o_fullscreen_anim1 = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects:dict1, nil]];
1271     [dict1 release];
1272
1273     [o_fullscreen_anim1 setAnimationBlockingMode: NSAnimationNonblocking];
1274     [o_fullscreen_anim1 setDuration: 0.2];
1275     [o_fullscreen_anim1 setFrameRate: 30];
1276     [o_fullscreen_anim2 startWhenAnimation: o_fullscreen_anim1 reachesProgress: 1.0];
1277
1278     /* Make sure o_fullscreen_window is the frontmost window */
1279     [o_fullscreen_window orderFront: self];
1280
1281     [o_fullscreen_anim1 startAnimation];
1282     /* fullscreenAnimation will be unlocked when animation ends */
1283 }
1284
1285 - (void)hasEndedFullscreen
1286 {
1287     b_fullscreen = NO;
1288
1289     /* This function is private and should be only triggered at the end of the fullscreen change animation */
1290     /* Make sure we don't see the o_video_view disappearing of the screen during this operation */
1291     NSDisableScreenUpdates();
1292     [o_video_view retain];
1293     [o_video_view removeFromSuperviewWithoutNeedingDisplay];
1294     [[o_temp_view superview] replaceSubview:o_temp_view with:o_video_view];
1295     [o_video_view release];
1296     [o_video_view setFrame:[o_temp_view frame]];
1297     if ([[o_video_view subviews] count] > 0)
1298         [[o_video_view window] makeFirstResponder: [[o_video_view subviews] objectAtIndex:0]];
1299     if (!b_nonembedded)
1300             [super makeKeyAndOrderFront:self]; /* our version contains a workaround */
1301     else
1302         [[o_video_view window] makeKeyAndOrderFront: self];
1303     [o_fullscreen_window orderOut: self];
1304     NSEnableScreenUpdates();
1305
1306     [o_fullscreen_window release];
1307     o_fullscreen_window = nil;
1308     [[o_video_view window] setLevel:i_originalLevel];
1309     [[o_video_view window] setAlphaValue: config_GetFloat(VLCIntf, "macosx-opaqueness")];
1310
1311     // if we quit fullscreen because there is no video anymore, make sure non-embedded window is not visible
1312     if (![[VLCMain sharedInstance] activeVideoPlayback] && b_nonembedded)
1313         [o_current_video_window orderOut: self];
1314
1315     o_current_video_window = nil;
1316     [self unlockFullscreenAnimation];
1317 }
1318
1319 - (void)animationDidEnd:(NSAnimation*)animation
1320 {
1321     NSArray *viewAnimations;
1322     if (o_makekey_anim == animation) {
1323         [o_makekey_anim release];
1324         return;
1325     }
1326     if ([animation currentValue] < 1.0)
1327         return;
1328
1329     /* Fullscreen ended or started (we are a delegate only for leaveFullscreen's/enterFullscren's anim2) */
1330     viewAnimations = [o_fullscreen_anim2 viewAnimations];
1331     if ([viewAnimations count] >=1 &&
1332         [[[viewAnimations objectAtIndex: 0] objectForKey: NSViewAnimationEffectKey] isEqualToString:NSViewAnimationFadeInEffect]) {
1333         /* Fullscreen ended */
1334         [self hasEndedFullscreen];
1335     } else
1336         /* Fullscreen started */
1337         [self hasBecomeFullscreen];
1338 }
1339
1340 - (void)makeKeyAndOrderFront: (id)sender
1341 {
1342     /* Hack
1343      * when we exit fullscreen and fade out, we may endup in
1344      * having a window that is faded. We can't have it fade in unless we
1345      * animate again. */
1346
1347     if (!b_window_is_invisible) {
1348         /* Make sure we don't do it too much */
1349         [super makeKeyAndOrderFront: sender];
1350         return;
1351     }
1352
1353     [super setAlphaValue:0.0f];
1354     [super makeKeyAndOrderFront: sender];
1355
1356     NSMutableDictionary * dict = [[NSMutableDictionary alloc] initWithCapacity:2];
1357     [dict setObject:self forKey:NSViewAnimationTargetKey];
1358     [dict setObject:NSViewAnimationFadeInEffect forKey:NSViewAnimationEffectKey];
1359
1360     o_makekey_anim = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObject:dict]];
1361     [dict release];
1362
1363     [o_makekey_anim setAnimationBlockingMode: NSAnimationNonblocking];
1364     [o_makekey_anim setDuration: 0.1];
1365     [o_makekey_anim setFrameRate: 30];
1366     [o_makekey_anim setDelegate: self];
1367
1368     [o_makekey_anim startAnimation];
1369     b_window_is_invisible = NO;
1370
1371     /* fullscreenAnimation will be unlocked when animation ends */
1372 }
1373
1374 #pragma mark -
1375 #pragma mark Lion native fullscreen handling
1376 - (void)windowWillEnterFullScreen:(NSNotification *)notification
1377 {
1378     // workaround, see #6668
1379     [NSApp setPresentationOptions:(NSApplicationPresentationFullScreen | NSApplicationPresentationAutoHideDock | NSApplicationPresentationAutoHideMenuBar)];
1380
1381     var_SetBool(pl_Get(VLCIntf), "fullscreen", true);
1382
1383     vout_thread_t *p_vout = getVout();
1384     if (p_vout) {
1385         var_SetBool(p_vout, "fullscreen", true);
1386         vlc_object_release(p_vout);
1387     }
1388
1389     [o_video_view setFrame: [[self contentView] frame]];
1390     b_fullscreen = YES;
1391
1392     [self recreateHideMouseTimer];
1393     i_originalLevel = [self level];
1394     [self setLevel:NSNormalWindowLevel];
1395
1396     if (b_dark_interface) {
1397         [o_titlebar_view removeFromSuperviewWithoutNeedingDisplay];
1398
1399         NSRect winrect;
1400         CGFloat f_titleBarHeight = [o_titlebar_view frame].size.height;
1401         winrect = [self frame];
1402
1403         winrect.size.height = winrect.size.height - f_titleBarHeight;
1404         [self setFrame: winrect display:NO animate:NO];
1405         winrect = [o_split_view frame];
1406         winrect.size.height = winrect.size.height + f_titleBarHeight;
1407         [o_split_view setFrame: winrect];
1408     }
1409
1410     if ([[VLCMain sharedInstance] activeVideoPlayback])
1411         [[o_controls_bar bottomBarView] setHidden: YES];
1412
1413     [self setMovableByWindowBackground: NO];
1414 }
1415
1416 - (void)windowDidEnterFullScreen:(NSNotification *)notification
1417 {
1418     // Indeed, we somehow can have an "inactive" fullscreen (but a visible window!).
1419     // But this creates some problems when leaving fs over remote intfs, so activate app here.
1420     [NSApp activateIgnoringOtherApps:YES];
1421
1422     [o_fspanel setVoutWasUpdated: (int)[[self screen] displayID]];
1423     [o_fspanel setActive: nil];
1424 }
1425
1426 - (void)windowWillExitFullScreen:(NSNotification *)notification
1427 {
1428
1429     var_SetBool(pl_Get(VLCIntf), "fullscreen", false);
1430
1431     vout_thread_t *p_vout = getVout();
1432     if (p_vout) {
1433         var_SetBool(p_vout, "fullscreen", false);
1434         vlc_object_release(p_vout);
1435     }
1436
1437     [o_video_view setFrame: [o_split_view frame]];
1438     [NSCursor setHiddenUntilMouseMoves: NO];
1439     [o_fspanel setNonActive: nil];
1440     [self setLevel:i_originalLevel];
1441     b_fullscreen = NO;
1442
1443     if (b_dark_interface) {
1444         NSRect winrect;
1445         CGFloat f_titleBarHeight = [o_titlebar_view frame].size.height;
1446         winrect = [self frame];
1447
1448         [o_titlebar_view setFrame: NSMakeRect(0, winrect.size.height - f_titleBarHeight,
1449                                               winrect.size.width, f_titleBarHeight)];
1450         [[self contentView] addSubview: o_titlebar_view];
1451
1452         winrect.size.height = winrect.size.height + f_titleBarHeight;
1453         [self setFrame: winrect display:NO animate:NO];
1454         winrect = [o_split_view frame];
1455         winrect.size.height = winrect.size.height - f_titleBarHeight;
1456         [o_split_view setFrame: winrect];
1457         [o_video_view setFrame: winrect];
1458     }
1459
1460     if ([[VLCMain sharedInstance] activeVideoPlayback])
1461         [[o_controls_bar bottomBarView] setHidden: NO];
1462
1463     [self setMovableByWindowBackground: YES];
1464 }
1465
1466 #pragma mark -
1467 #pragma mark split view delegate
1468 - (CGFloat)splitView:(NSSplitView *)splitView constrainMaxCoordinate:(CGFloat)proposedMax ofSubviewAt:(NSInteger)dividerIndex
1469 {
1470     if (dividerIndex == 0)
1471         return 300.;
1472     else
1473         return proposedMax;
1474 }
1475
1476 - (CGFloat)splitView:(NSSplitView *)splitView constrainMinCoordinate:(CGFloat)proposedMin ofSubviewAt:(NSInteger)dividerIndex
1477 {
1478     if (dividerIndex == 0)
1479         return 100.;
1480     else
1481         return proposedMin;
1482 }
1483
1484 - (BOOL)splitView:(NSSplitView *)splitView canCollapseSubview:(NSView *)subview
1485 {
1486     return ([subview isEqual:o_left_split_view]);
1487 }
1488
1489 - (BOOL)splitView:(NSSplitView *)splitView shouldAdjustSizeOfSubview:(NSView *)subview
1490 {
1491     if ([subview isEqual:o_left_split_view])
1492         return NO;
1493     return YES;
1494 }
1495
1496 #pragma mark -
1497 #pragma mark Side Bar Data handling
1498 /* taken under BSD-new from the PXSourceList sample project, adapted for VLC */
1499 - (NSUInteger)sourceList:(PXSourceList*)sourceList numberOfChildrenOfItem:(id)item
1500 {
1501     //Works the same way as the NSOutlineView data source: `nil` means a parent item
1502     if (item==nil)
1503         return [o_sidebaritems count];
1504     else
1505         return [[item children] count];
1506 }
1507
1508
1509 - (id)sourceList:(PXSourceList*)aSourceList child:(NSUInteger)index ofItem:(id)item
1510 {
1511     //Works the same way as the NSOutlineView data source: `nil` means a parent item
1512     if (item==nil)
1513         return [o_sidebaritems objectAtIndex:index];
1514     else
1515         return [[item children] objectAtIndex:index];
1516 }
1517
1518
1519 - (id)sourceList:(PXSourceList*)aSourceList objectValueForItem:(id)item
1520 {
1521     return [item title];
1522 }
1523
1524 - (void)sourceList:(PXSourceList*)aSourceList setObjectValue:(id)object forItem:(id)item
1525 {
1526     [item setTitle:object];
1527 }
1528
1529 - (BOOL)sourceList:(PXSourceList*)aSourceList isItemExpandable:(id)item
1530 {
1531     return [item hasChildren];
1532 }
1533
1534
1535 - (BOOL)sourceList:(PXSourceList*)aSourceList itemHasBadge:(id)item
1536 {
1537     if ([[item identifier] isEqualToString: @"playlist"] || [[item identifier] isEqualToString: @"medialibrary"])
1538         return YES;
1539
1540     return [item hasBadge];
1541 }
1542
1543
1544 - (NSInteger)sourceList:(PXSourceList*)aSourceList badgeValueForItem:(id)item
1545 {
1546     playlist_t * p_playlist = pl_Get(VLCIntf);
1547     NSInteger i_playlist_size;
1548
1549     if ([[item identifier] isEqualToString: @"playlist"]) {
1550         PL_LOCK;
1551         i_playlist_size = p_playlist->p_local_category->i_children;
1552         PL_UNLOCK;
1553
1554         return i_playlist_size;
1555     }
1556     if ([[item identifier] isEqualToString: @"medialibrary"]) {
1557         PL_LOCK;
1558         i_playlist_size = p_playlist->p_ml_category->i_children;
1559         PL_UNLOCK;
1560
1561         return i_playlist_size;
1562     }
1563
1564     return [item badgeValue];
1565 }
1566
1567
1568 - (BOOL)sourceList:(PXSourceList*)aSourceList itemHasIcon:(id)item
1569 {
1570     return [item hasIcon];
1571 }
1572
1573
1574 - (NSImage*)sourceList:(PXSourceList*)aSourceList iconForItem:(id)item
1575 {
1576     return [item icon];
1577 }
1578
1579 - (NSMenu*)sourceList:(PXSourceList*)aSourceList menuForEvent:(NSEvent*)theEvent item:(id)item
1580 {
1581     if ([theEvent type] == NSRightMouseDown || ([theEvent type] == NSLeftMouseDown && ([theEvent modifierFlags] & NSControlKeyMask) == NSControlKeyMask)) {
1582         if (item != nil) {
1583             NSMenu * m;
1584             if ([item sdtype] > 0)
1585             {
1586                 m = [[NSMenu alloc] init];
1587                 playlist_t * p_playlist = pl_Get(VLCIntf);
1588                 BOOL sd_loaded = playlist_IsServicesDiscoveryLoaded(p_playlist, [[item identifier] UTF8String]);
1589                 if (!sd_loaded)
1590                     [m addItemWithTitle:_NS("Enable") action:@selector(sdmenuhandler:) keyEquivalent:@""];
1591                 else
1592                     [m addItemWithTitle:_NS("Disable") action:@selector(sdmenuhandler:) keyEquivalent:@""];
1593                 [[m itemAtIndex:0] setRepresentedObject: [item identifier]];
1594             }
1595             return [m autorelease];
1596         }
1597     }
1598
1599     return nil;
1600 }
1601
1602 - (IBAction)sdmenuhandler:(id)sender
1603 {
1604     NSString * identifier = [sender representedObject];
1605     if ([identifier length] > 0 && ![identifier isEqualToString:@"lua{sd='freebox',longname='Freebox TV'}"]) {
1606         playlist_t * p_playlist = pl_Get(VLCIntf);
1607         BOOL sd_loaded = playlist_IsServicesDiscoveryLoaded(p_playlist, [identifier UTF8String]);
1608
1609         if (!sd_loaded)
1610             playlist_ServicesDiscoveryAdd(p_playlist, [identifier UTF8String]);
1611         else
1612             playlist_ServicesDiscoveryRemove(p_playlist, [identifier UTF8String]);
1613     }
1614 }
1615
1616 #pragma mark -
1617 #pragma mark Side Bar Delegate Methods
1618 /* taken under BSD-new from the PXSourceList sample project, adapted for VLC */
1619 - (BOOL)sourceList:(PXSourceList*)aSourceList isGroupAlwaysExpanded:(id)group
1620 {
1621     if ([[group identifier] isEqualToString:@"library"])
1622         return YES;
1623
1624     return NO;
1625 }
1626
1627 - (void)sourceListSelectionDidChange:(NSNotification *)notification
1628 {
1629     playlist_t * p_playlist = pl_Get(VLCIntf);
1630
1631     NSIndexSet *selectedIndexes = [o_sidebar_view selectedRowIndexes];
1632     id item = [o_sidebar_view itemAtRow:[selectedIndexes firstIndex]];
1633
1634
1635     //Set the label text to represent the new selection
1636     if ([item sdtype] > -1 && [[item identifier] length] > 0) {
1637         BOOL sd_loaded = playlist_IsServicesDiscoveryLoaded(p_playlist, [[item identifier] UTF8String]);
1638         if (!sd_loaded)
1639             playlist_ServicesDiscoveryAdd(p_playlist, [[item identifier] UTF8String]);
1640     }
1641
1642     [o_chosen_category_lbl setStringValue:[item title]];
1643
1644     if ([[item identifier] isEqualToString:@"playlist"]) {
1645         [[[VLCMain sharedInstance] playlist] setPlaylistRoot:p_playlist->p_local_category];
1646     } else if ([[item identifier] isEqualToString:@"medialibrary"]) {
1647         [[[VLCMain sharedInstance] playlist] setPlaylistRoot:p_playlist->p_ml_category];
1648     } else {
1649         playlist_item_t * pl_item;
1650         PL_LOCK;
1651         pl_item = playlist_ChildSearchName(p_playlist->p_root, [[item untranslatedTitle] UTF8String]);
1652         PL_UNLOCK;
1653         [[[VLCMain sharedInstance] playlist] setPlaylistRoot: pl_item];
1654     }
1655
1656     PL_LOCK;
1657     if ([[[VLCMain sharedInstance] playlist] currentPlaylistRoot] != p_playlist->p_local_category || p_playlist->p_local_category->i_children > 0)
1658         [self hideDropZone];
1659     else
1660         [self showDropZone];
1661     PL_UNLOCK;
1662
1663     if ([[item identifier] isEqualToString:@"podcast{longname=\"Podcasts\"}"])
1664         [self showPodcastControls];
1665     else
1666         [self hidePodcastControls];
1667 }
1668
1669 - (NSDragOperation)sourceList:(PXSourceList *)aSourceList validateDrop:(id <NSDraggingInfo>)info proposedItem:(id)item proposedChildIndex:(NSInteger)index
1670 {
1671     if ([[item identifier] isEqualToString:@"playlist"] || [[item identifier] isEqualToString:@"medialibrary"]) {
1672         NSPasteboard *o_pasteboard = [info draggingPasteboard];
1673         if ([[o_pasteboard types] containsObject: @"VLCPlaylistItemPboardType"] || [[o_pasteboard types] containsObject: NSFilenamesPboardType])
1674             return NSDragOperationGeneric;
1675     }
1676     return NSDragOperationNone;
1677 }
1678
1679 - (BOOL)sourceList:(PXSourceList *)aSourceList acceptDrop:(id <NSDraggingInfo>)info item:(id)item childIndex:(NSInteger)index
1680 {
1681     NSPasteboard *o_pasteboard = [info draggingPasteboard];
1682
1683     playlist_t * p_playlist = pl_Get(VLCIntf);
1684     playlist_item_t *p_node;
1685
1686     if ([[item identifier] isEqualToString:@"playlist"])
1687         p_node = p_playlist->p_local_category;
1688     else
1689         p_node = p_playlist->p_ml_category;
1690
1691     if ([[o_pasteboard types] containsObject: NSFilenamesPboardType]) {
1692         NSArray *o_values = [[o_pasteboard propertyListForType: NSFilenamesPboardType] sortedArrayUsingSelector: @selector(caseInsensitiveCompare:)];
1693         NSUInteger count = [o_values count];
1694         NSMutableArray *o_array = [NSMutableArray arrayWithCapacity:count];
1695
1696         for(NSUInteger i = 0; i < count; i++) {
1697             NSDictionary *o_dic;
1698             char *psz_uri = vlc_path2uri([[o_values objectAtIndex:i] UTF8String], NULL);
1699             if (!psz_uri)
1700                 continue;
1701
1702             o_dic = [NSDictionary dictionaryWithObject:[NSString stringWithCString:psz_uri encoding:NSUTF8StringEncoding] forKey:@"ITEM_URL"];
1703
1704             free(psz_uri);
1705
1706             [o_array addObject: o_dic];
1707         }
1708
1709         [[[VLCMain sharedInstance] playlist] appendNodeArray:o_array inNode: p_node atPos:-1 enqueue:YES];
1710         return YES;
1711     }
1712     else if ([[o_pasteboard types] containsObject: @"VLCPlaylistItemPboardType"]) {
1713         NSArray * array = [[[VLCMain sharedInstance] playlist] draggedItems];
1714
1715         NSUInteger count = [array count];
1716         playlist_item_t * p_item = NULL;
1717
1718         PL_LOCK;
1719         for(NSUInteger i = 0; i < count; i++) {
1720             p_item = [[array objectAtIndex:i] pointerValue];
1721             if (!p_item) continue;
1722             playlist_NodeAddCopy(p_playlist, p_item, p_node, PLAYLIST_END);
1723         }
1724         PL_UNLOCK;
1725
1726         return YES;
1727     }
1728     return NO;
1729 }
1730
1731 - (id)sourceList:(PXSourceList *)aSourceList persistentObjectForItem:(id)item
1732 {
1733     return [item identifier];
1734 }
1735
1736 - (id)sourceList:(PXSourceList *)aSourceList itemForPersistentObject:(id)object
1737 {
1738     /* the following code assumes for sakes of simplicity that only the top level
1739      * items are allowed to have children */
1740
1741     NSArray * array = [NSArray arrayWithArray: o_sidebaritems]; // read-only arrays are noticebly faster
1742     NSUInteger count = [array count];
1743     if (count < 1)
1744         return nil;
1745
1746     for (NSUInteger x = 0; x < count; x++) {
1747         id item = [array objectAtIndex: x]; // save one objc selector call
1748         if ([[item identifier] isEqualToString:object])
1749             return item;
1750     }
1751
1752     return nil;
1753 }
1754
1755 #pragma mark -
1756 #pragma mark Podcast
1757
1758 - (IBAction)addPodcast:(id)sender
1759 {
1760     [NSApp beginSheet:o_podcast_subscribe_window modalForWindow:self modalDelegate:self didEndSelector:NULL contextInfo:nil];
1761 }
1762
1763 - (IBAction)addPodcastWindowAction:(id)sender
1764 {
1765     [o_podcast_subscribe_window orderOut:sender];
1766     [NSApp endSheet: o_podcast_subscribe_window];
1767
1768     if (sender == o_podcast_subscribe_ok_btn && [[o_podcast_subscribe_url_fld stringValue] length] > 0) {
1769         NSMutableString * podcastConf = [[NSMutableString alloc] init];
1770         if (config_GetPsz(VLCIntf, "podcast-urls") != NULL)
1771             [podcastConf appendFormat:@"%s|", config_GetPsz(VLCIntf, "podcast-urls")];
1772
1773         [podcastConf appendString: [o_podcast_subscribe_url_fld stringValue]];
1774         config_PutPsz(VLCIntf, "podcast-urls", [podcastConf UTF8String]);
1775
1776         vlc_object_t *p_obj = (vlc_object_t*)vlc_object_find_name(VLCIntf->p_libvlc, "podcast");
1777         if (p_obj) {
1778             var_SetString(p_obj, "podcast-urls", [podcastConf UTF8String]);
1779             vlc_object_release(p_obj);
1780         }
1781         [podcastConf release];
1782     }
1783 }
1784
1785 - (IBAction)removePodcast:(id)sender
1786 {
1787     if (config_GetPsz(VLCIntf, "podcast-urls") != NULL) {
1788         [o_podcast_unsubscribe_pop removeAllItems];
1789         [o_podcast_unsubscribe_pop addItemsWithTitles:[[NSString stringWithUTF8String:config_GetPsz(VLCIntf, "podcast-urls")] componentsSeparatedByString:@"|"]];
1790         [NSApp beginSheet:o_podcast_unsubscribe_window modalForWindow:self modalDelegate:self didEndSelector:NULL contextInfo:nil];
1791     }
1792 }
1793
1794 - (IBAction)removePodcastWindowAction:(id)sender
1795 {
1796     [o_podcast_unsubscribe_window orderOut:sender];
1797     [NSApp endSheet: o_podcast_unsubscribe_window];
1798
1799     if (sender == o_podcast_unsubscribe_ok_btn) {
1800         NSMutableArray * urls = [[NSMutableArray alloc] initWithArray:[[NSString stringWithUTF8String:config_GetPsz(VLCIntf, "podcast-urls")] componentsSeparatedByString:@"|"]];
1801         [urls removeObjectAtIndex: [o_podcast_unsubscribe_pop indexOfSelectedItem]];
1802         config_PutPsz(VLCIntf, "podcast-urls", [[urls componentsJoinedByString:@"|"] UTF8String]);
1803         [urls release];
1804
1805         vlc_object_t *p_obj = (vlc_object_t*)vlc_object_find_name(VLCIntf->p_libvlc, "podcast");
1806         if (p_obj) {
1807             var_SetString(p_obj, "podcast-urls", config_GetPsz(VLCIntf, "podcast-urls"));
1808             vlc_object_release(p_obj);
1809         }
1810
1811         /* reload the podcast module, since it won't update its list when removing podcasts */
1812         playlist_t * p_playlist = pl_Get(VLCIntf);
1813         if (playlist_IsServicesDiscoveryLoaded(p_playlist, "podcast{longname=\"Podcasts\"}")) {
1814             playlist_ServicesDiscoveryRemove(p_playlist, "podcast{longname=\"Podcasts\"}");
1815             playlist_ServicesDiscoveryAdd(p_playlist, "podcast{longname=\"Podcasts\"}");
1816             [o_playlist_table reloadData];
1817         }
1818
1819     }
1820 }
1821
1822 - (void)showPodcastControls
1823 {
1824     NSRect podcastViewDimensions = [o_podcast_view frame];
1825     NSRect rightSplitRect = [o_right_split_view frame];
1826     NSRect playlistTableRect = [o_playlist_table frame];
1827
1828     podcastViewDimensions.size.width = rightSplitRect.size.width;
1829     podcastViewDimensions.origin.x = podcastViewDimensions.origin.y = .0;
1830     [o_podcast_view setFrame:podcastViewDimensions];
1831
1832     playlistTableRect.origin.y = playlistTableRect.origin.y + podcastViewDimensions.size.height;
1833     playlistTableRect.size.height = playlistTableRect.size.height - podcastViewDimensions.size.height;
1834     [o_playlist_table setFrame:playlistTableRect];
1835     [o_playlist_table setNeedsDisplay:YES];
1836
1837     [o_right_split_view addSubview: o_podcast_view positioned: NSWindowAbove relativeTo: o_right_split_view];
1838     b_podcastView_displayed = YES;
1839 }
1840
1841 - (void)hidePodcastControls
1842 {
1843     if (b_podcastView_displayed) {
1844         NSRect podcastViewDimensions = [o_podcast_view frame];
1845         NSRect playlistTableRect = [o_playlist_table frame];
1846
1847         playlistTableRect.origin.y = playlistTableRect.origin.y - podcastViewDimensions.size.height;
1848         playlistTableRect.size.height = playlistTableRect.size.height + podcastViewDimensions.size.height;
1849
1850         [o_podcast_view removeFromSuperviewWithoutNeedingDisplay];
1851         [o_playlist_table setFrame: playlistTableRect];
1852         b_podcastView_displayed = NO;
1853     }
1854 }
1855
1856 @end
1857
1858 @implementation VLCDetachedVideoWindow
1859
1860 - (void)awakeFromNib
1861 {
1862     [self setAcceptsMouseMovedEvents: YES];
1863
1864     [self setBackgroundColor: [NSColor blackColor]];
1865     if (b_dark_interface) {
1866         [self setOpaque: NO];
1867         [self display];
1868         [self setHasShadow:NO];
1869         [self setHasShadow:YES];
1870
1871         NSRect winrect = [self frame];
1872         CGFloat f_titleBarHeight = [o_titlebar_view frame].size.height;
1873
1874         [self setTitle: _NS("VLC media player")];
1875         [o_titlebar_view setFrame: NSMakeRect(0, winrect.size.height - f_titleBarHeight, winrect.size.width, f_titleBarHeight)];
1876         [[self contentView] addSubview: o_titlebar_view positioned: NSWindowAbove relativeTo: nil];
1877
1878         // native fs not supported with detached view yet
1879         [o_titlebar_view setFullscreenButtonHidden: YES];
1880     }
1881
1882     NSRect videoViewRect = [[self contentView] bounds];
1883     if (b_dark_interface)
1884         videoViewRect.size.height -= [o_titlebar_view frame].size.height;
1885     CGFloat f_bottomBarHeight = [[[self controlsBar] bottomBarView] frame].size.height;
1886     videoViewRect.size.height -= f_bottomBarHeight;
1887     videoViewRect.origin.y = f_bottomBarHeight;
1888     [o_video_view setFrame: videoViewRect];
1889 }
1890
1891 @end