/*****************************************************************************/

/*
 *      interface.c -- REALmagic simple windowing application in GTK
 *
 *      Copyright (C) 2001 Sigma Designs
 *                    written by Pascal Cannenterre <pascal_cannenterre@sdesigns.com>
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*****************************************************************************/
#include "define.h"
#include "gtk_callbacks.h"
#include "interface.h"

/* include pixmaps */
#include "../pixmaps/play.xpm"
#include "../pixmaps/stop.xpm"
#include "../pixmaps/pause.xpm"
#include "../pixmaps/frewind.xpm"
#include "../pixmaps/fforward.xpm"
#include "../pixmaps/fileopen.xpm"
#include "../pixmaps/next.xpm"
#include "../pixmaps/prev.xpm"
#include "../pixmaps/cd.xpm"
#include "../pixmaps/rtsp.xpm"
#include "../pixmaps/REALmagic.xpm"
#include "../pixmaps/volume.xpm"
#include "../pixmaps/eject.xpm"
#include "../pixmaps/repeat.xpm"
#include "../pixmaps/repeatAB.xpm"

void my_gtk_widget_destroy(GtkWidget ** widget)
{
  gtk_widget_destroy(*widget);
  *widget = NULL;
}

GtkWidget* create_pixmap(GtkWidget *widget, char ** filename)
{
  GtkWidget *pixmap;
  GdkPixmap *gdkpixmap;
  GdkBitmap *mask;
  GdkColormap *colormap;
  
  colormap = gtk_widget_get_colormap (widget);
  gdkpixmap = gdk_pixmap_colormap_create_from_xpm_d (NULL, colormap, &mask, 
						     NULL, filename );
  pixmap = gtk_pixmap_new (gdkpixmap, mask);
  
  return pixmap;
}

void set_window_icon(GtkWidget *widget, char ** pixmap_data)
{
	GdkPixmap *gdk_pixmap;
	GdkBitmap *gdk_mask;
      
 	gtk_widget_realize(widget);

	gdk_pixmap = gdk_pixmap_create_from_xpm_d (widget->window, &gdk_mask, 
						   NULL,pixmap_data);

	gdk_window_set_icon(widget->window, NULL, gdk_pixmap, gdk_mask);
}

gboolean gtk_destroy_message_box(GtkWidget *widget, gpointer data)
{
  if(GUI.msg_window != NULL)
    {
      gtk_grab_remove(GUI.msg_window);
      gtk_widget_destroy(GUI.msg_window);
      GUI.msg_window = NULL;
    }
  
  return TRUE;
}


void gtk_message_box(gchar * window_title, gchar * frame_title, gchar * msg, 
		     gboolean b_destroy, GtkSignalFunc funct, gpointer data)

{
  GtkWidget *vbox;
  GtkWidget *frame;
  GtkWidget *label;
  GtkWidget *button;

  GUI.msg_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_widget_ref (GUI.msg_window);
  
  gtk_signal_connect(GTK_OBJECT (GUI.msg_window), "destroy",
		     GTK_SIGNAL_FUNC(gtk_destroy_message_box),
		     NULL);
  
  gtk_window_set_title (GTK_WINDOW (GUI.msg_window), window_title);
  gtk_window_set_position (GTK_WINDOW (GUI.msg_window), GTK_WIN_POS_CENTER);

  vbox = gtk_vbox_new (FALSE, 5);
  gtk_container_add (GTK_CONTAINER (GUI.msg_window), vbox);
  gtk_container_set_border_width (GTK_CONTAINER (GUI.msg_window), 5);
  
  frame = gtk_frame_new (frame_title);
  label = gtk_label_new (msg);

  // gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_FILL);
  gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
  gtk_container_add (GTK_CONTAINER (frame), label);
  gtk_container_set_border_width (GTK_CONTAINER (frame), 5);

  gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
  
  button = gtk_button_new_with_label ("Close");
  
  gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 5);
  GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
  gtk_widget_grab_default (button);
  gtk_widget_realize(button);
  gtk_object_set_data_full (GTK_OBJECT (button), "msg_window", GUI.msg_window,
                            (GtkDestroyNotify) gtk_widget_unref);

  if(!b_destroy)
    gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
			       funct,
			       data);
  else
    gtk_signal_connect(GTK_OBJECT (button), "clicked",
		       GTK_SIGNAL_FUNC(gtk_destroy_message_box),
		       NULL);

  gtk_window_set_transient_for(GTK_WINDOW (GUI.msg_window), GTK_WINDOW(GUI.window) );
  gtk_grab_add(GUI.msg_window);
  gtk_widget_show_all (GUI.msg_window);
}

void gtk_quick_question_box(gchar * window_title, gchar * frame_title, gchar * msg,
			    GtkSignalFunc funct, gpointer data)

