]> git.sesse.net Git - vlc/blob - modules/gui/macosx/controls.m
Merge branch 1.0-bugfix
[vlc] / modules / gui / macosx / controls.m
1 /*****************************************************************************
2  * controls.m: MacOS X interface module
3  *****************************************************************************
4  * Copyright (C) 2002-2009 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 dot org>
10  *          Benjamin Pracht <bigben at videolan doit org>
11  *          Felix Paul Kühne <fkuehne at videolan dot org>
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 /*****************************************************************************
29  * Preamble
30  *****************************************************************************/
31 #include <stdlib.h>                                      /* malloc(), free() */
32 #include <sys/param.h>                                    /* for MAXPATHLEN */
33 #include <string.h>
34
35 #import "intf.h"
36 #import "vout.h"
37 #import "open.h"
38 #import "controls.h"
39 #import "playlist.h"
40 #include <vlc_osd.h>
41 #include <vlc_keys.h>
42
43 #pragma mark -
44 /*****************************************************************************
45  * VLCControls implementation
46  *****************************************************************************/
47 @implementation VLCControls
48
49 - (id)init
50 {
51     [super init];
52     o_fs_panel = [[VLCFSPanel alloc] init];
53     b_lockAspectRatio = YES;
54     return self;
55 }
56
57 - (void)awakeFromNib
58 {
59     [o_specificTime_mi setTitle: _NS("Jump To Time")];
60     [o_specificTime_cancel_btn setTitle: _NS("Cancel")];
61     [o_specificTime_ok_btn setTitle: _NS("OK")];
62     [o_specificTime_sec_lbl setStringValue: _NS("sec.")];
63     [o_specificTime_goTo_lbl setStringValue: _NS("Jump to time")];
64
65     o_repeat_off = [NSImage imageNamed:@"repeat_embedded"];
66
67     [self controlTintChanged];
68
69     [[NSNotificationCenter defaultCenter] addObserver: self
70                                              selector: @selector( controlTintChanged )
71                                                  name: NSControlTintDidChangeNotification
72                                                object: nil];
73 }
74
75 - (void)controlTintChanged
76 {
77     int i_repeat = 0;
78     if( [o_btn_repeat image] == o_repeat_single )
79         i_repeat = 1;
80     else if( [o_btn_repeat image] == o_repeat_all )
81         i_repeat = 2;
82
83     if( [NSColor currentControlTint] == NSGraphiteControlTint )
84     {
85         o_repeat_single = [NSImage imageNamed:@"repeat_single_embedded_graphite"];
86         o_repeat_all = [NSImage imageNamed:@"repeat_embedded_graphite"];
87         
88         [o_btn_shuffle setAlternateImage: [NSImage imageNamed: @"shuffle_embedded_graphite"]];
89         [o_btn_addNode setAlternateImage: [NSImage imageNamed: @"add_embedded_graphite"]];
90     }
91     else
92     {
93         o_repeat_single = [NSImage imageNamed:@"repeat_single_embedded_blue"];
94         o_repeat_all = [NSImage imageNamed:@"repeat_embedded_blue"];
95         
96         [o_btn_shuffle setAlternateImage: [NSImage imageNamed: @"shuffle_embedded_blue"]];
97         [o_btn_addNode setAlternateImage: [NSImage imageNamed: @"add_embedded_blue"]];
98     }
99     
100     /* update the repeat button, but keep its state */
101     if( i_repeat == 1 )
102         [self repeatOne];
103     else if( i_repeat == 2 )
104         [self repeatAll];
105     else
106         [self repeatOff];
107 }
108
109 - (void)dealloc
110 {
111     [[NSNotificationCenter defaultCenter] removeObserver: self];
112     
113     [o_repeat_single release];
114     [o_repeat_all release];
115     [o_repeat_off release];
116     
117     [super dealloc];
118 }
119
120 - (IBAction)play:(id)sender
121 {
122     intf_thread_t * p_intf = VLCIntf;
123     playlist_t * p_playlist = pl_Hold( p_intf );
124     bool empty;
125
126     PL_LOCK;
127     empty = playlist_IsEmpty( p_playlist );
128     PL_UNLOCK;
129
130     pl_Release( p_intf );
131
132     if( empty )
133         [o_main intfOpenFileGeneric: (id)sender];
134
135     var_SetInteger( p_intf->p_libvlc, "key-action", ACTIONID_PLAY_PAUSE );
136 }
137
138 - (id)voutView
139 {
140     id o_window;
141     id o_voutView = nil;
142     id o_embeddedViewList = [[VLCMain sharedInstance] embeddedList];
143     NSEnumerator *o_enumerator = [[NSApp orderedWindows] objectEnumerator];
144     while( !o_voutView && ( o_window = [o_enumerator nextObject] ) )
145     {
146         /* We have an embedded vout */
147         if( [o_embeddedViewList windowContainsEmbedded: o_window] )
148         {
149             o_voutView = [o_embeddedViewList viewForWindow: o_window];
150         }
151         /* We have a detached vout */
152         else if( [[o_window className] isEqualToString: @"VLCVoutWindow"] )
153         {
154             o_voutView = [o_window voutView];
155         }
156     }
157     return [[o_voutView retain] autorelease];
158 }
159
160 - (BOOL)aspectRatioIsLocked
161 {
162     return b_lockAspectRatio;
163 }
164
165 - (IBAction)stop:(id)sender
166 {
167     intf_thread_t * p_intf = VLCIntf;
168     var_SetInteger( p_intf->p_libvlc, "key-action", ACTIONID_STOP );
169     /* Close the window directly, because we do know that there
170      * won't be anymore video. It's currently waiting a bit. */
171     [[[self voutView] window] orderOut:self];
172 }
173
174 - (IBAction)faster:(id)sender
175 {
176     intf_thread_t * p_intf = VLCIntf;
177     var_SetInteger( p_intf->p_libvlc, "key-action", ACTIONID_FASTER );
178 }
179
180 - (IBAction)slower:(id)sender
181 {
182     intf_thread_t * p_intf = VLCIntf;
183     var_SetInteger( p_intf->p_libvlc, "key-action", ACTIONID_SLOWER );
184 }
185
186 - (IBAction)prev:(id)sender
187 {
188     intf_thread_t * p_intf = VLCIntf;
189     var_SetInteger( p_intf->p_libvlc, "key-action", ACTIONID_PREV );
190 }
191
192 - (IBAction)next:(id)sender
193 {
194     intf_thread_t * p_intf = VLCIntf;
195     var_SetInteger( p_intf->p_libvlc, "key-action", ACTIONID_NEXT );
196 }
197
198 - (IBAction)random:(id)sender
199 {
200     vlc_value_t val;
201     intf_thread_t * p_intf = VLCIntf;
202     playlist_t * p_playlist = pl_Hold( p_intf );
203
204     var_Get( p_playlist, "random", &val );
205     val.b_bool = !val.b_bool;
206     var_Set( p_playlist, "random", val );
207     if( val.b_bool )
208     {
209         vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Random On" ) );
210         config_PutInt( p_playlist, "random", 1 );
211     }
212     else
213     {
214         vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Random Off" ) );
215         config_PutInt( p_playlist, "random", 0 );
216     }
217
218     p_intf->p_sys->b_playmode_update = true;
219     p_intf->p_sys->b_intf_update = true;
220     pl_Release( p_intf );
221 }
222
223 /* three little ugly helpers */
224 - (void)repeatOne
225 {
226     [o_btn_repeat setImage: o_repeat_single];
227     [o_btn_repeat setAlternateImage: o_repeat_all];
228     [o_btn_repeat_embed setImage: [NSImage imageNamed:@"sidebarRepeatOneOn"]];
229 }
230 - (void)repeatAll
231 {
232     [o_btn_repeat setImage: o_repeat_all];
233     [o_btn_repeat setAlternateImage: o_repeat_off];
234     [o_btn_repeat_embed setImage: [NSImage imageNamed:@"sidebarRepeatOn"]];
235 }
236 - (void)repeatOff
237 {
238     [o_btn_repeat setImage: o_repeat_off];
239     [o_btn_repeat setAlternateImage: o_repeat_single];
240     [o_btn_repeat_embed setImage: [NSImage imageNamed:@"sidebarRepeat"]];
241 }
242 - (void)shuffle
243 {
244     vlc_value_t val;
245     playlist_t *p_playlist = pl_Hold( VLCIntf );
246     var_Get( p_playlist, "random", &val );
247     [o_btn_shuffle setState: val.b_bool];
248         if(val.b_bool)
249         [o_btn_shuffle_embed setImage: [NSImage imageNamed:@"sidebarShuffleOn"]];
250         else
251         [o_btn_shuffle_embed setImage: [NSImage imageNamed:@"sidebarShuffle"]];    
252     pl_Release( VLCIntf );
253 }
254
255 - (IBAction)repeatButtonAction:(id)sender
256 {
257     vlc_value_t looping,repeating;
258     intf_thread_t * p_intf = VLCIntf;
259     playlist_t * p_playlist = pl_Hold( p_intf );
260
261     var_Get( p_playlist, "repeat", &repeating );
262     var_Get( p_playlist, "loop", &looping );
263
264     if( !repeating.b_bool && !looping.b_bool )
265     {
266         /* was: no repeating at all, switching to Repeat One */
267  
268         /* set our button's look */
269         [self repeatOne];
270  
271         /* prepare core communication */
272         repeating.b_bool = true;
273         looping.b_bool = false;
274         config_PutInt( p_playlist, "repeat", 1 );
275         config_PutInt( p_playlist, "loop", 0 );
276  
277         /* show the change */
278         vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat One" ) );
279     }
280     else if( repeating.b_bool && !looping.b_bool )
281     {
282         /* was: Repeat One, switching to Repeat All */
283  
284         /* set our button's look */
285         [self repeatAll];
286  
287         /* prepare core communication */
288         repeating.b_bool = false;
289         looping.b_bool = true;
290         config_PutInt( p_playlist, "repeat", 0 );
291         config_PutInt( p_playlist, "loop", 1 );
292  
293         /* show the change */
294         vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat All" ) );
295     }
296     else
297     {
298         /* was: Repeat All or bug in VLC, switching to Repeat Off */
299  
300         /* set our button's look */
301         [self repeatOff];
302  
303         /* prepare core communication */
304         repeating.b_bool = false;
305         looping.b_bool = false;
306         config_PutInt( p_playlist, "repeat", 0 );
307         config_PutInt( p_playlist, "loop", 0 );
308  
309         /* show the change */
310         vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat Off" ) );
311     }
312
313     /* communicate with core and the main intf loop */
314     var_Set( p_playlist, "repeat", repeating );
315     var_Set( p_playlist, "loop", looping );
316     p_intf->p_sys->b_playmode_update = true;
317     p_intf->p_sys->b_intf_update = true;
318
319     pl_Release( p_intf );
320 }
321
322
323 - (IBAction)repeat:(id)sender
324 {
325     vlc_value_t val;
326     intf_thread_t * p_intf = VLCIntf;
327     playlist_t * p_playlist = pl_Hold( p_intf );
328
329     var_Get( p_playlist, "repeat", &val );
330     if (!val.b_bool)
331     {
332         var_Set( p_playlist, "loop", val );
333     }
334     val.b_bool = !val.b_bool;
335     var_Set( p_playlist, "repeat", val );
336     if( val.b_bool )
337     {
338         vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat One" ) );
339         config_PutInt( p_playlist, "repeat", 1 );
340     }
341     else
342     {
343         vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat Off" ) );
344         config_PutInt( p_playlist, "repeat", 0 );
345     }
346  
347     p_intf->p_sys->b_playmode_update = true;
348     p_intf->p_sys->b_intf_update = true;
349     pl_Release( p_intf );
350 }
351
352 - (IBAction)loop:(id)sender
353 {
354     vlc_value_t val;
355     intf_thread_t * p_intf = VLCIntf;
356     playlist_t * p_playlist = pl_Hold( p_intf );
357
358     var_Get( p_playlist, "loop", &val );
359     if (!val.b_bool)
360     {
361         var_Set( p_playlist, "repeat", val );
362     }
363     val.b_bool = !val.b_bool;
364     var_Set( p_playlist, "loop", val );
365     if( val.b_bool )
366     {
367         vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat All" ) );
368         config_PutInt( p_playlist, "loop", 1 );
369     }
370     else
371     {
372         vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat Off" ) );
373         config_PutInt( p_playlist, "loop", 0 );
374     }
375
376     p_intf->p_sys->b_playmode_update = true;
377     p_intf->p_sys->b_intf_update = true;
378     pl_Release( p_intf );
379 }
380
381 - (IBAction)forward:(id)sender
382 {
383     intf_thread_t * p_intf = VLCIntf;
384     var_SetInteger( p_intf->p_libvlc, "key-action", ACTIONID_JUMP_FORWARD_SHORT );
385 }
386
387 - (IBAction)backward:(id)sender
388 {
389     vlc_value_t val;
390     intf_thread_t * p_intf = VLCIntf;
391     var_SetInteger( p_intf->p_libvlc, "key-action", ACTIONID_JUMP_BACKWARD_SHORT );
392 }
393
394
395 - (IBAction)volumeUp:(id)sender
396 {
397     intf_thread_t * p_intf = VLCIntf;
398     var_SetInteger( p_intf->p_libvlc, "key-action", ACTIONID_VOL_UP );
399     /* Manage volume status */
400     [o_main manageVolumeSlider];
401 }
402
403 - (IBAction)volumeDown:(id)sender
404 {
405     intf_thread_t * p_intf = VLCIntf;
406     var_SetInteger( p_intf->p_libvlc, "key-action", ACTIONID_VOL_DOWN );
407     /* Manage volume status */
408     [o_main manageVolumeSlider];
409 }
410
411 - (IBAction)mute:(id)sender
412 {
413     intf_thread_t * p_intf = VLCIntf;
414     var_SetInteger( p_intf->p_libvlc, "key-action", ACTIONID_VOL_MUTE );
415     /* Manage volume status */
416     [o_main manageVolumeSlider];
417 }
418
419 - (IBAction)volumeSliderUpdated:(id)sender
420 {
421     intf_thread_t * p_intf = VLCIntf;
422     audio_volume_t i_volume = (audio_volume_t)[sender intValue];
423     int i_volume_step = 0;
424     i_volume_step = config_GetInt( p_intf->p_libvlc, "volume-step" );
425     aout_VolumeSet( p_intf, i_volume * i_volume_step );
426     /* Manage volume status */
427     [o_main manageVolumeSlider];
428 }
429
430 - (IBAction)showPosition: (id)sender
431 {
432     input_thread_t * p_input = pl_CurrentInput( VLCIntf );
433     if( p_input != NULL )
434     {
435         vout_thread_t *p_vout = input_GetVout( p_input );
436         if( p_vout != NULL )
437         {
438             var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_POSITION );
439             vlc_object_release( (vlc_object_t *)p_vout );
440         }
441         vlc_object_release( p_input );
442     }
443 }
444
445 - (IBAction)toogleFullscreen:(id)sender {
446     NSMenuItem *o_mi = [[NSMenuItem alloc] initWithTitle: _NS("Fullscreen") action: nil keyEquivalent:@""];
447     [self windowAction: [o_mi autorelease]];
448 }
449
450 - (BOOL) isFullscreen {
451     id o_vout_view = [self voutView];
452     if( o_vout_view )
453     {
454         return [o_vout_view isFullscreen];
455     }
456     return NO;
457 }
458
459 - (IBAction)windowAction:(id)sender
460 {
461     NSString *o_title = [sender title];
462     input_thread_t * p_input = pl_CurrentInput( VLCIntf );
463
464     if( p_input != NULL )
465     {
466         vout_thread_t *p_vout = input_GetVout( p_input );
467         if( p_vout != NULL )
468         {
469             id o_vout_view = [self voutView];
470             if( o_vout_view )
471             {
472                 if( [o_title isEqualToString: _NS("Half Size") ] )
473                     [o_vout_view scaleWindowWithFactor: 0.5 animate: YES];
474                 else if( [o_title isEqualToString: _NS("Normal Size") ] )
475                     [o_vout_view scaleWindowWithFactor: 1.0 animate: YES];
476                 else if( [o_title isEqualToString: _NS("Double Size") ] )
477                     [o_vout_view scaleWindowWithFactor: 2.0 animate: YES];
478                 else if( [o_title isEqualToString: _NS("Float on Top") ] )
479                     [o_vout_view toggleFloatOnTop];
480                 else if( [o_title isEqualToString: _NS("Fit to Screen") ] )
481                 {
482                     id o_window = [o_vout_view voutWindow];
483                     if( ![o_window isZoomed] )
484                         [o_window performZoom:self];
485                 }
486                 else if( [o_title isEqualToString: _NS("Snapshot") ] )
487                 {
488                     [o_vout_view snapshot];
489                 }
490                 else
491                 {
492                     /* Fullscreen state for next time will be saved here too */
493                     [o_vout_view toggleFullscreen];
494                 }
495             }
496             vlc_object_release( (vlc_object_t *)p_vout );
497         }
498         else
499         {
500             playlist_t * p_playlist = pl_Hold( VLCIntf );
501
502             if( [o_title isEqualToString: _NS("Fullscreen")] ||
503                 [sender isKindOfClass:[NSButton class]] )
504             {
505                 vlc_value_t val;
506                 var_Get( p_playlist, "fullscreen", &val );
507                 var_Set( p_playlist, "fullscreen", (vlc_value_t)!val.b_bool );
508             }
509
510             pl_Release( VLCIntf );
511         }
512         vlc_object_release( p_input );
513     }
514 }
515
516 - (IBAction)telxTransparent:(id)sender
517 {
518     intf_thread_t * p_intf = VLCIntf;
519     vlc_object_t *p_vbi;
520     p_vbi = (vlc_object_t *) vlc_object_find_name( p_intf,
521                     "zvbi", FIND_ANYWHERE );
522     if( p_vbi )
523     {
524         var_SetBool( p_vbi, "vbi-opaque", [sender state] );
525         [sender setState: ![sender state]];
526         vlc_object_release( p_vbi );
527     }
528 }
529
530 - (IBAction)telxNavLink:(id)sender
531 {
532     intf_thread_t * p_intf = VLCIntf;
533     vlc_object_t *p_vbi;
534     int i_page = 0;
535
536     if( [[sender title] isEqualToString: _NS("Index")] )
537         i_page = 'i' << 16;
538     else if( [[sender title] isEqualToString: _NS("Red")] )
539         i_page = 'r' << 16;
540     else if( [[sender title] isEqualToString: _NS("Green")] )
541         i_page = 'g' << 16;
542     else if( [[sender title] isEqualToString: _NS("Yellow")] )
543         i_page = 'y' << 16;
544     else if( [[sender title] isEqualToString: _NS("Blue")] )
545         i_page = 'b' << 16;
546     if( i_page == 0 ) return;
547
548     p_vbi = (vlc_object_t *) vlc_object_find_name( p_intf,
549                 "zvbi", FIND_ANYWHERE );
550     if( p_vbi )
551     {
552         var_SetInteger( p_vbi, "vbi-page", i_page );
553         vlc_object_release( p_vbi );
554     }
555 }
556
557 - (IBAction)lockVideosAspectRatio:(id)sender
558 {
559     if( [sender state] == NSOffState )
560         [sender setState: NSOnState];
561     else
562         [sender setState: NSOffState];
563
564     b_lockAspectRatio = !b_lockAspectRatio;
565 }
566
567 - (IBAction)addSubtitleFile:(id)sender
568 {
569     NSInteger i_returnValue = 0;
570     input_thread_t * p_input = pl_CurrentInput( VLCIntf );
571     if( !p_input ) return;
572
573     input_item_t *p_item = input_GetItem( p_input );
574     if( !p_item ) return;
575
576     char *path = input_item_GetURI( p_item );
577     if( !path ) path = strdup( "" );
578
579     NSOpenPanel * openPanel = [NSOpenPanel openPanel];
580     [openPanel setCanChooseFiles: YES];
581     [openPanel setCanChooseDirectories: NO];
582     [openPanel setAllowsMultipleSelection: YES];
583     i_returnValue = [openPanel runModalForDirectory: [NSString stringWithUTF8String: path] file: nil types: [NSArray arrayWithObjects: @"cdg",@"@idx",@"srt",@"sub",@"utf",@"ass",@"ssa",@"aqt",@"jss",@"psb",@"rt",@"smi", nil]];
584     free( path );
585
586     if( i_returnValue == NSOKButton )
587     {
588         NSUInteger c = 0;
589         if( !p_input ) return;
590         
591         c = [[openPanel filenames] count];
592         NSLog( @"count: %i", c );
593         for (int i = 0; [[openPanel filenames] count] > i ; i++)
594         {
595             msg_Dbg( VLCIntf, "loading subs from %s", [[[openPanel filenames] objectAtIndex: i] UTF8String] );
596             if( input_AddSubtitle( p_input, [[[openPanel filenames] objectAtIndex: i] UTF8String], TRUE ) )
597                 msg_Warn( VLCIntf, "unable to load subtitles from '%s'",
598                          [[[openPanel filenames] objectAtIndex: i] UTF8String] );
599         }
600     }
601 }
602
603 - (void)scrollWheel:(NSEvent *)theEvent
604 {
605     intf_thread_t * p_intf = VLCIntf;
606     float f_yabsvalue = [theEvent deltaY] > 0.0f ? [theEvent deltaY] : -[theEvent deltaY];
607     float f_xabsvalue = [theEvent deltaX] > 0.0f ? [theEvent deltaX] : -[theEvent deltaX];
608     int i, i_yvlckey, i_xvlckey;
609
610     if ([theEvent deltaY] < 0.0f)
611         i_yvlckey = KEY_MOUSEWHEELDOWN;
612     else
613         i_yvlckey = KEY_MOUSEWHEELUP;
614
615     if ([theEvent deltaX] < 0.0f)
616         i_xvlckey = KEY_MOUSEWHEELRIGHT;
617     else
618         i_xvlckey = KEY_MOUSEWHEELLEFT;
619
620     /* Send multiple key event, depending on the intensity of the event */
621     for (i = 0; i < (int)(f_yabsvalue/4.+1.) && f_yabsvalue > 0.05 ; i++)
622         var_SetInteger( p_intf->p_libvlc, "key-pressed", i_yvlckey );
623
624     /* Prioritize Y event (sound volume) over X event */
625     if (f_yabsvalue < 0.05)
626     {
627         for (i = 0; i < (int)(f_xabsvalue/6.+1.) && f_xabsvalue > 0.05; i++)
628          var_SetInteger( p_intf->p_libvlc, "key-pressed", i_xvlckey );
629     }
630 }
631
632 - (BOOL)keyEvent:(NSEvent *)o_event
633 {
634     BOOL eventHandled = NO;
635     unichar key = [[o_event charactersIgnoringModifiers] characterAtIndex: 0];
636
637     if( key )
638     {
639         input_thread_t * p_input = pl_CurrentInput( VLCIntf );
640         if( p_input != NULL )
641         {
642             vout_thread_t *p_vout = input_GetVout( p_input );
643
644             if( p_vout != NULL )
645             {
646                 /* Escape */
647                 if( key == (unichar) 0x1b )
648                 {
649                     id o_vout_view = [self voutView];
650                     if( o_vout_view && [o_vout_view isFullscreen] )
651                     {
652                         [o_vout_view toggleFullscreen];
653                         eventHandled = YES;
654                     }
655                 }
656                 else if( key == ' ' )
657                 {
658                     [self play:self];
659                     eventHandled = YES;
660                 }
661                 vlc_object_release( (vlc_object_t *)p_vout );
662             }
663             vlc_object_release( p_input );
664         }
665     }
666     return eventHandled;
667 }
668
669 - (void)setupVarMenuItem:(NSMenuItem *)o_mi
670                     target:(vlc_object_t *)p_object
671                     var:(const char *)psz_variable
672                     selector:(SEL)pf_callback
673 {
674     vlc_value_t val, text;
675     int i_type = var_Type( p_object, psz_variable );
676
677     switch( i_type & VLC_VAR_TYPE )
678     {
679     case VLC_VAR_VOID:
680     case VLC_VAR_BOOL:
681     case VLC_VAR_VARIABLE:
682     case VLC_VAR_STRING:
683     case VLC_VAR_INTEGER:
684         break;
685     default:
686         /* Variable doesn't exist or isn't handled */
687         return;
688     }
689  
690     /* Make sure we want to display the variable */
691     if( i_type & VLC_VAR_HASCHOICE )
692     {
693         var_Change( p_object, psz_variable, VLC_VAR_CHOICESCOUNT, &val, NULL );
694         if( val.i_int == 0 ) return;
695         if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE && val.i_int == 1 )
696             return;
697     }
698  
699     /* Get the descriptive name of the variable */
700     var_Change( p_object, psz_variable, VLC_VAR_GETTEXT, &text, NULL );
701     [o_mi setTitle: [[VLCMain sharedInstance] localizedString: text.psz_string ?
702                                         text.psz_string : strdup( psz_variable ) ]];
703
704     var_Get( p_object, psz_variable, &val );
705     if( i_type & VLC_VAR_HASCHOICE )
706     {
707         NSMenu *o_menu = [o_mi submenu];
708
709         [self setupVarMenu: o_menu forMenuItem: o_mi target:p_object
710                         var:psz_variable selector:pf_callback];
711  
712         free( text.psz_string );
713         return;
714     }
715
716     VLCAutoGeneratedMenuContent *o_data;
717     switch( i_type & VLC_VAR_TYPE )
718     {
719     case VLC_VAR_VOID:
720         o_data = [[VLCAutoGeneratedMenuContent alloc] initWithVariableName: psz_variable ofObject: p_object
721                 andValue: val ofType: i_type];
722         [o_mi setRepresentedObject: [o_data autorelease]];
723         break;
724
725     case VLC_VAR_BOOL:
726         o_data = [[VLCAutoGeneratedMenuContent alloc] initWithVariableName: psz_variable ofObject: p_object
727                 andValue: val ofType: i_type];
728         [o_mi setRepresentedObject: [o_data autorelease]];
729         if( !( i_type & VLC_VAR_ISCOMMAND ) )
730             [o_mi setState: val.b_bool ? TRUE : FALSE ];
731         break;
732
733     default:
734         free( text.psz_string );
735         return;
736     }
737
738     if( ( i_type & VLC_VAR_TYPE ) == VLC_VAR_STRING ) free( val.psz_string );
739     free( text.psz_string );
740 }
741
742
743 - (void)setupVarMenu:(NSMenu *)o_menu
744                     forMenuItem: (NSMenuItem *)o_parent
745                     target:(vlc_object_t *)p_object
746                     var:(const char *)psz_variable
747                     selector:(SEL)pf_callback
748 {
749     vlc_value_t val, val_list, text_list;
750     int i_type, i, i_nb_items;
751
752     /* remove previous items */
753     i_nb_items = [o_menu numberOfItems];
754     for( i = 0; i < i_nb_items; i++ )
755     {
756         [o_menu removeItemAtIndex: 0];
757     }
758
759     /* Check the type of the object variable */
760     i_type = var_Type( p_object, psz_variable );
761
762     /* Make sure we want to display the variable */
763     if( i_type & VLC_VAR_HASCHOICE )
764     {
765         var_Change( p_object, psz_variable, VLC_VAR_CHOICESCOUNT, &val, NULL );
766         if( val.i_int == 0 ) return;
767         if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE && val.i_int == 1 )
768             return;
769     }
770     else
771     {
772         return;
773     }
774
775     switch( i_type & VLC_VAR_TYPE )
776     {
777     case VLC_VAR_VOID:
778     case VLC_VAR_BOOL:
779     case VLC_VAR_VARIABLE:
780     case VLC_VAR_STRING:
781     case VLC_VAR_INTEGER:
782         break;
783     default:
784         /* Variable doesn't exist or isn't handled */
785         return;
786     }
787
788     if( var_Get( p_object, psz_variable, &val ) < 0 )
789     {
790         return;
791     }
792
793     if( var_Change( p_object, psz_variable, VLC_VAR_GETLIST,
794                     &val_list, &text_list ) < 0 )
795     {
796         if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
797         return;
798     }
799
800     /* make (un)sensitive */
801     [o_parent setEnabled: ( val_list.p_list->i_count > 1 )];
802
803     /* Aspect Ratio */
804     if( [[o_parent title] isEqualToString: _NS("Aspect-ratio")] == YES )
805     {
806         NSMenuItem *o_lmi_tmp2;
807         o_lmi_tmp2 = [o_menu addItemWithTitle: _NS("Lock Aspect Ratio") action: @selector(lockVideosAspectRatio:) keyEquivalent: @""];
808         [o_lmi_tmp2 setTarget: self];
809         [o_lmi_tmp2 setEnabled: YES];
810         [o_lmi_tmp2 setState: b_lockAspectRatio];
811         [o_parent setEnabled: YES];
812         [o_menu addItem: [NSMenuItem separatorItem]];
813     }
814
815     /* special case for the subtitles items */
816     if( [[o_parent title] isEqualToString: _NS("Subtitles Track")] == YES )
817     {
818         NSMenuItem * o_lmi_tmp;
819         o_lmi_tmp = [o_menu addItemWithTitle: _NS("Open File...") action: @selector(addSubtitleFile:) keyEquivalent: @""];
820         [o_lmi_tmp setTarget: self];
821         [o_lmi_tmp setEnabled: YES];
822         [o_parent setEnabled: YES];
823         [o_menu addItem: [NSMenuItem separatorItem]];
824     }
825
826     for( i = 0; i < val_list.p_list->i_count; i++ )
827     {
828         vlc_value_t another_val;
829         NSMenuItem * o_lmi;
830         NSString *o_title = @"";
831         VLCAutoGeneratedMenuContent *o_data;
832
833         switch( i_type & VLC_VAR_TYPE )
834         {
835         case VLC_VAR_STRING:
836             another_val.psz_string =
837                 strdup(val_list.p_list->p_values[i].psz_string);
838
839             o_title = [[VLCMain sharedInstance] localizedString: text_list.p_list->p_values[i].psz_string ?
840                 text_list.p_list->p_values[i].psz_string : val_list.p_list->p_values[i].psz_string ];
841
842             o_lmi = [o_menu addItemWithTitle: o_title action: pf_callback keyEquivalent: @""];
843             o_data = [[VLCAutoGeneratedMenuContent alloc] initWithVariableName: strdup(psz_variable) ofObject: p_object
844                     andValue: another_val ofType: i_type];
845             [o_lmi setRepresentedObject: [o_data autorelease]];
846             [o_lmi setTarget: self];
847
848             if( !strcmp( val.psz_string, val_list.p_list->p_values[i].psz_string ) && !( i_type & VLC_VAR_ISCOMMAND ) )
849                 [o_lmi setState: TRUE ];
850
851             break;
852
853         case VLC_VAR_INTEGER:
854
855              o_title = text_list.p_list->p_values[i].psz_string ?
856                                  [[VLCMain sharedInstance] localizedString: strdup( text_list.p_list->p_values[i].psz_string )] :
857                                  [NSString stringWithFormat: @"%d",
858                                  val_list.p_list->p_values[i].i_int];
859
860             o_lmi = [o_menu addItemWithTitle: o_title action: pf_callback keyEquivalent: @""];
861             o_data = [[VLCAutoGeneratedMenuContent alloc] initWithVariableName: strdup(psz_variable) ofObject: p_object
862                     andValue: val_list.p_list->p_values[i] ofType: i_type];
863             [o_lmi setRepresentedObject: [o_data autorelease]];
864             [o_lmi setTarget: self];
865
866             if( val_list.p_list->p_values[i].i_int == val.i_int && !( i_type & VLC_VAR_ISCOMMAND ) )
867                 [o_lmi setState: TRUE ];
868             break;
869
870         default:
871           break;
872         }
873     }
874
875     /* special case for the subtitles sub-menu
876      * In case that we don't have any subs, we don't want a separator item at the end */
877     if( [[o_parent title] isEqualToString: _NS("Subtitles Track")] == YES )
878     {
879         if( [o_menu numberOfItems] == 2 )
880             [o_menu removeItemAtIndex: 1];
881     }
882
883     /* clean up everything */
884     if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
885     var_FreeList( &val_list, &text_list );
886 }
887
888 - (IBAction)toggleVar:(id)sender
889 {
890     NSMenuItem *o_mi = (NSMenuItem *)sender;
891     VLCAutoGeneratedMenuContent *o_data = [o_mi representedObject];
892     [NSThread detachNewThreadSelector: @selector(toggleVarThread:)
893         toTarget: self withObject: o_data];
894
895     return;
896 }
897
898 - (int)toggleVarThread: (id)data
899 {
900     vlc_object_t *p_object;
901     NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
902
903     assert([data isKindOfClass:[VLCAutoGeneratedMenuContent class]]);
904     VLCAutoGeneratedMenuContent *menuContent = (VLCAutoGeneratedMenuContent *)data;
905
906     vlc_thread_set_priority( VLCIntf , VLC_THREAD_PRIORITY_LOW );
907
908     p_object = [menuContent vlcObject];
909
910     if( p_object != NULL )
911     {
912         var_Set( p_object, [menuContent name], [menuContent value] );
913         vlc_object_release( p_object );
914         [o_pool release];
915         return true;
916     }
917     [o_pool release];
918     return VLC_EGENERIC;
919 }
920
921 - (IBAction)goToSpecificTime:(id)sender
922 {
923     if( sender == o_specificTime_cancel_btn )
924     {
925         [NSApp endSheet: o_specificTime_win];
926         [o_specificTime_win close];
927     }
928     else if( sender == o_specificTime_ok_btn )
929     {
930         input_thread_t * p_input = pl_CurrentInput( VLCIntf );
931         if( p_input )
932         {
933             unsigned int timeInSec = 0;
934             NSString * fieldContent = [o_specificTime_enter_fld stringValue];
935             if( [[fieldContent componentsSeparatedByString: @":"] count] > 1 &&
936                 [[fieldContent componentsSeparatedByString: @":"] count] <= 3 )
937             {
938                 NSArray * ourTempArray = \
939                     [fieldContent componentsSeparatedByString: @":"];
940
941                 if( [[fieldContent componentsSeparatedByString: @":"] count] == 3 )
942                 {
943                     timeInSec += ([[ourTempArray objectAtIndex: 0] intValue] * 3600); //h
944                     timeInSec += ([[ourTempArray objectAtIndex: 1] intValue] * 60); //m
945                     timeInSec += [[ourTempArray objectAtIndex: 2] intValue];        //s
946                 }
947                 else
948                 {
949                     timeInSec += ([[ourTempArray objectAtIndex: 0] intValue] * 60); //m
950                     timeInSec += [[ourTempArray objectAtIndex: 1] intValue]; //s
951                 }
952             }
953             else
954                 timeInSec = [fieldContent intValue];
955
956             input_Control( p_input, INPUT_SET_TIME, (int64_t)(timeInSec * 1000000));
957             vlc_object_release( p_input );
958         }
959
960         [NSApp endSheet: o_specificTime_win];
961         [o_specificTime_win close];
962     }
963     else
964     {
965         input_thread_t * p_input = pl_CurrentInput( VLCIntf );
966         if( p_input )
967         {
968             /* we can obviously only do that if an input is available */
969             vlc_value_t pos, length;
970             var_Get( p_input, "time", &pos );
971             [o_specificTime_enter_fld setIntValue: (pos.i_time / 1000000)];
972             var_Get( p_input, "length", &length );
973             [o_specificTime_stepper setMaxValue: (length.i_time / 1000000)];
974
975             [NSApp beginSheet: o_specificTime_win modalForWindow: \
976                 [NSApp mainWindow] modalDelegate: self didEndSelector: nil \
977                 contextInfo: nil];
978             [o_specificTime_win makeKeyWindow];
979             vlc_object_release( p_input );
980         }
981     }
982 }
983
984 - (id)fspanel
985 {
986     if( o_fs_panel )
987         return o_fs_panel;
988     else
989     {
990         msg_Err( VLCIntf, "FSPanel is nil" );
991         return NULL;
992     }
993 }
994
995 @end
996
997 @implementation VLCControls (NSMenuValidation)
998
999 - (BOOL)validateMenuItem:(NSMenuItem *)o_mi
1000 {
1001     BOOL bEnabled = TRUE;
1002     vlc_value_t val;
1003     intf_thread_t * p_intf = VLCIntf;
1004     playlist_t * p_playlist = pl_Hold( p_intf );
1005     input_thread_t * p_input = playlist_CurrentInput( p_playlist );
1006
1007     if( [[o_mi title] isEqualToString: _NS("Faster")] ||
1008         [[o_mi title] isEqualToString: _NS("Slower")] )
1009     {
1010         if( p_input != NULL )
1011         {
1012             bEnabled = var_GetBool( p_input, "can-rate" );
1013         }
1014         else
1015         {
1016             bEnabled = FALSE;
1017         }
1018     }
1019     else if( [[o_mi title] isEqualToString: _NS("Stop")] )
1020     {
1021         if( p_input == NULL )
1022         {
1023             bEnabled = FALSE;
1024         }
1025         [o_main setupMenus]; /* Make sure input menu is up to date */
1026     }
1027     else if( [[o_mi title] isEqualToString: _NS("Previous")] ||
1028              [[o_mi title] isEqualToString: _NS("Next")] )
1029     {
1030         PL_LOCK;
1031         bEnabled = playlist_CurrentSize( p_playlist ) > 1;
1032         PL_UNLOCK;
1033     }
1034     else if( [[o_mi title] isEqualToString: _NS("Random")] )
1035     {
1036         int i_state;
1037         var_Get( p_playlist, "random", &val );
1038         i_state = val.b_bool ? NSOnState : NSOffState;
1039         [o_mi setState: i_state];
1040     }
1041     else if( [[o_mi title] isEqualToString: _NS("Repeat One")] )
1042     {
1043         int i_state;
1044         var_Get( p_playlist, "repeat", &val );
1045         i_state = val.b_bool ? NSOnState : NSOffState;
1046         [o_mi setState: i_state];
1047     }
1048     else if( [[o_mi title] isEqualToString: _NS("Repeat All")] )
1049     {
1050         int i_state;
1051         var_Get( p_playlist, "loop", &val );
1052         i_state = val.b_bool ? NSOnState : NSOffState;
1053         [o_mi setState: i_state];
1054     }
1055     else if( [[o_mi title] isEqualToString: _NS("Step Forward")] ||
1056              [[o_mi title] isEqualToString: _NS("Step Backward")] ||
1057              [[o_mi title] isEqualToString: _NS("Jump To Time")])
1058     {
1059         if( p_input != NULL )
1060         {
1061             var_Get( p_input, "can-seek", &val);
1062             bEnabled = val.b_bool;
1063         }
1064         else bEnabled = FALSE;
1065     }
1066     else if( [[o_mi title] isEqualToString: _NS("Mute")] )
1067     {
1068         [o_mi setState: p_intf->p_sys->b_mute ? NSOnState : NSOffState];
1069         [o_main setupMenus]; /* Make sure audio menu is up to date */
1070     }
1071     else if( [[o_mi title] isEqualToString: _NS("Half Size")] ||
1072                 [[o_mi title] isEqualToString: _NS("Normal Size")] ||
1073                 [[o_mi title] isEqualToString: _NS("Double Size")] ||
1074                 [[o_mi title] isEqualToString: _NS("Fit to Screen")] ||
1075                 [[o_mi title] isEqualToString: _NS("Snapshot")] ||
1076                 [[o_mi title] isEqualToString: _NS("Fullscreen")] ||
1077                 [[o_mi title] isEqualToString: _NS("Float on Top")] )
1078     {
1079         id o_window;
1080         NSArray *o_windows = [NSApp orderedWindows];
1081         NSEnumerator *o_enumerator = [o_windows objectEnumerator];
1082         bEnabled = FALSE;
1083  
1084         if( p_input != NULL )
1085         {
1086             vout_thread_t *p_vout = input_GetVout( p_input );
1087             if( p_vout != NULL )
1088             {
1089                 if( [[o_mi title] isEqualToString: _NS("Float on Top")] )
1090                 {
1091                     var_Get( p_vout, "video-on-top", &val );
1092                     [o_mi setState: val.b_bool ?  NSOnState : NSOffState];
1093                 }
1094     
1095                 while( (o_window = [o_enumerator nextObject]))
1096                 {
1097                     if( [[o_window className] isEqualToString: @"VLCVoutWindow"] ||
1098                                 [[[VLCMain sharedInstance] embeddedList]
1099                                 windowContainsEmbedded: o_window])
1100                     {
1101                         bEnabled = TRUE;
1102                         break;
1103                     }
1104                 }
1105     
1106                 vlc_object_release( (vlc_object_t *)p_vout );
1107             }
1108             vlc_object_release( p_input );
1109         }
1110         if( [[o_mi title] isEqualToString: _NS("Fullscreen")] )
1111         {
1112             var_Get( p_playlist, "fullscreen", &val );
1113             [o_mi setState: val.b_bool];
1114             bEnabled = TRUE;
1115         }
1116         [o_main setupMenus]; /* Make sure video menu is up to date */
1117     }
1118
1119     /* Special case for telx menu */
1120     if( [[o_mi title] isEqualToString: _NS("Normal Size")] )
1121     {
1122         NSMenuItem *item = [[o_mi menu] itemWithTitle:_NS("Teletext")];
1123                 bool b_telx = p_input && var_GetInteger( p_input, "teletext-es" ) >= 0;
1124
1125         [[item submenu] setAutoenablesItems:NO];
1126         for( int k=0; k < [[item submenu] numberOfItems]; k++ )
1127         {
1128             [[[item submenu] itemAtIndex:k] setEnabled: b_telx];
1129         }
1130     }
1131
1132     if( p_input ) vlc_object_release( p_input );
1133     pl_Release( p_intf );
1134
1135     return( bEnabled );
1136 }
1137
1138 @end
1139
1140 /*****************************************************************************
1141  * VLCAutoGeneratedMenuContent implementation
1142  *****************************************************************************
1143  * Object connected to a playlistitem which remembers the data belonging to
1144  * the variable of the autogenerated menu
1145  *****************************************************************************/
1146 @implementation VLCAutoGeneratedMenuContent
1147
1148 -(id) initWithVariableName:(const char *)name ofObject:(vlc_object_t *)object
1149         andValue:(vlc_value_t)val ofType:(int)type
1150 {
1151     self = [super init];
1152
1153     if( self != nil )
1154     {
1155         psz_name = strdup( name );
1156         _vlc_object = vlc_object_hold( object );
1157         value = val;
1158         i_type = type;
1159     }
1160
1161     return( self );
1162 }
1163
1164 - (void)dealloc
1165 {
1166     vlc_object_release( _vlc_object );
1167     free( psz_name );
1168     [super dealloc];
1169 }
1170
1171 - (const char *)name
1172 {
1173     return psz_name;
1174 }
1175
1176 - (vlc_value_t)value
1177 {
1178     return value;
1179 }
1180
1181 - (vlc_object_t *)vlcObject
1182 {
1183     return vlc_object_hold( _vlc_object );
1184 }
1185
1186
1187 - (int)type
1188 {
1189     return i_type;
1190 }
1191
1192 @end
1193
1194
1195 /*****************************************************************************
1196  * VLCTimeField implementation
1197  *****************************************************************************
1198  * we need this to catch our click-event in the controller window
1199  *****************************************************************************/
1200
1201 @implementation VLCTimeField
1202 - (void)mouseDown: (NSEvent *)ourEvent
1203 {
1204     if( [ourEvent clickCount] > 1 )
1205         [[[VLCMain sharedInstance] controls] goToSpecificTime: nil];
1206     else
1207         [[VLCMain sharedInstance] timeFieldWasClicked: self];
1208 }
1209 @end