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