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