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