* modules/audio_output/alsa.c: the aout-device obj var wasn't destroyed properly...
[vlc.git] / 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.14 2003/05/26 19:06:47 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[29];
100     int pi_objects[29];
101     int i = 0;
102
103     /* Initializations */
104     memset( pi_objects, 0, 29 * 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         ppsz_varnames[i] = "deinterlace";
133         pi_objects[i++] = p_object->i_object_id;
134         ppsz_varnames[i] = "directx-on-top";
135         pi_objects[i++] = p_object->i_object_id;
136         vlc_object_release( p_object );
137     }
138
139     /* Input menu */
140     ppsz_varnames[i++] = NULL; /* Separator */
141     ppsz_varnames[i++] = _("Input menu");
142     ppsz_varnames[i++] = NULL; /* Separator */
143
144     p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_INPUT,
145                                                 FIND_ANYWHERE );
146     if( p_object != NULL )
147     {
148         ppsz_varnames[i] = "title";
149         pi_objects[i++] = p_object->i_object_id;
150         ppsz_varnames[i] = "chapter";
151         pi_objects[i++] = p_object->i_object_id;
152         ppsz_varnames[i] = "navigation";
153         pi_objects[i++] = p_object->i_object_id;
154         ppsz_varnames[i] = "program";
155         pi_objects[i++] = p_object->i_object_id;
156
157         ppsz_varnames[i] = "video-es";
158         pi_objects[i++] = p_object->i_object_id;
159         ppsz_varnames[i] = "audio-es";
160         pi_objects[i++] = p_object->i_object_id;
161         ppsz_varnames[i] = "spu-es";
162         pi_objects[i++] = p_object->i_object_id;
163
164         vlc_object_release( p_object );
165     }
166
167     /* Misc stuff */
168     ppsz_varnames[i++] = NULL; /* Separator */
169     ppsz_varnames[i++] = _("Close");
170
171     /* Build menu */
172     Menu popupmenu( _p_intf, _p_main_interface, i,
173                      ppsz_varnames, pi_objects, PopupMenu_Events );
174
175     _p_main_interface->p_popup_menu = &popupmenu;
176     _p_main_interface->PopupMenu( &popupmenu, pos.x, pos.y );
177     _p_main_interface->p_popup_menu = NULL;
178 }
179
180 wxMenu *AudioMenu( intf_thread_t *_p_intf, Interface *_p_main_interface )
181 {
182     vlc_object_t *p_object;
183     char *ppsz_varnames[5];
184     int pi_objects[5];
185     int i = 0;
186
187     /* Initializations */
188     memset( pi_objects, 0, 5 * sizeof(int) );
189
190     p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_AOUT,
191                                                 FIND_ANYWHERE );
192     if( p_object != NULL )
193     {
194         ppsz_varnames[i] = "audio-device";
195         pi_objects[i++] = p_object->i_object_id;
196         ppsz_varnames[i] = "audio-channels";
197         pi_objects[i++] = p_object->i_object_id;
198         vlc_object_release( p_object );
199     }
200
201     p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_INPUT,
202                                                 FIND_ANYWHERE );
203     if( p_object != NULL )
204     {
205         ppsz_varnames[i] = "audio-es";
206         pi_objects[i++] = p_object->i_object_id;
207         vlc_object_release( p_object );
208     }
209
210     /* Build menu */
211     return new Menu( _p_intf, _p_main_interface, i,
212                      ppsz_varnames, pi_objects, AudioMenu_Events );
213 }
214
215 wxMenu *VideoMenu( intf_thread_t *_p_intf, Interface *_p_main_interface )
216 {
217     vlc_object_t *p_object;
218     char *ppsz_varnames[6];
219     int pi_objects[6];
220     int i = 0;
221
222     /* Initializations */
223     memset( pi_objects, 0, 6 * sizeof(int) );
224
225     p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_VOUT,
226                                                 FIND_ANYWHERE );
227     if( p_object != NULL )
228     {
229         ppsz_varnames[i] = "fullscreen";
230         pi_objects[i++] = p_object->i_object_id;
231         ppsz_varnames[i] = "deinterlace";
232         pi_objects[i++] = p_object->i_object_id;
233         ppsz_varnames[i] = "directx-on-top";
234         pi_objects[i++] = p_object->i_object_id;
235         vlc_object_release( p_object );
236     }
237
238     p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_INPUT,
239                                                 FIND_ANYWHERE );
240     if( p_object != NULL )
241     {
242         ppsz_varnames[i] = "video-es";
243         pi_objects[i++] = p_object->i_object_id;
244         ppsz_varnames[i] = "spu-es";
245         pi_objects[i++] = p_object->i_object_id;
246         vlc_object_release( p_object );
247     }
248
249     /* Build menu */
250     return new Menu( _p_intf, _p_main_interface, i,
251                      ppsz_varnames, pi_objects, VideoMenu_Events );
252 }
253
254 wxMenu *NavigMenu( intf_thread_t *_p_intf, Interface *_p_main_interface )
255 {
256     vlc_object_t *p_object;
257     char *ppsz_varnames[10];
258     int pi_objects[10];
259     int i = 0;
260
261     /* Initializations */
262     memset( pi_objects, 0, 4 * sizeof(int) );
263
264     p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_INPUT,
265                                                 FIND_ANYWHERE );
266     if( p_object != NULL )
267     {
268         ppsz_varnames[i] = "title";
269         pi_objects[i++] = p_object->i_object_id;
270         ppsz_varnames[i] = "chapter";
271         pi_objects[i++] = p_object->i_object_id;
272         ppsz_varnames[i] = "navigation";
273         pi_objects[i++] = p_object->i_object_id;
274         ppsz_varnames[i] = "program";
275         pi_objects[i++] = p_object->i_object_id;
276
277         ppsz_varnames[i] = "prev-title";
278         pi_objects[i++] = p_object->i_object_id;
279         ppsz_varnames[i] = "next-title";
280         pi_objects[i++] = p_object->i_object_id;
281         ppsz_varnames[i] = "prev-chapter";
282         pi_objects[i++] = p_object->i_object_id;
283         ppsz_varnames[i] = "next-chapter";
284         pi_objects[i++] = p_object->i_object_id;
285
286         vlc_object_release( p_object );
287     }
288
289     /* Build menu */
290     return new Menu( _p_intf, _p_main_interface, i,
291                      ppsz_varnames, pi_objects, NavigMenu_Events );
292 }
293
294 /*****************************************************************************
295  * Constructor.
296  *****************************************************************************/
297 Menu::Menu( intf_thread_t *_p_intf, Interface *_p_main_interface,
298             int i_count, char **ppsz_varnames, int *pi_objects,
299             int i_start_id ): wxMenu( )
300 {
301     vlc_object_t *p_object;
302     int i;
303
304     /* Initializations */
305     p_intf = _p_intf;
306     p_main_interface = _p_main_interface;
307
308     i_item_id = i_start_id;
309
310     for( i = 0; i < i_count; i++ )
311     {
312         if( !ppsz_varnames[i] )
313         {
314             AppendSeparator();
315             continue;
316         }
317
318         if( !pi_objects[i] )
319         {
320             Append( MenuDummy_Event, wxU(ppsz_varnames[i]) );
321             continue;
322         }
323
324         p_object = (vlc_object_t *)vlc_object_get( p_intf, pi_objects[i] );
325         if( p_object == NULL ) continue;
326
327         CreateMenuItem( this, ppsz_varnames[i], p_object );
328         vlc_object_release( p_object );
329     }
330
331     /* Special case for empty menus */
332     if( GetMenuItemCount() == 0 )
333     {
334         Append( MenuDummy_Event, wxU(_("Empty")) );
335         Enable( MenuDummy_Event, FALSE );
336     }
337 }
338
339 Menu::~Menu()
340 {
341 }
342
343 /*****************************************************************************
344  * Private methods.
345  *****************************************************************************/
346 void Menu::OnEntrySelected( wxCommandEvent& WXUNUSED(event) )
347 {
348 }
349
350 void Menu::CreateMenuItem( wxMenu *menu, char *psz_var,
351                            vlc_object_t *p_object )
352 {
353     wxMenuItemExt *menuitem;
354     vlc_value_t val, text;
355     int i_type;
356
357     /* Check the type of the object variable */
358     i_type = var_Type( p_object, psz_var );
359
360     switch( i_type & VLC_VAR_TYPE )
361     {
362     case VLC_VAR_VOID:
363     case VLC_VAR_BOOL:
364     case VLC_VAR_VARIABLE:
365     case VLC_VAR_STRING:
366     case VLC_VAR_INTEGER:
367         break;
368     default:
369         /* Variable doesn't exist or isn't handled */
370         return;
371     }
372
373     /* Make sure we want to display the variable */
374     if( i_type & VLC_VAR_HASCHOICE )
375     {
376         var_Change( p_object, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
377         if( val.i_int == 0 ) return;
378         if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE && val.i_int == 1 )
379             return;
380     }
381
382     /* Get the descriptive name of the variable */
383     var_Change( p_object, psz_var, VLC_VAR_GETTEXT, &text, NULL );
384
385     var_Get( p_object, psz_var, &val );
386
387     if( i_type & VLC_VAR_HASCHOICE )
388     {
389         menu->Append( MenuDummy_Event,
390                       wxU(text.psz_string ? text.psz_string : psz_var),
391                       CreateChoicesMenu( psz_var, p_object ),
392                       wxT("")/* Nothing for now (maybe use a GETLONGTEXT) */ );
393
394         if( text.psz_string ) free( text.psz_string );
395         return;
396     }
397
398
399     switch( i_type & VLC_VAR_TYPE )
400     {
401     case VLC_VAR_VOID:
402         menuitem = new wxMenuItemExt( menu, ++i_item_id,
403                                       wxU(text.psz_string ?
404                                         text.psz_string : psz_var),
405                                       wxT(""), wxITEM_NORMAL, strdup(psz_var),
406                                       p_object->i_object_id, val, i_type );
407         menu->Append( menuitem );
408         break;
409
410     case VLC_VAR_BOOL:
411         val.b_bool = !val.b_bool;
412         menuitem = new wxMenuItemExt( menu, ++i_item_id,
413                                       wxU(text.psz_string ?
414                                         text.psz_string : psz_var),
415                                       wxT(""), wxITEM_CHECK, strdup(psz_var),
416                                       p_object->i_object_id, val, i_type );
417         menu->Append( menuitem );
418         Check( i_item_id, val.b_bool ? FALSE : TRUE );
419         break;
420
421     default:
422         if( text.psz_string ) free( text.psz_string );
423         return;
424     }
425
426     if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
427     if( text.psz_string ) free( text.psz_string );
428 }
429
430 wxMenu *Menu::CreateChoicesMenu( char *psz_var, vlc_object_t *p_object )
431 {
432     vlc_value_t val, val_list, text_list;
433     int i_type, i;
434
435     /* Check the type of the object variable */
436     i_type = var_Type( p_object, psz_var );
437
438     /* Make sure we want to display the variable */
439     if( i_type & VLC_VAR_HASCHOICE )
440     {
441         var_Change( p_object, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
442         if( val.i_int == 0 ) return NULL;
443         if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE && val.i_int == 1 )
444             return NULL;
445     }
446     else
447     {
448         return NULL;
449     }
450
451     switch( i_type & VLC_VAR_TYPE )
452     {
453     case VLC_VAR_VOID:
454     case VLC_VAR_BOOL:
455     case VLC_VAR_VARIABLE:
456     case VLC_VAR_STRING:
457     case VLC_VAR_INTEGER:
458         break;
459     default:
460         /* Variable doesn't exist or isn't handled */
461         return NULL;
462     }
463
464     if( var_Get( p_object, psz_var, &val ) < 0 )
465     {
466         return NULL;
467     }
468
469     if( var_Change( p_object, psz_var, VLC_VAR_GETLIST,
470                     &val_list, &text_list ) < 0 )
471     {
472         if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
473         return NULL;
474     }
475
476     wxMenu *menu = new wxMenu;
477     for( i = 0; i < val_list.p_list->i_count; i++ )
478     {
479         vlc_value_t another_val;
480         wxMenuItemExt *menuitem;
481
482         switch( i_type & VLC_VAR_TYPE )
483         {
484         case VLC_VAR_VARIABLE:
485           menu->Append( MenuDummy_Event,
486                         wxU(text_list.p_list->p_values[i].psz_string ?
487                         text_list.p_list->p_values[i].psz_string :
488                         val_list.p_list->p_values[i].psz_string),
489                         CreateChoicesMenu(
490                             val_list.p_list->p_values[i].psz_string,
491                             p_object ), wxT("") );
492           break;
493
494         case VLC_VAR_STRING:
495           another_val.psz_string =
496               strdup(val_list.p_list->p_values[i].psz_string);
497           menuitem =
498               new wxMenuItemExt( menu, ++i_item_id,
499                                  wxU(text_list.p_list->p_values[i].psz_string ?
500                                  text_list.p_list->p_values[i].psz_string :
501                                  another_val.psz_string),
502                                  wxT(""), wxITEM_RADIO, strdup(psz_var),
503                                  p_object->i_object_id, another_val, i_type );
504
505           menu->Append( menuitem );
506
507           if( !strcmp( val.psz_string,
508                        val_list.p_list->p_values[i].psz_string ) )
509               menu->Check( i_item_id, TRUE );
510           break;
511
512         case VLC_VAR_INTEGER:
513           menuitem =
514               new wxMenuItemExt( menu, ++i_item_id,
515                                  text_list.p_list->p_values[i].psz_string ?
516                                  wxU(text_list.p_list->p_values[i].psz_string):
517                                  wxString::Format(wxT("%d"),
518                                  val_list.p_list->p_values[i].i_int),
519                                  wxT(""), wxITEM_RADIO, strdup(psz_var),
520                                  p_object->i_object_id,
521                                  val_list.p_list->p_values[i], i_type );
522
523           menu->Append( menuitem );
524
525           if( val_list.p_list->p_values[i].i_int == val.i_int )
526               menu->Check( i_item_id, TRUE );
527           break;
528
529         default:
530           break;
531         }
532     }
533
534     /* clean up everything */
535     if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
536     var_Change( p_object, psz_var, VLC_VAR_FREELIST, &val_list, &text_list );
537
538     return menu;
539 }
540
541 /*****************************************************************************
542  * A small helper class which intercepts all popup menu events
543  *****************************************************************************/
544 MenuEvtHandler::MenuEvtHandler( intf_thread_t *_p_intf,
545                                 Interface *_p_main_interface )
546 {
547     /* Initializations */
548     p_intf = _p_intf;
549     p_main_interface = _p_main_interface;
550 }
551
552 MenuEvtHandler::~MenuEvtHandler()
553 {
554 }
555
556 void MenuEvtHandler::OnMenuEvent( wxCommandEvent& event )
557 {
558     wxMenuItem *p_menuitem;
559
560     /* Check if this is an auto generated menu item */
561     if( event.GetId() < FirstAutoGenerated_Event )
562     {
563         event.Skip();
564         return;
565     }
566
567     if( (p_menuitem = p_main_interface->GetMenuBar()->FindItem(event.GetId()))
568         == NULL )
569     {
570         if( p_main_interface->p_popup_menu )
571         {
572             p_menuitem = 
573                 p_main_interface->p_popup_menu->FindItem( event.GetId() );
574         }
575     }
576
577     if( p_menuitem )
578     {
579         wxMenuItemExt *p_menuitemext = (wxMenuItemExt *)p_menuitem;
580         vlc_object_t *p_object;
581
582         p_object = (vlc_object_t *)vlc_object_get( p_intf,
583                                        p_menuitemext->i_object_id );
584         if( p_object == NULL ) return;
585
586         var_Set( p_object, p_menuitemext->psz_var, p_menuitemext->val );
587
588         vlc_object_release( p_object );
589     }
590     else
591         event.Skip();
592 }
593
594 /*****************************************************************************
595  * A small helper class which encapsulate wxMenuitem with some other useful
596  * things.
597  *****************************************************************************/
598 wxMenuItemExt::wxMenuItemExt( wxMenu* parentMenu, int id, const wxString& text,
599     const wxString& helpString, wxItemKind kind,
600     char *_psz_var, int _i_object_id, vlc_value_t _val, int _i_val_type ):
601     wxMenuItem( parentMenu, id, text, helpString, kind )
602 {
603     /* Initializations */
604     psz_var = _psz_var;
605     i_val_type = _i_val_type;
606     i_object_id = _i_object_id;
607     val = _val;
608 };
609
610 wxMenuItemExt::~wxMenuItemExt()
611 {
612     if( psz_var ) free( psz_var );
613     if( ((i_val_type & VLC_VAR_TYPE) == VLC_VAR_STRING)
614         && val.psz_string ) free( val.psz_string );
615 };