]> git.sesse.net Git - vlc/blob - modules/gui/qt4/menus.cpp
Clean popup menu
[vlc] / modules / gui / qt4 / menus.cpp
1 /*****************************************************************************
2  * menus.cpp : Qt menus
3  *****************************************************************************
4  * Copyright ( C ) 2006-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *          Jean-Baptiste Kempf <jb@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * ( at your option ) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /** \todo
26  * - Remove static currentGroup
27  */
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34
35 #include <vlc_intf_strings.h>
36
37 #include "main_interface.hpp"
38 #include "menus.hpp"
39 #include "dialogs_provider.hpp"
40 #include "input_manager.hpp"
41
42 #include <QMenu>
43 #include <QMenuBar>
44 #include <QAction>
45 #include <QActionGroup>
46 #include <QSignalMapper>
47 #include <QSystemTrayIcon>
48
49 enum
50 {
51     ITEM_NORMAL,
52     ITEM_CHECK,
53     ITEM_RADIO
54 };
55
56 static QActionGroup *currentGroup;
57
58 // Add static entries to menus
59 void addDPStaticEntry( QMenu *menu,
60                        const QString text,
61                        const char *help,
62                        const char *icon,
63                        const char *member,
64                        const char *shortcut )
65 {
66     QAction *action = NULL;
67     if( !EMPTY_STR( icon ) > 0 )
68     {
69         if( !EMPTY_STR( shortcut ) > 0 )
70             action = menu->addAction( QIcon( icon ), text, THEDP,
71                                       member, qtr( shortcut ) );
72         else
73             action = menu->addAction( QIcon( icon ), text, THEDP, member );
74     }
75     else
76     {
77         if( !EMPTY_STR( shortcut ) > 0 )
78             action = menu->addAction( text, THEDP, member, qtr( shortcut ) );
79         else
80             action = menu->addAction( text, THEDP, member );
81     }
82     action->setData( "_static_" );
83 }
84
85 void addMIMStaticEntry( intf_thread_t *p_intf,
86                         QMenu *menu,
87                         const QString text,
88                         const char *help,
89                         const char *icon,
90                         const char *member )
91 {
92     if( strlen( icon ) > 0 )
93     {
94         QAction *action = menu->addAction( text, THEMIM,  member );
95         action->setIcon( QIcon( icon ) );
96     }
97     else
98     {
99         menu->addAction( text, THEMIM, member );
100     }
101 }
102
103 void EnableDPStaticEntries( QMenu *menu, bool enable = true )
104 {
105     if( !menu )
106         return;
107
108     QAction *action;
109     foreach( action, menu->actions() )
110     {
111         if( action->data().toString() == "_static_" )
112             action->setEnabled( enable );
113     }
114 }
115
116 /**
117  * \return Number of static entries
118  */
119 int DeleteNonStaticEntries( QMenu *menu )
120 {
121     int i_ret = 0;
122     QAction *action;
123     if( !menu )
124         return VLC_EGENERIC;
125     foreach( action, menu->actions() )
126     {
127         if( action->data().toString() != "_static_" )
128             delete action;
129         else
130             i_ret++;
131     }
132 }
133
134 /*****************************************************************************
135  * Definitions of variables for the dynamic menus
136  *****************************************************************************/
137 #define PUSH_VAR( var ) varnames.push_back( var ); \
138     objects.push_back( p_object ? p_object->i_object_id : 0 )
139
140 #define PUSH_INPUTVAR( var ) varnames.push_back( var ); \
141     objects.push_back( p_input ? p_input->i_object_id : 0 );
142
143 #define PUSH_SEPARATOR if( objects.size() != i_last_separator ) { \
144     objects.push_back( 0 ); varnames.push_back( "" ); \
145     i_last_separator = objects.size(); }
146
147 static int InputAutoMenuBuilder( vlc_object_t *p_object,
148         vector<int> &objects,
149         vector<const char *> &varnames )
150 {
151     PUSH_VAR( "bookmark" );
152     PUSH_VAR( "title" );
153     PUSH_VAR( "chapter" );
154     PUSH_VAR( "program" );
155     PUSH_VAR( "navigation" );
156     PUSH_VAR( "dvd_menus" );
157     return VLC_SUCCESS;
158 }
159
160 static int VideoAutoMenuBuilder( vlc_object_t *p_object,
161         input_thread_t *p_input,
162         vector<int> &objects,
163         vector<const char *> &varnames )
164 {
165     PUSH_INPUTVAR( "video-es" );
166     PUSH_INPUTVAR( "spu-es" );
167     PUSH_VAR( "fullscreen" );
168     PUSH_VAR( "zoom" );
169     PUSH_VAR( "deinterlace" );
170     PUSH_VAR( "aspect-ratio" );
171     PUSH_VAR( "crop" );
172     PUSH_VAR( "video-on-top" );
173     PUSH_VAR( "directx-wallpaper" );
174     PUSH_VAR( "video-snapshot" );
175
176     if( p_object )
177     {
178         vlc_object_t *p_dec_obj = ( vlc_object_t * )vlc_object_find( p_object,
179                 VLC_OBJECT_DECODER,
180                 FIND_PARENT );
181         if( p_dec_obj )
182         {
183             vlc_object_t *p_object = p_dec_obj;
184             PUSH_VAR( "ffmpeg-pp-q" );
185             vlc_object_release( p_dec_obj );
186         }
187     }
188     return VLC_SUCCESS;
189 }
190
191 static int AudioAutoMenuBuilder( vlc_object_t *p_object,
192         input_thread_t *p_input,
193         vector<int> &objects,
194         vector<const char *> &varnames )
195 {
196     PUSH_INPUTVAR( "audio-es" );
197     PUSH_VAR( "audio-device" );
198     PUSH_VAR( "audio-channels" );
199     PUSH_VAR( "visual" );
200     PUSH_VAR( "equalizer" );
201     return VLC_SUCCESS;
202 }
203
204 static QAction * FindActionWithVar( QMenu *menu, const char *psz_var )
205 {
206     QAction *action;
207     foreach( action, menu->actions() )
208     {
209         if( action->data().toString() == psz_var )
210             return action;
211     }
212     return NULL;
213 }
214
215 static QAction * FindActionWithText( QMenu *menu, QString &text )
216 {
217     QAction *action;
218     foreach( action, menu->actions() )
219     {
220         if( action->text() == text )
221             return action;
222     }
223     return NULL;
224 }
225
226 /*****************************************************************************
227  * All normal menus
228  * Simple Code
229  *****************************************************************************/
230
231 #define BAR_ADD( func, title ) { \
232     QMenu *_menu = func; _menu->setTitle( title ); bar->addMenu( _menu ); }
233
234 #define BAR_DADD( func, title, id ) { \
235     QMenu *_menu = func; _menu->setTitle( title ); bar->addMenu( _menu ); \
236     MenuFunc *f = new MenuFunc( _menu, id ); \
237     CONNECT( _menu, aboutToShow(), THEDP->menusUpdateMapper, map() ); \
238     THEDP->menusUpdateMapper->setMapping( _menu, f ); }
239
240 #define ACT_ADD( _menu, val, title ) { \
241     QAction *_action = new QAction( title, _menu ); _action->setData( val ); \
242     _menu->addAction( _action ); }
243
244 /**
245  * Main Menu Bar Creation
246  **/
247 void QVLCMenu::createMenuBar( MainInterface *mi,
248                               intf_thread_t *p_intf,
249                               bool visual_selector_enabled )
250 {
251     /* QMainWindows->menuBar()
252        gives the QProcess::destroyed timeout issue on Cleanlooks style with
253        setDesktopAware set to false */
254     QMenuBar *bar = mi->menuBar();
255     BAR_ADD( FileMenu(), qtr( "&Media" ) );
256     BAR_ADD( PlaylistMenu( p_intf, mi ), qtr( "&Playlist" ) );
257     BAR_ADD( ToolsMenu( p_intf, NULL, mi, visual_selector_enabled, true ),
258              qtr( "&Tools" ) );
259     BAR_DADD( AudioMenu( p_intf, NULL ), qtr( "&Audio" ), 1 );
260     BAR_DADD( VideoMenu( p_intf, NULL ), qtr( "&Video" ), 2 );
261     BAR_DADD( NavigMenu( p_intf, NULL ), qtr( "&Playback" ), 3 );
262     BAR_ADD( HelpMenu( NULL ), qtr( "&Help" ) );
263 }
264 #undef BAR_ADD
265 #undef BAR_DADD
266
267 /**
268  * Media ( File ) Menu
269  * Opening, streaming and quit
270  **/
271 QMenu *QVLCMenu::FileMenu()
272 {
273     QMenu *menu = new QMenu();
274
275     addDPStaticEntry( menu, qtr( "&Open File..." ), "",
276         ":/pixmaps/file-asym_16px.png", SLOT( openFileDialog() ), "Ctrl+O" );
277     addDPStaticEntry( menu, qtr( I_OPEN_FOLDER ), "",
278         ":/pixmaps/folder-grey_16px.png", SLOT( PLAppendDir() ), "Ctrl+F" );
279     addDPStaticEntry( menu, qtr( "Open &Disc..." ), "",
280         ":/pixmaps/disc_16px.png", SLOT( openDiscDialog() ), "Ctrl+D" );
281     addDPStaticEntry( menu, qtr( "Open &Network..." ), "",
282         ":/pixmaps/network_16px.png", SLOT( openNetDialog() ), "Ctrl+N" );
283     addDPStaticEntry( menu, qtr( "Open &Capture Device..." ), "",
284         ":/pixmaps/capture-card_16px.png", SLOT( openCaptureDialog() ),
285         "Ctrl+C" );
286     menu->addSeparator();
287
288     addDPStaticEntry( menu, qtr( "&Streaming..." ), "",
289         ":/pixmaps/menus_stream_16px.png", SLOT( openThenStreamingDialogs() ),
290         "Ctrl+S" );
291     addDPStaticEntry( menu, qtr( "Conve&rt / Save..." ), "", "",
292         SLOT( openThenTranscodingDialogs() ), "Ctrl+R" );
293     menu->addSeparator();
294
295     addDPStaticEntry( menu, qtr( "&Quit" ) , "",
296         ":/pixmaps/menus_quit_16px.png", SLOT( quit() ), "Ctrl+Q" );
297     return menu;
298 }
299
300 /* Playlist/MediaLibrary Control */
301 QMenu *QVLCMenu::PlaylistMenu( intf_thread_t *p_intf, MainInterface *mi )
302 {
303     QMenu *menu = new QMenu();
304     menu->addMenu( SDMenu( p_intf ) );
305     menu->addAction( QIcon( ":/pixmaps/playlist_16px.png" ),
306                      qtr( "Show Playlist" ), mi, SLOT( togglePlaylist() ) );
307     menu->addSeparator();
308
309     addDPStaticEntry( menu, qtr( I_PL_LOAD ), "", "", SLOT( openAPlaylist() ),
310         "Ctrl+X" );
311     addDPStaticEntry( menu, qtr( I_PL_SAVE ), "", "", SLOT( saveAPlaylist() ),
312         "Ctrl+Y" );
313     menu->addSeparator();
314     menu->addAction( qtr( "Undock from interface" ), mi,
315                      SLOT( undockPlaylist() ), qtr( "Ctrl+U" ) );
316     return menu;
317 }
318
319 /**
320  * Tools/View Menu
321  * This is kept in the same menu for now, but could change if it gets much
322  * longer.
323  * This menu can be an interface menu but also a right click menu.
324  **/
325 QMenu *QVLCMenu::ToolsMenu( intf_thread_t *p_intf,
326                             QMenu *current,
327                             MainInterface *mi,
328                             bool visual_selector_enabled,
329                             bool with_intf )
330 {
331     QMenu *menu = new QMenu( current );
332     if( mi )
333     {
334         menu->addAction( QIcon( ":/pixmaps/playlist_16px.png" ),
335                          qtr( "Playlist..." ), mi, SLOT( togglePlaylist() ),
336                          qtr( "Ctrl+L" ) );
337     }
338     addDPStaticEntry( menu, qtr( I_MENU_EXT ), "",
339         ":/pixmaps/menus_settings_16px.png", SLOT( extendedDialog() ),
340         "Ctrl+E" );
341
342     menu->addSeparator();
343
344     if( with_intf )
345     {
346         QMenu *intfmenu = InterfacesMenu( p_intf, menu );
347         intfmenu->setTitle( qtr( "Add Interfaces" ) );
348         MenuFunc *f = new MenuFunc( intfmenu, 4 );
349         CONNECT( intfmenu, aboutToShow(), THEDP->menusUpdateMapper, map() );
350         THEDP->menusUpdateMapper->setMapping( intfmenu, f );
351         menu->addMenu( intfmenu );
352         menu->addSeparator();
353     }
354     if( mi )
355     {
356         /* Minimal View */
357         QAction *action=menu->addAction( qtr( "Minimal View..." ), mi,
358                 SLOT( toggleMinimalView() ), qtr( "Ctrl+H" ) );
359         action->setCheckable( true );
360         if( mi->getControlsVisibilityStatus() & CONTROLS_VISIBLE )
361             action->setChecked( true );
362
363         /* FullScreen View */
364         action = menu->addAction( qtr( "Toggle Fullscreen Interface" ), mi,
365                 SLOT( toggleFullScreen() ), qtr( "F11" ) );
366
367         /* Advanced Controls */
368         action = menu->addAction( qtr( "Advanced controls" ), mi,
369                 SLOT( toggleAdvanced() ) );
370         action->setCheckable( true );
371         if( mi->getControlsVisibilityStatus() & CONTROLS_ADVANCED )
372             action->setChecked( true );
373 #if 0 /* For Visualisations. Not yet working */
374         adv = menu->addAction( qtr( "Visualizations selector" ),
375                 mi, SLOT( visual() ) );
376         adv->setCheckable( true );
377         if( visual_selector_enabled ) adv->setChecked( true );
378 #endif
379     }
380
381     menu->addSeparator();
382
383     addDPStaticEntry( menu, qtr( I_MENU_MSG ), "",
384         ":/pixmaps/menus_messages_16px.png", SLOT( messagesDialog() ),
385         "Ctrl+M" );
386     addDPStaticEntry( menu, qtr( I_MENU_INFO ) , "", "",
387         SLOT( mediaInfoDialog() ), "Ctrl+I" );
388     addDPStaticEntry( menu, qtr( I_MENU_CODECINFO ) , "",
389         ":/pixmaps/menus_info_16px.png", SLOT( mediaCodecDialog() ), "Ctrl+J" );
390     addDPStaticEntry( menu, qtr( I_MENU_BOOKMARK ), "","",
391                       SLOT( bookmarksDialog() ), "Ctrl+B" );
392 #ifdef ENABLE_VLM
393     addDPStaticEntry( menu, qtr( I_MENU_VLM ), "", "", SLOT( vlmDialog() ),
394         "Ctrl+W" );
395 #endif
396
397     menu->addSeparator();
398     addDPStaticEntry( menu, qtr( "Preferences..." ), "",
399         ":/pixmaps/menus_preferences_16px.png", SLOT( prefsDialog() ), "Ctrl+P" );
400     return menu;
401 }
402
403 /**
404  * Interface Sub-Menu, to list extras interface and skins
405  **/
406 QMenu *QVLCMenu::InterfacesMenu( intf_thread_t *p_intf, QMenu *current )
407 {
408     vector<int> objects;
409     vector<const char *> varnames;
410     /** \todo add "switch to XXX" */
411     varnames.push_back( "intf-add" );
412     objects.push_back( p_intf->i_object_id );
413
414     QMenu *submenu = new QMenu( current );
415     QMenu *menu = Populate( p_intf, submenu, varnames, objects );
416
417     return menu;
418 }
419
420 /**
421  * Main Audio Menu
422  */
423 QMenu *QVLCMenu::AudioMenu( intf_thread_t *p_intf, QMenu * current )
424 {
425     vector<int> objects;
426     vector<const char *> varnames;
427     vlc_object_t *p_object;
428     input_thread_t *p_input;
429
430     if( !current )
431         current = new QMenu();
432
433     if( current->isEmpty() )
434     {
435         ACT_ADD( current, "audio-es", qtr( "Audio &Track" ) );
436         ACT_ADD( current, "audio-device", qtr( "Audio &Device" ) );
437         ACT_ADD( current, "audio-channels", qtr( "Audio &Channels" ) );
438         ACT_ADD( current, "visual", qtr( "&Visualizations" ) );
439         ACT_ADD( current, "equalizer", qtr( "&Equalizer" ) );
440     }
441
442     p_input = THEMIM->getInput();
443     if( p_input )
444         vlc_object_yield( p_input );
445     p_object = ( vlc_object_t * ) vlc_object_find( p_intf,
446                                                    VLC_OBJECT_AOUT,
447                                                    FIND_ANYWHERE );
448
449     AudioAutoMenuBuilder( p_object, p_input, objects, varnames );
450
451     if( p_object )
452         vlc_object_release( p_object );
453     if( p_input )
454         vlc_object_release( p_input );
455
456     return Populate( p_intf, current, varnames, objects );
457 }
458
459 /**
460  * Main Video Menu
461  * Subtitles are part of Video.
462  **/
463 QMenu *QVLCMenu::VideoMenu( intf_thread_t *p_intf, QMenu *current )
464 {
465     vlc_object_t *p_object;
466     input_thread_t *p_input;
467     vector<int> objects;
468     vector<const char *> varnames;
469
470     if( !current )
471         current = new QMenu();
472
473     if( current->isEmpty() )
474     {
475         ACT_ADD( current, "video-es", qtr( "Video &Track" ) );
476
477         QAction *action;
478         QMenu *submenu = new QMenu( qtr( "&Subtitles Track" ), current );
479         action = current->addMenu( submenu );
480         action->setData( "spu-es" );
481         action =  submenu->addAction( qfu( "Load File..." ), THEDP,
482                                       SLOT( loadSubtitlesFile() ) );
483         action->setData( "_static_" );
484
485         ACT_ADD( current, "fullscreen", qtr( "&Fullscreen" ) );
486         ACT_ADD( current, "zoom", qtr( "&Zoom" ) );
487         ACT_ADD( current, "deinterlace", qtr( "&Deinterlace" ) );
488         ACT_ADD( current, "aspect-ratio", qtr( "&Aspect Ratio" ) );
489         ACT_ADD( current, "crop", qtr( "&Crop" ) );
490         ACT_ADD( current, "video-on-top", qtr( "Always &On Top" ) );
491         /* ACT_ADD( current, "directx-wallpaper", qtr( "DirectX Wallpaper" ) ); */
492         ACT_ADD( current, "video-snapshot", qtr( "Snapshot" ) );
493         /* ACT_ADD( current, "ffmpeg-pp-q", qtr( "Decoder" ) ); */
494     }
495
496     p_input = THEMIM->getInput();
497     if( p_input )
498         vlc_object_yield( p_input );
499     p_object = ( vlc_object_t * )vlc_object_find( p_intf, VLC_OBJECT_VOUT,
500             FIND_ANYWHERE );
501
502     VideoAutoMenuBuilder( p_object, p_input, objects, varnames );
503
504     if( p_object )
505         vlc_object_release( p_object );
506     if( p_input )
507         vlc_object_release( p_input );
508
509     return Populate( p_intf, current, varnames, objects );
510 }
511
512 /**
513  * Navigation Menu
514  * For DVD, MP4, MOV and other chapter based format
515  **/
516 QMenu *QVLCMenu::NavigMenu( intf_thread_t *p_intf, QMenu *menu )
517 {
518     vlc_object_t *p_object;
519     vector<int> objects;
520     vector<const char *> varnames;
521
522     if( !menu )
523     {
524         menu = new QMenu();
525     }
526
527     if( menu->isEmpty() )
528     {
529         addDPStaticEntry( menu, qtr( I_MENU_GOTOTIME ), "","",
530                           SLOT( gotoTimeDialog() ), "Ctrl+T" );
531         menu->addSeparator();
532
533         ACT_ADD( menu, "bookmark", qtr( "&Bookmarks" ) );
534         ACT_ADD( menu, "title", qtr( "&Title" ) );
535         ACT_ADD( menu, "chapter", qtr( "&Chapter" ) );
536         ACT_ADD( menu, "program", qtr( "&Program" ) );
537         ACT_ADD( menu, "navigation", qtr( "&Navigation" ) );
538     }
539
540     p_object = ( vlc_object_t * )vlc_object_find( p_intf, VLC_OBJECT_INPUT,
541             FIND_ANYWHERE );
542     InputAutoMenuBuilder(  p_object, objects, varnames );
543     PUSH_VAR( "prev-title" );
544     PUSH_VAR( "next-title" );
545     PUSH_VAR( "prev-chapter" );
546     PUSH_VAR( "next-chapter" );
547     EnableDPStaticEntries( menu, ( p_object != NULL ) );
548     if( p_object )
549     {
550         vlc_object_release( p_object );
551     }
552     return Populate( p_intf, menu, varnames, objects, true );
553 }
554
555 /**
556  * Service Discovery SubMenu
557  **/
558 QMenu *QVLCMenu::SDMenu( intf_thread_t *p_intf )
559 {
560     QMenu *menu = new QMenu();
561     menu->setTitle( qtr( I_PL_SD ) );
562     char **ppsz_longnames;
563     char **ppsz_names = services_discovery_GetServicesNames( p_intf,
564                                                              &ppsz_longnames );
565     if( !ppsz_names )
566         return menu;
567
568     char **ppsz_name = ppsz_names, **ppsz_longname = ppsz_longnames;
569     for( ; *ppsz_name; ppsz_name++, ppsz_longname++ )
570     {
571         QAction *a = new QAction( qfu( *ppsz_longname ), menu );
572         a->setCheckable( true );
573         if( playlist_IsServicesDiscoveryLoaded( THEPL, *ppsz_name ) )
574             a->setChecked( true );
575         CONNECT( a , triggered(), THEDP->SDMapper, map() );
576         THEDP->SDMapper->setMapping( a, QString( *ppsz_name ) );
577         menu->addAction( a );
578
579         if( !strcmp( *ppsz_name, "podcast" ) )
580         {
581             QAction *b = new QAction( qfu( "Configure podcasts..." ), menu );
582             //b->setEnabled( a->isChecked() );
583             menu->addAction( b );
584             CONNECT( b, triggered(), THEDP, podcastConfigureDialog() );
585         }
586         free( *ppsz_name );
587         free( *ppsz_longname );
588     }
589     free( ppsz_names );
590     free( ppsz_longnames );
591     return menu;
592 }
593 /**
594  * Help/About Menu
595 **/
596 QMenu *QVLCMenu::HelpMenu( QMenu *current )
597 {
598     QMenu *menu = new QMenu( current );
599     addDPStaticEntry( menu, qtr( "Help..." ) , "",
600         ":/pixmaps/menus_help_16px.png", SLOT( helpDialog() ), "F1" );
601 #ifdef UPDATE_CHECK
602     addDPStaticEntry( menu, qtr( "Check for updates..." ) , "", "", SLOT( updateDialog() ), "");
603 #endif
604     menu->addSeparator();
605     addDPStaticEntry( menu, qtr( I_MENU_ABOUT ), "", "", SLOT( aboutDialog() ),
606         "Ctrl+F1" );
607     return menu;
608 }
609
610 #undef ACT_ADD
611
612 /*****************************************************************************
613  * Popup menus - Right Click menus                                           *
614  *****************************************************************************/
615 #define POPUP_BOILERPLATE \
616     unsigned int i_last_separator = 0; \
617     vector<int> objects; \
618     vector<const char *> varnames; \
619     input_thread_t *p_input = THEMIM->getInput();
620
621 #define CREATE_POPUP \
622     Populate( p_intf, menu, varnames, objects ); \
623     p_intf->p_sys->p_popup_menu = menu; \
624     menu->popup( QCursor::pos() ); \
625     p_intf->p_sys->p_popup_menu = NULL; \
626     i_last_separator = 0;
627
628 void QVLCMenu::PopupMenuControlEntries( QMenu *menu,
629                                         intf_thread_t *p_intf,
630                                         input_thread_t *p_input )
631 {
632     if( p_input )
633     {
634         vlc_value_t val;
635         var_Get( p_input, "state", &val );
636         if( val.i_int == PLAYING_S )
637             addMIMStaticEntry( p_intf, menu, qtr( "Pause" ), "",
638                     ":/pixmaps/pause_16px.png", SLOT( togglePlayPause() ) );
639         else
640             addMIMStaticEntry( p_intf, menu, qtr( "Play" ), "",
641                     ":/pixmaps/play_16px.png", SLOT( togglePlayPause() ) );
642     }
643     else if( THEPL->items.i_size )
644         addMIMStaticEntry( p_intf, menu, qtr( "Play" ), "",
645                 ":/pixmaps/play_16px.png", SLOT( togglePlayPause() ) );
646
647     addMIMStaticEntry( p_intf, menu, qtr( "Stop" ), "",
648             ":/pixmaps/stop_16px.png", SLOT( stop() ) );
649     addMIMStaticEntry( p_intf, menu, qtr( "Previous" ), "",
650             ":/pixmaps/previous_16px.png", SLOT( prev() ) );
651     addMIMStaticEntry( p_intf, menu, qtr( "Next" ), "",
652             ":/pixmaps/next_16px.png", SLOT( next() ) );
653     }
654
655 void QVLCMenu::PopupMenuStaticEntries( intf_thread_t *p_intf, QMenu *menu )
656 {
657 #if 0
658     QMenu *toolsmenu = ToolsMenu( p_intf, menu, false, true );
659     toolsmenu->setTitle( qtr( "Tools" ) );
660     menu->addMenu( toolsmenu );
661 #endif
662
663     QMenu *openmenu = new QMenu( qtr( "Open" ), menu );
664     openmenu->addAction( qtr( "Open &File..." ), THEDP,
665                          SLOT( openFileDialog() ) );
666     openmenu->addAction( qtr( "Open &Disc..." ), THEDP,
667                          SLOT( openDiscDialog() ) );
668     openmenu->addAction( qtr( "Open &Network..." ), THEDP,
669                          SLOT( openNetDialog() ) );
670     openmenu->addAction( qtr( "Open &Capture Device..." ), THEDP,
671                          SLOT( openCaptureDialog() ) );
672     menu->addMenu( openmenu );
673
674     menu->addSeparator();
675 #if 0
676     QMenu *helpmenu = HelpMenu( menu );
677     helpmenu->setTitle( qtr( "Help" ) );
678     menu->addMenu( helpmenu );
679 #endif
680
681     addDPStaticEntry( menu, qtr( "Quit" ), "", "", SLOT( quit() ) , "Ctrl+Q" );
682 }
683
684 /* Video Tracks and Subtitles tracks */
685 void QVLCMenu::VideoPopupMenu( intf_thread_t *p_intf )
686 {
687     POPUP_BOILERPLATE;
688     if( p_input )
689     {
690         vlc_object_yield( p_input );
691         vlc_object_t *p_vout = ( vlc_object_t * )vlc_object_find( p_input,
692                 VLC_OBJECT_VOUT, FIND_CHILD );
693         if( p_vout )
694         {
695             VideoAutoMenuBuilder( p_vout, p_input, objects, varnames );
696             vlc_object_release( p_vout );
697         }
698         vlc_object_release( p_input );
699     }
700     QMenu *menu = new QMenu();
701     CREATE_POPUP;
702 }
703
704 /* Audio Tracks */
705 void QVLCMenu::AudioPopupMenu( intf_thread_t *p_intf )
706 {
707     POPUP_BOILERPLATE;
708     if( p_input )
709     {
710         vlc_object_yield( p_input );
711         vlc_object_t *p_aout = ( vlc_object_t * )vlc_object_find( p_input,
712                 VLC_OBJECT_AOUT, FIND_ANYWHERE );
713         AudioAutoMenuBuilder( p_aout, p_input, objects, varnames );
714         if( p_aout )
715             vlc_object_release( p_aout );
716         vlc_object_release( p_input );
717     }
718     QMenu *menu = new QMenu();
719     CREATE_POPUP;
720 }
721
722 /* Navigation stuff, and general menus ( open ) */
723 void QVLCMenu::MiscPopupMenu( intf_thread_t *p_intf )
724 {
725     vlc_value_t val;
726     POPUP_BOILERPLATE;
727
728     if( p_input )
729     {
730         vlc_object_yield( p_input );
731         varnames.push_back( "audio-es" );
732         InputAutoMenuBuilder( VLC_OBJECT( p_input ), objects, varnames );
733         PUSH_SEPARATOR;
734     }
735
736     QMenu *menu = new QMenu();
737     Populate( p_intf, menu, varnames, objects );
738
739     menu->addSeparator();
740     PopupMenuControlEntries( menu, p_intf, p_input );
741
742     menu->addSeparator();
743     PopupMenuStaticEntries( p_intf, menu );
744
745     p_intf->p_sys->p_popup_menu = menu;
746     menu->popup( QCursor::pos() );
747     p_intf->p_sys->p_popup_menu = NULL;
748 }
749
750 /* Main Menu that sticks everything together  */
751 void QVLCMenu::PopupMenu( intf_thread_t *p_intf, bool show )
752 {
753     if( show )
754     {
755         /* Delete and recreate a popup if there is one */
756         if( p_intf->p_sys->p_popup_menu )
757             delete p_intf->p_sys->p_popup_menu;
758
759         QMenu *menu = new QMenu();
760         QMenu *submenu;
761         QAction *action;
762
763         POPUP_BOILERPLATE;
764
765         PopupMenuControlEntries( menu, p_intf, p_input );
766         menu->addSeparator();
767
768         if( p_input )
769         {
770             vlc_object_yield( p_input );
771             InputAutoMenuBuilder( VLC_OBJECT( p_input ), objects, varnames );
772
773             /* Audio menu */
774             vlc_object_t *p_aout = ( vlc_object_t * )
775                 vlc_object_find( p_input, VLC_OBJECT_AOUT, FIND_ANYWHERE );
776             AudioAutoMenuBuilder( p_aout, p_input, objects, varnames );
777             if( p_aout )
778                 vlc_object_release( p_aout );
779             submenu = Populate( p_intf, NULL, varnames, objects );
780             varnames.clear(); objects.clear();
781             action = menu->addMenu( submenu );
782             action->setText( qtr( "Audio" ) );
783             if( submenu->isEmpty() )
784                 action->setEnabled( false );
785
786             /* Video menu */
787             vlc_object_t *p_vout = ( vlc_object_t * )
788                 vlc_object_find( p_input, VLC_OBJECT_VOUT, FIND_CHILD );
789             VideoAutoMenuBuilder( p_vout, p_input, objects, varnames );
790             if( p_vout )
791                 vlc_object_release( p_vout );
792             submenu = Populate( p_intf, NULL, varnames, objects );
793             varnames.clear(); objects.clear();
794             action = menu->addMenu( submenu );
795             action->setText( qtr( "Video" ) );
796             if( submenu->isEmpty() )
797                 action->setEnabled( false );
798
799             vlc_object_release( p_input );
800         }
801
802         menu->addSeparator();
803         PopupMenuStaticEntries( p_intf, menu );
804
805         p_intf->p_sys->p_popup_menu = menu;
806         p_intf->p_sys->p_popup_menu->popup( QCursor::pos() );
807     }
808     else
809     {
810         // destroy popup if there is one
811         delete p_intf->p_sys->p_popup_menu;
812         p_intf->p_sys->p_popup_menu = NULL;
813     }
814 }
815
816 /************************************************************************
817  * Systray Menu                                                         *
818  ************************************************************************/
819
820 void QVLCMenu::updateSystrayMenu( MainInterface *mi,
821                                   intf_thread_t *p_intf,
822                                   bool b_force_visible )
823 {
824     POPUP_BOILERPLATE;
825
826     /* Get the systray menu and clean it */
827     QMenu *sysMenu = mi->getSysTrayMenu();
828     sysMenu->clear();
829
830     /* Hide / Show VLC and cone */
831     if( mi->isVisible() || b_force_visible )
832     {
833         sysMenu->addAction( QIcon( ":/vlc16.png" ),
834                 qtr( "Hide VLC media player in taskbar" ), mi,
835                 SLOT( toggleUpdateSystrayMenu() ) );
836     }
837     else
838     {
839         sysMenu->addAction( QIcon( ":/vlc16.png" ),
840                 qtr( "Show VLC media player" ), mi,
841                 SLOT( toggleUpdateSystrayMenu() ) );
842     }
843
844     sysMenu->addSeparator();
845     PopupMenuControlEntries( sysMenu, p_intf, p_input );
846
847     sysMenu->addSeparator();
848     addDPStaticEntry( sysMenu, qtr( "&Open Media" ), "",
849             ":/pixmaps/file-wide_16px.png", SLOT( openFileDialog() ), "" );
850     addDPStaticEntry( sysMenu, qtr( "&Quit" ) , "",
851         ":/pixmaps/menus_quit_16px.png", SLOT( quit() ), "" );
852
853     /* Set the menu */
854     mi->getSysTray()->setContextMenu( sysMenu );
855 }
856
857 #undef PUSH_VAR
858 #undef PUSH_SEPARATOR
859
860 /*************************************************************************
861  * Builders for automenus
862  *************************************************************************/
863 QMenu * QVLCMenu::Populate( intf_thread_t *p_intf,
864                             QMenu *current,
865                             vector< const char *> & varnames,
866                             vector<int> & objects,
867                             bool append )
868 {
869     QMenu *menu = current;
870     if( !menu )
871         menu = new QMenu();
872
873     /* Disable all non static entries */
874     QAction *p_action;
875     foreach( p_action, menu->actions() )
876     {
877         if( p_action->data().toString() != "_static_" )
878             p_action->setEnabled( false );
879     }
880
881     currentGroup = NULL;
882
883     vlc_object_t *p_object;
884     int i;
885
886     for( i = 0; i < ( int )objects.size() ; i++ )
887     {
888         if( !varnames[i] || !*varnames[i] )
889         {
890             menu->addSeparator();
891             continue;
892         }
893
894         if( objects[i] == 0 )
895         {
896             p_object = NULL;
897         }
898         else
899         {
900             p_object = ( vlc_object_t * )vlc_object_get( objects[i] );
901             if( !p_object )
902             {
903                 msg_Dbg( p_intf, "object %d not found !", objects[i] );
904                 continue;
905             }
906         }
907
908         /* Ugly specific stuff */
909         if( strstr( varnames[i], "intf-add" ) )
910             UpdateItem( p_intf, menu, varnames[i], p_object, false );
911         else
912             UpdateItem( p_intf, menu, varnames[i], p_object, true );
913         if( p_object )
914             vlc_object_release( p_object );
915     }
916     return menu;
917 }
918
919 /*****************************************************************************
920  * Private methods.
921  *****************************************************************************/
922
923 static bool IsMenuEmpty( const char *psz_var,
924                          vlc_object_t *p_object,
925                          bool b_root = true )
926 {
927     vlc_value_t val, val_list;
928     int i_type, i_result, i;
929
930     /* Check the type of the object variable */
931     i_type = var_Type( p_object, psz_var );
932
933     /* Check if we want to display the variable */
934     if( !( i_type & VLC_VAR_HASCHOICE ) ) return false;
935
936     var_Change( p_object, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
937     if( val.i_int == 0 ) return true;
938
939     if( ( i_type & VLC_VAR_TYPE ) != VLC_VAR_VARIABLE )
940     {
941         if( val.i_int == 1 && b_root ) return true;
942         else return false;
943     }
944
945     /* Check children variables in case of VLC_VAR_VARIABLE */
946     if( var_Change( p_object, psz_var, VLC_VAR_GETLIST, &val_list, NULL ) < 0 )
947     {
948         return true;
949     }
950
951     for( i = 0, i_result = true; i < val_list.p_list->i_count; i++ )
952     {
953         if( !IsMenuEmpty( val_list.p_list->p_values[i].psz_string,
954                     p_object, false ) )
955         {
956             i_result = false;
957             break;
958         }
959     }
960
961     /* clean up everything */
962     var_Change( p_object, psz_var, VLC_VAR_FREELIST, &val_list, NULL );
963
964     return i_result;
965 }
966
967 #define TEXT_OR_VAR qfu ( text.psz_string ? text.psz_string : psz_var )
968
969 void QVLCMenu::UpdateItem( intf_thread_t *p_intf, QMenu *menu,
970         const char *psz_var, vlc_object_t *p_object, bool b_submenu )
971 {
972     vlc_value_t val, text;
973     int i_type;
974
975     QAction *action = FindActionWithVar( menu, psz_var );
976     if( action )
977         DeleteNonStaticEntries( action->menu() );
978
979     if( !p_object )
980         return;
981
982     /* Check the type of the object variable */
983     if( !strcmp( psz_var, "audio-es" )
984      || !strcmp( psz_var, "video-es" ) )
985         i_type = VLC_VAR_INTEGER | VLC_VAR_HASCHOICE;
986     else
987         i_type = var_Type( p_object, psz_var );
988
989     switch( i_type & VLC_VAR_TYPE )
990     {
991         case VLC_VAR_VOID:
992         case VLC_VAR_BOOL:
993         case VLC_VAR_VARIABLE:
994         case VLC_VAR_STRING:
995         case VLC_VAR_INTEGER:
996         case VLC_VAR_FLOAT:
997             break;
998         default:
999             /* Variable doesn't exist or isn't handled */
1000             return;
1001     }
1002
1003     /* Make sure we want to display the variable */
1004     if( menu->isEmpty() && IsMenuEmpty( psz_var, p_object ) )
1005         return;
1006
1007     /* Get the descriptive name of the variable */
1008     int i_ret = var_Change( p_object, psz_var, VLC_VAR_GETTEXT, &text, NULL );
1009     if( i_ret != VLC_SUCCESS )
1010     {
1011         text.psz_string = NULL;
1012     }
1013
1014     if( !action )
1015     {
1016         action = new QAction( TEXT_OR_VAR, menu );
1017         menu->addAction( action );
1018         action->setData( psz_var );
1019     }
1020
1021     /* Some specific stuff */
1022     bool forceDisabled = false;
1023     if( !strcmp( psz_var, "spu-es" ) )
1024     {
1025         vlc_object_t *p_vout = ( vlc_object_t* )( vlc_object_find( p_intf,
1026                                     VLC_OBJECT_VOUT, FIND_ANYWHERE ) );
1027         forceDisabled = ( p_vout == NULL );
1028         if( p_vout )
1029             vlc_object_release( p_vout );
1030     }
1031
1032     if( i_type & VLC_VAR_HASCHOICE )
1033     {
1034         /* Append choices menu */
1035         if( b_submenu )
1036         {
1037             QMenu *submenu;
1038             submenu = action->menu();
1039             if( !submenu )
1040             {
1041                 submenu = new QMenu( menu );
1042                 action->setMenu( submenu );
1043             }
1044
1045             action->setEnabled(
1046                CreateChoicesMenu( submenu, psz_var, p_object, true ) == 0 );
1047             if( forceDisabled )
1048                 action->setEnabled( false );
1049         }
1050         else
1051             CreateChoicesMenu( menu, psz_var, p_object, true );
1052         FREENULL( text.psz_string );
1053         return;
1054     }
1055
1056     switch( i_type & VLC_VAR_TYPE )
1057     {
1058         case VLC_VAR_VOID:
1059             var_Get( p_object, psz_var, &val );
1060             CreateAndConnect( menu, psz_var, TEXT_OR_VAR, "", ITEM_NORMAL,
1061                     p_object->i_object_id, val, i_type );
1062             break;
1063
1064         case VLC_VAR_BOOL:
1065             var_Get( p_object, psz_var, &val );
1066             val.b_bool = !val.b_bool;
1067             CreateAndConnect( menu, psz_var, TEXT_OR_VAR, "", ITEM_CHECK,
1068                     p_object->i_object_id, val, i_type, !val.b_bool );
1069             break;
1070     }
1071     FREENULL( text.psz_string );
1072 }
1073
1074
1075 int QVLCMenu::CreateChoicesMenu( QMenu *submenu, const char *psz_var,
1076         vlc_object_t *p_object, bool b_root )
1077 {
1078     vlc_value_t val, val_list, text_list;
1079     int i_type, i;
1080
1081     /* Check the type of the object variable */
1082     i_type = var_Type( p_object, psz_var );
1083
1084     /* Make sure we want to display the variable */
1085     if( submenu->isEmpty() && IsMenuEmpty( psz_var, p_object, b_root ) )
1086         return VLC_EGENERIC;
1087
1088     switch( i_type & VLC_VAR_TYPE )
1089     {
1090         case VLC_VAR_VOID:
1091         case VLC_VAR_BOOL:
1092         case VLC_VAR_VARIABLE:
1093         case VLC_VAR_STRING:
1094         case VLC_VAR_INTEGER:
1095         case VLC_VAR_FLOAT:
1096             break;
1097         default:
1098             /* Variable doesn't exist or isn't handled */
1099             return VLC_EGENERIC;
1100     }
1101
1102     if( var_Change( p_object, psz_var, VLC_VAR_GETLIST,
1103                 &val_list, &text_list ) < 0 )
1104     {
1105         return VLC_EGENERIC;
1106     }
1107
1108 #define NORMAL_OR_RADIO i_type & VLC_VAR_ISCOMMAND ? ITEM_NORMAL: ITEM_RADIO
1109 #define NOTCOMMAND !( i_type & VLC_VAR_ISCOMMAND )
1110 #define CURVAL val_list.p_list->p_values[i]
1111 #define CURTEXT text_list.p_list->p_values[i].psz_string
1112
1113     for( i = 0; i < val_list.p_list->i_count; i++ )
1114     {
1115         vlc_value_t another_val;
1116         QString menutext;
1117         QMenu *subsubmenu = new QMenu( submenu );
1118
1119         switch( i_type & VLC_VAR_TYPE )
1120         {
1121             case VLC_VAR_VARIABLE:
1122                 CreateChoicesMenu( subsubmenu, CURVAL.psz_string, p_object, false );
1123                 subsubmenu->setTitle( qfu( CURTEXT ? CURTEXT :CURVAL.psz_string ) );
1124                 submenu->addMenu( subsubmenu );
1125                 break;
1126
1127             case VLC_VAR_STRING:
1128                 var_Get( p_object, psz_var, &val );
1129                 another_val.psz_string = strdup( CURVAL.psz_string );
1130                 menutext = qfu( CURTEXT ? CURTEXT : another_val.psz_string );
1131                 CreateAndConnect( submenu, psz_var, menutext, "", NORMAL_OR_RADIO,
1132                         p_object->i_object_id, another_val, i_type,
1133                         NOTCOMMAND && val.psz_string &&
1134                         !strcmp( val.psz_string, CURVAL.psz_string ) );
1135
1136                 free( val.psz_string );
1137                 break;
1138
1139             case VLC_VAR_INTEGER:
1140                 var_Get( p_object, psz_var, &val );
1141                 if( CURTEXT ) menutext = qfu( CURTEXT );
1142                 else menutext.sprintf( "%d", CURVAL.i_int );
1143                 CreateAndConnect( submenu, psz_var, menutext, "", NORMAL_OR_RADIO,
1144                         p_object->i_object_id, CURVAL, i_type,
1145                         NOTCOMMAND && CURVAL.i_int == val.i_int );
1146                 break;
1147
1148             case VLC_VAR_FLOAT:
1149                 var_Get( p_object, psz_var, &val );
1150                 if( CURTEXT ) menutext = qfu( CURTEXT );
1151                 else menutext.sprintf( "%.2f", CURVAL.f_float );
1152                 CreateAndConnect( submenu, psz_var, menutext, "", NORMAL_OR_RADIO,
1153                         p_object->i_object_id, CURVAL, i_type,
1154                         NOTCOMMAND && CURVAL.f_float == val.f_float );
1155                 break;
1156
1157             default:
1158                 break;
1159         }
1160     }
1161     currentGroup = NULL;
1162
1163     /* clean up everything */
1164     var_Change( p_object, psz_var, VLC_VAR_FREELIST, &val_list, &text_list );
1165
1166 #undef NORMAL_OR_RADIO
1167 #undef NOTCOMMAND
1168 #undef CURVAL
1169 #undef CURTEXT
1170     return VLC_SUCCESS;
1171 }
1172
1173 void QVLCMenu::CreateAndConnect( QMenu *menu, const char *psz_var,
1174         QString text, QString help,
1175         int i_item_type, int i_object_id,
1176         vlc_value_t val, int i_val_type,
1177         bool checked )
1178 {
1179     QAction *action = FindActionWithVar( menu, psz_var );
1180     if( !action )
1181     {
1182         /* This is a value */
1183         action = FindActionWithText( menu, text );
1184         if( !action )
1185         {
1186             action = new QAction( text, menu );
1187             menu->addAction( action );
1188         }
1189     }
1190
1191     action->setText( text );
1192     action->setToolTip( help );
1193
1194     action->setEnabled( i_object_id != 0 );
1195
1196     if( i_item_type == ITEM_CHECK )
1197     {
1198         action->setCheckable( true );
1199     }
1200     else if( i_item_type == ITEM_RADIO )
1201     {
1202         action->setCheckable( true );
1203         if( !currentGroup )
1204             currentGroup = new QActionGroup( menu );
1205         currentGroup->addAction( action );
1206     }
1207
1208     action->setChecked( checked );
1209
1210     MenuItemData *itemData = new MenuItemData( i_object_id, i_val_type,
1211             val, psz_var );
1212     CONNECT( action, triggered(), THEDP->menusMapper, map() );
1213     THEDP->menusMapper->setMapping( action, itemData );
1214     menu->addAction( action );
1215 }
1216
1217 void QVLCMenu::DoAction( intf_thread_t *p_intf, QObject *data )
1218 {
1219     MenuItemData *itemData = qobject_cast<MenuItemData *>( data );
1220     vlc_object_t *p_object = ( vlc_object_t * )vlc_object_get( itemData->i_object_id );
1221     if( p_object == NULL ) return;
1222
1223     var_Set( p_object, itemData->psz_var, itemData->val );
1224     vlc_object_release( p_object );
1225 }
1226