1 /*****************************************************************************
2 * menus.cpp : wxWindows plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2001 VideoLAN
5 * $Id: menus.cpp,v 1.9 2003/05/15 15:59:35 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() */
34 #ifdef WIN32 /* mingw32 hack */
39 /* Let vlc take care of the i18n stuff */
40 #define WXINTL_NO_GETTEXT_MACRO
42 #include <wx/wxprec.h>
44 #include <wx/listctrl.h>
48 #include "wxwindows.h"
50 class wxMenuItemExt: public wxMenuItem
54 wxMenuItemExt( wxMenu* parentMenu, int id, const wxString& text,
55 const wxString& helpString, wxItemKind kind,
56 char *_psz_var, int _i_object_id, vlc_value_t _val,
59 virtual ~wxMenuItemExt();
70 /*****************************************************************************
72 *****************************************************************************/
74 /* IDs for the controls and the menu commands */
78 FirstAutoGenerated_Event = wxID_HIGHEST + 1000,
81 VideoMenu_Events = wxID_HIGHEST + 1100,
82 NavigMenu_Events = wxID_HIGHEST + 1200,
83 PopupMenu_Events = wxID_HIGHEST + 1300,
86 BEGIN_EVENT_TABLE(Menu, wxMenu)
88 EVT_MENU(MenuDummy_Event, Menu::OnEntrySelected)
91 BEGIN_EVENT_TABLE(MenuEvtHandler, wxEvtHandler)
92 EVT_MENU(-1, MenuEvtHandler::OnMenuEvent)
95 void PopupMenu( intf_thread_t *_p_intf, Interface *_p_main_interface,
98 vlc_object_t *p_object;
99 char *ppsz_varnames[19];
103 /* Initializations */
104 memset( pi_objects, 0, 19 * sizeof(int) );
107 ppsz_varnames[i++] = _("Audio menu");
108 ppsz_varnames[i++] = NULL; /* Separator */
110 p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_AOUT,
112 if( p_object != NULL )
114 ppsz_varnames[i] = "audio-device";
115 pi_objects[i++] = p_object->i_object_id;
116 ppsz_varnames[i] = "audio-channels";
117 pi_objects[i++] = p_object->i_object_id;
118 vlc_object_release( p_object );
122 ppsz_varnames[i++] = NULL; /* Separator */
123 ppsz_varnames[i++] = _("Video menu");
124 ppsz_varnames[i++] = NULL; /* Separator */
126 p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_VOUT,
128 if( p_object != NULL )
130 ppsz_varnames[i] = "fullscreen";
131 pi_objects[i++] = p_object->i_object_id;
132 vlc_object_release( p_object );
136 ppsz_varnames[i++] = NULL; /* Separator */
137 ppsz_varnames[i++] = _("Input menu");
138 ppsz_varnames[i++] = NULL; /* Separator */
140 p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_INPUT,
142 if( p_object != NULL )
144 ppsz_varnames[i] = "title";
145 pi_objects[i++] = p_object->i_object_id;
146 ppsz_varnames[i] = "chapter";
147 pi_objects[i++] = p_object->i_object_id;
148 ppsz_varnames[i] = "navigation";
149 pi_objects[i++] = p_object->i_object_id;
150 ppsz_varnames[i] = "program";
151 pi_objects[i++] = p_object->i_object_id;
153 ppsz_varnames[i] = "video-es";
154 pi_objects[i++] = p_object->i_object_id;
155 ppsz_varnames[i] = "audio-es";
156 pi_objects[i++] = p_object->i_object_id;
157 ppsz_varnames[i] = "spu-es";
158 pi_objects[i++] = p_object->i_object_id;
160 vlc_object_release( p_object );
164 ppsz_varnames[i++] = NULL; /* Separator */
165 ppsz_varnames[i++] = _("Close");
168 Menu popupmenu( _p_intf, _p_main_interface, i,
169 ppsz_varnames, pi_objects, PopupMenu_Events );
171 _p_main_interface->p_popup_menu = &popupmenu;
172 _p_main_interface->PopupMenu( &popupmenu, pos.x, pos.y );
175 wxMenu *AudioMenu( intf_thread_t *_p_intf, Interface *_p_main_interface )
177 vlc_object_t *p_object;
178 char *ppsz_varnames[5];
182 /* Initializations */
183 memset( pi_objects, 0, 5 * sizeof(int) );
185 p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_AOUT,
187 if( p_object != NULL )
189 ppsz_varnames[i] = "audio-device";
190 pi_objects[i++] = p_object->i_object_id;
191 ppsz_varnames[i] = "audio-channels";
192 pi_objects[i++] = p_object->i_object_id;
193 vlc_object_release( p_object );
196 p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_INPUT,
198 if( p_object != NULL )
200 ppsz_varnames[i] = "audio-es";
201 pi_objects[i++] = p_object->i_object_id;
202 vlc_object_release( p_object );
206 return new Menu( _p_intf, _p_main_interface, i,
207 ppsz_varnames, pi_objects, AudioMenu_Events );
210 wxMenu *VideoMenu( intf_thread_t *_p_intf, Interface *_p_main_interface )
212 vlc_object_t *p_object;
213 char *ppsz_varnames[4];
217 /* Initializations */
218 memset( pi_objects, 0, 4 * sizeof(int) );
220 p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_VOUT,
222 if( p_object != NULL )
224 ppsz_varnames[i] = "fullscreen";
225 pi_objects[i++] = p_object->i_object_id;
226 vlc_object_release( p_object );
229 p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_INPUT,
231 if( p_object != NULL )
233 ppsz_varnames[i] = "video-es";
234 pi_objects[i++] = p_object->i_object_id;
235 ppsz_varnames[i] = "spu-es";
236 pi_objects[i++] = p_object->i_object_id;
237 vlc_object_release( p_object );
241 return new Menu( _p_intf, _p_main_interface, i,
242 ppsz_varnames, pi_objects, VideoMenu_Events );
245 wxMenu *NavigMenu( intf_thread_t *_p_intf, Interface *_p_main_interface )
247 vlc_object_t *p_object;
248 char *ppsz_varnames[6];
252 /* Initializations */
253 memset( pi_objects, 0, 4 * sizeof(int) );
255 p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_INPUT,
257 if( p_object != NULL )
259 ppsz_varnames[i] = "title";
260 pi_objects[i++] = p_object->i_object_id;
261 ppsz_varnames[i] = "chapter";
262 pi_objects[i++] = p_object->i_object_id;
263 ppsz_varnames[i] = "navigation";
264 pi_objects[i++] = p_object->i_object_id;
265 vlc_object_release( p_object );
266 ppsz_varnames[i] = "program";
267 pi_objects[i++] = p_object->i_object_id;
271 return new Menu( _p_intf, _p_main_interface, i,
272 ppsz_varnames, pi_objects, NavigMenu_Events );
275 /*****************************************************************************
277 *****************************************************************************/
278 Menu::Menu( intf_thread_t *_p_intf, Interface *_p_main_interface,
279 int i_count, char **ppsz_varnames, int *pi_objects,
280 int i_start_id ): wxMenu( )
282 vlc_object_t *p_object;
285 /* Initializations */
287 p_main_interface = _p_main_interface;
289 i_item_id = i_start_id;
291 for( i = 0; i < i_count; i++ )
293 if( !ppsz_varnames[i] )
301 Append( MenuDummy_Event, wxU(ppsz_varnames[i]) );
305 p_object = (vlc_object_t *)vlc_object_get( p_intf, pi_objects[i] );
306 if( p_object == NULL ) continue;
308 CreateMenuItem( this, ppsz_varnames[i], p_object );
309 vlc_object_release( p_object );
312 /* Special case for empty menus */
313 if( GetMenuItemCount() == 0 )
315 Append( MenuDummy_Event, wxU(_("Empty")) );
316 Enable( MenuDummy_Event, FALSE );
324 /*****************************************************************************
326 *****************************************************************************/
327 void Menu::OnEntrySelected( wxCommandEvent& WXUNUSED(event) )
331 void Menu::CreateMenuItem( wxMenu *menu, char *psz_var,
332 vlc_object_t *p_object )
334 wxMenuItemExt *menuitem;
335 vlc_value_t val, text;
338 /* Check the type of the object variable */
339 i_type = var_Type( p_object, psz_var );
341 switch( i_type & VLC_VAR_TYPE )
345 case VLC_VAR_VARIABLE:
347 case VLC_VAR_INTEGER:
350 /* Variable doesn't exist or isn't handled */
354 /* Make sure we want to display the variable */
355 if( i_type & VLC_VAR_HASCHOICE )
357 var_Change( p_object, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
358 if( val.i_int == 0 ) return;
361 /* Get the descriptive name of the variable */
362 var_Change( p_object, psz_var, VLC_VAR_GETTEXT, &text, NULL );
364 if( i_type & VLC_VAR_HASCHOICE )
366 menu->Append( MenuDummy_Event,
367 wxU(text.psz_string ? text.psz_string : psz_var),
368 CreateChoicesMenu( psz_var, p_object ),
369 wxT("")/* Nothing for now (maybe use a GETLONGTEXT) */ );
371 if( text.psz_string ) free( text.psz_string );
376 switch( i_type & VLC_VAR_TYPE )
379 menuitem = new wxMenuItemExt( menu, ++i_item_id,
380 wxU(text.psz_string ?
381 text.psz_string : psz_var),
382 wxT(""), wxITEM_NORMAL, strdup(psz_var),
383 p_object->i_object_id, val, i_type );
384 menu->Append( menuitem );
388 menuitem = new wxMenuItemExt( menu, ++i_item_id,
389 wxU(text.psz_string ?
390 text.psz_string : psz_var),
391 wxT(""), wxITEM_CHECK, strdup(psz_var),
392 p_object->i_object_id, val, i_type );
393 menu->Append( menuitem );
394 Check( i_item_id -1, val.b_bool ? FALSE : TRUE );
398 if( text.psz_string ) free( text.psz_string );
402 if( text.psz_string ) free( text.psz_string );
405 wxMenu *Menu::CreateChoicesMenu( char *psz_var, vlc_object_t *p_object )
407 vlc_value_t val, val_list, text_list;
410 /* Check the type of the object variable */
411 i_type = var_Type( p_object, psz_var );
413 /* Make sure we want to display the variable */
414 if( i_type & VLC_VAR_HASCHOICE )
416 var_Change( p_object, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
417 if( val.i_int == 0 ) return NULL;
424 switch( i_type & VLC_VAR_TYPE )
428 case VLC_VAR_VARIABLE:
430 case VLC_VAR_INTEGER:
433 /* Variable doesn't exist or isn't handled */
437 if( var_Get( p_object, psz_var, &val ) < 0 )
442 if( var_Change( p_object, psz_var, VLC_VAR_GETLIST,
443 &val_list, &text_list ) < 0 )
445 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
449 wxMenu *menu = new wxMenu;
450 for( i = 0; i < val_list.p_list->i_count; i++ )
452 vlc_value_t another_val;
453 wxMenuItemExt *menuitem;
455 switch( i_type & VLC_VAR_TYPE )
457 case VLC_VAR_VARIABLE:
458 menu->Append( MenuDummy_Event,
459 wxU(text_list.p_list->p_values[i].psz_string ?
460 text_list.p_list->p_values[i].psz_string :
461 val_list.p_list->p_values[i].psz_string),
463 val_list.p_list->p_values[i].psz_string,
464 p_object ), wxT("") );
468 another_val.psz_string =
469 strdup(val_list.p_list->p_values[i].psz_string);
471 new wxMenuItemExt( menu, ++i_item_id,
472 wxU(text_list.p_list->p_values[i].psz_string ?
473 text_list.p_list->p_values[i].psz_string :
474 another_val.psz_string),
475 wxT(""), wxITEM_RADIO, strdup(psz_var),
476 p_object->i_object_id, another_val, i_type );
478 menu->Append( menuitem );
480 if( !strcmp( val.psz_string,
481 val_list.p_list->p_values[i].psz_string ) )
482 menu->Check( i_item_id, TRUE );
485 case VLC_VAR_INTEGER:
487 new wxMenuItemExt( menu, ++i_item_id,
488 text_list.p_list->p_values[i].psz_string ?
489 wxU(text_list.p_list->p_values[i].psz_string):
490 wxString::Format(wxT("%d"),
491 val_list.p_list->p_values[i].i_int),
492 wxT(""), wxITEM_RADIO, strdup(psz_var),
493 p_object->i_object_id,
494 val_list.p_list->p_values[i], i_type );
496 menu->Append( menuitem );
498 if( val_list.p_list->p_values[i].i_int == val.i_int )
499 menu->Check( i_item_id, TRUE );
507 /* clean up everything */
508 if( i_type == VLC_VAR_STRING ) free( val.psz_string );
509 var_Change( p_object, psz_var, VLC_VAR_FREELIST, &val_list, &text_list );
514 /*****************************************************************************
515 * A small helper class which intercepts all popup menu events
516 *****************************************************************************/
517 MenuEvtHandler::MenuEvtHandler( intf_thread_t *_p_intf,
518 Interface *_p_main_interface )
520 /* Initializations */
522 p_main_interface = _p_main_interface;
525 MenuEvtHandler::~MenuEvtHandler()
529 void MenuEvtHandler::OnMenuEvent( wxCommandEvent& event )
531 wxMenuItem *p_menuitem;
533 /* Check if this is an auto generated menu item */
534 if( event.GetId() < FirstAutoGenerated_Event )
540 if( (p_menuitem = p_main_interface->GetMenuBar()->FindItem(event.GetId()))
543 if( p_main_interface->p_popup_menu )
546 p_main_interface->p_popup_menu->FindItem( event.GetId() );
552 wxMenuItemExt *p_menuitemext = (wxMenuItemExt *)p_menuitem;
553 vlc_object_t *p_object;
555 p_object = (vlc_object_t *)vlc_object_get( p_intf,
556 p_menuitemext->i_object_id );
557 if( p_object == NULL ) return;
559 var_Set( p_object, p_menuitemext->psz_var, p_menuitemext->val );
561 vlc_object_release( p_object );
567 /*****************************************************************************
568 * A small helper class which encapsulate wxMenuitem with some other useful
570 *****************************************************************************/
571 wxMenuItemExt::wxMenuItemExt( wxMenu* parentMenu, int id, const wxString& text,
572 const wxString& helpString, wxItemKind kind,
573 char *_psz_var, int _i_object_id, vlc_value_t _val, int _i_val_type ):
574 wxMenuItem( parentMenu, id, text, helpString, kind )
576 /* Initializations */
578 i_val_type = _i_val_type;
579 i_object_id = _i_object_id;
583 wxMenuItemExt::~wxMenuItemExt()
585 if( psz_var ) free( psz_var );
586 if( ((i_val_type & VLC_VAR_TYPE) == VLC_VAR_STRING)
587 && val.psz_string ) free( val.psz_string );