]> git.sesse.net Git - vlc/blob - modules/gui/qt4/menus.cpp
14e05bcd8108090eb421aceea867ce115cce60e4
[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         /* FullScreen View */
278         action = menu->addAction( qtr( "Toggle Fullscreen Interface" ), mi,
279                 SLOT( toggleFullScreen() ), qtr( "F11" ) );
280
281         /* Advanced Controls */
282         action = menu->addAction( qtr( "Advanced controls" ), mi,
283                 SLOT( toggleAdvanced() ) );
284         action->setCheckable( true );
285         if( mi->getControlsVisibilityStatus() & CONTROLS_ADVANCED )
286             action->setChecked( true );
287 #if 0 /* For Visualisations. Not yet working */
288         adv = menu->addAction( qtr( "Visualizations selector" ),
289                 mi, SLOT( visual() ) );
290         adv->setCheckable( true );
291         if( visual_selector_enabled ) adv->setChecked( true );
292 #endif
293     }
294
295     menu->addSeparator();
296
297     addDPStaticEntry( menu, qtr( I_MENU_MSG ), "",
298         ":/pixmaps/menus_messages_16px.png", SLOT( messagesDialog() ),
299         "Ctrl+M" );
300     addDPStaticEntry( menu, qtr( I_MENU_INFO ) , "", "",
301         SLOT( mediaInfoDialog() ), "Ctrl+I" );
302     addDPStaticEntry( menu, qtr( I_MENU_CODECINFO ) , "",
303         ":/pixmaps/menus_info_16px.png", SLOT( mediaCodecDialog() ), "Ctrl+J" );
304     addDPStaticEntry( menu, qtr( I_MENU_BOOKMARK ), "","",
305                       SLOT( bookmarksDialog() ), "Ctrl+B" );
306 #ifdef ENABLE_VLM
307     addDPStaticEntry( menu, qtr( I_MENU_VLM ), "", "", SLOT( vlmDialog() ),
308         "Ctrl+V" );
309 #endif
310
311     menu->addSeparator();
312     addDPStaticEntry( menu, qtr( "Preferences..." ), "",
313         ":/pixmaps/menus_preferences_16px.png", SLOT( prefsDialog() ), "Ctrl+P" );
314     return menu;
315 }
316
317 /**
318  * Interface Sub-Menu, to list extras interface and skins
319  **/
320 QMenu *QVLCMenu::InterfacesMenu( intf_thread_t *p_intf, QMenu *current )
321 {
322     vector<int> objects;
323     vector<const char *> varnames;
324     /** \todo add "switch to XXX" */
325     varnames.push_back( "intf-add" );
326     objects.push_back( p_intf->i_object_id );
327
328     QMenu *menu = Populate( p_intf, current, varnames, objects );
329
330     if( !p_intf->pf_show_dialog )
331     {
332         menu->addSeparator();
333         menu->addAction( qtr( "Switch to skins" ), THEDP, SLOT( switchToSkins() ),
334                 QString( "Ctrl+Z" ) );
335     }
336
337     CONNECT( menu, aboutToShow(), THEDP->menusUpdateMapper, map() );
338     THEDP->menusUpdateMapper->setMapping( menu, 4 );
339     return menu;
340 }
341
342 /**
343  * Main Audio Menu
344  */
345 QMenu *QVLCMenu::AudioMenu( intf_thread_t *p_intf, QMenu * current )
346 {
347     vector<int> objects;
348     vector<const char *> varnames;
349
350     vlc_object_t *p_object = ( vlc_object_t * )vlc_object_find( p_intf,
351             VLC_OBJECT_INPUT, FIND_ANYWHERE );
352     if( p_object != NULL )
353     {
354         PUSH_VAR( "audio-es" );
355         vlc_object_release( p_object );
356     }
357
358     p_object = ( vlc_object_t * )vlc_object_find( p_intf, VLC_OBJECT_AOUT,
359             FIND_ANYWHERE );
360     if( p_object )
361     {
362         AudioAutoMenuBuilder( p_object, objects, varnames );
363         vlc_object_release( p_object );
364     }
365     return Populate( p_intf, current, varnames, objects );
366 }
367
368 /**
369  * Main Video Menu
370  * Subtitles are part of Video.
371  **/
372 QMenu *QVLCMenu::VideoMenu( intf_thread_t *p_intf, QMenu *current )
373 {
374     vlc_object_t *p_object;
375     vector<int> objects;
376     vector<const char *> varnames;
377
378     p_object = ( vlc_object_t * )vlc_object_find( p_intf, VLC_OBJECT_INPUT,
379             FIND_ANYWHERE );
380     if( p_object != NULL )
381     {
382         PUSH_VAR( "video-es" );
383         PUSH_VAR( "spu-es" );
384         vlc_object_release( p_object );
385     }
386
387     p_object = ( vlc_object_t * )vlc_object_find( p_intf, VLC_OBJECT_VOUT,
388             FIND_ANYWHERE );
389     if( p_object != NULL )
390     {
391         VideoAutoMenuBuilder( p_object, objects, varnames );
392         vlc_object_release( p_object );
393     }
394     return Populate( p_intf, current, varnames, objects );
395 }
396
397 /**
398  * Navigation Menu
399  * For DVD, MP4, MOV and other chapter based format
400  **/
401 QMenu *QVLCMenu::NavigMenu( intf_thread_t *p_intf, QMenu *navMenu )
402 {
403     vlc_object_t *p_object;
404     vector<int> objects;
405     vector<const char *> varnames;
406
407     p_object = ( vlc_object_t * )vlc_object_find( p_intf, VLC_OBJECT_INPUT,
408             FIND_ANYWHERE );
409     if( p_object != NULL )
410     {
411         InputAutoMenuBuilder(  p_object, objects, varnames );
412         PUSH_VAR( "prev-title" ); PUSH_VAR ( "next-title" );
413         PUSH_VAR( "prev-chapter" ); PUSH_VAR( "next-chapter" );
414         vlc_object_release( p_object );
415     }
416     navMenu = new QMenu();
417     addDPStaticEntry( navMenu, qtr( I_MENU_GOTOTIME ), "","",
418         SLOT( gotoTimeDialog() ), "Ctrl+T" );
419     navMenu->addSeparator();
420     return Populate( p_intf, navMenu, varnames, objects, true );
421 }
422
423 /**
424  * Service Discovery SubMenu
425  **/
426 QMenu *QVLCMenu::SDMenu( intf_thread_t *p_intf )
427 {
428     QMenu *menu = new QMenu();
429     menu->setTitle( qtr( I_PL_SD ) );
430     char **ppsz_longnames;
431     char **ppsz_names = services_discovery_GetServicesNames( p_intf,
432                                                              &ppsz_longnames );
433     char **ppsz_name = ppsz_names, **ppsz_longname = ppsz_longnames;
434     for( ; *ppsz_name; ppsz_name++, ppsz_longname++ )
435     {
436         QAction *a = new QAction( qfu( *ppsz_longname ), menu );
437         a->setCheckable( true );
438         if( playlist_IsServicesDiscoveryLoaded( THEPL, *ppsz_name ) )
439             a->setChecked( true );
440         CONNECT( a , triggered(), THEDP->SDMapper, map() );
441         THEDP->SDMapper->setMapping( a, QString( *ppsz_name ) );
442         menu->addAction( a );
443
444         if( !strcmp( *ppsz_name, "podcast" ) )
445         {
446             QAction *b = new QAction( qfu( "Configure podcasts..." ), menu );
447             //b->setEnabled( a->isChecked() );
448             menu->addAction( b );
449             CONNECT( b, triggered(), THEDP, podcastConfigureDialog() );
450         }
451         free( *ppsz_name );
452         free( *ppsz_longname );
453     }
454     free( ppsz_names );
455     free( ppsz_longnames );
456     return menu;
457 }
458 /**
459  * Help/About Menu
460 **/
461 QMenu *QVLCMenu::HelpMenu()
462 {
463     QMenu *menu = new QMenu();
464     addDPStaticEntry( menu, qtr( "Help..." ) , "",
465         ":/pixmaps/menus_help_16px.png", SLOT( helpDialog() ), "F1" );
466 #ifdef UPDATE_CHECK
467     addDPStaticEntry( menu, qtr( "Update" ) , "", "", SLOT( updateDialog() ), "");
468 #endif
469     menu->addSeparator();
470     addDPStaticEntry( menu, qtr( I_MENU_ABOUT ), "", "", SLOT( aboutDialog() ),
471         "Ctrl+F1" );
472     return menu;
473 }
474
475
476 /*****************************************************************************
477  * Popup menus - Right Click menus                                           *
478  *****************************************************************************/
479 #define POPUP_BOILERPLATE \
480     unsigned int i_last_separator = 0; \
481     vector<int> objects; \
482     vector<const char *> varnames; \
483     input_thread_t *p_input = THEMIM->getInput();
484
485 #define CREATE_POPUP \
486     Populate( p_intf, menu, varnames, objects ); \
487     p_intf->p_sys->p_popup_menu = menu; \
488     menu->popup( QCursor::pos() ); \
489     p_intf->p_sys->p_popup_menu = NULL; \
490     i_last_separator = 0;
491
492 void QVLCMenu::PopupMenuControlEntries( QMenu *menu,
493                                         intf_thread_t *p_intf,
494                                         input_thread_t *p_input )
495 {
496     if( p_input )
497     {
498         vlc_value_t val;
499         var_Get( p_input, "state", &val );
500         if( val.i_int == PLAYING_S )
501             addMIMStaticEntry( p_intf, menu, qtr( "Pause" ), "",
502                     ":/pixmaps/pause_16px.png", SLOT( togglePlayPause() ) );
503         else
504             addMIMStaticEntry( p_intf, menu, qtr( "Play" ), "",
505                     ":/pixmaps/play_16px.png", SLOT( togglePlayPause() ) );
506     }
507     else if( THEPL->items.i_size && THEPL->i_enabled )
508         addMIMStaticEntry( p_intf, menu, qtr( "Play" ), "",
509                 ":/pixmaps/play_16px.png", SLOT( togglePlayPause() ) );
510
511     addMIMStaticEntry( p_intf, menu, qtr( "Stop" ), "",
512             ":/pixmaps/stop_16px.png", SLOT( stop() ) );
513     addMIMStaticEntry( p_intf, menu, qtr( "Previous" ), "",
514             ":/pixmaps/previous_16px.png", SLOT( prev() ) );
515     addMIMStaticEntry( p_intf, menu, qtr( "Next" ), "",
516             ":/pixmaps/next_16px.png", SLOT( next() ) );
517     }
518
519 void QVLCMenu::PopupMenuStaticEntries( intf_thread_t *p_intf, QMenu *menu )
520 {
521     QMenu *toolsmenu = ToolsMenu( p_intf, NULL, false, true );
522     toolsmenu->setTitle( qtr( "Tools" ) );
523     menu->addMenu( toolsmenu );
524
525     QMenu *openmenu = new QMenu( qtr( "Open" ) );
526     openmenu->addAction( qtr( "Open &File..." ), THEDP,
527                          SLOT( openFileDialog() ) );
528     openmenu->addAction( qtr( "Open &Disc..." ), THEDP,
529                          SLOT( openDiscDialog() ) );
530     openmenu->addAction( qtr( "Open &Network..." ), THEDP,
531                          SLOT( openNetDialog() ) );
532     openmenu->addAction( qtr( "Open &Capture Device..." ), THEDP,
533                          SLOT( openCaptureDialog() ) );
534     menu->addMenu( openmenu );
535
536     menu->addSeparator();
537     QMenu *helpmenu = HelpMenu();
538     helpmenu->setTitle( qtr( "Help" ) );
539     menu->addMenu( helpmenu );
540
541     addDPStaticEntry( menu, qtr( "Quit" ), "", "", SLOT( quit() ) , "Ctrl+Q" );
542 }
543
544 /* Video Tracks and Subtitles tracks */
545 void QVLCMenu::VideoPopupMenu( intf_thread_t *p_intf )
546 {
547     POPUP_BOILERPLATE;
548     if( p_input )
549     {
550         vlc_object_yield( p_input );
551         varnames.push_back( "video-es" );
552         objects.push_back( p_input->i_object_id );
553         varnames.push_back( "spu-es" );
554         objects.push_back( p_input->i_object_id );
555         vlc_object_t *p_vout = ( vlc_object_t * )vlc_object_find( p_input,
556                 VLC_OBJECT_VOUT, FIND_CHILD );
557         if( p_vout )
558         {
559             VideoAutoMenuBuilder( p_vout, objects, varnames );
560             vlc_object_release( p_vout );
561         }
562         vlc_object_release( p_input );
563     }
564     QMenu *menu = new QMenu();
565     CREATE_POPUP;
566 }
567
568 /* Audio Tracks */
569 void QVLCMenu::AudioPopupMenu( intf_thread_t *p_intf )
570 {
571     POPUP_BOILERPLATE;
572     if( p_input )
573     {
574         vlc_object_yield( p_input );
575         varnames.push_back( "audio-es" );
576         objects.push_back( p_input->i_object_id );
577         vlc_object_t *p_aout = ( vlc_object_t * )vlc_object_find( p_input,
578                 VLC_OBJECT_AOUT, FIND_ANYWHERE );
579         if( p_aout )
580         {
581             AudioAutoMenuBuilder( p_aout, objects, varnames );
582             vlc_object_release( p_aout );
583         }
584         vlc_object_release( p_input );
585     }
586     QMenu *menu = new QMenu();
587     CREATE_POPUP;
588 }
589
590 /* Navigation stuff, and general menus ( open ) */
591 void QVLCMenu::MiscPopupMenu( intf_thread_t *p_intf )
592 {
593     vlc_value_t val;
594     POPUP_BOILERPLATE;
595
596     if( p_input )
597     {
598         vlc_object_yield( p_input );
599         varnames.push_back( "audio-es" );
600         InputAutoMenuBuilder( VLC_OBJECT( p_input ), objects, varnames );
601         PUSH_SEPARATOR;
602     }
603
604     QMenu *menu = new QMenu();
605     Populate( p_intf, menu, varnames, objects );
606
607     menu->addSeparator();
608     PopupMenuControlEntries( menu, p_intf, p_input );
609
610     menu->addSeparator();
611     PopupMenuStaticEntries( p_intf, menu );
612
613     p_intf->p_sys->p_popup_menu = menu;
614     menu->popup( QCursor::pos() );
615     p_intf->p_sys->p_popup_menu = NULL;
616 }
617
618 /* Main Menu that sticks everything together  */
619 void QVLCMenu::PopupMenu( intf_thread_t *p_intf, bool show )
620 {
621     if( show )
622     {
623         // create a  popup if there is none
624         if( ! p_intf->p_sys->p_popup_menu )
625         {
626             POPUP_BOILERPLATE;
627             if( p_input )
628             {
629                 vlc_object_yield( p_input );
630                 InputAutoMenuBuilder( VLC_OBJECT( p_input ), objects, varnames );
631
632                 /* Audio menu */
633                 PUSH_SEPARATOR;
634                 varnames.push_back( "audio-es" );
635                 objects.push_back( p_input->i_object_id );
636                 vlc_object_t *p_aout = ( vlc_object_t * )
637                     vlc_object_find( p_input, VLC_OBJECT_AOUT, FIND_ANYWHERE );
638                 if( p_aout )
639                 {
640                     AudioAutoMenuBuilder( p_aout, objects, varnames );
641                     vlc_object_release( p_aout );
642                 }
643
644                 /* Video menu */
645                 PUSH_SEPARATOR;
646                 varnames.push_back( "video-es" );
647                 objects.push_back( p_input->i_object_id );
648                 varnames.push_back( "spu-es" );
649                 objects.push_back( p_input->i_object_id );
650                 vlc_object_t *p_vout = ( vlc_object_t * )
651                     vlc_object_find( p_input, VLC_OBJECT_VOUT, FIND_CHILD );
652                 if( p_vout )
653                 {
654                     VideoAutoMenuBuilder( p_vout, objects, varnames );
655                     vlc_object_release( p_vout );
656                 }
657             }
658
659             QMenu *menu = new QMenu();
660             Populate( p_intf, menu, varnames, objects );
661             menu->addSeparator();
662             PopupMenuControlEntries( menu, p_intf, p_input );
663             menu->addSeparator();
664             PopupMenuStaticEntries( p_intf, menu );
665
666             p_intf->p_sys->p_popup_menu = menu;
667         }
668         p_intf->p_sys->p_popup_menu->popup( QCursor::pos() );
669     }
670     else
671     {
672         // destroy popup if there is one
673         delete p_intf->p_sys->p_popup_menu;
674         p_intf->p_sys->p_popup_menu = NULL;
675     }
676 }
677
678 /************************************************************************
679  * Systray Menu                                                         *
680  ************************************************************************/
681
682 void QVLCMenu::updateSystrayMenu( MainInterface *mi,
683                                   intf_thread_t *p_intf,
684                                   bool b_force_visible )
685 {
686     POPUP_BOILERPLATE;
687
688     /* Get the systray menu and clean it */
689     QMenu *sysMenu = mi->getSysTrayMenu();
690     sysMenu->clear();
691
692     /* Hide / Show VLC and cone */
693     if( mi->isVisible() || b_force_visible )
694     {
695         sysMenu->addAction( QIcon( ":/vlc16.png" ),
696                 qtr( "Hide VLC media player in taskbar" ), mi,
697                 SLOT( toggleUpdateSystrayMenu() ) );
698     }
699     else
700     {
701         sysMenu->addAction( QIcon( ":/vlc16.png" ),
702                 qtr( "Show VLC media player" ), mi,
703                 SLOT( toggleUpdateSystrayMenu() ) );
704     }
705
706     sysMenu->addSeparator();
707     PopupMenuControlEntries( sysMenu, p_intf, p_input );
708
709     sysMenu->addSeparator();
710     addDPStaticEntry( sysMenu, qtr( "&Open Media" ), "",
711             ":/pixmaps/file-wide_16px.png", SLOT( openFileDialog() ), "" );
712     addDPStaticEntry( sysMenu, qtr( "&Quit" ) , "",
713         ":/pixmaps/menus_quit_16px.png", SLOT( quit() ), "" );
714
715     /* Set the menu */
716     mi->getSysTray()->setContextMenu( sysMenu );
717 }
718
719 #undef PUSH_VAR
720 #undef PUSH_SEPARATOR
721
722
723 /*************************************************************************
724  * Builders for automenus
725  *************************************************************************/
726 QMenu * QVLCMenu::Populate( intf_thread_t *p_intf,
727                             QMenu *current,
728                             vector< const char *> & varnames,
729                             vector<int> & objects,
730                             bool append )
731 {
732     QMenu *menu = current;
733     if( !menu )
734         menu = new QMenu();
735     else if( !append )
736         menu->clear();
737
738     currentGroup = NULL;
739
740     vlc_object_t *p_object;
741     vlc_bool_t b_section_empty = VLC_FALSE;
742     int i;
743
744 #define APPEND_EMPTY { QAction *action = menu->addAction( qtr( "Empty" ) ); \
745     action->setEnabled( false ); }
746
747     for( i = 0; i < ( int )objects.size() ; i++ )
748     {
749         if( !varnames[i] || !*varnames[i] )
750         {
751             if( b_section_empty )
752                 APPEND_EMPTY;
753             menu->addSeparator();
754             b_section_empty = VLC_TRUE;
755             continue;
756         }
757
758         if( objects[i] == 0 )
759         {
760             /// \bug What is this ?
761             // Append( menu, varnames[i], NULL );
762             b_section_empty = VLC_FALSE;
763             continue;
764         }
765
766         p_object = ( vlc_object_t * )vlc_object_get( p_intf, objects[i] );
767         if( p_object == NULL ) continue;
768
769         b_section_empty = VLC_FALSE;
770         /* Ugly specific stuff */
771         if( strstr( varnames[i], "intf-add" ) )
772             CreateItem( menu, varnames[i], p_object, false );
773         else
774             CreateItem( menu, varnames[i], p_object, true );
775         vlc_object_release( p_object );
776     }
777
778     /* Special case for empty menus */
779     if( menu->actions().size() == 0 || b_section_empty )
780         APPEND_EMPTY
781
782     return menu;
783 }
784 #undef APPEND_EMPTY
785
786 /*****************************************************************************
787  * Private methods.
788  *****************************************************************************/
789
790 static bool IsMenuEmpty( const char *psz_var,
791                          vlc_object_t *p_object,
792                          bool b_root = true )
793 {
794     vlc_value_t val, val_list;
795     int i_type, i_result, i;
796
797     /* Check the type of the object variable */
798     i_type = var_Type( p_object, psz_var );
799
800     /* Check if we want to display the variable */
801     if( !( i_type & VLC_VAR_HASCHOICE ) ) return false;
802
803     var_Change( p_object, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
804     if( val.i_int == 0 ) return true;
805
806     if( ( i_type & VLC_VAR_TYPE ) != VLC_VAR_VARIABLE )
807     {
808         /* Very evil hack ! intf-switch can have only one value */
809         if( !strcmp( psz_var, "intf-switch" ) ) return false;
810         if( val.i_int == 1 && b_root ) return true;
811         else return false;
812     }
813
814     /* Check children variables in case of VLC_VAR_VARIABLE */
815     if( var_Change( p_object, psz_var, VLC_VAR_GETLIST, &val_list, NULL ) < 0 )
816     {
817         return true;
818     }
819
820     for( i = 0, i_result = true; i < val_list.p_list->i_count; i++ )
821     {
822         if( !IsMenuEmpty( val_list.p_list->p_values[i].psz_string,
823                     p_object, false ) )
824         {
825             i_result = false;
826             break;
827         }
828     }
829
830     /* clean up everything */
831     var_Change( p_object, psz_var, VLC_VAR_FREELIST, &val_list, NULL );
832
833     return i_result;
834 }
835
836 void QVLCMenu::CreateItem( QMenu *menu, const char *psz_var,
837         vlc_object_t *p_object, bool b_submenu )
838 {
839     vlc_value_t val, text;
840     int i_type;
841
842     /* Check the type of the object variable */
843     i_type = var_Type( p_object, psz_var );
844
845     switch( i_type & VLC_VAR_TYPE )
846     {
847         case VLC_VAR_VOID:
848         case VLC_VAR_BOOL:
849         case VLC_VAR_VARIABLE:
850         case VLC_VAR_STRING:
851         case VLC_VAR_INTEGER:
852         case VLC_VAR_FLOAT:
853             break;
854         default:
855             /* Variable doesn't exist or isn't handled */
856             return;
857     }
858
859     /* Make sure we want to display the variable */
860     if( IsMenuEmpty( psz_var, p_object ) )  return;
861
862     /* Get the descriptive name of the variable */
863     var_Change( p_object, psz_var, VLC_VAR_GETTEXT, &text, NULL );
864
865     if( i_type & VLC_VAR_HASCHOICE )
866     {
867         /* Append choices menu */
868         if( b_submenu )
869         {
870             QMenu *submenu = new QMenu();
871             submenu->setTitle( qfu( text.psz_string ?
872                         text.psz_string : psz_var ) );
873             if( CreateChoicesMenu( submenu, psz_var, p_object, true ) == 0 )
874                 menu->addMenu( submenu );
875         }
876         else
877             CreateChoicesMenu( menu, psz_var, p_object, true );
878         FREENULL( text.psz_string );
879         return;
880     }
881
882 #define TEXT_OR_VAR qfu ( text.psz_string ? text.psz_string : psz_var )
883
884     switch( i_type & VLC_VAR_TYPE )
885     {
886         case VLC_VAR_VOID:
887             var_Get( p_object, psz_var, &val );
888             CreateAndConnect( menu, psz_var, TEXT_OR_VAR, "", ITEM_NORMAL,
889                     p_object->i_object_id, val, i_type );
890             break;
891
892         case VLC_VAR_BOOL:
893             var_Get( p_object, psz_var, &val );
894             val.b_bool = !val.b_bool;
895             CreateAndConnect( menu, psz_var, TEXT_OR_VAR, "", ITEM_CHECK,
896                     p_object->i_object_id, val, i_type, !val.b_bool );
897             break;
898     }
899     FREENULL( text.psz_string );
900 }
901
902
903 int QVLCMenu::CreateChoicesMenu( QMenu *submenu, const char *psz_var,
904         vlc_object_t *p_object, bool b_root )
905 {
906     vlc_value_t val, val_list, text_list;
907     int i_type, i;
908
909     /* Check the type of the object variable */
910     i_type = var_Type( p_object, psz_var );
911
912     /* Make sure we want to display the variable */
913     if( IsMenuEmpty( psz_var, p_object, b_root ) ) return VLC_EGENERIC;
914
915     switch( i_type & VLC_VAR_TYPE )
916     {
917         case VLC_VAR_VOID:
918         case VLC_VAR_BOOL:
919         case VLC_VAR_VARIABLE:
920         case VLC_VAR_STRING:
921         case VLC_VAR_INTEGER:
922         case VLC_VAR_FLOAT:
923             break;
924         default:
925             /* Variable doesn't exist or isn't handled */
926             return VLC_EGENERIC;
927     }
928
929     if( var_Change( p_object, psz_var, VLC_VAR_GETLIST,
930                 &val_list, &text_list ) < 0 )
931     {
932         return VLC_EGENERIC;
933     }
934
935 #define NORMAL_OR_RADIO i_type & VLC_VAR_ISCOMMAND ? ITEM_NORMAL: ITEM_RADIO
936 #define NOTCOMMAND !( i_type & VLC_VAR_ISCOMMAND )
937 #define CURVAL val_list.p_list->p_values[i]
938 #define CURTEXT text_list.p_list->p_values[i].psz_string
939
940     for( i = 0; i < val_list.p_list->i_count; i++ )
941     {
942         vlc_value_t another_val;
943         QString menutext;
944         QMenu *subsubmenu = new QMenu();
945
946         switch( i_type & VLC_VAR_TYPE )
947         {
948             case VLC_VAR_VARIABLE:
949                 CreateChoicesMenu( subsubmenu, CURVAL.psz_string, p_object, false );
950                 subsubmenu->setTitle( qfu( CURTEXT ? CURTEXT :CURVAL.psz_string ) );
951                 submenu->addMenu( subsubmenu );
952                 break;
953
954             case VLC_VAR_STRING:
955                 var_Get( p_object, psz_var, &val );
956                 another_val.psz_string = strdup( CURVAL.psz_string );
957                 menutext = qfu( CURTEXT ? CURTEXT : another_val.psz_string );
958                 CreateAndConnect( submenu, psz_var, menutext, "", NORMAL_OR_RADIO,
959                         p_object->i_object_id, another_val, i_type,
960                         NOTCOMMAND && val.psz_string &&
961                         !strcmp( val.psz_string, CURVAL.psz_string ) );
962
963                 if( val.psz_string ) free( val.psz_string );
964                 break;
965
966             case VLC_VAR_INTEGER:
967                 var_Get( p_object, psz_var, &val );
968                 if( CURTEXT ) menutext = qfu( CURTEXT );
969                 else menutext.sprintf( "%d", CURVAL.i_int );
970                 CreateAndConnect( submenu, psz_var, menutext, "", NORMAL_OR_RADIO,
971                         p_object->i_object_id, CURVAL, i_type,
972                         NOTCOMMAND && CURVAL.i_int == val.i_int );
973                 break;
974
975             case VLC_VAR_FLOAT:
976                 var_Get( p_object, psz_var, &val );
977                 if( CURTEXT ) menutext = qfu( CURTEXT );
978                 else menutext.sprintf( "%.2f", CURVAL.f_float );
979                 CreateAndConnect( submenu, psz_var, menutext, "", NORMAL_OR_RADIO,
980                         p_object->i_object_id, CURVAL, i_type,
981                         NOTCOMMAND && CURVAL.f_float == val.f_float );
982                 break;
983
984             default:
985                 break;
986         }
987     }
988     currentGroup = NULL;
989
990     /* clean up everything */
991     var_Change( p_object, psz_var, VLC_VAR_FREELIST, &val_list, &text_list );
992
993 #undef NORMAL_OR_RADIO
994 #undef NOTCOMMAND
995 #undef CURVAL
996 #undef CURTEXT
997     return VLC_SUCCESS;
998 }
999
1000 void QVLCMenu::CreateAndConnect( QMenu *menu, const char *psz_var,
1001         QString text, QString help,
1002         int i_item_type, int i_object_id,
1003         vlc_value_t val, int i_val_type,
1004         bool checked )
1005 {
1006     QAction *action = new QAction( text, menu );
1007     action->setText( text );
1008     action->setToolTip( help );
1009
1010     if( i_item_type == ITEM_CHECK )
1011     {
1012         action->setCheckable( true );
1013     }
1014     else if( i_item_type == ITEM_RADIO )
1015     {
1016         action->setCheckable( true );
1017         if( !currentGroup )
1018             currentGroup = new QActionGroup( menu );
1019         currentGroup->addAction( action );
1020     }
1021
1022     if( checked )
1023     {
1024         action->setChecked( true );
1025     }
1026     MenuItemData *itemData = new MenuItemData( i_object_id, i_val_type,
1027             val, psz_var );
1028     CONNECT( action, triggered(), THEDP->menusMapper, map() );
1029     THEDP->menusMapper->setMapping( action, itemData );
1030     menu->addAction( action );
1031 }
1032
1033 void QVLCMenu::DoAction( intf_thread_t *p_intf, QObject *data )
1034 {
1035     MenuItemData *itemData = qobject_cast<MenuItemData *>( data );
1036     vlc_object_t *p_object = ( vlc_object_t * )vlc_object_get( p_intf,
1037             itemData->i_object_id );
1038     if( p_object == NULL ) return;
1039
1040     var_Set( p_object, itemData->psz_var, itemData->val );
1041     vlc_object_release( p_object );
1042 }
1043