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