]> git.sesse.net Git - vlc/blob - modules/gui/wxwindows/menus.cpp
* modules/gui/wxwindows/*: small fixes and improvements.
[vlc] / modules / gui / wxwindows / menus.cpp
1 /*****************************************************************************
2  * menus.cpp : wxWindows plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2001 VideoLAN
5  * $Id: menus.cpp,v 1.9 2003/05/15 15:59:35 gbazin Exp $
6  *
7  * Authors: Gildas Bazin <gbazin@netcourrier.com>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <errno.h>                                                 /* ENOMEM */
29 #include <string.h>                                            /* strerror() */
30 #include <stdio.h>
31
32 #include <vlc/vlc.h>
33
34 #ifdef WIN32                                                 /* mingw32 hack */
35 #undef Yield
36 #undef CreateDialog
37 #endif
38
39 /* Let vlc take care of the i18n stuff */
40 #define WXINTL_NO_GETTEXT_MACRO
41
42 #include <wx/wxprec.h>
43 #include <wx/wx.h>
44 #include <wx/listctrl.h>
45
46 #include <vlc/intf.h>
47
48 #include "wxwindows.h"
49
50 class wxMenuItemExt: public wxMenuItem
51 {
52 public:
53     /* Constructor */
54     wxMenuItemExt( wxMenu* parentMenu, int id, const wxString& text,
55                    const wxString& helpString, wxItemKind kind,
56                    char *_psz_var, int _i_object_id, vlc_value_t _val,
57                    int _i_val_type );
58
59     virtual ~wxMenuItemExt();
60
61     char *psz_var;
62     int  i_val_type;
63     int  i_object_id;
64     vlc_value_t val;
65
66 private:
67
68 };
69
70 /*****************************************************************************
71  * Event Table.
72  *****************************************************************************/
73
74 /* IDs for the controls and the menu commands */
75 enum
76 {
77     /* menu items */
78     FirstAutoGenerated_Event = wxID_HIGHEST + 1000,
79     MenuDummy_Event,
80     AudioMenu_Events,
81     VideoMenu_Events = wxID_HIGHEST + 1100,
82     NavigMenu_Events = wxID_HIGHEST + 1200,
83     PopupMenu_Events = wxID_HIGHEST + 1300,
84 };
85
86 BEGIN_EVENT_TABLE(Menu, wxMenu)
87     /* Menu events */
88     EVT_MENU(MenuDummy_Event, Menu::OnEntrySelected)
89 END_EVENT_TABLE()
90
91 BEGIN_EVENT_TABLE(MenuEvtHandler, wxEvtHandler)
92     EVT_MENU(-1, MenuEvtHandler::OnMenuEvent)
93 END_EVENT_TABLE()
94
95 void PopupMenu( intf_thread_t *_p_intf, Interface *_p_main_interface,
96                 const wxPoint& pos )
97 {
98     vlc_object_t *p_object;
99     char *ppsz_varnames[19];
100     int pi_objects[19];
101     int i = 0;
102
103     /* Initializations */
104     memset( pi_objects, 0, 19 * sizeof(int) );
105
106     /* Audio menu */
107     ppsz_varnames[i++] = _("Audio menu");
108     ppsz_varnames[i++] = NULL; /* Separator */
109
110     p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_AOUT,
111                                                 FIND_ANYWHERE );
112     if( p_object != NULL )
113     {
114         ppsz_varnames[i] = "audio-device";
115         pi_objects[i++] = p_object->i_object_id;
116         ppsz_varnames[i] = "audio-channels";
117         pi_objects[i++] = p_object->i_object_id;
118         vlc_object_release( p_object );
119     }
120
121     /* Video menu */
122     ppsz_varnames[i++] = NULL; /* Separator */
123     ppsz_varnames[i++] = _("Video menu");
124     ppsz_varnames[i++] = NULL; /* Separator */
125
126     p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_VOUT,
127                                                 FIND_ANYWHERE );
128     if( p_object != NULL )
129     {
130         ppsz_varnames[i] = "fullscreen";
131         pi_objects[i++] = p_object->i_object_id;
132         vlc_object_release( p_object );
133     }
134
135     /* Input menu */
136     ppsz_varnames[i++] = NULL; /* Separator */
137     ppsz_varnames[i++] = _("Input menu");
138     ppsz_varnames[i++] = NULL; /* Separator */
139
140     p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_INPUT,
141                                                 FIND_ANYWHERE );
142     if( p_object != NULL )
143     {
144         ppsz_varnames[i] = "title";
145         pi_objects[i++] = p_object->i_object_id;
146         ppsz_varnames[i] = "chapter";
147         pi_objects[i++] = p_object->i_object_id;
148         ppsz_varnames[i] = "navigation";
149         pi_objects[i++] = p_object->i_object_id;
150         ppsz_varnames[i] = "program";
151         pi_objects[i++] = p_object->i_object_id;
152
153         ppsz_varnames[i] = "video-es";
154         pi_objects[i++] = p_object->i_object_id;
155         ppsz_varnames[i] = "audio-es";
156         pi_objects[i++] = p_object->i_object_id;
157         ppsz_varnames[i] = "spu-es";
158         pi_objects[i++] = p_object->i_object_id;
159
160         vlc_object_release( p_object );
161     }
162
163     /* Misc stuff */
164     ppsz_varnames[i++] = NULL; /* Separator */
165     ppsz_varnames[i++] = _("Close");
166
167     /* Build menu */
168     Menu popupmenu( _p_intf, _p_main_interface, i,
169                      ppsz_varnames, pi_objects, PopupMenu_Events );
170
171     _p_main_interface->p_popup_menu = &popupmenu;
172     _p_main_interface->PopupMenu( &popupmenu, pos.x, pos.y );
173 }
174
175 wxMenu *AudioMenu( intf_thread_t *_p_intf, Interface *_p_main_interface )
176 {
177     vlc_object_t *p_object;
178     char *ppsz_varnames[5];
179     int pi_objects[5];
180     int i = 0;
181
182     /* Initializations */
183     memset( pi_objects, 0, 5 * sizeof(int) );
184
185     p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_AOUT,
186                                                 FIND_ANYWHERE );
187     if( p_object != NULL )
188     {
189         ppsz_varnames[i] = "audio-device";
190         pi_objects[i++] = p_object->i_object_id;
191         ppsz_varnames[i] = "audio-channels";
192         pi_objects[i++] = p_object->i_object_id;
193         vlc_object_release( p_object );
194     }
195
196     p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_INPUT,
197                                                 FIND_ANYWHERE );
198     if( p_object != NULL )
199     {
200         ppsz_varnames[i] = "audio-es";
201         pi_objects[i++] = p_object->i_object_id;
202         vlc_object_release( p_object );
203     }
204
205     /* Build menu */
206     return new Menu( _p_intf, _p_main_interface, i,
207                      ppsz_varnames, pi_objects, AudioMenu_Events );
208 }
209
210 wxMenu *VideoMenu( intf_thread_t *_p_intf, Interface *_p_main_interface )
211 {
212     vlc_object_t *p_object;
213     char *ppsz_varnames[4];
214     int pi_objects[4];
215     int i = 0;
216
217     /* Initializations */
218     memset( pi_objects, 0, 4 * sizeof(int) );
219
220     p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_VOUT,
221                                                 FIND_ANYWHERE );
222     if( p_object != NULL )
223     {
224         ppsz_varnames[i] = "fullscreen";
225         pi_objects[i++] = p_object->i_object_id;
226         vlc_object_release( p_object );
227     }
228
229     p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_INPUT,
230                                                 FIND_ANYWHERE );
231     if( p_object != NULL )
232     {
233         ppsz_varnames[i] = "video-es";
234         pi_objects[i++] = p_object->i_object_id;
235         ppsz_varnames[i] = "spu-es";
236         pi_objects[i++] = p_object->i_object_id;
237         vlc_object_release( p_object );
238     }
239
240     /* Build menu */
241     return new Menu( _p_intf, _p_main_interface, i,
242                      ppsz_varnames, pi_objects, VideoMenu_Events );
243 }
244
245 wxMenu *NavigMenu( intf_thread_t *_p_intf, Interface *_p_main_interface )
246 {
247     vlc_object_t *p_object;
248     char *ppsz_varnames[6];
249     int pi_objects[6];
250     int i = 0;
251
252     /* Initializations */
253     memset( pi_objects, 0, 4 * sizeof(int) );
254
255     p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_INPUT,
256                                                 FIND_ANYWHERE );
257     if( p_object != NULL )
258     {
259         ppsz_varnames[i] = "title";
260         pi_objects[i++] = p_object->i_object_id;
261         ppsz_varnames[i] = "chapter";
262         pi_objects[i++] = p_object->i_object_id;
263         ppsz_varnames[i] = "navigation";
264         pi_objects[i++] = p_object->i_object_id;
265         vlc_object_release( p_object );
266         ppsz_varnames[i] = "program";
267         pi_objects[i++] = p_object->i_object_id;
268     }
269
270     /* Build menu */
271     return new Menu( _p_intf, _p_main_interface, i,
272                      ppsz_varnames, pi_objects, NavigMenu_Events );
273 }
274
275 /*****************************************************************************
276  * Constructor.
277  *****************************************************************************/
278 Menu::Menu( intf_thread_t *_p_intf, Interface *_p_main_interface,
279             int i_count, char **ppsz_varnames, int *pi_objects,
280             int i_start_id ): wxMenu( )
281 {
282     vlc_object_t *p_object;
283     int i;
284
285     /* Initializations */
286     p_intf = _p_intf;
287     p_main_interface = _p_main_interface;
288
289     i_item_id = i_start_id;
290
291     for( i = 0; i < i_count; i++ )
292     {
293         if( !ppsz_varnames[i] )
294         {
295             AppendSeparator();
296             continue;
297         }
298
299         if( !pi_objects[i] )
300         {
301             Append( MenuDummy_Event, wxU(ppsz_varnames[i]) );
302             continue;
303         }
304
305         p_object = (vlc_object_t *)vlc_object_get( p_intf, pi_objects[i] );
306         if( p_object == NULL ) continue;
307
308         CreateMenuItem( this, ppsz_varnames[i], p_object );
309         vlc_object_release( p_object );
310     }
311
312     /* Special case for empty menus */
313     if( GetMenuItemCount() == 0 )
314     {
315         Append( MenuDummy_Event, wxU(_("Empty")) );
316         Enable( MenuDummy_Event, FALSE );
317     }
318 }
319
320 Menu::~Menu()
321 {
322 }
323
324 /*****************************************************************************
325  * Private methods.
326  *****************************************************************************/
327 void Menu::OnEntrySelected( wxCommandEvent& WXUNUSED(event) )
328 {
329 }
330
331 void Menu::CreateMenuItem( wxMenu *menu, char *psz_var,
332                            vlc_object_t *p_object )
333 {
334     wxMenuItemExt *menuitem;
335     vlc_value_t val, text;
336     int i_type;
337
338     /* Check the type of the object variable */
339     i_type = var_Type( p_object, psz_var );
340
341     switch( i_type & VLC_VAR_TYPE )
342     {
343     case VLC_VAR_VOID:
344     case VLC_VAR_BOOL:
345     case VLC_VAR_VARIABLE:
346     case VLC_VAR_STRING:
347     case VLC_VAR_INTEGER:
348         break;
349     default:
350         /* Variable doesn't exist or isn't handled */
351         return;
352     }
353
354     /* Make sure we want to display the variable */
355     if( i_type & VLC_VAR_HASCHOICE )
356     {
357         var_Change( p_object, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
358         if( val.i_int == 0 ) return;
359     }
360
361     /* Get the descriptive name of the variable */
362     var_Change( p_object, psz_var, VLC_VAR_GETTEXT, &text, NULL );
363
364     if( i_type & VLC_VAR_HASCHOICE )
365     {
366         menu->Append( MenuDummy_Event,
367                       wxU(text.psz_string ? text.psz_string : psz_var),
368                       CreateChoicesMenu( psz_var, p_object ),
369                       wxT("")/* Nothing for now (maybe use a GETLONGTEXT) */ );
370
371         if( text.psz_string ) free( text.psz_string );
372         return;
373     }
374
375
376     switch( i_type & VLC_VAR_TYPE )
377     {
378     case VLC_VAR_VOID:
379         menuitem = new wxMenuItemExt( menu, ++i_item_id,
380                                       wxU(text.psz_string ?
381                                         text.psz_string : psz_var),
382                                       wxT(""), wxITEM_NORMAL, strdup(psz_var),
383                                       p_object->i_object_id, val, i_type );
384         menu->Append( menuitem );
385         break;
386
387     case VLC_VAR_BOOL:
388         menuitem = new wxMenuItemExt( menu, ++i_item_id,
389                                       wxU(text.psz_string ?
390                                         text.psz_string : psz_var),
391                                       wxT(""), wxITEM_CHECK, strdup(psz_var),
392                                       p_object->i_object_id, val, i_type );
393         menu->Append( menuitem );
394         Check( i_item_id -1, val.b_bool ? FALSE : TRUE );
395         break;
396
397     default:
398         if( text.psz_string ) free( text.psz_string );
399         return;
400     }
401
402     if( text.psz_string ) free( text.psz_string );
403 }
404
405 wxMenu *Menu::CreateChoicesMenu( char *psz_var, vlc_object_t *p_object )
406 {
407     vlc_value_t val, val_list, text_list;
408     int i_type, i;
409
410     /* Check the type of the object variable */
411     i_type = var_Type( p_object, psz_var );
412
413     /* Make sure we want to display the variable */
414     if( i_type & VLC_VAR_HASCHOICE )
415     {
416         var_Change( p_object, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
417         if( val.i_int == 0 ) return NULL;
418     }
419     else
420     {
421         return NULL;
422     }
423
424     switch( i_type & VLC_VAR_TYPE )
425     {
426     case VLC_VAR_VOID:
427     case VLC_VAR_BOOL:
428     case VLC_VAR_VARIABLE:
429     case VLC_VAR_STRING:
430     case VLC_VAR_INTEGER:
431         break;
432     default:
433         /* Variable doesn't exist or isn't handled */
434         return NULL;
435     }
436
437     if( var_Get( p_object, psz_var, &val ) < 0 )
438     {
439         return NULL;
440     }
441
442     if( var_Change( p_object, psz_var, VLC_VAR_GETLIST,
443                     &val_list, &text_list ) < 0 )
444     {
445         if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
446         return NULL;
447     }
448
449     wxMenu *menu = new wxMenu;
450     for( i = 0; i < val_list.p_list->i_count; i++ )
451     {
452         vlc_value_t another_val;
453         wxMenuItemExt *menuitem;
454
455         switch( i_type & VLC_VAR_TYPE )
456         {
457         case VLC_VAR_VARIABLE:
458           menu->Append( MenuDummy_Event,
459                         wxU(text_list.p_list->p_values[i].psz_string ?
460                         text_list.p_list->p_values[i].psz_string :
461                         val_list.p_list->p_values[i].psz_string),
462                         CreateChoicesMenu(
463                             val_list.p_list->p_values[i].psz_string,
464                             p_object ), wxT("") );
465           break;
466
467         case VLC_VAR_STRING:
468           another_val.psz_string =
469               strdup(val_list.p_list->p_values[i].psz_string);
470           menuitem =
471               new wxMenuItemExt( menu, ++i_item_id,
472                                  wxU(text_list.p_list->p_values[i].psz_string ?
473                                  text_list.p_list->p_values[i].psz_string :
474                                  another_val.psz_string),
475                                  wxT(""), wxITEM_RADIO, strdup(psz_var),
476                                  p_object->i_object_id, another_val, i_type );
477
478           menu->Append( menuitem );
479
480           if( !strcmp( val.psz_string,
481                        val_list.p_list->p_values[i].psz_string ) )
482               menu->Check( i_item_id, TRUE );
483           break;
484
485         case VLC_VAR_INTEGER:
486           menuitem =
487               new wxMenuItemExt( menu, ++i_item_id,
488                                  text_list.p_list->p_values[i].psz_string ?
489                                  wxU(text_list.p_list->p_values[i].psz_string):
490                                  wxString::Format(wxT("%d"),
491                                  val_list.p_list->p_values[i].i_int),
492                                  wxT(""), wxITEM_RADIO, strdup(psz_var),
493                                  p_object->i_object_id,
494                                  val_list.p_list->p_values[i], i_type );
495
496           menu->Append( menuitem );
497
498           if( val_list.p_list->p_values[i].i_int == val.i_int )
499               menu->Check( i_item_id, TRUE );
500           break;
501
502         default:
503           break;
504         }
505     }
506
507     /* clean up everything */
508     if( i_type == VLC_VAR_STRING ) free( val.psz_string );
509     var_Change( p_object, psz_var, VLC_VAR_FREELIST, &val_list, &text_list );
510
511     return menu;
512 }
513
514 /*****************************************************************************
515  * A small helper class which intercepts all popup menu events
516  *****************************************************************************/
517 MenuEvtHandler::MenuEvtHandler( intf_thread_t *_p_intf,
518                                 Interface *_p_main_interface )
519 {
520     /* Initializations */
521     p_intf = _p_intf;
522     p_main_interface = _p_main_interface;
523 }
524
525 MenuEvtHandler::~MenuEvtHandler()
526 {
527 }
528
529 void MenuEvtHandler::OnMenuEvent( wxCommandEvent& event )
530 {
531     wxMenuItem *p_menuitem;
532
533     /* Check if this is an auto generated menu item */
534     if( event.GetId() < FirstAutoGenerated_Event )
535     {
536         event.Skip();
537         return;
538     }
539
540     if( (p_menuitem = p_main_interface->GetMenuBar()->FindItem(event.GetId()))
541         == NULL )
542     {
543         if( p_main_interface->p_popup_menu )
544         {
545             p_menuitem = 
546                 p_main_interface->p_popup_menu->FindItem( event.GetId() );
547         }
548     }
549
550     if( p_menuitem )
551     {
552         wxMenuItemExt *p_menuitemext = (wxMenuItemExt *)p_menuitem;
553         vlc_object_t *p_object;
554
555         p_object = (vlc_object_t *)vlc_object_get( p_intf,
556                                        p_menuitemext->i_object_id );
557         if( p_object == NULL ) return;
558
559         var_Set( p_object, p_menuitemext->psz_var, p_menuitemext->val );
560
561         vlc_object_release( p_object );
562     }
563     else
564         event.Skip();
565 }
566
567 /*****************************************************************************
568  * A small helper class which encapsulate wxMenuitem with some other useful
569  * things.
570  *****************************************************************************/
571 wxMenuItemExt::wxMenuItemExt( wxMenu* parentMenu, int id, const wxString& text,
572     const wxString& helpString, wxItemKind kind,
573     char *_psz_var, int _i_object_id, vlc_value_t _val, int _i_val_type ):
574     wxMenuItem( parentMenu, id, text, helpString, kind )
575 {
576     /* Initializations */
577     psz_var = _psz_var;
578     i_val_type = _i_val_type;
579     i_object_id = _i_object_id;
580     val = _val;
581 };
582
583 wxMenuItemExt::~wxMenuItemExt()
584 {
585     if( psz_var ) free( psz_var );
586     if( ((i_val_type & VLC_VAR_TYPE) == VLC_VAR_STRING)
587         && val.psz_string ) free( val.psz_string );
588 };