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