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