1 /*****************************************************************************
2 * menus.cpp : wxWindows plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2001 VideoLAN
5 * $Id: menus.cpp,v 1.28 2003/11/29 16:36:56 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,
67 OpenFileSimple_Event = wxID_HIGHEST + 1100,
71 FirstAutoGenerated_Event = wxID_HIGHEST + 1999,
72 SettingsMenu_Events = wxID_HIGHEST + 5000,
73 AudioMenu_Events = wxID_HIGHEST + 2000,
74 VideoMenu_Events = wxID_HIGHEST + 3000,
75 NavigMenu_Events = wxID_HIGHEST + 4000,
76 PopupMenu_Events = wxID_HIGHEST + 6000
79 BEGIN_EVENT_TABLE(Menu, wxMenu)
82 BEGIN_EVENT_TABLE(MenuEvtHandler, wxEvtHandler)
83 EVT_MENU(OpenFileSimple_Event, MenuEvtHandler::OnShowDialog)
84 EVT_MENU(OpenFile_Event, MenuEvtHandler::OnShowDialog)
85 EVT_MENU(OpenDisc_Event, MenuEvtHandler::OnShowDialog)
86 EVT_MENU(OpenNet_Event, MenuEvtHandler::OnShowDialog)
87 EVT_MENU(-1, MenuEvtHandler::OnMenuEvent)
90 wxMenu *OpenStreamMenu( intf_thread_t *p_intf )
92 wxMenu *menu = new wxMenu;
93 menu->Append( OpenFileSimple_Event, wxU(_("Simple &Open ...")) );
94 menu->Append( OpenFile_Event, wxU(_("Open &File...")) );
95 menu->Append( OpenDisc_Event, wxU(_("Open &Disc...")) );
96 menu->Append( OpenNet_Event, wxU(_("Open &Network Stream...")) );
100 void PopupMenu( intf_thread_t *p_intf, wxWindow *p_parent,
103 #define MAX_POPUP_ITEMS 35
105 vlc_object_t *p_object;
106 char *ppsz_varnames[MAX_POPUP_ITEMS];
107 int pi_objects[MAX_POPUP_ITEMS];
110 /* Initializations */
111 memset( pi_objects, 0, MAX_POPUP_ITEMS * sizeof(int) );
114 ppsz_varnames[i++] = _("Audio menu");
115 ppsz_varnames[i++] = NULL; /* Separator */
117 p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_AOUT,
119 if( p_object != NULL )
121 ppsz_varnames[i] = "audio-device";
122 pi_objects[i++] = p_object->i_object_id;
123 ppsz_varnames[i] = "audio-channels";
124 pi_objects[i++] = p_object->i_object_id;
125 ppsz_varnames[i] = "visual";
126 pi_objects[i++] = p_object->i_object_id;
127 vlc_object_release( p_object );
131 ppsz_varnames[i++] = NULL; /* Separator */
132 ppsz_varnames[i++] = _("Video menu");
133 ppsz_varnames[i++] = NULL; /* Separator */
135 p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_VOUT,
137 if( p_object != NULL )
139 vlc_object_t *p_dec_obj;
141 ppsz_varnames[i] = "fullscreen";
142 pi_objects[i++] = p_object->i_object_id;
143 ppsz_varnames[i] = "deinterlace";
144 pi_objects[i++] = p_object->i_object_id;
145 ppsz_varnames[i] = "aspect-ratio";
146 pi_objects[i++] = p_object->i_object_id;
147 ppsz_varnames[i] = "crop";
148 pi_objects[i++] = p_object->i_object_id;
149 ppsz_varnames[i] = "directx-on-top";
150 pi_objects[i++] = p_object->i_object_id;
151 ppsz_varnames[i] = "xvideo-on-top";
152 pi_objects[i++] = p_object->i_object_id;
153 ppsz_varnames[i] = "x11-on-top";
154 pi_objects[i++] = p_object->i_object_id;
156 p_dec_obj = (vlc_object_t *)vlc_object_find( p_object,
159 if( p_dec_obj != NULL )
161 ppsz_varnames[i] = "ffmpeg-pp-q";
162 pi_objects[i++] = p_dec_obj->i_object_id;
163 vlc_object_release( p_dec_obj );
166 vlc_object_release( p_object );
170 ppsz_varnames[i++] = NULL; /* Separator */
171 ppsz_varnames[i++] = _("Input menu");
172 ppsz_varnames[i++] = NULL; /* Separator */
174 p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
176 if( p_object != NULL )
178 ppsz_varnames[i] = "title";
179 pi_objects[i++] = p_object->i_object_id;
180 ppsz_varnames[i] = "chapter";
181 pi_objects[i++] = p_object->i_object_id;
182 ppsz_varnames[i] = "program";
183 pi_objects[i++] = p_object->i_object_id;
184 ppsz_varnames[i] = "navigation";
185 pi_objects[i++] = p_object->i_object_id;
186 ppsz_varnames[i] = "dvd_menus";
187 pi_objects[i++] = p_object->i_object_id;
189 ppsz_varnames[i] = "video-es";
190 pi_objects[i++] = p_object->i_object_id;
191 ppsz_varnames[i] = "audio-es";
192 pi_objects[i++] = p_object->i_object_id;
193 ppsz_varnames[i] = "spu-es";
194 pi_objects[i++] = p_object->i_object_id;
196 vlc_object_release( p_object );
200 ppsz_varnames[i++] = NULL; /* Separator */
201 ppsz_varnames[i++] = _("Interface menu");
202 ppsz_varnames[i++] = NULL; /* Separator */
204 /* vlc_object_find is needed because of the dialogs provider case */
205 p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_INTF,
207 if( p_object != NULL )
209 ppsz_varnames[i] = "intf-switch";
210 pi_objects[i++] = p_object->i_object_id;
211 ppsz_varnames[i] = "intf-add";
212 pi_objects[i++] = p_object->i_object_id;
214 vlc_object_release( p_object );
218 Menu popupmenu( p_intf, p_parent, i,
219 ppsz_varnames, pi_objects, PopupMenu_Events );
222 /* Add static entries */
223 popupmenu.AppendSeparator();
224 popupmenu.Append( MenuDummy_Event, wxU("Open"),
225 OpenStreamMenu( p_intf ), wxT("") );
228 p_intf->p_sys->p_popup_menu = &popupmenu;
229 p_parent->PopupMenu( &popupmenu, pos.x, pos.y );
230 p_intf->p_sys->p_popup_menu = NULL;
233 wxMenu *AudioMenu( intf_thread_t *_p_intf, wxWindow *p_parent )
235 #define MAX_AUDIO_ITEMS 10
237 vlc_object_t *p_object;
238 char *ppsz_varnames[MAX_AUDIO_ITEMS];
239 int pi_objects[MAX_AUDIO_ITEMS];
242 /* Initializations */
243 memset( pi_objects, 0, MAX_AUDIO_ITEMS * sizeof(int) );
245 p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_INPUT,
247 if( p_object != NULL )
249 ppsz_varnames[i] = "audio-es";
250 pi_objects[i++] = p_object->i_object_id;
251 vlc_object_release( p_object );
254 p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_AOUT,
256 if( p_object != NULL )
258 ppsz_varnames[i] = "audio-device";
259 pi_objects[i++] = p_object->i_object_id;
260 ppsz_varnames[i] = "audio-channels";
261 pi_objects[i++] = p_object->i_object_id;
262 ppsz_varnames[i] = "visual";
263 pi_objects[i++] = p_object->i_object_id;
264 vlc_object_release( p_object );
268 return new Menu( _p_intf, p_parent, i,
269 ppsz_varnames, pi_objects, AudioMenu_Events );
272 wxMenu *VideoMenu( intf_thread_t *_p_intf, wxWindow *p_parent )
274 #define MAX_VIDEO_ITEMS 15
276 vlc_object_t *p_object;
277 char *ppsz_varnames[MAX_VIDEO_ITEMS];
278 int pi_objects[MAX_VIDEO_ITEMS];
281 /* Initializations */
282 memset( pi_objects, 0, MAX_VIDEO_ITEMS * sizeof(int) );
284 p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_INPUT,
286 if( p_object != NULL )
288 ppsz_varnames[i] = "video-es";
289 pi_objects[i++] = p_object->i_object_id;
290 ppsz_varnames[i] = "spu-es";
291 pi_objects[i++] = p_object->i_object_id;
292 vlc_object_release( p_object );
295 p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_VOUT,
297 if( p_object != NULL )
299 vlc_object_t *p_dec_obj;
301 ppsz_varnames[i] = "fullscreen";
302 pi_objects[i++] = p_object->i_object_id;
303 ppsz_varnames[i] = "deinterlace";
304 pi_objects[i++] = p_object->i_object_id;
305 ppsz_varnames[i] = "aspect-ratio";
306 pi_objects[i++] = p_object->i_object_id;
307 ppsz_varnames[i] = "crop";
308 pi_objects[i++] = p_object->i_object_id;
309 ppsz_varnames[i] = "directx-on-top";
310 pi_objects[i++] = p_object->i_object_id;
311 ppsz_varnames[i] = "xvideo-on-top";
312 pi_objects[i++] = p_object->i_object_id;
313 ppsz_varnames[i] = "x11-on-top";
314 pi_objects[i++] = p_object->i_object_id;
316 p_dec_obj = (vlc_object_t *)vlc_object_find( p_object,
319 if( p_dec_obj != NULL )
321 ppsz_varnames[i] = "ffmpeg-pp-q";
322 pi_objects[i++] = p_dec_obj->i_object_id;
323 vlc_object_release( p_dec_obj );
326 vlc_object_release( p_object );
330 return new Menu( _p_intf, p_parent, i,
331 ppsz_varnames, pi_objects, VideoMenu_Events );
334 wxMenu *NavigMenu( intf_thread_t *_p_intf, wxWindow *p_parent )
336 #define MAX_NAVIG_ITEMS 10
338 vlc_object_t *p_object;
339 char *ppsz_varnames[MAX_NAVIG_ITEMS];
340 int pi_objects[MAX_NAVIG_ITEMS];
343 /* Initializations */
344 memset( pi_objects, 0, MAX_NAVIG_ITEMS * sizeof(int) );
346 p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_INPUT,
348 if( p_object != NULL )
350 ppsz_varnames[i] = "title";
351 pi_objects[i++] = p_object->i_object_id;
352 ppsz_varnames[i] = "chapter";
353 pi_objects[i++] = p_object->i_object_id;
354 ppsz_varnames[i] = "program";
355 pi_objects[i++] = p_object->i_object_id;
356 ppsz_varnames[i] = "navigation";
357 pi_objects[i++] = p_object->i_object_id;
358 ppsz_varnames[i] = "dvd_menus";
359 pi_objects[i++] = p_object->i_object_id;
361 ppsz_varnames[i] = "prev-title";
362 pi_objects[i++] = p_object->i_object_id;
363 ppsz_varnames[i] = "next-title";
364 pi_objects[i++] = p_object->i_object_id;
365 ppsz_varnames[i] = "prev-chapter";
366 pi_objects[i++] = p_object->i_object_id;
367 ppsz_varnames[i] = "next-chapter";
368 pi_objects[i++] = p_object->i_object_id;
370 vlc_object_release( p_object );
374 return new Menu( _p_intf, p_parent, i,
375 ppsz_varnames, pi_objects, NavigMenu_Events );
378 wxMenu *SettingsMenu( intf_thread_t *_p_intf, wxWindow *p_parent )
380 #define MAX_SETTINGS_ITEMS 10
382 vlc_object_t *p_object;
383 char *ppsz_varnames[MAX_SETTINGS_ITEMS];
384 int pi_objects[MAX_SETTINGS_ITEMS];
387 /* Initializations */
388 memset( pi_objects, 0, MAX_SETTINGS_ITEMS * sizeof(int) );
390 p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_INTF,
392 if( p_object != NULL )
394 ppsz_varnames[i] = "intf-switch";
395 pi_objects[i++] = p_object->i_object_id;
396 ppsz_varnames[i] = "intf-add";
397 pi_objects[i++] = p_object->i_object_id;
398 vlc_object_release( p_object );
402 return new Menu( _p_intf, p_parent, i,
403 ppsz_varnames, pi_objects, SettingsMenu_Events );
406 /*****************************************************************************
408 *****************************************************************************/
409 Menu::Menu( intf_thread_t *_p_intf, wxWindow *p_parent,
410 int i_count, char **ppsz_varnames, int *pi_objects,
411 int i_start_id ): wxMenu( )
413 vlc_object_t *p_object;
414 vlc_bool_t b_section_empty = VLC_FALSE;
417 /* Initializations */
420 i_item_id = i_start_id;
422 for( i = 0; i < i_count; i++ )
424 if( !ppsz_varnames[i] )
426 if( b_section_empty )
428 Append( MenuDummy_Event + i, wxU(_("Empty")) );
429 Enable( MenuDummy_Event + i, FALSE );
433 b_section_empty = VLC_TRUE;
439 Append( MenuDummy_Event, wxU(ppsz_varnames[i]) );
440 b_section_empty = VLC_FALSE;
444 p_object = (vlc_object_t *)vlc_object_get( p_intf, pi_objects[i] );
445 if( p_object == NULL ) continue;
447 b_section_empty = VLC_FALSE;
448 CreateMenuItem( this, ppsz_varnames[i], p_object );
449 vlc_object_release( p_object );
452 /* Special case for empty menus */
453 if( GetMenuItemCount() == 0 || b_section_empty )
455 Append( MenuDummy_Event + i, wxU(_("Empty")) );
456 Enable( MenuDummy_Event + i, FALSE );
464 /*****************************************************************************
466 *****************************************************************************/
467 void Menu::CreateMenuItem( wxMenu *menu, char *psz_var,
468 vlc_object_t *p_object )
470 wxMenuItemExt *menuitem;
471 vlc_value_t val, text;
474 /* Check the type of the object variable */
475 i_type = var_Type( p_object, psz_var );
477 switch( i_type & VLC_VAR_TYPE )
481 case VLC_VAR_VARIABLE:
483 case VLC_VAR_INTEGER:
486 /* Variable doesn't exist or isn't handled */
490 /* Make sure we want to display the variable */
491 if( i_type & VLC_VAR_HASCHOICE )
493 var_Change( p_object, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
494 if( val.i_int == 0 ) return;
495 if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE && val.i_int == 1 )
499 /* Get the descriptive name of the variable */
500 var_Change( p_object, psz_var, VLC_VAR_GETTEXT, &text, NULL );
502 var_Get( p_object, psz_var, &val );
504 if( i_type & VLC_VAR_HASCHOICE )
506 menu->Append( MenuDummy_Event,
507 wxU(text.psz_string ? text.psz_string : psz_var),
508 CreateChoicesMenu( psz_var, p_object ),
509 wxT("")/* Nothing for now (maybe use a GETLONGTEXT) */ );
511 if( text.psz_string ) free( text.psz_string );
516 switch( i_type & VLC_VAR_TYPE )
519 menuitem = new wxMenuItemExt( menu, ++i_item_id,
520 wxU(text.psz_string ?
521 text.psz_string : psz_var),
522 wxT(""), wxITEM_NORMAL, strdup(psz_var),
523 p_object->i_object_id, val, i_type );
524 menu->Append( menuitem );
528 val.b_bool = !val.b_bool;
529 menuitem = new wxMenuItemExt( menu, ++i_item_id,
530 wxU(text.psz_string ?
531 text.psz_string : psz_var),
532 wxT(""), wxITEM_CHECK, strdup(psz_var),
533 p_object->i_object_id, val, i_type );
534 menu->Append( menuitem );
535 Check( i_item_id, val.b_bool ? FALSE : TRUE );
539 if( text.psz_string ) free( text.psz_string );
543 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
544 if( text.psz_string ) free( text.psz_string );
547 wxMenu *Menu::CreateChoicesMenu( char *psz_var, vlc_object_t *p_object )
549 vlc_value_t val, val_list, text_list;
552 /* Check the type of the object variable */
553 i_type = var_Type( p_object, psz_var );
555 /* Make sure we want to display the variable */
556 if( i_type & VLC_VAR_HASCHOICE )
558 var_Change( p_object, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
559 if( val.i_int == 0 ) return NULL;
560 if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE && val.i_int == 1 )
568 switch( i_type & VLC_VAR_TYPE )
572 case VLC_VAR_VARIABLE:
574 case VLC_VAR_INTEGER:
577 /* Variable doesn't exist or isn't handled */
581 if( var_Get( p_object, psz_var, &val ) < 0 )
586 if( var_Change( p_object, psz_var, VLC_VAR_GETLIST,
587 &val_list, &text_list ) < 0 )
589 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
593 wxMenu *menu = new wxMenu;
594 for( i = 0; i < val_list.p_list->i_count; i++ )
596 vlc_value_t another_val;
597 wxMenuItemExt *menuitem;
599 switch( i_type & VLC_VAR_TYPE )
601 case VLC_VAR_VARIABLE:
602 menu->Append( MenuDummy_Event,
603 wxU(text_list.p_list->p_values[i].psz_string ?
604 text_list.p_list->p_values[i].psz_string :
605 val_list.p_list->p_values[i].psz_string),
607 val_list.p_list->p_values[i].psz_string,
608 p_object ), wxT("") );
612 another_val.psz_string =
613 strdup(val_list.p_list->p_values[i].psz_string);
615 new wxMenuItemExt( menu, ++i_item_id,
616 wxU(text_list.p_list->p_values[i].psz_string ?
617 text_list.p_list->p_values[i].psz_string :
618 another_val.psz_string), wxT(""),
619 i_type & VLC_VAR_ISCOMMAND ?
620 wxITEM_NORMAL : wxITEM_RADIO,
622 p_object->i_object_id, another_val, i_type );
624 menu->Append( menuitem );
626 if( !strcmp( val.psz_string,
627 val_list.p_list->p_values[i].psz_string ) )
628 menu->Check( i_item_id, TRUE );
631 case VLC_VAR_INTEGER:
633 new wxMenuItemExt( menu, ++i_item_id,
634 text_list.p_list->p_values[i].psz_string ?
636 text_list.p_list->p_values[i].psz_string) :
637 wxString::Format(wxT("%d"),
638 val_list.p_list->p_values[i].i_int), wxT(""),
639 i_type & VLC_VAR_ISCOMMAND ?
640 wxITEM_NORMAL : wxITEM_RADIO,
642 p_object->i_object_id,
643 val_list.p_list->p_values[i], i_type );
645 menu->Append( menuitem );
647 if( val_list.p_list->p_values[i].i_int == val.i_int )
648 menu->Check( i_item_id, TRUE );
656 /* clean up everything */
657 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
658 var_Change( p_object, psz_var, VLC_VAR_FREELIST, &val_list, &text_list );
663 void Menu::OnShowDialog( wxCommandEvent& event )
665 if( p_intf->p_sys->pf_show_dialog )
669 switch( event.GetId() )
671 case OpenFileSimple_Event:
672 i_id = INTF_DIALOG_FILE_SIMPLE;
675 i_id = INTF_DIALOG_FILE;
678 i_id = INTF_DIALOG_DISC;
681 i_id = INTF_DIALOG_NET;
684 i_id = INTF_DIALOG_FILE;
689 p_intf->p_sys->pf_show_dialog( p_intf, i_id, 1, 0 );
693 /*****************************************************************************
694 * A small helper class which intercepts all popup menu events
695 *****************************************************************************/
696 MenuEvtHandler::MenuEvtHandler( intf_thread_t *_p_intf,
697 Interface *_p_main_interface )
699 /* Initializations */
701 p_main_interface = _p_main_interface;
704 MenuEvtHandler::~MenuEvtHandler()
708 void MenuEvtHandler::OnShowDialog( wxCommandEvent& event )
710 if( p_intf->p_sys->pf_show_dialog )
714 switch( event.GetId() )
716 case OpenFileSimple_Event:
717 i_id = INTF_DIALOG_FILE_SIMPLE;
720 i_id = INTF_DIALOG_FILE;
723 i_id = INTF_DIALOG_DISC;
726 i_id = INTF_DIALOG_NET;
729 i_id = INTF_DIALOG_FILE;
734 p_intf->p_sys->pf_show_dialog( p_intf, i_id, 1, 0 );
738 void MenuEvtHandler::OnMenuEvent( wxCommandEvent& event )
740 wxMenuItem *p_menuitem = NULL;
742 /* Check if this is an auto generated menu item */
743 if( event.GetId() < FirstAutoGenerated_Event )
749 if( !p_main_interface ||
750 (p_menuitem = p_main_interface->GetMenuBar()->FindItem(event.GetId()))
753 if( p_intf->p_sys->p_popup_menu )
756 p_intf->p_sys->p_popup_menu->FindItem( event.GetId() );
762 wxMenuItemExt *p_menuitemext = (wxMenuItemExt *)p_menuitem;
763 vlc_object_t *p_object;
765 p_object = (vlc_object_t *)vlc_object_get( p_intf,
766 p_menuitemext->i_object_id );
767 if( p_object == NULL ) return;
769 var_Set( p_object, p_menuitemext->psz_var, p_menuitemext->val );
771 vlc_object_release( p_object );
777 /*****************************************************************************
778 * A small helper class which encapsulate wxMenuitem with some other useful
780 *****************************************************************************/
781 wxMenuItemExt::wxMenuItemExt( wxMenu* parentMenu, int id, const wxString& text,
782 const wxString& helpString, wxItemKind kind,
783 char *_psz_var, int _i_object_id, vlc_value_t _val, int _i_val_type ):
784 wxMenuItem( parentMenu, id, text, helpString, kind )
786 /* Initializations */
788 i_val_type = _i_val_type;
789 i_object_id = _i_object_id;
793 wxMenuItemExt::~wxMenuItemExt()
795 if( psz_var ) free( psz_var );
796 if( ((i_val_type & VLC_VAR_TYPE) == VLC_VAR_STRING)
797 && val.psz_string ) free( val.psz_string );