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