]> git.sesse.net Git - vlc/blob - plugins/gtk/gtk_menu.c
*Removed an occurance of former angle item in gtk.
[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.22 2002/03/06 01:20:56 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     char            psz_chapter[5];
250     GtkWidget *     p_popup_menu;
251
252     p_intf    = GetIntf( GTK_WIDGET(menuitem), "intf_window" );
253     p_area    = p_input_bank->pp_input[0]->stream.p_selected_area;
254     i_chapter = (gint)((long)user_data);
255
256     if( menuitem->active && !p_intf->p_sys->b_chapter_update )
257     {
258         p_area->i_part = i_chapter;
259         input_ChangeArea( p_input_bank->pp_input[0], (input_area_t*)p_area );
260
261         snprintf( psz_chapter, 4, "%02d", p_area->i_part );
262         psz_chapter[ 4 ] = '\0';
263         gtk_label_set_text( p_intf->p_sys->p_label_chapter, psz_chapter );
264
265         p_intf->p_sys->b_chapter_update = 1;
266         p_popup_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT( 
267                              p_intf->p_sys->p_popup ), "popup_navigation" ) );
268
269         vlc_mutex_lock( &p_input_bank->pp_input[0]->stream.stream_lock );
270         GtkTitleMenu( p_intf, p_popup_menu, GtkPopupNavigationToggle );
271         vlc_mutex_unlock( &p_input_bank->pp_input[0]->stream.stream_lock );
272
273         p_intf->p_sys->b_chapter_update = 0;
274
275         input_SetStatus( p_input_bank->pp_input[0], INPUT_STATUS_PLAY );
276     }
277 }
278
279
280 /****************************************************************************
281  * Functions to generate menus
282  ****************************************************************************/
283
284 /*****************************************************************************
285  * GtkRadioMenu: update interactive menus of the interface
286  *****************************************************************************
287  * Sets up menus with information from input
288  * Warning: since this function is designed to be called by management
289  * function, the interface lock has to be taken
290  *****************************************************************************/
291 static gint GtkRadioMenu( intf_thread_t * p_intf,
292                             GtkWidget * p_root, GSList * p_menu_group,
293                             char * psz_item_name,
294                             int i_nb, int i_selected,
295                      void( *pf_toggle )( GtkCheckMenuItem *, gpointer ) )
296 {
297     char                psz_name[ GTK_MENU_LABEL_SIZE ];
298     GtkWidget *         p_menu;
299     GtkWidget *         p_submenu;
300     GtkWidget *         p_item_group;
301     GtkWidget *         p_item;
302     GtkWidget *         p_item_selected;
303     GSList *            p_group;
304     gint                i_item;
305
306     /* temporary hack to avoid blank menu when an open menu is removed */
307     if( GTK_MENU_ITEM(p_root)->submenu != NULL )
308     {
309         gtk_menu_popdown( GTK_MENU( GTK_MENU_ITEM(p_root)->submenu ) );
310     }
311     /* removes previous menu */
312     gtk_menu_item_remove_submenu( GTK_MENU_ITEM( p_root ) );
313     gtk_widget_set_sensitive( p_root, FALSE );
314
315     p_item_group = NULL;
316     p_submenu = NULL;
317     p_item_selected = NULL;
318     p_group = p_menu_group;
319
320     p_menu = gtk_menu_new();
321
322     for( i_item = 0 ; i_item < i_nb ; i_item++ )
323     {
324         /* we group chapters in packets of ten for small screens */
325         if( ( i_item % 10 == 0 ) && ( i_nb > 20 ) )
326         {
327             if( i_item != 0 )
328             {
329                 gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_item_group ),
330                                            p_submenu );
331                 gtk_menu_append( GTK_MENU( p_menu ), p_item_group );
332             }
333
334             snprintf( psz_name, GTK_MENU_LABEL_SIZE,
335                       "%ss %d to %d", psz_item_name, i_item + 1, i_item + 10);
336             psz_name[ GTK_MENU_LABEL_SIZE - 1 ] = '\0';
337             p_item_group = gtk_menu_item_new_with_label( psz_name );
338             gtk_widget_show( p_item_group );
339             p_submenu = gtk_menu_new();
340         }
341
342         snprintf( psz_name, GTK_MENU_LABEL_SIZE, "%s %d",
343                   psz_item_name, i_item + 1 );
344         psz_name[ GTK_MENU_LABEL_SIZE - 1 ] = '\0';
345
346         p_item = gtk_radio_menu_item_new_with_label( p_group, psz_name );
347         p_group = gtk_radio_menu_item_group( GTK_RADIO_MENU_ITEM( p_item ) );
348
349         if( i_selected == i_item + 1 )
350         {
351             p_item_selected = p_item;
352         }
353         
354         gtk_widget_show( p_item );
355
356         /* setup signal hanling */
357         gtk_signal_connect( GTK_OBJECT( p_item ),
358                             "toggled",
359                             GTK_SIGNAL_FUNC( pf_toggle ),
360                             (gpointer)((long)(i_item + 1)) );
361
362         if( i_nb > 20 )
363         {
364             gtk_menu_append( GTK_MENU( p_submenu ), p_item );
365         }
366         else
367         {
368             gtk_menu_append( GTK_MENU( p_menu ), p_item );
369         }
370     }
371
372     if( i_nb > 20 )
373     {
374         gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_item_group ), p_submenu );
375         gtk_menu_append( GTK_MENU( p_menu ), p_item_group );
376     }
377
378     /* link the new menu to the title menu item */
379     gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_root ), p_menu );
380
381     /* toggle currently selected chapter
382      * We have to release the lock since input_ToggleES needs it */
383     if( p_item_selected != NULL )
384     {
385         vlc_mutex_unlock( &p_input_bank->pp_input[0]->stream.stream_lock );
386         gtk_check_menu_item_set_active( GTK_CHECK_MENU_ITEM( p_item_selected ),
387                                         TRUE );
388         vlc_mutex_lock( &p_input_bank->pp_input[0]->stream.stream_lock );
389     }
390
391     /* be sure that menu is sensitive, if there are several items */
392     if( i_nb > 1 )
393     {
394         gtk_widget_set_sensitive( p_root, TRUE );
395     }
396
397     return TRUE;
398 }
399
400 /*****************************************************************************
401  * GtkProgramMenu: update the programs menu of the interface 
402  *****************************************************************************
403  * Builds the program menu according to what have been found in the PAT 
404  * by the input. Usefull for multi-programs streams such as DVB ones.
405  *****************************************************************************/
406 static gint GtkProgramMenu( gpointer            p_data,
407                             GtkWidget *         p_root,
408                             pgrm_descriptor_t * p_pgrm,
409                       void(*pf_toggle )( GtkCheckMenuItem *, gpointer ) )
410 {
411     intf_thread_t *     p_intf;
412     GtkWidget *         p_menu;
413     GtkWidget *         p_item;
414     GtkWidget *         p_item_active;
415     GSList *            p_group;
416     char                psz_name[ GTK_MENU_LABEL_SIZE ];
417     gint                i;
418
419     /* cast */
420     p_intf = (intf_thread_t *)p_data;
421
422     /* temporary hack to avoid blank menu when an open menu is removed */
423     if( GTK_MENU_ITEM(p_root)->submenu != NULL )
424     {
425         gtk_menu_popdown( GTK_MENU( GTK_MENU_ITEM(p_root)->submenu ) );
426     }
427     /* removes previous menu */
428     gtk_menu_item_remove_submenu( GTK_MENU_ITEM( p_root ) );
429     gtk_widget_set_sensitive( p_root, FALSE );
430
431     p_group = NULL;
432
433     /* menu container */
434     p_menu = gtk_menu_new();
435
436     p_item_active = NULL;
437
438     /* create a set of program buttons and append them to the container */
439     for( i = 0 ; i < p_input_bank->pp_input[0]->stream.i_pgrm_number ; i++ )
440     {
441         snprintf( psz_name, GTK_MENU_LABEL_SIZE, "id %d",
442             p_input_bank->pp_input[0]->stream.pp_programs[i]->i_number );
443         psz_name[GTK_MENU_LABEL_SIZE-1] = '\0';
444             
445         p_item = gtk_radio_menu_item_new_with_label( p_group, psz_name );
446         p_group =
447             gtk_radio_menu_item_group( GTK_RADIO_MENU_ITEM( p_item ) );
448
449         if( p_pgrm == p_input_bank->pp_input[0]->stream.pp_programs[i] )
450         {
451             /* don't lose p_item when we append into menu */
452             p_item_active = p_item;
453         }
454
455         gtk_widget_show( p_item );
456
457         /* setup signal hanling */
458         gtk_signal_connect( GTK_OBJECT( p_item ), "toggled",
459                         GTK_SIGNAL_FUNC( pf_toggle ),
460                         (gpointer)(ptrdiff_t)( p_input_bank->pp_input[0]->
461                         stream.pp_programs[i]->i_number ) );
462
463         gtk_menu_append( GTK_MENU( p_menu ), p_item );
464     }
465
466     /* link the new menu to the menubar item */
467     gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_root ), p_menu );
468
469     /* activation will call signals so we can only do it
470      * when submenu is attached to menu - to get intf_window 
471      * We have to release the lock since input_ToggleES needs it */
472     if( p_item_active != NULL )
473     {
474         gtk_check_menu_item_set_active( GTK_CHECK_MENU_ITEM( p_item_active ),
475                                         TRUE );
476     }
477
478     /* be sure that menu is sensitive if more than 1 program */
479     if( p_input_bank->pp_input[0]->stream.i_pgrm_number > 1 )
480     {
481         gtk_widget_set_sensitive( p_root, TRUE );
482     }
483
484     return TRUE;
485 }
486
487 /*****************************************************************************
488  * GtkLanguageMenus: update interactive menus of the interface
489  *****************************************************************************
490  * Sets up menus with information from input:
491  *  -languages
492  *  -sub-pictures
493  * Warning: since this function is designed to be called by management
494  * function, the interface lock has to be taken
495  *****************************************************************************/
496 static gint GtkLanguageMenus( gpointer          p_data,
497                               GtkWidget *       p_root,
498                               es_descriptor_t * p_es,
499                               gint              i_cat,
500                         void(*pf_toggle )( GtkCheckMenuItem *, gpointer ) )
501 {
502     intf_thread_t *     p_intf;
503     GtkWidget *         p_menu;
504     GtkWidget *         p_separator;
505     GtkWidget *         p_item;
506     GtkWidget *         p_item_active;
507     GSList *            p_group;
508     char                psz_name[ GTK_MENU_LABEL_SIZE ];
509     gint                i_item;
510     gint                i;
511
512     
513
514     /* cast */
515     p_intf = (intf_thread_t *)p_data;
516
517     /* temporary hack to avoid blank menu when an open menu is removed */
518     if( GTK_MENU_ITEM(p_root)->submenu != NULL )
519     {
520         gtk_menu_popdown( GTK_MENU( GTK_MENU_ITEM(p_root)->submenu ) );
521     }
522     /* removes previous menu */
523     gtk_menu_item_remove_submenu( GTK_MENU_ITEM( p_root ) );
524     gtk_widget_set_sensitive( p_root, FALSE );
525
526     p_group = NULL;
527
528     /* menu container */
529     p_menu = gtk_menu_new();
530
531     /* special case for "off" item */
532     snprintf( psz_name, GTK_MENU_LABEL_SIZE, "None" );
533     psz_name[ GTK_MENU_LABEL_SIZE - 1 ] = '\0';
534
535     p_item = gtk_radio_menu_item_new_with_label( p_group, psz_name );
536     p_group = gtk_radio_menu_item_group( GTK_RADIO_MENU_ITEM( p_item ) );
537
538     gtk_widget_show( p_item );
539
540     /* signal hanling for off */
541     gtk_signal_connect( GTK_OBJECT( p_item ), "toggled",
542                         GTK_SIGNAL_FUNC ( pf_toggle ), NULL );
543
544     gtk_menu_append( GTK_MENU( p_menu ), p_item );
545
546     p_separator = gtk_menu_item_new();
547     gtk_widget_set_sensitive( p_separator, FALSE );
548     gtk_widget_show( p_separator );
549     gtk_menu_append( GTK_MENU( p_menu ), p_separator );
550
551     p_item_active = NULL;
552     i_item = 0;
553
554     vlc_mutex_lock( &p_input_bank->pp_input[0]->stream.stream_lock );
555
556 #define ES p_input_bank->pp_input[0]->stream.pp_es[i]
557     /* create a set of language buttons and append them to the container */
558     for( i = 0 ; i < p_input_bank->pp_input[0]->stream.i_es_number ; i++ )
559     {
560         if( ( ES->i_cat == i_cat ) &&
561             ( !ES->p_pgrm ||
562               ES->p_pgrm ==
563                  p_input_bank->pp_input[0]->stream.p_selected_program ) )
564         {
565             i_item++;
566             strcpy( psz_name,
567                     p_input_bank->pp_input[0]->stream.pp_es[i]->psz_desc );
568             if( psz_name[0] == '\0' )
569             {
570                 snprintf( psz_name, GTK_MENU_LABEL_SIZE,
571                           "Language %d", i_item );
572                 psz_name[ GTK_MENU_LABEL_SIZE - 1 ] = '\0';
573             }
574
575             p_item = gtk_radio_menu_item_new_with_label( p_group, psz_name );
576             p_group =
577                 gtk_radio_menu_item_group( GTK_RADIO_MENU_ITEM( p_item ) );
578
579             if( p_es == p_input_bank->pp_input[0]->stream.pp_es[i] )
580             {
581                 /* don't lose p_item when we append into menu */
582                 p_item_active = p_item;
583             }
584
585             gtk_widget_show( p_item );
586
587             /* setup signal hanling */
588             gtk_signal_connect( GTK_OBJECT( p_item ), "toggled",
589                             GTK_SIGNAL_FUNC( pf_toggle ),
590                             (gpointer)( p_input_bank->pp_input[0]->stream.pp_es[i] ) );
591
592             gtk_menu_append( GTK_MENU( p_menu ), p_item );
593         }
594     }
595
596     vlc_mutex_unlock( &p_input_bank->pp_input[0]->stream.stream_lock );
597
598     /* link the new menu to the menubar item */
599     gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_root ), p_menu );
600
601     /* acitvation will call signals so we can only do it
602      * when submenu is attached to menu - to get intf_window 
603      * We have to release the lock since input_ToggleES needs it */
604     if( p_item_active != NULL )
605     {
606         gtk_check_menu_item_set_active( GTK_CHECK_MENU_ITEM( p_item_active ),
607                                         TRUE );
608     }
609
610     /* be sure that menu is sensitive if non empty */
611     if( i_item > 0 )
612     {
613         gtk_widget_set_sensitive( p_root, TRUE );
614     }
615
616     return TRUE;
617 }
618
619 /*****************************************************************************
620  * GtkTitleMenu: sets menus for titles and chapters selection
621  *****************************************************************************
622  * Generates two types of menus:
623  *  -simple list of titles
624  *  -cascaded lists of chapters for each title
625  *****************************************************************************/
626 static gint GtkTitleMenu( gpointer       p_data,
627                             GtkWidget *    p_navigation, 
628                             void(*pf_toggle )( GtkCheckMenuItem *, gpointer ) )
629 {
630     intf_thread_t *     p_intf;
631     char                psz_name[ GTK_MENU_LABEL_SIZE ];
632     GtkWidget *         p_title_menu;
633     GtkWidget *         p_title_submenu;
634     GtkWidget *         p_title_item;
635     GtkWidget *         p_item_active;
636     GtkWidget *         p_chapter_menu;
637     GtkWidget *         p_chapter_submenu;
638     GtkWidget *         p_title_menu_item;
639     GtkWidget *         p_chapter_menu_item;
640     GtkWidget *         p_item;
641     GSList *            p_title_group;
642     GSList *            p_chapter_group;
643     gint                i_title;
644     gint                i_chapter;
645     gint                i_title_nb;
646     gint                i_chapter_nb;
647
648     /* cast */
649     p_intf = (intf_thread_t*)p_data;
650
651     /* temporary hack to avoid blank menu when an open menu is removed */
652     if( GTK_MENU_ITEM(p_navigation)->submenu != NULL )
653     {
654         gtk_menu_popdown( GTK_MENU( GTK_MENU_ITEM(p_navigation)->submenu ) );
655     }
656     /* removes previous menu */
657     gtk_menu_item_remove_submenu( GTK_MENU_ITEM( p_navigation ) );
658     gtk_widget_set_sensitive( p_navigation, FALSE );
659
660     p_title_menu = gtk_menu_new();
661     p_title_group = NULL;
662     p_title_submenu = NULL;
663     p_title_menu_item = NULL;
664     p_chapter_group = NULL;
665     p_chapter_submenu = NULL;
666     p_chapter_menu_item = NULL;
667     p_item_active = NULL;
668     i_title_nb = p_input_bank->pp_input[0]->stream.i_area_nb;
669
670     /* loop on titles */
671     for( i_title = 1 ; i_title < i_title_nb ; i_title++ )
672     {
673         /* we group titles in packets of ten for small screens */
674         if( ( i_title % 10 == 1 ) && ( i_title_nb > 20 ) )
675         {
676             if( i_title != 1 )
677             {
678                 gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_title_menu_item ),
679                                            p_title_submenu );
680                 gtk_menu_append( GTK_MENU( p_title_menu ), p_title_menu_item );
681             }
682
683             snprintf( psz_name, GTK_MENU_LABEL_SIZE,
684                       "%d - %d", i_title, i_title + 9 );
685             psz_name[ GTK_MENU_LABEL_SIZE - 1 ] = '\0';
686             p_title_menu_item = gtk_menu_item_new_with_label( psz_name );
687             gtk_widget_show( p_title_menu_item );
688             p_title_submenu = gtk_menu_new();
689         }
690
691         snprintf( psz_name, GTK_MENU_LABEL_SIZE, "Title %d (%d)", i_title,
692                   p_input_bank->pp_input[0]->stream.pp_areas[i_title]->i_part_nb );
693         psz_name[ GTK_MENU_LABEL_SIZE - 1 ] = '\0';
694 #if 0
695         if( pf_toggle == on_menubar_title_toggle )
696         {
697             p_title_item = gtk_radio_menu_item_new_with_label( p_title_group,
698                                                            psz_name );
699             p_title_group =
700               gtk_radio_menu_item_group( GTK_RADIO_MENU_ITEM( p_title_item ) );
701
702             if( p_input_bank->pp_input[0]->stream.pp_areas[i_title] ==
703                          p_input_bank->pp_input[0]->stream.p_selected_area )
704             {
705                 p_item_active = p_title_item;
706             }
707
708             /* setup signal hanling */
709             gtk_signal_connect( GTK_OBJECT( p_title_item ),
710                      "toggled",
711                      GTK_SIGNAL_FUNC( pf_toggle ),
712                      (gpointer)(p_input_bank->pp_input[0]->stream.pp_areas[i_title]) );
713
714             if( p_input_bank->pp_input[0]->stream.i_area_nb > 1 )
715             {
716                 /* be sure that menu is sensitive */
717                 gtk_widget_set_sensitive( p_navigation, TRUE );
718             }
719         }
720         else
721 #endif
722         {
723             p_title_item = gtk_menu_item_new_with_label( psz_name );
724
725 #if 1    
726             p_chapter_menu = gtk_menu_new();
727             i_chapter_nb =
728                     p_input_bank->pp_input[0]->stream.pp_areas[i_title]->i_part_nb;
729     
730             for( i_chapter = 0 ; i_chapter < i_chapter_nb ; i_chapter++ )
731             {
732                 /* we group chapters in packets of ten for small screens */
733                 if( ( i_chapter % 10 == 0 ) && ( i_chapter_nb > 20 ) )
734                 {
735                     if( i_chapter != 0 )
736                     {
737                         gtk_menu_item_set_submenu(
738                                     GTK_MENU_ITEM( p_chapter_menu_item ),
739                                     p_chapter_submenu );
740                         gtk_menu_append( GTK_MENU( p_chapter_menu ),
741                                          p_chapter_menu_item );
742                     }
743
744                     snprintf( psz_name, GTK_MENU_LABEL_SIZE,
745                               "%d - %d", i_chapter + 1, i_chapter + 10 );
746                     psz_name[ GTK_MENU_LABEL_SIZE - 1 ] = '\0';
747                     p_chapter_menu_item =
748                             gtk_menu_item_new_with_label( psz_name );
749                     gtk_widget_show( p_chapter_menu_item );
750                     p_chapter_submenu = gtk_menu_new();
751                 }
752
753                 snprintf( psz_name, GTK_MENU_LABEL_SIZE,
754                           "Chapter %d", i_chapter + 1 );
755                 psz_name[ GTK_MENU_LABEL_SIZE - 1 ] = '\0';
756     
757                 p_item = gtk_radio_menu_item_new_with_label(
758                                                 p_chapter_group, psz_name );
759                 p_chapter_group = gtk_radio_menu_item_group(
760                                                 GTK_RADIO_MENU_ITEM( p_item ) );
761                 gtk_widget_show( p_item );
762
763 #define p_area p_input_bank->pp_input[0]->stream.pp_areas[i_title]
764                 if( ( p_area ==
765                         p_input_bank->pp_input[0]->stream.p_selected_area ) &&
766                     ( p_area->i_part == i_chapter + 1 ) )
767                 {
768                     p_item_active = p_item;
769                 }
770 #undef p_area
771
772                 /* setup signal hanling */
773                 gtk_signal_connect( GTK_OBJECT( p_item ),
774                            "toggled",
775                            GTK_SIGNAL_FUNC( pf_toggle ),
776                            (gpointer)POS2DATA( i_title, i_chapter + 1) );
777
778                 if( i_chapter_nb > 20 )
779                 {
780                     gtk_menu_append( GTK_MENU( p_chapter_submenu ), p_item );
781                 }
782                 else
783                 {
784                     gtk_menu_append( GTK_MENU( p_chapter_menu ), p_item );
785                 }
786             }
787
788             if( i_chapter_nb > 20 )
789             {
790                 gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_chapter_menu_item ),
791                                            p_chapter_submenu );
792                 gtk_menu_append( GTK_MENU( p_chapter_menu ),
793                                  p_chapter_menu_item );
794             }
795
796             /* link the new menu to the title menu item */
797             gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_title_item ),
798                                        p_chapter_menu );
799
800             if( p_input_bank->pp_input[0]->stream.pp_areas[i_title]->i_part_nb > 1 )
801             {
802                 /* be sure that menu is sensitive */
803                 gtk_widget_set_sensitive( p_navigation, TRUE );
804             }
805 #else
806             GtkRadioMenu( p_intf, p_title_item, p_chapter_group, "Chapter",
807                 p_input_bank->pp_input[0]->stream.pp_areas[i_title]->i_part_nb,
808                 i_title * 100,
809                 p_input_bank->pp_input[0]->stream.p_selected_area->i_part +
810                 p_input_bank->pp_input[0]->stream.p_selected_area->i_id *100,
811                 pf_toggle );
812
813 #endif
814         }
815         gtk_widget_show( p_title_item );
816
817         if( i_title_nb > 20 )
818         {
819             gtk_menu_append( GTK_MENU( p_title_submenu ), p_title_item );
820         }
821         else
822         {
823             gtk_menu_append( GTK_MENU( p_title_menu ), p_title_item );
824         }
825     }
826
827     if( i_title_nb > 20 )
828     {
829         gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_title_menu_item ),
830                                    p_title_submenu );
831         gtk_menu_append( GTK_MENU( p_title_menu ), p_title_menu_item );
832     }
833
834     /* be sure that menu is sensitive */
835     gtk_widget_set_sensitive( p_title_menu, TRUE );
836
837     /* link the new menu to the menubar item */
838     gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_navigation ), p_title_menu );
839
840     /* Default selected chapter
841      * We have to release the lock since input_ToggleES needs it */
842     if( p_item_active != NULL )
843     {
844         vlc_mutex_unlock( &p_input_bank->pp_input[0]->stream.stream_lock );
845         gtk_check_menu_item_set_active( GTK_CHECK_MENU_ITEM( p_item_active ),
846                                         TRUE );
847         vlc_mutex_lock( &p_input_bank->pp_input[0]->stream.stream_lock );
848     }
849 #if 0
850     if( p_input_bank->pp_input[0]->stream.i_area_nb > 1 )
851     {
852         /* be sure that menu is sensitive */
853         gtk_widget_set_sensitive( p_navigation, TRUE );
854     }
855 #endif
856
857     return TRUE;
858 }
859
860 /*****************************************************************************
861  * GtkSetupMenus: function that generates title/chapter/audio/subpic
862  * menus with help from preceding functions
863  *****************************************************************************
864  * Function called with the lock on stream
865  *****************************************************************************/
866 gint GtkSetupMenus( intf_thread_t * p_intf )
867 {
868     es_descriptor_t *   p_audio_es;
869     es_descriptor_t *   p_spu_es;
870     GtkWidget *         p_menubar_menu;
871     GtkWidget *         p_popup_menu;
872     gint                i;
873
874     p_intf->p_sys->b_chapter_update |= p_intf->p_sys->b_title_update;
875     p_intf->p_sys->b_audio_update |= p_intf->p_sys->b_title_update |
876                                      p_intf->p_sys->b_program_update;
877     p_intf->p_sys->b_spu_update |= p_intf->p_sys->b_title_update |
878                                    p_intf->p_sys->b_program_update;
879
880     if( p_intf->p_sys->b_program_update )
881     { 
882         pgrm_descriptor_t * p_pgrm;
883         
884         if( p_input_bank->pp_input[0]->stream.p_new_program )
885         {
886             p_pgrm = p_input_bank->pp_input[0]->stream.p_new_program;
887         }
888         else
889         {
890             p_pgrm = p_input_bank->pp_input[0]->stream.p_selected_program;
891         }
892
893         p_menubar_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT( 
894                             p_intf->p_sys->p_window ), "menubar_program" ) );
895         GtkProgramMenu( p_intf, p_menubar_menu, p_pgrm,
896                         GtkMenubarProgramToggle );
897         
898         p_intf->p_sys->b_program_update = 1;
899         p_popup_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT( 
900                             p_intf->p_sys->p_popup ), "popup_program" ) );
901         GtkProgramMenu( p_intf, p_popup_menu, p_pgrm,
902                         GtkPopupProgramToggle );
903
904         p_intf->p_sys->b_program_update = 0;
905     }
906     
907     if( p_intf->p_sys->b_title_update )
908     { 
909         char            psz_title[5];
910
911         p_menubar_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT( 
912                             p_intf->p_sys->p_window ), "menubar_title" ) );
913         GtkRadioMenu( p_intf, p_menubar_menu, NULL, "Title",
914                       p_input_bank->pp_input[0]->stream.i_area_nb - 1,
915                       p_input_bank->pp_input[0]->stream.p_selected_area->i_id,
916                       GtkMenubarTitleToggle );
917
918         snprintf( psz_title, 4, "%d",
919                   p_input_bank->pp_input[0]->stream.p_selected_area->i_id );
920         psz_title[ 4 ] = '\0';
921         gtk_label_set_text( p_intf->p_sys->p_label_title, psz_title );
922
923         p_intf->p_sys->b_title_update = 0;
924     }
925
926     if( p_intf->p_sys->b_chapter_update )
927     {
928         char            psz_chapter[5];
929
930         p_popup_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT( 
931                              p_intf->p_sys->p_popup ), "popup_navigation" ) );
932         GtkTitleMenu( p_intf, p_popup_menu, GtkPopupNavigationToggle );
933 #if 0
934         GtkRadioMenu( p_intf, p_menubar_menu, NULL, "Title",
935                         p_input_bank->pp_input[0]->stream.i_area_nb - 1,
936                         p_input_bank->pp_input[0]->stream.p_selected_area->i_id,
937                         on_menubar_chapter_toggle );
938 #endif
939
940         p_menubar_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT( 
941                              p_intf->p_sys->p_window ), "menubar_chapter" ) );
942
943         GtkRadioMenu( p_intf, p_menubar_menu, NULL, "Chapter",
944                         p_input_bank->pp_input[0]->stream.p_selected_area->i_part_nb,
945                         p_input_bank->pp_input[0]->stream.p_selected_area->i_part,
946                         GtkMenubarChapterToggle );
947
948
949         snprintf( psz_chapter, 4, "%d", 
950                   p_input_bank->pp_input[0]->stream.p_selected_area->i_part );
951         psz_chapter[ 4 ] = '\0';
952         gtk_label_set_text( p_intf->p_sys->p_label_chapter, psz_chapter );
953
954         p_intf->p_sys->i_part =
955                 p_input_bank->pp_input[0]->stream.p_selected_area->i_part;
956
957         p_intf->p_sys->b_chapter_update = 0;
958     }
959
960     /* look for selected ES */
961     p_audio_es = NULL;
962     p_spu_es = NULL;
963
964     for( i = 0 ; i < p_input_bank->pp_input[0]->stream.i_selected_es_number ; i++ )
965     {
966         if( p_input_bank->pp_input[0]->stream.pp_selected_es[i]->i_cat == AUDIO_ES )
967         {
968             p_audio_es = p_input_bank->pp_input[0]->stream.pp_selected_es[i];
969         }
970
971         if( p_input_bank->pp_input[0]->stream.pp_selected_es[i]->i_cat == SPU_ES )
972         {
973             p_spu_es = p_input_bank->pp_input[0]->stream.pp_selected_es[i];
974         }
975     }
976
977     vlc_mutex_unlock( &p_input_bank->pp_input[0]->stream.stream_lock );
978
979     /* audio menus */
980     if( p_intf->p_sys->b_audio_update )
981     {
982         /* find audio root menu */
983         p_menubar_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
984                              p_intf->p_sys->p_window ), "menubar_audio" ) );
985     
986         p_popup_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT( 
987                      p_intf->p_sys->p_popup ), "popup_audio" ) );
988     
989         p_intf->p_sys->b_audio_update = 1;
990         GtkLanguageMenus( p_intf, p_menubar_menu, p_audio_es, AUDIO_ES,
991                             GtkMenubarAudioToggle );
992         p_intf->p_sys->b_audio_update = 1;
993         GtkLanguageMenus( p_intf, p_popup_menu, p_audio_es, AUDIO_ES,
994                             GtkPopupAudioToggle );
995     
996         p_intf->p_sys->b_audio_update = 0;
997     }
998     
999     /* sub picture menus */
1000     if( p_intf->p_sys->b_spu_update )
1001     {
1002         /* find spu root menu */
1003         p_menubar_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
1004                           p_intf->p_sys->p_window ), "menubar_subpictures" ) );
1005     
1006         p_popup_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT( 
1007                      p_intf->p_sys->p_popup ), "popup_subpictures" ) );
1008     
1009         p_intf->p_sys->b_spu_update = 1;
1010         GtkLanguageMenus( p_intf, p_menubar_menu, p_spu_es, SPU_ES,
1011                             GtkMenubarSubtitleToggle  );
1012         p_intf->p_sys->b_spu_update = 1;
1013         GtkLanguageMenus( p_intf, p_popup_menu, p_spu_es, SPU_ES,
1014                             GtkPopupSubtitleToggle );
1015     
1016         p_intf->p_sys->b_spu_update = 0;
1017     }
1018
1019     vlc_mutex_lock( &p_input_bank->pp_input[0]->stream.stream_lock );
1020
1021     return TRUE;
1022 }
1023