]> git.sesse.net Git - vlc/blob - modules/gui/qt4/menus.cpp
Qt4 - add ifdef if someone compiles vlc without vlm support
[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 #include <vlc/vlc.h>
26
27 #include <vlc_intf_strings.h>
28
29 #include "main_interface.hpp"
30 #include "menus.hpp"
31 #include "dialogs_provider.hpp"
32 #include "input_manager.hpp"
33
34 #include <QMenu>
35 #include <QMenuBar>
36 #include <QAction>
37 #include <QActionGroup>
38 #include <QSignalMapper>
39 #include <QSystemTrayIcon>
40
41 enum
42 {
43     ITEM_NORMAL,
44     ITEM_CHECK,
45     ITEM_RADIO
46 };
47
48 static QActionGroup *currentGroup;
49
50 // Add static entries to menus
51 void addDPStaticEntry( QMenu *menu,
52                        const QString text,
53                        char *help,
54                        char *icon,
55                        const char *member,
56                        char *shortcut )
57 {
58     if( !EMPTY_STR( icon ) > 0 )
59     {
60         if( !EMPTY_STR( shortcut ) > 0 )
61             menu->addAction( QIcon( icon ), text, THEDP, member, qtr( shortcut ) );
62         else
63             menu->addAction( QIcon( icon ), text, THEDP, member );
64     }
65     else
66     {
67         if( !EMPTY_STR( shortcut ) > 0 )
68             menu->addAction( text, THEDP, member, qtr( shortcut ) );
69         else
70             menu->addAction( text, THEDP, member );
71     }
72 }
73
74 void addMIMStaticEntry( intf_thread_t *p_intf,
75                         QMenu *menu,
76                         const QString text,
77                         char *help,
78                         char *icon,
79                         const char *member )
80 {
81     if( strlen( icon ) > 0 )
82     {
83         QAction *action = menu->addAction( text, THEMIM,  member );
84         action->setIcon( QIcon( icon ) );
85     }
86     else
87     {
88         menu->addAction( text, THEMIM, member );
89     }
90 }
91
92 /*****************************************************************************
93  * Definitions of variables for the dynamic menus
94  *****************************************************************************/
95 #define PUSH_VAR( var ) varnames.push_back( var ); \
96     objects.push_back( p_object->i_object_id )
97
98 #define PUSH_SEPARATOR if( objects.size() != i_last_separator ) { \
99     objects.push_back( 0 ); varnames.push_back( "" ); \
100     i_last_separator = objects.size(); }
101
102 static int InputAutoMenuBuilder( vlc_object_t *p_object,
103         vector<int> &objects,
104         vector<const char *> &varnames )
105 {
106     PUSH_VAR( "bookmark" );
107     PUSH_VAR( "title" );
108     PUSH_VAR( "chapter" );
109     PUSH_VAR( "program" );
110     PUSH_VAR( "navigation" );
111     PUSH_VAR( "dvd_menus" );
112     return VLC_SUCCESS;
113 }
114
115 static int VideoAutoMenuBuilder( vlc_object_t *p_object,
116         vector<int> &objects,
117         vector<const char *> &varnames )
118 {
119     PUSH_VAR( "fullscreen" );
120     PUSH_VAR( "zoom" );
121     PUSH_VAR( "deinterlace" );
122     PUSH_VAR( "aspect-ratio" );
123     PUSH_VAR( "crop" );
124     PUSH_VAR( "video-on-top" );
125     PUSH_VAR( "directx-wallpaper" );
126     PUSH_VAR( "video-snapshot" );
127
128     vlc_object_t *p_dec_obj = ( vlc_object_t * )vlc_object_find( p_object,
129             VLC_OBJECT_DECODER,
130             FIND_PARENT );
131     if( p_dec_obj != NULL )
132     {
133         vlc_object_t *p_object = p_dec_obj;
134         PUSH_VAR( "ffmpeg-pp-q" );
135         vlc_object_release( p_dec_obj );
136     }
137     return VLC_SUCCESS;
138 }
139
140 static int AudioAutoMenuBuilder( vlc_object_t *p_object,
141         vector<int> &objects,
142         vector<const char *> &varnames )
143 {
144     PUSH_VAR( "audio-device" );
145     PUSH_VAR( "audio-channels" );
146     PUSH_VAR( "visual" );
147     PUSH_VAR( "equalizer" );
148     return VLC_SUCCESS;
149 }
150
151 /*****************************************************************************
152  * All normal menus
153  * Simple Code
154  *****************************************************************************/
155
156 #define BAR_ADD( func, title ) { \
157     QMenu *menu = func; menu->setTitle( title ); bar->addMenu( menu ); }
158
159 #define BAR_DADD( func, title, id ) { \
160     QMenu *menu = func; menu->setTitle( title ); bar->addMenu( menu ); \
161     MenuFunc *f = new MenuFunc( menu, id ); \
162     CONNECT( menu, aboutToShow(), THEDP->menusUpdateMapper, map() ); \
163     THEDP->menusUpdateMapper->setMapping( menu, f ); }
164
165 /**
166  * Main Menu Bar Creation
167  **/
168 void QVLCMenu::createMenuBar( MainInterface *mi,
169                               intf_thread_t *p_intf,
170                               bool visual_selector_enabled )
171 {
172     QMenuBar *bar = mi->menuBar();
173     BAR_ADD( FileMenu(), qtr( "&Media" ) );
174     BAR_ADD( PlaylistMenu( p_intf, mi ), qtr( "&Playlist" ) );
175     BAR_ADD( ToolsMenu( p_intf, mi, visual_selector_enabled, true ), qtr( "&Tools" ) );
176     BAR_DADD( AudioMenu( p_intf, NULL ), qtr( "&Audio" ), 2 );
177     BAR_DADD( VideoMenu( p_intf, NULL ), qtr( "&Video" ), 1 );
178     BAR_DADD( NavigMenu( p_intf, NULL ), qtr( "&Playback" ), 3 );
179
180     BAR_ADD( HelpMenu(), qtr( "&Help" ) );
181 }
182 #undef BAR_ADD
183 #undef BAR_DADD
184
185 /**
186  * Media ( File ) Menu
187  * Opening, streaming and quit
188  **/
189 QMenu *QVLCMenu::FileMenu()
190 {
191     QMenu *menu = new QMenu();
192
193     addDPStaticEntry( menu, qtr( "&Open File..." ), "",
194         ":/pixmaps/file-asym_16px.png", SLOT( openFileDialog() ), "Ctrl+O" );
195     addDPStaticEntry( menu, qtr( I_OPEN_FOLDER ), "",
196         ":/pixmaps/folder-grey_16px.png", SLOT( PLAppendDir() ), "Ctrl+F" );
197     addDPStaticEntry( menu, qtr( "Open &Disc..." ), "",
198         ":/pixmaps/disc_16px.png", SLOT( openDiscDialog() ), "Ctrl+D" );
199     addDPStaticEntry( menu, qtr( "Open &Network..." ), "",
200         ":/pixmaps/network_16px.png", SLOT( openNetDialog() ), "Ctrl+N" );
201     addDPStaticEntry( menu, qtr( "Open &Capture Device..." ), "",
202         ":/pixmaps/capture-card_16px.png", SLOT( openCaptureDialog() ),
203         "Ctrl+C" );
204     menu->addSeparator();
205
206     addDPStaticEntry( menu, qtr( "&Streaming..." ), "",
207         ":/pixmaps/menus_stream_16px.png", SLOT( openThenStreamingDialogs() ),
208         "Ctrl+S" );
209     addDPStaticEntry( menu, qtr( "Conve&rt / Save..." ), "", "",
210         SLOT( openThenTranscodingDialogs() ), "Ctrl+R" );
211     menu->addSeparator();
212
213     addDPStaticEntry( menu, qtr( "&Quit" ) , "",
214         ":/pixmaps/menus_quit_16px.png", SLOT( quit() ), "Ctrl+Q" );
215     return menu;
216 }
217
218 /* Playlist/MediaLibrary Control */
219 QMenu *QVLCMenu::PlaylistMenu( intf_thread_t *p_intf, MainInterface *mi )
220 {
221     QMenu *menu = new QMenu();
222     menu->addMenu( SDMenu( p_intf ) );
223     menu->addAction( QIcon( ":/pixmaps/playlist_16px.png" ),
224                      qtr( "Show Playlist" ), mi, SLOT( togglePlaylist() ) );
225     menu->addSeparator();
226
227     addDPStaticEntry( menu, qtr( I_PL_LOAD ), "", "", SLOT( openAPlaylist() ),
228         "Ctrl+X" );
229     addDPStaticEntry( menu, qtr( I_PL_SAVE ), "", "", SLOT( saveAPlaylist() ),
230         "Ctrl+Y" );
231     menu->addSeparator();
232     menu->addAction( qtr( "Undock from interface" ), mi,
233                      SLOT( undockPlaylist() ), qtr( "Ctrl+U" ) );
234     return menu;
235 }
236
237 /**
238  * Tools/View Menu
239  * This is kept in the same menu for now, but could change if it gets much
240  * longer.
241  * This menu can be an interface menu but also a right click menu.
242  **/
243 QMenu *QVLCMenu::ToolsMenu( intf_thread_t *p_intf,
244                             MainInterface *mi,
245                             bool visual_selector_enabled,
246                             bool with_intf )
247 {
248     QMenu *menu = new QMenu;
249     if( mi )
250     {
251         menu->addAction( QIcon( ":/pixmaps/playlist_16px.png" ),
252                          qtr( "Playlist..." ), mi, SLOT( togglePlaylist() ),
253                          qtr( "Ctrl+L" ) );
254     }
255     addDPStaticEntry( menu, qtr( I_MENU_EXT ), "",
256         ":/pixmaps/menus_settings_16px.png", SLOT( extendedDialog() ),
257         "Ctrl+E" );
258
259     menu->addSeparator();
260
261     if( with_intf )
262     {
263         QMenu *intfmenu = InterfacesMenu( p_intf, NULL );
264         intfmenu->setTitle( qtr( "Add Interfaces" ) );
265         menu->addMenu( intfmenu );
266         menu->addSeparator();
267     }
268     if( mi )
269     {
270         /* Minimal View */
271         QAction *action=menu->addAction( qtr( "Minimal View..." ), mi,
272                 SLOT( toggleMinimalView() ), qtr( "Ctrl+H" ) );
273         action->setCheckable( true );
274         if( mi->getControlsVisibilityStatus() & CONTROLS_VISIBLE )
275             action->setChecked( true );
276
277         /* Advanced Controls */
278         action = menu->addAction( qtr( "Advanced controls" ), mi,
279                 SLOT( toggleAdvanced() ) );
280         action->setCheckable( true );
281         if( mi->getControlsVisibilityStatus() & CONTROLS_ADVANCED )
282             action->setChecked( true );
283 #if 0 /* For Visualisations. Not yet working */
284         adv = menu->addAction( qtr( "Visualizations selector" ),
285                 mi, SLOT( visual() ) );
286         adv->setCheckable( true );
287         if( visual_selector_enabled ) adv->setChecked( true );
288 #endif
289     }
290
291     menu->addSeparator();
292
293     addDPStaticEntry( menu, qtr( I_MENU_MSG ), "",
294         ":/pixmaps/menus_messages_16px.png", SLOT( messagesDialog() ),
295         "Ctrl+M" );
296     addDPStaticEntry( menu, qtr( I_MENU_INFO ) , "", "",
297         SLOT( mediaInfoDialog() ), "Ctrl+I" );
298     addDPStaticEntry( menu, qtr( I_MENU_CODECINFO ) , "",
299         ":/pixmaps/menus_info_16px.png", SLOT( mediaCodecDialog() ), "Ctrl+J" );
300
301
302     addDPStaticEntry( menu, qtr( I_MENU_BOOKMARK ), "","", 
303                       SLOT( bookmarksDialog() ), "Ctrl+B" );
304 #ifdef ENABLE_VLM
305     addDPStaticEntry( menu, qtr( I_MENU_VLM ), "", "", SLOT( vlmDialog() ),
306         "Ctrl+V" );
307 #endif
308
309     menu->addSeparator();
310     addDPStaticEntry( menu, qtr( "Preferences..." ), "",
311         ":/pixmaps/menus_preferences_16px.png", SLOT( prefsDialog() ), "Ctrl+P" );
312     return menu;
313 }
314
315 /**
316  * Interface Sub-Menu, to list extras interface and skins
317  **/
318 QMenu *QVLCMenu::InterfacesMenu( intf_thread_t *p_intf, QMenu *current )
319 {
320     vector<int> objects;
321     vector<const char *> varnames;
322     /** \todo add "switch to XXX" */
323     varnames.push_back( "intf-add" );
324     objects.push_back( p_intf->i_object_id );
325
326     QMenu *menu = Populate( p_intf, current, varnames, objects );
327
328     if( !p_intf->pf_show_dialog )
329     {
330         menu->addSeparator();
331         menu->addAction( qtr( "Switch to skins" ), THEDP, SLOT( switchToSkins() ),
332                 QString( "Ctrl+Z" ) );
333     }
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( "Update" ) , "", "", 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( p_intf, 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         /* Very evil hack ! intf-switch can have only one value */
807         if( !strcmp( psz_var, "intf-switch" ) ) return false;
808         if( val.i_int == 1 && b_root ) return true;
809         else return false;
810     }
811
812     /* Check children variables in case of VLC_VAR_VARIABLE */
813     if( var_Change( p_object, psz_var, VLC_VAR_GETLIST, &val_list, NULL ) < 0 )
814     {
815         return true;
816     }
817
818     for( i = 0, i_result = true; i < val_list.p_list->i_count; i++ )
819     {
820         if( !IsMenuEmpty( val_list.p_list->p_values[i].psz_string,
821                     p_object, false ) )
822         {
823             i_result = false;
824             break;
825         }
826     }
827
828     /* clean up everything */
829     var_Change( p_object, psz_var, VLC_VAR_FREELIST, &val_list, NULL );
830
831     return i_result;
832 }
833
834 void QVLCMenu::CreateItem( QMenu *menu, const char *psz_var,
835         vlc_object_t *p_object, bool b_submenu )
836 {
837     vlc_value_t val, text;
838     int i_type;
839
840     /* Check the type of the object variable */
841     i_type = var_Type( p_object, psz_var );
842
843     switch( i_type & VLC_VAR_TYPE )
844     {
845         case VLC_VAR_VOID:
846         case VLC_VAR_BOOL:
847         case VLC_VAR_VARIABLE:
848         case VLC_VAR_STRING:
849         case VLC_VAR_INTEGER:
850         case VLC_VAR_FLOAT:
851             break;
852         default:
853             /* Variable doesn't exist or isn't handled */
854             return;
855     }
856
857     /* Make sure we want to display the variable */
858     if( IsMenuEmpty( psz_var, p_object ) )  return;
859
860     /* Get the descriptive name of the variable */
861     var_Change( p_object, psz_var, VLC_VAR_GETTEXT, &text, NULL );
862
863     if( i_type & VLC_VAR_HASCHOICE )
864     {
865         /* Append choices menu */
866         if( b_submenu )
867         {
868             QMenu *submenu = new QMenu();
869             submenu->setTitle( qfu( text.psz_string ?
870                         text.psz_string : psz_var ) );
871             if( CreateChoicesMenu( submenu, psz_var, p_object, true ) == 0 )
872                 menu->addMenu( submenu );
873         }
874         else
875             CreateChoicesMenu( menu, psz_var, p_object, true );
876         FREENULL( text.psz_string );
877         return;
878     }
879
880 #define TEXT_OR_VAR qfu ( text.psz_string ? text.psz_string : psz_var )
881
882     switch( i_type & VLC_VAR_TYPE )
883     {
884         case VLC_VAR_VOID:
885             var_Get( p_object, psz_var, &val );
886             CreateAndConnect( menu, psz_var, TEXT_OR_VAR, "", ITEM_NORMAL,
887                     p_object->i_object_id, val, i_type );
888             break;
889
890         case VLC_VAR_BOOL:
891             var_Get( p_object, psz_var, &val );
892             val.b_bool = !val.b_bool;
893             CreateAndConnect( menu, psz_var, TEXT_OR_VAR, "", ITEM_CHECK,
894                     p_object->i_object_id, val, i_type, !val.b_bool );
895             break;
896     }
897     FREENULL( text.psz_string );
898 }
899
900
901 int QVLCMenu::CreateChoicesMenu( QMenu *submenu, const char *psz_var,
902         vlc_object_t *p_object, bool b_root )
903 {
904     vlc_value_t val, val_list, text_list;
905     int i_type, i;
906
907     /* Check the type of the object variable */
908     i_type = var_Type( p_object, psz_var );
909
910     /* Make sure we want to display the variable */
911     if( IsMenuEmpty( psz_var, p_object, b_root ) ) return VLC_EGENERIC;
912
913     switch( i_type & VLC_VAR_TYPE )
914     {
915         case VLC_VAR_VOID:
916         case VLC_VAR_BOOL:
917         case VLC_VAR_VARIABLE:
918         case VLC_VAR_STRING:
919         case VLC_VAR_INTEGER:
920         case VLC_VAR_FLOAT:
921             break;
922         default:
923             /* Variable doesn't exist or isn't handled */
924             return VLC_EGENERIC;
925     }
926
927     if( var_Change( p_object, psz_var, VLC_VAR_GETLIST,
928                 &val_list, &text_list ) < 0 )
929     {
930         return VLC_EGENERIC;
931     }
932
933 #define NORMAL_OR_RADIO i_type & VLC_VAR_ISCOMMAND ? ITEM_NORMAL: ITEM_RADIO
934 #define NOTCOMMAND !( i_type & VLC_VAR_ISCOMMAND )
935 #define CURVAL val_list.p_list->p_values[i]
936 #define CURTEXT text_list.p_list->p_values[i].psz_string
937
938     for( i = 0; i < val_list.p_list->i_count; i++ )
939     {
940         vlc_value_t another_val;
941         QString menutext;
942         QMenu *subsubmenu = new QMenu();
943
944         switch( i_type & VLC_VAR_TYPE )
945         {
946             case VLC_VAR_VARIABLE:
947                 CreateChoicesMenu( subsubmenu, CURVAL.psz_string, p_object, false );
948                 subsubmenu->setTitle( qfu( CURTEXT ? CURTEXT :CURVAL.psz_string ) );
949                 submenu->addMenu( subsubmenu );
950                 break;
951
952             case VLC_VAR_STRING:
953                 var_Get( p_object, psz_var, &val );
954                 another_val.psz_string = strdup( CURVAL.psz_string );
955                 menutext = qfu( CURTEXT ? CURTEXT : another_val.psz_string );
956                 CreateAndConnect( submenu, psz_var, menutext, "", NORMAL_OR_RADIO,
957                         p_object->i_object_id, another_val, i_type,
958                         NOTCOMMAND && val.psz_string &&
959                         !strcmp( val.psz_string, CURVAL.psz_string ) );
960
961                 if( val.psz_string ) free( val.psz_string );
962                 break;
963
964             case VLC_VAR_INTEGER:
965                 var_Get( p_object, psz_var, &val );
966                 if( CURTEXT ) menutext = qfu( CURTEXT );
967                 else menutext.sprintf( "%d", CURVAL.i_int );
968                 CreateAndConnect( submenu, psz_var, menutext, "", NORMAL_OR_RADIO,
969                         p_object->i_object_id, CURVAL, i_type,
970                         NOTCOMMAND && CURVAL.i_int == val.i_int );
971                 break;
972
973             case VLC_VAR_FLOAT:
974                 var_Get( p_object, psz_var, &val );
975                 if( CURTEXT ) menutext = qfu( CURTEXT );
976                 else menutext.sprintf( "%.2f", CURVAL.f_float );
977                 CreateAndConnect( submenu, psz_var, menutext, "", NORMAL_OR_RADIO,
978                         p_object->i_object_id, CURVAL, i_type,
979                         NOTCOMMAND && CURVAL.f_float == val.f_float );
980                 break;
981
982             default:
983                 break;
984         }
985     }
986     currentGroup = NULL;
987
988     /* clean up everything */
989     var_Change( p_object, psz_var, VLC_VAR_FREELIST, &val_list, &text_list );
990
991 #undef NORMAL_OR_RADIO
992 #undef NOTCOMMAND
993 #undef CURVAL
994 #undef CURTEXT
995     return VLC_SUCCESS;
996 }
997
998 void QVLCMenu::CreateAndConnect( QMenu *menu, const char *psz_var,
999         QString text, QString help,
1000         int i_item_type, int i_object_id,
1001         vlc_value_t val, int i_val_type,
1002         bool checked )
1003 {
1004     QAction *action = new QAction( text, menu );
1005     action->setText( text );
1006     action->setToolTip( help );
1007
1008     if( i_item_type == ITEM_CHECK )
1009     {
1010         action->setCheckable( true );
1011     }
1012     else if( i_item_type == ITEM_RADIO )
1013     {
1014         action->setCheckable( true );
1015         if( !currentGroup )
1016             currentGroup = new QActionGroup( menu );
1017         currentGroup->addAction( action );
1018     }
1019
1020     if( checked )
1021     {
1022         action->setChecked( true );
1023     }
1024     MenuItemData *itemData = new MenuItemData( i_object_id, i_val_type,
1025             val, psz_var );
1026     CONNECT( action, triggered(), THEDP->menusMapper, map() );
1027     THEDP->menusMapper->setMapping( action, itemData );
1028     menu->addAction( action );
1029 }
1030
1031 void QVLCMenu::DoAction( intf_thread_t *p_intf, QObject *data )
1032 {
1033     MenuItemData *itemData = qobject_cast<MenuItemData *>( data );
1034     vlc_object_t *p_object = ( vlc_object_t * )vlc_object_get( p_intf,
1035             itemData->i_object_id );
1036     if( p_object == NULL ) return;
1037
1038     var_Set( p_object, itemData->psz_var, itemData->val );
1039     vlc_object_release( p_object );
1040 }
1041