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