]> git.sesse.net Git - vlc/blob - modules/gui/qt4/menus.cpp
df69127cbf140e3793c864e0f1860318075ddf22
[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+A" );
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+G" );
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     }
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     MIM_SADD( qtr("Stop"), "", "", stop() ); \
419     MIM_SADD( qtr("Previous"), "", "", prev() ); \
420     MIM_SADD( qtr("Next"), "", "", next() ); \
421     if( p_input ) \
422     { \
423         var_Get( p_input, "state", &val ); \
424         if( val.i_int == PAUSE_S ) \
425             MIM_SADD( qtr("Play"), "", "", togglePlayPause() ) \
426         else \
427             MIM_SADD( qtr("Pause"), "", "", togglePlayPause() ) \
428     } \
429     else if( THEPL->items.i_size && THEPL->i_enabled ) \
430         MIM_SADD( qtr("Play"), "", "", togglePlayPause() ) \
431     \
432     QMenu *intfmenu = InterfacesMenu( p_intf, NULL ); \
433     intfmenu->setTitle( qtr("Interfaces" ) ); \
434     menu->addMenu( intfmenu ); \
435     \
436     QMenu *toolsmenu = ToolsMenu( p_intf, NULL, false, false ); \
437     toolsmenu->setTitle( qtr("Tools" ) ); \
438     menu->addMenu( toolsmenu ); \
439
440 void QVLCMenu::VideoPopupMenu( intf_thread_t *p_intf )
441 {
442     POPUP_BOILERPLATE;
443     if( p_input )
444     {
445         vlc_object_yield( p_input );
446         varnames.push_back( "video-es" );
447         objects.push_back( p_input->i_object_id );
448         varnames.push_back( "spu-es" );
449         objects.push_back( p_input->i_object_id );
450         vlc_object_t *p_vout = (vlc_object_t *)vlc_object_find( p_input,
451                                                 VLC_OBJECT_VOUT, FIND_CHILD );
452         if( p_vout )
453         {
454             VideoAutoMenuBuilder( p_vout, objects, varnames );
455             vlc_object_release( p_vout );
456         }
457         vlc_object_release( p_input );
458     }
459     CREATE_POPUP;
460 }
461
462 void QVLCMenu::AudioPopupMenu( intf_thread_t *p_intf )
463 {
464     POPUP_BOILERPLATE;
465     if( p_input )
466     {
467         vlc_object_yield( p_input );
468         varnames.push_back( "audio-es" );
469         objects.push_back( p_input->i_object_id );
470         vlc_object_t *p_aout = (vlc_object_t *)vlc_object_find( p_input,
471                                              VLC_OBJECT_AOUT, FIND_ANYWHERE );
472         if( p_aout )
473         {
474             AudioAutoMenuBuilder( p_aout, objects, varnames );
475             vlc_object_release( p_aout );
476         }
477         vlc_object_release( p_input );
478     }
479     CREATE_POPUP;
480 }
481
482 /* Navigation stuff, and general */
483 void QVLCMenu::MiscPopupMenu( intf_thread_t *p_intf )
484 {
485     POPUP_BOILERPLATE;
486     if( p_input )
487     {
488         vlc_object_yield( p_input );
489         varnames.push_back( "audio-es" );
490         InputAutoMenuBuilder( VLC_OBJECT(p_input), objects, varnames );
491         PUSH_SEPARATOR;
492     }
493
494     QMenu *menu = new QMenu();
495     Populate( p_intf, menu, varnames, objects );
496     menu->addSeparator();
497     POPUP_STATIC_ENTRIES;
498
499     p_intf->p_sys->p_popup_menu = menu;
500     menu->popup( QCursor::pos() );
501     p_intf->p_sys->p_popup_menu = NULL;
502 }
503
504 void QVLCMenu::PopupMenu( intf_thread_t *p_intf )
505 {
506     POPUP_BOILERPLATE;
507     if( p_input )
508     {
509         vlc_object_yield( p_input );
510         InputAutoMenuBuilder( VLC_OBJECT(p_input), objects, varnames );
511
512         /* Video menu */
513         PUSH_SEPARATOR;
514         varnames.push_back( "video-es" );
515         objects.push_back( p_input->i_object_id );
516         varnames.push_back( "spu-es" );
517         objects.push_back( p_input->i_object_id );
518         vlc_object_t *p_vout = (vlc_object_t *)vlc_object_find( p_input,
519                                                 VLC_OBJECT_VOUT, FIND_CHILD );
520         if( p_vout )
521         {
522             VideoAutoMenuBuilder( p_vout, objects, varnames );
523             vlc_object_release( p_vout );
524         }
525         /* Audio menu */
526         PUSH_SEPARATOR
527         varnames.push_back( "audio-es" );
528         objects.push_back( p_input->i_object_id );
529         vlc_object_t *p_aout = (vlc_object_t *)vlc_object_find( p_input,
530                                              VLC_OBJECT_AOUT, FIND_ANYWHERE );
531         if( p_aout )
532         {
533             AudioAutoMenuBuilder( p_aout, objects, varnames );
534             vlc_object_release( p_aout );
535         }
536     }
537
538     QMenu *menu = new QMenu();
539     Populate( p_intf, menu, varnames, objects );
540     menu->addSeparator();
541     POPUP_STATIC_ENTRIES;
542
543     p_intf->p_sys->p_popup_menu = menu;
544     menu->popup( QCursor::pos() );
545     p_intf->p_sys->p_popup_menu = NULL;
546 }
547
548 #undef PUSH_VAR
549 #undef PUSH_SEPARATOR
550
551 /*************************************************************************
552  * Builders for automenus
553  *************************************************************************/
554 QMenu * QVLCMenu::Populate( intf_thread_t *p_intf, QMenu *current,
555                             vector< const char *> & varnames,
556                             vector<int> & objects, bool append )
557 {
558     QMenu *menu = current;
559     if( !menu )
560         menu = new QMenu();
561     else if( !append )
562         menu->clear();
563
564     currentGroup = NULL;
565
566     vlc_object_t *p_object;
567     vlc_bool_t b_section_empty = VLC_FALSE;
568     int i;
569
570 #define APPEND_EMPTY { QAction *action = menu->addAction( qtr("Empty" ) ); \
571                        action->setEnabled( false ); }
572
573     for( i = 0; i < (int)objects.size() ; i++ )
574     {
575         if( !varnames[i] || !*varnames[i] )
576         {
577             if( b_section_empty )
578                 APPEND_EMPTY;
579             menu->addSeparator();
580             b_section_empty = VLC_TRUE;
581             continue;
582         }
583
584         if( objects[i] == 0  )
585         {
586             /// \bug What is this ?
587             // Append( menu, varnames[i], NULL );
588             b_section_empty = VLC_FALSE;
589             continue;
590         }
591
592         p_object = (vlc_object_t *)vlc_object_get( p_intf,
593                                                    objects[i] );
594         if( p_object == NULL ) continue;
595
596         b_section_empty = VLC_FALSE;
597         /* Ugly specific stuff */
598         if( strstr(varnames[i], "intf-add" ) )
599             CreateItem( menu, varnames[i], p_object, false );
600         else
601             CreateItem( menu, varnames[i], p_object, true );
602         vlc_object_release( p_object );
603     }
604
605     /* Special case for empty menus */
606     if( menu->actions().size() == 0 || b_section_empty )
607         APPEND_EMPTY
608
609     return menu;
610 }
611
612 /*****************************************************************************
613  * Private methods.
614  *****************************************************************************/
615
616 static bool IsMenuEmpty( const char *psz_var, vlc_object_t *p_object,
617                          bool b_root = TRUE )
618 {
619     vlc_value_t val, val_list;
620     int i_type, i_result, i;
621
622     /* Check the type of the object variable */
623     i_type = var_Type( p_object, psz_var );
624
625     /* Check if we want to display the variable */
626     if( !(i_type & VLC_VAR_HASCHOICE) ) return FALSE;
627
628     var_Change( p_object, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
629     if( val.i_int == 0 ) return TRUE;
630
631     if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE )
632     {
633         /* Very evil hack ! intf-switch can have only one value */
634         if( !strcmp( psz_var, "intf-switch" ) ) return FALSE;
635         if( val.i_int == 1 && b_root ) return TRUE;
636         else return FALSE;
637     }
638
639     /* Check children variables in case of VLC_VAR_VARIABLE */
640     if( var_Change( p_object, psz_var, VLC_VAR_GETLIST, &val_list, NULL ) < 0 )
641     {
642         return TRUE;
643     }
644
645     for( i = 0, i_result = TRUE; i < val_list.p_list->i_count; i++ )
646     {
647         if( !IsMenuEmpty( val_list.p_list->p_values[i].psz_string,
648                           p_object, FALSE ) )
649         {
650             i_result = FALSE;
651             break;
652         }
653     }
654
655     /* clean up everything */
656     var_Change( p_object, psz_var, VLC_VAR_FREELIST, &val_list, NULL );
657
658     return i_result;
659 }
660
661 void QVLCMenu::CreateItem( QMenu *menu, const char *psz_var,
662                            vlc_object_t *p_object, bool b_submenu )
663 {
664     vlc_value_t val, text;
665     int i_type;
666
667     /* Check the type of the object variable */
668     i_type = var_Type( p_object, psz_var );
669
670     switch( i_type & VLC_VAR_TYPE )
671     {
672     case VLC_VAR_VOID:
673     case VLC_VAR_BOOL:
674     case VLC_VAR_VARIABLE:
675     case VLC_VAR_STRING:
676     case VLC_VAR_INTEGER:
677     case VLC_VAR_FLOAT:
678         break;
679     default:
680         /* Variable doesn't exist or isn't handled */
681         return;
682     }
683
684     /* Make sure we want to display the variable */
685     if( IsMenuEmpty( psz_var, p_object ) )  return;
686
687     /* Get the descriptive name of the variable */
688     var_Change( p_object, psz_var, VLC_VAR_GETTEXT, &text, NULL );
689
690     if( i_type & VLC_VAR_HASCHOICE )
691     {
692         /* Append choices menu */
693         if( b_submenu )
694         {
695             QMenu *submenu = new QMenu();
696             submenu->setTitle( qfu( text.psz_string ?
697                                     text.psz_string : psz_var ) );
698             if( CreateChoicesMenu( submenu, psz_var, p_object, true ) == 0)
699                 menu->addMenu( submenu );
700         }
701         else
702             CreateChoicesMenu( menu, psz_var, p_object, true );
703         FREENULL( text.psz_string );
704         return;
705     }
706
707 #define TEXT_OR_VAR qfu ( text.psz_string ? text.psz_string : psz_var )
708
709     switch( i_type & VLC_VAR_TYPE )
710     {
711     case VLC_VAR_VOID:
712         var_Get( p_object, psz_var, &val );
713         CreateAndConnect( menu, psz_var, TEXT_OR_VAR, "", ITEM_NORMAL,
714                           p_object->i_object_id, val, i_type );
715         break;
716
717     case VLC_VAR_BOOL:
718         var_Get( p_object, psz_var, &val );
719         CreateAndConnect( menu, psz_var, TEXT_OR_VAR, "", ITEM_CHECK,
720                           p_object->i_object_id, val, i_type, val.b_bool );
721         break;
722     }
723     FREENULL( text.psz_string );
724 }
725
726
727 int QVLCMenu::CreateChoicesMenu( QMenu *submenu, const char *psz_var,
728                                  vlc_object_t *p_object, bool b_root )
729 {
730     vlc_value_t val, val_list, text_list;
731     int i_type, i;
732
733     /* Check the type of the object variable */
734     i_type = var_Type( p_object, psz_var );
735
736     /* Make sure we want to display the variable */
737     if( IsMenuEmpty( psz_var, p_object, b_root ) ) return VLC_EGENERIC;
738
739     switch( i_type & VLC_VAR_TYPE )
740     {
741     case VLC_VAR_VOID:
742     case VLC_VAR_BOOL:
743     case VLC_VAR_VARIABLE:
744     case VLC_VAR_STRING:
745     case VLC_VAR_INTEGER:
746     case VLC_VAR_FLOAT:
747         break;
748     default:
749         /* Variable doesn't exist or isn't handled */
750         return VLC_EGENERIC;
751     }
752
753     if( var_Change( p_object, psz_var, VLC_VAR_GETLIST,
754                     &val_list, &text_list ) < 0 )
755     {
756         return VLC_EGENERIC;
757     }
758 #define NORMAL_OR_RADIO i_type & VLC_VAR_ISCOMMAND ? ITEM_NORMAL: ITEM_RADIO
759 #define NOTCOMMAND !(i_type & VLC_VAR_ISCOMMAND)
760 #define CURVAL val_list.p_list->p_values[i]
761 #define CURTEXT text_list.p_list->p_values[i].psz_string
762
763     for( i = 0; i < val_list.p_list->i_count; i++ )
764     {
765         vlc_value_t another_val;
766         QString menutext;
767         QMenu *subsubmenu = new QMenu();
768
769         switch( i_type & VLC_VAR_TYPE )
770         {
771         case VLC_VAR_VARIABLE:
772             CreateChoicesMenu( subsubmenu, CURVAL.psz_string, p_object, false );
773             subsubmenu->setTitle( qfu( CURTEXT ? CURTEXT :CURVAL.psz_string ) );
774             submenu->addMenu( subsubmenu );
775             break;
776
777         case VLC_VAR_STRING:
778           var_Get( p_object, psz_var, &val );
779           another_val.psz_string = strdup( CURVAL.psz_string );
780           menutext = qfu( CURTEXT ? CURTEXT : another_val.psz_string );
781           CreateAndConnect( submenu, psz_var, menutext, "", NORMAL_OR_RADIO,
782                             p_object->i_object_id, another_val, i_type,
783                             NOTCOMMAND && val.psz_string &&
784                             !strcmp( val.psz_string, CURVAL.psz_string ) );
785
786           if( val.psz_string ) free( val.psz_string );
787           break;
788
789         case VLC_VAR_INTEGER:
790           var_Get( p_object, psz_var, &val );
791           if( CURTEXT ) menutext = qfu( CURTEXT );
792           else menutext.sprintf( "%d", CURVAL.i_int);
793           CreateAndConnect( submenu, psz_var, menutext, "", NORMAL_OR_RADIO,
794                             p_object->i_object_id, CURVAL, i_type,
795                             NOTCOMMAND && CURVAL.i_int == val.i_int );
796           break;
797
798         case VLC_VAR_FLOAT:
799           var_Get( p_object, psz_var, &val );
800           if( CURTEXT ) menutext = qfu( CURTEXT );
801           else menutext.sprintf( "%.2f", CURVAL.f_float );
802           CreateAndConnect( submenu, psz_var, menutext, "", NORMAL_OR_RADIO,
803                             p_object->i_object_id, CURVAL, i_type,
804                             NOTCOMMAND && CURVAL.f_float == val.f_float );
805           break;
806
807         default:
808           break;
809         }
810     }
811     currentGroup = NULL;
812
813     /* clean up everything */
814     var_Change( p_object, psz_var, VLC_VAR_FREELIST, &val_list, &text_list );
815
816 #undef NORMAL_OR_RADIO
817 #undef NOTCOMMAND
818 #undef CURVAL
819 #undef CURTEXT
820     return VLC_SUCCESS;
821 }
822
823 void QVLCMenu::CreateAndConnect( QMenu *menu, const char *psz_var,
824                                  QString text, QString help,
825                                  int i_item_type, int i_object_id,
826                                  vlc_value_t val, int i_val_type,
827                                  bool checked )
828 {
829     QAction *action = new QAction( text, menu );
830     action->setText( text );
831     action->setToolTip( help );
832
833     if( i_item_type == ITEM_CHECK )
834     {
835         action->setCheckable( true );
836     }
837     else if( i_item_type == ITEM_RADIO )
838     {
839         action->setCheckable( true );
840         if( !currentGroup )
841             currentGroup = new QActionGroup(menu);
842         currentGroup->addAction( action );
843     }
844
845     if( checked )
846     {
847         action->setChecked( true );
848     }
849     MenuItemData *itemData = new MenuItemData( i_object_id, i_val_type,
850                                                val, psz_var );
851     CONNECT( action, triggered(), THEDP->menusMapper, map() );
852     THEDP->menusMapper->setMapping( action, itemData );
853     menu->addAction( action );
854 }
855
856 void QVLCMenu::DoAction( intf_thread_t *p_intf, QObject *data )
857 {
858     MenuItemData *itemData = qobject_cast<MenuItemData *>(data);
859     vlc_object_t *p_object = (vlc_object_t *)vlc_object_get( p_intf,
860                                            itemData->i_object_id );
861     if( p_object == NULL ) return;
862
863     var_Set( p_object, itemData->psz_var, itemData->val );
864     vlc_object_release( p_object );
865 }