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