1 /*****************************************************************************
2 * menus.cpp : wxWindows plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2001 VideoLAN
5 * $Id: menus.cpp,v 1.15 2003/06/05 21:22:28 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 #if defined MODULE_NAME_IS_skins
49 # include "../skins/src/skin_common.h"
52 #include "wxwindows.h"
54 class wxMenuItemExt: public wxMenuItem
58 wxMenuItemExt( wxMenu* parentMenu, int id, const wxString& text,
59 const wxString& helpString, wxItemKind kind,
60 char *_psz_var, int _i_object_id, vlc_value_t _val,
63 virtual ~wxMenuItemExt();
74 /*****************************************************************************
76 *****************************************************************************/
78 /* IDs for the controls and the menu commands */
82 FirstAutoGenerated_Event = wxID_HIGHEST + 1000,
85 VideoMenu_Events = wxID_HIGHEST + 1100,
86 NavigMenu_Events = wxID_HIGHEST + 1200,
87 PopupMenu_Events = wxID_HIGHEST + 1300,
90 BEGIN_EVENT_TABLE(Menu, wxMenu)
92 EVT_MENU(MenuDummy_Event, Menu::OnEntrySelected)
95 BEGIN_EVENT_TABLE(MenuEvtHandler, wxEvtHandler)
96 EVT_MENU(-1, MenuEvtHandler::OnMenuEvent)
99 void PopupMenu( intf_thread_t *p_intf, wxWindow *p_parent,
102 vlc_object_t *p_object;
103 char *ppsz_varnames[29];
107 /* Initializations */
108 memset( pi_objects, 0, 29 * sizeof(int) );
111 ppsz_varnames[i++] = _("Audio menu");
112 ppsz_varnames[i++] = NULL; /* Separator */
114 p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_AOUT,
116 if( p_object != NULL )
118 ppsz_varnames[i] = "audio-device";
119 pi_objects[i++] = p_object->i_object_id;
120 ppsz_varnames[i] = "audio-channels";
121 pi_objects[i++] = p_object->i_object_id;
122 vlc_object_release( p_object );
126 ppsz_varnames[i++] = NULL; /* Separator */
127 ppsz_varnames[i++] = _("Video menu");
128 ppsz_varnames[i++] = NULL; /* Separator */
130 p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_VOUT,
132 if( p_object != NULL )
134 ppsz_varnames[i] = "fullscreen";
135 pi_objects[i++] = p_object->i_object_id;
136 ppsz_varnames[i] = "deinterlace";
137 pi_objects[i++] = p_object->i_object_id;
138 ppsz_varnames[i] = "directx-on-top";
139 pi_objects[i++] = p_object->i_object_id;
140 vlc_object_release( p_object );
144 ppsz_varnames[i++] = NULL; /* Separator */
145 ppsz_varnames[i++] = _("Input menu");
146 ppsz_varnames[i++] = NULL; /* Separator */
148 p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
150 if( p_object != NULL )
152 ppsz_varnames[i] = "title";
153 pi_objects[i++] = p_object->i_object_id;
154 ppsz_varnames[i] = "chapter";
155 pi_objects[i++] = p_object->i_object_id;
156 ppsz_varnames[i] = "navigation";
157 pi_objects[i++] = p_object->i_object_id;
158 ppsz_varnames[i] = "program";
159 pi_objects[i++] = p_object->i_object_id;
161 ppsz_varnames[i] = "video-es";
162 pi_objects[i++] = p_object->i_object_id;
163 ppsz_varnames[i] = "audio-es";
164 pi_objects[i++] = p_object->i_object_id;
165 ppsz_varnames[i] = "spu-es";
166 pi_objects[i++] = p_object->i_object_id;
168 vlc_object_release( p_object );
172 ppsz_varnames[i++] = NULL; /* Separator */
173 ppsz_varnames[i++] = _("Close Menu");
176 Menu popupmenu( p_intf, p_parent, i,
177 ppsz_varnames, pi_objects, PopupMenu_Events );
179 p_intf->p_sys->p_popup_menu = &popupmenu;
180 p_parent->PopupMenu( &popupmenu, pos.x, pos.y );
181 p_intf->p_sys->p_popup_menu = NULL;
184 wxMenu *AudioMenu( intf_thread_t *_p_intf, wxWindow *p_parent )
186 vlc_object_t *p_object;
187 char *ppsz_varnames[5];
191 /* Initializations */
192 memset( pi_objects, 0, 5 * sizeof(int) );
194 p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_AOUT,
196 if( p_object != NULL )
198 ppsz_varnames[i] = "audio-device";
199 pi_objects[i++] = p_object->i_object_id;
200 ppsz_varnames[i] = "audio-channels";
201 pi_objects[i++] = p_object->i_object_id;
202 vlc_object_release( p_object );
205 p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_INPUT,
207 if( p_object != NULL )
209 ppsz_varnames[i] = "audio-es";
210 pi_objects[i++] = p_object->i_object_id;
211 vlc_object_release( p_object );
215 return new Menu( _p_intf, p_parent, i,
216 ppsz_varnames, pi_objects, AudioMenu_Events );
219 wxMenu *VideoMenu( intf_thread_t *_p_intf, wxWindow *p_parent )
221 vlc_object_t *p_object;
222 char *ppsz_varnames[6];
226 /* Initializations */
227 memset( pi_objects, 0, 6 * sizeof(int) );
229 p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_VOUT,
231 if( p_object != NULL )
233 ppsz_varnames[i] = "fullscreen";
234 pi_objects[i++] = p_object->i_object_id;
235 ppsz_varnames[i] = "deinterlace";
236 pi_objects[i++] = p_object->i_object_id;
237 ppsz_varnames[i] = "directx-on-top";
238 pi_objects[i++] = p_object->i_object_id;
239 vlc_object_release( p_object );
242 p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_INPUT,
244 if( p_object != NULL )
246 ppsz_varnames[i] = "video-es";
247 pi_objects[i++] = p_object->i_object_id;
248 ppsz_varnames[i] = "spu-es";
249 pi_objects[i++] = p_object->i_object_id;
250 vlc_object_release( p_object );
254 return new Menu( _p_intf, p_parent, i,
255 ppsz_varnames, pi_objects, VideoMenu_Events );
258 wxMenu *NavigMenu( intf_thread_t *_p_intf, wxWindow *p_parent )
260 vlc_object_t *p_object;
261 char *ppsz_varnames[10];
265 /* Initializations */
266 memset( pi_objects, 0, 4 * sizeof(int) );
268 p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_INPUT,
270 if( p_object != NULL )
272 ppsz_varnames[i] = "title";
273 pi_objects[i++] = p_object->i_object_id;
274 ppsz_varnames[i] = "chapter";
275 pi_objects[i++] = p_object->i_object_id;
276 ppsz_varnames[i] = "navigation";
277 pi_objects[i++] = p_object->i_object_id;
278 ppsz_varnames[i] = "program";
279 pi_objects[i++] = p_object->i_object_id;
281 ppsz_varnames[i] = "prev-title";
282 pi_objects[i++] = p_object->i_object_id;
283 ppsz_varnames[i] = "next-title";
284 pi_objects[i++] = p_object->i_object_id;
285 ppsz_varnames[i] = "prev-chapter";
286 pi_objects[i++] = p_object->i_object_id;
287 ppsz_varnames[i] = "next-chapter";
288 pi_objects[i++] = p_object->i_object_id;
290 vlc_object_release( p_object );
294 return new Menu( _p_intf, p_parent, i,
295 ppsz_varnames, pi_objects, NavigMenu_Events );
298 /*****************************************************************************
300 *****************************************************************************/
301 Menu::Menu( intf_thread_t *_p_intf, wxWindow *p_parent,
302 int i_count, char **ppsz_varnames, int *pi_objects,
303 int i_start_id ): wxMenu( )
305 vlc_object_t *p_object;
308 /* Initializations */
311 i_item_id = i_start_id;
313 for( i = 0; i < i_count; i++ )
315 if( !ppsz_varnames[i] )
323 Append( MenuDummy_Event, wxU(ppsz_varnames[i]) );
327 p_object = (vlc_object_t *)vlc_object_get( p_intf, pi_objects[i] );
328 if( p_object == NULL ) continue;
330 CreateMenuItem( this, ppsz_varnames[i], p_object );
331 vlc_object_release( p_object );
334 /* Special case for empty menus */
335 if( GetMenuItemCount() == 0 )
337 Append( MenuDummy_Event, wxU(_("Empty")) );
338 Enable( MenuDummy_Event, FALSE );
346 /*****************************************************************************
348 *****************************************************************************/
349 void Menu::OnEntrySelected( wxCommandEvent& WXUNUSED(event) )
353 void Menu::CreateMenuItem( wxMenu *menu, char *psz_var,
354 vlc_object_t *p_object )
356 wxMenuItemExt *menuitem;
357 vlc_value_t val, text;
360 /* Check the type of the object variable */
361 i_type = var_Type( p_object, psz_var );
363 switch( i_type & VLC_VAR_TYPE )
367 case VLC_VAR_VARIABLE:
369 case VLC_VAR_INTEGER:
372 /* Variable doesn't exist or isn't handled */
376 /* Make sure we want to display the variable */
377 if( i_type & VLC_VAR_HASCHOICE )
379 var_Change( p_object, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
380 if( val.i_int == 0 ) return;
381 if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE && val.i_int == 1 )
385 /* Get the descriptive name of the variable */
386 var_Change( p_object, psz_var, VLC_VAR_GETTEXT, &text, NULL );
388 var_Get( p_object, psz_var, &val );
390 if( i_type & VLC_VAR_HASCHOICE )
392 menu->Append( MenuDummy_Event,
393 wxU(text.psz_string ? text.psz_string : psz_var),
394 CreateChoicesMenu( psz_var, p_object ),
395 wxT("")/* Nothing for now (maybe use a GETLONGTEXT) */ );
397 if( text.psz_string ) free( text.psz_string );
402 switch( i_type & VLC_VAR_TYPE )
405 menuitem = new wxMenuItemExt( menu, ++i_item_id,
406 wxU(text.psz_string ?
407 text.psz_string : psz_var),
408 wxT(""), wxITEM_NORMAL, strdup(psz_var),
409 p_object->i_object_id, val, i_type );
410 menu->Append( menuitem );
414 val.b_bool = !val.b_bool;
415 menuitem = new wxMenuItemExt( menu, ++i_item_id,
416 wxU(text.psz_string ?
417 text.psz_string : psz_var),
418 wxT(""), wxITEM_CHECK, strdup(psz_var),
419 p_object->i_object_id, val, i_type );
420 menu->Append( menuitem );
421 Check( i_item_id, val.b_bool ? FALSE : TRUE );
425 if( text.psz_string ) free( text.psz_string );
429 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
430 if( text.psz_string ) free( text.psz_string );
433 wxMenu *Menu::CreateChoicesMenu( char *psz_var, vlc_object_t *p_object )
435 vlc_value_t val, val_list, text_list;
438 /* Check the type of the object variable */
439 i_type = var_Type( p_object, psz_var );
441 /* Make sure we want to display the variable */
442 if( i_type & VLC_VAR_HASCHOICE )
444 var_Change( p_object, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
445 if( val.i_int == 0 ) return NULL;
446 if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE && val.i_int == 1 )
454 switch( i_type & VLC_VAR_TYPE )
458 case VLC_VAR_VARIABLE:
460 case VLC_VAR_INTEGER:
463 /* Variable doesn't exist or isn't handled */
467 if( var_Get( p_object, psz_var, &val ) < 0 )
472 if( var_Change( p_object, psz_var, VLC_VAR_GETLIST,
473 &val_list, &text_list ) < 0 )
475 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
479 wxMenu *menu = new wxMenu;
480 for( i = 0; i < val_list.p_list->i_count; i++ )
482 vlc_value_t another_val;
483 wxMenuItemExt *menuitem;
485 switch( i_type & VLC_VAR_TYPE )
487 case VLC_VAR_VARIABLE:
488 menu->Append( MenuDummy_Event,
489 wxU(text_list.p_list->p_values[i].psz_string ?
490 text_list.p_list->p_values[i].psz_string :
491 val_list.p_list->p_values[i].psz_string),
493 val_list.p_list->p_values[i].psz_string,
494 p_object ), wxT("") );
498 another_val.psz_string =
499 strdup(val_list.p_list->p_values[i].psz_string);
501 new wxMenuItemExt( menu, ++i_item_id,
502 wxU(text_list.p_list->p_values[i].psz_string ?
503 text_list.p_list->p_values[i].psz_string :
504 another_val.psz_string),
505 wxT(""), wxITEM_RADIO, strdup(psz_var),
506 p_object->i_object_id, another_val, i_type );
508 menu->Append( menuitem );
510 if( !strcmp( val.psz_string,
511 val_list.p_list->p_values[i].psz_string ) )
512 menu->Check( i_item_id, TRUE );
515 case VLC_VAR_INTEGER:
517 new wxMenuItemExt( menu, ++i_item_id,
518 text_list.p_list->p_values[i].psz_string ?
519 wxU(text_list.p_list->p_values[i].psz_string):
520 wxString::Format(wxT("%d"),
521 val_list.p_list->p_values[i].i_int),
522 wxT(""), wxITEM_RADIO, strdup(psz_var),
523 p_object->i_object_id,
524 val_list.p_list->p_values[i], i_type );
526 menu->Append( menuitem );
528 if( val_list.p_list->p_values[i].i_int == val.i_int )
529 menu->Check( i_item_id, TRUE );
537 /* clean up everything */
538 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
539 var_Change( p_object, psz_var, VLC_VAR_FREELIST, &val_list, &text_list );
544 /*****************************************************************************
545 * A small helper class which intercepts all popup menu events
546 *****************************************************************************/
547 MenuEvtHandler::MenuEvtHandler( intf_thread_t *_p_intf,
548 Interface *_p_main_interface )
550 /* Initializations */
552 p_main_interface = _p_main_interface;
555 MenuEvtHandler::~MenuEvtHandler()
559 void MenuEvtHandler::OnMenuEvent( wxCommandEvent& event )
561 wxMenuItem *p_menuitem = NULL;
563 /* Check if this is an auto generated menu item */
564 if( event.GetId() < FirstAutoGenerated_Event )
570 if( !p_main_interface ||
571 (p_menuitem = p_main_interface->GetMenuBar()->FindItem(event.GetId()))
574 if( p_intf->p_sys->p_popup_menu )
577 p_intf->p_sys->p_popup_menu->FindItem( event.GetId() );
583 wxMenuItemExt *p_menuitemext = (wxMenuItemExt *)p_menuitem;
584 vlc_object_t *p_object;
586 p_object = (vlc_object_t *)vlc_object_get( p_intf,
587 p_menuitemext->i_object_id );
588 if( p_object == NULL ) return;
590 var_Set( p_object, p_menuitemext->psz_var, p_menuitemext->val );
592 vlc_object_release( p_object );
598 /*****************************************************************************
599 * A small helper class which encapsulate wxMenuitem with some other useful
601 *****************************************************************************/
602 wxMenuItemExt::wxMenuItemExt( wxMenu* parentMenu, int id, const wxString& text,
603 const wxString& helpString, wxItemKind kind,
604 char *_psz_var, int _i_object_id, vlc_value_t _val, int _i_val_type ):
605 wxMenuItem( parentMenu, id, text, helpString, kind )
607 /* Initializations */
609 i_val_type = _i_val_type;
610 i_object_id = _i_object_id;
614 wxMenuItemExt::~wxMenuItemExt()
616 if( psz_var ) free( psz_var );
617 if( ((i_val_type & VLC_VAR_TYPE) == VLC_VAR_STRING)
618 && val.psz_string ) free( val.psz_string );