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