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