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