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