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