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