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