]> git.sesse.net Git - vlc/blob - modules/gui/qt4/menus.cpp
0a171b5c30edcd622beff69b476ba602f545ce24
[vlc] / modules / gui / qt4 / menus.cpp
1 /*****************************************************************************
2  * menus.cpp : Qt menus
3  *****************************************************************************
4  * Copyright ( C ) 2006-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *          Jean-Baptiste Kempf <jb@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * ( at your option ) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #include <vlc/vlc.h>
26
27 #include <vlc_intf_strings.h>
28
29 #include "main_interface.hpp"
30 #include "menus.hpp"
31 #include "dialogs_provider.hpp"
32 #include "input_manager.hpp"
33
34 #include <QMenu>
35 #include <QMenuBar>
36 #include <QAction>
37 #include <QActionGroup>
38 #include <QSignalMapper>
39 #include <QSystemTrayIcon>
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 void addDPStaticEntry( QMenu *menu,
52                        const QString text,
53                        char *help,
54                        char *icon,
55                        const char *member,
56                        char *shortcut )
57 {
58     if( !EMPTY_STR( icon ) > 0 )
59     {
60         if( !EMPTY_STR( shortcut ) > 0 )
61             menu->addAction( QIcon( icon ), text, THEDP, member, qtr( shortcut ) );
62         else
63             menu->addAction( QIcon( icon ), text, THEDP, member );
64     }
65     else
66     {
67         if( !EMPTY_STR( shortcut ) > 0 )
68             menu->addAction( text, THEDP, member, qtr( shortcut ) );
69         else
70             menu->addAction( text, THEDP, member );
71     }
72 }
73
74 void addMIMStaticEntry( intf_thread_t *p_intf,
75                         QMenu *menu,
76                         const QString text,
77                         char *help,
78                         char *icon,
79                         const char *member )
80 {
81     if( strlen( icon ) > 0 )
82     {
83         QAction *action = menu->addAction( text, THEMIM,  member );
84         action->setIcon( QIcon( icon ) );
85     }
86     else
87     {
88         menu->addAction( text, THEMIM, member );
89     }
90 }
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  * Simple Code
154  *****************************************************************************/
155
156 #define BAR_ADD( func, title ) { \
157     QMenu *menu = func; menu->setTitle( title ); bar->addMenu( menu ); }
158
159 #define BAR_DADD( func, title, id ) { \
160     QMenu *menu = func; menu->setTitle( title ); bar->addMenu( menu ); \
161     MenuFunc *f = new MenuFunc( menu, id ); \
162     CONNECT( menu, aboutToShow(), THEDP->menusUpdateMapper, map() ); \
163     THEDP->menusUpdateMapper->setMapping( menu, f ); }
164
165 /**
166  * Main Menu Bar Creation
167  **/
168 void QVLCMenu::createMenuBar( MainInterface *mi,
169                               intf_thread_t *p_intf,
170                               bool visual_selector_enabled )
171 {
172     QMenuBar *bar = mi->menuBar();
173     BAR_ADD( FileMenu(), qtr( "&Media" ) );
174     BAR_ADD( PlaylistMenu( p_intf, mi ), qtr( "&Playlist" ) );
175     BAR_ADD( ToolsMenu( p_intf, mi, visual_selector_enabled, true ), qtr( "&Tools" ) );
176     BAR_DADD( AudioMenu( p_intf, NULL ), qtr( "&Audio" ), 2 );
177     BAR_DADD( VideoMenu( p_intf, NULL ), qtr( "&Video" ), 1 );
178     BAR_DADD( NavigMenu( p_intf, NULL ), qtr( "&Playback" ), 3 );
179
180     BAR_ADD( HelpMenu(), qtr( "&Help" ) );
181 }
182 #undef BAR_ADD
183 #undef BAR_DADD
184
185 /**
186  * Media ( File ) Menu
187  * Opening, streaming and quit
188  **/
189 QMenu *QVLCMenu::FileMenu()
190 {
191     QMenu *menu = new QMenu();
192
193     addDPStaticEntry( menu, qtr( "&Open File..." ), "",
194         ":/pixmaps/file-asym_16px.png", SLOT( openFileDialog() ), "Ctrl+O" );
195     addDPStaticEntry( menu, qtr( I_OPEN_FOLDER ), "",
196         ":/pixmaps/folder-grey_16px.png", SLOT( PLAppendDir() ), "Ctrl+F" );
197     addDPStaticEntry( menu, qtr( "Open &Disc..." ), "",
198         ":/pixmaps/disc_16px.png", SLOT( openDiscDialog() ), "Ctrl+D" );
199     addDPStaticEntry( menu, qtr( "Open &Network..." ), "",
200         ":/pixmaps/network_16px.png", SLOT( openNetDialog() ), "Ctrl+N" );
201     addDPStaticEntry( menu, qtr( "Open &Capture Device..." ), "",
202         ":/pixmaps/capture-card_16px.png", SLOT( openCaptureDialog() ),
203         "Ctrl+C" );
204     menu->addSeparator();
205
206     addDPStaticEntry( menu, qtr( "&Streaming..." ), "",
207         ":/pixmaps/menus_stream_16px.png", SLOT( openThenStreamingDialogs() ),
208         "Ctrl+S" );
209     addDPStaticEntry( menu, qtr( "Conve&rt / Save..." ), "", "",
210         SLOT( openThenTranscodingDialogs() ), "Ctrl+R" );
211     menu->addSeparator();
212
213     addDPStaticEntry( menu, qtr( "&Quit" ) , "",
214         ":/pixmaps/menus_quit_16px.png", SLOT( quit() ), "Ctrl+Q" );
215     return menu;
216 }
217
218 /* Playlist/MediaLibrary Control */
219 QMenu *QVLCMenu::PlaylistMenu( intf_thread_t *p_intf, MainInterface *mi )
220 {
221     QMenu *menu = new QMenu();
222     menu->addMenu( SDMenu( p_intf ) );
223     menu->addAction( QIcon( ":/pixmaps/playlist_16px.png" ),
224                      qtr( "Show Playlist" ), mi, SLOT( togglePlaylist() ) );
225     menu->addSeparator();
226
227     addDPStaticEntry( menu, qtr( I_PL_LOAD ), "", "", SLOT( openAPlaylist() ),
228         "Ctrl+X" );
229     addDPStaticEntry( menu, qtr( I_PL_SAVE ), "", "", SLOT( saveAPlaylist() ),
230         "Ctrl+Y" );
231     menu->addSeparator();
232     menu->addAction( qtr( "Undock from interface" ), mi,
233                      SLOT( undockPlaylist() ), qtr( "Ctrl+U" ) );
234     return menu;
235 }
236
237 /**
238  * Tools/View Menu
239  * This is kept in the same menu for now, but could change if it gets much
240  * longer.
241  * This menu can be an interface menu but also a right click menu.
242  **/
243 QMenu *QVLCMenu::ToolsMenu( intf_thread_t *p_intf,
244                             MainInterface *mi,
245                             bool visual_selector_enabled,
246                             bool with_intf )
247 {
248     QMenu *menu = new QMenu;
249     if( mi )
250     {
251         menu->addAction( QIcon( ":/pixmaps/playlist_16px.png" ),
252                          qtr( "Playlist..." ), mi, SLOT( togglePlaylist() ),
253                          qtr( "Ctrl+L" ) );
254     }
255     addDPStaticEntry( menu, qtr( I_MENU_EXT ), "",
256         ":/pixmaps/menus_settings_16px.png", SLOT( extendedDialog() ),
257         "Ctrl+E" );
258
259     menu->addSeparator();
260
261     if( with_intf )
262     {
263         QMenu *intfmenu = InterfacesMenu( p_intf, NULL );
264         intfmenu->setTitle( qtr( "Add Interfaces" ) );
265         menu->addMenu( intfmenu );
266         menu->addSeparator();
267     }
268     if( mi )
269     {
270         /* Minimal View */
271         QAction *action=menu->addAction( qtr( "Minimal View..." ), mi,
272                 SLOT( toggleMinimalView() ), qtr( "Ctrl+H" ) );
273         action->setCheckable( true );
274         if( mi->getControlsVisibilityStatus() & CONTROLS_VISIBLE )
275             action->setChecked( true );
276
277         /* Advanced Controls */
278         action = menu->addAction( qtr( "Advanced controls" ), mi,
279                 SLOT( toggleAdvanced() ) );
280         action->setCheckable( true );
281         if( mi->getControlsVisibilityStatus() & CONTROLS_ADVANCED )
282             action->setChecked( true );
283 #if 0 /* For Visualisations. Not yet working */
284         adv = menu->addAction( qtr( "Visualizations selector" ),
285                 mi, SLOT( visual() ) );
286         adv->setCheckable( true );
287         if( visual_selector_enabled ) adv->setChecked( true );
288 #endif
289     }
290
291     menu->addSeparator();
292
293     addDPStaticEntry( menu, qtr( I_MENU_MSG ), "",
294         ":/pixmaps/menus_messages_16px.png", SLOT( messagesDialog() ),
295         "Ctrl+M" );
296     addDPStaticEntry( menu, qtr( I_MENU_INFO ) , "", "",
297         SLOT( mediaInfoDialog() ), "Ctrl+I" );
298     addDPStaticEntry( menu, qtr( I_MENU_CODECINFO ) , "",
299         ":/pixmaps/menus_info_16px.png", SLOT( mediaCodecDialog() ), "Ctrl+J" );
300
301 #if 0 /* Not Implemented yet */
302     addDPStaticEntry( menu, qtr( I_MENU_BOOKMARK ), "","", "bookmarksDialog()", "Ctrl+B" );
303 #endif
304     addDPStaticEntry( menu, qtr( I_MENU_VLM ), "", "", SLOT( vlmDialog() ),
305         "Ctrl+V" );
306
307     menu->addSeparator();
308     addDPStaticEntry( menu, qtr( "Preferences..." ), "",
309         ":/pixmaps/menus_preferences_16px.png", SLOT( prefsDialog() ), "Ctrl+P" );
310     return menu;
311 }
312
313 /**
314  * Interface Sub-Menu, to list extras interface and skins
315  **/
316 QMenu *QVLCMenu::InterfacesMenu( intf_thread_t *p_intf, QMenu *current )
317 {
318     vector<int> objects;
319     vector<const char *> varnames;
320     /** \todo add "switch to XXX" */
321     varnames.push_back( "intf-add" );
322     objects.push_back( p_intf->i_object_id );
323
324     QMenu *menu = Populate( p_intf, current, varnames, objects );
325
326     if( !p_intf->pf_show_dialog )
327     {
328         menu->addSeparator();
329         menu->addAction( qtr( "Switch to skins" ), THEDP, SLOT( switchToSkins() ),
330                 QString( "Ctrl+Z" ) );
331     }
332
333     CONNECT( menu, aboutToShow(), THEDP->menusUpdateMapper, map() );
334     THEDP->menusUpdateMapper->setMapping( menu, 4 );
335     return menu;
336 }
337
338 /**
339  * Main Audio Menu
340  */
341 QMenu *QVLCMenu::AudioMenu( intf_thread_t *p_intf, QMenu * current )
342 {
343     vector<int> objects;
344     vector<const char *> varnames;
345
346     vlc_object_t *p_object = ( vlc_object_t * )vlc_object_find( p_intf,
347             VLC_OBJECT_INPUT, FIND_ANYWHERE );
348     if( p_object != NULL )
349     {
350         PUSH_VAR( "audio-es" );
351         vlc_object_release( p_object );
352     }
353
354     p_object = ( vlc_object_t * )vlc_object_find( p_intf, VLC_OBJECT_AOUT,
355             FIND_ANYWHERE );
356     if( p_object )
357     {
358         AudioAutoMenuBuilder( p_object, objects, varnames );
359         vlc_object_release( p_object );
360     }
361     return Populate( p_intf, current, varnames, objects );
362 }
363
364 /**
365  * Main Video Menu
366  * Subtitles are part of Video.
367  **/
368 QMenu *QVLCMenu::VideoMenu( intf_thread_t *p_intf, QMenu *current )
369 {
370     vlc_object_t *p_object;
371     vector<int> objects;
372     vector<const char *> varnames;
373
374     p_object = ( vlc_object_t * )vlc_object_find( p_intf, VLC_OBJECT_INPUT,
375             FIND_ANYWHERE );
376     if( p_object != NULL )
377     {
378         PUSH_VAR( "video-es" );
379         PUSH_VAR( "spu-es" );
380         vlc_object_release( p_object );
381     }
382
383     p_object = ( vlc_object_t * )vlc_object_find( p_intf, VLC_OBJECT_VOUT,
384             FIND_ANYWHERE );
385     if( p_object != NULL )
386     {
387         VideoAutoMenuBuilder( p_object, objects, varnames );
388         vlc_object_release( p_object );
389     }
390     return Populate( p_intf, current, varnames, objects );
391 }
392
393 /**
394  * Navigation Menu
395  * For DVD, MP4, MOV and other chapter based format
396  **/
397 QMenu *QVLCMenu::NavigMenu( intf_thread_t *p_intf, QMenu *navMenu )
398 {
399     vlc_object_t *p_object;
400     vector<int> objects;
401     vector<const char *> varnames;
402
403     p_object = ( vlc_object_t * )vlc_object_find( p_intf, VLC_OBJECT_INPUT,
404             FIND_ANYWHERE );
405     if( p_object != NULL )
406     {
407         InputAutoMenuBuilder(  p_object, objects, varnames );
408         PUSH_VAR( "prev-title" ); PUSH_VAR ( "next-title" );
409         PUSH_VAR( "prev-chapter" ); PUSH_VAR( "next-chapter" );
410         vlc_object_release( p_object );
411     }
412     navMenu = new QMenu();
413     addDPStaticEntry( navMenu, qtr( I_MENU_GOTOTIME ), "","",
414         SLOT( gotoTimeDialog() ), "Ctrl+T" );
415     navMenu->addSeparator();
416     return Populate( p_intf, navMenu, varnames, objects, true );
417 }
418
419 /**
420  * Service Discovery SubMenu
421  **/
422 QMenu *QVLCMenu::SDMenu( intf_thread_t *p_intf )
423 {
424     QMenu *menu = new QMenu();
425     menu->setTitle( qtr( I_PL_SD ) );
426     char **ppsz_longnames;
427     char **ppsz_names = services_discovery_GetServicesNames( p_intf,
428                                                              &ppsz_longnames );
429     char **ppsz_name = ppsz_names, **ppsz_longname = ppsz_longnames;
430     for( ; *ppsz_name; ppsz_name++, ppsz_longname++ )
431     {
432         QAction *a = new QAction( qfu( *ppsz_longname ), menu );
433         a->setCheckable( true );
434         if( playlist_IsServicesDiscoveryLoaded( THEPL, *ppsz_name ) )
435             a->setChecked( true );
436         CONNECT( a , triggered(), THEDP->SDMapper, map() );
437         THEDP->SDMapper->setMapping( a, QString( *ppsz_name ) );
438         menu->addAction( a );
439
440         if( !strcmp( *ppsz_name, "podcast" ) )
441         {
442             QAction *b = new QAction( qfu( "Configure podcasts..." ), menu );
443             //b->setEnabled( a->isChecked() );
444             menu->addAction( b );
445             CONNECT( b, triggered(), THEDP, podcastConfigureDialog() );
446         }
447         free( *ppsz_name );
448         free( *ppsz_longname );
449     }
450     free( ppsz_names );
451     free( ppsz_longnames );
452     return menu;
453 }
454 /**
455  * Help/About Menu
456 **/
457 QMenu *QVLCMenu::HelpMenu()
458 {
459     QMenu *menu = new QMenu();
460     addDPStaticEntry( menu, qtr( "Help..." ) , "",
461         ":/pixmaps/menus_help_16px.png", SLOT( helpDialog() ), "F1" );
462 #ifdef UPDATE_CHECK
463     addDPStaticEntry( menu, qtr( "Update" ) , "", "", SLOT( updateDialog() ), "");
464 #endif
465     menu->addSeparator();
466     addDPStaticEntry( menu, qtr( I_MENU_ABOUT ), "", "", SLOT( aboutDialog() ),
467         "Ctrl+F1" );
468     return menu;
469 }
470
471
472 /*****************************************************************************
473  * Popup menus - Right Click menus                                           *
474  *****************************************************************************/
475 #define POPUP_BOILERPLATE \
476     unsigned int i_last_separator = 0; \
477     vector<int> objects; \
478     vector<const char *> varnames; \
479     input_thread_t *p_input = THEMIM->getInput();
480
481 #define CREATE_POPUP \
482     Populate( p_intf, menu, varnames, objects ); \
483     p_intf->p_sys->p_popup_menu = menu; \
484     menu->popup( QCursor::pos() ); \
485     p_intf->p_sys->p_popup_menu = NULL; \
486     i_last_separator = 0;
487
488 void QVLCMenu::PopupMenuControlEntries( QMenu *menu,
489                                         intf_thread_t *p_intf,
490                                         input_thread_t *p_input )
491 {
492     if( p_input )
493     {
494         vlc_value_t val;
495         var_Get( p_input, "state", &val );
496         if( val.i_int == PLAYING_S )
497             addMIMStaticEntry( p_intf, menu, qtr( "Pause" ), "",
498                     ":/pixmaps/pause_16px.png", SLOT( togglePlayPause() ) );
499         else
500             addMIMStaticEntry( p_intf, menu, qtr( "Play" ), "",
501                     ":/pixmaps/play_16px.png", SLOT( togglePlayPause() ) );
502     }
503     else if( THEPL->items.i_size && THEPL->i_enabled )
504         addMIMStaticEntry( p_intf, menu, qtr( "Play" ), "",
505                 ":/pixmaps/play_16px.png", SLOT( togglePlayPause() ) );
506
507     addMIMStaticEntry( p_intf, menu, qtr( "Stop" ), "",
508             ":/pixmaps/stop_16px.png", SLOT( stop() ) );
509     addMIMStaticEntry( p_intf, menu, qtr( "Previous" ), "",
510             ":/pixmaps/previous_16px.png", SLOT( prev() ) );
511     addMIMStaticEntry( p_intf, menu, qtr( "Next" ), "",
512             ":/pixmaps/next_16px.png", SLOT( next() ) );
513     }
514
515 void QVLCMenu::PopupMenuStaticEntries( intf_thread_t *p_intf, QMenu *menu )
516 {
517     QMenu *toolsmenu = ToolsMenu( p_intf, NULL, false, true );
518     toolsmenu->setTitle( qtr( "Tools" ) );
519     menu->addMenu( toolsmenu );
520
521     QMenu *openmenu = new QMenu( qtr( "Open" ) );
522     openmenu->addAction( qtr( "Open &File..." ), THEDP,
523                          SLOT( openFileDialog() ) );
524     openmenu->addAction( qtr( "Open &Disc..." ), THEDP,
525                          SLOT( openDiscDialog() ) );
526     openmenu->addAction( qtr( "Open &Network..." ), THEDP,
527                          SLOT( openNetDialog() ) );
528     openmenu->addAction( qtr( "Open &Capture Device..." ), THEDP,
529                          SLOT( openCaptureDialog() ) );
530     menu->addMenu( openmenu );
531
532     menu->addSeparator();
533     QMenu *helpmenu = HelpMenu();
534     helpmenu->setTitle( qtr( "Help" ) );
535     menu->addMenu( helpmenu );
536
537     addDPStaticEntry( menu, qtr( "Quit" ), "", "", SLOT( quit() ) , "Ctrl+Q" );
538 }
539
540 /* Video Tracks and Subtitles tracks */
541 void QVLCMenu::VideoPopupMenu( intf_thread_t *p_intf )
542 {
543     POPUP_BOILERPLATE;
544     if( p_input )
545     {
546         vlc_object_yield( p_input );
547         varnames.push_back( "video-es" );
548         objects.push_back( p_input->i_object_id );
549         varnames.push_back( "spu-es" );
550         objects.push_back( p_input->i_object_id );
551         vlc_object_t *p_vout = ( vlc_object_t * )vlc_object_find( p_input,
552                 VLC_OBJECT_VOUT, FIND_CHILD );
553         if( p_vout )
554         {
555             VideoAutoMenuBuilder( p_vout, objects, varnames );
556             vlc_object_release( p_vout );
557         }
558         vlc_object_release( p_input );
559     }
560     QMenu *menu = new QMenu();
561     CREATE_POPUP;
562 }
563
564 /* Audio Tracks */
565 void QVLCMenu::AudioPopupMenu( intf_thread_t *p_intf )
566 {
567     POPUP_BOILERPLATE;
568     if( p_input )
569     {
570         vlc_object_yield( p_input );
571         varnames.push_back( "audio-es" );
572         objects.push_back( p_input->i_object_id );
573         vlc_object_t *p_aout = ( vlc_object_t * )vlc_object_find( p_input,
574                 VLC_OBJECT_AOUT, FIND_ANYWHERE );
575         if( p_aout )
576         {
577             AudioAutoMenuBuilder( p_aout, objects, varnames );
578             vlc_object_release( p_aout );
579         }
580         vlc_object_release( p_input );
581     }
582     QMenu *menu = new QMenu();
583     CREATE_POPUP;
584 }
585
586 /* Navigation stuff, and general menus ( open ) */
587 void QVLCMenu::MiscPopupMenu( intf_thread_t *p_intf )
588 {
589     vlc_value_t val;
590     POPUP_BOILERPLATE;
591
592     if( p_input )
593     {
594         vlc_object_yield( p_input );
595         varnames.push_back( "audio-es" );
596         InputAutoMenuBuilder( VLC_OBJECT( p_input ), objects, varnames );
597         PUSH_SEPARATOR;
598     }
599
600     QMenu *menu = new QMenu();
601     Populate( p_intf, menu, varnames, objects );
602
603     menu->addSeparator();
604     PopupMenuControlEntries( menu, p_intf, p_input );
605
606     menu->addSeparator();
607     PopupMenuStaticEntries( p_intf, menu );
608
609     p_intf->p_sys->p_popup_menu = menu;
610     menu->popup( QCursor::pos() );
611     p_intf->p_sys->p_popup_menu = NULL;
612 }
613
614 /* Main Menu that sticks everything together  */
615 void QVLCMenu::PopupMenu( intf_thread_t *p_intf, bool show )
616 {
617     if( show )
618     {
619         // create a  popup if there is none
620         if( ! p_intf->p_sys->p_popup_menu )
621         {
622             POPUP_BOILERPLATE;
623             if( p_input )
624             {
625                 vlc_object_yield( p_input );
626                 InputAutoMenuBuilder( VLC_OBJECT( p_input ), objects, varnames );
627
628                 /* Audio menu */
629                 PUSH_SEPARATOR;
630                 varnames.push_back( "audio-es" );
631                 objects.push_back( p_input->i_object_id );
632                 vlc_object_t *p_aout = ( vlc_object_t * )
633                     vlc_object_find( p_input, VLC_OBJECT_AOUT, FIND_ANYWHERE );
634                 if( p_aout )
635                 {
636                     AudioAutoMenuBuilder( p_aout, objects, varnames );
637                     vlc_object_release( p_aout );
638                 }
639
640                 /* Video menu */
641                 PUSH_SEPARATOR;
642                 varnames.push_back( "video-es" );
643                 objects.push_back( p_input->i_object_id );
644                 varnames.push_back( "spu-es" );
645                 objects.push_back( p_input->i_object_id );
646                 vlc_object_t *p_vout = ( vlc_object_t * )
647                     vlc_object_find( p_input, VLC_OBJECT_VOUT, FIND_CHILD );
648                 if( p_vout )
649                 {
650                     VideoAutoMenuBuilder( p_vout, objects, varnames );
651                     vlc_object_release( p_vout );
652                 }
653             }
654
655             QMenu *menu = new QMenu();
656             Populate( p_intf, menu, varnames, objects );
657             menu->addSeparator();
658             PopupMenuControlEntries( menu, p_intf, p_input );
659             menu->addSeparator();
660             PopupMenuStaticEntries( p_intf, menu );
661
662             p_intf->p_sys->p_popup_menu = menu;
663         }
664         p_intf->p_sys->p_popup_menu->popup( QCursor::pos() );
665     }
666     else
667     {
668         // destroy popup if there is one
669         delete p_intf->p_sys->p_popup_menu;
670         p_intf->p_sys->p_popup_menu = NULL;
671     }
672 }
673
674 /************************************************************************
675  * Systray Menu                                                         *
676  ************************************************************************/
677
678 void QVLCMenu::updateSystrayMenu( MainInterface *mi,
679                                   intf_thread_t *p_intf,
680                                   bool b_force_visible )
681 {
682     POPUP_BOILERPLATE;
683
684     /* Get the systray menu and clean it */
685     QMenu *sysMenu = mi->getSysTrayMenu();
686     sysMenu->clear();
687
688     /* Hide / Show VLC and cone */
689     if( mi->isVisible() || b_force_visible )
690     {
691         sysMenu->addAction( QIcon( ":/vlc16.png" ),
692                 qtr( "Hide VLC media player in taskbar" ), mi,
693                 SLOT( toggleUpdateSystrayMenu() ) );
694     }
695     else
696     {
697         sysMenu->addAction( QIcon( ":/vlc16.png" ),
698                 qtr( "Show VLC media player" ), mi,
699                 SLOT( toggleUpdateSystrayMenu() ) );
700     }
701
702     sysMenu->addSeparator();
703     PopupMenuControlEntries( sysMenu, p_intf, p_input );
704
705     sysMenu->addSeparator();
706     addDPStaticEntry( sysMenu, qtr( "&Open Media" ), "",
707             ":/pixmaps/file-wide_16px.png", SLOT( openFileDialog() ), "" );
708     addDPStaticEntry( sysMenu, qtr( "&Quit" ) , "",
709         ":/pixmaps/menus_quit_16px.png", SLOT( quit() ), "" );
710
711     /* Set the menu */
712     mi->getSysTray()->setContextMenu( sysMenu );
713 }
714
715 #undef PUSH_VAR
716 #undef PUSH_SEPARATOR
717
718
719 /*************************************************************************
720  * Builders for automenus
721  *************************************************************************/
722 QMenu * QVLCMenu::Populate( intf_thread_t *p_intf,
723                             QMenu *current,
724                             vector< const char *> & varnames,
725                             vector<int> & objects,
726                             bool append )
727 {
728     QMenu *menu = current;
729     if( !menu )
730         menu = new QMenu();
731     else if( !append )
732         menu->clear();
733
734     currentGroup = NULL;
735
736     vlc_object_t *p_object;
737     vlc_bool_t b_section_empty = VLC_FALSE;
738     int i;
739
740 #define APPEND_EMPTY { QAction *action = menu->addAction( qtr( "Empty" ) ); \
741     action->setEnabled( false ); }
742
743     for( i = 0; i < ( int )objects.size() ; i++ )
744     {
745         if( !varnames[i] || !*varnames[i] )
746         {
747             if( b_section_empty )
748                 APPEND_EMPTY;
749             menu->addSeparator();
750             b_section_empty = VLC_TRUE;
751             continue;
752         }
753
754         if( objects[i] == 0 )
755         {
756             /// \bug What is this ?
757             // Append( menu, varnames[i], NULL );
758             b_section_empty = VLC_FALSE;
759             continue;
760         }
761
762         p_object = ( vlc_object_t * )vlc_object_get( p_intf, objects[i] );
763         if( p_object == NULL ) continue;
764
765         b_section_empty = VLC_FALSE;
766         /* Ugly specific stuff */
767         if( strstr( varnames[i], "intf-add" ) )
768             CreateItem( menu, varnames[i], p_object, false );
769         else
770             CreateItem( menu, varnames[i], p_object, true );
771         vlc_object_release( p_object );
772     }
773
774     /* Special case for empty menus */
775     if( menu->actions().size() == 0 || b_section_empty )
776         APPEND_EMPTY
777
778     return menu;
779 }
780 #undef APPEND_EMPTY
781
782 /*****************************************************************************
783  * Private methods.
784  *****************************************************************************/
785
786 static bool IsMenuEmpty( const char *psz_var,
787                          vlc_object_t *p_object,
788                          bool b_root = true )
789 {
790     vlc_value_t val, val_list;
791     int i_type, i_result, i;
792
793     /* Check the type of the object variable */
794     i_type = var_Type( p_object, psz_var );
795
796     /* Check if we want to display the variable */
797     if( !( i_type & VLC_VAR_HASCHOICE ) ) return false;
798
799     var_Change( p_object, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
800     if( val.i_int == 0 ) return true;
801
802     if( ( i_type & VLC_VAR_TYPE ) != VLC_VAR_VARIABLE )
803     {
804         /* Very evil hack ! intf-switch can have only one value */
805         if( !strcmp( psz_var, "intf-switch" ) ) return false;
806         if( val.i_int == 1 && b_root ) return true;
807         else return false;
808     }
809
810     /* Check children variables in case of VLC_VAR_VARIABLE */
811     if( var_Change( p_object, psz_var, VLC_VAR_GETLIST, &val_list, NULL ) < 0 )
812     {
813         return true;
814     }
815
816     for( i = 0, i_result = true; i < val_list.p_list->i_count; i++ )
817     {
818         if( !IsMenuEmpty( val_list.p_list->p_values[i].psz_string,
819                     p_object, false ) )
820         {
821             i_result = false;
822             break;
823         }
824     }
825
826     /* clean up everything */
827     var_Change( p_object, psz_var, VLC_VAR_FREELIST, &val_list, NULL );
828
829     return i_result;
830 }
831
832 void QVLCMenu::CreateItem( QMenu *menu, const char *psz_var,
833         vlc_object_t *p_object, bool b_submenu )
834 {
835     vlc_value_t val, text;
836     int i_type;
837
838     /* Check the type of the object variable */
839     i_type = var_Type( p_object, psz_var );
840
841     switch( i_type & VLC_VAR_TYPE )
842     {
843         case VLC_VAR_VOID:
844         case VLC_VAR_BOOL:
845         case VLC_VAR_VARIABLE:
846         case VLC_VAR_STRING:
847         case VLC_VAR_INTEGER:
848         case VLC_VAR_FLOAT:
849             break;
850         default:
851             /* Variable doesn't exist or isn't handled */
852             return;
853     }
854
855     /* Make sure we want to display the variable */
856     if( IsMenuEmpty( psz_var, p_object ) )  return;
857
858     /* Get the descriptive name of the variable */
859     var_Change( p_object, psz_var, VLC_VAR_GETTEXT, &text, NULL );
860
861     if( i_type & VLC_VAR_HASCHOICE )
862     {
863         /* Append choices menu */
864         if( b_submenu )
865         {
866             QMenu *submenu = new QMenu();
867             submenu->setTitle( qfu( text.psz_string ?
868                         text.psz_string : psz_var ) );
869             if( CreateChoicesMenu( submenu, psz_var, p_object, true ) == 0 )
870                 menu->addMenu( submenu );
871         }
872         else
873             CreateChoicesMenu( menu, psz_var, p_object, true );
874         FREENULL( text.psz_string );
875         return;
876     }
877
878 #define TEXT_OR_VAR qfu ( text.psz_string ? text.psz_string : psz_var )
879
880     switch( i_type & VLC_VAR_TYPE )
881     {
882         case VLC_VAR_VOID:
883             var_Get( p_object, psz_var, &val );
884             CreateAndConnect( menu, psz_var, TEXT_OR_VAR, "", ITEM_NORMAL,
885                     p_object->i_object_id, val, i_type );
886             break;
887
888         case VLC_VAR_BOOL:
889             var_Get( p_object, psz_var, &val );
890             val.b_bool = !val.b_bool;
891             CreateAndConnect( menu, psz_var, TEXT_OR_VAR, "", ITEM_CHECK,
892                     p_object->i_object_id, val, i_type, !val.b_bool );
893             break;
894     }
895     FREENULL( text.psz_string );
896 }
897
898
899 int QVLCMenu::CreateChoicesMenu( QMenu *submenu, const char *psz_var,
900         vlc_object_t *p_object, bool b_root )
901 {
902     vlc_value_t val, val_list, text_list;
903     int i_type, i;
904
905     /* Check the type of the object variable */
906     i_type = var_Type( p_object, psz_var );
907
908     /* Make sure we want to display the variable */
909     if( IsMenuEmpty( psz_var, p_object, b_root ) ) return VLC_EGENERIC;
910
911     switch( i_type & VLC_VAR_TYPE )
912     {
913         case VLC_VAR_VOID:
914         case VLC_VAR_BOOL:
915         case VLC_VAR_VARIABLE:
916         case VLC_VAR_STRING:
917         case VLC_VAR_INTEGER:
918         case VLC_VAR_FLOAT:
919             break;
920         default:
921             /* Variable doesn't exist or isn't handled */
922             return VLC_EGENERIC;
923     }
924
925     if( var_Change( p_object, psz_var, VLC_VAR_GETLIST,
926                 &val_list, &text_list ) < 0 )
927     {
928         return VLC_EGENERIC;
929     }
930
931 #define NORMAL_OR_RADIO i_type & VLC_VAR_ISCOMMAND ? ITEM_NORMAL: ITEM_RADIO
932 #define NOTCOMMAND !( i_type & VLC_VAR_ISCOMMAND )
933 #define CURVAL val_list.p_list->p_values[i]
934 #define CURTEXT text_list.p_list->p_values[i].psz_string
935
936     for( i = 0; i < val_list.p_list->i_count; i++ )
937     {
938         vlc_value_t another_val;
939         QString menutext;
940         QMenu *subsubmenu = new QMenu();
941
942         switch( i_type & VLC_VAR_TYPE )
943         {
944             case VLC_VAR_VARIABLE:
945                 CreateChoicesMenu( subsubmenu, CURVAL.psz_string, p_object, false );
946                 subsubmenu->setTitle( qfu( CURTEXT ? CURTEXT :CURVAL.psz_string ) );
947                 submenu->addMenu( subsubmenu );
948                 break;
949
950             case VLC_VAR_STRING:
951                 var_Get( p_object, psz_var, &val );
952                 another_val.psz_string = strdup( CURVAL.psz_string );
953                 menutext = qfu( CURTEXT ? CURTEXT : another_val.psz_string );
954                 CreateAndConnect( submenu, psz_var, menutext, "", NORMAL_OR_RADIO,
955                         p_object->i_object_id, another_val, i_type,
956                         NOTCOMMAND && val.psz_string &&
957                         !strcmp( val.psz_string, CURVAL.psz_string ) );
958
959                 if( val.psz_string ) free( val.psz_string );
960                 break;
961
962             case VLC_VAR_INTEGER:
963                 var_Get( p_object, psz_var, &val );
964                 if( CURTEXT ) menutext = qfu( CURTEXT );
965                 else menutext.sprintf( "%d", CURVAL.i_int );
966                 CreateAndConnect( submenu, psz_var, menutext, "", NORMAL_OR_RADIO,
967                         p_object->i_object_id, CURVAL, i_type,
968                         NOTCOMMAND && CURVAL.i_int == val.i_int );
969                 break;
970
971             case VLC_VAR_FLOAT:
972                 var_Get( p_object, psz_var, &val );
973                 if( CURTEXT ) menutext = qfu( CURTEXT );
974                 else menutext.sprintf( "%.2f", CURVAL.f_float );
975                 CreateAndConnect( submenu, psz_var, menutext, "", NORMAL_OR_RADIO,
976                         p_object->i_object_id, CURVAL, i_type,
977                         NOTCOMMAND && CURVAL.f_float == val.f_float );
978                 break;
979
980             default:
981                 break;
982         }
983     }
984     currentGroup = NULL;
985
986     /* clean up everything */
987     var_Change( p_object, psz_var, VLC_VAR_FREELIST, &val_list, &text_list );
988
989 #undef NORMAL_OR_RADIO
990 #undef NOTCOMMAND
991 #undef CURVAL
992 #undef CURTEXT
993     return VLC_SUCCESS;
994 }
995
996 void QVLCMenu::CreateAndConnect( QMenu *menu, const char *psz_var,
997         QString text, QString help,
998         int i_item_type, int i_object_id,
999         vlc_value_t val, int i_val_type,
1000         bool checked )
1001 {
1002     QAction *action = new QAction( text, menu );
1003     action->setText( text );
1004     action->setToolTip( help );
1005
1006     if( i_item_type == ITEM_CHECK )
1007     {
1008         action->setCheckable( true );
1009     }
1010     else if( i_item_type == ITEM_RADIO )
1011     {
1012         action->setCheckable( true );
1013         if( !currentGroup )
1014             currentGroup = new QActionGroup( menu );
1015         currentGroup->addAction( action );
1016     }
1017
1018     if( checked )
1019     {
1020         action->setChecked( true );
1021     }
1022     MenuItemData *itemData = new MenuItemData( i_object_id, i_val_type,
1023             val, psz_var );
1024     CONNECT( action, triggered(), THEDP->menusMapper, map() );
1025     THEDP->menusMapper->setMapping( action, itemData );
1026     menu->addAction( action );
1027 }
1028
1029 void QVLCMenu::DoAction( intf_thread_t *p_intf, QObject *data )
1030 {
1031     MenuItemData *itemData = qobject_cast<MenuItemData *>( data );
1032     vlc_object_t *p_object = ( vlc_object_t * )vlc_object_get( p_intf,
1033             itemData->i_object_id );
1034     if( p_object == NULL ) return;
1035
1036     var_Set( p_object, itemData->psz_var, itemData->val );
1037     vlc_object_release( p_object );
1038 }
1039