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