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