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