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