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