Make the global b_can_pace_control flag public
[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: @"VLCWindow"] )
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)windowAction:(id)sender
387 {
388     NSString *o_title = [sender title];
389
390     vout_thread_t *p_vout = vlc_object_find( VLCIntf, VLC_OBJECT_VOUT,
391                                               FIND_ANYWHERE );
392     if( p_vout != NULL )
393     {
394         id o_vout_view = [self getVoutView];
395         if( o_vout_view )
396         {
397             if( [o_title isEqualToString: _NS("Half Size") ] )
398                 [o_vout_view scaleWindowWithFactor: 0.5];
399             else if( [o_title isEqualToString: _NS("Normal Size") ] )
400                 [o_vout_view scaleWindowWithFactor: 1.0];
401             else if( [o_title isEqualToString: _NS("Double Size") ] )
402                 [o_vout_view scaleWindowWithFactor: 2.0];
403             else if( [o_title isEqualToString: _NS("Float on Top") ] )
404                 [o_vout_view toggleFloatOnTop];
405             else if( [o_title isEqualToString: _NS("Fit to Screen") ] )
406             {
407                 id o_window = [o_vout_view getWindow];
408                 if( ![o_window isZoomed] )
409                     [o_window performZoom:self];
410             }
411             else if( [o_title isEqualToString: _NS("Snapshot") ] )
412             {
413                 [o_vout_view snapshot];
414             }
415             else
416             {
417                 [o_vout_view toggleFullscreen];
418             }
419         }
420         vlc_object_release( (vlc_object_t *)p_vout );
421     }
422     else
423     {
424         playlist_t * p_playlist = pl_Yield( VLCIntf );
425
426         if( [o_title isEqualToString: _NS("Fullscreen")] ||
427             [sender isKindOfClass:[NSButton class]] )
428         {
429             vlc_value_t val;
430             var_Get( p_playlist, "fullscreen", &val );
431             var_Set( p_playlist, "fullscreen", (vlc_value_t)!val.b_bool );
432         }
433
434         vlc_object_release( p_playlist );
435     }
436
437 }
438
439 - (BOOL)keyEvent:(NSEvent *)o_event
440 {
441     BOOL eventHandled = NO;
442     unichar key = [[o_event charactersIgnoringModifiers] characterAtIndex: 0];
443
444     if( key )
445     {
446         vout_thread_t *p_vout = vlc_object_find( VLCIntf, VLC_OBJECT_VOUT,
447                                               FIND_ANYWHERE );
448         if( p_vout != NULL )
449         {
450             /* Escape */
451             if( key == (unichar) 0x1b )
452             {
453                 id o_vout_view = [self getVoutView];
454                 if( o_vout_view && [o_vout_view isFullscreen] )
455                 {
456                     [o_vout_view toggleFullscreen];
457                     eventHandled = YES;
458                 }
459             }
460             else if( key == ' ' )
461             {
462                 [self play:self];
463                 eventHandled = YES;
464             }
465             vlc_object_release( (vlc_object_t *)p_vout );
466         }
467     }
468     return eventHandled;
469 }
470
471 - (void)setupVarMenuItem:(NSMenuItem *)o_mi
472                     target:(vlc_object_t *)p_object
473                     var:(const char *)psz_variable
474                     selector:(SEL)pf_callback
475 {
476     vlc_value_t val, text;
477     int i_type = var_Type( p_object, psz_variable );
478
479     switch( i_type & VLC_VAR_TYPE )
480     {
481     case VLC_VAR_VOID:
482     case VLC_VAR_BOOL:
483     case VLC_VAR_VARIABLE:
484     case VLC_VAR_STRING:
485     case VLC_VAR_INTEGER:
486         break;
487     default:
488         /* Variable doesn't exist or isn't handled */
489         return;
490     }
491     
492     /* Make sure we want to display the variable */
493     if( i_type & VLC_VAR_HASCHOICE )
494     {
495         var_Change( p_object, psz_variable, VLC_VAR_CHOICESCOUNT, &val, NULL );
496         if( val.i_int == 0 ) return;
497         if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE && val.i_int == 1 )
498             return;
499     }
500     
501     /* Get the descriptive name of the variable */
502     var_Change( p_object, psz_variable, VLC_VAR_GETTEXT, &text, NULL );
503     [o_mi setTitle: [[VLCMain sharedInstance] localizedString: text.psz_string ?
504                                         text.psz_string : strdup( psz_variable ) ]];
505
506     var_Get( p_object, psz_variable, &val );
507     if( i_type & VLC_VAR_HASCHOICE )
508     {
509         NSMenu *o_menu = [o_mi submenu];
510
511         [self setupVarMenu: o_menu forMenuItem: o_mi target:p_object
512                         var:psz_variable selector:pf_callback];
513         
514         if( text.psz_string ) free( text.psz_string );
515         return;
516     }
517
518     VLCMenuExt *o_data;
519     switch( i_type & VLC_VAR_TYPE )
520     {
521     case VLC_VAR_VOID:
522         o_data = [[VLCMenuExt alloc] initWithVar: psz_variable Object: p_object->i_object_id
523                 Value: val ofType: i_type];
524         [o_mi setRepresentedObject: [NSValue valueWithPointer:[o_data retain]]];
525         break;
526
527     case VLC_VAR_BOOL:
528         o_data = [[VLCMenuExt alloc] initWithVar: psz_variable Object: p_object->i_object_id
529                 Value: val ofType: i_type];
530         [o_mi setRepresentedObject: [NSValue valueWithPointer:[o_data retain]]];
531         if( !( i_type & VLC_VAR_ISCOMMAND ) )
532             [o_mi setState: val.b_bool ? TRUE : FALSE ];
533         break;
534
535     default:
536         if( text.psz_string ) free( text.psz_string );
537         return;
538     }
539
540     if( ( i_type & VLC_VAR_TYPE ) == VLC_VAR_STRING ) free( val.psz_string );
541     if( text.psz_string ) free( text.psz_string );
542 }
543
544
545 - (void)setupVarMenu:(NSMenu *)o_menu
546                     forMenuItem: (NSMenuItem *)o_parent
547                     target:(vlc_object_t *)p_object
548                     var:(const char *)psz_variable
549                     selector:(SEL)pf_callback
550 {
551     vlc_value_t val, val_list, text_list;
552     int i_type, i, i_nb_items;
553
554     /* remove previous items */
555     i_nb_items = [o_menu numberOfItems];
556     for( i = 0; i < i_nb_items; i++ )
557     {
558         [o_menu removeItemAtIndex: 0];
559     }
560
561     /* Check the type of the object variable */
562     i_type = var_Type( p_object, psz_variable );
563
564     /* Make sure we want to display the variable */
565     if( i_type & VLC_VAR_HASCHOICE )
566     {
567         var_Change( p_object, psz_variable, VLC_VAR_CHOICESCOUNT, &val, NULL );
568         if( val.i_int == 0 ) return;
569         if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE && val.i_int == 1 )
570             return;
571     }
572     else
573     {
574         return;
575     }
576
577     switch( i_type & VLC_VAR_TYPE )
578     {
579     case VLC_VAR_VOID:
580     case VLC_VAR_BOOL:
581     case VLC_VAR_VARIABLE:
582     case VLC_VAR_STRING:
583     case VLC_VAR_INTEGER:
584         break;
585     default:
586         /* Variable doesn't exist or isn't handled */
587         return;
588     }
589
590     if( var_Get( p_object, psz_variable, &val ) < 0 )
591     {
592         return;
593     }
594
595     if( var_Change( p_object, psz_variable, VLC_VAR_GETLIST,
596                     &val_list, &text_list ) < 0 )
597     {
598         if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
599         return;
600     }
601
602     /* make (un)sensitive */
603     [o_parent setEnabled: ( val_list.p_list->i_count > 1 )];
604
605     for( i = 0; i < val_list.p_list->i_count; i++ )
606     {
607         vlc_value_t another_val;
608         NSMenuItem * o_lmi;
609         NSString *o_title = @"";
610         VLCMenuExt *o_data;
611
612         switch( i_type & VLC_VAR_TYPE )
613         {
614         case VLC_VAR_STRING:
615             another_val.psz_string =
616                 strdup(val_list.p_list->p_values[i].psz_string);
617
618             o_title = [[VLCMain sharedInstance] localizedString: text_list.p_list->p_values[i].psz_string ?
619                 text_list.p_list->p_values[i].psz_string : val_list.p_list->p_values[i].psz_string ];
620
621             o_lmi = [o_menu addItemWithTitle: o_title action: pf_callback keyEquivalent: @""];
622             o_data = [[VLCMenuExt alloc] initWithVar: strdup(psz_variable) Object: p_object->i_object_id
623                     Value: another_val ofType: i_type];
624             [o_lmi setRepresentedObject: [NSValue valueWithPointer:[o_data retain]]];
625             [o_lmi setTarget: self];
626
627             if( !strcmp( val.psz_string, val_list.p_list->p_values[i].psz_string ) && !( i_type & VLC_VAR_ISCOMMAND ) )
628                 [o_lmi setState: TRUE ];
629
630             break;
631
632         case VLC_VAR_INTEGER:
633
634              o_title = text_list.p_list->p_values[i].psz_string ?
635                                  [[VLCMain sharedInstance] localizedString: strdup( text_list.p_list->p_values[i].psz_string )] :
636                                  [NSString stringWithFormat: @"%d",
637                                  val_list.p_list->p_values[i].i_int];
638
639             o_lmi = [[o_menu addItemWithTitle: o_title action: pf_callback keyEquivalent: @""] retain ];
640             o_data = [[VLCMenuExt alloc] initWithVar: strdup(psz_variable) Object: p_object->i_object_id
641                     Value: val_list.p_list->p_values[i] ofType: i_type];
642             [o_lmi setRepresentedObject: [NSValue valueWithPointer:[ o_data retain]]];
643             [o_lmi setTarget: self];
644
645             if( val_list.p_list->p_values[i].i_int == val.i_int && !( i_type & VLC_VAR_ISCOMMAND ) )
646                 [o_lmi setState: TRUE ];
647             break;
648
649         default:
650           break;
651         }
652     }
653
654     /* clean up everything */
655     if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
656     var_Change( p_object, psz_variable, VLC_VAR_FREELIST, &val_list, &text_list );
657 }
658
659 - (IBAction)toggleVar:(id)sender
660 {
661     NSMenuItem *o_mi = (NSMenuItem *)sender;
662     VLCMenuExt *o_data = [[o_mi representedObject] pointerValue];
663     [NSThread detachNewThreadSelector: @selector(toggleVarThread:)
664         toTarget: self withObject: o_data];
665
666     return;
667 }
668
669 - (int)toggleVarThread: (id)_o_data
670 {
671     vlc_object_t *p_object;
672     NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
673     VLCMenuExt *o_data = (VLCMenuExt *)_o_data;
674
675     vlc_thread_set_priority( VLCIntf , VLC_THREAD_PRIORITY_LOW );
676
677     p_object = (vlc_object_t *)vlc_object_get( VLCIntf,
678                                     [o_data objectID] );
679
680     if( p_object != NULL )
681     {
682         var_Set( p_object, strdup([o_data name]), [o_data value] );
683         vlc_object_release( p_object );
684         [o_pool release];
685         return VLC_TRUE;
686     }
687     [o_pool release];
688     return VLC_EGENERIC;
689 }
690
691 - (IBAction)goToSpecificTime:(id)sender
692 {
693     if( sender == o_specificTime_cancel_btn )
694     {
695         [NSApp endSheet: o_specificTime_win];
696         [o_specificTime_win close];
697     }
698     else if( sender == o_specificTime_ok_btn )
699     {
700         input_thread_t * p_input = (input_thread_t *)vlc_object_find( VLCIntf, \
701             VLC_OBJECT_INPUT, FIND_ANYWHERE );
702         if( p_input )
703         {
704             unsigned int timeInSec = 0;
705             NSString * fieldContent = [o_specificTime_enter_fld stringValue];
706             if( [[fieldContent componentsSeparatedByString: @":"] count] > 1 && 
707                 [[fieldContent componentsSeparatedByString: @":"] count] <= 3 )
708             {
709                 NSArray * ourTempArray = \
710                     [fieldContent componentsSeparatedByString: @":"];
711
712                 if( [[fieldContent componentsSeparatedByString: @":"] count] == 3 )
713                 {
714                     timeInSec += ([[ourTempArray objectAtIndex: 0] intValue] * 3600); //h
715                     timeInSec += ([[ourTempArray objectAtIndex: 1] intValue] * 60); //m
716                     timeInSec += [[ourTempArray objectAtIndex: 2] intValue];        //s
717                 }
718                 else
719                 {
720                     timeInSec += ([[ourTempArray objectAtIndex: 0] intValue] * 60); //m
721                     timeInSec += [[ourTempArray objectAtIndex: 1] intValue]; //s
722                 }
723             }
724             else
725                 timeInSec = [fieldContent intValue];
726
727             input_Control( p_input, INPUT_SET_TIME, (int64_t)(timeInSec * 1000000));
728             vlc_object_release( p_input );
729         }
730     
731         [NSApp endSheet: o_specificTime_win];
732         [o_specificTime_win close];
733     }
734     else
735     {
736         input_thread_t * p_input = (input_thread_t *)vlc_object_find( VLCIntf, \
737             VLC_OBJECT_INPUT, FIND_ANYWHERE );
738         if( p_input )
739         {
740             /* we can obviously only do that if an input is available */
741             vlc_value_t pos, length;
742             var_Get( p_input, "time", &pos );
743             [o_specificTime_enter_fld setIntValue: (pos.i_time / 1000000)];
744             var_Get( p_input, "length", &length );
745             [o_specificTime_stepper setMaxValue: (length.i_time / 1000000)];
746
747             [NSApp beginSheet: o_specificTime_win modalForWindow: \
748                 [NSApp mainWindow] modalDelegate: self didEndSelector: nil \
749                 contextInfo: nil];
750             [o_specificTime_win makeKeyWindow];
751             vlc_object_release( p_input );
752         }
753     }
754 }
755
756 - (id)getFSPanel
757 {
758     if( o_fs_panel )
759         return o_fs_panel;
760     else
761     {
762         msg_Err( VLCIntf, "FSPanel is nil" );
763         return NULL;
764     }
765 }
766
767 @end
768
769 @implementation VLCControls (NSMenuValidation)
770
771 - (BOOL)validateMenuItem:(NSMenuItem *)o_mi
772 {
773     BOOL bEnabled = TRUE;
774     vlc_value_t val;
775     intf_thread_t * p_intf = VLCIntf;
776     playlist_t * p_playlist = pl_Yield( p_intf );
777
778     vlc_mutex_lock( &p_playlist->object_lock );
779
780 #define p_input p_playlist->p_input
781
782     if( [[o_mi title] isEqualToString: _NS("Faster")] ||
783         [[o_mi title] isEqualToString: _NS("Slower")] )
784     {
785         if( p_input != NULL )
786         {
787             bEnabled = p_input->b_can_pace_control;
788         }
789         else
790         {
791             bEnabled = FALSE;
792         }
793     }
794     else if( [[o_mi title] isEqualToString: _NS("Stop")] )
795     {
796         if( p_input == NULL )
797         {
798             bEnabled = FALSE;
799         }
800         [o_main setupMenus]; /* Make sure input menu is up to date */
801     }
802     else if( [[o_mi title] isEqualToString: _NS("Previous")] ||
803              [[o_mi title] isEqualToString: _NS("Next")] )
804     {
805             /** \todo fix i_size use */
806             bEnabled = p_playlist->items.i_size > 1;
807     }
808     else if( [[o_mi title] isEqualToString: _NS("Random")] )
809     {
810         int i_state;
811         var_Get( p_playlist, "random", &val );
812         i_state = val.b_bool ? NSOnState : NSOffState;
813         [o_mi setState: i_state];
814     }
815     else if( [[o_mi title] isEqualToString: _NS("Repeat One")] )
816     {
817         int i_state;
818         var_Get( p_playlist, "repeat", &val );
819         i_state = val.b_bool ? NSOnState : NSOffState;
820         [o_mi setState: i_state];
821     }
822     else if( [[o_mi title] isEqualToString: _NS("Repeat All")] )
823     {
824         int i_state;
825         var_Get( p_playlist, "loop", &val );
826         i_state = val.b_bool ? NSOnState : NSOffState;
827         [o_mi setState: i_state];
828     }
829     else if( [[o_mi title] isEqualToString: _NS("Step Forward")] ||
830              [[o_mi title] isEqualToString: _NS("Step Backward")] ||
831              [[o_mi title] isEqualToString: _NS("Jump To Time")])
832     {
833         if( p_input != NULL )
834         {
835             var_Get( p_input, "seekable", &val);
836             bEnabled = val.b_bool;
837         }
838         else bEnabled = FALSE;
839     }
840     else if( [[o_mi title] isEqualToString: _NS("Mute")] )
841     {
842         [o_mi setState: p_intf->p_sys->b_mute ? NSOnState : NSOffState];
843         [o_main setupMenus]; /* Make sure audio menu is up to date */
844     }
845     else if( [[o_mi title] isEqualToString: _NS("Half Size")] ||
846                 [[o_mi title] isEqualToString: _NS("Normal Size")] ||
847                 [[o_mi title] isEqualToString: _NS("Double Size")] ||
848                 [[o_mi title] isEqualToString: _NS("Fit to Screen")] ||
849                 [[o_mi title] isEqualToString: _NS("Snapshot")] ||
850                 [[o_mi title] isEqualToString: _NS("Fullscreen")] ||
851                 [[o_mi title] isEqualToString: _NS("Float on Top")] )
852     {
853         id o_window;
854         NSArray *o_windows = [NSApp orderedWindows];
855         NSEnumerator *o_enumerator = [o_windows objectEnumerator];
856         bEnabled = FALSE;
857         
858         vout_thread_t   *p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT,
859                                               FIND_ANYWHERE );
860         if( p_vout != NULL )
861         {
862             if( [[o_mi title] isEqualToString: _NS("Float on Top")] )
863             {
864                 var_Get( p_vout, "video-on-top", &val );
865                 [o_mi setState: val.b_bool ?  NSOnState : NSOffState];
866             }
867
868             while( (o_window = [o_enumerator nextObject]))
869             {
870                 if( [[o_window className] isEqualToString: @"VLCWindow"] ||
871                             [[[VLCMain sharedInstance] getEmbeddedList]
872                             windowContainsEmbedded: o_window])
873                 {
874                     bEnabled = TRUE;
875                     break;
876                 }
877             }
878             vlc_object_release( (vlc_object_t *)p_vout );
879         }
880         else if( [[o_mi title] isEqualToString: _NS("Fullscreen")] )
881         {
882             var_Get( p_playlist, "fullscreen", &val );
883             [o_mi setState: val.b_bool];
884             bEnabled = TRUE;
885         }
886         [o_main setupMenus]; /* Make sure video menu is up to date */
887     }
888
889     vlc_mutex_unlock( &p_playlist->object_lock );
890     vlc_object_release( p_playlist );
891
892     return( bEnabled );
893 }
894
895 @end
896
897 /*****************************************************************************
898  * VLCMenuExt implementation 
899  *****************************************************************************
900  * Object connected to a playlistitem which remembers the data belonging to
901  * the variable of the autogenerated menu
902  *****************************************************************************/
903 @implementation VLCMenuExt
904
905 - (id)initWithVar: (const char *)_psz_name Object: (int)i_id
906         Value: (vlc_value_t)val ofType: (int)_i_type
907 {
908     self = [super init];
909
910     if( self != nil )
911     {
912         psz_name = strdup( _psz_name );
913         i_object_id = i_id;
914         value = val;
915         i_type = _i_type;
916     }
917
918     return( self );
919 }
920
921 - (void)dealloc
922 {
923     free( psz_name );
924     [super dealloc];
925 }
926
927 - (char *)name
928 {
929     return psz_name;
930 }
931
932 - (int)objectID
933 {
934     return i_object_id;
935 }
936
937 - (vlc_value_t)value
938 {
939     return value;
940 }
941
942 - (int)type
943 {
944     return i_type;
945 }
946
947 @end
948
949
950 /*****************************************************************************
951  * VLCTimeField implementation 
952  *****************************************************************************
953  * we need this to catch our click-event in the controller window
954  *****************************************************************************/
955
956 @implementation VLCTimeField
957 - (void)mouseDown: (NSEvent *)ourEvent
958 {
959     if( [ourEvent clickCount] > 1 )
960         [[[VLCMain sharedInstance] getControls] goToSpecificTime: nil];
961 }
962 @end