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