* src/input/input_programs.c: implemented prev-title, next-title, prev-chapter and...
[vlc.git] / modules / gui / wxwindows / menus.cpp
1 /*****************************************************************************
2  * menus.cpp : wxWindows plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2001 VideoLAN
5  * $Id: menus.cpp,v 1.10 2003/05/15 21:31:53 gbazin Exp $
6  *
7  * Authors: Gildas Bazin <gbazin@netcourrier.com>
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., 59 Temple Place - Suite 330, Boston, MA  02111, 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
34 #ifdef WIN32                                                 /* mingw32 hack */
35 #undef Yield
36 #undef CreateDialog
37 #endif
38
39 /* Let vlc take care of the i18n stuff */
40 #define WXINTL_NO_GETTEXT_MACRO
41
42 #include <wx/wxprec.h>
43 #include <wx/wx.h>
44 #include <wx/listctrl.h>
45
46 #include <vlc/intf.h>
47
48 #include "wxwindows.h"
49
50 class wxMenuItemExt: public wxMenuItem
51 {
52 public:
53     /* Constructor */
54     wxMenuItemExt( wxMenu* parentMenu, int id, const wxString& text,
55                    const wxString& helpString, wxItemKind kind,
56                    char *_psz_var, int _i_object_id, vlc_value_t _val,
57                    int _i_val_type );
58
59     virtual ~wxMenuItemExt();
60
61     char *psz_var;
62     int  i_val_type;
63     int  i_object_id;
64     vlc_value_t val;
65
66 private:
67
68 };
69
70 /*****************************************************************************
71  * Event Table.
72  *****************************************************************************/
73
74 /* IDs for the controls and the menu commands */
75 enum
76 {
77     /* menu items */
78     FirstAutoGenerated_Event = wxID_HIGHEST + 1000,
79     MenuDummy_Event,
80     AudioMenu_Events,
81     VideoMenu_Events = wxID_HIGHEST + 1100,
82     NavigMenu_Events = wxID_HIGHEST + 1200,
83     PopupMenu_Events = wxID_HIGHEST + 1300,
84 };
85
86 BEGIN_EVENT_TABLE(Menu, wxMenu)
87     /* Menu events */
88     EVT_MENU(MenuDummy_Event, Menu::OnEntrySelected)
89 END_EVENT_TABLE()
90
91 BEGIN_EVENT_TABLE(MenuEvtHandler, wxEvtHandler)
92     EVT_MENU(-1, MenuEvtHandler::OnMenuEvent)
93 END_EVENT_TABLE()
94
95 void PopupMenu( intf_thread_t *_p_intf, Interface *_p_main_interface,
96                 const wxPoint& pos )
97 {
98     vlc_object_t *p_object;
99     char *ppsz_varnames[19];
100     int pi_objects[19];
101     int i = 0;
102
103     /* Initializations */
104     memset( pi_objects, 0, 19 * sizeof(int) );
105
106     /* Audio menu */
107     ppsz_varnames[i++] = _("Audio menu");
108     ppsz_varnames[i++] = NULL; /* Separator */
109
110     p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_AOUT,
111                                                 FIND_ANYWHERE );
112     if( p_object != NULL )
113     {
114         ppsz_varnames[i] = "audio-device";
115         pi_objects[i++] = p_object->i_object_id;
116         ppsz_varnames[i] = "audio-channels";
117         pi_objects[i++] = p_object->i_object_id;
118         vlc_object_release( p_object );
119     }
120
121     /* Video menu */
122     ppsz_varnames[i++] = NULL; /* Separator */
123     ppsz_varnames[i++] = _("Video menu");
124     ppsz_varnames[i++] = NULL; /* Separator */
125
126     p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_VOUT,
127                                                 FIND_ANYWHERE );
128     if( p_object != NULL )
129     {
130         ppsz_varnames[i] = "fullscreen";
131         pi_objects[i++] = p_object->i_object_id;
132         vlc_object_release( p_object );
133     }
134
135     /* Input menu */
136     ppsz_varnames[i++] = NULL; /* Separator */
137     ppsz_varnames[i++] = _("Input menu");
138     ppsz_varnames[i++] = NULL; /* Separator */
139
140     p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_INPUT,
141                                                 FIND_ANYWHERE );
142     if( p_object != NULL )
143     {
144         ppsz_varnames[i] = "title";
145         pi_objects[i++] = p_object->i_object_id;
146         ppsz_varnames[i] = "chapter";
147         pi_objects[i++] = p_object->i_object_id;
148         ppsz_varnames[i] = "navigation";
149         pi_objects[i++] = p_object->i_object_id;
150         ppsz_varnames[i] = "program";
151         pi_objects[i++] = p_object->i_object_id;
152
153         ppsz_varnames[i] = "video-es";
154         pi_objects[i++] = p_object->i_object_id;
155         ppsz_varnames[i] = "audio-es";
156         pi_objects[i++] = p_object->i_object_id;
157         ppsz_varnames[i] = "spu-es";
158         pi_objects[i++] = p_object->i_object_id;
159
160         vlc_object_release( p_object );
161     }
162
163     /* Misc stuff */
164     ppsz_varnames[i++] = NULL; /* Separator */
165     ppsz_varnames[i++] = _("Close");
166
167     /* Build menu */
168     Menu popupmenu( _p_intf, _p_main_interface, i,
169                      ppsz_varnames, pi_objects, PopupMenu_Events );
170
171     _p_main_interface->p_popup_menu = &popupmenu;
172     _p_main_interface->PopupMenu( &popupmenu, pos.x, pos.y );
173 }
174
175 wxMenu *AudioMenu( intf_thread_t *_p_intf, Interface *_p_main_interface )
176 {
177     vlc_object_t *p_object;
178     char *ppsz_varnames[5];
179     int pi_objects[5];
180     int i = 0;
181
182     /* Initializations */
183     memset( pi_objects, 0, 5 * sizeof(int) );
184
185     p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_AOUT,
186                                                 FIND_ANYWHERE );
187     if( p_object != NULL )
188     {
189         ppsz_varnames[i] = "audio-device";
190         pi_objects[i++] = p_object->i_object_id;
191         ppsz_varnames[i] = "audio-channels";
192         pi_objects[i++] = p_object->i_object_id;
193         vlc_object_release( p_object );
194     }
195
196     p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_INPUT,
197                                                 FIND_ANYWHERE );
198     if( p_object != NULL )
199     {
200         ppsz_varnames[i] = "audio-es";
201         pi_objects[i++] = p_object->i_object_id;
202         vlc_object_release( p_object );
203     }
204
205     /* Build menu */
206     return new Menu( _p_intf, _p_main_interface, i,
207                      ppsz_varnames, pi_objects, AudioMenu_Events );
208 }
209
210 wxMenu *VideoMenu( intf_thread_t *_p_intf, Interface *_p_main_interface )
211 {
212     vlc_object_t *p_object;
213     char *ppsz_varnames[4];
214     int pi_objects[4];
215     int i = 0;
216
217     /* Initializations */
218     memset( pi_objects, 0, 4 * sizeof(int) );
219
220     p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_VOUT,
221                                                 FIND_ANYWHERE );
222     if( p_object != NULL )
223     {
224         ppsz_varnames[i] = "fullscreen";
225         pi_objects[i++] = p_object->i_object_id;
226         vlc_object_release( p_object );
227     }
228
229     p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_INPUT,
230                                                 FIND_ANYWHERE );
231     if( p_object != NULL )
232     {
233         ppsz_varnames[i] = "video-es";
234         pi_objects[i++] = p_object->i_object_id;
235         ppsz_varnames[i] = "spu-es";
236         pi_objects[i++] = p_object->i_object_id;
237         vlc_object_release( p_object );
238     }
239
240     /* Build menu */
241     return new Menu( _p_intf, _p_main_interface, i,
242                      ppsz_varnames, pi_objects, VideoMenu_Events );
243 }
244
245 wxMenu *NavigMenu( intf_thread_t *_p_intf, Interface *_p_main_interface )
246 {
247     vlc_object_t *p_object;
248     char *ppsz_varnames[10];
249     int pi_objects[10];
250     int i = 0;
251
252     /* Initializations */
253     memset( pi_objects, 0, 4 * sizeof(int) );
254
255     p_object = (vlc_object_t *)vlc_object_find( _p_intf, VLC_OBJECT_INPUT,
256                                                 FIND_ANYWHERE );
257     if( p_object != NULL )
258     {
259         ppsz_varnames[i] = "title";
260         pi_objects[i++] = p_object->i_object_id;
261         ppsz_varnames[i] = "chapter";
262         pi_objects[i++] = p_object->i_object_id;
263         ppsz_varnames[i] = "navigation";
264         pi_objects[i++] = p_object->i_object_id;
265         ppsz_varnames[i] = "program";
266         pi_objects[i++] = p_object->i_object_id;
267
268         ppsz_varnames[i] = "prev-title";
269         pi_objects[i++] = p_object->i_object_id;
270         ppsz_varnames[i] = "next-title";
271         pi_objects[i++] = p_object->i_object_id;
272         ppsz_varnames[i] = "prev-chapter";
273         pi_objects[i++] = p_object->i_object_id;
274         ppsz_varnames[i] = "next-chapter";
275         pi_objects[i++] = p_object->i_object_id;
276
277         vlc_object_release( p_object );
278     }
279
280     /* Build menu */
281     return new Menu( _p_intf, _p_main_interface, i,
282                      ppsz_varnames, pi_objects, NavigMenu_Events );
283 }
284
285 /*****************************************************************************
286  * Constructor.
287  *****************************************************************************/
288 Menu::Menu( intf_thread_t *_p_intf, Interface *_p_main_interface,
289             int i_count, char **ppsz_varnames, int *pi_objects,
290             int i_start_id ): wxMenu( )
291 {
292     vlc_object_t *p_object;
293     int i;
294
295     /* Initializations */
296     p_intf = _p_intf;
297     p_main_interface = _p_main_interface;
298
299     i_item_id = i_start_id;
300
301     for( i = 0; i < i_count; i++ )
302     {
303         if( !ppsz_varnames[i] )
304         {
305             AppendSeparator();
306             continue;
307         }
308
309         if( !pi_objects[i] )
310         {
311             Append( MenuDummy_Event, wxU(ppsz_varnames[i]) );
312             continue;
313         }
314
315         p_object = (vlc_object_t *)vlc_object_get( p_intf, pi_objects[i] );
316         if( p_object == NULL ) continue;
317
318         CreateMenuItem( this, ppsz_varnames[i], p_object );
319         vlc_object_release( p_object );
320     }
321
322     /* Special case for empty menus */
323     if( GetMenuItemCount() == 0 )
324     {
325         Append( MenuDummy_Event, wxU(_("Empty")) );
326         Enable( MenuDummy_Event, FALSE );
327     }
328 }
329
330 Menu::~Menu()
331 {
332 }
333
334 /*****************************************************************************
335  * Private methods.
336  *****************************************************************************/
337 void Menu::OnEntrySelected( wxCommandEvent& WXUNUSED(event) )
338 {
339 }
340
341 void Menu::CreateMenuItem( wxMenu *menu, char *psz_var,
342                            vlc_object_t *p_object )
343 {
344     wxMenuItemExt *menuitem;
345     vlc_value_t val, text;
346     int i_type;
347
348     /* Check the type of the object variable */
349     i_type = var_Type( p_object, psz_var );
350
351     switch( i_type & VLC_VAR_TYPE )
352     {
353     case VLC_VAR_VOID:
354     case VLC_VAR_BOOL:
355     case VLC_VAR_VARIABLE:
356     case VLC_VAR_STRING:
357     case VLC_VAR_INTEGER:
358         break;
359     default:
360         /* Variable doesn't exist or isn't handled */
361         return;
362     }
363
364     /* Make sure we want to display the variable */
365     if( i_type & VLC_VAR_HASCHOICE )
366     {
367         var_Change( p_object, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
368         if( val.i_int == 0 ) return;
369     }
370
371     /* Get the descriptive name of the variable */
372     var_Change( p_object, psz_var, VLC_VAR_GETTEXT, &text, NULL );
373
374     if( i_type & VLC_VAR_HASCHOICE )
375     {
376         menu->Append( MenuDummy_Event,
377                       wxU(text.psz_string ? text.psz_string : psz_var),
378                       CreateChoicesMenu( psz_var, p_object ),
379                       wxT("")/* Nothing for now (maybe use a GETLONGTEXT) */ );
380
381         if( text.psz_string ) free( text.psz_string );
382         return;
383     }
384
385
386     switch( i_type & VLC_VAR_TYPE )
387     {
388     case VLC_VAR_VOID:
389         menuitem = new wxMenuItemExt( menu, ++i_item_id,
390                                       wxU(text.psz_string ?
391                                         text.psz_string : psz_var),
392                                       wxT(""), wxITEM_NORMAL, strdup(psz_var),
393                                       p_object->i_object_id, val, i_type );
394         menu->Append( menuitem );
395         break;
396
397     case VLC_VAR_BOOL:
398         menuitem = new wxMenuItemExt( menu, ++i_item_id,
399                                       wxU(text.psz_string ?
400                                         text.psz_string : psz_var),
401                                       wxT(""), wxITEM_CHECK, strdup(psz_var),
402                                       p_object->i_object_id, val, i_type );
403         menu->Append( menuitem );
404         Check( i_item_id -1, val.b_bool ? FALSE : TRUE );
405         break;
406
407     default:
408         if( text.psz_string ) free( text.psz_string );
409         return;
410     }
411
412     if( text.psz_string ) free( text.psz_string );
413 }
414
415 wxMenu *Menu::CreateChoicesMenu( char *psz_var, vlc_object_t *p_object )
416 {
417     vlc_value_t val, val_list, text_list;
418     int i_type, i;
419
420     /* Check the type of the object variable */
421     i_type = var_Type( p_object, psz_var );
422
423     /* Make sure we want to display the variable */
424     if( i_type & VLC_VAR_HASCHOICE )
425     {
426         var_Change( p_object, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
427         if( val.i_int == 0 ) return NULL;
428     }
429     else
430     {
431         return NULL;
432     }
433
434     switch( i_type & VLC_VAR_TYPE )
435     {
436     case VLC_VAR_VOID:
437     case VLC_VAR_BOOL:
438     case VLC_VAR_VARIABLE:
439     case VLC_VAR_STRING:
440     case VLC_VAR_INTEGER:
441         break;
442     default:
443         /* Variable doesn't exist or isn't handled */
444         return NULL;
445     }
446
447     if( var_Get( p_object, psz_var, &val ) < 0 )
448     {
449         return NULL;
450     }
451
452     if( var_Change( p_object, psz_var, VLC_VAR_GETLIST,
453                     &val_list, &text_list ) < 0 )
454     {
455         if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
456         return NULL;
457     }
458
459     wxMenu *menu = new wxMenu;
460     for( i = 0; i < val_list.p_list->i_count; i++ )
461     {
462         vlc_value_t another_val;
463         wxMenuItemExt *menuitem;
464
465         switch( i_type & VLC_VAR_TYPE )
466         {
467         case VLC_VAR_VARIABLE:
468           menu->Append( MenuDummy_Event,
469                         wxU(text_list.p_list->p_values[i].psz_string ?
470                         text_list.p_list->p_values[i].psz_string :
471                         val_list.p_list->p_values[i].psz_string),
472                         CreateChoicesMenu(
473                             val_list.p_list->p_values[i].psz_string,
474                             p_object ), wxT("") );
475           break;
476
477         case VLC_VAR_STRING:
478           another_val.psz_string =
479               strdup(val_list.p_list->p_values[i].psz_string);
480           menuitem =
481               new wxMenuItemExt( menu, ++i_item_id,
482                                  wxU(text_list.p_list->p_values[i].psz_string ?
483                                  text_list.p_list->p_values[i].psz_string :
484                                  another_val.psz_string),
485                                  wxT(""), wxITEM_RADIO, strdup(psz_var),
486                                  p_object->i_object_id, another_val, i_type );
487
488           menu->Append( menuitem );
489
490           if( !strcmp( val.psz_string,
491                        val_list.p_list->p_values[i].psz_string ) )
492               menu->Check( i_item_id, TRUE );
493           break;
494
495         case VLC_VAR_INTEGER:
496           menuitem =
497               new wxMenuItemExt( menu, ++i_item_id,
498                                  text_list.p_list->p_values[i].psz_string ?
499                                  wxU(text_list.p_list->p_values[i].psz_string):
500                                  wxString::Format(wxT("%d"),
501                                  val_list.p_list->p_values[i].i_int),
502                                  wxT(""), wxITEM_RADIO, strdup(psz_var),
503                                  p_object->i_object_id,
504                                  val_list.p_list->p_values[i], i_type );
505
506           menu->Append( menuitem );
507
508           if( val_list.p_list->p_values[i].i_int == val.i_int )
509               menu->Check( i_item_id, TRUE );
510           break;
511
512         default:
513           break;
514         }
515     }
516
517     /* clean up everything */
518     if( i_type == VLC_VAR_STRING ) free( val.psz_string );
519     var_Change( p_object, psz_var, VLC_VAR_FREELIST, &val_list, &text_list );
520
521     return menu;
522 }
523
524 /*****************************************************************************
525  * A small helper class which intercepts all popup menu events
526  *****************************************************************************/
527 MenuEvtHandler::MenuEvtHandler( intf_thread_t *_p_intf,
528                                 Interface *_p_main_interface )
529 {
530     /* Initializations */
531     p_intf = _p_intf;
532     p_main_interface = _p_main_interface;
533 }
534
535 MenuEvtHandler::~MenuEvtHandler()
536 {
537 }
538
539 void MenuEvtHandler::OnMenuEvent( wxCommandEvent& event )
540 {
541     wxMenuItem *p_menuitem;
542
543     /* Check if this is an auto generated menu item */
544     if( event.GetId() < FirstAutoGenerated_Event )
545     {
546         event.Skip();
547         return;
548     }
549
550     if( (p_menuitem = p_main_interface->GetMenuBar()->FindItem(event.GetId()))
551         == NULL )
552     {
553         if( p_main_interface->p_popup_menu )
554         {
555             p_menuitem = 
556                 p_main_interface->p_popup_menu->FindItem( event.GetId() );
557         }
558     }
559
560     if( p_menuitem )
561     {
562         wxMenuItemExt *p_menuitemext = (wxMenuItemExt *)p_menuitem;
563         vlc_object_t *p_object;
564
565         p_object = (vlc_object_t *)vlc_object_get( p_intf,
566                                        p_menuitemext->i_object_id );
567         if( p_object == NULL ) return;
568
569         var_Set( p_object, p_menuitemext->psz_var, p_menuitemext->val );
570
571         vlc_object_release( p_object );
572     }
573     else
574         event.Skip();
575 }
576
577 /*****************************************************************************
578  * A small helper class which encapsulate wxMenuitem with some other useful
579  * things.
580  *****************************************************************************/
581 wxMenuItemExt::wxMenuItemExt( wxMenu* parentMenu, int id, const wxString& text,
582     const wxString& helpString, wxItemKind kind,
583     char *_psz_var, int _i_object_id, vlc_value_t _val, int _i_val_type ):
584     wxMenuItem( parentMenu, id, text, helpString, kind )
585 {
586     /* Initializations */
587     psz_var = _psz_var;
588     i_val_type = _i_val_type;
589     i_object_id = _i_object_id;
590     val = _val;
591 };
592
593 wxMenuItemExt::~wxMenuItemExt()
594 {
595     if( psz_var ) free( psz_var );
596     if( ((i_val_type & VLC_VAR_TYPE) == VLC_VAR_STRING)
597         && val.psz_string ) free( val.psz_string );
598 };