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