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