{
  GtkWidget *vbox;
  GtkWidget *frame;
  GtkWidget *label;
  GtkWidget *button, *button2;
  GtkWidget *hbuttonbox;
  
  GUI.msg_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_widget_ref (GUI.msg_window);
  
  gtk_signal_connect(GTK_OBJECT (GUI.msg_window), "destroy",
		     GTK_SIGNAL_FUNC(gtk_destroy_message_box),
		     NULL);
  
  gtk_window_set_title (GTK_WINDOW (GUI.msg_window), window_title);
  gtk_window_set_position (GTK_WINDOW (GUI.msg_window), GTK_WIN_POS_CENTER);

  vbox = gtk_vbox_new (FALSE, 5);
  gtk_container_add (GTK_CONTAINER (GUI.msg_window), vbox);
  gtk_container_set_border_width (GTK_CONTAINER (GUI.msg_window), 5);
  
  frame = gtk_frame_new (frame_title);
  label = gtk_label_new (msg);

  // gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_FILL);
  gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
  gtk_container_add (GTK_CONTAINER (frame), label);
  gtk_container_set_border_width (GTK_CONTAINER (frame), 5);

  gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
  
  hbuttonbox = gtk_hbutton_box_new ();
  gtk_widget_show (hbuttonbox);
  gtk_box_pack_end(GTK_BOX (vbox), hbuttonbox, FALSE, FALSE, 5);
  gtk_button_box_set_layout (GTK_BUTTON_BOX (hbuttonbox), GTK_BUTTONBOX_SPREAD);

  button2 = gtk_button_new_with_label ("OK");
  gtk_container_add (GTK_CONTAINER (hbuttonbox), button2);
  
  button = gtk_button_new_with_label ("Cancel");
  gtk_container_add (GTK_CONTAINER (hbuttonbox), button);
  GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
  gtk_widget_grab_default (button);
  gtk_widget_realize(button);
  

  gtk_object_set_data_full (GTK_OBJECT (button), "msg_window", GUI.msg_window,
                            (GtkDestroyNotify) gtk_widget_unref);

  if(funct != NULL)
    gtk_signal_connect(GTK_OBJECT (button2), "clicked",
		       funct,
		       data);
  else
    gtk_signal_connect(GTK_OBJECT (button2), "clicked",
		       GTK_SIGNAL_FUNC(gtk_destroy_message_box),
		       NULL);

  gtk_signal_connect(GTK_OBJECT (button), "clicked",
		       GTK_SIGNAL_FUNC(gtk_destroy_message_box),
		       NULL);
  
  gtk_window_set_transient_for(GTK_WINDOW (GUI.msg_window), GTK_WINDOW(GUI.window) );
  gtk_grab_add(GUI.msg_window);
  gtk_widget_show_all (GUI.msg_window);
}
 

gboolean upper_bound_cb(GtkAdjustment *adj, gpointer data)
{
  G_PRINT("\n\n&&&&&&&Upper bound : %f\tValue : %f&&&&&&&&&\n\n", adj->upper, adj->value );
  return TRUE;
}

GtkWidget *get_display_bar(GtkWidget *parent, GtkWidget *vbox_parent)
{
  GtkWidget *handlebox;
  GtkWidget *table;
  GtkWidget *label_state, *label_timer, 
    *label_repeat, *label_keypad;
  
  table = gtk_table_new (1, 8, FALSE);
  gtk_table_set_col_spacings (GTK_TABLE (table), 2);
  
  handlebox = gtk_handle_box_new ();
  gtk_box_pack_end ( GTK_BOX (vbox_parent),
		     handlebox, FALSE, FALSE,0);
 
  label_state = gtk_label_new ("State");
  gtk_label_set_justify (GTK_LABEL (label_state), GTK_JUSTIFY_CENTER);
  gtk_table_attach (GTK_TABLE (table), label_state, 0, 1, 0, 1,
		    GTK_FILL,
		    GTK_FILL, 0, 0);
  gtk_widget_show (label_state);
  
  GUI.statusbar = gtk_statusbar_new();
  GUI.context_id = gtk_statusbar_get_context_id(GTK_STATUSBAR(GUI.statusbar), 
						"DVD Player Statusbar");
  gtk_table_attach (GTK_TABLE (table), GUI.statusbar, 1, 2, 0, 1,
		    GTK_FILL|GTK_EXPAND|GTK_SHRINK,
		    GTK_FILL, 0, 0);
  gtk_widget_show(GUI.statusbar);
    
  label_timer = gtk_label_new ("Timer");
  gtk_label_set_justify (GTK_LABEL (label_timer), GTK_JUSTIFY_CENTER);
  gtk_table_attach (GTK_TABLE (table), label_timer, 2, 3, 0, 1,
		    GTK_FILL,
		    GTK_FILL, 0, 0);
  gtk_widget_show (label_timer);
  
  GUI.timer = gtk_statusbar_new();
  GUI.timer_id = gtk_statusbar_get_context_id(GTK_STATUSBAR(GUI.timer), 
					      "Timer");
  gtk_table_attach (GTK_TABLE (table), GUI.timer, 3, 4, 0, 1,
		    GTK_FILL|GTK_EXPAND|GTK_SHRINK,
		    GTK_FILL, 0, 0);
  gtk_widget_show(GUI.timer);
  
  label_repeat = gtk_label_new ("Repeat Mode");
  gtk_label_set_justify (GTK_LABEL (label_repeat), GTK_JUSTIFY_CENTER);
  gtk_table_attach (GTK_TABLE (table), label_repeat, 4, 5, 0, 1,
		    GTK_FILL,
		    GTK_FILL, 0, 0);
  gtk_widget_show(label_repeat);
  
  GUI.repeat_statusbar = gtk_statusbar_new();
  GUI.repeat_context_id = gtk_statusbar_get_context_id(GTK_STATUSBAR(GUI.repeat_statusbar), 
						       "Repeat Status");
  gtk_table_attach (GTK_TABLE (table), GUI.repeat_statusbar, 5, 6, 0, 1,
		    GTK_FILL|GTK_EXPAND|GTK_SHRINK,
		    GTK_FILL, 0, 0);
  gtk_widget_show(GUI.repeat_statusbar);

  label_keypad = gtk_label_new ("Keypad");
  gtk_label_set_justify (GTK_LABEL (label_keypad), GTK_JUSTIFY_CENTER);
  gtk_table_attach (GTK_TABLE (table), label_keypad, 6, 7, 0, 1,
		    GTK_FILL,
		    GTK_FILL, 0, 0);
  gtk_widget_show(label_keypad);
  
  GUI.keypad_statusbar = gtk_statusbar_new();
  GUI.keypad_context_id = gtk_statusbar_get_context_id(GTK_STATUSBAR(GUI.keypad_statusbar), 
						       "DVD Player Keypad Statusbar");
  gtk_table_attach (GTK_TABLE (table), GUI.keypad_statusbar, 7, 8, 0, 1,
		    GTK_FILL|GTK_EXPAND|GTK_SHRINK,
		    GTK_FILL, 0, 0);
  gtk_widget_show(GUI.keypad_statusbar);
  
  gtk_container_add ( GTK_CONTAINER ( handlebox ) , table);
  gtk_widget_show(table);
  
  return (handlebox);
}

