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