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