]> git.sesse.net Git - vlc/blob - modules/gui/macosx/MainWindow.m
macosx: only show fspanel when needed, additional fixed for lion fullscreen behavior
[vlc] / modules / gui / macosx / MainWindow.m
1 /*****************************************************************************
2  * MainWindow.m: MacOS X interface module
3  *****************************************************************************
4  * Copyright (C) 2002-2012 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Felix Paul Kühne <fkuehne -at- videolan -dot- org>
8  *          Jon Lech Johansen <jon-vl@nanocrew.net>
9  *          Christophe Massiot <massiot@via.ecp.fr>
10  *          Derk-Jan Hartman <hartman at videolan.org>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *****************************************************************************/
26
27 #import "CompatibilityFixes.h"
28 #import "MainWindow.h"
29 #import "intf.h"
30 #import "CoreInteraction.h"
31 #import "AudioEffects.h"
32 #import "MainMenu.h"
33 #import "open.h"
34 #import "controls.h" // TODO: remove me
35 #import "playlist.h"
36 #import "SideBarItem.h"
37 #import <math.h>
38 #import <vlc_playlist.h>
39 #import <vlc_url.h>
40 #import <vlc_strings.h>
41 #import <vlc_services_discovery.h>
42
43 #import "ControlsBar.h"
44 #import "VideoView.h"
45 #import "VLCVoutWindowController.h"
46
47
48 @interface VLCMainWindow (Internal)
49 - (void)resizePlaylistAfterCollapse;
50 - (void)makeSplitViewVisible;
51 - (void)makeSplitViewHidden;
52 - (void)showPodcastControls;
53 - (void)hidePodcastControls;
54 @end
55
56
57 @implementation VLCMainWindow
58
59 @synthesize nativeFullscreenMode=b_nativeFullscreenMode;
60 @synthesize nonembedded=b_nonembedded;
61 @synthesize fsPanel=o_fspanel;
62
63 static VLCMainWindow *_o_sharedInstance = nil;
64
65 + (VLCMainWindow *)sharedInstance
66 {
67     return _o_sharedInstance ? _o_sharedInstance : [[self alloc] init];
68 }
69
70 #pragma mark -
71 #pragma mark Initialization
72
73 - (id)init
74 {
75     if (_o_sharedInstance) {
76         [self dealloc];
77         return _o_sharedInstance;
78     } else
79         _o_sharedInstance = [super init];
80
81     return _o_sharedInstance;
82 }
83
84 - (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)styleMask
85                   backing:(NSBackingStoreType)backingType defer:(BOOL)flag
86 {
87     self = [super initWithContentRect:contentRect styleMask:styleMask
88                               backing:backingType defer:flag];
89     _o_sharedInstance = self;
90
91     [[VLCMain sharedInstance] updateTogglePlaylistState];
92
93     return self;
94 }
95
96 - (BOOL)isEvent:(NSEvent *)o_event forKey:(const char *)keyString
97 {
98     char *key;
99     NSString *o_key;
100
101     key = config_GetPsz(VLCIntf, keyString);
102     o_key = [NSString stringWithFormat:@"%s", key];
103     FREENULL(key);
104
105     unsigned int i_keyModifiers = [[VLCStringUtility sharedInstance] VLCModifiersToCocoa:o_key];
106
107     NSString * characters = [o_event charactersIgnoringModifiers];
108     if ([characters length] > 0) {
109         return [[characters lowercaseString] isEqualToString: [[VLCStringUtility sharedInstance] VLCKeyToString: o_key]] &&
110                 (i_keyModifiers & NSShiftKeyMask)     == ([o_event modifierFlags] & NSShiftKeyMask) &&
111                 (i_keyModifiers & NSControlKeyMask)   == ([o_event modifierFlags] & NSControlKeyMask) &&
112                 (i_keyModifiers & NSAlternateKeyMask) == ([o_event modifierFlags] & NSAlternateKeyMask) &&
113                 (i_keyModifiers & NSCommandKeyMask)   == ([o_event modifierFlags] & NSCommandKeyMask);
114     }
115     return NO;
116 }
117
118 - (BOOL)performKeyEquivalent:(NSEvent *)o_event
119 {
120     BOOL b_force = NO;
121     // these are key events which should be handled by vlc core, but are attached to a main menu item
122     if (![self isEvent: o_event forKey: "key-vol-up"] &&
123         ![self isEvent: o_event forKey: "key-vol-down"] &&
124         ![self isEvent: o_event forKey: "key-vol-mute"]) {
125         /* We indeed want to prioritize some Cocoa key equivalent against libvlc,
126          so we perform the menu equivalent now. */
127         if ([[NSApp mainMenu] performKeyEquivalent:o_event])
128             return TRUE;
129     }
130     else
131         b_force = YES;
132
133     return [[VLCMain sharedInstance] hasDefinedShortcutKey:o_event force:b_force] ||
134            [(VLCControls *)[[VLCMain sharedInstance] controls] keyEvent:o_event];
135 }
136
137 - (void)dealloc
138 {
139     if (b_dark_interface)
140         [o_color_backdrop release];
141
142     [[NSNotificationCenter defaultCenter] removeObserver: self];
143     [o_sidebaritems release];
144
145     [super dealloc];
146 }
147
148 - (void)awakeFromNib
149 {
150     // sets lion fullscreen behaviour
151     [super awakeFromNib];
152
153     BOOL b_splitviewShouldBeHidden = NO;
154
155     /* setup the styled interface */
156     b_nativeFullscreenMode = NO;
157 #ifdef MAC_OS_X_VERSION_10_7
158     if (!OSX_SNOW_LEOPARD)
159         b_nativeFullscreenMode = var_InheritBool(VLCIntf, "macosx-nativefullscreenmode");
160 #endif
161     t_hide_mouse_timer = nil;
162     [self useOptimizedDrawing: YES];
163
164     [[o_search_fld cell] setPlaceholderString: _NS("Search")];
165     [[o_search_fld cell] accessibilitySetOverrideValue:_NS("Enter a term to search the playlist. Results will be selected in the table.") forAttribute:NSAccessibilityDescriptionAttribute];
166
167     [o_dropzone_btn setTitle: _NS("Open media...")];
168     [[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];
169     [o_dropzone_lbl setStringValue: _NS("Drop media here")];
170
171     [o_podcast_add_btn setTitle: _NS("Subscribe")];
172     [o_podcast_remove_btn setTitle: _NS("Unsubscribe")];
173     [o_podcast_subscribe_title_lbl setStringValue: _NS("Subscribe to a podcast")];
174     [o_podcast_subscribe_subtitle_lbl setStringValue: _NS("Enter URL of the podcast to subscribe to:")];
175     [o_podcast_subscribe_cancel_btn setTitle: _NS("Cancel")];
176     [o_podcast_subscribe_ok_btn setTitle: _NS("Subscribe")];
177     [o_podcast_unsubscribe_title_lbl setStringValue: _NS("Unsubscribe from a podcast")];
178     [o_podcast_unsubscribe_subtitle_lbl setStringValue: _NS("Select the podcast you would like to unsubscribe from:")];
179     [o_podcast_unsubscribe_ok_btn setTitle: _NS("Unsubscribe")];
180     [o_podcast_unsubscribe_cancel_btn setTitle: _NS("Cancel")];
181
182     /* interface builder action */
183     float f_threshold_height = f_min_video_height + [[o_controls_bar bottomBarView] frame].size.height;
184     if (b_dark_interface)
185         f_threshold_height += [o_titlebar_view frame].size.height;
186     if ([[self contentView] frame].size.height < f_threshold_height)
187         b_splitviewShouldBeHidden = YES;
188
189     [self setDelegate: self];
190     [self setExcludedFromWindowsMenu: YES];
191     [self setAcceptsMouseMovedEvents: YES];
192     // Set that here as IB seems to be buggy
193     if (b_dark_interface) {
194         [self setContentMinSize:NSMakeSize(604., 288. + [o_titlebar_view frame].size.height)];
195     } else {
196         [self setContentMinSize:NSMakeSize(604., 288.)];
197     }
198
199     [self setTitle: _NS("VLC media player")];
200
201     b_dropzone_active = YES;
202     [o_dropzone_view setFrame: [o_playlist_table frame]];
203     [o_left_split_view setFrame: [o_sidebar_view frame]];
204
205     if (!OSX_SNOW_LEOPARD) {
206         /* the default small size of the search field is slightly different on Lion, let's work-around that */
207         NSRect frame;
208         frame = [o_search_fld frame];
209         frame.origin.y = frame.origin.y + 2.0;
210         frame.size.height = frame.size.height - 1.0;
211         [o_search_fld setFrame: frame];
212     }
213
214     /* create the sidebar */
215     o_sidebaritems = [[NSMutableArray alloc] init];
216     SideBarItem *libraryItem = [SideBarItem itemWithTitle:_NS("LIBRARY") identifier:@"library"];
217     SideBarItem *playlistItem = [SideBarItem itemWithTitle:_NS("Playlist") identifier:@"playlist"];
218     [playlistItem setIcon: [NSImage imageNamed:@"sidebar-playlist"]];
219     SideBarItem *medialibraryItem = [SideBarItem itemWithTitle:_NS("Media Library") identifier:@"medialibrary"];
220     [medialibraryItem setIcon: [NSImage imageNamed:@"sidebar-playlist"]];
221     SideBarItem *mycompItem = [SideBarItem itemWithTitle:_NS("MY COMPUTER") identifier:@"mycomputer"];
222     SideBarItem *devicesItem = [SideBarItem itemWithTitle:_NS("DEVICES") identifier:@"devices"];
223     SideBarItem *lanItem = [SideBarItem itemWithTitle:_NS("LOCAL NETWORK") identifier:@"localnetwork"];
224     SideBarItem *internetItem = [SideBarItem itemWithTitle:_NS("INTERNET") identifier:@"internet"];
225
226     /* SD subnodes, inspired by the Qt4 intf */
227     char **ppsz_longnames;
228     int *p_categories;
229     char **ppsz_names = vlc_sd_GetNames(pl_Get(VLCIntf), &ppsz_longnames, &p_categories);
230     if (!ppsz_names)
231         msg_Err(VLCIntf, "no sd item found"); //TODO
232     char **ppsz_name = ppsz_names, **ppsz_longname = ppsz_longnames;
233     int *p_category = p_categories;
234     NSMutableArray *internetItems = [[NSMutableArray alloc] init];
235     NSMutableArray *devicesItems = [[NSMutableArray alloc] init];
236     NSMutableArray *lanItems = [[NSMutableArray alloc] init];
237     NSMutableArray *mycompItems = [[NSMutableArray alloc] init];
238     NSString *o_identifier;
239     for (; *ppsz_name; ppsz_name++, ppsz_longname++, p_category++) {
240         o_identifier = [NSString stringWithCString: *ppsz_name encoding: NSUTF8StringEncoding];
241         switch (*p_category) {
242             case SD_CAT_INTERNET:
243                     [internetItems addObject: [SideBarItem itemWithTitle: _NS(*ppsz_longname) identifier: o_identifier]];
244                     if (!strncmp(*ppsz_name, "podcast", 7))
245                         [[internetItems lastObject] setIcon: [NSImage imageNamed:@"sidebar-podcast"]];
246                     else
247                         [[internetItems lastObject] setIcon: [NSImage imageNamed:@"NSApplicationIcon"]];
248                     [[internetItems lastObject] setSdtype: SD_CAT_INTERNET];
249                     [[internetItems lastObject] setUntranslatedTitle: [NSString stringWithUTF8String: *ppsz_longname]];
250                 break;
251             case SD_CAT_DEVICES:
252                     [devicesItems addObject: [SideBarItem itemWithTitle: _NS(*ppsz_longname) identifier: o_identifier]];
253                     [[devicesItems lastObject] setIcon: [NSImage imageNamed:@"NSApplicationIcon"]];
254                     [[devicesItems lastObject] setSdtype: SD_CAT_DEVICES];
255                     [[devicesItems lastObject] setUntranslatedTitle: [NSString stringWithUTF8String: *ppsz_longname]];
256                 break;
257             case SD_CAT_LAN:
258                     [lanItems addObject: [SideBarItem itemWithTitle: _NS(*ppsz_longname) identifier: o_identifier]];
259                     [[lanItems lastObject] setIcon: [NSImage imageNamed:@"sidebar-local"]];
260                     [[lanItems lastObject] setSdtype: SD_CAT_LAN];
261                     [[lanItems lastObject] setUntranslatedTitle: [NSString stringWithUTF8String: *ppsz_longname]];
262                 break;
263             case SD_CAT_MYCOMPUTER:
264                     [mycompItems addObject: [SideBarItem itemWithTitle: _NS(*ppsz_longname) identifier: o_identifier]];
265                     if (!strncmp(*ppsz_name, "video_dir", 9))
266                         [[mycompItems lastObject] setIcon: [NSImage imageNamed:@"sidebar-movie"]];
267                     else if (!strncmp(*ppsz_name, "audio_dir", 9))
268                         [[mycompItems lastObject] setIcon: [NSImage imageNamed:@"sidebar-music"]];
269                     else if (!strncmp(*ppsz_name, "picture_dir", 11))
270                         [[mycompItems lastObject] setIcon: [NSImage imageNamed:@"sidebar-pictures"]];
271                     else
272                         [[mycompItems lastObject] setIcon: [NSImage imageNamed:@"NSApplicationIcon"]];
273                     [[mycompItems lastObject] setUntranslatedTitle: [NSString stringWithUTF8String: *ppsz_longname]];
274                     [[mycompItems lastObject] setSdtype: SD_CAT_MYCOMPUTER];
275                 break;
276             default:
277                 msg_Warn(VLCIntf, "unknown SD type found, skipping (%s)", *ppsz_name);
278                 break;
279         }
280
281         free(*ppsz_name);
282         free(*ppsz_longname);
283     }
284     [mycompItem setChildren: [NSArray arrayWithArray: mycompItems]];
285     [devicesItem setChildren: [NSArray arrayWithArray: devicesItems]];
286     [lanItem setChildren: [NSArray arrayWithArray: lanItems]];
287     [internetItem setChildren: [NSArray arrayWithArray: internetItems]];
288     [mycompItems release];
289     [devicesItems release];
290     [lanItems release];
291     [internetItems release];
292     free(ppsz_names);
293     free(ppsz_longnames);
294     free(p_categories);
295
296     [libraryItem setChildren: [NSArray arrayWithObjects: playlistItem, medialibraryItem, nil]];
297     [o_sidebaritems addObject: libraryItem];
298     if ([mycompItem hasChildren])
299         [o_sidebaritems addObject: mycompItem];
300     if ([devicesItem hasChildren])
301         [o_sidebaritems addObject: devicesItem];
302     if ([lanItem hasChildren])
303         [o_sidebaritems addObject: lanItem];
304     if ([internetItem hasChildren])
305         [o_sidebaritems addObject: internetItem];
306
307     [o_sidebar_view reloadData];
308     [o_sidebar_view selectRowIndexes:[NSIndexSet indexSetWithIndex:1] byExtendingSelection:NO];
309     [o_sidebar_view setDropItem:playlistItem dropChildIndex:NSOutlineViewDropOnItemIndex];
310     [o_sidebar_view registerForDraggedTypes:[NSArray arrayWithObjects: NSFilenamesPboardType, @"VLCPlaylistItemPboardType", nil]];
311
312     [o_sidebar_view setAutosaveName:@"mainwindow-sidebar"];
313     [(PXSourceList *)o_sidebar_view setDataSource:self];
314     [o_sidebar_view setDelegate:self];
315     [o_sidebar_view setAutosaveExpandedItems:YES];
316
317     [o_sidebar_view expandItem: libraryItem expandChildren: YES];
318
319     /* make sure we display the desired default appearance when VLC launches for the first time */
320     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
321     if (![defaults objectForKey:@"VLCFirstRun"]) {
322         [defaults setObject:[NSDate date] forKey:@"VLCFirstRun"];
323
324         NSUInteger i_sidebaritem_count = [o_sidebaritems count];
325         for (NSUInteger x = 0; x < i_sidebaritem_count; x++)
326             [o_sidebar_view expandItem: [o_sidebaritems objectAtIndex: x] expandChildren: YES];
327     }
328
329     if (b_dark_interface) {
330         [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(windowResizedOrMoved:) name: NSWindowDidResizeNotification object: nil];
331         [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(windowResizedOrMoved:) name: NSWindowDidMoveNotification object: nil];
332
333         [self setBackgroundColor: [NSColor clearColor]];
334         [self setOpaque: NO];
335         [self display];
336         [self setHasShadow:NO];
337         [self setHasShadow:YES];
338
339         NSRect winrect = [self frame];
340         CGFloat f_titleBarHeight = [o_titlebar_view frame].size.height;
341
342         [o_titlebar_view setFrame: NSMakeRect(0, winrect.size.height - f_titleBarHeight,
343                                               winrect.size.width, f_titleBarHeight)];
344         [[self contentView] addSubview: o_titlebar_view positioned: NSWindowAbove relativeTo: o_split_view];
345
346         if (winrect.size.height > 100) {
347             [self setFrame: winrect display:YES animate:YES];
348             previousSavedFrame = winrect;
349         }
350
351         winrect = [o_split_view frame];
352         winrect.size.height = winrect.size.height - f_titleBarHeight;
353         [o_split_view setFrame: winrect];
354         [o_video_view setFrame: winrect];
355
356         o_color_backdrop = [[VLCColorView alloc] initWithFrame: [o_split_view frame]];
357         [[self contentView] addSubview: o_color_backdrop positioned: NSWindowBelow relativeTo: o_split_view];
358         [o_color_backdrop setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
359
360     } else {
361         [o_video_view setFrame: [o_split_view frame]];
362         [o_playlist_table setBorderType: NSNoBorder];
363         [o_sidebar_scrollview setBorderType: NSNoBorder];
364     }
365
366     [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(someWindowWillClose:) name: NSWindowWillCloseNotification object: nil];
367     [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(someWindowWillMiniaturize:) name: NSWindowWillMiniaturizeNotification object:nil];
368     [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(applicationWillTerminate:) name: NSApplicationWillTerminateNotification object: nil];
369     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(mainSplitViewWillResizeSubviews:) name:NSSplitViewWillResizeSubviewsNotification object:o_split_view];
370
371     if (b_splitviewShouldBeHidden) {
372         [self hideSplitView];
373         i_lastSplitViewHeight = 300;
374     }
375
376     /* sanity check for the window size */
377     NSRect frame = [self frame];
378     NSSize screenSize = [[self screen] frame].size;
379     if (screenSize.width <= frame.size.width || screenSize.height <= frame.size.height) {
380         nativeVideoSize = screenSize;
381         [self resizeWindow];
382     }
383
384     /* update fs button to reflect state for next startup */
385     if (var_InheritBool(pl_Get(VLCIntf), "fullscreen"))
386         [o_controls_bar setFullscreenState:YES];
387
388     /* restore split view */
389     i_lastLeftSplitViewWidth = 200;
390     /* trick NSSplitView implementation, which pretends to know better than us */
391     if (!config_GetInt(VLCIntf, "macosx-show-sidebar"))
392         [self performSelector:@selector(toggleLeftSubSplitView) withObject:nil afterDelay:0.05];
393 }
394
395 #pragma mark -
396 #pragma mark appearance management
397
398 - (VLCMainWindowControlsBar *)controlsBar;
399 {
400     return (VLCMainWindowControlsBar *)o_controls_bar;
401 }
402
403 - (void)resizePlaylistAfterCollapse
404 {
405     NSRect plrect;
406     plrect = [o_playlist_table frame];
407     plrect.size.height = i_lastSplitViewHeight - 20.0; // actual pl top bar height, which differs from its frame
408     [[o_playlist_table animator] setFrame: plrect];
409
410     NSRect rightSplitRect;
411     rightSplitRect = [o_right_split_view frame];
412     plrect = [o_dropzone_box frame];
413     plrect.origin.x = (rightSplitRect.size.width - plrect.size.width) / 2;
414     plrect.origin.y = (rightSplitRect.size.height - plrect.size.height) / 2;
415     [[o_dropzone_box animator] setFrame: plrect];
416 }
417
418 - (void)makeSplitViewVisible
419 {
420     if (b_dark_interface)
421         [self setContentMinSize: NSMakeSize(604., 288. + [o_titlebar_view frame].size.height)];
422     else
423         [self setContentMinSize: NSMakeSize(604., 288.)];
424
425     NSRect old_frame = [self frame];
426     float newHeight = [self minSize].height;
427     if (old_frame.size.height < newHeight) {
428         NSRect new_frame = old_frame;
429         new_frame.origin.y = old_frame.origin.y + old_frame.size.height - newHeight;
430         new_frame.size.height = newHeight;
431
432         [[self animator] setFrame: new_frame display: YES animate: YES];
433     }
434
435     [o_video_view setHidden: YES];
436     [o_split_view setHidden: NO];
437     [self makeFirstResponder: nil];
438
439 }
440
441 - (void)makeSplitViewHidden
442 {
443     if (b_dark_interface)
444         [self setContentMinSize: NSMakeSize(604., f_min_video_height + [o_titlebar_view frame].size.height)];
445     else
446         [self setContentMinSize: NSMakeSize(604., f_min_video_height)];
447
448     [o_split_view setHidden: YES];
449     [o_video_view setHidden: NO];
450
451     if ([[o_video_view subviews] count] > 0)
452         [self makeFirstResponder: [[o_video_view subviews] objectAtIndex:0]];
453 }
454
455 // only exception for an controls bar button action
456 - (IBAction)togglePlaylist:(id)sender
457 {
458     if (![self isVisible] && sender != nil) {
459         [self makeKeyAndOrderFront: sender];
460         return;
461     }
462
463     BOOL b_activeVideo = [[VLCMain sharedInstance] activeVideoPlayback];
464     BOOL b_restored = NO;
465
466     // TODO: implement toggle playlist in this situation (triggerd via menu item).
467     // but for now we block this case, to avoid displaying only the half
468     if (b_nativeFullscreenMode && b_fullscreen && b_activeVideo && sender != nil)
469         return;
470
471     if (b_dropzone_active && ([[NSApp currentEvent] modifierFlags] & NSAlternateKeyMask) != 0) {
472         [self hideDropZone];
473         return;
474     }
475
476     if (!(b_nativeFullscreenMode && b_fullscreen) && !b_splitview_removed && ((([[NSApp currentEvent] modifierFlags] & NSAlternateKeyMask) != 0 && b_activeVideo)
477                                                                               || (b_nonembedded && sender != nil)
478                                                                               || (!b_activeVideo && sender != nil)
479                                                                               || b_minimized_view))
480         [self hideSplitView];
481     else {
482         if (b_splitview_removed) {
483             if (!b_nonembedded || (sender != nil && b_nonembedded))
484                 [self showSplitView];
485
486             if (sender == nil)
487                 b_minimized_view = YES;
488             else
489                 b_minimized_view = NO;
490
491             if (b_activeVideo)
492                 b_restored = YES;
493         }
494
495         if (!b_nonembedded) {
496             if (([o_video_view isHidden] && b_activeVideo) || b_restored || (b_activeVideo && sender == nil))
497                 [self makeSplitViewHidden];
498             else
499                 [self makeSplitViewVisible];
500         } else {
501             [o_split_view setHidden: NO];
502             [o_playlist_table setHidden: NO];
503             [o_video_view setHidden: YES];
504         }
505     }
506 }
507
508 - (IBAction)dropzoneButtonAction:(id)sender
509 {
510     [[[VLCMain sharedInstance] open] openFileGeneric];
511 }
512
513 #pragma mark -
514 #pragma mark overwritten default functionality
515
516 - (void)windowResizedOrMoved:(NSNotification *)notification
517 {
518     [self saveFrameUsingName: [self frameAutosaveName]];
519 }
520
521 - (void)applicationWillTerminate:(NSNotification *)notification
522 {
523     config_PutInt(VLCIntf, "macosx-show-sidebar", ![o_split_view isSubviewCollapsed:o_left_split_view]);
524
525     [self saveFrameUsingName: [self frameAutosaveName]];
526 }
527
528
529 - (void)someWindowWillClose:(NSNotification *)notification
530 {
531     id obj = [notification object];
532
533     if ([obj class] == [VLCVideoWindowCommon class] || [obj class] == [VLCDetachedVideoWindow class] || ([obj class] == [VLCMainWindow class] && !b_nonembedded)) {
534         if ([[VLCMain sharedInstance] activeVideoPlayback])
535             [[VLCCoreInteraction sharedInstance] stop];
536     }
537 }
538
539 - (void)someWindowWillMiniaturize:(NSNotification *)notification
540 {
541     if (config_GetInt(VLCIntf, "macosx-pause-minimized")) {
542         id obj = [notification object];
543
544         if ([obj class] == [VLCVideoWindowCommon class] || [obj class] == [VLCDetachedVideoWindow class] || ([obj class] == [VLCMainWindow class] && !b_nonembedded)) {
545             if ([[VLCMain sharedInstance] activeVideoPlayback])
546                 [[VLCCoreInteraction sharedInstance] pause];
547         }
548     }
549 }
550
551 #pragma mark -
552 #pragma mark Update interface and respond to foreign events
553 - (void)showDropZone
554 {
555     b_dropzone_active = YES;
556     [o_right_split_view addSubview: o_dropzone_view positioned:NSWindowAbove relativeTo:o_playlist_table];
557     [o_dropzone_view setFrame: [o_playlist_table frame]];
558     [[o_playlist_table animator] setHidden:YES];
559 }
560
561 - (void)hideDropZone
562 {
563     b_dropzone_active = NO;
564     [o_dropzone_view removeFromSuperview];
565     [[o_playlist_table animator] setHidden: NO];
566 }
567
568 - (void)hideSplitView
569 {
570     NSRect winrect = [self frame];
571     i_lastSplitViewHeight = [o_split_view frame].size.height;
572     winrect.size.height = winrect.size.height - i_lastSplitViewHeight;
573     winrect.origin.y = winrect.origin.y + i_lastSplitViewHeight;
574     [self setFrame: winrect display: YES animate: YES];
575     [self performSelector:@selector(hideDropZone) withObject:nil afterDelay:0.1];
576     if (b_dark_interface) {
577         [self setContentMinSize: NSMakeSize(604., [[o_controls_bar bottomBarView] frame].size.height + [o_titlebar_view frame].size.height)];
578         [self setContentMaxSize: NSMakeSize(FLT_MAX, [[o_controls_bar bottomBarView] frame].size.height + [o_titlebar_view frame].size.height)];
579     } else {
580         [self setContentMinSize: NSMakeSize(604., [[o_controls_bar bottomBarView] frame].size.height)];
581         [self setContentMaxSize: NSMakeSize(FLT_MAX, [[o_controls_bar bottomBarView] frame].size.height)];
582     }
583
584     b_splitview_removed = YES;
585 }
586
587 - (void)showSplitView
588 {
589     [self updateWindow];
590     if (b_dark_interface)
591         [self setContentMinSize:NSMakeSize(604., 288. + [o_titlebar_view frame].size.height)];
592     else
593         [self setContentMinSize:NSMakeSize(604., 288.)];
594     [self setContentMaxSize: NSMakeSize(FLT_MAX, FLT_MAX)];
595
596     NSRect winrect;
597     winrect = [self frame];
598     winrect.size.height = winrect.size.height + i_lastSplitViewHeight;
599     winrect.origin.y = winrect.origin.y - i_lastSplitViewHeight;
600     [self setFrame: winrect display: YES animate: YES];
601
602     [self performSelector:@selector(resizePlaylistAfterCollapse) withObject: nil afterDelay:0.75];
603
604     b_splitview_removed = NO;
605 }
606
607 - (void)updateTimeSlider
608 {
609     [o_controls_bar updateTimeSlider];
610     [[self controlsBar] updatePosAndTimeInFSPanel:o_fspanel];
611
612     [[[VLCMain sharedInstance] voutController] updateWindowsControlsBarWithSelector:@selector(updateTimeSlider)];
613 }
614
615 - (void)updateName
616 {
617     input_thread_t * p_input;
618     p_input = pl_CurrentInput(VLCIntf);
619     if (p_input) {
620         NSString *aString;
621         char *format = var_InheritString(VLCIntf, "input-title-format");
622         char *formated = str_format_meta(pl_Get(VLCIntf), format);
623         free(format);
624         aString = [NSString stringWithUTF8String:formated];
625         free(formated);
626
627         char *uri = input_item_GetURI(input_GetItem(p_input));
628
629         NSURL * o_url = [NSURL URLWithString: [NSString stringWithUTF8String: uri]];
630         if ([o_url isFileURL]) {
631             [self setRepresentedURL: o_url];
632             [[[VLCMain sharedInstance] voutController] updateWindowsUsingBlock:^(VLCVideoWindowCommon *o_window) {
633                 [o_window setRepresentedURL:o_url];
634             }];
635         } else {
636             [self setRepresentedURL: nil];
637             [[[VLCMain sharedInstance] voutController] updateWindowsUsingBlock:^(VLCVideoWindowCommon *o_window) {
638                 [o_window setRepresentedURL:nil];
639             }];
640         }
641         free(uri);
642
643         if ([aString isEqualToString:@""]) {
644             if ([o_url isFileURL])
645                 aString = [[NSFileManager defaultManager] displayNameAtPath: [o_url path]];
646             else
647                 aString = [o_url absoluteString];
648         }
649
650         if ([aString length] > 0) {
651             [self setTitle: aString];
652             [[[VLCMain sharedInstance] voutController] updateWindowsUsingBlock:^(VLCVideoWindowCommon *o_window) {
653                 [o_window setTitle:aString];
654             }];
655
656             [o_fspanel setStreamTitle: aString];
657         } else {
658            [self setTitle: _NS("VLC media player")];
659             [self setRepresentedURL: nil];
660         }
661
662         vlc_object_release(p_input);
663     } else {
664         [self setTitle: _NS("VLC media player")];
665         [self setRepresentedURL: nil];
666     }
667 }
668
669 - (void)updateWindow
670 {
671     [o_controls_bar updateControls];
672     [[[VLCMain sharedInstance] voutController] updateWindowsControlsBarWithSelector:@selector(updateControls)];
673
674     bool b_seekable = false;
675
676     playlist_t * p_playlist = pl_Get(VLCIntf);
677     input_thread_t * p_input = playlist_CurrentInput(p_playlist);
678     if (p_input) {
679         /* seekable streams */
680         b_seekable = var_GetBool(p_input, "can-seek");
681
682         vlc_object_release(p_input);
683     }
684
685     [self updateTimeSlider];
686     if ([o_fspanel respondsToSelector:@selector(setSeekable:)])
687         [o_fspanel setSeekable: b_seekable];
688
689     PL_LOCK;
690     if ([[[VLCMain sharedInstance] playlist] currentPlaylistRoot] != p_playlist->p_local_category || p_playlist->p_local_category->i_children > 0)
691         [self hideDropZone];
692     else
693         [self showDropZone];
694     PL_UNLOCK;
695     [o_sidebar_view setNeedsDisplay:YES];
696 }
697
698 - (void)setPause
699 {
700     [o_controls_bar setPause];
701     [o_fspanel setPause];
702
703     [[[VLCMain sharedInstance] voutController] updateWindowsControlsBarWithSelector:@selector(setPause)];
704 }
705
706 - (void)setPlay
707 {
708     [o_controls_bar setPlay];
709     [o_fspanel setPlay];
710
711     [[[VLCMain sharedInstance] voutController] updateWindowsControlsBarWithSelector:@selector(setPlay)];
712
713 }
714
715 - (void)updateVolumeSlider
716 {
717     [[self controlsBar] updateVolumeSlider];
718     [o_fspanel setVolumeLevel: [[VLCCoreInteraction sharedInstance] volume]];
719 }
720
721 #pragma mark -
722 #pragma mark Video Output handling
723
724 - (void)setVideoplayEnabled
725 {
726     BOOL b_videoPlayback = [[VLCMain sharedInstance] activeVideoPlayback];
727
728     if (b_videoPlayback) {
729         if (!b_fullscreen)
730             frameBeforePlayback = [self frame];
731     } else {
732         if (!b_nonembedded && !b_fullscreen && frameBeforePlayback.size.width > 0 && frameBeforePlayback.size.height > 0)
733             [[self animator] setFrame:frameBeforePlayback display:YES];
734
735         // update fs button to reflect state for next startup
736         if (var_InheritBool(pl_Get(VLCIntf), "fullscreen")) {
737             [o_controls_bar setFullscreenState:YES];
738         }
739
740         [self makeFirstResponder: nil];
741         [[[VLCMain sharedInstance] voutController] updateWindowLevelForHelperWindows: NSNormalWindowLevel];
742
743         // restore alpha value to 1 for the case that macosx-opaqueness is set to < 1
744         [self setAlphaValue:1.0];
745     }
746
747     if (b_nativeFullscreenMode) {
748         if ([NSApp presentationOptions] & NSApplicationPresentationFullScreen)
749             [[o_controls_bar bottomBarView] setHidden: b_videoPlayback];
750         else
751             [[o_controls_bar bottomBarView] setHidden: NO];
752         if (b_videoPlayback && b_fullscreen)
753             [o_fspanel setActive: nil];
754         if (!b_videoPlayback)
755             [o_fspanel setNonActive: nil];
756     }
757 }
758
759 //  Called automatically if window's acceptsMouseMovedEvents property is true
760 - (void)mouseMoved:(NSEvent *)theEvent
761 {
762     if (b_fullscreen)
763         [self recreateHideMouseTimer];
764
765     [super mouseMoved: theEvent];
766 }
767
768 - (void)recreateHideMouseTimer
769 {
770     if (t_hide_mouse_timer != nil) {
771         [t_hide_mouse_timer invalidate];
772         [t_hide_mouse_timer release];
773     }
774
775     t_hide_mouse_timer = [NSTimer scheduledTimerWithTimeInterval:2
776                                                           target:self
777                                                         selector:@selector(hideMouseCursor:)
778                                                         userInfo:nil
779                                                          repeats:NO];
780     [t_hide_mouse_timer retain];
781 }
782
783 //  NSTimer selectors require this function signature as per Apple's docs
784 - (void)hideMouseCursor:(NSTimer *)timer
785 {
786     [NSCursor setHiddenUntilMouseMoves: YES];
787 }
788
789 #pragma mark -
790 #pragma mark Lion native fullscreen handling
791 - (void)windowWillEnterFullScreen:(NSNotification *)notification
792 {
793     [super windowWillEnterFullScreen:notification];
794
795     // update split view frame after removing title bar
796     [o_split_view setFrame: [o_video_view frame]];
797 }
798
799 - (void)windowWillExitFullScreen:(NSNotification *)notification
800 {
801     [super windowWillExitFullScreen: notification];
802
803     // update split view frame after readding title bar
804     [o_split_view setFrame: [o_video_view frame]];
805 }
806 #pragma mark -
807 #pragma mark Fullscreen support
808
809 - (void)showFullscreenController
810 {
811
812     id currentWindow = [NSApp keyWindow];
813     if ([currentWindow respondsToSelector:@selector(hasActiveVideo)] && [currentWindow hasActiveVideo]) {
814         if ([currentWindow respondsToSelector:@selector(fullscreen)] && [currentWindow fullscreen]) {
815
816             if ([[VLCMain sharedInstance] activeVideoPlayback])
817                 [o_fspanel fadeIn];
818         }
819
820     }
821     
822     
823 }
824
825 - (void)makeKeyAndOrderFront: (id)sender
826 {
827     /* Hack
828      * when we exit fullscreen and fade out, we may endup in
829      * having a window that is faded. We can't have it fade in unless we
830      * animate again. */
831
832     if (!b_window_is_invisible) {
833         /* Make sure we don't do it too much */
834         [super makeKeyAndOrderFront: sender];
835         return;
836     }
837
838     [super setAlphaValue:0.0f];
839     [super makeKeyAndOrderFront: sender];
840
841     NSMutableDictionary * dict = [[NSMutableDictionary alloc] initWithCapacity:2];
842     [dict setObject:self forKey:NSViewAnimationTargetKey];
843     [dict setObject:NSViewAnimationFadeInEffect forKey:NSViewAnimationEffectKey];
844
845     o_makekey_anim = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObject:dict]];
846     [dict release];
847
848     [o_makekey_anim setAnimationBlockingMode: NSAnimationNonblocking];
849     [o_makekey_anim setDuration: 0.1];
850     [o_makekey_anim setFrameRate: 30];
851     [o_makekey_anim setDelegate: self];
852
853     [o_makekey_anim startAnimation];
854     b_window_is_invisible = NO;
855
856     /* fullscreenAnimation will be unlocked when animation ends */
857 }
858
859 #pragma mark -
860 #pragma mark split view delegate
861 - (CGFloat)splitView:(NSSplitView *)splitView constrainMaxCoordinate:(CGFloat)proposedMax ofSubviewAt:(NSInteger)dividerIndex
862 {
863     if (dividerIndex == 0)
864         return 300.;
865     else
866         return proposedMax;
867 }
868
869 - (CGFloat)splitView:(NSSplitView *)splitView constrainMinCoordinate:(CGFloat)proposedMin ofSubviewAt:(NSInteger)dividerIndex
870 {
871     if (dividerIndex == 0)
872         return 100.;
873     else
874         return proposedMin;
875 }
876
877 - (BOOL)splitView:(NSSplitView *)splitView canCollapseSubview:(NSView *)subview
878 {
879     return ([subview isEqual:o_left_split_view]);
880 }
881
882 - (BOOL)splitView:(NSSplitView *)splitView shouldAdjustSizeOfSubview:(NSView *)subview
883 {
884     if ([subview isEqual:o_left_split_view])
885         return NO;
886     return YES;
887 }
888
889 - (void)mainSplitViewWillResizeSubviews:(id)object
890 {
891     i_lastLeftSplitViewWidth = [o_left_split_view frame].size.width;
892     config_PutInt(VLCIntf, "macosx-show-sidebar", ![o_split_view isSubviewCollapsed:o_left_split_view]);
893     [[[VLCMain sharedInstance] mainMenu] updateSidebarMenuItem];
894 }
895
896 - (void)toggleLeftSubSplitView
897 {
898     [o_split_view adjustSubviews];
899     if ([o_split_view isSubviewCollapsed:o_left_split_view])
900         [o_split_view setPosition:i_lastLeftSplitViewWidth ofDividerAtIndex:0];
901     else
902         [o_split_view setPosition:[o_split_view minPossiblePositionOfDividerAtIndex:0] ofDividerAtIndex:0];
903 }
904
905 #pragma mark -
906 #pragma mark Side Bar Data handling
907 /* taken under BSD-new from the PXSourceList sample project, adapted for VLC */
908 - (NSUInteger)sourceList:(PXSourceList*)sourceList numberOfChildrenOfItem:(id)item
909 {
910     //Works the same way as the NSOutlineView data source: `nil` means a parent item
911     if (item==nil)
912         return [o_sidebaritems count];
913     else
914         return [[item children] count];
915 }
916
917
918 - (id)sourceList:(PXSourceList*)aSourceList child:(NSUInteger)index ofItem:(id)item
919 {
920     //Works the same way as the NSOutlineView data source: `nil` means a parent item
921     if (item==nil)
922         return [o_sidebaritems objectAtIndex:index];
923     else
924         return [[item children] objectAtIndex:index];
925 }
926
927
928 - (id)sourceList:(PXSourceList*)aSourceList objectValueForItem:(id)item
929 {
930     return [item title];
931 }
932
933 - (void)sourceList:(PXSourceList*)aSourceList setObjectValue:(id)object forItem:(id)item
934 {
935     [item setTitle:object];
936 }
937
938 - (BOOL)sourceList:(PXSourceList*)aSourceList isItemExpandable:(id)item
939 {
940     return [item hasChildren];
941 }
942
943
944 - (BOOL)sourceList:(PXSourceList*)aSourceList itemHasBadge:(id)item
945 {
946     if ([[item identifier] isEqualToString: @"playlist"] || [[item identifier] isEqualToString: @"medialibrary"])
947         return YES;
948
949     return [item hasBadge];
950 }
951
952
953 - (NSInteger)sourceList:(PXSourceList*)aSourceList badgeValueForItem:(id)item
954 {
955     playlist_t * p_playlist = pl_Get(VLCIntf);
956     NSInteger i_playlist_size;
957
958     if ([[item identifier] isEqualToString: @"playlist"]) {
959         PL_LOCK;
960         i_playlist_size = p_playlist->p_local_category->i_children;
961         PL_UNLOCK;
962
963         return i_playlist_size;
964     }
965     if ([[item identifier] isEqualToString: @"medialibrary"]) {
966         PL_LOCK;
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         [[[VLCMain sharedInstance] playlist] setPlaylistRoot:p_playlist->p_ml_category];
1057     } else {
1058         playlist_item_t * pl_item;
1059         PL_LOCK;
1060         pl_item = playlist_ChildSearchName(p_playlist->p_root, [[item untranslatedTitle] UTF8String]);
1061         PL_UNLOCK;
1062         [[[VLCMain sharedInstance] playlist] setPlaylistRoot: pl_item];
1063     }
1064
1065     PL_LOCK;
1066     if ([[[VLCMain sharedInstance] playlist] currentPlaylistRoot] != p_playlist->p_local_category || p_playlist->p_local_category->i_children > 0)
1067         [self hideDropZone];
1068     else
1069         [self showDropZone];
1070     PL_UNLOCK;
1071
1072     if ([[item identifier] isEqualToString:@"podcast{longname=\"Podcasts\"}"])
1073         [self showPodcastControls];
1074     else
1075         [self hidePodcastControls];
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
1185         vlc_object_t *p_obj = (vlc_object_t*)vlc_object_find_name(VLCIntf->p_libvlc, "podcast");
1186         if (p_obj) {
1187             var_SetString(p_obj, "podcast-urls", [podcastConf UTF8String]);
1188             vlc_object_release(p_obj);
1189         }
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         [urls release];
1213
1214         vlc_object_t *p_obj = (vlc_object_t*)vlc_object_find_name(VLCIntf->p_libvlc, "podcast");
1215         if (p_obj) {
1216             var_SetString(p_obj, "podcast-urls", config_GetPsz(VLCIntf, "podcast-urls"));
1217             vlc_object_release(p_obj);
1218         }
1219
1220         /* reload the podcast module, since it won't update its list when removing podcasts */
1221         playlist_t * p_playlist = pl_Get(VLCIntf);
1222         if (playlist_IsServicesDiscoveryLoaded(p_playlist, "podcast{longname=\"Podcasts\"}")) {
1223             playlist_ServicesDiscoveryRemove(p_playlist, "podcast{longname=\"Podcasts\"}");
1224             playlist_ServicesDiscoveryAdd(p_playlist, "podcast{longname=\"Podcasts\"}");
1225             [o_playlist_table reloadData];
1226         }
1227
1228     }
1229 }
1230
1231 - (void)showPodcastControls
1232 {
1233     NSRect podcastViewDimensions = [o_podcast_view frame];
1234     NSRect rightSplitRect = [o_right_split_view frame];
1235     NSRect playlistTableRect = [o_playlist_table frame];
1236
1237     podcastViewDimensions.size.width = rightSplitRect.size.width;
1238     podcastViewDimensions.origin.x = podcastViewDimensions.origin.y = .0;
1239     [o_podcast_view setFrame:podcastViewDimensions];
1240
1241     playlistTableRect.origin.y = playlistTableRect.origin.y + podcastViewDimensions.size.height;
1242     playlistTableRect.size.height = playlistTableRect.size.height - podcastViewDimensions.size.height;
1243     [o_playlist_table setFrame:playlistTableRect];
1244     [o_playlist_table setNeedsDisplay:YES];
1245
1246     [o_right_split_view addSubview: o_podcast_view positioned: NSWindowAbove relativeTo: o_right_split_view];
1247     b_podcastView_displayed = YES;
1248 }
1249
1250 - (void)hidePodcastControls
1251 {
1252     if (b_podcastView_displayed) {
1253         NSRect podcastViewDimensions = [o_podcast_view frame];
1254         NSRect playlistTableRect = [o_playlist_table frame];
1255
1256         playlistTableRect.origin.y = playlistTableRect.origin.y - podcastViewDimensions.size.height;
1257         playlistTableRect.size.height = playlistTableRect.size.height + podcastViewDimensions.size.height;
1258
1259         [o_podcast_view removeFromSuperviewWithoutNeedingDisplay];
1260         [o_playlist_table setFrame: playlistTableRect];
1261         b_podcastView_displayed = NO;
1262     }
1263 }
1264
1265 @end
1266
1267 @implementation VLCDetachedVideoWindow
1268
1269 - (void)awakeFromNib
1270 {
1271     // sets lion fullscreen behaviour
1272     [super awakeFromNib];
1273     [self setAcceptsMouseMovedEvents: YES];
1274
1275     if (b_dark_interface) {
1276         [self setBackgroundColor: [NSColor clearColor]];
1277
1278         [self setOpaque: NO];
1279         [self display];
1280         [self setHasShadow:NO];
1281         [self setHasShadow:YES];
1282
1283         NSRect winrect = [self frame];
1284         CGFloat f_titleBarHeight = [o_titlebar_view frame].size.height;
1285
1286         [self setTitle: _NS("VLC media player")];
1287         [o_titlebar_view setFrame: NSMakeRect(0, winrect.size.height - f_titleBarHeight, winrect.size.width, f_titleBarHeight)];
1288         [[self contentView] addSubview: o_titlebar_view positioned: NSWindowAbove relativeTo: nil];
1289
1290     } else {
1291         [self setBackgroundColor: [NSColor blackColor]];
1292     }
1293
1294     NSRect videoViewRect = [[self contentView] bounds];
1295     if (b_dark_interface)
1296         videoViewRect.size.height -= [o_titlebar_view frame].size.height;
1297     CGFloat f_bottomBarHeight = [[[self controlsBar] bottomBarView] frame].size.height;
1298     videoViewRect.size.height -= f_bottomBarHeight;
1299     videoViewRect.origin.y = f_bottomBarHeight;
1300     [o_video_view setFrame: videoViewRect];
1301
1302     if (b_dark_interface) {
1303         o_color_backdrop = [[VLCColorView alloc] initWithFrame: [o_video_view frame]];
1304         [[self contentView] addSubview: o_color_backdrop positioned: NSWindowBelow relativeTo: o_video_view];
1305         [o_color_backdrop setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
1306
1307         [self setContentMinSize: NSMakeSize(363., f_min_video_height + [[[self controlsBar] bottomBarView] frame].size.height + [o_titlebar_view frame].size.height)];
1308     } else {
1309         [self setContentMinSize: NSMakeSize(363., f_min_video_height + [[[self controlsBar] bottomBarView] frame].size.height)];
1310     }
1311 }
1312
1313 - (void)dealloc
1314 {
1315     if (b_dark_interface)
1316         [o_color_backdrop release];
1317
1318     [super dealloc];
1319 }
1320
1321 @end