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