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