1 /*****************************************************************************
2 * menus.cpp : wxWindows plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2001 VideoLAN
5 * $Id: menus.cpp,v 1.10 2003/05/15 21:31:53 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[10];
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 ppsz_varnames[i] = "program";
266 pi_objects[i++] = p_object->i_object_id;
268 ppsz_varnames[i] = "prev-title";
269 pi_objects[i++] = p_object->i_object_id;
270 ppsz_varnames[i] = "next-title";
271 pi_objects[i++] = p_object->i_object_id;
272 ppsz_varnames[i] = "prev-chapter";
273 pi_objects[i++] = p_object->i_object_id;
274 ppsz_varnames[i] = "next-chapter";
275 pi_objects[i++] = p_object->i_object_id;
277 vlc_object_release( p_object );
281 return new Menu( _p_intf, _p_main_interface, i,
282 ppsz_varnames, pi_objects, NavigMenu_Events );
285 /*****************************************************************************
287 *****************************************************************************/
288 Menu::Menu( intf_thread_t *_p_intf, Interface *_p_main_interface,
289 int i_count, char **ppsz_varnames, int *pi_objects,
290 int i_start_id ): wxMenu( )
292 vlc_object_t *p_object;
295 /* Initializations */
297 p_main_interface = _p_main_interface;
299 i_item_id = i_start_id;
301 for( i = 0; i < i_count; i++ )
303 if( !ppsz_varnames[i] )
311 Append( MenuDummy_Event, wxU(ppsz_varnames[i]) );
315 p_object = (vlc_object_t *)vlc_object_get( p_intf, pi_objects[i] );
316 if( p_object == NULL ) continue;
318 CreateMenuItem( this, ppsz_varnames[i], p_object );
319 vlc_object_release( p_object );
322 /* Special case for empty menus */
323 if( GetMenuItemCount() == 0 )
325 Append( MenuDummy_Event, wxU(_("Empty")) );
326 Enable( MenuDummy_Event, FALSE );
334 /*****************************************************************************
336 *****************************************************************************/
337 void Menu::OnEntrySelected( wxCommandEvent& WXUNUSED(event) )
341 void Menu::CreateMenuItem( wxMenu *menu, char *psz_var,
342 vlc_object_t *p_object )
344 wxMenuItemExt *menuitem;
345 vlc_value_t val, text;
348 /* Check the type of the object variable */
349 i_type = var_Type( p_object, psz_var );
351 switch( i_type & VLC_VAR_TYPE )
355 case VLC_VAR_VARIABLE:
357 case VLC_VAR_INTEGER:
360 /* Variable doesn't exist or isn't handled */
364 /* Make sure we want to display the variable */
365 if( i_type & VLC_VAR_HASCHOICE )
367 var_Change( p_object, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
368 if( val.i_int == 0 ) return;
371 /* Get the descriptive name of the variable */
372 var_Change( p_object, psz_var, VLC_VAR_GETTEXT, &text, NULL );
374 if( i_type & VLC_VAR_HASCHOICE )
376 menu->Append( MenuDummy_Event,
377 wxU(text.psz_string ? text.psz_string : psz_var),
378 CreateChoicesMenu( psz_var, p_object ),
379 wxT("")/* Nothing for now (maybe use a GETLONGTEXT) */ );
381 if( text.psz_string ) free( text.psz_string );
386 switch( i_type & VLC_VAR_TYPE )
389 menuitem = new wxMenuItemExt( menu, ++i_item_id,
390 wxU(text.psz_string ?
391 text.psz_string : psz_var),
392 wxT(""), wxITEM_NORMAL, strdup(psz_var),
393 p_object->i_object_id, val, i_type );
394 menu->Append( menuitem );
398 menuitem = new wxMenuItemExt( menu, ++i_item_id,
399 wxU(text.psz_string ?
400 text.psz_string : psz_var),
401 wxT(""), wxITEM_CHECK, strdup(psz_var),
402 p_object->i_object_id, val, i_type );
403 menu->Append( menuitem );
404 Check( i_item_id -1, val.b_bool ? FALSE : TRUE );
408 if( text.psz_string ) free( text.psz_string );
412 if( text.psz_string ) free( text.psz_string );
415 wxMenu *Menu::CreateChoicesMenu( char *psz_var, vlc_object_t *p_object )
417 vlc_value_t val, val_list, text_list;
420 /* Check the type of the object variable */
421 i_type = var_Type( p_object, psz_var );
423 /* Make sure we want to display the variable */
424 if( i_type & VLC_VAR_HASCHOICE )
426 var_Change( p_object, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
427 if( val.i_int == 0 ) return NULL;
434 switch( i_type & VLC_VAR_TYPE )
438 case VLC_VAR_VARIABLE:
440 case VLC_VAR_INTEGER:
443 /* Variable doesn't exist or isn't handled */
447 if( var_Get( p_object, psz_var, &val ) < 0 )
452 if( var_Change( p_object, psz_var, VLC_VAR_GETLIST,
453 &val_list, &text_list ) < 0 )
455 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
459 wxMenu *menu = new wxMenu;
460 for( i = 0; i < val_list.p_list->i_count; i++ )
462 vlc_value_t another_val;
463 wxMenuItemExt *menuitem;
465 switch( i_type & VLC_VAR_TYPE )
467 case VLC_VAR_VARIABLE:
468 menu->Append( MenuDummy_Event,
469 wxU(text_list.p_list->p_values[i].psz_string ?
470 text_list.p_list->p_values[i].psz_string :
471 val_list.p_list->p_values[i].psz_string),
473 val_list.p_list->p_values[i].psz_string,
474 p_object ), wxT("") );
478 another_val.psz_string =
479 strdup(val_list.p_list->p_values[i].psz_string);
481 new wxMenuItemExt( menu, ++i_item_id,
482 wxU(text_list.p_list->p_values[i].psz_string ?
483 text_list.p_list->p_values[i].psz_string :
484 another_val.psz_string),
485 wxT(""), wxITEM_RADIO, strdup(psz_var),
486 p_object->i_object_id, another_val, i_type );
488 menu->Append( menuitem );
490 if( !strcmp( val.psz_string,
491 val_list.p_list->p_values[i].psz_string ) )
492 menu->Check( i_item_id, TRUE );
495 case VLC_VAR_INTEGER:
497 new wxMenuItemExt( menu, ++i_item_id,
498 text_list.p_list->p_values[i].psz_string ?
499 wxU(text_list.p_list->p_values[i].psz_string):
500 wxString::Format(wxT("%d"),
501 val_list.p_list->p_values[i].i_int),
502 wxT(""), wxITEM_RADIO, strdup(psz_var),
503 p_object->i_object_id,
504 val_list.p_list->p_values[i], i_type );
506 menu->Append( menuitem );
508 if( val_list.p_list->p_values[i].i_int == val.i_int )
509 menu->Check( i_item_id, TRUE );
517 /* clean up everything */
518 if( i_type == VLC_VAR_STRING ) free( val.psz_string );
519 var_Change( p_object, psz_var, VLC_VAR_FREELIST, &val_list, &text_list );
524 /*****************************************************************************
525 * A small helper class which intercepts all popup menu events
526 *****************************************************************************/
527 MenuEvtHandler::MenuEvtHandler( intf_thread_t *_p_intf,
528 Interface *_p_main_interface )
530 /* Initializations */
532 p_main_interface = _p_main_interface;
535 MenuEvtHandler::~MenuEvtHandler()
539 void MenuEvtHandler::OnMenuEvent( wxCommandEvent& event )
541 wxMenuItem *p_menuitem;
543 /* Check if this is an auto generated menu item */
544 if( event.GetId() < FirstAutoGenerated_Event )
550 if( (p_menuitem = p_main_interface->GetMenuBar()->FindItem(event.GetId()))
553 if( p_main_interface->p_popup_menu )
556 p_main_interface->p_popup_menu->FindItem( event.GetId() );
562 wxMenuItemExt *p_menuitemext = (wxMenuItemExt *)p_menuitem;
563 vlc_object_t *p_object;
565 p_object = (vlc_object_t *)vlc_object_get( p_intf,
566 p_menuitemext->i_object_id );
567 if( p_object == NULL ) return;
569 var_Set( p_object, p_menuitemext->psz_var, p_menuitemext->val );
571 vlc_object_release( p_object );
577 /*****************************************************************************
578 * A small helper class which encapsulate wxMenuitem with some other useful
580 *****************************************************************************/
581 wxMenuItemExt::wxMenuItemExt( wxMenu* parentMenu, int id, const wxString& text,
582 const wxString& helpString, wxItemKind kind,
583 char *_psz_var, int _i_object_id, vlc_value_t _val, int _i_val_type ):
584 wxMenuItem( parentMenu, id, text, helpString, kind )
586 /* Initializations */
588 i_val_type = _i_val_type;
589 i_object_id = _i_object_id;
593 wxMenuItemExt::~wxMenuItemExt()
595 if( psz_var ) free( psz_var );
596 if( ((i_val_type & VLC_VAR_TYPE) == VLC_VAR_STRING)
597 && val.psz_string ) free( val.psz_string );