1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2006 the VideoLAN team
7 * Authors: ClÃment Stenac <zorglub@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
27 #include <QActionGroup>
28 #include <QSignalMapper>
30 #include "main_interface.hpp"
33 #include "dialogs_provider.hpp"
34 #include "input_manager.hpp"
43 static QActionGroup *currentGroup;
45 // Add static entries to menus
46 #define DP_SADD( text, help, icon, slot ) { if( strlen(icon) > 0 ) { QAction *action = menu->addAction( text, THEDP, SLOT( slot ) ); action->setIcon(QIcon(icon));} else { menu->addAction( text, THEDP, SLOT( slot ) ); } }
47 #define MIM_SADD( text, help, icon, slot ) { if( strlen(icon) > 0 ) { QAction *action = menu->addAction( text, THEMIM, SLOT( slot ) ); action->setIcon(QIcon(icon));} else { menu->addAction( text, THEMIM, SLOT( slot ) ); } }
50 /*****************************************************************************
51 * Definitions of variables for the dynamic menus
52 *****************************************************************************/
53 #define PUSH_VAR( var ) varnames.push_back( var ); \
54 objects.push_back( p_object->i_object_id )
56 #define PUSH_SEPARATOR if( objects.size() != i_last_separator ) { \
57 objects.push_back( 0 ); varnames.push_back( "" ); \
58 i_last_separator = objects.size(); }
60 static int InputAutoMenuBuilder( vlc_object_t *p_object,
62 vector<const char *> &varnames )
64 PUSH_VAR( "bookmark");
66 PUSH_VAR ("chapter" );
67 PUSH_VAR( "program" );
68 PUSH_VAR( "navigation" );
69 PUSH_VAR( "dvd_menus" );
73 static int VideoAutoMenuBuilder( vlc_object_t *p_object,
75 vector<const char *> &varnames )
77 PUSH_VAR( "fullscreen" );
79 PUSH_VAR( "deinterlace" );
80 PUSH_VAR( "aspect-ratio" );
82 PUSH_VAR( "video-on-top" );
83 PUSH_VAR( "directx-wallpaper" );
84 PUSH_VAR( "video-snapshot" );
86 vlc_object_t *p_dec_obj = (vlc_object_t *)vlc_object_find( p_object,
89 if( p_dec_obj != NULL )
91 vlc_object_t *p_object = p_dec_obj;
92 PUSH_VAR( "ffmpeg-pp-q" );
93 vlc_object_release( p_dec_obj );
98 static int AudioAutoMenuBuilder( vlc_object_t *p_object,
100 vector<const char *> &varnames )
102 PUSH_VAR( "audio-device" );
103 PUSH_VAR( "audio-channels" );
104 PUSH_VAR( "visual" );
105 PUSH_VAR( "equalizer" );
109 /*****************************************************************************
111 *****************************************************************************/
113 #define BAR_ADD( func, title ) { \
114 QMenu *menu = func; menu->setTitle( title ); bar->addMenu( menu ); }
116 #define BAR_DADD( func, title, id ) { \
117 QMenu *menu = func; menu->setTitle( title ); bar->addMenu( menu ); \
118 MenuFunc *f = new MenuFunc( menu, id ); \
119 CONNECT( menu, aboutToShow(), THEDP->menusUpdateMapper, map() ); \
120 THEDP->menusUpdateMapper->setMapping( menu, f ); }
122 void QVLCMenu::createMenuBar( MainInterface *mi, intf_thread_t *p_intf,
125 QMenuBar *bar = mi->menuBar();
126 BAR_ADD( FileMenu(), qtr("File") );
129 BAR_ADD( PlaylistMenu( mi,p_intf ), qtr("Playlist" ) );
131 BAR_ADD( ToolsMenu( p_intf ), qtr("Tools") );
132 BAR_DADD( VideoMenu( p_intf, NULL ), qtr("Video"), 1 );
133 BAR_DADD( AudioMenu( p_intf, NULL ), qtr("Audio"), 2 );
134 BAR_DADD( NavigMenu( p_intf, NULL ), qtr("Navigation"), 3 );
136 // BAR_ADD( HelpMenu(), qtr("Help" ) );
138 QMenu *QVLCMenu::FileMenu()
140 QMenu *menu = new QMenu();
141 DP_SADD( qtr("Quick &Open File...") , "", "", simpleOpenDialog() );
142 DP_SADD( qtr("&Advanced Open..." ), "", "", openDialog() );
143 menu->addSeparator();
144 DP_SADD( qtr("Streaming..."), "", "", streamingDialog() );
145 menu->addSeparator();
146 DP_SADD( qtr("&Quit") , "", "", quit() );
150 QMenu *QVLCMenu::PlaylistMenu( MainInterface *mi, intf_thread_t *p_intf )
152 QMenu *menu = new QMenu();
153 menu->addMenu( SDMenu( p_intf ) );
154 menu->addSeparator();
156 DP_SADD( qtr( "Open playlist file"), "", "", openPlaylist() );
157 // DP_SADD( qtr( "Save playlist to file" ), "", "", savePlaylist() );
158 menu->addSeparator();
159 menu->addAction( qtr("Undock from interface"), mi,
160 SLOT( undockPlaylist() ) );
164 QMenu *QVLCMenu::ToolsMenu( intf_thread_t *p_intf, bool with_intf )
166 QMenu *menu = new QMenu();
169 QMenu *intfmenu = InterfacesMenu( p_intf, NULL );
170 intfmenu->setTitle( qtr("Interfaces" ) );
171 menu->addMenu( intfmenu );
172 /** \todo ADD EXT GUI HERE */
173 menu->addSeparator();
175 DP_SADD( qtr("Messages" ), "", "", messagesDialog() );
176 DP_SADD( qtr("Information") , "", "", streaminfoDialog() );
177 DP_SADD( qtr("Bookmarks"), "", "", bookmarksDialog() );
178 menu->addSeparator();
179 DP_SADD( qtr("Preferences"), "", "", prefsDialog() );
180 DP_SADD( qtr("Extended"), "","",extendedDialog() );
184 QMenu *QVLCMenu::InterfacesMenu( intf_thread_t *p_intf, QMenu *current )
187 vector<const char *> varnames;
188 /** \todo add "switch to XXX" */
189 varnames.push_back( "intf-add" );
190 objects.push_back( p_intf->i_object_id );
192 QMenu *menu = Populate( p_intf, current, varnames, objects );
193 CONNECT( menu, aboutToShow(), THEDP->menusUpdateMapper, map() );
194 THEDP->menusUpdateMapper->setMapping( menu, 4 );
199 QMenu *QVLCMenu::AudioMenu( intf_thread_t *p_intf, QMenu * current )
202 vector<const char *> varnames;
204 vlc_object_t *p_object = (vlc_object_t *)vlc_object_find( p_intf,
205 VLC_OBJECT_INPUT, FIND_ANYWHERE );
206 if( p_object != NULL )
208 PUSH_VAR( "audio-es" );
209 vlc_object_release( p_object );
212 p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_AOUT,
216 AudioAutoMenuBuilder( p_object, objects, varnames );
217 vlc_object_release( p_object );
219 return Populate( p_intf, current, varnames, objects );
223 QMenu *QVLCMenu::VideoMenu( intf_thread_t *p_intf, QMenu *current )
225 vlc_object_t *p_object;
227 vector<const char *> varnames;
229 p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
231 if( p_object != NULL )
233 PUSH_VAR( "video-es" );
234 PUSH_VAR( "spu-es" );
235 vlc_object_release( p_object );
238 p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_VOUT,
240 if( p_object != NULL )
242 VideoAutoMenuBuilder( p_object, objects, varnames );
243 vlc_object_release( p_object );
245 return Populate( p_intf, current, varnames, objects );
248 QMenu *QVLCMenu::NavigMenu( intf_thread_t *p_intf, QMenu *current )
250 vlc_object_t *p_object;
252 vector<const char *> varnames;
255 p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
257 if( p_object != NULL )
259 InputAutoMenuBuilder( p_object, objects, varnames );
260 PUSH_VAR( "prev-title"); PUSH_VAR ( "next-title" );
261 PUSH_VAR( "prev-chapter"); PUSH_VAR( "next-chapter" );
262 vlc_object_release( p_object );
264 return Populate( p_intf, current, varnames, objects );
267 QMenu *QVLCMenu::SDMenu( intf_thread_t *p_intf )
269 QMenu *menu = new QMenu();
270 menu->setTitle( qtr( "Additional sources" ) );
271 vlc_list_t *p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE,
274 for( int i_index = 0 ; i_index < p_list->i_count; i_index++ )
276 module_t * p_parser = (module_t *)p_list->p_values[i_index].p_object ;
277 if( !strcmp( p_parser->psz_capability, "services_discovery" ) )
280 for( int i_index = 0 ; i_index < p_list->i_count; i_index++ )
282 module_t * p_parser = (module_t *)p_list->p_values[i_index].p_object;
283 if( !strcmp( p_parser->psz_capability, "services_discovery" ) )
285 QAction *a = new QAction( qfu( p_parser->psz_longname ), menu );
286 a->setCheckable( true );
287 /* hack to handle submodules properly */
289 while( p_parser->pp_shortcuts[++i] != NULL );
291 if( playlist_IsServicesDiscoveryLoaded( THEPL,
292 i>=0?p_parser->pp_shortcuts[i] : p_parser->psz_object_name ) )
294 a->setChecked( true );
296 CONNECT( a , triggered(), THEDP->SDMapper, map() );
297 THEDP->SDMapper->setMapping( a, i>=0? p_parser->pp_shortcuts[i] :
298 p_parser->psz_object_name );
299 menu->addAction( a );
302 vlc_list_release( p_list );
306 /*****************************************************************************
308 *****************************************************************************/
309 #define POPUP_BOILERPLATE \
310 unsigned int i_last_separator = 0; \
311 vector<int> objects; \
312 vector<const char *> varnames; \
313 input_thread_t *p_input = THEMIM->getInput();
315 #define CREATE_POPUP \
316 QMenu *menu = new QMenu(); \
317 Populate( p_intf, menu, varnames, objects ); \
318 p_intf->p_sys->p_popup_menu = menu; \
319 menu->popup( QCursor::pos() ); \
320 p_intf->p_sys->p_popup_menu = NULL; \
321 i_last_separator = 0;
323 #define POPUP_STATIC_ENTRIES \
325 MIM_SADD( qtr("Stop"), "", "", stop() ); \
326 MIM_SADD( qtr("Previous"), "", "", prev() ); \
327 MIM_SADD( qtr("Next"), "", "", next() ); \
330 var_Get( p_input, "state", &val ); \
331 if( val.i_int == PAUSE_S ) \
332 MIM_SADD( qtr("Play"), "", "", togglePlayPause() ) \
334 MIM_SADD( qtr("Pause"), "", "", togglePlayPause() ) \
336 else if( THEPL->i_size && THEPL->i_enabled ) \
337 MIM_SADD( qtr("Play"), "", "", togglePlayPause() ) \
339 QMenu *intfmenu = InterfacesMenu( p_intf, NULL ); \
340 intfmenu->setTitle( qtr("Interfaces" ) ); \
341 menu->addMenu( intfmenu ); \
343 QMenu *toolsmenu = ToolsMenu( p_intf, false ); \
344 toolsmenu->setTitle( qtr("Tools" ) ); \
345 menu->addMenu( toolsmenu ); \
347 void QVLCMenu::VideoPopupMenu( intf_thread_t *p_intf )
352 vlc_object_yield( p_input );
353 varnames.push_back( "video-es" );
354 objects.push_back( p_input->i_object_id );
355 varnames.push_back( "spu-es" );
356 objects.push_back( p_input->i_object_id );
357 vlc_object_t *p_vout = (vlc_object_t *)vlc_object_find( p_input,
358 VLC_OBJECT_VOUT, FIND_CHILD );
361 VideoAutoMenuBuilder( p_vout, objects, varnames );
362 vlc_object_release( p_vout );
364 vlc_object_release( p_input );
369 void QVLCMenu::AudioPopupMenu( intf_thread_t *p_intf )
374 vlc_object_yield( p_input );
375 varnames.push_back( "audio-es" );
376 objects.push_back( p_input->i_object_id );
377 vlc_object_t *p_aout = (vlc_object_t *)vlc_object_find( p_input,
378 VLC_OBJECT_AOUT, FIND_ANYWHERE );
381 AudioAutoMenuBuilder( p_aout, objects, varnames );
382 vlc_object_release( p_aout );
384 vlc_object_release( p_input );
389 /* Navigation stuff, and general */
390 void QVLCMenu::MiscPopupMenu( intf_thread_t *p_intf )
395 vlc_object_yield( p_input );
396 varnames.push_back( "audio-es" );
397 InputAutoMenuBuilder( VLC_OBJECT(p_input), objects, varnames );
401 QMenu *menu = new QMenu();
402 Populate( p_intf, menu, varnames, objects );
403 menu->addSeparator();
404 POPUP_STATIC_ENTRIES;
406 p_intf->p_sys->p_popup_menu = menu;
407 menu->popup( QCursor::pos() );
408 p_intf->p_sys->p_popup_menu = NULL;
411 void QVLCMenu::PopupMenu( intf_thread_t *p_intf )
416 vlc_object_yield( p_input );
417 InputAutoMenuBuilder( VLC_OBJECT(p_input), objects, varnames );
421 varnames.push_back( "video-es" );
422 objects.push_back( p_input->i_object_id );
423 varnames.push_back( "spu-es" );
424 objects.push_back( p_input->i_object_id );
425 vlc_object_t *p_vout = (vlc_object_t *)vlc_object_find( p_input,
426 VLC_OBJECT_VOUT, FIND_CHILD );
429 VideoAutoMenuBuilder( p_vout, objects, varnames );
430 vlc_object_release( p_vout );
434 varnames.push_back( "audio-es" );
435 objects.push_back( p_input->i_object_id );
436 vlc_object_t *p_aout = (vlc_object_t *)vlc_object_find( p_input,
437 VLC_OBJECT_AOUT, FIND_ANYWHERE );
440 AudioAutoMenuBuilder( p_aout, objects, varnames );
441 vlc_object_release( p_aout );
445 QMenu *menu = new QMenu();
446 Populate( p_intf, menu, varnames, objects );
447 menu->addSeparator();
448 POPUP_STATIC_ENTRIES;
450 p_intf->p_sys->p_popup_menu = menu;
451 menu->popup( QCursor::pos() );
452 p_intf->p_sys->p_popup_menu = NULL;
456 #undef PUSH_SEPARATOR
458 /*************************************************************************
459 * Builders for automenus
460 *************************************************************************/
461 QMenu * QVLCMenu::Populate( intf_thread_t *p_intf, QMenu *current,
462 vector< const char *> & varnames,
463 vector<int> & objects, bool append )
465 QMenu *menu = current;
473 vlc_object_t *p_object;
474 vlc_bool_t b_section_empty = VLC_FALSE;
477 #define APPEND_EMPTY { QAction *action = menu->addAction( qtr("Empty" ) ); \
478 action->setEnabled( false ); }
480 for( i = 0; i < (int)objects.size() ; i++ )
482 if( !varnames[i] || !*varnames[i] )
484 if( b_section_empty )
486 menu->addSeparator();
487 b_section_empty = VLC_TRUE;
491 if( objects[i] == 0 )
493 /// \bug What is this ?
494 // Append( menu, varnames[i], NULL );
495 b_section_empty = VLC_FALSE;
499 p_object = (vlc_object_t *)vlc_object_get( p_intf,
501 if( p_object == NULL ) continue;
503 b_section_empty = VLC_FALSE;
504 /* Ugly specific stuff */
505 if( strstr(varnames[i], "intf-add" ) )
506 CreateItem( menu, varnames[i], p_object, false );
508 CreateItem( menu, varnames[i], p_object, true );
509 vlc_object_release( p_object );
512 /* Special case for empty menus */
513 if( menu->actions().size() == 0 || b_section_empty )
519 /*****************************************************************************
521 *****************************************************************************/
523 static bool IsMenuEmpty( const char *psz_var, vlc_object_t *p_object,
526 vlc_value_t val, val_list;
527 int i_type, i_result, i;
529 /* Check the type of the object variable */
530 i_type = var_Type( p_object, psz_var );
532 /* Check if we want to display the variable */
533 if( !(i_type & VLC_VAR_HASCHOICE) ) return FALSE;
535 var_Change( p_object, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
536 if( val.i_int == 0 ) return TRUE;
538 if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE )
540 /* Very evil hack ! intf-switch can have only one value */
541 if( !strcmp( psz_var, "intf-switch" ) ) return FALSE;
542 if( val.i_int == 1 && b_root ) return TRUE;
546 /* Check children variables in case of VLC_VAR_VARIABLE */
547 if( var_Change( p_object, psz_var, VLC_VAR_GETLIST, &val_list, NULL ) < 0 )
552 for( i = 0, i_result = TRUE; i < val_list.p_list->i_count; i++ )
554 if( !IsMenuEmpty( val_list.p_list->p_values[i].psz_string,
562 /* clean up everything */
563 var_Change( p_object, psz_var, VLC_VAR_FREELIST, &val_list, NULL );
568 void QVLCMenu::CreateItem( QMenu *menu, const char *psz_var,
569 vlc_object_t *p_object, bool b_submenu )
571 vlc_value_t val, text;
574 /* Check the type of the object variable */
575 i_type = var_Type( p_object, psz_var );
577 switch( i_type & VLC_VAR_TYPE )
581 case VLC_VAR_VARIABLE:
583 case VLC_VAR_INTEGER:
587 /* Variable doesn't exist or isn't handled */
591 /* Make sure we want to display the variable */
592 if( IsMenuEmpty( psz_var, p_object ) ) return;
594 /* Get the descriptive name of the variable */
595 var_Change( p_object, psz_var, VLC_VAR_GETTEXT, &text, NULL );
597 if( i_type & VLC_VAR_HASCHOICE )
599 /* Append choices menu */
602 QMenu *submenu = new QMenu();
603 submenu->setTitle( qfu( text.psz_string ?
604 text.psz_string : psz_var ) );
605 if( CreateChoicesMenu( submenu, psz_var, p_object, true ) == 0)
606 menu->addMenu( submenu );
609 CreateChoicesMenu( menu, psz_var, p_object, true );
610 FREENULL( text.psz_string );
614 #define TEXT_OR_VAR qfu ( text.psz_string ? text.psz_string : psz_var )
616 switch( i_type & VLC_VAR_TYPE )
619 var_Get( p_object, psz_var, &val );
620 CreateAndConnect( menu, psz_var, TEXT_OR_VAR, "", ITEM_NORMAL,
621 p_object->i_object_id, val, i_type );
625 var_Get( p_object, psz_var, &val );
626 CreateAndConnect( menu, psz_var, TEXT_OR_VAR, "", ITEM_CHECK,
627 p_object->i_object_id, val, i_type, val.b_bool );
630 FREENULL( text.psz_string );
634 int QVLCMenu::CreateChoicesMenu( QMenu *submenu, const char *psz_var,
635 vlc_object_t *p_object, bool b_root )
637 vlc_value_t val, val_list, text_list;
640 /* Check the type of the object variable */
641 i_type = var_Type( p_object, psz_var );
643 /* Make sure we want to display the variable */
644 if( IsMenuEmpty( psz_var, p_object, b_root ) ) return VLC_EGENERIC;
646 switch( i_type & VLC_VAR_TYPE )
650 case VLC_VAR_VARIABLE:
652 case VLC_VAR_INTEGER:
656 /* Variable doesn't exist or isn't handled */
660 if( var_Change( p_object, psz_var, VLC_VAR_GETLIST,
661 &val_list, &text_list ) < 0 )
665 #define NORMAL_OR_RADIO i_type & VLC_VAR_ISCOMMAND ? ITEM_NORMAL: ITEM_RADIO
666 #define NOTCOMMAND !(i_type & VLC_VAR_ISCOMMAND)
667 #define CURVAL val_list.p_list->p_values[i]
668 #define CURTEXT text_list.p_list->p_values[i].psz_string
670 for( i = 0; i < val_list.p_list->i_count; i++ )
672 vlc_value_t another_val;
674 QMenu *subsubmenu = new QMenu();
676 switch( i_type & VLC_VAR_TYPE )
678 case VLC_VAR_VARIABLE:
679 CreateChoicesMenu( subsubmenu, CURVAL.psz_string, p_object, false );
680 subsubmenu->setTitle( qfu( CURTEXT ? CURTEXT :CURVAL.psz_string ) );
681 submenu->addMenu( subsubmenu );
685 var_Get( p_object, psz_var, &val );
686 another_val.psz_string = strdup( CURVAL.psz_string );
687 menutext = qfu( CURTEXT ? CURTEXT : another_val.psz_string );
688 CreateAndConnect( submenu, psz_var, menutext, "", NORMAL_OR_RADIO,
689 p_object->i_object_id, another_val, i_type,
690 NOTCOMMAND && val.psz_string &&
691 !strcmp( val.psz_string, CURVAL.psz_string ) );
693 if( val.psz_string ) free( val.psz_string );
696 case VLC_VAR_INTEGER:
697 var_Get( p_object, psz_var, &val );
698 if( CURTEXT ) menutext = qfu( CURTEXT );
699 else menutext.sprintf( "%d", CURVAL.i_int);
700 CreateAndConnect( submenu, psz_var, menutext, "", NORMAL_OR_RADIO,
701 p_object->i_object_id, CURVAL, i_type,
702 NOTCOMMAND && CURVAL.i_int == val.i_int );
706 var_Get( p_object, psz_var, &val );
707 if( CURTEXT ) menutext = qfu( CURTEXT );
708 else menutext.sprintf( "%.2f", CURVAL.f_float );
709 CreateAndConnect( submenu, psz_var, menutext, "", NORMAL_OR_RADIO,
710 p_object->i_object_id, CURVAL, i_type,
711 NOTCOMMAND && CURVAL.f_float == val.f_float );
720 /* clean up everything */
721 var_Change( p_object, psz_var, VLC_VAR_FREELIST, &val_list, &text_list );
723 #undef NORMAL_OR_RADIO
730 void QVLCMenu::CreateAndConnect( QMenu *menu, const char *psz_var,
731 QString text, QString help,
732 int i_item_type, int i_object_id,
733 vlc_value_t val, int i_val_type,
736 QAction *action = new QAction( text, menu );
737 action->setText( text );
738 action->setToolTip( help );
740 if( i_item_type == ITEM_CHECK )
742 action->setCheckable( true );
744 else if( i_item_type == ITEM_RADIO )
746 action->setCheckable( true );
748 currentGroup = new QActionGroup(menu);
749 currentGroup->addAction( action );
754 action->setChecked( true );
756 MenuItemData *itemData = new MenuItemData( i_object_id, i_val_type,
758 CONNECT( action, triggered(), THEDP->menusMapper, map() );
759 THEDP->menusMapper->setMapping( action, itemData );
760 menu->addAction( action );
763 void QVLCMenu::DoAction( intf_thread_t *p_intf, QObject *data )
765 MenuItemData *itemData = qobject_cast<MenuItemData *>(data);
766 vlc_object_t *p_object = (vlc_object_t *)vlc_object_get( p_intf,
767 itemData->i_object_id );
768 if( p_object == NULL ) return;
770 var_Set( p_object, itemData->psz_var, itemData->val );
771 vlc_object_release( p_object );