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