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