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