1 /*****************************************************************************
2 * menus.cpp : wxWindows plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2001 VideoLAN
5 * $Id: menus.cpp,v 1.2 2003/05/05 22:23:40 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,
83 BEGIN_EVENT_TABLE(Menu, wxMenu)
85 EVT_MENU(MenuDummy_Event, Menu::OnEntrySelected)
88 BEGIN_EVENT_TABLE(MenuEvtHandler, wxEvtHandler)
89 EVT_MENU(-1, MenuEvtHandler::OnMenuEvent)
92 void PopupMenu( intf_thread_t *_p_intf, Interface *_p_main_interface )
94 vlc_object_t *p_object;
95 char *ppsz_varnames[19];
100 memset( pi_objects, 0, 19 * sizeof(int) );
103 ppsz_varnames[i++] = _("Audio menu");
104 ppsz_varnames[i++] = NULL; /* Separator */
106 p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_AOUT,
108 if( p_object != NULL )
110 ppsz_varnames[i] = "audio-device";
111 pi_objects[i++] = p_object->i_object_id;
112 ppsz_varnames[i] = "audio-channels";
113 pi_objects[i++] = p_object->i_object_id;
114 vlc_object_release( p_object );
118 ppsz_varnames[i++] = NULL; /* Separator */
119 ppsz_varnames[i++] = _("Video menu");
120 ppsz_varnames[i++] = NULL; /* Separator */
122 p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_VOUT,
124 if( p_object != NULL )
126 ppsz_varnames[i] = "fullscreen";
127 pi_objects[i++] = p_object->i_object_id;
128 vlc_object_release( p_object );
132 ppsz_varnames[i++] = NULL; /* Separator */
133 ppsz_varnames[i++] = _("Input menu");
134 ppsz_varnames[i++] = NULL; /* Separator */
136 p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_INPUT,
138 if( p_object != NULL )
140 ppsz_varnames[i] = "title";
141 pi_objects[i++] = p_object->i_object_id;
142 ppsz_varnames[i] = "chapter";
143 pi_objects[i++] = p_object->i_object_id;
144 ppsz_varnames[i] = "navigation";
145 pi_objects[i++] = p_object->i_object_id;
147 ppsz_varnames[i] = "video-es";
148 pi_objects[i++] = p_object->i_object_id;
149 ppsz_varnames[i] = "audio-es";
150 pi_objects[i++] = p_object->i_object_id;
151 ppsz_varnames[i] = "spu-es";
152 pi_objects[i++] = p_object->i_object_id;
154 vlc_object_release( p_object );
158 ppsz_varnames[i++] = NULL; /* Separator */
159 ppsz_varnames[i++] = _("Close");
162 wxMenu *popupmenu = new Menu( _p_intf, _p_main_interface, i,
163 ppsz_varnames, pi_objects );
165 _p_main_interface->p_popup_menu = popupmenu;
166 wxPoint mousepos = wxGetMousePosition();
167 _p_main_interface->PopupMenu( popupmenu,
168 _p_main_interface->ScreenToClient(mousepos).x,
169 _p_main_interface->ScreenToClient(mousepos).y
173 wxMenu *AudioMenu( intf_thread_t *_p_intf, Interface *_p_main_interface )
175 vlc_object_t *p_object;
176 char *ppsz_varnames[5];
180 /* Initializations */
181 memset( pi_objects, 0, 5 * sizeof(int) );
184 ppsz_varnames[i++] = NULL; /* Separator */
187 p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_AOUT,
189 if( p_object != NULL )
191 ppsz_varnames[i] = "audio-device";
192 pi_objects[i++] = p_object->i_object_id;
193 ppsz_varnames[i] = "audio-channels";
194 pi_objects[i++] = p_object->i_object_id;
195 vlc_object_release( p_object );
198 p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_INPUT,
200 if( p_object != NULL )
202 ppsz_varnames[i] = "audio-es";
203 pi_objects[i++] = p_object->i_object_id;
204 vlc_object_release( p_object );
208 return new Menu( _p_intf, _p_main_interface, i,
209 ppsz_varnames, pi_objects );
212 wxMenu *VideoMenu( intf_thread_t *_p_intf, Interface *_p_main_interface )
214 vlc_object_t *p_object;
215 char *ppsz_varnames[4];
219 /* Initializations */
220 memset( pi_objects, 0, 4 * sizeof(int) );
222 ppsz_varnames[i++] = NULL; /* Separator */
224 p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_VOUT,
226 if( p_object != NULL )
228 ppsz_varnames[i] = "fullscreen";
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] = "video-es";
238 pi_objects[i++] = p_object->i_object_id;
239 ppsz_varnames[i] = "spu-es";
240 pi_objects[i++] = p_object->i_object_id;
241 vlc_object_release( p_object );
245 return new Menu( _p_intf, _p_main_interface, i,
246 ppsz_varnames, pi_objects );
249 /*****************************************************************************
251 *****************************************************************************/
252 Menu::Menu( intf_thread_t *_p_intf, Interface *_p_main_interface,
253 int i_count, char **ppsz_varnames, int *pi_objects ):
256 vlc_object_t *p_object;
259 /* Initializations */
261 p_main_interface = _p_main_interface;
263 i_item_id = MenuLast_Event;
265 for( i = 0; i < i_count; i++ )
267 if( !ppsz_varnames[i] )
275 Append( MenuDummy_Event, ppsz_varnames[i] );
279 p_object = (vlc_object_t *)vlc_object_get( p_intf, pi_objects[i] );
280 if( p_object == NULL ) continue;
282 CreateMenuItem( this, ppsz_varnames[i], p_object );
283 vlc_object_release( p_object );
292 /*****************************************************************************
294 *****************************************************************************/
295 void Menu::OnEntrySelected( wxCommandEvent& WXUNUSED(event) )
299 void Menu::CreateMenuItem( wxMenu *menu, char *psz_var,
300 vlc_object_t *p_object )
302 wxMenuItemExt *menuitem;
303 vlc_value_t val, text;
306 /* Check the type of the object variable */
307 i_type = var_Type( p_object, psz_var );
309 switch( i_type & VLC_VAR_TYPE )
313 case VLC_VAR_VARIABLE:
315 case VLC_VAR_INTEGER:
318 /* Variable doesn't exist or isn't handled */
322 /* Make sure we want to display the variable */
323 if( i_type & VLC_VAR_HASCHOICE )
325 var_Change( p_object, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
326 if( val.i_int == 0 ) return;
329 /* Get the descriptive name of the variable */
330 var_Change( p_object, psz_var, VLC_VAR_GETTEXT, &text, NULL );
332 if( i_type & VLC_VAR_HASCHOICE )
334 menu->Append( MenuDummy_Event,
335 text.psz_string ? text.psz_string : psz_var,
336 CreateChoicesMenu( psz_var, p_object ),
337 "" /* Nothing for now (maybe use a GETLONGTEXT) */ );
339 if( text.psz_string ) free( text.psz_string );
344 switch( i_type & VLC_VAR_TYPE )
347 menuitem = new wxMenuItemExt( menu, ++i_item_id,
349 text.psz_string : psz_var,
350 "", wxITEM_NORMAL, strdup(psz_var),
351 p_object->i_object_id, val, i_type );
352 menu->Append( menuitem );
356 menuitem = new wxMenuItemExt( menu, ++i_item_id,
358 text.psz_string : psz_var,
359 "", wxITEM_CHECK, strdup(psz_var),
360 p_object->i_object_id, val, i_type );
361 menu->Append( menuitem );
362 Check( i_item_id -1, val.b_bool ? FALSE : TRUE );
366 if( text.psz_string ) free( text.psz_string );
370 if( text.psz_string ) free( text.psz_string );
373 wxMenu *Menu::CreateChoicesMenu( char *psz_var, vlc_object_t *p_object )
375 vlc_value_t val, val_list, text_list;
378 /* Check the type of the object variable */
379 i_type = var_Type( p_object, psz_var );
381 /* Make sure we want to display the variable */
382 if( i_type & VLC_VAR_HASCHOICE )
384 var_Change( p_object, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
385 if( val.i_int == 0 ) return NULL;
392 switch( i_type & VLC_VAR_TYPE )
396 case VLC_VAR_VARIABLE:
398 case VLC_VAR_INTEGER:
401 /* Variable doesn't exist or isn't handled */
405 if( var_Get( p_object, psz_var, &val ) < 0 )
410 if( var_Change( p_object, psz_var, VLC_VAR_GETLIST,
411 &val_list, &text_list ) < 0 )
413 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
417 wxMenu *menu = new wxMenu;
418 for( i = 0; i < val_list.p_list->i_count; i++ )
420 vlc_value_t another_val;
421 wxMenuItemExt *menuitem;
423 switch( i_type & VLC_VAR_TYPE )
425 case VLC_VAR_VARIABLE:
426 menu->Append( MenuDummy_Event,
427 text_list.p_list->p_values[i].psz_string ?
428 text_list.p_list->p_values[i].psz_string :
429 val_list.p_list->p_values[i].psz_string,
431 val_list.p_list->p_values[i].psz_string,
436 another_val.psz_string =
437 strdup(val_list.p_list->p_values[i].psz_string);
439 new wxMenuItemExt( this, ++i_item_id,
440 text_list.p_list->p_values[i].psz_string ?
441 text_list.p_list->p_values[i].psz_string :
442 another_val.psz_string,
443 "", wxITEM_RADIO, strdup(psz_var),
444 p_object->i_object_id, another_val, i_type );
446 menu->Append( menuitem );
448 if( !strcmp( val.psz_string,
449 val_list.p_list->p_values[i].psz_string ) )
450 menu->Check( i_item_id, TRUE );
453 case VLC_VAR_INTEGER:
455 new wxMenuItemExt( this, ++i_item_id,
456 text_list.p_list->p_values[i].psz_string ?
457 text_list.p_list->p_values[i].psz_string :
458 wxString::Format("%d",
459 val_list.p_list->p_values[i].i_int),
460 "", wxITEM_RADIO, strdup(psz_var),
461 p_object->i_object_id,
462 val_list.p_list->p_values[i], i_type );
464 menu->Append( menuitem );
466 if( !((i_type & VLC_VAR_FLAGS) & VLC_VAR_ISCOMMAND) &&
467 val_list.p_list->p_values[i].i_int == val.i_int )
468 menu->Check( i_item_id, TRUE );
477 /* clean up everything */
478 if( i_type == VLC_VAR_STRING ) free( val.psz_string );
479 var_Change( p_object, psz_var, VLC_VAR_FREELIST, &val_list, &text_list );
484 /*****************************************************************************
485 * A small helper class which intercepts all popup menu events
486 *****************************************************************************/
487 MenuEvtHandler::MenuEvtHandler( intf_thread_t *_p_intf,
488 Interface *_p_main_interface )
490 /* Initializations */
492 p_main_interface = _p_main_interface;
495 MenuEvtHandler::~MenuEvtHandler()
499 void MenuEvtHandler::OnMenuEvent( wxCommandEvent& event )
501 wxMenuItem *p_menuitem;
503 /* Check if this is an auto generated menu item */
504 if( event.GetId() < FirstAutoGenerated_Event )
510 if( (p_menuitem = p_main_interface->GetMenuBar()->FindItem(event.GetId()))
513 if( p_main_interface->p_popup_menu )
516 p_main_interface->p_popup_menu->FindItem( event.GetId() );
522 wxMenuItemExt *p_menuitemext = (wxMenuItemExt *)p_menuitem;
523 vlc_object_t *p_object;
525 p_object = (vlc_object_t *)vlc_object_get( p_intf,
526 p_menuitemext->i_object_id );
527 if( p_object == NULL ) return;
529 var_Set( p_object, p_menuitemext->psz_var, p_menuitemext->val );
531 vlc_object_release( p_object );
537 /*****************************************************************************
538 * A small helper class which encapsulate wxMenuitem with some other useful
540 *****************************************************************************/
541 wxMenuItemExt::wxMenuItemExt( wxMenu* parentMenu, int id, const wxString& text,
542 const wxString& helpString, wxItemKind kind,
543 char *_psz_var, int _i_object_id, vlc_value_t _val, int _i_val_type ):
544 wxMenuItem( parentMenu, id, text, helpString, kind )
546 /* Initializations */
548 i_val_type = _i_val_type;
549 i_object_id = _i_object_id;
553 wxMenuItemExt::~wxMenuItemExt()
555 if( psz_var ) free( psz_var );
556 if( ((i_val_type & VLC_VAR_TYPE) == VLC_VAR_STRING)
557 && val.psz_string ) free( val.psz_string );