9d711b4cca22a318a0380fd43649a7fdfac1637b
[vlc.git] / modules / gui / wxwindows / menus.cpp
1 /*****************************************************************************
2  * menus.cpp : wxWindows plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
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 #include <vlc/intf.h>
34
35 #include "wxwindows.h"
36
37 class wxMenuItemExt: public wxMenuItem
38 {
39 public:
40     /* Constructor */
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,
44                    int _i_val_type );
45
46     virtual ~wxMenuItemExt();
47
48     char *psz_var;
49     int  i_val_type;
50     int  i_object_id;
51     vlc_value_t val;
52
53 private:
54
55 };
56
57 class Menu: public wxMenu
58 {
59 public:
60     /* Constructor */
61     Menu( intf_thread_t *p_intf, int i_start_id );
62     virtual ~Menu();
63
64     void Populate( int i_count, char **ppsz_names, int *pi_objects );
65     void Clear();
66
67 private:
68     wxMenu *Menu::CreateDummyMenu();
69     void   Menu::CreateMenuItem( wxMenu *, char *, vlc_object_t * );
70     wxMenu *Menu::CreateChoicesMenu( char *, vlc_object_t *, bool );
71
72     DECLARE_EVENT_TABLE();
73
74     intf_thread_t *p_intf;
75
76     int i_start_id;
77     int i_item_id;
78 };
79
80 /*****************************************************************************
81  * Event Table.
82  *****************************************************************************/
83
84 /* IDs for the controls and the menu commands */
85 enum
86 {
87     /* menu items */
88     MenuDummy_Event = wxID_HIGHEST + 1000,
89     OpenFileSimple_Event = wxID_HIGHEST + 1100,
90     OpenFile_Event,
91     OpenDisc_Event,
92     OpenNet_Event,
93     OpenCapture_Event,
94     Preferences_Event,
95     FirstAutoGenerated_Event = wxID_HIGHEST + 1999,
96     SettingsMenu_Events = wxID_HIGHEST + 5000,
97     AudioMenu_Events = wxID_HIGHEST + 2000,
98     VideoMenu_Events = wxID_HIGHEST + 3000,
99     NavigMenu_Events = wxID_HIGHEST + 4000,
100     PopupMenu_Events = wxID_HIGHEST + 6000,
101     Hotkeys_Events = wxID_HIGHEST + 7000
102 };
103
104 BEGIN_EVENT_TABLE(Menu, wxMenu)
105 END_EVENT_TABLE()
106
107 BEGIN_EVENT_TABLE(MenuEvtHandler, wxEvtHandler)
108     EVT_MENU(OpenFileSimple_Event, MenuEvtHandler::OnShowDialog)
109     EVT_MENU(OpenFile_Event, MenuEvtHandler::OnShowDialog)
110     EVT_MENU(OpenDisc_Event, MenuEvtHandler::OnShowDialog)
111     EVT_MENU(OpenNet_Event, MenuEvtHandler::OnShowDialog)
112     EVT_MENU(OpenCapture_Event, MenuEvtHandler::OnShowDialog)
113     EVT_MENU(Preferences_Event, MenuEvtHandler::OnShowDialog)
114     EVT_MENU(-1, MenuEvtHandler::OnMenuEvent)
115 END_EVENT_TABLE()
116
117 wxMenu *OpenStreamMenu( intf_thread_t *p_intf )
118 {
119     wxMenu *menu = new wxMenu;
120     menu->Append( OpenFileSimple_Event, wxU(_("Quick &Open File...")) );
121     menu->Append( OpenFile_Event, wxU(_("Open &File...")) );
122     menu->Append( OpenDisc_Event, wxU(_("Open &Disc...")) );
123     menu->Append( OpenNet_Event, wxU(_("Open &Network Stream...")) );
124     menu->Append( OpenCapture_Event, wxU(_("Open &Capture Device...")) );
125     return menu;
126 }
127
128 void PopupMenu( intf_thread_t *p_intf, wxWindow *p_parent,
129                 const wxPoint& pos )
130 {
131 #define MAX_POPUP_ITEMS 35
132
133     vlc_object_t *p_object;
134     char *ppsz_varnames[MAX_POPUP_ITEMS];
135     int pi_objects[MAX_POPUP_ITEMS];
136     int i = 0;
137
138     /* Initializations */
139     memset( pi_objects, 0, MAX_POPUP_ITEMS * sizeof(int) );
140
141     /* Audio menu */
142     ppsz_varnames[i++] = _("Audio menu");
143     ppsz_varnames[i++] = NULL; /* Separator */
144
145     p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_AOUT,
146                                                 FIND_ANYWHERE );
147     if( p_object != NULL )
148     {
149         ppsz_varnames[i] = "audio-device";
150         pi_objects[i++] = p_object->i_object_id;
151         ppsz_varnames[i] = "audio-channels";
152         pi_objects[i++] = p_object->i_object_id;
153         ppsz_varnames[i] = "visual";
154         pi_objects[i++] = p_object->i_object_id;
155         vlc_object_release( p_object );
156     }
157
158     /* Video menu */
159     ppsz_varnames[i++] = NULL; /* Separator */
160     ppsz_varnames[i++] = _("Video menu");
161     ppsz_varnames[i++] = NULL; /* Separator */
162
163     p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_VOUT,
164                                                 FIND_ANYWHERE );
165     if( p_object != NULL )
166     {
167         vlc_object_t *p_dec_obj;
168
169         ppsz_varnames[i] = "fullscreen";
170         pi_objects[i++] = p_object->i_object_id;
171         ppsz_varnames[i] = "zoom";
172         pi_objects[i++] = p_object->i_object_id;
173         ppsz_varnames[i] = "deinterlace";
174         pi_objects[i++] = p_object->i_object_id;
175         ppsz_varnames[i] = "aspect-ratio";
176         pi_objects[i++] = p_object->i_object_id;
177         ppsz_varnames[i] = "crop";
178         pi_objects[i++] = p_object->i_object_id;
179         ppsz_varnames[i] = "video-on-top";
180         pi_objects[i++] = p_object->i_object_id;
181         ppsz_varnames[i] = "directx-wallpaper";
182         pi_objects[i++] = p_object->i_object_id;
183
184         p_dec_obj = (vlc_object_t *)vlc_object_find( p_object,
185                                                      VLC_OBJECT_DECODER,
186                                                      FIND_PARENT );
187         if( p_dec_obj != NULL )
188         {
189             ppsz_varnames[i] = "ffmpeg-pp-q";
190             pi_objects[i++] = p_dec_obj->i_object_id;
191             vlc_object_release( p_dec_obj );
192         }
193
194         vlc_object_release( p_object );
195     }
196
197     /* Input menu */
198     ppsz_varnames[i++] = NULL; /* Separator */
199     ppsz_varnames[i++] = _("Input menu");
200     ppsz_varnames[i++] = NULL; /* Separator */
201
202     p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
203                                                 FIND_ANYWHERE );
204     if( p_object != NULL )
205     {
206         ppsz_varnames[i] = "bookmark";
207         pi_objects[i++] = p_object->i_object_id;
208         ppsz_varnames[i] = "title";
209         pi_objects[i++] = p_object->i_object_id;
210         ppsz_varnames[i] = "chapter";
211         pi_objects[i++] = p_object->i_object_id;
212         ppsz_varnames[i] = "program";
213         pi_objects[i++] = p_object->i_object_id;
214         ppsz_varnames[i] = "navigation";
215         pi_objects[i++] = p_object->i_object_id;
216         ppsz_varnames[i] = "dvd_menus";
217         pi_objects[i++] = p_object->i_object_id;
218
219         ppsz_varnames[i] = "video-es";
220         pi_objects[i++] = p_object->i_object_id;
221         ppsz_varnames[i] = "audio-es";
222         pi_objects[i++] = p_object->i_object_id;
223         ppsz_varnames[i] = "spu-es";
224         pi_objects[i++] = p_object->i_object_id;
225
226         vlc_object_release( p_object );
227     }
228
229     /* Interface menu */
230     ppsz_varnames[i++] = NULL; /* Separator */
231     ppsz_varnames[i++] = _("Interface menu");
232     ppsz_varnames[i++] = NULL; /* Separator */
233
234     /* vlc_object_find is needed because of the dialogs provider case */
235     p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_INTF,
236                                                 FIND_PARENT );
237     if( p_object != NULL )
238     {
239         ppsz_varnames[i] = "intf-switch";
240         pi_objects[i++] = p_object->i_object_id;
241         ppsz_varnames[i] = "intf-add";
242         pi_objects[i++] = p_object->i_object_id;
243
244         vlc_object_release( p_object );
245     }
246
247     /* Build menu */
248     Menu popupmenu( p_intf, PopupMenu_Events );
249     popupmenu.Populate( i, ppsz_varnames, pi_objects );
250
251     /* Add static entries */
252     popupmenu.AppendSeparator();
253     popupmenu.Append( MenuDummy_Event, wxU("Open..."),
254                       OpenStreamMenu( p_intf ), wxT("") );
255     popupmenu.Append( Preferences_Event, wxU(_("&Preferences...")) );
256
257     p_intf->p_sys->p_popup_menu = &popupmenu;
258     p_parent->PopupMenu( &popupmenu, pos.x, pos.y );
259     p_intf->p_sys->p_popup_menu = NULL;
260 }
261
262 wxMenu *AudioMenu( intf_thread_t *_p_intf, wxWindow *p_parent, wxMenu *p_menu )
263 {
264 #define MAX_AUDIO_ITEMS 10
265
266     vlc_object_t *p_object;
267     char *ppsz_varnames[MAX_AUDIO_ITEMS];
268     int pi_objects[MAX_AUDIO_ITEMS];
269     int i = 0;
270
271     /* Initializations */
272     memset( pi_objects, 0, MAX_AUDIO_ITEMS * sizeof(int) );
273
274     p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_INPUT,
275                                                 FIND_ANYWHERE );
276     if( p_object != NULL )
277     {
278         ppsz_varnames[i] = "audio-es";
279         pi_objects[i++] = p_object->i_object_id;
280         vlc_object_release( p_object );
281     }
282
283     p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_AOUT,
284                                                 FIND_ANYWHERE );
285     if( p_object != NULL )
286     {
287         ppsz_varnames[i] = "audio-device";
288         pi_objects[i++] = p_object->i_object_id;
289         ppsz_varnames[i] = "audio-channels";
290         pi_objects[i++] = p_object->i_object_id;
291         ppsz_varnames[i] = "visual";
292         pi_objects[i++] = p_object->i_object_id;
293         vlc_object_release( p_object );
294     }
295
296     /* Build menu */
297     Menu *p_vlc_menu = (Menu *)p_menu;
298     if( !p_vlc_menu )
299         p_vlc_menu = new Menu( _p_intf, AudioMenu_Events );
300     else
301         p_vlc_menu->Clear();
302
303     p_vlc_menu->Populate( i, ppsz_varnames, pi_objects );
304
305     return p_vlc_menu;
306 }
307
308 wxMenu *VideoMenu( intf_thread_t *_p_intf, wxWindow *p_parent, wxMenu *p_menu )
309 {
310 #define MAX_VIDEO_ITEMS 15
311
312     vlc_object_t *p_object;
313     char *ppsz_varnames[MAX_VIDEO_ITEMS];
314     int pi_objects[MAX_VIDEO_ITEMS];
315     int i = 0;
316
317     /* Initializations */
318     memset( pi_objects, 0, MAX_VIDEO_ITEMS * sizeof(int) );
319
320     p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_INPUT,
321                                                 FIND_ANYWHERE );
322     if( p_object != NULL )
323     {
324         ppsz_varnames[i] = "video-es";
325         pi_objects[i++] = p_object->i_object_id;
326         ppsz_varnames[i] = "spu-es";
327         pi_objects[i++] = p_object->i_object_id;
328         vlc_object_release( p_object );
329     }
330
331     p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_VOUT,
332                                                 FIND_ANYWHERE );
333     if( p_object != NULL )
334     {
335         vlc_object_t *p_dec_obj;
336
337         ppsz_varnames[i] = "fullscreen";
338         pi_objects[i++] = p_object->i_object_id;
339         ppsz_varnames[i] = "zoom";
340         pi_objects[i++] = p_object->i_object_id;
341         ppsz_varnames[i] = "deinterlace";
342         pi_objects[i++] = p_object->i_object_id;
343         ppsz_varnames[i] = "aspect-ratio";
344         pi_objects[i++] = p_object->i_object_id;
345         ppsz_varnames[i] = "crop";
346         pi_objects[i++] = p_object->i_object_id;
347         ppsz_varnames[i] = "video-on-top";
348         pi_objects[i++] = p_object->i_object_id;
349         ppsz_varnames[i] = "directx-wallpaper";
350         pi_objects[i++] = p_object->i_object_id;
351
352         p_dec_obj = (vlc_object_t *)vlc_object_find( p_object,
353                                                      VLC_OBJECT_DECODER,
354                                                      FIND_PARENT );
355         if( p_dec_obj != NULL )
356         {
357             ppsz_varnames[i] = "ffmpeg-pp-q";
358             pi_objects[i++] = p_dec_obj->i_object_id;
359             vlc_object_release( p_dec_obj );
360         }
361
362         vlc_object_release( p_object );
363     }
364
365     /* Build menu */
366     Menu *p_vlc_menu = (Menu *)p_menu;
367     if( !p_vlc_menu )
368         p_vlc_menu = new Menu( _p_intf, VideoMenu_Events );
369     else
370         p_vlc_menu->Clear();
371
372     p_vlc_menu->Populate( i, ppsz_varnames, pi_objects );
373
374     return p_vlc_menu;
375 }
376
377 wxMenu *NavigMenu( intf_thread_t *_p_intf, wxWindow *p_parent, wxMenu *p_menu )
378 {
379 #define MAX_NAVIG_ITEMS 10
380
381     vlc_object_t *p_object;
382     char *ppsz_varnames[MAX_NAVIG_ITEMS];
383     int pi_objects[MAX_NAVIG_ITEMS];
384     int i = 0;
385
386     /* Initializations */
387     memset( pi_objects, 0, MAX_NAVIG_ITEMS * sizeof(int) );
388
389     p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_INPUT,
390                                                 FIND_ANYWHERE );
391     if( p_object != NULL )
392     {
393         ppsz_varnames[i] = "bookmark";
394         pi_objects[i++] = p_object->i_object_id;
395         ppsz_varnames[i] = "title";
396         pi_objects[i++] = p_object->i_object_id;
397         ppsz_varnames[i] = "chapter";
398         pi_objects[i++] = p_object->i_object_id;
399         ppsz_varnames[i] = "program";
400         pi_objects[i++] = p_object->i_object_id;
401         ppsz_varnames[i] = "navigation";
402         pi_objects[i++] = p_object->i_object_id;
403         ppsz_varnames[i] = "dvd_menus";
404         pi_objects[i++] = p_object->i_object_id;
405
406         ppsz_varnames[i] = "prev-title";
407         pi_objects[i++] = p_object->i_object_id;
408         ppsz_varnames[i] = "next-title";
409         pi_objects[i++] = p_object->i_object_id;
410         ppsz_varnames[i] = "prev-chapter";
411         pi_objects[i++] = p_object->i_object_id;
412         ppsz_varnames[i] = "next-chapter";
413         pi_objects[i++] = p_object->i_object_id;
414
415         vlc_object_release( p_object );
416     }
417
418     /* Build menu */
419     Menu *p_vlc_menu = (Menu *)p_menu;
420     if( !p_vlc_menu )
421         p_vlc_menu = new Menu( _p_intf, NavigMenu_Events );
422     else
423         p_vlc_menu->Clear();
424
425     p_vlc_menu->Populate( i, ppsz_varnames, pi_objects );
426
427     return p_vlc_menu;
428 }
429
430 wxMenu *SettingsMenu( intf_thread_t *_p_intf, wxWindow *p_parent,
431                       wxMenu *p_menu )
432 {
433 #define MAX_SETTINGS_ITEMS 10
434
435     vlc_object_t *p_object;
436     char *ppsz_varnames[MAX_SETTINGS_ITEMS];
437     int pi_objects[MAX_SETTINGS_ITEMS];
438     int i = 0;
439
440     /* Initializations */
441     memset( pi_objects, 0, MAX_SETTINGS_ITEMS * sizeof(int) );
442
443     p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_INTF,
444                                                 FIND_PARENT );
445     if( p_object != NULL )
446     {
447         ppsz_varnames[i] = "intf-switch";
448         pi_objects[i++] = p_object->i_object_id;
449         ppsz_varnames[i] = "intf-add";
450         pi_objects[i++] = p_object->i_object_id;
451         vlc_object_release( p_object );
452     }
453
454     /* Build menu */
455     Menu *p_vlc_menu = (Menu *)p_menu;
456     if( !p_vlc_menu )
457         p_vlc_menu = new Menu( _p_intf, SettingsMenu_Events );
458     else
459         p_vlc_menu->Clear();
460
461     p_vlc_menu->Populate( i, ppsz_varnames, pi_objects );
462
463     return p_vlc_menu;
464 }
465
466 /*****************************************************************************
467  * Constructor.
468  *****************************************************************************/
469 Menu::Menu( intf_thread_t *_p_intf, int _i_start_id ) : wxMenu( )
470 {
471     /* Initializations */
472     p_intf = _p_intf;
473     i_start_id = _i_start_id;
474 }
475
476 Menu::~Menu()
477 {
478 }
479
480 /*****************************************************************************
481  * Public methods.
482  *****************************************************************************/
483 void Menu::Populate( int i_count, char **ppsz_varnames, int *pi_objects )
484 {
485     vlc_object_t *p_object;
486     vlc_bool_t b_section_empty = VLC_FALSE;
487     int i;
488
489     i_item_id = i_start_id;
490
491     for( i = 0; i < i_count; i++ )
492     {
493         if( !ppsz_varnames[i] )
494         {
495             if( b_section_empty )
496             {
497                 Append( MenuDummy_Event + i, wxU(_("Empty")) );
498                 Enable( MenuDummy_Event + i, FALSE );
499             }
500
501             AppendSeparator();
502             b_section_empty = VLC_TRUE;
503             continue;
504         }
505
506         if( !pi_objects[i] )
507         {
508             Append( MenuDummy_Event, wxU(ppsz_varnames[i]) );
509             b_section_empty = VLC_FALSE;
510             continue;
511         }
512
513         p_object = (vlc_object_t *)vlc_object_get( p_intf, pi_objects[i] );
514         if( p_object == NULL ) continue;
515
516         b_section_empty = VLC_FALSE;
517         CreateMenuItem( this, ppsz_varnames[i], p_object );
518         vlc_object_release( p_object );
519     }
520
521     /* Special case for empty menus */
522     if( GetMenuItemCount() == 0 || b_section_empty )
523     {
524         Append( MenuDummy_Event + i, wxU(_("Empty")) );
525         Enable( MenuDummy_Event + i, FALSE );
526     }
527 }
528
529 /* Work-around helper for buggy wxGTK */
530 static void RecursiveDestroy( wxMenu *menu )
531 {
532     wxMenuItemList::Node *node = menu->GetMenuItems().GetFirst();
533     for( ; node; )
534     {
535         wxMenuItem *item = node->GetData();
536         node = node->GetNext();
537
538         /* Delete the submenus */
539         wxMenu *submenu = item->GetSubMenu();
540         if( submenu )
541         {
542             RecursiveDestroy( submenu );
543         }
544         menu->Delete( item );
545     }
546 }
547
548 void Menu::Clear( )
549 {
550     RecursiveDestroy( this );
551 }
552
553 /*****************************************************************************
554  * Private methods.
555  *****************************************************************************/
556 static bool IsMenuEmpty( char *psz_var, vlc_object_t *p_object,
557                          bool b_root = TRUE )
558 {
559     vlc_value_t val, val_list;
560     int i_type, i_result, i;
561
562     /* Check the type of the object variable */
563     i_type = var_Type( p_object, psz_var );
564
565     /* Check if we want to display the variable */
566     if( !(i_type & VLC_VAR_HASCHOICE) ) return FALSE;
567
568     var_Change( p_object, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
569     if( val.i_int == 0 ) return TRUE;
570
571     if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE )
572     {
573         if( val.i_int == 1 && b_root ) return TRUE;
574         else return FALSE;
575     }
576
577     /* Check children variables in case of VLC_VAR_VARIABLE */
578     if( var_Change( p_object, psz_var, VLC_VAR_GETLIST, &val_list, NULL ) < 0 )
579     {
580         return TRUE;
581     }
582
583     for( i = 0, i_result = TRUE; i < val_list.p_list->i_count; i++ )
584     {
585         if( !IsMenuEmpty( val_list.p_list->p_values[i].psz_string,
586                           p_object, FALSE ) )
587         {
588             i_result = FALSE;
589             break;
590         }
591     }
592
593     /* clean up everything */
594     var_Change( p_object, psz_var, VLC_VAR_FREELIST, &val_list, NULL );
595
596     return i_result;
597 }
598
599 void Menu::CreateMenuItem( wxMenu *menu, char *psz_var,
600                            vlc_object_t *p_object )
601 {
602     wxMenuItemExt *menuitem;
603     vlc_value_t val, text;
604     int i_type;
605
606     /* Check the type of the object variable */
607     i_type = var_Type( p_object, psz_var );
608
609     switch( i_type & VLC_VAR_TYPE )
610     {
611     case VLC_VAR_VOID:
612     case VLC_VAR_BOOL:
613     case VLC_VAR_VARIABLE:
614     case VLC_VAR_STRING:
615     case VLC_VAR_INTEGER:
616     case VLC_VAR_FLOAT:
617         break;
618     default:
619         /* Variable doesn't exist or isn't handled */
620         return;
621     }
622
623     /* Make sure we want to display the variable */
624     if( IsMenuEmpty( psz_var, p_object ) ) return;
625
626     /* Get the descriptive name of the variable */
627     var_Change( p_object, psz_var, VLC_VAR_GETTEXT, &text, NULL );
628
629     if( i_type & VLC_VAR_HASCHOICE )
630     {
631         menu->Append( MenuDummy_Event,
632                       wxU(text.psz_string ? text.psz_string : psz_var),
633                       CreateChoicesMenu( psz_var, p_object, TRUE ),
634                       wxT("")/* Nothing for now (maybe use a GETLONGTEXT) */ );
635
636         if( text.psz_string ) free( text.psz_string );
637         return;
638     }
639
640
641     switch( i_type & VLC_VAR_TYPE )
642     {
643     case VLC_VAR_VOID:
644         var_Get( p_object, psz_var, &val );
645         menuitem = new wxMenuItemExt( menu, ++i_item_id,
646                                       wxU(text.psz_string ?
647                                         text.psz_string : psz_var),
648                                       wxT(""), wxITEM_NORMAL, strdup(psz_var),
649                                       p_object->i_object_id, val, i_type );
650         menu->Append( menuitem );
651         break;
652
653     case VLC_VAR_BOOL:
654         var_Get( p_object, psz_var, &val );
655         val.b_bool = !val.b_bool;
656         menuitem = new wxMenuItemExt( menu, ++i_item_id,
657                                       wxU(text.psz_string ?
658                                         text.psz_string : psz_var),
659                                       wxT(""), wxITEM_CHECK, strdup(psz_var),
660                                       p_object->i_object_id, val, i_type );
661         menu->Append( menuitem );
662         Check( i_item_id, val.b_bool ? FALSE : TRUE );
663         break;
664     }
665
666     if( text.psz_string ) free( text.psz_string );
667 }
668
669 wxMenu *Menu::CreateChoicesMenu( char *psz_var, vlc_object_t *p_object,
670                                  bool b_root )
671 {
672     vlc_value_t val, val_list, text_list;
673     int i_type, i;
674
675     /* Check the type of the object variable */
676     i_type = var_Type( p_object, psz_var );
677
678     /* Make sure we want to display the variable */
679     if( IsMenuEmpty( psz_var, p_object, b_root ) ) return NULL;
680
681     switch( i_type & VLC_VAR_TYPE )
682     {
683     case VLC_VAR_VOID:
684     case VLC_VAR_BOOL:
685     case VLC_VAR_VARIABLE:
686     case VLC_VAR_STRING:
687     case VLC_VAR_INTEGER:
688     case VLC_VAR_FLOAT:
689         break;
690     default:
691         /* Variable doesn't exist or isn't handled */
692         return NULL;
693     }
694
695     if( var_Change( p_object, psz_var, VLC_VAR_GETLIST,
696                     &val_list, &text_list ) < 0 )
697     {
698         return NULL;
699     }
700
701     wxMenu *menu = new wxMenu;
702     for( i = 0; i < val_list.p_list->i_count; i++ )
703     {
704         vlc_value_t another_val;
705         wxMenuItemExt *menuitem;
706
707         switch( i_type & VLC_VAR_TYPE )
708         {
709         case VLC_VAR_VARIABLE:
710           menu->Append( MenuDummy_Event,
711                         wxU(text_list.p_list->p_values[i].psz_string ?
712                         text_list.p_list->p_values[i].psz_string :
713                         val_list.p_list->p_values[i].psz_string),
714                         CreateChoicesMenu(
715                             val_list.p_list->p_values[i].psz_string,
716                             p_object, FALSE ), wxT("") );
717           break;
718
719         case VLC_VAR_STRING:
720           var_Get( p_object, psz_var, &val );
721
722           another_val.psz_string =
723               strdup(val_list.p_list->p_values[i].psz_string);
724           menuitem =
725               new wxMenuItemExt( menu, ++i_item_id,
726                                  wxU(text_list.p_list->p_values[i].psz_string ?
727                                  text_list.p_list->p_values[i].psz_string :
728                                  another_val.psz_string), wxT(""),
729                                  i_type & VLC_VAR_ISCOMMAND ?
730                                    wxITEM_NORMAL : wxITEM_RADIO,
731                                  strdup(psz_var),
732                                  p_object->i_object_id, another_val, i_type );
733
734           menu->Append( menuitem );
735
736           if( !(i_type & VLC_VAR_ISCOMMAND) && val.psz_string &&
737               !strcmp( val.psz_string,
738                        val_list.p_list->p_values[i].psz_string ) )
739               menu->Check( i_item_id, TRUE );
740
741           if( val.psz_string ) free( val.psz_string );
742           break;
743
744         case VLC_VAR_INTEGER:
745           var_Get( p_object, psz_var, &val );
746
747           menuitem =
748               new wxMenuItemExt( menu, ++i_item_id,
749                                  text_list.p_list->p_values[i].psz_string ?
750                                  (wxString)wxU(
751                                    text_list.p_list->p_values[i].psz_string) :
752                                  wxString::Format(wxT("%d"),
753                                  val_list.p_list->p_values[i].i_int), wxT(""),
754                                  i_type & VLC_VAR_ISCOMMAND ?
755                                    wxITEM_NORMAL : wxITEM_RADIO,
756                                  strdup(psz_var),
757                                  p_object->i_object_id,
758                                  val_list.p_list->p_values[i], i_type );
759
760           menu->Append( menuitem );
761
762           if( !(i_type & VLC_VAR_ISCOMMAND) &&
763               val_list.p_list->p_values[i].i_int == val.i_int )
764               menu->Check( i_item_id, TRUE );
765           break;
766
767         case VLC_VAR_FLOAT:
768           var_Get( p_object, psz_var, &val );
769
770           menuitem =
771               new wxMenuItemExt( menu, ++i_item_id,
772                                  text_list.p_list->p_values[i].psz_string ?
773                                  (wxString)wxU(
774                                    text_list.p_list->p_values[i].psz_string) :
775                                  wxString::Format(wxT("%.2f"),
776                                  val_list.p_list->p_values[i].f_float),wxT(""),
777                                  i_type & VLC_VAR_ISCOMMAND ?
778                                    wxITEM_NORMAL : wxITEM_RADIO,
779                                  strdup(psz_var),
780                                  p_object->i_object_id,
781                                  val_list.p_list->p_values[i], i_type );
782
783           menu->Append( menuitem );
784
785           if( !(i_type & VLC_VAR_ISCOMMAND) &&
786               val_list.p_list->p_values[i].f_float == val.f_float )
787               menu->Check( i_item_id, TRUE );
788           break;
789
790         default:
791           break;
792         }
793     }
794
795     /* clean up everything */
796     var_Change( p_object, psz_var, VLC_VAR_FREELIST, &val_list, &text_list );
797
798     return menu;
799 }
800
801 /*****************************************************************************
802  * A small helper class which intercepts all popup menu events
803  *****************************************************************************/
804 MenuEvtHandler::MenuEvtHandler( intf_thread_t *_p_intf,
805                                 Interface *_p_main_interface )
806 {
807     /* Initializations */
808     p_intf = _p_intf;
809     p_main_interface = _p_main_interface;
810 }
811
812 MenuEvtHandler::~MenuEvtHandler()
813 {
814 }
815
816 void MenuEvtHandler::OnShowDialog( wxCommandEvent& event )
817 {
818     if( p_intf->p_sys->pf_show_dialog )
819     {
820         int i_id;
821
822         switch( event.GetId() )
823         {
824         case OpenFileSimple_Event:
825             i_id = INTF_DIALOG_FILE_SIMPLE;
826             break;
827         case OpenFile_Event:
828             i_id = INTF_DIALOG_FILE;
829             break;
830         case OpenDisc_Event:
831             i_id = INTF_DIALOG_DISC;
832             break;
833         case OpenNet_Event:
834             i_id = INTF_DIALOG_NET;
835             break;
836         case OpenCapture_Event:
837             i_id = INTF_DIALOG_CAPTURE;
838             break;
839         case Preferences_Event:
840             i_id = INTF_DIALOG_PREFS;
841             break;
842         default:
843             i_id = INTF_DIALOG_FILE;
844             break;
845
846         }
847
848         p_intf->p_sys->pf_show_dialog( p_intf, i_id, 1, 0 );
849     }
850 }
851
852 void MenuEvtHandler::OnMenuEvent( wxCommandEvent& event )
853 {
854     wxMenuItem *p_menuitem = NULL;
855     int i_hotkey_event = p_intf->p_sys->i_first_hotkey_event;
856     int i_hotkeys = p_intf->p_sys->i_hotkeys;
857
858     /* Check if this is an auto generated menu item */
859     if( event.GetId() < FirstAutoGenerated_Event )
860     {
861         event.Skip();
862         return;
863     }
864
865     /* Check if this is an hotkey event */
866     if( event.GetId() >= i_hotkey_event &&
867         event.GetId() < i_hotkey_event + i_hotkeys )
868     {
869         vlc_value_t val;
870
871         val.i_int =
872             p_intf->p_vlc->p_hotkeys[event.GetId() - i_hotkey_event].i_key;
873
874         /* Get the key combination and send it to the hotkey handler */
875         var_Set( p_intf->p_vlc, "key-pressed", val );
876         msg_Err( p_intf, "received key event: %i", event.GetId() );
877         return;
878     }
879
880     if( !p_main_interface ||
881         (p_menuitem = p_main_interface->GetMenuBar()->FindItem(event.GetId()))
882         == NULL )
883     {
884         if( p_intf->p_sys->p_popup_menu )
885         {
886             p_menuitem = 
887                 p_intf->p_sys->p_popup_menu->FindItem( event.GetId() );
888         }
889     }
890
891     if( p_menuitem )
892     {
893         wxMenuItemExt *p_menuitemext = (wxMenuItemExt *)p_menuitem;
894         vlc_object_t *p_object;
895
896         p_object = (vlc_object_t *)vlc_object_get( p_intf,
897                                        p_menuitemext->i_object_id );
898         if( p_object == NULL ) return;
899
900         wxMutexGuiLeave(); // We don't want deadlocks
901         var_Set( p_object, p_menuitemext->psz_var, p_menuitemext->val );
902         wxMutexGuiEnter();
903
904         vlc_object_release( p_object );
905     }
906     else
907         event.Skip();
908 }
909
910 /*****************************************************************************
911  * A small helper class which encapsulate wxMenuitem with some other useful
912  * things.
913  *****************************************************************************/
914 wxMenuItemExt::wxMenuItemExt( wxMenu* parentMenu, int id, const wxString& text,
915     const wxString& helpString, wxItemKind kind,
916     char *_psz_var, int _i_object_id, vlc_value_t _val, int _i_val_type ):
917     wxMenuItem( parentMenu, id, text, helpString, kind )
918 {
919     /* Initializations */
920     psz_var = _psz_var;
921     i_val_type = _i_val_type;
922     i_object_id = _i_object_id;
923     val = _val;
924 };
925
926 wxMenuItemExt::~wxMenuItemExt()
927 {
928     if( psz_var ) free( psz_var );
929     if( ((i_val_type & VLC_VAR_TYPE) == VLC_VAR_STRING)
930         && val.psz_string ) free( val.psz_string );
931 };