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