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->setTitle( qtr("Operations") );
135 manageMenu->addAction( "Quick &Add File...", THEDP,
136 SLOT( simpleAppendDialog() ) );
137 manageMenu->addSeparator();
138 // manageMenu->addMenu( SDMenu( p_intf ) );
140 bar->addMenu( manageMenu );
143 QMenu *QVLCMenu::SDMenu( intf_thread_t *p_intf )
145 QMenu *menu = new QMenu();
146 menu->setTitle( qtr( "Services Discovery" ) );
147 playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
150 assert( p_playlist );
151 vlc_list_t *p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE,
154 for( int i_index = 0 ; i_index < p_list->i_count; i_index++ )
156 module_t * p_parser = (module_t *)p_list->p_values[i_index].p_object ;
157 if( !strcmp( p_parser->psz_capability, "services_discovery" ) )
160 if( i_num ) pp_sds = (char **)calloc( i_num, sizeof(void *) );
161 for( int i_index = 0 ; i_index < p_list->i_count; i_index++ )
163 module_t * p_parser = (module_t *)p_list->p_values[i_index].p_object;
164 if( !strcmp( p_parser->psz_capability, "services_discovery" ) )
166 QAction *a = menu->addAction(
167 qfu( p_parser->psz_longname ?
168 p_parser->psz_longname :
169 ( p_parser->psz_shortname ?
170 p_parser->psz_shortname :
171 p_parser->psz_object_name ) ) );
172 a->setCheckable( true );
173 /* hack to handle submodules properly */
175 while( p_parser->pp_shortcuts[++i] != NULL );
177 if( playlist_IsServicesDiscoveryLoaded( p_playlist,
178 i>=0?p_parser->pp_shortcuts[i] : p_parser->psz_object_name ) )
180 a->setChecked( true );
182 pp_sds[i_num++] = i>=0? p_parser->pp_shortcuts[i] :
183 p_parser->psz_object_name;
186 vlc_list_release( p_list );
187 vlc_object_release( p_playlist );
191 QMenu *QVLCMenu::FileMenu()
193 QMenu *menu = new QMenu();
194 DP_SADD( qtr("Quick &Open File...") , "", "", simpleOpenDialog() );
195 DP_SADD( qtr("&Advanced Open..." ), "", "", openDialog() );
196 menu->addSeparator();
197 DP_SADD( qtr("Streaming..."), "", "", streamingDialog() );
198 menu->addSeparator();
199 DP_SADD( qtr("&Quit") , "", "", quit() );
203 QMenu *QVLCMenu::ToolsMenu( intf_thread_t *p_intf, bool with_intf )
205 QMenu *menu = new QMenu();
208 QMenu *intfmenu = InterfacesMenu( p_intf, NULL );
209 intfmenu->setTitle( qtr("Interfaces" ) );
210 menu->addMenu( intfmenu );
211 /** \todo ADD EXT GUI HERE */
212 menu->addSeparator();
214 DP_SADD( qtr("Messages" ), "", "", messagesDialog() );
215 DP_SADD( qtr("Information") , "", "", streaminfoDialog() );
216 DP_SADD( qtr("Bookmarks"), "", "", bookmarksDialog() );
217 menu->addSeparator();
218 DP_SADD( qtr("Preferences"), "", "", prefsDialog() );
222 QMenu *QVLCMenu::InterfacesMenu( intf_thread_t *p_intf, QMenu *current )
225 vector<const char *> varnames;
226 /** \todo add "switch to XXX" */
227 varnames.push_back( "intf-add" );
228 objects.push_back( p_intf->i_object_id );
230 QMenu *menu = Populate( p_intf, current, varnames, objects );
231 connect( menu, SIGNAL( aboutToShow() ),
232 THEDP->menusUpdateMapper, SLOT(map()) );
233 THEDP->menusUpdateMapper->setMapping( menu, 4 );
238 QMenu *QVLCMenu::AudioMenu( intf_thread_t *p_intf, QMenu * current )
241 vector<const char *> varnames;
243 vlc_object_t *p_object = (vlc_object_t *)vlc_object_find( p_intf,
244 VLC_OBJECT_INPUT, FIND_ANYWHERE );
245 if( p_object != NULL )
247 PUSH_VAR( "audio-es" );
248 vlc_object_release( p_object );
251 p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_AOUT,
255 AudioAutoMenuBuilder( p_object, objects, varnames );
256 vlc_object_release( p_object );
258 return Populate( p_intf, current, varnames, objects );
262 QMenu *QVLCMenu::VideoMenu( intf_thread_t *p_intf, QMenu *current )
264 vlc_object_t *p_object;
266 vector<const char *> varnames;
268 p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
270 if( p_object != NULL )
272 PUSH_VAR( "video-es" );
273 PUSH_VAR( "spu-es" );
274 vlc_object_release( p_object );
277 p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_VOUT,
279 if( p_object != NULL )
281 VideoAutoMenuBuilder( p_object, objects, varnames );
282 vlc_object_release( p_object );
284 return Populate( p_intf, current, varnames, objects );
287 QMenu *QVLCMenu::NavigMenu( intf_thread_t *p_intf, QMenu *current )
289 vlc_object_t *p_object;
291 vector<const char *> varnames;
294 p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
296 if( p_object != NULL )
298 InputAutoMenuBuilder( p_object, objects, varnames );
299 PUSH_VAR( "prev-title"); PUSH_VAR ( "next-title" );
300 PUSH_VAR( "prev-chapter"); PUSH_VAR( "next-chapter" );
301 vlc_object_release( p_object );
303 return Populate( p_intf, current, varnames, objects );
307 /*****************************************************************************
309 *****************************************************************************/
310 #define POPUP_BOILERPLATE \
311 unsigned int i_last_separator = 0; \
312 vector<int> objects; \
313 vector<const char *> varnames; \
314 input_thread_t *p_input = THEMIM->getInput();
316 #define CREATE_POPUP \
317 QMenu *menu = new QMenu(); \
318 Populate( p_intf, menu, varnames, objects ); \
319 p_intf->p_sys->p_popup_menu = menu; \
320 menu->popup( QCursor::pos() ); \
321 p_intf->p_sys->p_popup_menu = NULL; \
322 i_last_separator = 0;
324 #define POPUP_STATIC_ENTRIES \
326 MIM_SADD( qtr("Stop"), "", "", stop() ); \
327 MIM_SADD( qtr("Previous"), "", "", prev() ); \
328 MIM_SADD( qtr("Next"), "", "", next() ); \
331 var_Get( p_input, "state", &val ); \
332 if( val.i_int == PAUSE_S ) \
333 MIM_SADD( qtr("Play"), "", "", togglePlayPause() ) \
335 MIM_SADD( qtr("Pause"), "", "", togglePlayPause() ) \
337 else if( THEPL->i_size && THEPL->i_enabled ) \
338 MIM_SADD( qtr("Play"), "", "", togglePlayPause() ) \
340 QMenu *intfmenu = InterfacesMenu( p_intf, NULL ); \
341 intfmenu->setTitle( qtr("Interfaces" ) ); \
342 menu->addMenu( intfmenu ); \
344 QMenu *toolsmenu = ToolsMenu( p_intf, false ); \
345 toolsmenu->setTitle( qtr("Tools" ) ); \
346 menu->addMenu( toolsmenu ); \
348 void QVLCMenu::VideoPopupMenu( intf_thread_t *p_intf )
353 vlc_object_yield( p_input );
354 varnames.push_back( "video-es" );
355 objects.push_back( p_input->i_object_id );
356 varnames.push_back( "spu-es" );
357 objects.push_back( p_input->i_object_id );
358 vlc_object_t *p_vout = (vlc_object_t *)vlc_object_find( p_input,
359 VLC_OBJECT_VOUT, FIND_CHILD );
362 VideoAutoMenuBuilder( p_vout, objects, varnames );
363 vlc_object_release( p_vout );
365 vlc_object_release( p_input );
370 void QVLCMenu::AudioPopupMenu( intf_thread_t *p_intf )
375 vlc_object_yield( p_input );
376 varnames.push_back( "audio-es" );
377 objects.push_back( p_input->i_object_id );
378 vlc_object_t *p_aout = (vlc_object_t *)vlc_object_find( p_input,
379 VLC_OBJECT_AOUT, FIND_ANYWHERE );
382 AudioAutoMenuBuilder( p_aout, objects, varnames );
383 vlc_object_release( p_aout );
385 vlc_object_release( p_input );
390 /* Navigation stuff, and general */
391 void QVLCMenu::MiscPopupMenu( intf_thread_t *p_intf )
396 vlc_object_yield( p_input );
397 varnames.push_back( "audio-es" );
398 InputAutoMenuBuilder( VLC_OBJECT(p_input), objects, varnames );
402 QMenu *menu = new QMenu();
403 Populate( p_intf, menu, varnames, objects );
404 menu->addSeparator();
405 POPUP_STATIC_ENTRIES;
407 p_intf->p_sys->p_popup_menu = menu;
408 menu->popup( QCursor::pos() );
409 p_intf->p_sys->p_popup_menu = NULL;
412 void QVLCMenu::PopupMenu( intf_thread_t *p_intf )
417 vlc_object_yield( p_input );
418 InputAutoMenuBuilder( VLC_OBJECT(p_input), objects, varnames );
422 varnames.push_back( "video-es" );
423 objects.push_back( p_input->i_object_id );
424 varnames.push_back( "spu-es" );
425 objects.push_back( p_input->i_object_id );
426 vlc_object_t *p_vout = (vlc_object_t *)vlc_object_find( p_input,
427 VLC_OBJECT_VOUT, FIND_CHILD );
430 VideoAutoMenuBuilder( p_vout, objects, varnames );
431 vlc_object_release( p_vout );
435 varnames.push_back( "audio-es" );
436 objects.push_back( p_input->i_object_id );
437 vlc_object_t *p_aout = (vlc_object_t *)vlc_object_find( p_input,
438 VLC_OBJECT_AOUT, FIND_ANYWHERE );
441 AudioAutoMenuBuilder( p_aout, objects, varnames );
442 vlc_object_release( p_aout );
446 QMenu *menu = new QMenu();
447 Populate( p_intf, menu, varnames, objects );
448 menu->addSeparator();
449 POPUP_STATIC_ENTRIES;
451 p_intf->p_sys->p_popup_menu = menu;
452 menu->popup( QCursor::pos() );
453 p_intf->p_sys->p_popup_menu = NULL;
457 #undef PUSH_SEPARATOR
459 /*************************************************************************
460 * Builders for automenus
461 *************************************************************************/
462 QMenu * QVLCMenu::Populate( intf_thread_t *p_intf, QMenu *current,
463 vector< const char *> & varnames,
464 vector<int> & objects, bool append )
466 QMenu *menu = current;
474 vlc_object_t *p_object;
475 vlc_bool_t b_section_empty = VLC_FALSE;
478 #define APPEND_EMPTY { QAction *action = menu->addAction( qtr("Empty" ) ); \
479 action->setEnabled( false ); }
481 for( i = 0; i < (int)objects.size() ; i++ )
483 if( !varnames[i] || !*varnames[i] )
485 if( b_section_empty )
487 menu->addSeparator();
488 b_section_empty = VLC_TRUE;
492 if( objects[i] == 0 )
494 /// \bug What is this ?
495 // Append( menu, varnames[i], NULL );
496 b_section_empty = VLC_FALSE;
500 p_object = (vlc_object_t *)vlc_object_get( p_intf,
502 if( p_object == NULL ) continue;
504 b_section_empty = VLC_FALSE;
505 /* Ugly specific stuff */
506 if( strstr(varnames[i], "intf-add" ) )
507 CreateItem( menu, varnames[i], p_object, false );
509 CreateItem( menu, varnames[i], p_object, true );
510 vlc_object_release( p_object );
513 /* Special case for empty menus */
514 if( menu->actions().size() == 0 || b_section_empty )
520 /*****************************************************************************
522 *****************************************************************************/
524 static bool IsMenuEmpty( const char *psz_var, vlc_object_t *p_object,
527 vlc_value_t val, val_list;
528 int i_type, i_result, i;
530 /* Check the type of the object variable */
531 i_type = var_Type( p_object, psz_var );
533 /* Check if we want to display the variable */
534 if( !(i_type & VLC_VAR_HASCHOICE) ) return FALSE;
536 var_Change( p_object, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
537 if( val.i_int == 0 ) return TRUE;
539 if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE )
541 /* Very evil hack ! intf-switch can have only one value */
542 if( !strcmp( psz_var, "intf-switch" ) ) return FALSE;
543 if( val.i_int == 1 && b_root ) return TRUE;
547 /* Check children variables in case of VLC_VAR_VARIABLE */
548 if( var_Change( p_object, psz_var, VLC_VAR_GETLIST, &val_list, NULL ) < 0 )
553 for( i = 0, i_result = TRUE; i < val_list.p_list->i_count; i++ )
555 if( !IsMenuEmpty( val_list.p_list->p_values[i].psz_string,
563 /* clean up everything */
564 var_Change( p_object, psz_var, VLC_VAR_FREELIST, &val_list, NULL );
569 void QVLCMenu::CreateItem( QMenu *menu, const char *psz_var,
570 vlc_object_t *p_object, bool b_submenu )
572 vlc_value_t val, text;
575 /* Check the type of the object variable */
576 i_type = var_Type( p_object, psz_var );
578 switch( i_type & VLC_VAR_TYPE )
582 case VLC_VAR_VARIABLE:
584 case VLC_VAR_INTEGER:
588 /* Variable doesn't exist or isn't handled */
592 /* Make sure we want to display the variable */
593 if( IsMenuEmpty( psz_var, p_object ) ) return;
595 /* Get the descriptive name of the variable */
596 var_Change( p_object, psz_var, VLC_VAR_GETTEXT, &text, NULL );
598 if( i_type & VLC_VAR_HASCHOICE )
600 /* Append choices menu */
603 QMenu *submenu = new QMenu();
604 submenu->setTitle( qfu( text.psz_string ?
605 text.psz_string : psz_var ) );
606 if( CreateChoicesMenu( submenu, psz_var, p_object, true ) == 0)
607 menu->addMenu( submenu );
610 CreateChoicesMenu( menu, psz_var, p_object, true );
611 FREENULL( text.psz_string );
615 #define TEXT_OR_VAR qfu ( text.psz_string ? text.psz_string : psz_var )
617 switch( i_type & VLC_VAR_TYPE )
620 var_Get( p_object, psz_var, &val );
621 CreateAndConnect( menu, psz_var, TEXT_OR_VAR, "", ITEM_NORMAL,
622 p_object->i_object_id, val, i_type );
626 var_Get( p_object, psz_var, &val );
627 val.b_bool = !val.b_bool;
628 CreateAndConnect( menu, psz_var, TEXT_OR_VAR, "", ITEM_CHECK,
629 p_object->i_object_id, val, i_type, val.b_bool );
632 FREENULL( text.psz_string );
636 int QVLCMenu::CreateChoicesMenu( QMenu *submenu, const char *psz_var,
637 vlc_object_t *p_object, bool b_root )
639 vlc_value_t val, val_list, text_list;
642 /* Check the type of the object variable */
643 i_type = var_Type( p_object, psz_var );
645 /* Make sure we want to display the variable */
646 if( IsMenuEmpty( psz_var, p_object, b_root ) ) return VLC_EGENERIC;
648 switch( i_type & VLC_VAR_TYPE )
652 case VLC_VAR_VARIABLE:
654 case VLC_VAR_INTEGER:
658 /* Variable doesn't exist or isn't handled */
662 if( var_Change( p_object, psz_var, VLC_VAR_GETLIST,
663 &val_list, &text_list ) < 0 )
667 #define NORMAL_OR_RADIO i_type & VLC_VAR_ISCOMMAND ? ITEM_NORMAL: ITEM_RADIO
668 #define NOTCOMMAND !(i_type & VLC_VAR_ISCOMMAND)
669 #define CURVAL val_list.p_list->p_values[i]
670 #define CURTEXT text_list.p_list->p_values[i].psz_string
672 for( i = 0; i < val_list.p_list->i_count; i++ )
674 vlc_value_t another_val;
676 QMenu *subsubmenu = new QMenu();
678 switch( i_type & VLC_VAR_TYPE )
680 case VLC_VAR_VARIABLE:
681 CreateChoicesMenu( subsubmenu, CURVAL.psz_string, p_object, false );
682 subsubmenu->setTitle( qfu( CURTEXT ? CURTEXT :CURVAL.psz_string ) );
683 submenu->addMenu( subsubmenu );
687 var_Get( p_object, psz_var, &val );
688 another_val.psz_string = strdup( CURVAL.psz_string );
690 menutext = qfu( CURTEXT ? CURTEXT : another_val.psz_string );
691 CreateAndConnect( submenu, psz_var, menutext, "", NORMAL_OR_RADIO,
692 p_object->i_object_id, another_val, i_type,
693 NOTCOMMAND && val.psz_string &&
694 !strcmp( val.psz_string, CURVAL.psz_string ) );
696 if( val.psz_string ) free( val.psz_string );
699 case VLC_VAR_INTEGER:
700 var_Get( p_object, psz_var, &val );
701 if( CURTEXT ) menutext = qfu( CURTEXT );
702 else menutext.sprintf( "%d", CURVAL.i_int);
703 CreateAndConnect( submenu, psz_var, menutext, "", NORMAL_OR_RADIO,
704 p_object->i_object_id, CURVAL, i_type,
705 NOTCOMMAND && CURVAL.i_int == val.i_int );
709 var_Get( p_object, psz_var, &val );
710 if( CURTEXT ) menutext = qfu( CURTEXT );
711 else menutext.sprintf( "%.2f", CURVAL.f_float );
712 CreateAndConnect( submenu, psz_var, menutext, "", NORMAL_OR_RADIO,
713 p_object->i_object_id, CURVAL, i_type,
714 NOTCOMMAND && CURVAL.f_float == val.f_float );
722 /* clean up everything */
723 var_Change( p_object, psz_var, VLC_VAR_FREELIST, &val_list, &text_list );
725 #undef NORMAL_OR_RADIO
732 void QVLCMenu::CreateAndConnect( QMenu *menu, const char *psz_var,
733 QString text, QString help,
734 int i_item_type, int i_object_id,
735 vlc_value_t val, int i_val_type,
738 QAction *action = new QAction( text, menu );
739 action->setText( text );
740 action->setToolTip( help );
742 if( i_item_type == ITEM_CHECK )
744 action->setCheckable( true );
747 else if( i_item_type == ITEM_RADIO )
749 action->setCheckable( true );
751 currentGroup = new QActionGroup(menu);
752 currentGroup->addAction( action );
756 if( checked ) action->setChecked( true );
758 MenuItemData *itemData = new MenuItemData( i_object_id, i_val_type,
760 connect( action, SIGNAL(triggered()), THEDP->menusMapper, SLOT(map()) );
761 THEDP->menusMapper->setMapping( action, itemData );
762 menu->addAction( action );
765 void QVLCMenu::DoAction( intf_thread_t *p_intf, QObject *data )
767 MenuItemData *itemData = qobject_cast<MenuItemData *>(data);
768 vlc_object_t *p_object = (vlc_object_t *)vlc_object_get( p_intf,
769 itemData->i_object_id );
770 if( p_object == NULL ) return;
772 var_Set( p_object, itemData->psz_var, itemData->val );
773 vlc_object_release( p_object );