]> git.sesse.net Git - vlc/blob - modules/gui/qt4/menus.cpp
* Add random/loop buttons
[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 "menus.hpp"
25 #include "dialogs_provider.hpp"
26 #include "input_manager.hpp"
27 #include <QMenu>
28 #include <QMenuBar>
29 #include <QAction>
30 #include <QActionGroup>
31 #include <QSignalMapper>
32
33 enum
34 {
35     ITEM_NORMAL,
36     ITEM_CHECK,
37     ITEM_RADIO
38 };
39
40 static QActionGroup *currentGroup;
41 static char ** pp_sds;
42
43 // Add static entries to menus
44 #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 ) ); } }
45 #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 ) ); } }
46 #define PL_SADD
47
48 /*****************************************************************************
49  * Definitions of variables for the dynamic menus
50  *****************************************************************************/
51 #define PUSH_VAR( var ) varnames.push_back( var ); \
52                         objects.push_back( p_object->i_object_id )
53
54 #define PUSH_SEPARATOR if( objects.size() != i_last_separator ) { \
55                            objects.push_back( 0 ); varnames.push_back( "" ); \
56                            i_last_separator = objects.size(); }
57
58 static int InputAutoMenuBuilder( vlc_object_t *p_object,
59                                  vector<int> &objects,
60                                  vector<const char *> &varnames )
61 {
62     PUSH_VAR( "bookmark");
63     PUSH_VAR( "title" );
64     PUSH_VAR ("chapter" );
65     PUSH_VAR( "program" );
66     PUSH_VAR( "navigation" );
67     PUSH_VAR( "dvd_menus" );
68     return VLC_SUCCESS;
69 }
70
71 static int VideoAutoMenuBuilder( vlc_object_t *p_object,
72                                  vector<int> &objects,
73                                  vector<const char *> &varnames )
74 {
75     PUSH_VAR( "fullscreen" );
76     PUSH_VAR( "zoom" );
77     PUSH_VAR( "deinterlace" );
78     PUSH_VAR( "aspect-ratio" );
79     PUSH_VAR( "crop" );
80     PUSH_VAR( "video-on-top" );
81     PUSH_VAR( "directx-wallpaper" );
82     PUSH_VAR( "video-snapshot" );
83
84     vlc_object_t *p_dec_obj = (vlc_object_t *)vlc_object_find( p_object,
85                                                  VLC_OBJECT_DECODER,
86                                                  FIND_PARENT );
87     if( p_dec_obj != NULL )
88     {
89         PUSH_VAR( "ffmpeg-pp-q" );
90         vlc_object_release( p_dec_obj );
91     }
92     return VLC_SUCCESS;
93 }
94
95 static int AudioAutoMenuBuilder( vlc_object_t *p_object,
96                                  vector<int> &objects,
97                                  vector<const char *> &varnames )
98 {
99     PUSH_VAR( "audio-device" );
100     PUSH_VAR( "audio-channels" );
101     PUSH_VAR( "visual" );
102     PUSH_VAR( "equalizer" );
103     return VLC_SUCCESS;
104 }
105
106 /*****************************************************************************
107  * All normal menus
108  *****************************************************************************/
109
110 #define BAR_ADD( func, title ) { \
111     QMenu *menu = func; menu->setTitle( title  ); bar->addMenu( menu ); }
112
113 #define BAR_DADD( func, title, id ) { \
114     QMenu *menu = func; menu->setTitle( title  ); bar->addMenu( menu ); \
115     MenuFunc *f = new MenuFunc( menu, id ); \
116     connect( menu, SIGNAL( aboutToShow() ), \
117             THEDP->menusUpdateMapper, SLOT(map()) ); \
118     THEDP->menusUpdateMapper->setMapping( menu, f ); }
119
120 void QVLCMenu::createMenuBar( QMenuBar *bar, intf_thread_t *p_intf )
121 {
122     BAR_ADD( FileMenu(), qtr("File") );
123     BAR_ADD( ToolsMenu( p_intf ), qtr("Tools") );
124     BAR_DADD( VideoMenu( p_intf, NULL ), qtr("Video"), 1 );
125     BAR_DADD( AudioMenu( p_intf, NULL ), qtr("Audio"), 2 );
126     BAR_DADD( NavigMenu( p_intf, NULL ), qtr("Navigation"), 3 );
127
128     //    BAR_ADD( HelpMenu(), qtr("Help" ) );
129 }
130
131 void QVLCMenu::createPlMenuBar( QMenuBar *bar, intf_thread_t *p_intf )
132 {
133     QMenu *manageMenu = new QMenu();
134     manageMenu->addAction( "Quick &Add File...", THEDP,
135                            SLOT( simpleAppendDialog() ) );
136     manageMenu->addSeparator();
137     manageMenu->addMenu( SDMenu( p_intf ) );
138
139     bar->addMenu( manageMenu );
140 }
141
142 QMenu *QVLCMenu::SDMenu( intf_thread_t *p_intf )
143 {
144     QMenu *menu = new QMenu();
145     menu->setTitle( qtr( "Services Discovery" ) );
146     playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
147                                             VLC_OBJECT_PLAYLIST,
148                                             FIND_ANYWHERE );
149     assert( p_playlist );
150     vlc_list_t *p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE,
151                                         FIND_ANYWHERE );
152     int i_num = 0;
153     for( int i_index = 0 ; i_index < p_list->i_count; i_index++ )
154     {
155         module_t * p_parser = (module_t *)p_list->p_values[i_index].p_object ;
156         if( !strcmp( p_parser->psz_capability, "services_discovery" ) )
157             i_num++;
158     }
159     if( i_num )  pp_sds = (char **)calloc( i_num, sizeof(void *) );
160     for( int i_index = 0 ; i_index < p_list->i_count; i_index++ )
161     {
162         module_t * p_parser = (module_t *)p_list->p_values[i_index].p_object;
163         if( !strcmp( p_parser->psz_capability, "services_discovery" ) )
164         {
165             QAction *a = menu->addAction(
166                             qfu( p_parser->psz_longname ?
167                                    p_parser->psz_longname :
168                                    ( p_parser->psz_shortname ?
169                                        p_parser->psz_shortname :
170                                        p_parser->psz_object_name ) ) );
171             a->setCheckable( true );
172             /* hack to handle submodules properly */
173             int i = -1;
174             while( p_parser->pp_shortcuts[++i] != NULL );
175             i--;
176             if( playlist_IsServicesDiscoveryLoaded( p_playlist,
177                  i>=0?p_parser->pp_shortcuts[i] : p_parser->psz_object_name ) )
178             {
179                 a->setChecked( true );
180             }
181             pp_sds[i_num++] = i>=0? p_parser->pp_shortcuts[i] :
182                                     p_parser->psz_object_name;
183         }
184     }
185     vlc_list_release( p_list );
186     vlc_object_release( p_playlist );
187     return menu;
188 }
189
190 QMenu *QVLCMenu::FileMenu()
191 {
192     QMenu *menu = new QMenu();
193     DP_SADD( qtr("Quick &Open File...") , "", "", simpleOpenDialog() );
194     DP_SADD( qtr("&Advanced Open..." ), "", "", openDialog() );
195     menu->addSeparator();
196     DP_SADD( qtr("Streaming..."), "", "", streamingDialog() );
197     menu->addSeparator();
198     DP_SADD( qtr("&Quit") , "", "", quit() );
199     return menu;
200 }
201
202 QMenu *QVLCMenu::ToolsMenu( intf_thread_t *p_intf, bool with_intf )
203 {
204     QMenu *menu = new QMenu();
205     if( with_intf )
206     {
207         QMenu *intfmenu = InterfacesMenu( p_intf, NULL );
208         intfmenu->setTitle( qtr("Interfaces" ) );
209         menu->addMenu( intfmenu );
210         /** \todo ADD EXT GUI HERE */
211         menu->addSeparator();
212     }
213     DP_SADD( qtr("Messages" ), "", "", messagesDialog() );
214     DP_SADD( qtr("Information") , "", "", streaminfoDialog() );
215     DP_SADD( qtr("Bookmarks"), "", "", bookmarksDialog() );
216     menu->addSeparator();
217     DP_SADD( qtr("Preferences"), "", "", prefsDialog() );
218     return menu;
219 }
220
221 QMenu *QVLCMenu::InterfacesMenu( intf_thread_t *p_intf, QMenu *current )
222 {
223     vector<int> objects;
224     vector<const char *> varnames;
225     /** \todo add "switch to XXX" */
226     varnames.push_back( "intf-add" );
227     objects.push_back( p_intf->i_object_id );
228
229     QMenu *menu = Populate( p_intf, current, varnames, objects );
230     connect( menu, SIGNAL( aboutToShow() ),
231              THEDP->menusUpdateMapper, SLOT(map()) );
232     THEDP->menusUpdateMapper->setMapping( menu, 4 );
233
234     return menu;
235 }
236
237 QMenu *QVLCMenu::AudioMenu( intf_thread_t *p_intf, QMenu * current )
238 {
239     vector<int> objects;
240     vector<const char *> varnames;
241
242     vlc_object_t *p_object = (vlc_object_t *)vlc_object_find( p_intf,
243                                         VLC_OBJECT_INPUT, FIND_ANYWHERE );
244     if( p_object != NULL )
245     {
246         PUSH_VAR( "audio-es" );
247         vlc_object_release( p_object );
248     }
249
250     p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_AOUT,
251                                                 FIND_ANYWHERE );
252     if( p_object )
253     {
254         AudioAutoMenuBuilder( p_object, objects, varnames );
255         vlc_object_release( p_object );
256     }
257     return Populate( p_intf, current, varnames, objects );
258 }
259
260
261 QMenu *QVLCMenu::VideoMenu( intf_thread_t *p_intf, QMenu *current )
262 {
263     vlc_object_t *p_object;
264     vector<int> objects;
265     vector<const char *> varnames;
266
267     p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
268                                                 FIND_ANYWHERE );
269     if( p_object != NULL )
270     {
271         PUSH_VAR( "video-es" );
272         PUSH_VAR( "spu-es" );
273         vlc_object_release( p_object );
274     }
275
276     p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_VOUT,
277                                                 FIND_ANYWHERE );
278     if( p_object != NULL )
279     {
280         VideoAutoMenuBuilder( p_object, objects, varnames );
281         vlc_object_release( p_object );
282     }
283     return Populate( p_intf, current, varnames, objects );
284 }
285
286 QMenu *QVLCMenu::NavigMenu( intf_thread_t *p_intf, QMenu *current )
287 {
288     vlc_object_t *p_object;
289     vector<int> objects;
290     vector<const char *> varnames;
291
292     /* FIXME */
293     p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
294                                                 FIND_ANYWHERE );
295     if( p_object != NULL )
296     {
297         InputAutoMenuBuilder( p_object, objects, varnames );
298         PUSH_VAR( "prev-title"); PUSH_VAR ( "next-title" );
299         PUSH_VAR( "prev-chapter"); PUSH_VAR( "next-chapter" );
300         vlc_object_release( p_object );
301     }
302     return Populate( p_intf, current, varnames, objects );
303 }
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         val.b_bool = !val.b_bool;
627         CreateAndConnect( menu, psz_var, TEXT_OR_VAR, "", ITEM_CHECK,
628                           p_object->i_object_id, val, i_type, val.b_bool );
629         break;
630     }
631     FREENULL( text.psz_string );
632 }
633
634
635 int QVLCMenu::CreateChoicesMenu( QMenu *submenu, const char *psz_var, 
636                                  vlc_object_t *p_object, bool b_root )
637 {
638     vlc_value_t val, val_list, text_list;
639     int i_type, i;
640
641     /* Check the type of the object variable */
642     i_type = var_Type( p_object, psz_var );
643
644     /* Make sure we want to display the variable */
645     if( IsMenuEmpty( psz_var, p_object, b_root ) ) return VLC_EGENERIC;
646
647     switch( i_type & VLC_VAR_TYPE )
648     {
649     case VLC_VAR_VOID:
650     case VLC_VAR_BOOL:
651     case VLC_VAR_VARIABLE:
652     case VLC_VAR_STRING:
653     case VLC_VAR_INTEGER:
654     case VLC_VAR_FLOAT:
655         break;
656     default:
657         /* Variable doesn't exist or isn't handled */
658         return VLC_EGENERIC;
659     }
660
661     if( var_Change( p_object, psz_var, VLC_VAR_GETLIST,
662                     &val_list, &text_list ) < 0 )
663     {
664         return VLC_EGENERIC;
665     }
666 #define NORMAL_OR_RADIO i_type & VLC_VAR_ISCOMMAND ? ITEM_NORMAL: ITEM_RADIO
667 #define NOTCOMMAND !(i_type & VLC_VAR_ISCOMMAND)
668 #define CURVAL val_list.p_list->p_values[i]
669 #define CURTEXT text_list.p_list->p_values[i].psz_string
670
671     for( i = 0; i < val_list.p_list->i_count; i++ )
672     {
673         vlc_value_t another_val;
674         QString menutext;
675         QMenu *subsubmenu = new QMenu();
676
677         switch( i_type & VLC_VAR_TYPE )
678         {
679         case VLC_VAR_VARIABLE:
680             CreateChoicesMenu( subsubmenu, CURVAL.psz_string, p_object, false );
681             subsubmenu->setTitle( qfu( CURTEXT ? CURTEXT :CURVAL.psz_string ) );
682             submenu->addMenu( subsubmenu );
683             break;
684
685         case VLC_VAR_STRING:
686           var_Get( p_object, psz_var, &val );
687           another_val.psz_string = strdup( CURVAL.psz_string );
688
689           menutext = qfu( CURTEXT ? CURTEXT : another_val.psz_string );
690           CreateAndConnect( submenu, psz_var, menutext, "", NORMAL_OR_RADIO,
691                             p_object->i_object_id, another_val, i_type,
692                             NOTCOMMAND && val.psz_string &&
693                             !strcmp( val.psz_string, CURVAL.psz_string ) );
694
695           if( val.psz_string ) free( val.psz_string );
696           break;
697
698         case VLC_VAR_INTEGER:
699           var_Get( p_object, psz_var, &val );
700           if( CURTEXT ) menutext = qfu( CURTEXT );
701           else menutext.sprintf( "%d", CURVAL.i_int);
702           CreateAndConnect( submenu, psz_var, menutext, "", NORMAL_OR_RADIO,
703                             p_object->i_object_id, CURVAL, i_type,
704                             NOTCOMMAND && CURVAL.i_int == val.i_int );
705           break;
706
707         case VLC_VAR_FLOAT:
708           var_Get( p_object, psz_var, &val );
709           if( CURTEXT ) menutext = qfu( CURTEXT );
710           else menutext.sprintf( "%.2f", CURVAL.f_float );
711           CreateAndConnect( submenu, psz_var, menutext, "", NORMAL_OR_RADIO,
712                             p_object->i_object_id, CURVAL, i_type,
713                             NOTCOMMAND && CURVAL.f_float == val.f_float );
714           break;
715
716         default:
717           break;
718         }
719     }
720
721     /* clean up everything */
722     var_Change( p_object, psz_var, VLC_VAR_FREELIST, &val_list, &text_list );
723
724 #undef NORMAL_OR_RADIO
725 #undef NOTCOMMAND
726 #undef CURVAL
727 #undef CURTEXT
728     return VLC_SUCCESS;
729 }
730
731 void QVLCMenu::CreateAndConnect( QMenu *menu, const char *psz_var,
732                                  QString text, QString help,
733                                  int i_item_type, int i_object_id,
734                                  vlc_value_t val, int i_val_type,
735                                  bool checked )
736 {
737     QAction *action = new QAction( text, menu );
738     action->setText( text );
739     action->setToolTip( help );
740
741     if( i_item_type == ITEM_CHECK )
742     {
743         action->setCheckable( true );
744         currentGroup = NULL;
745     }
746     else if( i_item_type == ITEM_RADIO )
747     {
748         action->setCheckable( true );
749         if( !currentGroup )
750             currentGroup = new QActionGroup(menu);
751         currentGroup->addAction( action );
752     }
753     else
754         currentGroup = NULL;
755     if( checked ) action->setChecked( true );
756
757     MenuItemData *itemData = new MenuItemData( i_object_id, i_val_type,
758                                                val, psz_var );
759     connect( action, SIGNAL(triggered()), THEDP->menusMapper, SLOT(map()) );
760     THEDP->menusMapper->setMapping( action, itemData );
761     menu->addAction( action );
762 }
763
764 void QVLCMenu::DoAction( intf_thread_t *p_intf, QObject *data )
765 {
766     MenuItemData *itemData = qobject_cast<MenuItemData *>(data);
767     vlc_object_t *p_object = (vlc_object_t *)vlc_object_get( p_intf,
768                                            itemData->i_object_id );
769     if( p_object == NULL ) return;
770
771     var_Set( p_object, itemData->psz_var, itemData->val );
772     vlc_object_release( p_object );
773 }