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