]> git.sesse.net Git - vlc/blob - plugins/gtk/gtk_menu.c
*p_es->p_demux_data is available in p_config->p_demux_data so that the input
[vlc] / plugins / gtk / gtk_menu.c
1 /*****************************************************************************
2  * gtk_menu.c : functions to handle menu items.
3  *****************************************************************************
4  * Copyright (C) 2000, 2001 VideoLAN
5  * $Id: gtk_menu.c,v 1.23 2002/03/14 01:35:28 stef Exp $
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *          Stéphane Borel <stef@via.ecp.fr>
9  *          Johan Bilien <jobi@via.ecp.fr>
10  *      
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  * 
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include <sys/types.h>                                              /* off_t */
30 #include <stdlib.h>
31
32 #include <videolan/vlc.h>
33
34 #ifdef MODULE_NAME_IS_gnome
35 #   include <gnome.h>
36 #else
37 #   include <gtk/gtk.h>
38 #endif
39
40 #include <string.h>
41
42 #include "stream_control.h"
43 #include "input_ext-intf.h"
44
45 #include "interface.h"
46 #include "intf_playlist.h"
47
48 #include "video.h"
49 #include "video_output.h"
50 #include "audio_output.h"
51
52 #include "gtk_callbacks.h"
53 #include "gtk_interface.h"
54 #include "gtk_support.h"
55 #include "gtk_playlist.h"
56 #include "gtk_common.h"
57
58 /*
59  * Local Prototypes
60  */
61 static gint GtkLanguageMenus( gpointer , GtkWidget *, es_descriptor_t *, gint,
62                         void(*pf_toggle )( GtkCheckMenuItem *, gpointer ) );
63
64 void GtkMenubarAudioToggle   ( GtkCheckMenuItem *, gpointer );
65 void GtkPopupAudioToggle     ( GtkCheckMenuItem *, gpointer );
66 void GtkMenubarSubtitleToggle( GtkCheckMenuItem *, gpointer );
67 void GtkPopupSubtitleToggle  ( GtkCheckMenuItem *, gpointer );
68 static gint GtkTitleMenu( gpointer, GtkWidget *, 
69                     void(*pf_toggle )( GtkCheckMenuItem *, gpointer ) );
70 static gint GtkRadioMenu( intf_thread_t *, GtkWidget *, GSList *,
71                           char *, int, int,
72                    void( *pf_toggle )( GtkCheckMenuItem *, gpointer ) );
73
74 gint GtkSetupMenus( intf_thread_t * p_intf );
75
76 /****************************************************************************
77  * Gtk*Toggle: callbacks to toggle the value of a checkmenuitem
78  ****************************************************************************
79  * We need separate functions for menubar and popup here since we can't use
80  * user_data to transmit intf_* and we need to refresh the other menu.
81  ****************************************************************************/
82
83 #define GTKLANGTOGGLE( intf, window, menu, type, callback, b_update )   \
84     intf_thread_t *         p_intf;                                     \
85     GtkWidget *             p_menu;                                     \
86     es_descriptor_t *       p_es;                                       \
87                                                                         \
88     p_intf = GetIntf( GTK_WIDGET(menuitem), (intf) );                   \
89                                                                         \
90     if( !p_intf->p_sys->b_update )                                      \
91     {                                                                   \
92         p_menu = GTK_WIDGET( gtk_object_get_data(                       \
93                    GTK_OBJECT( p_intf->p_sys->window ), (menu) ) );     \
94         p_es = (es_descriptor_t*)user_data;                             \
95                                                                         \
96         input_ToggleES( p_input_bank->pp_input[0],                      \
97                         p_es, menuitem->active );                       \
98                                                                         \
99         p_intf->p_sys->b_update = menuitem->active;                     \
100                                                                         \
101         if( p_intf->p_sys->b_update )                                   \
102         {                                                               \
103             GtkLanguageMenus( p_intf, p_menu, p_es, type, callback );   \
104         }                                                               \
105                                                                         \
106         p_intf->p_sys->b_update = 0;                                    \
107     }
108
109 /*
110  * Audio
111  */ 
112
113 void GtkMenubarAudioToggle( GtkCheckMenuItem * menuitem, gpointer user_data )
114 {
115     GTKLANGTOGGLE( "intf_window", p_popup, "popup_audio", AUDIO_ES,
116                    GtkPopupAudioToggle, b_audio_update );
117 }
118
119 void GtkPopupAudioToggle( GtkCheckMenuItem * menuitem, gpointer user_data )
120 {
121     GTKLANGTOGGLE( "intf_popup", p_window, "menubar_audio", AUDIO_ES,
122                    GtkMenubarAudioToggle, b_audio_update );
123 }
124
125 /* 
126  * Subtitles
127  */ 
128
129 void GtkMenubarSubtitleToggle( GtkCheckMenuItem * menuitem, gpointer user_data )
130 {
131     GTKLANGTOGGLE( "intf_window", p_popup, "popup_subpictures", SPU_ES,
132                    GtkPopupSubtitleToggle, b_spu_update );
133 }
134
135 void GtkPopupSubtitleToggle( GtkCheckMenuItem * menuitem, gpointer user_data )
136 {
137     GTKLANGTOGGLE( "intf_popup", p_window, "menubar_subpictures", SPU_ES,
138                    GtkMenubarSubtitleToggle, b_spu_update );
139 }
140
141 #undef GTKLANGTOGGLE
142
143 /*
144  * Navigation
145  */
146
147 void GtkPopupNavigationToggle( GtkCheckMenuItem * menuitem,
148                                gpointer user_data )
149 {
150     intf_thread_t * p_intf = GetIntf( GTK_WIDGET(menuitem), "intf_popup" );
151
152     if( menuitem->active &&
153         !p_intf->p_sys->b_title_update &&
154         !p_intf->p_sys->b_chapter_update )
155     {
156         input_area_t   *p_area;
157
158         gint i_title   = DATA2TITLE( user_data );
159         gint i_chapter = DATA2CHAPTER( user_data );
160
161         p_area = p_input_bank->pp_input[0]->stream.p_selected_area;
162
163         if( p_area != p_input_bank->pp_input[0]->stream.pp_areas[i_title] )
164         {
165             p_area = p_input_bank->pp_input[0]->stream.pp_areas[i_title];
166             p_intf->p_sys->b_title_update = 1;
167         }
168
169         p_area->i_part = i_chapter;
170
171         input_ChangeArea( p_input_bank->pp_input[0], (input_area_t*)p_area );
172
173         p_intf->p_sys->b_chapter_update = 1;
174         vlc_mutex_lock( &p_input_bank->pp_input[0]->stream.stream_lock );
175         GtkSetupMenus( p_intf );
176         vlc_mutex_unlock( &p_input_bank->pp_input[0]->stream.stream_lock );
177
178         input_SetStatus( p_input_bank->pp_input[0], INPUT_STATUS_PLAY );
179     }
180 }
181
182 /*
183  * Program
184  */
185 #define GTKPROGRAMTOGGLE( intf )                                            \
186     intf_thread_t * p_intf = GetIntf( GTK_WIDGET(menuitem), intf );         \
187                                                                             \
188     if( menuitem->active && !p_intf->p_sys->b_program_update )              \
189     {                                                                       \
190         u16 i_program_id = (ptrdiff_t)user_data;                            \
191                                                                             \
192         input_ChangeProgram( p_input_bank->pp_input[0], i_program_id );     \
193                                                                             \
194         p_intf->p_sys->b_program_update = 1;                                \
195                                                                             \
196         vlc_mutex_lock( &p_input_bank->pp_input[0]->stream.stream_lock );   \
197         GtkSetupMenus( p_intf );                                            \
198         vlc_mutex_unlock( &p_input_bank->pp_input[0]->stream.stream_lock ); \
199                                                                             \
200         p_intf->p_sys->b_program_update = 0;                                \
201                                                                             \
202         input_SetStatus( p_input_bank->pp_input[0], INPUT_STATUS_PLAY );    \
203     }
204
205 void GtkMenubarProgramToggle( GtkCheckMenuItem * menuitem, gpointer user_data )
206 {
207     GTKPROGRAMTOGGLE( "intf_window" );
208 }
209
210 void GtkPopupProgramToggle( GtkCheckMenuItem * menuitem, gpointer user_data )
211 {
212     GTKPROGRAMTOGGLE( "intf_popup" );
213 }
214
215 /*
216  * Title
217  */
218
219 void GtkMenubarTitleToggle( GtkCheckMenuItem * menuitem, gpointer user_data )
220 {
221     intf_thread_t * p_intf = GetIntf( GTK_WIDGET(menuitem), "intf_window" );
222
223     if( menuitem->active && !p_intf->p_sys->b_title_update )
224     {
225         gint i_title = (gint)((long)user_data);
226         input_ChangeArea( p_input_bank->pp_input[0],
227                           p_input_bank->pp_input[0]->stream.pp_areas[i_title] );
228
229         p_intf->p_sys->b_title_update = 1;
230         vlc_mutex_lock( &p_input_bank->pp_input[0]->stream.stream_lock );
231         GtkSetupMenus( p_intf );
232         vlc_mutex_unlock( &p_input_bank->pp_input[0]->stream.stream_lock );
233         p_intf->p_sys->b_title_update = 0;
234
235         input_SetStatus( p_input_bank->pp_input[0], INPUT_STATUS_PLAY );
236
237     }
238 }
239
240 /*
241  * Chapter
242  */
243
244 void GtkMenubarChapterToggle( GtkCheckMenuItem * menuitem, gpointer user_data )
245 {
246     intf_thread_t * p_intf;
247     input_area_t *  p_area;
248     gint            i_chapter;
249     GtkWidget *     p_popup_menu;
250
251     p_intf    = GetIntf( GTK_WIDGET(menuitem), "intf_window" );
252     p_area    = p_input_bank->pp_input[0]->stream.p_selected_area;
253     i_chapter = (gint)((long)user_data);
254
255     if( menuitem->active && !p_intf->p_sys->b_chapter_update )
256     {
257         p_area->i_part = i_chapter;
258         input_ChangeArea( p_input_bank->pp_input[0], (input_area_t*)p_area );
259
260         p_intf->p_sys->b_chapter_update = 1;
261         p_popup_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT( 
262                              p_intf->p_sys->p_popup ), "popup_navigation" ) );
263
264         vlc_mutex_lock( &p_input_bank->pp_input[0]->stream.stream_lock );
265         GtkTitleMenu( p_intf, p_popup_menu, GtkPopupNavigationToggle );
266         vlc_mutex_unlock( &p_input_bank->pp_input[0]->stream.stream_lock );
267
268         p_intf->p_sys->b_chapter_update = 0;
269
270         input_SetStatus( p_input_bank->pp_input[0], INPUT_STATUS_PLAY );
271     }
272 }
273
274
275 /****************************************************************************
276  * Functions to generate menus
277  ****************************************************************************/
278
279 /*****************************************************************************
280  * GtkRadioMenu: update interactive menus of the interface
281  *****************************************************************************
282  * Sets up menus with information from input
283  * Warning: since this function is designed to be called by management
284  * function, the interface lock has to be taken
285  *****************************************************************************/
286 static gint GtkRadioMenu( intf_thread_t * p_intf,
287                             GtkWidget * p_root, GSList * p_menu_group,
288                             char * psz_item_name,
289                             int i_nb, int i_selected,
290                      void( *pf_toggle )( GtkCheckMenuItem *, gpointer ) )
291 {
292     char                psz_name[ GTK_MENU_LABEL_SIZE ];
293     GtkWidget *         p_menu;
294     GtkWidget *         p_submenu;
295     GtkWidget *         p_item_group;
296     GtkWidget *         p_item;
297     GtkWidget *         p_item_selected;
298     GSList *            p_group;
299     gint                i_item;
300
301     /* temporary hack to avoid blank menu when an open menu is removed */
302     if( GTK_MENU_ITEM(p_root)->submenu != NULL )
303     {
304         gtk_menu_popdown( GTK_MENU( GTK_MENU_ITEM(p_root)->submenu ) );
305     }
306     /* removes previous menu */
307     gtk_menu_item_remove_submenu( GTK_MENU_ITEM( p_root ) );
308     gtk_widget_set_sensitive( p_root, FALSE );
309
310     p_item_group = NULL;
311     p_submenu = NULL;
312     p_item_selected = NULL;
313     p_group = p_menu_group;
314
315     p_menu = gtk_menu_new();
316
317     for( i_item = 0 ; i_item < i_nb ; i_item++ )
318     {
319         /* we group chapters in packets of ten for small screens */
320         if( ( i_item % 10 == 0 ) && ( i_nb > 20 ) )
321         {
322             if( i_item != 0 )
323             {
324                 gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_item_group ),
325                                            p_submenu );
326                 gtk_menu_append( GTK_MENU( p_menu ), p_item_group );
327             }
328
329             snprintf( psz_name, GTK_MENU_LABEL_SIZE,
330                       "%ss %d to %d", psz_item_name, i_item + 1, i_item + 10);
331             psz_name[ GTK_MENU_LABEL_SIZE - 1 ] = '\0';
332             p_item_group = gtk_menu_item_new_with_label( psz_name );
333             gtk_widget_show( p_item_group );
334             p_submenu = gtk_menu_new();
335         }
336
337         snprintf( psz_name, GTK_MENU_LABEL_SIZE, "%s %d",
338                   psz_item_name, i_item + 1 );
339         psz_name[ GTK_MENU_LABEL_SIZE - 1 ] = '\0';
340
341         p_item = gtk_radio_menu_item_new_with_label( p_group, psz_name );
342         p_group = gtk_radio_menu_item_group( GTK_RADIO_MENU_ITEM( p_item ) );
343
344         if( i_selected == i_item + 1 )
345         {
346             p_item_selected = p_item;
347         }
348         
349         gtk_widget_show( p_item );
350
351         /* setup signal hanling */
352         gtk_signal_connect( GTK_OBJECT( p_item ),
353                             "toggled",
354                             GTK_SIGNAL_FUNC( pf_toggle ),
355                             (gpointer)((long)(i_item + 1)) );
356
357         if( i_nb > 20 )
358         {
359             gtk_menu_append( GTK_MENU( p_submenu ), p_item );
360         }
361         else
362         {
363             gtk_menu_append( GTK_MENU( p_menu ), p_item );
364         }
365     }
366
367     if( i_nb > 20 )
368     {
369         gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_item_group ), p_submenu );
370         gtk_menu_append( GTK_MENU( p_menu ), p_item_group );
371     }
372
373     /* link the new menu to the title menu item */
374     gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_root ), p_menu );
375
376     /* toggle currently selected chapter
377      * We have to release the lock since input_ToggleES needs it */
378     if( p_item_selected != NULL )
379     {
380         gtk_check_menu_item_set_active( GTK_CHECK_MENU_ITEM( p_item_selected ),
381                                         TRUE );
382     }
383
384     /* be sure that menu is sensitive, if there are several items */
385     if( i_nb > 1 )
386     {
387         gtk_widget_set_sensitive( p_root, TRUE );
388     }
389
390     return TRUE;
391 }
392
393 /*****************************************************************************
394  * GtkProgramMenu: update the programs menu of the interface 
395  *****************************************************************************
396  * Builds the program menu according to what have been found in the PAT 
397  * by the input. Usefull for multi-programs streams such as DVB ones.
398  *****************************************************************************/
399 static gint GtkProgramMenu( gpointer            p_data,
400                             GtkWidget *         p_root,
401                             pgrm_descriptor_t * p_pgrm,
402                       void(*pf_toggle )( GtkCheckMenuItem *, gpointer ) )
403 {
404     intf_thread_t *     p_intf;
405     GtkWidget *         p_menu;
406     GtkWidget *         p_item;
407     GtkWidget *         p_item_active;
408     GSList *            p_group;
409     char                psz_name[ GTK_MENU_LABEL_SIZE ];
410     gint                i;
411
412     /* cast */
413     p_intf = (intf_thread_t *)p_data;
414
415     /* temporary hack to avoid blank menu when an open menu is removed */
416     if( GTK_MENU_ITEM(p_root)->submenu != NULL )
417     {
418         gtk_menu_popdown( GTK_MENU( GTK_MENU_ITEM(p_root)->submenu ) );
419     }
420     /* removes previous menu */
421     gtk_menu_item_remove_submenu( GTK_MENU_ITEM( p_root ) );
422     gtk_widget_set_sensitive( p_root, FALSE );
423
424     p_group = NULL;
425
426     /* menu container */
427     p_menu = gtk_menu_new();
428
429     p_item_active = NULL;
430
431     /* create a set of program buttons and append them to the container */
432     for( i = 0 ; i < p_input_bank->pp_input[0]->stream.i_pgrm_number ; i++ )
433     {
434         snprintf( psz_name, GTK_MENU_LABEL_SIZE, "id %d",
435             p_input_bank->pp_input[0]->stream.pp_programs[i]->i_number );
436         psz_name[GTK_MENU_LABEL_SIZE-1] = '\0';
437             
438         p_item = gtk_radio_menu_item_new_with_label( p_group, psz_name );
439         p_group =
440             gtk_radio_menu_item_group( GTK_RADIO_MENU_ITEM( p_item ) );
441
442         if( p_pgrm == p_input_bank->pp_input[0]->stream.pp_programs[i] )
443         {
444             /* don't lose p_item when we append into menu */
445             p_item_active = p_item;
446         }
447
448         gtk_widget_show( p_item );
449
450         /* setup signal hanling */
451         gtk_signal_connect( GTK_OBJECT( p_item ), "toggled",
452                         GTK_SIGNAL_FUNC( pf_toggle ),
453                         (gpointer)(ptrdiff_t)( p_input_bank->pp_input[0]->
454                         stream.pp_programs[i]->i_number ) );
455
456         gtk_menu_append( GTK_MENU( p_menu ), p_item );
457     }
458
459     /* link the new menu to the menubar item */
460     gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_root ), p_menu );
461
462     /* activation will call signals so we can only do it
463      * when submenu is attached to menu - to get intf_window 
464      * We have to release the lock since input_ToggleES needs it */
465     if( p_item_active != NULL )
466     {
467         gtk_check_menu_item_set_active( GTK_CHECK_MENU_ITEM( p_item_active ),
468                                         TRUE );
469     }
470
471     /* be sure that menu is sensitive if more than 1 program */
472     if( p_input_bank->pp_input[0]->stream.i_pgrm_number > 1 )
473     {
474         gtk_widget_set_sensitive( p_root, TRUE );
475     }
476
477     return TRUE;
478 }
479
480 /*****************************************************************************
481  * GtkLanguageMenus: update interactive menus of the interface
482  *****************************************************************************
483  * Sets up menus with information from input:
484  *  -languages
485  *  -sub-pictures
486  * Warning: since this function is designed to be called by management
487  * function, the interface lock has to be taken
488  *****************************************************************************/
489 static gint GtkLanguageMenus( gpointer          p_data,
490                               GtkWidget *       p_root,
491                               es_descriptor_t * p_es,
492                               gint              i_cat,
493                         void(*pf_toggle )( GtkCheckMenuItem *, gpointer ) )
494 {
495     intf_thread_t *     p_intf;
496     GtkWidget *         p_menu;
497     GtkWidget *         p_separator;
498     GtkWidget *         p_item;
499     GtkWidget *         p_item_active;
500     GSList *            p_group;
501     char                psz_name[ GTK_MENU_LABEL_SIZE ];
502     gint                i_item;
503     gint                i;
504
505     
506
507     /* cast */
508     p_intf = (intf_thread_t *)p_data;
509
510     /* temporary hack to avoid blank menu when an open menu is removed */
511     if( GTK_MENU_ITEM(p_root)->submenu != NULL )
512     {
513         gtk_menu_popdown( GTK_MENU( GTK_MENU_ITEM(p_root)->submenu ) );
514     }
515     /* removes previous menu */
516     gtk_menu_item_remove_submenu( GTK_MENU_ITEM( p_root ) );
517     gtk_widget_set_sensitive( p_root, FALSE );
518
519     p_group = NULL;
520
521     /* menu container */
522     p_menu = gtk_menu_new();
523
524     /* special case for "off" item */
525     snprintf( psz_name, GTK_MENU_LABEL_SIZE, "None" );
526     psz_name[ GTK_MENU_LABEL_SIZE - 1 ] = '\0';
527
528     p_item = gtk_radio_menu_item_new_with_label( p_group, psz_name );
529     p_group = gtk_radio_menu_item_group( GTK_RADIO_MENU_ITEM( p_item ) );
530
531     gtk_widget_show( p_item );
532
533     /* signal hanling for off */
534     gtk_signal_connect( GTK_OBJECT( p_item ), "toggled",
535                         GTK_SIGNAL_FUNC ( pf_toggle ), NULL );
536
537     gtk_menu_append( GTK_MENU( p_menu ), p_item );
538
539     p_separator = gtk_menu_item_new();
540     gtk_widget_set_sensitive( p_separator, FALSE );
541     gtk_widget_show( p_separator );
542     gtk_menu_append( GTK_MENU( p_menu ), p_separator );
543
544     p_item_active = NULL;
545     i_item = 0;
546
547     vlc_mutex_lock( &p_input_bank->pp_input[0]->stream.stream_lock );
548
549 #define ES p_input_bank->pp_input[0]->stream.pp_es[i]
550     /* create a set of language buttons and append them to the container */
551     for( i = 0 ; i < p_input_bank->pp_input[0]->stream.i_es_number ; i++ )
552     {
553         if( ( ES->i_cat == i_cat ) &&
554             ( !ES->p_pgrm ||
555               ES->p_pgrm ==
556                  p_input_bank->pp_input[0]->stream.p_selected_program ) )
557         {
558             i_item++;
559             strcpy( psz_name,
560                     p_input_bank->pp_input[0]->stream.pp_es[i]->psz_desc );
561             if( psz_name[0] == '\0' )
562             {
563                 snprintf( psz_name, GTK_MENU_LABEL_SIZE,
564                           "Language %d", i_item );
565                 psz_name[ GTK_MENU_LABEL_SIZE - 1 ] = '\0';
566             }
567
568             p_item = gtk_radio_menu_item_new_with_label( p_group, psz_name );
569             p_group =
570                 gtk_radio_menu_item_group( GTK_RADIO_MENU_ITEM( p_item ) );
571
572             if( p_es == p_input_bank->pp_input[0]->stream.pp_es[i] )
573             {
574                 /* don't lose p_item when we append into menu */
575                 p_item_active = p_item;
576             }
577
578             gtk_widget_show( p_item );
579
580             /* setup signal hanling */
581             gtk_signal_connect( GTK_OBJECT( p_item ), "toggled",
582                             GTK_SIGNAL_FUNC( pf_toggle ),
583                             (gpointer)( p_input_bank->pp_input[0]->stream.pp_es[i] ) );
584
585             gtk_menu_append( GTK_MENU( p_menu ), p_item );
586         }
587     }
588
589     vlc_mutex_unlock( &p_input_bank->pp_input[0]->stream.stream_lock );
590
591     /* link the new menu to the menubar item */
592     gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_root ), p_menu );
593
594     /* acitvation will call signals so we can only do it
595      * when submenu is attached to menu - to get intf_window 
596      * We have to release the lock since input_ToggleES needs it */
597     if( p_item_active != NULL )
598     {
599         gtk_check_menu_item_set_active( GTK_CHECK_MENU_ITEM( p_item_active ),
600                                         TRUE );
601     }
602
603     /* be sure that menu is sensitive if non empty */
604     if( i_item > 0 )
605     {
606         gtk_widget_set_sensitive( p_root, TRUE );
607     }
608
609     return TRUE;
610 }
611
612 /*****************************************************************************
613  * GtkTitleMenu: sets menus for titles and chapters selection
614  *****************************************************************************
615  * Generates two types of menus:
616  *  -simple list of titles
617  *  -cascaded lists of chapters for each title
618  *****************************************************************************/
619 static gint GtkTitleMenu( gpointer       p_data,
620                             GtkWidget *    p_navigation, 
621                             void(*pf_toggle )( GtkCheckMenuItem *, gpointer ) )
622 {
623     intf_thread_t *     p_intf;
624     char                psz_name[ GTK_MENU_LABEL_SIZE ];
625     GtkWidget *         p_title_menu;
626     GtkWidget *         p_title_submenu;
627     GtkWidget *         p_title_item;
628     GtkWidget *         p_item_active;
629     GtkWidget *         p_chapter_menu;
630     GtkWidget *         p_chapter_submenu;
631     GtkWidget *         p_title_menu_item;
632     GtkWidget *         p_chapter_menu_item;
633     GtkWidget *         p_item;
634     GSList *            p_title_group;
635     GSList *            p_chapter_group;
636     gint                i_title;
637     gint                i_chapter;
638     gint                i_title_nb;
639     gint                i_chapter_nb;
640
641     /* cast */
642     p_intf = (intf_thread_t*)p_data;
643
644     /* temporary hack to avoid blank menu when an open menu is removed */
645     if( GTK_MENU_ITEM(p_navigation)->submenu != NULL )
646     {
647         gtk_menu_popdown( GTK_MENU( GTK_MENU_ITEM(p_navigation)->submenu ) );
648     }
649     /* removes previous menu */
650     gtk_menu_item_remove_submenu( GTK_MENU_ITEM( p_navigation ) );
651     gtk_widget_set_sensitive( p_navigation, FALSE );
652
653     p_title_menu = gtk_menu_new();
654     p_title_group = NULL;
655     p_title_submenu = NULL;
656     p_title_menu_item = NULL;
657     p_chapter_group = NULL;
658     p_chapter_submenu = NULL;
659     p_chapter_menu_item = NULL;
660     p_item_active = NULL;
661     i_title_nb = p_input_bank->pp_input[0]->stream.i_area_nb;
662
663     /* loop on titles */
664     for( i_title = 1 ; i_title < i_title_nb ; i_title++ )
665     {
666         /* we group titles in packets of ten for small screens */
667         if( ( i_title % 10 == 1 ) && ( i_title_nb > 20 ) )
668         {
669             if( i_title != 1 )
670             {
671                 gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_title_menu_item ),
672                                            p_title_submenu );
673                 gtk_menu_append( GTK_MENU( p_title_menu ), p_title_menu_item );
674             }
675
676             snprintf( psz_name, GTK_MENU_LABEL_SIZE,
677                       "%d - %d", i_title, i_title + 9 );
678             psz_name[ GTK_MENU_LABEL_SIZE - 1 ] = '\0';
679             p_title_menu_item = gtk_menu_item_new_with_label( psz_name );
680             gtk_widget_show( p_title_menu_item );
681             p_title_submenu = gtk_menu_new();
682         }
683
684         snprintf( psz_name, GTK_MENU_LABEL_SIZE, "Title %d (%d)", i_title,
685                   p_input_bank->pp_input[0]->stream.pp_areas[i_title]->i_part_nb );
686         psz_name[ GTK_MENU_LABEL_SIZE - 1 ] = '\0';
687 #if 0
688         if( pf_toggle == on_menubar_title_toggle )
689         {
690             p_title_item = gtk_radio_menu_item_new_with_label( p_title_group,
691                                                            psz_name );
692             p_title_group =
693               gtk_radio_menu_item_group( GTK_RADIO_MENU_ITEM( p_title_item ) );
694
695             if( p_input_bank->pp_input[0]->stream.pp_areas[i_title] ==
696                          p_input_bank->pp_input[0]->stream.p_selected_area )
697             {
698                 p_item_active = p_title_item;
699             }
700
701             /* setup signal hanling */
702             gtk_signal_connect( GTK_OBJECT( p_title_item ),
703                      "toggled",
704                      GTK_SIGNAL_FUNC( pf_toggle ),
705                      (gpointer)(p_input_bank->pp_input[0]->stream.pp_areas[i_title]) );
706
707             if( p_input_bank->pp_input[0]->stream.i_area_nb > 1 )
708             {
709                 /* be sure that menu is sensitive */
710                 gtk_widget_set_sensitive( p_navigation, TRUE );
711             }
712         }
713         else
714 #endif
715         {
716             p_title_item = gtk_menu_item_new_with_label( psz_name );
717
718 #if 1    
719             p_chapter_menu = gtk_menu_new();
720             i_chapter_nb =
721                     p_input_bank->pp_input[0]->stream.pp_areas[i_title]->i_part_nb;
722     
723             for( i_chapter = 0 ; i_chapter < i_chapter_nb ; i_chapter++ )
724             {
725                 /* we group chapters in packets of ten for small screens */
726                 if( ( i_chapter % 10 == 0 ) && ( i_chapter_nb > 20 ) )
727                 {
728                     if( i_chapter != 0 )
729                     {
730                         gtk_menu_item_set_submenu(
731                                     GTK_MENU_ITEM( p_chapter_menu_item ),
732                                     p_chapter_submenu );
733                         gtk_menu_append( GTK_MENU( p_chapter_menu ),
734                                          p_chapter_menu_item );
735                     }
736
737                     snprintf( psz_name, GTK_MENU_LABEL_SIZE,
738                               "%d - %d", i_chapter + 1, i_chapter + 10 );
739                     psz_name[ GTK_MENU_LABEL_SIZE - 1 ] = '\0';
740                     p_chapter_menu_item =
741                             gtk_menu_item_new_with_label( psz_name );
742                     gtk_widget_show( p_chapter_menu_item );
743                     p_chapter_submenu = gtk_menu_new();
744                 }
745
746                 snprintf( psz_name, GTK_MENU_LABEL_SIZE,
747                           "Chapter %d", i_chapter + 1 );
748                 psz_name[ GTK_MENU_LABEL_SIZE - 1 ] = '\0';
749     
750                 p_item = gtk_radio_menu_item_new_with_label(
751                                                 p_chapter_group, psz_name );
752                 p_chapter_group = gtk_radio_menu_item_group(
753                                                 GTK_RADIO_MENU_ITEM( p_item ) );
754                 gtk_widget_show( p_item );
755
756 #define p_area p_input_bank->pp_input[0]->stream.pp_areas[i_title]
757                 if( ( p_area ==
758                         p_input_bank->pp_input[0]->stream.p_selected_area ) &&
759                     ( p_area->i_part == i_chapter + 1 ) )
760                 {
761                     p_item_active = p_item;
762                 }
763 #undef p_area
764
765                 /* setup signal hanling */
766                 gtk_signal_connect( GTK_OBJECT( p_item ),
767                            "toggled",
768                            GTK_SIGNAL_FUNC( pf_toggle ),
769                            (gpointer)POS2DATA( i_title, i_chapter + 1) );
770
771                 if( i_chapter_nb > 20 )
772                 {
773                     gtk_menu_append( GTK_MENU( p_chapter_submenu ), p_item );
774                 }
775                 else
776                 {
777                     gtk_menu_append( GTK_MENU( p_chapter_menu ), p_item );
778                 }
779             }
780
781             if( i_chapter_nb > 20 )
782             {
783                 gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_chapter_menu_item ),
784                                            p_chapter_submenu );
785                 gtk_menu_append( GTK_MENU( p_chapter_menu ),
786                                  p_chapter_menu_item );
787             }
788
789             /* link the new menu to the title menu item */
790             gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_title_item ),
791                                        p_chapter_menu );
792
793             if( p_input_bank->pp_input[0]->stream.pp_areas[i_title]->i_part_nb > 1 )
794             {
795                 /* be sure that menu is sensitive */
796                 gtk_widget_set_sensitive( p_navigation, TRUE );
797             }
798 #else
799             GtkRadioMenu( p_intf, p_title_item, p_chapter_group, "Chapter",
800                 p_input_bank->pp_input[0]->stream.pp_areas[i_title]->i_part_nb,
801                 i_title * 100,
802                 p_input_bank->pp_input[0]->stream.p_selected_area->i_part +
803                 p_input_bank->pp_input[0]->stream.p_selected_area->i_id *100,
804                 pf_toggle );
805
806 #endif
807         }
808         gtk_widget_show( p_title_item );
809
810         if( i_title_nb > 20 )
811         {
812             gtk_menu_append( GTK_MENU( p_title_submenu ), p_title_item );
813         }
814         else
815         {
816             gtk_menu_append( GTK_MENU( p_title_menu ), p_title_item );
817         }
818     }
819
820     if( i_title_nb > 20 )
821     {
822         gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_title_menu_item ),
823                                    p_title_submenu );
824         gtk_menu_append( GTK_MENU( p_title_menu ), p_title_menu_item );
825     }
826
827     /* be sure that menu is sensitive */
828     gtk_widget_set_sensitive( p_title_menu, TRUE );
829
830     /* link the new menu to the menubar item */
831     gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_navigation ), p_title_menu );
832
833     /* Default selected chapter
834      * We have to release the lock since input_ToggleES needs it */
835     if( p_item_active != NULL )
836     {
837         gtk_check_menu_item_set_active( GTK_CHECK_MENU_ITEM( p_item_active ),
838                                         TRUE );
839     }
840 #if 0
841     if( p_input_bank->pp_input[0]->stream.i_area_nb > 1 )
842     {
843         /* be sure that menu is sensitive */
844         gtk_widget_set_sensitive( p_navigation, TRUE );
845     }
846 #endif
847
848     return TRUE;
849 }
850
851 /*****************************************************************************
852  * GtkSetupMenus: function that generates title/chapter/audio/subpic
853  * menus with help from preceding functions
854  *****************************************************************************
855  * Function called with the lock on stream
856  *****************************************************************************/
857 gint GtkSetupMenus( intf_thread_t * p_intf )
858 {
859     es_descriptor_t *   p_audio_es;
860     es_descriptor_t *   p_spu_es;
861     GtkWidget *         p_menubar_menu;
862     GtkWidget *         p_popup_menu;
863     gint                i;
864
865     p_intf->p_sys->b_chapter_update |= p_intf->p_sys->b_title_update;
866     p_intf->p_sys->b_audio_update |= p_intf->p_sys->b_title_update |
867                                      p_intf->p_sys->b_program_update;
868     p_intf->p_sys->b_spu_update |= p_intf->p_sys->b_title_update |
869                                    p_intf->p_sys->b_program_update;
870
871     if( p_intf->p_sys->b_program_update )
872     { 
873         pgrm_descriptor_t * p_pgrm;
874         
875         if( p_input_bank->pp_input[0]->stream.p_new_program )
876         {
877             p_pgrm = p_input_bank->pp_input[0]->stream.p_new_program;
878         }
879         else
880         {
881             p_pgrm = p_input_bank->pp_input[0]->stream.p_selected_program;
882         }
883
884         p_menubar_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT( 
885                             p_intf->p_sys->p_window ), "menubar_program" ) );
886         GtkProgramMenu( p_intf, p_menubar_menu, p_pgrm,
887                         GtkMenubarProgramToggle );
888         
889         p_intf->p_sys->b_program_update = 1;
890         p_popup_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT( 
891                             p_intf->p_sys->p_popup ), "popup_program" ) );
892         GtkProgramMenu( p_intf, p_popup_menu, p_pgrm,
893                         GtkPopupProgramToggle );
894
895         p_intf->p_sys->b_program_update = 0;
896     }
897     
898     if( p_intf->p_sys->b_title_update )
899     { 
900         char            psz_title[5];
901
902         p_menubar_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT( 
903                             p_intf->p_sys->p_window ), "menubar_title" ) );
904         GtkRadioMenu( p_intf, p_menubar_menu, NULL, "Title",
905                       p_input_bank->pp_input[0]->stream.i_area_nb - 1,
906                       p_input_bank->pp_input[0]->stream.p_selected_area->i_id,
907                       GtkMenubarTitleToggle );
908
909         snprintf( psz_title, 4, "%d",
910                   p_input_bank->pp_input[0]->stream.p_selected_area->i_id );
911         psz_title[ 4 ] = '\0';
912         gtk_label_set_text( p_intf->p_sys->p_label_title, psz_title );
913
914         p_intf->p_sys->b_title_update = 0;
915     }
916
917     if( p_intf->p_sys->b_chapter_update )
918     {
919         char            psz_chapter[5];
920
921         p_popup_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT( 
922                              p_intf->p_sys->p_popup ), "popup_navigation" ) );
923         GtkTitleMenu( p_intf, p_popup_menu, GtkPopupNavigationToggle );
924 #if 0
925         GtkRadioMenu( p_intf, p_menubar_menu, NULL, "Title",
926                         p_input_bank->pp_input[0]->stream.i_area_nb - 1,
927                         p_input_bank->pp_input[0]->stream.p_selected_area->i_id,
928                         on_menubar_chapter_toggle );
929 #endif
930
931         p_menubar_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT( 
932                              p_intf->p_sys->p_window ), "menubar_chapter" ) );
933
934         GtkRadioMenu( p_intf, p_menubar_menu, NULL, "Chapter",
935                         p_input_bank->pp_input[0]->stream.p_selected_area->i_part_nb,
936                         p_input_bank->pp_input[0]->stream.p_selected_area->i_part,
937                         GtkMenubarChapterToggle );
938
939
940         snprintf( psz_chapter, 4, "%d", 
941                   p_input_bank->pp_input[0]->stream.p_selected_area->i_part );
942         psz_chapter[ 4 ] = '\0';
943         gtk_label_set_text( p_intf->p_sys->p_label_chapter, psz_chapter );
944
945         p_intf->p_sys->i_part =
946                 p_input_bank->pp_input[0]->stream.p_selected_area->i_part;
947
948         p_intf->p_sys->b_chapter_update = 0;
949     }
950
951     /* look for selected ES */
952     p_audio_es = NULL;
953     p_spu_es = NULL;
954
955     for( i = 0 ; i < p_input_bank->pp_input[0]->stream.i_selected_es_number ; i++ )
956     {
957         if( p_input_bank->pp_input[0]->stream.pp_selected_es[i]->i_cat == AUDIO_ES )
958         {
959             p_audio_es = p_input_bank->pp_input[0]->stream.pp_selected_es[i];
960         }
961
962         if( p_input_bank->pp_input[0]->stream.pp_selected_es[i]->i_cat == SPU_ES )
963         {
964             p_spu_es = p_input_bank->pp_input[0]->stream.pp_selected_es[i];
965         }
966     }
967
968     vlc_mutex_unlock( &p_input_bank->pp_input[0]->stream.stream_lock );
969
970     /* audio menus */
971     if( p_intf->p_sys->b_audio_update )
972     {
973         /* find audio root menu */
974         p_menubar_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
975                              p_intf->p_sys->p_window ), "menubar_audio" ) );
976     
977         p_popup_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT( 
978                      p_intf->p_sys->p_popup ), "popup_audio" ) );
979     
980         p_intf->p_sys->b_audio_update = 1;
981         GtkLanguageMenus( p_intf, p_menubar_menu, p_audio_es, AUDIO_ES,
982                             GtkMenubarAudioToggle );
983         p_intf->p_sys->b_audio_update = 1;
984         GtkLanguageMenus( p_intf, p_popup_menu, p_audio_es, AUDIO_ES,
985                             GtkPopupAudioToggle );
986     
987         p_intf->p_sys->b_audio_update = 0;
988     }
989     
990     /* sub picture menus */
991     if( p_intf->p_sys->b_spu_update )
992     {
993         /* find spu root menu */
994         p_menubar_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
995                           p_intf->p_sys->p_window ), "menubar_subpictures" ) );
996     
997         p_popup_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT( 
998                      p_intf->p_sys->p_popup ), "popup_subpictures" ) );
999     
1000         p_intf->p_sys->b_spu_update = 1;
1001         GtkLanguageMenus( p_intf, p_menubar_menu, p_spu_es, SPU_ES,
1002                             GtkMenubarSubtitleToggle  );
1003         p_intf->p_sys->b_spu_update = 1;
1004         GtkLanguageMenus( p_intf, p_popup_menu, p_spu_es, SPU_ES,
1005                             GtkPopupSubtitleToggle );
1006     
1007         p_intf->p_sys->b_spu_update = 0;
1008     }
1009
1010     vlc_mutex_lock( &p_input_bank->pp_input[0]->stream.stream_lock );
1011
1012     return TRUE;
1013 }
1014