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