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