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