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