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 *****************************************************************************/
25 #include "dialogs_provider.hpp"
26 #include "input_manager.hpp"
30 #include <QActionGroup>
31 #include <QSignalMapper>
40 static QActionGroup *currentGroup;
41 static char ** pp_sds;
43 // Add static entries to menus
44 #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 ) ); } }
45 #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 ) ); } }
48 /*****************************************************************************
49 * Definitions of variables for the dynamic menus
50 *****************************************************************************/
51 #define PUSH_VAR( var ) varnames.push_back( var ); \
52 objects.push_back( p_object->i_object_id )
54 #define PUSH_SEPARATOR if( objects.size() != i_last_separator ) { \
55 objects.push_back( 0 ); varnames.push_back( "" ); \
56 i_last_separator = objects.size(); }
58 static int InputAutoMenuBuilder( vlc_object_t *p_object,
60 vector<const char *> &varnames )
62 PUSH_VAR( "bookmark");
64 PUSH_VAR ("chapter" );
65 PUSH_VAR( "program" );
66 PUSH_VAR( "navigation" );
67 PUSH_VAR( "dvd_menus" );
71 static int VideoAutoMenuBuilder( vlc_object_t *p_object,
73 vector<const char *> &varnames )
75 PUSH_VAR( "fullscreen" );
77 PUSH_VAR( "deinterlace" );
78 PUSH_VAR( "aspect-ratio" );
80 PUSH_VAR( "video-on-top" );
81 PUSH_VAR( "directx-wallpaper" );
82 PUSH_VAR( "video-snapshot" );
84 vlc_object_t *p_dec_obj = (vlc_object_t *)vlc_object_find( p_object,
87 if( p_dec_obj != NULL )
89 PUSH_VAR( "ffmpeg-pp-q" );
90 vlc_object_release( p_dec_obj );
95 static int AudioAutoMenuBuilder( vlc_object_t *p_object,
97 vector<const char *> &varnames )
99 PUSH_VAR( "audio-device" );
100 PUSH_VAR( "audio-channels" );
101 PUSH_VAR( "visual" );
102 PUSH_VAR( "equalizer" );
106 /*****************************************************************************
108 *****************************************************************************/
110 #define BAR_ADD( func, title ) { \
111 QMenu *menu = func; menu->setTitle( title ); bar->addMenu( menu ); }
113 #define BAR_DADD( func, title, id ) { \
114 QMenu *menu = func; menu->setTitle( title ); bar->addMenu( menu ); \
115 MenuFunc *f = new MenuFunc( menu, id ); \
116 connect( menu, SIGNAL( aboutToShow() ), \
117 THEDP->menusUpdateMapper, SLOT(map()) ); \
118 THEDP->menusUpdateMapper->setMapping( menu, f ); }
120 void QVLCMenu::createMenuBar( QMenuBar *bar, intf_thread_t *p_intf )
122 BAR_ADD( FileMenu(), qtr("File") );
123 BAR_ADD( ToolsMenu( p_intf ), qtr("Tools") );
124 BAR_DADD( VideoMenu( p_intf, NULL ), qtr("Video"), 1 );
125 BAR_DADD( AudioMenu( p_intf, NULL ), qtr("Audio"), 2 );
126 BAR_DADD( NavigMenu( p_intf, NULL ), qtr("Navigation"), 3 );
128 // BAR_ADD( HelpMenu(), qtr("Help" ) );
131 void QVLCMenu::createPlMenuBar( QMenuBar *bar, intf_thread_t *p_intf )
133 QMenu *manageMenu = new QMenu();
134 manageMenu->addAction( "Quick &Add File...", THEDP,
135 SLOT( simpleAppendDialog() ) );
136 manageMenu->addSeparator();
137 manageMenu->addMenu( SDMenu( p_intf ) );
139 bar->addMenu( manageMenu );
142 QMenu *QVLCMenu::SDMenu( intf_thread_t *p_intf )
144 QMenu *menu = new QMenu();
145 menu->setTitle( qtr( "Services Discovery" ) );
146 playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
149 assert( p_playlist );
150 vlc_list_t *p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE,
153 for( int i_index = 0 ; i_index < p_list->i_count; i_index++ )
155 module_t * p_parser = (module_t *)p_list->p_values[i_index].p_object ;
156 if( !strcmp( p_parser->psz_capability, "services_discovery" ) )
159 if( i_num ) pp_sds = (char **)calloc( i_num, sizeof(void *) );
160 for( int i_index = 0 ; i_index < p_list->i_count; i_index++ )
162 module_t * p_parser = (module_t *)p_list->p_values[i_index].p_object;
163 if( !strcmp( p_parser->psz_capability, "services_discovery" ) )
165 QAction *a = menu->addAction(
166 qfu( p_parser->psz_longname ?
167 p_parser->psz_longname :
168 ( p_parser->psz_shortname ?
169 p_parser->psz_shortname :
170 p_parser->psz_object_name ) ) );
171 a->setCheckable( true );
172 /* hack to handle submodules properly */
174 while( p_parser->pp_shortcuts[++i] != NULL );
176 if( playlist_IsServicesDiscoveryLoaded( p_playlist,
177 i>=0?p_parser->pp_shortcuts[i] : p_parser->psz_object_name ) )
179 a->setChecked( true );
181 pp_sds[i_num++] = i>=0? p_parser->pp_shortcuts[i] :
182 p_parser->psz_object_name;
185 vlc_list_release( p_list );
186 vlc_object_release( p_playlist );
190 QMenu *QVLCMenu::FileMenu()
192 QMenu *menu = new QMenu();
193 DP_SADD( qtr("Quick &Open File...") , "", "", simpleOpenDialog() );
194 DP_SADD( qtr("&Advanced Open..." ), "", "", openDialog() );
195 menu->addSeparator();
196 DP_SADD( qtr("Streaming..."), "", "", streamingDialog() );
197 menu->addSeparator();
198 DP_SADD( qtr("&Quit") , "", "", quit() );
202 QMenu *QVLCMenu::ToolsMenu( intf_thread_t *p_intf, bool with_intf )
204 QMenu *menu = new QMenu();
207 QMenu *intfmenu = InterfacesMenu( p_intf, NULL );
208 intfmenu->setTitle( qtr("Interfaces" ) );
209 menu->addMenu( intfmenu );
210 /** \todo ADD EXT GUI HERE */
211 menu->addSeparator();
213 DP_SADD( qtr("Messages" ), "", "", messagesDialog() );
214 DP_SADD( qtr("Information") , "", "", streaminfoDialog() );
215 DP_SADD( qtr("Bookmarks"), "", "", bookmarksDialog() );
216 menu->addSeparator();
217 DP_SADD( qtr("Preferences"), "", "", prefsDialog() );
221 QMenu *QVLCMenu::InterfacesMenu( intf_thread_t *p_intf, QMenu *current )
224 vector<const char *> varnames;
225 /** \todo add "switch to XXX" */
226 varnames.push_back( "intf-add" );
227 objects.push_back( p_intf->i_object_id );
229 QMenu *menu = Populate( p_intf, current, varnames, objects );
230 connect( menu, SIGNAL( aboutToShow() ),
231 THEDP->menusUpdateMapper, SLOT(map()) );
232 THEDP->menusUpdateMapper->setMapping( menu, 4 );
237 QMenu *QVLCMenu::AudioMenu( intf_thread_t *p_intf, QMenu * current )
240 vector<const char *> varnames;
242 vlc_object_t *p_object = (vlc_object_t *)vlc_object_find( p_intf,
243 VLC_OBJECT_INPUT, FIND_ANYWHERE );
244 if( p_object != NULL )
246 PUSH_VAR( "audio-es" );
247 vlc_object_release( p_object );
250 p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_AOUT,
254 AudioAutoMenuBuilder( p_object, objects, varnames );
255 vlc_object_release( p_object );
257 return Populate( p_intf, current, varnames, objects );
261 QMenu *QVLCMenu::VideoMenu( intf_thread_t *p_intf, QMenu *current )
263 vlc_object_t *p_object;
265 vector<const char *> varnames;
267 p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
269 if( p_object != NULL )
271 PUSH_VAR( "video-es" );
272 PUSH_VAR( "spu-es" );
273 vlc_object_release( p_object );
276 p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_VOUT,
278 if( p_object != NULL )
280 VideoAutoMenuBuilder( p_object, objects, varnames );
281 vlc_object_release( p_object );
283 return Populate( p_intf, current, varnames, objects );
286 QMenu *QVLCMenu::NavigMenu( intf_thread_t *p_intf, QMenu *current )
288 vlc_object_t *p_object;
290 vector<const char *> varnames;
293 p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
295 if( p_object != NULL )
297 InputAutoMenuBuilder( p_object, objects, varnames );
298 PUSH_VAR( "prev-title"); PUSH_VAR ( "next-title" );
299 PUSH_VAR( "prev-chapter"); PUSH_VAR( "next-chapter" );
300 vlc_object_release( p_object );
302 return Populate( p_intf, current, varnames, objects );
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 val.b_bool = !val.b_bool;
627 CreateAndConnect( menu, psz_var, TEXT_OR_VAR, "", ITEM_CHECK,
628 p_object->i_object_id, val, i_type, val.b_bool );
631 FREENULL( text.psz_string );
635 int QVLCMenu::CreateChoicesMenu( QMenu *submenu, const char *psz_var,
636 vlc_object_t *p_object, bool b_root )
638 vlc_value_t val, val_list, text_list;
641 /* Check the type of the object variable */
642 i_type = var_Type( p_object, psz_var );
644 /* Make sure we want to display the variable */
645 if( IsMenuEmpty( psz_var, p_object, b_root ) ) return VLC_EGENERIC;
647 switch( i_type & VLC_VAR_TYPE )
651 case VLC_VAR_VARIABLE:
653 case VLC_VAR_INTEGER:
657 /* Variable doesn't exist or isn't handled */
661 if( var_Change( p_object, psz_var, VLC_VAR_GETLIST,
662 &val_list, &text_list ) < 0 )
666 #define NORMAL_OR_RADIO i_type & VLC_VAR_ISCOMMAND ? ITEM_NORMAL: ITEM_RADIO
667 #define NOTCOMMAND !(i_type & VLC_VAR_ISCOMMAND)
668 #define CURVAL val_list.p_list->p_values[i]
669 #define CURTEXT text_list.p_list->p_values[i].psz_string
671 for( i = 0; i < val_list.p_list->i_count; i++ )
673 vlc_value_t another_val;
675 QMenu *subsubmenu = new QMenu();
677 switch( i_type & VLC_VAR_TYPE )
679 case VLC_VAR_VARIABLE:
680 CreateChoicesMenu( subsubmenu, CURVAL.psz_string, p_object, false );
681 subsubmenu->setTitle( qfu( CURTEXT ? CURTEXT :CURVAL.psz_string ) );
682 submenu->addMenu( subsubmenu );
686 var_Get( p_object, psz_var, &val );
687 another_val.psz_string = strdup( CURVAL.psz_string );
689 menutext = qfu( CURTEXT ? CURTEXT : another_val.psz_string );
690 CreateAndConnect( submenu, psz_var, menutext, "", NORMAL_OR_RADIO,
691 p_object->i_object_id, another_val, i_type,
692 NOTCOMMAND && val.psz_string &&
693 !strcmp( val.psz_string, CURVAL.psz_string ) );
695 if( val.psz_string ) free( val.psz_string );
698 case VLC_VAR_INTEGER:
699 var_Get( p_object, psz_var, &val );
700 if( CURTEXT ) menutext = qfu( CURTEXT );
701 else menutext.sprintf( "%d", CURVAL.i_int);
702 CreateAndConnect( submenu, psz_var, menutext, "", NORMAL_OR_RADIO,
703 p_object->i_object_id, CURVAL, i_type,
704 NOTCOMMAND && CURVAL.i_int == val.i_int );
708 var_Get( p_object, psz_var, &val );
709 if( CURTEXT ) menutext = qfu( CURTEXT );
710 else menutext.sprintf( "%.2f", CURVAL.f_float );
711 CreateAndConnect( submenu, psz_var, menutext, "", NORMAL_OR_RADIO,
712 p_object->i_object_id, CURVAL, i_type,
713 NOTCOMMAND && CURVAL.f_float == val.f_float );
721 /* clean up everything */
722 var_Change( p_object, psz_var, VLC_VAR_FREELIST, &val_list, &text_list );
724 #undef NORMAL_OR_RADIO
731 void QVLCMenu::CreateAndConnect( QMenu *menu, const char *psz_var,
732 QString text, QString help,
733 int i_item_type, int i_object_id,
734 vlc_value_t val, int i_val_type,
737 QAction *action = new QAction( text, menu );
738 action->setText( text );
739 action->setToolTip( help );
741 if( i_item_type == ITEM_CHECK )
743 action->setCheckable( true );
746 else if( i_item_type == ITEM_RADIO )
748 action->setCheckable( true );
750 currentGroup = new QActionGroup(menu);
751 currentGroup->addAction( action );
755 if( checked ) action->setChecked( true );
757 MenuItemData *itemData = new MenuItemData( i_object_id, i_val_type,
759 connect( action, SIGNAL(triggered()), THEDP->menusMapper, SLOT(map()) );
760 THEDP->menusMapper->setMapping( action, itemData );
761 menu->addAction( action );
764 void QVLCMenu::DoAction( intf_thread_t *p_intf, QObject *data )
766 MenuItemData *itemData = qobject_cast<MenuItemData *>(data);
767 vlc_object_t *p_object = (vlc_object_t *)vlc_object_get( p_intf,
768 itemData->i_object_id );
769 if( p_object == NULL ) return;
771 var_Set( p_object, itemData->psz_var, itemData->val );
772 vlc_object_release( p_object );