]> git.sesse.net Git - vlc/blob - modules/gui/wxwindows/menus.cpp
* ALL: changed the prototype of input_AddES() to include enough information so we...
[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.2 2003/05/05 22:23:40 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     MenuLast_Event,
81 };
82
83 BEGIN_EVENT_TABLE(Menu, wxMenu)
84     /* Menu events */
85     EVT_MENU(MenuDummy_Event, Menu::OnEntrySelected)
86 END_EVENT_TABLE()
87
88 BEGIN_EVENT_TABLE(MenuEvtHandler, wxEvtHandler)
89     EVT_MENU(-1, MenuEvtHandler::OnMenuEvent)
90 END_EVENT_TABLE()
91
92 void PopupMenu( intf_thread_t *_p_intf, Interface *_p_main_interface )
93 {
94     vlc_object_t *p_object;
95     char *ppsz_varnames[19];
96     int pi_objects[19];
97     int i = 0;
98
99     /* Initializations */
100     memset( pi_objects, 0, 19 * sizeof(int) );
101
102     /* Audio menu */
103     ppsz_varnames[i++] = _("Audio menu");
104     ppsz_varnames[i++] = NULL; /* Separator */
105
106     p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_AOUT,
107                                                 FIND_ANYWHERE );
108     if( p_object != NULL )
109     {
110         ppsz_varnames[i] = "audio-device";
111         pi_objects[i++] = p_object->i_object_id;
112         ppsz_varnames[i] = "audio-channels";
113         pi_objects[i++] = p_object->i_object_id;
114         vlc_object_release( p_object );
115     }
116
117     /* Video menu */
118     ppsz_varnames[i++] = NULL; /* Separator */
119     ppsz_varnames[i++] = _("Video menu");
120     ppsz_varnames[i++] = NULL; /* Separator */
121
122     p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_VOUT,
123                                                 FIND_ANYWHERE );
124     if( p_object != NULL )
125     {
126         ppsz_varnames[i] = "fullscreen";
127         pi_objects[i++] = p_object->i_object_id;
128         vlc_object_release( p_object );
129     }
130
131     /* Input menu */
132     ppsz_varnames[i++] = NULL; /* Separator */
133     ppsz_varnames[i++] = _("Input menu");
134     ppsz_varnames[i++] = NULL; /* Separator */
135
136     p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_INPUT,
137                                                 FIND_ANYWHERE );
138     if( p_object != NULL )
139     {
140         ppsz_varnames[i] = "title";
141         pi_objects[i++] = p_object->i_object_id;
142         ppsz_varnames[i] = "chapter";
143         pi_objects[i++] = p_object->i_object_id;
144         ppsz_varnames[i] = "navigation";
145         pi_objects[i++] = p_object->i_object_id;
146
147         ppsz_varnames[i] = "video-es";
148         pi_objects[i++] = p_object->i_object_id;
149         ppsz_varnames[i] = "audio-es";
150         pi_objects[i++] = p_object->i_object_id;
151         ppsz_varnames[i] = "spu-es";
152         pi_objects[i++] = p_object->i_object_id;
153
154         vlc_object_release( p_object );
155     }
156
157     /* Misc stuff */
158     ppsz_varnames[i++] = NULL; /* Separator */
159     ppsz_varnames[i++] = _("Close");
160
161     /* Build menu */
162     wxMenu *popupmenu = new Menu( _p_intf, _p_main_interface, i,
163                                   ppsz_varnames, pi_objects );
164
165     _p_main_interface->p_popup_menu = popupmenu;
166     wxPoint mousepos = wxGetMousePosition();
167     _p_main_interface->PopupMenu( popupmenu,
168                                  _p_main_interface->ScreenToClient(mousepos).x,
169                                  _p_main_interface->ScreenToClient(mousepos).y
170                                  );
171 }
172
173 wxMenu *AudioMenu( intf_thread_t *_p_intf, Interface *_p_main_interface )
174 {
175     vlc_object_t *p_object;
176     char *ppsz_varnames[5];
177     int pi_objects[5];
178     int i = 0;
179
180     /* Initializations */
181     memset( pi_objects, 0, 5 * sizeof(int) );
182
183     /* Audio menu */
184     ppsz_varnames[i++] = NULL; /* Separator */
185
186     /* Audio menu */
187     p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_AOUT,
188                                                 FIND_ANYWHERE );
189     if( p_object != NULL )
190     {
191         ppsz_varnames[i] = "audio-device";
192         pi_objects[i++] = p_object->i_object_id;
193         ppsz_varnames[i] = "audio-channels";
194         pi_objects[i++] = p_object->i_object_id;
195         vlc_object_release( p_object );
196     }
197
198     p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_INPUT,
199                                                 FIND_ANYWHERE );
200     if( p_object != NULL )
201     {
202         ppsz_varnames[i] = "audio-es";
203         pi_objects[i++] = p_object->i_object_id;
204         vlc_object_release( p_object );
205     }
206
207     /* Build menu */
208     return new Menu( _p_intf, _p_main_interface, i,
209                      ppsz_varnames, pi_objects );
210 }
211
212 wxMenu *VideoMenu( intf_thread_t *_p_intf, Interface *_p_main_interface )
213 {
214     vlc_object_t *p_object;
215     char *ppsz_varnames[4];
216     int pi_objects[4];
217     int i = 0;
218
219     /* Initializations */
220     memset( pi_objects, 0, 4 * sizeof(int) );
221
222     ppsz_varnames[i++] = NULL; /* Separator */
223
224     p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_VOUT,
225                                                 FIND_ANYWHERE );
226     if( p_object != NULL )
227     {
228         ppsz_varnames[i] = "fullscreen";
229         pi_objects[i++] = p_object->i_object_id;
230         vlc_object_release( p_object );
231     }
232
233     p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_INPUT,
234                                                 FIND_ANYWHERE );
235     if( p_object != NULL )
236     {
237         ppsz_varnames[i] = "video-es";
238         pi_objects[i++] = p_object->i_object_id;
239         ppsz_varnames[i] = "spu-es";
240         pi_objects[i++] = p_object->i_object_id;
241         vlc_object_release( p_object );
242     }
243
244     /* Build menu */
245     return new Menu( _p_intf, _p_main_interface, i,
246                      ppsz_varnames, pi_objects );
247 }
248
249 /*****************************************************************************
250  * Constructor.
251  *****************************************************************************/
252 Menu::Menu( intf_thread_t *_p_intf, Interface *_p_main_interface,
253             int i_count, char **ppsz_varnames, int *pi_objects ):
254     wxMenu( )
255 {
256     vlc_object_t *p_object;
257     int i;
258
259     /* Initializations */
260     p_intf = _p_intf;
261     p_main_interface = _p_main_interface;
262
263     i_item_id = MenuLast_Event;
264
265     for( i = 0; i < i_count; i++ )
266     {
267         if( !ppsz_varnames[i] )
268         {
269             AppendSeparator();
270             continue;
271         }
272
273         if( !pi_objects[i] )
274         {
275             Append( MenuDummy_Event, ppsz_varnames[i] );
276             continue;
277         }
278
279         p_object = (vlc_object_t *)vlc_object_get( p_intf, pi_objects[i] );
280         if( p_object == NULL ) continue;
281
282         CreateMenuItem( this, ppsz_varnames[i], p_object );
283         vlc_object_release( p_object );
284     }
285
286 }
287
288 Menu::~Menu()
289 {
290 }
291
292 /*****************************************************************************
293  * Private methods.
294  *****************************************************************************/
295 void Menu::OnEntrySelected( wxCommandEvent& WXUNUSED(event) )
296 {
297 }
298
299 void Menu::CreateMenuItem( wxMenu *menu, char *psz_var,
300                            vlc_object_t *p_object )
301 {
302     wxMenuItemExt *menuitem;
303     vlc_value_t val, text;
304     int i_type;
305
306     /* Check the type of the object variable */
307     i_type = var_Type( p_object, psz_var );
308
309     switch( i_type & VLC_VAR_TYPE )
310     {
311     case VLC_VAR_VOID:
312     case VLC_VAR_BOOL:
313     case VLC_VAR_VARIABLE:
314     case VLC_VAR_STRING:
315     case VLC_VAR_INTEGER:
316         break;
317     default:
318         /* Variable doesn't exist or isn't handled */
319         return;
320     }
321
322     /* Make sure we want to display the variable */
323     if( i_type & VLC_VAR_HASCHOICE )
324     {
325         var_Change( p_object, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
326         if( val.i_int == 0 ) return;
327     }
328
329     /* Get the descriptive name of the variable */
330     var_Change( p_object, psz_var, VLC_VAR_GETTEXT, &text, NULL );
331
332     if( i_type & VLC_VAR_HASCHOICE )
333     {
334         menu->Append( MenuDummy_Event,
335                       text.psz_string ? text.psz_string : psz_var,
336                       CreateChoicesMenu( psz_var, p_object ),
337                       "" /* Nothing for now (maybe use a GETLONGTEXT) */ );
338
339         if( text.psz_string ) free( text.psz_string );
340         return;
341     }
342
343
344     switch( i_type & VLC_VAR_TYPE )
345     {
346     case VLC_VAR_VOID:
347         menuitem = new wxMenuItemExt( menu, ++i_item_id,
348                                       text.psz_string ?
349                                         text.psz_string : psz_var,
350                                       "", wxITEM_NORMAL, strdup(psz_var),
351                                       p_object->i_object_id, val, i_type );
352         menu->Append( menuitem );
353         break;
354
355     case VLC_VAR_BOOL:
356         menuitem = new wxMenuItemExt( menu, ++i_item_id,
357                                       text.psz_string ?
358                                         text.psz_string : psz_var,
359                                       "", wxITEM_CHECK, strdup(psz_var),
360                                       p_object->i_object_id, val, i_type );
361         menu->Append( menuitem );
362         Check( i_item_id -1, val.b_bool ? FALSE : TRUE );
363         break;
364
365     default:
366         if( text.psz_string ) free( text.psz_string );
367         return;
368     }
369
370     if( text.psz_string ) free( text.psz_string );
371 }
372
373 wxMenu *Menu::CreateChoicesMenu( char *psz_var, vlc_object_t *p_object )
374 {
375     vlc_value_t val, val_list, text_list;
376     int i_type, i;
377
378     /* Check the type of the object variable */
379     i_type = var_Type( p_object, psz_var );
380
381     /* Make sure we want to display the variable */
382     if( i_type & VLC_VAR_HASCHOICE )
383     {
384         var_Change( p_object, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
385         if( val.i_int == 0 ) return NULL;
386     }
387     else
388     {
389         return NULL;
390     }
391
392     switch( i_type & VLC_VAR_TYPE )
393     {
394     case VLC_VAR_VOID:
395     case VLC_VAR_BOOL:
396     case VLC_VAR_VARIABLE:
397     case VLC_VAR_STRING:
398     case VLC_VAR_INTEGER:
399         break;
400     default:
401         /* Variable doesn't exist or isn't handled */
402         return NULL;
403     }
404
405     if( var_Get( p_object, psz_var, &val ) < 0 )
406     {
407         return NULL;
408     }
409
410     if( var_Change( p_object, psz_var, VLC_VAR_GETLIST,
411                     &val_list, &text_list ) < 0 )
412     {
413         if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
414         return NULL;
415     }
416
417     wxMenu *menu = new wxMenu;
418     for( i = 0; i < val_list.p_list->i_count; i++ )
419     {
420         vlc_value_t another_val;
421         wxMenuItemExt *menuitem;
422
423         switch( i_type & VLC_VAR_TYPE )
424         {
425         case VLC_VAR_VARIABLE:
426           menu->Append( MenuDummy_Event,
427                         text_list.p_list->p_values[i].psz_string ?
428                         text_list.p_list->p_values[i].psz_string :
429                         val_list.p_list->p_values[i].psz_string,
430                         CreateChoicesMenu(
431                             val_list.p_list->p_values[i].psz_string,
432                             p_object ), "" );
433           break;
434
435         case VLC_VAR_STRING:
436           another_val.psz_string =
437               strdup(val_list.p_list->p_values[i].psz_string);
438           menuitem =
439               new wxMenuItemExt( this, ++i_item_id,
440                                  text_list.p_list->p_values[i].psz_string ?
441                                  text_list.p_list->p_values[i].psz_string :
442                                  another_val.psz_string,
443                                  "", wxITEM_RADIO, strdup(psz_var),
444                                  p_object->i_object_id, another_val, i_type );
445
446           menu->Append( menuitem );
447
448           if( !strcmp( val.psz_string,
449                        val_list.p_list->p_values[i].psz_string ) )
450               menu->Check( i_item_id, TRUE );
451           break;
452
453         case VLC_VAR_INTEGER:
454           menuitem =
455               new wxMenuItemExt( this, ++i_item_id,
456                                  text_list.p_list->p_values[i].psz_string ?
457                                  text_list.p_list->p_values[i].psz_string :
458                                  wxString::Format("%d",
459                                  val_list.p_list->p_values[i].i_int),
460                                  "", wxITEM_RADIO, strdup(psz_var),
461                                  p_object->i_object_id,
462                                  val_list.p_list->p_values[i], i_type );
463
464           menu->Append( menuitem );
465
466           if( !((i_type & VLC_VAR_FLAGS) & VLC_VAR_ISCOMMAND) &&
467               val_list.p_list->p_values[i].i_int == val.i_int )
468               menu->Check( i_item_id, TRUE );
469           break;
470
471         default:
472           break;
473         }
474
475     }
476
477     /* clean up everything */
478     if( i_type == VLC_VAR_STRING ) free( val.psz_string );
479     var_Change( p_object, psz_var, VLC_VAR_FREELIST, &val_list, &text_list );
480
481     return menu;
482 }
483
484 /*****************************************************************************
485  * A small helper class which intercepts all popup menu events
486  *****************************************************************************/
487 MenuEvtHandler::MenuEvtHandler( intf_thread_t *_p_intf,
488                                 Interface *_p_main_interface )
489 {
490     /* Initializations */
491     p_intf = _p_intf;
492     p_main_interface = _p_main_interface;
493 }
494
495 MenuEvtHandler::~MenuEvtHandler()
496 {
497 }
498
499 void MenuEvtHandler::OnMenuEvent( wxCommandEvent& event )
500 {
501     wxMenuItem *p_menuitem;
502
503     /* Check if this is an auto generated menu item */
504     if( event.GetId() < FirstAutoGenerated_Event )
505     {
506         event.Skip();
507         return;
508     }
509
510     if( (p_menuitem = p_main_interface->GetMenuBar()->FindItem(event.GetId()))
511         == NULL )
512     {
513         if( p_main_interface->p_popup_menu )
514         {
515             p_menuitem = 
516                 p_main_interface->p_popup_menu->FindItem( event.GetId() );
517         }
518     }
519
520     if( p_menuitem )
521     {
522         wxMenuItemExt *p_menuitemext = (wxMenuItemExt *)p_menuitem;
523         vlc_object_t *p_object;
524
525         p_object = (vlc_object_t *)vlc_object_get( p_intf,
526                                        p_menuitemext->i_object_id );
527         if( p_object == NULL ) return;
528
529         var_Set( p_object, p_menuitemext->psz_var, p_menuitemext->val );
530
531         vlc_object_release( p_object );
532     }
533     else
534         event.Skip();
535 }
536
537 /*****************************************************************************
538  * A small helper class which encapsulate wxMenuitem with some other useful
539  * things.
540  *****************************************************************************/
541 wxMenuItemExt::wxMenuItemExt( wxMenu* parentMenu, int id, const wxString& text,
542     const wxString& helpString, wxItemKind kind,
543     char *_psz_var, int _i_object_id, vlc_value_t _val, int _i_val_type ):
544     wxMenuItem( parentMenu, id, text, helpString, kind )
545 {
546     /* Initializations */
547     psz_var = _psz_var;
548     i_val_type = _i_val_type;
549     i_object_id = _i_object_id;
550     val = _val;
551 };
552
553 wxMenuItemExt::~wxMenuItemExt()
554 {
555     if( psz_var ) free( psz_var );
556     if( ((i_val_type & VLC_VAR_TYPE) == VLC_VAR_STRING)
557         && val.psz_string ) free( val.psz_string );
558 };