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