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