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