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