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