]> git.sesse.net Git - vlc/blob - modules/gui/macosx/intf.m
Preliminary interaction framework for Mac OS X. Not finished, since core changes...
[vlc] / modules / gui / macosx / intf.m
1 /*****************************************************************************
2  * intf.m: MacOS X interface module
3  *****************************************************************************
4  * Copyright (C) 2002-2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
8  *          Christophe Massiot <massiot@via.ecp.fr>
9  *          Derk-Jan Hartman <hartman at videolan.org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  * 
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include <stdlib.h>                                      /* malloc(), free() */
30 #include <sys/param.h>                                    /* for MAXPATHLEN */
31 #include <string.h>
32 #include <vlc_keys.h>
33
34 #include "intf.h"
35 #include "vout.h"
36 #include "prefs.h"
37 #include "playlist.h"
38 #include "controls.h"
39 #include "about.h"
40 #include "open.h"
41 #include "wizard.h"
42 #include "extended.h"
43 #include "bookmarks.h"
44 #include "sfilters.h"
45 #include "interaction.h"
46 /*#include "update.h"*/
47
48 /*****************************************************************************
49  * Local prototypes.
50  *****************************************************************************/
51 static void Run ( intf_thread_t *p_intf );
52
53 /*****************************************************************************
54  * OpenIntf: initialize interface
55  *****************************************************************************/
56 int E_(OpenIntf) ( vlc_object_t *p_this )
57 {
58     intf_thread_t *p_intf = (intf_thread_t*) p_this;
59
60     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
61     if( p_intf->p_sys == NULL )
62     {
63         return( 1 );
64     }
65
66     memset( p_intf->p_sys, 0, sizeof( *p_intf->p_sys ) );
67
68     p_intf->p_sys->o_pool = [[NSAutoreleasePool alloc] init];
69
70     /* Put Cocoa into multithread mode as soon as possible.
71      * http://developer.apple.com/techpubs/macosx/Cocoa/
72      * TasksAndConcepts/ProgrammingTopics/Multithreading/index.html
73      * This thread does absolutely nothing at all. */
74     [NSThread detachNewThreadSelector:@selector(self) toTarget:[NSString string] withObject:nil];
75
76     p_intf->p_sys->o_sendport = [[NSPort port] retain];
77     p_intf->p_sys->p_sub = msg_Subscribe( p_intf );
78     p_intf->b_play = VLC_TRUE;
79     p_intf->pf_run = Run;
80
81     return( 0 );
82 }
83
84 /*****************************************************************************
85  * CloseIntf: destroy interface
86  *****************************************************************************/
87 void E_(CloseIntf) ( vlc_object_t *p_this )
88 {
89     intf_thread_t *p_intf = (intf_thread_t*) p_this;
90
91     msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
92
93     [p_intf->p_sys->o_sendport release];
94     [p_intf->p_sys->o_pool release];
95
96     free( p_intf->p_sys );
97 }
98
99 /*****************************************************************************
100  * Run: main loop
101  *****************************************************************************/
102 static void Run( intf_thread_t *p_intf )
103 {
104     /* Do it again - for some unknown reason, vlc_thread_create() often
105      * fails to go to real-time priority with the first launched thread
106      * (???) --Meuuh */
107     vlc_thread_set_priority( p_intf, VLC_THREAD_PRIORITY_LOW );
108     [[VLCMain sharedInstance] setIntf: p_intf];
109     [NSBundle loadNibNamed: @"MainMenu" owner: NSApp];
110     [NSApp run];
111     [[VLCMain sharedInstance] terminate];
112 }
113
114 int ExecuteOnMainThread( id target, SEL sel, void * p_arg )
115 {
116     int i_ret = 0;
117
118     //NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
119
120     if( [target respondsToSelector: @selector(performSelectorOnMainThread:
121                                              withObject:waitUntilDone:)] )
122     {
123         [target performSelectorOnMainThread: sel
124                 withObject: [NSValue valueWithPointer: p_arg]
125                 waitUntilDone: NO];
126     }
127     else if( NSApp != nil && [[VLCMain sharedInstance] respondsToSelector: @selector(getIntf)] )
128     {
129         NSValue * o_v1;
130         NSValue * o_v2;
131         NSArray * o_array;
132         NSPort * o_recv_port;
133         NSInvocation * o_inv;
134         NSPortMessage * o_msg;
135         intf_thread_t * p_intf;
136         NSConditionLock * o_lock;
137         NSMethodSignature * o_sig;
138
139         id * val[] = { &o_lock, &o_v2 };
140
141         p_intf = (intf_thread_t *)VLCIntf;
142
143         o_recv_port = [[NSPort port] retain];
144         o_v1 = [NSValue valueWithPointer: val];
145         o_v2 = [NSValue valueWithPointer: p_arg];
146
147         o_sig = [target methodSignatureForSelector: sel];
148         o_inv = [NSInvocation invocationWithMethodSignature: o_sig];
149         [o_inv setArgument: &o_v1 atIndex: 2];
150         [o_inv setTarget: target];
151         [o_inv setSelector: sel];
152
153         o_array = [NSArray arrayWithObject:
154             [NSData dataWithBytes: &o_inv length: sizeof(o_inv)]];
155         o_msg = [[NSPortMessage alloc]
156             initWithSendPort: p_intf->p_sys->o_sendport
157             receivePort: o_recv_port components: o_array];
158
159         o_lock = [[NSConditionLock alloc] initWithCondition: 0];
160         [o_msg sendBeforeDate: [NSDate distantPast]];
161         [o_lock lockWhenCondition: 1];
162         [o_lock unlock];
163         [o_lock release];
164
165         [o_msg release];
166         [o_recv_port release];
167     }
168     else
169     {
170         i_ret = 1;
171     }
172
173     //[o_pool release];
174
175     return( i_ret );
176 }
177
178 /*****************************************************************************
179  * playlistChanged: Callback triggered by the intf-change playlist
180  * variable, to let the intf update the playlist.
181  *****************************************************************************/
182 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
183                      vlc_value_t old_val, vlc_value_t new_val, void *param )
184 {
185     intf_thread_t * p_intf = VLCIntf;
186     p_intf->p_sys->b_playlist_update = TRUE;
187     p_intf->p_sys->b_intf_update = TRUE;
188     p_intf->p_sys->b_playmode_update = TRUE;
189     return VLC_SUCCESS;
190 }
191
192 /*****************************************************************************
193  * FullscreenChanged: Callback triggered by the fullscreen-change playlist
194  * variable, to let the intf update the controller.
195  *****************************************************************************/
196 static int FullscreenChanged( vlc_object_t *p_this, const char *psz_variable,
197                      vlc_value_t old_val, vlc_value_t new_val, void *param )
198 {
199     intf_thread_t * p_intf = VLCIntf;
200     p_intf->p_sys->b_fullscreen_update = TRUE;
201     return VLC_SUCCESS;
202 }
203
204 /*****************************************************************************
205  * InteractCallback: Callback triggered by the interaction
206  * variable, to let the intf display error and interaction dialogs
207  *****************************************************************************/
208 static int InteractCallback( vlc_object_t *p_this, const char *psz_variable,
209                      vlc_value_t old_val, vlc_value_t new_val, void *param )
210 {
211     NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
212     VLCMain *interface = (VLCMain *)param;
213     interaction_dialog_t *p_dialog = (interaction_dialog_t *)(new_val.p_address);
214     NSValue *o_value = [NSValue valueWithPointer:p_dialog];
215     
216     [[NSNotificationCenter defaultCenter] postNotificationName: @"VLCNewInteractionEventNotification" object:[interface getInteractionList]
217      userInfo:[NSDictionary dictionaryWithObject:o_value forKey:@"VLCDialogPointer"]];
218     
219     [o_pool release];
220     return VLC_SUCCESS;
221 }
222
223 static struct
224 {
225     unichar i_nskey;
226     unsigned int i_vlckey;
227 } nskeys_to_vlckeys[] =
228 {
229     { NSUpArrowFunctionKey, KEY_UP },
230     { NSDownArrowFunctionKey, KEY_DOWN },
231     { NSLeftArrowFunctionKey, KEY_LEFT },
232     { NSRightArrowFunctionKey, KEY_RIGHT },
233     { NSF1FunctionKey, KEY_F1 },
234     { NSF2FunctionKey, KEY_F2 },
235     { NSF3FunctionKey, KEY_F3 },
236     { NSF4FunctionKey, KEY_F4 },
237     { NSF5FunctionKey, KEY_F5 },
238     { NSF6FunctionKey, KEY_F6 },
239     { NSF7FunctionKey, KEY_F7 },
240     { NSF8FunctionKey, KEY_F8 },
241     { NSF9FunctionKey, KEY_F9 },
242     { NSF10FunctionKey, KEY_F10 },
243     { NSF11FunctionKey, KEY_F11 },
244     { NSF12FunctionKey, KEY_F12 },
245     { NSHomeFunctionKey, KEY_HOME },
246     { NSEndFunctionKey, KEY_END },
247     { NSPageUpFunctionKey, KEY_PAGEUP },
248     { NSPageDownFunctionKey, KEY_PAGEDOWN },
249     { NSTabCharacter, KEY_TAB },
250     { NSCarriageReturnCharacter, KEY_ENTER },
251     { NSEnterCharacter, KEY_ENTER },
252     { NSBackspaceCharacter, KEY_BACKSPACE },
253     { (unichar) ' ', KEY_SPACE },
254     { (unichar) 0x1b, KEY_ESC },
255     {0,0}
256 };
257
258 unichar VLCKeyToCocoa( unsigned int i_key )
259 {
260     unsigned int i;
261
262     for( i = 0; nskeys_to_vlckeys[i].i_vlckey != 0; i++ )
263     {
264         if( nskeys_to_vlckeys[i].i_vlckey == (i_key & ~KEY_MODIFIER) )
265         {
266             return nskeys_to_vlckeys[i].i_nskey;
267         }
268     }
269     return (unichar)(i_key & ~KEY_MODIFIER);
270 }
271
272 unsigned int CocoaKeyToVLC( unichar i_key )
273 {
274     unsigned int i;
275
276     for( i = 0; nskeys_to_vlckeys[i].i_nskey != 0; i++ )
277     {
278         if( nskeys_to_vlckeys[i].i_nskey == i_key )
279         {
280             return nskeys_to_vlckeys[i].i_vlckey;
281         }
282     }
283     return (unsigned int)i_key;
284 }
285
286 unsigned int VLCModifiersToCocoa( unsigned int i_key )
287 {
288     unsigned int new = 0;
289     if( i_key & KEY_MODIFIER_COMMAND )
290         new |= NSCommandKeyMask;
291     if( i_key & KEY_MODIFIER_ALT )
292         new |= NSAlternateKeyMask;
293     if( i_key & KEY_MODIFIER_SHIFT )
294         new |= NSShiftKeyMask;
295     if( i_key & KEY_MODIFIER_CTRL )
296         new |= NSControlKeyMask;
297     return new;
298 }
299
300 /*****************************************************************************
301  * VLCMain implementation
302  *****************************************************************************/
303 @implementation VLCMain
304
305 static VLCMain *_o_sharedMainInstance = nil;
306
307 + (VLCMain *)sharedInstance
308 {
309     return _o_sharedMainInstance ? _o_sharedMainInstance : [[self alloc] init];
310 }
311
312 - (id)init
313 {
314     if( _o_sharedMainInstance) {
315         [self dealloc];
316     } else {
317         _o_sharedMainInstance = [super init];
318     }
319     
320     o_about = [[VLAboutBox alloc] init];
321     o_prefs = nil;
322     o_open = [[VLCOpen alloc] init];
323     o_wizard = [[VLCWizard alloc] init];
324     o_extended = nil;
325     o_bookmarks = [[VLCBookmarks alloc] init];
326     o_embedded_list = [[VLCEmbeddedList alloc] init];
327     o_interaction_list = [[VLCInteractionList alloc] init];
328     o_sfilters = nil;
329     /*o_update = [[VLCUpdate alloc] init];*/
330
331     i_lastShownVolume = -1;
332     return _o_sharedMainInstance;
333 }
334
335 - (void)setIntf: (intf_thread_t *)p_mainintf {
336     p_intf = p_mainintf;
337 }
338
339 - (intf_thread_t *)getIntf {
340     return p_intf;
341 }
342
343 - (void)awakeFromNib
344 {
345     unsigned int i_key = 0;
346     playlist_t *p_playlist;
347     vlc_value_t val;
348
349     /* Check if we already did this once. Opening the other nibs calls it too, because VLCMain is the owner */
350     if( nib_main_loaded ) return;
351
352     [self initStrings];
353     [o_window setExcludedFromWindowsMenu: TRUE];
354     [o_msgs_panel setExcludedFromWindowsMenu: TRUE];
355     [o_msgs_panel setDelegate: self];
356
357     i_key = config_GetInt( p_intf, "key-quit" );
358     [o_mi_quit setKeyEquivalent: [NSString stringWithFormat:@"%C", VLCKeyToCocoa( i_key )]];
359     [o_mi_quit setKeyEquivalentModifierMask: VLCModifiersToCocoa(i_key)];
360     i_key = config_GetInt( p_intf, "key-play-pause" );
361     [o_mi_play setKeyEquivalent: [NSString stringWithFormat:@"%C", VLCKeyToCocoa( i_key )]];
362     [o_mi_play setKeyEquivalentModifierMask: VLCModifiersToCocoa(i_key)];
363     i_key = config_GetInt( p_intf, "key-stop" );
364     [o_mi_stop setKeyEquivalent: [NSString stringWithFormat:@"%C", VLCKeyToCocoa( i_key )]];
365     [o_mi_stop setKeyEquivalentModifierMask: VLCModifiersToCocoa(i_key)];
366     i_key = config_GetInt( p_intf, "key-faster" );
367     [o_mi_faster setKeyEquivalent: [NSString stringWithFormat:@"%C", VLCKeyToCocoa( i_key )]];
368     [o_mi_faster setKeyEquivalentModifierMask: VLCModifiersToCocoa(i_key)];
369     i_key = config_GetInt( p_intf, "key-slower" );
370     [o_mi_slower setKeyEquivalent: [NSString stringWithFormat:@"%C", VLCKeyToCocoa( i_key )]];
371     [o_mi_slower setKeyEquivalentModifierMask: VLCModifiersToCocoa(i_key)];
372     i_key = config_GetInt( p_intf, "key-prev" );
373     [o_mi_previous setKeyEquivalent: [NSString stringWithFormat:@"%C", VLCKeyToCocoa( i_key )]];
374     [o_mi_previous setKeyEquivalentModifierMask: VLCModifiersToCocoa(i_key)];
375     i_key = config_GetInt( p_intf, "key-next" );
376     [o_mi_next setKeyEquivalent: [NSString stringWithFormat:@"%C", VLCKeyToCocoa( i_key )]];
377     [o_mi_next setKeyEquivalentModifierMask: VLCModifiersToCocoa(i_key)];
378     i_key = config_GetInt( p_intf, "key-jump+short" );
379     [o_mi_fwd setKeyEquivalent: [NSString stringWithFormat:@"%C", VLCKeyToCocoa( i_key )]];
380     [o_mi_fwd setKeyEquivalentModifierMask: VLCModifiersToCocoa(i_key)];
381     i_key = config_GetInt( p_intf, "key-jump-short" );
382     [o_mi_bwd setKeyEquivalent: [NSString stringWithFormat:@"%C", VLCKeyToCocoa( i_key )]];
383     [o_mi_bwd setKeyEquivalentModifierMask: VLCModifiersToCocoa(i_key)];
384     i_key = config_GetInt( p_intf, "key-jump+medium" );
385     [o_mi_fwd1m setKeyEquivalent: [NSString stringWithFormat:@"%C", VLCKeyToCocoa( i_key )]];
386     [o_mi_fwd1m setKeyEquivalentModifierMask: VLCModifiersToCocoa(i_key)];
387     i_key = config_GetInt( p_intf, "key-jump-medium" );
388     [o_mi_bwd1m setKeyEquivalent: [NSString stringWithFormat:@"%C", VLCKeyToCocoa( i_key )]];
389     [o_mi_bwd1m setKeyEquivalentModifierMask: VLCModifiersToCocoa(i_key)];
390     i_key = config_GetInt( p_intf, "key-jump+long" );
391     [o_mi_fwd5m setKeyEquivalent: [NSString stringWithFormat:@"%C", VLCKeyToCocoa( i_key )]];
392     [o_mi_fwd5m setKeyEquivalentModifierMask: VLCModifiersToCocoa(i_key)];
393     i_key = config_GetInt( p_intf, "key-jump-long" );
394     [o_mi_bwd5m setKeyEquivalent: [NSString stringWithFormat:@"%C", VLCKeyToCocoa( i_key )]];
395     [o_mi_bwd5m setKeyEquivalentModifierMask: VLCModifiersToCocoa(i_key)];
396     i_key = config_GetInt( p_intf, "key-vol-up" );
397     [o_mi_vol_up setKeyEquivalent: [NSString stringWithFormat:@"%C", VLCKeyToCocoa( i_key )]];
398     [o_mi_vol_up setKeyEquivalentModifierMask: VLCModifiersToCocoa(i_key)];
399     i_key = config_GetInt( p_intf, "key-vol-down" );
400     [o_mi_vol_down setKeyEquivalent: [NSString stringWithFormat:@"%C", VLCKeyToCocoa( i_key )]];
401     [o_mi_vol_down setKeyEquivalentModifierMask: VLCModifiersToCocoa(i_key)];
402     i_key = config_GetInt( p_intf, "key-vol-mute" );
403     [o_mi_mute setKeyEquivalent: [NSString stringWithFormat:@"%C", VLCKeyToCocoa( i_key )]];
404     [o_mi_mute setKeyEquivalentModifierMask: VLCModifiersToCocoa(i_key)];
405     i_key = config_GetInt( p_intf, "key-fullscreen" );
406     [o_mi_fullscreen setKeyEquivalent: [NSString stringWithFormat:@"%C", VLCKeyToCocoa( i_key )]];
407     [o_mi_fullscreen setKeyEquivalentModifierMask: VLCModifiersToCocoa(i_key)];
408     i_key = config_GetInt( p_intf, "key-snapshot" );
409     [o_mi_snapshot setKeyEquivalent: [NSString stringWithFormat:@"%C", VLCKeyToCocoa( i_key )]];
410     [o_mi_snapshot setKeyEquivalentModifierMask: VLCModifiersToCocoa(i_key)];
411
412     var_Create( p_intf, "intf-change", VLC_VAR_BOOL );
413
414     [self setSubmenusEnabled: FALSE];
415     [self manageVolumeSlider];
416     [o_window setDelegate: self];
417     
418     if( [o_window frame].size.height <= 200 )
419     {
420         b_small_window = YES;
421         [o_window setFrame: NSMakeRect( [o_window frame].origin.x,
422             [o_window frame].origin.y, [o_window frame].size.width,
423             [o_window minSize].height ) display: YES animate:YES];
424         [o_playlist_view setAutoresizesSubviews: NO];
425     }
426     else
427     {
428         b_small_window = NO;
429         [o_playlist_view setFrame: NSMakeRect( 10, 10, [o_window frame].size.width - 20, [o_window frame].size.height - 105 )];
430         [o_playlist_view setNeedsDisplay:YES];
431         [o_playlist_view setAutoresizesSubviews: YES];
432         [[o_window contentView] addSubview: o_playlist_view];
433     }
434     [self updateTogglePlaylistState];
435
436     o_size_with_playlist = [o_window frame].size;
437
438     p_playlist = (playlist_t *) vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
439
440     if( p_playlist )
441     {
442         /* Check if we need to start playing */
443         if( p_intf->b_play )
444         {
445             playlist_LockControl( p_playlist, PLAYLIST_AUTOPLAY );
446         }
447         var_Create( p_playlist, "fullscreen", VLC_VAR_BOOL | VLC_VAR_DOINHERIT);
448         val.b_bool = VLC_FALSE;
449
450         var_AddCallback( p_playlist, "fullscreen", FullscreenChanged, self);
451
452         [o_btn_fullscreen setState: ( var_Get( p_playlist, "fullscreen", &val )>=0 && val.b_bool )];
453         vlc_object_release( p_playlist );
454     }
455     
456     var_Create( p_intf, "interaction", VLC_VAR_ADDRESS );
457     var_AddCallback( p_intf, "interaction", InteractCallback, self );
458     p_intf->b_interaction = VLC_TRUE;
459     
460     nib_main_loaded = TRUE;
461 }
462
463 - (void)initStrings
464 {
465     [o_window setTitle: _NS("VLC - Controller")];
466     [self setScrollField:_NS("VLC media player") stopAfter:-1];
467
468     /* button controls */
469     [o_btn_prev setToolTip: _NS("Previous")];
470     [o_btn_rewind setToolTip: _NS("Rewind")];
471     [o_btn_play setToolTip: _NS("Play")];
472     [o_btn_stop setToolTip: _NS("Stop")];
473     [o_btn_ff setToolTip: _NS("Fast Forward")];
474     [o_btn_next setToolTip: _NS("Next")];
475     [o_btn_fullscreen setToolTip: _NS("Fullscreen")];
476     [o_volumeslider setToolTip: _NS("Volume")];
477     [o_timeslider setToolTip: _NS("Position")];
478     [o_btn_playlist setToolTip: _NS("Playlist")];
479
480     /* messages panel */
481     [o_msgs_panel setTitle: _NS("Messages")];
482     [o_msgs_btn_crashlog setTitle: _NS("Open CrashLog")];
483
484     /* main menu */
485     [o_mi_about setTitle: [_NS("About VLC media player") \
486         stringByAppendingString: @"..."]];
487 /*    [o_mi_checkForUpdate setTitle: _NS("Check for Update...")];*/
488     [o_mi_prefs setTitle: _NS("Preferences...")];
489     [o_mi_add_intf setTitle: _NS("Add Interface")];
490     [o_mu_add_intf setTitle: _NS("Add Interface")];
491     [o_mi_services setTitle: _NS("Services")];
492     [o_mi_hide setTitle: _NS("Hide VLC")];
493     [o_mi_hide_others setTitle: _NS("Hide Others")];
494     [o_mi_show_all setTitle: _NS("Show All")];
495     [o_mi_quit setTitle: _NS("Quit VLC")];
496
497     [o_mu_file setTitle: _ANS("1:File")];
498     [o_mi_open_generic setTitle: _NS("Open File...")];
499     [o_mi_open_file setTitle: _NS("Quick Open File...")];
500     [o_mi_open_disc setTitle: _NS("Open Disc...")];
501     [o_mi_open_net setTitle: _NS("Open Network...")];
502     [o_mi_open_recent setTitle: _NS("Open Recent")];
503     [o_mi_open_recent_cm setTitle: _NS("Clear Menu")];
504     [o_mi_open_wizard setTitle: _NS("Streaming/Exporting Wizard...")];
505
506     [o_mu_edit setTitle: _NS("Edit")];
507     [o_mi_cut setTitle: _NS("Cut")];
508     [o_mi_copy setTitle: _NS("Copy")];
509     [o_mi_paste setTitle: _NS("Paste")];
510     [o_mi_clear setTitle: _NS("Clear")];
511     [o_mi_select_all setTitle: _NS("Select All")];
512
513     [o_mu_controls setTitle: _NS("Playback")];
514     [o_mi_play setTitle: _NS("Play")];
515     [o_mi_stop setTitle: _NS("Stop")];
516     [o_mi_faster setTitle: _NS("Faster")];
517     [o_mi_slower setTitle: _NS("Slower")];
518     [o_mi_previous setTitle: _NS("Previous")];
519     [o_mi_next setTitle: _NS("Next")];
520     [o_mi_random setTitle: _NS("Random")];
521     [o_mi_repeat setTitle: _NS("Repeat One")];
522     [o_mi_loop setTitle: _NS("Repeat All")];
523     [o_mi_fwd setTitle: _NS("Step Forward")];
524     [o_mi_bwd setTitle: _NS("Step Backward")];
525
526     [o_mi_program setTitle: _NS("Program")];
527     [o_mu_program setTitle: _NS("Program")];
528     [o_mi_title setTitle: _NS("Title")];
529     [o_mu_title setTitle: _NS("Title")];
530     [o_mi_chapter setTitle: _NS("Chapter")];
531     [o_mu_chapter setTitle: _NS("Chapter")];
532
533     [o_mu_audio setTitle: _NS("Audio")];
534     [o_mi_vol_up setTitle: _NS("Volume Up")];
535     [o_mi_vol_down setTitle: _NS("Volume Down")];
536     [o_mi_mute setTitle: _NS("Mute")];
537     [o_mi_audiotrack setTitle: _NS("Audio Track")];
538     [o_mu_audiotrack setTitle: _NS("Audio Track")];
539     [o_mi_channels setTitle: _NS("Audio Channels")];
540     [o_mu_channels setTitle: _NS("Audio Channels")];
541     [o_mi_device setTitle: _NS("Audio Device")];
542     [o_mu_device setTitle: _NS("Audio Device")];
543     [o_mi_visual setTitle: _NS("Visualizations")];
544     [o_mu_visual setTitle: _NS("Visualizations")];
545
546     [o_mu_video setTitle: _NS("Video")];
547     [o_mi_half_window setTitle: _NS("Half Size")];
548     [o_mi_normal_window setTitle: _NS("Normal Size")];
549     [o_mi_double_window setTitle: _NS("Double Size")];
550     [o_mi_fittoscreen setTitle: _NS("Fit to Screen")];
551     [o_mi_fullscreen setTitle: _NS("Fullscreen")];
552     [o_mi_floatontop setTitle: _NS("Float on Top")];
553     [o_mi_snapshot setTitle: _NS("Snapshot")];
554     [o_mi_videotrack setTitle: _NS("Video Track")];
555     [o_mu_videotrack setTitle: _NS("Video Track")];
556     [o_mi_screen setTitle: _NS("Video Device")];
557     [o_mu_screen setTitle: _NS("Video Device")];
558     [o_mi_subtitle setTitle: _NS("Subtitles Track")];
559     [o_mu_subtitle setTitle: _NS("Subtitles Track")];
560     [o_mi_deinterlace setTitle: _NS("Deinterlace")];
561     [o_mu_deinterlace setTitle: _NS("Deinterlace")];
562     [o_mi_ffmpeg_pp setTitle: _NS("Post processing")];
563     [o_mu_ffmpeg_pp setTitle: _NS("Post processing")];
564
565     [o_mu_window setTitle: _NS("Window")];
566     [o_mi_minimize setTitle: _NS("Minimize Window")];
567     [o_mi_close_window setTitle: _NS("Close Window")];
568     [o_mi_controller setTitle: _NS("Controller")];
569     [o_mi_equalizer setTitle: _NS("Equalizer")];
570     [o_mi_extended setTitle: _NS("Extended Controls")];
571     [o_mi_bookmarks setTitle: _NS("Bookmarks")];
572     [o_mi_playlist setTitle: _NS("Playlist")];
573     [o_mi_info setTitle: _NS("Info")];
574     [o_mi_messages setTitle: _NS("Messages")];
575
576     [o_mi_bring_atf setTitle: _NS("Bring All to Front")];
577
578     [o_mu_help setTitle: _NS("Help")];
579     [o_mi_readme setTitle: _NS("ReadMe...")];
580     [o_mi_documentation setTitle: _NS("Online Documentation")];
581     [o_mi_reportabug setTitle: _NS("Report a Bug")];
582     [o_mi_website setTitle: _NS("VideoLAN Website")];
583     [o_mi_license setTitle: _NS("License")];
584     [o_mi_donation setTitle: _NS("Make a donation")];
585     [o_mi_forum setTitle: _NS("Online Forum")];
586
587     /* dock menu */
588     [o_dmi_play setTitle: _NS("Play")];
589     [o_dmi_stop setTitle: _NS("Stop")];
590     [o_dmi_next setTitle: _NS("Next")];
591     [o_dmi_previous setTitle: _NS("Previous")];
592     [o_dmi_mute setTitle: _NS("Mute")];
593
594     /* error panel */
595     [o_error setTitle: _NS("Error")];
596     [o_err_lbl setStringValue: _NS("An error has occurred which probably " \
597         "prevented the execution of your request:")];
598     [o_err_bug_lbl setStringValue: _NS("If you believe that it is a bug, " \
599         "please follow the instructions at:")];
600     [o_err_btn_msgs setTitle: _NS("Open Messages Window")];
601     [o_err_btn_dismiss setTitle: _NS("Dismiss")];
602     [o_err_ckbk_surpress setTitle: _NS("Suppress further errors")];
603
604     [o_info_window setTitle: _NS("Info")];
605 }
606
607 - (void)applicationWillFinishLaunching:(NSNotification *)o_notification
608 {
609     o_msg_lock = [[NSLock alloc] init];
610     o_msg_arr = [[NSMutableArray arrayWithCapacity: 200] retain];
611
612     o_img_play = [[NSImage imageNamed: @"play"] retain];
613     o_img_play_pressed = [[NSImage imageNamed: @"play_blue"] retain];
614     o_img_pause = [[NSImage imageNamed: @"pause"] retain];
615     o_img_pause_pressed = [[NSImage imageNamed: @"pause_blue"] retain];
616
617     [p_intf->p_sys->o_sendport setDelegate: self];
618     [[NSRunLoop currentRunLoop]
619         addPort: p_intf->p_sys->o_sendport
620         forMode: NSDefaultRunLoopMode];
621
622     [NSTimer scheduledTimerWithTimeInterval: 0.5
623         target: self selector: @selector(manageIntf:)
624         userInfo: nil repeats: FALSE];
625
626     [NSThread detachNewThreadSelector: @selector(manage)
627         toTarget: self withObject: nil];
628
629     [o_controls setupVarMenuItem: o_mi_add_intf target: (vlc_object_t *)p_intf
630         var: "intf-add" selector: @selector(toggleVar:)];
631
632     vlc_thread_set_priority( p_intf, VLC_THREAD_PRIORITY_LOW );
633 }
634
635 - (BOOL)application:(NSApplication *)o_app openFile:(NSString *)o_filename
636 {
637     NSDictionary *o_dic = [NSDictionary dictionaryWithObjectsAndKeys: o_filename, @"ITEM_URL", nil];
638     [o_playlist appendArray:
639         [NSArray arrayWithObject: o_dic] atPos: -1 enqueue: NO];
640
641     return( TRUE );
642 }
643
644 - (NSString *)localizedString:(char *)psz
645 {
646     NSString * o_str = nil;
647
648     if( psz != NULL )
649     {
650         o_str = [[[NSString alloc] initWithUTF8String: psz] autorelease];
651     }
652     if ( o_str == NULL )
653     {
654         msg_Err( VLCIntf, "could not translate: %s", psz );
655     }
656
657     return( o_str );
658 }
659
660 - (char *)delocalizeString:(NSString *)id
661 {
662     NSData * o_data = [id dataUsingEncoding: NSUTF8StringEncoding
663                           allowLossyConversion: NO];
664     char * psz_string;
665
666     if ( o_data == nil )
667     {
668         o_data = [id dataUsingEncoding: NSUTF8StringEncoding
669                      allowLossyConversion: YES];
670         psz_string = malloc( [o_data length] + 1 );
671         [o_data getBytes: psz_string];
672         psz_string[ [o_data length] ] = '\0';
673         msg_Err( VLCIntf, "cannot convert to wanted encoding: %s",
674                  psz_string );
675     }
676     else
677     {
678         psz_string = malloc( [o_data length] + 1 );
679         [o_data getBytes: psz_string];
680         psz_string[ [o_data length] ] = '\0';
681     }
682
683     return psz_string;
684 }
685
686 /* i_width is in pixels */
687 - (NSString *)wrapString: (NSString *)o_in_string toWidth: (int) i_width
688 {
689     NSMutableString *o_wrapped;
690     NSString *o_out_string;
691     NSRange glyphRange, effectiveRange, charRange;
692     NSRect lineFragmentRect;
693     unsigned glyphIndex, breaksInserted = 0;
694
695     NSTextStorage *o_storage = [[NSTextStorage alloc] initWithString: o_in_string
696         attributes: [NSDictionary dictionaryWithObjectsAndKeys:
697         [NSFont labelFontOfSize: 0.0], NSFontAttributeName, nil]];
698     NSLayoutManager *o_layout_manager = [[NSLayoutManager alloc] init];
699     NSTextContainer *o_container = [[NSTextContainer alloc]
700         initWithContainerSize: NSMakeSize(i_width, 2000)];
701
702     [o_layout_manager addTextContainer: o_container];
703     [o_container release];
704     [o_storage addLayoutManager: o_layout_manager];
705     [o_layout_manager release];
706
707     o_wrapped = [o_in_string mutableCopy];
708     glyphRange = [o_layout_manager glyphRangeForTextContainer: o_container];
709
710     for( glyphIndex = glyphRange.location ; glyphIndex < NSMaxRange(glyphRange) ;
711             glyphIndex += effectiveRange.length) {
712         lineFragmentRect = [o_layout_manager lineFragmentRectForGlyphAtIndex: glyphIndex
713                                             effectiveRange: &effectiveRange];
714         charRange = [o_layout_manager characterRangeForGlyphRange: effectiveRange
715                                     actualGlyphRange: &effectiveRange];
716         if ([o_wrapped lineRangeForRange:
717                 NSMakeRange(charRange.location + breaksInserted, charRange.length)].length > charRange.length) {
718             [o_wrapped insertString: @"\n" atIndex: NSMaxRange(charRange) + breaksInserted];
719             breaksInserted++;
720         }
721     }
722     o_out_string = [NSString stringWithString: o_wrapped];
723     [o_wrapped release];
724     [o_storage release];
725
726     return o_out_string;
727 }
728
729
730 /*****************************************************************************
731  * hasDefinedShortcutKey: Check to see if the key press is a defined VLC
732  * shortcut key.  If it is, pass it off to VLC for handling and return YES,
733  * otherwise ignore it and return NO (where it will get handled by Cocoa).
734  *****************************************************************************/
735 - (BOOL)hasDefinedShortcutKey:(NSEvent *)o_event
736 {
737     unichar key = 0;
738     vlc_value_t val;
739     unsigned int i_pressed_modifiers = 0;
740     struct hotkey *p_hotkeys;
741     int i;
742
743     val.i_int = 0;
744     p_hotkeys = p_intf->p_vlc->p_hotkeys;
745
746     i_pressed_modifiers = [o_event modifierFlags];
747
748     if( i_pressed_modifiers & NSShiftKeyMask )
749         val.i_int |= KEY_MODIFIER_SHIFT;
750     if( i_pressed_modifiers & NSControlKeyMask )
751         val.i_int |= KEY_MODIFIER_CTRL;
752     if( i_pressed_modifiers & NSAlternateKeyMask )
753         val.i_int |= KEY_MODIFIER_ALT;
754     if( i_pressed_modifiers & NSCommandKeyMask )
755         val.i_int |= KEY_MODIFIER_COMMAND;
756
757     key = [[o_event charactersIgnoringModifiers] characterAtIndex: 0];
758
759     switch( key )
760     {
761         case NSDeleteCharacter:
762         case NSDeleteFunctionKey:
763         case NSDeleteCharFunctionKey:
764         case NSBackspaceCharacter:
765         case NSUpArrowFunctionKey:
766         case NSDownArrowFunctionKey:
767         case NSRightArrowFunctionKey:
768         case NSLeftArrowFunctionKey:
769         case NSEnterCharacter:
770         case NSCarriageReturnCharacter:
771             return NO;
772     }
773
774     val.i_int |= CocoaKeyToVLC( key );
775
776     for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
777     {
778         if( p_hotkeys[i].i_key == val.i_int )
779         {
780             var_Set( p_intf->p_vlc, "key-pressed", val );
781             return YES;
782         }
783     }
784
785     return NO;
786 }
787
788 - (id)getControls
789 {
790     if ( o_controls )
791     {
792         return o_controls;
793     }
794     return nil;
795 }
796
797 - (id)getPlaylist
798 {
799     if ( o_playlist )
800     {
801         return o_playlist;
802     }
803     return nil;
804 }
805
806 - (id)getInfo
807 {
808     if ( o_info )
809     {
810         return o_info;
811     }
812     return nil;
813 }
814
815 - (id)getWizard
816 {
817     if ( o_wizard )
818     {
819         return o_wizard;
820     }
821     return nil;
822 }
823
824 - (id)getBookmarks
825 {
826     if ( o_bookmarks )
827     {
828         return o_bookmarks;
829     }
830     return nil;
831 }
832
833 - (id)getEmbeddedList
834 {
835     if( o_embedded_list )
836     {
837         return o_embedded_list;
838     }
839     return nil;
840 }
841
842 - (id)getInteractionList
843 {
844     if( o_interaction_list )
845     {
846         return o_interaction_list;
847     }
848     return nil;
849 }
850
851 - (void)manage
852 {
853     playlist_t * p_playlist;
854
855     /* new thread requires a new pool */
856     NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
857
858     vlc_thread_set_priority( p_intf, VLC_THREAD_PRIORITY_LOW );
859
860     p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
861                                               FIND_ANYWHERE );
862
863     if( p_playlist != NULL )
864     {
865         var_AddCallback( p_playlist, "intf-change", PlaylistChanged, self );
866         var_AddCallback( p_playlist, "item-change", PlaylistChanged, self );
867         var_AddCallback( p_playlist, "item-append", PlaylistChanged, self );
868         var_AddCallback( p_playlist, "item-deleted", PlaylistChanged, self );
869         var_AddCallback( p_playlist, "playlist-current", PlaylistChanged, self );
870
871         vlc_object_release( p_playlist );
872     }
873
874     while( !p_intf->b_die )
875     {
876         vlc_mutex_lock( &p_intf->change_lock );
877
878 #define p_input p_intf->p_sys->p_input
879
880         if( p_input == NULL )
881         {
882             p_input = (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
883                                            FIND_ANYWHERE );
884
885             /* Refresh the interface */
886             if( p_input )
887             {
888                 msg_Dbg( p_intf, "input has changed, refreshing interface" );
889                 p_intf->p_sys->b_input_update = VLC_TRUE;
890             }
891         }
892         else if( p_input->b_die || p_input->b_dead )
893         {
894             /* input stopped */
895             p_intf->p_sys->b_intf_update = VLC_TRUE;
896             p_intf->p_sys->i_play_status = END_S;
897             [self setScrollField: _NS("VLC media player") stopAfter:-1];
898             vlc_object_release( p_input );
899             p_input = NULL;
900         }
901 #undef p_input
902
903         /* Manage volume status */
904         [self manageVolumeSlider];
905
906         vlc_mutex_unlock( &p_intf->change_lock );
907         msleep( 100000 );
908     }
909     [o_pool release];
910 }
911
912 - (void)manageIntf:(NSTimer *)o_timer
913 {
914     vlc_value_t val;
915
916     if( p_intf->p_vlc->b_die == VLC_TRUE )
917     {
918         [o_timer invalidate];
919         return;
920     }
921
922 #define p_input p_intf->p_sys->p_input
923     if( p_intf->p_sys->b_input_update )
924     {
925         /* Called when new input is opened */
926         p_intf->p_sys->b_current_title_update = VLC_TRUE;
927         p_intf->p_sys->b_intf_update = VLC_TRUE;
928         p_intf->p_sys->b_input_update = VLC_FALSE;
929     }
930     if( p_intf->p_sys->b_intf_update )
931     {
932         vlc_bool_t b_input = VLC_FALSE;
933         vlc_bool_t b_plmul = VLC_FALSE;
934         vlc_bool_t b_control = VLC_FALSE;
935         vlc_bool_t b_seekable = VLC_FALSE;
936         vlc_bool_t b_chapters = VLC_FALSE;
937
938         playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
939                                                    FIND_ANYWHERE );
940         b_plmul = p_playlist->i_size > 1;
941
942         vlc_object_release( p_playlist );
943
944         if( ( b_input = ( p_input != NULL ) ) )
945         {
946             /* seekable streams */
947             var_Get( p_input, "seekable", &val);
948             b_seekable = val.b_bool;
949
950             /* check wether slow/fast motion is possible*/
951             b_control = p_input->input.b_can_pace_control;
952
953             /* chapters & titles */
954             //b_chapters = p_input->stream.i_area_nb > 1;
955         }
956
957         [o_btn_stop setEnabled: b_input];
958         [o_btn_ff setEnabled: b_seekable];
959         [o_btn_rewind setEnabled: b_seekable];
960         [o_btn_prev setEnabled: (b_plmul || b_chapters)];
961         [o_btn_next setEnabled: (b_plmul || b_chapters)];
962
963         [o_timeslider setFloatValue: 0.0];
964         [o_timeslider setEnabled: b_seekable];
965         [o_timefield setStringValue: @"0:00:00"];
966
967         p_intf->p_sys->b_intf_update = VLC_FALSE;
968     }
969
970     if( p_intf->p_sys->b_playmode_update )
971     {
972         [o_playlist playModeUpdated];
973         p_intf->p_sys->b_playmode_update = VLC_FALSE;
974     }
975     if( p_intf->p_sys->b_playlist_update )
976     {
977         [o_playlist playlistUpdated];
978         p_intf->p_sys->b_playlist_update = VLC_FALSE;
979     }
980
981     if( p_intf->p_sys->b_fullscreen_update )
982     {
983         playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
984                                                    FIND_ANYWHERE );
985         var_Get( p_playlist, "fullscreen", &val );
986         [o_btn_fullscreen setState: val.b_bool];
987         vlc_object_release( p_playlist );
988
989         p_intf->p_sys->b_fullscreen_update = VLC_FALSE;
990     }
991
992     if( p_input && !p_input->b_die )
993     {
994         vlc_value_t val;
995
996         if( p_intf->p_sys->b_current_title_update )
997         {
998             NSString *o_temp;
999             vout_thread_t *p_vout;
1000             playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1001                                                        FIND_ANYWHERE );
1002
1003             if( p_playlist == NULL || p_playlist->status.p_item == NULL )
1004             {
1005                 return;
1006             }
1007             o_temp = [NSString stringWithUTF8String:
1008                 p_playlist->status.p_item->input.psz_name];
1009             if( o_temp == NULL )
1010                 o_temp = [NSString stringWithCString:
1011                     p_playlist->status.p_item->input.psz_name];
1012             [self setScrollField: o_temp stopAfter:-1];
1013
1014             p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT,
1015                                                     FIND_ANYWHERE );
1016             if( p_vout != NULL )
1017             {
1018                 id o_vout_wnd;
1019                 NSEnumerator * o_enum = [[NSApp orderedWindows] objectEnumerator];
1020
1021                 while( ( o_vout_wnd = [o_enum nextObject] ) )
1022                 {
1023                     if( [[o_vout_wnd className] isEqualToString: @"VLCWindow"] )
1024                     {
1025                         [o_vout_wnd updateTitle];
1026                     }
1027                 }
1028                 vlc_object_release( (vlc_object_t *)p_vout );
1029             }
1030             [o_playlist updateRowSelection];
1031             vlc_object_release( p_playlist );
1032             p_intf->p_sys->b_current_title_update = FALSE;
1033         }
1034
1035         if( p_input && [o_timeslider isEnabled] )
1036         {
1037             /* Update the slider */
1038             vlc_value_t time;
1039             NSString * o_time;
1040             mtime_t i_seconds;
1041             vlc_value_t pos;
1042             float f_updated;
1043
1044             var_Get( p_input, "position", &pos );
1045             f_updated = 10000. * pos.f_float;
1046             [o_timeslider setFloatValue: f_updated];
1047
1048             var_Get( p_input, "time", &time );
1049             i_seconds = time.i_time / 1000000;
1050
1051             o_time = [NSString stringWithFormat: @"%d:%02d:%02d",
1052                             (int) (i_seconds / (60 * 60)),
1053                             (int) (i_seconds / 60 % 60),
1054                             (int) (i_seconds % 60)];
1055             [o_timefield setStringValue: o_time];
1056         }
1057         
1058         if( p_intf->p_sys->b_volume_update )
1059         {
1060             NSString *o_text;
1061             int i_volume_step = 0;
1062             o_text = [NSString stringWithFormat: _NS("Volume: %d%%"), i_lastShownVolume * 400 / AOUT_VOLUME_MAX];
1063             if( i_lastShownVolume != -1 )
1064             [self setScrollField:o_text stopAfter:1000000];
1065             i_volume_step = config_GetInt( p_intf->p_vlc, "volume-step" );
1066             [o_volumeslider setFloatValue: (float)i_lastShownVolume / i_volume_step];
1067             [o_volumeslider setEnabled: TRUE];
1068             p_intf->p_sys->b_mute = ( i_lastShownVolume == 0 );
1069             p_intf->p_sys->b_volume_update = FALSE;
1070         }
1071
1072         /* Manage Playing status */
1073         var_Get( p_input, "state", &val );
1074         if( p_intf->p_sys->i_play_status != val.i_int )
1075         {
1076             p_intf->p_sys->i_play_status = val.i_int;
1077             [self playStatusUpdated: p_intf->p_sys->i_play_status];
1078         }
1079     }
1080     else
1081     {
1082         p_intf->p_sys->i_play_status = END_S;
1083         p_intf->p_sys->b_intf_update = VLC_TRUE;
1084         [self playStatusUpdated: p_intf->p_sys->i_play_status];
1085         [self setSubmenusEnabled: FALSE];
1086     }
1087
1088 #undef p_input
1089
1090     [self updateMessageArray];
1091
1092     if( (i_end_scroll != -1) && (mdate() > i_end_scroll) )
1093         [self resetScrollField];
1094
1095
1096     [NSTimer scheduledTimerWithTimeInterval: 0.3
1097         target: self selector: @selector(manageIntf:)
1098         userInfo: nil repeats: FALSE];
1099 }
1100
1101 - (void)setupMenus
1102 {
1103 #define p_input p_intf->p_sys->p_input
1104     if( p_input != NULL )
1105     {
1106         [o_controls setupVarMenuItem: o_mi_program target: (vlc_object_t *)p_input
1107             var: "program" selector: @selector(toggleVar:)];
1108
1109         [o_controls setupVarMenuItem: o_mi_title target: (vlc_object_t *)p_input
1110             var: "title" selector: @selector(toggleVar:)];
1111
1112         [o_controls setupVarMenuItem: o_mi_chapter target: (vlc_object_t *)p_input
1113             var: "chapter" selector: @selector(toggleVar:)];
1114
1115         [o_controls setupVarMenuItem: o_mi_audiotrack target: (vlc_object_t *)p_input
1116             var: "audio-es" selector: @selector(toggleVar:)];
1117
1118         [o_controls setupVarMenuItem: o_mi_videotrack target: (vlc_object_t *)p_input
1119             var: "video-es" selector: @selector(toggleVar:)];
1120
1121         [o_controls setupVarMenuItem: o_mi_subtitle target: (vlc_object_t *)p_input
1122             var: "spu-es" selector: @selector(toggleVar:)];
1123
1124         aout_instance_t * p_aout = vlc_object_find( p_intf, VLC_OBJECT_AOUT,
1125                                                     FIND_ANYWHERE );
1126         if ( p_aout != NULL )
1127         {
1128             [o_controls setupVarMenuItem: o_mi_channels target: (vlc_object_t *)p_aout
1129                 var: "audio-channels" selector: @selector(toggleVar:)];
1130
1131             [o_controls setupVarMenuItem: o_mi_device target: (vlc_object_t *)p_aout
1132                 var: "audio-device" selector: @selector(toggleVar:)];
1133
1134             [o_controls setupVarMenuItem: o_mi_visual target: (vlc_object_t *)p_aout
1135                 var: "visual" selector: @selector(toggleVar:)];
1136             vlc_object_release( (vlc_object_t *)p_aout );
1137         }
1138
1139         vout_thread_t * p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT,
1140                                                             FIND_ANYWHERE );
1141
1142         if ( p_vout != NULL )
1143         {
1144             vlc_object_t * p_dec_obj;
1145
1146             [o_controls setupVarMenuItem: o_mi_screen target: (vlc_object_t *)p_vout
1147                 var: "video-device" selector: @selector(toggleVar:)];
1148
1149             [o_controls setupVarMenuItem: o_mi_deinterlace target: (vlc_object_t *)p_vout
1150                 var: "deinterlace" selector: @selector(toggleVar:)];
1151
1152             p_dec_obj = (vlc_object_t *)vlc_object_find(
1153                                                  (vlc_object_t *)p_vout,
1154                                                  VLC_OBJECT_DECODER,
1155                                                  FIND_PARENT );
1156             if ( p_dec_obj != NULL )
1157             {
1158                [o_controls setupVarMenuItem: o_mi_ffmpeg_pp target:
1159                     (vlc_object_t *)p_dec_obj var:"ffmpeg-pp-q" selector:
1160                     @selector(toggleVar:)];
1161
1162                 vlc_object_release(p_dec_obj);
1163             }
1164             vlc_object_release( (vlc_object_t *)p_vout );
1165         }
1166     }
1167 #undef p_input
1168 }
1169
1170 - (void)setScrollField:(NSString *)o_string stopAfter:(int)timeout
1171 {
1172     if( timeout != -1 )
1173         i_end_scroll = mdate() + timeout;
1174     else
1175         i_end_scroll = -1;
1176     [o_scrollfield setStringValue: o_string];
1177 }
1178
1179 - (void)resetScrollField
1180 {
1181     i_end_scroll = -1;
1182 #define p_input p_intf->p_sys->p_input
1183     if( p_input && !p_input->b_die )
1184     {
1185         NSString *o_temp;
1186         playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1187                                                    FIND_ANYWHERE );
1188         if( p_playlist == NULL )
1189         {
1190             return;
1191         }
1192         o_temp = [NSString stringWithUTF8String:
1193                   p_playlist->status.p_item->input.psz_name];
1194         if( o_temp == NULL )
1195             o_temp = [NSString stringWithCString:
1196                     p_playlist->status.p_item->input.psz_name];
1197         [self setScrollField: o_temp stopAfter:-1];
1198         vlc_object_release( p_playlist );
1199         return;
1200     }
1201 #undef p_input
1202     [self setScrollField: _NS("VLC media player") stopAfter:-1];
1203 }
1204
1205 - (void)updateMessageArray
1206 {
1207     int i_start, i_stop;
1208     vlc_value_t quiet;
1209
1210     vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
1211     i_stop = *p_intf->p_sys->p_sub->pi_stop;
1212     vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
1213
1214     if( p_intf->p_sys->p_sub->i_start != i_stop )
1215     {
1216         NSColor *o_white = [NSColor whiteColor];
1217         NSColor *o_red = [NSColor redColor];
1218         NSColor *o_yellow = [NSColor yellowColor];
1219         NSColor *o_gray = [NSColor grayColor];
1220
1221         NSColor * pp_color[4] = { o_white, o_red, o_yellow, o_gray };
1222         static const char * ppsz_type[4] = { ": ", " error: ",
1223                                              " warning: ", " debug: " };
1224
1225         for( i_start = p_intf->p_sys->p_sub->i_start;
1226              i_start != i_stop;
1227              i_start = (i_start+1) % VLC_MSG_QSIZE )
1228         {
1229             NSString *o_msg;
1230             NSDictionary *o_attr;
1231             NSAttributedString *o_msg_color;
1232
1233             int i_type = p_intf->p_sys->p_sub->p_msg[i_start].i_type;
1234
1235             [o_msg_lock lock];
1236
1237             if( [o_msg_arr count] + 2 > 400 )
1238             {
1239                 unsigned rid[] = { 0, 1 };
1240                 [o_msg_arr removeObjectsFromIndices: (unsigned *)&rid
1241                            numIndices: sizeof(rid)/sizeof(rid[0])];
1242             }
1243
1244             o_attr = [NSDictionary dictionaryWithObject: o_gray
1245                 forKey: NSForegroundColorAttributeName];
1246             o_msg = [NSString stringWithFormat: @"%s%s",
1247                 p_intf->p_sys->p_sub->p_msg[i_start].psz_module,
1248                 ppsz_type[i_type]];
1249             o_msg_color = [[NSAttributedString alloc]
1250                 initWithString: o_msg attributes: o_attr];
1251             [o_msg_arr addObject: [o_msg_color autorelease]];
1252
1253             o_attr = [NSDictionary dictionaryWithObject: pp_color[i_type]
1254                 forKey: NSForegroundColorAttributeName];
1255             o_msg = [NSString stringWithFormat: @"%s\n",
1256                 p_intf->p_sys->p_sub->p_msg[i_start].psz_msg];
1257             o_msg_color = [[NSAttributedString alloc]
1258                 initWithString: o_msg attributes: o_attr];
1259             [o_msg_arr addObject: [o_msg_color autorelease]];
1260
1261             [o_msg_lock unlock];
1262
1263             var_Get( p_intf->p_vlc, "verbose", &quiet );
1264
1265             if( i_type == 1 && quiet.i_int > -1 )
1266             {
1267                 NSString *o_my_msg = [NSString stringWithFormat: @"%s: %s\n",
1268                     p_intf->p_sys->p_sub->p_msg[i_start].psz_module,
1269                     p_intf->p_sys->p_sub->p_msg[i_start].psz_msg];
1270
1271                 NSRange s_r = NSMakeRange( [[o_err_msg string] length], 0 );
1272                 [o_err_msg setEditable: YES];
1273                 [o_err_msg setSelectedRange: s_r];
1274                 [o_err_msg insertText: o_my_msg];
1275
1276                 [o_error makeKeyAndOrderFront: self];
1277                 [o_err_msg setEditable: NO];
1278             }
1279         }
1280
1281         vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
1282         p_intf->p_sys->p_sub->i_start = i_start;
1283         vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
1284     }
1285 }
1286
1287 - (void)playStatusUpdated:(int)i_status
1288 {
1289     if( i_status == PLAYING_S )
1290     {
1291         [o_btn_play setImage: o_img_pause];
1292         [o_btn_play setAlternateImage: o_img_pause_pressed];
1293         [o_btn_play setToolTip: _NS("Pause")];
1294         [o_mi_play setTitle: _NS("Pause")];
1295         [o_dmi_play setTitle: _NS("Pause")];
1296     }
1297     else
1298     {
1299         [o_btn_play setImage: o_img_play];
1300         [o_btn_play setAlternateImage: o_img_play_pressed];
1301         [o_btn_play setToolTip: _NS("Play")];
1302         [o_mi_play setTitle: _NS("Play")];
1303         [o_dmi_play setTitle: _NS("Play")];
1304     }
1305 }
1306
1307 - (void)setSubmenusEnabled:(BOOL)b_enabled
1308 {
1309     [o_mi_program setEnabled: b_enabled];
1310     [o_mi_title setEnabled: b_enabled];
1311     [o_mi_chapter setEnabled: b_enabled];
1312     [o_mi_audiotrack setEnabled: b_enabled];
1313     [o_mi_visual setEnabled: b_enabled];
1314     [o_mi_videotrack setEnabled: b_enabled];
1315     [o_mi_subtitle setEnabled: b_enabled];
1316     [o_mi_channels setEnabled: b_enabled];
1317     [o_mi_deinterlace setEnabled: b_enabled];
1318     [o_mi_ffmpeg_pp setEnabled: b_enabled];
1319     [o_mi_device setEnabled: b_enabled];
1320     [o_mi_screen setEnabled: b_enabled];
1321 }
1322
1323 - (void)manageVolumeSlider
1324 {
1325     audio_volume_t i_volume;
1326     aout_VolumeGet( p_intf, &i_volume );
1327
1328     if( i_volume != i_lastShownVolume )
1329     {
1330         i_lastShownVolume = i_volume;
1331         p_intf->p_sys->b_volume_update = TRUE;
1332     }
1333 }
1334
1335 - (IBAction)timesliderUpdate:(id)sender
1336 {
1337 #define p_input p_intf->p_sys->p_input
1338     float f_updated;
1339
1340     switch( [[NSApp currentEvent] type] )
1341     {
1342         case NSLeftMouseUp:
1343         case NSLeftMouseDown:
1344         case NSLeftMouseDragged:
1345             f_updated = [sender floatValue];
1346             break;
1347
1348         default:
1349             return;
1350     }
1351
1352     if( p_input != NULL )
1353     {
1354         vlc_value_t time;
1355         vlc_value_t pos;
1356         mtime_t i_seconds;
1357         NSString * o_time;
1358
1359         pos.f_float = f_updated / 10000.;
1360         var_Set( p_input, "position", pos );
1361         [o_timeslider setFloatValue: f_updated];
1362
1363         var_Get( p_input, "time", &time );
1364         i_seconds = time.i_time / 1000000;
1365
1366         o_time = [NSString stringWithFormat: @"%d:%02d:%02d",
1367                         (int) (i_seconds / (60 * 60)),
1368                         (int) (i_seconds / 60 % 60),
1369                         (int) (i_seconds % 60)];
1370         [o_timefield setStringValue: o_time];
1371     }
1372 #undef p_input
1373 }
1374
1375 - (void)terminate
1376 {
1377     playlist_t * p_playlist;
1378     vout_thread_t * p_vout;
1379
1380 #define p_input p_intf->p_sys->p_input
1381     if( p_input )
1382     {
1383         vlc_object_release( p_input );
1384         p_input = NULL;
1385     }
1386 #undef p_input
1387
1388     /* Stop playback */
1389     if( ( p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1390                                         FIND_ANYWHERE ) ) )
1391     {
1392         playlist_Stop( p_playlist );
1393         vlc_object_release( p_playlist );
1394     }
1395
1396     /* FIXME - Wait here until all vouts are terminated because
1397        libvlc's VLC_CleanUp destroys interfaces before vouts, which isn't
1398        good on OS X. We definitly need a cleaner way to handle this,
1399        but this may hopefully be good enough for now.
1400          -- titer 2003/11/22 */
1401     while( ( p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT,
1402                                        FIND_ANYWHERE ) ) )
1403     {
1404         vlc_object_release( p_vout );
1405         msleep( 100000 );
1406     }
1407     msleep( 500000 );
1408
1409     /* save the prefs if they were changed in the extended panel */
1410     if (o_extended && [o_extended getConfigChanged])
1411     {
1412         [o_extended savePrefs];
1413     }
1414     
1415     p_intf->b_interaction = VLC_FALSE;
1416     var_DelCallback( p_intf, "interaction", InteractCallback, self );
1417
1418     /* release some other objects here, because it isn't sure whether dealloc
1419      * will be called later on -- FK (10/6/05) */
1420     if( nib_about_loaded && o_about )
1421         [o_about release];
1422     
1423     if( nib_open_loaded && o_open )
1424         [o_open release];
1425     
1426     if( nib_extended_loaded && o_extended )
1427     {
1428         [o_extended collapsAll];
1429         [o_extended release];
1430     }
1431     
1432     if( nib_bookmarks_loaded && o_bookmarks )
1433         [o_bookmarks release];
1434
1435     if( nib_wizard_loaded && o_wizard )
1436         [o_wizard release];
1437         
1438     if( o_embedded_list != nil )
1439         [o_embedded_list release];
1440
1441     if( o_interaction_list != nil )
1442         [o_interaction_list release];
1443
1444     if( o_img_pause_pressed != nil )
1445     {
1446         [o_img_pause_pressed release];
1447         o_img_pause_pressed = nil;
1448     }
1449
1450     if( o_img_pause_pressed != nil )
1451     {
1452         [o_img_pause_pressed release];
1453         o_img_pause_pressed = nil;
1454     }
1455
1456     if( o_img_pause != nil )
1457     {
1458         [o_img_pause release];
1459         o_img_pause = nil;
1460     }
1461
1462     if( o_img_play != nil )
1463     {
1464         [o_img_play release];
1465         o_img_play = nil;
1466     }
1467
1468     if( o_msg_arr != nil )
1469     {
1470         [o_msg_arr removeAllObjects];
1471         [o_msg_arr release];
1472         o_msg_arr = nil;
1473     }
1474
1475     if( o_msg_lock != nil )
1476     {
1477         [o_msg_lock release];
1478         o_msg_lock = nil;
1479     }
1480
1481     /* write cached user defaults to disk */
1482     [[NSUserDefaults standardUserDefaults] synchronize];
1483
1484     p_intf->b_die = VLC_TRUE;
1485 }
1486
1487 - (IBAction)clearRecentItems:(id)sender
1488 {
1489     [[NSDocumentController sharedDocumentController]
1490                           clearRecentDocuments: nil];
1491 }
1492
1493 - (void)openRecentItem:(id)sender
1494 {
1495     [self application: nil openFile: [sender title]];
1496 }
1497
1498 - (IBAction)intfOpenFile:(id)sender
1499 {
1500     if (!nib_open_loaded)
1501     {
1502         nib_open_loaded = [NSBundle loadNibNamed:@"Open" owner:self];
1503         [o_open awakeFromNib];
1504         [o_open openFile];
1505     } else {
1506         [o_open openFile];
1507     }
1508 }
1509
1510 - (IBAction)intfOpenFileGeneric:(id)sender
1511 {
1512     if (!nib_open_loaded)
1513     {
1514         nib_open_loaded = [NSBundle loadNibNamed:@"Open" owner:self];
1515         [o_open awakeFromNib];
1516         [o_open openFileGeneric];
1517     } else {
1518         [o_open openFileGeneric];
1519     }
1520 }
1521
1522 - (IBAction)intfOpenDisc:(id)sender
1523 {
1524     if (!nib_open_loaded)
1525     {
1526         nib_open_loaded = [NSBundle loadNibNamed:@"Open" owner:self];
1527         [o_open awakeFromNib];
1528         [o_open openDisc];
1529     } else {
1530         [o_open openDisc];
1531     }
1532 }
1533
1534 - (IBAction)intfOpenNet:(id)sender
1535 {
1536     if (!nib_open_loaded)
1537     {
1538         nib_open_loaded = [NSBundle loadNibNamed:@"Open" owner:self];
1539         [o_open awakeFromNib];
1540         [o_open openNet];
1541     } else {
1542         [o_open openNet];
1543     }
1544 }
1545
1546 - (IBAction)showWizard:(id)sender
1547 {
1548     if (!nib_wizard_loaded)
1549     {
1550         nib_wizard_loaded = [NSBundle loadNibNamed:@"Wizard" owner:self];
1551         [o_wizard initStrings];
1552         [o_wizard resetWizard];
1553         [o_wizard showWizard];
1554     } else {
1555         [o_wizard resetWizard];
1556         [o_wizard showWizard];
1557     }
1558 }
1559
1560 - (IBAction)showExtended:(id)sender
1561 {
1562     if ( o_extended == nil )
1563     {
1564         o_extended = [[VLCExtended alloc] init];
1565     }
1566     if (!nib_extended_loaded)
1567     {
1568         nib_extended_loaded = [NSBundle loadNibNamed:@"Extended" owner:self];
1569         [o_extended initStrings];
1570         [o_extended showPanel];
1571     } else {
1572         [o_extended showPanel];
1573     }
1574 }
1575
1576 - (IBAction)showSFilters:(id)sender
1577 {
1578     if ( o_sfilters == nil )
1579     {
1580         o_sfilters = [[VLCsFilters alloc] init];
1581     }
1582     if (!nib_sfilters_loaded)
1583     {
1584         nib_sfilters_loaded = [NSBundle loadNibNamed:@"SFilters" owner:self];
1585         [o_sfilters initStrings];
1586         [o_sfilters showAsPanel];
1587     } else {
1588         [o_sfilters showAsPanel];
1589     }
1590 }
1591
1592 - (IBAction)showBookmarks:(id)sender
1593 {
1594     /* we need the wizard-nib for the bookmarks's extract functionality */
1595     if (!nib_wizard_loaded)
1596     {
1597         nib_wizard_loaded = [NSBundle loadNibNamed:@"Wizard" owner:self];
1598         [o_wizard initStrings];
1599     }
1600     
1601     if (!nib_bookmarks_loaded)
1602     {
1603         nib_bookmarks_loaded = [NSBundle loadNibNamed:@"Bookmarks" owner:self];
1604         [o_bookmarks showBookmarks];
1605     } else {
1606         [o_bookmarks showBookmarks];
1607     }
1608 }
1609
1610 - (IBAction)viewAbout:(id)sender
1611 {
1612     if (!nib_about_loaded)
1613     {
1614         nib_about_loaded = [NSBundle loadNibNamed:@"About" owner:self];
1615         [o_about showPanel];
1616     } else {
1617         [o_about showPanel];
1618     }
1619 }
1620
1621 - (IBAction)viewPreferences:(id)sender
1622 {
1623 /* GRUIIIIIIIK */
1624     if( o_prefs == nil )
1625         o_prefs = [[VLCPrefs alloc] init];
1626     [o_prefs showPrefs];
1627 }
1628
1629 /*- (IBAction)checkForUpdate:(id)sender
1630 {
1631     if (!nib_update_loaded)
1632     {
1633         nib_update_loaded = [NSBundle loadNibNamed:@"Update" owner:self];
1634         [o_update showUpdateWindow];
1635     } else {
1636         [o_update showUpdateWindow];
1637     }
1638 }*/
1639
1640 - (IBAction)closeError:(id)sender
1641 {
1642     vlc_value_t val;
1643
1644     if( [o_err_ckbk_surpress state] == NSOnState )
1645     {
1646         val.i_int = -1;
1647         var_Set( p_intf->p_vlc, "verbose", val );
1648     }
1649     [o_err_msg setString: @""];
1650     [o_error performClose: self];
1651 }
1652
1653 - (IBAction)openReadMe:(id)sender
1654 {
1655     NSString * o_path = [[NSBundle mainBundle]
1656         pathForResource: @"README.MacOSX" ofType: @"rtf"];
1657
1658     [[NSWorkspace sharedWorkspace] openFile: o_path
1659                                    withApplication: @"TextEdit"];
1660 }
1661
1662 - (IBAction)openDocumentation:(id)sender
1663 {
1664     NSURL * o_url = [NSURL URLWithString:
1665         @"http://www.videolan.org/doc/"];
1666
1667     [[NSWorkspace sharedWorkspace] openURL: o_url];
1668 }
1669
1670 - (IBAction)reportABug:(id)sender
1671 {
1672     NSURL * o_url = [NSURL URLWithString:
1673         @"http://www.videolan.org/support/bug-reporting.html"];
1674
1675     [[NSWorkspace sharedWorkspace] openURL: o_url];
1676 }
1677
1678 - (IBAction)openWebsite:(id)sender
1679 {
1680     NSURL * o_url = [NSURL URLWithString: @"http://www.videolan.org/"];
1681
1682     [[NSWorkspace sharedWorkspace] openURL: o_url];
1683 }
1684
1685 - (IBAction)openLicense:(id)sender
1686 {
1687     NSString * o_path = [[NSBundle mainBundle]
1688         pathForResource: @"COPYING" ofType: nil];
1689
1690     [[NSWorkspace sharedWorkspace] openFile: o_path
1691                                    withApplication: @"TextEdit"];
1692 }
1693
1694 - (IBAction)openForum:(id)sender
1695 {
1696     NSURL * o_url = [NSURL URLWithString: @"http://forum.videolan.org/"];
1697
1698     [[NSWorkspace sharedWorkspace] openURL: o_url];
1699 }
1700
1701 - (IBAction)openDonate:(id)sender
1702 {
1703     NSURL * o_url = [NSURL URLWithString: @"http://www.videolan.org/contribute.html#paypal"];
1704
1705     [[NSWorkspace sharedWorkspace] openURL: o_url];
1706 }
1707
1708 - (IBAction)openCrashLog:(id)sender
1709 {
1710     NSString * o_path = [@"~/Library/Logs/CrashReporter/VLC.crash.log"
1711                                     stringByExpandingTildeInPath];
1712
1713
1714     if ( [[NSFileManager defaultManager] fileExistsAtPath: o_path ] )
1715     {
1716         [[NSWorkspace sharedWorkspace] openFile: o_path
1717                                     withApplication: @"Console"];
1718     }
1719     else
1720     {
1721         NSBeginInformationalAlertSheet(_NS("No CrashLog found"), @"Continue", nil, nil, o_msgs_panel, self, NULL, NULL, nil, _NS("You haven't experienced any heavy crashes yet.") );
1722
1723     }
1724 }
1725
1726 - (void)windowDidBecomeKey:(NSNotification *)o_notification
1727 {
1728     if( [o_notification object] == o_msgs_panel )
1729     {
1730         id o_msg;
1731         NSEnumerator * o_enum;
1732
1733         [o_messages setString: @""];
1734
1735         [o_msg_lock lock];
1736
1737         o_enum = [o_msg_arr objectEnumerator];
1738
1739         while( ( o_msg = [o_enum nextObject] ) != nil )
1740         {
1741             [o_messages insertText: o_msg];
1742         }
1743
1744         [o_msg_lock unlock];
1745     }
1746 }
1747
1748 - (IBAction)togglePlaylist:(id)sender
1749 {
1750     NSRect o_rect = [o_window frame];
1751     /*First, check if the playlist is visible*/
1752     if( o_rect.size.height <= 200 )
1753     {
1754         b_small_window = YES; /* we know we are small, make sure this is actually set (see case below) */
1755         /* make large */
1756         if ( o_size_with_playlist.height > 200 )
1757         {
1758             o_rect.size.height = o_size_with_playlist.height;
1759         } else {
1760             o_rect.size.height = 500;
1761         }
1762         
1763         if ( o_size_with_playlist.width > [o_window minSize].width )
1764         {
1765             o_rect.size.width = o_size_with_playlist.width;
1766         } else {
1767             o_rect.size.width = 500;
1768         }
1769         
1770         o_rect.size.height = (o_size_with_playlist.height > 200) ?
1771             o_size_with_playlist.height : 500;
1772         o_rect.origin.x = [o_window frame].origin.x;
1773         o_rect.origin.y = [o_window frame].origin.y - o_rect.size.height +
1774                                                 [o_window minSize].height;
1775         [o_btn_playlist setState: YES];
1776     }
1777     else
1778     {
1779         /* make small */
1780         o_rect.size.height = [o_window minSize].height;
1781         o_rect.size.width = [o_window minSize].width;
1782         o_rect.origin.x = [o_window frame].origin.x;
1783         /* Calculate the position of the lower right corner after resize */
1784         o_rect.origin.y = [o_window frame].origin.y +
1785             [o_window frame].size.height - [o_window minSize].height;
1786
1787         [o_playlist_view setAutoresizesSubviews: NO];
1788         [o_playlist_view removeFromSuperview];
1789         [o_btn_playlist setState: NO];
1790         b_small_window = NO; /* we aren't small here just yet. we are doing an animated resize after this */
1791     }
1792
1793     [o_window setFrame: o_rect display:YES animate: YES];
1794 }
1795
1796 - (void)updateTogglePlaylistState
1797 {
1798     if( [o_window frame].size.height <= 200 )
1799     {
1800         [o_btn_playlist setState: NO];
1801     }
1802     else
1803     {
1804         [o_btn_playlist setState: YES];
1805     }
1806 }
1807
1808 - (NSSize)windowWillResize:(NSWindow *)sender toSize:(NSSize)proposedFrameSize
1809 {
1810     /* Not triggered on a window resize or maxification of the window. only by window mouse dragging resize */
1811
1812    /*Stores the size the controller one resize, to be able to restore it when
1813      toggling the playlist*/
1814     o_size_with_playlist = proposedFrameSize;
1815
1816     if( proposedFrameSize.height <= 200 )
1817     {
1818         if( b_small_window == NO )
1819         {
1820             /* if large and going to small then hide */
1821             b_small_window = YES;
1822             [o_playlist_view setAutoresizesSubviews: NO];
1823             [o_playlist_view removeFromSuperview];
1824         }
1825         return NSMakeSize( proposedFrameSize.width, [o_window minSize].height);
1826     }
1827     return proposedFrameSize;
1828 }
1829
1830 - (void)windowDidResize:(NSNotification *)notif
1831 {
1832     if( [o_window frame].size.height > 200 && b_small_window )
1833     {
1834         /* If large and coming from small then show */
1835         [o_playlist_view setAutoresizesSubviews: YES];
1836         [o_playlist_view setFrame: NSMakeRect( 10, 10, [o_window frame].size.width - 20, [o_window frame].size.height - [o_window minSize].height - 10 )];
1837         [o_playlist_view setNeedsDisplay:YES];
1838         [[o_window contentView] addSubview: o_playlist_view];
1839         b_small_window = NO;
1840     }
1841     [self updateTogglePlaylistState];
1842 }
1843
1844 @end
1845
1846 @implementation VLCMain (NSMenuValidation)
1847
1848 - (BOOL)validateMenuItem:(NSMenuItem *)o_mi
1849 {
1850     NSString *o_title = [o_mi title];
1851     BOOL bEnabled = TRUE;
1852
1853     /* Recent Items Menu */
1854     if( [o_title isEqualToString: _NS("Clear Menu")] )
1855     {
1856         NSMenu * o_menu = [o_mi_open_recent submenu];
1857         int i_nb_items = [o_menu numberOfItems];
1858         NSArray * o_docs = [[NSDocumentController sharedDocumentController]
1859                                                        recentDocumentURLs];
1860         UInt32 i_nb_docs = [o_docs count];
1861
1862         if( i_nb_items > 1 )
1863         {
1864             while( --i_nb_items )
1865             {
1866                 [o_menu removeItemAtIndex: 0];
1867             }
1868         }
1869
1870         if( i_nb_docs > 0 )
1871         {
1872             NSURL * o_url;
1873             NSString * o_doc;
1874
1875             [o_menu insertItem: [NSMenuItem separatorItem] atIndex: 0];
1876
1877             while( TRUE )
1878             {
1879                 i_nb_docs--;
1880
1881                 o_url = [o_docs objectAtIndex: i_nb_docs];
1882
1883                 if( [o_url isFileURL] )
1884                 {
1885                     o_doc = [o_url path];
1886                 }
1887                 else
1888                 {
1889                     o_doc = [o_url absoluteString];
1890                 }
1891
1892                 [o_menu insertItemWithTitle: o_doc
1893                     action: @selector(openRecentItem:)
1894                     keyEquivalent: @"" atIndex: 0];
1895
1896                 if( i_nb_docs == 0 )
1897                 {
1898                     break;
1899                 }
1900             }
1901         }
1902         else
1903         {
1904             bEnabled = FALSE;
1905         }
1906     }
1907     return( bEnabled );
1908 }
1909
1910 @end
1911
1912 @implementation VLCMain (Internal)
1913
1914 - (void)handlePortMessage:(NSPortMessage *)o_msg
1915 {
1916     id ** val;
1917     NSData * o_data;
1918     NSValue * o_value;
1919     NSInvocation * o_inv;
1920     NSConditionLock * o_lock;
1921
1922     o_data = [[o_msg components] lastObject];
1923     o_inv = *((NSInvocation **)[o_data bytes]);
1924     [o_inv getArgument: &o_value atIndex: 2];
1925     val = (id **)[o_value pointerValue];
1926     [o_inv setArgument: val[1] atIndex: 2];
1927     o_lock = *(val[0]);
1928
1929     [o_lock lock];
1930     [o_inv invoke];
1931     [o_lock unlockWithCondition: 1];
1932 }
1933
1934 @end