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