1 /*****************************************************************************
2 * menus.cpp : wxWindows plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2001 VideoLAN
5 * $Id: menus.cpp,v 1.19 2003/08/27 11:53:26 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("") );
182 popupmenu.AppendSeparator();
183 popupmenu.Append( MenuDummy_Event, wxU(_("Close Menu")) );
186 p_intf->p_sys->p_popup_menu = &popupmenu;
187 p_parent->PopupMenu( &popupmenu, pos.x, pos.y );
188 p_intf->p_sys->p_popup_menu = NULL;
191 wxMenu *AudioMenu( intf_thread_t *_p_intf, wxWindow *p_parent )
193 vlc_object_t *p_object;
194 char *ppsz_varnames[5];
198 /* Initializations */
199 memset( pi_objects, 0, 5 * sizeof(int) );
201 p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_AOUT,
203 if( p_object != NULL )
205 ppsz_varnames[i] = "audio-device";
206 pi_objects[i++] = p_object->i_object_id;
207 ppsz_varnames[i] = "audio-channels";
208 pi_objects[i++] = p_object->i_object_id;
209 vlc_object_release( p_object );
212 p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_INPUT,
214 if( p_object != NULL )
216 ppsz_varnames[i] = "audio-es";
217 pi_objects[i++] = p_object->i_object_id;
218 vlc_object_release( p_object );
222 return new Menu( _p_intf, p_parent, i,
223 ppsz_varnames, pi_objects, AudioMenu_Events );
226 wxMenu *VideoMenu( intf_thread_t *_p_intf, wxWindow *p_parent )
228 vlc_object_t *p_object;
229 char *ppsz_varnames[6];
233 /* Initializations */
234 memset( pi_objects, 0, 6 * sizeof(int) );
236 p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_VOUT,
238 if( p_object != NULL )
240 ppsz_varnames[i] = "fullscreen";
241 pi_objects[i++] = p_object->i_object_id;
242 ppsz_varnames[i] = "deinterlace";
243 pi_objects[i++] = p_object->i_object_id;
244 ppsz_varnames[i] = "directx-on-top";
245 pi_objects[i++] = p_object->i_object_id;
246 vlc_object_release( p_object );
249 p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_INPUT,
251 if( p_object != NULL )
253 ppsz_varnames[i] = "video-es";
254 pi_objects[i++] = p_object->i_object_id;
255 ppsz_varnames[i] = "spu-es";
256 pi_objects[i++] = p_object->i_object_id;
257 vlc_object_release( p_object );
261 return new Menu( _p_intf, p_parent, i,
262 ppsz_varnames, pi_objects, VideoMenu_Events );
265 wxMenu *NavigMenu( intf_thread_t *_p_intf, wxWindow *p_parent )
267 vlc_object_t *p_object;
268 char *ppsz_varnames[10];
272 /* Initializations */
273 memset( pi_objects, 0, 4 * sizeof(int) );
275 p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_INPUT,
277 if( p_object != NULL )
279 ppsz_varnames[i] = "title";
280 pi_objects[i++] = p_object->i_object_id;
281 ppsz_varnames[i] = "chapter";
282 pi_objects[i++] = p_object->i_object_id;
283 ppsz_varnames[i] = "program";
284 pi_objects[i++] = p_object->i_object_id;
285 ppsz_varnames[i] = "navigation";
286 pi_objects[i++] = p_object->i_object_id;
287 ppsz_varnames[i] = "dvd_menus";
288 pi_objects[i++] = p_object->i_object_id;
290 ppsz_varnames[i] = "prev-title";
291 pi_objects[i++] = p_object->i_object_id;
292 ppsz_varnames[i] = "next-title";
293 pi_objects[i++] = p_object->i_object_id;
294 ppsz_varnames[i] = "prev-chapter";
295 pi_objects[i++] = p_object->i_object_id;
296 ppsz_varnames[i] = "next-chapter";
297 pi_objects[i++] = p_object->i_object_id;
299 vlc_object_release( p_object );
303 return new Menu( _p_intf, p_parent, i,
304 ppsz_varnames, pi_objects, NavigMenu_Events );
307 /*****************************************************************************
309 *****************************************************************************/
310 Menu::Menu( intf_thread_t *_p_intf, wxWindow *p_parent,
311 int i_count, char **ppsz_varnames, int *pi_objects,
312 int i_start_id ): wxMenu( )
314 vlc_object_t *p_object;
317 /* Initializations */
320 i_item_id = i_start_id;
322 for( i = 0; i < i_count; i++ )
324 if( !ppsz_varnames[i] )
332 Append( MenuDummy_Event, wxU(ppsz_varnames[i]) );
336 p_object = (vlc_object_t *)vlc_object_get( p_intf, pi_objects[i] );
337 if( p_object == NULL ) continue;
339 CreateMenuItem( this, ppsz_varnames[i], p_object );
340 vlc_object_release( p_object );
343 /* Special case for empty menus */
344 if( GetMenuItemCount() == 0 )
346 Append( MenuDummy_Event, wxU(_("Empty")) );
347 Enable( MenuDummy_Event, FALSE );
355 /*****************************************************************************
357 *****************************************************************************/
358 void Menu::CreateMenuItem( wxMenu *menu, char *psz_var,
359 vlc_object_t *p_object )
361 wxMenuItemExt *menuitem;
362 vlc_value_t val, text;
365 /* Check the type of the object variable */
366 i_type = var_Type( p_object, psz_var );
368 switch( i_type & VLC_VAR_TYPE )
372 case VLC_VAR_VARIABLE:
374 case VLC_VAR_INTEGER:
377 /* Variable doesn't exist or isn't handled */
381 /* Make sure we want to display the variable */
382 if( i_type & VLC_VAR_HASCHOICE )
384 var_Change( p_object, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
385 if( val.i_int == 0 ) return;
386 if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE && val.i_int == 1 )
390 /* Get the descriptive name of the variable */
391 var_Change( p_object, psz_var, VLC_VAR_GETTEXT, &text, NULL );
393 var_Get( p_object, psz_var, &val );
395 if( i_type & VLC_VAR_HASCHOICE )
397 menu->Append( MenuDummy_Event,
398 wxU(text.psz_string ? text.psz_string : psz_var),
399 CreateChoicesMenu( psz_var, p_object ),
400 wxT("")/* Nothing for now (maybe use a GETLONGTEXT) */ );
402 if( text.psz_string ) free( text.psz_string );
407 switch( i_type & VLC_VAR_TYPE )
410 menuitem = new wxMenuItemExt( menu, ++i_item_id,
411 wxU(text.psz_string ?
412 text.psz_string : psz_var),
413 wxT(""), wxITEM_NORMAL, strdup(psz_var),
414 p_object->i_object_id, val, i_type );
415 menu->Append( menuitem );
419 val.b_bool = !val.b_bool;
420 menuitem = new wxMenuItemExt( menu, ++i_item_id,
421 wxU(text.psz_string ?
422 text.psz_string : psz_var),
423 wxT(""), wxITEM_CHECK, strdup(psz_var),
424 p_object->i_object_id, val, i_type );
425 menu->Append( menuitem );
426 Check( i_item_id, val.b_bool ? FALSE : TRUE );
430 if( text.psz_string ) free( text.psz_string );
434 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
435 if( text.psz_string ) free( text.psz_string );
438 wxMenu *Menu::CreateChoicesMenu( char *psz_var, vlc_object_t *p_object )
440 vlc_value_t val, val_list, text_list;
443 /* Check the type of the object variable */
444 i_type = var_Type( p_object, psz_var );
446 /* Make sure we want to display the variable */
447 if( i_type & VLC_VAR_HASCHOICE )
449 var_Change( p_object, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
450 if( val.i_int == 0 ) return NULL;
451 if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE && val.i_int == 1 )
459 switch( i_type & VLC_VAR_TYPE )
463 case VLC_VAR_VARIABLE:
465 case VLC_VAR_INTEGER:
468 /* Variable doesn't exist or isn't handled */
472 if( var_Get( p_object, psz_var, &val ) < 0 )
477 if( var_Change( p_object, psz_var, VLC_VAR_GETLIST,
478 &val_list, &text_list ) < 0 )
480 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
484 wxMenu *menu = new wxMenu;
485 for( i = 0; i < val_list.p_list->i_count; i++ )
487 vlc_value_t another_val;
488 wxMenuItemExt *menuitem;
490 switch( i_type & VLC_VAR_TYPE )
492 case VLC_VAR_VARIABLE:
493 menu->Append( MenuDummy_Event,
494 wxU(text_list.p_list->p_values[i].psz_string ?
495 text_list.p_list->p_values[i].psz_string :
496 val_list.p_list->p_values[i].psz_string),
498 val_list.p_list->p_values[i].psz_string,
499 p_object ), wxT("") );
503 another_val.psz_string =
504 strdup(val_list.p_list->p_values[i].psz_string);
506 new wxMenuItemExt( menu, ++i_item_id,
507 wxU(text_list.p_list->p_values[i].psz_string ?
508 text_list.p_list->p_values[i].psz_string :
509 another_val.psz_string), wxT(""),
510 i_type & VLC_VAR_ISCOMMAND ?
511 wxITEM_NORMAL : wxITEM_RADIO,
513 p_object->i_object_id, another_val, i_type );
515 menu->Append( menuitem );
517 if( !strcmp( val.psz_string,
518 val_list.p_list->p_values[i].psz_string ) )
519 menu->Check( i_item_id, TRUE );
522 case VLC_VAR_INTEGER:
524 new wxMenuItemExt( menu, ++i_item_id,
525 text_list.p_list->p_values[i].psz_string ?
527 text_list.p_list->p_values[i].psz_string) :
528 wxString::Format(wxT("%d"),
529 val_list.p_list->p_values[i].i_int), wxT(""),
530 i_type & VLC_VAR_ISCOMMAND ?
531 wxITEM_NORMAL : wxITEM_RADIO,
533 p_object->i_object_id,
534 val_list.p_list->p_values[i], i_type );
536 menu->Append( menuitem );
538 if( val_list.p_list->p_values[i].i_int == val.i_int )
539 menu->Check( i_item_id, TRUE );
547 /* clean up everything */
548 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
549 var_Change( p_object, psz_var, VLC_VAR_FREELIST, &val_list, &text_list );
554 void Menu::OnShowDialog( wxCommandEvent& event )
556 if( p_intf->p_sys->pf_show_dialog )
560 switch( event.GetId() )
562 case OpenFileSimple_Event:
563 i_id = INTF_DIALOG_FILE_SIMPLE;
566 i_id = INTF_DIALOG_FILE;
569 i_id = INTF_DIALOG_DISC;
572 i_id = INTF_DIALOG_NET;
575 i_id = INTF_DIALOG_FILE;
580 p_intf->p_sys->pf_show_dialog( p_intf, i_id, 1, 0 );
584 /*****************************************************************************
585 * A small helper class which intercepts all popup menu events
586 *****************************************************************************/
587 MenuEvtHandler::MenuEvtHandler( intf_thread_t *_p_intf,
588 Interface *_p_main_interface )
590 /* Initializations */
592 p_main_interface = _p_main_interface;
595 MenuEvtHandler::~MenuEvtHandler()
599 void MenuEvtHandler::OnShowDialog( wxCommandEvent& event )
601 if( p_intf->p_sys->pf_show_dialog )
605 switch( event.GetId() )
607 case OpenFileSimple_Event:
608 i_id = INTF_DIALOG_FILE_SIMPLE;
611 i_id = INTF_DIALOG_FILE;
614 i_id = INTF_DIALOG_DISC;
617 i_id = INTF_DIALOG_NET;
620 i_id = INTF_DIALOG_FILE;
625 p_intf->p_sys->pf_show_dialog( p_intf, i_id, 1, 0 );
629 void MenuEvtHandler::OnMenuEvent( wxCommandEvent& event )
631 wxMenuItem *p_menuitem = NULL;
633 /* Check if this is an auto generated menu item */
634 if( event.GetId() < FirstAutoGenerated_Event )
640 if( !p_main_interface ||
641 (p_menuitem = p_main_interface->GetMenuBar()->FindItem(event.GetId()))
644 if( p_intf->p_sys->p_popup_menu )
647 p_intf->p_sys->p_popup_menu->FindItem( event.GetId() );
653 wxMenuItemExt *p_menuitemext = (wxMenuItemExt *)p_menuitem;
654 vlc_object_t *p_object;
656 p_object = (vlc_object_t *)vlc_object_get( p_intf,
657 p_menuitemext->i_object_id );
658 if( p_object == NULL ) return;
660 var_Set( p_object, p_menuitemext->psz_var, p_menuitemext->val );
662 vlc_object_release( p_object );
668 /*****************************************************************************
669 * A small helper class which encapsulate wxMenuitem with some other useful
671 *****************************************************************************/
672 wxMenuItemExt::wxMenuItemExt( wxMenu* parentMenu, int id, const wxString& text,
673 const wxString& helpString, wxItemKind kind,
674 char *_psz_var, int _i_object_id, vlc_value_t _val, int _i_val_type ):
675 wxMenuItem( parentMenu, id, text, helpString, kind )
677 /* Initializations */
679 i_val_type = _i_val_type;
680 i_object_id = _i_object_id;
684 wxMenuItemExt::~wxMenuItemExt()
686 if( psz_var ) free( psz_var );
687 if( ((i_val_type & VLC_VAR_TYPE) == VLC_VAR_STRING)
688 && val.psz_string ) free( val.psz_string );