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