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