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