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