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