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