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