]> git.sesse.net Git - vlc/blob - modules/gui/qt4/menus.cpp
Qt4 - Menus: modify slightly the capture card icon, change a label and spaces...
[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  *****************************************************************************/
156
157 #define BAR_ADD( func, title ) { \
158     QMenu *menu = func; menu->setTitle( title  ); bar->addMenu( menu ); }
159
160 #define BAR_DADD( func, title, id ) { \
161     QMenu *menu = func; menu->setTitle( title  ); bar->addMenu( menu ); \
162     MenuFunc *f = new MenuFunc( menu, id ); \
163     CONNECT( menu, aboutToShow(), THEDP->menusUpdateMapper, map() ); \
164     THEDP->menusUpdateMapper->setMapping( menu, f ); }
165
166 void QVLCMenu::createMenuBar( MainInterface *mi, intf_thread_t *p_intf,
167         bool playlist, bool adv_controls_enabled,
168         bool visual_selector_enabled )
169 {
170 #ifndef WIN32
171     /* Ugly klugde
172      * Remove SIGCHLD from the ignored signal the time to initialise
173      * Qt because it call gconf to get the icon theme */
174     sigset_t set;
175
176     sigemptyset( &set );
177     sigaddset( &set, SIGCHLD );
178     pthread_sigmask( SIG_UNBLOCK, &set, NULL );
179 #endif
180     QMenuBar *bar = mi->menuBar();
181 #ifndef WIN32
182     pthread_sigmask( SIG_BLOCK, &set, NULL );
183 #endif
184     BAR_ADD( FileMenu(), qtr("&Media") );
185     if( playlist )
186     {
187         BAR_ADD( PlaylistMenu( mi,p_intf ), qtr("&Playlist" ) );
188     }
189     BAR_ADD( ToolsMenu( p_intf, mi, adv_controls_enabled,
190                 visual_selector_enabled ), qtr("&Tools") );
191     BAR_DADD( VideoMenu( p_intf, NULL ), qtr("&Video"), 1 );
192     BAR_DADD( AudioMenu( p_intf, NULL ), qtr("&Audio"), 2 );
193     BAR_DADD( NavigMenu( p_intf, NULL ), qtr("&Navigation"), 3 );
194
195     BAR_ADD( HelpMenu(), qtr("&Help" ) );
196 }
197 QMenu *QVLCMenu::FileMenu()
198 {
199     QMenu *menu = new QMenu();
200     DP_SADD( menu, qtr("Open &File..." ), "", "", openFileDialog(), "Ctrl+O" );
201     DP_SADD( menu, qtr("Open &Disc..." ), "", "", openDiscDialog(), "Ctrl+D" );
202     DP_SADD( menu, qtr("Open &Network..." ), "",
203                 ":/pixmaps/vlc_network_16px.png", openNetDialog(), "Ctrl+N" );
204     DP_SADD( menu, qtr("Open &Capture Device..." ), "",
205             ":/pixmaps/vlc_capture-card_16px.png", openCaptureDialog(),
206             "Ctrl+C" );
207     menu->addSeparator();
208     DP_SADD( menu, qtr("&Streaming..."), "", ":/pixmaps/vlc_stream_16px.png",
209             openThenStreamingDialogs(), "Ctrl+S" );
210     DP_SADD( menu, qtr("Conve&rt / Save..."), "", "",
211             openThenTranscodingDialogs(), "Ctrl+R" );
212     menu->addSeparator();
213     DP_SADD( menu, qtr("&Quit") , "", ":/pixmaps/vlc_quit_16px.png", quit(),
214             "Ctrl+Q");
215     return menu;
216 }
217
218 QMenu *QVLCMenu::PlaylistMenu( MainInterface *mi, intf_thread_t *p_intf )
219 {
220     QMenu *menu = new QMenu();
221     menu->addMenu( SDMenu( p_intf ) );
222     menu->addSeparator();
223
224     DP_SADD( menu, qtr( I_PL_LOAD ), "", "", openPlaylist(), "Ctrl+L" );
225     DP_SADD( menu, qtr( I_PL_SAVE ), "", "", savePlaylist(), "Ctrl+K" );
226     menu->addSeparator();
227     menu->addAction( qtr("Undock from interface"), mi,
228             SLOT( undockPlaylist() ), qtr("Ctrl+U") );
229     return menu;
230 }
231
232 QMenu *QVLCMenu::ToolsMenu( intf_thread_t *p_intf, MainInterface *mi,
233         bool adv_controls_enabled,
234         bool visual_selector_enabled, bool with_intf )
235 {
236     QMenu *menu = new QMenu();
237     if( with_intf )
238     {
239         QMenu *intfmenu = InterfacesMenu( p_intf, NULL );
240         intfmenu->setTitle( qtr("Interfaces" ) );
241         menu->addMenu( intfmenu );
242         menu->addSeparator();
243     }
244     DP_SADD( menu, qtr( I_MENU_MSG ), "", ":/pixmaps/vlc_messages_16px.png",
245              messagesDialog(), "Ctrl+M" );
246     DP_SADD( menu, qtr( I_MENU_INFO ) , "", "", mediaInfoDialog(), "Ctrl+J" );
247     DP_SADD( menu, qtr( I_MENU_CODECINFO ) , "", ":/pixmaps/vlc_info_16px.png",
248              mediaCodecDialog(), "Ctrl+I" );
249     DP_SADD( menu, qtr( I_MENU_GOTOTIME ), "","", gotoTimeDialog(), "Ctrl+T" );
250     DP_SADD( menu, qtr( I_MENU_BOOKMARK ), "","", bookmarksDialog(), "Ctrl+B" );
251     DP_SADD( menu, qtr( I_MENU_VLM ), "","", vlmDialog(), "Ctrl+V" );
252
253     menu->addSeparator();
254     if( mi )
255     {
256         QAction *adv = menu->addAction( qtr("Advanced controls" ),
257                 mi, SLOT( advanced() ) );
258         adv->setCheckable( true );
259         if( adv_controls_enabled ) adv->setChecked( true );
260 #if 0
261         adv = menu->addAction( qtr("Visualizations selector" ),
262                 mi, SLOT( visual() ) );
263         adv->setCheckable( true );
264         if( visual_selector_enabled ) adv->setChecked( true );
265 #endif
266         menu->addAction ( qtr( "Playlist"), mi, SLOT( playlist() ) );
267     }
268     DP_SADD( menu, qtr( I_MENU_EXT ), "","",extendedDialog(), "Ctrl+E" );
269     DP_SADD( menu, qtr( "Hide Menus..." ), "","",hideMenus(), "Ctrl+H" );
270     menu->addSeparator();
271     DP_SADD( menu, qtr("Preferences"), "", "", prefsDialog(), "Ctrl+P" );
272     return menu;
273 }
274
275 QMenu *QVLCMenu::InterfacesMenu( intf_thread_t *p_intf, QMenu *current )
276 {
277     vector<int> objects;
278     vector<const char *> varnames;
279     /** \todo add "switch to XXX" */
280     varnames.push_back( "intf-add" );
281     objects.push_back( p_intf->i_object_id );
282
283     QMenu *menu = Populate( p_intf, current, varnames, objects );
284
285     if( !p_intf->pf_show_dialog )
286     {
287         menu->addSeparator();
288         menu->addAction( qtr("Switch to skins"), THEDP, SLOT( switchToSkins() ),
289                 QString("Ctrl+Z") );
290     }
291
292     CONNECT( menu, aboutToShow(), THEDP->menusUpdateMapper, map() );
293     THEDP->menusUpdateMapper->setMapping( menu, 4 );
294     return menu;
295 }
296
297 QMenu *QVLCMenu::AudioMenu( intf_thread_t *p_intf, QMenu * current )
298 {
299     vector<int> objects;
300     vector<const char *> varnames;
301
302     vlc_object_t *p_object = (vlc_object_t *)vlc_object_find( p_intf,
303             VLC_OBJECT_INPUT, FIND_ANYWHERE );
304     if( p_object != NULL )
305     {
306         PUSH_VAR( "audio-es" );
307         vlc_object_release( p_object );
308     }
309
310     p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_AOUT,
311             FIND_ANYWHERE );
312     if( p_object )
313     {
314         AudioAutoMenuBuilder( p_object, objects, varnames );
315         vlc_object_release( p_object );
316     }
317     return Populate( p_intf, current, varnames, objects );
318 }
319
320
321 QMenu *QVLCMenu::VideoMenu( intf_thread_t *p_intf, QMenu *current )
322 {
323     vlc_object_t *p_object;
324     vector<int> objects;
325     vector<const char *> varnames;
326
327     p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
328             FIND_ANYWHERE );
329     if( p_object != NULL )
330     {
331         PUSH_VAR( "video-es" );
332         PUSH_VAR( "spu-es" );
333         vlc_object_release( p_object );
334     }
335
336     p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_VOUT,
337             FIND_ANYWHERE );
338     if( p_object != NULL )
339     {
340         VideoAutoMenuBuilder( p_object, objects, varnames );
341         vlc_object_release( p_object );
342     }
343     return Populate( p_intf, current, varnames, objects );
344 }
345
346 QMenu *QVLCMenu::NavigMenu( intf_thread_t *p_intf, QMenu *current )
347 {
348     vlc_object_t *p_object;
349     vector<int> objects;
350     vector<const char *> varnames;
351
352     /* FIXME */
353     p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
354             FIND_ANYWHERE );
355     if( p_object != NULL )
356     {
357         InputAutoMenuBuilder( p_object, objects, varnames );
358         PUSH_VAR( "prev-title"); PUSH_VAR ( "next-title" );
359         PUSH_VAR( "prev-chapter"); PUSH_VAR( "next-chapter" );
360         vlc_object_release( p_object );
361     }
362     return Populate( p_intf, current, varnames, objects );
363 }
364
365 QMenu *QVLCMenu::SDMenu( intf_thread_t *p_intf )
366 {
367     QMenu *menu = new QMenu();
368     menu->setTitle( qtr( I_PL_SD ) );
369     vlc_list_t *p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE,
370             FIND_ANYWHERE );
371     int i_num = 0;
372     for( int i_index = 0 ; i_index < p_list->i_count; i_index++ )
373     {
374         module_t * p_parser = (module_t *)p_list->p_values[i_index].p_object ;
375         if( !strcmp( p_parser->psz_capability, "services_discovery" ) )
376             i_num++;
377     }
378     for( int i_index = 0 ; i_index < p_list->i_count; i_index++ )
379     {
380         module_t * p_parser = (module_t *)p_list->p_values[i_index].p_object;
381         if( !strcmp( p_parser->psz_capability, "services_discovery" ) )
382         {
383             QAction *a = new QAction( qfu( p_parser->psz_longname ), menu );
384             a->setCheckable( true );
385             /* hack to handle submodules properly */
386             int i = -1;
387             while( p_parser->pp_shortcuts[++i] != NULL );
388             i--;
389             if( playlist_IsServicesDiscoveryLoaded( THEPL,
390                         i>=0?p_parser->pp_shortcuts[i] : p_parser->psz_object_name ) )
391             {
392                 a->setChecked( true );
393             }
394             CONNECT( a , triggered(), THEDP->SDMapper, map() );
395             THEDP->SDMapper->setMapping( a, i>=0? p_parser->pp_shortcuts[i] :
396                     p_parser->psz_object_name );
397             menu->addAction( a );
398         }
399     }
400     vlc_list_release( p_list );
401     return menu;
402 }
403
404 QMenu *QVLCMenu::HelpMenu()
405 {
406     QMenu *menu = new QMenu();
407     DP_SADD( menu, qtr("Help") , "", ":/pixmaps/vlc_help_16px.png", helpDialog(), "F1" );
408     menu->addSeparator();
409     DP_SADD( menu, qtr( I_MENU_ABOUT ), "", "", aboutDialog(), "Ctrl+F1");
410     return menu;
411 }
412
413
414 /*****************************************************************************
415  * Popup menus                                                               *
416  *****************************************************************************/
417 #define POPUP_BOILERPLATE \
418     unsigned int i_last_separator = 0; \
419     vector<int> objects; \
420     vector<const char *> varnames; \
421     input_thread_t *p_input = THEMIM->getInput();
422
423 #define CREATE_POPUP \
424     QMenu *menu = new QMenu(); \
425     Populate( p_intf, menu, varnames, objects ); \
426     p_intf->p_sys->p_popup_menu = menu; \
427     menu->popup( QCursor::pos() ); \
428     p_intf->p_sys->p_popup_menu = NULL; \
429     i_last_separator = 0;
430
431 #define POPUP_PLAY_ENTRIES( menu )\
432     vlc_value_t val; \
433     if( p_input ) \
434     { \
435         var_Get( p_input, "state", &val ); \
436         if( val.i_int == PLAYING_S ) \
437             MIM_SADD( menu, qtr("Pause"), "", ":/pixmaps/vlc_pause_16px.png", \
438                     togglePlayPause() ) \
439         else \
440             MIM_SADD( menu, qtr("Play"), "", ":/pixmaps/vlc_play_16px.png", \
441                     togglePlayPause() ) \
442     } \
443     else if( THEPL->items.i_size && THEPL->i_enabled ) \
444         MIM_SADD( menu, qtr("Play"), "", ":/pixmaps/vlc_play_16px.png", \
445                 togglePlayPause() ); \
446     \
447     MIM_SADD( menu, qtr("Stop"), "", ":/pixmaps/vlc_stop_16px.png", stop() ); \
448     MIM_SADD( menu, qtr("Previous"), "", ":/pixmaps/vlc_previous_16px.png", \
449             prev() ); \
450     MIM_SADD( menu, qtr("Next"), "", ":/pixmaps/vlc_next_16px.png", next() );
451
452 #define POPUP_STATIC_ENTRIES \
453     POPUP_PLAY_ENTRIES( menu ); \
454     \
455     menu->addSeparator(); \
456     QMenu *intfmenu = InterfacesMenu( p_intf, NULL ); \
457     intfmenu->setTitle( qtr("Interfaces" ) ); \
458     menu->addMenu( intfmenu ); \
459     \
460     QMenu *toolsmenu = ToolsMenu( p_intf, NULL, false, false, false ); \
461     toolsmenu->setTitle( qtr("Tools" ) ); \
462     menu->addMenu( toolsmenu ); \
463     \
464     QMenu *openmenu = new QMenu( qtr("Open") ); \
465     openmenu->addAction( qtr("Open &File..." ), THEDP, SLOT( openFileDialog() ) ); \
466     openmenu->addAction( qtr("Open &Disc..." ), THEDP, SLOT( openDiscDialog() ) ); \
467     openmenu->addAction( qtr("Open &Network..." ), THEDP, SLOT( openNetDialog() ) ); \
468     openmenu->addAction( qtr("Open &Capture Device..." ), THEDP, \
469             SLOT( openCaptureDialog() ) ); \
470     menu->addMenu( openmenu ); \
471     \
472     menu->addSeparator(); \
473     QMenu *helpmenu = HelpMenu(); \
474     helpmenu->setTitle( qtr("Help") ); \
475     menu->addMenu( helpmenu ); \
476     \
477     DP_SADD( menu, qtr("Quit"), "", "", quit() , "Ctrl+Q" );
478
479 void QVLCMenu::VideoPopupMenu( intf_thread_t *p_intf )
480 {
481     POPUP_BOILERPLATE;
482     if( p_input )
483     {
484         vlc_object_yield( p_input );
485         varnames.push_back( "video-es" );
486         objects.push_back( p_input->i_object_id );
487         varnames.push_back( "spu-es" );
488         objects.push_back( p_input->i_object_id );
489         vlc_object_t *p_vout = (vlc_object_t *)vlc_object_find( p_input,
490                 VLC_OBJECT_VOUT, FIND_CHILD );
491         if( p_vout )
492         {
493             VideoAutoMenuBuilder( p_vout, objects, varnames );
494             vlc_object_release( p_vout );
495         }
496         vlc_object_release( p_input );
497     }
498     CREATE_POPUP;
499 }
500
501 void QVLCMenu::AudioPopupMenu( intf_thread_t *p_intf )
502 {
503     POPUP_BOILERPLATE;
504     if( p_input )
505     {
506         vlc_object_yield( p_input );
507         varnames.push_back( "audio-es" );
508         objects.push_back( p_input->i_object_id );
509         vlc_object_t *p_aout = (vlc_object_t *)vlc_object_find( p_input,
510                 VLC_OBJECT_AOUT, FIND_ANYWHERE );
511         if( p_aout )
512         {
513             AudioAutoMenuBuilder( p_aout, objects, varnames );
514             vlc_object_release( p_aout );
515         }
516         vlc_object_release( p_input );
517     }
518     CREATE_POPUP;
519 }
520
521 /* Navigation stuff, and general */
522 void QVLCMenu::MiscPopupMenu( intf_thread_t *p_intf )
523 {
524     POPUP_BOILERPLATE;
525     if( p_input )
526     {
527         vlc_object_yield( p_input );
528         varnames.push_back( "audio-es" );
529         InputAutoMenuBuilder( VLC_OBJECT(p_input), objects, varnames );
530         PUSH_SEPARATOR;
531     }
532
533     QMenu *menu = new QMenu();
534     Populate( p_intf, menu, varnames, objects );
535     menu->addSeparator();
536     POPUP_STATIC_ENTRIES;
537
538     p_intf->p_sys->p_popup_menu = menu;
539     menu->popup( QCursor::pos() );
540     p_intf->p_sys->p_popup_menu = NULL;
541 }
542
543 void QVLCMenu::PopupMenu( intf_thread_t *p_intf, bool show )
544 {
545     if( show )
546     {
547         // create a  popup if there is none
548         if( ! p_intf->p_sys->p_popup_menu )
549         {
550             POPUP_BOILERPLATE;
551             if( p_input )
552             {
553                 vlc_object_yield( p_input );
554                 InputAutoMenuBuilder( VLC_OBJECT(p_input), objects, varnames );
555
556                 /* Video menu */
557                 PUSH_SEPARATOR;
558                 varnames.push_back( "video-es" );
559                 objects.push_back( p_input->i_object_id );
560                 varnames.push_back( "spu-es" );
561                 objects.push_back( p_input->i_object_id );
562                 vlc_object_t *p_vout = (vlc_object_t *)vlc_object_find( p_input,
563                         VLC_OBJECT_VOUT, FIND_CHILD );
564                 if( p_vout )
565                 {
566                     VideoAutoMenuBuilder( p_vout, objects, varnames );
567                     vlc_object_release( p_vout );
568                 }
569                 /* Audio menu */
570                 PUSH_SEPARATOR
571                     varnames.push_back( "audio-es" );
572                 objects.push_back( p_input->i_object_id );
573                 vlc_object_t *p_aout = (vlc_object_t *)vlc_object_find( p_input,
574                         VLC_OBJECT_AOUT, FIND_ANYWHERE );
575                 if( p_aout )
576                 {
577                     AudioAutoMenuBuilder( p_aout, objects, varnames );
578                     vlc_object_release( p_aout );
579                 }
580             }
581
582             QMenu *menu = new QMenu();
583             Populate( p_intf, menu, varnames, objects );
584             menu->addSeparator();
585             POPUP_STATIC_ENTRIES;
586
587             p_intf->p_sys->p_popup_menu = menu;
588         }
589         p_intf->p_sys->p_popup_menu->popup( QCursor::pos() );
590     }
591     else
592     {
593         // destroy popup if there is one
594         delete p_intf->p_sys->p_popup_menu;
595         p_intf->p_sys->p_popup_menu = NULL;
596     }
597 }
598
599 /************************************************************************
600  * Systray Menu                                                         *
601  ************************************************************************/
602
603 void QVLCMenu::updateSystrayMenu( MainInterface *mi, intf_thread_t *p_intf,
604                                     bool b_force_visible )
605 {
606     POPUP_BOILERPLATE;
607     QMenu *sysMenu = mi->getSysTrayMenu();
608     sysMenu->clear();
609     if( mi->isVisible() || b_force_visible )
610     {
611         sysMenu->addAction( QIcon( ":/vlc16.png" ),
612                 qtr("Hide VLC media player"), mi,
613                 SLOT( toggleUpdateSystrayMenu() ) );
614     }
615     else
616     {
617         sysMenu->addAction( QIcon( ":/vlc16.png" ),
618                 qtr("Show VLC media player"), mi,
619                 SLOT( toggleUpdateSystrayMenu() ) );
620     }
621     sysMenu->addSeparator();
622     POPUP_PLAY_ENTRIES( sysMenu );
623     sysMenu->addSeparator();
624     DP_SADD( sysMenu, qtr("&Open Media" ), "", "", openFileDialog(), "" );
625     DP_SADD( sysMenu, qtr("&Quit") , "", ":/pixmaps/vlc_quit_16px.png",
626              quit(), "" );
627
628     mi->getSysTray()->setContextMenu( sysMenu );
629 }
630
631 #undef PUSH_VAR
632 #undef PUSH_SEPARATOR
633
634
635 /*************************************************************************
636  * Builders for automenus
637  *************************************************************************/
638 QMenu * QVLCMenu::Populate( intf_thread_t *p_intf, QMenu *current,
639         vector< const char *> & varnames,
640         vector<int> & objects, bool append )
641 {
642     QMenu *menu = current;
643     if( !menu )
644         menu = new QMenu();
645     else if( !append )
646         menu->clear();
647
648     currentGroup = NULL;
649
650     vlc_object_t *p_object;
651     vlc_bool_t b_section_empty = VLC_FALSE;
652     int i;
653
654 #define APPEND_EMPTY { QAction *action = menu->addAction( qtr("Empty" ) ); \
655     action->setEnabled( false ); }
656
657     for( i = 0; i < (int)objects.size() ; i++ )
658     {
659         if( !varnames[i] || !*varnames[i] )
660         {
661             if( b_section_empty )
662                 APPEND_EMPTY;
663             menu->addSeparator();
664             b_section_empty = VLC_TRUE;
665             continue;
666         }
667
668         if( objects[i] == 0  )
669         {
670             /// \bug What is this ?
671             // Append( menu, varnames[i], NULL );
672             b_section_empty = VLC_FALSE;
673             continue;
674         }
675
676         p_object = (vlc_object_t *)vlc_object_get( p_intf,
677                 objects[i] );
678         if( p_object == NULL ) continue;
679
680         b_section_empty = VLC_FALSE;
681         /* Ugly specific stuff */
682         if( strstr(varnames[i], "intf-add" ) )
683             CreateItem( menu, varnames[i], p_object, false );
684         else
685             CreateItem( menu, varnames[i], p_object, true );
686         vlc_object_release( p_object );
687     }
688
689     /* Special case for empty menus */
690     if( menu->actions().size() == 0 || b_section_empty )
691         APPEND_EMPTY
692
693             return menu;
694 }
695
696 /*****************************************************************************
697  * Private methods.
698  *****************************************************************************/
699
700 static bool IsMenuEmpty( const char *psz_var, vlc_object_t *p_object,
701         bool b_root = TRUE )
702 {
703     vlc_value_t val, val_list;
704     int i_type, i_result, i;
705
706     /* Check the type of the object variable */
707     i_type = var_Type( p_object, psz_var );
708
709     /* Check if we want to display the variable */
710     if( !(i_type & VLC_VAR_HASCHOICE) ) return FALSE;
711
712     var_Change( p_object, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
713     if( val.i_int == 0 ) return TRUE;
714
715     if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE )
716     {
717         /* Very evil hack ! intf-switch can have only one value */
718         if( !strcmp( psz_var, "intf-switch" ) ) return FALSE;
719         if( val.i_int == 1 && b_root ) return TRUE;
720         else return FALSE;
721     }
722
723     /* Check children variables in case of VLC_VAR_VARIABLE */
724     if( var_Change( p_object, psz_var, VLC_VAR_GETLIST, &val_list, NULL ) < 0 )
725     {
726         return TRUE;
727     }
728
729     for( i = 0, i_result = TRUE; i < val_list.p_list->i_count; i++ )
730     {
731         if( !IsMenuEmpty( val_list.p_list->p_values[i].psz_string,
732                     p_object, FALSE ) )
733         {
734             i_result = FALSE;
735             break;
736         }
737     }
738
739     /* clean up everything */
740     var_Change( p_object, psz_var, VLC_VAR_FREELIST, &val_list, NULL );
741
742     return i_result;
743 }
744
745 void QVLCMenu::CreateItem( QMenu *menu, const char *psz_var,
746         vlc_object_t *p_object, bool b_submenu )
747 {
748     vlc_value_t val, text;
749     int i_type;
750
751     /* Check the type of the object variable */
752     i_type = var_Type( p_object, psz_var );
753
754     switch( i_type & VLC_VAR_TYPE )
755     {
756         case VLC_VAR_VOID:
757         case VLC_VAR_BOOL:
758         case VLC_VAR_VARIABLE:
759         case VLC_VAR_STRING:
760         case VLC_VAR_INTEGER:
761         case VLC_VAR_FLOAT:
762             break;
763         default:
764             /* Variable doesn't exist or isn't handled */
765             return;
766     }
767
768     /* Make sure we want to display the variable */
769     if( IsMenuEmpty( psz_var, p_object ) )  return;
770
771     /* Get the descriptive name of the variable */
772     var_Change( p_object, psz_var, VLC_VAR_GETTEXT, &text, NULL );
773
774     if( i_type & VLC_VAR_HASCHOICE )
775     {
776         /* Append choices menu */
777         if( b_submenu )
778         {
779             QMenu *submenu = new QMenu();
780             submenu->setTitle( qfu( text.psz_string ?
781                         text.psz_string : psz_var ) );
782             if( CreateChoicesMenu( submenu, psz_var, p_object, true ) == 0)
783                 menu->addMenu( submenu );
784         }
785         else
786             CreateChoicesMenu( menu, psz_var, p_object, true );
787         FREENULL( text.psz_string );
788         return;
789     }
790
791 #define TEXT_OR_VAR qfu ( text.psz_string ? text.psz_string : psz_var )
792
793     switch( i_type & VLC_VAR_TYPE )
794     {
795         case VLC_VAR_VOID:
796             var_Get( p_object, psz_var, &val );
797             CreateAndConnect( menu, psz_var, TEXT_OR_VAR, "", ITEM_NORMAL,
798                     p_object->i_object_id, val, i_type );
799             break;
800
801         case VLC_VAR_BOOL:
802             var_Get( p_object, psz_var, &val );
803             val.b_bool = !val.b_bool;
804             CreateAndConnect( menu, psz_var, TEXT_OR_VAR, "", ITEM_CHECK,
805                     p_object->i_object_id, val, i_type, !val.b_bool );
806             break;
807     }
808     FREENULL( text.psz_string );
809 }
810
811
812 int QVLCMenu::CreateChoicesMenu( QMenu *submenu, const char *psz_var,
813         vlc_object_t *p_object, bool b_root )
814 {
815     vlc_value_t val, val_list, text_list;
816     int i_type, i;
817
818     /* Check the type of the object variable */
819     i_type = var_Type( p_object, psz_var );
820
821     /* Make sure we want to display the variable */
822     if( IsMenuEmpty( psz_var, p_object, b_root ) ) return VLC_EGENERIC;
823
824     switch( i_type & VLC_VAR_TYPE )
825     {
826         case VLC_VAR_VOID:
827         case VLC_VAR_BOOL:
828         case VLC_VAR_VARIABLE:
829         case VLC_VAR_STRING:
830         case VLC_VAR_INTEGER:
831         case VLC_VAR_FLOAT:
832             break;
833         default:
834             /* Variable doesn't exist or isn't handled */
835             return VLC_EGENERIC;
836     }
837
838     if( var_Change( p_object, psz_var, VLC_VAR_GETLIST,
839                 &val_list, &text_list ) < 0 )
840     {
841         return VLC_EGENERIC;
842     }
843 #define NORMAL_OR_RADIO i_type & VLC_VAR_ISCOMMAND ? ITEM_NORMAL: ITEM_RADIO
844 #define NOTCOMMAND !(i_type & VLC_VAR_ISCOMMAND)
845 #define CURVAL val_list.p_list->p_values[i]
846 #define CURTEXT text_list.p_list->p_values[i].psz_string
847
848     for( i = 0; i < val_list.p_list->i_count; i++ )
849     {
850         vlc_value_t another_val;
851         QString menutext;
852         QMenu *subsubmenu = new QMenu();
853
854         switch( i_type & VLC_VAR_TYPE )
855         {
856             case VLC_VAR_VARIABLE:
857                 CreateChoicesMenu( subsubmenu, CURVAL.psz_string, p_object, false );
858                 subsubmenu->setTitle( qfu( CURTEXT ? CURTEXT :CURVAL.psz_string ) );
859                 submenu->addMenu( subsubmenu );
860                 break;
861
862             case VLC_VAR_STRING:
863                 var_Get( p_object, psz_var, &val );
864                 another_val.psz_string = strdup( CURVAL.psz_string );
865                 menutext = qfu( CURTEXT ? CURTEXT : another_val.psz_string );
866                 CreateAndConnect( submenu, psz_var, menutext, "", NORMAL_OR_RADIO,
867                         p_object->i_object_id, another_val, i_type,
868                         NOTCOMMAND && val.psz_string &&
869                         !strcmp( val.psz_string, CURVAL.psz_string ) );
870
871                 if( val.psz_string ) free( val.psz_string );
872                 break;
873
874             case VLC_VAR_INTEGER:
875                 var_Get( p_object, psz_var, &val );
876                 if( CURTEXT ) menutext = qfu( CURTEXT );
877                 else menutext.sprintf( "%d", CURVAL.i_int);
878                 CreateAndConnect( submenu, psz_var, menutext, "", NORMAL_OR_RADIO,
879                         p_object->i_object_id, CURVAL, i_type,
880                         NOTCOMMAND && CURVAL.i_int == val.i_int );
881                 break;
882
883             case VLC_VAR_FLOAT:
884                 var_Get( p_object, psz_var, &val );
885                 if( CURTEXT ) menutext = qfu( CURTEXT );
886                 else menutext.sprintf( "%.2f", CURVAL.f_float );
887                 CreateAndConnect( submenu, psz_var, menutext, "", NORMAL_OR_RADIO,
888                         p_object->i_object_id, CURVAL, i_type,
889                         NOTCOMMAND && CURVAL.f_float == val.f_float );
890                 break;
891
892             default:
893                 break;
894         }
895     }
896     currentGroup = NULL;
897
898     /* clean up everything */
899     var_Change( p_object, psz_var, VLC_VAR_FREELIST, &val_list, &text_list );
900
901 #undef NORMAL_OR_RADIO
902 #undef NOTCOMMAND
903 #undef CURVAL
904 #undef CURTEXT
905     return VLC_SUCCESS;
906 }
907
908 void QVLCMenu::CreateAndConnect( QMenu *menu, const char *psz_var,
909         QString text, QString help,
910         int i_item_type, int i_object_id,
911         vlc_value_t val, int i_val_type,
912         bool checked )
913 {
914     QAction *action = new QAction( text, menu );
915     action->setText( text );
916     action->setToolTip( help );
917
918     if( i_item_type == ITEM_CHECK )
919     {
920         action->setCheckable( true );
921     }
922     else if( i_item_type == ITEM_RADIO )
923     {
924         action->setCheckable( true );
925         if( !currentGroup )
926             currentGroup = new QActionGroup( menu );
927         currentGroup->addAction( action );
928     }
929
930     if( checked )
931     {
932         action->setChecked( true );
933     }
934     MenuItemData *itemData = new MenuItemData( i_object_id, i_val_type,
935             val, psz_var );
936     CONNECT( action, triggered(), THEDP->menusMapper, map() );
937     THEDP->menusMapper->setMapping( action, itemData );
938     menu->addAction( action );
939 }
940
941 void QVLCMenu::DoAction( intf_thread_t *p_intf, QObject *data )
942 {
943     MenuItemData *itemData = qobject_cast<MenuItemData *>(data);
944     vlc_object_t *p_object = (vlc_object_t *)vlc_object_get( p_intf,
945             itemData->i_object_id );
946     if( p_object == NULL ) return;
947
948     var_Set( p_object, itemData->psz_var, itemData->val );
949     vlc_object_release( p_object );}