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