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