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