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