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