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;
42 // Add static entries to menus
43 #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 ) ); } }
44 #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 ) ); } }
47 /*****************************************************************************
48 * Definitions of variables for the dynamic menus
49 *****************************************************************************/
50 #define PUSH_VAR( var ) varnames.push_back( var ); \
51 objects.push_back( p_object->i_object_id )
53 #define PUSH_SEPARATOR if( objects.size() != i_last_separator ) { \
54 objects.push_back( 0 ); varnames.push_back( "" ); \
55 i_last_separator = objects.size(); }
57 static int InputAutoMenuBuilder( vlc_object_t *p_object,
59 vector<const char *> &varnames )
61 PUSH_VAR( "bookmark");
63 PUSH_VAR ("chapter" );
64 PUSH_VAR( "program" );
65 PUSH_VAR( "navigation" );
66 PUSH_VAR( "dvd_menus" );
70 static int VideoAutoMenuBuilder( vlc_object_t *p_object,
72 vector<const char *> &varnames )
74 PUSH_VAR( "fullscreen" );
76 PUSH_VAR( "deinterlace" );
77 PUSH_VAR( "aspect-ratio" );
79 PUSH_VAR( "video-on-top" );
80 PUSH_VAR( "directx-wallpaper" );
81 PUSH_VAR( "video-snapshot" );
83 vlc_object_t *p_dec_obj = (vlc_object_t *)vlc_object_find( p_object,
86 if( p_dec_obj != NULL )
88 vlc_object_t *p_object = p_dec_obj;
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" ) );
130 QMenu *QVLCMenu::FileMenu()
132 QMenu *menu = new QMenu();
133 DP_SADD( qtr("Quick &Open File...") , "", "", simpleOpenDialog() );
134 DP_SADD( qtr("&Advanced Open..." ), "", "", openDialog() );
135 menu->addSeparator();
136 DP_SADD( qtr("Streaming..."), "", "", streamingDialog() );
137 menu->addSeparator();
138 DP_SADD( qtr("&Quit") , "", "", quit() );
142 QMenu *QVLCMenu::ToolsMenu( intf_thread_t *p_intf, bool with_intf )
144 QMenu *menu = new QMenu();
147 QMenu *intfmenu = InterfacesMenu( p_intf, NULL );
148 intfmenu->setTitle( qtr("Interfaces" ) );
149 menu->addMenu( intfmenu );
150 /** \todo ADD EXT GUI HERE */
151 menu->addSeparator();
153 DP_SADD( qtr("Messages" ), "", "", messagesDialog() );
154 DP_SADD( qtr("Information") , "", "", streaminfoDialog() );
155 DP_SADD( qtr("Bookmarks"), "", "", bookmarksDialog() );
156 menu->addSeparator();
157 DP_SADD( qtr("Preferences"), "", "", prefsDialog() );
161 QMenu *QVLCMenu::InterfacesMenu( intf_thread_t *p_intf, QMenu *current )
164 vector<const char *> varnames;
165 /** \todo add "switch to XXX" */
166 varnames.push_back( "intf-add" );
167 objects.push_back( p_intf->i_object_id );
169 QMenu *menu = Populate( p_intf, current, varnames, objects );
170 connect( menu, SIGNAL( aboutToShow() ),
171 THEDP->menusUpdateMapper, SLOT(map()) );
172 THEDP->menusUpdateMapper->setMapping( menu, 4 );
177 QMenu *QVLCMenu::AudioMenu( intf_thread_t *p_intf, QMenu * current )
180 vector<const char *> varnames;
182 vlc_object_t *p_object = (vlc_object_t *)vlc_object_find( p_intf,
183 VLC_OBJECT_INPUT, FIND_ANYWHERE );
184 if( p_object != NULL )
186 PUSH_VAR( "audio-es" );
187 vlc_object_release( p_object );
190 p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_AOUT,
194 AudioAutoMenuBuilder( p_object, objects, varnames );
195 vlc_object_release( p_object );
197 return Populate( p_intf, current, varnames, objects );
201 QMenu *QVLCMenu::VideoMenu( intf_thread_t *p_intf, QMenu *current )
203 vlc_object_t *p_object;
205 vector<const char *> varnames;
207 p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
209 if( p_object != NULL )
211 PUSH_VAR( "video-es" );
212 PUSH_VAR( "spu-es" );
213 vlc_object_release( p_object );
216 p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_VOUT,
218 if( p_object != NULL )
220 VideoAutoMenuBuilder( p_object, objects, varnames );
221 vlc_object_release( p_object );
223 return Populate( p_intf, current, varnames, objects );
226 QMenu *QVLCMenu::NavigMenu( intf_thread_t *p_intf, QMenu *current )
228 vlc_object_t *p_object;
230 vector<const char *> varnames;
233 p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
235 if( p_object != NULL )
237 InputAutoMenuBuilder( p_object, objects, varnames );
238 PUSH_VAR( "prev-title"); PUSH_VAR ( "next-title" );
239 PUSH_VAR( "prev-chapter"); PUSH_VAR( "next-chapter" );
240 vlc_object_release( p_object );
242 return Populate( p_intf, current, varnames, objects );
246 /*****************************************************************************
248 *****************************************************************************/
249 #define POPUP_BOILERPLATE \
250 unsigned int i_last_separator = 0; \
251 vector<int> objects; \
252 vector<const char *> varnames; \
253 input_thread_t *p_input = THEMIM->getInput();
255 #define CREATE_POPUP \
256 QMenu *menu = new QMenu(); \
257 Populate( p_intf, menu, varnames, objects ); \
258 p_intf->p_sys->p_popup_menu = menu; \
259 menu->popup( QCursor::pos() ); \
260 p_intf->p_sys->p_popup_menu = NULL; \
261 i_last_separator = 0;
263 #define POPUP_STATIC_ENTRIES \
265 MIM_SADD( qtr("Stop"), "", "", stop() ); \
266 MIM_SADD( qtr("Previous"), "", "", prev() ); \
267 MIM_SADD( qtr("Next"), "", "", next() ); \
270 var_Get( p_input, "state", &val ); \
271 if( val.i_int == PAUSE_S ) \
272 MIM_SADD( qtr("Play"), "", "", togglePlayPause() ) \
274 MIM_SADD( qtr("Pause"), "", "", togglePlayPause() ) \
276 else if( THEPL->i_size && THEPL->i_enabled ) \
277 MIM_SADD( qtr("Play"), "", "", togglePlayPause() ) \
279 QMenu *intfmenu = InterfacesMenu( p_intf, NULL ); \
280 intfmenu->setTitle( qtr("Interfaces" ) ); \
281 menu->addMenu( intfmenu ); \
283 QMenu *toolsmenu = ToolsMenu( p_intf, false ); \
284 toolsmenu->setTitle( qtr("Tools" ) ); \
285 menu->addMenu( toolsmenu ); \
287 void QVLCMenu::VideoPopupMenu( intf_thread_t *p_intf )
292 vlc_object_yield( p_input );
293 varnames.push_back( "video-es" );
294 objects.push_back( p_input->i_object_id );
295 varnames.push_back( "spu-es" );
296 objects.push_back( p_input->i_object_id );
297 vlc_object_t *p_vout = (vlc_object_t *)vlc_object_find( p_input,
298 VLC_OBJECT_VOUT, FIND_CHILD );
301 VideoAutoMenuBuilder( p_vout, objects, varnames );
302 vlc_object_release( p_vout );
304 vlc_object_release( p_input );
309 void QVLCMenu::AudioPopupMenu( intf_thread_t *p_intf )
314 vlc_object_yield( p_input );
315 varnames.push_back( "audio-es" );
316 objects.push_back( p_input->i_object_id );
317 vlc_object_t *p_aout = (vlc_object_t *)vlc_object_find( p_input,
318 VLC_OBJECT_AOUT, FIND_ANYWHERE );
321 AudioAutoMenuBuilder( p_aout, objects, varnames );
322 vlc_object_release( p_aout );
324 vlc_object_release( p_input );
329 /* Navigation stuff, and general */
330 void QVLCMenu::MiscPopupMenu( intf_thread_t *p_intf )
335 vlc_object_yield( p_input );
336 varnames.push_back( "audio-es" );
337 InputAutoMenuBuilder( VLC_OBJECT(p_input), objects, varnames );
341 QMenu *menu = new QMenu();
342 Populate( p_intf, menu, varnames, objects );
343 menu->addSeparator();
344 POPUP_STATIC_ENTRIES;
346 p_intf->p_sys->p_popup_menu = menu;
347 menu->popup( QCursor::pos() );
348 p_intf->p_sys->p_popup_menu = NULL;
351 void QVLCMenu::PopupMenu( intf_thread_t *p_intf )
356 vlc_object_yield( p_input );
357 InputAutoMenuBuilder( VLC_OBJECT(p_input), objects, varnames );
361 varnames.push_back( "video-es" );
362 objects.push_back( p_input->i_object_id );
363 varnames.push_back( "spu-es" );
364 objects.push_back( p_input->i_object_id );
365 vlc_object_t *p_vout = (vlc_object_t *)vlc_object_find( p_input,
366 VLC_OBJECT_VOUT, FIND_CHILD );
369 VideoAutoMenuBuilder( p_vout, objects, varnames );
370 vlc_object_release( p_vout );
374 varnames.push_back( "audio-es" );
375 objects.push_back( p_input->i_object_id );
376 vlc_object_t *p_aout = (vlc_object_t *)vlc_object_find( p_input,
377 VLC_OBJECT_AOUT, FIND_ANYWHERE );
380 AudioAutoMenuBuilder( p_aout, objects, varnames );
381 vlc_object_release( p_aout );
385 QMenu *menu = new QMenu();
386 Populate( p_intf, menu, varnames, objects );
387 menu->addSeparator();
388 POPUP_STATIC_ENTRIES;
390 p_intf->p_sys->p_popup_menu = menu;
391 menu->popup( QCursor::pos() );
392 p_intf->p_sys->p_popup_menu = NULL;
396 #undef PUSH_SEPARATOR
398 /*************************************************************************
399 * Builders for automenus
400 *************************************************************************/
401 QMenu * QVLCMenu::Populate( intf_thread_t *p_intf, QMenu *current,
402 vector< const char *> & varnames,
403 vector<int> & objects, bool append )
405 QMenu *menu = current;
413 vlc_object_t *p_object;
414 vlc_bool_t b_section_empty = VLC_FALSE;
417 #define APPEND_EMPTY { QAction *action = menu->addAction( qtr("Empty" ) ); \
418 action->setEnabled( false ); }
420 for( i = 0; i < (int)objects.size() ; i++ )
422 if( !varnames[i] || !*varnames[i] )
424 if( b_section_empty )
426 menu->addSeparator();
427 b_section_empty = VLC_TRUE;
431 if( objects[i] == 0 )
433 /// \bug What is this ?
434 // Append( menu, varnames[i], NULL );
435 b_section_empty = VLC_FALSE;
439 p_object = (vlc_object_t *)vlc_object_get( p_intf,
441 if( p_object == NULL ) continue;
443 b_section_empty = VLC_FALSE;
444 /* Ugly specific stuff */
445 if( strstr(varnames[i], "intf-add" ) )
446 CreateItem( menu, varnames[i], p_object, false );
448 CreateItem( menu, varnames[i], p_object, true );
449 vlc_object_release( p_object );
452 /* Special case for empty menus */
453 if( menu->actions().size() == 0 || b_section_empty )
459 /*****************************************************************************
461 *****************************************************************************/
463 static bool IsMenuEmpty( const char *psz_var, vlc_object_t *p_object,
466 vlc_value_t val, val_list;
467 int i_type, i_result, i;
469 /* Check the type of the object variable */
470 i_type = var_Type( p_object, psz_var );
472 /* Check if we want to display the variable */
473 if( !(i_type & VLC_VAR_HASCHOICE) ) return FALSE;
475 var_Change( p_object, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
476 if( val.i_int == 0 ) return TRUE;
478 if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE )
480 /* Very evil hack ! intf-switch can have only one value */
481 if( !strcmp( psz_var, "intf-switch" ) ) return FALSE;
482 if( val.i_int == 1 && b_root ) return TRUE;
486 /* Check children variables in case of VLC_VAR_VARIABLE */
487 if( var_Change( p_object, psz_var, VLC_VAR_GETLIST, &val_list, NULL ) < 0 )
492 for( i = 0, i_result = TRUE; i < val_list.p_list->i_count; i++ )
494 if( !IsMenuEmpty( val_list.p_list->p_values[i].psz_string,
502 /* clean up everything */
503 var_Change( p_object, psz_var, VLC_VAR_FREELIST, &val_list, NULL );
508 void QVLCMenu::CreateItem( QMenu *menu, const char *psz_var,
509 vlc_object_t *p_object, bool b_submenu )
511 vlc_value_t val, text;
514 /* Check the type of the object variable */
515 i_type = var_Type( p_object, psz_var );
517 switch( i_type & VLC_VAR_TYPE )
521 case VLC_VAR_VARIABLE:
523 case VLC_VAR_INTEGER:
527 /* Variable doesn't exist or isn't handled */
531 /* Make sure we want to display the variable */
532 if( IsMenuEmpty( psz_var, p_object ) ) return;
534 /* Get the descriptive name of the variable */
535 var_Change( p_object, psz_var, VLC_VAR_GETTEXT, &text, NULL );
537 if( i_type & VLC_VAR_HASCHOICE )
539 /* Append choices menu */
542 QMenu *submenu = new QMenu();
543 submenu->setTitle( qfu( text.psz_string ?
544 text.psz_string : psz_var ) );
545 if( CreateChoicesMenu( submenu, psz_var, p_object, true ) == 0)
546 menu->addMenu( submenu );
549 CreateChoicesMenu( menu, psz_var, p_object, true );
550 FREENULL( text.psz_string );
554 #define TEXT_OR_VAR qfu ( text.psz_string ? text.psz_string : psz_var )
556 switch( i_type & VLC_VAR_TYPE )
559 var_Get( p_object, psz_var, &val );
560 CreateAndConnect( menu, psz_var, TEXT_OR_VAR, "", ITEM_NORMAL,
561 p_object->i_object_id, val, i_type );
565 var_Get( p_object, psz_var, &val );
566 CreateAndConnect( menu, psz_var, TEXT_OR_VAR, "", ITEM_CHECK,
567 p_object->i_object_id, val, i_type, val.b_bool );
570 FREENULL( text.psz_string );
574 int QVLCMenu::CreateChoicesMenu( QMenu *submenu, const char *psz_var,
575 vlc_object_t *p_object, bool b_root )
577 vlc_value_t val, val_list, text_list;
580 /* Check the type of the object variable */
581 i_type = var_Type( p_object, psz_var );
583 /* Make sure we want to display the variable */
584 if( IsMenuEmpty( psz_var, p_object, b_root ) ) return VLC_EGENERIC;
586 switch( i_type & VLC_VAR_TYPE )
590 case VLC_VAR_VARIABLE:
592 case VLC_VAR_INTEGER:
596 /* Variable doesn't exist or isn't handled */
600 if( var_Change( p_object, psz_var, VLC_VAR_GETLIST,
601 &val_list, &text_list ) < 0 )
605 #define NORMAL_OR_RADIO i_type & VLC_VAR_ISCOMMAND ? ITEM_NORMAL: ITEM_RADIO
606 #define NOTCOMMAND !(i_type & VLC_VAR_ISCOMMAND)
607 #define CURVAL val_list.p_list->p_values[i]
608 #define CURTEXT text_list.p_list->p_values[i].psz_string
610 for( i = 0; i < val_list.p_list->i_count; i++ )
612 vlc_value_t another_val;
614 QMenu *subsubmenu = new QMenu();
616 switch( i_type & VLC_VAR_TYPE )
618 case VLC_VAR_VARIABLE:
619 CreateChoicesMenu( subsubmenu, CURVAL.psz_string, p_object, false );
620 subsubmenu->setTitle( qfu( CURTEXT ? CURTEXT :CURVAL.psz_string ) );
621 submenu->addMenu( subsubmenu );
625 var_Get( p_object, psz_var, &val );
626 another_val.psz_string = strdup( CURVAL.psz_string );
627 menutext = qfu( CURTEXT ? CURTEXT : another_val.psz_string );
628 CreateAndConnect( submenu, psz_var, menutext, "", NORMAL_OR_RADIO,
629 p_object->i_object_id, another_val, i_type,
630 NOTCOMMAND && val.psz_string &&
631 !strcmp( val.psz_string, CURVAL.psz_string ) );
633 if( val.psz_string ) free( val.psz_string );
636 case VLC_VAR_INTEGER:
637 var_Get( p_object, psz_var, &val );
638 if( CURTEXT ) menutext = qfu( CURTEXT );
639 else menutext.sprintf( "%d", CURVAL.i_int);
640 CreateAndConnect( submenu, psz_var, menutext, "", NORMAL_OR_RADIO,
641 p_object->i_object_id, CURVAL, i_type,
642 NOTCOMMAND && CURVAL.i_int == val.i_int );
646 var_Get( p_object, psz_var, &val );
647 if( CURTEXT ) menutext = qfu( CURTEXT );
648 else menutext.sprintf( "%.2f", CURVAL.f_float );
649 CreateAndConnect( submenu, psz_var, menutext, "", NORMAL_OR_RADIO,
650 p_object->i_object_id, CURVAL, i_type,
651 NOTCOMMAND && CURVAL.f_float == val.f_float );
660 /* clean up everything */
661 var_Change( p_object, psz_var, VLC_VAR_FREELIST, &val_list, &text_list );
663 #undef NORMAL_OR_RADIO
670 void QVLCMenu::CreateAndConnect( QMenu *menu, const char *psz_var,
671 QString text, QString help,
672 int i_item_type, int i_object_id,
673 vlc_value_t val, int i_val_type,
676 QAction *action = new QAction( text, menu );
677 action->setText( text );
678 action->setToolTip( help );
680 if( i_item_type == ITEM_CHECK )
682 action->setCheckable( true );
684 else if( i_item_type == ITEM_RADIO )
686 action->setCheckable( true );
688 currentGroup = new QActionGroup(menu);
689 currentGroup->addAction( action );
694 action->setChecked( true );
696 MenuItemData *itemData = new MenuItemData( i_object_id, i_val_type,
698 connect( action, SIGNAL(triggered()), THEDP->menusMapper, SLOT(map()) );
699 THEDP->menusMapper->setMapping( action, itemData );
700 menu->addAction( action );
703 void QVLCMenu::DoAction( intf_thread_t *p_intf, QObject *data )
705 MenuItemData *itemData = qobject_cast<MenuItemData *>(data);
706 vlc_object_t *p_object = (vlc_object_t *)vlc_object_get( p_intf,
707 itemData->i_object_id );
708 if( p_object == NULL ) return;
710 var_Set( p_object, itemData->psz_var, itemData->val );
711 vlc_object_release( p_object );