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