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