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