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