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