macosx: vlc object handling improvements (refs #6883)
[vlc.git] / modules / gui / macosx / MainMenu.m
1 /*****************************************************************************
2  * MainMenu.m: MacOS X interface module
3  *****************************************************************************
4  * Copyright (C) 2011-2012 Felix Paul Kühne
5  * $Id$
6  *
7  * Authors: Felix Paul Kühne <fkuehne -at- videolan -dot- org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #import "MainMenu.h"
25 #import <vlc_common.h>
26 #import <vlc_playlist.h>
27
28 #import "intf.h"
29 #import "open.h"
30 #import "wizard.h"
31 #import "about.h"
32 #import "AudioEffects.h"
33 #import "TrackSynchronization.h"
34 #import "VideoEffects.h"
35 #import "bookmarks.h"
36 #import "simple_prefs.h"
37 #import "coredialogs.h"
38 #import "controls.h"
39 #import "playlist.h"
40 #import "playlistinfo.h"
41 #import "VideoView.h"
42 #import "CoreInteraction.h"
43 #import "MainWindow.h"
44 #import "ExtensionsManager.h"
45 #import "ConvertAndSave.h"
46
47 @implementation VLCMainMenu
48 static VLCMainMenu *_o_sharedInstance = nil;
49
50 + (VLCMainMenu *)sharedInstance
51 {
52     return _o_sharedInstance ? _o_sharedInstance : [[self alloc] init];
53 }
54
55 #pragma mark -
56 #pragma mark Initialization
57
58 - (id)init
59 {
60     if( _o_sharedInstance)
61     {
62         [self dealloc];
63         return _o_sharedInstance;
64     }
65     else
66     {
67         _o_sharedInstance = [super init];
68     }
69
70     return _o_sharedInstance;
71 }
72
73 - (void)dealloc
74 {
75     [[NSNotificationCenter defaultCenter] removeObserver: self];
76
77     if (b_nib_about_loaded)
78         [o_about release];
79
80     if (b_nib_videoeffects_loaded)
81         [o_videoeffects release];
82
83     if (b_nib_audioeffects_loaded)
84         [o_audioeffects release];
85
86     if (b_nib_tracksynchro_loaded)
87         [o_trackSynchronization release];
88
89     if (b_nib_convertandsave_loaded)
90         [o_convertandsave release];
91
92     [o_extMgr release];
93
94     [super dealloc];
95 }
96
97 - (void)awakeFromNib
98 {
99     [[NSNotificationCenter defaultCenter] addObserver: self
100                                              selector: @selector(applicationWillFinishLaunching:)
101                                                  name: NSApplicationWillFinishLaunchingNotification
102                                                object: nil];
103 }
104
105 - (void)applicationWillFinishLaunching:(NSNotification *)o_notification
106 {
107     p_intf = VLCIntf;
108
109     NSString* o_key;
110     playlist_t *p_playlist;
111     vlc_value_t val;
112     id o_vlcmain = [VLCMain sharedInstance];
113     char * key;
114
115     /* Check if we already did this once. Opening the other nibs calls it too,
116      because VLCMain is the owner */
117     if( b_mainMenu_setup ) return;
118
119     /* Get ExtensionsManager */
120     o_extMgr = [ExtensionsManager getInstance:p_intf];
121     [o_extMgr retain];
122
123     [self initStrings];
124
125     key = config_GetPsz( p_intf, "key-quit" );
126     o_key = [NSString stringWithFormat:@"%s", key];
127     [o_mi_quit setKeyEquivalent: [o_vlcmain VLCKeyToString: o_key]];
128     [o_mi_quit setKeyEquivalentModifierMask: [o_vlcmain VLCModifiersToCocoa:o_key]];
129     FREENULL( key );
130
131     key = config_GetPsz( p_intf, "key-play-pause" );
132     o_key = [NSString stringWithFormat:@"%s", key];
133     [o_mi_play setKeyEquivalent: [o_vlcmain VLCKeyToString: o_key]];
134     [o_mi_play setKeyEquivalentModifierMask: [o_vlcmain VLCModifiersToCocoa:o_key]];
135     FREENULL( key );
136
137     key = config_GetPsz( p_intf, "key-stop" );
138     o_key = [NSString stringWithFormat:@"%s", key];
139     [o_mi_stop setKeyEquivalent: [o_vlcmain VLCKeyToString: o_key]];
140     [o_mi_stop setKeyEquivalentModifierMask: [o_vlcmain VLCModifiersToCocoa:o_key]];
141     FREENULL( key );
142
143     key = config_GetPsz( p_intf, "key-prev" );
144     o_key = [NSString stringWithFormat:@"%s", key];
145     [o_mi_previous setKeyEquivalent: [o_vlcmain VLCKeyToString: o_key]];
146     [o_mi_previous setKeyEquivalentModifierMask: [o_vlcmain VLCModifiersToCocoa:o_key]];
147     FREENULL( key );
148
149     key = config_GetPsz( p_intf, "key-next" );
150     o_key = [NSString stringWithFormat:@"%s", key];
151     [o_mi_next setKeyEquivalent: [o_vlcmain VLCKeyToString: o_key]];
152     [o_mi_next setKeyEquivalentModifierMask: [o_vlcmain VLCModifiersToCocoa:o_key]];
153     FREENULL( key );
154
155     key = config_GetPsz( p_intf, "key-jump+short" );
156     o_key = [NSString stringWithFormat:@"%s", key];
157     [o_mi_fwd setKeyEquivalent: [o_vlcmain VLCKeyToString: o_key]];
158     [o_mi_fwd setKeyEquivalentModifierMask: [o_vlcmain VLCModifiersToCocoa:o_key]];
159     FREENULL( key );
160
161     key = config_GetPsz( p_intf, "key-jump-short" );
162     o_key = [NSString stringWithFormat:@"%s", key];
163     [o_mi_bwd setKeyEquivalent: [o_vlcmain VLCKeyToString: o_key]];
164     [o_mi_bwd setKeyEquivalentModifierMask: [o_vlcmain VLCModifiersToCocoa:o_key]];
165     FREENULL( key );
166
167     key = config_GetPsz( p_intf, "key-vol-up" );
168     o_key = [NSString stringWithFormat:@"%s", key];
169     [o_mi_vol_up setKeyEquivalent: [o_vlcmain VLCKeyToString: o_key]];
170     [o_mi_vol_up setKeyEquivalentModifierMask: [o_vlcmain VLCModifiersToCocoa:o_key]];
171     FREENULL( key );
172
173     key = config_GetPsz( p_intf, "key-vol-down" );
174     o_key = [NSString stringWithFormat:@"%s", key];
175     [o_mi_vol_down setKeyEquivalent: [o_vlcmain VLCKeyToString: o_key]];
176     [o_mi_vol_down setKeyEquivalentModifierMask: [o_vlcmain VLCModifiersToCocoa:o_key]];
177     FREENULL( key );
178
179     key = config_GetPsz( p_intf, "key-vol-mute" );
180     o_key = [NSString stringWithFormat:@"%s", key];
181     [o_mi_mute setKeyEquivalent: [o_vlcmain VLCKeyToString: o_key]];
182     [o_mi_mute setKeyEquivalentModifierMask: [o_vlcmain VLCModifiersToCocoa:o_key]];
183     FREENULL( key );
184
185     key = config_GetPsz( p_intf, "key-toggle-fullscreen" );
186     o_key = [NSString stringWithFormat:@"%s", key];
187     [o_mi_fullscreen setKeyEquivalent: [o_vlcmain VLCKeyToString: o_key]];
188     [o_mi_fullscreen setKeyEquivalentModifierMask: [o_vlcmain VLCModifiersToCocoa:o_key]];
189     FREENULL( key );
190
191     key = config_GetPsz( p_intf, "key-snapshot" );
192     o_key = [NSString stringWithFormat:@"%s", key];
193     [o_mi_snapshot setKeyEquivalent: [o_vlcmain VLCKeyToString: o_key]];
194     [o_mi_snapshot setKeyEquivalentModifierMask: [o_vlcmain VLCModifiersToCocoa:o_key]];
195     FREENULL( key );
196
197     key = config_GetPsz( p_intf, "key-random" );
198     o_key = [NSString stringWithFormat:@"%s", key];
199     [o_mi_random setKeyEquivalent: [o_vlcmain VLCKeyToString: o_key]];
200     [o_mi_random setKeyEquivalentModifierMask: [o_vlcmain VLCModifiersToCocoa:o_key]];
201     FREENULL( key );
202
203     key = config_GetPsz( p_intf, "key-zoom-half" );
204     o_key = [NSString stringWithFormat:@"%s", key];
205     [o_mi_half_window setKeyEquivalent: [o_vlcmain VLCKeyToString: o_key]];
206     [o_mi_half_window setKeyEquivalentModifierMask: [o_vlcmain VLCModifiersToCocoa:o_key]];
207     FREENULL( key );
208
209     key = config_GetPsz( p_intf, "key-zoom-original" );
210     o_key = [NSString stringWithFormat:@"%s", key];
211     [o_mi_normal_window setKeyEquivalent: [o_vlcmain VLCKeyToString: o_key]];
212     [o_mi_normal_window setKeyEquivalentModifierMask: [o_vlcmain VLCModifiersToCocoa:o_key]];
213     FREENULL( key );
214
215     key = config_GetPsz( p_intf, "key-zoom-double" );
216     o_key = [NSString stringWithFormat:@"%s", key];
217     [o_mi_double_window setKeyEquivalent: [o_vlcmain VLCKeyToString: o_key]];
218     [o_mi_double_window setKeyEquivalentModifierMask: [o_vlcmain VLCModifiersToCocoa:o_key]];
219     FREENULL( key );
220
221     [self setSubmenusEnabled: FALSE];
222
223     [[NSNotificationCenter defaultCenter] addObserver: self
224                                              selector: @selector(refreshVoutDeviceMenu:)
225                                                  name: NSApplicationDidChangeScreenParametersNotification
226                                                object: nil];
227
228     /* we're done */
229     b_mainMenu_setup = YES;
230
231     [self setupVarMenuItem: o_mi_add_intf target: (vlc_object_t *)p_intf
232                              var: "intf-add" selector: @selector(toggleVar:)];
233
234     [self setupExtensionsMenu];
235 }
236
237 - (void)initStrings
238 {
239     /* main menu */
240     [o_mi_about setTitle: [_NS("About VLC media player") \
241                            stringByAppendingString: @"..."]];
242     [o_mi_checkForUpdate setTitle: _NS("Check for Update...")];
243     [o_mi_prefs setTitle: _NS("Preferences...")];
244     [o_mi_extensions setTitle: _NS("Extensions")];
245     [o_mu_extensions setTitle: _NS("Extensions")];
246     [o_mi_add_intf setTitle: _NS("Add Interface")];
247     [o_mu_add_intf setTitle: _NS("Add Interface")];
248     [o_mi_services setTitle: _NS("Services")];
249     [o_mi_hide setTitle: _NS("Hide VLC")];
250     [o_mi_hide_others setTitle: _NS("Hide Others")];
251     [o_mi_show_all setTitle: _NS("Show All")];
252     [o_mi_quit setTitle: _NS("Quit VLC")];
253
254     [o_mu_file setTitle: _ANS("1:File")];
255     [o_mi_open_generic setTitle: _NS("Advanced Open File...")];
256     [o_mi_open_file setTitle: _NS("Open File...")];
257     [o_mi_open_disc setTitle: _NS("Open Disc...")];
258     [o_mi_open_net setTitle: _NS("Open Network...")];
259     [o_mi_open_capture setTitle: _NS("Open Capture Device...")];
260     [o_mi_open_recent setTitle: _NS("Open Recent")];
261     [o_mi_open_wizard setTitle: _NS("Streaming/Exporting Wizard...")];
262     [o_mi_convertandsave setTitle: _NS("Convert / Save...")];
263
264     [o_mu_edit setTitle: _NS("Edit")];
265     [o_mi_cut setTitle: _NS("Cut")];
266     [o_mi_copy setTitle: _NS("Copy")];
267     [o_mi_paste setTitle: _NS("Paste")];
268     [o_mi_clear setTitle: _NS("Clear")];
269     [o_mi_select_all setTitle: _NS("Select All")];
270
271     [o_mu_controls setTitle: _NS("Playback")];
272     [o_mi_play setTitle: _NS("Play")];
273     [o_mi_stop setTitle: _NS("Stop")];
274     [o_mi_record setTitle: _NS("Record")];
275     [o_mi_rate setView: o_mi_rate_view];
276     [o_mi_rate_lbl setStringValue: _NS("Playback Speed")];
277     [o_mi_rate_lbl_gray setStringValue: _NS("Playback Speed")];
278     [o_mi_rate_slower_lbl setStringValue: _NS("Slower")];
279     [o_mi_rate_normal_lbl setStringValue: _NS("Normal")];
280     [o_mi_rate_faster_lbl setStringValue: _NS("Faster")];
281     [o_mi_trackSynchronization setTitle: _NS("Track Synchronization")];
282     [o_mi_previous setTitle: _NS("Previous")];
283     [o_mi_next setTitle: _NS("Next")];
284     [o_mi_random setTitle: _NS("Random")];
285     [o_mi_repeat setTitle: _NS("Repeat One")];
286     [o_mi_loop setTitle: _NS("Repeat All")];
287     [o_mi_quitAfterPB setTitle: _NS("Quit after Playback")];
288     [o_mi_fwd setTitle: _NS("Step Forward")];
289     [o_mi_bwd setTitle: _NS("Step Backward")];
290     [o_mi_toggleJumpButtons setTitle: _NS("Show Previous & Next Buttons")];
291     [o_mi_toggleJumpButtons setState: config_GetInt( VLCIntf, "macosx-show-playback-buttons")];
292     [o_mi_togglePlaymodeButtons setTitle: _NS("Show Shuffle & Repeat Buttons")];
293     [o_mi_togglePlaymodeButtons setState: config_GetInt( VLCIntf, "macosx-show-playmode-buttons")];
294     [o_mu_playlistTableColumns setTitle: _NS("Playlist Table Columns")];
295     [o_mi_ptc_tracknum setTitle: _NS("Track Number")];
296     [o_mi_ptc_title setTitle: _NS("Title")];
297     [o_mi_ptc_artist setTitle: _NS("Artist")];
298     [o_mi_ptc_duration setTitle: _NS("Duration")];
299     [o_mi_ptc_genre setTitle: _NS("Genre")];
300     [o_mi_ptc_album setTitle: _NS("Album")];
301     [o_mi_ptc_description setTitle: _NS("Description")];
302     [o_mi_ptc_date setTitle: _NS("Date")];
303     [o_mi_ptc_language setTitle: _NS("Language")];
304     [o_mi_ptc_uri setTitle: _NS("URI")];
305
306     [o_mi_program setTitle: _NS("Program")];
307     [o_mu_program setTitle: _NS("Program")];
308     [o_mi_title setTitle: _NS("Title")];
309     [o_mu_title setTitle: _NS("Title")];
310     [o_mi_chapter setTitle: _NS("Chapter")];
311     [o_mu_chapter setTitle: _NS("Chapter")];
312
313     [o_mu_audio setTitle: _NS("Audio")];
314     [o_mi_vol_up setTitle: _NS("Increase Volume")];
315     [o_mi_vol_down setTitle: _NS("Decrease Volume")];
316     [o_mi_mute setTitle: _NS("Mute")];
317     [o_mi_audiotrack setTitle: _NS("Audio Track")];
318     [o_mu_audiotrack setTitle: _NS("Audio Track")];
319     [o_mi_channels setTitle: _NS("Audio Channels")];
320     [o_mu_channels setTitle: _NS("Audio Channels")];
321     [o_mi_device setTitle: _NS("Audio Device")];
322     [o_mu_device setTitle: _NS("Audio Device")];
323     [o_mi_visual setTitle: _NS("Visualizations")];
324     [o_mu_visual setTitle: _NS("Visualizations")];
325
326     [o_mu_video setTitle: _NS("Video")];
327     [o_mi_half_window setTitle: _NS("Half Size")];
328     [o_mi_normal_window setTitle: _NS("Normal Size")];
329     [o_mi_double_window setTitle: _NS("Double Size")];
330     [o_mi_fittoscreen setTitle: _NS("Fit to Screen")];
331     [o_mi_fullscreen setTitle: _NS("Fullscreen")];
332     [o_mi_floatontop setTitle: _NS("Float on Top")];
333     [o_mi_snapshot setTitle: _NS("Snapshot")];
334     [o_mi_videotrack setTitle: _NS("Video Track")];
335     [o_mu_videotrack setTitle: _NS("Video Track")];
336     [o_mi_aspect_ratio setTitle: _NS("Aspect-ratio")];
337     [o_mu_aspect_ratio setTitle: _NS("Aspect-ratio")];
338     [o_mi_crop setTitle: _NS("Crop")];
339     [o_mu_crop setTitle: _NS("Crop")];
340     [o_mi_screen setTitle: _NS("Fullscreen Video Device")];
341     [o_mu_screen setTitle: _NS("Fullscreen Video Device")];
342     [o_mi_subtitle setTitle: _NS("Subtitles Track")];
343     [o_mu_subtitle setTitle: _NS("Subtitles Track")];
344     [o_mi_addSub setTitle: _NS("Open File...")];
345     [o_mi_deinterlace setTitle: _NS("Deinterlace")];
346     [o_mu_deinterlace setTitle: _NS("Deinterlace")];
347     [o_mi_deinterlace_mode setTitle: _NS("Deinterlace mode")];
348     [o_mu_deinterlace_mode setTitle: _NS("Deinterlace mode")];
349     [o_mi_ffmpeg_pp setTitle: _NS("Post processing")];
350     [o_mu_ffmpeg_pp setTitle: _NS("Post processing")];
351     [o_mi_teletext setTitle: _NS("Teletext")];
352     [o_mi_teletext_transparent setTitle: _NS("Transparent")];
353     [o_mi_teletext_index setTitle: _NS("Index")];
354     [o_mi_teletext_red setTitle: _NS("Red")];
355     [o_mi_teletext_green setTitle: _NS("Green")];
356     [o_mi_teletext_yellow setTitle: _NS("Yellow")];
357     [o_mi_teletext_blue setTitle: _NS("Blue")];
358
359     [o_mu_window setTitle: _NS("Window")];
360     [o_mi_minimize setTitle: _NS("Minimize Window")];
361     [o_mi_close_window setTitle: _NS("Close Window")];
362     [o_mi_player setTitle: _NS("Player...")];
363     [o_mi_controller setTitle: _NS("Main Window...")];
364     [o_mi_audioeffects setTitle: _NS("Audio Effects...")];
365     [o_mi_videoeffects setTitle: _NS("Video Filters...")];
366     [o_mi_bookmarks setTitle: _NS("Bookmarks...")];
367     [o_mi_playlist setTitle: _NS("Playlist...")];
368     [o_mi_info setTitle: _NS("Media Information...")];
369     [o_mi_messages setTitle: _NS("Messages...")];
370     [o_mi_errorsAndWarnings setTitle: _NS("Errors and Warnings...")];
371
372     [o_mi_bring_atf setTitle: _NS("Bring All to Front")];
373
374     [o_mu_help setTitle: _NS("Help")];
375     [o_mi_help setTitle: _NS("VLC media player Help...")];
376     [o_mi_readme setTitle: _NS("ReadMe / FAQ...")];
377     [o_mi_license setTitle: _NS("License")];
378     [o_mi_documentation setTitle: _NS("Online Documentation...")];
379     [o_mi_website setTitle: _NS("VideoLAN Website...")];
380     [o_mi_donation setTitle: _NS("Make a donation...")];
381     [o_mi_forum setTitle: _NS("Online Forum...")];
382
383     /* dock menu */
384     [o_dmi_play setTitle: _NS("Play")];
385     [o_dmi_stop setTitle: _NS("Stop")];
386     [o_dmi_next setTitle: _NS("Next")];
387     [o_dmi_previous setTitle: _NS("Previous")];
388     [o_dmi_mute setTitle: _NS("Mute")];
389
390     /* vout menu */
391     [o_vmi_play setTitle: _NS("Play")];
392     [o_vmi_stop setTitle: _NS("Stop")];
393     [o_vmi_prev setTitle: _NS("Previous")];
394     [o_vmi_next setTitle: _NS("Next")];
395     [o_vmi_volup setTitle: _NS("Volume Up")];
396     [o_vmi_voldown setTitle: _NS("Volume Down")];
397     [o_vmi_mute setTitle: _NS("Mute")];
398     [o_vmi_fullscreen setTitle: _NS("Fullscreen")];
399     [o_vmi_snapshot setTitle: _NS("Snapshot")];
400 }
401
402 #pragma mark -
403 #pragma mark Termination
404
405 - (void)releaseRepresentedObjects:(NSMenu *)the_menu
406 {
407     if( !p_intf ) return;
408
409     NSArray *menuitems_array = [the_menu itemArray];
410     NSUInteger menuItemCount = [menuitems_array count];
411     for( NSUInteger i=0; i < menuItemCount; i++ )
412     {
413         NSMenuItem *one_item = [menuitems_array objectAtIndex: i];
414         if( [one_item hasSubmenu] )
415             [self releaseRepresentedObjects: [one_item submenu]];
416
417         [one_item setRepresentedObject:NULL];
418     }
419 }
420
421 #pragma mark -
422 #pragma mark Interface update
423
424 - (void)setupMenus
425 {
426     playlist_t * p_playlist = pl_Get( p_intf );
427     input_thread_t * p_input = playlist_CurrentInput( p_playlist );
428     if( p_input != NULL )
429     {
430         [o_mi_record setEnabled: var_GetBool( p_input, "can-record" )];
431
432         [self setupVarMenuItem: o_mi_program target: (vlc_object_t *)p_input
433                                  var: "program" selector: @selector(toggleVar:)];
434
435         [self setupVarMenuItem: o_mi_title target: (vlc_object_t *)p_input
436                                  var: "title" selector: @selector(toggleVar:)];
437
438         [self setupVarMenuItem: o_mi_chapter target: (vlc_object_t *)p_input
439                                  var: "chapter" selector: @selector(toggleVar:)];
440
441         [self setupVarMenuItem: o_mi_audiotrack target: (vlc_object_t *)p_input
442                                  var: "audio-es" selector: @selector(toggleVar:)];
443
444         [self setupVarMenuItem: o_mi_videotrack target: (vlc_object_t *)p_input
445                                  var: "video-es" selector: @selector(toggleVar:)];
446
447         [self setupVarMenuItem: o_mi_subtitle target: (vlc_object_t *)p_input
448                                  var: "spu-es" selector: @selector(toggleVar:)];
449
450         /* special case for "Open File" inside the subtitles menu item */
451         if( [o_mi_videotrack isEnabled] == YES )
452             [o_mi_subtitle setEnabled: YES];
453
454         audio_output_t * p_aout = input_GetAout( p_input );
455         if( p_aout != NULL )
456         {
457             [self setupVarMenuItem: o_mi_channels target: (vlc_object_t *)p_aout
458                                      var: "audio-channels" selector: @selector(toggleVar:)];
459
460             [self setupVarMenuItem: o_mi_device target: (vlc_object_t *)p_aout
461                                      var: "audio-device" selector: @selector(toggleVar:)];
462
463             [self setupVarMenuItem: o_mi_visual target: (vlc_object_t *)p_aout
464                                      var: "visual" selector: @selector(toggleVar:)];
465             vlc_object_release( p_aout );
466         }
467
468         vout_thread_t * p_vout = input_GetVout( p_input );
469
470         if( p_vout != NULL )
471         {
472             [self setupVarMenuItem: o_mi_aspect_ratio target: (vlc_object_t *)p_vout
473                                      var: "aspect-ratio" selector: @selector(toggleVar:)];
474
475             [self setupVarMenuItem: o_mi_crop target: (vlc_object_t *) p_vout
476                                      var: "crop" selector: @selector(toggleVar:)];
477
478             [self setupVarMenuItem: o_mi_deinterlace target: (vlc_object_t *)p_vout
479                                      var: "deinterlace" selector: @selector(toggleVar:)];
480
481             [self setupVarMenuItem: o_mi_deinterlace_mode target: (vlc_object_t *)p_vout
482                                      var: "deinterlace-mode" selector: @selector(toggleVar:)];
483
484 #if 1
485             [self setupVarMenuItem: o_mi_ffmpeg_pp target:
486              (vlc_object_t *)p_vout var:"postprocess" selector:
487              @selector(toggleVar:)];
488 #endif
489             vlc_object_release( p_vout );
490
491             [self refreshVoutDeviceMenu:nil];
492             [self setSubmenusEnabled: YES];
493         }
494         vlc_object_release( p_input );
495     }
496     else
497     {
498         [o_mi_record setEnabled: NO];
499     }
500 }
501
502 - (void)refreshVoutDeviceMenu:(NSNotification *)o_notification
503 {
504     NSUInteger count = [o_mu_screen numberOfItems];
505     NSMenu * o_submenu = o_mu_screen;
506     if (count > 0)
507     {
508         if (OSX_LEOPARD)
509         {
510             NSUInteger count = [o_submenu numberOfItems];
511             for( NSUInteger i = 0; i < count; i++ )
512                 [o_submenu removeItemAtIndex: 0];
513         }
514         else
515             [o_submenu removeAllItems];
516     }
517
518     NSArray * o_screens = [NSScreen screens];
519     NSMenuItem * o_mitem;
520     count = [o_screens count];
521     [o_submenu addItemWithTitle: _NS("Default") action:@selector(toggleFullscreenDevice:) keyEquivalent:@""];
522     o_mitem = [o_submenu itemAtIndex: 0];
523     [o_mitem setTag: 0];
524     [o_mitem setEnabled: YES];
525     [o_mitem setTarget: self];
526     NSRect s_rect;
527     for (NSUInteger i = 0; i < count; i++)
528     {
529         s_rect = [[o_screens objectAtIndex: i] frame];
530         [o_submenu addItemWithTitle: [NSString stringWithFormat: @"%@ %li (%ix%i)", _NS("Screen"), i+1,
531                                       (int)s_rect.size.width, (int)s_rect.size.height] action:@selector(toggleFullscreenDevice:) keyEquivalent:@""];
532         o_mitem = [o_submenu itemAtIndex:i+1];
533         [o_mitem setTag: (int)[[o_screens objectAtIndex: i] displayID]];
534         [o_mitem setEnabled: YES];
535         [o_mitem setTarget: self];
536     }
537     [[o_submenu itemWithTag: config_GetInt( VLCIntf, "macosx-vdev" )] setState: NSOnState];
538 }
539
540 - (void)setSubmenusEnabled:(BOOL)b_enabled
541 {
542     [o_mi_program setEnabled: b_enabled];
543     [o_mi_title setEnabled: b_enabled];
544     [o_mi_chapter setEnabled: b_enabled];
545     [o_mi_audiotrack setEnabled: b_enabled];
546     [o_mi_visual setEnabled: b_enabled];
547     [o_mi_videotrack setEnabled: b_enabled];
548     [o_mi_subtitle setEnabled: b_enabled];
549     [o_mi_channels setEnabled: b_enabled];
550     [o_mi_deinterlace setEnabled: b_enabled];
551     [o_mi_deinterlace_mode setEnabled: b_enabled];
552     [o_mi_ffmpeg_pp setEnabled: b_enabled];
553     [o_mi_device setEnabled: b_enabled];
554     [o_mi_screen setEnabled: b_enabled];
555     [o_mi_aspect_ratio setEnabled: b_enabled];
556     [o_mi_crop setEnabled: b_enabled];
557     [o_mi_teletext setEnabled: b_enabled];
558 }
559
560 - (void)setRateControlsEnabled:(BOOL)b_enabled
561 {
562     NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
563     [o_mi_rate_sld setEnabled: b_enabled];
564     [o_mi_rate_sld setIntValue: [[VLCCoreInteraction sharedInstance] playbackRate]];
565     int i = [[VLCCoreInteraction sharedInstance] playbackRate];
566     double speed =  pow( 2, (double)i / 17 );
567     [o_mi_rate_fld setStringValue: [NSString stringWithFormat:@"%.2fx", speed]];
568     if (b_enabled) {
569         [o_mi_rate_lbl setHidden: NO];
570         [o_mi_rate_lbl_gray setHidden: YES];
571     }
572     else
573     {
574         [o_mi_rate_lbl setHidden: YES];
575         [o_mi_rate_lbl_gray setHidden: NO];
576     }
577     [o_pool release];
578 }
579
580 #pragma mark -
581 #pragma mark Extensions
582
583 - (void)setupExtensionsMenu
584 {
585     /* Load extensions if needed */
586     // TODO: Implement preference for autoloading extensions on mac
587
588     // if( !var_InheritBool( p_intf, "qt-autoload-extensions")
589     //     && ![o_extMgr isLoaded] )
590     // {
591     //     return;
592     // }
593
594     if( ![o_extMgr isLoaded] && ![o_extMgr cannotLoad] )
595     {
596         [o_extMgr loadExtensions];
597     }
598
599     /* Let the ExtensionsManager itself build the menu */
600     [o_extMgr buildMenu:o_mu_extensions];
601 }
602
603 #pragma mark -
604 #pragma mark Playback
605 - (IBAction)toggleRecord:(id)sender
606 {
607     [[VLCCoreInteraction sharedInstance] toggleRecord];
608 }
609
610 - (void)updateRecordState:(BOOL)b_value
611 {
612     [o_mi_record setState:b_value];
613 }
614
615 - (IBAction)setPlaybackRate:(id)sender
616 {
617     [[VLCCoreInteraction sharedInstance] setPlaybackRate: [o_mi_rate_sld intValue]];
618     int i = [[VLCCoreInteraction sharedInstance] playbackRate];
619     double speed =  pow( 2, (double)i / 17 );
620     [o_mi_rate_fld setStringValue: [NSString stringWithFormat:@"%.2fx", speed]];
621 }
622
623 - (void)updatePlaybackRate
624 {
625     int i = [[VLCCoreInteraction sharedInstance] playbackRate];
626     double speed =  pow( 2, (double)i / 17 );
627     [o_mi_rate_fld setStringValue: [NSString stringWithFormat:@"%.2fx", speed]];
628     [o_mi_rate_sld setIntValue: i];
629 }
630
631 - (IBAction)toggleJumpButtons:(id)sender
632 {
633     BOOL b_value = !config_GetInt( VLCIntf, "macosx-show-playback-buttons" );
634     config_PutInt( VLCIntf, "macosx-show-playback-buttons", b_value );
635     [[[VLCMain sharedInstance] mainWindow] toggleJumpButtons];
636     [o_mi_toggleJumpButtons setState: b_value];
637 }
638
639 - (IBAction)togglePlaymodeButtons:(id)sender
640 {
641     BOOL b_value = !config_GetInt( VLCIntf, "macosx-show-playmode-buttons" );
642     config_PutInt( VLCIntf, "macosx-show-playmode-buttons", b_value );
643     [[[VLCMain sharedInstance] mainWindow] togglePlaymodeButtons];
644     [o_mi_togglePlaymodeButtons setState: b_value];
645 }
646
647 - (IBAction)togglePlaylistColumnTable:(id)sender
648 {
649     [sender setState: ![sender state]];
650
651     NSString * o_column;
652
653     if (sender == o_mi_ptc_tracknum)
654         o_column = TRACKNUM_COLUMN;
655     else if (sender == o_mi_ptc_title)
656         o_column = TITLE_COLUMN;
657     else if (sender == o_mi_ptc_artist)
658         o_column = ARTIST_COLUMN;
659     else if (sender == o_mi_ptc_duration)
660         o_column = DURATION_COLUMN;
661     else if (sender == o_mi_ptc_genre)
662         o_column = GENRE_COLUMN;
663     else if (sender == o_mi_ptc_album)
664         o_column = ALBUM_COLUMN;
665     else if (sender == o_mi_ptc_description)
666         o_column = DESCRIPTION_COLUMN;
667     else if (sender == o_mi_ptc_date)
668         o_column = DATE_COLUMN;
669     else if (sender == o_mi_ptc_language)
670         o_column = LANGUAGE_COLUMN;
671     else if (sender == o_mi_ptc_uri)
672         o_column = URI_COLUMN;
673
674     [[[VLCMain sharedInstance] playlist] setColumn: o_column state: [sender state]];
675 }
676
677 - (void)setPlaylistColumnTableState:(NSInteger)i_state forColumn:(NSString *)o_column
678 {
679     if ([o_column isEqualToString: TRACKNUM_COLUMN])
680         [o_mi_ptc_tracknum setState: i_state];
681     else if ([o_column isEqualToString: TITLE_COLUMN])
682         [o_mi_ptc_title setState: i_state];
683     else if ([o_column isEqualToString: ARTIST_COLUMN])
684         [o_mi_ptc_artist setState: i_state];
685     else if ([o_column isEqualToString: DURATION_COLUMN])
686         [o_mi_ptc_duration setState: i_state];
687     else if ([o_column isEqualToString: GENRE_COLUMN])
688         [o_mi_ptc_genre setState: i_state];
689     else if ([o_column isEqualToString: ALBUM_COLUMN])
690         [o_mi_ptc_album setState: i_state];
691     else if ([o_column isEqualToString: DESCRIPTION_COLUMN])
692         [o_mi_ptc_description setState: i_state];
693     else if ([o_column isEqualToString: DATE_COLUMN])
694         [o_mi_ptc_date setState: i_state];
695     else if ([o_column isEqualToString: LANGUAGE_COLUMN])
696         [o_mi_ptc_language setState: i_state];
697     else if ([o_column isEqualToString: URI_COLUMN])
698         [o_mi_ptc_uri setState: i_state];
699 }
700
701 #pragma mark -
702 #pragma mark video menu
703 - (IBAction)toggleFullscreen:(id)sender
704 {
705     [[VLCCoreInteraction sharedInstance] toggleFullscreen];
706 }
707
708 - (IBAction)resizeVideoWindow:(id)sender
709 {
710     input_thread_t *p_input = pl_CurrentInput( VLCIntf );
711     if (p_input)
712     {
713         vout_thread_t *p_vout = getVout();
714         if (p_vout)
715         {
716             if (sender == o_mi_half_window)
717                 var_SetFloat( p_vout, "zoom", 0.5 );
718             else if (sender == o_mi_normal_window)
719                 var_SetFloat( p_vout, "zoom", 1.0 );
720             else if (sender == o_mi_double_window)
721                 var_SetFloat( p_vout, "zoom", 2.0 );
722             else
723             {
724                 [[[[[VLCMain sharedInstance] mainWindow] videoView] window] performZoom:sender];
725             }
726             vlc_object_release( p_vout );
727         }
728         vlc_object_release( p_input );
729     }
730 }
731
732 - (IBAction)floatOnTop:(id)sender
733 {
734     input_thread_t *p_input = pl_CurrentInput( VLCIntf );
735     if (p_input)
736     {
737         vout_thread_t *p_vout = getVout();
738         if (p_vout)
739         {
740             var_ToggleBool( p_vout, "video-on-top" );
741             vlc_object_release( p_vout );
742         }
743         vlc_object_release( p_input );
744     }
745 }
746
747 - (IBAction)createVideoSnapshot:(id)sender
748 {
749     input_thread_t *p_input = pl_CurrentInput( VLCIntf );
750     if (p_input)
751     {
752         vout_thread_t *p_vout = getVout();
753         if (p_vout)
754         {
755             var_TriggerCallback( p_vout, "video-snapshot" );
756             vlc_object_release( p_vout );
757         }
758         vlc_object_release( p_input );
759     }
760 }
761
762 - (IBAction)toggleFullscreenDevice:(id)sender
763 {
764     config_PutInt( VLCIntf, "macosx-vdev", [sender tag] );
765     [self refreshVoutDeviceMenu: nil];
766 }
767
768 - (id)voutMenu
769 {
770     return o_vout_menu;
771 }
772
773 #pragma mark -
774 #pragma mark Panels
775
776 - (IBAction)intfOpenFile:(id)sender
777 {
778     [[[VLCMain sharedInstance] open] openFile];
779 }
780
781 - (IBAction)intfOpenFileGeneric:(id)sender
782 {
783     [[[VLCMain sharedInstance] open] openFileGeneric];
784 }
785
786 - (IBAction)intfOpenDisc:(id)sender
787 {
788     [[[VLCMain sharedInstance] open] openDisc];
789 }
790
791 - (IBAction)intfOpenNet:(id)sender
792 {
793     [[[VLCMain sharedInstance] open] openNet];
794 }
795
796 - (IBAction)intfOpenCapture:(id)sender
797 {
798     [[[VLCMain sharedInstance] open] openCapture];
799 }
800
801 - (IBAction)showWizard:(id)sender
802 {
803     [[[VLCMain sharedInstance] wizard] resetWizard];
804     [[[VLCMain sharedInstance] wizard] showWizard];
805 }
806
807 - (IBAction)showConvertAndSave:(id)sender
808 {
809     if( o_convertandsave == nil )
810         o_convertandsave = [[VLCConvertAndSave alloc] init];
811
812     if( !b_nib_convertandsave_loaded )
813         b_nib_convertandsave_loaded = [NSBundle loadNibNamed:@"ConvertAndSave" owner: NSApp];
814
815     [o_convertandsave toggleWindow];
816 }
817
818 - (IBAction)showVideoEffects:(id)sender
819 {
820     if( o_videoeffects == nil )
821         o_videoeffects = [[VLCVideoEffects alloc] init];
822
823     if( !b_nib_videoeffects_loaded )
824         b_nib_videoeffects_loaded = [NSBundle loadNibNamed:@"VideoEffects" owner: NSApp];
825
826     [o_videoeffects toggleWindow:sender];
827 }
828
829 - (IBAction)showTrackSynchronization:(id)sender
830 {
831     if (!o_trackSynchronization)
832         o_trackSynchronization = [[VLCTrackSynchronization alloc] init];
833
834     if (!b_nib_tracksynchro_loaded)
835         b_nib_tracksynchro_loaded = [NSBundle loadNibNamed:@"SyncTracks" owner:NSApp];
836
837     [o_trackSynchronization toggleWindow:sender];
838 }
839
840 - (IBAction)showAudioEffects:(id)sender
841 {
842     if (!o_audioeffects)
843         o_audioeffects = [[VLCAudioEffects alloc] init];
844
845     if (!b_nib_audioeffects_loaded)
846         b_nib_audioeffects_loaded = [NSBundle loadNibNamed:@"AudioEffects" owner:NSApp];
847
848     [o_audioeffects toggleWindow:sender];
849 }
850
851 - (IBAction)showBookmarks:(id)sender
852 {
853     [[[VLCMain sharedInstance] bookmarks] showBookmarks];
854 }
855
856 - (IBAction)viewPreferences:(id)sender
857 {
858     [[[VLCMain sharedInstance] simplePreferences] showSimplePrefs];
859 }
860
861 #pragma mark -
862 #pragma mark Help and Docs
863
864 - (void)initAbout
865 {
866     if(! o_about )
867         o_about = [[VLAboutBox alloc] init];
868
869     if( !b_nib_about_loaded )
870         b_nib_about_loaded = [NSBundle loadNibNamed:@"About" owner: NSApp];
871 }
872
873 - (IBAction)viewAbout:(id)sender
874 {
875     [self initAbout];
876     [o_about showAbout];
877 }
878
879 - (IBAction)showLicense:(id)sender
880 {
881     [self initAbout];
882     [o_about showGPL: sender];
883 }
884
885 - (IBAction)viewHelp:(id)sender
886 {
887     [self initAbout];
888     [o_about showHelp];
889 }
890
891 - (IBAction)openReadMe:(id)sender
892 {
893     NSString * o_path = [[NSBundle mainBundle] pathForResource: @"README.MacOSX" ofType: @"rtf"];
894
895     [[NSWorkspace sharedWorkspace] openFile: o_path withApplication: @"TextEdit"];
896 }
897
898 - (IBAction)openDocumentation:(id)sender
899 {
900     NSURL * o_url = [NSURL URLWithString: @"http://www.videolan.org/doc/"];
901
902     [[NSWorkspace sharedWorkspace] openURL: o_url];
903 }
904
905 - (IBAction)openWebsite:(id)sender
906 {
907     NSURL * o_url = [NSURL URLWithString: @"http://www.videolan.org/"];
908
909     [[NSWorkspace sharedWorkspace] openURL: o_url];
910 }
911
912 - (IBAction)openForum:(id)sender
913 {
914     NSURL * o_url = [NSURL URLWithString: @"http://forum.videolan.org/"];
915
916     [[NSWorkspace sharedWorkspace] openURL: o_url];
917 }
918
919 - (IBAction)openDonate:(id)sender
920 {
921     NSURL * o_url = [NSURL URLWithString: @"http://www.videolan.org/contribute.html#paypal"];
922
923     [[NSWorkspace sharedWorkspace] openURL: o_url];
924 }
925
926 #pragma mark -
927 #pragma mark Errors, warnings and messages
928
929 - (IBAction)viewErrorsAndWarnings:(id)sender
930 {
931     [[[[VLCMain sharedInstance] coreDialogProvider] errorPanel] showPanel];
932 }
933
934 - (IBAction)showInformationPanel:(id)sender
935 {
936     [[[VLCMain sharedInstance] info] initPanel];
937 }
938
939 #pragma mark -
940 #pragma mark convinience stuff for other objects
941 - (void)setPlay
942 {
943     [o_mi_play setTitle: _NS("Play")];
944     [o_dmi_play setTitle: _NS("Play")];
945     [o_vmi_play setTitle: _NS("Play")];
946 }
947
948 - (void)setPause
949 {
950     [o_mi_play setTitle: _NS("Pause")];
951     [o_dmi_play setTitle: _NS("Pause")];
952     [o_vmi_play setTitle: _NS("Pause")];
953 }
954
955 - (void)setRepeatOne
956 {
957     [o_mi_repeat setState: NSOnState];
958     [o_mi_loop setState: NSOffState];
959 }
960
961 - (void)setRepeatAll
962 {
963     [o_mi_repeat setState: NSOffState];
964     [o_mi_loop setState: NSOnState];
965 }
966
967 - (void)setRepeatOff
968 {
969     [o_mi_repeat setState: NSOffState];
970     [o_mi_loop setState: NSOffState];
971 }
972
973 - (void)setShuffle
974 {
975     bool b_value;
976     playlist_t *p_playlist = pl_Get( VLCIntf );
977     b_value = var_GetBool( p_playlist, "random" );
978
979     [o_mi_random setState: b_value];
980 }
981
982 #pragma mark -
983 #pragma mark Dynamic menu creation and validation
984
985 - (void)setupVarMenuItem:(NSMenuItem *)o_mi
986                   target:(vlc_object_t *)p_object
987                      var:(const char *)psz_variable
988                 selector:(SEL)pf_callback
989 {
990     vlc_value_t val, text;
991     int i_type = var_Type( p_object, psz_variable );
992
993     switch( i_type & VLC_VAR_TYPE )
994     {
995         case VLC_VAR_VOID:
996         case VLC_VAR_BOOL:
997         case VLC_VAR_VARIABLE:
998         case VLC_VAR_STRING:
999         case VLC_VAR_INTEGER:
1000             break;
1001         default:
1002             /* Variable doesn't exist or isn't handled */
1003             return;
1004     }
1005
1006     /* Make sure we want to display the variable
1007      * special case for spu-es, which we want to display in any case */
1008     if( i_type & VLC_VAR_HASCHOICE && strcmp( psz_variable, "spu-es" ) )
1009     {
1010         var_Change( p_object, psz_variable, VLC_VAR_CHOICESCOUNT, &val, NULL );
1011         if( val.i_int == 0 )
1012             return;
1013         if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE && val.i_int == 1 )
1014             return;
1015     }
1016
1017     /* Get the descriptive name of the variable */
1018     var_Change( p_object, psz_variable, VLC_VAR_GETTEXT, &text, NULL );
1019     [o_mi setTitle: _NS( text.psz_string ? text.psz_string : psz_variable )];
1020
1021     if( i_type & VLC_VAR_HASCHOICE )
1022     {
1023         NSMenu *o_menu = [o_mi submenu];
1024
1025         [self setupVarMenu: o_menu forMenuItem: o_mi target:p_object
1026                        var:psz_variable selector:pf_callback];
1027
1028         free( text.psz_string );
1029         return;
1030     }
1031
1032     if( var_Get( p_object, psz_variable, &val ) < 0 )
1033     {
1034         return;
1035     }
1036
1037     VLCAutoGeneratedMenuContent *o_data;
1038     switch( i_type & VLC_VAR_TYPE )
1039     {
1040         case VLC_VAR_VOID:
1041             o_data = [[VLCAutoGeneratedMenuContent alloc] initWithVariableName: psz_variable ofObject: p_object
1042                                                                       andValue: val ofType: i_type];
1043             [o_mi setRepresentedObject: [o_data autorelease]];
1044             break;
1045
1046         case VLC_VAR_BOOL:
1047             o_data = [[VLCAutoGeneratedMenuContent alloc] initWithVariableName: psz_variable ofObject: p_object
1048                                                                       andValue: val ofType: i_type];
1049             [o_mi setRepresentedObject: [o_data autorelease]];
1050             if( !( i_type & VLC_VAR_ISCOMMAND ) )
1051                 [o_mi setState: val.b_bool ? TRUE : FALSE ];
1052             break;
1053
1054         default:
1055             break;
1056     }
1057
1058     if( ( i_type & VLC_VAR_TYPE ) == VLC_VAR_STRING ) free( val.psz_string );
1059     free( text.psz_string );
1060 }
1061
1062
1063 - (void)setupVarMenu:(NSMenu *)o_menu
1064          forMenuItem: (NSMenuItem *)o_parent
1065               target:(vlc_object_t *)p_object
1066                  var:(const char *)psz_variable
1067             selector:(SEL)pf_callback
1068 {
1069     vlc_value_t val, val_list, text_list;
1070     int i_type, i;
1071
1072     /* remove previous items */
1073     if (OSX_LEOPARD)
1074     {
1075         int i_nb_items;
1076         i_nb_items = [o_menu numberOfItems];
1077         for( i = 0; i < i_nb_items; i++ )
1078             [o_menu removeItemAtIndex: 0];
1079     }
1080     else
1081     {
1082         /* this is more efficient then the legacy code, but 10.6+ only */
1083         [o_menu removeAllItems];
1084     }
1085
1086     /* Aspect Ratio */
1087     if( [[o_parent title] isEqualToString: _NS("Aspect-ratio")] == YES )
1088     {
1089         NSMenuItem *o_lmi_tmp2;
1090         o_lmi_tmp2 = [o_menu addItemWithTitle: _NS("Lock Aspect Ratio") action: @selector(lockVideosAspectRatio:) keyEquivalent: @""];
1091         [o_lmi_tmp2 setTarget: [[VLCMain sharedInstance] controls]];
1092         [o_lmi_tmp2 setEnabled: YES];
1093         [o_lmi_tmp2 setState: [[VLCCoreInteraction sharedInstance] aspectRatioIsLocked]];
1094         [o_parent setEnabled: YES];
1095         [o_menu addItem: [NSMenuItem separatorItem]];
1096     }
1097
1098     /* special case for the subtitles item */
1099     if( [[o_parent title] isEqualToString: _NS("Subtitles Track")] == YES )
1100     {
1101         NSMenuItem * o_lmi_tmp;
1102         o_lmi_tmp = [o_menu addItemWithTitle: _NS("Open File...") action: @selector(addSubtitleFile:) keyEquivalent: @""];
1103         [o_lmi_tmp setTarget: [[VLCMain sharedInstance] controls]];
1104         [o_lmi_tmp setEnabled: YES];
1105         [o_parent setEnabled: YES];
1106     }
1107
1108     /* Check the type of the object variable */
1109     i_type = var_Type( p_object, psz_variable );
1110
1111     /* Make sure we want to display the variable */
1112     if( i_type & VLC_VAR_HASCHOICE )
1113     {
1114         var_Change( p_object, psz_variable, VLC_VAR_CHOICESCOUNT, &val, NULL );
1115         if( val.i_int == 0 ) return;
1116         if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE && val.i_int == 1 )
1117             return;
1118     }
1119     else
1120         return;
1121
1122     switch( i_type & VLC_VAR_TYPE )
1123     {
1124         case VLC_VAR_VOID:
1125         case VLC_VAR_BOOL:
1126         case VLC_VAR_VARIABLE:
1127         case VLC_VAR_STRING:
1128         case VLC_VAR_INTEGER:
1129             break;
1130         default:
1131             /* Variable doesn't exist or isn't handled */
1132             return;
1133     }
1134
1135     if( var_Get( p_object, psz_variable, &val ) < 0 )
1136     {
1137         return;
1138     }
1139
1140     if( var_Change( p_object, psz_variable, VLC_VAR_GETLIST,
1141                    &val_list, &text_list ) < 0 )
1142     {
1143         if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
1144         return;
1145     }
1146
1147     /* make (un)sensitive */
1148     [o_parent setEnabled: ( val_list.p_list->i_count > 1 )];
1149
1150     /* another special case for the subtitles item */
1151     if( [[o_parent title] isEqualToString: _NS("Subtitles Track")] == YES )
1152         [o_menu addItem: [NSMenuItem separatorItem]];
1153
1154     for( i = 0; i < val_list.p_list->i_count; i++ )
1155     {
1156         NSMenuItem * o_lmi;
1157         NSString *o_title = @"";
1158         VLCAutoGeneratedMenuContent *o_data;
1159
1160         switch( i_type & VLC_VAR_TYPE )
1161         {
1162             case VLC_VAR_STRING:
1163
1164                 o_title = _NS( text_list.p_list->p_values[i].psz_string ? text_list.p_list->p_values[i].psz_string : val_list.p_list->p_values[i].psz_string );
1165
1166                 o_lmi = [o_menu addItemWithTitle: o_title action: pf_callback keyEquivalent: @""];
1167                 o_data = [[VLCAutoGeneratedMenuContent alloc] initWithVariableName: psz_variable ofObject: p_object
1168                                                                           andValue: val_list.p_list->p_values[i] ofType: i_type];
1169                 [o_lmi setRepresentedObject: [o_data autorelease]];
1170                 [o_lmi setTarget: self];
1171
1172                 if( !strcmp( val.psz_string, val_list.p_list->p_values[i].psz_string ) && !( i_type & VLC_VAR_ISCOMMAND ) )
1173                     [o_lmi setState: TRUE ];
1174
1175                 break;
1176
1177             case VLC_VAR_INTEGER:
1178
1179                 o_title = text_list.p_list->p_values[i].psz_string ?
1180                 _NS( text_list.p_list->p_values[i].psz_string ) : [NSString stringWithFormat: @"%"PRId64, val_list.p_list->p_values[i].i_int];
1181
1182                 o_lmi = [o_menu addItemWithTitle: o_title action: pf_callback keyEquivalent: @""];
1183                 o_data = [[VLCAutoGeneratedMenuContent alloc] initWithVariableName: psz_variable ofObject: p_object
1184                                                                           andValue: val_list.p_list->p_values[i] ofType: i_type];
1185                 [o_lmi setRepresentedObject: [o_data autorelease]];
1186                 [o_lmi setTarget: self];
1187
1188                 if( val_list.p_list->p_values[i].i_int == val.i_int && !( i_type & VLC_VAR_ISCOMMAND ) )
1189                     [o_lmi setState: TRUE ];
1190                 break;
1191
1192             default:
1193                 break;
1194         }
1195     }
1196
1197     /* special case for the subtitles sub-menu
1198      * In case that we don't have any subs, we don't want a separator item at the end */
1199     if( [[o_parent title] isEqualToString: _NS("Subtitles Track")] == YES )
1200     {
1201         if( [o_menu numberOfItems] == 2 )
1202             [o_menu removeItemAtIndex: 1];
1203     }
1204
1205     /* clean up everything */
1206     if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
1207     var_FreeList( &val_list, &text_list );
1208 }
1209
1210 - (IBAction)toggleVar:(id)sender
1211 {
1212     NSMenuItem *o_mi = (NSMenuItem *)sender;
1213     VLCAutoGeneratedMenuContent *o_data = [o_mi representedObject];
1214     [NSThread detachNewThreadSelector: @selector(toggleVarThread:)
1215                              toTarget: self withObject: o_data];
1216
1217     return;
1218 }
1219
1220 - (int)toggleVarThread: (id)data
1221 {
1222     vlc_object_t *p_object;
1223     NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
1224
1225     assert([data isKindOfClass:[VLCAutoGeneratedMenuContent class]]);
1226     VLCAutoGeneratedMenuContent *menuContent = (VLCAutoGeneratedMenuContent *)data;
1227
1228     /* Preserve settings across vouts via the playlist object: */
1229     if( !strcmp( [menuContent name], "fullscreen" ) || !strcmp( [menuContent name], "video-on-top" ) )
1230         var_Set( pl_Get( VLCIntf ), [menuContent name] , [menuContent value] );
1231
1232     /* save our audio device across multiple sessions */
1233     if( !strcmp( [menuContent name], "audio-device" ) )
1234         config_PutInt( VLCIntf, "macosx-audio-device", [menuContent value].i_int );
1235
1236     p_object = [menuContent vlcObject];
1237
1238     if( p_object != NULL )
1239     {
1240         var_Set( p_object, [menuContent name], [menuContent value] );
1241         vlc_object_release( p_object );
1242         [o_pool release];
1243         return true;
1244     }
1245     [o_pool release];
1246     return VLC_EGENERIC;
1247 }
1248
1249 @end
1250
1251 @implementation VLCMainMenu (NSMenuValidation)
1252
1253 - (BOOL)validateMenuItem:(NSMenuItem *)o_mi
1254 {
1255     NSString *o_title = [o_mi title];
1256     BOOL bEnabled = TRUE;
1257     vlc_value_t val;
1258     playlist_t * p_playlist = pl_Get( p_intf );
1259     input_thread_t * p_input = playlist_CurrentInput( p_playlist );
1260
1261     if( [o_title isEqualToString: _NS("Stop")] )
1262     {
1263         if( p_input == NULL )
1264         {
1265             bEnabled = FALSE;
1266         }
1267         [self setupMenus]; /* Make sure input menu is up to date */
1268     }
1269     else if( [o_title isEqualToString: _NS("Previous")] ||
1270             [o_title isEqualToString: _NS("Next")] )
1271     {
1272         PL_LOCK;
1273         bEnabled = playlist_CurrentSize( p_playlist ) > 1;
1274         PL_UNLOCK;
1275     }
1276     else if( [o_title isEqualToString: _NS("Random")] )
1277     {
1278         int i_state;
1279         var_Get( p_playlist, "random", &val );
1280         i_state = val.b_bool ? NSOnState : NSOffState;
1281         [o_mi setState: i_state];
1282     }
1283     else if( [o_title isEqualToString: _NS("Repeat One")] )
1284     {
1285         int i_state;
1286         var_Get( p_playlist, "repeat", &val );
1287         i_state = val.b_bool ? NSOnState : NSOffState;
1288         [o_mi setState: i_state];
1289     }
1290     else if( [o_title isEqualToString: _NS("Repeat All")] )
1291     {
1292         int i_state;
1293         var_Get( p_playlist, "loop", &val );
1294         i_state = val.b_bool ? NSOnState : NSOffState;
1295         [o_mi setState: i_state];
1296     }
1297     else if( [o_title isEqualToString: _NS("Quit after Playback")] )
1298     {
1299         int i_state;
1300         var_Get( p_playlist, "play-and-exit", &val );
1301         i_state = val.b_bool ? NSOnState : NSOffState;
1302         [o_mi setState: i_state];
1303     }
1304     else if( [o_title isEqualToString: _NS("Step Forward")] ||
1305             [o_title isEqualToString: _NS("Step Backward")] ||
1306             [o_title isEqualToString: _NS("Jump To Time")])
1307     {
1308         if( p_input != NULL )
1309         {
1310             var_Get( p_input, "can-seek", &val);
1311             bEnabled = val.b_bool;
1312         }
1313         else bEnabled = FALSE;
1314     }
1315     else if( [o_title isEqualToString: _NS("Mute")] )
1316     {
1317         [o_mi setState: [[VLCCoreInteraction sharedInstance] isMuted] ? NSOnState : NSOffState];
1318         [self setupMenus]; /* Make sure audio menu is up to date */
1319     }
1320     else if( [o_title isEqualToString: _NS("Half Size")] ||
1321             [o_title isEqualToString: _NS("Normal Size")] ||
1322             [o_title isEqualToString: _NS("Double Size")] ||
1323             [o_title isEqualToString: _NS("Fit to Screen")] ||
1324             [o_title isEqualToString: _NS("Snapshot")] ||
1325             [o_title isEqualToString: _NS("Fullscreen")] ||
1326             [o_title isEqualToString: _NS("Float on Top")] )
1327     {
1328         bEnabled = FALSE;
1329
1330         if( p_input != NULL )
1331         {
1332             vout_thread_t *p_vout = input_GetVout( p_input );
1333             if( p_vout != NULL )
1334             {
1335                 if( [o_title isEqualToString: _NS("Float on Top")] )
1336                     [o_mi setState: var_GetBool( p_vout, "video-on-top" )];
1337
1338                 bEnabled = TRUE;
1339
1340                 vlc_object_release( p_vout );
1341             }
1342         }
1343         if( [o_title isEqualToString: _NS("Fullscreen")] )
1344         {
1345         [o_mi setState: var_GetBool( p_playlist, "fullscreen" )];
1346             bEnabled = TRUE;
1347         }
1348         [self setupMenus]; /* Make sure video menu is up to date */
1349     }
1350
1351     /* Special case for telx menu */
1352     if( [o_title isEqualToString: _NS("Normal Size")] )
1353     {
1354         NSMenuItem *item = [[o_mi menu] itemWithTitle:_NS("Teletext")];
1355
1356
1357 bool b_telx = p_input && var_GetInteger( p_input, "teletext-es" ) >= 0;
1358
1359         [[item submenu] setAutoenablesItems:NO];
1360         for( int k=0; k < [[item submenu] numberOfItems]; k++ )
1361         {
1362             [[[item submenu] itemAtIndex:k] setEnabled: b_telx];
1363         }
1364     }
1365
1366     if( p_input ) vlc_object_release( p_input );
1367
1368     return( bEnabled );
1369 }
1370
1371 @end
1372
1373
1374 /*****************************************************************************
1375  * VLCAutoGeneratedMenuContent implementation
1376  *****************************************************************************
1377  * Object connected to a playlistitem which remembers the data belonging to
1378  * the variable of the autogenerated menu
1379  *****************************************************************************/
1380 @implementation VLCAutoGeneratedMenuContent
1381
1382 -(id) initWithVariableName:(const char *)name ofObject:(vlc_object_t *)object
1383                   andValue:(vlc_value_t)val ofType:(int)type
1384 {
1385     self = [super init];
1386
1387     if( self != nil )
1388     {
1389         _vlc_object = vlc_object_hold( object );
1390         psz_name = strdup( name );
1391         i_type = type;
1392         value = val;
1393         if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING )
1394             value.psz_string = strdup( val.psz_string );
1395     }
1396
1397     return( self );
1398 }
1399
1400 - (void)dealloc
1401 {
1402     if( _vlc_object )
1403         vlc_object_release( _vlc_object );
1404     if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING )
1405         free( value.psz_string );
1406     free( psz_name );
1407     [super dealloc];
1408 }
1409
1410 - (const char *)name
1411 {
1412     return psz_name;
1413 }
1414
1415 - (vlc_value_t)value
1416 {
1417     return value;
1418 }
1419
1420 - (vlc_object_t *)vlcObject
1421 {
1422     return vlc_object_hold( _vlc_object );
1423 }
1424
1425
1426 - (int)type
1427 {
1428     return i_type;
1429 }
1430
1431 @end