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