1 /*****************************************************************************
2 * menus.cpp : wxWindows plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2004 VideoLAN
5 * $Id: menus.cpp,v 1.30 2004/01/25 03:29:01 hartman 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"
37 class wxMenuItemExt: public wxMenuItem
41 wxMenuItemExt( wxMenu* parentMenu, int id, const wxString& text,
42 const wxString& helpString, wxItemKind kind,
43 char *_psz_var, int _i_object_id, vlc_value_t _val,
46 virtual ~wxMenuItemExt();
57 /*****************************************************************************
59 *****************************************************************************/
61 /* IDs for the controls and the menu commands */
65 MenuDummy_Event = wxID_HIGHEST + 1000,
66 OpenFileSimple_Event = wxID_HIGHEST + 1100,
70 FirstAutoGenerated_Event = wxID_HIGHEST + 1999,
71 SettingsMenu_Events = wxID_HIGHEST + 5000,
72 AudioMenu_Events = wxID_HIGHEST + 2000,
73 VideoMenu_Events = wxID_HIGHEST + 3000,
74 NavigMenu_Events = wxID_HIGHEST + 4000,
75 PopupMenu_Events = wxID_HIGHEST + 6000
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(_("Quick &Open File...")) );
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 #define MAX_POPUP_ITEMS 35
104 vlc_object_t *p_object;
105 char *ppsz_varnames[MAX_POPUP_ITEMS];
106 int pi_objects[MAX_POPUP_ITEMS];
109 /* Initializations */
110 memset( pi_objects, 0, MAX_POPUP_ITEMS * sizeof(int) );
113 ppsz_varnames[i++] = _("Audio menu");
114 ppsz_varnames[i++] = NULL; /* Separator */
116 p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_AOUT,
118 if( p_object != NULL )
120 ppsz_varnames[i] = "audio-device";
121 pi_objects[i++] = p_object->i_object_id;
122 ppsz_varnames[i] = "audio-channels";
123 pi_objects[i++] = p_object->i_object_id;
124 ppsz_varnames[i] = "visual";
125 pi_objects[i++] = p_object->i_object_id;
126 vlc_object_release( p_object );
130 ppsz_varnames[i++] = NULL; /* Separator */
131 ppsz_varnames[i++] = _("Video menu");
132 ppsz_varnames[i++] = NULL; /* Separator */
134 p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_VOUT,
136 if( p_object != NULL )
138 vlc_object_t *p_dec_obj;
140 ppsz_varnames[i] = "fullscreen";
141 pi_objects[i++] = p_object->i_object_id;
142 ppsz_varnames[i] = "deinterlace";
143 pi_objects[i++] = p_object->i_object_id;
144 ppsz_varnames[i] = "aspect-ratio";
145 pi_objects[i++] = p_object->i_object_id;
146 ppsz_varnames[i] = "crop";
147 pi_objects[i++] = p_object->i_object_id;
148 ppsz_varnames[i] = "video-on-top";
149 pi_objects[i++] = p_object->i_object_id;
151 p_dec_obj = (vlc_object_t *)vlc_object_find( p_object,
154 if( p_dec_obj != NULL )
156 ppsz_varnames[i] = "ffmpeg-pp-q";
157 pi_objects[i++] = p_dec_obj->i_object_id;
158 vlc_object_release( p_dec_obj );
161 vlc_object_release( p_object );
165 ppsz_varnames[i++] = NULL; /* Separator */
166 ppsz_varnames[i++] = _("Input menu");
167 ppsz_varnames[i++] = NULL; /* Separator */
169 p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
171 if( p_object != NULL )
173 ppsz_varnames[i] = "title";
174 pi_objects[i++] = p_object->i_object_id;
175 ppsz_varnames[i] = "chapter";
176 pi_objects[i++] = p_object->i_object_id;
177 ppsz_varnames[i] = "program";
178 pi_objects[i++] = p_object->i_object_id;
179 ppsz_varnames[i] = "navigation";
180 pi_objects[i++] = p_object->i_object_id;
181 ppsz_varnames[i] = "dvd_menus";
182 pi_objects[i++] = p_object->i_object_id;
184 ppsz_varnames[i] = "video-es";
185 pi_objects[i++] = p_object->i_object_id;
186 ppsz_varnames[i] = "audio-es";
187 pi_objects[i++] = p_object->i_object_id;
188 ppsz_varnames[i] = "spu-es";
189 pi_objects[i++] = p_object->i_object_id;
191 vlc_object_release( p_object );
195 ppsz_varnames[i++] = NULL; /* Separator */
196 ppsz_varnames[i++] = _("Interface menu");
197 ppsz_varnames[i++] = NULL; /* Separator */
199 /* vlc_object_find is needed because of the dialogs provider case */
200 p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_INTF,
202 if( p_object != NULL )
204 ppsz_varnames[i] = "intf-switch";
205 pi_objects[i++] = p_object->i_object_id;
206 ppsz_varnames[i] = "intf-add";
207 pi_objects[i++] = p_object->i_object_id;
209 vlc_object_release( p_object );
213 Menu popupmenu( p_intf, p_parent, i,
214 ppsz_varnames, pi_objects, PopupMenu_Events );
217 /* Add static entries */
218 popupmenu.AppendSeparator();
219 popupmenu.Append( MenuDummy_Event, wxU("Open..."),
220 OpenStreamMenu( p_intf ), wxT("") );
223 p_intf->p_sys->p_popup_menu = &popupmenu;
224 p_parent->PopupMenu( &popupmenu, pos.x, pos.y );
225 p_intf->p_sys->p_popup_menu = NULL;
228 wxMenu *AudioMenu( intf_thread_t *_p_intf, wxWindow *p_parent )
230 #define MAX_AUDIO_ITEMS 10
232 vlc_object_t *p_object;
233 char *ppsz_varnames[MAX_AUDIO_ITEMS];
234 int pi_objects[MAX_AUDIO_ITEMS];
237 /* Initializations */
238 memset( pi_objects, 0, MAX_AUDIO_ITEMS * sizeof(int) );
240 p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_INPUT,
242 if( p_object != NULL )
244 ppsz_varnames[i] = "audio-es";
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_AOUT,
251 if( p_object != NULL )
253 ppsz_varnames[i] = "audio-device";
254 pi_objects[i++] = p_object->i_object_id;
255 ppsz_varnames[i] = "audio-channels";
256 pi_objects[i++] = p_object->i_object_id;
257 ppsz_varnames[i] = "visual";
258 pi_objects[i++] = p_object->i_object_id;
259 vlc_object_release( p_object );
263 return new Menu( _p_intf, p_parent, i,
264 ppsz_varnames, pi_objects, AudioMenu_Events );
267 wxMenu *VideoMenu( intf_thread_t *_p_intf, wxWindow *p_parent )
269 #define MAX_VIDEO_ITEMS 15
271 vlc_object_t *p_object;
272 char *ppsz_varnames[MAX_VIDEO_ITEMS];
273 int pi_objects[MAX_VIDEO_ITEMS];
276 /* Initializations */
277 memset( pi_objects, 0, MAX_VIDEO_ITEMS * sizeof(int) );
279 p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_INPUT,
281 if( p_object != NULL )
283 ppsz_varnames[i] = "video-es";
284 pi_objects[i++] = p_object->i_object_id;
285 ppsz_varnames[i] = "spu-es";
286 pi_objects[i++] = p_object->i_object_id;
287 vlc_object_release( p_object );
290 p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_VOUT,
292 if( p_object != NULL )
294 vlc_object_t *p_dec_obj;
296 ppsz_varnames[i] = "fullscreen";
297 pi_objects[i++] = p_object->i_object_id;
298 ppsz_varnames[i] = "deinterlace";
299 pi_objects[i++] = p_object->i_object_id;
300 ppsz_varnames[i] = "aspect-ratio";
301 pi_objects[i++] = p_object->i_object_id;
302 ppsz_varnames[i] = "crop";
303 pi_objects[i++] = p_object->i_object_id;
304 ppsz_varnames[i] = "directx-on-top";
305 pi_objects[i++] = p_object->i_object_id;
306 ppsz_varnames[i] = "xvideo-on-top";
307 pi_objects[i++] = p_object->i_object_id;
308 ppsz_varnames[i] = "x11-on-top";
309 pi_objects[i++] = p_object->i_object_id;
311 p_dec_obj = (vlc_object_t *)vlc_object_find( p_object,
314 if( p_dec_obj != NULL )
316 ppsz_varnames[i] = "ffmpeg-pp-q";
317 pi_objects[i++] = p_dec_obj->i_object_id;
318 vlc_object_release( p_dec_obj );
321 vlc_object_release( p_object );
325 return new Menu( _p_intf, p_parent, i,
326 ppsz_varnames, pi_objects, VideoMenu_Events );
329 wxMenu *NavigMenu( intf_thread_t *_p_intf, wxWindow *p_parent )
331 #define MAX_NAVIG_ITEMS 10
333 vlc_object_t *p_object;
334 char *ppsz_varnames[MAX_NAVIG_ITEMS];
335 int pi_objects[MAX_NAVIG_ITEMS];
338 /* Initializations */
339 memset( pi_objects, 0, MAX_NAVIG_ITEMS * sizeof(int) );
341 p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_INPUT,
343 if( p_object != NULL )
345 ppsz_varnames[i] = "title";
346 pi_objects[i++] = p_object->i_object_id;
347 ppsz_varnames[i] = "chapter";
348 pi_objects[i++] = p_object->i_object_id;
349 ppsz_varnames[i] = "program";
350 pi_objects[i++] = p_object->i_object_id;
351 ppsz_varnames[i] = "navigation";
352 pi_objects[i++] = p_object->i_object_id;
353 ppsz_varnames[i] = "dvd_menus";
354 pi_objects[i++] = p_object->i_object_id;
356 ppsz_varnames[i] = "prev-title";
357 pi_objects[i++] = p_object->i_object_id;
358 ppsz_varnames[i] = "next-title";
359 pi_objects[i++] = p_object->i_object_id;
360 ppsz_varnames[i] = "prev-chapter";
361 pi_objects[i++] = p_object->i_object_id;
362 ppsz_varnames[i] = "next-chapter";
363 pi_objects[i++] = p_object->i_object_id;
365 vlc_object_release( p_object );
369 return new Menu( _p_intf, p_parent, i,
370 ppsz_varnames, pi_objects, NavigMenu_Events );
373 wxMenu *SettingsMenu( intf_thread_t *_p_intf, wxWindow *p_parent )
375 #define MAX_SETTINGS_ITEMS 10
377 vlc_object_t *p_object;
378 char *ppsz_varnames[MAX_SETTINGS_ITEMS];
379 int pi_objects[MAX_SETTINGS_ITEMS];
382 /* Initializations */
383 memset( pi_objects, 0, MAX_SETTINGS_ITEMS * sizeof(int) );
385 p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_INTF,
387 if( p_object != NULL )
389 ppsz_varnames[i] = "intf-switch";
390 pi_objects[i++] = p_object->i_object_id;
391 ppsz_varnames[i] = "intf-add";
392 pi_objects[i++] = p_object->i_object_id;
393 vlc_object_release( p_object );
397 return new Menu( _p_intf, p_parent, i,
398 ppsz_varnames, pi_objects, SettingsMenu_Events );
401 /*****************************************************************************
403 *****************************************************************************/
404 Menu::Menu( intf_thread_t *_p_intf, wxWindow *p_parent,
405 int i_count, char **ppsz_varnames, int *pi_objects,
406 int i_start_id ): wxMenu( )
408 vlc_object_t *p_object;
409 vlc_bool_t b_section_empty = VLC_FALSE;
412 /* Initializations */
415 i_item_id = i_start_id;
417 for( i = 0; i < i_count; i++ )
419 if( !ppsz_varnames[i] )
421 if( b_section_empty )
423 Append( MenuDummy_Event + i, wxU(_("Empty")) );
424 Enable( MenuDummy_Event + i, FALSE );
428 b_section_empty = VLC_TRUE;
434 Append( MenuDummy_Event, wxU(ppsz_varnames[i]) );
435 b_section_empty = VLC_FALSE;
439 p_object = (vlc_object_t *)vlc_object_get( p_intf, pi_objects[i] );
440 if( p_object == NULL ) continue;
442 b_section_empty = VLC_FALSE;
443 CreateMenuItem( this, ppsz_varnames[i], p_object );
444 vlc_object_release( p_object );
447 /* Special case for empty menus */
448 if( GetMenuItemCount() == 0 || b_section_empty )
450 Append( MenuDummy_Event + i, wxU(_("Empty")) );
451 Enable( MenuDummy_Event + i, FALSE );
459 /*****************************************************************************
461 *****************************************************************************/
462 void Menu::CreateMenuItem( wxMenu *menu, char *psz_var,
463 vlc_object_t *p_object )
465 wxMenuItemExt *menuitem;
466 vlc_value_t val, text;
469 /* Check the type of the object variable */
470 i_type = var_Type( p_object, psz_var );
472 switch( i_type & VLC_VAR_TYPE )
476 case VLC_VAR_VARIABLE:
478 case VLC_VAR_INTEGER:
481 /* Variable doesn't exist or isn't handled */
485 /* Make sure we want to display the variable */
486 if( i_type & VLC_VAR_HASCHOICE )
488 var_Change( p_object, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
489 if( val.i_int == 0 ) return;
490 if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE && val.i_int == 1 )
494 /* Get the descriptive name of the variable */
495 var_Change( p_object, psz_var, VLC_VAR_GETTEXT, &text, NULL );
497 var_Get( p_object, psz_var, &val );
499 if( i_type & VLC_VAR_HASCHOICE )
501 menu->Append( MenuDummy_Event,
502 wxU(text.psz_string ? text.psz_string : psz_var),
503 CreateChoicesMenu( psz_var, p_object ),
504 wxT("")/* Nothing for now (maybe use a GETLONGTEXT) */ );
506 if( text.psz_string ) free( text.psz_string );
511 switch( i_type & VLC_VAR_TYPE )
514 menuitem = new wxMenuItemExt( menu, ++i_item_id,
515 wxU(text.psz_string ?
516 text.psz_string : psz_var),
517 wxT(""), wxITEM_NORMAL, strdup(psz_var),
518 p_object->i_object_id, val, i_type );
519 menu->Append( menuitem );
523 val.b_bool = !val.b_bool;
524 menuitem = new wxMenuItemExt( menu, ++i_item_id,
525 wxU(text.psz_string ?
526 text.psz_string : psz_var),
527 wxT(""), wxITEM_CHECK, strdup(psz_var),
528 p_object->i_object_id, val, i_type );
529 menu->Append( menuitem );
530 Check( i_item_id, val.b_bool ? FALSE : TRUE );
534 if( text.psz_string ) free( text.psz_string );
538 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
539 if( text.psz_string ) free( text.psz_string );
542 wxMenu *Menu::CreateChoicesMenu( char *psz_var, vlc_object_t *p_object )
544 vlc_value_t val, val_list, text_list;
547 /* Check the type of the object variable */
548 i_type = var_Type( p_object, psz_var );
550 /* Make sure we want to display the variable */
551 if( i_type & VLC_VAR_HASCHOICE )
553 var_Change( p_object, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
554 if( val.i_int == 0 ) return NULL;
555 if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE && val.i_int == 1 )
563 switch( i_type & VLC_VAR_TYPE )
567 case VLC_VAR_VARIABLE:
569 case VLC_VAR_INTEGER:
572 /* Variable doesn't exist or isn't handled */
576 if( var_Get( p_object, psz_var, &val ) < 0 )
581 if( var_Change( p_object, psz_var, VLC_VAR_GETLIST,
582 &val_list, &text_list ) < 0 )
584 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
588 wxMenu *menu = new wxMenu;
589 for( i = 0; i < val_list.p_list->i_count; i++ )
591 vlc_value_t another_val;
592 wxMenuItemExt *menuitem;
594 switch( i_type & VLC_VAR_TYPE )
596 case VLC_VAR_VARIABLE:
597 menu->Append( MenuDummy_Event,
598 wxU(text_list.p_list->p_values[i].psz_string ?
599 text_list.p_list->p_values[i].psz_string :
600 val_list.p_list->p_values[i].psz_string),
602 val_list.p_list->p_values[i].psz_string,
603 p_object ), wxT("") );
607 another_val.psz_string =
608 strdup(val_list.p_list->p_values[i].psz_string);
610 new wxMenuItemExt( menu, ++i_item_id,
611 wxU(text_list.p_list->p_values[i].psz_string ?
612 text_list.p_list->p_values[i].psz_string :
613 another_val.psz_string), wxT(""),
614 i_type & VLC_VAR_ISCOMMAND ?
615 wxITEM_NORMAL : wxITEM_RADIO,
617 p_object->i_object_id, another_val, i_type );
619 menu->Append( menuitem );
621 if( !strcmp( val.psz_string,
622 val_list.p_list->p_values[i].psz_string ) )
623 menu->Check( i_item_id, TRUE );
626 case VLC_VAR_INTEGER:
628 new wxMenuItemExt( menu, ++i_item_id,
629 text_list.p_list->p_values[i].psz_string ?
631 text_list.p_list->p_values[i].psz_string) :
632 wxString::Format(wxT("%d"),
633 val_list.p_list->p_values[i].i_int), wxT(""),
634 i_type & VLC_VAR_ISCOMMAND ?
635 wxITEM_NORMAL : wxITEM_RADIO,
637 p_object->i_object_id,
638 val_list.p_list->p_values[i], i_type );
640 menu->Append( menuitem );
642 if( val_list.p_list->p_values[i].i_int == val.i_int )
643 menu->Check( i_item_id, TRUE );
651 /* clean up everything */
652 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
653 var_Change( p_object, psz_var, VLC_VAR_FREELIST, &val_list, &text_list );
658 void Menu::OnShowDialog( wxCommandEvent& event )
660 if( p_intf->p_sys->pf_show_dialog )
664 switch( event.GetId() )
666 case OpenFileSimple_Event:
667 i_id = INTF_DIALOG_FILE_SIMPLE;
670 i_id = INTF_DIALOG_FILE;
673 i_id = INTF_DIALOG_DISC;
676 i_id = INTF_DIALOG_NET;
679 i_id = INTF_DIALOG_FILE;
684 p_intf->p_sys->pf_show_dialog( p_intf, i_id, 1, 0 );
688 /*****************************************************************************
689 * A small helper class which intercepts all popup menu events
690 *****************************************************************************/
691 MenuEvtHandler::MenuEvtHandler( intf_thread_t *_p_intf,
692 Interface *_p_main_interface )
694 /* Initializations */
696 p_main_interface = _p_main_interface;
699 MenuEvtHandler::~MenuEvtHandler()
703 void MenuEvtHandler::OnShowDialog( wxCommandEvent& event )
705 if( p_intf->p_sys->pf_show_dialog )
709 switch( event.GetId() )
711 case OpenFileSimple_Event:
712 i_id = INTF_DIALOG_FILE_SIMPLE;
715 i_id = INTF_DIALOG_FILE;
718 i_id = INTF_DIALOG_DISC;
721 i_id = INTF_DIALOG_NET;
724 i_id = INTF_DIALOG_FILE;
729 p_intf->p_sys->pf_show_dialog( p_intf, i_id, 1, 0 );
733 void MenuEvtHandler::OnMenuEvent( wxCommandEvent& event )
735 wxMenuItem *p_menuitem = NULL;
737 /* Check if this is an auto generated menu item */
738 if( event.GetId() < FirstAutoGenerated_Event )
744 if( !p_main_interface ||
745 (p_menuitem = p_main_interface->GetMenuBar()->FindItem(event.GetId()))
748 if( p_intf->p_sys->p_popup_menu )
751 p_intf->p_sys->p_popup_menu->FindItem( event.GetId() );
757 wxMenuItemExt *p_menuitemext = (wxMenuItemExt *)p_menuitem;
758 vlc_object_t *p_object;
760 p_object = (vlc_object_t *)vlc_object_get( p_intf,
761 p_menuitemext->i_object_id );
762 if( p_object == NULL ) return;
764 var_Set( p_object, p_menuitemext->psz_var, p_menuitemext->val );
766 vlc_object_release( p_object );
772 /*****************************************************************************
773 * A small helper class which encapsulate wxMenuitem with some other useful
775 *****************************************************************************/
776 wxMenuItemExt::wxMenuItemExt( wxMenu* parentMenu, int id, const wxString& text,
777 const wxString& helpString, wxItemKind kind,
778 char *_psz_var, int _i_object_id, vlc_value_t _val, int _i_val_type ):
779 wxMenuItem( parentMenu, id, text, helpString, kind )
781 /* Initializations */
783 i_val_type = _i_val_type;
784 i_object_id = _i_object_id;
788 wxMenuItemExt::~wxMenuItemExt()
790 if( psz_var ) free( psz_var );
791 if( ((i_val_type & VLC_VAR_TYPE) == VLC_VAR_STRING)
792 && val.psz_string ) free( val.psz_string );