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