1 /*****************************************************************************
2 * menus.cpp : wxWindows plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2001 VideoLAN
5 * $Id: menus.cpp,v 1.16 2003/07/17 17:30: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 #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 MenuDummy_Event = wxID_HIGHEST + 1000,
87 FirstAutoGenerated_Event = wxID_HIGHEST + 1999,
88 AudioMenu_Events = wxID_HIGHEST + 2000,
89 VideoMenu_Events = wxID_HIGHEST + 3000,
90 NavigMenu_Events = wxID_HIGHEST + 4000,
91 PopupMenu_Events = wxID_HIGHEST + 5000
94 BEGIN_EVENT_TABLE(Menu, wxMenu)
97 BEGIN_EVENT_TABLE(MenuEvtHandler, wxEvtHandler)
98 EVT_MENU(OpenFileSimple_Event, MenuEvtHandler::OnShowDialog)
99 EVT_MENU(OpenFile_Event, MenuEvtHandler::OnShowDialog)
100 EVT_MENU(OpenDisc_Event, MenuEvtHandler::OnShowDialog)
101 EVT_MENU(OpenNet_Event, MenuEvtHandler::OnShowDialog)
102 EVT_MENU(-1, MenuEvtHandler::OnMenuEvent)
105 wxMenu *OpenStreamMenu( intf_thread_t *p_intf )
107 wxMenu *menu = new wxMenu;
108 menu->Append( OpenFileSimple_Event, wxU(_("Simple &Open ...")) );
109 menu->Append( OpenFile_Event, wxU(_("Open &File...")) );
110 menu->Append( OpenDisc_Event, wxU(_("Open &Disc...")) );
111 menu->Append( OpenNet_Event, wxU(_("Open &Network Stream...")) );
115 void PopupMenu( intf_thread_t *p_intf, wxWindow *p_parent,
118 vlc_object_t *p_object;
119 char *ppsz_varnames[29];
123 /* Initializations */
124 memset( pi_objects, 0, 29 * sizeof(int) );
127 ppsz_varnames[i++] = _("Audio menu");
128 ppsz_varnames[i++] = NULL; /* Separator */
130 p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_AOUT,
132 if( p_object != NULL )
134 ppsz_varnames[i] = "audio-device";
135 pi_objects[i++] = p_object->i_object_id;
136 ppsz_varnames[i] = "audio-channels";
137 pi_objects[i++] = p_object->i_object_id;
138 vlc_object_release( p_object );
142 ppsz_varnames[i++] = NULL; /* Separator */
143 ppsz_varnames[i++] = _("Video menu");
144 ppsz_varnames[i++] = NULL; /* Separator */
146 p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_VOUT,
148 if( p_object != NULL )
150 ppsz_varnames[i] = "fullscreen";
151 pi_objects[i++] = p_object->i_object_id;
152 ppsz_varnames[i] = "deinterlace";
153 pi_objects[i++] = p_object->i_object_id;
154 ppsz_varnames[i] = "directx-on-top";
155 pi_objects[i++] = p_object->i_object_id;
156 vlc_object_release( p_object );
160 ppsz_varnames[i++] = NULL; /* Separator */
161 ppsz_varnames[i++] = _("Input menu");
162 ppsz_varnames[i++] = NULL; /* Separator */
164 p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
166 if( p_object != NULL )
168 ppsz_varnames[i] = "title";
169 pi_objects[i++] = p_object->i_object_id;
170 ppsz_varnames[i] = "chapter";
171 pi_objects[i++] = p_object->i_object_id;
172 ppsz_varnames[i] = "navigation";
173 pi_objects[i++] = p_object->i_object_id;
174 ppsz_varnames[i] = "program";
175 pi_objects[i++] = p_object->i_object_id;
177 ppsz_varnames[i] = "video-es";
178 pi_objects[i++] = p_object->i_object_id;
179 ppsz_varnames[i] = "audio-es";
180 pi_objects[i++] = p_object->i_object_id;
181 ppsz_varnames[i] = "spu-es";
182 pi_objects[i++] = p_object->i_object_id;
184 vlc_object_release( p_object );
188 Menu popupmenu( p_intf, p_parent, i,
189 ppsz_varnames, pi_objects, PopupMenu_Events );
192 /* Add static entries */
193 popupmenu.AppendSeparator();
194 popupmenu.Append( MenuDummy_Event, wxU("Open"),
195 OpenStreamMenu( p_intf ), wxT("") );
196 popupmenu.AppendSeparator();
197 popupmenu.Append( MenuDummy_Event, wxU(_("Close Menu")) );
200 p_intf->p_sys->p_popup_menu = &popupmenu;
201 p_parent->PopupMenu( &popupmenu, pos.x, pos.y );
202 p_intf->p_sys->p_popup_menu = NULL;
205 wxMenu *AudioMenu( intf_thread_t *_p_intf, wxWindow *p_parent )
207 vlc_object_t *p_object;
208 char *ppsz_varnames[5];
212 /* Initializations */
213 memset( pi_objects, 0, 5 * sizeof(int) );
215 p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_AOUT,
217 if( p_object != NULL )
219 ppsz_varnames[i] = "audio-device";
220 pi_objects[i++] = p_object->i_object_id;
221 ppsz_varnames[i] = "audio-channels";
222 pi_objects[i++] = p_object->i_object_id;
223 vlc_object_release( p_object );
226 p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_INPUT,
228 if( p_object != NULL )
230 ppsz_varnames[i] = "audio-es";
231 pi_objects[i++] = p_object->i_object_id;
232 vlc_object_release( p_object );
236 return new Menu( _p_intf, p_parent, i,
237 ppsz_varnames, pi_objects, AudioMenu_Events );
240 wxMenu *VideoMenu( intf_thread_t *_p_intf, wxWindow *p_parent )
242 vlc_object_t *p_object;
243 char *ppsz_varnames[6];
247 /* Initializations */
248 memset( pi_objects, 0, 6 * sizeof(int) );
250 p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_VOUT,
252 if( p_object != NULL )
254 ppsz_varnames[i] = "fullscreen";
255 pi_objects[i++] = p_object->i_object_id;
256 ppsz_varnames[i] = "deinterlace";
257 pi_objects[i++] = p_object->i_object_id;
258 ppsz_varnames[i] = "directx-on-top";
259 pi_objects[i++] = p_object->i_object_id;
260 vlc_object_release( p_object );
263 p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_INPUT,
265 if( p_object != NULL )
267 ppsz_varnames[i] = "video-es";
268 pi_objects[i++] = p_object->i_object_id;
269 ppsz_varnames[i] = "spu-es";
270 pi_objects[i++] = p_object->i_object_id;
271 vlc_object_release( p_object );
275 return new Menu( _p_intf, p_parent, i,
276 ppsz_varnames, pi_objects, VideoMenu_Events );
279 wxMenu *NavigMenu( intf_thread_t *_p_intf, wxWindow *p_parent )
281 vlc_object_t *p_object;
282 char *ppsz_varnames[10];
286 /* Initializations */
287 memset( pi_objects, 0, 4 * sizeof(int) );
289 p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_INPUT,
291 if( p_object != NULL )
293 ppsz_varnames[i] = "title";
294 pi_objects[i++] = p_object->i_object_id;
295 ppsz_varnames[i] = "chapter";
296 pi_objects[i++] = p_object->i_object_id;
297 ppsz_varnames[i] = "navigation";
298 pi_objects[i++] = p_object->i_object_id;
299 ppsz_varnames[i] = "program";
300 pi_objects[i++] = p_object->i_object_id;
302 ppsz_varnames[i] = "prev-title";
303 pi_objects[i++] = p_object->i_object_id;
304 ppsz_varnames[i] = "next-title";
305 pi_objects[i++] = p_object->i_object_id;
306 ppsz_varnames[i] = "prev-chapter";
307 pi_objects[i++] = p_object->i_object_id;
308 ppsz_varnames[i] = "next-chapter";
309 pi_objects[i++] = p_object->i_object_id;
311 vlc_object_release( p_object );
315 return new Menu( _p_intf, p_parent, i,
316 ppsz_varnames, pi_objects, NavigMenu_Events );
319 /*****************************************************************************
321 *****************************************************************************/
322 Menu::Menu( intf_thread_t *_p_intf, wxWindow *p_parent,
323 int i_count, char **ppsz_varnames, int *pi_objects,
324 int i_start_id ): wxMenu( )
326 vlc_object_t *p_object;
329 /* Initializations */
332 i_item_id = i_start_id;
334 for( i = 0; i < i_count; i++ )
336 if( !ppsz_varnames[i] )
344 Append( MenuDummy_Event, wxU(ppsz_varnames[i]) );
348 p_object = (vlc_object_t *)vlc_object_get( p_intf, pi_objects[i] );
349 if( p_object == NULL ) continue;
351 CreateMenuItem( this, ppsz_varnames[i], p_object );
352 vlc_object_release( p_object );
355 /* Special case for empty menus */
356 if( GetMenuItemCount() == 0 )
358 Append( MenuDummy_Event, wxU(_("Empty")) );
359 Enable( MenuDummy_Event, FALSE );
367 /*****************************************************************************
369 *****************************************************************************/
370 void Menu::CreateMenuItem( wxMenu *menu, char *psz_var,
371 vlc_object_t *p_object )
373 wxMenuItemExt *menuitem;
374 vlc_value_t val, text;
377 /* Check the type of the object variable */
378 i_type = var_Type( p_object, psz_var );
380 switch( i_type & VLC_VAR_TYPE )
384 case VLC_VAR_VARIABLE:
386 case VLC_VAR_INTEGER:
389 /* Variable doesn't exist or isn't handled */
393 /* Make sure we want to display the variable */
394 if( i_type & VLC_VAR_HASCHOICE )
396 var_Change( p_object, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
397 if( val.i_int == 0 ) return;
398 if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE && val.i_int == 1 )
402 /* Get the descriptive name of the variable */
403 var_Change( p_object, psz_var, VLC_VAR_GETTEXT, &text, NULL );
405 var_Get( p_object, psz_var, &val );
407 if( i_type & VLC_VAR_HASCHOICE )
409 menu->Append( MenuDummy_Event,
410 wxU(text.psz_string ? text.psz_string : psz_var),
411 CreateChoicesMenu( psz_var, p_object ),
412 wxT("")/* Nothing for now (maybe use a GETLONGTEXT) */ );
414 if( text.psz_string ) free( text.psz_string );
419 switch( i_type & VLC_VAR_TYPE )
422 menuitem = new wxMenuItemExt( menu, ++i_item_id,
423 wxU(text.psz_string ?
424 text.psz_string : psz_var),
425 wxT(""), wxITEM_NORMAL, strdup(psz_var),
426 p_object->i_object_id, val, i_type );
427 menu->Append( menuitem );
431 val.b_bool = !val.b_bool;
432 menuitem = new wxMenuItemExt( menu, ++i_item_id,
433 wxU(text.psz_string ?
434 text.psz_string : psz_var),
435 wxT(""), wxITEM_CHECK, strdup(psz_var),
436 p_object->i_object_id, val, i_type );
437 menu->Append( menuitem );
438 Check( i_item_id, val.b_bool ? FALSE : TRUE );
442 if( text.psz_string ) free( text.psz_string );
446 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
447 if( text.psz_string ) free( text.psz_string );
450 wxMenu *Menu::CreateChoicesMenu( char *psz_var, vlc_object_t *p_object )
452 vlc_value_t val, val_list, text_list;
455 /* Check the type of the object variable */
456 i_type = var_Type( p_object, psz_var );
458 /* Make sure we want to display the variable */
459 if( i_type & VLC_VAR_HASCHOICE )
461 var_Change( p_object, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
462 if( val.i_int == 0 ) return NULL;
463 if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE && val.i_int == 1 )
471 switch( i_type & VLC_VAR_TYPE )
475 case VLC_VAR_VARIABLE:
477 case VLC_VAR_INTEGER:
480 /* Variable doesn't exist or isn't handled */
484 if( var_Get( p_object, psz_var, &val ) < 0 )
489 if( var_Change( p_object, psz_var, VLC_VAR_GETLIST,
490 &val_list, &text_list ) < 0 )
492 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
496 wxMenu *menu = new wxMenu;
497 for( i = 0; i < val_list.p_list->i_count; i++ )
499 vlc_value_t another_val;
500 wxMenuItemExt *menuitem;
502 switch( i_type & VLC_VAR_TYPE )
504 case VLC_VAR_VARIABLE:
505 menu->Append( MenuDummy_Event,
506 wxU(text_list.p_list->p_values[i].psz_string ?
507 text_list.p_list->p_values[i].psz_string :
508 val_list.p_list->p_values[i].psz_string),
510 val_list.p_list->p_values[i].psz_string,
511 p_object ), wxT("") );
515 another_val.psz_string =
516 strdup(val_list.p_list->p_values[i].psz_string);
518 new wxMenuItemExt( menu, ++i_item_id,
519 wxU(text_list.p_list->p_values[i].psz_string ?
520 text_list.p_list->p_values[i].psz_string :
521 another_val.psz_string),
522 wxT(""), wxITEM_RADIO, strdup(psz_var),
523 p_object->i_object_id, another_val, i_type );
525 menu->Append( menuitem );
527 if( !strcmp( val.psz_string,
528 val_list.p_list->p_values[i].psz_string ) )
529 menu->Check( i_item_id, TRUE );
532 case VLC_VAR_INTEGER:
534 new wxMenuItemExt( menu, ++i_item_id,
535 text_list.p_list->p_values[i].psz_string ?
537 text_list.p_list->p_values[i].psz_string) :
538 wxString::Format(wxT("%d"),
539 val_list.p_list->p_values[i].i_int),
540 wxT(""), wxITEM_RADIO, strdup(psz_var),
541 p_object->i_object_id,
542 val_list.p_list->p_values[i], i_type );
544 menu->Append( menuitem );
546 if( val_list.p_list->p_values[i].i_int == val.i_int )
547 menu->Check( i_item_id, TRUE );
555 /* clean up everything */
556 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
557 var_Change( p_object, psz_var, VLC_VAR_FREELIST, &val_list, &text_list );
562 void Menu::OnShowDialog( wxCommandEvent& event )
564 if( p_intf->p_sys->pf_show_dialog )
568 switch( event.GetId() )
570 case OpenFileSimple_Event:
571 i_id = INTF_DIALOG_FILE_SIMPLE;
574 i_id = INTF_DIALOG_FILE;
577 i_id = INTF_DIALOG_DISC;
580 i_id = INTF_DIALOG_NET;
583 i_id = INTF_DIALOG_FILE;
588 p_intf->p_sys->pf_show_dialog( p_intf, i_id, 1 );
592 /*****************************************************************************
593 * A small helper class which intercepts all popup menu events
594 *****************************************************************************/
595 MenuEvtHandler::MenuEvtHandler( intf_thread_t *_p_intf,
596 Interface *_p_main_interface )
598 /* Initializations */
600 p_main_interface = _p_main_interface;
603 MenuEvtHandler::~MenuEvtHandler()
607 void MenuEvtHandler::OnShowDialog( wxCommandEvent& event )
609 if( p_intf->p_sys->pf_show_dialog )
613 switch( event.GetId() )
615 case OpenFileSimple_Event:
616 i_id = INTF_DIALOG_FILE_SIMPLE;
619 i_id = INTF_DIALOG_FILE;
622 i_id = INTF_DIALOG_DISC;
625 i_id = INTF_DIALOG_NET;
628 i_id = INTF_DIALOG_FILE;
633 p_intf->p_sys->pf_show_dialog( p_intf, i_id, 1 );
637 void MenuEvtHandler::OnMenuEvent( wxCommandEvent& event )
639 wxMenuItem *p_menuitem = NULL;
641 /* Check if this is an auto generated menu item */
642 if( event.GetId() < FirstAutoGenerated_Event )
648 if( !p_main_interface ||
649 (p_menuitem = p_main_interface->GetMenuBar()->FindItem(event.GetId()))
652 if( p_intf->p_sys->p_popup_menu )
655 p_intf->p_sys->p_popup_menu->FindItem( event.GetId() );
661 wxMenuItemExt *p_menuitemext = (wxMenuItemExt *)p_menuitem;
662 vlc_object_t *p_object;
664 p_object = (vlc_object_t *)vlc_object_get( p_intf,
665 p_menuitemext->i_object_id );
666 if( p_object == NULL ) return;
668 var_Set( p_object, p_menuitemext->psz_var, p_menuitemext->val );
670 vlc_object_release( p_object );
676 /*****************************************************************************
677 * A small helper class which encapsulate wxMenuitem with some other useful
679 *****************************************************************************/
680 wxMenuItemExt::wxMenuItemExt( wxMenu* parentMenu, int id, const wxString& text,
681 const wxString& helpString, wxItemKind kind,
682 char *_psz_var, int _i_object_id, vlc_value_t _val, int _i_val_type ):
683 wxMenuItem( parentMenu, id, text, helpString, kind )
685 /* Initializations */
687 i_val_type = _i_val_type;
688 i_object_id = _i_object_id;
692 wxMenuItemExt::~wxMenuItemExt()
694 if( psz_var ) free( psz_var );
695 if( ((i_val_type & VLC_VAR_TYPE) == VLC_VAR_STRING)
696 && val.psz_string ) free( val.psz_string );