]> git.sesse.net Git - vlc/blob - modules/gui/qt4/menus.cpp
34bd9571daf41b7a7ab9c25491580c118266d906
[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     QMenu *toolsmenu = ToolsMenu( p_intf, menu, false, true );
658     toolsmenu->setTitle( qtr( "Tools" ) );
659     menu->addMenu( toolsmenu );
660
661     QMenu *openmenu = new QMenu( qtr( "Open" ), menu );
662     openmenu->addAction( qtr( "Open &File..." ), THEDP,
663                          SLOT( openFileDialog() ) );
664     openmenu->addAction( qtr( "Open &Disc..." ), THEDP,
665                          SLOT( openDiscDialog() ) );
666     openmenu->addAction( qtr( "Open &Network..." ), THEDP,
667                          SLOT( openNetDialog() ) );
668     openmenu->addAction( qtr( "Open &Capture Device..." ), THEDP,
669                          SLOT( openCaptureDialog() ) );
670     menu->addMenu( openmenu );
671
672     menu->addSeparator();
673     QMenu *helpmenu = HelpMenu( menu );
674     helpmenu->setTitle( qtr( "Help" ) );
675     menu->addMenu( helpmenu );
676
677     addDPStaticEntry( menu, qtr( "Quit" ), "", "", SLOT( quit() ) , "Ctrl+Q" );
678 }
679
680 /* Video Tracks and Subtitles tracks */
681 void QVLCMenu::VideoPopupMenu( intf_thread_t *p_intf )
682 {
683     POPUP_BOILERPLATE;
684     if( p_input )
685     {
686         vlc_object_yield( p_input );
687         vlc_object_t *p_vout = ( vlc_object_t * )vlc_object_find( p_input,
688                 VLC_OBJECT_VOUT, FIND_CHILD );
689         if( p_vout )
690         {
691             VideoAutoMenuBuilder( p_vout, p_input, objects, varnames );
692             vlc_object_release( p_vout );
693         }
694         vlc_object_release( p_input );
695     }
696     QMenu *menu = new QMenu();
697     CREATE_POPUP;
698 }
699
700 /* Audio Tracks */
701 void QVLCMenu::AudioPopupMenu( intf_thread_t *p_intf )
702 {
703     POPUP_BOILERPLATE;
704     if( p_input )
705     {
706         vlc_object_yield( p_input );
707         vlc_object_t *p_aout = ( vlc_object_t * )vlc_object_find( p_input,
708                 VLC_OBJECT_AOUT, FIND_ANYWHERE );
709         AudioAutoMenuBuilder( p_aout, p_input, objects, varnames );
710         if( p_aout )
711             vlc_object_release( p_aout );
712         vlc_object_release( p_input );
713     }
714     QMenu *menu = new QMenu();
715     CREATE_POPUP;
716 }
717
718 /* Navigation stuff, and general menus ( open ) */
719 void QVLCMenu::MiscPopupMenu( intf_thread_t *p_intf )
720 {
721     vlc_value_t val;
722     POPUP_BOILERPLATE;
723
724     if( p_input )
725     {
726         vlc_object_yield( p_input );
727         varnames.push_back( "audio-es" );
728         InputAutoMenuBuilder( VLC_OBJECT( p_input ), objects, varnames );
729         PUSH_SEPARATOR;
730     }
731
732     QMenu *menu = new QMenu();
733     Populate( p_intf, menu, varnames, objects );
734
735     menu->addSeparator();
736     PopupMenuControlEntries( menu, p_intf, p_input );
737
738     menu->addSeparator();
739     PopupMenuStaticEntries( p_intf, menu );
740
741     p_intf->p_sys->p_popup_menu = menu;
742     menu->popup( QCursor::pos() );
743     p_intf->p_sys->p_popup_menu = NULL;
744 }
745
746 /* Main Menu that sticks everything together  */
747 void QVLCMenu::PopupMenu( intf_thread_t *p_intf, bool show )
748 {
749     if( show )
750     {
751         // create a  popup if there is none
752         if( ! p_intf->p_sys->p_popup_menu )
753         {
754             POPUP_BOILERPLATE;
755             if( p_input )
756             {
757                 vlc_object_yield( p_input );
758                 InputAutoMenuBuilder( VLC_OBJECT( p_input ), objects, varnames );
759
760                 /* Audio menu */
761                 PUSH_SEPARATOR;
762                 vlc_object_t *p_aout = ( vlc_object_t * )
763                     vlc_object_find( p_input, VLC_OBJECT_AOUT, FIND_ANYWHERE );
764                 AudioAutoMenuBuilder( p_aout, p_input, objects, varnames );
765                 if( p_aout )
766                     vlc_object_release( p_aout );
767
768                 /* Video menu */
769                 PUSH_SEPARATOR;
770                 vlc_object_t *p_vout = ( vlc_object_t * )
771                     vlc_object_find( p_input, VLC_OBJECT_VOUT, FIND_CHILD );
772                 VideoAutoMenuBuilder( p_vout, p_input, objects, varnames );
773                 if( p_vout )
774                     vlc_object_release( p_vout );
775
776                 vlc_object_release( p_input );
777             }
778
779             QMenu *menu = new QMenu();
780             Populate( p_intf, menu, varnames, objects );
781             menu->addSeparator();
782             PopupMenuControlEntries( menu, p_intf, p_input );
783             menu->addSeparator();
784             PopupMenuStaticEntries( p_intf, menu );
785
786             p_intf->p_sys->p_popup_menu = menu;
787         }
788         p_intf->p_sys->p_popup_menu->popup( QCursor::pos() );
789     }
790     else
791     {
792         // destroy popup if there is one
793         delete p_intf->p_sys->p_popup_menu;
794         p_intf->p_sys->p_popup_menu = NULL;
795     }
796 }
797
798 /************************************************************************
799  * Systray Menu                                                         *
800  ************************************************************************/
801
802 void QVLCMenu::updateSystrayMenu( MainInterface *mi,
803                                   intf_thread_t *p_intf,
804                                   bool b_force_visible )
805 {
806     POPUP_BOILERPLATE;
807
808     /* Get the systray menu and clean it */
809     QMenu *sysMenu = mi->getSysTrayMenu();
810     sysMenu->clear();
811
812     /* Hide / Show VLC and cone */
813     if( mi->isVisible() || b_force_visible )
814     {
815         sysMenu->addAction( QIcon( ":/vlc16.png" ),
816                 qtr( "Hide VLC media player in taskbar" ), mi,
817                 SLOT( toggleUpdateSystrayMenu() ) );
818     }
819     else
820     {
821         sysMenu->addAction( QIcon( ":/vlc16.png" ),
822                 qtr( "Show VLC media player" ), mi,
823                 SLOT( toggleUpdateSystrayMenu() ) );
824     }
825
826     sysMenu->addSeparator();
827     PopupMenuControlEntries( sysMenu, p_intf, p_input );
828
829     sysMenu->addSeparator();
830     addDPStaticEntry( sysMenu, qtr( "&Open Media" ), "",
831             ":/pixmaps/file-wide_16px.png", SLOT( openFileDialog() ), "" );
832     addDPStaticEntry( sysMenu, qtr( "&Quit" ) , "",
833         ":/pixmaps/menus_quit_16px.png", SLOT( quit() ), "" );
834
835     /* Set the menu */
836     mi->getSysTray()->setContextMenu( sysMenu );
837 }
838
839 #undef PUSH_VAR
840 #undef PUSH_SEPARATOR
841
842 /*************************************************************************
843  * Builders for automenus
844  *************************************************************************/
845 QMenu * QVLCMenu::Populate( intf_thread_t *p_intf,
846                             QMenu *current,
847                             vector< const char *> & varnames,
848                             vector<int> & objects,
849                             bool append )
850 {
851     QMenu *menu = current;
852     if( !menu )
853         menu = new QMenu();
854
855     /* Disable all non static entries */
856     QAction *p_action;
857     foreach( p_action, menu->actions() )
858     {
859         if( p_action->data().toString() != "_static_" )
860             p_action->setEnabled( false );
861     }
862
863     currentGroup = NULL;
864
865     vlc_object_t *p_object;
866     int i;
867
868     for( i = 0; i < ( int )objects.size() ; i++ )
869     {
870         if( !varnames[i] || !*varnames[i] )
871         {
872             menu->addSeparator();
873             continue;
874         }
875
876         if( objects[i] == 0 )
877         {
878             p_object = NULL;
879         }
880         else
881         {
882             p_object = ( vlc_object_t * )vlc_object_get( objects[i] );
883             if( !p_object )
884             {
885                 msg_Dbg( p_intf, "object %d not found !", objects[i] );
886                 continue;
887             }
888         }
889
890         /* Ugly specific stuff */
891         if( strstr( varnames[i], "intf-add" ) )
892             UpdateItem( p_intf, menu, varnames[i], p_object, false );
893         else
894             UpdateItem( p_intf, menu, varnames[i], p_object, true );
895         if( p_object )
896             vlc_object_release( p_object );
897     }
898     return menu;
899 }
900
901 /*****************************************************************************
902  * Private methods.
903  *****************************************************************************/
904
905 static bool IsMenuEmpty( const char *psz_var,
906                          vlc_object_t *p_object,
907                          bool b_root = true )
908 {
909     vlc_value_t val, val_list;
910     int i_type, i_result, i;
911
912     /* Check the type of the object variable */
913     i_type = var_Type( p_object, psz_var );
914
915     /* Check if we want to display the variable */
916     if( !( i_type & VLC_VAR_HASCHOICE ) ) return false;
917
918     var_Change( p_object, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
919     if( val.i_int == 0 ) return true;
920
921     if( ( i_type & VLC_VAR_TYPE ) != VLC_VAR_VARIABLE )
922     {
923         if( val.i_int == 1 && b_root ) return true;
924         else return false;
925     }
926
927     /* Check children variables in case of VLC_VAR_VARIABLE */
928     if( var_Change( p_object, psz_var, VLC_VAR_GETLIST, &val_list, NULL ) < 0 )
929     {
930         return true;
931     }
932
933     for( i = 0, i_result = true; i < val_list.p_list->i_count; i++ )
934     {
935         if( !IsMenuEmpty( val_list.p_list->p_values[i].psz_string,
936                     p_object, false ) )
937         {
938             i_result = false;
939             break;
940         }
941     }
942
943     /* clean up everything */
944     var_Change( p_object, psz_var, VLC_VAR_FREELIST, &val_list, NULL );
945
946     return i_result;
947 }
948
949 #define TEXT_OR_VAR qfu ( text.psz_string ? text.psz_string : psz_var )
950
951 void QVLCMenu::UpdateItem( intf_thread_t *p_intf, QMenu *menu,
952         const char *psz_var, vlc_object_t *p_object, bool b_submenu )
953 {
954     vlc_value_t val, text;
955     int i_type;
956
957     QAction *action = FindActionWithVar( menu, psz_var );
958     if( action )
959         DeleteNonStaticEntries( action->menu() );
960
961     if( !p_object )
962         return;
963
964     /* Check the type of the object variable */
965     if( !strcmp( psz_var, "audio-es" )
966      || !strcmp( psz_var, "video-es" ) )
967         i_type = VLC_VAR_INTEGER | VLC_VAR_HASCHOICE;
968     else
969         i_type = var_Type( p_object, psz_var );
970
971     switch( i_type & VLC_VAR_TYPE )
972     {
973         case VLC_VAR_VOID:
974         case VLC_VAR_BOOL:
975         case VLC_VAR_VARIABLE:
976         case VLC_VAR_STRING:
977         case VLC_VAR_INTEGER:
978         case VLC_VAR_FLOAT:
979             break;
980         default:
981             /* Variable doesn't exist or isn't handled */
982             return;
983     }
984
985     /* Make sure we want to display the variable */
986     if( menu->isEmpty() && IsMenuEmpty( psz_var, p_object ) )
987         return;
988
989     /* Get the descriptive name of the variable */
990     int i_ret = var_Change( p_object, psz_var, VLC_VAR_GETTEXT, &text, NULL );
991     if( i_ret != VLC_SUCCESS )
992     {
993         text.psz_string = NULL;
994     }
995
996     if( !action )
997     {
998         action = new QAction( TEXT_OR_VAR, menu );
999         menu->addAction( action );
1000         action->setData( psz_var );
1001     }
1002
1003     /* Some specific stuff */
1004     bool forceDisabled = false;
1005     if( !strcmp( psz_var, "spu-es" ) )
1006     {
1007         vlc_object_t *p_vout = ( vlc_object_t* )( vlc_object_find( p_intf,
1008                                     VLC_OBJECT_VOUT, FIND_ANYWHERE ) );
1009         forceDisabled = ( p_vout == NULL );
1010         if( p_vout )
1011             vlc_object_release( p_vout );
1012     }
1013
1014     if( i_type & VLC_VAR_HASCHOICE )
1015     {
1016         /* Append choices menu */
1017         if( b_submenu )
1018         {
1019             QMenu *submenu;
1020             submenu = action->menu();
1021             if( !submenu )
1022             {
1023                 submenu = new QMenu( menu );
1024                 action->setMenu( submenu );
1025             }
1026
1027             action->setEnabled(
1028                CreateChoicesMenu( submenu, psz_var, p_object, true ) == 0 );
1029             if( forceDisabled )
1030                 action->setEnabled( false );
1031         }
1032         else
1033             CreateChoicesMenu( menu, psz_var, p_object, true );
1034         FREENULL( text.psz_string );
1035         return;
1036     }
1037
1038     switch( i_type & VLC_VAR_TYPE )
1039     {
1040         case VLC_VAR_VOID:
1041             var_Get( p_object, psz_var, &val );
1042             CreateAndConnect( menu, psz_var, TEXT_OR_VAR, "", ITEM_NORMAL,
1043                     p_object->i_object_id, val, i_type );
1044             break;
1045
1046         case VLC_VAR_BOOL:
1047             var_Get( p_object, psz_var, &val );
1048             val.b_bool = !val.b_bool;
1049             CreateAndConnect( menu, psz_var, TEXT_OR_VAR, "", ITEM_CHECK,
1050                     p_object->i_object_id, val, i_type, !val.b_bool );
1051             break;
1052     }
1053     FREENULL( text.psz_string );
1054 }
1055
1056
1057 int QVLCMenu::CreateChoicesMenu( QMenu *submenu, const char *psz_var,
1058         vlc_object_t *p_object, bool b_root )
1059 {
1060     vlc_value_t val, val_list, text_list;
1061     int i_type, i;
1062
1063     /* Check the type of the object variable */
1064     i_type = var_Type( p_object, psz_var );
1065
1066     /* Make sure we want to display the variable */
1067     if( submenu->isEmpty() && IsMenuEmpty( psz_var, p_object, b_root ) )
1068         return VLC_EGENERIC;
1069
1070     switch( i_type & VLC_VAR_TYPE )
1071     {
1072         case VLC_VAR_VOID:
1073         case VLC_VAR_BOOL:
1074         case VLC_VAR_VARIABLE:
1075         case VLC_VAR_STRING:
1076         case VLC_VAR_INTEGER:
1077         case VLC_VAR_FLOAT:
1078             break;
1079         default:
1080             /* Variable doesn't exist or isn't handled */
1081             return VLC_EGENERIC;
1082     }
1083
1084     if( var_Change( p_object, psz_var, VLC_VAR_GETLIST,
1085                 &val_list, &text_list ) < 0 )
1086     {
1087         return VLC_EGENERIC;
1088     }
1089
1090 #define NORMAL_OR_RADIO i_type & VLC_VAR_ISCOMMAND ? ITEM_NORMAL: ITEM_RADIO
1091 #define NOTCOMMAND !( i_type & VLC_VAR_ISCOMMAND )
1092 #define CURVAL val_list.p_list->p_values[i]
1093 #define CURTEXT text_list.p_list->p_values[i].psz_string
1094
1095     for( i = 0; i < val_list.p_list->i_count; i++ )
1096     {
1097         vlc_value_t another_val;
1098         QString menutext;
1099         QMenu *subsubmenu = new QMenu( submenu );
1100
1101         switch( i_type & VLC_VAR_TYPE )
1102         {
1103             case VLC_VAR_VARIABLE:
1104                 CreateChoicesMenu( subsubmenu, CURVAL.psz_string, p_object, false );
1105                 subsubmenu->setTitle( qfu( CURTEXT ? CURTEXT :CURVAL.psz_string ) );
1106                 submenu->addMenu( subsubmenu );
1107                 break;
1108
1109             case VLC_VAR_STRING:
1110                 var_Get( p_object, psz_var, &val );
1111                 another_val.psz_string = strdup( CURVAL.psz_string );
1112                 menutext = qfu( CURTEXT ? CURTEXT : another_val.psz_string );
1113                 CreateAndConnect( submenu, psz_var, menutext, "", NORMAL_OR_RADIO,
1114                         p_object->i_object_id, another_val, i_type,
1115                         NOTCOMMAND && val.psz_string &&
1116                         !strcmp( val.psz_string, CURVAL.psz_string ) );
1117
1118                 free( val.psz_string );
1119                 break;
1120
1121             case VLC_VAR_INTEGER:
1122                 var_Get( p_object, psz_var, &val );
1123                 if( CURTEXT ) menutext = qfu( CURTEXT );
1124                 else menutext.sprintf( "%d", CURVAL.i_int );
1125                 CreateAndConnect( submenu, psz_var, menutext, "", NORMAL_OR_RADIO,
1126                         p_object->i_object_id, CURVAL, i_type,
1127                         NOTCOMMAND && CURVAL.i_int == val.i_int );
1128                 break;
1129
1130             case VLC_VAR_FLOAT:
1131                 var_Get( p_object, psz_var, &val );
1132                 if( CURTEXT ) menutext = qfu( CURTEXT );
1133                 else menutext.sprintf( "%.2f", CURVAL.f_float );
1134                 CreateAndConnect( submenu, psz_var, menutext, "", NORMAL_OR_RADIO,
1135                         p_object->i_object_id, CURVAL, i_type,
1136                         NOTCOMMAND && CURVAL.f_float == val.f_float );
1137                 break;
1138
1139             default:
1140                 break;
1141         }
1142     }
1143     currentGroup = NULL;
1144
1145     /* clean up everything */
1146     var_Change( p_object, psz_var, VLC_VAR_FREELIST, &val_list, &text_list );
1147
1148 #undef NORMAL_OR_RADIO
1149 #undef NOTCOMMAND
1150 #undef CURVAL
1151 #undef CURTEXT
1152     return VLC_SUCCESS;
1153 }
1154
1155 void QVLCMenu::CreateAndConnect( QMenu *menu, const char *psz_var,
1156         QString text, QString help,
1157         int i_item_type, int i_object_id,
1158         vlc_value_t val, int i_val_type,
1159         bool checked )
1160 {
1161     QAction *action = FindActionWithVar( menu, psz_var );
1162     if( !action )
1163     {
1164         /* This is a value */
1165         action = FindActionWithText( menu, text );
1166         if( !action )
1167         {
1168             action = new QAction( text, menu );
1169             menu->addAction( action );
1170         }
1171     }
1172
1173     action->setText( text );
1174     action->setToolTip( help );
1175
1176     action->setEnabled( i_object_id != 0 );
1177
1178     if( i_item_type == ITEM_CHECK )
1179     {
1180         action->setCheckable( true );
1181     }
1182     else if( i_item_type == ITEM_RADIO )
1183     {
1184         action->setCheckable( true );
1185         if( !currentGroup )
1186             currentGroup = new QActionGroup( menu );
1187         currentGroup->addAction( action );
1188     }
1189
1190     action->setChecked( checked );
1191
1192     MenuItemData *itemData = new MenuItemData( i_object_id, i_val_type,
1193             val, psz_var );
1194     CONNECT( action, triggered(), THEDP->menusMapper, map() );
1195     THEDP->menusMapper->setMapping( action, itemData );
1196     menu->addAction( action );
1197 }
1198
1199 void QVLCMenu::DoAction( intf_thread_t *p_intf, QObject *data )
1200 {
1201     MenuItemData *itemData = qobject_cast<MenuItemData *>( data );
1202     vlc_object_t *p_object = ( vlc_object_t * )vlc_object_get( itemData->i_object_id );
1203     if( p_object == NULL ) return;
1204
1205     var_Set( p_object, itemData->psz_var, itemData->val );
1206     vlc_object_release( p_object );
1207 }
1208