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