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