1 /*****************************************************************************
2 * menus.cpp : wxWindows plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2001 VideoLAN
5 * $Id: menus.cpp,v 1.1 2003/05/04 22:42:16 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[16];
100 memset( pi_objects, 0, 16 * 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;
146 vlc_object_release( p_object );
150 ppsz_varnames[i++] = NULL; /* Separator */
151 ppsz_varnames[i++] = _("Close");
154 wxMenu *popupmenu = new Menu( _p_intf, _p_main_interface, i,
155 ppsz_varnames, pi_objects );
157 _p_main_interface->p_popup_menu = popupmenu;
158 wxPoint mousepos = wxGetMousePosition();
159 _p_main_interface->PopupMenu( popupmenu,
160 _p_main_interface->ScreenToClient(mousepos).x,
161 _p_main_interface->ScreenToClient(mousepos).y
165 wxMenu *AudioMenu( intf_thread_t *_p_intf, Interface *_p_main_interface )
167 vlc_object_t *p_object;
168 char *ppsz_varnames[4];
172 /* Initializations */
173 memset( pi_objects, 0, 4 * sizeof(int) );
176 ppsz_varnames[i++] = NULL; /* Separator */
179 p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_AOUT,
181 if( p_object != NULL )
183 ppsz_varnames[i] = "audio-device";
184 pi_objects[i++] = p_object->i_object_id;
185 ppsz_varnames[i] = "audio-channels";
186 pi_objects[i++] = p_object->i_object_id;
187 vlc_object_release( p_object );
191 return new Menu( _p_intf, _p_main_interface, i,
192 ppsz_varnames, pi_objects );
195 wxMenu *VideoMenu( intf_thread_t *_p_intf, Interface *_p_main_interface )
197 vlc_object_t *p_object;
198 char *ppsz_varnames[4];
202 /* Initializations */
203 memset( pi_objects, 0, 4 * sizeof(int) );
205 ppsz_varnames[i++] = NULL; /* Separator */
207 p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_VOUT,
209 if( p_object != NULL )
211 ppsz_varnames[i] = "fullscreen";
212 pi_objects[i++] = p_object->i_object_id;
213 vlc_object_release( p_object );
217 return new Menu( _p_intf, _p_main_interface, i,
218 ppsz_varnames, pi_objects );
221 /*****************************************************************************
223 *****************************************************************************/
224 Menu::Menu( intf_thread_t *_p_intf, Interface *_p_main_interface,
225 int i_count, char **ppsz_varnames, int *pi_objects ):
228 vlc_object_t *p_object;
231 /* Initializations */
233 p_main_interface = _p_main_interface;
235 i_item_id = MenuLast_Event;
237 for( i = 0; i < i_count; i++ )
239 if( !ppsz_varnames[i] )
247 Append( MenuDummy_Event, ppsz_varnames[i] );
251 p_object = (vlc_object_t *)vlc_object_get( p_intf, pi_objects[i] );
252 if( p_object == NULL ) continue;
254 CreateMenuItem( this, ppsz_varnames[i], p_object );
255 vlc_object_release( p_object );
264 /*****************************************************************************
266 *****************************************************************************/
267 void Menu::OnEntrySelected( wxCommandEvent& WXUNUSED(event) )
271 void Menu::CreateMenuItem( wxMenu *menu, char *psz_var,
272 vlc_object_t *p_object )
274 wxMenuItemExt *menuitem;
275 vlc_value_t val, text;
278 /* Check the type of the object variable */
279 i_type = var_Type( p_object, psz_var );
281 switch( i_type & VLC_VAR_TYPE )
285 case VLC_VAR_VARIABLE:
287 case VLC_VAR_INTEGER:
290 /* Variable doesn't exist or isn't handled */
294 /* Make sure we want to display the variable */
295 if( i_type & VLC_VAR_HASCHOICE )
297 var_Change( p_object, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
298 if( val.i_int == 0 ) return;
301 /* Get the descriptive name of the variable */
302 var_Change( p_object, psz_var, VLC_VAR_GETTEXT, &text, NULL );
304 if( i_type & VLC_VAR_HASCHOICE )
306 menu->Append( MenuDummy_Event,
307 text.psz_string ? text.psz_string : psz_var,
308 CreateChoicesMenu( psz_var, p_object ),
309 "" /* Nothing for now (maybe use a GETLONGTEXT) */ );
311 if( text.psz_string ) free( text.psz_string );
316 switch( i_type & VLC_VAR_TYPE )
319 menuitem = new wxMenuItemExt( menu, ++i_item_id,
321 text.psz_string : psz_var,
322 "", wxITEM_NORMAL, strdup(psz_var),
323 p_object->i_object_id, val, i_type );
324 menu->Append( menuitem );
328 menuitem = new wxMenuItemExt( menu, ++i_item_id,
330 text.psz_string : psz_var,
331 "", wxITEM_CHECK, strdup(psz_var),
332 p_object->i_object_id, val, i_type );
333 menu->Append( menuitem );
334 Check( i_item_id -1, val.b_bool ? FALSE : TRUE );
338 if( text.psz_string ) free( text.psz_string );
342 if( text.psz_string ) free( text.psz_string );
345 wxMenu *Menu::CreateChoicesMenu( char *psz_var, vlc_object_t *p_object )
347 vlc_value_t val, val_list, text_list;
350 /* Check the type of the object variable */
351 i_type = var_Type( p_object, psz_var );
353 /* Make sure we want to display the variable */
354 if( i_type & VLC_VAR_HASCHOICE )
356 var_Change( p_object, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
357 if( val.i_int == 0 ) return NULL;
364 switch( i_type & VLC_VAR_TYPE )
368 case VLC_VAR_VARIABLE:
370 case VLC_VAR_INTEGER:
373 /* Variable doesn't exist or isn't handled */
377 if( var_Get( p_object, psz_var, &val ) < 0 )
382 if( var_Change( p_object, psz_var, VLC_VAR_GETLIST,
383 &val_list, &text_list ) < 0 )
385 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
389 wxMenu *menu = new wxMenu;
390 for( i = 0; i < val_list.p_list->i_count; i++ )
392 vlc_value_t another_val;
393 wxMenuItemExt *menuitem;
395 switch( i_type & VLC_VAR_TYPE )
397 case VLC_VAR_VARIABLE:
398 menu->Append( MenuDummy_Event,
399 text_list.p_list->p_values[i].psz_string ?
400 text_list.p_list->p_values[i].psz_string :
401 val_list.p_list->p_values[i].psz_string,
403 val_list.p_list->p_values[i].psz_string,
408 another_val.psz_string =
409 strdup(val_list.p_list->p_values[i].psz_string);
411 new wxMenuItemExt( this, ++i_item_id,
412 text_list.p_list->p_values[i].psz_string ?
413 text_list.p_list->p_values[i].psz_string :
414 another_val.psz_string,
415 "", wxITEM_RADIO, strdup(psz_var),
416 p_object->i_object_id, another_val, i_type );
418 menu->Append( menuitem );
420 if( !strcmp( val.psz_string,
421 val_list.p_list->p_values[i].psz_string ) )
422 menu->Check( i_item_id, TRUE );
425 case VLC_VAR_INTEGER:
427 new wxMenuItemExt( this, ++i_item_id,
428 text_list.p_list->p_values[i].psz_string ?
429 text_list.p_list->p_values[i].psz_string :
430 wxString::Format("%d",
431 val_list.p_list->p_values[i].i_int),
432 "", wxITEM_RADIO, strdup(psz_var),
433 p_object->i_object_id,
434 val_list.p_list->p_values[i], i_type );
436 menu->Append( menuitem );
438 if( !((i_type & VLC_VAR_FLAGS) & VLC_VAR_ISCOMMAND) &&
439 val_list.p_list->p_values[i].i_int == val.i_int )
440 menu->Check( i_item_id, TRUE );
449 /* clean up everything */
450 if( i_type == VLC_VAR_STRING ) free( val.psz_string );
451 var_Change( p_object, psz_var, VLC_VAR_FREELIST, &val_list, &text_list );
456 /*****************************************************************************
457 * A small helper class which intercepts all popup menu events
458 *****************************************************************************/
459 MenuEvtHandler::MenuEvtHandler( intf_thread_t *_p_intf,
460 Interface *_p_main_interface )
462 /* Initializations */
464 p_main_interface = _p_main_interface;
467 MenuEvtHandler::~MenuEvtHandler()
471 void MenuEvtHandler::OnMenuEvent( wxCommandEvent& event )
473 wxMenuItem *p_menuitem;
475 /* Check if this is an auto generated menu item */
476 if( event.GetId() < FirstAutoGenerated_Event )
482 if( (p_menuitem = p_main_interface->GetMenuBar()->FindItem(event.GetId()))
485 if( p_main_interface->p_popup_menu )
488 p_main_interface->p_popup_menu->FindItem( event.GetId() );
494 wxMenuItemExt *p_menuitemext = (wxMenuItemExt *)p_menuitem;
495 vlc_object_t *p_object;
497 p_object = (vlc_object_t *)vlc_object_get( p_intf,
498 p_menuitemext->i_object_id );
499 if( p_object == NULL ) return;
501 var_Set( p_object, p_menuitemext->psz_var, p_menuitemext->val );
503 vlc_object_release( p_object );
509 /*****************************************************************************
510 * A small helper class which encapsulate wxMenuitem with some other useful
512 *****************************************************************************/
513 wxMenuItemExt::wxMenuItemExt( wxMenu* parentMenu, int id, const wxString& text,
514 const wxString& helpString, wxItemKind kind,
515 char *_psz_var, int _i_object_id, vlc_value_t _val, int _i_val_type ):
516 wxMenuItem( parentMenu, id, text, helpString, kind )
518 /* Initializations */
520 i_val_type = _i_val_type;
521 i_object_id = _i_object_id;
525 wxMenuItemExt::~wxMenuItemExt()
527 if( psz_var ) free( psz_var );
528 if( ((i_val_type & VLC_VAR_TYPE) == VLC_VAR_STRING)
529 && val.psz_string ) free( val.psz_string );