1 /*****************************************************************************
2 * menus.cpp : wxWindows plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2001 VideoLAN
5 * $Id: menus.cpp,v 1.20 2003/08/28 15:59:04 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() */
35 #include "wxwindows.h"
36 #include <wx/listctrl.h>
38 class wxMenuItemExt: public wxMenuItem
42 wxMenuItemExt( wxMenu* parentMenu, int id, const wxString& text,
43 const wxString& helpString, wxItemKind kind,
44 char *_psz_var, int _i_object_id, vlc_value_t _val,
47 virtual ~wxMenuItemExt();
58 /*****************************************************************************
60 *****************************************************************************/
62 /* IDs for the controls and the menu commands */
66 MenuDummy_Event = wxID_HIGHEST + 1000,
71 FirstAutoGenerated_Event = wxID_HIGHEST + 1999,
72 AudioMenu_Events = wxID_HIGHEST + 2000,
73 VideoMenu_Events = wxID_HIGHEST + 3000,
74 NavigMenu_Events = wxID_HIGHEST + 4000,
75 PopupMenu_Events = wxID_HIGHEST + 5000
78 BEGIN_EVENT_TABLE(Menu, wxMenu)
81 BEGIN_EVENT_TABLE(MenuEvtHandler, wxEvtHandler)
82 EVT_MENU(OpenFileSimple_Event, MenuEvtHandler::OnShowDialog)
83 EVT_MENU(OpenFile_Event, MenuEvtHandler::OnShowDialog)
84 EVT_MENU(OpenDisc_Event, MenuEvtHandler::OnShowDialog)
85 EVT_MENU(OpenNet_Event, MenuEvtHandler::OnShowDialog)
86 EVT_MENU(-1, MenuEvtHandler::OnMenuEvent)
89 wxMenu *OpenStreamMenu( intf_thread_t *p_intf )
91 wxMenu *menu = new wxMenu;
92 menu->Append( OpenFileSimple_Event, wxU(_("Simple &Open ...")) );
93 menu->Append( OpenFile_Event, wxU(_("Open &File...")) );
94 menu->Append( OpenDisc_Event, wxU(_("Open &Disc...")) );
95 menu->Append( OpenNet_Event, wxU(_("Open &Network Stream...")) );
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] = "program";
157 pi_objects[i++] = p_object->i_object_id;
158 ppsz_varnames[i] = "navigation";
159 pi_objects[i++] = p_object->i_object_id;
160 ppsz_varnames[i] = "dvd_menus";
161 pi_objects[i++] = p_object->i_object_id;
163 ppsz_varnames[i] = "video-es";
164 pi_objects[i++] = p_object->i_object_id;
165 ppsz_varnames[i] = "audio-es";
166 pi_objects[i++] = p_object->i_object_id;
167 ppsz_varnames[i] = "spu-es";
168 pi_objects[i++] = p_object->i_object_id;
170 vlc_object_release( p_object );
174 Menu popupmenu( p_intf, p_parent, i,
175 ppsz_varnames, pi_objects, PopupMenu_Events );
178 /* Add static entries */
179 popupmenu.AppendSeparator();
180 popupmenu.Append( MenuDummy_Event, wxU("Open"),
181 OpenStreamMenu( p_intf ), wxT("") );
184 p_intf->p_sys->p_popup_menu = &popupmenu;
185 p_parent->PopupMenu( &popupmenu, pos.x, pos.y );
186 p_intf->p_sys->p_popup_menu = NULL;
189 wxMenu *AudioMenu( intf_thread_t *_p_intf, wxWindow *p_parent )
191 vlc_object_t *p_object;
192 char *ppsz_varnames[5];
196 /* Initializations */
197 memset( pi_objects, 0, 5 * sizeof(int) );
199 p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_AOUT,
201 if( p_object != NULL )
203 ppsz_varnames[i] = "audio-device";
204 pi_objects[i++] = p_object->i_object_id;
205 ppsz_varnames[i] = "audio-channels";
206 pi_objects[i++] = p_object->i_object_id;
207 vlc_object_release( p_object );
210 p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_INPUT,
212 if( p_object != NULL )
214 ppsz_varnames[i] = "audio-es";
215 pi_objects[i++] = p_object->i_object_id;
216 vlc_object_release( p_object );
220 return new Menu( _p_intf, p_parent, i,
221 ppsz_varnames, pi_objects, AudioMenu_Events );
224 wxMenu *VideoMenu( intf_thread_t *_p_intf, wxWindow *p_parent )
226 vlc_object_t *p_object;
227 char *ppsz_varnames[6];
231 /* Initializations */
232 memset( pi_objects, 0, 6 * sizeof(int) );
234 p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_VOUT,
236 if( p_object != NULL )
238 ppsz_varnames[i] = "fullscreen";
239 pi_objects[i++] = p_object->i_object_id;
240 ppsz_varnames[i] = "deinterlace";
241 pi_objects[i++] = p_object->i_object_id;
242 ppsz_varnames[i] = "directx-on-top";
243 pi_objects[i++] = p_object->i_object_id;
244 vlc_object_release( p_object );
247 p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_INPUT,
249 if( p_object != NULL )
251 ppsz_varnames[i] = "video-es";
252 pi_objects[i++] = p_object->i_object_id;
253 ppsz_varnames[i] = "spu-es";
254 pi_objects[i++] = p_object->i_object_id;
255 vlc_object_release( p_object );
259 return new Menu( _p_intf, p_parent, i,
260 ppsz_varnames, pi_objects, VideoMenu_Events );
263 wxMenu *NavigMenu( intf_thread_t *_p_intf, wxWindow *p_parent )
265 vlc_object_t *p_object;
266 char *ppsz_varnames[10];
270 /* Initializations */
271 memset( pi_objects, 0, 4 * sizeof(int) );
273 p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_INPUT,
275 if( p_object != NULL )
277 ppsz_varnames[i] = "title";
278 pi_objects[i++] = p_object->i_object_id;
279 ppsz_varnames[i] = "chapter";
280 pi_objects[i++] = p_object->i_object_id;
281 ppsz_varnames[i] = "program";
282 pi_objects[i++] = p_object->i_object_id;
283 ppsz_varnames[i] = "navigation";
284 pi_objects[i++] = p_object->i_object_id;
285 ppsz_varnames[i] = "dvd_menus";
286 pi_objects[i++] = p_object->i_object_id;
288 ppsz_varnames[i] = "prev-title";
289 pi_objects[i++] = p_object->i_object_id;
290 ppsz_varnames[i] = "next-title";
291 pi_objects[i++] = p_object->i_object_id;
292 ppsz_varnames[i] = "prev-chapter";
293 pi_objects[i++] = p_object->i_object_id;
294 ppsz_varnames[i] = "next-chapter";
295 pi_objects[i++] = p_object->i_object_id;
297 vlc_object_release( p_object );
301 return new Menu( _p_intf, p_parent, i,
302 ppsz_varnames, pi_objects, NavigMenu_Events );
305 /*****************************************************************************
307 *****************************************************************************/
308 Menu::Menu( intf_thread_t *_p_intf, wxWindow *p_parent,
309 int i_count, char **ppsz_varnames, int *pi_objects,
310 int i_start_id ): wxMenu( )
312 vlc_object_t *p_object;
315 /* Initializations */
318 i_item_id = i_start_id;
320 for( i = 0; i < i_count; i++ )
322 if( !ppsz_varnames[i] )
330 Append( MenuDummy_Event, wxU(ppsz_varnames[i]) );
334 p_object = (vlc_object_t *)vlc_object_get( p_intf, pi_objects[i] );
335 if( p_object == NULL ) continue;
337 CreateMenuItem( this, ppsz_varnames[i], p_object );
338 vlc_object_release( p_object );
341 /* Special case for empty menus */
342 if( GetMenuItemCount() == 0 )
344 Append( MenuDummy_Event, wxU(_("Empty")) );
345 Enable( MenuDummy_Event, FALSE );
353 /*****************************************************************************
355 *****************************************************************************/
356 void Menu::CreateMenuItem( wxMenu *menu, char *psz_var,
357 vlc_object_t *p_object )
359 wxMenuItemExt *menuitem;
360 vlc_value_t val, text;
363 /* Check the type of the object variable */
364 i_type = var_Type( p_object, psz_var );
366 switch( i_type & VLC_VAR_TYPE )
370 case VLC_VAR_VARIABLE:
372 case VLC_VAR_INTEGER:
375 /* Variable doesn't exist or isn't handled */
379 /* Make sure we want to display the variable */
380 if( i_type & VLC_VAR_HASCHOICE )
382 var_Change( p_object, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
383 if( val.i_int == 0 ) return;
384 if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE && val.i_int == 1 )
388 /* Get the descriptive name of the variable */
389 var_Change( p_object, psz_var, VLC_VAR_GETTEXT, &text, NULL );
391 var_Get( p_object, psz_var, &val );
393 if( i_type & VLC_VAR_HASCHOICE )
395 menu->Append( MenuDummy_Event,
396 wxU(text.psz_string ? text.psz_string : psz_var),
397 CreateChoicesMenu( psz_var, p_object ),
398 wxT("")/* Nothing for now (maybe use a GETLONGTEXT) */ );
400 if( text.psz_string ) free( text.psz_string );
405 switch( i_type & VLC_VAR_TYPE )
408 menuitem = new wxMenuItemExt( menu, ++i_item_id,
409 wxU(text.psz_string ?
410 text.psz_string : psz_var),
411 wxT(""), wxITEM_NORMAL, strdup(psz_var),
412 p_object->i_object_id, val, i_type );
413 menu->Append( menuitem );
417 val.b_bool = !val.b_bool;
418 menuitem = new wxMenuItemExt( menu, ++i_item_id,
419 wxU(text.psz_string ?
420 text.psz_string : psz_var),
421 wxT(""), wxITEM_CHECK, strdup(psz_var),
422 p_object->i_object_id, val, i_type );
423 menu->Append( menuitem );
424 Check( i_item_id, val.b_bool ? FALSE : TRUE );
428 if( text.psz_string ) free( text.psz_string );
432 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
433 if( text.psz_string ) free( text.psz_string );
436 wxMenu *Menu::CreateChoicesMenu( char *psz_var, vlc_object_t *p_object )
438 vlc_value_t val, val_list, text_list;
441 /* Check the type of the object variable */
442 i_type = var_Type( p_object, psz_var );
444 /* Make sure we want to display the variable */
445 if( i_type & VLC_VAR_HASCHOICE )
447 var_Change( p_object, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
448 if( val.i_int == 0 ) return NULL;
449 if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE && val.i_int == 1 )
457 switch( i_type & VLC_VAR_TYPE )
461 case VLC_VAR_VARIABLE:
463 case VLC_VAR_INTEGER:
466 /* Variable doesn't exist or isn't handled */
470 if( var_Get( p_object, psz_var, &val ) < 0 )
475 if( var_Change( p_object, psz_var, VLC_VAR_GETLIST,
476 &val_list, &text_list ) < 0 )
478 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
482 wxMenu *menu = new wxMenu;
483 for( i = 0; i < val_list.p_list->i_count; i++ )
485 vlc_value_t another_val;
486 wxMenuItemExt *menuitem;
488 switch( i_type & VLC_VAR_TYPE )
490 case VLC_VAR_VARIABLE:
491 menu->Append( MenuDummy_Event,
492 wxU(text_list.p_list->p_values[i].psz_string ?
493 text_list.p_list->p_values[i].psz_string :
494 val_list.p_list->p_values[i].psz_string),
496 val_list.p_list->p_values[i].psz_string,
497 p_object ), wxT("") );
501 another_val.psz_string =
502 strdup(val_list.p_list->p_values[i].psz_string);
504 new wxMenuItemExt( menu, ++i_item_id,
505 wxU(text_list.p_list->p_values[i].psz_string ?
506 text_list.p_list->p_values[i].psz_string :
507 another_val.psz_string), wxT(""),
508 i_type & VLC_VAR_ISCOMMAND ?
509 wxITEM_NORMAL : wxITEM_RADIO,
511 p_object->i_object_id, another_val, i_type );
513 menu->Append( menuitem );
515 if( !strcmp( val.psz_string,
516 val_list.p_list->p_values[i].psz_string ) )
517 menu->Check( i_item_id, TRUE );
520 case VLC_VAR_INTEGER:
522 new wxMenuItemExt( menu, ++i_item_id,
523 text_list.p_list->p_values[i].psz_string ?
525 text_list.p_list->p_values[i].psz_string) :
526 wxString::Format(wxT("%d"),
527 val_list.p_list->p_values[i].i_int), wxT(""),
528 i_type & VLC_VAR_ISCOMMAND ?
529 wxITEM_NORMAL : wxITEM_RADIO,
531 p_object->i_object_id,
532 val_list.p_list->p_values[i], i_type );
534 menu->Append( menuitem );
536 if( val_list.p_list->p_values[i].i_int == val.i_int )
537 menu->Check( i_item_id, TRUE );
545 /* clean up everything */
546 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
547 var_Change( p_object, psz_var, VLC_VAR_FREELIST, &val_list, &text_list );
552 void Menu::OnShowDialog( wxCommandEvent& event )
554 if( p_intf->p_sys->pf_show_dialog )
558 switch( event.GetId() )
560 case OpenFileSimple_Event:
561 i_id = INTF_DIALOG_FILE_SIMPLE;
564 i_id = INTF_DIALOG_FILE;
567 i_id = INTF_DIALOG_DISC;
570 i_id = INTF_DIALOG_NET;
573 i_id = INTF_DIALOG_FILE;
578 p_intf->p_sys->pf_show_dialog( p_intf, i_id, 1, 0 );
582 /*****************************************************************************
583 * A small helper class which intercepts all popup menu events
584 *****************************************************************************/
585 MenuEvtHandler::MenuEvtHandler( intf_thread_t *_p_intf,
586 Interface *_p_main_interface )
588 /* Initializations */
590 p_main_interface = _p_main_interface;
593 MenuEvtHandler::~MenuEvtHandler()
597 void MenuEvtHandler::OnShowDialog( wxCommandEvent& event )
599 if( p_intf->p_sys->pf_show_dialog )
603 switch( event.GetId() )
605 case OpenFileSimple_Event:
606 i_id = INTF_DIALOG_FILE_SIMPLE;
609 i_id = INTF_DIALOG_FILE;
612 i_id = INTF_DIALOG_DISC;
615 i_id = INTF_DIALOG_NET;
618 i_id = INTF_DIALOG_FILE;
623 p_intf->p_sys->pf_show_dialog( p_intf, i_id, 1, 0 );
627 void MenuEvtHandler::OnMenuEvent( wxCommandEvent& event )
629 wxMenuItem *p_menuitem = NULL;
631 /* Check if this is an auto generated menu item */
632 if( event.GetId() < FirstAutoGenerated_Event )
638 if( !p_main_interface ||
639 (p_menuitem = p_main_interface->GetMenuBar()->FindItem(event.GetId()))
642 if( p_intf->p_sys->p_popup_menu )
645 p_intf->p_sys->p_popup_menu->FindItem( event.GetId() );
651 wxMenuItemExt *p_menuitemext = (wxMenuItemExt *)p_menuitem;
652 vlc_object_t *p_object;
654 p_object = (vlc_object_t *)vlc_object_get( p_intf,
655 p_menuitemext->i_object_id );
656 if( p_object == NULL ) return;
658 var_Set( p_object, p_menuitemext->psz_var, p_menuitemext->val );
660 vlc_object_release( p_object );
666 /*****************************************************************************
667 * A small helper class which encapsulate wxMenuitem with some other useful
669 *****************************************************************************/
670 wxMenuItemExt::wxMenuItemExt( wxMenu* parentMenu, int id, const wxString& text,
671 const wxString& helpString, wxItemKind kind,
672 char *_psz_var, int _i_object_id, vlc_value_t _val, int _i_val_type ):
673 wxMenuItem( parentMenu, id, text, helpString, kind )
675 /* Initializations */
677 i_val_type = _i_val_type;
678 i_object_id = _i_object_id;
682 wxMenuItemExt::~wxMenuItemExt()
684 if( psz_var ) free( psz_var );
685 if( ((i_val_type & VLC_VAR_TYPE) == VLC_VAR_STRING)
686 && val.psz_string ) free( val.psz_string );