]> git.sesse.net Git - vlc/blob - modules/gui/qt4/menus.cpp
8b59d2c73e904420c418e7670f54703f84655b52
[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  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #include <QMenu>
25 #include <QMenuBar>
26 #include <QAction>
27 #include <QActionGroup>
28 #include <QSignalMapper>
29
30 #ifndef WIN32
31 #   include <signal.h>
32 #endif
33
34 #include <vlc_intf_strings.h>
35
36 #include "main_interface.hpp"
37 #include "menus.hpp"
38 #include "dialogs_provider.hpp"
39 #include "input_manager.hpp"
40
41 enum
42 {
43     ITEM_NORMAL,
44     ITEM_CHECK,
45     ITEM_RADIO
46 };
47
48 static QActionGroup *currentGroup;
49
50 // Add static entries to menus
51 #define DP_SADD( text, help, icon, slot, shortcut ) \
52     { \
53     if( strlen(icon) > 0 ) \
54     { \
55         if( strlen(shortcut) > 0 ) \
56         { \
57             menu->addAction( QIcon(icon), text, THEDP, SLOT( slot ), \
58                     tr(shortcut) );\
59         } \
60         else \
61         { \
62             menu->addAction( QIcon(icon), text, THEDP, SLOT( slot ) );\
63         } \
64     } \
65     else \
66     { \
67         if( strlen(shortcut) > 0 ) \
68         { \
69             menu->addAction( text, THEDP, SLOT( slot ), \
70                    qtr(shortcut) ); \
71         } \
72         else \
73         { \
74             menu->addAction( text, THEDP, SLOT( slot ) ); \
75         } \
76     } \
77     }
78 #define MIM_SADD( text, help, icon, slot ) \
79     { \
80     if( strlen(icon) > 0 ) \
81     { \
82         QAction *action = menu->addAction( text, THEMIM, SLOT( slot ) ); \
83         action->setIcon(QIcon(icon)); \
84     } \
85     else \
86     { \
87         menu->addAction( text, THEMIM, SLOT( slot ) ); \
88     } \
89     }
90 #define PL_SADD
91
92 /*****************************************************************************
93  * Definitions of variables for the dynamic menus
94  *****************************************************************************/
95 #define PUSH_VAR( var ) varnames.push_back( var ); \
96                         objects.push_back( p_object->i_object_id )
97
98 #define PUSH_SEPARATOR if( objects.size() != i_last_separator ) { \
99                            objects.push_back( 0 ); varnames.push_back( "" ); \
100                            i_last_separator = objects.size(); }
101
102 static int InputAutoMenuBuilder( vlc_object_t *p_object,
103                                  vector<int> &objects,
104                                  vector<const char *> &varnames )
105 {
106     PUSH_VAR( "bookmark");
107     PUSH_VAR( "title" );
108     PUSH_VAR ("chapter" );
109     PUSH_VAR( "program" );
110     PUSH_VAR( "navigation" );
111     PUSH_VAR( "dvd_menus" );
112     return VLC_SUCCESS;
113 }
114
115 static int VideoAutoMenuBuilder( vlc_object_t *p_object,
116                                  vector<int> &objects,
117                                  vector<const char *> &varnames )
118 {
119     PUSH_VAR( "fullscreen" );
120     PUSH_VAR( "zoom" );
121     PUSH_VAR( "deinterlace" );
122     PUSH_VAR( "aspect-ratio" );
123     PUSH_VAR( "crop" );
124     PUSH_VAR( "video-on-top" );
125     PUSH_VAR( "directx-wallpaper" );
126     PUSH_VAR( "video-snapshot" );
127
128     vlc_object_t *p_dec_obj = (vlc_object_t *)vlc_object_find( p_object,
129                                                  VLC_OBJECT_DECODER,
130                                                  FIND_PARENT );
131     if( p_dec_obj != NULL )
132     {
133         vlc_object_t *p_object = p_dec_obj;
134         PUSH_VAR( "ffmpeg-pp-q" );
135         vlc_object_release( p_dec_obj );
136     }
137     return VLC_SUCCESS;
138 }
139
140 static int AudioAutoMenuBuilder( vlc_object_t *p_object,
141                                  vector<int> &objects,
142                                  vector<const char *> &varnames )
143 {
144     PUSH_VAR( "audio-device" );
145     PUSH_VAR( "audio-channels" );
146     PUSH_VAR( "visual" );
147     PUSH_VAR( "equalizer" );
148     return VLC_SUCCESS;
149 }
150
151 /*****************************************************************************
152  * All normal menus
153  *****************************************************************************/
154
155 #define BAR_ADD( func, title ) { \
156     QMenu *menu = func; menu->setTitle( title  ); bar->addMenu( menu ); }
157
158 #define BAR_DADD( func, title, id ) { \
159     QMenu *menu = func; menu->setTitle( title  ); bar->addMenu( menu ); \
160     MenuFunc *f = new MenuFunc( menu, id ); \
161     CONNECT( menu, aboutToShow(), THEDP->menusUpdateMapper, map() ); \
162     THEDP->menusUpdateMapper->setMapping( menu, f ); }
163
164 void QVLCMenu::createMenuBar( MainInterface *mi, intf_thread_t *p_intf,
165                               bool playlist, bool adv_controls_enabled,
166                               bool visual_selector_enabled )
167 {
168 #ifndef WIN32
169     /* Ugly klugde
170      * Remove SIGCHLD from the ignored signal the time to initialise
171      * Qt because it call gconf to get the icon theme */
172     sigset_t set;
173
174     sigemptyset( &set );
175     sigaddset( &set, SIGCHLD );
176     pthread_sigmask (SIG_UNBLOCK, &set, NULL);
177 #endif
178     QMenuBar *bar = mi->menuBar();
179 #ifndef WIN32 
180     pthread_sigmask (SIG_BLOCK, &set, NULL);
181 #endif
182     BAR_ADD( FileMenu(), qtr("Media") );
183     if( playlist )
184     {
185         BAR_ADD( PlaylistMenu( mi,p_intf ), qtr("Playlist" ) );
186     }
187     BAR_ADD( ToolsMenu( p_intf, mi, adv_controls_enabled,
188                         visual_selector_enabled ), qtr("Tools") );
189     BAR_DADD( VideoMenu( p_intf, NULL ), qtr("Video"), 1 );
190     BAR_DADD( AudioMenu( p_intf, NULL ), qtr("Audio"), 2 );
191     BAR_DADD( NavigMenu( p_intf, NULL ), qtr("Navigation"), 3 );
192
193     BAR_ADD( HelpMenu(), qtr("Help" ) );
194 }
195 QMenu *QVLCMenu::FileMenu()
196 {
197     QMenu *menu = new QMenu();
198     DP_SADD( qtr("Open &File..." ), "", "", openFileDialog(), "Ctrl+O" );
199     DP_SADD( qtr("Open &Disc..." ), "", "", openDiscDialog(), "Ctrl+D" );
200     DP_SADD( qtr("Open &Network..." ), "", "", openNetDialog(), "Ctrl+N" );
201     DP_SADD( qtr("Open &Capture Device..." ), "", "", openCaptureDialog(),
202             "Ctrl+C" );
203     menu->addSeparator();
204     DP_SADD( qtr("&Streaming..."), "", "", openThenStreamingDialogs(), "Ctrl+S" );
205     menu->addSeparator();
206     DP_SADD( qtr("&Quit") , "", "", quit(), "Ctrl+Q");
207     return menu;
208 }
209
210 QMenu *QVLCMenu::PlaylistMenu( MainInterface *mi, intf_thread_t *p_intf )
211 {
212     QMenu *menu = new QMenu();
213     menu->addMenu( SDMenu( p_intf ) );
214     menu->addSeparator();
215
216     DP_SADD( qtr(I_PL_LOAD), "", "", openPlaylist(), "Ctrl+L" );
217     DP_SADD( qtr(I_PL_SAVE), "", "", savePlaylist(), "Ctrl+K" );
218     menu->addSeparator();
219     menu->addAction( qtr("Undock from interface"), mi,
220                      SLOT( undockPlaylist() ), qtr("Ctrl+U") );
221     return menu;
222 }
223
224 QMenu *QVLCMenu::ToolsMenu( intf_thread_t *p_intf, MainInterface *mi,
225                             bool adv_controls_enabled,
226                             bool visual_selector_enabled, bool with_intf )
227 {
228     QMenu *menu = new QMenu();
229     if( with_intf )
230     {
231         QMenu *intfmenu = InterfacesMenu( p_intf, NULL );
232         intfmenu->setTitle( qtr("Interfaces" ) );
233         menu->addMenu( intfmenu );
234         menu->addSeparator();
235     }
236     DP_SADD( qtr(I_MENU_MSG), "", "", messagesDialog(), "Ctrl+M" );
237     DP_SADD( qtr(I_MENU_INFO) , "", "", mediaInfoDialog(), "Ctrl+J" );
238     DP_SADD( qtr(I_MENU_CODECINFO) , "", "", mediaCodecDialog(), "Ctrl+I" );
239     menu->addSeparator();
240     if( mi )
241     {
242         QAction *adv = menu->addAction( qtr("Advanced controls" ),
243                                         mi, SLOT( advanced() ) );
244         adv->setCheckable( true );
245         if( adv_controls_enabled ) adv->setChecked( true );
246 #if 0
247         adv = menu->addAction( qtr("Visualizations selector" ),
248                                mi, SLOT( visual() ) );
249         adv->setCheckable( true );
250         if( visual_selector_enabled ) adv->setChecked( true );
251 #endif
252     }
253     DP_SADD( qtr(I_MENU_EXT), "","",extendedDialog(), "Ctrl+E" );
254     DP_SADD( qtr(I_MENU_GOTOTIME), "","",gotoTimeDialog(), "Ctrl+T" );
255     menu->addSeparator();
256     DP_SADD( qtr("Preferences"), "", "", prefsDialog(), "Ctrl+P" );
257     return menu;
258 }
259
260 QMenu *QVLCMenu::InterfacesMenu( intf_thread_t *p_intf, QMenu *current )
261 {
262     vector<int> objects;
263     vector<const char *> varnames;
264     /** \todo add "switch to XXX" */
265     varnames.push_back( "intf-add" );
266     objects.push_back( p_intf->i_object_id );
267
268     QMenu *menu = Populate( p_intf, current, varnames, objects );
269
270     if( !p_intf->pf_show_dialog )
271     {
272         menu->addSeparator();
273         menu->addAction( qtr("Switch to skins"), THEDP, SLOT(switchToSkins()),
274                  qtr("Ctrl+Z") );
275     }
276
277     CONNECT( menu, aboutToShow(), THEDP->menusUpdateMapper, map() );
278     THEDP->menusUpdateMapper->setMapping( menu, 4 );
279     return menu;
280 }
281
282 QMenu *QVLCMenu::AudioMenu( intf_thread_t *p_intf, QMenu * current )
283 {
284     vector<int> objects;
285     vector<const char *> varnames;
286
287     vlc_object_t *p_object = (vlc_object_t *)vlc_object_find( p_intf,
288                                         VLC_OBJECT_INPUT, FIND_ANYWHERE );
289     if( p_object != NULL )
290     {
291         PUSH_VAR( "audio-es" );
292         vlc_object_release( p_object );
293     }
294
295     p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_AOUT,
296                                                 FIND_ANYWHERE );
297     if( p_object )
298     {
299         AudioAutoMenuBuilder( p_object, objects, varnames );
300         vlc_object_release( p_object );
301     }
302     return Populate( p_intf, current, varnames, objects );
303 }
304
305
306 QMenu *QVLCMenu::VideoMenu( intf_thread_t *p_intf, QMenu *current )
307 {
308     vlc_object_t *p_object;
309     vector<int> objects;
310     vector<const char *> varnames;
311
312     p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
313                                                 FIND_ANYWHERE );
314     if( p_object != NULL )
315     {
316         PUSH_VAR( "video-es" );
317         PUSH_VAR( "spu-es" );
318         vlc_object_release( p_object );
319     }
320
321     p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_VOUT,
322                                                 FIND_ANYWHERE );
323     if( p_object != NULL )
324     {
325         VideoAutoMenuBuilder( p_object, objects, varnames );
326         vlc_object_release( p_object );
327     }
328     return Populate( p_intf, current, varnames, objects );
329 }
330
331 QMenu *QVLCMenu::NavigMenu( intf_thread_t *p_intf, QMenu *current )
332 {
333     vlc_object_t *p_object;
334     vector<int> objects;
335     vector<const char *> varnames;
336
337     /* FIXME */
338     p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
339                                                 FIND_ANYWHERE );
340     if( p_object != NULL )
341     {
342         InputAutoMenuBuilder( p_object, objects, varnames );
343         PUSH_VAR( "prev-title"); PUSH_VAR ( "next-title" );
344         PUSH_VAR( "prev-chapter"); PUSH_VAR( "next-chapter" );
345         vlc_object_release( p_object );
346     }
347     return Populate( p_intf, current, varnames, objects );
348 }
349
350 QMenu *QVLCMenu::SDMenu( intf_thread_t *p_intf )
351 {
352     QMenu *menu = new QMenu();
353     menu->setTitle( qtr(I_PL_SD) );
354     vlc_list_t *p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE,
355                                         FIND_ANYWHERE );
356     int i_num = 0;
357     for( int i_index = 0 ; i_index < p_list->i_count; i_index++ )
358     {
359         module_t * p_parser = (module_t *)p_list->p_values[i_index].p_object ;
360         if( !strcmp( p_parser->psz_capability, "services_discovery" ) )
361             i_num++;
362     }
363     for( int i_index = 0 ; i_index < p_list->i_count; i_index++ )
364     {
365         module_t * p_parser = (module_t *)p_list->p_values[i_index].p_object;
366         if( !strcmp( p_parser->psz_capability, "services_discovery" ) )
367         {
368             QAction *a = new QAction( qfu( p_parser->psz_longname ), menu );
369             a->setCheckable( true );
370             /* hack to handle submodules properly */
371             int i = -1;
372             while( p_parser->pp_shortcuts[++i] != NULL );
373             i--;
374             if( playlist_IsServicesDiscoveryLoaded( THEPL,
375                  i>=0?p_parser->pp_shortcuts[i] : p_parser->psz_object_name ) )
376             {
377                 a->setChecked( true );
378             }
379             CONNECT( a , triggered(), THEDP->SDMapper, map() );
380             THEDP->SDMapper->setMapping( a, i>=0? p_parser->pp_shortcuts[i] :
381                                                   p_parser->psz_object_name );
382             menu->addAction( a );
383         }
384     }
385     vlc_list_release( p_list );
386     return menu;
387 }
388
389 QMenu *QVLCMenu::HelpMenu()
390 {
391     QMenu *menu = new QMenu();
392     DP_SADD( qtr("Help") , "", "", helpDialog(), "F1" );
393     menu->addSeparator();
394     DP_SADD( qtr(I_MENU_ABOUT), "", "", aboutDialog(), "Ctrl+Shift+F1");
395     return menu;
396 }
397
398
399 /*****************************************************************************
400  * Popup menus
401  *****************************************************************************/
402 #define POPUP_BOILERPLATE \
403     unsigned int i_last_separator = 0; \
404     vector<int> objects; \
405     vector<const char *> varnames; \
406     input_thread_t *p_input = THEMIM->getInput();
407
408 #define CREATE_POPUP \
409     QMenu *menu = new QMenu(); \
410     Populate( p_intf, menu, varnames, objects ); \
411     p_intf->p_sys->p_popup_menu = menu; \
412     menu->popup( QCursor::pos() ); \
413     p_intf->p_sys->p_popup_menu = NULL; \
414     i_last_separator = 0;
415
416 #define POPUP_STATIC_ENTRIES \
417     vlc_value_t val; \
418     if( p_input ) \
419     { \
420         var_Get( p_input, "state", &val ); \
421         if( val.i_int == PAUSE_S ) \
422             MIM_SADD( qtr("Play"), "", "", togglePlayPause() ) \
423         else \
424             MIM_SADD( qtr("Pause"), "", "", togglePlayPause() ) \
425     } \
426     else if( THEPL->items.i_size && THEPL->i_enabled ) \
427         MIM_SADD( qtr("Play"), "", "", togglePlayPause() );\
428     \
429     MIM_SADD( qtr("Stop"), "", "", stop() ); \
430     MIM_SADD( qtr("Previous"), "", "", prev() ); \
431     MIM_SADD( qtr("Next"), "", "", next() ); \
432     menu->addSeparator(); \
433     QMenu *intfmenu = InterfacesMenu( p_intf, NULL ); \
434     intfmenu->setTitle( qtr("Interfaces" ) ); \
435     menu->addMenu( intfmenu ); \
436     \
437     QMenu *toolsmenu = ToolsMenu( p_intf, NULL, false, false ); \
438     toolsmenu->setTitle( qtr("Tools" ) ); \
439     menu->addMenu( toolsmenu ); \
440     DP_SADD( qtr("Quit"), "", "", quit() , NULL );
441
442 void QVLCMenu::VideoPopupMenu( intf_thread_t *p_intf )
443 {
444     POPUP_BOILERPLATE;
445     if( p_input )
446     {
447         vlc_object_yield( p_input );
448         varnames.push_back( "video-es" );
449         objects.push_back( p_input->i_object_id );
450         varnames.push_back( "spu-es" );
451         objects.push_back( p_input->i_object_id );
452         vlc_object_t *p_vout = (vlc_object_t *)vlc_object_find( p_input,
453                                                 VLC_OBJECT_VOUT, FIND_CHILD );
454         if( p_vout )
455         {
456             VideoAutoMenuBuilder( p_vout, objects, varnames );
457             vlc_object_release( p_vout );
458         }
459         vlc_object_release( p_input );
460     }
461     CREATE_POPUP;
462 }
463
464 void QVLCMenu::AudioPopupMenu( intf_thread_t *p_intf )
465 {
466     POPUP_BOILERPLATE;
467     if( p_input )
468     {
469         vlc_object_yield( p_input );
470         varnames.push_back( "audio-es" );
471         objects.push_back( p_input->i_object_id );
472         vlc_object_t *p_aout = (vlc_object_t *)vlc_object_find( p_input,
473                                              VLC_OBJECT_AOUT, FIND_ANYWHERE );
474         if( p_aout )
475         {
476             AudioAutoMenuBuilder( p_aout, objects, varnames );
477             vlc_object_release( p_aout );
478         }
479         vlc_object_release( p_input );
480     }
481     CREATE_POPUP;
482 }
483
484 /* Navigation stuff, and general */
485 void QVLCMenu::MiscPopupMenu( intf_thread_t *p_intf )
486 {
487     POPUP_BOILERPLATE;
488     if( p_input )
489     {
490         vlc_object_yield( p_input );
491         varnames.push_back( "audio-es" );
492         InputAutoMenuBuilder( VLC_OBJECT(p_input), objects, varnames );
493         PUSH_SEPARATOR;
494     }
495
496     QMenu *menu = new QMenu();
497     Populate( p_intf, menu, varnames, objects );
498     menu->addSeparator();
499     POPUP_STATIC_ENTRIES;
500
501     p_intf->p_sys->p_popup_menu = menu;
502     menu->popup( QCursor::pos() );
503     p_intf->p_sys->p_popup_menu = NULL;
504 }
505
506 void QVLCMenu::PopupMenu( intf_thread_t *p_intf )
507 {
508     POPUP_BOILERPLATE;
509     if( p_input )
510     {
511         vlc_object_yield( p_input );
512         InputAutoMenuBuilder( VLC_OBJECT(p_input), objects, varnames );
513
514         /* Video menu */
515         PUSH_SEPARATOR;
516         varnames.push_back( "video-es" );
517         objects.push_back( p_input->i_object_id );
518         varnames.push_back( "spu-es" );
519         objects.push_back( p_input->i_object_id );
520         vlc_object_t *p_vout = (vlc_object_t *)vlc_object_find( p_input,
521                                                 VLC_OBJECT_VOUT, FIND_CHILD );
522         if( p_vout )
523         {
524             VideoAutoMenuBuilder( p_vout, objects, varnames );
525             vlc_object_release( p_vout );
526         }
527         /* Audio menu */
528         PUSH_SEPARATOR
529         varnames.push_back( "audio-es" );
530         objects.push_back( p_input->i_object_id );
531         vlc_object_t *p_aout = (vlc_object_t *)vlc_object_find( p_input,
532                                              VLC_OBJECT_AOUT, FIND_ANYWHERE );
533         if( p_aout )
534         {
535             AudioAutoMenuBuilder( p_aout, objects, varnames );
536             vlc_object_release( p_aout );
537         }
538     }
539
540     QMenu *menu = new QMenu();
541     Populate( p_intf, menu, varnames, objects );
542     menu->addSeparator();
543     POPUP_STATIC_ENTRIES;
544
545     p_intf->p_sys->p_popup_menu = menu;
546     menu->popup( QCursor::pos() );
547     p_intf->p_sys->p_popup_menu = NULL;
548 }
549
550 #undef PUSH_VAR
551 #undef PUSH_SEPARATOR
552
553 /*************************************************************************
554  * Builders for automenus
555  *************************************************************************/
556 QMenu * QVLCMenu::Populate( intf_thread_t *p_intf, QMenu *current,
557                             vector< const char *> & varnames,
558                             vector<int> & objects, bool append )
559 {
560     QMenu *menu = current;
561     if( !menu )
562         menu = new QMenu();
563     else if( !append )
564         menu->clear();
565
566     currentGroup = NULL;
567
568     vlc_object_t *p_object;
569     vlc_bool_t b_section_empty = VLC_FALSE;
570     int i;
571
572 #define APPEND_EMPTY { QAction *action = menu->addAction( qtr("Empty" ) ); \
573                        action->setEnabled( false ); }
574
575     for( i = 0; i < (int)objects.size() ; i++ )
576     {
577         if( !varnames[i] || !*varnames[i] )
578         {
579             if( b_section_empty )
580                 APPEND_EMPTY;
581             menu->addSeparator();
582             b_section_empty = VLC_TRUE;
583             continue;
584         }
585
586         if( objects[i] == 0  )
587         {
588             /// \bug What is this ?
589             // Append( menu, varnames[i], NULL );
590             b_section_empty = VLC_FALSE;
591             continue;
592         }
593
594         p_object = (vlc_object_t *)vlc_object_get( p_intf,
595                                                    objects[i] );
596         if( p_object == NULL ) continue;
597
598         b_section_empty = VLC_FALSE;
599         /* Ugly specific stuff */
600         if( strstr(varnames[i], "intf-add" ) )
601             CreateItem( menu, varnames[i], p_object, false );
602         else
603             CreateItem( menu, varnames[i], p_object, true );
604         vlc_object_release( p_object );
605     }
606
607     /* Special case for empty menus */
608     if( menu->actions().size() == 0 || b_section_empty )
609         APPEND_EMPTY
610
611     return menu;
612 }
613
614 /*****************************************************************************
615  * Private methods.
616  *****************************************************************************/
617
618 static bool IsMenuEmpty( const char *psz_var, vlc_object_t *p_object,
619                          bool b_root = TRUE )
620 {
621     vlc_value_t val, val_list;
622     int i_type, i_result, i;
623
624     /* Check the type of the object variable */
625     i_type = var_Type( p_object, psz_var );
626
627     /* Check if we want to display the variable */
628     if( !(i_type & VLC_VAR_HASCHOICE) ) return FALSE;
629
630     var_Change( p_object, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
631     if( val.i_int == 0 ) return TRUE;
632
633     if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE )
634     {
635         /* Very evil hack ! intf-switch can have only one value */
636         if( !strcmp( psz_var, "intf-switch" ) ) return FALSE;
637         if( val.i_int == 1 && b_root ) return TRUE;
638         else return FALSE;
639     }
640
641     /* Check children variables in case of VLC_VAR_VARIABLE */
642     if( var_Change( p_object, psz_var, VLC_VAR_GETLIST, &val_list, NULL ) < 0 )
643     {
644         return TRUE;
645     }
646
647     for( i = 0, i_result = TRUE; i < val_list.p_list->i_count; i++ )
648     {
649         if( !IsMenuEmpty( val_list.p_list->p_values[i].psz_string,
650                           p_object, FALSE ) )
651         {
652             i_result = FALSE;
653             break;
654         }
655     }
656
657     /* clean up everything */
658     var_Change( p_object, psz_var, VLC_VAR_FREELIST, &val_list, NULL );
659
660     return i_result;
661 }
662
663 void QVLCMenu::CreateItem( QMenu *menu, const char *psz_var,
664                            vlc_object_t *p_object, bool b_submenu )
665 {
666     vlc_value_t val, text;
667     int i_type;
668
669     /* Check the type of the object variable */
670     i_type = var_Type( p_object, psz_var );
671
672     switch( i_type & VLC_VAR_TYPE )
673     {
674     case VLC_VAR_VOID:
675     case VLC_VAR_BOOL:
676     case VLC_VAR_VARIABLE:
677     case VLC_VAR_STRING:
678     case VLC_VAR_INTEGER:
679     case VLC_VAR_FLOAT:
680         break;
681     default:
682         /* Variable doesn't exist or isn't handled */
683         return;
684     }
685
686     /* Make sure we want to display the variable */
687     if( IsMenuEmpty( psz_var, p_object ) )  return;
688
689     /* Get the descriptive name of the variable */
690     var_Change( p_object, psz_var, VLC_VAR_GETTEXT, &text, NULL );
691
692     if( i_type & VLC_VAR_HASCHOICE )
693     {
694         /* Append choices menu */
695         if( b_submenu )
696         {
697             QMenu *submenu = new QMenu();
698             submenu->setTitle( qfu( text.psz_string ?
699                                     text.psz_string : psz_var ) );
700             if( CreateChoicesMenu( submenu, psz_var, p_object, true ) == 0)
701                 menu->addMenu( submenu );
702         }
703         else
704             CreateChoicesMenu( menu, psz_var, p_object, true );
705         FREENULL( text.psz_string );
706         return;
707     }
708
709 #define TEXT_OR_VAR qfu ( text.psz_string ? text.psz_string : psz_var )
710
711     switch( i_type & VLC_VAR_TYPE )
712     {
713     case VLC_VAR_VOID:
714         var_Get( p_object, psz_var, &val );
715         CreateAndConnect( menu, psz_var, TEXT_OR_VAR, "", ITEM_NORMAL,
716                           p_object->i_object_id, val, i_type );
717         break;
718
719     case VLC_VAR_BOOL:
720         var_Get( p_object, psz_var, &val );
721         val.b_bool = !val.b_bool;
722         CreateAndConnect( menu, psz_var, TEXT_OR_VAR, "", ITEM_CHECK,
723                           p_object->i_object_id, val, i_type, !val.b_bool );
724         break;
725     }
726     FREENULL( text.psz_string );
727 }
728
729
730 int QVLCMenu::CreateChoicesMenu( QMenu *submenu, const char *psz_var,
731                                  vlc_object_t *p_object, bool b_root )
732 {
733     vlc_value_t val, val_list, text_list;
734     int i_type, i;
735
736     /* Check the type of the object variable */
737     i_type = var_Type( p_object, psz_var );
738
739     /* Make sure we want to display the variable */
740     if( IsMenuEmpty( psz_var, p_object, b_root ) ) return VLC_EGENERIC;
741
742     switch( i_type & VLC_VAR_TYPE )
743     {
744     case VLC_VAR_VOID:
745     case VLC_VAR_BOOL:
746     case VLC_VAR_VARIABLE:
747     case VLC_VAR_STRING:
748     case VLC_VAR_INTEGER:
749     case VLC_VAR_FLOAT:
750         break;
751     default:
752         /* Variable doesn't exist or isn't handled */
753         return VLC_EGENERIC;
754     }
755
756     if( var_Change( p_object, psz_var, VLC_VAR_GETLIST,
757                     &val_list, &text_list ) < 0 )
758     {
759         return VLC_EGENERIC;
760     }
761 #define NORMAL_OR_RADIO i_type & VLC_VAR_ISCOMMAND ? ITEM_NORMAL: ITEM_RADIO
762 #define NOTCOMMAND !(i_type & VLC_VAR_ISCOMMAND)
763 #define CURVAL val_list.p_list->p_values[i]
764 #define CURTEXT text_list.p_list->p_values[i].psz_string
765
766     for( i = 0; i < val_list.p_list->i_count; i++ )
767     {
768         vlc_value_t another_val;
769         QString menutext;
770         QMenu *subsubmenu = new QMenu();
771
772         switch( i_type & VLC_VAR_TYPE )
773         {
774         case VLC_VAR_VARIABLE:
775             CreateChoicesMenu( subsubmenu, CURVAL.psz_string, p_object, false );
776             subsubmenu->setTitle( qfu( CURTEXT ? CURTEXT :CURVAL.psz_string ) );
777             submenu->addMenu( subsubmenu );
778             break;
779
780         case VLC_VAR_STRING:
781           var_Get( p_object, psz_var, &val );
782           another_val.psz_string = strdup( CURVAL.psz_string );
783           menutext = qfu( CURTEXT ? CURTEXT : another_val.psz_string );
784           CreateAndConnect( submenu, psz_var, menutext, "", NORMAL_OR_RADIO,
785                             p_object->i_object_id, another_val, i_type,
786                             NOTCOMMAND && val.psz_string &&
787                             !strcmp( val.psz_string, CURVAL.psz_string ) );
788
789           if( val.psz_string ) free( val.psz_string );
790           break;
791
792         case VLC_VAR_INTEGER:
793           var_Get( p_object, psz_var, &val );
794           if( CURTEXT ) menutext = qfu( CURTEXT );
795           else menutext.sprintf( "%d", CURVAL.i_int);
796           CreateAndConnect( submenu, psz_var, menutext, "", NORMAL_OR_RADIO,
797                             p_object->i_object_id, CURVAL, i_type,
798                             NOTCOMMAND && CURVAL.i_int == val.i_int );
799           break;
800
801         case VLC_VAR_FLOAT:
802           var_Get( p_object, psz_var, &val );
803           if( CURTEXT ) menutext = qfu( CURTEXT );
804           else menutext.sprintf( "%.2f", CURVAL.f_float );
805           CreateAndConnect( submenu, psz_var, menutext, "", NORMAL_OR_RADIO,
806                             p_object->i_object_id, CURVAL, i_type,
807                             NOTCOMMAND && CURVAL.f_float == val.f_float );
808           break;
809
810         default:
811           break;
812         }
813     }
814     currentGroup = NULL;
815
816     /* clean up everything */
817     var_Change( p_object, psz_var, VLC_VAR_FREELIST, &val_list, &text_list );
818
819 #undef NORMAL_OR_RADIO
820 #undef NOTCOMMAND
821 #undef CURVAL
822 #undef CURTEXT
823     return VLC_SUCCESS;
824 }
825
826 void QVLCMenu::CreateAndConnect( QMenu *menu, const char *psz_var,
827                                  QString text, QString help,
828                                  int i_item_type, int i_object_id,
829                                  vlc_value_t val, int i_val_type,
830                                  bool checked )
831 {
832     QAction *action = new QAction( text, menu );
833     action->setText( text );
834     action->setToolTip( help );
835
836     if( i_item_type == ITEM_CHECK )
837     {
838         action->setCheckable( true );
839     }
840     else if( i_item_type == ITEM_RADIO )
841     {
842         action->setCheckable( true );
843         if( !currentGroup )
844             currentGroup = new QActionGroup(menu);
845         currentGroup->addAction( action );
846     }
847
848     if( checked )
849     {
850         action->setChecked( true );
851     }
852     MenuItemData *itemData = new MenuItemData( i_object_id, i_val_type,
853                                                val, psz_var );
854     CONNECT( action, triggered(), THEDP->menusMapper, map() );
855     THEDP->menusMapper->setMapping( action, itemData );
856     menu->addAction( action );
857 }
858
859 void QVLCMenu::DoAction( intf_thread_t *p_intf, QObject *data )
860 {
861     MenuItemData *itemData = qobject_cast<MenuItemData *>(data);
862     vlc_object_t *p_object = (vlc_object_t *)vlc_object_get( p_intf,
863                                            itemData->i_object_id );
864     if( p_object == NULL ) return;
865
866     var_Set( p_object, itemData->psz_var, itemData->val );
867     vlc_object_release( p_object );
868 }