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