]> git.sesse.net Git - vlc/blob - modules/gui/wxwidgets/menus.cpp
* modules/gui/wxwidgets/menus.cpp: avoid using STL strings. This gets rid of weird...
[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         PUSH_VAR( "ffmpeg-pp-q" );
178         vlc_object_release( p_dec_obj );
179     }
180     return VLC_SUCCESS;
181 }
182
183 int AudioAutoMenuBuilder( vlc_object_t *p_object, vector<int> &ri_objects,
184                           vector<const char *> &rs_varnames )
185 {
186     PUSH_VAR( "audio-device" );
187     PUSH_VAR( "audio-channels" );
188     PUSH_VAR( "visual" );
189     PUSH_VAR( "equalizer" );
190     return VLC_SUCCESS;
191 }
192
193 int IntfAutoMenuBuilder( intf_thread_t *p_intf, vector<int> &ri_objects,
194                          vector<const char *> &rs_varnames, bool is_popup)
195 {
196     /* vlc_object_find is needed because of the dialogs provider case */
197     vlc_object_t *p_object;
198     p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_INTF,
199                                                 FIND_PARENT );
200     if( p_object != NULL )
201     {
202         if( is_popup )
203         {
204 #ifndef WIN32
205             PUSH_VAR( "intf-switch" );
206 #endif
207         }
208         else
209         {
210             PUSH_VAR( "intf-switch" );
211         }
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
226 #define PUSH_SEPARATOR if( ai_objects.size() != i_last_separator ) { \
227                             ai_objects.push_back( 0 ); \
228                             as_varnames.push_back( "" ); \
229                             i_last_separator = ai_objects.size(); }
230
231 #define POPUP_BOILERPLATE \
232     unsigned int i_last_separator = 0; \
233     vector<int> ai_objects; \
234     vector<const char *> as_varnames; \
235     playlist_t *p_playlist = (playlist_t *) vlc_object_find( p_intf, \
236                                           VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );\
237     if( !p_playlist ) \
238         return; \
239     input_thread_t *p_input = p_playlist->p_input
240
241 #define CREATE_POPUP    \
242     Menu popupmenu( p_intf, PopupMenu_Events ); \
243     popupmenu.Populate( as_varnames, ai_objects ); \
244     p_intf->p_sys->p_popup_menu = &popupmenu; \
245     p_parent->PopupMenu( &popupmenu, pos.x, pos.y ); \
246     p_intf->p_sys->p_popup_menu = NULL; \
247     i_last_separator = 0 /* stop compiler warning */
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<const char *> 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<const char *> 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<const char *> 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<const char *> 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<const char *> & ras_varnames,
558                      vector<int> & rai_objects )
559 {
560     vlc_object_t *p_object;
561     vlc_bool_t b_section_empty = VLC_FALSE;
562     int i;
563
564     i_item_id = i_start_id;
565
566     for( i = 0; i < (int)rai_objects.size() ; i++ )
567     {
568         if( !ras_varnames[i] || !*ras_varnames[i] )
569         {
570             if( b_section_empty )
571             {
572                 Append( MenuDummy_Event + i, wxU(_("Empty")) );
573                 Enable( MenuDummy_Event + i, FALSE );
574             }
575             AppendSeparator();
576             b_section_empty = VLC_TRUE;
577             continue;
578         }
579
580         if( rai_objects[i] == 0  )
581         {
582             Append( MenuDummy_Event, wxU(ras_varnames[i]) );
583             b_section_empty = VLC_FALSE;
584             continue;
585         }
586
587         p_object = (vlc_object_t *)vlc_object_get( p_intf,
588                                                    rai_objects[i] );
589         if( p_object == NULL ) continue;
590
591         b_section_empty = VLC_FALSE;
592         CreateMenuItem( this, ras_varnames[i], p_object );
593         vlc_object_release( p_object );
594     }
595
596     /* Special case for empty menus */
597     if( GetMenuItemCount() == 0 || b_section_empty )
598     {
599         Append( MenuDummy_Event + i, wxU(_("Empty")) );
600         Enable( MenuDummy_Event + i, FALSE );
601     }
602 }
603
604 /* Work-around helper for buggy wxGTK */
605 static void RecursiveDestroy( wxMenu *menu )
606 {
607     wxMenuItemList::Node *node = menu->GetMenuItems().GetFirst();
608     for( ; node; )
609     {
610         wxMenuItem *item = node->GetData();
611         node = node->GetNext();
612
613         /* Delete the submenus */
614         wxMenu *submenu = item->GetSubMenu();
615         if( submenu )
616         {
617             RecursiveDestroy( submenu );
618         }
619         menu->Delete( item );
620     }
621 }
622
623 void Menu::Clear( )
624 {
625     RecursiveDestroy( this );
626 }
627
628 /*****************************************************************************
629  * Private methods.
630  *****************************************************************************/
631 static bool IsMenuEmpty( const char *psz_var, vlc_object_t *p_object,
632                          bool b_root = TRUE )
633 {
634     vlc_value_t val, val_list;
635     int i_type, i_result, i;
636
637     /* Check the type of the object variable */
638     i_type = var_Type( p_object, psz_var );
639
640     /* Check if we want to display the variable */
641     if( !(i_type & VLC_VAR_HASCHOICE) ) return FALSE;
642
643     var_Change( p_object, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
644     if( val.i_int == 0 ) return TRUE;
645
646     if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE )
647     {
648         /* Very evil hack ! intf-switch can have only one value */ 
649         if( !strcmp( psz_var, "intf-switch" ) ) return FALSE;
650         if( val.i_int == 1 && b_root ) return TRUE;
651         else return FALSE;
652     }
653
654     /* Check children variables in case of VLC_VAR_VARIABLE */
655     if( var_Change( p_object, psz_var, VLC_VAR_GETLIST, &val_list, NULL ) < 0 )
656     {
657         return TRUE;
658     }
659
660     for( i = 0, i_result = TRUE; i < val_list.p_list->i_count; i++ )
661     {
662         if( !IsMenuEmpty( val_list.p_list->p_values[i].psz_string,
663                           p_object, FALSE ) )
664         {
665             i_result = FALSE;
666             break;
667         }
668     }
669
670     /* clean up everything */
671     var_Change( p_object, psz_var, VLC_VAR_FREELIST, &val_list, NULL );
672
673     return i_result;
674 }
675
676 void Menu::CreateMenuItem( wxMenu *menu, const char *psz_var,
677                            vlc_object_t *p_object )
678 {
679     wxMenuItemExt *menuitem;
680     vlc_value_t val, text;
681     int i_type;
682
683     /* Check the type of the object variable */
684     i_type = var_Type( p_object, psz_var );
685
686     switch( i_type & VLC_VAR_TYPE )
687     {
688     case VLC_VAR_VOID:
689     case VLC_VAR_BOOL:
690     case VLC_VAR_VARIABLE:
691     case VLC_VAR_STRING:
692     case VLC_VAR_INTEGER:
693     case VLC_VAR_FLOAT:
694         break;
695     default:
696         /* Variable doesn't exist or isn't handled */
697         return;
698     }
699
700     /* Make sure we want to display the variable */
701     if( IsMenuEmpty( psz_var, p_object ) )  return;
702
703     /* Get the descriptive name of the variable */
704     var_Change( p_object, psz_var, VLC_VAR_GETTEXT, &text, NULL );
705
706     if( i_type & VLC_VAR_HASCHOICE )
707     {
708         menu->Append( MenuDummy_Event,
709                       wxU(text.psz_string ? text.psz_string : psz_var),
710                       CreateChoicesMenu( psz_var, p_object, TRUE ),
711                       wxT("")/* Nothing for now (maybe use a GETLONGTEXT) */ );
712
713         if( text.psz_string ) free( text.psz_string );
714         return;
715     }
716
717
718     switch( i_type & VLC_VAR_TYPE )
719     {
720     case VLC_VAR_VOID:
721         var_Get( p_object, psz_var, &val );
722         menuitem = new wxMenuItemExt( menu, ++i_item_id,
723                                       wxU(text.psz_string ?
724                                         text.psz_string : psz_var),
725                                       wxT(""), wxITEM_NORMAL, strdup(psz_var),
726                                       p_object->i_object_id, val, i_type );
727         menu->Append( menuitem );
728         break;
729
730     case VLC_VAR_BOOL:
731         var_Get( p_object, psz_var, &val );
732         val.b_bool = !val.b_bool;
733         menuitem = new wxMenuItemExt( menu, ++i_item_id,
734                                       wxU(text.psz_string ?
735                                         text.psz_string : psz_var),
736                                       wxT(""), wxITEM_CHECK, strdup(psz_var),
737                                       p_object->i_object_id, val, i_type );
738         menu->Append( menuitem );
739         Check( i_item_id, val.b_bool ? FALSE : TRUE );
740         break;
741     }
742
743     if( text.psz_string ) free( text.psz_string );
744 }
745
746 wxMenu *Menu::CreateChoicesMenu( const char *psz_var, vlc_object_t *p_object,
747                                  bool b_root )
748 {
749     vlc_value_t val, val_list, text_list;
750     int i_type, i;
751
752     /* Check the type of the object variable */
753     i_type = var_Type( p_object, psz_var );
754
755     /* Make sure we want to display the variable */
756     if( IsMenuEmpty( psz_var, p_object, b_root ) ) return NULL;
757
758     switch( i_type & VLC_VAR_TYPE )
759     {
760     case VLC_VAR_VOID:
761     case VLC_VAR_BOOL:
762     case VLC_VAR_VARIABLE:
763     case VLC_VAR_STRING:
764     case VLC_VAR_INTEGER:
765     case VLC_VAR_FLOAT:
766         break;
767     default:
768         /* Variable doesn't exist or isn't handled */
769         return NULL;
770     }
771
772     if( var_Change( p_object, psz_var, VLC_VAR_GETLIST,
773                     &val_list, &text_list ) < 0 )
774     {
775         return NULL;
776     }
777
778     wxMenu *menu = new wxMenu;
779     for( i = 0; i < val_list.p_list->i_count; i++ )
780     {
781         vlc_value_t another_val;
782         wxMenuItemExt *menuitem;
783
784         switch( i_type & VLC_VAR_TYPE )
785         {
786         case VLC_VAR_VARIABLE:
787           menu->Append( MenuDummy_Event,
788                         wxU(text_list.p_list->p_values[i].psz_string ?
789                         text_list.p_list->p_values[i].psz_string :
790                         val_list.p_list->p_values[i].psz_string),
791                         CreateChoicesMenu(
792                             val_list.p_list->p_values[i].psz_string,
793                             p_object, FALSE ), wxT("") );
794           break;
795
796         case VLC_VAR_STRING:
797           var_Get( p_object, psz_var, &val );
798
799           another_val.psz_string =
800               strdup(val_list.p_list->p_values[i].psz_string);
801           menuitem =
802               new wxMenuItemExt( menu, ++i_item_id,
803                                  wxU(text_list.p_list->p_values[i].psz_string ?
804                                  text_list.p_list->p_values[i].psz_string :
805                                  another_val.psz_string), wxT(""),
806                                  i_type & VLC_VAR_ISCOMMAND ?
807                                    wxITEM_NORMAL : wxITEM_RADIO,
808                                  strdup(psz_var),
809                                  p_object->i_object_id, another_val, i_type );
810
811           menu->Append( menuitem );
812
813           if( !(i_type & VLC_VAR_ISCOMMAND) && val.psz_string &&
814               !strcmp( val.psz_string,
815                        val_list.p_list->p_values[i].psz_string ) )
816               menu->Check( i_item_id, TRUE );
817
818           if( val.psz_string ) free( val.psz_string );
819           break;
820
821         case VLC_VAR_INTEGER:
822           var_Get( p_object, psz_var, &val );
823
824           menuitem =
825               new wxMenuItemExt( menu, ++i_item_id,
826                                  text_list.p_list->p_values[i].psz_string ?
827                                  (wxString)wxU(
828                                    text_list.p_list->p_values[i].psz_string) :
829                                  wxString::Format(wxT("%d"),
830                                  val_list.p_list->p_values[i].i_int), wxT(""),
831                                  i_type & VLC_VAR_ISCOMMAND ?
832                                    wxITEM_NORMAL : wxITEM_RADIO,
833                                  strdup(psz_var),
834                                  p_object->i_object_id,
835                                  val_list.p_list->p_values[i], i_type );
836
837           menu->Append( menuitem );
838
839           if( !(i_type & VLC_VAR_ISCOMMAND) &&
840               val_list.p_list->p_values[i].i_int == val.i_int )
841               menu->Check( i_item_id, TRUE );
842           break;
843
844         case VLC_VAR_FLOAT:
845           var_Get( p_object, psz_var, &val );
846
847           menuitem =
848               new wxMenuItemExt( menu, ++i_item_id,
849                                  text_list.p_list->p_values[i].psz_string ?
850                                  (wxString)wxU(
851                                    text_list.p_list->p_values[i].psz_string) :
852                                  wxString::Format(wxT("%.2f"),
853                                  val_list.p_list->p_values[i].f_float),wxT(""),
854                                  i_type & VLC_VAR_ISCOMMAND ?
855                                    wxITEM_NORMAL : wxITEM_RADIO,
856                                  strdup(psz_var),
857                                  p_object->i_object_id,
858                                  val_list.p_list->p_values[i], i_type );
859
860           menu->Append( menuitem );
861
862           if( !(i_type & VLC_VAR_ISCOMMAND) &&
863               val_list.p_list->p_values[i].f_float == val.f_float )
864               menu->Check( i_item_id, TRUE );
865           break;
866
867         default:
868           break;
869         }
870     }
871
872     /* clean up everything */
873     var_Change( p_object, psz_var, VLC_VAR_FREELIST, &val_list, &text_list );
874
875     return menu;
876 }
877
878 /*****************************************************************************
879  * A small helper class which intercepts all popup menu events
880  *****************************************************************************/
881 MenuEvtHandler::MenuEvtHandler( intf_thread_t *_p_intf,
882                                 Interface *_p_main_interface )
883 {
884     /* Initializations */
885     p_intf = _p_intf;
886     p_main_interface = _p_main_interface;
887 }
888
889 MenuEvtHandler::~MenuEvtHandler()
890 {
891 }
892
893 void MenuEvtHandler::OnShowDialog( wxCommandEvent& event )
894 {
895     if( p_intf->p_sys->pf_show_dialog )
896     {
897         int i_id;
898
899         switch( event.GetId() )
900         {
901         case OpenFileSimple_Event:
902             i_id = INTF_DIALOG_FILE_SIMPLE;
903             break;
904         case OpenFile_Event:
905             i_id = INTF_DIALOG_FILE;
906             break;
907         case OpenDirectory_Event:
908             i_id = INTF_DIALOG_DIRECTORY;
909             break;
910         case OpenDisc_Event:
911             i_id = INTF_DIALOG_DISC;
912             break;
913         case OpenNet_Event:
914             i_id = INTF_DIALOG_NET;
915             break;
916         case OpenCapture_Event:
917             i_id = INTF_DIALOG_CAPTURE;
918             break;
919         case MediaInfo_Event:
920             i_id = INTF_DIALOG_FILEINFO;
921             break;
922         case Messages_Event:
923             i_id = INTF_DIALOG_MESSAGES;
924             break;
925         case Preferences_Event:
926             i_id = INTF_DIALOG_PREFS;
927             break;
928         default:
929             i_id = INTF_DIALOG_FILE;
930             break;
931
932         }
933
934         p_intf->p_sys->pf_show_dialog( p_intf, i_id, 1, 0 );
935     }
936 }
937
938 void MenuEvtHandler::OnMenuEvent( wxCommandEvent& event )
939 {
940     wxMenuItem *p_menuitem = NULL;
941     int i_hotkey_event = p_intf->p_sys->i_first_hotkey_event;
942     int i_hotkeys = p_intf->p_sys->i_hotkeys;
943
944     if( event.GetId() >= Play_Event && event.GetId() <= Stop_Event )
945     {
946         input_thread_t *p_input;
947         playlist_t * p_playlist =
948             (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
949                                            FIND_ANYWHERE );
950         if( !p_playlist ) return;
951
952         switch( event.GetId() )
953         {
954         case Play_Event:
955         case Pause_Event:
956             p_input =
957                 (input_thread_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
958                                                    FIND_ANYWHERE );
959             if( !p_input ) playlist_Play( p_playlist );
960             else
961             {
962                 vlc_value_t val;
963                 var_Get( p_input, "state", &val );
964                 if( val.i_int != PAUSE_S ) val.i_int = PAUSE_S;
965                 else val.i_int = PLAYING_S;
966                 var_Set( p_input, "state", val );
967                 vlc_object_release( p_input );
968             }
969             break;
970         case Stop_Event:
971             playlist_Stop( p_playlist );
972             break;
973         case Previous_Event:
974             playlist_Prev( p_playlist );
975             break;
976         case Next_Event:
977             playlist_Next( p_playlist );
978             break;
979         }
980
981         vlc_object_release( p_playlist );
982         return;
983     }
984
985     /* Check if this is an auto generated menu item */
986     if( event.GetId() < FirstAutoGenerated_Event )
987     {
988         event.Skip();
989         return;
990     }
991
992     /* Check if this is an hotkey event */
993     if( event.GetId() >= i_hotkey_event &&
994         event.GetId() < i_hotkey_event + i_hotkeys )
995     {
996         vlc_value_t val;
997
998         val.i_int =
999             p_intf->p_vlc->p_hotkeys[event.GetId() - i_hotkey_event].i_key;
1000
1001         /* Get the key combination and send it to the hotkey handler */
1002         var_Set( p_intf->p_vlc, "key-pressed", val );
1003         return;
1004     }
1005
1006     if( !p_main_interface ||
1007         (p_menuitem = p_main_interface->GetMenuBar()->FindItem(event.GetId()))
1008         == NULL )
1009     {
1010         if( p_intf->p_sys->p_popup_menu )
1011         {
1012             p_menuitem =
1013                 p_intf->p_sys->p_popup_menu->FindItem( event.GetId() );
1014         }
1015     }
1016
1017     if( p_menuitem )
1018     {
1019         wxMenuItemExt *p_menuitemext = (wxMenuItemExt *)p_menuitem;
1020         vlc_object_t *p_object;
1021
1022         p_object = (vlc_object_t *)vlc_object_get( p_intf,
1023                                        p_menuitemext->i_object_id );
1024         if( p_object == NULL ) return;
1025
1026         wxMutexGuiLeave(); // We don't want deadlocks
1027         var_Set( p_object, p_menuitemext->psz_var, p_menuitemext->val );
1028         //wxMutexGuiEnter();
1029
1030         vlc_object_release( p_object );
1031     }
1032     else
1033         event.Skip();
1034 }
1035
1036 /*****************************************************************************
1037  * A small helper class which encapsulate wxMenuitem with some other useful
1038  * things.
1039  *****************************************************************************/
1040 wxMenuItemExt::wxMenuItemExt( wxMenu* parentMenu, int id, const wxString& text,
1041     const wxString& helpString, wxItemKind kind,
1042     char *_psz_var, int _i_object_id, vlc_value_t _val, int _i_val_type ):
1043     wxMenuItem( parentMenu, id, text, helpString, kind )
1044 {
1045     /* Initializations */
1046     psz_var = _psz_var;
1047     i_val_type = _i_val_type;
1048     i_object_id = _i_object_id;
1049     val = _val;
1050 };
1051
1052 wxMenuItemExt::~wxMenuItemExt()
1053 {
1054     if( psz_var ) free( psz_var );
1055     if( ((i_val_type & VLC_VAR_TYPE) == VLC_VAR_STRING)
1056         && val.psz_string ) free( val.psz_string );
1057 };