]> git.sesse.net Git - vlc/blob - modules/gui/qt4/menus.cpp
macosx: reimplemented the right-click Reveal-in-Finder feature on the window title...
[vlc] / modules / gui / qt4 / menus.cpp
1 /*****************************************************************************
2  * menus.cpp : Qt menus
3  *****************************************************************************
4  * Copyright © 2006-2011 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Clément Stenac <zorglub@videolan.org>
8  *          Jean-Baptiste Kempf <jb@videolan.org>
9  *          Jean-Philippe André <jpeg@videolan.org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * ( at your option ) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 /** \todo
27  * - Remove static currentGroup
28  */
29
30 #define __STDC_FORMAT_MACROS 1
31 #define __STDC_CONSTANT_MACROS 1
32
33 #ifdef HAVE_CONFIG_H
34 # include "config.h"
35 #endif
36
37 #include <vlc_common.h>
38 #include <vlc_intf_strings.h>
39 #include <vlc_vout.h>                             /* vout_thread_t */
40 #include <vlc_aout.h>                             /* audio_output_t */
41
42 #include "menus.hpp"
43
44 #include "main_interface.hpp"                     /* View modifications */
45 #include "dialogs_provider.hpp"                   /* Dialogs display */
46 #include "input_manager.hpp"                      /* Input Management */
47 #include "recents.hpp"                            /* Recent Items */
48 #include "actions_manager.hpp"                    /* Actions Management: play+volume */
49 #include "extensions_manager.hpp"                 /* Extensions menu */
50 #include "util/qmenuview.hpp"                     /* Simple Playlist menu */
51 #include "components/playlist/playlist_model.hpp" /* PLModel getter */
52
53 #include <QMenu>
54 #include <QMenuBar>
55 #include <QAction>
56 #include <QActionGroup>
57 #include <QSignalMapper>
58 #include <QStatusBar>
59
60 /*
61   This file defines the main menus and the pop-up menu (right-click menu)
62   and the systray menu (in that order in the file)
63
64   There are 4 menus that have to be rebuilt everytime there are called:
65   Audio, Video, Navigation, view
66   4 functions are building those menus: AudioMenu, VideoMenu, NavigMenu, View
67   and 3 functions associated are collecting the objects :
68   InputAutoMenuBuilder, AudioAutoMenuBuilder, VideoAutoMenuBuilder.
69
70   A QSignalMapper decides when to rebuild those menus cf MenuFunc in the .hpp
71   Just before one of those menus are aboutToShow(), they are rebuild.
72   */
73
74 enum
75 {
76     ITEM_NORMAL, /* not a checkbox, nor a radio */
77     ITEM_CHECK,  /* Checkbox */
78     ITEM_RADIO   /* Radiobox */
79 };
80
81 static QActionGroup *currentGroup;
82
83 QMenu *VLCMenuBar::recentsMenu = NULL;
84
85 /**
86  * @brief Add static entries to DP in menus
87  **/
88 QAction *addDPStaticEntry( QMenu *menu,
89                        const QString& text,
90                        const char *icon,
91                        const char *member,
92                        const char *shortcut = NULL,
93                        QAction::MenuRole role = QAction::NoRole
94                        )
95 {
96     QAction *action = NULL;
97 #ifndef __APPLE__ /* We don't set icons in menus in MacOS X */
98     if( !EMPTY_STR( icon ) )
99     {
100         if( !EMPTY_STR( shortcut ) )
101             action = menu->addAction( QIcon( icon ), text, THEDP,
102                                       member, qtr( shortcut ) );
103         else
104             action = menu->addAction( QIcon( icon ), text, THEDP, member );
105     }
106     else
107 #endif
108     {
109         if( !EMPTY_STR( shortcut ) )
110             action = menu->addAction( text, THEDP, member, qtr( shortcut ) );
111         else
112             action = menu->addAction( text, THEDP, member );
113     }
114 #ifdef __APPLE__
115     action->setMenuRole( role )
116 #else
117     Q_UNUSED( role );
118 #endif
119     action->setData( VLCMenuBar::ACTION_STATIC );
120     return action;
121 }
122
123 /**
124  * @brief Add static entries to MIM in menus
125  **/
126 QAction* addMIMStaticEntry( intf_thread_t *p_intf,
127                             QMenu *menu,
128                             const QString& text,
129                             const char *icon,
130                             const char *member,
131                             bool bStatic = false )
132 {
133     QAction *action;
134 #ifndef __APPLE__ /* We don't set icons in menus in MacOS X */
135     if( !EMPTY_STR( icon ) )
136     {
137         action = menu->addAction( text, THEMIM,  member );
138         action->setIcon( QIcon( icon ) );
139     }
140     else
141 #endif
142     {
143         action = menu->addAction( text, THEMIM, member );
144     }
145     action->setData( VLCMenuBar::ACTION_STATIC |
146                      ( bStatic ) ? VLCMenuBar::ACTION_ALWAYS_ENABLED
147                                  : VLCMenuBar::ACTION_NONE
148                    );
149     return action;
150 }
151
152 /**
153  * @brief Enable all static entries of a menu, disable the others
154  * @param menu the menu in which the entries will be disabled
155  * @param enable if false, disable all entries
156  **/
157 void VLCMenuBar::EnableStaticEntries( QMenu *menu, bool enable = true )
158 {
159     if( !menu ) return;
160
161     QList< QAction* > actions = menu->actions();
162     for( int i = 0; i < actions.count(); ++i )
163     {
164         int actionflags = actions[i]->data().toInt();
165         if ( actionflags & ACTION_MANAGED )
166             actions[i]->setEnabled(
167                 ( actionflags & ACTION_ALWAYS_ENABLED )
168                 ||
169                 enable
170             );
171     }
172 }
173
174 /**
175  * \return Number of static entries
176  **/
177 inline int DeleteNonStaticEntries( QMenu *menu )
178 {
179     if( !menu ) return VLC_EGENERIC;
180
181     int i_ret = 0;
182
183     QList< QAction* > actions = menu->actions();
184     for( int i = 0; i < actions.count(); ++i )
185     {
186         if( actions[i]->data().toInt() & VLCMenuBar::ACTION_NO_CLEANUP )
187             i_ret++;
188         else
189             delete actions[i];
190     }
191     return i_ret;
192 }
193
194 /**
195  * \return QAction associated to psz_var variable
196  **/
197 static QAction * FindActionWithVar( QMenu *menu, const char *psz_var )
198 {
199     QList< QAction* > actions = menu->actions();
200     for( int i = 0; i < actions.count(); ++i )
201     {
202         if( actions[i]->data().toString() == psz_var )
203             return actions[i];
204     }
205     return NULL;
206 }
207
208 /*****************************************************************************
209  * Definitions of variables for the dynamic menus
210  *****************************************************************************/
211 #define PUSH_VAR( var ) varnames.append( var ); \
212     objects.append( VLC_OBJECT(p_object) )
213
214 #define PUSH_INPUTVAR( var ) varnames.append( var ); \
215     objects.append( VLC_OBJECT(p_input) );
216
217 static int InputAutoMenuBuilder( input_thread_t *p_object,
218         QVector<vlc_object_t *> &objects,
219         QVector<const char *> &varnames )
220 {
221     PUSH_VAR( "bookmark" );
222     PUSH_VAR( "title" );
223     PUSH_VAR( "chapter" );
224     PUSH_VAR( "navigation" );
225     PUSH_VAR( "program" );
226     return VLC_SUCCESS;
227 }
228
229 static int VideoAutoMenuBuilder( vout_thread_t *p_object,
230         input_thread_t *p_input,
231         QVector<vlc_object_t *> &objects,
232         QVector<const char *> &varnames )
233 {
234     PUSH_INPUTVAR( "video-es" );
235     PUSH_INPUTVAR( "spu-es" );
236     PUSH_VAR( "fullscreen" );
237     PUSH_VAR( "video-on-top" );
238     PUSH_VAR( "video-wallpaper" );
239     PUSH_VAR( "video-snapshot" );
240     PUSH_VAR( "zoom" );
241     PUSH_VAR( "autoscale" );
242     PUSH_VAR( "aspect-ratio" );
243     PUSH_VAR( "crop" );
244     PUSH_VAR( "deinterlace" );
245     PUSH_VAR( "deinterlace-mode" );
246     PUSH_VAR( "postprocess" );
247
248     return VLC_SUCCESS;
249 }
250
251 static int AudioAutoMenuBuilder( audio_output_t *p_object,
252         input_thread_t *p_input,
253         QVector<vlc_object_t *> &objects,
254         QVector<const char *> &varnames )
255 {
256     PUSH_INPUTVAR( "audio-es" );
257     PUSH_VAR( "audio-channels" );
258     PUSH_VAR( "audio-device" );
259     PUSH_VAR( "visual" );
260     return VLC_SUCCESS;
261 }
262
263 /*****************************************************************************
264  * All normal menus
265  * Simple Code
266  *****************************************************************************/
267
268 // Static menu
269 static inline void addMenuToMainbar( QMenu *func, QString title, QMenuBar *bar ) {
270     func->setTitle( title );
271     bar->addMenu( func);
272 }
273
274 // Dynamic menu
275 #define BAR_DADD( func, title, id ) { \
276     QMenu *_menu = func; _menu->setTitle( title ); bar->addMenu( _menu ); \
277     MenuFunc *f = new MenuFunc( _menu, id ); \
278     CONNECT( _menu, aboutToShow(), THEDP->menusUpdateMapper, map() ); \
279     THEDP->menusUpdateMapper->setMapping( _menu, f ); }
280
281 // Add a simple action
282 static inline void addAction( QMenu *_menu, QVariant val, QString title ) {
283     QAction *_action = new QAction( title, _menu );
284     _action->setData( val );
285     _menu->addAction( _action );
286 }
287
288 // Add an action with a submenu
289 static inline QMenu *addActionWithSubmenu( QMenu *_menu, QVariant val, QString title ) {
290     QAction *_action = new QAction( title, _menu );
291     QMenu *_submenu = new QMenu( _menu );
292     _action->setData( val );
293     _action->setMenu( _submenu );
294     _menu->addAction( _action );
295     return _submenu;
296 }
297
298 // Add an action that is a checkbox
299 static inline void addActionWithCheckbox( QMenu *_menu, QVariant val, QString title ) {
300     QAction *_action = new QAction( title, _menu );
301     _action->setData( val );
302     _action->setCheckable( true );
303     _menu->addAction( _action );
304 }
305
306 /**
307  * Main Menu Bar Creation
308  **/
309 void VLCMenuBar::createMenuBar( MainInterface *mi,
310                               intf_thread_t *p_intf )
311 {
312     /* QMainWindows->menuBar()
313        gives the QProcess::destroyed timeout issue on Cleanlooks style with
314        setDesktopAware set to false */
315     QMenuBar *bar = mi->menuBar();
316
317     addMenuToMainbar( FileMenu( p_intf, bar, mi ), qtr( "&Media" ), bar );
318
319     /* Dynamic menus, rebuilt before being showed */
320     BAR_DADD( NavigMenu( p_intf, bar ), qtr( "P&layback" ), 3 );
321     BAR_DADD( AudioMenu( p_intf, bar ), qtr( "&Audio" ), 1 );
322     BAR_DADD( VideoMenu( p_intf, bar ), qtr( "&Video" ), 2 );
323
324     addMenuToMainbar( ToolsMenu( bar ), qtr( "&Tools" ), bar );
325
326     /* View menu, a bit different */
327     BAR_DADD( ViewMenu( p_intf, NULL, mi ), qtr( "V&iew" ), 4 );
328
329     addMenuToMainbar( HelpMenu( bar ), qtr( "&Help" ), bar );
330
331 }
332
333 /**
334  * Media ( File ) Menu
335  * Opening, streaming and quit
336  **/
337 QMenu *VLCMenuBar::FileMenu( intf_thread_t *p_intf, QWidget *parent, MainInterface *mi )
338 {
339     QMenu *menu = new QMenu( parent );
340     QAction *action;
341
342     addDPStaticEntry( menu, qtr( "Open &File..." ),
343         ":/type/file-asym", SLOT( simpleOpenDialog() ), "Ctrl+O" );
344     addDPStaticEntry( menu, qtr( I_OP_OPDIR ),
345         ":/type/folder-grey", SLOT( PLOpenDir() ), "Ctrl+F" );
346     addDPStaticEntry( menu, qtr( "Open &Disc..." ),
347         ":/type/disc", SLOT( openDiscDialog() ), "Ctrl+D" );
348     addDPStaticEntry( menu, qtr( "Open &Network Stream..." ),
349         ":/type/network", SLOT( openNetDialog() ), "Ctrl+N" );
350     addDPStaticEntry( menu, qtr( "Open &Capture Device..." ),
351         ":/type/capture-card", SLOT( openCaptureDialog() ), "Ctrl+C" );
352
353     menu->addSeparator();
354
355     addDPStaticEntry( menu, qtr( "&Open (advanced)..." ),
356         ":/type/file-asym", SLOT( openFileDialog() ), "Ctrl+Shift+O" );
357     menu->addSeparator();
358
359     addDPStaticEntry( menu, qtr( "Open &Location from clipboard" ),
360                       NULL, SLOT( openUrlDialog() ), "Ctrl+V" );
361
362     if( var_InheritBool( p_intf, "qt-recentplay" ) )
363     {
364         recentsMenu = new QMenu( qtr( "Open &Recent Media" ), menu );
365         updateRecents( p_intf );
366         menu->addMenu( recentsMenu );
367     }
368     menu->addSeparator();
369
370     addDPStaticEntry( menu, qtr( I_PL_SAVE ), "", SLOT( saveAPlaylist() ),
371         "Ctrl+Y" );
372     menu->addSeparator();
373
374 #ifdef ENABLE_SOUT
375     addDPStaticEntry( menu, qtr( "Conve&rt / Save..." ), "",
376         SLOT( openAndTranscodingDialogs() ), "Ctrl+R" );
377     addDPStaticEntry( menu, qtr( "&Stream..." ),
378         ":/menu/stream", SLOT( openAndStreamingDialogs() ), "Ctrl+S" );
379     menu->addSeparator();
380 #endif
381
382     action = addMIMStaticEntry( p_intf, menu, qtr( "Quit at the end of playlist" ), "",
383                                SLOT( activatePlayQuit( bool ) ) );
384     action->setCheckable( true );
385     action->setChecked( THEMIM->getPlayExitState() );
386
387     if( mi->getSysTray() )
388     {
389         action = menu->addAction( qtr( "Close to systray"), mi,
390                                  SLOT( toggleUpdateSystrayMenu() ) );
391     }
392
393     addDPStaticEntry( menu, qtr( "&Quit" ) ,
394         ":/menu/quit", SLOT( quit() ), "Ctrl+Q" );
395     return menu;
396 }
397
398 /**
399  * Tools, like Media Information, Preferences or Messages
400  **/
401 QMenu *VLCMenuBar::ToolsMenu( QMenu *menu )
402 {
403     addDPStaticEntry( menu, qtr( "&Effects and Filters"), ":/menu/settings",
404             SLOT( extendedDialog() ), "Ctrl+E" );
405
406     addDPStaticEntry( menu, qtr( "&Track Synchronization"), ":/menu/settings",
407             SLOT( synchroDialog() ), "" );
408
409     addDPStaticEntry( menu, qtr( I_MENU_INFO ) , ":/menu/info",
410         SLOT( mediaInfoDialog() ), "Ctrl+I" );
411     addDPStaticEntry( menu, qtr( I_MENU_CODECINFO ) ,
412         ":/menu/info", SLOT( mediaCodecDialog() ), "Ctrl+J" );
413
414 #ifdef ENABLE_VLM
415     addDPStaticEntry( menu, qtr( I_MENU_VLM ), "", SLOT( vlmDialog() ),
416         "Ctrl+W" );
417 #endif
418
419     addDPStaticEntry( menu, qtr( "Program Guide" ), "", SLOT( epgDialog() ),
420         "" );
421
422     addDPStaticEntry( menu, qtr( I_MENU_MSG ),
423         ":/menu/messages", SLOT( messagesDialog() ), "Ctrl+M" );
424
425     addDPStaticEntry( menu, qtr( "Plu&gins and extensions" ),
426         "", SLOT( pluginDialog() ) );
427     menu->addSeparator();
428
429     addDPStaticEntry( menu, qtr( "Customi&ze Interface..." ),
430         ":/menu/preferences", SLOT( toolbarDialog() ) );
431
432     addDPStaticEntry( menu, qtr( "&Preferences" ),
433         ":/menu/preferences", SLOT( prefsDialog() ), "Ctrl+P", QAction::PreferencesRole );
434
435     return menu;
436 }
437
438 /**
439  * View Menu
440  * Interface modification, load other interfaces, activate Extensions
441  * \param current, set to NULL for menu creation, else for menu update
442  **/
443 QMenu *VLCMenuBar::ViewMenu( intf_thread_t *p_intf, QMenu *current, MainInterface *_mi )
444 {
445     QAction *action;
446     QMenu *menu;
447
448     MainInterface *mi = _mi ? _mi : p_intf->p_sys->p_mi;
449     assert( mi );
450
451     if( !current )
452     {
453         menu = new QMenu( qtr( "&View" ), mi );
454     }
455     else
456     {
457         menu = current;
458         //menu->clear();
459         //HACK menu->clear() does not delete submenus
460         QList<QAction*> actions = menu->actions();
461         foreach( QAction *a, actions )
462         {
463             QMenu *m = a->menu();
464             if( a->parent() == menu ) delete a;
465             else menu->removeAction( a );
466             if( m && m->parent() == menu ) delete m;
467         }
468     }
469
470     menu->addAction(
471 #ifndef __APPLE__
472             QIcon( ":/menu/playlist_menu" ),
473 #endif
474             qtr( "Play&list" ), mi,
475             SLOT( togglePlaylist() ), qtr( "Ctrl+L" ) );
476
477     menu->addSeparator();
478
479     /* Minimal View */
480     action = menu->addAction( qtr( "Mi&nimal Interface" ) );
481     action->setShortcut( qtr( "Ctrl+H" ) );
482     action->setCheckable( true );
483     action->setChecked( (mi->getControlsVisibilityStatus() & CONTROLS_HIDDEN ) );
484
485     CONNECT( action, triggered( bool ), mi, toggleMinimalView( bool ) );
486     CONNECT( mi, minimalViewToggled( bool ), action, setChecked( bool ) );
487
488     /* FullScreen View */
489     action = menu->addAction( qtr( "&Fullscreen Interface" ), mi,
490             SLOT( toggleInterfaceFullScreen() ), QString( "F11" ) );
491     action->setCheckable( true );
492     action->setChecked( mi->isInterfaceFullScreen() );
493     CONNECT( mi, fullscreenInterfaceToggled( bool ),
494              action, setChecked( bool ) );
495
496     /* Advanced Controls */
497     action = menu->addAction( qtr( "&Advanced Controls" ), mi,
498             SLOT( toggleAdvancedButtons() ) );
499     action->setCheckable( true );
500     if( mi->getControlsVisibilityStatus() & CONTROLS_ADVANCED )
501         action->setChecked( true );
502
503     /* Docked Playlist */
504     action = menu->addAction( qtr( "Docked Playlist" ) );
505     action->setCheckable( true );
506     action->setChecked( mi->isPlDocked() );
507     CONNECT( action, triggered( bool ), mi, dockPlaylist( bool ) );
508
509     action = menu->addAction( qtr( "Status Bar" ) );
510     action->setCheckable( true );
511     action->setChecked( mi->statusBar()->isVisible() );
512     CONNECT( action, triggered( bool ), mi, setStatusBarVisibility( bool) );
513 #if 0 /* For Visualisations. Not yet working */
514     adv = menu->addAction( qtr( "Visualizations selector" ), mi,
515                            SLOT( visual() ) );
516     adv->setCheckable( true );
517     if( visual_selector_enabled ) adv->setChecked( true );
518 #endif
519
520     menu->addSeparator();
521
522     InterfacesMenu( p_intf, menu );
523     menu->addSeparator();
524
525     /* Extensions */
526     ExtensionsMenu( p_intf, menu );
527
528     return menu;
529 }
530
531 /**
532  * Interface Sub-Menu, to list extras interface and skins
533  **/
534 QMenu *VLCMenuBar::InterfacesMenu( intf_thread_t *p_intf, QMenu *current )
535 {
536     QVector<vlc_object_t *> objects;
537     QVector<const char *> varnames;
538     varnames.append( "intf-add" );
539     objects.append( VLC_OBJECT(p_intf) );
540
541     return Populate( p_intf, current, varnames, objects );
542 }
543
544 /**
545  * Extensions menu: populate the current menu with extensions
546  **/
547 void VLCMenuBar::ExtensionsMenu( intf_thread_t *p_intf, QMenu *extMenu )
548 {
549     /* Get ExtensionsManager and load extensions if needed */
550     ExtensionsManager *extMgr = ExtensionsManager::getInstance( p_intf );
551
552     if( !var_InheritBool( p_intf, "qt-autoload-extensions")
553         && !extMgr->isLoaded() )
554     {
555         return;
556     }
557
558     if( !extMgr->isLoaded() && !extMgr->cannotLoad() )
559     {
560         extMgr->loadExtensions();
561     }
562
563     /* Let the ExtensionsManager build itself the menu */
564     extMenu->addSeparator();
565     extMgr->menu( extMenu );
566 }
567
568 static inline void VolumeEntries( intf_thread_t *p_intf, QMenu *current )
569 {
570     current->addSeparator();
571
572     QAction *action = current->addAction( qtr( "&Increase Volume" ),
573                 ActionsManager::getInstance( p_intf ), SLOT( AudioUp() ) );
574     action->setData( VLCMenuBar::ACTION_STATIC );
575     action = current->addAction( qtr( "&Decrease Volume" ),
576                 ActionsManager::getInstance( p_intf ), SLOT( AudioDown() ) );
577     action->setData( VLCMenuBar::ACTION_STATIC );
578     action = current->addAction( qtr( "&Mute" ),
579                 ActionsManager::getInstance( p_intf ), SLOT( toggleMuteAudio() ) );
580     action->setData( VLCMenuBar::ACTION_STATIC );
581 }
582
583 /**
584  * Main Audio Menu
585  **/
586 QMenu *VLCMenuBar::AudioMenu( intf_thread_t *p_intf, QMenu * current )
587 {
588     QVector<vlc_object_t *> objects;
589     QVector<const char *> varnames;
590     audio_output_t *p_aout;
591     input_thread_t *p_input;
592
593     if( current->isEmpty() )
594     {
595         addActionWithSubmenu( current, "audio-es", qtr( "Audio &Track" ) );
596         addActionWithSubmenu( current, "audio-channels", qtr( "Audio &Channels" ) );
597         addActionWithSubmenu( current, "audio-device", qtr( "Audio &Device" ) );
598         current->addSeparator();
599
600         addActionWithSubmenu( current, "visual", qtr( "&Visualizations" ) );
601         VolumeEntries( p_intf, current );
602     }
603
604     p_input = THEMIM->getInput();
605     p_aout = THEMIM->getAout();
606     EnableStaticEntries( current, ( p_aout != NULL ) );
607     AudioAutoMenuBuilder( p_aout, p_input, objects, varnames );
608     if( p_aout )
609     {
610         vlc_object_release( p_aout );
611     }
612
613     return Populate( p_intf, current, varnames, objects );
614 }
615
616 /* Subtitles */
617 QMenu *VLCMenuBar::SubtitleMenu( QMenu *current )
618 {
619     QAction *action;
620     QMenu *submenu = new QMenu( qtr( "&Subtitles Track" ), current );
621     action = current->addMenu( submenu );
622     action->setData( "spu-es" );
623     addDPStaticEntry( submenu, qtr( "Open File..." ), "",
624                       SLOT( loadSubtitlesFile() ) );
625     submenu->addSeparator();
626     return submenu;
627 }
628
629 /**
630  * Main Video Menu
631  * Subtitles are part of Video.
632  **/
633 QMenu *VLCMenuBar::VideoMenu( intf_thread_t *p_intf, QMenu *current, bool b_subtitle )
634 {
635     vout_thread_t *p_vout;
636     input_thread_t *p_input;
637     QVector<vlc_object_t *> objects;
638     QVector<const char *> varnames;
639
640     if( current->isEmpty() )
641     {
642         addActionWithSubmenu( current, "video-es", qtr( "Video &Track" ) );
643         if( b_subtitle)
644             SubtitleMenu( current );
645
646         current->addSeparator();
647         /* Surface modifiers */
648         addActionWithCheckbox( current, "fullscreen", qtr( "&Fullscreen" ) );
649         addActionWithCheckbox( current, "autoscale", qtr( "Always Fit &Window" ) );
650         addActionWithCheckbox( current, "video-on-top", qtr( "Always &on Top" ) );
651         addActionWithCheckbox( current, "video-wallpaper", qtr( "Set as Wall&paper" ) );
652
653         current->addSeparator();
654         /* Size modifiers */
655         addActionWithSubmenu( current, "zoom", qtr( "&Zoom" ) );
656         addActionWithSubmenu( current, "aspect-ratio", qtr( "&Aspect Ratio" ) );
657         addActionWithSubmenu( current, "crop", qtr( "&Crop" ) );
658
659         current->addSeparator();
660         /* Rendering modifiers */
661         addActionWithSubmenu( current, "deinterlace", qtr( "&Deinterlace" ) );
662         addActionWithSubmenu( current, "deinterlace-mode", qtr( "&Deinterlace mode" ) );
663         addActionWithSubmenu( current, "postprocess", qtr( "&Post processing" ) );
664
665         current->addSeparator();
666         /* Other actions */
667         addAction( current, "video-snapshot", qtr( "Take &Snapshot" ) );
668     }
669
670     p_input = THEMIM->getInput();
671
672     p_vout = THEMIM->getVout();
673     VideoAutoMenuBuilder( p_vout, p_input, objects, varnames );
674
675     if( p_vout )
676         vlc_object_release( p_vout );
677
678     return Populate( p_intf, current, varnames, objects );
679 }
680
681 /**
682  * Navigation Menu
683  * For DVD, MP4, MOV and other chapter based format
684  **/
685 QMenu *VLCMenuBar::NavigMenu( intf_thread_t *p_intf, QMenu *menu )
686 {
687     QAction *action;
688     QMenu *submenu;
689
690     addActionWithSubmenu( menu, "title", qtr( "T&itle" ) );
691     addActionWithSubmenu( menu, "chapter", qtr( "&Chapter" ) );
692     submenu = addActionWithSubmenu( menu, "navigation", qtr( "&Navigation" ) );
693     submenu->setTearOffEnabled( true );
694     addActionWithSubmenu( menu, "program", qtr( "&Program" ) );
695
696     /* FixMe: sync I_MENU_BOOKMARK string */
697     submenu = new QMenu( qtr( "Custom &Bookmarks" ), menu );
698     submenu->setTearOffEnabled( true );
699     addDPStaticEntry( submenu, qtr( "&Manage" ), "",
700                       SLOT( bookmarksDialog() ), "Ctrl+B" );
701     submenu->addSeparator();
702     action = menu->addMenu( submenu );
703     action->setData( "bookmark" );
704
705     menu->addSeparator();
706
707     PopupMenuControlEntries( menu, p_intf );
708
709     EnableStaticEntries( menu, ( THEMIM->getInput() != NULL ) );
710     return RebuildNavigMenu( p_intf, menu, true );
711 }
712
713 QMenu *VLCMenuBar::RebuildNavigMenu( intf_thread_t *p_intf, QMenu *menu, bool b_keep )
714 {
715     /* */
716     input_thread_t *p_object;
717     QVector<vlc_object_t *> objects;
718     QVector<const char *> varnames;
719
720     /* Get the input and hold it */
721     p_object = THEMIM->getInput();
722
723     InputAutoMenuBuilder( p_object, objects, varnames );
724
725     /* Title and so on */
726     PUSH_VAR( "prev-title" );
727     PUSH_VAR( "next-title" );
728     PUSH_VAR( "prev-chapter" );
729     PUSH_VAR( "next-chapter" );
730
731     /* */
732     EnableStaticEntries( menu, (p_object != NULL ) );
733     Populate( p_intf, menu, varnames, objects );
734
735     /* Remove playback actions to recreate them */
736     if( !b_keep )
737     {
738         QList< QAction* > actions = menu->actions();
739         for( int i = 0; i < actions.count(); i++ )
740             if( actions[i]->data().toInt() & ACTION_DELETE_ON_REBUILD )
741                 delete actions[i];
742     }
743
744     PopupMenuPlaylistEntries( menu, p_intf, p_object );
745
746     return menu;
747 }
748
749 /**
750  * Help/About Menu
751 **/
752 QMenu *VLCMenuBar::HelpMenu( QWidget *parent )
753 {
754     QMenu *menu = new QMenu( parent );
755     addDPStaticEntry( menu, qtr( "&Help..." ) ,
756         ":/menu/help", SLOT( helpDialog() ), "F1" );
757 #ifdef UPDATE_CHECK
758     addDPStaticEntry( menu, qtr( "Check for &Updates..." ) , "",
759                       SLOT( updateDialog() ) );
760 #endif
761     menu->addSeparator();
762     addDPStaticEntry( menu, qtr( I_MENU_ABOUT ), ":/menu/info",
763             SLOT( aboutDialog() ), "Shift+F1", QAction::AboutRole );
764     return menu;
765 }
766
767 /*****************************************************************************
768  * Popup menus - Right Click menus                                           *
769  *****************************************************************************/
770 #define POPUP_BOILERPLATE \
771     static QMenu* menu = NULL;  \
772     delete menu; menu = NULL; \
773     if( !show ) \
774         return; \
775     QVector<vlc_object_t *> objects; \
776     QVector<const char *> varnames; \
777     input_thread_t *p_input = THEMIM->getInput();
778
779 #define CREATE_POPUP \
780     menu = new QMenu(); \
781     Populate( p_intf, menu, varnames, objects ); \
782     menu->popup( QCursor::pos() ); \
783
784 void VLCMenuBar::PopupMenuPlaylistEntries( QMenu *menu,
785                                         intf_thread_t *p_intf,
786                                         input_thread_t *p_input )
787 {
788     QAction *action;
789
790     /* Play or Pause action and icon */
791     if( !p_input || var_GetInteger( p_input, "state" ) != PLAYING_S )
792     {
793         action = menu->addAction( qtr( "&Play" ),
794                 ActionsManager::getInstance( p_intf ), SLOT( play() ) );
795 #ifndef __APPLE__ /* No icons in menus in Mac */
796         action->setIcon( QIcon( ":/menu/play" ) );
797 #endif
798     }
799     else
800     {
801         action = addMIMStaticEntry( p_intf, menu, qtr( "Pause" ),
802                 ":/menu/pause", SLOT( togglePlayPause() ) );
803     }
804     action->setData( ACTION_DELETE_ON_REBUILD );
805
806     /* Stop */
807     action = addMIMStaticEntry( p_intf, menu, qtr( "&Stop" ),
808             ":/menu/stop", SLOT( stop() ), true );
809     if( !p_input )
810         action->setEnabled( false );
811     action->setData( ACTION_DELETE_ON_REBUILD );
812
813     /* Next / Previous */
814     bool bPlaylistEmpty = THEMIM->hasEmptyPlaylist();
815     action = addMIMStaticEntry( p_intf, menu, qtr( "Pre&vious" ),
816             ":/menu/previous", SLOT( prev() ), true );
817     action->setEnabled( !bPlaylistEmpty );
818     action->setData( ACTION_NO_CLEANUP + ACTION_DELETE_ON_REBUILD );
819     CONNECT( THEMIM, playlistNotEmpty(bool), action, setEnabled(bool) );
820
821     action = addMIMStaticEntry( p_intf, menu, qtr( "Ne&xt" ),
822             ":/menu/next", SLOT( next() ), true );
823     action->setEnabled( !bPlaylistEmpty );
824     action->setData( ACTION_NO_CLEANUP + ACTION_DELETE_ON_REBUILD );
825     CONNECT( THEMIM, playlistNotEmpty(bool), action, setEnabled(bool) );
826
827     menu->addSeparator();
828 }
829
830 void VLCMenuBar::PopupMenuControlEntries( QMenu *menu, intf_thread_t *p_intf,
831                                         bool b_normal )
832 {
833     QAction *action;
834     QMenu *rateMenu = new QMenu( qtr( "Sp&eed" ), menu );
835     rateMenu->setTearOffEnabled( true );
836
837     if( b_normal )
838     {
839         /* Faster/Slower */
840         action = rateMenu->addAction( qtr( "&Faster" ), THEMIM->getIM(),
841                                   SLOT( faster() ) );
842 #ifndef __APPLE__ /* No icons in menus in Mac */
843         action->setIcon( QIcon( ":/toolbar/faster2") );
844 #endif
845         action->setData( ACTION_STATIC );
846     }
847
848     action = rateMenu->addAction( qtr( "Faster (fine)" ), THEMIM->getIM(),
849                               SLOT( littlefaster() ) );
850     action->setData( ACTION_STATIC );
851
852     action = rateMenu->addAction( qtr( "N&ormal Speed" ), THEMIM->getIM(),
853                               SLOT( normalRate() ) );
854     action->setData( ACTION_STATIC );
855
856     action = rateMenu->addAction( qtr( "Slower (fine)" ), THEMIM->getIM(),
857                               SLOT( littleslower() ) );
858     action->setData( ACTION_STATIC );
859
860     if( b_normal )
861     {
862         action = rateMenu->addAction( qtr( "Slo&wer" ), THEMIM->getIM(),
863                                   SLOT( slower() ) );
864 #ifndef __APPLE__ /* No icons in menus in Mac */
865         action->setIcon( QIcon( ":/toolbar/slower2") );
866 #endif
867         action->setData( ACTION_STATIC );
868     }
869
870     action = menu->addMenu( rateMenu );
871     action->setData( ACTION_STATIC );
872
873     menu->addSeparator();
874
875     if( !b_normal ) return;
876
877     action = menu->addAction( qtr( "&Jump Forward" ), THEMIM->getIM(),
878              SLOT( jumpFwd() ) );
879 #ifndef __APPLE__ /* No icons in menus in Mac */
880     action->setIcon( QIcon( ":/toolbar/skip_fw") );
881 #endif
882     action->setData( ACTION_STATIC );
883
884     action = menu->addAction( qtr( "Jump Bac&kward" ), THEMIM->getIM(),
885              SLOT( jumpBwd() ) );
886 #ifndef __APPLE__ /* No icons in menus in Mac */
887     action->setIcon( QIcon( ":/toolbar/skip_back") );
888 #endif
889     action->setData( ACTION_STATIC );
890
891     action = menu->addAction( qtr( I_MENU_GOTOTIME ), THEDP, SLOT( gotoTimeDialog() ), qtr( "Ctrl+T" ) );
892     action->setData( ACTION_ALWAYS_ENABLED );
893
894     menu->addSeparator();
895 }
896
897 void VLCMenuBar::PopupMenuStaticEntries( QMenu *menu )
898 {
899     QMenu *openmenu = new QMenu( qtr( "Open Media" ), menu );
900     addDPStaticEntry( openmenu, qtr( "&Open File..." ),
901         ":/type/file-asym", SLOT( openFileDialog() ) );
902     addDPStaticEntry( openmenu, qtr( I_OP_OPDIR ),
903         ":/type/folder-grey", SLOT( PLOpenDir() ) );
904     addDPStaticEntry( openmenu, qtr( "Open &Disc..." ),
905         ":/type/disc", SLOT( openDiscDialog() ) );
906     addDPStaticEntry( openmenu, qtr( "Open &Network..." ),
907         ":/type/network", SLOT( openNetDialog() ) );
908     addDPStaticEntry( openmenu, qtr( "Open &Capture Device..." ),
909         ":/type/capture-card", SLOT( openCaptureDialog() ) );
910     menu->addMenu( openmenu );
911
912     menu->addSeparator();
913 #if 0
914     QMenu *helpmenu = HelpMenu( menu );
915     helpmenu->setTitle( qtr( "Help" ) );
916     menu->addMenu( helpmenu );
917 #endif
918
919     addDPStaticEntry( menu, qtr( "Quit" ), ":/menu/quit",
920                       SLOT( quit() ), "Ctrl+Q", QAction::QuitRole );
921 }
922
923 /* Video Tracks and Subtitles tracks */
924 void VLCMenuBar::VideoPopupMenu( intf_thread_t *p_intf, bool show )
925 {
926     POPUP_BOILERPLATE
927     if( p_input )
928     {
929         vout_thread_t *p_vout = THEMIM->getVout();
930         if( p_vout )
931         {
932             VideoAutoMenuBuilder( p_vout, p_input, objects, varnames );
933             vlc_object_release( p_vout );
934         }
935     }
936     CREATE_POPUP
937 }
938
939 /* Audio Tracks */
940 void VLCMenuBar::AudioPopupMenu( intf_thread_t *p_intf, bool show )
941 {
942     POPUP_BOILERPLATE
943     if( p_input )
944     {
945         audio_output_t *p_aout = THEMIM->getAout();
946         AudioAutoMenuBuilder( p_aout, p_input, objects, varnames );
947         if( p_aout )
948             vlc_object_release( p_aout );
949     }
950     CREATE_POPUP
951 }
952
953 /* Navigation stuff, and general menus ( open ), used only for skins */
954 void VLCMenuBar::MiscPopupMenu( intf_thread_t *p_intf, bool show )
955 {
956     POPUP_BOILERPLATE
957
958     if( p_input )
959     {
960         varnames.append( "audio-es" );
961         InputAutoMenuBuilder( p_input, objects, varnames );
962         menu->addSeparator();
963     }
964
965     menu = new QMenu();
966     Populate( p_intf, menu, varnames, objects );
967
968     menu->addSeparator();
969     PopupMenuPlaylistEntries( menu, p_intf, p_input );
970
971     menu->addSeparator();
972     PopupMenuControlEntries( menu, p_intf );
973
974     menu->addSeparator();
975     PopupMenuStaticEntries( menu );
976
977     menu->popup( QCursor::pos() );
978 }
979
980 /* Main Menu that sticks everything together  */
981 void VLCMenuBar::PopupMenu( intf_thread_t *p_intf, bool show )
982 {
983     POPUP_BOILERPLATE
984
985     /* */
986     menu = new QMenu();
987     QAction *action;
988     bool b_isFullscreen = false;
989     MainInterface *mi = p_intf->p_sys->p_mi;
990
991     PopupMenuPlaylistEntries( menu, p_intf, p_input );
992     menu->addSeparator();
993
994     if( p_input )
995     {
996         QMenu *submenu;
997         vout_thread_t *p_vout = THEMIM->getVout();
998
999         /* Add a fullscreen switch button, since it is the most used function */
1000         if( p_vout )
1001         {
1002             vlc_value_t val; var_Get( p_vout, "fullscreen", &val );
1003
1004             b_isFullscreen = !( !val.b_bool );
1005             if( b_isFullscreen )
1006             {
1007                 val.b_bool = false;
1008                 CreateAndConnect( menu, "fullscreen",
1009                         qtr( "Leave Fullscreen" ),"" , ITEM_NORMAL,
1010                         VLC_OBJECT(p_vout), val, VLC_VAR_BOOL, b_isFullscreen );
1011             }
1012             vlc_object_release( p_vout );
1013
1014             menu->addSeparator();
1015         }
1016
1017         /* Input menu */
1018         InputAutoMenuBuilder( p_input, objects, varnames );
1019
1020         /* Audio menu */
1021         submenu = new QMenu( menu );
1022         action = menu->addMenu( AudioMenu( p_intf, submenu ) );
1023         action->setText( qtr( "&Audio" ) );
1024         if( action->menu()->isEmpty() )
1025             action->setEnabled( false );
1026
1027         /* Video menu */
1028         submenu = new QMenu( menu );
1029         action = menu->addMenu( VideoMenu( p_intf, submenu, false ) );
1030         action->setText( qtr( "&Video" ) );
1031         if( action->menu()->isEmpty() )
1032             action->setEnabled( false );
1033
1034         submenu = SubtitleMenu( menu );
1035         submenu->setTitle( qtr( "Subti&tle") );
1036         UpdateItem( p_intf, menu, "spu-es", VLC_OBJECT(p_input), true );
1037
1038         /* Playback menu for chapters */
1039         submenu = new QMenu( menu );
1040         action = menu->addMenu( NavigMenu( p_intf, submenu ) );
1041         action->setText( qtr( "&Playback" ) );
1042         if( action->menu()->isEmpty() )
1043             action->setEnabled( false );
1044     }
1045
1046     menu->addSeparator();
1047
1048     /* Add some special entries for windowed mode: Interface Menu */
1049     if( !b_isFullscreen )
1050     {
1051         QMenu *submenu = new QMenu( qtr( "Tools" ), menu );
1052         /*QMenu *tools =*/ ToolsMenu( submenu );
1053         submenu->addSeparator();
1054
1055         /* In skins interface, append some items */
1056         if( !mi )
1057         {
1058             submenu->setTitle( qtr( "Interface" ) );
1059             if( p_intf->p_sys->b_isDialogProvider )
1060             {
1061                 vlc_object_t* p_object = p_intf->p_parent;
1062
1063                 objects.clear(); varnames.clear();
1064                 objects.append( p_object );
1065                 varnames.append( "intf-skins" );
1066                 Populate( p_intf, submenu, varnames, objects );
1067
1068                 objects.clear(); varnames.clear();
1069                 objects.append( p_object );
1070                 varnames.append( "intf-skins-interactive" );
1071                 Populate( p_intf, submenu, varnames, objects );
1072             }
1073             else
1074                 msg_Warn( p_intf, "could not find parent interface" );
1075         }
1076         else
1077         {
1078             QMenu *bar = menu; // Needed for next macro
1079             BAR_DADD( ViewMenu( p_intf, NULL, mi ), qtr( "V&iew" ), 4 );
1080         }
1081
1082         menu->addMenu( submenu );
1083     }
1084
1085     /* */
1086     QMenuView *plMenu = new QMenuView( menu, 25 );
1087     plMenu->setTitle( qtr("Playlist") );
1088     PLModel *model = PLModel::getPLModel( p_intf );
1089     plMenu->setModel( model );
1090     CONNECT( plMenu, activated(const QModelIndex&),
1091              model, activateItem(const QModelIndex&));
1092     menu->addMenu( plMenu );
1093
1094     /* Static entries for ending, like open */
1095     PopupMenuStaticEntries( menu );
1096
1097     menu->popup( QCursor::pos() );
1098 }
1099
1100 #undef CREATE_POPUP
1101 #undef POPUP_BOILERPLATE
1102 #undef BAR_DADD
1103
1104 /************************************************************************
1105  * Systray Menu                                                         *
1106  ************************************************************************/
1107
1108 void VLCMenuBar::updateSystrayMenu( MainInterface *mi,
1109                                   intf_thread_t *p_intf,
1110                                   bool b_force_visible )
1111 {
1112     input_thread_t *p_input = THEMIM->getInput();
1113
1114     /* Get the systray menu and clean it */
1115     QMenu *sysMenu = mi->getSysTrayMenu();
1116     sysMenu->clear();
1117
1118 #ifndef Q_WS_MAC
1119     /* Hide / Show VLC and cone */
1120     if( mi->isVisible() || b_force_visible )
1121     {
1122         sysMenu->addAction( QIcon( ":/logo/vlc16.png" ),
1123                             qtr( "&Hide VLC media player in taskbar" ), mi,
1124                             SLOT( hideUpdateSystrayMenu() ) );
1125     }
1126     else
1127     {
1128         sysMenu->addAction( QIcon( ":/logo/vlc16.png" ),
1129                             qtr( "Sho&w VLC media player" ), mi,
1130                             SLOT( showUpdateSystrayMenu() ) );
1131     }
1132     sysMenu->addSeparator();
1133 #endif
1134
1135     PopupMenuPlaylistEntries( sysMenu, p_intf, p_input );
1136     PopupMenuControlEntries( sysMenu, p_intf, false );
1137
1138     VolumeEntries( p_intf, sysMenu );
1139     sysMenu->addSeparator();
1140     addDPStaticEntry( sysMenu, qtr( "&Open Media" ),
1141             ":/type/file-wide", SLOT( openFileDialog() ) );
1142     addDPStaticEntry( sysMenu, qtr( "&Quit" ) ,
1143             ":/menu/quit", SLOT( quit() ) );
1144
1145     /* Set the menu */
1146     mi->getSysTray()->setContextMenu( sysMenu );
1147 }
1148
1149
1150 #undef PUSH_VAR
1151
1152 /*************************************************************************
1153  * Builders for automenus
1154  *************************************************************************/
1155 QMenu * VLCMenuBar::Populate( intf_thread_t *p_intf,
1156                             QMenu *current,
1157                             QVector< const char *> & varnames,
1158                             QVector<vlc_object_t *> & objects )
1159 {
1160     QMenu *menu = current;
1161     assert( menu );
1162
1163     currentGroup = NULL;
1164
1165     for( int i = 0; i < (int)objects.count() ; i++ )
1166     {
1167         if( !varnames[i] || !*varnames[i] )
1168         {
1169             menu->addSeparator();
1170             continue;
1171         }
1172
1173         UpdateItem( p_intf, menu, varnames[i], objects[i], true );
1174     }
1175     return menu;
1176 }
1177
1178 /*****************************************************************************
1179  * Private methods.
1180  *****************************************************************************/
1181
1182 static bool IsMenuEmpty( const char *psz_var,
1183                          vlc_object_t *p_object,
1184                          bool b_root = true )
1185 {
1186     vlc_value_t val, val_list;
1187     int i_type, i_result, i;
1188
1189     /* Check the type of the object variable */
1190     i_type = var_Type( p_object, psz_var );
1191
1192     /* Check if we want to display the variable */
1193     if( !( i_type & VLC_VAR_HASCHOICE ) ) return false;
1194
1195     var_Change( p_object, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
1196     if( val.i_int == 0 ) return true;
1197
1198     if( ( i_type & VLC_VAR_TYPE ) != VLC_VAR_VARIABLE )
1199     {
1200         if( val.i_int == 1 && b_root ) return true;
1201         else return false;
1202     }
1203
1204     /* Check children variables in case of VLC_VAR_VARIABLE */
1205     if( var_Change( p_object, psz_var, VLC_VAR_GETLIST, &val_list, NULL ) < 0 )
1206     {
1207         return true;
1208     }
1209
1210     for( i = 0, i_result = true; i < val_list.p_list->i_count; i++ )
1211     {
1212         if( !IsMenuEmpty( val_list.p_list->p_values[i].psz_string,
1213                     p_object, false ) )
1214         {
1215             i_result = false;
1216             break;
1217         }
1218     }
1219
1220     /* clean up everything */
1221     var_FreeList( &val_list, NULL );
1222
1223     return i_result;
1224 }
1225
1226 #define TEXT_OR_VAR qfu ( text.psz_string ? text.psz_string : psz_var )
1227
1228 void VLCMenuBar::UpdateItem( intf_thread_t *p_intf, QMenu *menu,
1229         const char *psz_var, vlc_object_t *p_object, bool b_submenu )
1230 {
1231     vlc_value_t val, text;
1232     int i_type;
1233
1234     QAction *action = FindActionWithVar( menu, psz_var );
1235     if( action )
1236         DeleteNonStaticEntries( action->menu() );
1237
1238     if( !p_object )
1239     {
1240         if( action )
1241             action->setEnabled( false );
1242         return;
1243     }
1244
1245     /* Check the type of the object variable */
1246     /* This HACK is needed so we have a radio button for audio and video tracks
1247        instread of a checkbox */
1248     if( !strcmp( psz_var, "audio-es" )
1249      || !strcmp( psz_var, "video-es" ) )
1250         i_type = VLC_VAR_INTEGER | VLC_VAR_HASCHOICE;
1251     else
1252         i_type = var_Type( p_object, psz_var );
1253
1254     switch( i_type & VLC_VAR_TYPE )
1255     {
1256         case VLC_VAR_VOID:
1257         case VLC_VAR_BOOL:
1258         case VLC_VAR_VARIABLE:
1259         case VLC_VAR_STRING:
1260         case VLC_VAR_INTEGER:
1261         case VLC_VAR_FLOAT:
1262             break;
1263         default:
1264             /* Variable doesn't exist or isn't handled */
1265             if( action )
1266                 action->setEnabled( false );
1267             return;
1268     }
1269
1270     /* Make sure we want to display the variable */
1271     if( menu->isEmpty() && IsMenuEmpty( psz_var, p_object ) )
1272     {
1273         if( action )
1274             action->setEnabled( false );
1275         return;
1276     }
1277
1278     /* Get the descriptive name of the variable */
1279     int i_ret = var_Change( p_object, psz_var, VLC_VAR_GETTEXT, &text, NULL );
1280     if( i_ret != VLC_SUCCESS )
1281     {
1282         text.psz_string = NULL;
1283     }
1284
1285     if( !action )
1286     {
1287         action = new QAction( TEXT_OR_VAR, menu );
1288         menu->addAction( action );
1289         action->setData( psz_var );
1290     }
1291
1292     /* Some specific stuff */
1293     bool forceDisabled = false;
1294     if( !strcmp( psz_var, "spu-es" ) )
1295     {
1296         vout_thread_t *p_vout = THEMIM->getVout();
1297         forceDisabled = ( p_vout == NULL );
1298         if( p_vout )
1299             vlc_object_release( p_vout );
1300     }
1301
1302     if( i_type & VLC_VAR_HASCHOICE )
1303     {
1304         /* Append choices menu */
1305         if( b_submenu )
1306         {
1307             QMenu *submenu;
1308             submenu = action->menu();
1309             if( !submenu )
1310             {
1311                 submenu = new QMenu( menu );
1312                 action->setMenu( submenu );
1313             }
1314
1315             action->setEnabled(
1316                CreateChoicesMenu( submenu, psz_var, p_object, true ) == 0 );
1317             if( forceDisabled )
1318                 action->setEnabled( false );
1319         }
1320         else
1321         {
1322             action->setEnabled(
1323                 CreateChoicesMenu( menu, psz_var, p_object, true ) == 0 );
1324         }
1325         FREENULL( text.psz_string );
1326         return;
1327     }
1328
1329     switch( i_type & VLC_VAR_TYPE )
1330     {
1331         case VLC_VAR_VOID:
1332             val.i_int = 0;  // Prevent the copy of an uninitialized value
1333             CreateAndConnect( menu, psz_var, TEXT_OR_VAR, "", ITEM_NORMAL,
1334                     p_object, val, i_type );
1335             break;
1336
1337         case VLC_VAR_BOOL:
1338             var_Get( p_object, psz_var, &val );
1339             val.b_bool = !val.b_bool;
1340             CreateAndConnect( menu, psz_var, TEXT_OR_VAR, "", ITEM_CHECK,
1341                     p_object, val, i_type, !val.b_bool );
1342             break;
1343     }
1344     FREENULL( text.psz_string );
1345 }
1346
1347 #undef TEXT_OR_VAR
1348
1349 /** HACK for the navigation submenu:
1350  * "title %2i" variables take the value 0 if not set
1351  */
1352 static bool CheckTitle( vlc_object_t *p_object, const char *psz_var )
1353 {
1354     int i_title = 0;
1355     if( sscanf( psz_var, "title %2i", &i_title ) <= 0 )
1356         return true;
1357
1358     int i_current_title = var_GetInteger( p_object, "title" );
1359     return ( i_title == i_current_title );
1360 }
1361
1362
1363 int VLCMenuBar::CreateChoicesMenu( QMenu *submenu, const char *psz_var,
1364         vlc_object_t *p_object, bool b_root )
1365 {
1366     vlc_value_t val, val_list, text_list;
1367     int i_type, i;
1368
1369     /* Check the type of the object variable */
1370     i_type = var_Type( p_object, psz_var );
1371
1372     /* Make sure we want to display the variable */
1373     if( submenu->isEmpty() && IsMenuEmpty( psz_var, p_object, b_root ) )
1374         return VLC_EGENERIC;
1375
1376     switch( i_type & VLC_VAR_TYPE )
1377     {
1378         case VLC_VAR_VOID:
1379         case VLC_VAR_BOOL:
1380         case VLC_VAR_VARIABLE:
1381         case VLC_VAR_STRING:
1382         case VLC_VAR_INTEGER:
1383         case VLC_VAR_FLOAT:
1384             break;
1385         default:
1386             /* Variable doesn't exist or isn't handled */
1387             return VLC_EGENERIC;
1388     }
1389
1390     if( var_Change( p_object, psz_var, VLC_VAR_GETLIST,
1391                     &val_list, &text_list ) < 0 )
1392     {
1393         return VLC_EGENERIC;
1394     }
1395
1396 #define CURVAL val_list.p_list->p_values[i]
1397 #define CURTEXT text_list.p_list->p_values[i].psz_string
1398 #define RADIO_OR_COMMAND  ( i_type & ( VLC_VAR_ISCOMMAND | VLC_VAR_HASCHOICE ) ) ? ITEM_RADIO : ITEM_NORMAL
1399
1400     for( i = 0; i < val_list.p_list->i_count; i++ )
1401     {
1402         vlc_value_t another_val;
1403         QString menutext;
1404         QMenu *subsubmenu = new QMenu( submenu );
1405
1406         switch( i_type & VLC_VAR_TYPE )
1407         {
1408             case VLC_VAR_VARIABLE:
1409                 CreateChoicesMenu( subsubmenu, CURVAL.psz_string, p_object, false );
1410                 subsubmenu->setTitle( qfu( CURTEXT ? CURTEXT :CURVAL.psz_string ) );
1411                 submenu->addMenu( subsubmenu );
1412                 break;
1413
1414             case VLC_VAR_STRING:
1415                 var_Get( p_object, psz_var, &val );
1416                 another_val.psz_string = strdup( CURVAL.psz_string );
1417                 menutext = qfu( CURTEXT ? CURTEXT : another_val.psz_string );
1418                 CreateAndConnect( submenu, psz_var, menutext, "", RADIO_OR_COMMAND,
1419                         p_object, another_val, i_type,
1420                         val.psz_string && !strcmp( val.psz_string, CURVAL.psz_string ) );
1421
1422                 free( val.psz_string );
1423                 break;
1424
1425             case VLC_VAR_INTEGER:
1426                 var_Get( p_object, psz_var, &val );
1427                 if( CURTEXT ) menutext = qfu( CURTEXT );
1428                 else menutext = QString::number( CURVAL.i_int );
1429                 CreateAndConnect( submenu, psz_var, menutext, "", RADIO_OR_COMMAND,
1430                         p_object, CURVAL, i_type,
1431                         ( CURVAL.i_int == val.i_int )
1432                         && CheckTitle( p_object, psz_var ) );
1433                 break;
1434
1435             case VLC_VAR_FLOAT:
1436                 var_Get( p_object, psz_var, &val );
1437                 if( CURTEXT ) menutext = qfu( CURTEXT );
1438                 else menutext.sprintf( "%.2f", CURVAL.f_float );
1439                 CreateAndConnect( submenu, psz_var, menutext, "", RADIO_OR_COMMAND,
1440                         p_object, CURVAL, i_type,
1441                         CURVAL.f_float == val.f_float );
1442                 break;
1443
1444             default:
1445                 break;
1446         }
1447     }
1448     currentGroup = NULL;
1449
1450     /* clean up everything */
1451     var_FreeList( &val_list, &text_list );
1452
1453 #undef RADIO_OR_COMMAND
1454 #undef CURVAL
1455 #undef CURTEXT
1456     return submenu->isEmpty() ? VLC_EGENERIC : VLC_SUCCESS;
1457 }
1458
1459 void VLCMenuBar::CreateAndConnect( QMenu *menu, const char *psz_var,
1460         const QString& text, const QString& help,
1461         int i_item_type, vlc_object_t *p_obj,
1462         vlc_value_t val, int i_val_type,
1463         bool checked )
1464 {
1465     QAction *action = FindActionWithVar( menu, psz_var );
1466
1467     bool b_new = false;
1468     if( !action )
1469     {
1470         action = new QAction( text, menu );
1471         menu->addAction( action );
1472         b_new = true;
1473     }
1474
1475     action->setToolTip( help );
1476     action->setEnabled( p_obj != NULL );
1477
1478     if( i_item_type == ITEM_CHECK )
1479     {
1480         action->setCheckable( true );
1481     }
1482     else if( i_item_type == ITEM_RADIO )
1483     {
1484         action->setCheckable( true );
1485         if( !currentGroup )
1486             currentGroup = new QActionGroup( menu );
1487         currentGroup->addAction( action );
1488     }
1489
1490     action->setChecked( checked );
1491
1492     MenuItemData *itemData = qFindChild<MenuItemData*>( action, QString() );
1493     delete itemData;
1494     itemData = new MenuItemData( action, p_obj, i_val_type, val, psz_var );
1495
1496     /* remove previous signal-slot connection(s) if any */
1497     action->disconnect( );
1498
1499     CONNECT( action, triggered(), THEDP->menusMapper, map() );
1500     THEDP->menusMapper->setMapping( action, itemData );
1501
1502     if( b_new )
1503         menu->addAction( action );
1504 }
1505
1506 void VLCMenuBar::DoAction( QObject *data )
1507 {
1508     MenuItemData *itemData = qobject_cast<MenuItemData *>( data );
1509     vlc_object_t *p_object = itemData->p_obj;
1510     if( p_object == NULL ) return;
1511
1512     /* Preserve settings across vouts via the playlist object: */
1513     if( !strcmp( itemData->psz_var, "fullscreen" )
1514      || !strcmp( itemData->psz_var, "video-on-top" ) )
1515         var_Set( pl_Get( p_object ), itemData->psz_var, itemData->val );
1516
1517     var_Set( p_object, itemData->psz_var, itemData->val );
1518 }
1519
1520 void VLCMenuBar::updateRecents( intf_thread_t *p_intf )
1521 {
1522     if( recentsMenu )
1523     {
1524         QAction* action;
1525         RecentsMRL* rmrl = RecentsMRL::getInstance( p_intf );
1526         QStringList l = rmrl->recents();
1527
1528         recentsMenu->clear();
1529
1530         if( !l.count() )
1531         {
1532             recentsMenu->setEnabled( false );
1533         }
1534         else
1535         {
1536             for( int i = 0; i < l.count(); ++i )
1537             {
1538                 QString mrl = l.at( i );
1539                 char *psz = decode_URI_duplicate( qtu( mrl ) );
1540                 QString text = qfu( psz );
1541
1542                 free( psz );
1543                 action = recentsMenu->addAction(
1544                         QString( i < 9 ? "&%1: ": "%1: " ).arg( i + 1 ) +
1545                             QApplication::fontMetrics().elidedText( text,
1546                                                           Qt::ElideLeft, 400 ),
1547                         rmrl->signalMapper, SLOT( map() ),
1548                         i < 9 ? QString( "Ctrl+%1" ).arg( i + 1 ) : "" );
1549                 rmrl->signalMapper->setMapping( action, l.at( i ) );
1550             }
1551
1552             recentsMenu->addSeparator();
1553             recentsMenu->addAction( qtr("&Clear"), rmrl, SLOT( clear() ) );
1554             recentsMenu->setEnabled( true );
1555         }
1556     }
1557 }