]> git.sesse.net Git - vlc/blob - modules/gui/macosx/MainWindow.m
str_format_meta: take input thread pointer rather than playlist
[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     /* setup the styled interface */
162     b_nativeFullscreenMode = NO;
163 #ifdef MAC_OS_X_VERSION_10_7
164     if (!OSX_SNOW_LEOPARD)
165         b_nativeFullscreenMode = var_InheritBool(VLCIntf, "macosx-nativefullscreenmode");
166 #endif
167     [self useOptimizedDrawing: YES];
168
169     [[o_search_fld cell] setPlaceholderString: _NS("Search")];
170     [[o_search_fld cell] accessibilitySetOverrideValue:_NS("Enter a term to search the playlist. Results will be selected in the table.") forAttribute:NSAccessibilityDescriptionAttribute];
171
172     [o_dropzone_btn setTitle: _NS("Open media...")];
173     [[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];
174     [o_dropzone_lbl setStringValue: _NS("Drop media here")];
175
176     [o_podcast_add_btn setTitle: _NS("Subscribe")];
177     [o_podcast_remove_btn setTitle: _NS("Unsubscribe")];
178     [o_podcast_subscribe_title_lbl setStringValue: _NS("Subscribe to a podcast")];
179     [o_podcast_subscribe_subtitle_lbl setStringValue: _NS("Enter URL of the podcast to subscribe to:")];
180     [o_podcast_subscribe_cancel_btn setTitle: _NS("Cancel")];
181     [o_podcast_subscribe_ok_btn setTitle: _NS("Subscribe")];
182     [o_podcast_unsubscribe_title_lbl setStringValue: _NS("Unsubscribe from a podcast")];
183     [o_podcast_unsubscribe_subtitle_lbl setStringValue: _NS("Select the podcast you would like to unsubscribe from:")];
184     [o_podcast_unsubscribe_ok_btn setTitle: _NS("Unsubscribe")];
185     [o_podcast_unsubscribe_cancel_btn setTitle: _NS("Cancel")];
186
187     /* interface builder action */
188     float f_threshold_height = f_min_video_height + [o_controls_bar height];
189     if (b_dark_interface)
190         f_threshold_height += [o_titlebar_view frame].size.height;
191     if ([[self contentView] frame].size.height < f_threshold_height)
192         b_splitviewShouldBeHidden = YES;
193
194     [self setDelegate: self];
195     [self setExcludedFromWindowsMenu: YES];
196     [self setAcceptsMouseMovedEvents: YES];
197     // Set that here as IB seems to be buggy
198     if (b_dark_interface)
199         [self setContentMinSize:NSMakeSize(604., 288. + [o_titlebar_view frame].size.height)];
200     else
201         [self setContentMinSize:NSMakeSize(604., 288.)];
202
203     [self setTitle: _NS("VLC media player")];
204
205     b_dropzone_active = YES;
206     [o_dropzone_view setFrame: [o_playlist_table frame]];
207     [o_left_split_view setFrame: [o_sidebar_view frame]];
208
209     if (!OSX_SNOW_LEOPARD) {
210         /* the default small size of the search field is slightly different on Lion, let's work-around that */
211         NSRect frame;
212         frame = [o_search_fld frame];
213         frame.origin.y = frame.origin.y + 2.0;
214         frame.size.height = frame.size.height - 1.0;
215         [o_search_fld setFrame: frame];
216     }
217
218     /* create the sidebar */
219     o_sidebaritems = [[NSMutableArray alloc] init];
220     SideBarItem *libraryItem = [SideBarItem itemWithTitle:_NS("LIBRARY") identifier:@"library"];
221     SideBarItem *playlistItem = [SideBarItem itemWithTitle:_NS("Playlist") identifier:@"playlist"];
222     [playlistItem setIcon: [NSImage imageNamed:@"sidebar-playlist"]];
223     SideBarItem *medialibraryItem = [SideBarItem itemWithTitle:_NS("Media Library") identifier:@"medialibrary"];
224     [medialibraryItem setIcon: [NSImage imageNamed:@"sidebar-playlist"]];
225     SideBarItem *mycompItem = [SideBarItem itemWithTitle:_NS("MY COMPUTER") identifier:@"mycomputer"];
226     SideBarItem *devicesItem = [SideBarItem itemWithTitle:_NS("DEVICES") identifier:@"devices"];
227     SideBarItem *lanItem = [SideBarItem itemWithTitle:_NS("LOCAL NETWORK") identifier:@"localnetwork"];
228     SideBarItem *internetItem = [SideBarItem itemWithTitle:_NS("INTERNET") identifier:@"internet"];
229
230     /* SD subnodes, inspired by the Qt4 intf */
231     char **ppsz_longnames = NULL;
232     int *p_categories = NULL;
233     char **ppsz_names = vlc_sd_GetNames(pl_Get(VLCIntf), &ppsz_longnames, &p_categories);
234     if (!ppsz_names)
235         msg_Err(VLCIntf, "no sd item found"); //TODO
236     char **ppsz_name = ppsz_names, **ppsz_longname = ppsz_longnames;
237     int *p_category = p_categories;
238     NSMutableArray *internetItems = [[NSMutableArray alloc] init];
239     NSMutableArray *devicesItems = [[NSMutableArray alloc] init];
240     NSMutableArray *lanItems = [[NSMutableArray alloc] init];
241     NSMutableArray *mycompItems = [[NSMutableArray alloc] init];
242     NSString *o_identifier;
243     for (; ppsz_name && *ppsz_name; ppsz_name++, ppsz_longname++, p_category++) {
244         o_identifier = [NSString stringWithCString: *ppsz_name encoding: NSUTF8StringEncoding];
245         switch (*p_category) {
246             case SD_CAT_INTERNET:
247                     [internetItems addObject: [SideBarItem itemWithTitle: _NS(*ppsz_longname) identifier: o_identifier]];
248                     if (!strncmp(*ppsz_name, "podcast", 7))
249                         [[internetItems lastObject] setIcon: [NSImage imageNamed:@"sidebar-podcast"]];
250                     else
251                         [[internetItems lastObject] setIcon: [NSImage imageNamed:@"NSApplicationIcon"]];
252                     [[internetItems lastObject] setSdtype: SD_CAT_INTERNET];
253                     [[internetItems lastObject] setUntranslatedTitle: [NSString stringWithUTF8String:*ppsz_longname]];
254                 break;
255             case SD_CAT_DEVICES:
256                     [devicesItems addObject: [SideBarItem itemWithTitle: _NS(*ppsz_longname) identifier: o_identifier]];
257                     [[devicesItems lastObject] setIcon: [NSImage imageNamed:@"NSApplicationIcon"]];
258                     [[devicesItems lastObject] setSdtype: SD_CAT_DEVICES];
259                     [[devicesItems lastObject] setUntranslatedTitle: [NSString stringWithUTF8String:*ppsz_longname]];
260                 break;
261             case SD_CAT_LAN:
262                     [lanItems addObject: [SideBarItem itemWithTitle: _NS(*ppsz_longname) identifier: o_identifier]];
263                     [[lanItems lastObject] setIcon: [NSImage imageNamed:@"sidebar-local"]];
264                     [[lanItems lastObject] setSdtype: SD_CAT_LAN];
265                     [[lanItems lastObject] setUntranslatedTitle: [NSString stringWithUTF8String:*ppsz_longname]];
266                 break;
267             case SD_CAT_MYCOMPUTER:
268                     [mycompItems addObject: [SideBarItem itemWithTitle: _NS(*ppsz_longname) identifier: o_identifier]];
269                     if (!strncmp(*ppsz_name, "video_dir", 9))
270                         [[mycompItems lastObject] setIcon: [NSImage imageNamed:@"sidebar-movie"]];
271                     else if (!strncmp(*ppsz_name, "audio_dir", 9))
272                         [[mycompItems lastObject] setIcon: [NSImage imageNamed:@"sidebar-music"]];
273                     else if (!strncmp(*ppsz_name, "picture_dir", 11))
274                         [[mycompItems lastObject] setIcon: [NSImage imageNamed:@"sidebar-pictures"]];
275                     else
276                         [[mycompItems lastObject] setIcon: [NSImage imageNamed:@"NSApplicationIcon"]];
277                     [[mycompItems lastObject] setUntranslatedTitle: [NSString stringWithUTF8String:*ppsz_longname]];
278                     [[mycompItems lastObject] setSdtype: SD_CAT_MYCOMPUTER];
279                 break;
280             default:
281                 msg_Warn(VLCIntf, "unknown SD type found, skipping (%s)", *ppsz_name);
282                 break;
283         }
284
285         free(*ppsz_name);
286         free(*ppsz_longname);
287     }
288     [mycompItem setChildren: [NSArray arrayWithArray: mycompItems]];
289     [devicesItem setChildren: [NSArray arrayWithArray: devicesItems]];
290     [lanItem setChildren: [NSArray arrayWithArray: lanItems]];
291     [internetItem setChildren: [NSArray arrayWithArray: internetItems]];
292     [mycompItems release];
293     [devicesItems release];
294     [lanItems release];
295     [internetItems release];
296     free(ppsz_names);
297     free(ppsz_longnames);
298     free(p_categories);
299
300     [libraryItem setChildren: [NSArray arrayWithObjects:playlistItem, medialibraryItem, nil]];
301     [o_sidebaritems addObject: libraryItem];
302     if ([mycompItem hasChildren])
303         [o_sidebaritems addObject: mycompItem];
304     if ([devicesItem hasChildren])
305         [o_sidebaritems addObject: devicesItem];
306     if ([lanItem hasChildren])
307         [o_sidebaritems addObject: lanItem];
308     if ([internetItem hasChildren])
309         [o_sidebaritems addObject: internetItem];
310
311     [o_sidebar_view reloadData];
312     [o_sidebar_view setDropItem:playlistItem dropChildIndex:NSOutlineViewDropOnItemIndex];
313     [o_sidebar_view registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType, @"VLCPlaylistItemPboardType", nil]];
314
315     [o_sidebar_view setAutosaveName:@"mainwindow-sidebar"];
316     [(PXSourceList *)o_sidebar_view setDataSource:self];
317     [o_sidebar_view setDelegate:self];
318     [o_sidebar_view setAutosaveExpandedItems:YES];
319
320     [o_sidebar_view expandItem: libraryItem expandChildren: YES];
321
322     o_fspanel = [[VLCFSPanel alloc] initWithContentRect:NSMakeRect(110.,267.,549.,87.)
323                                               styleMask:NSTexturedBackgroundWindowMask
324                                                 backing:NSBackingStoreBuffered
325                                                   defer: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         [o_fspanel center];
337     }
338
339     // select playlist item by default
340     [o_sidebar_view selectRowIndexes:[NSIndexSet indexSetWithIndex:1] byExtendingSelection:NO];
341
342     if (b_dark_interface) {
343         [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(windowResizedOrMoved:) name: NSWindowDidResizeNotification object: nil];
344         [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(windowResizedOrMoved:) name: NSWindowDidMoveNotification object: nil];
345
346         [self setBackgroundColor: [NSColor clearColor]];
347         [self setOpaque: NO];
348         [self display];
349         [self setHasShadow:NO];
350         [self setHasShadow:YES];
351
352         NSRect winrect = [self frame];
353         CGFloat f_titleBarHeight = [o_titlebar_view frame].size.height;
354
355         [o_titlebar_view setFrame: NSMakeRect(0, winrect.size.height - f_titleBarHeight,
356                                               winrect.size.width, f_titleBarHeight)];
357         [[self contentView] addSubview: o_titlebar_view positioned: NSWindowAbove relativeTo: o_split_view];
358
359         if (winrect.size.height > 100) {
360             [self setFrame: winrect display:YES animate:YES];
361             previousSavedFrame = winrect;
362         }
363
364         winrect = [o_split_view frame];
365         winrect.size.height = winrect.size.height - f_titleBarHeight;
366         [o_split_view setFrame: winrect];
367         [o_video_view setFrame: winrect];
368
369         o_color_backdrop = [[VLCColorView alloc] initWithFrame: [o_split_view frame]];
370         [[self contentView] addSubview: o_color_backdrop positioned: NSWindowBelow relativeTo: o_split_view];
371         [o_color_backdrop setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
372     } else {
373         [o_video_view setFrame: [o_split_view frame]];
374         [o_playlist_table setBorderType: NSNoBorder];
375         [o_sidebar_scrollview setBorderType: NSNoBorder];
376     }
377
378     [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(someWindowWillClose:) name: NSWindowWillCloseNotification object: nil];
379     [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(someWindowWillMiniaturize:) name: NSWindowWillMiniaturizeNotification object:nil];
380     [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(applicationWillTerminate:) name: NSApplicationWillTerminateNotification object: nil];
381     [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(mainSplitViewDidResizeSubviews:) name: NSSplitViewDidResizeSubviewsNotification object:o_split_view];
382
383     if (b_splitviewShouldBeHidden) {
384         [self hideSplitView: YES];
385         i_lastSplitViewHeight = 300;
386     }
387
388     /* sanity check for the window size */
389     NSRect frame = [self frame];
390     NSSize screenSize = [[self screen] frame].size;
391     if (screenSize.width <= frame.size.width || screenSize.height <= frame.size.height) {
392         nativeVideoSize = screenSize;
393         [self resizeWindow];
394     }
395
396     /* update fs button to reflect state for next startup */
397     if (var_InheritBool(pl_Get(VLCIntf), "fullscreen"))
398         [o_controls_bar setFullscreenState:YES];
399
400     /* restore split view */
401     i_lastLeftSplitViewWidth = 200;
402     /* trick NSSplitView implementation, which pretends to know better than us */
403     if (!config_GetInt(VLCIntf, "macosx-show-sidebar"))
404         [self performSelector:@selector(toggleLeftSubSplitView) withObject:nil afterDelay:0.05];
405 }
406
407 #pragma mark -
408 #pragma mark appearance management
409
410 - (VLCMainWindowControlsBar *)controlsBar;
411 {
412     return (VLCMainWindowControlsBar *)o_controls_bar;
413 }
414
415 - (void)resizePlaylistAfterCollapse
416 {
417     // no animation here since we might be in the middle of another resize animation
418     NSRect plrect;
419     plrect = [o_playlist_table frame];
420     plrect.size.height = [o_split_view frame].size.height - 20.0; // actual pl top bar height, which differs from its frame
421     [o_playlist_table setFrame: plrect];
422     [o_playlist_table setNeedsDisplay: YES];
423
424     NSRect rightSplitRect;
425     rightSplitRect = [o_right_split_view frame];
426     plrect = [o_dropzone_box frame];
427     plrect.origin.x = (rightSplitRect.size.width - plrect.size.width) / 2;
428     plrect.origin.y = (rightSplitRect.size.height - plrect.size.height) / 2;
429     [o_dropzone_view setFrame: [o_playlist_table frame]];
430     [o_dropzone_box setFrame: plrect];
431     [o_dropzone_view setNeedsDisplay: YES];
432 }
433
434 - (void)makeSplitViewVisible
435 {
436     if (b_dark_interface)
437         [self setContentMinSize: NSMakeSize(604., 288. + [o_titlebar_view frame].size.height)];
438     else
439         [self setContentMinSize: NSMakeSize(604., 288.)];
440
441     NSRect old_frame = [self frame];
442     float newHeight = [self minSize].height;
443     if (old_frame.size.height < newHeight) {
444         NSRect new_frame = old_frame;
445         new_frame.origin.y = old_frame.origin.y + old_frame.size.height - newHeight;
446         new_frame.size.height = newHeight;
447
448         [[self animator] setFrame: new_frame display: YES animate: YES];
449     }
450
451     [o_video_view setHidden: YES];
452     [o_split_view setHidden: NO];
453     if ([self fullscreen]) {
454         [[o_controls_bar bottomBarView] setHidden: NO];
455         [o_fspanel setNonActive:nil];
456     }
457
458     [self makeFirstResponder: o_playlist_table];
459 }
460
461 - (void)makeSplitViewHidden
462 {
463     if (b_dark_interface)
464         [self setContentMinSize: NSMakeSize(604., f_min_video_height + [o_titlebar_view frame].size.height)];
465     else
466         [self setContentMinSize: NSMakeSize(604., f_min_video_height)];
467
468     [o_split_view setHidden: YES];
469     [o_video_view setHidden: NO];
470     if ([self fullscreen]) {
471         [[o_controls_bar bottomBarView] setHidden: YES];
472         [o_fspanel setActive:nil];
473     }
474
475     if ([[o_video_view subviews] count] > 0)
476         [self makeFirstResponder: [[o_video_view subviews] objectAtIndex:0]];
477 }
478
479
480 - (void)changePlaylistState:(VLCPlaylistStateEvent)event
481 {
482     // Beware, this code is really ugly
483
484     msg_Dbg(VLCIntf, "toggle playlist from state: removed splitview %i, minimized view %i. Event %i", b_splitview_removed, b_minimized_view, event);
485     if (![self isVisible] && event == psUserMenuEvent) {
486         [self makeKeyAndOrderFront: nil];
487         return;
488     }
489
490     BOOL b_activeVideo = [[VLCMain sharedInstance] activeVideoPlayback];
491     BOOL b_restored = NO;
492
493     // ignore alt if triggered through main menu shortcut
494     BOOL b_have_alt_key = ([[NSApp currentEvent] modifierFlags] & NSAlternateKeyMask) != 0;
495     if (event == psUserMenuEvent)
496         b_have_alt_key = NO;
497
498     // eUserMenuEvent is now handled same as eUserEvent
499     if(event == psUserMenuEvent)
500         event = psUserEvent;
501
502     if (b_dropzone_active && b_have_alt_key) {
503         [self hideDropZone];
504         return;
505     }
506
507     if (!(b_nativeFullscreenMode && b_fullscreen) && !b_splitview_removed && ((b_have_alt_key && b_activeVideo)
508                                                                               || (b_nonembedded && event == psUserEvent)
509                                                                               || (!b_activeVideo && event == psUserEvent)
510                                                                               || (b_minimized_view && event == psVideoStartedOrStoppedEvent))) {
511         // for starting playback, window is resized through resized events
512         // for stopping playback, resize through reset to previous frame
513         [self hideSplitView: event != psVideoStartedOrStoppedEvent];
514         b_minimized_view = NO;
515     } else {
516         if (b_splitview_removed) {
517             if (!b_nonembedded || (event == psUserEvent && b_nonembedded))
518                 [self showSplitView: event != psVideoStartedOrStoppedEvent];
519
520             if (event != psUserEvent)
521                 b_minimized_view = YES;
522             else
523                 b_minimized_view = NO;
524
525             if (b_activeVideo)
526                 b_restored = YES;
527         }
528
529         if (!b_nonembedded) {
530             if (([o_video_view isHidden] && b_activeVideo) || b_restored || (b_activeVideo && event != psUserEvent))
531                 [self makeSplitViewHidden];
532             else
533                 [self makeSplitViewVisible];
534         } else {
535             [o_split_view setHidden: NO];
536             [o_playlist_table setHidden: NO];
537             [o_video_view setHidden: YES];
538         }
539     }
540
541     msg_Dbg(VLCIntf, "toggle playlist to state: removed splitview %i, minimized view %i", b_splitview_removed, b_minimized_view);
542 }
543
544 - (IBAction)dropzoneButtonAction:(id)sender
545 {
546     [[[VLCMain sharedInstance] open] openFileGeneric];
547 }
548
549 #pragma mark -
550 #pragma mark overwritten default functionality
551
552 - (void)windowResizedOrMoved:(NSNotification *)notification
553 {
554     [self saveFrameUsingName: [self frameAutosaveName]];
555 }
556
557 - (void)applicationWillTerminate:(NSNotification *)notification
558 {
559     config_PutInt(VLCIntf, "macosx-show-sidebar", ![o_split_view isSubviewCollapsed:o_left_split_view]);
560
561     [self saveFrameUsingName: [self frameAutosaveName]];
562 }
563
564
565 - (void)someWindowWillClose:(NSNotification *)notification
566 {
567     id obj = [notification object];
568
569     // hasActiveVideo is defined for VLCVideoWindowCommon and subclasses
570     if ([obj respondsToSelector:@selector(hasActiveVideo)] && [obj hasActiveVideo]) {
571         if ([[VLCMain sharedInstance] activeVideoPlayback])
572             [[VLCCoreInteraction sharedInstance] stop];
573     }
574 }
575
576 - (void)someWindowWillMiniaturize:(NSNotification *)notification
577 {
578     if (config_GetInt(VLCIntf, "macosx-pause-minimized")) {
579         id obj = [notification object];
580
581         if ([obj class] == [VLCVideoWindowCommon class] || [obj class] == [VLCDetachedVideoWindow class] || ([obj class] == [VLCMainWindow class] && !b_nonembedded)) {
582             if ([[VLCMain sharedInstance] activeVideoPlayback])
583                 [[VLCCoreInteraction sharedInstance] pause];
584         }
585     }
586 }
587
588 #pragma mark -
589 #pragma mark Update interface and respond to foreign events
590 - (void)showDropZone
591 {
592     b_dropzone_active = YES;
593     [o_right_split_view addSubview: o_dropzone_view positioned:NSWindowAbove relativeTo:o_playlist_table];
594     [o_dropzone_view setFrame: [o_playlist_table frame]];
595     [[o_playlist_table animator] setHidden:YES];
596 }
597
598 - (void)hideDropZone
599 {
600     b_dropzone_active = NO;
601     [o_dropzone_view removeFromSuperview];
602     [[o_playlist_table animator] setHidden: NO];
603 }
604
605 - (void)hideSplitView:(BOOL)b_with_resize
606 {
607     if (b_with_resize) {
608         NSRect winrect = [self frame];
609         i_lastSplitViewHeight = [o_split_view frame].size.height;
610         winrect.size.height = winrect.size.height - i_lastSplitViewHeight;
611         winrect.origin.y = winrect.origin.y + i_lastSplitViewHeight;
612         [self setFrame: winrect display: YES animate: YES];
613     }
614
615     [self performSelector:@selector(hideDropZone) withObject:nil afterDelay:0.1];
616     if (b_dark_interface) {
617         [self setContentMinSize: NSMakeSize(604., [o_controls_bar height] + [o_titlebar_view frame].size.height)];
618         [self setContentMaxSize: NSMakeSize(FLT_MAX, [o_controls_bar height] + [o_titlebar_view frame].size.height)];
619     } else {
620         [self setContentMinSize: NSMakeSize(604., [o_controls_bar height])];
621         [self setContentMaxSize: NSMakeSize(FLT_MAX, [o_controls_bar height])];
622     }
623
624     b_splitview_removed = YES;
625 }
626
627 - (void)showSplitView:(BOOL)b_with_resize
628 {
629     [self updateWindow];
630     if (b_dark_interface)
631         [self setContentMinSize:NSMakeSize(604., 288. + [o_titlebar_view frame].size.height)];
632     else
633         [self setContentMinSize:NSMakeSize(604., 288.)];
634     [self setContentMaxSize: NSMakeSize(FLT_MAX, FLT_MAX)];
635
636     if (b_with_resize) {
637         NSRect winrect;
638         winrect = [self frame];
639         winrect.size.height = winrect.size.height + i_lastSplitViewHeight;
640         winrect.origin.y = winrect.origin.y - i_lastSplitViewHeight;
641         [self setFrame: winrect display: YES animate: YES];
642     }
643
644     [self performSelector:@selector(resizePlaylistAfterCollapse) withObject: nil afterDelay:0.75];
645
646     b_splitview_removed = NO;
647 }
648
649 - (void)updateTimeSlider
650 {
651     [o_controls_bar updateTimeSlider];
652     [o_fspanel updatePositionAndTime];
653
654     [[[VLCMain sharedInstance] voutController] updateWindowsControlsBarWithSelector:@selector(updateTimeSlider)];
655 }
656
657 - (void)updateName
658 {
659     input_thread_t * p_input;
660     p_input = pl_CurrentInput(VLCIntf);
661     if (p_input) {
662         NSString *aString;
663
664         if (!config_GetPsz(VLCIntf, "video-title")) {
665             char *format = var_InheritString(VLCIntf, "input-title-format");
666             char *formated = str_format_meta(p_input, format);
667             free(format);
668             aString = [NSString stringWithUTF8String:formated];
669             free(formated);
670         } else
671             aString = [NSString stringWithUTF8String:config_GetPsz(VLCIntf, "video-title")];
672
673         char *uri = input_item_GetURI(input_GetItem(p_input));
674
675         NSURL * o_url = [NSURL URLWithString:[NSString stringWithUTF8String:uri]];
676         if ([o_url isFileURL]) {
677             [self setRepresentedURL: o_url];
678             [[[VLCMain sharedInstance] voutController] updateWindowsUsingBlock:^(VLCVideoWindowCommon *o_window) {
679                 [o_window setRepresentedURL:o_url];
680             }];
681         } else {
682             [self setRepresentedURL: nil];
683             [[[VLCMain sharedInstance] voutController] updateWindowsUsingBlock:^(VLCVideoWindowCommon *o_window) {
684                 [o_window setRepresentedURL:nil];
685             }];
686         }
687         free(uri);
688
689         if ([aString isEqualToString:@""]) {
690             if ([o_url isFileURL])
691                 aString = [[NSFileManager defaultManager] displayNameAtPath: [o_url path]];
692             else
693                 aString = [o_url absoluteString];
694         }
695
696         if ([aString length] > 0) {
697             [self setTitle: aString];
698             [[[VLCMain sharedInstance] voutController] updateWindowsUsingBlock:^(VLCVideoWindowCommon *o_window) {
699                 [o_window setTitle:aString];
700             }];
701
702             [o_fspanel setStreamTitle: aString];
703         } else {
704             [self setTitle: _NS("VLC media player")];
705             [self setRepresentedURL: nil];
706         }
707
708         vlc_object_release(p_input);
709     } else {
710         [self setTitle: _NS("VLC media player")];
711         [self setRepresentedURL: nil];
712     }
713 }
714
715 - (void)updateWindow
716 {
717     [o_controls_bar updateControls];
718     [[[VLCMain sharedInstance] voutController] updateWindowsControlsBarWithSelector:@selector(updateControls)];
719
720     bool b_seekable = false;
721
722     playlist_t * p_playlist = pl_Get(VLCIntf);
723     input_thread_t * p_input = playlist_CurrentInput(p_playlist);
724     if (p_input) {
725         /* seekable streams */
726         b_seekable = var_GetBool(p_input, "can-seek");
727
728         vlc_object_release(p_input);
729     }
730
731     [self updateTimeSlider];
732     if ([o_fspanel respondsToSelector:@selector(setSeekable:)])
733         [o_fspanel setSeekable: b_seekable];
734
735     PL_LOCK;
736     if ([[[VLCMain sharedInstance] playlist] currentPlaylistRoot] != p_playlist->p_local_category || p_playlist->p_local_category->i_children > 0)
737         [self hideDropZone];
738     else
739         [self showDropZone];
740     PL_UNLOCK;
741     [o_sidebar_view setNeedsDisplay:YES];
742 }
743
744 - (void)setPause
745 {
746     [o_controls_bar setPause];
747     [o_fspanel setPause];
748
749     [[[VLCMain sharedInstance] voutController] updateWindowsControlsBarWithSelector:@selector(setPause)];
750 }
751
752 - (void)setPlay
753 {
754     [o_controls_bar setPlay];
755     [o_fspanel setPlay];
756
757     [[[VLCMain sharedInstance] voutController] updateWindowsControlsBarWithSelector:@selector(setPlay)];
758 }
759
760 - (void)updateVolumeSlider
761 {
762     [[self controlsBar] updateVolumeSlider];
763     [o_fspanel setVolumeLevel: [[VLCCoreInteraction sharedInstance] volume]];
764 }
765
766 #pragma mark -
767 #pragma mark Video Output handling
768
769 - (void)videoplayWillBeStarted
770 {
771     if (!b_fullscreen)
772         frameBeforePlayback = [self frame];
773 }
774
775 - (void)setVideoplayEnabled
776 {
777     BOOL b_videoPlayback = [[VLCMain sharedInstance] activeVideoPlayback];
778         
779     if (!b_videoPlayback) {
780         if (!b_nonembedded && (!b_nativeFullscreenMode || (b_nativeFullscreenMode && !b_fullscreen)) && frameBeforePlayback.size.width > 0 && frameBeforePlayback.size.height > 0) {
781
782             // only resize back to minimum view of this is still desired final state
783             float f_threshold_height = f_min_video_height + [o_controls_bar height];
784             if(frameBeforePlayback.size.height > f_threshold_height || b_minimized_view)
785                 [[self animator] setFrame:frameBeforePlayback display:YES];
786         }
787
788         frameBeforePlayback = NSMakeRect(0, 0, 0, 0);
789
790         // update fs button to reflect state for next startup
791         if (var_InheritBool(VLCIntf, "fullscreen") || var_GetBool(pl_Get(VLCIntf), "fullscreen")) {
792             [o_controls_bar setFullscreenState:YES];
793         }
794
795         [self makeFirstResponder: o_playlist_table];
796         [[[VLCMain sharedInstance] voutController] updateWindowLevelForHelperWindows: NSNormalWindowLevel];
797
798         // restore alpha value to 1 for the case that macosx-opaqueness is set to < 1
799         [self setAlphaValue:1.0];
800     }
801
802     if (b_nativeFullscreenMode) {
803         if ([self hasActiveVideo] && [self fullscreen]) {
804             [[o_controls_bar bottomBarView] setHidden: b_videoPlayback];
805             [o_fspanel setActive: nil];
806         } else {
807             [[o_controls_bar bottomBarView] setHidden: NO];
808             [o_fspanel setNonActive: nil];
809         }
810     }
811
812     [self changePlaylistState: psVideoStartedOrStoppedEvent];
813 }
814
815 #pragma mark -
816 #pragma mark Lion native fullscreen handling
817 - (void)windowWillEnterFullScreen:(NSNotification *)notification
818 {
819     [super windowWillEnterFullScreen:notification];
820
821     // update split view frame after removing title bar
822     if (b_dark_interface) {
823         NSRect frame = [[self contentView] frame];
824         frame.origin.y += [o_controls_bar height];
825         frame.size.height -= [o_controls_bar height];
826         [o_split_view setFrame:frame];
827     }
828 }
829
830 - (void)windowWillExitFullScreen:(NSNotification *)notification
831 {
832     [super windowWillExitFullScreen: notification];
833
834     // update split view frame after readding title bar
835     if (b_dark_interface) {
836         NSRect frame = [o_split_view frame];
837         frame.size.height -= [o_titlebar_view frame].size.height;
838         [o_split_view setFrame:frame];
839     }
840 }
841 #pragma mark -
842 #pragma mark Fullscreen support
843
844 - (void)showFullscreenController
845 {
846     id currentWindow = [NSApp keyWindow];
847     if ([currentWindow respondsToSelector:@selector(hasActiveVideo)] && [currentWindow hasActiveVideo]) {
848         if ([currentWindow respondsToSelector:@selector(fullscreen)] && [currentWindow fullscreen] && ![[currentWindow videoView] isHidden]) {
849
850             if ([[VLCMain sharedInstance] activeVideoPlayback])
851                 [o_fspanel fadeIn];
852         }
853     }
854
855 }
856
857 #pragma mark -
858 #pragma mark split view delegate
859 - (CGFloat)splitView:(NSSplitView *)splitView constrainMaxCoordinate:(CGFloat)proposedMax ofSubviewAt:(NSInteger)dividerIndex
860 {
861     if (dividerIndex == 0)
862         return 300.;
863     else
864         return proposedMax;
865 }
866
867 - (CGFloat)splitView:(NSSplitView *)splitView constrainMinCoordinate:(CGFloat)proposedMin ofSubviewAt:(NSInteger)dividerIndex
868 {
869     if (dividerIndex == 0)
870         return 100.;
871     else
872         return proposedMin;
873 }
874
875 - (BOOL)splitView:(NSSplitView *)splitView canCollapseSubview:(NSView *)subview
876 {
877     return ([subview isEqual:o_left_split_view]);
878 }
879
880 - (BOOL)splitView:(NSSplitView *)splitView shouldAdjustSizeOfSubview:(NSView *)subview
881 {
882     if ([subview isEqual:o_left_split_view])
883         return NO;
884     return YES;
885 }
886
887 - (void)mainSplitViewDidResizeSubviews:(id)object
888 {
889     i_lastLeftSplitViewWidth = [o_left_split_view frame].size.width;
890     config_PutInt(VLCIntf, "macosx-show-sidebar", ![o_split_view isSubviewCollapsed:o_left_split_view]);
891     [[[VLCMain sharedInstance] mainMenu] updateSidebarMenuItem];
892 }
893
894 - (void)toggleLeftSubSplitView
895 {
896     [o_split_view adjustSubviews];
897     if ([o_split_view isSubviewCollapsed:o_left_split_view])
898         [o_split_view setPosition:i_lastLeftSplitViewWidth ofDividerAtIndex:0];
899     else
900         [o_split_view setPosition:[o_split_view minPossiblePositionOfDividerAtIndex:0] ofDividerAtIndex:0];
901     [[[VLCMain sharedInstance] mainMenu] updateSidebarMenuItem];
902 }
903
904 #pragma mark -
905 #pragma mark Side Bar Data handling
906 /* taken under BSD-new from the PXSourceList sample project, adapted for VLC */
907 - (NSUInteger)sourceList:(PXSourceList*)sourceList numberOfChildrenOfItem:(id)item
908 {
909     //Works the same way as the NSOutlineView data source: `nil` means a parent item
910     if (item==nil)
911         return [o_sidebaritems count];
912     else
913         return [[item children] count];
914 }
915
916
917 - (id)sourceList:(PXSourceList*)aSourceList child:(NSUInteger)index ofItem:(id)item
918 {
919     //Works the same way as the NSOutlineView data source: `nil` means a parent item
920     if (item==nil)
921         return [o_sidebaritems objectAtIndex:index];
922     else
923         return [[item children] objectAtIndex:index];
924 }
925
926
927 - (id)sourceList:(PXSourceList*)aSourceList objectValueForItem:(id)item
928 {
929     return [item title];
930 }
931
932 - (void)sourceList:(PXSourceList*)aSourceList setObjectValue:(id)object forItem:(id)item
933 {
934     [item setTitle:object];
935 }
936
937 - (BOOL)sourceList:(PXSourceList*)aSourceList isItemExpandable:(id)item
938 {
939     return [item hasChildren];
940 }
941
942
943 - (BOOL)sourceList:(PXSourceList*)aSourceList itemHasBadge:(id)item
944 {
945     if ([[item identifier] isEqualToString: @"playlist"] || [[item identifier] isEqualToString: @"medialibrary"])
946         return YES;
947
948     return [item hasBadge];
949 }
950
951
952 - (NSInteger)sourceList:(PXSourceList*)aSourceList badgeValueForItem:(id)item
953 {
954     playlist_t * p_playlist = pl_Get(VLCIntf);
955     NSInteger i_playlist_size = 0;
956
957     if ([[item identifier] isEqualToString: @"playlist"]) {
958         PL_LOCK;
959         i_playlist_size = p_playlist->p_local_category->i_children;
960         PL_UNLOCK;
961
962         return i_playlist_size;
963     }
964     if ([[item identifier] isEqualToString: @"medialibrary"]) {
965         PL_LOCK;
966         if (p_playlist->p_ml_category)
967             i_playlist_size = p_playlist->p_ml_category->i_children;
968         PL_UNLOCK;
969
970         return i_playlist_size;
971     }
972
973     return [item badgeValue];
974 }
975
976
977 - (BOOL)sourceList:(PXSourceList*)aSourceList itemHasIcon:(id)item
978 {
979     return [item hasIcon];
980 }
981
982
983 - (NSImage*)sourceList:(PXSourceList*)aSourceList iconForItem:(id)item
984 {
985     return [item icon];
986 }
987
988 - (NSMenu*)sourceList:(PXSourceList*)aSourceList menuForEvent:(NSEvent*)theEvent item:(id)item
989 {
990     if ([theEvent type] == NSRightMouseDown || ([theEvent type] == NSLeftMouseDown && ([theEvent modifierFlags] & NSControlKeyMask) == NSControlKeyMask)) {
991         if (item != nil) {
992             NSMenu * m;
993             if ([item sdtype] > 0)
994             {
995                 m = [[NSMenu alloc] init];
996                 playlist_t * p_playlist = pl_Get(VLCIntf);
997                 BOOL sd_loaded = playlist_IsServicesDiscoveryLoaded(p_playlist, [[item identifier] UTF8String]);
998                 if (!sd_loaded)
999                     [m addItemWithTitle:_NS("Enable") action:@selector(sdmenuhandler:) keyEquivalent:@""];
1000                 else
1001                     [m addItemWithTitle:_NS("Disable") action:@selector(sdmenuhandler:) keyEquivalent:@""];
1002                 [[m itemAtIndex:0] setRepresentedObject: [item identifier]];
1003             }
1004             return [m autorelease];
1005         }
1006     }
1007
1008     return nil;
1009 }
1010
1011 - (IBAction)sdmenuhandler:(id)sender
1012 {
1013     NSString * identifier = [sender representedObject];
1014     if ([identifier length] > 0 && ![identifier isEqualToString:@"lua{sd='freebox',longname='Freebox TV'}"]) {
1015         playlist_t * p_playlist = pl_Get(VLCIntf);
1016         BOOL sd_loaded = playlist_IsServicesDiscoveryLoaded(p_playlist, [identifier UTF8String]);
1017
1018         if (!sd_loaded)
1019             playlist_ServicesDiscoveryAdd(p_playlist, [identifier UTF8String]);
1020         else
1021             playlist_ServicesDiscoveryRemove(p_playlist, [identifier UTF8String]);
1022     }
1023 }
1024
1025 #pragma mark -
1026 #pragma mark Side Bar Delegate Methods
1027 /* taken under BSD-new from the PXSourceList sample project, adapted for VLC */
1028 - (BOOL)sourceList:(PXSourceList*)aSourceList isGroupAlwaysExpanded:(id)group
1029 {
1030     if ([[group identifier] isEqualToString:@"library"])
1031         return YES;
1032
1033     return NO;
1034 }
1035
1036 - (void)sourceListSelectionDidChange:(NSNotification *)notification
1037 {
1038     playlist_t * p_playlist = pl_Get(VLCIntf);
1039
1040     NSIndexSet *selectedIndexes = [o_sidebar_view selectedRowIndexes];
1041     id item = [o_sidebar_view itemAtRow:[selectedIndexes firstIndex]];
1042
1043
1044     //Set the label text to represent the new selection
1045     if ([item sdtype] > -1 && [[item identifier] length] > 0) {
1046         BOOL sd_loaded = playlist_IsServicesDiscoveryLoaded(p_playlist, [[item identifier] UTF8String]);
1047         if (!sd_loaded)
1048             playlist_ServicesDiscoveryAdd(p_playlist, [[item identifier] UTF8String]);
1049     }
1050
1051     [o_chosen_category_lbl setStringValue:[item title]];
1052
1053     if ([[item identifier] isEqualToString:@"playlist"]) {
1054         [[[VLCMain sharedInstance] playlist] setPlaylistRoot:p_playlist->p_local_category];
1055     } else if ([[item identifier] isEqualToString:@"medialibrary"]) {
1056         if (p_playlist->p_ml_category)
1057             [[[VLCMain sharedInstance] playlist] setPlaylistRoot:p_playlist->p_ml_category];
1058     } else {
1059         playlist_item_t * pl_item;
1060         PL_LOCK;
1061         pl_item = playlist_ChildSearchName(p_playlist->p_root, [[item untranslatedTitle] UTF8String]);
1062         PL_UNLOCK;
1063         [[[VLCMain sharedInstance] playlist] setPlaylistRoot: pl_item];
1064     }
1065
1066     PL_LOCK;
1067     if ([[[VLCMain sharedInstance] playlist] currentPlaylistRoot] != p_playlist->p_local_category || p_playlist->p_local_category->i_children > 0)
1068         [self hideDropZone];
1069     else
1070         [self showDropZone];
1071     PL_UNLOCK;
1072
1073     if ([[item identifier] isEqualToString:@"podcast{longname=\"Podcasts\"}"])
1074         [self showPodcastControls];
1075     else
1076         [self hidePodcastControls];
1077
1078     [[NSNotificationCenter defaultCenter] postNotificationName: @"VLCMediaKeySupportSettingChanged"
1079                                                         object: nil
1080                                                       userInfo: nil];
1081 }
1082
1083 - (NSDragOperation)sourceList:(PXSourceList *)aSourceList validateDrop:(id <NSDraggingInfo>)info proposedItem:(id)item proposedChildIndex:(NSInteger)index
1084 {
1085     if ([[item identifier] isEqualToString:@"playlist"] || [[item identifier] isEqualToString:@"medialibrary"]) {
1086         NSPasteboard *o_pasteboard = [info draggingPasteboard];
1087         if ([[o_pasteboard types] containsObject: @"VLCPlaylistItemPboardType"] || [[o_pasteboard types] containsObject: NSFilenamesPboardType])
1088             return NSDragOperationGeneric;
1089     }
1090     return NSDragOperationNone;
1091 }
1092
1093 - (BOOL)sourceList:(PXSourceList *)aSourceList acceptDrop:(id <NSDraggingInfo>)info item:(id)item childIndex:(NSInteger)index
1094 {
1095     NSPasteboard *o_pasteboard = [info draggingPasteboard];
1096
1097     playlist_t * p_playlist = pl_Get(VLCIntf);
1098     playlist_item_t *p_node;
1099
1100     if ([[item identifier] isEqualToString:@"playlist"])
1101         p_node = p_playlist->p_local_category;
1102     else
1103         p_node = p_playlist->p_ml_category;
1104
1105     if ([[o_pasteboard types] containsObject: NSFilenamesPboardType]) {
1106         NSArray *o_values = [[o_pasteboard propertyListForType: NSFilenamesPboardType] sortedArrayUsingSelector: @selector(caseInsensitiveCompare:)];
1107         NSUInteger count = [o_values count];
1108         NSMutableArray *o_array = [NSMutableArray arrayWithCapacity:count];
1109
1110         for(NSUInteger i = 0; i < count; i++) {
1111             NSDictionary *o_dic;
1112             char *psz_uri = vlc_path2uri([[o_values objectAtIndex:i] UTF8String], NULL);
1113             if (!psz_uri)
1114                 continue;
1115
1116             o_dic = [NSDictionary dictionaryWithObject:[NSString stringWithCString:psz_uri encoding:NSUTF8StringEncoding] forKey:@"ITEM_URL"];
1117
1118             free(psz_uri);
1119
1120             [o_array addObject: o_dic];
1121         }
1122
1123         [[[VLCMain sharedInstance] playlist] appendNodeArray:o_array inNode: p_node atPos:-1 enqueue:YES];
1124         return YES;
1125     }
1126     else if ([[o_pasteboard types] containsObject: @"VLCPlaylistItemPboardType"]) {
1127         NSArray * array = [[[VLCMain sharedInstance] playlist] draggedItems];
1128
1129         NSUInteger count = [array count];
1130         playlist_item_t * p_item = NULL;
1131
1132         PL_LOCK;
1133         for(NSUInteger i = 0; i < count; i++) {
1134             p_item = [[array objectAtIndex:i] pointerValue];
1135             if (!p_item) continue;
1136             playlist_NodeAddCopy(p_playlist, p_item, p_node, PLAYLIST_END);
1137         }
1138         PL_UNLOCK;
1139
1140         return YES;
1141     }
1142     return NO;
1143 }
1144
1145 - (id)sourceList:(PXSourceList *)aSourceList persistentObjectForItem:(id)item
1146 {
1147     return [item identifier];
1148 }
1149
1150 - (id)sourceList:(PXSourceList *)aSourceList itemForPersistentObject:(id)object
1151 {
1152     /* the following code assumes for sakes of simplicity that only the top level
1153      * items are allowed to have children */
1154
1155     NSArray * array = [NSArray arrayWithArray: o_sidebaritems]; // read-only arrays are noticebly faster
1156     NSUInteger count = [array count];
1157     if (count < 1)
1158         return nil;
1159
1160     for (NSUInteger x = 0; x < count; x++) {
1161         id item = [array objectAtIndex:x]; // save one objc selector call
1162         if ([[item identifier] isEqualToString:object])
1163             return item;
1164     }
1165
1166     return nil;
1167 }
1168
1169 #pragma mark -
1170 #pragma mark Podcast
1171
1172 - (IBAction)addPodcast:(id)sender
1173 {
1174     [NSApp beginSheet:o_podcast_subscribe_window modalForWindow:self modalDelegate:self didEndSelector:NULL contextInfo:nil];
1175 }
1176
1177 - (IBAction)addPodcastWindowAction:(id)sender
1178 {
1179     [o_podcast_subscribe_window orderOut:sender];
1180     [NSApp endSheet: o_podcast_subscribe_window];
1181
1182     if (sender == o_podcast_subscribe_ok_btn && [[o_podcast_subscribe_url_fld stringValue] length] > 0) {
1183         NSMutableString * podcastConf = [[NSMutableString alloc] init];
1184         if (config_GetPsz(VLCIntf, "podcast-urls") != NULL)
1185             [podcastConf appendFormat:@"%s|", config_GetPsz(VLCIntf, "podcast-urls")];
1186
1187         [podcastConf appendString: [o_podcast_subscribe_url_fld stringValue]];
1188         config_PutPsz(VLCIntf, "podcast-urls", [podcastConf UTF8String]);
1189         var_SetString(pl_Get(VLCIntf), "podcast-urls", [podcastConf UTF8String]);
1190         [podcastConf release];
1191     }
1192 }
1193
1194 - (IBAction)removePodcast:(id)sender
1195 {
1196     if (config_GetPsz(VLCIntf, "podcast-urls") != NULL) {
1197         [o_podcast_unsubscribe_pop removeAllItems];
1198         [o_podcast_unsubscribe_pop addItemsWithTitles:[[NSString stringWithUTF8String:config_GetPsz(VLCIntf, "podcast-urls")] componentsSeparatedByString:@"|"]];
1199         [NSApp beginSheet:o_podcast_unsubscribe_window modalForWindow:self modalDelegate:self didEndSelector:NULL contextInfo:nil];
1200     }
1201 }
1202
1203 - (IBAction)removePodcastWindowAction:(id)sender
1204 {
1205     [o_podcast_unsubscribe_window orderOut:sender];
1206     [NSApp endSheet: o_podcast_unsubscribe_window];
1207
1208     if (sender == o_podcast_unsubscribe_ok_btn) {
1209         NSMutableArray * urls = [[NSMutableArray alloc] initWithArray:[[NSString stringWithUTF8String:config_GetPsz(VLCIntf, "podcast-urls")] componentsSeparatedByString:@"|"]];
1210         [urls removeObjectAtIndex: [o_podcast_unsubscribe_pop indexOfSelectedItem]];
1211         config_PutPsz(VLCIntf, "podcast-urls", [[urls componentsJoinedByString:@"|"] UTF8String]);
1212         var_SetString(pl_Get(VLCIntf), "podcast-urls", config_GetPsz(VLCIntf, "podcast-urls"));
1213         [urls release];
1214
1215         /* reload the podcast module, since it won't update its list when removing podcasts */
1216         playlist_t * p_playlist = pl_Get(VLCIntf);
1217         if (playlist_IsServicesDiscoveryLoaded(p_playlist, "podcast{longname=\"Podcasts\"}")) {
1218             playlist_ServicesDiscoveryRemove(p_playlist, "podcast{longname=\"Podcasts\"}");
1219             playlist_ServicesDiscoveryAdd(p_playlist, "podcast{longname=\"Podcasts\"}");
1220             [[[VLCMain sharedInstance] playlist] playlistUpdated];
1221         }
1222     }
1223 }
1224
1225 - (void)showPodcastControls
1226 {
1227     NSRect podcastViewDimensions = [o_podcast_view frame];
1228     NSRect rightSplitRect = [o_right_split_view frame];
1229     NSRect playlistTableRect = [o_playlist_table frame];
1230
1231     podcastViewDimensions.size.width = rightSplitRect.size.width;
1232     podcastViewDimensions.origin.x = podcastViewDimensions.origin.y = .0;
1233     [o_podcast_view setFrame:podcastViewDimensions];
1234
1235     playlistTableRect.origin.y = playlistTableRect.origin.y + podcastViewDimensions.size.height;
1236     playlistTableRect.size.height = playlistTableRect.size.height - podcastViewDimensions.size.height;
1237     [o_playlist_table setFrame:playlistTableRect];
1238     [o_playlist_table setNeedsDisplay:YES];
1239
1240     [o_right_split_view addSubview: o_podcast_view positioned: NSWindowAbove relativeTo: o_right_split_view];
1241     b_podcastView_displayed = YES;
1242 }
1243
1244 - (void)hidePodcastControls
1245 {
1246     if (b_podcastView_displayed) {
1247         NSRect podcastViewDimensions = [o_podcast_view frame];
1248         NSRect playlistTableRect = [o_playlist_table frame];
1249
1250         playlistTableRect.origin.y = playlistTableRect.origin.y - podcastViewDimensions.size.height;
1251         playlistTableRect.size.height = playlistTableRect.size.height + podcastViewDimensions.size.height;
1252
1253         [o_podcast_view removeFromSuperviewWithoutNeedingDisplay];
1254         [o_playlist_table setFrame: playlistTableRect];
1255         b_podcastView_displayed = NO;
1256     }
1257 }
1258
1259 @end
1260
1261 @implementation VLCDetachedVideoWindow
1262
1263 - (void)awakeFromNib
1264 {
1265     // sets lion fullscreen behaviour
1266     [super awakeFromNib];
1267     [self setAcceptsMouseMovedEvents: YES];
1268
1269     if (b_dark_interface) {
1270         [self setBackgroundColor: [NSColor clearColor]];
1271
1272         [self setOpaque: NO];
1273         [self display];
1274         [self setHasShadow:NO];
1275         [self setHasShadow:YES];
1276
1277         NSRect winrect = [self frame];
1278         CGFloat f_titleBarHeight = [o_titlebar_view frame].size.height;
1279
1280         [self setTitle: _NS("VLC media player")];
1281         [o_titlebar_view setFrame: NSMakeRect(0, winrect.size.height - f_titleBarHeight, winrect.size.width, f_titleBarHeight)];
1282         [[self contentView] addSubview: o_titlebar_view positioned: NSWindowAbove relativeTo: nil];
1283
1284     } else {
1285         [self setBackgroundColor: [NSColor blackColor]];
1286     }
1287
1288     NSRect videoViewRect = [[self contentView] bounds];
1289     if (b_dark_interface)
1290         videoViewRect.size.height -= [o_titlebar_view frame].size.height;
1291     CGFloat f_bottomBarHeight = [[self controlsBar] height];
1292     videoViewRect.size.height -= f_bottomBarHeight;
1293     videoViewRect.origin.y = f_bottomBarHeight;
1294     [o_video_view setFrame: videoViewRect];
1295
1296     if (b_dark_interface) {
1297         o_color_backdrop = [[VLCColorView alloc] initWithFrame: [o_video_view frame]];
1298         [[self contentView] addSubview: o_color_backdrop positioned: NSWindowBelow relativeTo: o_video_view];
1299         [o_color_backdrop setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
1300
1301         [self setContentMinSize: NSMakeSize(363., f_min_video_height + [[self controlsBar] height] + [o_titlebar_view frame].size.height)];
1302     } else {
1303         [self setContentMinSize: NSMakeSize(363., f_min_video_height + [[self controlsBar] height])];
1304     }
1305 }
1306
1307 - (void)dealloc
1308 {
1309     if (b_dark_interface)
1310         [o_color_backdrop release];
1311
1312     [super dealloc];
1313 }
1314
1315 @end