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