]> git.sesse.net Git - vlc/blob - modules/gui/wince/menus.cpp
FSF address change.
[vlc] / modules / gui / wince / menus.cpp
1 /*****************************************************************************
2  * menus.cpp : WinCE gui plugin for VLC
3  *****************************************************************************
4  * Copyright (C) 2000-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Marodon Cedric <cedric_marodon@yahoo.fr>
8  *          Gildas Bazin <gbazin@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>                                      /* malloc(), free() */
29 #include <string.h>                                            /* strerror() */
30 #include <stdio.h>
31 #include <vlc/vlc.h>
32 #include <vlc/intf.h>
33
34 #include "wince.h"
35
36 /*****************************************************************************
37  * Event Table.
38  *****************************************************************************/
39
40 /* IDs for the controls and the menu commands */
41 enum
42 {
43     /* menu items */
44     MenuDummy_Event = 1000,
45     OpenFileSimple_Event = 1100,
46     OpenFile_Event,
47     OpenDisc_Event,
48     OpenNet_Event,
49     FirstAutoGenerated_Event = 1999,
50     SettingsMenu_Events = 2000,
51     AudioMenu_Events = 3000,
52     VideoMenu_Events = 4000,
53     NavigMenu_Events = 5000,
54     PopupMenu_Events = 6000
55 };
56
57 HMENU OpenStreamMenu( intf_thread_t *p_intf )
58 {
59     HMENU hmenu = CreatePopupMenu();
60     AppendMenu( hmenu, MF_STRING, ID_FILE_QUICKOPEN,
61                 _T("Quick &Open File...") );
62     AppendMenu( hmenu, MF_STRING, ID_FILE_OPENFILE,
63                 _T("Open &File...") );
64     AppendMenu( hmenu, MF_STRING, ID_FILE_OPENNET,
65                 _T("Open &Network Stream...") );
66     return hmenu;
67 }
68
69 HMENU MiscMenu( intf_thread_t *p_intf )
70 {
71     HMENU hmenu = CreatePopupMenu();
72     AppendMenu( hmenu, MF_STRING, ID_VIEW_STREAMINFO, _T("Media &Info...") );
73     AppendMenu( hmenu, MF_STRING, ID_VIEW_MESSAGES, _T("&Messages...") );
74     AppendMenu( hmenu, MF_STRING, ID_PREFERENCES, _T("&Preferences...") );
75     return hmenu;
76 }
77
78 void PopupMenu( intf_thread_t *p_intf, HWND p_parent, POINT point )
79 {
80 #define MAX_POPUP_ITEMS 45
81
82     vlc_object_t *p_object, *p_input;
83     char *ppsz_varnames[MAX_POPUP_ITEMS];
84     int pi_objects[MAX_POPUP_ITEMS];
85     int i = 0, i_last_separator = 0;
86
87     /* Initializations */
88     memset( pi_objects, 0, MAX_POPUP_ITEMS * sizeof(int) );
89
90     ppsz_varnames[i] = "VLC media player";
91     pi_objects[i++] = 0;
92     ppsz_varnames[i++] = NULL; /* Separator */
93     i_last_separator = i;
94
95     /* Input menu */
96     p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_INPUT,
97                                                 FIND_ANYWHERE );
98     if( p_object != NULL )
99     {
100         ppsz_varnames[i] = "bookmark";
101         pi_objects[i++] = p_object->i_object_id;
102         ppsz_varnames[i] = "title";
103         pi_objects[i++] = p_object->i_object_id;
104         ppsz_varnames[i] = "chapter";
105         pi_objects[i++] = p_object->i_object_id;
106         ppsz_varnames[i] = "program";
107         pi_objects[i++] = p_object->i_object_id;
108         ppsz_varnames[i] = "navigation";
109         pi_objects[i++] = p_object->i_object_id;
110         ppsz_varnames[i] = "dvd_menus";
111         pi_objects[i++] = p_object->i_object_id;
112
113         ppsz_varnames[i] = "video-es";
114         pi_objects[i++] = p_object->i_object_id;
115         ppsz_varnames[i] = "audio-es";
116         pi_objects[i++] = p_object->i_object_id;
117         ppsz_varnames[i] = "spu-es";
118         pi_objects[i++] = p_object->i_object_id;
119     }
120     p_input = p_object;
121     if( !p_input ) goto interfacemenu;
122
123     /* Video menu */
124     if( i != i_last_separator ) ppsz_varnames[i++] = NULL; /* Separator */
125     i_last_separator = i;
126
127     p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_VOUT,
128                                                 FIND_ANYWHERE );
129     if( p_object != NULL )
130     {
131         vlc_object_t *p_dec_obj;
132
133         ppsz_varnames[i] = "fullscreen";
134         pi_objects[i++] = p_object->i_object_id;
135         ppsz_varnames[i] = "zoom";
136         pi_objects[i++] = p_object->i_object_id;
137         ppsz_varnames[i] = "deinterlace";
138         pi_objects[i++] = p_object->i_object_id;
139         ppsz_varnames[i] = "aspect-ratio";
140         pi_objects[i++] = p_object->i_object_id;
141         ppsz_varnames[i] = "crop";
142         pi_objects[i++] = p_object->i_object_id;
143         ppsz_varnames[i] = "video-on-top";
144         pi_objects[i++] = p_object->i_object_id;
145         ppsz_varnames[i] = "directx-wallpaper";
146         pi_objects[i++] = p_object->i_object_id;
147         ppsz_varnames[i] = "video-snapshot";
148         pi_objects[i++] = p_object->i_object_id;
149
150         p_dec_obj = (vlc_object_t *)vlc_object_find( p_object,
151                                                      VLC_OBJECT_DECODER,
152                                                      FIND_PARENT );
153         if( p_dec_obj != NULL )
154         {
155             ppsz_varnames[i] = "ffmpeg-pp-q";
156             pi_objects[i++] = p_dec_obj->i_object_id;
157             vlc_object_release( p_dec_obj );
158         }
159
160         vlc_object_release( p_object );
161     }
162
163     /* Audio menu */
164     if( i != i_last_separator ) ppsz_varnames[i++] = NULL; /* Separator */
165     i_last_separator  = i;
166
167     p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_AOUT,
168                                                 FIND_ANYWHERE );
169     if( p_object != NULL )
170     {
171         ppsz_varnames[i] = "audio-device";
172         pi_objects[i++] = p_object->i_object_id;
173         ppsz_varnames[i] = "audio-channels";
174         pi_objects[i++] = p_object->i_object_id;
175         ppsz_varnames[i] = "visual";
176         pi_objects[i++] = p_object->i_object_id;
177         ppsz_varnames[i] = "equalizer";
178         pi_objects[i++] = p_object->i_object_id;
179         vlc_object_release( p_object );
180     }
181
182  interfacemenu:
183     /* Interface menu */
184     if( i != i_last_separator ) ppsz_varnames[i++] = NULL; /* Separator */
185     i_last_separator = i;
186
187     /* vlc_object_find is needed because of the dialogs provider case */
188     p_object = (vlc_object_t *)vlc_object_find( p_intf, VLC_OBJECT_INTF,
189                                                 FIND_PARENT );
190     if( p_object != NULL )
191     {
192         /* Nothing for now */
193         vlc_object_release( p_object );
194     }
195
196     /* Build menu */
197     vector<MenuItemExt*> popup_menu;
198     HMENU hmenu = CreatePopupMenu();
199     RefreshMenu( p_intf, &popup_menu, hmenu, i,
200                  ppsz_varnames, pi_objects, PopupMenu_Events );
201     MenuItemExt::ClearList( &popup_menu );
202
203
204     /* Add static entries */
205     if( p_input != NULL )
206     {
207         vlc_value_t val;
208         AppendMenu( hmenu, MF_SEPARATOR, 0, _T("") );
209         AppendMenu( hmenu, MF_STRING, StopStream_Event, _T("Stop") );
210         AppendMenu( hmenu, MF_STRING, PrevStream_Event, _T("Previous") );
211         AppendMenu( hmenu, MF_STRING, NextStream_Event, _T("Next") );
212
213         var_Get( p_input, "state", &val );
214         if( val.i_int == PAUSE_S )
215             AppendMenu( hmenu, MF_STRING, PlayStream_Event, _T("Play") );
216         else
217             AppendMenu( hmenu, MF_STRING, PlayStream_Event, _T("Pause") );
218
219         vlc_object_release( p_input );
220     }
221     else
222     {
223         playlist_t * p_playlist =
224             (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
225                                            FIND_ANYWHERE );
226         if( p_playlist && p_playlist->i_size )
227         {
228             AppendMenu( hmenu, MF_SEPARATOR, 0, _T("") );
229             AppendMenu( hmenu, MF_STRING, PlayStream_Event, _T("Play") );
230         }
231         if( p_playlist ) vlc_object_release( p_playlist );
232     }
233
234     AppendMenu( hmenu, MF_POPUP|MF_STRING, (UINT)MiscMenu( p_intf ),
235                 _T("Miscellaneous") );
236     AppendMenu( hmenu, MF_POPUP|MF_STRING, (UINT)OpenStreamMenu( p_intf ),
237                 _T("Open") );
238
239     TrackPopupMenu( hmenu, 0, point.x, point.y, 0, p_parent, 0 );
240     PostMessage( p_parent, WM_NULL, 0, 0 );
241     DestroyMenu( hmenu );
242 }
243
244 void RefreshAudioMenu( intf_thread_t *p_intf, HMENU hMenu )
245 {
246 #define MAX_AUDIO_ITEMS 10
247
248     vlc_object_t *p_object;
249     char *ppsz_varnames[MAX_AUDIO_ITEMS];
250     int pi_objects[MAX_AUDIO_ITEMS];
251     int i;
252
253     /* Delete old menu */
254     int count = wce_GetMenuItemCount( hMenu );
255     for( i = 0; i <= count; i++ ) RemoveMenu( hMenu, 0, MF_BYPOSITION );
256
257     if( p_intf->p_sys->p_audio_menu )
258         MenuItemExt::ClearList( p_intf->p_sys->p_audio_menu );
259     else p_intf->p_sys->p_audio_menu = new vector<MenuItemExt*>;
260
261
262     /* Initializations */
263     memset( pi_objects, 0, MAX_AUDIO_ITEMS * sizeof(int) );
264     i = 0;
265
266     p_object = (vlc_object_t *)
267         vlc_object_find( p_intf, VLC_OBJECT_INPUT, FIND_ANYWHERE );
268     if( p_object != NULL )
269     {
270         ppsz_varnames[i] = "audio-es";
271         pi_objects[i++] = p_object->i_object_id;
272         vlc_object_release( p_object );
273     }
274
275     p_object = (vlc_object_t *)
276         vlc_object_find( p_intf, VLC_OBJECT_AOUT, FIND_ANYWHERE );
277     if( p_object != NULL )
278     {
279         ppsz_varnames[i] = "audio-device";
280         pi_objects[i++] = p_object->i_object_id;
281         ppsz_varnames[i] = "audio-channels";
282         pi_objects[i++] = p_object->i_object_id;
283         ppsz_varnames[i] = "visual";
284         pi_objects[i++] = p_object->i_object_id;
285         vlc_object_release( p_object );
286     }
287
288     /* Build menu */
289     RefreshMenu( p_intf, p_intf->p_sys->p_audio_menu,
290                  hMenu, i, ppsz_varnames, pi_objects, AudioMenu_Events );
291 }
292
293 void RefreshVideoMenu( intf_thread_t *p_intf, HMENU hMenu )
294 {
295 #define MAX_VIDEO_ITEMS 15
296
297     vlc_object_t *p_object;
298     char *ppsz_varnames[MAX_VIDEO_ITEMS];
299     int pi_objects[MAX_VIDEO_ITEMS];
300     int i;
301
302     /* Delete old menu */
303     int count = wce_GetMenuItemCount( hMenu );
304     for( i = 0; i <= count; i++ ) RemoveMenu( hMenu, 0, MF_BYPOSITION );
305
306     if( p_intf->p_sys->p_video_menu )
307         MenuItemExt::ClearList( p_intf->p_sys->p_video_menu );
308     else p_intf->p_sys->p_video_menu = new vector<MenuItemExt*>;
309
310     /* Initializations */
311     memset( pi_objects, 0, MAX_VIDEO_ITEMS * sizeof(int) );
312     i = 0;
313
314     p_object = (vlc_object_t *)
315         vlc_object_find( p_intf, VLC_OBJECT_INPUT, FIND_ANYWHERE );
316     if( p_object != NULL )
317     {
318         ppsz_varnames[i] = "video-es";
319         pi_objects[i++] = p_object->i_object_id;
320         ppsz_varnames[i] = "spu-es";
321         pi_objects[i++] = p_object->i_object_id;
322         vlc_object_release( p_object );
323     }
324
325     p_object = (vlc_object_t *)
326         vlc_object_find( p_intf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
327     if( p_object != NULL )
328     {
329         vlc_object_t *p_dec_obj;
330
331         ppsz_varnames[i] = "fullscreen";
332         pi_objects[i++] = p_object->i_object_id;
333 #ifdef WINCE
334         ppsz_varnames[i] = "transform";
335         pi_objects[i++] = p_object->i_object_id;
336 #endif
337         ppsz_varnames[i] = "zoom";
338         pi_objects[i++] = p_object->i_object_id;
339         ppsz_varnames[i] = "deinterlace";
340         pi_objects[i++] = p_object->i_object_id;
341         ppsz_varnames[i] = "aspect-ratio";
342         pi_objects[i++] = p_object->i_object_id;
343         ppsz_varnames[i] = "crop";
344         pi_objects[i++] = p_object->i_object_id;
345         ppsz_varnames[i] = "directx-on-top";
346         pi_objects[i++] = p_object->i_object_id;
347
348         p_dec_obj = (vlc_object_t *)
349             vlc_object_find( p_object, VLC_OBJECT_DECODER, FIND_PARENT );
350         if( p_dec_obj != NULL )
351         {
352             ppsz_varnames[i] = "ffmpeg-pp-q";
353             pi_objects[i++] = p_dec_obj->i_object_id;
354             vlc_object_release( p_dec_obj );
355         }
356
357         vlc_object_release( p_object );
358     }
359
360     /* Build menu */
361     RefreshMenu( p_intf, p_intf->p_sys->p_video_menu, hMenu, i,
362                  ppsz_varnames, pi_objects, VideoMenu_Events );
363 }
364
365 void RefreshNavigMenu( intf_thread_t *p_intf, HMENU hMenu )
366 {
367 #define MAX_NAVIG_ITEMS 10
368
369     vlc_object_t *p_object;
370     char *ppsz_varnames[MAX_NAVIG_ITEMS];
371     int pi_objects[MAX_NAVIG_ITEMS];
372     int i;
373
374     /* Delete old menu */
375     int count = wce_GetMenuItemCount( hMenu );
376     for( i = 0; i <= count; i++ ) RemoveMenu( hMenu, 0, MF_BYPOSITION );
377         
378     if( p_intf->p_sys->p_navig_menu )
379         MenuItemExt::ClearList( p_intf->p_sys->p_navig_menu );
380     else p_intf->p_sys->p_navig_menu = new vector<MenuItemExt*>;
381
382     /* Initializations */
383     memset( pi_objects, 0, MAX_NAVIG_ITEMS * sizeof(int) );
384     i = 0;
385
386     p_object = (vlc_object_t *)
387         vlc_object_find( p_intf, VLC_OBJECT_INPUT, FIND_ANYWHERE );
388     if( p_object != NULL )
389     {
390         ppsz_varnames[i] = "title";
391         pi_objects[i++] = p_object->i_object_id;
392         ppsz_varnames[i] = "chapter";
393         pi_objects[i++] = p_object->i_object_id;
394         ppsz_varnames[i] = "program";
395         pi_objects[i++] = p_object->i_object_id;
396         ppsz_varnames[i] = "navigation";
397         pi_objects[i++] = p_object->i_object_id;
398         ppsz_varnames[i] = "dvd_menus";
399         pi_objects[i++] = p_object->i_object_id;
400
401         ppsz_varnames[i] = "prev-title";
402         pi_objects[i++] = p_object->i_object_id;
403         ppsz_varnames[i] = "next-title";
404         pi_objects[i++] = p_object->i_object_id;
405         ppsz_varnames[i] = "prev-chapter";
406         pi_objects[i++] = p_object->i_object_id;
407         ppsz_varnames[i] = "next-chapter";
408         pi_objects[i++] = p_object->i_object_id;
409
410         vlc_object_release( p_object );
411     }
412
413     /* Build menu */
414     RefreshMenu( p_intf, p_intf->p_sys->p_navig_menu, hMenu, i,
415                  ppsz_varnames, pi_objects, NavigMenu_Events );
416 }
417
418 void RefreshSettingsMenu( intf_thread_t *p_intf, HMENU hMenu )
419 {
420 #define MAX_SETTINGS_ITEMS 10
421
422     vlc_object_t *p_object;
423     char *ppsz_varnames[MAX_SETTINGS_ITEMS];
424     int pi_objects[MAX_SETTINGS_ITEMS];
425     int i;
426
427     /* Delete old menu */
428     int count = wce_GetMenuItemCount( hMenu );
429     for( i = 0; i <= count; i++ ) RemoveMenu( hMenu, 0, MF_BYPOSITION );
430
431     if( p_intf->p_sys->p_settings_menu )
432         MenuItemExt::ClearList( p_intf->p_sys->p_settings_menu );
433     else p_intf->p_sys->p_settings_menu = new vector<MenuItemExt*>;
434
435     /* Initializations */
436     memset( pi_objects, 0, MAX_SETTINGS_ITEMS * sizeof(int) );
437     i = 0;
438
439     AppendMenu( hMenu, MF_STRING, ID_PREFERENCES, _T("&Preferences...") );
440
441     p_object = (vlc_object_t *)
442         vlc_object_find( p_intf, VLC_OBJECT_INTF, FIND_PARENT );
443     if( p_object != NULL )
444     {
445         ppsz_varnames[i] = "intf-switch";
446         pi_objects[i++] = p_object->i_object_id;
447         ppsz_varnames[i] = "intf-add";
448         pi_objects[i++] = p_object->i_object_id;
449         vlc_object_release( p_object );
450     }
451
452     /* Build menu */
453     RefreshMenu( p_intf, p_intf->p_sys->p_settings_menu, hMenu, i,
454                  ppsz_varnames, pi_objects, SettingsMenu_Events );
455 }
456
457 /*****************************************************************************
458  * Refresh the menu.
459  *****************************************************************************/
460 void RefreshMenu( intf_thread_t *p_intf, vector<MenuItemExt*> *p_menu_list,
461                   HMENU hMenu , int i_count, char **ppsz_varnames, 
462                   int *pi_objects, int i_start_id )
463 {
464     vlc_object_t *p_object;
465     vlc_bool_t b_section_empty = VLC_FALSE;
466     int i;
467
468     /* Initializations */
469     int i_item_id = i_start_id;
470
471     for( i = 0; i < i_count; i++ )
472     {
473         if( !ppsz_varnames[i] )
474         {
475             if( b_section_empty )
476             {
477                 AppendMenu( hMenu, MF_GRAYED | MF_STRING,
478                             MenuDummy_Event + i, _T("Empty") );
479             }
480
481             AppendMenu( hMenu, MF_SEPARATOR, 0, _T("") );
482             b_section_empty = VLC_TRUE;
483             continue;
484         }
485
486         if( !pi_objects[i] )
487         {
488             AppendMenu( hMenu, MF_GRAYED | MF_STRING,
489                         MenuDummy_Event, _FROMMB(ppsz_varnames[i]) );
490
491             b_section_empty = VLC_FALSE;
492             continue;
493         }
494
495         p_object = (vlc_object_t *)vlc_object_get( p_intf, pi_objects[i] );
496         if( p_object == NULL ) continue;
497
498         b_section_empty = VLC_FALSE;
499         CreateMenuItem( p_intf, p_menu_list, hMenu, ppsz_varnames[i],
500                         p_object, &i_item_id );
501         vlc_object_release( p_object );
502     }
503
504     /* Special case for empty menus */
505     if( wce_GetMenuItemCount(hMenu) == 0 || b_section_empty )
506     {
507         AppendMenu( hMenu, MF_GRAYED | MF_STRING,
508                     MenuDummy_Event + i, _T("Empty") );
509     }
510 }
511
512 /*****************************************************************************
513  * Private methods.
514  *****************************************************************************/
515 void CreateMenuItem( intf_thread_t *p_intf, vector<MenuItemExt*> *p_menu_list,
516                      HMENU hMenu, char *psz_var, vlc_object_t *p_object,
517                      int *pi_item_id )
518 {
519     MenuItemExt *pMenuItemExt;
520     HMENU hMenuItem;
521     vlc_value_t val, text;
522     int i_type;
523
524     /* Check the type of the object variable */
525     i_type = var_Type( p_object, psz_var );
526
527     switch( i_type & VLC_VAR_TYPE )
528     {
529     case VLC_VAR_VOID:
530     case VLC_VAR_BOOL:
531     case VLC_VAR_VARIABLE:
532     case VLC_VAR_STRING:
533     case VLC_VAR_INTEGER:
534     case VLC_VAR_FLOAT:
535         break;
536     default:
537         /* Variable doesn't exist or isn't handled */
538         return;
539     }
540
541     /* Make sure we want to display the variable */
542     if( i_type & VLC_VAR_HASCHOICE )
543     {
544         var_Change( p_object, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
545         if( val.i_int == 0 ) return;
546         if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE && val.i_int == 1 )
547             return;
548     }
549
550     /* Get the descriptive name of the variable */
551     var_Change( p_object, psz_var, VLC_VAR_GETTEXT, &text, NULL );
552
553     var_Get( p_object, psz_var, &val );
554
555     if( i_type & VLC_VAR_HASCHOICE )
556     {
557         hMenuItem = CreateChoicesMenu( p_intf, p_menu_list, psz_var,
558                                        p_object, pi_item_id );
559         AppendMenu( hMenu, MF_STRING | MF_POPUP, (UINT)hMenuItem,
560                     _FROMMB(text.psz_string ? text.psz_string : psz_var) );
561         if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
562         if( text.psz_string ) free( text.psz_string );
563         return;
564     }
565
566     switch( i_type & VLC_VAR_TYPE )
567     {
568     case VLC_VAR_VOID:
569         AppendMenu( hMenu, MF_STRING , ++(*pi_item_id),
570                     _FROMMB(text.psz_string ? text.psz_string : psz_var) );
571         pMenuItemExt = new MenuItemExt( p_intf, *pi_item_id, psz_var,
572                                         p_object->i_object_id, val, i_type );
573         p_menu_list->push_back( pMenuItemExt );
574         break;
575
576     case VLC_VAR_BOOL:
577         val.b_bool = !val.b_bool;
578         AppendMenu( hMenu, MF_STRING | MF_CHECKED, ++(*pi_item_id),
579                     _FROMMB(text.psz_string ? text.psz_string : psz_var) );
580         pMenuItemExt = new MenuItemExt( p_intf, *pi_item_id, psz_var,
581                                         p_object->i_object_id, val, i_type );
582         p_menu_list->push_back( pMenuItemExt );
583         CheckMenuItem( hMenu, *pi_item_id ,
584                        ( val.b_bool ? MF_UNCHECKED : MF_CHECKED ) |
585                        MF_BYCOMMAND ); 
586         break;
587
588     default:
589         if( text.psz_string ) free( text.psz_string );
590         return;
591     }
592
593     if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
594     if( text.psz_string ) free( text.psz_string );
595 }
596
597 HMENU CreateChoicesMenu( intf_thread_t *p_intf,
598                          vector<MenuItemExt*> *p_menu_list, char *psz_var, 
599                          vlc_object_t *p_object, int *pi_item_id )
600 {
601     MenuItemExt *pMenuItemExt;
602     vlc_value_t val, val_list, text_list;
603     int i_type, i;
604     HMENU hSubMenu = CreatePopupMenu();
605
606     /* Check the type of the object variable */
607     i_type = var_Type( p_object, psz_var );
608
609     /* Make sure we want to display the variable */
610     if( i_type & VLC_VAR_HASCHOICE )
611     {
612         var_Change( p_object, psz_var, VLC_VAR_CHOICESCOUNT, &val, NULL );
613         if( val.i_int == 0 ) return NULL;
614         if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE && val.i_int == 1 )
615             return NULL;
616     }
617     else
618     {
619         return NULL;
620     }
621
622     switch( i_type & VLC_VAR_TYPE )
623     {
624     case VLC_VAR_VOID:
625     case VLC_VAR_BOOL:
626     case VLC_VAR_VARIABLE:
627     case VLC_VAR_STRING:
628     case VLC_VAR_INTEGER:
629         break;
630     default:
631         /* Variable doesn't exist or isn't handled */
632         return NULL;
633     }
634
635     if( var_Get( p_object, psz_var, &val ) < 0 ) return NULL;
636
637     if( var_Change( p_object, psz_var, VLC_VAR_GETLIST,
638                     &val_list, &text_list ) < 0 )
639     {
640         if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
641         return NULL;
642     }
643
644     for( i = 0; i < val_list.p_list->i_count; i++ )
645     {
646         vlc_value_t another_val;
647         HMENU hMenuItem;
648         char *psz_tmp;
649
650         switch( i_type & VLC_VAR_TYPE )
651         {
652         case VLC_VAR_VARIABLE:
653             hMenuItem = CreateChoicesMenu( p_intf, p_menu_list,
654               val_list.p_list->p_values[i].psz_string, p_object, pi_item_id );
655             AppendMenu( hSubMenu, MF_STRING | MF_POPUP, (UINT)hMenuItem,
656                         _FROMMB(text_list.p_list->p_values[i].psz_string ?
657                           text_list.p_list->p_values[i].psz_string :
658                           val_list.p_list->p_values[i].psz_string) );
659             break;
660
661         case VLC_VAR_STRING:
662             another_val.psz_string =
663                 strdup(val_list.p_list->p_values[i].psz_string);
664             AppendMenu( hSubMenu, MF_STRING, ++(*pi_item_id),
665                         _FROMMB(text_list.p_list->p_values[i].psz_string ?
666                           text_list.p_list->p_values[i].psz_string :
667                           val_list.p_list->p_values[i].psz_string) );
668             pMenuItemExt = new MenuItemExt( p_intf, *pi_item_id, psz_var,
669                           p_object->i_object_id, another_val, i_type );
670             p_menu_list->push_back( pMenuItemExt );
671
672             if( !(i_type & VLC_VAR_ISCOMMAND) && val.psz_string &&
673                 !strcmp( val.psz_string,
674                          val_list.p_list->p_values[i].psz_string ) )
675               CheckMenuItem( hSubMenu, *pi_item_id, MF_CHECKED | MF_BYCOMMAND);
676             break;
677
678         case VLC_VAR_INTEGER:
679             asprintf( &psz_tmp, "%d", val_list.p_list->p_values[i].i_int );
680             AppendMenu( hSubMenu, MF_STRING, ++(*pi_item_id),
681                         _FROMMB(text_list.p_list->p_values[i].psz_string ?
682                           text_list.p_list->p_values[i].psz_string : psz_tmp));
683             pMenuItemExt = new MenuItemExt( p_intf, *pi_item_id, psz_var,
684                 p_object->i_object_id, val_list.p_list->p_values[i], i_type );
685             p_menu_list->push_back( pMenuItemExt );
686
687             if( val_list.p_list->p_values[i].i_int == val.i_int )
688               CheckMenuItem( hSubMenu, *pi_item_id, MF_CHECKED | MF_BYCOMMAND);
689             break;
690
691         default:
692             break;
693         }
694     }
695
696     /* Clean up everything */
697     if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
698     var_Change( p_object, psz_var, VLC_VAR_FREELIST, &val_list, &text_list );
699
700     return hSubMenu;
701 }
702
703 int wce_GetMenuItemCount(HMENU hMenu)
704 {
705     const int MAX_NUM_ITEMS = 256;
706     int iPos, iCount;
707
708     MENUITEMINFO mii;
709     memset( (char *)&mii, 0, sizeof(MENUITEMINFO) );
710     mii.cbSize = sizeof(MENUITEMINFO);
711
712     iCount = 0;
713     for( iPos = 0; iPos < MAX_NUM_ITEMS; iPos++ )
714     {
715         if( !GetMenuItemInfo(hMenu, (UINT)iPos, TRUE, &mii) ) break;
716         iCount++;
717     }
718
719     return iCount;
720 }
721
722 void OnMenuEvent( intf_thread_t *p_intf, int id )
723 {
724     MenuItemExt *p_menuitemext = NULL;
725     vector<MenuItemExt*>::iterator iter;
726
727     if( p_intf->p_sys->p_settings_menu )
728     for( iter = p_intf->p_sys->p_settings_menu->begin();
729          iter != p_intf->p_sys->p_settings_menu->end(); iter++ )
730         if( (*iter)->id == id )
731         {
732             p_menuitemext = *iter;
733             break;
734         }
735
736     if( p_intf->p_sys->p_audio_menu && !p_menuitemext )
737     for( iter = p_intf->p_sys->p_audio_menu->begin();
738          iter != p_intf->p_sys->p_audio_menu->end(); iter++ ) 
739         if( (*iter)->id == id )
740         {
741             p_menuitemext = *iter;
742             break;
743         }
744
745     if( p_intf->p_sys->p_video_menu && !p_menuitemext )
746     for( iter = p_intf->p_sys->p_video_menu->begin();
747          iter != p_intf->p_sys->p_video_menu->end(); iter++ )
748         if( (*iter)->id == id )
749         {
750             p_menuitemext = *iter;
751             break;
752         }
753
754     if( p_intf->p_sys->p_navig_menu && !p_menuitemext )
755     for( iter = p_intf->p_sys->p_navig_menu->begin();
756          iter != p_intf->p_sys->p_navig_menu->end(); iter++ )
757         if( (*iter)->id == id )
758         {
759             p_menuitemext = *iter;
760             break;
761         }
762
763     if( p_menuitemext )
764     {
765         vlc_object_t *p_object = (vlc_object_t *)
766             vlc_object_get( p_intf, p_menuitemext->i_object_id );
767         if( p_object == NULL ) return;
768
769         var_Set( p_object, p_menuitemext->psz_var, p_menuitemext->val );
770         int i_type = var_Type( p_object, p_menuitemext->psz_var );
771         switch( i_type & VLC_VAR_TYPE )
772         {
773         case VLC_VAR_VOID:
774         case VLC_VAR_BOOL:
775         case VLC_VAR_VARIABLE:
776         case VLC_VAR_STRING:
777         case VLC_VAR_INTEGER:
778             break;
779         default:
780             /* Variable doesn't exist or isn't handled */
781             return;
782         }
783
784         vlc_object_release( p_object );
785     }
786 }
787
788 /*****************************************************************************
789  * A small helper class which encapsulate wxMenuitem with some other useful
790  * things.
791  *****************************************************************************/
792 MenuItemExt::MenuItemExt( intf_thread_t *p_intf, int _id, char *_psz_var,
793                           int _i_object_id, vlc_value_t _val, int _i_val_type )
794 {
795     /* Initializations */
796     id = _id;
797     p_intf = p_intf;
798     psz_var = strdup( _psz_var );
799     i_val_type = _i_val_type;
800     i_object_id = _i_object_id;
801     val = _val;
802 };
803
804 MenuItemExt::~MenuItemExt()
805 {
806     if( psz_var ) free( psz_var );
807     if( ((i_val_type & VLC_VAR_TYPE) == VLC_VAR_STRING)
808         && val.psz_string ) free( val.psz_string );
809 };
810
811 void MenuItemExt::ClearList( vector<MenuItemExt*> *p_menu_list )
812 {
813     vector<MenuItemExt*>::iterator iter;
814
815     if( !p_menu_list ) return;
816     for( iter = p_menu_list->begin(); iter != p_menu_list->end(); iter++ )
817     {
818         delete *iter;
819     }
820     p_menu_list->clear();
821 }