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