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