]> git.sesse.net Git - vlc/blob - plugins/gtk/intf_gtk.c
*Changed the level arg in intf_WarnMsg so that it is more logical: the
[vlc] / plugins / gtk / intf_gtk.c
1 /*****************************************************************************
2  * intf_gtk.c: Gtk+ interface
3  *****************************************************************************
4  * Copyright (C) 1999, 2000 VideoLAN
5  * $Id: intf_gtk.c,v 1.17 2001/05/07 03:14:09 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
33 #include <errno.h>                                                 /* ENOMEM */
34 #include <stdlib.h>                                                /* free() */
35 #include <string.h>                                            /* strerror() */
36 #include <stdio.h>
37
38 #include <gtk/gtk.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
53 #include "gtk_callbacks.h"
54 #include "gtk_interface.h"
55 #include "gtk_support.h"
56 #include "intf_gtk.h"
57
58 #include "main.h"
59
60 /*****************************************************************************
61  * Local prototypes.
62  *****************************************************************************/
63 static int  intf_Probe      ( probedata_t *p_data );
64 static int  intf_Open       ( intf_thread_t *p_intf );
65 static void intf_Close      ( intf_thread_t *p_intf );
66 static void intf_Run        ( intf_thread_t *p_intf );
67
68 static gint GtkManage       ( gpointer p_data );
69 static gint GtkLanguageMenus( gpointer, GtkWidget *, es_descriptor_t *, gint,
70                               void (*pf_activate)(GtkMenuItem *, gpointer) );
71 static gint GtkChapterMenu  ( gpointer, GtkWidget *,
72                               void (*pf_activate)(GtkMenuItem *, gpointer) );
73 static gint GtkTitleMenu    ( gpointer, GtkWidget *, 
74                               void (*pf_activate)(GtkMenuItem *, gpointer) );
75 static void GtkDisplayDate  ( GtkAdjustment *p_adj );
76
77 void GtkPlayListManage( gpointer p_data );
78
79 /*****************************************************************************
80  * g_atexit: kludge to avoid the Gtk+ thread to segfault at exit
81  *****************************************************************************
82  * gtk_init() makes several calls to g_atexit() which calls atexit() to
83  * register tidying callbacks to be called at program exit. Since the Gtk+
84  * plugin is likely to be unloaded at program exit, we have to export this
85  * symbol to intercept the g_atexit() calls. Talk about crude hack.
86  *****************************************************************************/
87 void g_atexit( GVoidFunc func )
88 {
89     intf_thread_t *p_intf = p_main->p_intf;
90
91     if( p_intf->p_sys->pf_gdk_callback == NULL )
92     {
93         p_intf->p_sys->pf_gdk_callback = func;
94     }
95     else if( p_intf->p_sys->pf_gtk_callback == NULL )
96     {
97         p_intf->p_sys->pf_gtk_callback = func;
98     }
99     /* else nothing, but we could do something here */
100     return;
101 }
102
103 /*****************************************************************************
104  * Functions exported as capabilities. They are declared as static so that
105  * we don't pollute the namespace too much.
106  *****************************************************************************/
107 void _M( intf_getfunctions )( function_list_t * p_function_list )
108 {
109     p_function_list->pf_probe = intf_Probe;
110     p_function_list->functions.intf.pf_open  = intf_Open;
111     p_function_list->functions.intf.pf_close = intf_Close;
112     p_function_list->functions.intf.pf_run   = intf_Run;
113 }
114
115 /*****************************************************************************
116  * intf_Probe: probe the interface and return a score
117  *****************************************************************************
118  * This function tries to initialize Gtk+ and returns a score to the
119  * plugin manager so that it can select the best plugin.
120  *****************************************************************************/
121 static int intf_Probe( probedata_t *p_data )
122 {
123     if( TestMethod( INTF_METHOD_VAR, "gtk" ) )
124     {
125         return( 999 );
126     }
127
128     if( TestProgram( "gvlc" ) )
129     {
130         return( 190 );
131     }
132
133     return( 90 );
134 }
135
136 /*****************************************************************************
137  * intf_Open: initialize and create window
138  *****************************************************************************/
139 static int intf_Open( intf_thread_t *p_intf )
140 {
141     /* Allocate instance and initialize some members */
142     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
143     if( p_intf->p_sys == NULL )
144     {
145         intf_ErrMsg("error: %s", strerror(ENOMEM));
146         return( 1 );
147     }
148
149     /* Initialize Gtk+ thread */
150     p_intf->p_sys->b_popup_changed = 0;
151     p_intf->p_sys->b_window_changed = 0;
152     p_intf->p_sys->b_playlist_changed = 0;
153
154     p_intf->p_sys->b_menus_update = 1;
155     p_intf->p_sys->b_slider_free = 1;
156
157
158     p_intf->p_sys->i_playing = -1;
159
160     p_intf->p_sys->pf_gtk_callback = NULL;
161     p_intf->p_sys->pf_gdk_callback = NULL;
162
163     return( 0 );
164 }
165
166 /*****************************************************************************
167  * intf_Close: destroy interface window
168  *****************************************************************************/
169 static void intf_Close( intf_thread_t *p_intf )
170 {
171     /* Destroy structure */
172     free( p_intf->p_sys );
173 }
174
175 /*****************************************************************************
176  * intf_Run: Gtk+ thread
177  *****************************************************************************
178  * this part of the interface is in a separate thread so that we can call
179  * gtk_main() from within it without annoying the rest of the program.
180  * XXX: the approach may look kludgy, and probably is, but I could not find
181  * a better way to dynamically load a Gtk+ interface at runtime.
182  *****************************************************************************/
183 static void intf_Run( intf_thread_t *p_intf )
184 {
185     /* gtk_init needs to know the command line. We don't care, so we
186      * give it an empty one */
187     char  *p_args[] = { "" };
188     char **pp_args  = p_args;
189     int    i_args   = 1;
190
191     /* The data types we are allowed to receive */
192     static GtkTargetEntry target_table[] =
193     {
194         { "text/uri-list", 0, DROP_ACCEPT_TEXT_URI_LIST },
195         { "text/plain", 0, DROP_ACCEPT_TEXT_PLAIN }
196     };
197
198     /* intf_Manage callback timeout */
199     int i_timeout;
200
201     /* Initialize Gtk+ */
202     gtk_init( &i_args, &pp_args );
203
204     /* Create some useful widgets that will certainly be used */
205     p_intf->p_sys->p_window = create_intf_window( );
206     p_intf->p_sys->p_popup = create_intf_popup( );
207     p_intf->p_sys->p_disc = create_intf_disc( );
208     p_intf->p_sys->p_network = create_intf_network( );
209     p_intf->p_sys->p_playlist = create_intf_playlist( );
210
211     
212     /* Set the title of the main window */
213     gtk_window_set_title( GTK_WINDOW(p_intf->p_sys->p_window),
214                           VOUT_TITLE " (Gtk+ interface)");
215
216     /* Accept file drops on the main window */
217     gtk_drag_dest_set( GTK_WIDGET( p_intf->p_sys->p_window ),
218                        GTK_DEST_DEFAULT_ALL, target_table,
219                        1, GDK_ACTION_COPY );
220
221     /* Accept file drops on the playlist window */
222     gtk_drag_dest_set( GTK_WIDGET( lookup_widget( p_intf->p_sys->p_playlist,
223                                                   "playlist_clist") ),
224                        GTK_DEST_DEFAULT_ALL, target_table,
225                        1, GDK_ACTION_COPY );
226
227     /* Get the interface labels */
228     p_intf->p_sys->p_slider_frame = GTK_FRAME( gtk_object_get_data(
229         GTK_OBJECT(p_intf->p_sys->p_window ), "slider_frame" ) ); 
230
231     /* Connect the date display to the slider */
232     #define P_SLIDER GTK_RANGE( gtk_object_get_data( \
233                          GTK_OBJECT( p_intf->p_sys->p_window ), "slider" ) )
234     p_intf->p_sys->p_adj = gtk_range_get_adjustment( P_SLIDER );
235
236     gtk_signal_connect ( GTK_OBJECT( p_intf->p_sys->p_adj ), "value_changed",
237                          GTK_SIGNAL_FUNC( GtkDisplayDate ), NULL );
238     p_intf->p_sys->f_adj_oldvalue = 0;
239     #undef P_SLIDER
240
241     /* We don't create these ones yet because we perhaps won't need them */
242     p_intf->p_sys->p_about = NULL;
243     p_intf->p_sys->p_modules = NULL;
244     p_intf->p_sys->p_fileopen = NULL;
245
246     /* Store p_intf to keep an eye on it */
247     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_window),
248                          "p_intf", p_intf );
249
250     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_popup),
251                          "p_intf", p_intf );
252
253     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_playlist),
254                          "p_intf", p_intf );
255
256     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_disc),
257                          "p_intf", p_intf );
258
259     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_network),
260                          "p_intf", p_intf );
261
262     gtk_object_set_data( GTK_OBJECT(p_intf->p_sys->p_adj),
263                          "p_intf", p_intf );
264
265     /* Show the control window */
266     gtk_widget_show( p_intf->p_sys->p_window );
267
268     /* Sleep to avoid using all CPU - since some interfaces needs to access
269      * keyboard events, a 100ms delay is a good compromise */
270     i_timeout = gtk_timeout_add( INTF_IDLE_SLEEP / 1000, GtkManage, p_intf );
271
272     /* Enter Gtk mode */
273     gtk_main();
274
275     /* Remove the timeout */
276     gtk_timeout_remove( i_timeout );
277
278     /* Launch stored callbacks */
279     if( p_intf->p_sys->pf_gtk_callback != NULL )
280     {
281         p_intf->p_sys->pf_gtk_callback();
282
283         if( p_intf->p_sys->pf_gdk_callback != NULL )
284         {
285             p_intf->p_sys->pf_gdk_callback();
286         }
287     }
288 }
289
290 /* following functions are local */
291
292 /*****************************************************************************
293  * GtkManage: manage main thread messages
294  *****************************************************************************
295  * In this function, called approx. 10 times a second, we check what the
296  * main program wanted to tell us.
297  *****************************************************************************/
298
299 static gint GtkManage( gpointer p_data )
300 {
301 #define p_intf ((intf_thread_t *)p_data)
302
303     GtkPlayListManage( p_data );
304
305     vlc_mutex_lock( &p_intf->change_lock );
306     
307     /* If the "display popup" flag has changed */
308     if( p_intf->b_menu_change )
309     {
310         if( !GTK_IS_WIDGET( p_intf->p_sys->p_popup ) )
311         {
312             p_intf->p_sys->p_popup = create_intf_popup();
313             gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_popup ),
314                                  "p_popup", p_intf );
315         }
316         gtk_menu_popup( GTK_MENU( p_intf->p_sys->p_popup ),
317                         NULL, NULL, NULL, NULL, 0, GDK_CURRENT_TIME );
318         p_intf->b_menu_change = 0;
319     }
320
321     if( p_intf->p_input != NULL )
322     {
323         /* Used by TS input when PMT changes */
324         if( p_intf->p_input->stream.b_changed )
325         {
326             p_intf->p_sys->b_menus_update = 1;
327             p_intf->p_input->stream.b_changed = 0;
328             intf_WarnMsg( 3, 
329                           "Interface menus refreshed as stream has changed" );
330         }
331
332     }
333     
334     /* Update language/chapter menus after user request */
335     if( p_intf->p_input != NULL && p_intf->p_sys->p_window != NULL &&
336         p_intf->p_sys->b_menus_update )
337     {
338         es_descriptor_t *   p_audio_es;
339         es_descriptor_t *   p_spu_es;
340         GtkWidget *         p_menubar_menu;
341         GtkWidget *         p_popup_menu;
342         gint                i;
343
344         p_menubar_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT( 
345                      p_intf->p_sys->p_window ), "menubar_title" ) );
346
347         GtkTitleMenu( p_intf, p_menubar_menu, on_menubar_title_activate );
348
349         p_menubar_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT( 
350                      p_intf->p_sys->p_window ), "menubar_chapter" ) );
351
352         GtkChapterMenu( p_intf, p_menubar_menu, on_menubar_chapter_activate );
353
354         p_popup_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT( 
355                      p_intf->p_sys->p_popup ), "popup_navigation" ) );
356
357         GtkTitleMenu( p_intf, p_popup_menu, on_popup_navigation_activate );
358     
359         /* look for selected ES */
360         p_audio_es = NULL;
361         p_spu_es = NULL;
362
363         for( i = 0 ; i < p_intf->p_input->stream.i_selected_es_number ; i++ )
364         {
365             if( p_intf->p_input->stream.pp_es[i]->i_cat == AUDIO_ES )
366             {
367                 p_audio_es = p_intf->p_input->stream.pp_es[i];
368             }
369     
370             if( p_intf->p_input->stream.pp_es[i]->i_cat == SPU_ES )
371             {
372                 p_spu_es = p_intf->p_input->stream.pp_es[i];
373             }
374         }
375
376         /* audio menus */
377
378         /* find audio root menu */
379         p_menubar_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
380                              p_intf->p_sys->p_window ), "menubar_audio" ) );
381
382         p_popup_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT( 
383                      p_intf->p_sys->p_popup ), "popup_audio" ) );
384
385         GtkLanguageMenus( p_intf, p_menubar_menu, p_audio_es, AUDIO_ES,
386                           on_menubar_audio_activate );
387         GtkLanguageMenus( p_intf, p_popup_menu, p_audio_es, AUDIO_ES,
388                           on_popup_audio_activate );
389
390         /* sub picture menus */
391
392         /* find spu root menu */
393         p_menubar_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT(
394                           p_intf->p_sys->p_window ), "menubar_subpictures" ) );
395
396         p_popup_menu = GTK_WIDGET( gtk_object_get_data( GTK_OBJECT( 
397                      p_intf->p_sys->p_popup ), "popup_subpictures" ) );
398
399         GtkLanguageMenus( p_intf, p_menubar_menu, p_spu_es, SPU_ES,
400                           on_menubar_subpictures_activate  );
401         GtkLanguageMenus( p_intf, p_popup_menu, p_spu_es, SPU_ES,
402                           on_popup_subpictures_activate );
403
404         /* everything is ready */
405         p_intf->p_sys->b_menus_update = 0;
406     }
407
408     /* Manage the slider */
409     if( p_intf->p_input != NULL && p_intf->p_input->stream.b_seekable )
410     {
411         float newvalue = p_intf->p_sys->p_adj->value;
412
413 #define p_area p_intf->p_input->stream.p_selected_area
414         /* If the user hasn't touched the slider since the last time,
415          * then the input can safely change it */
416         if( newvalue == p_intf->p_sys->f_adj_oldvalue )
417         {
418             /* Update the value */
419             p_intf->p_sys->p_adj->value = p_intf->p_sys->f_adj_oldvalue =
420                 ( 100. * p_area->i_tell ) / p_area->i_size;
421
422             gtk_signal_emit_by_name( GTK_OBJECT( p_intf->p_sys->p_adj ),
423                                      "value_changed" );
424         }
425         /* Otherwise, send message to the input if the user has
426          * finished dragging the slider */
427         else if( p_intf->p_sys->b_slider_free )
428         {
429             off_t i_seek = ( newvalue * p_area->i_size ) / 100;
430
431             input_Seek( p_intf->p_input, i_seek );
432
433             /* Update the old value */
434             p_intf->p_sys->f_adj_oldvalue = newvalue;
435         }
436 #undef p_area
437     }
438
439     /* Manage core vlc functions through the callback */
440     p_intf->pf_manage( p_intf );
441
442     if( p_intf->b_die )
443     {
444         vlc_mutex_unlock( &p_intf->change_lock );
445
446         /* Prepare to die, young Skywalker */
447         gtk_main_quit();
448
449         /* Just in case */
450         return( FALSE );
451     }
452
453     vlc_mutex_unlock( &p_intf->change_lock );
454
455     return( TRUE );
456
457 #undef p_intf
458 }
459
460 /*****************************************************************************
461  * GtkMenuRadioItem: give a menu item adapted to language/title selection,
462  * ie the menu item is a radio button.
463  *****************************************************************************/
464 static GtkWidget * GtkMenuRadioItem( GtkWidget * p_menu,
465                                      GSList **   p_button_group,
466                                      gint        b_active,
467                                      char *      psz_name )
468 {
469     GtkWidget *     p_item;
470
471 #if 0
472     GtkWidget *     p_button;
473
474     /* create button */
475     p_button =
476         gtk_radio_button_new_with_label( *p_button_group, psz_name );
477
478     /* add button to group */
479     *p_button_group =
480         gtk_radio_button_group( GTK_RADIO_BUTTON( p_button ) );
481
482     /* prepare button for display */
483     gtk_widget_show( p_button );
484
485     /* create menu item to store button */
486     p_item = gtk_menu_item_new();
487
488     /* put button inside item */
489     gtk_container_add( GTK_CONTAINER( p_item ), p_button );
490
491     /* add item to menu */
492     gtk_menu_append( GTK_MENU( p_menu ), p_item );
493
494           gtk_signal_connect( GTK_OBJECT( p_item ), "activate",
495                GTK_SIGNAL_FUNC( on_audio_toggle ),
496                NULL );
497
498
499     /* prepare item for display */
500     gtk_widget_show( p_item );
501
502     /* is it the selected item ? */
503     if( b_active )
504     {
505         gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( p_button ), TRUE );
506     }
507 #else
508     p_item = gtk_menu_item_new_with_label( psz_name );
509     gtk_menu_append( GTK_MENU( p_menu ), p_item );
510     gtk_widget_show( p_item );
511 #endif
512
513     return p_item;
514 }
515
516 /*****************************************************************************
517  * GtkLanguageMenus: update interactive menus of the interface
518  *****************************************************************************
519  * Sets up menus with information from input:
520  *  -languages
521  *  -sub-pictures
522  * Warning: since this function is designed to be called by management
523  * function, the interface lock has to be taken
524  *****************************************************************************/
525 static gint GtkLanguageMenus( gpointer          p_data,
526                               GtkWidget *       p_root,
527                               es_descriptor_t * p_es,
528                               gint              i_cat,
529                         void(*pf_activate )( GtkMenuItem *, gpointer ) )
530 {
531     intf_thread_t *     p_intf;
532     GtkWidget *         p_menu;
533     GtkWidget *         p_separator;
534     GtkWidget *         p_item;
535     GSList *            p_button_group;
536     char *              psz_name;
537     gint                b_active;
538     gint                i;
539
540     
541
542     /* cast */
543     p_intf = (intf_thread_t *)p_data;
544
545     vlc_mutex_lock( &p_intf->p_input->stream.stream_lock );
546
547     p_button_group = NULL;
548
549     /* menu container for audio */
550     p_menu = gtk_menu_new();
551
552     /* create a set of language buttons and append them to the container */
553     b_active = ( p_es == NULL );
554     psz_name = "Off";
555
556     p_item = GtkMenuRadioItem( p_menu, &p_button_group, b_active, psz_name );
557
558     /* setup signal hanling */
559     gtk_signal_connect( GTK_OBJECT( p_item ), "activate",
560             GTK_SIGNAL_FUNC ( pf_activate ), NULL );
561
562     p_separator = gtk_menu_item_new();
563     gtk_widget_show( p_separator );
564     gtk_menu_append( GTK_MENU( p_menu ), p_separator );
565     gtk_widget_set_sensitive( p_separator, FALSE );
566
567     for( i = 0 ; i < p_intf->p_input->stream.i_es_number ; i++ )
568     {
569         if( p_intf->p_input->stream.pp_es[i]->i_cat == i_cat )
570         {
571             b_active = ( p_es == p_intf->p_input->stream.pp_es[i] ) ? 1 : 0;
572             psz_name = p_intf->p_input->stream.pp_es[i]->psz_desc;
573
574             p_item = GtkMenuRadioItem( p_menu, &p_button_group,
575                                        b_active, psz_name );
576
577             /* setup signal hanling */
578             gtk_signal_connect( GTK_OBJECT( p_item ), "activate",
579                GTK_SIGNAL_FUNC( pf_activate ),
580                  (gpointer)( p_intf->p_input->stream.pp_es[i] ) );
581
582         }
583     }
584
585     /* link the new menu to the menubar item */
586     gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_root ), p_menu );
587
588     /* be sure that menu is sensitive */
589     gtk_widget_set_sensitive( p_root, TRUE );
590
591     vlc_mutex_unlock( &p_intf->p_input->stream.stream_lock );
592
593     return TRUE;
594 }
595
596 /*****************************************************************************
597  * GtkChapterMenu: generate chapter menu for current title
598  *****************************************************************************/
599 static gint GtkChapterMenu( gpointer p_data, GtkWidget * p_chapter,
600                         void(*pf_activate )( GtkMenuItem *, gpointer ) )
601 {
602     intf_thread_t *     p_intf;
603     char                psz_name[ GTK_MENU_LABEL_SIZE ];
604     GtkWidget *         p_chapter_menu;
605     GtkWidget *         p_item;
606     GSList *            p_chapter_button_group;
607     gint                i_title;
608     gint                i_chapter;
609     gint                b_active;
610
611     /* cast */
612     p_intf = (intf_thread_t*)p_data;
613
614     i_title = p_intf->p_input->stream.p_selected_area->i_id;
615     p_chapter_menu = gtk_menu_new();
616
617     for( i_chapter = 0;
618          i_chapter < p_intf->p_input->stream.pp_areas[i_title]->i_part_nb ;
619          i_chapter++ )
620     {
621         b_active = ( p_intf->p_input->stream.pp_areas[i_title]->i_part
622                      == i_chapter + 1 ) ? 1 : 0;
623         
624         snprintf( psz_name, GTK_MENU_LABEL_SIZE,
625                   "Chapter %d", i_chapter + 1 );
626         psz_name[ GTK_MENU_LABEL_SIZE - 1 ] = '\0';
627
628         p_item = GtkMenuRadioItem( p_chapter_menu, &p_chapter_button_group,
629                                    b_active, psz_name );
630         /* setup signal hanling */
631         gtk_signal_connect( GTK_OBJECT( p_item ),
632                         "activate",
633                         GTK_SIGNAL_FUNC( pf_activate ),
634                         (gpointer)(i_chapter + 1) );
635     }
636
637     /* link the new menu to the title menu item */
638     gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_chapter ),
639                                p_chapter_menu );
640
641     /* be sure that chapter menu is sensitive */
642     gtk_widget_set_sensitive( p_chapter, TRUE );
643
644     return TRUE;
645 }
646
647 /*****************************************************************************
648  * GtkTitleMenu: sets menus for titles and chapters selection
649  *****************************************************************************
650  * Generates two type of menus:
651  *  -simple list of titles
652  *  -cascaded lists of chapters for each title
653  *****************************************************************************/
654 static gint GtkTitleMenu( gpointer       p_data,
655                           GtkWidget *    p_navigation, 
656                           void(*pf_activate )( GtkMenuItem *, gpointer ) )
657 {
658     intf_thread_t *     p_intf;
659     char                psz_name[ GTK_MENU_LABEL_SIZE ];
660     GtkWidget *         p_title_menu;
661     GtkWidget *         p_title_item;
662     GtkWidget *         p_chapter_menu;
663     GtkWidget *         p_item;
664     GSList *            p_title_button_group;
665     GSList *            p_chapter_button_group;
666     gint                i_title;
667     gint                i_chapter;
668     gint                b_active;
669
670     /* cast */
671     p_intf = (intf_thread_t*)p_data;
672
673     p_title_menu = gtk_menu_new();
674     p_title_button_group = NULL;
675     p_chapter_button_group = NULL;
676
677     /* loop on titles */
678     for( i_title = 1 ;
679          i_title < p_intf->p_input->stream.i_area_nb ;
680          i_title++ )
681     {
682         b_active = ( p_intf->p_input->stream.pp_areas[i_title] ==
683                      p_intf->p_input->stream.p_selected_area ) ? 1 : 0;
684         snprintf( psz_name, GTK_MENU_LABEL_SIZE, "Title %d", i_title );
685         psz_name[ GTK_MENU_LABEL_SIZE - 1 ] = '\0';
686
687         p_title_item = GtkMenuRadioItem( p_title_menu, &p_title_button_group,
688                                          b_active, psz_name );
689
690         if( pf_activate == on_menubar_title_activate )
691         {
692             /* setup signal hanling */
693             gtk_signal_connect( GTK_OBJECT( p_title_item ),
694                      "activate",
695                      GTK_SIGNAL_FUNC( pf_activate ),
696                      (gpointer)(p_intf->p_input->stream.pp_areas[i_title]) );
697         }
698         else
699         {
700             p_chapter_menu = gtk_menu_new();
701     
702             for( i_chapter = 0;
703                  i_chapter <
704                         p_intf->p_input->stream.pp_areas[i_title]->i_part_nb ;
705                  i_chapter++ )
706             {
707                 b_active = ( p_intf->p_input->stream.pp_areas[i_title]->i_part
708                              == i_chapter + 1 ) ? 1 : 0;
709                 
710                 snprintf( psz_name, GTK_MENU_LABEL_SIZE,
711                           "Chapter %d", i_chapter + 1 );
712                 psz_name[ GTK_MENU_LABEL_SIZE - 1 ] = '\0';
713     
714                 p_item = GtkMenuRadioItem( p_chapter_menu,
715                                            &p_chapter_button_group,
716                                            b_active, psz_name );
717     
718                 /* setup signal hanling */
719                 gtk_signal_connect( GTK_OBJECT( p_item ),
720                            "activate",
721                            GTK_SIGNAL_FUNC( pf_activate ),
722                            (gpointer)( ( i_title * 100 ) + ( i_chapter + 1) ) );
723         }
724
725         /* link the new menu to the title menu item */
726         gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_title_item ),
727                                    p_chapter_menu );
728         }
729
730         /* be sure that chapter menu is sensitive */
731         gtk_widget_set_sensitive( p_title_menu, TRUE );
732
733     }
734
735     /* link the new menu to the menubar audio item */
736     gtk_menu_item_set_submenu( GTK_MENU_ITEM( p_navigation ), p_title_menu );
737
738     /* be sure that audio menu is sensitive */
739     gtk_widget_set_sensitive( p_navigation, TRUE );
740
741
742     return TRUE;
743 }
744
745 void GtkDisplayDate( GtkAdjustment *p_adj )
746 {
747     intf_thread_t *p_intf;
748
749     p_intf = gtk_object_get_data( GTK_OBJECT( p_adj ), "p_intf" );
750
751     if( p_intf->p_input != NULL )
752     {
753 #define p_area p_intf->p_input->stream.p_selected_area
754         char psz_time[ OFFSETTOTIME_MAX_SIZE ];
755
756         vlc_mutex_lock( &p_intf->p_input->stream.stream_lock );
757
758         gtk_frame_set_label( p_intf->p_sys->p_slider_frame,
759                             input_OffsetToTime( p_intf->p_input, psz_time,
760                                    ( p_area->i_size * p_adj->value ) / 100 ) );
761
762         vlc_mutex_unlock( &p_intf->p_input->stream.stream_lock );
763 #undef p_area
764      }
765 }
766
767