1 /*****************************************************************************
2 * menus.cpp : wxWindows plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2001 VideoLAN
5 * $Id: menus.cpp,v 1.21 2003/10/14 22:41:41 gbazin Exp $
7 * Authors: Gildas Bazin <gbazin@netcourrier.com>
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
28 #include <errno.h> /* ENOMEM */
29 #include <string.h> /* strerror() */
35 #include "wxwindows.h"
36 #include <wx/listctrl.h>
38 class wxMenuItemExt: public wxMenuItem
42 wxMenuItemExt( wxMenu* parentMenu, int id, const wxString& text,
43 const wxString& helpString, wxItemKind kind,
44 char *_psz_var, int _i_object_id, vlc_value_t _val,
47 virtual ~wxMenuItemExt();
58 /*****************************************************************************
60 *****************************************************************************/
62 /* IDs for the controls and the menu commands */
66 MenuDummy_Event = wxID_HIGHEST + 1000,
71 FirstAutoGenerated_Event = wxID_HIGHEST + 1999,
72 AudioMenu_Events = wxID_HIGHEST + 2000,
73 VideoMenu_Events = wxID_HIGHEST + 3000,
74 NavigMenu_Events = wxID_HIGHEST + 4000,
75 MiscMenu_Events = wxID_HIGHEST + 5000,
76 PopupMenu_Events = wxID_HIGHEST + 6000
79 BEGIN_EVENT_TABLE(Menu, wxMenu)
82 BEGIN_EVENT_TABLE(MenuEvtHandler, wxEvtHandler)
83 EVT_MENU(OpenFileSimple_Event, MenuEvtHandler::OnShowDialog)
84 EVT_MENU(OpenFile_Event, MenuEvtHandler::OnShowDialog)
85 EVT_MENU(OpenDisc_Event, MenuEvtHandler::OnShowDialog)
86 EVT_MENU(OpenNet_Event, MenuEvtHandler::OnShowDialog)
87 EVT_MENU(-1, MenuEvtHandler::OnMenuEvent)
90 wxMenu *OpenStreamMenu( intf_thread_t *p_intf )
92 wxMenu *menu = new wxMenu;
93 menu->Append( OpenFileSimple_Event, wxU(_("Simple &Open ...")) );
94 menu->Append( OpenFile_Event, wxU(_("Open &File...")) );
95 menu->Append( OpenDisc_Event, wxU(_("Open &Disc...")) );
96 menu->Append( OpenNet_Event, wxU(_("Open &Network Stream...")) );
100 void PopupMenu( intf_thread_t *p_intf, wxWindow *p_parent,
103 vlc_object_t *p_object;
104 char *ppsz_varnames[29];
108 /* Initializations */
109 memset( pi_objects, 0, 29 * sizeof(int) );
112 ppsz_varnames[i++] = _("Audio menu");
113 ppsz_varnames[i++] = NULL; /* Separator */
115 p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_AOUT,
117 if( p_object != NULL )
119 ppsz_varnames[i] = "audio-device";
120 pi_objects[i++] = p_object->i_object_id;
121 ppsz_varnames[i] = "audio-channels";
122 pi_objects[i++] = p_object->i_object_id;
123 vlc_object_release( p_object );
127 ppsz_varnames[i++] = NULL; /* Separator */
128 ppsz_varnames[i++] = _("Video menu");
129 ppsz_varnames[i++] = NULL; /* Separator */
131 p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_VOUT,
133 if( p_object != NULL )
135 ppsz_varnames[i] = "fullscreen";
136 pi_objects[i++] = p_object->i_object_id;
137 ppsz_varnames[i] = "deinterlace";
138 pi_objects[i++] = p_object->i_object_id;
139 ppsz_varnames[i] = "aspect-ratio";
140 pi_objects[i++] = p_object->i_object_id;
141 ppsz_varnames[i] = "crop";
142 pi_objects[i++] = p_object->i_object_id;
143 ppsz_varnames[i] = "directx-on-top";
144 pi_objects[i++] = p_object->i_object_id;
145 vlc_object_release( p_object );
149 ppsz_varnames[i++] = NULL; /* Separator */
150 ppsz_varnames[i++] = _("Input menu");
151 ppsz_varnames[i++] = NULL; /* Separator */
153 p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
155 if( p_object != NULL )
157 ppsz_varnames[i] = "title";
158 pi_objects[i++] = p_object->i_object_id;
159 ppsz_varnames[i] = "chapter";
160 pi_objects[i++] = p_object->i_object_id;
161 ppsz_varnames[i] = "program";
162 pi_objects[i++] = p_object->i_object_id;
163 ppsz_varnames[i] = "navigation";
164 pi_objects[i++] = p_object->i_object_id;
165 ppsz_varnames[i] = "dvd_menus";
166 pi_objects[i++] = p_object->i_object_id;
168 ppsz_varnames[i] = "video-es";
169 pi_objects[i++] = p_object->i_object_id;
170 ppsz_varnames[i] = "audio-es";
171 pi_objects[i++] = p_object->i_object_id;
172 ppsz_varnames[i] = "spu-es";
173 pi_objects[i++] = p_object->i_object_id;
175 vlc_object_release( p_object );
179 ppsz_varnames[i++] = NULL; /* Separator */
180 ppsz_varnames[i++] = _("Interface menu");
181 ppsz_varnames[i++] = NULL; /* Separator */
183 /* vlc_object_find is needed because of the dialogs provider case */
184 p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_INTF,
186 if( p_object != NULL )
188 ppsz_varnames[i] = "intf-switch";
189 pi_objects[i++] = p_object->i_object_id;
190 ppsz_varnames[i] = "intf-add";
191 pi_objects[i++] = p_object->i_object_id;
193 vlc_object_release( p_object );
197 Menu popupmenu( p_intf, p_parent, i,
198 ppsz_varnames, pi_objects, PopupMenu_Events );
201 /* Add static entries */
202 popupmenu.AppendSeparator();
203 popupmenu.Append( MenuDummy_Event, wxU("Open"),
204 OpenStreamMenu( p_intf ), wxT("") );
207 p_intf->p_sys->p_popup_menu = &popupmenu;
208 p_parent->PopupMenu( &popupmenu, pos.x, pos.y );
209 p_intf->p_sys->p_popup_menu = NULL;
212 wxMenu *AudioMenu( intf_thread_t *_p_intf, wxWindow *p_parent )
214 vlc_object_t *p_object;
215 char *ppsz_varnames[5];
219 /* Initializations */
220 memset( pi_objects, 0, 5 * sizeof(int) );
222 p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_AOUT,
224 if( p_object != NULL )
226 ppsz_varnames[i] = "audio-device";
227 pi_objects[i++] = p_object->i_object_id;
228 ppsz_varnames[i] = "audio-channels";
229 pi_objects[i++] = p_object->i_object_id;
230 vlc_object_release( p_object );
233 p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_INPUT,
235 if( p_object != NULL )
237 ppsz_varnames[i] = "audio-es";
238 pi_objects[i++] = p_object->i_object_id;
239 vlc_object_release( p_object );
243 return new Menu( _p_intf, p_parent, i,
244 ppsz_varnames, pi_objects, AudioMenu_Events );
247 wxMenu *VideoMenu( intf_thread_t *_p_intf, wxWindow *p_parent )
249 vlc_object_t *p_object;
250 char *ppsz_varnames[6];
254 /* Initializations */
255 memset( pi_objects, 0, 6 * sizeof(int) );
257 p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_VOUT,
259 if( p_object != NULL )
261 ppsz_varnames[i] = "fullscreen";
262 pi_objects[i++] = p_object->i_object_id;
263 ppsz_varnames[i] = "deinterlace";
264 pi_objects[i++] = p_object->i_object_id;
265 ppsz_varnames[i] = "aspect-ratio";
266 pi_objects[i++] = p_object->i_object_id;
267 ppsz_varnames[i] = "crop";
268 pi_objects[i++] = p_object->i_object_id;
269 ppsz_varnames[i] = "directx-on-top";
270 pi_objects[i++] = p_object->i_object_id;
271 vlc_object_release( p_object );
274 p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_INPUT,
276 if( p_object != NULL )
278 ppsz_varnames[i] = "video-es";
279 pi_objects[i++] = p_object->i_object_id;
280 ppsz_varnames[i] = "spu-es";
281 pi_objects[i++] = p_object->i_object_id;
282 vlc_object_release( p_object );
286 return new Menu( _p_intf, p_parent, i,
287 ppsz_varnames, pi_objects, VideoMenu_Events );
290 wxMenu *NavigMenu( intf_thread_t *_p_intf, wxWindow *p_parent )
292 vlc_object_t *p_object;
293 char *ppsz_varnames[10];
297 /* Initializations */
298 memset( pi_objects, 0, 4 * sizeof(int) );
300 p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_INPUT,
302 if( p_object != NULL )
304 ppsz_varnames[i] = "title";
305 pi_objects[i++] = p_object->i_object_id;
306 ppsz_varnames[i] = "chapter";
307 pi_objects[i++] = p_object->i_object_id;
308 ppsz_varnames[i] = "program";
309 pi_objects[i++] = p_object->i_object_id;
310 ppsz_varnames[i] = "navigation";
311 pi_objects[i++] = p_object->i_object_id;
312 ppsz_varnames[i] = "dvd_menus";
313 pi_objects[i++] = p_object->i_object_id;
315 ppsz_varnames[i] = "prev-title";
316 pi_objects[i++] = p_object->i_object_id;
317 ppsz_varnames[i] = "next-title";
318 pi_objects[i++] = p_object->i_object_id;
319 ppsz_varnames[i] = "prev-chapter";
320 pi_objects[i++] = p_object->i_object_id;
321 ppsz_varnames[i] = "next-chapter";
322 pi_objects[i++] = p_object->i_object_id;
324 vlc_object_release( p_object );
328 return new Menu( _p_intf, p_parent, i,
329 ppsz_varnames, pi_objects, NavigMenu_Events );
332 wxMenu *MiscMenu( intf_thread_t *_p_intf, wxWindow *p_parent )
334 vlc_object_t *p_object;
335 char *ppsz_varnames[10];
339 /* Initializations */
340 memset( pi_objects, 0, 4 * sizeof(int) );
342 p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_INTF,
344 if( p_object != NULL )
346 ppsz_varnames[i] = "intf-switch";
347 pi_objects[i++] = p_object->i_object_id;
348 ppsz_varnames[i] = "intf-add";
349 pi_objects[i++] = p_object->i_object_id;
350 vlc_object_release( p_object );
354 return new Menu( _p_intf, p_parent, i,
355 ppsz_varnames, pi_objects, MiscMenu_Events );
358 /*****************************************************************************
360 *****************************************************************************/
361 Menu::Menu( intf_thread_t *_p_intf, wxWindow *p_parent,
362 int i_count, char **ppsz_varnames, int *pi_objects,
363 int i_start_id ): wxMenu( )
365 vlc_object_t *p_object;
368 /* Initializations */
371 i_item_id = i_start_id;
373 for( i = 0; i < i_count; i++ )
375 if( !ppsz_varnames[i] )
383 Append( MenuDummy_Event, wxU(ppsz_varnames[i]) );
387 p_object = (vlc_object_t *)vlc_object_get( p_intf, pi_objects[i] );
388 if( p_object == NULL ) continue;
390 CreateMenuItem( this, ppsz_varnames[i], p_object );
391 vlc_object_release( p_object );
394 /* Special case for empty menus */
395 if( GetMenuItemCount() == 0 )
397 Append( MenuDummy_Event, wxU(_("Empty")) );
398 Enable( MenuDummy_Event, FALSE );
406 /*****************************************************************************
408 *****************************************************************************/
409 void Menu::CreateMenuItem( wxMenu *menu, char *psz_var,
410 vlc_object_t *p_object )
412 wxMenuItemExt *menuitem;
413 vlc_value_t val, text;
416 /* Check the type of the object variable */
417 i_type = var_Type( p_object, psz_var );
419 switch( i_type & VLC_VAR_TYPE )
423 case VLC_VAR_VARIABLE:
425 case VLC_VAR_INTEGER:
428 /* Variable doesn't exist or isn't handled */
432 /* Make sure we want to display the variable */
433 if( i_type & VLC_VAR_HASCHOICE )
435 var_Change( p_object, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
436 if( val.i_int == 0 ) return;
437 if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE && val.i_int == 1 )
441 /* Get the descriptive name of the variable */
442 var_Change( p_object, psz_var, VLC_VAR_GETTEXT, &text, NULL );
444 var_Get( p_object, psz_var, &val );
446 if( i_type & VLC_VAR_HASCHOICE )
448 menu->Append( MenuDummy_Event,
449 wxU(text.psz_string ? text.psz_string : psz_var),
450 CreateChoicesMenu( psz_var, p_object ),
451 wxT("")/* Nothing for now (maybe use a GETLONGTEXT) */ );
453 if( text.psz_string ) free( text.psz_string );
458 switch( i_type & VLC_VAR_TYPE )
461 menuitem = new wxMenuItemExt( menu, ++i_item_id,
462 wxU(text.psz_string ?
463 text.psz_string : psz_var),
464 wxT(""), wxITEM_NORMAL, strdup(psz_var),
465 p_object->i_object_id, val, i_type );
466 menu->Append( menuitem );
470 val.b_bool = !val.b_bool;
471 menuitem = new wxMenuItemExt( menu, ++i_item_id,
472 wxU(text.psz_string ?
473 text.psz_string : psz_var),
474 wxT(""), wxITEM_CHECK, strdup(psz_var),
475 p_object->i_object_id, val, i_type );
476 menu->Append( menuitem );
477 Check( i_item_id, val.b_bool ? FALSE : TRUE );
481 if( text.psz_string ) free( text.psz_string );
485 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
486 if( text.psz_string ) free( text.psz_string );
489 wxMenu *Menu::CreateChoicesMenu( char *psz_var, vlc_object_t *p_object )
491 vlc_value_t val, val_list, text_list;
494 /* Check the type of the object variable */
495 i_type = var_Type( p_object, psz_var );
497 /* Make sure we want to display the variable */
498 if( i_type & VLC_VAR_HASCHOICE )
500 var_Change( p_object, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
501 if( val.i_int == 0 ) return NULL;
502 if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE && val.i_int == 1 )
510 switch( i_type & VLC_VAR_TYPE )
514 case VLC_VAR_VARIABLE:
516 case VLC_VAR_INTEGER:
519 /* Variable doesn't exist or isn't handled */
523 if( var_Get( p_object, psz_var, &val ) < 0 )
528 if( var_Change( p_object, psz_var, VLC_VAR_GETLIST,
529 &val_list, &text_list ) < 0 )
531 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
535 wxMenu *menu = new wxMenu;
536 for( i = 0; i < val_list.p_list->i_count; i++ )
538 vlc_value_t another_val;
539 wxMenuItemExt *menuitem;
541 switch( i_type & VLC_VAR_TYPE )
543 case VLC_VAR_VARIABLE:
544 menu->Append( MenuDummy_Event,
545 wxU(text_list.p_list->p_values[i].psz_string ?
546 text_list.p_list->p_values[i].psz_string :
547 val_list.p_list->p_values[i].psz_string),
549 val_list.p_list->p_values[i].psz_string,
550 p_object ), wxT("") );
554 another_val.psz_string =
555 strdup(val_list.p_list->p_values[i].psz_string);
557 new wxMenuItemExt( menu, ++i_item_id,
558 wxU(text_list.p_list->p_values[i].psz_string ?
559 text_list.p_list->p_values[i].psz_string :
560 another_val.psz_string), wxT(""),
561 i_type & VLC_VAR_ISCOMMAND ?
562 wxITEM_NORMAL : wxITEM_RADIO,
564 p_object->i_object_id, another_val, i_type );
566 menu->Append( menuitem );
568 if( !strcmp( val.psz_string,
569 val_list.p_list->p_values[i].psz_string ) )
570 menu->Check( i_item_id, TRUE );
573 case VLC_VAR_INTEGER:
575 new wxMenuItemExt( menu, ++i_item_id,
576 text_list.p_list->p_values[i].psz_string ?
578 text_list.p_list->p_values[i].psz_string) :
579 wxString::Format(wxT("%d"),
580 val_list.p_list->p_values[i].i_int), wxT(""),
581 i_type & VLC_VAR_ISCOMMAND ?
582 wxITEM_NORMAL : wxITEM_RADIO,
584 p_object->i_object_id,
585 val_list.p_list->p_values[i], i_type );
587 menu->Append( menuitem );
589 if( val_list.p_list->p_values[i].i_int == val.i_int )
590 menu->Check( i_item_id, TRUE );
598 /* clean up everything */
599 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
600 var_Change( p_object, psz_var, VLC_VAR_FREELIST, &val_list, &text_list );
605 void Menu::OnShowDialog( wxCommandEvent& event )
607 if( p_intf->p_sys->pf_show_dialog )
611 switch( event.GetId() )
613 case OpenFileSimple_Event:
614 i_id = INTF_DIALOG_FILE_SIMPLE;
617 i_id = INTF_DIALOG_FILE;
620 i_id = INTF_DIALOG_DISC;
623 i_id = INTF_DIALOG_NET;
626 i_id = INTF_DIALOG_FILE;
631 p_intf->p_sys->pf_show_dialog( p_intf, i_id, 1, 0 );
635 /*****************************************************************************
636 * A small helper class which intercepts all popup menu events
637 *****************************************************************************/
638 MenuEvtHandler::MenuEvtHandler( intf_thread_t *_p_intf,
639 Interface *_p_main_interface )
641 /* Initializations */
643 p_main_interface = _p_main_interface;
646 MenuEvtHandler::~MenuEvtHandler()
650 void MenuEvtHandler::OnShowDialog( wxCommandEvent& event )
652 if( p_intf->p_sys->pf_show_dialog )
656 switch( event.GetId() )
658 case OpenFileSimple_Event:
659 i_id = INTF_DIALOG_FILE_SIMPLE;
662 i_id = INTF_DIALOG_FILE;
665 i_id = INTF_DIALOG_DISC;
668 i_id = INTF_DIALOG_NET;
671 i_id = INTF_DIALOG_FILE;
676 p_intf->p_sys->pf_show_dialog( p_intf, i_id, 1, 0 );
680 void MenuEvtHandler::OnMenuEvent( wxCommandEvent& event )
682 wxMenuItem *p_menuitem = NULL;
684 /* Check if this is an auto generated menu item */
685 if( event.GetId() < FirstAutoGenerated_Event )
691 if( !p_main_interface ||
692 (p_menuitem = p_main_interface->GetMenuBar()->FindItem(event.GetId()))
695 if( p_intf->p_sys->p_popup_menu )
698 p_intf->p_sys->p_popup_menu->FindItem( event.GetId() );
704 wxMenuItemExt *p_menuitemext = (wxMenuItemExt *)p_menuitem;
705 vlc_object_t *p_object;
707 p_object = (vlc_object_t *)vlc_object_get( p_intf,
708 p_menuitemext->i_object_id );
709 if( p_object == NULL ) return;
711 var_Set( p_object, p_menuitemext->psz_var, p_menuitemext->val );
713 vlc_object_release( p_object );
719 /*****************************************************************************
720 * A small helper class which encapsulate wxMenuitem with some other useful
722 *****************************************************************************/
723 wxMenuItemExt::wxMenuItemExt( wxMenu* parentMenu, int id, const wxString& text,
724 const wxString& helpString, wxItemKind kind,
725 char *_psz_var, int _i_object_id, vlc_value_t _val, int _i_val_type ):
726 wxMenuItem( parentMenu, id, text, helpString, kind )
728 /* Initializations */
730 i_val_type = _i_val_type;
731 i_object_id = _i_object_id;
735 wxMenuItemExt::~wxMenuItemExt()
737 if( psz_var ) free( psz_var );
738 if( ((i_val_type & VLC_VAR_TYPE) == VLC_VAR_STRING)
739 && val.psz_string ) free( val.psz_string );