]> git.sesse.net Git - vlc/blob - plugins/gnome/intf_gnome.c
-corrected some bugs in gnome interface: language menu are now
[vlc] / plugins / gnome / intf_gnome.c
1 /*****************************************************************************
2  * intf_gnome.c: Gnome interface
3  *****************************************************************************
4  * Copyright (C) 1999, 2000 VideoLAN
5  * $Id: intf_gnome.c,v 1.31 2001/04/20 05:40:03 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 gnome
26 #include "modules_inner.h"
27
28 /*****************************************************************************
29  * Preamble
30  *****************************************************************************/
31 #include "defs.h"
32
33 #include <errno.h>                                                 /* ENOMEM */
34 #include <stdlib.h>                                                /* free() */
35 #include <string.h>                                            /* strerror() */
36 #include <stdio.h>
37
38 #include <gnome.h>
39
40 #include "config.h"
41 #include "common.h"
42 #include "threads.h"
43 #include "mtime.h"
44 #include "tests.h"
45 #include "modules.h"
46
47 #include "stream_control.h"
48 #include "input_ext-intf.h"
49
50 #include "intf_msg.h"
51 #include "interface.h"
52 #include "intf_playlist.h"
53
54 #include "gnome_callbacks.h"
55 #include "gnome_interface.h"
56 #include "gnome_support.h"
57 #include "intf_gnome.h"
58
59 #include "main.h"
60
61 /*****************************************************************************
62  * Local prototypes.
63  *****************************************************************************/
64 static int  intf_Probe     ( probedata_t *p_data );
65 static int  intf_Open      ( intf_thread_t *p_intf );
66 static void intf_Close     ( intf_thread_t *p_intf );
67 static void intf_Run       ( intf_thread_t *p_intf );
68
69 static gint GnomeManage    ( gpointer p_data );
70 static gint GnomeLanguageMenus( gpointer, GtkWidget *, es_descriptor_t *, gint,
71                               void (*pf_toggle)(GtkCheckMenuItem *, gpointer) );
72 static gint GnomeChapterMenu  ( gpointer, GtkWidget *,
73                               void (*pf_toggle)(GtkCheckMenuItem *, gpointer) );
74 static gint GnomeTitleMenu    ( gpointer, GtkWidget *, 
75                               void (*pf_toggle)(GtkCheckMenuItem *, gpointer) );
76 static gint GnomeSetupMenu    ( intf_thread_t * p_intf );
77 static void GnomeDisplayDate  ( GtkAdjustment *p_adj );
78 static gint GnomeDiscModeManage( intf_thread_t * p_intf );
79 static gint GnomeFileModeManage( intf_thread_t * p_intf );
80 static gint GnomeNetworkModeManage( intf_thread_t * p_intf );
81
82 /*****************************************************************************
83  * g_atexit: kludge to avoid the Gnome thread to segfault at exit
84  *****************************************************************************
85  * gtk_init() makes several calls to g_atexit() which calls atexit() to
86  * register tidying callbacks to be called at program exit. Since the Gnome
87  * plugin is likely to be unloaded at program exit, we have to export this
88  * symbol to intercept the g_atexit() calls. Talk about crude hack.
89  *****************************************************************************/
90 void g_atexit( GVoidFunc func )
91 {
92     intf_thread_t *p_intf = p_main->p_intf;
93
94     if( p_intf->p_sys->pf_gdk_callback == NULL )
95     {
96         p_intf->p_sys->pf_gdk_callback = func;
97     }
98     else if( p_intf->p_sys->pf_gtk_callback == NULL )
99     {
100         p_intf->p_sys->pf_gtk_callback = func;
101     }
102     /* else nothing, but we could do something here */
103     return;
104 }
105
106 /*****************************************************************************
107  * Functions exported as capabilities. They are declared as static so that
108  * we don't pollute the namespace too much.
109  *****************************************************************************/
110 void _M( intf_getfunctions )( function_list_t * p_function_list )
111 {
112     p_function_list->pf_probe = intf_Probe;
113     p_function_list->functions.intf.pf_open  = intf_Open;
114     p_function_list->functions.intf.pf_close = intf_Close;
115     p_function_list->functions.intf.pf_run   = intf_Run;
116 }
117
118 /*****************************************************************************
119  * intf_Probe: probe the interface and return a score
120  *****************************************************************************
121  * This function tries to initialize Gnome and returns a score to the
122  * plugin manager so that it can select the best plugin.
123  *****************************************************************************/
124 static int intf_Probe( probedata_t *p_data )
125 {
126     if( TestMethod( INTF_METHOD_VAR, "gnome" ) )
127     {
128         return( 999 );
129     }
130
131     if( TestProgram( "gnome-vlc" ) )
132     {
133         return( 200 );
134     }
135
136     return( 100 );
137 }
138
139 /*****************************************************************************
140  * intf_Open: initialize and create window
141  *****************************************************************************/
142 static int intf_Open( intf_thread_t *p_intf )
143 {
144     /* Allocate instance and initialize some members */
145     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
146     if( p_intf->p_sys == NULL )
147     {
148         intf_ErrMsg("error: %s", strerror(ENOMEM));
149         return( 1 );
150     }
151
152     /* Initialize Gnome thread */
153     p_intf->p_sys->b_popup_changed = 0;
154     p_intf->p_sys->b_window_changed = 0;
155     p_intf->p_sys->b_playlist_changed = 0;
156     p_intf->p_sys->b_title_update = 1;
157     p_intf->p_sys->b_chapter_update = 1;
158     p_intf->p_sys->b_audio_update = 1;
159     p_intf->p_sys->b_spu_update = 1;
160
161     p_intf->p_sys->b_slider_free = 1;
162
163     p_intf->p_sys->b_mode_changed = 1;
164     p_intf->p_sys->i_intf_mode = FILE_MODE;
165
166     p_intf->p_sys->i_part = 0;
167
168     p_intf->p_sys->pf_gtk_callback = NULL;
169     p_intf->p_sys->pf_gdk_callback = NULL;
170
171     return( 0 );
172 }
173
174 /*****************************************************************************
175  * intf_Close: destroy interface window
176  *****************************************************************************/
177 static void intf_Close( intf_thread_t *p_intf )
178 {
179     /* Destroy structure */
180     free( p_intf->p_sys );
181 }
182
183 /*****************************************************************************
184  * intf_Run: Gnome thread
185  *****************************************************************************
186  * this part of the interface is in a separate thread so that we can call
187  * gtk_main() from within it without annoying the rest of the program.
188  * XXX: the approach may look kludgy, and probably is, but I could not find
189  * a better way to dynamically load a Gnome interface at runtime.
190  *****************************************************************************/
191 static void intf_Run( intf_thread_t *p_intf )
192 {
193     /* gnome_init needs to know the command line. We don't care, so we
194      * give it an empty one */
195     char *p_args[] = { "" };
196     int   i_args   = 1;
197
198     /* The data types we are allowed to receive */
199     static GtkTargetEntry target_table[] =
200     {
201         { "text/uri-list", 0, DROP_ACCEPT_TEXT_URI_LIST },
202         { "text/plain",    0, DROP_ACCEPT_TEXT_PLAIN }
203     };
204
205     /* intf_Manage callback timeout */
206     int i_timeout;
207
208     /* Initialize Gnome */
209     gnome_init( p_main->psz_arg0, VERSION, i_args, p_args );
210
211     /* Create some useful widgets that will certainly be used */
212     p_intf->p_sys->p_window = create_intf_window( );
213     p_intf->p_sys->p_popup = create_intf_popup( );
214     p_intf->p_sys->p_disc = create_intf_disc( );
215     p_intf->p_sys->p_network = create_intf_network( );
216
217     /* Set the title of the main window */
218     gtk_window_set_title( GTK_WINDOW(p_intf->p_sys->p_window),
219                           VOUT_TITLE " (Gnome interface)");
220
221     /* Accept file drops on the main window */
222     gtk_drag_dest_set( GTK_WIDGET( p_intf->p_sys->p_window ),
223                        GTK_DEST_DEFAULT_ALL, target_table,
224                        1, GDK_ACTION_COPY );
225
226     /* Get the interface labels */
227     #define P_LABEL( name ) GTK_LABEL( gtk_object_get_data( \
228                          GTK_OBJECT( p_intf->p_sys->p_window ), name ) )
229     p_intf->p_sys->p_label_date = P_LABEL( "label_date" );
230     p_intf->p_sys->p_label_status = P_LABEL( "label_status" );
231     p_intf->p_sys->p_label_title = P_LABEL( "label_title" );
232     p_intf->p_sys->p_label_chapter = P_LABEL( "label_chapter" );
233     #undef P_LABEL
234
235     /* Connect the date display to the slider */
236     #define P_SLIDER GTK_RANGE( gtk_object_get_data( \
237                          GTK_OBJECT( p_intf->p_sys->p_window ), "slider" ) )
238     p_intf->p_sys->p_adj = gtk_range_get_adjustment( P_SLIDER );
239
240     gtk_signal_connect ( GTK_OBJECT( p_intf->p_sys->p_adj ), "value_changed",
241                          GTK_SIGNAL_FUNC( GnomeDisplayDate ), NULL );
242     p_intf->p_sys->f_adj_oldvalue = 0;
243     #undef P_SLIDER
244
245     /* We don't create these ones yet because we perhaps won't need them */
246     p_intf->p_sys->p_about = NULL;
247     p_intf->p_sys->p_playlist = NULL;
248     p_intf->p_sys->p_modules = NULL;
249     p_intf->p_sys->p_fileopen = NULL;
250
251     /* Store p_intf to keep an eye on it */
252     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_window),
253                          "p_intf", p_intf );
254
255     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_popup),
256                          "p_intf", p_intf );
257
258     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_disc),
259                          "p_intf", p_intf );
260
261     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_network),
262                          "p_intf", p_intf );
263
264     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_adj),
265                          "p_intf", p_intf );
266
267     /* Show the control window */
268     gtk_widget_show( p_intf->p_sys->p_window );
269
270     /* Sleep to avoid using all CPU - since some interfaces needs to access
271      * keyboard events, a 100ms delay is a good compromise */
272     i_timeout = gtk_timeout_add( INTF_IDLE_SLEEP / 1000, GnomeManage, p_intf );
273
274     /* Enter gnome mode */
275     gtk_main();
276
277     /* Remove the timeout */
278     gtk_timeout_remove( i_timeout );
279
280     /* Get rid of stored callbacks so we can unload the plugin */
281     if( p_intf->p_sys->pf_gtk_callback != NULL )
282     {
283         p_intf->p_sys->pf_gtk_callback( );
284         p_intf->p_sys->pf_gtk_callback = NULL;
285
286     }
287
288     if( p_intf->p_sys->pf_gdk_callback != NULL )
289     {
290         p_intf->p_sys->pf_gdk_callback( );
291         p_intf->p_sys->pf_gdk_callback = NULL;
292     }
293 }
294
295 /* following functions are local */
296
297 /*****************************************************************************
298  * GnomeManage: manage main thread messages
299  *****************************************************************************
300  * In this function, called approx. 10 times a second, we check what the
301  * main program wanted to tell us.
302  *****************************************************************************/
303 static gint GnomeManage( gpointer p_data )
304 {
305 #define p_intf ((intf_thread_t *)p_data)
306
307     vlc_mutex_lock( &p_intf->change_lock );
308
309     /* If the "display popup" flag has changed */
310     if( p_intf->b_menu_change )
311     {
312         gnome_popup_menu_do_popup( p_intf->p_sys->p_popup,
313                                    NULL, NULL, NULL, NULL );
314         p_intf->b_menu_change = 0;
315     }
316
317     if( p_intf->p_input != NULL )
318     {
319         float           newvalue;
320
321         /* New input or stream map change */
322         if( p_intf->p_input->stream.b_changed || p_intf->p_sys->b_mode_changed )
323         {
324             switch( p_intf->p_input->stream.i_method & 0xf0 )
325             {
326                 case INPUT_METHOD_FILE:
327                     GnomeFileModeManage( p_intf );
328                     break;
329                 case INPUT_METHOD_DISC:
330                     GnomeDiscModeManage( p_intf );
331                     break;
332                 case INPUT_METHOD_NETWORK:
333                     GnomeNetworkModeManage( p_intf );
334                     break;
335                 default:
336                     intf_ErrMsg( "intf error: can't determine input method" );
337                     break;
338             }
339
340             p_intf->p_input->stream.b_changed = 0;
341             p_intf->p_sys->b_mode_changed = 0;
342             intf_WarnMsg( 2, 
343                           "Interface menus refreshed as stream has changed" );
344         }
345
346         /* Update language/chapter menus after user request */
347         GnomeSetupMenu( p_intf );
348
349 #define p_area p_intf->p_input->stream.p_selected_area
350         /* Update menus when chapter changes */
351         p_intf->p_sys->b_chapter_update =
352                     ( p_intf->p_sys->i_part != p_area->i_part );
353
354         if( p_intf->p_input->stream.b_seekable )
355         {
356             /* Manage the slider */
357             newvalue = p_intf->p_sys->p_adj->value;
358     
359             /* If the user hasn't touched the slider since the last time,
360              * then the input can safely change it */
361             if( newvalue == p_intf->p_sys->f_adj_oldvalue )
362             {
363                 /* Update the value */
364                 p_intf->p_sys->p_adj->value = p_intf->p_sys->f_adj_oldvalue =
365                     ( 100. * p_area->i_tell ) / p_area->i_size;
366     
367                 gtk_signal_emit_by_name( GTK_OBJECT( p_intf->p_sys->p_adj ),
368                                          "value_changed" );
369             }
370             /* Otherwise, send message to the input if the user has
371              * finished dragging the slider */
372             else if( p_intf->p_sys->b_slider_free )
373             {
374                 off_t i_seek = ( newvalue * p_area->i_size ) / 100;
375     
376                 input_Seek( p_intf->p_input, i_seek );
377     
378                 /* Update the old value */
379                 p_intf->p_sys->f_adj_oldvalue = newvalue;
380             }
381         }
382 #undef p_area
383     }
384
385     /* Manage core vlc functions through the callback */
386     p_intf->pf_manage( p_intf );
387
388     if( p_intf->b_die )
389     {
390         vlc_mutex_unlock( &p_intf->change_lock );
391
392         /* Prepare to die, young Skywalker */
393         gtk_main_quit();
394
395         /* Just in case */
396         return( FALSE );
397     }
398
399     vlc_mutex_unlock( &p_intf->change_lock );
400
401     return( TRUE );
402
403 #undef p_intf
404 }
405
406 /*****************************************************************************
407  * GnomeLanguageMenus: update interactive menus of the interface
408  *****************************************************************************
409  * Sets up menus with information from input:
410  *  -languages
411  *  -sub-pictures
412  * Warning: since this function is designed to be called by management
413  * function, the interface lock has to be taken
414  *****************************************************************************/
415 static gint GnomeLanguageMenus( gpointer          p_data,
416                                 GtkWidget *       p_root,
417                                 es_descriptor_t * p_es,
418                                 gint              i_cat,
419                           void(*pf_toggle )( GtkCheckMenuItem *, gpointer ) )
420 {
421     intf_thread_t *     p_intf;
422     GtkWidget *         p_menu;
423     GtkWidget *         p_separator;
424     GtkWidget *         p_item;
425     GtkWidget *         p_item_active;
426     GSList *            p_group;
427     char *              psz_name;
428     gint                i_item;
429     gint                i;
430
431     
432
433     /* cast */
434     p_intf = (intf_thread_t *)p_data;
435
436     /* removes previous menu */
437     gtk_menu_item_remove_submenu( GTK_MENU_ITEM( p_root ) );
438     gtk_widget_set_sensitive( p_root, FALSE );
439
440     p_group = NULL;
441
442     /* menu container */
443     p_menu = gtk_menu_new();
444
445     /* special case for "off" item */
446     psz_name = "Off";
447
448     p_item = gtk_radio_menu_item_new_with_label( p_group, psz_name );
449     p_group = gtk_radio_menu_item_group( GTK_RADIO_MENU_ITEM( p_item ) );
450
451     gtk_widget_show( p_item );
452
453     /* signal hanling for off */
454     gtk_signal_connect( GTK_OBJECT( p_item ), "toggled",
455                         GTK_SIGNAL_FUNC ( pf_toggle ), NULL );
456
457     gtk_menu_append( GTK_MENU( p_menu ), p_item );
458
459     p_separator = gtk_menu_item_new();
460     gtk_widget_set_sensitive( p_separator, FALSE );
461     gtk_widget_show( p_separator );
462     gtk_menu_append( GTK_MENU( p_menu ), p_separator );
463
464     vlc_mutex_lock( &p_intf->p_input->stream.stream_lock );
465     p_item_active = NULL;
466     i_item = 0;
467
468     /* create a set of language buttons and append them to the container */
469     for( i = 0 ; i < p_intf->p_input->stream.i_es_number ; i++ )
470     {
471         if( p_intf->p_input->stream.pp_es[i]->i_cat == i_cat )
472         {
473             i_item++;
474             psz_name = p_intf->p_input->stream.pp_es[i]->psz_desc;
475             if( psz_name[0] == '\0' )
476             {
477                 sprintf( psz_name, "Language %d", i_item );
478             }
479
480             p_item = gtk_radio_menu_item_new_with_label( p_group, psz_name );
481             p_group =
482                 gtk_radio_menu_item_group( GTK_RADIO_MENU_ITEM( p_item ) );
483
484             if( p_es == p_intf->p_input->stream.pp_es[i] )
485             {
486                 /* don't lose p_item when we append into menu */
487                 p_item_active = p_item;
488             }
489
490             gtk_widget_show( p_item );
491
492             /* setup signal hanling */
493             gtk_signal_connect( GTK_OBJECT( p_item ), "toggled",
494                             GTK_SIGNAL_FUNC( pf_toggle ),
495                             (gpointer)( p_intf->p_input->stream.pp_es[i] ) );
496
497             gtk_menu_append( GTK_MENU( p_menu ), p_item );
498         }
499     }
500
501     vlc_mutex_unlock( &p_intf->p_input->stream.stream_lock );
502
503     /* link the new menu to the menubar item */
504     gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_root ), p_menu );
505
506     /* acitvation will call signals so we can only do it
507      * when submenu is attached to menu - to get intf_window */
508     if( p_item_active != NULL )
509     {
510         gtk_check_menu_item_set_active( GTK_CHECK_MENU_ITEM( p_item_active ),
511                                         TRUE );
512     }
513
514     /* be sure that menu is sensitive if non empty */
515     if( i_item > 0 )
516     {
517         gtk_widget_set_sensitive( p_root, TRUE );
518     }
519
520     return TRUE;
521 }
522
523 /*****************************************************************************
524  * GnomeChapterMenu: generate chapter menu for current title
525  *****************************************************************************/
526 static gint GnomeChapterMenu( gpointer p_data, GtkWidget * p_chapter,
527                         void(*pf_toggle )( GtkCheckMenuItem *, gpointer ) )
528 {
529     intf_thread_t *     p_intf;
530     char                psz_name[10];
531     GtkWidget *         p_chapter_menu;
532     GtkWidget *         p_chapter_submenu;
533     GtkWidget *         p_menu_item;
534     GtkWidget *         p_item;
535     GtkWidget *         p_item_selected;
536     GSList *            p_chapter_group;
537     gint                i_title;
538     gint                i_chapter;
539     gint                i_nb;
540
541     /* cast */
542     p_intf = (intf_thread_t*)p_data;
543
544     /* removes previous menu */
545     gtk_menu_item_remove_submenu( GTK_MENU_ITEM( p_chapter ) );
546     gtk_widget_set_sensitive( p_chapter, FALSE );
547
548     p_chapter_submenu = NULL;
549     p_chapter_group = NULL;
550     p_item_selected = NULL;
551     p_menu_item = NULL;
552
553     i_title = p_intf->p_input->stream.p_selected_area->i_id;
554     p_chapter_menu = gtk_menu_new();
555     i_nb = p_intf->p_input->stream.pp_areas[i_title]->i_part_nb;
556
557     for( i_chapter = 0 ; i_chapter < i_nb ; i_chapter++ )
558     {
559         /* we group chapters in packets of ten for small screens */
560         if( ( i_chapter % 10 == 0 ) && ( i_nb > 20 ) )
561         {
562             if( i_chapter != 0 )
563             {
564                 gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_menu_item ),
565                                            p_chapter_submenu );
566                 gtk_menu_append( GTK_MENU( p_chapter_menu ), p_menu_item );
567             }
568
569             sprintf( psz_name, "%d - %d", i_chapter + 1, i_chapter + 10);
570             p_menu_item = gtk_menu_item_new_with_label( psz_name );
571             gtk_widget_show( p_menu_item );
572             p_chapter_submenu = gtk_menu_new();
573         }
574
575         sprintf( psz_name, "Chapter %d", i_chapter + 1 );
576
577         p_item = gtk_radio_menu_item_new_with_label( p_chapter_group,
578                                                      psz_name );
579         p_chapter_group =
580             gtk_radio_menu_item_group( GTK_RADIO_MENU_ITEM( p_item ) );
581
582         if( p_intf->p_input->stream.pp_areas[i_title]->i_part
583                      == i_chapter + 1 )
584         {
585             p_item_selected = p_item;
586         }
587         
588         gtk_widget_show( p_item );
589
590         /* setup signal hanling */
591         gtk_signal_connect( GTK_OBJECT( p_item ),
592                         "toggled",
593                         GTK_SIGNAL_FUNC( pf_toggle ),
594                         (gpointer)(i_chapter + 1) );
595
596         if( i_nb > 20 )
597         {
598             gtk_menu_append( GTK_MENU( p_chapter_submenu ), p_item );
599         }
600         else
601         {
602             gtk_menu_append( GTK_MENU( p_chapter_menu ), p_item );
603         }
604     }
605
606     if( i_nb > 20 )
607     {
608         gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_menu_item ),
609                                    p_chapter_submenu );
610         gtk_menu_append( GTK_MENU( p_chapter_menu ), p_menu_item );
611     }
612
613     /* link the new menu to the title menu item */
614     gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_chapter ),
615                                p_chapter_menu );
616
617     /* toggle currently selected chapter */
618     if( p_item_selected != NULL )
619     {
620         gtk_check_menu_item_set_active( GTK_CHECK_MENU_ITEM( p_item_selected ),
621                                         TRUE );
622     }
623
624     /* be sure that chapter menu is sensitive, if there are several items */
625     if( p_intf->p_input->stream.pp_areas[i_title]->i_part_nb > 1 )
626     {
627         gtk_widget_set_sensitive( p_chapter, TRUE );
628     }
629
630     return TRUE;
631 }
632
633 /*****************************************************************************
634  * GnomeTitleMenu: sets menus for titles and chapters selection
635  *****************************************************************************
636  * Generates two types of menus:
637  *  -simple list of titles
638  *  -cascaded lists of chapters for each title
639  *****************************************************************************/
640 static gint GnomeTitleMenu( gpointer       p_data,
641                             GtkWidget *    p_navigation, 
642                             void(*pf_toggle )( GtkCheckMenuItem *, gpointer ) )
643 {
644     intf_thread_t *     p_intf;
645     char                psz_name[10];
646     GtkWidget *         p_title_menu;
647     GtkWidget *         p_title_submenu;
648     GtkWidget *         p_title_item;
649     GtkWidget *         p_item_active;
650     GtkWidget *         p_chapter_menu;
651     GtkWidget *         p_chapter_submenu;
652     GtkWidget *         p_title_menu_item;
653     GtkWidget *         p_chapter_menu_item;
654     GtkWidget *         p_item;
655     GSList *            p_title_group;
656     GSList *            p_chapter_group;
657     gint                i_title;
658     gint                i_chapter;
659     gint                i_title_nb;
660     gint                i_chapter_nb;
661
662     /* cast */
663     p_intf = (intf_thread_t*)p_data;
664
665     p_title_menu = gtk_menu_new();
666     p_title_group = NULL;
667     p_title_submenu = NULL;
668     p_title_menu_item = NULL;
669     p_chapter_group = NULL;
670     p_chapter_submenu = NULL;
671     p_chapter_menu_item = NULL;
672     p_item_active = NULL;
673     i_title_nb = p_intf->p_input->stream.i_area_nb;
674
675     /* loop on titles */
676     for( i_title = 1 ; i_title < i_title_nb ; i_title++ )
677     {
678         /* we group titles in packets of ten for small screens */
679         if( ( i_title % 10 == 1 ) && ( i_title_nb > 20 ) )
680         {
681             if( i_title != 1 )
682             {
683                 gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_title_menu_item ),
684                                            p_title_submenu );
685                 gtk_menu_append( GTK_MENU( p_title_menu ), p_title_menu_item );
686             }
687
688             sprintf( psz_name, "%d - %d", i_title, i_title + 9 );
689             p_title_menu_item = gtk_menu_item_new_with_label( psz_name );
690             gtk_widget_show( p_title_menu_item );
691             p_title_submenu = gtk_menu_new();
692         }
693
694         sprintf( psz_name, "Title %d (%d)", i_title, p_intf->p_input->stream.pp_areas[i_title]->i_part_nb );
695
696         if( pf_toggle == on_menubar_title_toggle )
697         {
698             p_title_item = gtk_radio_menu_item_new_with_label( p_title_group,
699                                                            psz_name );
700             p_title_group =
701               gtk_radio_menu_item_group( GTK_RADIO_MENU_ITEM( p_title_item ) );
702
703             if( p_intf->p_input->stream.pp_areas[i_title] ==
704                          p_intf->p_input->stream.p_selected_area )
705             {
706                 p_item_active = p_title_item;
707             }
708
709             /* setup signal hanling */
710             gtk_signal_connect( GTK_OBJECT( p_title_item ),
711                      "toggled",
712                      GTK_SIGNAL_FUNC( pf_toggle ),
713                      (gpointer)(p_intf->p_input->stream.pp_areas[i_title]) );
714
715             if( p_intf->p_input->stream.i_area_nb > 1 )
716             {
717                 /* be sure that menu is sensitive */
718                 gtk_widget_set_sensitive( p_navigation, TRUE );
719             }
720         }
721         else
722         {
723     
724             p_title_item = gtk_menu_item_new_with_label( psz_name );
725             p_chapter_menu = gtk_menu_new();
726             i_chapter_nb =
727                     p_intf->p_input->stream.pp_areas[i_title]->i_part_nb;
728     
729             for( i_chapter = 0 ; i_chapter < i_chapter_nb ; i_chapter++ )
730             {
731                 /* we group chapters in packets of ten for small screens */
732                 if( ( i_chapter % 10 == 0 ) && ( i_chapter_nb > 20 ) )
733                 {
734                     if( i_chapter != 0 )
735                     {
736                         gtk_menu_item_set_submenu(
737                                     GTK_MENU_ITEM( p_chapter_menu_item ),
738                                     p_chapter_submenu );
739                         gtk_menu_append( GTK_MENU( p_chapter_menu ),
740                                          p_chapter_menu_item );
741                     }
742
743                     sprintf( psz_name, "%d - %d", i_chapter + 1,
744                                                   i_chapter + 10);
745                     p_chapter_menu_item =
746                             gtk_menu_item_new_with_label( psz_name );
747                     gtk_widget_show( p_chapter_menu_item );
748                     p_chapter_submenu = gtk_menu_new();
749                 }
750
751                 sprintf( psz_name, "Chapter %d", i_chapter + 1 );
752     
753                 p_item = gtk_radio_menu_item_new_with_label(
754                                                 p_chapter_group, psz_name );
755                 p_chapter_group = gtk_radio_menu_item_group(
756                                                 GTK_RADIO_MENU_ITEM( p_item ) );
757                 gtk_widget_show( p_item );
758
759 #define p_area p_intf->p_input->stream.pp_areas[i_title]
760                 if( ( p_area == p_intf->p_input->stream.p_selected_area ) &&
761                     ( p_area->i_part == i_chapter + 1 ) )
762                 {
763                     p_item_active = p_item;
764                 }
765 #undef p_area
766
767                 /* setup signal hanling */
768                 gtk_signal_connect( GTK_OBJECT( p_item ),
769                            "toggled",
770                            GTK_SIGNAL_FUNC( pf_toggle ),
771                            (gpointer)( ( i_title * 100 ) + ( i_chapter + 1) ) );
772
773                 if( i_chapter_nb > 20 )
774                 {
775                     gtk_menu_append( GTK_MENU( p_chapter_submenu ), p_item );
776                 }
777                 else
778                 {
779                     gtk_menu_append( GTK_MENU( p_chapter_menu ), p_item );
780                 }
781             }
782
783             if( i_chapter_nb > 20 )
784             {
785                 gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_chapter_menu_item ),
786                                            p_chapter_submenu );
787                 gtk_menu_append( GTK_MENU( p_chapter_menu ),
788                                  p_chapter_menu_item );
789             }
790
791             /* link the new menu to the title menu item */
792             gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_title_item ),
793                                        p_chapter_menu );
794
795             if( p_intf->p_input->stream.pp_areas[i_title]->i_part_nb > 1 )
796             {
797                 /* be sure that menu is sensitive */
798                 gtk_widget_set_sensitive( p_navigation, TRUE );
799             }
800         }
801         gtk_widget_show( p_title_item );
802
803         if( i_title_nb > 20 )
804         {
805             gtk_menu_append( GTK_MENU( p_title_submenu ), p_title_item );
806         }
807         else
808         {
809             gtk_menu_append( GTK_MENU( p_title_menu ), p_title_item );
810         }
811     }
812
813     if( i_title_nb > 20 )
814     {
815         gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_title_menu_item ),
816                                    p_title_submenu );
817         gtk_menu_append( GTK_MENU( p_title_menu ), p_title_menu_item );
818     }
819
820     /* be sure that menu is sensitive */
821     gtk_widget_set_sensitive( p_title_menu, TRUE );
822
823     /* link the new menu to the menubar item */
824     gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_navigation ), p_title_menu );
825
826     if( p_item_active != NULL )
827     {
828         gtk_check_menu_item_set_active( GTK_CHECK_MENU_ITEM( p_item_active ),
829                                         TRUE );
830     }
831
832
833     return TRUE;
834 }
835
836 /*****************************************************************************
837  * GnomeSetupMenu: function that generates title/chapter/audio/subpic
838  * menus with help from preceding functions
839  *****************************************************************************/
840 static gint GnomeSetupMenu( intf_thread_t * p_intf )
841 {
842     es_descriptor_t *   p_audio_es;
843     es_descriptor_t *   p_spu_es;
844     GtkWidget *         p_menubar_menu;
845     GtkWidget *         p_popup_menu;
846     gint                i;
847
848     p_intf->p_sys->b_title_update &= ( p_intf->p_input->stream.i_area_nb > 1 );
849     p_intf->p_sys->b_chapter_update |= p_intf->p_sys->b_title_update;
850     p_intf->p_sys->b_audio_update |= p_intf->p_sys->b_title_update;
851     p_intf->p_sys->b_spu_update |= p_intf->p_sys->b_title_update;
852
853     if( p_intf->p_sys->b_title_update )
854     { 
855         char            psz_title[3];
856
857         p_menubar_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT( 
858                             p_intf->p_sys->p_window ), "menubar_title" ) );
859         GnomeTitleMenu( p_intf, p_menubar_menu, on_menubar_title_toggle );
860
861         snprintf( psz_title, 3, "%02d",
862                   p_intf->p_input->stream.p_selected_area->i_id );
863         gtk_label_set_text( p_intf->p_sys->p_label_title, psz_title );
864
865         p_intf->p_sys->b_title_update = 0;
866     }
867
868     if( p_intf->p_sys->b_chapter_update )
869     {
870         char            psz_chapter[3];
871
872         p_popup_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT( 
873                              p_intf->p_sys->p_popup ), "popup_navigation" ) );
874         GnomeTitleMenu( p_intf, p_popup_menu, on_popup_navigation_toggle );
875      
876         p_menubar_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT( 
877                              p_intf->p_sys->p_window ), "menubar_chapter" ) );
878         GnomeChapterMenu( p_intf, p_menubar_menu, on_menubar_chapter_toggle );
879
880         snprintf( psz_chapter, 3, "%02d", 
881                   p_intf->p_input->stream.p_selected_area->i_part );
882         gtk_label_set_text( p_intf->p_sys->p_label_chapter, psz_chapter );
883
884         p_intf->p_sys->i_part =
885                 p_intf->p_input->stream.p_selected_area->i_part;
886         p_intf->p_sys->b_chapter_update = 0;
887     }
888     
889     /* look for selected ES */
890     p_audio_es = NULL;
891     p_spu_es = NULL;
892
893     for( i = 0 ; i < p_intf->p_input->stream.i_selected_es_number ; i++ )
894     {
895         if( p_intf->p_input->stream.pp_selected_es[i]->i_cat == AUDIO_ES )
896         {
897             p_audio_es = p_intf->p_input->stream.pp_selected_es[i];
898         }
899
900         if( p_intf->p_input->stream.pp_selected_es[i]->i_cat == SPU_ES )
901         {
902             p_spu_es = p_intf->p_input->stream.pp_selected_es[i];
903         }
904     }
905
906     /* audio menus */
907     if( p_intf->p_sys->b_audio_update )
908     {
909         /* find audio root menu */
910         p_menubar_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
911                              p_intf->p_sys->p_window ), "menubar_audio" ) );
912     
913         p_popup_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT( 
914                      p_intf->p_sys->p_popup ), "popup_audio" ) );
915     
916         GnomeLanguageMenus( p_intf, p_menubar_menu, p_audio_es, AUDIO_ES,
917                             on_menubar_audio_toggle );
918         GnomeLanguageMenus( p_intf, p_popup_menu, p_audio_es, AUDIO_ES,
919                             on_popup_audio_toggle );
920
921         p_intf->p_sys->b_audio_update = 0;
922     }
923     
924     /* sub picture menus */
925     if( p_intf->p_sys->b_spu_update )
926     {
927         /* find spu root menu */
928         p_menubar_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
929                           p_intf->p_sys->p_window ), "menubar_subtitle" ) );
930     
931         p_popup_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT( 
932                      p_intf->p_sys->p_popup ), "popup_subtitle" ) );
933     
934         GnomeLanguageMenus( p_intf, p_menubar_menu, p_spu_es, SPU_ES,
935                             on_menubar_subtitle_toggle  );
936         GnomeLanguageMenus( p_intf, p_popup_menu, p_spu_es, SPU_ES,
937                             on_popup_subtitle_toggle );
938
939         p_intf->p_sys->b_spu_update = 0;
940     }
941
942     return TRUE;
943 }
944
945 /*****************************************************************************
946  * GnomeDisplayDate: display stream date
947  *****************************************************************************
948  * This function displays the current date related to the position in
949  * the stream. It is called whenever the slider changes its value.
950  *****************************************************************************/
951 void GnomeDisplayDate( GtkAdjustment *p_adj )
952 {
953     intf_thread_t *p_intf;
954    
955     p_intf = gtk_object_get_data( GTK_OBJECT( p_adj ), "p_intf" );
956
957     if( p_intf->p_input != NULL )
958     {
959 #define p_area p_intf->p_input->stream.p_selected_area
960         char psz_time[ OFFSETTOTIME_MAX_SIZE ];
961
962         vlc_mutex_lock( &p_intf->p_input->stream.stream_lock );
963
964         gtk_label_set_text( p_intf->p_sys->p_label_date,
965                             input_OffsetToTime( p_intf->p_input, psz_time,
966                                    ( p_area->i_size * p_adj->value ) / 100 ) );
967
968         vlc_mutex_unlock( &p_intf->p_input->stream.stream_lock );
969 #undef p_area
970      }
971 }
972
973
974 /*****************************************************************************
975  * GnomeDiscModeManage
976  *****************************************************************************/
977 static gint GnomeDiscModeManage( intf_thread_t * p_intf )
978 {
979     GtkWidget *     p_dvd_box;
980     GtkWidget *     p_file_box;
981     GtkWidget *     p_network_box;
982
983     p_file_box = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
984                  p_intf->p_sys->p_window ), "file_box" ) );
985     gtk_widget_hide( GTK_WIDGET( p_file_box ) );
986
987     p_network_box = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
988                  p_intf->p_sys->p_window ), "network_box" ) );
989     gtk_widget_hide( GTK_WIDGET( p_network_box ) );
990
991     p_dvd_box = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
992                  p_intf->p_sys->p_window ), "dvd_box" ) );
993     gtk_widget_show( GTK_WIDGET( p_dvd_box ) );
994
995     gtk_label_set_text( p_intf->p_sys->p_label_status,
996                         "Status: playing DVD" );
997
998     return TRUE;
999 }
1000
1001 /*****************************************************************************
1002  * GnomeFileModeManage
1003  *****************************************************************************/
1004 static gint GnomeFileModeManage( intf_thread_t * p_intf )
1005 {
1006     GtkWidget *     p_dvd_box;
1007     GtkWidget *     p_file_box;
1008     GtkWidget *     p_network_box;
1009 //    char *          psz_name;
1010
1011     p_network_box = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
1012                  p_intf->p_sys->p_window ), "network_box" ) );
1013     gtk_widget_hide( GTK_WIDGET( p_network_box ) );
1014
1015     p_dvd_box = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
1016                  p_intf->p_sys->p_window ), "dvd_box" ) );
1017     gtk_widget_hide( GTK_WIDGET( p_dvd_box ) );
1018
1019     p_file_box = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
1020                  p_intf->p_sys->p_window ), "file_box" ) );
1021     gtk_widget_show( GTK_WIDGET( p_file_box ) );
1022 #if 0
1023     psz_name = malloc( 16 + strlen( p_intf->p_input->p_source ) );
1024     sprintf( psz_name, "Status: playing %s", p_intf->p_input->p_source );
1025
1026     gtk_label_set_text( p_intf->p_sys->p_label_status, psz_name );
1027
1028     free( psz_name );
1029 #else
1030     gtk_label_set_text( p_intf->p_sys->p_label_status,
1031                         "Status: foo" );
1032 #endif
1033
1034     return TRUE;
1035 }
1036
1037 /*****************************************************************************
1038  * GnomeNetworkModeManage
1039  *****************************************************************************/
1040 static gint GnomeNetworkModeManage( intf_thread_t * p_intf )
1041 {
1042     GtkWidget *     p_dvd_box;
1043     GtkWidget *     p_file_box;
1044     GtkWidget *     p_network_box;
1045
1046     p_dvd_box = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
1047                  p_intf->p_sys->p_window ), "dvd_box" ) );
1048     gtk_widget_hide( GTK_WIDGET( p_dvd_box ) );
1049
1050     p_file_box = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
1051                  p_intf->p_sys->p_window ), "file_box" ) );
1052     gtk_widget_hide( GTK_WIDGET( p_file_box ) );
1053
1054     p_network_box = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
1055                  p_intf->p_sys->p_window ), "network_box" ) );
1056     gtk_widget_show( GTK_WIDGET( p_network_box ) );
1057
1058     gtk_label_set_text( p_intf->p_sys->p_label_status,
1059                         "Status: waiting for stream" );
1060
1061     return TRUE;
1062 }