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