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