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