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 ) );
140 QMenu *QVLCMenu::SDMenu( intf_thread_t *p_intf )
142 QMenu *menu = new QMenu();
143 menu->setTitle( qtr( "Services Discovery" ) );
144 playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
147 assert( p_playlist );
148 vlc_list_t *p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE,
151 for( int i_index = 0 ; i_index < p_list->i_count; i_index++ )
153 module_t * p_parser = (module_t *)p_list->p_values[i_index].p_object ;
154 if( !strcmp( p_parser->psz_capability, "services_discovery" ) )
157 if( i_num ) pp_sds = (char **)calloc( i_num, sizeof(void *) );
158 for( int i_index = 0 ; i_index < p_list->i_count; i_index++ )
160 module_t * p_parser = (module_t *)p_list->p_values[i_index].p_object;
161 if( !strcmp( p_parser->psz_capability, "services_discovery" ) )
163 QAction *a = menu->addAction(
164 qfu( p_parser->psz_longname ?
165 p_parser->psz_longname :
166 ( p_parser->psz_shortname ?
167 p_parser->psz_shortname :
168 p_parser->psz_object_name ) ) );
169 a->setCheckable( true );
170 /* hack to handle submodules properly */
172 while( p_parser->pp_shortcuts[++i] != NULL );
174 if( playlist_IsServicesDiscoveryLoaded( p_playlist,
175 i>=0?p_parser->pp_shortcuts[i] : p_parser->psz_object_name ) )
177 a->setChecked( true );
179 pp_sds[i_num++] = i>=0? p_parser->pp_shortcuts[i] :
180 p_parser->psz_object_name;
183 vlc_list_release( p_list );
184 vlc_object_release( p_playlist );
188 QMenu *QVLCMenu::FileMenu()
190 QMenu *menu = new QMenu();
191 DP_SADD( qtr("Quick &Open File...") , "", "", simpleOpenDialog() );
192 DP_SADD( qtr("&Advanced Open..." ), "", "", openDialog() );
193 menu->addSeparator();
194 DP_SADD( qtr("Streaming..."), "", "", streamingDialog() );
195 menu->addSeparator();
196 DP_SADD( qtr("&Quit") , "", "", quit() );
200 QMenu *QVLCMenu::ToolsMenu( intf_thread_t *p_intf, bool with_intf )
202 QMenu *menu = new QMenu();
205 QMenu *intfmenu = InterfacesMenu( p_intf, NULL );
206 intfmenu->setTitle( qtr("Interfaces" ) );
207 menu->addMenu( intfmenu );
208 /** \todo ADD EXT GUI HERE */
209 menu->addSeparator();
211 DP_SADD( qtr("Messages" ), "", "", messagesDialog() );
212 DP_SADD( qtr("Information") , "", "", streaminfoDialog() );
213 DP_SADD( qtr("Bookmarks"), "", "", bookmarksDialog() );
214 menu->addSeparator();
215 DP_SADD( qtr("Preferences"), "", "", prefsDialog() );
219 QMenu *QVLCMenu::InterfacesMenu( intf_thread_t *p_intf, QMenu *current )
222 vector<const char *> varnames;
223 /** \todo add "switch to XXX" */
224 varnames.push_back( "intf-add" );
225 objects.push_back( p_intf->i_object_id );
227 QMenu *menu = Populate( p_intf, current, varnames, objects );
228 connect( menu, SIGNAL( aboutToShow() ),
229 THEDP->menusUpdateMapper, SLOT(map()) );
230 THEDP->menusUpdateMapper->setMapping( menu, 4 );
235 QMenu *QVLCMenu::AudioMenu( intf_thread_t *p_intf, QMenu * current )
238 vector<const char *> varnames;
240 vlc_object_t *p_object = (vlc_object_t *)vlc_object_find( p_intf,
241 VLC_OBJECT_INPUT, FIND_ANYWHERE );
242 if( p_object != NULL )
244 PUSH_VAR( "audio-es" );
245 vlc_object_release( p_object );
248 p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_AOUT,
252 AudioAutoMenuBuilder( p_object, objects, varnames );
253 vlc_object_release( p_object );
255 return Populate( p_intf, current, varnames, objects );
259 QMenu *QVLCMenu::VideoMenu( intf_thread_t *p_intf, QMenu *current )
261 vlc_object_t *p_object;
263 vector<const char *> varnames;
265 p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
267 if( p_object != NULL )
269 PUSH_VAR( "video-es" );
270 PUSH_VAR( "spu-es" );
271 vlc_object_release( p_object );
274 p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_VOUT,
276 if( p_object != NULL )
278 VideoAutoMenuBuilder( p_object, objects, varnames );
279 vlc_object_release( p_object );
281 return Populate( p_intf, current, varnames, objects );
284 QMenu *QVLCMenu::NavigMenu( intf_thread_t *p_intf, QMenu *current )
286 vlc_object_t *p_object;
288 vector<const char *> varnames;
291 p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
293 if( p_object != NULL )
295 InputAutoMenuBuilder( p_object, objects, varnames );
296 PUSH_VAR( "prev-title"); PUSH_VAR ( "next-title" );
297 PUSH_VAR( "prev-chapter"); PUSH_VAR( "next-chapter" );
298 vlc_object_release( p_object );
300 return Populate( p_intf, current, varnames, objects );
304 /*****************************************************************************
306 *****************************************************************************/
307 #define POPUP_BOILERPLATE \
308 unsigned int i_last_separator = 0; \
309 vector<int> objects; \
310 vector<const char *> varnames; \
311 input_thread_t *p_input = THEMIM->getInput();
313 #define CREATE_POPUP \
314 QMenu *menu = new QMenu(); \
315 Populate( p_intf, menu, varnames, objects ); \
316 p_intf->p_sys->p_popup_menu = menu; \
317 menu->popup( QCursor::pos() ); \
318 p_intf->p_sys->p_popup_menu = NULL; \
319 i_last_separator = 0;
321 #define POPUP_STATIC_ENTRIES \
323 MIM_SADD( qtr("Stop"), "", "", stop() ); \
324 MIM_SADD( qtr("Previous"), "", "", prev() ); \
325 MIM_SADD( qtr("Next"), "", "", next() ); \
328 var_Get( p_input, "state", &val ); \
329 if( val.i_int == PAUSE_S ) \
330 MIM_SADD( qtr("Play"), "", "", togglePlayPause() ) \
332 MIM_SADD( qtr("Pause"), "", "", togglePlayPause() ) \
334 else if( THEPL->i_size && THEPL->i_enabled ) \
335 MIM_SADD( qtr("Play"), "", "", togglePlayPause() ) \
337 QMenu *intfmenu = InterfacesMenu( p_intf, NULL ); \
338 intfmenu->setTitle( qtr("Interfaces" ) ); \
339 menu->addMenu( intfmenu ); \
341 QMenu *toolsmenu = ToolsMenu( p_intf, false ); \
342 toolsmenu->setTitle( qtr("Tools" ) ); \
343 menu->addMenu( toolsmenu ); \
345 void QVLCMenu::VideoPopupMenu( intf_thread_t *p_intf )
350 vlc_object_yield( p_input );
351 varnames.push_back( "video-es" );
352 objects.push_back( p_input->i_object_id );
353 varnames.push_back( "spu-es" );
354 objects.push_back( p_input->i_object_id );
355 vlc_object_t *p_vout = (vlc_object_t *)vlc_object_find( p_input,
356 VLC_OBJECT_VOUT, FIND_CHILD );
359 VideoAutoMenuBuilder( p_vout, objects, varnames );
360 vlc_object_release( p_vout );
362 vlc_object_release( p_input );
367 void QVLCMenu::AudioPopupMenu( intf_thread_t *p_intf )
372 vlc_object_yield( p_input );
373 varnames.push_back( "audio-es" );
374 objects.push_back( p_input->i_object_id );
375 vlc_object_t *p_aout = (vlc_object_t *)vlc_object_find( p_input,
376 VLC_OBJECT_AOUT, FIND_ANYWHERE );
379 AudioAutoMenuBuilder( p_aout, objects, varnames );
380 vlc_object_release( p_aout );
382 vlc_object_release( p_input );
387 /* Navigation stuff, and general */
388 void QVLCMenu::MiscPopupMenu( intf_thread_t *p_intf )
393 vlc_object_yield( p_input );
394 varnames.push_back( "audio-es" );
395 InputAutoMenuBuilder( VLC_OBJECT(p_input), objects, varnames );
399 QMenu *menu = new QMenu();
400 Populate( p_intf, menu, varnames, objects );
401 menu->addSeparator();
402 POPUP_STATIC_ENTRIES;
404 p_intf->p_sys->p_popup_menu = menu;
405 menu->popup( QCursor::pos() );
406 p_intf->p_sys->p_popup_menu = NULL;
409 void QVLCMenu::PopupMenu( intf_thread_t *p_intf )
414 vlc_object_yield( p_input );
415 InputAutoMenuBuilder( VLC_OBJECT(p_input), objects, varnames );
419 varnames.push_back( "video-es" );
420 objects.push_back( p_input->i_object_id );
421 varnames.push_back( "spu-es" );
422 objects.push_back( p_input->i_object_id );
423 vlc_object_t *p_vout = (vlc_object_t *)vlc_object_find( p_input,
424 VLC_OBJECT_VOUT, FIND_CHILD );
427 VideoAutoMenuBuilder( p_vout, objects, varnames );
428 vlc_object_release( p_vout );
432 varnames.push_back( "audio-es" );
433 objects.push_back( p_input->i_object_id );
434 vlc_object_t *p_aout = (vlc_object_t *)vlc_object_find( p_input,
435 VLC_OBJECT_AOUT, FIND_ANYWHERE );
438 AudioAutoMenuBuilder( p_aout, objects, varnames );
439 vlc_object_release( p_aout );
443 QMenu *menu = new QMenu();
444 Populate( p_intf, menu, varnames, objects );
445 menu->addSeparator();
446 POPUP_STATIC_ENTRIES;
448 p_intf->p_sys->p_popup_menu = menu;
449 menu->popup( QCursor::pos() );
450 p_intf->p_sys->p_popup_menu = NULL;
454 #undef PUSH_SEPARATOR
456 /*************************************************************************
457 * Builders for automenus
458 *************************************************************************/
459 QMenu * QVLCMenu::Populate( intf_thread_t *p_intf, QMenu *current,
460 vector< const char *> & varnames,
461 vector<int> & objects, bool append )
463 QMenu *menu = current;
471 vlc_object_t *p_object;
472 vlc_bool_t b_section_empty = VLC_FALSE;
475 #define APPEND_EMPTY { QAction *action = menu->addAction( qtr("Empty" ) ); \
476 action->setEnabled( false ); }
478 for( i = 0; i < (int)objects.size() ; i++ )
480 if( !varnames[i] || !*varnames[i] )
482 if( b_section_empty )
484 menu->addSeparator();
485 b_section_empty = VLC_TRUE;
489 if( objects[i] == 0 )
491 /// \bug What is this ?
492 // Append( menu, varnames[i], NULL );
493 b_section_empty = VLC_FALSE;
497 p_object = (vlc_object_t *)vlc_object_get( p_intf,
499 if( p_object == NULL ) continue;
501 b_section_empty = VLC_FALSE;
502 /* Ugly specific stuff */
503 if( strstr(varnames[i], "intf-add" ) )
504 CreateItem( menu, varnames[i], p_object, false );
506 CreateItem( menu, varnames[i], p_object, true );
507 vlc_object_release( p_object );
510 /* Special case for empty menus */
511 if( menu->actions().size() == 0 || b_section_empty )
517 /*****************************************************************************
519 *****************************************************************************/
521 static bool IsMenuEmpty( const char *psz_var, vlc_object_t *p_object,
524 vlc_value_t val, val_list;
525 int i_type, i_result, i;
527 /* Check the type of the object variable */
528 i_type = var_Type( p_object, psz_var );
530 /* Check if we want to display the variable */
531 if( !(i_type & VLC_VAR_HASCHOICE) ) return FALSE;
533 var_Change( p_object, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
534 if( val.i_int == 0 ) return TRUE;
536 if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE )
538 /* Very evil hack ! intf-switch can have only one value */
539 if( !strcmp( psz_var, "intf-switch" ) ) return FALSE;
540 if( val.i_int == 1 && b_root ) return TRUE;
544 /* Check children variables in case of VLC_VAR_VARIABLE */
545 if( var_Change( p_object, psz_var, VLC_VAR_GETLIST, &val_list, NULL ) < 0 )
550 for( i = 0, i_result = TRUE; i < val_list.p_list->i_count; i++ )
552 if( !IsMenuEmpty( val_list.p_list->p_values[i].psz_string,
560 /* clean up everything */
561 var_Change( p_object, psz_var, VLC_VAR_FREELIST, &val_list, NULL );
566 void QVLCMenu::CreateItem( QMenu *menu, const char *psz_var,
567 vlc_object_t *p_object, bool b_submenu )
569 vlc_value_t val, text;
572 /* Check the type of the object variable */
573 i_type = var_Type( p_object, psz_var );
575 switch( i_type & VLC_VAR_TYPE )
579 case VLC_VAR_VARIABLE:
581 case VLC_VAR_INTEGER:
585 /* Variable doesn't exist or isn't handled */
589 /* Make sure we want to display the variable */
590 if( IsMenuEmpty( psz_var, p_object ) ) return;
592 /* Get the descriptive name of the variable */
593 var_Change( p_object, psz_var, VLC_VAR_GETTEXT, &text, NULL );
595 if( i_type & VLC_VAR_HASCHOICE )
597 /* Append choices menu */
600 QMenu *submenu = new QMenu();
601 submenu->setTitle( qfu( text.psz_string ?
602 text.psz_string : psz_var ) );
603 if( CreateChoicesMenu( submenu, psz_var, p_object, true ) == 0)
604 menu->addMenu( submenu );
607 CreateChoicesMenu( menu, psz_var, p_object, true );
608 FREENULL( text.psz_string );
612 #define TEXT_OR_VAR qfu ( text.psz_string ? text.psz_string : psz_var )
614 switch( i_type & VLC_VAR_TYPE )
617 var_Get( p_object, psz_var, &val );
618 CreateAndConnect( menu, psz_var, TEXT_OR_VAR, "", ITEM_NORMAL,
619 p_object->i_object_id, val, i_type );
623 var_Get( p_object, psz_var, &val );
624 val.b_bool = !val.b_bool;
625 CreateAndConnect( menu, psz_var, TEXT_OR_VAR, "", ITEM_CHECK,
626 p_object->i_object_id, val, i_type, val.b_bool );
629 FREENULL( text.psz_string );
633 int QVLCMenu::CreateChoicesMenu( QMenu *submenu, const char *psz_var,
634 vlc_object_t *p_object, bool b_root )
636 vlc_value_t val, val_list, text_list;
639 /* Check the type of the object variable */
640 i_type = var_Type( p_object, psz_var );
642 /* Make sure we want to display the variable */
643 if( IsMenuEmpty( psz_var, p_object, b_root ) ) return VLC_EGENERIC;
645 switch( i_type & VLC_VAR_TYPE )
649 case VLC_VAR_VARIABLE:
651 case VLC_VAR_INTEGER:
655 /* Variable doesn't exist or isn't handled */
659 if( var_Change( p_object, psz_var, VLC_VAR_GETLIST,
660 &val_list, &text_list ) < 0 )
664 #define NORMAL_OR_RADIO i_type & VLC_VAR_ISCOMMAND ? ITEM_NORMAL: ITEM_RADIO
665 #define NOTCOMMAND !(i_type & VLC_VAR_ISCOMMAND)
666 #define CURVAL val_list.p_list->p_values[i]
667 #define CURTEXT text_list.p_list->p_values[i].psz_string
669 for( i = 0; i < val_list.p_list->i_count; i++ )
671 vlc_value_t another_val;
673 QMenu *subsubmenu = new QMenu();
675 switch( i_type & VLC_VAR_TYPE )
677 case VLC_VAR_VARIABLE:
678 CreateChoicesMenu( subsubmenu, CURVAL.psz_string, p_object, false );
679 subsubmenu->setTitle( qfu( CURTEXT ? CURTEXT :CURVAL.psz_string ) );
680 submenu->addMenu( subsubmenu );
684 var_Get( p_object, psz_var, &val );
685 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 );
719 /* clean up everything */
720 var_Change( p_object, psz_var, VLC_VAR_FREELIST, &val_list, &text_list );
722 #undef NORMAL_OR_RADIO
729 void QVLCMenu::CreateAndConnect( QMenu *menu, const char *psz_var,
730 QString text, QString help,
731 int i_item_type, int i_object_id,
732 vlc_value_t val, int i_val_type,
735 QAction *action = new QAction( text, menu );
736 action->setText( text );
737 action->setToolTip( help );
739 if( i_item_type == ITEM_CHECK )
741 action->setCheckable( true );
744 else if( i_item_type == ITEM_RADIO )
746 action->setCheckable( true );
748 currentGroup = new QActionGroup(menu);
749 currentGroup->addAction( action );
753 if( checked ) action->setChecked( true );
755 MenuItemData *itemData = new MenuItemData( i_object_id, i_val_type,
757 connect( action, SIGNAL(triggered()), THEDP->menusMapper, SLOT(map()) );
758 THEDP->menusMapper->setMapping( action, itemData );
759 menu->addAction( action );
762 void QVLCMenu::DoAction( intf_thread_t *p_intf, QObject *data )
764 MenuItemData *itemData = qobject_cast<MenuItemData *>(data);
765 vlc_object_t *p_object = (vlc_object_t *)vlc_object_get( p_intf,
766 itemData->i_object_id );
767 if( p_object == NULL ) return;
769 var_Set( p_object, itemData->psz_var, itemData->val );
770 vlc_object_release( p_object );