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