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