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