]> git.sesse.net Git - vlc/blob - modules/gui/qt4/menus.cpp
open directory and playlist
[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( "Directory", THEDP, SLOT( openMLDirectory() ));
150     subML->addAction( "&Advanced add...", THEDP,
151                            SLOT( MLAppendDialog() ) );
152     manageMenu->addMenu( subML );
153     manageMenu->addAction( "Open playlist file", THEDP, SLOT( openPlaylist() ));
154     manageMenu->addSeparator();
155
156 //    manageMenu->addMenu( SDMenu( p_intf ) );
157
158     bar->addMenu( manageMenu );
159 }
160
161 QMenu *QVLCMenu::SDMenu( intf_thread_t *p_intf )
162 {
163     QMenu *menu = new QMenu();
164     menu->setTitle( qtr( "Services Discovery" ) );
165     playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
166                                             VLC_OBJECT_PLAYLIST,
167                                             FIND_ANYWHERE );
168     assert( p_playlist );
169     vlc_list_t *p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE,
170                                         FIND_ANYWHERE );
171     int i_num = 0;
172     for( int i_index = 0 ; i_index < p_list->i_count; i_index++ )
173     {
174         module_t * p_parser = (module_t *)p_list->p_values[i_index].p_object ;
175         if( !strcmp( p_parser->psz_capability, "services_discovery" ) )
176             i_num++;
177     }
178     if( i_num )  pp_sds = (char **)calloc( i_num, sizeof(void *) );
179     for( int i_index = 0 ; i_index < p_list->i_count; i_index++ )
180     {
181         module_t * p_parser = (module_t *)p_list->p_values[i_index].p_object;
182         if( !strcmp( p_parser->psz_capability, "services_discovery" ) )
183         {
184             QAction *a = menu->addAction(
185                             qfu( p_parser->psz_longname ?
186                                    p_parser->psz_longname :
187                                    ( p_parser->psz_shortname ?
188                                        p_parser->psz_shortname :
189                                        p_parser->psz_object_name ) ) );
190             a->setCheckable( true );
191             /* hack to handle submodules properly */
192             int i = -1;
193             while( p_parser->pp_shortcuts[++i] != NULL );
194             i--;
195             if( playlist_IsServicesDiscoveryLoaded( p_playlist,
196                  i>=0?p_parser->pp_shortcuts[i] : p_parser->psz_object_name ) )
197             {
198                 a->setChecked( true );
199             }
200             pp_sds[i_num++] = i>=0? p_parser->pp_shortcuts[i] :
201                                     p_parser->psz_object_name;
202         }
203     }
204     vlc_list_release( p_list );
205     vlc_object_release( p_playlist );
206     return menu;
207 }
208
209 QMenu *QVLCMenu::FileMenu()
210 {
211     QMenu *menu = new QMenu();
212     DP_SADD( qtr("Quick &Open File...") , "", "", simpleOpenDialog() );
213     DP_SADD( qtr("&Advanced Open..." ), "", "", openDialog() );
214     menu->addSeparator();
215     DP_SADD( qtr("Streaming..."), "", "", streamingDialog() );
216     menu->addSeparator();
217     DP_SADD( qtr("&Quit") , "", "", quit() );
218     return menu;
219 }
220
221 QMenu *QVLCMenu::ToolsMenu( intf_thread_t *p_intf, bool with_intf )
222 {
223     QMenu *menu = new QMenu();
224     if( with_intf )
225     {
226         QMenu *intfmenu = InterfacesMenu( p_intf, NULL );
227         intfmenu->setTitle( qtr("Interfaces" ) );
228         menu->addMenu( intfmenu );
229         /** \todo ADD EXT GUI HERE */
230         menu->addSeparator();
231     }
232     DP_SADD( qtr("Messages" ), "", "", messagesDialog() );
233     DP_SADD( qtr("Information") , "", "", streaminfoDialog() );
234     DP_SADD( qtr("Bookmarks"), "", "", bookmarksDialog() );
235     menu->addSeparator();
236     DP_SADD( qtr("Preferences"), "", "", prefsDialog() );
237     return menu;
238 }
239
240 QMenu *QVLCMenu::InterfacesMenu( intf_thread_t *p_intf, QMenu *current )
241 {
242     vector<int> objects;
243     vector<const char *> varnames;
244     /** \todo add "switch to XXX" */
245     varnames.push_back( "intf-add" );
246     objects.push_back( p_intf->i_object_id );
247
248     QMenu *menu = Populate( p_intf, current, varnames, objects );
249     connect( menu, SIGNAL( aboutToShow() ),
250              THEDP->menusUpdateMapper, SLOT(map()) );
251     THEDP->menusUpdateMapper->setMapping( menu, 4 );
252
253     return menu;
254 }
255
256 QMenu *QVLCMenu::AudioMenu( intf_thread_t *p_intf, QMenu * current )
257 {
258     vector<int> objects;
259     vector<const char *> varnames;
260
261     vlc_object_t *p_object = (vlc_object_t *)vlc_object_find( p_intf,
262                                         VLC_OBJECT_INPUT, FIND_ANYWHERE );
263     if( p_object != NULL )
264     {
265         PUSH_VAR( "audio-es" );
266         vlc_object_release( p_object );
267     }
268
269     p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_AOUT,
270                                                 FIND_ANYWHERE );
271     if( p_object )
272     {
273         AudioAutoMenuBuilder( p_object, objects, varnames );
274         vlc_object_release( p_object );
275     }
276     return Populate( p_intf, current, varnames, objects );
277 }
278
279
280 QMenu *QVLCMenu::VideoMenu( intf_thread_t *p_intf, QMenu *current )
281 {
282     vlc_object_t *p_object;
283     vector<int> objects;
284     vector<const char *> varnames;
285
286     p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
287                                                 FIND_ANYWHERE );
288     if( p_object != NULL )
289     {
290         PUSH_VAR( "video-es" );
291         PUSH_VAR( "spu-es" );
292         vlc_object_release( p_object );
293     }
294
295     p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_VOUT,
296                                                 FIND_ANYWHERE );
297     if( p_object != NULL )
298     {
299         VideoAutoMenuBuilder( p_object, objects, varnames );
300         vlc_object_release( p_object );
301     }
302     return Populate( p_intf, current, varnames, objects );
303 }
304
305 QMenu *QVLCMenu::NavigMenu( intf_thread_t *p_intf, QMenu *current )
306 {
307     vlc_object_t *p_object;
308     vector<int> objects;
309     vector<const char *> varnames;
310
311     /* FIXME */
312     p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
313                                                 FIND_ANYWHERE );
314     if( p_object != NULL )
315     {
316         InputAutoMenuBuilder( p_object, objects, varnames );
317         PUSH_VAR( "prev-title"); PUSH_VAR ( "next-title" );
318         PUSH_VAR( "prev-chapter"); PUSH_VAR( "next-chapter" );
319         vlc_object_release( p_object );
320     }
321     return Populate( p_intf, current, varnames, objects );
322 }
323
324
325 /*****************************************************************************
326  * Popup menus
327  *****************************************************************************/
328 #define POPUP_BOILERPLATE \
329     unsigned int i_last_separator = 0; \
330     vector<int> objects; \
331     vector<const char *> varnames; \
332     input_thread_t *p_input = THEMIM->getInput();
333
334 #define CREATE_POPUP \
335     QMenu *menu = new QMenu(); \
336     Populate( p_intf, menu, varnames, objects ); \
337     p_intf->p_sys->p_popup_menu = menu; \
338     menu->popup( QCursor::pos() ); \
339     p_intf->p_sys->p_popup_menu = NULL; \
340     i_last_separator = 0;
341
342 #define POPUP_STATIC_ENTRIES \
343     vlc_value_t val; \
344     MIM_SADD( qtr("Stop"), "", "", stop() ); \
345     MIM_SADD( qtr("Previous"), "", "", prev() ); \
346     MIM_SADD( qtr("Next"), "", "", next() ); \
347     if( p_input ) \
348     { \
349         var_Get( p_input, "state", &val ); \
350         if( val.i_int == PAUSE_S ) \
351             MIM_SADD( qtr("Play"), "", "", togglePlayPause() ) \
352         else \
353             MIM_SADD( qtr("Pause"), "", "", togglePlayPause() ) \
354     } \
355     else if( THEPL->i_size && THEPL->i_enabled ) \
356         MIM_SADD( qtr("Play"), "", "", togglePlayPause() ) \
357     \
358     QMenu *intfmenu = InterfacesMenu( p_intf, NULL ); \
359     intfmenu->setTitle( qtr("Interfaces" ) ); \
360     menu->addMenu( intfmenu ); \
361     \
362     QMenu *toolsmenu = ToolsMenu( p_intf, false ); \
363     toolsmenu->setTitle( qtr("Tools" ) ); \
364     menu->addMenu( toolsmenu ); \
365
366 void QVLCMenu::VideoPopupMenu( intf_thread_t *p_intf )
367 {
368     POPUP_BOILERPLATE;
369     if( p_input )
370     {
371         vlc_object_yield( p_input );
372         varnames.push_back( "video-es" );
373         objects.push_back( p_input->i_object_id );
374         varnames.push_back( "spu-es" );
375         objects.push_back( p_input->i_object_id );
376         vlc_object_t *p_vout = (vlc_object_t *)vlc_object_find( p_input,
377                                                 VLC_OBJECT_VOUT, FIND_CHILD );
378         if( p_vout )
379         {
380             VideoAutoMenuBuilder( p_vout, objects, varnames );
381             vlc_object_release( p_vout );
382         }
383         vlc_object_release( p_input );
384     }
385     CREATE_POPUP;
386 }
387
388 void QVLCMenu::AudioPopupMenu( intf_thread_t *p_intf )
389 {
390     POPUP_BOILERPLATE;
391     if( p_input )
392     {
393         vlc_object_yield( p_input );
394         varnames.push_back( "audio-es" );
395         objects.push_back( p_input->i_object_id );
396         vlc_object_t *p_aout = (vlc_object_t *)vlc_object_find( p_input,
397                                              VLC_OBJECT_AOUT, FIND_ANYWHERE );
398         if( p_aout )
399         {
400             AudioAutoMenuBuilder( p_aout, objects, varnames );
401             vlc_object_release( p_aout );
402         }
403         vlc_object_release( p_input );
404     }
405     CREATE_POPUP;
406 }
407
408 /* Navigation stuff, and general */
409 void QVLCMenu::MiscPopupMenu( intf_thread_t *p_intf )
410 {
411     POPUP_BOILERPLATE;
412     if( p_input )
413     {
414         vlc_object_yield( p_input );
415         varnames.push_back( "audio-es" );
416         InputAutoMenuBuilder( VLC_OBJECT(p_input), objects, varnames );
417         PUSH_SEPARATOR;
418     }
419
420     QMenu *menu = new QMenu();
421     Populate( p_intf, menu, varnames, objects );
422     menu->addSeparator();
423     POPUP_STATIC_ENTRIES;
424
425     p_intf->p_sys->p_popup_menu = menu;
426     menu->popup( QCursor::pos() );
427     p_intf->p_sys->p_popup_menu = NULL;
428 }
429
430 void QVLCMenu::PopupMenu( intf_thread_t *p_intf )
431 {
432     POPUP_BOILERPLATE;
433     if( p_input )
434     {
435         vlc_object_yield( p_input );
436         InputAutoMenuBuilder( VLC_OBJECT(p_input), objects, varnames );
437
438         /* Video menu */
439         PUSH_SEPARATOR;
440         varnames.push_back( "video-es" );
441         objects.push_back( p_input->i_object_id );
442         varnames.push_back( "spu-es" );
443         objects.push_back( p_input->i_object_id );
444         vlc_object_t *p_vout = (vlc_object_t *)vlc_object_find( p_input,
445                                                 VLC_OBJECT_VOUT, FIND_CHILD );
446         if( p_vout )
447         {
448             VideoAutoMenuBuilder( p_vout, objects, varnames );
449             vlc_object_release( p_vout );
450         }
451         /* Audio menu */
452         PUSH_SEPARATOR
453         varnames.push_back( "audio-es" );
454         objects.push_back( p_input->i_object_id );
455         vlc_object_t *p_aout = (vlc_object_t *)vlc_object_find( p_input,
456                                              VLC_OBJECT_AOUT, FIND_ANYWHERE );
457         if( p_aout )
458         {
459             AudioAutoMenuBuilder( p_aout, objects, varnames );
460             vlc_object_release( p_aout );
461         }
462     }
463
464     QMenu *menu = new QMenu();
465     Populate( p_intf, menu, varnames, objects );
466     menu->addSeparator();
467     POPUP_STATIC_ENTRIES;
468
469     p_intf->p_sys->p_popup_menu = menu;
470     menu->popup( QCursor::pos() );
471     p_intf->p_sys->p_popup_menu = NULL;
472 }
473
474 #undef PUSH_VAR
475 #undef PUSH_SEPARATOR
476
477 /*************************************************************************
478  * Builders for automenus
479  *************************************************************************/
480 QMenu * QVLCMenu::Populate( intf_thread_t *p_intf, QMenu *current,
481                             vector< const char *> & varnames,
482                             vector<int> & objects, bool append )
483 {
484     QMenu *menu = current;
485     if( !menu )
486         menu = new QMenu();
487     else if( !append )
488         menu->clear();
489
490     currentGroup = NULL;
491
492     vlc_object_t *p_object;
493     vlc_bool_t b_section_empty = VLC_FALSE;
494     int i;
495
496 #define APPEND_EMPTY { QAction *action = menu->addAction( qtr("Empty" ) ); \
497                        action->setEnabled( false ); }
498
499     for( i = 0; i < (int)objects.size() ; i++ )
500     {
501         if( !varnames[i] || !*varnames[i] )
502         {
503             if( b_section_empty )
504                 APPEND_EMPTY;
505             menu->addSeparator();
506             b_section_empty = VLC_TRUE;
507             continue;
508         }
509
510         if( objects[i] == 0  )
511         {
512             /// \bug What is this ?
513             // Append( menu, varnames[i], NULL );
514             b_section_empty = VLC_FALSE;
515             continue;
516         }
517
518         p_object = (vlc_object_t *)vlc_object_get( p_intf,
519                                                    objects[i] );
520         if( p_object == NULL ) continue;
521
522         b_section_empty = VLC_FALSE;
523         /* Ugly specific stuff */
524         if( strstr(varnames[i], "intf-add" ) )
525             CreateItem( menu, varnames[i], p_object, false );
526         else
527             CreateItem( menu, varnames[i], p_object, true );
528         vlc_object_release( p_object );
529     }
530
531     /* Special case for empty menus */
532     if( menu->actions().size() == 0 || b_section_empty )
533         APPEND_EMPTY
534
535     return menu;
536 }
537
538 /*****************************************************************************
539  * Private methods.
540  *****************************************************************************/
541
542 static bool IsMenuEmpty( const char *psz_var, vlc_object_t *p_object,
543                          bool b_root = TRUE )
544 {
545     vlc_value_t val, val_list;
546     int i_type, i_result, i;
547
548     /* Check the type of the object variable */
549     i_type = var_Type( p_object, psz_var );
550
551     /* Check if we want to display the variable */
552     if( !(i_type & VLC_VAR_HASCHOICE) ) return FALSE;
553
554     var_Change( p_object, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
555     if( val.i_int == 0 ) return TRUE;
556
557     if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE )
558     {
559         /* Very evil hack ! intf-switch can have only one value */
560         if( !strcmp( psz_var, "intf-switch" ) ) return FALSE;
561         if( val.i_int == 1 && b_root ) return TRUE;
562         else return FALSE;
563     }
564
565     /* Check children variables in case of VLC_VAR_VARIABLE */
566     if( var_Change( p_object, psz_var, VLC_VAR_GETLIST, &val_list, NULL ) < 0 )
567     {
568         return TRUE;
569     }
570
571     for( i = 0, i_result = TRUE; i < val_list.p_list->i_count; i++ )
572     {
573         if( !IsMenuEmpty( val_list.p_list->p_values[i].psz_string,
574                           p_object, FALSE ) )
575         {
576             i_result = FALSE;
577             break;
578         }
579     }
580
581     /* clean up everything */
582     var_Change( p_object, psz_var, VLC_VAR_FREELIST, &val_list, NULL );
583
584     return i_result;
585 }
586
587 void QVLCMenu::CreateItem( QMenu *menu, const char *psz_var,
588                            vlc_object_t *p_object, bool b_submenu )
589 {
590     vlc_value_t val, text;
591     int i_type;
592
593     /* Check the type of the object variable */
594     i_type = var_Type( p_object, psz_var );
595
596     switch( i_type & VLC_VAR_TYPE )
597     {
598     case VLC_VAR_VOID:
599     case VLC_VAR_BOOL:
600     case VLC_VAR_VARIABLE:
601     case VLC_VAR_STRING:
602     case VLC_VAR_INTEGER:
603     case VLC_VAR_FLOAT:
604         break;
605     default:
606         /* Variable doesn't exist or isn't handled */
607         return;
608     }
609
610     /* Make sure we want to display the variable */
611     if( IsMenuEmpty( psz_var, p_object ) )  return;
612
613     /* Get the descriptive name of the variable */
614     var_Change( p_object, psz_var, VLC_VAR_GETTEXT, &text, NULL );
615
616     if( i_type & VLC_VAR_HASCHOICE )
617     {
618         /* Append choices menu */
619         if( b_submenu )
620         {
621             QMenu *submenu = new QMenu();
622             submenu->setTitle( qfu( text.psz_string ?
623                                     text.psz_string : psz_var ) );
624             if( CreateChoicesMenu( submenu, psz_var, p_object, true ) == 0)
625                 menu->addMenu( submenu );
626         }
627         else
628             CreateChoicesMenu( menu, psz_var, p_object, true );
629         FREENULL( text.psz_string );
630         return;
631     }
632
633 #define TEXT_OR_VAR qfu ( text.psz_string ? text.psz_string : psz_var )
634
635     switch( i_type & VLC_VAR_TYPE )
636     {
637     case VLC_VAR_VOID:
638         var_Get( p_object, psz_var, &val );
639         CreateAndConnect( menu, psz_var, TEXT_OR_VAR, "", ITEM_NORMAL,
640                           p_object->i_object_id, val, i_type );
641         break;
642
643     case VLC_VAR_BOOL:
644         var_Get( p_object, psz_var, &val );
645         CreateAndConnect( menu, psz_var, TEXT_OR_VAR, "", ITEM_CHECK,
646                           p_object->i_object_id, val, i_type, val.b_bool );
647         break;
648     }
649     FREENULL( text.psz_string );
650 }
651
652
653 int QVLCMenu::CreateChoicesMenu( QMenu *submenu, const char *psz_var,
654                                  vlc_object_t *p_object, bool b_root )
655 {
656     vlc_value_t val, val_list, text_list;
657     int i_type, i;
658
659     /* Check the type of the object variable */
660     i_type = var_Type( p_object, psz_var );
661
662     /* Make sure we want to display the variable */
663     if( IsMenuEmpty( psz_var, p_object, b_root ) ) return VLC_EGENERIC;
664
665     switch( i_type & VLC_VAR_TYPE )
666     {
667     case VLC_VAR_VOID:
668     case VLC_VAR_BOOL:
669     case VLC_VAR_VARIABLE:
670     case VLC_VAR_STRING:
671     case VLC_VAR_INTEGER:
672     case VLC_VAR_FLOAT:
673         break;
674     default:
675         /* Variable doesn't exist or isn't handled */
676         return VLC_EGENERIC;
677     }
678
679     if( var_Change( p_object, psz_var, VLC_VAR_GETLIST,
680                     &val_list, &text_list ) < 0 )
681     {
682         return VLC_EGENERIC;
683     }
684 #define NORMAL_OR_RADIO i_type & VLC_VAR_ISCOMMAND ? ITEM_NORMAL: ITEM_RADIO
685 #define NOTCOMMAND !(i_type & VLC_VAR_ISCOMMAND)
686 #define CURVAL val_list.p_list->p_values[i]
687 #define CURTEXT text_list.p_list->p_values[i].psz_string
688
689     for( i = 0; i < val_list.p_list->i_count; i++ )
690     {
691         vlc_value_t another_val;
692         QString menutext;
693         QMenu *subsubmenu = new QMenu();
694
695         switch( i_type & VLC_VAR_TYPE )
696         {
697         case VLC_VAR_VARIABLE:
698             CreateChoicesMenu( subsubmenu, CURVAL.psz_string, p_object, false );
699             subsubmenu->setTitle( qfu( CURTEXT ? CURTEXT :CURVAL.psz_string ) );
700             submenu->addMenu( subsubmenu );
701             break;
702
703         case VLC_VAR_STRING:
704           var_Get( p_object, psz_var, &val );
705           another_val.psz_string = strdup( CURVAL.psz_string );
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     currentGroup = NULL;
738
739     /* clean up everything */
740     var_Change( p_object, psz_var, VLC_VAR_FREELIST, &val_list, &text_list );
741
742 #undef NORMAL_OR_RADIO
743 #undef NOTCOMMAND
744 #undef CURVAL
745 #undef CURTEXT
746     return VLC_SUCCESS;
747 }
748
749 void QVLCMenu::CreateAndConnect( QMenu *menu, const char *psz_var,
750                                  QString text, QString help,
751                                  int i_item_type, int i_object_id,
752                                  vlc_value_t val, int i_val_type,
753                                  bool checked )
754 {
755     QAction *action = new QAction( text, menu );
756     action->setText( text );
757     action->setToolTip( help );
758
759     if( i_item_type == ITEM_CHECK )
760     {
761         action->setCheckable( true );
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
771     if( checked )
772     {
773         action->setChecked( true );
774     }
775     MenuItemData *itemData = new MenuItemData( i_object_id, i_val_type,
776                                                val, psz_var );
777     connect( action, SIGNAL(triggered()), THEDP->menusMapper, SLOT(map()) );
778     THEDP->menusMapper->setMapping( action, itemData );
779     menu->addAction( action );
780 }
781
782 void QVLCMenu::DoAction( intf_thread_t *p_intf, QObject *data )
783 {
784     MenuItemData *itemData = qobject_cast<MenuItemData *>(data);
785     vlc_object_t *p_object = (vlc_object_t *)vlc_object_get( p_intf,
786                                            itemData->i_object_id );
787     if( p_object == NULL ) return;
788
789     var_Set( p_object, itemData->psz_var, itemData->val );
790     vlc_object_release( p_object );
791 }