GtkWidget *get_seek_bar(GtkWidget *parent, GtkWidget *vbox_parent)
{
  GtkWidget *seek_scale;
  
  GtkWidget *handlebox;
  GtkWidget *table;
  
  handlebox = gtk_handle_box_new ();
  gtk_box_pack_end ( GTK_BOX (vbox_parent),
		     handlebox, FALSE, FALSE,0);
  
  table = gtk_table_new (1, 2, FALSE);
  gtk_table_set_col_spacings (GTK_TABLE (table), 0);
  
  seek_scale = 
    gtk_hscale_new ( GTK_ADJUSTMENT(gtk_adjustment_new ( 0, 0, SEEK_BAR_DEFAULT_LENGTH, 1, 0, 0)) );
  gtk_widget_ref (seek_scale);
  gtk_object_set_data_full (GTK_OBJECT (parent), "seek_scale", seek_scale,
			    (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show(seek_scale);
  gtk_scale_set_draw_value (GTK_SCALE (seek_scale), FALSE);
//   gtk_scale_set_value_pos(GTK_SCALE (seek_scale), GTK_POS_RIGHT);
  gtk_range_set_update_policy( GTK_RANGE(seek_scale),
			       GTK_UPDATE_DELAYED);
  gtk_table_attach (GTK_TABLE (table), seek_scale, 0, 2, 0, 1,
		    GTK_FILL|GTK_EXPAND|GTK_SHRINK,
		    GTK_FILL,0, 0);

  
  gtk_container_add ( GTK_CONTAINER ( handlebox ) , table);
  gtk_widget_show(table);

  gtk_signal_connect(GTK_OBJECT(GTK_RANGE(seek_scale)->adjustment),"value_changed",
		     GTK_SIGNAL_FUNC(time_search_cb), NULL);
  
  gtk_signal_connect(GTK_OBJECT(GTK_RANGE(seek_scale)->adjustment),"changed",
		     GTK_SIGNAL_FUNC(upper_bound_cb), NULL);

   gtk_signal_connect (GTK_OBJECT (seek_scale), "button-press-event",
                      GTK_SIGNAL_FUNC (time_searching_cb),
                      NULL);

  return (handlebox); 
}

void create_popup_vol_menu()
{
  GtkWidget *volume_scale = NULL;
 
  GUI.popup_vol = gtk_window_new(GTK_WINDOW_POPUP);
  gtk_window_set_policy(GTK_WINDOW(GUI.popup_vol), FALSE, FALSE, TRUE);
  gtk_window_set_position(GTK_WINDOW(GUI.popup_vol), GTK_WIN_POS_MOUSE);
  volume_scale = 
    gtk_vscale_new (GTK_ADJUSTMENT (gtk_adjustment_new ( (VOL_MIN+VOL_MAX)-VOL_RESET, 
							 VOL_MIN, VOL_MAX, 1, 0, 0)));
  gtk_range_set_update_policy( GTK_RANGE(volume_scale),
			       GTK_UPDATE_DISCONTINUOUS);
  gtk_scale_set_draw_value(GTK_SCALE(volume_scale), FALSE);
  gtk_container_add (GTK_CONTAINER (GUI.popup_vol), volume_scale);
  gtk_widget_show (volume_scale);

  gtk_signal_connect(GTK_OBJECT(GTK_RANGE(volume_scale)->adjustment), "value_changed", 
		     GTK_SIGNAL_FUNC(volume_changed_popup_cb), 
		     NULL);
  // gtk_widget_show(GUI.popup_vol);
}

GtkWidget *get_toolbar(GtkWidget *parent, GtkWidget *vbox_parent)
{
  GtkWidget * frewind_button, *play_button, *stop_button, *pause_button, 
    *fforward_button, *prev_button, *next_button, *radio_disc, *radio_file,
    *radio_rtsp, *disc_button, *file_button, *rtsp_button, *eject_button,
    *repeat_button, *repeatAB_button, *vol_button;

  GtkWidget* handlebox;
  GtkWidget *hbox;
  GSList *group;
  
  /* Ok, we need a toolbar, an icon with a mask (one for all of 
     the buttons) and an icon widget to put this icon in (but 
     we'll create a separate widget for each button) */
  GtkWidget * toolbar;
  GtkStyle * style;
  GdkPixmap * icon_play, *icon_stop, *icon_pause, *icon_fforward, 
    *icon_frewind, *icon_prev, *icon_next, *icon_disc, *icon_file,
    *icon_rtsp, *icon_eject, *icon_repeat, *icon_repeatAB, *icon_vol;
  GdkBitmap * mask_play, *mask_stop, *mask_pause, *mask_fforward,
    *mask_frewind, *mask_prev, *mask_next, *mask_disc, *mask_file, 
    *mask_rtsp, *mask_eject, *mask_repeat, *mask_repeatAB, *mask_vol;
  GtkWidget * playw, *stopw, *pausew, *fforwardw, *frewindw, *prevw, 
    *nextw, *file_w, *disc_w, *rtsp_w, *ejectw, *repeat_w, 
    *repeatAB_w, *vol_w;
  
  /* to make it nice we'll put the toolbar into the handle box, 
   * so that it can be detached from the main window */
  handlebox = gtk_handle_box_new ();
  gtk_box_pack_end ( GTK_BOX (vbox_parent),
		       handlebox, FALSE, FALSE,0);
  /* toolbar will be horizontal, with both icons and text, and
   * with 5pxl spaces between items and finally, 
   * we'll also put it into our handlebox */
  toolbar = gtk_toolbar_new ( GTK_ORIENTATION_HORIZONTAL,
			      GTK_TOOLBAR_BOTH);
  gtk_container_set_border_width ( GTK_CONTAINER ( toolbar ) ,0);
  gtk_toolbar_set_space_size ( GTK_TOOLBAR ( toolbar ), 5 );
  hbox = gtk_hbox_new (FALSE,1);
  gtk_box_pack_start (GTK_BOX(hbox),toolbar, TRUE, TRUE, 0);

  gtk_container_add ( GTK_CONTAINER ( handlebox ) , hbox);
  gtk_widget_show(hbox);
  
  /* now we create icon with mask: we'll reuse it to create
   * icon widgets for toolbar items */
  /* Get the style of the button to get the */
  /* background color. */
  style = gtk_widget_get_style(parent);
  
  /* Now on to the xpm stuff */
  
  icon_vol = gdk_pixmap_create_from_xpm_d (parent->window, &mask_vol,
					   &style->bg[GTK_STATE_NORMAL],
					   volume_xpm);
  vol_w = gtk_pixmap_new (icon_vol, mask_vol);

  icon_repeat = gdk_pixmap_create_from_xpm_d (parent->window, &mask_repeat,
					      &style->bg[GTK_STATE_NORMAL],
					      repeat_xpm);
  repeat_w = gtk_pixmap_new (icon_repeat, mask_repeat);

  icon_repeatAB = gdk_pixmap_create_from_xpm_d (parent->window, &mask_repeatAB,
						&style->bg[GTK_STATE_NORMAL],
						repeatAB_xpm);
  repeatAB_w = gtk_pixmap_new (icon_repeatAB, mask_repeatAB);
  
  icon_disc = gdk_pixmap_create_from_xpm_d (parent->window, &mask_disc,
					     &style->bg[GTK_STATE_NORMAL],
					       cd_xpm);
  disc_w = gtk_pixmap_new (icon_disc, mask_disc);

  icon_file = gdk_pixmap_create_from_xpm_d (parent->window, &mask_file,
					    &style->bg[GTK_STATE_NORMAL],
					    fileopen_xpm);
  file_w = gtk_pixmap_new (icon_file, mask_file);

  icon_rtsp = gdk_pixmap_create_from_xpm_d (parent->window, &mask_rtsp,
					    &style->bg[GTK_STATE_NORMAL],
					    rtsp_xpm);
  rtsp_w = gtk_pixmap_new (icon_rtsp, mask_rtsp);
  
  icon_prev = gdk_pixmap_create_from_xpm_d (parent->window, &mask_prev,
					     &style->bg[GTK_STATE_NORMAL],
					       prev_xpm);
  prevw = gtk_pixmap_new (icon_prev, mask_prev);
  
  icon_frewind = gdk_pixmap_create_from_xpm_d (parent->window, &mask_frewind,
					     &style->bg[GTK_STATE_NORMAL],
					       frewind_xpm);
  frewindw = gtk_pixmap_new (icon_frewind, mask_frewind);
  
  icon_play = gdk_pixmap_create_from_xpm_d (parent->window, &mask_play,
					  &style->bg[GTK_STATE_NORMAL],
					    play_xpm);
  playw = gtk_pixmap_new (icon_play, mask_play);
  
  icon_stop = gdk_pixmap_create_from_xpm_d (parent->window, &mask_stop,
					  &style->bg[GTK_STATE_NORMAL],
					    stop_xpm);
  stopw = gtk_pixmap_new (icon_stop, mask_stop);
  
  icon_pause = gdk_pixmap_create_from_xpm_d (parent->window, &mask_pause,
						 &style->bg[GTK_STATE_NORMAL],
					     pause_xpm);
  pausew = gtk_pixmap_new (icon_pause, mask_pause);
  
  icon_fforward = gdk_pixmap_create_from_xpm_d (parent->window, &mask_fforward,
						  &style->bg[GTK_STATE_NORMAL],
						fforward_xpm);
  fforwardw = gtk_pixmap_new (icon_fforward, mask_fforward);
  
  icon_next = gdk_pixmap_create_from_xpm_d (parent->window, &mask_next,
					     &style->bg[GTK_STATE_NORMAL],
					       next_xpm);
  nextw = gtk_pixmap_new (icon_next, mask_next);

  icon_eject = gdk_pixmap_create_from_xpm_d (parent->window, &mask_eject,
					     &style->bg[GTK_STATE_NORMAL],
					     eject_xpm);
  ejectw = gtk_pixmap_new (icon_eject, mask_eject);
    
  radio_disc = gtk_radio_button_new (NULL);
  gtk_widget_show(radio_disc);
  group = gtk_radio_button_group (GTK_RADIO_BUTTON (radio_disc));
  
  radio_file = gtk_radio_button_new (group);
  gtk_widget_show(radio_file);

  radio_rtsp = gtk_radio_button_new(gtk_radio_button_group (GTK_RADIO_BUTTON (radio_file)));
  gtk_widget_show(radio_rtsp);
  
  
  prev_button =
    gtk_toolbar_append_item ( GTK_TOOLBAR (toolbar), /* our toolbar */
			      NULL,               /* button label */
			      "Previous Chapter/Track",   /* this button's tooltip */
			      "Previous",     /* tooltip private info */
			      prevw,                 /* icon widget */
			      GTK_SIGNAL_FUNC (prev_cb), /* a signal */
			      NULL );

  frewind_button = 
    gtk_toolbar_append_item ( GTK_TOOLBAR (toolbar), /* our toolbar */
			      NULL,               /* button label */
			      "F Rewind",   /* this button's tooltip */
			      "F Rewind",     /* tooltip private info */
			      frewindw,                 /* icon widget */
			      GTK_SIGNAL_FUNC (frewind_cb), /* a signal */
			      NULL );
  
  play_button = 
    gtk_toolbar_append_item ( GTK_TOOLBAR (toolbar), /* our toolbar */
			      NULL,               /* button label */
			      "Play",   /* this button's tooltip */
			      "Play",          /* tooltip private info */
			      playw,                 /* icon widget */
			      GTK_SIGNAL_FUNC (play_cb), /* a signal */
			      NULL );
  stop_button = 
    gtk_toolbar_append_item ( GTK_TOOLBAR (toolbar), /* our toolbar */
			      NULL,               /* button label */
			      "Stop",   /* this button's tooltip */
			      "Stop",          /* tooltip private info */
			      stopw,                 /* icon widget */
			      GTK_SIGNAL_FUNC (stop_cb), /* a signal */
			      NULL );
  
  pause_button = 
    gtk_toolbar_append_item ( GTK_TOOLBAR (toolbar), /* our toolbar */
			      NULL,               /* button label */
			      "Pause",   /* this button's tooltip */
			      "Pause",          /* tooltip private info */
			      pausew,                 /* icon widget */
			      GTK_SIGNAL_FUNC (pause_cb), /* a signal */
			      NULL );
  fforward_button = 
    gtk_toolbar_append_item ( GTK_TOOLBAR (toolbar), /* our toolbar */
			      NULL,               /* button label */
			      "F Forward",   /* this button's tooltip */
			      "F Forward",          /* tooltip private info */
			      fforwardw,                 /* icon widget */
			      GTK_SIGNAL_FUNC (fforward_cb), /* a signal */
			      NULL );
  next_button =
    gtk_toolbar_append_item ( GTK_TOOLBAR (toolbar), /* our toolbar */
			      NULL,               /* button label */
			      "Next Chapter/Track",   /* this button's tooltip */
			      "Next",     /* tooltip private info */
			      nextw,                 /* icon widget */
			      GTK_SIGNAL_FUNC (next_cb), /* a signal */
			      NULL );

  eject_button =
    gtk_toolbar_append_item ( GTK_TOOLBAR (toolbar), /* our toolbar */
			      NULL,               /* button label */
			      "Eject Disc",   /* this button's tooltip */
			      "Eject",     /* tooltip private info */
			      ejectw,                 /* icon widget */
			      GTK_SIGNAL_FUNC (eject_cb), /* a signal */
			      NULL );
  
  gtk_toolbar_append_space ( GTK_TOOLBAR ( toolbar));/* space after item */
  gtk_toolbar_set_space_size(GTK_TOOLBAR ( toolbar), 5);

  repeat_button = 
    gtk_toolbar_append_item ( GTK_TOOLBAR (toolbar), /* our toolbar */
			      NULL,               /* button label */
			      "Repeat Mode",   /* this button's tooltip */
			      "Repeat Mode",     /* tooltip private info */
			      repeat_w,                 /* icon widget */
			      GTK_SIGNAL_FUNC (repeat_mode_cb), /* a signal */
			      GUINT_TO_POINTER(HANDLE_REPEAT));
  repeatAB_button = 
    gtk_toolbar_append_item ( GTK_TOOLBAR (toolbar), /* our toolbar */
			      NULL,               /* button label */
			      "Repeat AB Mode",   /* this button's tooltip */
			      "Repeat AB Mode",     /* tooltip private info */
			      repeatAB_w,                 /* icon widget */
			      GTK_SIGNAL_FUNC (repeat_mode_cb), /* a signal */
			      GUINT_TO_POINTER(HANDLE_REPEATAB));
  
  

  gtk_toolbar_append_space ( GTK_TOOLBAR ( toolbar));/* space after item */
  gtk_toolbar_set_space_size(GTK_TOOLBAR ( toolbar), 10);

  disc_button =
    gtk_toolbar_append_element ( GTK_TOOLBAR (toolbar), /* our toolbar */
				 GTK_TOOLBAR_CHILD_RADIOBUTTON, /* button style */
				 GTK_WIDGET(radio_disc),
				 NULL,               /* button label */
				 "DVD,(S)VCD,CDDA playing",   /* this button's tooltip */
				 "DISC Mode",     /* tooltip private info */
				 disc_w,                 /* icon widget */
				 GTK_SIGNAL_FUNC (change_player_mode_cb), /* a signal */
				 GUINT_TO_POINTER(DRIVE_NUMBER) );

  file_button =
    gtk_toolbar_append_element ( GTK_TOOLBAR (toolbar), /* our toolbar */
				 GTK_TOOLBAR_CHILD_RADIOBUTTON, /* button style */
				 GTK_WIDGET(radio_file),
				 NULL,               /* button label */
				 "FILE playing",   /* this button's tooltip */
				 "FILE Mode",     /* tooltip private info */
				 file_w,                 /* icon widget */
				 GTK_SIGNAL_FUNC (change_player_mode_cb), /* a signal */
				 GUINT_TO_POINTER(LOCALFILE) );
  rtsp_button =
    gtk_toolbar_append_element ( GTK_TOOLBAR (toolbar), /* our toolbar */
				 GTK_TOOLBAR_CHILD_RADIOBUTTON, /* button style */
				 GTK_WIDGET(radio_rtsp),
				 NULL,               /* button label */
				 "RTSP streaming",   /* this button's tooltip */
				 "STREAMING Mode",     /* tooltip private info */
				 rtsp_w,                 /* icon widget */
				 GTK_SIGNAL_FUNC (change_player_mode_cb), /* a signal */
				 GUINT_TO_POINTER(RTSPURL) );

  gtk_toolbar_append_space ( GTK_TOOLBAR ( toolbar));/* space after item */
  gtk_toolbar_set_space_size(GTK_TOOLBAR ( toolbar), 20);
  
  vol_button =
    gtk_toolbar_append_item ( GTK_TOOLBAR (toolbar), /* our toolbar */
			      NULL,               /* button label */
			      "Volume",   /* this button's tooltip */
			      "Volume",          /* tooltip private info */
			      vol_w,                 /* icon widget */
			      GTK_SIGNAL_FUNC (vol_popup_cb), /* a signal */
			      NULL );
  
  gtk_widget_ref (disc_button);
  gtk_object_set_data_full (GTK_OBJECT (handlebox), "disc_button", disc_button,
			    (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_ref (file_button);
  gtk_object_set_data_full (GTK_OBJECT (handlebox), "file_button", file_button,
			      (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_ref (rtsp_button);
  gtk_object_set_data_full (GTK_OBJECT (handlebox), "rtsp_button", rtsp_button,
			    (GtkDestroyNotify) gtk_widget_unref);
#ifdef _VOD_
#else
  gtk_widget_hide(rtsp_button); 
#endif
  
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (disc_button), TRUE);
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (file_button), FALSE);
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (rtsp_button), FALSE);
  
  gtk_widget_show (toolbar);
  
  return (handlebox);
}



/* Create a new hbox with an image and a label packed into it */
/* and return the box. */

GtkWidget *xpm_button( GtkWidget *parent,
		       gchar     * xpm_filename)
{
  GtkWidget *button1;
  GtkWidget *pixmapwid;
  GdkPixmap *pixmap;
  GdkBitmap *mask;
  GtkStyle *style;
  
  /* Create a new button */
  button1 = gtk_button_new ();
  
  /* Realize the button - to have his style... */
  /* gtk_widget_realize(button1); */
  /* Get the style of the button to get the */
  /* background color. */
  style = gtk_widget_get_style(parent);
  
  /* Now on to the xpm stuff */
  pixmap = gdk_pixmap_create_from_xpm (parent->window, &mask,
				       &style->bg[GTK_STATE_NORMAL],
				       xpm_filename);
  pixmapwid = gtk_pixmap_new (pixmap, mask);
  
  gtk_container_add (GTK_CONTAINER (button1), pixmapwid);
  
  gtk_widget_show(pixmapwid);
    
    return( button1 );
}

gboolean gtk_destroy_file_selector(GtkWidget *widget, gpointer data)
{
  
  if(GUI.filew != NULL)
    {
      gtk_grab_remove(GUI.filew);
      gtk_widget_destroy(GUI.filew);
      GUI.filew = NULL;
    }
  
  return TRUE;
}

/* The Gtk File Selector */
void get_file_selector( GtkWidget  *window )
{
/* I test if there is already a visible or an iconified window of the file selector */
 if (GUI.filew != NULL && GUI.filew->window != NULL)
   {
     gdk_window_show(GUI.filew->window);
     gdk_window_raise(GUI.filew->window);
   }
 else
   {
     
     /* Create a new file selection widget */
     GUI.filew = gtk_file_selection_new ("Open a file ...");

     /* Setting the file selector in a modal bahaviour */
     /*gtk_window_set_modal(filew,TRUE);*/

     gtk_signal_connect(GTK_OBJECT (GUI.filew), "destroy",
			GTK_SIGNAL_FUNC(gtk_destroy_file_selector) ,
			NULL);
     /* Connect the ok_button to file_ok_sel function */
     gtk_signal_connect (GTK_OBJECT (GTK_FILE_SELECTION (GUI.filew)->ok_button),
		      "clicked", (GtkSignalFunc) file_ok_sel, GUI.filew );
     
     /* Connect the cancel_button to destroy the widget */
     gtk_signal_connect_object(GTK_OBJECT (GTK_FILE_SELECTION
					   (GUI.filew)->cancel_button),
			       "clicked", 
			       GTK_SIGNAL_FUNC(gtk_destroy_file_selector),
			       NULL);
     
     /* Lets set the filename, as if this were a save dialog, and we are giving
     a default filename */
     if(Info.sFilename == NULL)
       gtk_file_selection_set_filename (GTK_FILE_SELECTION(GUI.filew),
					"sample.mpg");
     else
       gtk_file_selection_set_filename (GTK_FILE_SELECTION(GUI.filew), 
					Info.sFilename);
     
     
     set_window_icon(GUI.filew,fileopen_xpm);
     gtk_window_set_transient_for(GTK_WINDOW(GUI.filew), GTK_WINDOW(GUI.window) );
     gtk_grab_add(GUI.filew);
     gtk_widget_show(GUI.filew);
     gtk_file_selection_hide_fileop_buttons(GTK_FILE_SELECTION(GUI.filew));
   }
}


/* This is the GtkItemFactoryEntry structure used to generate new menus.
   Item 1: The menu path. The letter after the underscore indicates an
           accelerator key once the menu is open.
   Item 2: The accelerator key for the entry
   Item 3: The callback function.
   Item 4: The callback action.  This changes the parameters with
           which the function is called.  The default is 0.
   Item 5: The item type, used to define what kind of an item it is.
           Here are the possible values:

           NULL               -> "<Item>"
           ""                 -> "<Item>"
           "<Title>"          -> create a title item
           "<Item>"           -> create a simple item
           "<CheckItem>"      -> create a check item
           "<ToggleItem>"     -> create a toggle item
           "<RadioItem>"      -> create a radio item
           <path>             -> path of a radio item to link against
           "<Separator>"      -> create a separator
           "<Branch>"         -> create an item to hold sub items (optional)
           "<LastBranch>"     -> create a right justified branch 
*/

void help_keyboard_shortcuts_cb (GtkWidget *widget, gpointer data)
{

  gtk_message_box("Help",
		  "Keyboard Shortcuts",
		  g_strconcat("Playing controls:\n",
			      "'Play'       -> P\n",
			      "'Pause'      -> <Alt>+P\n",
			      "'Stop'       -> S\n",
			      "'Close'      -> <Control>+C\n",
			      "'Previous'   -> B\n",
			      "'Next'       -> N\n",
			      "'Rewind'     -> R\n",
			      "'Forward'    -> F\n",
			      "\n",
			      "Fullscreen mode:\n",
			      "'On'    -> <Control>+F\n",
			      "           or, mouse left\n", 
			      "           button double click.\n",
			      "'Off'   -> <Escape> or <Space>\n",
			      "           or, mouse left button\n",
			      "           simple click\n",
			      "\n",
			      "To get this help window:\n",
			      "<Control>+K\n",
			      NULL),
		  TRUE,
		  NULL,
		  NULL);
}

static GtkItemFactoryEntry menu_items[] = {
  { "/_File",                 NULL,            NULL,              0, "<Branch>" },
  { "/File/_Open a file",            "<control>O",    get_file_selector, 0, NULL },
#ifdef _VOD_
  { "/File/Open an _URL",            "<control>U",      get_VOD_playing, 0, NULL },
#endif
  { "/File/_Close",           "<control>C",    close_cb,          0, NULL },
  { "/File/sep1",             NULL,            NULL,              0, "<Separator>" },
  { "/File/_Quit",            "<control>Q",    gtk_main_quit,     0, NULL },
  { "/_Controls",             NULL,            NULL,              0, "<Branch>" },
  { "/Controls/_Play",        "P",             play_cb,           0, NULL },
  { "/Controls/_Stop",        "S",             stop_cb,           0, NULL },
  { "/Controls/_Pause",       "<alt>P",        pause_cb,          0, NULL },
  { "/Controls/Previous",     "B",             prev_cb,           0, NULL },
  { "/Controls/_Next",        "N",             next_cb,           0, NULL },
  { "/Controls/_Rewind",      "R",             frewind_cb,        0, NULL },
  { "/Controls/_Forward",     "F",             fforward_cb,       0, NULL },
/*{ "/Controls/Resume",       NULL,            toggle_resume_mode_cb,       0, NULL },*/
  { "/_DVD-(S)VCD",                   NULL,            NULL, 0,  "<Branch>" },
  { "/DVD-(S)VCD/Menu",           NULL,            NULL,                 0,  "<Branch>" },
  { "/DVD-(S)VCD/Menu/_Root",     "<control>M",            dvd_menu_call_cb,     DVD_ROOT_ID,          NULL},
  { "/DVD-(S)VCD/Menu/_Title",    "<control>T",            dvd_menu_call_cb,     DVD_TITLE_ID,         NULL},
  { "/DVD-(S)VCD/Menu/Chapter",   "<control>H",            dvd_menu_call_cb,     DVD_PTT_ID,           NULL},
  { "/DVD-(S)VCD/Menu/Angle",     "<control>G",            dvd_menu_call_cb,     DVD_ANGLE_ID,         NULL},
  { "/DVD-(S)VCD/Menu/Audio",     "<control>U",            dvd_menu_call_cb,     DVD_AUDIO_ID,         NULL},
  { "/DVD-(S)VCD/Menu/Subtitle",  "<control>B",            dvd_menu_call_cb,     DVD_SUBPICTURE_ID,    NULL},
  { "/DVD-(S)VCD/Menu/Resume From Menu",    "<control>R",            dvd_menu_call_cb,     FMP_RESUME,           NULL},
  { "/DVD-(S)VCD/Navigation",         NULL,            NULL, 0,  "<Branch>" },
  { "/DVD-(S)VCD/Navigation/Up",      "KP_8",          dvd_navigation_cb,     UP,         NULL},
  { "/DVD-(S)VCD/Navigation/Down",    "KP_2",          dvd_navigation_cb,     DOWN,       NULL},
  { "/DVD-(S)VCD/Navigation/Left",    "KP_4",          dvd_navigation_cb,     LEFT,       NULL},
  { "/DVD-(S)VCD/Navigation/Right",   "KP_6",          dvd_navigation_cb,     RIGHT,      NULL},
  { "/DVD-(S)VCD/Navigation/Select",  "KP_Enter",      dvd_navigation_cb,     SEL,     NULL},
  { "/DVD-(S)VCD/Change _Language",   "<control>L",    dvd_change_langage_cb, 0,   NULL },
  { "/DVD-(S)VCD/Change _Subtitles",  "<control>S",    dvd_change_subtitles_cb, 0, NULL },
  { "/DVD-(S)VCD/Change _Angle",      "<control>A",    dvd_change_angle_cb, 0, NULL },
  { "/DVD-(S)VCD/Co_nfiguration",     "<control>N",    get_dvd_options_window, 0, NULL },
  { "/_View",                 NULL,            NULL,                       0, "<Branch>" },
  { "/View/Toolbar",          NULL,            set_gui_element_visible_cb, TOOLBAR, "<CheckItem>" },
  { "/View/Slider",           NULL,            set_gui_element_visible_cb, SLIDER, "<CheckItem>" },
  { "/View/Keypad",           NULL,            set_gui_element_visible_cb, KEYPAD, "<CheckItem>" },
  { "/View/Statusbar",        NULL,            set_gui_element_visible_cb, STATUSBAR, "<CheckItem>" },
  { "/View/Playlist",         NULL,            set_gui_element_visible_cb, PLAYLIST, "<CheckItem>" },
  { "/View/sep2",             NULL,            NULL, 0, "<Separator>" },
  { "/View/_FullScreen",      "<control>F",    fullscreen_on_cb , 0, NULL },
  { "/View/sep3",             NULL,            NULL, 0, "<Separator>" },
  { "/View/_Settings",      "<alt>S",        get_options_window, 0, NULL },
  { "/_Help",                  NULL,           NULL, 0, "<LastBranch>" },
  { "/Help/_Key Shortcuts",    "<control>K",   help_keyboard_shortcuts_cb, 0, NULL },
  { "/Help/sep3",              NULL,           NULL, 0, "<Separator>" },
  { "/Help/_About",            "<alt>A" ,  help_about_cb, 0, NULL },
};


void get_main_menu( GtkWidget  *window,
                    GtkWidget **menubar )
{
  GtkAccelGroup *accel_group;
  gint nmenu_items = sizeof (menu_items) / sizeof (menu_items[0]);

  accel_group = gtk_accel_group_new ();

  /* This function initializes the item factory.
     Param 1: The type of menu - can be GTK_TYPE_MENU_BAR, GTK_TYPE_MENU,
              or GTK_TYPE_OPTION_MENU.
     Param 2: The path of the menu.
     Param 3: A pointer to a gtk_accel_group.  The item factory sets up
              the accelerator table while generating menus.
  */

  GUI.item_factory = gtk_item_factory_new (GTK_TYPE_MENU_BAR, "<main>", 
				       accel_group);

  /* This function generates the menu items. Pass the item factory,
     the number of items in the array, the array itself, and any
     callback data for the the menu items. */
  gtk_item_factory_create_items ( GUI.item_factory, nmenu_items, menu_items, NULL);

  /* Attach the new accelerator group to the window. */
  gtk_window_add_accel_group (GTK_WINDOW (window), accel_group);

  if (menubar)
    /* Finally, return the actual menu bar created by the item factory. */ 
    *menubar = gtk_item_factory_get_widget ( GUI.item_factory, "<main>");
}


GtkWidget*
create_window(void)
{
  GtkWidget *window;
  GtkWidget *drawingarea;
  GtkWidget *main_vbox;
  GtkWidget *menubar;
  GtkWidget *handlebox, *handlebox2,
    *handlebox3, *handlebox4;
  
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_policy(GTK_WINDOW(window), FALSE, TRUE, FALSE);
  gtk_window_set_title (GTK_WINDOW (window), GTKPLAYERNAME );
  gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
  gtk_window_set_default_size (GTK_WINDOW (window), 640, 480);

  drawingarea = gtk_drawing_area_new ();
  gtk_widget_ref (drawingarea);
  gtk_object_set_data_full (GTK_OBJECT (window), "drawingarea", drawingarea,
                            (GtkDestroyNotify) gtk_widget_unref);

  /* Connecting callbacks to the TOP-LEVEL Window and to the Drawingarea */
  
  gtk_signal_connect (GTK_OBJECT (drawingarea), "configure_event",
		      GTK_SIGNAL_FUNC (window_move_drawingarea),
		      NULL);
  
  gtk_signal_connect (GTK_OBJECT (drawingarea), "button-press-event",
                      GTK_SIGNAL_FUNC (deal_with_pointer_cb),
                      NULL);
  gtk_signal_connect (GTK_OBJECT (window), "key-press-event",
		      GTK_SIGNAL_FUNC (deal_with_pointer_cb),
		      NULL);
  
  gtk_signal_connect (GTK_OBJECT (window), "configure_event",
                      GTK_SIGNAL_FUNC (window_move_toplevel),
                      NULL);
  gtk_signal_connect (GTK_OBJECT (window), "delete_event",
                      GTK_SIGNAL_FUNC (gtk_main_quit),
                      NULL);
  gtk_signal_connect (GTK_OBJECT (window), "destroy", 
		      GTK_SIGNAL_FUNC (gtk_main_quit), 
		      "WM destroy");
  
  set_window_icon(window,REALmagic_xpm);
  /* realize, map and show the drawing area */
  gtk_widget_show (drawingarea);
  GTK_WIDGET_SET_FLAGS (drawingarea, GTK_CAN_FOCUS);
  gtk_widget_add_events(drawingarea, GDK_KEY_PRESS_MASK);
  gtk_widget_add_events(drawingarea, GDK_KEY_RELEASE_MASK);
  gtk_widget_add_events(drawingarea, GDK_BUTTON_PRESS_MASK);
  gtk_widget_add_events(drawingarea, GDK_POINTER_MOTION_MASK);
  gtk_widget_add_events(drawingarea, GDK_POINTER_MOTION_HINT_MASK);

  /* V Box to pack the menubar, the drawing area and the toolbar */
  main_vbox = gtk_vbox_new (FALSE, 1);
  /* V Box's border width setting */
  gtk_container_border_width (GTK_CONTAINER (main_vbox), 1);
  /* adding the V Box in the TOP-LEVEL window */
  gtk_container_add (GTK_CONTAINER (window), main_vbox);
  /* realize, map and show the V Box */
  gtk_widget_show (main_vbox);
  /* menubar by GtkItemFactory */
  get_main_menu (window, &menubar);
  /* pack into the V Box */
  gtk_box_pack_start (GTK_BOX (main_vbox), menubar, FALSE, TRUE, 0);

  /* toolbar in a handlebox */
  handlebox4 = get_display_bar(window,main_vbox);
  gtk_widget_ref (handlebox4);
  gtk_object_set_data_full (GTK_OBJECT (window), "statusbarbox", handlebox4,
                            (GtkDestroyNotify) gtk_widget_unref);
  handlebox3 = get_keypad_toolbar(window,main_vbox);
  gtk_widget_ref (handlebox3);
  gtk_object_set_data_full (GTK_OBJECT (window), "keypad", handlebox3,
                            (GtkDestroyNotify) gtk_widget_unref);
  handlebox2 = get_seek_bar(window,main_vbox);
  gtk_widget_ref (handlebox2);
  gtk_object_set_data_full (GTK_OBJECT (window), "seekbar", handlebox2,
                            (GtkDestroyNotify) gtk_widget_unref);
  handlebox  = get_toolbar(window,main_vbox);
  gtk_widget_ref (handlebox);
  gtk_object_set_data_full (GTK_OBJECT (window), "toolbar", handlebox,
                            (GtkDestroyNotify) gtk_widget_unref);

  /* pack into the V Box */
  gtk_box_pack_start (GTK_BOX (main_vbox), drawingarea, TRUE, TRUE, 0);

  
  /* showing the menubar and the handlebox with the toolbar */
  gtk_widget_show (menubar);
  gtk_widget_realize (handlebox);
  gtk_widget_realize (handlebox2);
  gtk_widget_realize (handlebox3);
  gtk_widget_realize (handlebox4);
  /* gtk_widget_hide (handlebox); */
/*   gtk_widget_hide (handlebox2); */
/*   gtk_widget_hide (handlebox3); */
/*   gtk_widget_hide (handlebox4); */

  return window;
}
