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