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