]> git.sesse.net Git - vlc/blob - modules/gui/qt4/menus.cpp
Qt: fix subtitle popup menu
[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, bool b_popup )
630 {
631     input_thread_t *p_input;
632     QVector<vlc_object_t *> objects;
633     QVector<const char *> varnames;
634
635     if( current->isEmpty() || b_popup )
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         /* Subtitles menu */
1050         submenu = new QMenu( menu );
1051         action = menu->addMenu( SubtitleMenu( p_intf, submenu, true ) );
1052         action->setText( qtr( "Subti&tle") );
1053         UpdateItem( p_intf, submenu, "spu-es", VLC_OBJECT(p_input), true );
1054
1055         /* Playback menu for chapters */
1056         submenu = new QMenu( menu );
1057         action = menu->addMenu( NavigMenu( p_intf, submenu ) );
1058         action->setText( qtr( "&Playback" ) );
1059         if( action->menu()->isEmpty() )
1060             action->setEnabled( false );
1061     }
1062
1063     menu->addSeparator();
1064
1065     /* Add some special entries for windowed mode: Interface Menu */
1066     if( !b_isFullscreen )
1067     {
1068         QMenu *submenu = new QMenu( qtr( "T&ools" ), menu );
1069         /*QMenu *tools =*/ ToolsMenu( p_intf, submenu );
1070         submenu->addSeparator();
1071
1072         /* In skins interface, append some items */
1073         if( !mi )
1074         {
1075             submenu->setTitle( qtr( "Interface" ) );
1076             if( p_intf->p_sys->b_isDialogProvider )
1077             {
1078                 /* list of skins available */
1079                 vlc_object_t* p_object = p_intf->p_parent;
1080
1081                 objects.clear(); varnames.clear();
1082                 objects.append( p_object );
1083                 varnames.append( "intf-skins" );
1084                 Populate( p_intf, submenu, varnames, objects );
1085
1086                 objects.clear(); varnames.clear();
1087                 objects.append( p_object );
1088                 varnames.append( "intf-skins-interactive" );
1089                 Populate( p_intf, submenu, varnames, objects );
1090
1091                 submenu->addSeparator();
1092
1093                 /* Extensions */
1094                 ExtensionsMenu( p_intf, submenu );
1095
1096             }
1097             else
1098                 msg_Warn( p_intf, "could not find parent interface" );
1099         }
1100         else
1101         {
1102             QMenu *bar = menu; // Needed for next macro
1103             BAR_DADD( ViewMenu( p_intf, NULL, mi ), qtr( "V&iew" ), 4 );
1104         }
1105
1106         menu->addMenu( submenu );
1107     }
1108
1109     /* */
1110     QMenuView *plMenu = new QMenuView( menu, 25 );
1111     plMenu->setTitle( qtr("Playlist") );
1112     PLModel *model = PLModel::getPLModel( p_intf );
1113     plMenu->setModel( model );
1114     CONNECT( plMenu, activated(const QModelIndex&),
1115              model->sigs, activateItemSlot(const QModelIndex&));
1116     menu->addMenu( plMenu );
1117
1118     /* Static entries for ending, like open */
1119     PopupMenuStaticEntries( menu );
1120
1121     menu->popup( QCursor::pos() );
1122 }
1123
1124 #undef CREATE_POPUP
1125 #undef POPUP_BOILERPLATE
1126 #undef BAR_DADD
1127
1128 /************************************************************************
1129  * Systray Menu                                                         *
1130  ************************************************************************/
1131
1132 void VLCMenuBar::updateSystrayMenu( MainInterface *mi,
1133                                   intf_thread_t *p_intf,
1134                                   bool b_force_visible )
1135 {
1136     input_thread_t *p_input = THEMIM->getInput();
1137
1138     /* Get the systray menu and clean it */
1139     QMenu *sysMenu = mi->getSysTrayMenu();
1140     sysMenu->clear();
1141
1142 #ifndef Q_OS_MAC
1143     /* Hide / Show VLC and cone */
1144     if( mi->isVisible() || b_force_visible )
1145     {
1146         sysMenu->addAction( QIcon( ":/logo/vlc16.png" ),
1147                             qtr( "&Hide VLC media player in taskbar" ), mi,
1148                             SLOT( hideUpdateSystrayMenu() ) );
1149     }
1150     else
1151     {
1152         sysMenu->addAction( QIcon( ":/logo/vlc16.png" ),
1153                             qtr( "Sho&w VLC media player" ), mi,
1154                             SLOT( showUpdateSystrayMenu() ) );
1155     }
1156     sysMenu->addSeparator();
1157 #endif
1158
1159     PopupMenuPlaylistEntries( sysMenu, p_intf, p_input );
1160     PopupMenuControlEntries( sysMenu, p_intf, false );
1161
1162     VolumeEntries( p_intf, sysMenu );
1163     sysMenu->addSeparator();
1164     addDPStaticEntry( sysMenu, qtr( "&Open Media" ),
1165             ":/type/file-wide", SLOT( openFileDialog() ) );
1166     addDPStaticEntry( sysMenu, qtr( "&Quit" ) ,
1167             ":/menu/exit", SLOT( quit() ) );
1168
1169     /* Set the menu */
1170     mi->getSysTray()->setContextMenu( sysMenu );
1171 }
1172
1173
1174 #undef PUSH_VAR
1175
1176 /*************************************************************************
1177  * Builders for automenus
1178  *************************************************************************/
1179 QMenu * VLCMenuBar::Populate( intf_thread_t *p_intf,
1180                             QMenu *current,
1181                             QVector< const char *> & varnames,
1182                             QVector<vlc_object_t *> & objects )
1183 {
1184     QMenu *menu = current;
1185     assert( menu );
1186
1187     currentGroup = NULL;
1188
1189     for( int i = 0; i < (int)objects.count() ; i++ )
1190     {
1191         if( !varnames[i] || !*varnames[i] )
1192         {
1193             menu->addSeparator();
1194             continue;
1195         }
1196
1197         UpdateItem( p_intf, menu, varnames[i], objects[i], true );
1198     }
1199     return menu;
1200 }
1201
1202 /*****************************************************************************
1203  * Private methods.
1204  *****************************************************************************/
1205
1206 static bool IsMenuEmpty( const char *psz_var,
1207                          vlc_object_t *p_object,
1208                          bool b_root = true )
1209 {
1210     vlc_value_t val, val_list;
1211     int i_type, i_result, i;
1212
1213     /* Check the type of the object variable */
1214     i_type = var_Type( p_object, psz_var );
1215
1216     /* Check if we want to display the variable */
1217     if( !( i_type & VLC_VAR_HASCHOICE ) ) return false;
1218
1219     var_Change( p_object, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
1220     if( val.i_int == 0 ) return true;
1221
1222     if( ( i_type & VLC_VAR_TYPE ) != VLC_VAR_VARIABLE )
1223     {
1224         if( val.i_int == 1 && b_root ) return true;
1225         else return false;
1226     }
1227
1228     /* Check children variables in case of VLC_VAR_VARIABLE */
1229     if( var_Change( p_object, psz_var, VLC_VAR_GETLIST, &val_list, NULL ) < 0 )
1230     {
1231         return true;
1232     }
1233
1234     for( i = 0, i_result = true; i < val_list.p_list->i_count; i++ )
1235     {
1236         if( !IsMenuEmpty( val_list.p_list->p_values[i].psz_string,
1237                     p_object, false ) )
1238         {
1239             i_result = false;
1240             break;
1241         }
1242     }
1243
1244     /* clean up everything */
1245     var_FreeList( &val_list, NULL );
1246
1247     return i_result;
1248 }
1249
1250 #define TEXT_OR_VAR qfue ( text.psz_string ? text.psz_string : psz_var )
1251
1252 void VLCMenuBar::UpdateItem( intf_thread_t *p_intf, QMenu *menu,
1253         const char *psz_var, vlc_object_t *p_object, bool b_submenu )
1254 {
1255     vlc_value_t val, text;
1256     int i_type;
1257
1258     QAction *action = FindActionWithVar( menu, psz_var );
1259     if( action )
1260         DeleteNonStaticEntries( action->menu() );
1261
1262     if( !p_object )
1263     {
1264         if( action )
1265             action->setEnabled( false );
1266         return;
1267     }
1268
1269     /* Check the type of the object variable */
1270     /* This HACK is needed so we have a radio button for audio and video tracks
1271        instread of a checkbox */
1272     if( !strcmp( psz_var, "audio-es" )
1273      || !strcmp( psz_var, "video-es" ) )
1274         i_type = VLC_VAR_INTEGER | VLC_VAR_HASCHOICE;
1275     else
1276         i_type = var_Type( p_object, psz_var );
1277
1278     switch( i_type & VLC_VAR_TYPE )
1279     {
1280         case VLC_VAR_VOID:
1281         case VLC_VAR_BOOL:
1282         case VLC_VAR_VARIABLE:
1283         case VLC_VAR_STRING:
1284         case VLC_VAR_INTEGER:
1285         case VLC_VAR_FLOAT:
1286             break;
1287         default:
1288             /* Variable doesn't exist or isn't handled */
1289             if( action )
1290                 action->setEnabled( false );
1291             return;
1292     }
1293
1294     /* Make sure we want to display the variable */
1295     if( menu->isEmpty() && IsMenuEmpty( psz_var, p_object ) )
1296     {
1297         if( action )
1298             action->setEnabled( false );
1299         return;
1300     }
1301
1302     /* Get the descriptive name of the variable */
1303     int i_ret = var_Change( p_object, psz_var, VLC_VAR_GETTEXT, &text, NULL );
1304     if( i_ret != VLC_SUCCESS )
1305     {
1306         text.psz_string = NULL;
1307     }
1308
1309     if( !action )
1310     {
1311         action = new QAction( TEXT_OR_VAR, menu );
1312         menu->addAction( action );
1313         action->setData( psz_var );
1314     }
1315
1316     /* Some specific stuff */
1317     bool forceDisabled = false;
1318     if( !strcmp( psz_var, "spu-es" ) )
1319     {
1320         vout_thread_t *p_vout = THEMIM->getVout();
1321         forceDisabled = ( p_vout == NULL );
1322         if( p_vout )
1323             vlc_object_release( p_vout );
1324     }
1325
1326     if( i_type & VLC_VAR_HASCHOICE )
1327     {
1328         /* Append choices menu */
1329         if( b_submenu )
1330         {
1331             QMenu *submenu;
1332             submenu = action->menu();
1333             if( !submenu )
1334             {
1335                 submenu = new QMenu( menu );
1336                 action->setMenu( submenu );
1337             }
1338
1339             action->setEnabled(
1340                CreateChoicesMenu( submenu, psz_var, p_object, true ) == 0 );
1341             if( forceDisabled )
1342                 action->setEnabled( false );
1343         }
1344         else
1345         {
1346             action->setEnabled(
1347                 CreateChoicesMenu( menu, psz_var, p_object, true ) == 0 );
1348         }
1349         FREENULL( text.psz_string );
1350         return;
1351     }
1352
1353     switch( i_type & VLC_VAR_TYPE )
1354     {
1355         case VLC_VAR_VOID:
1356             val.i_int = 0;  // Prevent the copy of an uninitialized value
1357             CreateAndConnect( menu, psz_var, TEXT_OR_VAR, "", ITEM_NORMAL,
1358                     p_object, val, i_type );
1359             break;
1360
1361         case VLC_VAR_BOOL:
1362             var_Get( p_object, psz_var, &val );
1363             val.b_bool = !val.b_bool;
1364             CreateAndConnect( menu, psz_var, TEXT_OR_VAR, "", ITEM_CHECK,
1365                     p_object, val, i_type, !val.b_bool );
1366             break;
1367     }
1368     FREENULL( text.psz_string );
1369 }
1370
1371 #undef TEXT_OR_VAR
1372
1373 /** HACK for the navigation submenu:
1374  * "title %2i" variables take the value 0 if not set
1375  */
1376 static bool CheckTitle( vlc_object_t *p_object, const char *psz_var )
1377 {
1378     int i_title = 0;
1379     if( sscanf( psz_var, "title %2i", &i_title ) <= 0 )
1380         return true;
1381
1382     int i_current_title = var_GetInteger( p_object, "title" );
1383     return ( i_title == i_current_title );
1384 }
1385
1386
1387 int VLCMenuBar::CreateChoicesMenu( QMenu *submenu, const char *psz_var,
1388         vlc_object_t *p_object, bool b_root )
1389 {
1390     vlc_value_t val, val_list, text_list;
1391     int i_type, i;
1392
1393     /* Check the type of the object variable */
1394     i_type = var_Type( p_object, psz_var );
1395
1396     /* Make sure we want to display the variable */
1397     if( submenu->isEmpty() && IsMenuEmpty( psz_var, p_object, b_root ) )
1398         return VLC_EGENERIC;
1399
1400     switch( i_type & VLC_VAR_TYPE )
1401     {
1402         case VLC_VAR_VOID:
1403         case VLC_VAR_BOOL:
1404         case VLC_VAR_VARIABLE:
1405         case VLC_VAR_STRING:
1406         case VLC_VAR_INTEGER:
1407         case VLC_VAR_FLOAT:
1408             break;
1409         default:
1410             /* Variable doesn't exist or isn't handled */
1411             return VLC_EGENERIC;
1412     }
1413
1414     if( var_Change( p_object, psz_var, VLC_VAR_GETLIST,
1415                     &val_list, &text_list ) < 0 )
1416     {
1417         return VLC_EGENERIC;
1418     }
1419
1420 #define CURVAL val_list.p_list->p_values[i]
1421 #define CURTEXT text_list.p_list->p_values[i].psz_string
1422 #define RADIO_OR_COMMAND  ( i_type & ( VLC_VAR_ISCOMMAND | VLC_VAR_HASCHOICE ) ) ? ITEM_RADIO : ITEM_NORMAL
1423
1424     for( i = 0; i < val_list.p_list->i_count; i++ )
1425     {
1426         vlc_value_t another_val;
1427         QString menutext;
1428         QMenu *subsubmenu = new QMenu( submenu );
1429
1430         switch( i_type & VLC_VAR_TYPE )
1431         {
1432             case VLC_VAR_VARIABLE:
1433                 CreateChoicesMenu( subsubmenu, CURVAL.psz_string, p_object, false );
1434                 subsubmenu->setTitle( qfue( CURTEXT ? CURTEXT :CURVAL.psz_string ) );
1435                 submenu->addMenu( subsubmenu );
1436                 break;
1437
1438             case VLC_VAR_STRING:
1439                 var_Get( p_object, psz_var, &val );
1440                 another_val.psz_string = strdup( CURVAL.psz_string );
1441                 menutext = qfue( CURTEXT ? CURTEXT : another_val.psz_string );
1442                 CreateAndConnect( submenu, psz_var, menutext, "", RADIO_OR_COMMAND,
1443                         p_object, another_val, i_type,
1444                         val.psz_string && !strcmp( val.psz_string, CURVAL.psz_string ) );
1445
1446                 free( val.psz_string );
1447                 break;
1448
1449             case VLC_VAR_INTEGER:
1450                 var_Get( p_object, psz_var, &val );
1451                 if( CURTEXT ) menutext = qfue( CURTEXT );
1452                 else menutext = QString::number( CURVAL.i_int );
1453                 CreateAndConnect( submenu, psz_var, menutext, "", RADIO_OR_COMMAND,
1454                         p_object, CURVAL, i_type,
1455                         ( CURVAL.i_int == val.i_int )
1456                         && CheckTitle( p_object, psz_var ) );
1457                 break;
1458
1459             case VLC_VAR_FLOAT:
1460                 var_Get( p_object, psz_var, &val );
1461                 if( CURTEXT ) menutext = qfue( CURTEXT );
1462                 else menutext.sprintf( "%.2f", CURVAL.f_float );
1463                 CreateAndConnect( submenu, psz_var, menutext, "", RADIO_OR_COMMAND,
1464                         p_object, CURVAL, i_type,
1465                         CURVAL.f_float == val.f_float );
1466                 break;
1467
1468             default:
1469                 break;
1470         }
1471     }
1472     currentGroup = NULL;
1473
1474     /* clean up everything */
1475     var_FreeList( &val_list, &text_list );
1476
1477 #undef RADIO_OR_COMMAND
1478 #undef CURVAL
1479 #undef CURTEXT
1480
1481     return submenu->isEmpty() ? VLC_EGENERIC : VLC_SUCCESS;
1482 }
1483
1484 void VLCMenuBar::CreateAndConnect( QMenu *menu, const char *psz_var,
1485         const QString& text, const QString& help,
1486         int i_item_type, vlc_object_t *p_obj,
1487         vlc_value_t val, int i_val_type,
1488         bool checked )
1489 {
1490     QAction *action = FindActionWithVar( menu, psz_var );
1491
1492     bool b_new = false;
1493     if( !action )
1494     {
1495         action = new QAction( text, menu );
1496         menu->addAction( action );
1497         b_new = true;
1498     }
1499
1500     action->setToolTip( help );
1501     action->setEnabled( p_obj != NULL );
1502
1503     if( i_item_type == ITEM_CHECK )
1504     {
1505         action->setCheckable( true );
1506     }
1507     else if( i_item_type == ITEM_RADIO )
1508     {
1509         action->setCheckable( true );
1510         if( !currentGroup )
1511             currentGroup = new QActionGroup( menu );
1512         currentGroup->addAction( action );
1513     }
1514
1515     action->setChecked( checked );
1516
1517     MenuItemData *itemData = action->findChild<MenuItemData*>( QString() );
1518     delete itemData;
1519     itemData = new MenuItemData( action, p_obj, i_val_type, val, psz_var );
1520
1521     /* remove previous signal-slot connection(s) if any */
1522     action->disconnect( );
1523
1524     CONNECT( action, triggered(), THEDP->menusMapper, map() );
1525     THEDP->menusMapper->setMapping( action, itemData );
1526
1527     if( b_new )
1528         menu->addAction( action );
1529 }
1530
1531 void VLCMenuBar::DoAction( QObject *data )
1532 {
1533     MenuItemData *itemData = qobject_cast<MenuItemData *>( data );
1534     vlc_object_t *p_object = itemData->p_obj;
1535     if( p_object == NULL ) return;
1536     const char *var = itemData->psz_var;
1537     vlc_value_t val = itemData->val;
1538
1539     /* Preserve settings across vouts via the playlist object: */
1540     if( !strcmp( var, "fullscreen" )
1541      || !strcmp( var, "video-on-top" ) )
1542         var_Set( pl_Get( p_object ), var, val );
1543
1544     if ((var_Type( p_object, var) & VLC_VAR_CLASS) == VLC_VAR_VOID)
1545         var_TriggerCallback( p_object, var );
1546     else
1547         var_Set( p_object, var, val );
1548 }
1549
1550 void VLCMenuBar::updateAudioDevice( intf_thread_t * p_intf, audio_output_t *p_aout, QMenu *current )
1551 {
1552     char **ids, **names;
1553     char *selected;
1554
1555     if( !p_aout || !current )
1556         return;
1557
1558     current->clear();
1559     int i_result = aout_DevicesList( p_aout, &ids, &names);
1560     selected = aout_DeviceGet( p_aout );
1561
1562     QActionGroup *actionGroup = new QActionGroup(current);
1563     QAction *action;
1564
1565     for( int i = 0; i < i_result; i++ )
1566     {
1567         action = new QAction( qfue( names[i] ), NULL );
1568         action->setData( ids[i] );
1569         action->setCheckable( true );
1570         if( (selected && !strcmp( ids[i], selected ) ) ||
1571             (selected == NULL && ids[i] && ids[i][0] == '\0' ) )
1572             action->setChecked( true );
1573         actionGroup->addAction( action );
1574         current->addAction( action );
1575         CONNECT(action, changed(), THEMIM->menusAudioMapper, map());
1576         THEMIM->menusAudioMapper->setMapping(action, ids[i]);
1577         free( ids[i] );
1578         free( names[i] );
1579     }
1580     free( ids );
1581     free( names );
1582     free( selected );
1583 }
1584
1585 void VLCMenuBar::updateRecents( intf_thread_t *p_intf )
1586 {
1587     if( recentsMenu )
1588     {
1589         QAction* action;
1590         RecentsMRL* rmrl = RecentsMRL::getInstance( p_intf );
1591         QStringList l = rmrl->recents();
1592
1593         recentsMenu->clear();
1594
1595         if( !l.count() )
1596         {
1597             recentsMenu->setEnabled( false );
1598         }
1599         else
1600         {
1601             for( int i = 0; i < l.count(); ++i )
1602             {
1603                 QString mrl = l.at( i );
1604                 char *psz = decode_URI_duplicate( qtu( mrl ) );
1605                 QString text = qfu( psz );
1606
1607                 text.replace("&", "&&");
1608 #ifdef _WIN32
1609 # define FILE_SCHEME "file:///"
1610 #else
1611 # define FILE_SCHEME "file://"
1612 #endif
1613                 if ( text.startsWith( FILE_SCHEME ) )
1614                     text.remove( 0, strlen( FILE_SCHEME ) );
1615 #undef FILE_SCHEME
1616
1617                 free( psz );
1618                 action = recentsMenu->addAction(
1619                         QString( i < 9 ? "&%1: ": "%1: " ).arg( i + 1 ) +
1620                             QApplication::fontMetrics().elidedText( text,
1621                                                           Qt::ElideLeft, 400 ),
1622                         rmrl->signalMapper, SLOT( map() ),
1623                         i < 9 ? QString( "Ctrl+%1" ).arg( i + 1 ) : "" );
1624                 rmrl->signalMapper->setMapping( action, l.at( i ) );
1625             }
1626
1627             recentsMenu->addSeparator();
1628             recentsMenu->addAction( qtr("&Clear"), rmrl, SLOT( clear() ) );
1629             recentsMenu->setEnabled( true );
1630         }
1631     }
1632 }