]> git.sesse.net Git - vlc/blob - modules/gui/macosx/MainWindow.m
macosx: fix main window positioning on startup (close #9818)
[vlc] / modules / gui / macosx / MainWindow.m
1 /*****************************************************************************
2  * MainWindow.m: MacOS X interface module
3  *****************************************************************************
4  * Copyright (C) 2002-2013 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  *          David Fuhrmann <david dot fuhrmann at googlemail dot com>
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26  *****************************************************************************/
27
28 #import "CompatibilityFixes.h"
29 #import "MainWindow.h"
30 #import "intf.h"
31 #import "CoreInteraction.h"
32 #import "AudioEffects.h"
33 #import "MainMenu.h"
34 #import "open.h"
35 #import "controls.h" // TODO: remove me
36 #import "playlist.h"
37 #import "SideBarItem.h"
38 #import <math.h>
39 #import <vlc_playlist.h>
40 #import <vlc_url.h>
41 #import <vlc_strings.h>
42 #import <vlc_services_discovery.h>
43
44 #import "ControlsBar.h"
45 #import "VideoView.h"
46 #import "VLCVoutWindowController.h"
47
48
49 @interface VLCMainWindow (Internal)
50 - (void)resizePlaylistAfterCollapse;
51 - (void)makeSplitViewVisible;
52 - (void)makeSplitViewHidden;
53 - (void)showPodcastControls;
54 - (void)hidePodcastControls;
55 @end
56
57
58 @implementation VLCMainWindow
59
60 @synthesize nativeFullscreenMode=b_nativeFullscreenMode;
61 @synthesize nonembedded=b_nonembedded;
62 @synthesize fsPanel=o_fspanel;
63
64 static VLCMainWindow *_o_sharedInstance = nil;
65
66 + (VLCMainWindow *)sharedInstance
67 {
68     return _o_sharedInstance ? _o_sharedInstance : [[self alloc] init];
69 }
70
71 #pragma mark -
72 #pragma mark Initialization
73
74 - (id)init
75 {
76     if (_o_sharedInstance) {
77         [self dealloc];
78         return _o_sharedInstance;
79     } else
80         _o_sharedInstance = [super init];
81
82     return _o_sharedInstance;
83 }
84
85 - (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)styleMask
86                   backing:(NSBackingStoreType)backingType defer:(BOOL)flag
87 {
88     self = [super initWithContentRect:contentRect styleMask:styleMask
89                               backing:backingType defer:flag];
90     _o_sharedInstance = self;
91
92     [[VLCMain sharedInstance] updateTogglePlaylistState];
93
94     return self;
95 }
96
97 - (BOOL)isEvent:(NSEvent *)o_event forKey:(const char *)keyString
98 {
99     char *key;
100     NSString *o_key;
101
102     key = config_GetPsz(VLCIntf, keyString);
103     o_key = [NSString stringWithFormat:@"%s", key];
104     FREENULL(key);
105
106     unsigned int i_keyModifiers = [[VLCStringUtility sharedInstance] VLCModifiersToCocoa:o_key];
107
108     NSString * characters = [o_event charactersIgnoringModifiers];
109     if ([characters length] > 0) {
110         return [[characters lowercaseString] isEqualToString: [[VLCStringUtility sharedInstance] VLCKeyToString: o_key]] &&
111                 (i_keyModifiers & NSShiftKeyMask)     == ([o_event modifierFlags] & NSShiftKeyMask) &&
112                 (i_keyModifiers & NSControlKeyMask)   == ([o_event modifierFlags] & NSControlKeyMask) &&
113                 (i_keyModifiers & NSAlternateKeyMask) == ([o_event modifierFlags] & NSAlternateKeyMask) &&
114                 (i_keyModifiers & NSCommandKeyMask)   == ([o_event modifierFlags] & NSCommandKeyMask);
115     }
116     return NO;
117 }
118
119 - (BOOL)performKeyEquivalent:(NSEvent *)o_event
120 {
121     BOOL b_force = NO;
122     // these are key events which should be handled by vlc core, but are attached to a main menu item
123     if (![self isEvent: o_event forKey: "key-vol-up"] &&
124         ![self isEvent: o_event forKey: "key-vol-down"] &&
125         ![self isEvent: o_event forKey: "key-vol-mute"] &&
126         ![self isEvent: o_event forKey: "key-prev"] &&
127         ![self isEvent: o_event forKey: "key-next"] &&
128         ![self isEvent: o_event forKey: "key-jump+short"] &&
129         ![self isEvent: o_event forKey: "key-jump-short"]) {
130         /* We indeed want to prioritize some Cocoa key equivalent against libvlc,
131          so we perform the menu equivalent now. */
132         if ([[NSApp mainMenu] performKeyEquivalent:o_event])
133             return TRUE;
134     }
135     else
136         b_force = YES;
137
138     return [[VLCMain sharedInstance] hasDefinedShortcutKey:o_event force:b_force] ||
139            [(VLCControls *)[[VLCMain sharedInstance] controls] keyEvent:o_event];
140 }
141
142 - (void)dealloc
143 {
144     if (b_dark_interface)
145         [o_color_backdrop release];
146
147     [[NSNotificationCenter defaultCenter] removeObserver: self];
148     [o_sidebaritems release];
149     [o_fspanel release];
150
151     [super dealloc];
152 }
153
154 - (void)awakeFromNib
155 {
156     // sets lion fullscreen behaviour
157     [super awakeFromNib];
158
159     BOOL b_splitviewShouldBeHidden = NO;
160
161     if (!OSX_SNOW_LEOPARD)
162         [self setRestorable: NO];
163     [self setFrameAutosaveName:@"mainwindow"];
164
165     /* setup the styled interface */
166     b_nativeFullscreenMode = NO;
167 #ifdef MAC_OS_X_VERSION_10_7
168     if (!OSX_SNOW_LEOPARD)
169         b_nativeFullscreenMode = var_InheritBool(VLCIntf, "macosx-nativefullscreenmode");
170 #endif
171     [self useOptimizedDrawing: YES];
172
173     [[o_search_fld cell] setPlaceholderString: _NS("Search")];
174     [[o_search_fld cell] accessibilitySetOverrideValue:_NS("Enter a term to search the playlist. Results will be selected in the table.") forAttribute:NSAccessibilityDescriptionAttribute];
175
176     [o_dropzone_btn setTitle: _NS("Open media...")];
177     [[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];
178     [o_dropzone_lbl setStringValue: _NS("Drop media here")];
179
180     [o_podcast_add_btn setTitle: _NS("Subscribe")];
181     [o_podcast_remove_btn setTitle: _NS("Unsubscribe")];
182     [o_podcast_subscribe_title_lbl setStringValue: _NS("Subscribe to a podcast")];
183     [o_podcast_subscribe_subtitle_lbl setStringValue: _NS("Enter URL of the podcast to subscribe to:")];
184     [o_podcast_subscribe_cancel_btn setTitle: _NS("Cancel")];
185     [o_podcast_subscribe_ok_btn setTitle: _NS("Subscribe")];
186     [o_podcast_unsubscribe_title_lbl setStringValue: _NS("Unsubscribe from a podcast")];
187     [o_podcast_unsubscribe_subtitle_lbl setStringValue: _NS("Select the podcast you would like to unsubscribe from:")];
188     [o_podcast_unsubscribe_ok_btn setTitle: _NS("Unsubscribe")];
189     [o_podcast_unsubscribe_cancel_btn setTitle: _NS("Cancel")];
190
191     /* interface builder action */
192     CGFloat f_threshold_height = f_min_video_height + [o_controls_bar height];
193     if (b_dark_interface)
194         f_threshold_height += [o_titlebar_view frame].size.height;
195     if ([[self contentView] frame].size.height < f_threshold_height)
196         b_splitviewShouldBeHidden = YES;
197
198     [self setDelegate: self];
199     [self setExcludedFromWindowsMenu: YES];
200     [self setAcceptsMouseMovedEvents: YES];
201     // Set that here as IB seems to be buggy
202     if (b_dark_interface)
203         [self setContentMinSize:NSMakeSize(604., 288. + [o_titlebar_view frame].size.height)];
204     else
205         [self setContentMinSize:NSMakeSize(604., 288.)];
206
207     [self setTitle: _NS("VLC media player")];
208
209     b_dropzone_active = YES;
210     [o_dropzone_view setFrame: [o_playlist_table frame]];
211     [o_left_split_view setFrame: [o_sidebar_view frame]];
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 = NULL;
236     int *p_categories = NULL;
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_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 setDropItem:playlistItem dropChildIndex:NSOutlineViewDropOnItemIndex];
317     [o_sidebar_view registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType, @"VLCPlaylistItemPboardType", nil]];
318
319     [o_sidebar_view setAutosaveName:@"mainwindow-sidebar"];
320     [(PXSourceList *)o_sidebar_view setDataSource:self];
321     [o_sidebar_view setDelegate:self];
322     [o_sidebar_view setAutosaveExpandedItems:YES];
323
324     [o_sidebar_view expandItem: libraryItem expandChildren: YES];
325
326     o_fspanel = [[VLCFSPanel alloc] initWithContentRect:NSMakeRect(110.,267.,549.,87.)
327                                               styleMask:NSTexturedBackgroundWindowMask
328                                                 backing:NSBackingStoreBuffered
329                                                   defer:YES];
330
331     /* make sure we display the desired default appearance when VLC launches for the first time */
332     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
333     if (![defaults objectForKey:@"VLCFirstRun"]) {
334         [defaults setObject:[NSDate date] forKey:@"VLCFirstRun"];
335
336         NSUInteger i_sidebaritem_count = [o_sidebaritems count];
337         for (NSUInteger x = 0; x < i_sidebaritem_count; x++)
338             [o_sidebar_view expandItem: [o_sidebaritems objectAtIndex:x] expandChildren: YES];
339
340         [o_fspanel center];
341     }
342
343     // select playlist item by default
344     [o_sidebar_view selectRowIndexes:[NSIndexSet indexSetWithIndex:1] byExtendingSelection:NO];
345
346     if (b_dark_interface) {
347         [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(windowResizedOrMoved:) name: NSWindowDidResizeNotification object: nil];
348         [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(windowResizedOrMoved:) name: NSWindowDidMoveNotification object: nil];
349
350         [self setBackgroundColor: [NSColor clearColor]];
351         [self setOpaque: NO];
352         [self display];
353         [self setHasShadow:NO];
354         [self setHasShadow:YES];
355
356         NSRect winrect = [self frame];
357         CGFloat f_titleBarHeight = [o_titlebar_view frame].size.height;
358
359         [o_titlebar_view setFrame: NSMakeRect(0, winrect.size.height - f_titleBarHeight,
360                                               winrect.size.width, f_titleBarHeight)];
361         [[self contentView] addSubview: o_titlebar_view positioned: NSWindowAbove relativeTo: o_split_view];
362
363         if (winrect.size.height > 100) {
364             [self setFrame: winrect display:YES animate:YES];
365             previousSavedFrame = winrect;
366         }
367
368         winrect = [o_split_view frame];
369         winrect.size.height = winrect.size.height - f_titleBarHeight;
370         [o_split_view setFrame: winrect];
371         [o_video_view setFrame: winrect];
372
373         o_color_backdrop = [[VLCColorView alloc] initWithFrame: [o_split_view frame]];
374         [[self contentView] addSubview: o_color_backdrop positioned: NSWindowBelow relativeTo: o_split_view];
375         [o_color_backdrop setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
376     } else {
377         [o_video_view setFrame: [o_split_view frame]];
378         [o_playlist_table setBorderType: NSNoBorder];
379         [o_sidebar_scrollview setBorderType: NSNoBorder];
380     }
381
382     [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(someWindowWillClose:) name: NSWindowWillCloseNotification object: nil];
383     [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(someWindowWillMiniaturize:) name: NSWindowWillMiniaturizeNotification object:nil];
384     [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(applicationWillTerminate:) name: NSApplicationWillTerminateNotification object: nil];
385     [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(mainSplitViewDidResizeSubviews:) name: NSSplitViewDidResizeSubviewsNotification object:o_split_view];
386
387     if (b_splitviewShouldBeHidden) {
388         [self hideSplitView: YES];
389         f_lastSplitViewHeight = 300;
390     }
391
392     /* sanity check for the window size */
393     NSRect frame = [self frame];
394     NSSize screenSize = [[self screen] frame].size;
395     if (screenSize.width <= frame.size.width || screenSize.height <= frame.size.height) {
396         nativeVideoSize = screenSize;
397         [self resizeWindow];
398     }
399
400     /* update fs button to reflect state for next startup */
401     if (var_InheritBool(pl_Get(VLCIntf), "fullscreen"))
402         [o_controls_bar setFullscreenState:YES];
403
404     /* restore split view */
405     f_lastLeftSplitViewWidth = 200;
406     /* trick NSSplitView implementation, which pretends to know better than us */
407     if (!config_GetInt(VLCIntf, "macosx-show-sidebar"))
408         [self performSelector:@selector(toggleLeftSubSplitView) withObject:nil afterDelay:0.05];
409 }
410
411 #pragma mark -
412 #pragma mark appearance management
413
414 - (VLCMainWindowControlsBar *)controlsBar;
415 {
416     return (VLCMainWindowControlsBar *)o_controls_bar;
417 }
418
419 - (void)resizePlaylistAfterCollapse
420 {
421     // no animation here since we might be in the middle of another resize animation
422     NSRect plrect;
423     plrect = [o_playlist_table frame];
424     plrect.size.height = [o_split_view frame].size.height - 20.0; // actual pl top bar height, which differs from its frame
425     [o_playlist_table setFrame: plrect];
426     [o_playlist_table setNeedsDisplay: YES];
427
428     NSRect rightSplitRect;
429     rightSplitRect = [o_right_split_view frame];
430     plrect = [o_dropzone_box frame];
431     plrect.origin.x = (rightSplitRect.size.width - plrect.size.width) / 2;
432     plrect.origin.y = (rightSplitRect.size.height - plrect.size.height) / 2;
433     [o_dropzone_view setFrame: [o_playlist_table frame]];
434     [o_dropzone_box setFrame: plrect];
435     [o_dropzone_view setNeedsDisplay: YES];
436 }
437
438 - (void)makeSplitViewVisible
439 {
440     if (b_dark_interface)
441         [self setContentMinSize: NSMakeSize(604., 288. + [o_titlebar_view frame].size.height)];
442     else
443         [self setContentMinSize: NSMakeSize(604., 288.)];
444
445     NSRect old_frame = [self frame];
446     CGFloat newHeight = [self minSize].height;
447     if (old_frame.size.height < newHeight) {
448         NSRect new_frame = old_frame;
449         new_frame.origin.y = old_frame.origin.y + old_frame.size.height - newHeight;
450         new_frame.size.height = newHeight;
451
452         [[self animator] setFrame: new_frame display: YES animate: YES];
453     }
454
455     [o_video_view setHidden: YES];
456     [o_split_view setHidden: NO];
457     if ([self fullscreen]) {
458         [[o_controls_bar bottomBarView] setHidden: NO];
459         [o_fspanel setNonActive:nil];
460     }
461
462     [self makeFirstResponder: o_playlist_table];
463 }
464
465 - (void)makeSplitViewHidden
466 {
467     if (b_dark_interface)
468         [self setContentMinSize: NSMakeSize(604., f_min_video_height + [o_titlebar_view frame].size.height)];
469     else
470         [self setContentMinSize: NSMakeSize(604., f_min_video_height)];
471
472     [o_split_view setHidden: YES];
473     [o_video_view setHidden: NO];
474     if ([self fullscreen]) {
475         [[o_controls_bar bottomBarView] setHidden: YES];
476         [o_fspanel setActive:nil];
477     }
478
479     if ([[o_video_view subviews] count] > 0)
480         [self makeFirstResponder: [[o_video_view subviews] objectAtIndex:0]];
481 }
482
483
484 - (void)changePlaylistState:(VLCPlaylistStateEvent)event
485 {
486     // Beware, this code is really ugly
487
488     msg_Dbg(VLCIntf, "toggle playlist from state: removed splitview %i, minimized view %i. Event %i", b_splitview_removed, b_minimized_view, event);
489     if (![self isVisible] && event == psUserMenuEvent) {
490         [self makeKeyAndOrderFront: nil];
491         return;
492     }
493
494     BOOL b_activeVideo = [[VLCMain sharedInstance] activeVideoPlayback];
495     BOOL b_restored = NO;
496
497     // ignore alt if triggered through main menu shortcut
498     BOOL b_have_alt_key = ([[NSApp currentEvent] modifierFlags] & NSAlternateKeyMask) != 0;
499     if (event == psUserMenuEvent)
500         b_have_alt_key = NO;
501
502     // eUserMenuEvent is now handled same as eUserEvent
503     if(event == psUserMenuEvent)
504         event = psUserEvent;
505
506     if (b_dropzone_active && b_have_alt_key) {
507         [self hideDropZone];
508         return;
509     }
510
511     if (!(b_nativeFullscreenMode && b_fullscreen) && !b_splitview_removed && ((b_have_alt_key && b_activeVideo)
512                                                                               || (b_nonembedded && event == psUserEvent)
513                                                                               || (!b_activeVideo && event == psUserEvent)
514                                                                               || (b_minimized_view && event == psVideoStartedOrStoppedEvent))) {
515         // for starting playback, window is resized through resized events
516         // for stopping playback, resize through reset to previous frame
517         [self hideSplitView: event != psVideoStartedOrStoppedEvent];
518         b_minimized_view = NO;
519     } else {
520         if (b_splitview_removed) {
521             if (!b_nonembedded || (event == psUserEvent && b_nonembedded))
522                 [self showSplitView: event != psVideoStartedOrStoppedEvent];
523
524             if (event != psUserEvent)
525                 b_minimized_view = YES;
526             else
527                 b_minimized_view = NO;
528
529             if (b_activeVideo)
530                 b_restored = YES;
531         }
532
533         if (!b_nonembedded) {
534             if (([o_video_view isHidden] && b_activeVideo) || b_restored || (b_activeVideo && event != psUserEvent))
535                 [self makeSplitViewHidden];
536             else
537                 [self makeSplitViewVisible];
538         } else {
539             [o_split_view setHidden: NO];
540             [o_playlist_table setHidden: NO];
541             [o_video_view setHidden: YES];
542         }
543     }
544
545     msg_Dbg(VLCIntf, "toggle playlist to state: removed splitview %i, minimized view %i", b_splitview_removed, b_minimized_view);
546 }
547
548 - (IBAction)dropzoneButtonAction:(id)sender
549 {
550     [[[VLCMain sharedInstance] open] openFileGeneric];
551 }
552
553 #pragma mark -
554 #pragma mark overwritten default functionality
555
556 - (void)windowResizedOrMoved:(NSNotification *)notification
557 {
558     [self saveFrameUsingName: [self frameAutosaveName]];
559 }
560
561 - (void)applicationWillTerminate:(NSNotification *)notification
562 {
563     config_PutInt(VLCIntf, "macosx-show-sidebar", ![o_split_view isSubviewCollapsed:o_left_split_view]);
564
565     [self saveFrameUsingName: [self frameAutosaveName]];
566 }
567
568
569 - (void)someWindowWillClose:(NSNotification *)notification
570 {
571     id obj = [notification object];
572
573     // hasActiveVideo is defined for VLCVideoWindowCommon and subclasses
574     if ([obj respondsToSelector:@selector(hasActiveVideo)] && [obj hasActiveVideo]) {
575         if ([[VLCMain sharedInstance] activeVideoPlayback])
576             [[VLCCoreInteraction sharedInstance] stop];
577     }
578 }
579
580 - (void)someWindowWillMiniaturize:(NSNotification *)notification
581 {
582     if (config_GetInt(VLCIntf, "macosx-pause-minimized")) {
583         id obj = [notification object];
584
585         if ([obj class] == [VLCVideoWindowCommon class] || [obj class] == [VLCDetachedVideoWindow class] || ([obj class] == [VLCMainWindow class] && !b_nonembedded)) {
586             if ([[VLCMain sharedInstance] activeVideoPlayback])
587                 [[VLCCoreInteraction sharedInstance] pause];
588         }
589     }
590 }
591
592 #pragma mark -
593 #pragma mark Update interface and respond to foreign events
594 - (void)showDropZone
595 {
596     b_dropzone_active = YES;
597     [o_right_split_view addSubview: o_dropzone_view positioned:NSWindowAbove relativeTo:o_playlist_table];
598     [o_dropzone_view setFrame: [o_playlist_table frame]];
599     [o_playlist_table setHidden:YES];
600 }
601
602 - (void)hideDropZone
603 {
604     b_dropzone_active = NO;
605     [o_dropzone_view removeFromSuperview];
606     [o_playlist_table setHidden: NO];
607 }
608
609 - (void)hideSplitView:(BOOL)b_with_resize
610 {
611     if (b_with_resize) {
612         NSRect winrect = [self frame];
613         f_lastSplitViewHeight = [o_split_view frame].size.height;
614         winrect.size.height = winrect.size.height - f_lastSplitViewHeight;
615         winrect.origin.y = winrect.origin.y + f_lastSplitViewHeight;
616         [self setFrame: winrect display: YES animate: YES];
617     }
618
619     [self performSelector:@selector(hideDropZone) withObject:nil afterDelay:0.1];
620     if (b_dark_interface) {
621         [self setContentMinSize: NSMakeSize(604., [o_controls_bar height] + [o_titlebar_view frame].size.height)];
622         [self setContentMaxSize: NSMakeSize(FLT_MAX, [o_controls_bar height] + [o_titlebar_view frame].size.height)];
623     } else {
624         [self setContentMinSize: NSMakeSize(604., [o_controls_bar height])];
625         [self setContentMaxSize: NSMakeSize(FLT_MAX, [o_controls_bar height])];
626     }
627
628     b_splitview_removed = YES;
629 }
630
631 - (void)showSplitView:(BOOL)b_with_resize
632 {
633     [self updateWindow];
634     if (b_dark_interface)
635         [self setContentMinSize:NSMakeSize(604., 288. + [o_titlebar_view frame].size.height)];
636     else
637         [self setContentMinSize:NSMakeSize(604., 288.)];
638     [self setContentMaxSize: NSMakeSize(FLT_MAX, FLT_MAX)];
639
640     if (b_with_resize) {
641         NSRect winrect;
642         winrect = [self frame];
643         winrect.size.height = winrect.size.height + f_lastSplitViewHeight;
644         winrect.origin.y = winrect.origin.y - f_lastSplitViewHeight;
645         [self setFrame: winrect display: YES animate: YES];
646     }
647
648     [self performSelector:@selector(resizePlaylistAfterCollapse) withObject: nil afterDelay:0.75];
649
650     b_splitview_removed = NO;
651 }
652
653 - (void)updateTimeSlider
654 {
655     [o_controls_bar updateTimeSlider];
656     [o_fspanel updatePositionAndTime];
657
658     [[[VLCMain sharedInstance] voutController] updateWindowsControlsBarWithSelector:@selector(updateTimeSlider)];
659 }
660
661 - (void)updateName
662 {
663     input_thread_t * p_input;
664     p_input = pl_CurrentInput(VLCIntf);
665     if (p_input) {
666         NSString *aString;
667
668         if (!config_GetPsz(VLCIntf, "video-title")) {
669             char *format = var_InheritString(VLCIntf, "input-title-format");
670             char *formated = str_format_meta(p_input, format);
671             free(format);
672             aString = [NSString stringWithUTF8String:formated];
673             free(formated);
674         } else
675             aString = [NSString stringWithUTF8String:config_GetPsz(VLCIntf, "video-title")];
676
677         char *uri = input_item_GetURI(input_GetItem(p_input));
678
679         NSURL * o_url = [NSURL URLWithString:[NSString stringWithUTF8String:uri]];
680         if ([o_url isFileURL]) {
681             [self setRepresentedURL: o_url];
682             [[[VLCMain sharedInstance] voutController] updateWindowsUsingBlock:^(VLCVideoWindowCommon *o_window) {
683                 [o_window setRepresentedURL:o_url];
684             }];
685         } else {
686             [self setRepresentedURL: nil];
687             [[[VLCMain sharedInstance] voutController] updateWindowsUsingBlock:^(VLCVideoWindowCommon *o_window) {
688                 [o_window setRepresentedURL:nil];
689             }];
690         }
691         free(uri);
692
693         if ([aString isEqualToString:@""]) {
694             if ([o_url isFileURL])
695                 aString = [[NSFileManager defaultManager] displayNameAtPath: [o_url path]];
696             else
697                 aString = [o_url absoluteString];
698         }
699
700         if ([aString length] > 0) {
701             [self setTitle: aString];
702             [[[VLCMain sharedInstance] voutController] updateWindowsUsingBlock:^(VLCVideoWindowCommon *o_window) {
703                 [o_window setTitle:aString];
704             }];
705
706             [o_fspanel setStreamTitle: aString];
707         } else {
708             [self setTitle: _NS("VLC media player")];
709             [self setRepresentedURL: nil];
710         }
711
712         vlc_object_release(p_input);
713     } else {
714         [self setTitle: _NS("VLC media player")];
715         [self setRepresentedURL: nil];
716     }
717 }
718
719 - (void)updateWindow
720 {
721     [o_controls_bar updateControls];
722     [[[VLCMain sharedInstance] voutController] updateWindowsControlsBarWithSelector:@selector(updateControls)];
723
724     bool b_seekable = false;
725
726     playlist_t * p_playlist = pl_Get(VLCIntf);
727     input_thread_t * p_input = playlist_CurrentInput(p_playlist);
728     if (p_input) {
729         /* seekable streams */
730         b_seekable = var_GetBool(p_input, "can-seek");
731
732         vlc_object_release(p_input);
733     }
734
735     [self updateTimeSlider];
736     if ([o_fspanel respondsToSelector:@selector(setSeekable:)])
737         [o_fspanel setSeekable: b_seekable];
738
739     PL_LOCK;
740     if ([[[VLCMain sharedInstance] playlist] currentPlaylistRoot] != p_playlist->p_local_category || p_playlist->p_local_category->i_children > 0)
741         [self hideDropZone];
742     else
743         [self showDropZone];
744     PL_UNLOCK;
745     [o_sidebar_view setNeedsDisplay:YES];
746
747     [self _updatePlaylistTitle];
748 }
749
750 - (void)setPause
751 {
752     [o_controls_bar setPause];
753     [o_fspanel setPause];
754
755     [[[VLCMain sharedInstance] voutController] updateWindowsControlsBarWithSelector:@selector(setPause)];
756 }
757
758 - (void)setPlay
759 {
760     [o_controls_bar setPlay];
761     [o_fspanel setPlay];
762
763     [[[VLCMain sharedInstance] voutController] updateWindowsControlsBarWithSelector:@selector(setPlay)];
764 }
765
766 - (void)updateVolumeSlider
767 {
768     [[self controlsBar] updateVolumeSlider];
769     [o_fspanel setVolumeLevel: [[VLCCoreInteraction sharedInstance] volume]];
770 }
771
772 #pragma mark -
773 #pragma mark Video Output handling
774
775 - (void)videoplayWillBeStarted
776 {
777     if (!b_fullscreen)
778         frameBeforePlayback = [self frame];
779 }
780
781 - (void)setVideoplayEnabled
782 {
783     BOOL b_videoPlayback = [[VLCMain sharedInstance] activeVideoPlayback];
784         
785     if (!b_videoPlayback) {
786         if (!b_nonembedded && (!b_nativeFullscreenMode || (b_nativeFullscreenMode && !b_fullscreen)) && frameBeforePlayback.size.width > 0 && frameBeforePlayback.size.height > 0) {
787
788             // only resize back to minimum view of this is still desired final state
789             CGFloat f_threshold_height = f_min_video_height + [o_controls_bar height];
790             if(frameBeforePlayback.size.height > f_threshold_height || b_minimized_view) {
791                 [[self animator] setFrame:frameBeforePlayback display:YES];
792             }
793         }
794
795         frameBeforePlayback = NSMakeRect(0, 0, 0, 0);
796
797         // update fs button to reflect state for next startup
798         if (var_InheritBool(VLCIntf, "fullscreen") || var_GetBool(pl_Get(VLCIntf), "fullscreen")) {
799             [o_controls_bar setFullscreenState:YES];
800         }
801
802         [self makeFirstResponder: o_playlist_table];
803         [[[VLCMain sharedInstance] voutController] updateWindowLevelForHelperWindows: NSNormalWindowLevel];
804
805         // restore alpha value to 1 for the case that macosx-opaqueness is set to < 1
806         [self setAlphaValue:1.0];
807     }
808
809     if (b_nativeFullscreenMode) {
810         if ([self hasActiveVideo] && [self fullscreen]) {
811             [[o_controls_bar bottomBarView] setHidden: b_videoPlayback];
812             [o_fspanel setActive: nil];
813         } else {
814             [[o_controls_bar bottomBarView] setHidden: NO];
815             [o_fspanel setNonActive: nil];
816         }
817     }
818 }
819
820 #pragma mark -
821 #pragma mark Lion native fullscreen handling
822 - (void)windowWillEnterFullScreen:(NSNotification *)notification
823 {
824     [super windowWillEnterFullScreen:notification];
825
826     // update split view frame after removing title bar
827     if (b_dark_interface) {
828         NSRect frame = [[self contentView] frame];
829         frame.origin.y += [o_controls_bar height];
830         frame.size.height -= [o_controls_bar height];
831         [o_split_view setFrame:frame];
832     }
833 }
834
835 - (void)windowWillExitFullScreen:(NSNotification *)notification
836 {
837     [super windowWillExitFullScreen: notification];
838
839     // update split view frame after readding title bar
840     if (b_dark_interface) {
841         NSRect frame = [o_split_view frame];
842         frame.size.height -= [o_titlebar_view frame].size.height;
843         [o_split_view setFrame:frame];
844     }
845 }
846 #pragma mark -
847 #pragma mark Fullscreen support
848
849 - (void)showFullscreenController
850 {
851     id currentWindow = [NSApp keyWindow];
852     if ([currentWindow respondsToSelector:@selector(hasActiveVideo)] && [currentWindow hasActiveVideo]) {
853         if ([currentWindow respondsToSelector:@selector(fullscreen)] && [currentWindow fullscreen] && ![[currentWindow videoView] isHidden]) {
854
855             if ([[VLCMain sharedInstance] activeVideoPlayback])
856                 [o_fspanel fadeIn];
857         }
858     }
859
860 }
861
862 #pragma mark -
863 #pragma mark split view delegate
864 - (CGFloat)splitView:(NSSplitView *)splitView constrainMaxCoordinate:(CGFloat)proposedMax ofSubviewAt:(NSInteger)dividerIndex
865 {
866     if (dividerIndex == 0)
867         return 300.;
868     else
869         return proposedMax;
870 }
871
872 - (CGFloat)splitView:(NSSplitView *)splitView constrainMinCoordinate:(CGFloat)proposedMin ofSubviewAt:(NSInteger)dividerIndex
873 {
874     if (dividerIndex == 0)
875         return 100.;
876     else
877         return proposedMin;
878 }
879
880 - (BOOL)splitView:(NSSplitView *)splitView canCollapseSubview:(NSView *)subview
881 {
882     return ([subview isEqual:o_left_split_view]);
883 }
884
885 - (BOOL)splitView:(NSSplitView *)splitView shouldAdjustSizeOfSubview:(NSView *)subview
886 {
887     if ([subview isEqual:o_left_split_view])
888         return NO;
889     return YES;
890 }
891
892 - (void)mainSplitViewDidResizeSubviews:(id)object
893 {
894     f_lastLeftSplitViewWidth = [o_left_split_view frame].size.width;
895     config_PutInt(VLCIntf, "macosx-show-sidebar", ![o_split_view isSubviewCollapsed:o_left_split_view]);
896     [[[VLCMain sharedInstance] mainMenu] updateSidebarMenuItem];
897 }
898
899 - (void)toggleLeftSubSplitView
900 {
901     [o_split_view adjustSubviews];
902     if ([o_split_view isSubviewCollapsed:o_left_split_view])
903         [o_split_view setPosition:f_lastLeftSplitViewWidth ofDividerAtIndex:0];
904     else
905         [o_split_view setPosition:[o_split_view minPossiblePositionOfDividerAtIndex:0] ofDividerAtIndex:0];
906     [[[VLCMain sharedInstance] mainMenu] updateSidebarMenuItem];
907 }
908
909 #pragma mark -
910 #pragma mark private playlist magic
911 - (void)_updatePlaylistTitle
912 {
913     playlist_t * p_playlist = pl_Get(VLCIntf);
914     PL_LOCK;
915     playlist_item_t * currentPlaylistRoot = [[[VLCMain sharedInstance] playlist] currentPlaylistRoot];
916     PL_UNLOCK;
917     if (currentPlaylistRoot == p_playlist->p_local_category || currentPlaylistRoot == p_playlist->p_ml_category) {
918         if (currentPlaylistRoot == p_playlist->p_local_category)
919             [o_chosen_category_lbl setStringValue: [_NS("Playlist") stringByAppendingString:[self _playbackDurationOfNode:p_playlist->p_local_category]]];
920         else
921             [o_chosen_category_lbl setStringValue: [_NS("Media Library") stringByAppendingString:[self _playbackDurationOfNode:p_playlist->p_ml_category]]];
922     }
923 }
924
925 - (NSString *)_playbackDurationOfNode:(playlist_item_t*)node
926 {
927     if (!node)
928         return @"";
929
930     playlist_t * p_playlist = pl_Get(VLCIntf);
931     PL_LOCK;
932     mtime_t mt_duration = playlist_GetNodeDuration( node );
933     PL_UNLOCK;
934
935     if (mt_duration < 1)
936         return @"";
937
938     mt_duration = mt_duration / 1000000;
939
940     NSDate *date = [NSDate dateWithTimeIntervalSince1970:mt_duration];
941     NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
942     [formatter setDateFormat:@"HH:mm:ss"];
943     [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
944
945     NSString *playbackDuration = [NSString stringWithFormat:@" — %@",[formatter stringFromDate:date]];
946     [formatter release];
947     return playbackDuration;
948 }
949
950 #pragma mark -
951 #pragma mark Side Bar Data handling
952 /* taken under BSD-new from the PXSourceList sample project, adapted for VLC */
953 - (NSUInteger)sourceList:(PXSourceList*)sourceList numberOfChildrenOfItem:(id)item
954 {
955     //Works the same way as the NSOutlineView data source: `nil` means a parent item
956     if (item==nil)
957         return [o_sidebaritems count];
958     else
959         return [[item children] count];
960 }
961
962
963 - (id)sourceList:(PXSourceList*)aSourceList child:(NSUInteger)index ofItem:(id)item
964 {
965     //Works the same way as the NSOutlineView data source: `nil` means a parent item
966     if (item==nil)
967         return [o_sidebaritems objectAtIndex:index];
968     else
969         return [[item children] objectAtIndex:index];
970 }
971
972
973 - (id)sourceList:(PXSourceList*)aSourceList objectValueForItem:(id)item
974 {
975     return [item title];
976 }
977
978 - (void)sourceList:(PXSourceList*)aSourceList setObjectValue:(id)object forItem:(id)item
979 {
980     [item setTitle:object];
981 }
982
983 - (BOOL)sourceList:(PXSourceList*)aSourceList isItemExpandable:(id)item
984 {
985     return [item hasChildren];
986 }
987
988
989 - (BOOL)sourceList:(PXSourceList*)aSourceList itemHasBadge:(id)item
990 {
991     if ([[item identifier] isEqualToString: @"playlist"] || [[item identifier] isEqualToString: @"medialibrary"])
992         return YES;
993
994     return [item hasBadge];
995 }
996
997
998 - (NSInteger)sourceList:(PXSourceList*)aSourceList badgeValueForItem:(id)item
999 {
1000     playlist_t * p_playlist = pl_Get(VLCIntf);
1001     NSInteger i_playlist_size = 0;
1002
1003     if ([[item identifier] isEqualToString: @"playlist"]) {
1004         PL_LOCK;
1005         i_playlist_size = p_playlist->p_local_category->i_children;
1006         PL_UNLOCK;
1007
1008         return i_playlist_size;
1009     }
1010     if ([[item identifier] isEqualToString: @"medialibrary"]) {
1011         PL_LOCK;
1012         if (p_playlist->p_ml_category)
1013             i_playlist_size = p_playlist->p_ml_category->i_children;
1014         PL_UNLOCK;
1015
1016         return i_playlist_size;
1017     }
1018
1019     return [item badgeValue];
1020 }
1021
1022
1023 - (BOOL)sourceList:(PXSourceList*)aSourceList itemHasIcon:(id)item
1024 {
1025     return [item hasIcon];
1026 }
1027
1028
1029 - (NSImage*)sourceList:(PXSourceList*)aSourceList iconForItem:(id)item
1030 {
1031     return [item icon];
1032 }
1033
1034 - (NSMenu*)sourceList:(PXSourceList*)aSourceList menuForEvent:(NSEvent*)theEvent item:(id)item
1035 {
1036     if ([theEvent type] == NSRightMouseDown || ([theEvent type] == NSLeftMouseDown && ([theEvent modifierFlags] & NSControlKeyMask) == NSControlKeyMask)) {
1037         if (item != nil) {
1038             if ([item sdtype] > 0)
1039             {
1040                 NSMenu *m = [[NSMenu alloc] init];
1041                 playlist_t * p_playlist = pl_Get(VLCIntf);
1042                 BOOL sd_loaded = playlist_IsServicesDiscoveryLoaded(p_playlist, [[item identifier] UTF8String]);
1043                 if (!sd_loaded)
1044                     [m addItemWithTitle:_NS("Enable") action:@selector(sdmenuhandler:) keyEquivalent:@""];
1045                 else
1046                     [m addItemWithTitle:_NS("Disable") action:@selector(sdmenuhandler:) keyEquivalent:@""];
1047                 [[m itemAtIndex:0] setRepresentedObject: [item identifier]];
1048
1049                 return [m autorelease];
1050             }
1051         }
1052     }
1053
1054     return nil;
1055 }
1056
1057 - (IBAction)sdmenuhandler:(id)sender
1058 {
1059     NSString * identifier = [sender representedObject];
1060     if ([identifier length] > 0 && ![identifier isEqualToString:@"lua{sd='freebox',longname='Freebox TV'}"]) {
1061         playlist_t * p_playlist = pl_Get(VLCIntf);
1062         BOOL sd_loaded = playlist_IsServicesDiscoveryLoaded(p_playlist, [identifier UTF8String]);
1063
1064         if (!sd_loaded)
1065             playlist_ServicesDiscoveryAdd(p_playlist, [identifier UTF8String]);
1066         else
1067             playlist_ServicesDiscoveryRemove(p_playlist, [identifier UTF8String]);
1068     }
1069 }
1070
1071 #pragma mark -
1072 #pragma mark Side Bar Delegate Methods
1073 /* taken under BSD-new from the PXSourceList sample project, adapted for VLC */
1074 - (BOOL)sourceList:(PXSourceList*)aSourceList isGroupAlwaysExpanded:(id)group
1075 {
1076     if ([[group identifier] isEqualToString:@"library"])
1077         return YES;
1078
1079     return NO;
1080 }
1081
1082 - (void)sourceListSelectionDidChange:(NSNotification *)notification
1083 {
1084     playlist_t * p_playlist = pl_Get(VLCIntf);
1085
1086     NSIndexSet *selectedIndexes = [o_sidebar_view selectedRowIndexes];
1087     id item = [o_sidebar_view itemAtRow:[selectedIndexes firstIndex]];
1088
1089     //Set the label text to represent the new selection
1090     if ([item sdtype] > -1 && [[item identifier] length] > 0) {
1091         BOOL sd_loaded = playlist_IsServicesDiscoveryLoaded(p_playlist, [[item identifier] UTF8String]);
1092         if (!sd_loaded)
1093             playlist_ServicesDiscoveryAdd(p_playlist, [[item identifier] UTF8String]);
1094     }
1095
1096     [o_chosen_category_lbl setStringValue:[item title]];
1097
1098     if ([[item identifier] isEqualToString:@"playlist"]) {
1099         [[[VLCMain sharedInstance] playlist] setPlaylistRoot:p_playlist->p_local_category];
1100         [o_chosen_category_lbl setStringValue: [[o_chosen_category_lbl stringValue] stringByAppendingString:[self _playbackDurationOfNode:p_playlist->p_local_category]]];
1101     } else if ([[item identifier] isEqualToString:@"medialibrary"]) {
1102         if (p_playlist->p_ml_category) {
1103             [[[VLCMain sharedInstance] playlist] setPlaylistRoot:p_playlist->p_ml_category];
1104             [o_chosen_category_lbl setStringValue: [[o_chosen_category_lbl stringValue] stringByAppendingString:[self _playbackDurationOfNode:p_playlist->p_ml_category]]];
1105         }
1106     } else {
1107         playlist_item_t * pl_item;
1108         PL_LOCK;
1109         pl_item = playlist_ChildSearchName(p_playlist->p_root, [[item untranslatedTitle] UTF8String]);
1110         PL_UNLOCK;
1111         [[[VLCMain sharedInstance] playlist] setPlaylistRoot: pl_item];
1112     }
1113
1114     PL_LOCK;
1115     if ([[[VLCMain sharedInstance] playlist] currentPlaylistRoot] != p_playlist->p_local_category || p_playlist->p_local_category->i_children > 0)
1116         [self hideDropZone];
1117     else
1118         [self showDropZone];
1119     PL_UNLOCK;
1120
1121     if ([[item identifier] isEqualToString:@"podcast{longname=\"Podcasts\"}"])
1122         [self showPodcastControls];
1123     else
1124         [self hidePodcastControls];
1125
1126     [[NSNotificationCenter defaultCenter] postNotificationName: @"VLCMediaKeySupportSettingChanged"
1127                                                         object: nil
1128                                                       userInfo: nil];
1129 }
1130
1131 - (NSDragOperation)sourceList:(PXSourceList *)aSourceList validateDrop:(id <NSDraggingInfo>)info proposedItem:(id)item proposedChildIndex:(NSInteger)index
1132 {
1133     if ([[item identifier] isEqualToString:@"playlist"] || [[item identifier] isEqualToString:@"medialibrary"]) {
1134         NSPasteboard *o_pasteboard = [info draggingPasteboard];
1135         if ([[o_pasteboard types] containsObject: @"VLCPlaylistItemPboardType"] || [[o_pasteboard types] containsObject: NSFilenamesPboardType])
1136             return NSDragOperationGeneric;
1137     }
1138     return NSDragOperationNone;
1139 }
1140
1141 - (BOOL)sourceList:(PXSourceList *)aSourceList acceptDrop:(id <NSDraggingInfo>)info item:(id)item childIndex:(NSInteger)index
1142 {
1143     NSPasteboard *o_pasteboard = [info draggingPasteboard];
1144
1145     playlist_t * p_playlist = pl_Get(VLCIntf);
1146     playlist_item_t *p_node;
1147
1148     if ([[item identifier] isEqualToString:@"playlist"])
1149         p_node = p_playlist->p_local_category;
1150     else
1151         p_node = p_playlist->p_ml_category;
1152
1153     if ([[o_pasteboard types] containsObject: NSFilenamesPboardType]) {
1154         NSArray *o_values = [[o_pasteboard propertyListForType: NSFilenamesPboardType] sortedArrayUsingSelector: @selector(caseInsensitiveCompare:)];
1155         NSUInteger count = [o_values count];
1156         NSMutableArray *o_array = [NSMutableArray arrayWithCapacity:count];
1157
1158         for(NSUInteger i = 0; i < count; i++) {
1159             NSDictionary *o_dic;
1160             char *psz_uri = vlc_path2uri([[o_values objectAtIndex:i] UTF8String], NULL);
1161             if (!psz_uri)
1162                 continue;
1163
1164             o_dic = [NSDictionary dictionaryWithObject:[NSString stringWithCString:psz_uri encoding:NSUTF8StringEncoding] forKey:@"ITEM_URL"];
1165
1166             free(psz_uri);
1167
1168             [o_array addObject: o_dic];
1169         }
1170
1171         [[[VLCMain sharedInstance] playlist] appendNodeArray:o_array inNode: p_node atPos:-1 enqueue:YES];
1172         return YES;
1173     }
1174     else if ([[o_pasteboard types] containsObject: @"VLCPlaylistItemPboardType"]) {
1175         NSArray * array = [[[VLCMain sharedInstance] playlist] draggedItems];
1176
1177         NSUInteger count = [array count];
1178         playlist_item_t * p_item = NULL;
1179
1180         PL_LOCK;
1181         for(NSUInteger i = 0; i < count; i++) {
1182             p_item = [[array objectAtIndex:i] pointerValue];
1183             if (!p_item) continue;
1184             playlist_NodeAddCopy(p_playlist, p_item, p_node, PLAYLIST_END);
1185         }
1186         PL_UNLOCK;
1187
1188         return YES;
1189     }
1190     return NO;
1191 }
1192
1193 - (id)sourceList:(PXSourceList *)aSourceList persistentObjectForItem:(id)item
1194 {
1195     return [item identifier];
1196 }
1197
1198 - (id)sourceList:(PXSourceList *)aSourceList itemForPersistentObject:(id)object
1199 {
1200     /* the following code assumes for sakes of simplicity that only the top level
1201      * items are allowed to have children */
1202
1203     NSArray * array = [NSArray arrayWithArray: o_sidebaritems]; // read-only arrays are noticebly faster
1204     NSUInteger count = [array count];
1205     if (count < 1)
1206         return nil;
1207
1208     for (NSUInteger x = 0; x < count; x++) {
1209         id item = [array objectAtIndex:x]; // save one objc selector call
1210         if ([[item identifier] isEqualToString:object])
1211             return item;
1212     }
1213
1214     return nil;
1215 }
1216
1217 #pragma mark -
1218 #pragma mark Podcast
1219
1220 - (IBAction)addPodcast:(id)sender
1221 {
1222     [NSApp beginSheet:o_podcast_subscribe_window modalForWindow:self modalDelegate:self didEndSelector:NULL contextInfo:nil];
1223 }
1224
1225 - (IBAction)addPodcastWindowAction:(id)sender
1226 {
1227     [o_podcast_subscribe_window orderOut:sender];
1228     [NSApp endSheet: o_podcast_subscribe_window];
1229
1230     if (sender == o_podcast_subscribe_ok_btn && [[o_podcast_subscribe_url_fld stringValue] length] > 0) {
1231         NSMutableString * podcastConf = [[NSMutableString alloc] init];
1232         if (config_GetPsz(VLCIntf, "podcast-urls") != NULL)
1233             [podcastConf appendFormat:@"%s|", config_GetPsz(VLCIntf, "podcast-urls")];
1234
1235         [podcastConf appendString: [o_podcast_subscribe_url_fld stringValue]];
1236         config_PutPsz(VLCIntf, "podcast-urls", [podcastConf UTF8String]);
1237         var_SetString(pl_Get(VLCIntf), "podcast-urls", [podcastConf UTF8String]);
1238         [podcastConf release];
1239     }
1240 }
1241
1242 - (IBAction)removePodcast:(id)sender
1243 {
1244     if (config_GetPsz(VLCIntf, "podcast-urls") != NULL) {
1245         [o_podcast_unsubscribe_pop removeAllItems];
1246         [o_podcast_unsubscribe_pop addItemsWithTitles:[[NSString stringWithUTF8String:config_GetPsz(VLCIntf, "podcast-urls")] componentsSeparatedByString:@"|"]];
1247         [NSApp beginSheet:o_podcast_unsubscribe_window modalForWindow:self modalDelegate:self didEndSelector:NULL contextInfo:nil];
1248     }
1249 }
1250
1251 - (IBAction)removePodcastWindowAction:(id)sender
1252 {
1253     [o_podcast_unsubscribe_window orderOut:sender];
1254     [NSApp endSheet: o_podcast_unsubscribe_window];
1255
1256     if (sender == o_podcast_unsubscribe_ok_btn) {
1257         NSMutableArray * urls = [[NSMutableArray alloc] initWithArray:[[NSString stringWithUTF8String:config_GetPsz(VLCIntf, "podcast-urls")] componentsSeparatedByString:@"|"]];
1258         [urls removeObjectAtIndex: [o_podcast_unsubscribe_pop indexOfSelectedItem]];
1259         config_PutPsz(VLCIntf, "podcast-urls", [[urls componentsJoinedByString:@"|"] UTF8String]);
1260         var_SetString(pl_Get(VLCIntf), "podcast-urls", config_GetPsz(VLCIntf, "podcast-urls"));
1261         [urls release];
1262
1263         /* reload the podcast module, since it won't update its list when removing podcasts */
1264         playlist_t * p_playlist = pl_Get(VLCIntf);
1265         if (playlist_IsServicesDiscoveryLoaded(p_playlist, "podcast{longname=\"Podcasts\"}")) {
1266             playlist_ServicesDiscoveryRemove(p_playlist, "podcast{longname=\"Podcasts\"}");
1267             playlist_ServicesDiscoveryAdd(p_playlist, "podcast{longname=\"Podcasts\"}");
1268             [[[VLCMain sharedInstance] playlist] playlistUpdated];
1269         }
1270     }
1271 }
1272
1273 - (void)showPodcastControls
1274 {
1275     NSRect podcastViewDimensions = [o_podcast_view frame];
1276     NSRect rightSplitRect = [o_right_split_view frame];
1277     NSRect playlistTableRect = [o_playlist_table frame];
1278
1279     podcastViewDimensions.size.width = rightSplitRect.size.width;
1280     podcastViewDimensions.origin.x = podcastViewDimensions.origin.y = .0;
1281     [o_podcast_view setFrame:podcastViewDimensions];
1282
1283     playlistTableRect.origin.y = playlistTableRect.origin.y + podcastViewDimensions.size.height;
1284     playlistTableRect.size.height = playlistTableRect.size.height - podcastViewDimensions.size.height;
1285     [o_playlist_table setFrame:playlistTableRect];
1286     [o_playlist_table setNeedsDisplay:YES];
1287
1288     [o_right_split_view addSubview: o_podcast_view positioned: NSWindowAbove relativeTo: o_right_split_view];
1289     b_podcastView_displayed = YES;
1290 }
1291
1292 - (void)hidePodcastControls
1293 {
1294     if (b_podcastView_displayed) {
1295         NSRect podcastViewDimensions = [o_podcast_view frame];
1296         NSRect playlistTableRect = [o_playlist_table frame];
1297
1298         playlistTableRect.origin.y = playlistTableRect.origin.y - podcastViewDimensions.size.height;
1299         playlistTableRect.size.height = playlistTableRect.size.height + podcastViewDimensions.size.height;
1300
1301         [o_podcast_view removeFromSuperviewWithoutNeedingDisplay];
1302         [o_playlist_table setFrame: playlistTableRect];
1303         b_podcastView_displayed = NO;
1304     }
1305 }
1306
1307 @end
1308
1309 @implementation VLCDetachedVideoWindow
1310
1311 - (void)awakeFromNib
1312 {
1313     // sets lion fullscreen behaviour
1314     [super awakeFromNib];
1315     [self setAcceptsMouseMovedEvents: YES];
1316
1317     if (b_dark_interface) {
1318         [self setBackgroundColor: [NSColor clearColor]];
1319
1320         [self setOpaque: NO];
1321         [self display];
1322         [self setHasShadow:NO];
1323         [self setHasShadow:YES];
1324
1325         NSRect winrect = [self frame];
1326         CGFloat f_titleBarHeight = [o_titlebar_view frame].size.height;
1327
1328         [self setTitle: _NS("VLC media player")];
1329         [o_titlebar_view setFrame: NSMakeRect(0, winrect.size.height - f_titleBarHeight, winrect.size.width, f_titleBarHeight)];
1330         [[self contentView] addSubview: o_titlebar_view positioned: NSWindowAbove relativeTo: nil];
1331
1332     } else {
1333         [self setBackgroundColor: [NSColor blackColor]];
1334     }
1335
1336     NSRect videoViewRect = [[self contentView] bounds];
1337     if (b_dark_interface)
1338         videoViewRect.size.height -= [o_titlebar_view frame].size.height;
1339     CGFloat f_bottomBarHeight = [[self controlsBar] height];
1340     videoViewRect.size.height -= f_bottomBarHeight;
1341     videoViewRect.origin.y = f_bottomBarHeight;
1342     [o_video_view setFrame: videoViewRect];
1343
1344     if (b_dark_interface) {
1345         o_color_backdrop = [[VLCColorView alloc] initWithFrame: [o_video_view frame]];
1346         [[self contentView] addSubview: o_color_backdrop positioned: NSWindowBelow relativeTo: o_video_view];
1347         [o_color_backdrop setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
1348
1349         [self setContentMinSize: NSMakeSize(363., f_min_video_height + [[self controlsBar] height] + [o_titlebar_view frame].size.height)];
1350     } else {
1351         [self setContentMinSize: NSMakeSize(363., f_min_video_height + [[self controlsBar] height])];
1352     }
1353 }
1354
1355 - (void)dealloc
1356 {
1357     if (b_dark_interface)
1358         [o_color_backdrop release];
1359
1360     [super dealloc];
1361 }
1362
1363 @end