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