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