/*****************************************************************************/
/*
 *      callbacks.c -- REALmagic simple windowing application in GTK
 *
 *      Copyright (C) 2000-2002 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 "manageconfig.h"

// cannot #include <X11/extensions/xf86vmode.h> because BOOL and BYTE conflict.
Bool XF86VidModeGetViewPort(
			    Display *display,
			    int screen,
			    int *x_return,
			    int *y_return);

gboolean loadconfig(FMPOVERLAYVDO *ovl)
{  
	
  if (!loadOverlayConfig(ovl)) 
    {
      gtk_message_box("Warning","AnalogOverlay Autocalibration",
		      "Push the 'Close' button to start the calibration for this screen resolution.\r
If your monitor resolution is less than root
 window resolution (using ViewPort) please join the upper left corner.
\rYour monitor will be blanked out for a few seconds..." ,
		      FALSE,
		      GTK_SIGNAL_FUNC(autocalibrateAndStartPlaying),
		      ovl);
      return FALSE;
    }
  return TRUE;
}

gboolean deal_with_pointer_cb(GtkWidget *widget, GdkEvent*event, gpointer data)
{
  G_MESSAGE("Allloooo !!\n");
  if(!ovl.b_fullscreen &&
     (event->type == GDK_2BUTTON_PRESS ||
      event->type == GDK_3BUTTON_PRESS) 
     )
    {
      fullscreen_on_cb (widget,event,data);
    }
  else if(event->type == GDK_BUTTON_PRESS ||
	  event->type == GDK_KEY_PRESS ||
	  event->type == GDK_KEY_RELEASE 
	  )
    {
      fullscreen_off_cb (GTK_WIDGET(widget),event,data);
    }
  
  return FALSE;
}

void fullscreen_on_cb (GtkWidget *widget, GdkEvent*event, gpointer data)
{ 
  gint xof,yof;
  GtkWidget * draw;
  
  draw = GTK_WIDGET(gtk_object_get_data (GTK_OBJECT (GUI.window), "drawingarea"));

  if(ovl.b_overlay_access 
     && ovl.b_vga_cable_present 
     && !ovl.b_fullscreen)
    {
      while (gdk_pointer_grab(GUI.window->window, TRUE, 
			      /* GDK_ALL_EVENTS_MASK, */
			      GDK_BUTTON_PRESS_MASK,
			      draw->window, 
			      NULL, 
			      GDK_CURRENT_TIME))
	sleep(1);
      ovl.b_fullscreen = TRUE;
      
      if (!XF86VidModeGetViewPort(ovl.dpy,ovl.scr,&xof,&yof)) 
	{ 
	  gtk_message_box("Fatal Error","Cannot get View Port",
			  "There is trouble with your X server.
\rThe application will be closed.", 
			  FALSE,
			  GTK_SIGNAL_FUNC(gtk_main_quit),
			  NULL);
	}
      else
	{
	  prepareForFullScreen();
	  setOverlay(1,UNUSED_ARG); 
	  ovl.b_vga = TRUE;
	  if(!ovl.b_cfg_loaded)
	    loadconfig(&ovl);
	  setOverlayDestination ((DWORD)(0-xof),(DWORD)(0-yof),
				 (DWORD)ovl.xres,(DWORD)ovl.yres);
	}
    }
}

gboolean fullscreen_off_cb (GtkWidget *widget, GdkEvent *event,gpointer data)
{ 
  if(ovl.b_fullscreen && 
     (event->button.type == GDK_BUTTON_PRESS ||
      event->key.keyval == GDK_Escape ||
      event->key.keyval == GDK_space ))
    {
      gdk_pointer_ungrab(GDK_CURRENT_TIME);
      ovl.b_fullscreen = FALSE;
      // Set overlay mode
      setOverlay(2, ovl.key_color);
      window_move_toplevel(GUI.window,NULL,NULL);
    }
  return FALSE;
}

void set_playlist_size_and_position_cb(GtkWidget       *widget)
{
  if(GUI.playlistwin != NULL)
    {
      if(GTK_WIDGET_VISIBLE(GUI.playlistwin))
	{
	  gint xx,yy,wwidth,hheight;
	  
	  gdk_window_get_origin(widget->window , &xx ,&yy);
	  gdk_window_get_size(widget->window ,&wwidth, &hheight);
  
	  gtk_widget_set_uposition(GTK_WIDGET(GUI.playlistwin), xx, yy+hheight+30);
	  gtk_widget_set_usize(GTK_WIDGET(GUI.playlistwin), wwidth, -1);
	}
    }
}


gboolean
window_move_toplevel                            (GtkWidget       *widget,
					GdkEventConfigure *event,
					gpointer         user_data)
{
  gint x,y,xof,yof,width,height;
  GtkWidget * draw;
  
  set_playlist_size_and_position_cb(widget);
  
  draw = GTK_WIDGET(gtk_object_get_data (GTK_OBJECT (widget), "drawingarea"));
  gdk_window_get_origin(draw->window , &x ,&y);
  gdk_window_get_size(draw->window ,&width,&height);

  // G_MESSAGE("New position window x=%d y=%d size w = %d  h = %d\n",x,y,width,height);
  // be careful of viewport.
  if (!XF86VidModeGetViewPort(ovl.dpy,ovl.scr,&xof,&yof)) 
    {
      gtk_message_box("Fatal Error","Cannot get View Port",
			"There is trouble with your X server.
\rThe application will be closed.",
		      FALSE,
		      GTK_SIGNAL_FUNC(gtk_main_quit),
		      NULL );
       
    }
  else
    {
      // change the overlay position and overlay size
      if(ovl.b_overlay_access && ovl.b_vga && !ovl.b_fullscreen)
	{
	  setOverlayDestination ((DWORD)(x-xof),(DWORD)(y-yof),(DWORD)width,(DWORD)height);
	}
    }
      return FALSE;
}

gboolean
window_move_drawingarea                            (GtkWidget       *widget,
					GdkEventConfigure *event,
					gpointer         user_data)
{
  gint x,y,xof,yof,width,height;
  DWORD FMPreturn;
  
  gdk_window_get_origin(widget->window , &x ,&y);
  gdk_window_get_size(widget->window ,&width,&height);

  // G_MESSAGE("New position window x=%d y=%d size w = %d  h = %d\n",x,y,width,height);
  // be careful of viewport.
  if (!XF86VidModeGetViewPort(ovl.dpy,ovl.scr,&xof,&yof))
    {
      gtk_message_box("Fatal Error","Cannot get View Port",
		      "There is trouble with your X server.\n
The application will be closed.",
		      FALSE,
		      GTK_SIGNAL_FUNC(gtk_main_quit),
		      NULL );
    }
  else
    {
      // change the overlay position and overlay size
      if(ovl.b_overlay_access && ovl.b_vga && !ovl.b_fullscreen)
	{
	  FMPreturn = setOverlayDestination ((DWORD)(x-xof),(DWORD)(y-yof),(DWORD)width,(DWORD)height);
	}
      // G_MESSAGE("Change the window size or position return: %ld",FMPreturn);
    }
      return FALSE;
}

void file_ok_sel( GtkWidget        *w,
                  GtkFileSelection       *fs)
{
  gchar * tmpfilename = NULL;
  
  gtk_widget_hide(GTK_WIDGET(fs));

  tmpfilename = 
    gtk_file_selection_get_filename (GTK_FILE_SELECTION (fs));
  
  if(GUI.playlistwin == NULL)
    {
      GtkWidget *playlist_check = NULL;

      GUI.playlistwin = create_playlistwin();
      gtk_widget_show(GUI.playlistwin);
      set_playlist_size_and_position_cb(GUI.window);
      load_playlist(NULL);
      
      playlist_check = gtk_item_factory_get_widget ( GUI.item_factory, "/View/Playlist");
      if(playlist_check != NULL)
	gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (playlist_check), TRUE);
    }
  
  playlist_add(tmpfilename);
  GUI.playlist_selected_row = GUI.playlist_length - 1;
  playlist_play();
  
  gtk_destroy_file_selector(NULL, NULL);
}

gboolean playfile(gchar *tmpfilename)
{
  if(Info.bDiscType != LOCALFILE)
    toggle_player_mode(LOCALFILE);
  
  g_free(Info.sFilename);
  Info.sFilename = g_strdup(tmpfilename);
  
  if(checkFile(Info.sFilename))
    {
      DWORD tmp = Info.dwStreamtype;
      getMpegFileProperties(&Info);
      
      if(Info.dwStreamtype != tmp &&
	 Info.playing != CLOSED)
	{
	  close_cb(NULL, NULL);
	  usleep(WAIT_FOR_FMPCLOSE); 
	}
      else if(Info.dwStreamtype == tmp &&
	      Info.playing > STOPPED)
	stop_cb(NULL, NULL);
      
      play_cb(NULL, NULL);
            
      gtk_window_set_title (GTK_WINDOW (GUI.window), 
			    g_strconcat(GTKPLAYERNAME, " : ",
					g_basename((gchar*)Info.sFilename), NULL) );
      return TRUE;
    }
  else
    return FALSE;
}

gboolean checkFile(char * sFilename)
{
  int fd = -1;

  if(sFilename == "" || 
     sFilename == NULL)
    return FALSE;

  fd = open(sFilename,O_RDONLY|O_LARGEFILE);
   
  if(fd == -1)
    {
      gtk_message_box("Error","File error",
		      g_strconcat("The file ",(gchar*)sFilename," couldn't be opened ! Sorry.",NULL),
		      TRUE, NULL, NULL );
      return FALSE;
    }
  close(fd);
  return TRUE;
}

gboolean checkFileValidity(INFO * info, gchar * sFilename)
{
  int fd = -1;
  
  if(sFilename == "" || 
     sFilename == NULL)
    return FALSE;
  
  fd = open(sFilename,O_RDONLY|O_LARGEFILE);
  if(fd == -1)
    return FALSE;
  close(fd);
  
  info->dwStreamtype = getStreamType(sFilename);
  
  if(info->dwStreamtype == UNDETERMINED_STREAM_TYPE ||
     info->dwStreamtype == UNSUPPORTED_STREAM_TYPE)
    return FALSE;
  
  g_free(info->sFilename);
  info->sFilename = strdup(sFilename);
  
  return TRUE;
}

gboolean getMpegFileProperties(INFO * info)
{
  info->dwStreamtype = getStreamType(info->sFilename);
 
  if(info->dwStreamtype == UNDETERMINED_STREAM_TYPE)
    {
      gtk_message_box("Warning","Undetermined Stream Type",
		      g_strconcat("Unable to determine the type of ",
				  info->sFilename,
				  "\rIt'll be opened as a SYSTEM stream. Sorry.",NULL),
		      TRUE, NULL, NULL ); 
      info->dwStreamtype = FMPF_SYSTEM;
      return FALSE;
    }
  else if(info->dwStreamtype == UNSUPPORTED_STREAM_TYPE)
    {
      gtk_message_box("Warning","Unsupported Stream Type",
		      g_strconcat("'",info->sFilename, "'",
				  " type is not supported.",
				  "\rIt'll be opened as a SYSTEM stream. Sorry.",
				  NULL),
		      TRUE, NULL, NULL );
      info->dwStreamtype = FMPF_SYSTEM;
      return FALSE;
    }
  
  return TRUE;
}

void startDisplayUp(FMPOVERLAYVDO *ovl)
{
  
  ovl->b_overlay_access = waitForAnalogOverlayAccess();
  restoreGuiState(*ovl);
  ovl->b_vga_cable_present = check_vga_cable_presence();
  
  if(ovl->video_output_mode == FMPV_VIDEOOUT_VGA)
    {
      if (ovl->b_vga_cable_present)
	{
	  switch_overlay_on(ovl);
	  window_move_toplevel(GUI.window,NULL,NULL);
	}
      else
	{
	  gtk_message_box("Warning","VGA overlay cable not present",
			  "Please, make sure your VGA overlay cable is properly attached.
\rFor the moment, the TV output will be used.",
			  TRUE, NULL, NULL );
	  switch_overlay_back(ovl);
	  setOutputMode(FMPV_VIDEOOUT_TV);
	  ovl->video_output_mode = FMPV_VIDEOOUT_TV;
	  setTvStandard(ovl->tv_standard);
	  setComponentMode(ovl->tv_component);
	  minimizeGuiSize();
	}
    }   
  else
    {
      switch_overlay_back(ovl);
      setOutputMode(ovl->video_output_mode);			      
      setTvStandard(ovl->tv_standard);
      setComponentMode(ovl->tv_component);
      minimizeGuiSize();
    }

  
  
  if(ovl->b_vga && ovl->b_fullscreen)
    {
      ovl->b_fullscreen = FALSE;
      fullscreen_on_cb(NULL, NULL, NULL);
    }
}


void autocalibrateAndStartPlaying(FMPOVERLAYVDO *ovl)
{
  handle_Stop_Close(&Info);
  handle_Stop_Close(&Info);
  autocalibrate(ovl);
  handle_Play_Pause(&Info);
  switch_overlay_on(ovl);
  restoreGuiState(*ovl);
  loadconfig(ovl);
  window_move_toplevel(GUI.window,NULL,NULL);
  gtk_destroy_message_box(NULL, NULL);
}

/* Obligatory basic callback */
void print_hello( GtkWidget *w,
                         gpointer   data )
{
  G_MESSAGE("Hello, World!\n");
}


void switch_overlay_back(FMPOVERLAYVDO * ovl)
{
  DWORD FMPreturn;
  if(ovl->b_overlay_access)
    {
      FMPreturn = setOverlay(0, UNUSED_ARG);
      ovl->b_vga = FALSE;
    }
}

void switch_overlay_on(FMPOVERLAYVDO * ovl)
{
  DWORD FMPreturn;
  if(ovl->b_overlay_access)
    {
      FMPreturn = setOverlay(2, ovl->key_color);
      ovl->b_vga = TRUE;
      if(!ovl->b_cfg_loaded)
	loadconfig(ovl);
    }
}

void close_cb (GtkWidget *widget, gpointer data)
{ 
  int previous_state = Info.playing;
  
  if(Info.playing == STOP_FOR_RESUME)
    resumeDisplayFromStop(&ovl);
  
  if(previous_state != CLOSED)
    {
      saveGuiState(&ovl);
      G_WARNING("Gui state saved");
      switch_overlay_back(&ovl);
      ovl.b_overlay_access = FALSE;
    }
  
  stop_cb(NULL, NULL);
  
  handle_Stop_Close(&Info);

  if(previous_state != CLOSED)
    {
      gtk_timeout_remove(GUI.timer_tag);
      GUI.timer_tag = 0;
      gtk_window_set_title (GTK_WINDOW (GUI.window), GTKPLAYERNAME );
    }
}

void eject_cb (GtkWidget *widget, gpointer data)
{
  // if(Info.playing != CLOSED)
    close_cb(NULL, NULL);
  handle_Eject_disc(&Info);
}

/** Toolbar a partir d'une fenetre et d'un packer existant dans cette fenetre */
void play_cb (GtkWidget *widget, gpointer data)
{
  int previous_state = Info.playing;
  
  if(widget != NULL &&
     Info.playing == CLOSED && 
     Info.bDiscType == LOCALFILE)
    {
      get_file_selector(NULL);
    }
  else if(widget != NULL &&
	  Info.sUrl == NULL && 
	  Info.bDiscType == RTSPURL)
    {
#ifdef _VOD_
      get_VOD_playing();
#endif
    }
  else
    {

      handle_Play_Pause(&Info);

      if(previous_state == CLOSED)
	{
	  if(GUI.timer_tag != 0)
	    {
	      gtk_timeout_remove(GUI.timer_tag);
	      GUI.timer_tag = 0;
	    }
	  GUI.timer_tag = 
	    gtk_timeout_add(TIMER_REFRESHING_DELAY,
			    (GtkFunction)refresh_timer_timeout_cb,
			    NULL);
	  
	  if(Info.playing != CLOSED)
	    startDisplayUp(&ovl);
	  G_WARNING("Display started");
	}
    }
}

void stop_cb (GtkWidget *widget, gpointer data)
{
  handle_Stop_Close(&Info);	 
}

void pause_cb (GtkWidget *widget, gpointer data)
{
  handle_Play_Pause(&Info); 
}
 
void fforward_cb (GtkWidget *widget, gpointer data)
{
  handle_FForward(&Info);
}

void frewind_cb (GtkWidget *widget, gpointer data)
{ 
  handle_FRewind(&Info);
}

void prev_cb (GtkWidget *widget, gpointer data)
{
  handle_Prev_Track(&Info);
}

void next_cb (GtkWidget *widget, gpointer data)
{
  handle_Next_Track(&Info);
}

void fakeTimer()
{
  if( Info.playing == PLAYING ||
      Info.playing == TRICK_MODE )
    Info.dwTime = Info.dwTime + 1;
}

gint refresh_timer_timeout_cb(gpointer data)
{
  
  gchar * msg=NULL, *msg2=NULL;
  gchar * result = NULL;
  DWORD time;
  BYTE h,m,s;
  DWORD unused;

  if(Info.bDiscType != RTSPURL &&
     Info.bDiscType != LOCALFILE)
    {
      getCurrentPlayingTime(&time);
      refreshTitleChapter(&Info);
    }
  else
    {
      fakeTimer();
    }

  h = time / 3600;
  m = (time % 3600) / 60;
  s = (time % 60);
  
  if(Info.bDiscType == FMPC_DVD_VIDEO)
    {         
      msg = g_strdup_printf("%02d:%02d:%02d",
			    h,m,s);
      
      msg2 = g_strdup_printf(" Ttle:%02d Chpt:%03d",
			     Info.bTitle, Info.bChapter);
    }
  else if(Info.bDiscType == FMPC_CDDA)
    {   
      CDDAGetStatus(&unused, (BYTE*)&unused, &time);

      m = time / 60;
      s = (time % 60);
      
      msg = g_strdup_printf("%02d:%02d",
			    m,s);
      msg2 = g_strdup_printf(" Trck:%02d",
			     Info.CDDAcurrenttrack);
    }
  else if(Info.bDiscType == FMPC_VCD ||
	  Info.bDiscType == FMPC_SVCD )
    {
      msg = g_strdup_printf("%02d:%02d:%02d",
			    h,m,s);
      msg2 = g_strdup_printf(" Trck:%02d",
			     Info.bTitle);
    }
  else if(Info.bDiscType == RTSPURL)
    {
      if(Info.dwTime > Info.totalTime)
	Info.dwTime = 0;
      time = Info.dwTime;
      h = time / 3600;
      m = (time % 3600) / 60;
      s = (time % 60);
      
      msg = g_strdup_printf("%02d:%02d:%02d",
			    h,m,s);
      msg2 = g_strdup(" ");
    }
  else
    {
      time = Info.dwTime;
      
      h = time / 3600;
      m = (time % 3600) / 60;
      s = (time % 60);
      
      msg = g_strdup_printf("%02d:%02d:%02d",
			    h,m,s);
      msg2 = g_strdup(" ");
    }
  
  result = g_strconcat(msg,msg2, NULL);
  // g_strstrip(result);
  
  gdk_threads_enter();
  gtk_statusbar_pop(GTK_STATUSBAR(GUI.timer), GUI.timer_id);
  gtk_statusbar_push( GTK_STATUSBAR(GUI.timer), GUI.timer_id, 
		      result);
  if(GUI.refresh_interval >= SEEK_BAR_REFRESHING_INTERVAL)
    {
#ifdef _VOD_
      if(Info.bDiscType == RTSPURL)
	{
	  // retrieveInfo(&Info);
	}
#endif
      refresh_seek_bar_current_time(time);
      GUI.refresh_interval = 0;
    }
  GUI.refresh_interval++;
  refresh_seek_bar_upper_bound();
  gdk_threads_leave();
  
 
  g_free(msg);
  g_free(msg2);
  g_free(result);
  return TRUE;
}

void refresh_seek_bar_upper_bound()
{
  GtkAdjustment *seek_bar;
  
  seek_bar = GTK_RANGE(gtk_object_get_data(GTK_OBJECT (GUI.window),"seek_scale"))->adjustment;
  
  if(seek_bar != NULL)
    {
      if(Info.bDiscType == FMPC_DVD_VIDEO ||
	 Info.bDiscType == RTSPURL ||
	 Info.bDiscType == LOCALFILE ||
	 Info.bDiscType == FMPC_VCD ||
	 Info.bDiscType == FMPC_SVCD)
	{
	  if(Info.previous_totalTime != Info.totalTime)
	    { 
	      seek_bar->upper = Info.totalTime;
	      gtk_adjustment_changed(seek_bar);
	      Info.previous_totalTime = Info.totalTime;
	    }
	}
      else if(Info.bDiscType == FMPC_CDDA)
	{
	  if(Info.previous_trackTime != Info.CDDATrackDuration[Info.CDDAcurrenttrack-1] )
	    {
	      seek_bar->upper = Info.CDDATrackDuration[Info.CDDAcurrenttrack-1];
	      gtk_adjustment_changed(seek_bar);
	      Info.previous_trackTime = Info.CDDATrackDuration[Info.CDDAcurrenttrack-1];
	    }
	}
    }
}

void refresh_seek_bar_current_time(DWORD current_time)
{
  GtkAdjustment *seek_bar;
  seek_bar = GTK_RANGE(gtk_object_get_data(GTK_OBJECT (GUI.window),"seek_scale"))->adjustment; 
  
  if(seek_bar != NULL)
    {
      gtk_adjustment_set_value(seek_bar, current_time);
    }
  else
    G_WARNING("Problem with the seek bar !!!");
}

gboolean time_searching_cb(GtkWidget *widget, GdkEvent *event,gpointer data)
{
  G_PRINT("\nSOME EVENTS ARE RECEIVED !!\n");
  return TRUE;
}

gboolean time_search_cb(GtkAdjustment *adj,gpointer   data )
{
  DWORD time, real_time;
  time = (DWORD)(adj->value); 

  if(Info.bDiscType == RTSPURL)
    {
      real_time = Info.dwTime;
    }
  else if(Info.bDiscType == LOCALFILE)
    {

      
    }
  else
    getCurrentPlayingTime(&real_time);
 
  if( abs(time - real_time) > SEEK_TIME_ACCURACY )
    {
      Info.dwSearch = convertSecondstoHhMmSs(time);
      /* G_WARNING("\n***********SEEKING FOR %ld***********\n",  */
/* 		time); */
      handle_Time_Search(&Info);
    }

  return TRUE;
}

void dvd_navigation_cb(GtkWidget *widget, guint action, gpointer data)
{
  DWORD direction = (DWORD) action;  
  handle_Navigation_disc(&Info, direction);
}

void dvd_menu_call_cb(GtkWidget *widget, guint action, gpointer data)
{
  switch(action)
    {
    case DVD_ROOT_ID:
      handle_Menu_Call_disc(&Info);
      break;
    case DVD_TITLE_ID:
      handle_Title_Call_disc(&Info); 
      break;
    case FMP_RESUME:
      handle_Resume_disc(&Info);
      break;
    default:
      handle_SubMenu_Call_dvd(&Info, (BYTE)action);
      break;
    }
}

void dvd_change_langage_cb(GtkWidget *widget, guint action, gpointer data)
{
  handle_Langage_Change_disc(&Info);
}

void dvd_change_subtitles_cb(GtkWidget *widget, guint action, gpointer data)
{ 
  handle_Subtitle_Change_disc(&Info);
}

void dvd_change_angle_cb(GtkWidget *widget, guint action, gpointer data)
{
  handle_Angle_Change_disc(&Info);
}


void set_gui_element_visible_cb(GtkWidget *widget, guint action, gpointer data)
{
  GtkWidget *toolbar = NULL, *slider = NULL,
    *keypad = NULL, *statusbar = NULL;
  GtkWidget *toolbar_check = NULL, *seekbar_check = NULL,
    *keypad_check = NULL, *statusbar_check = NULL, *playlist_check = NULL;
  
  switch(action)
    {
    case TOOLBAR:
      toolbar_check   = gtk_item_factory_get_widget ( GUI.item_factory, "/View/Toolbar");
      toolbar = GTK_WIDGET(gtk_object_get_data (GTK_OBJECT (GUI.window), "toolbar"));
      if(toolbar != NULL)
	{
	  if (GTK_CHECK_MENU_ITEM (toolbar_check)->active)
	    gtk_widget_show(toolbar);
	  else
	    gtk_widget_hide(toolbar);
	}
      break;
    case SLIDER:
      seekbar_check   = gtk_item_factory_get_widget ( GUI.item_factory, "/View/Slider");
      slider    = GTK_WIDGET(gtk_object_get_data (GTK_OBJECT (GUI.window), "seekbar"));
      if(slider != NULL)
	{
	  if (GTK_CHECK_MENU_ITEM (seekbar_check)->active)
	    gtk_widget_show(slider);
	  else
	    gtk_widget_hide(slider);
	}
      break; 
    case KEYPAD:
      keypad_check    = gtk_item_factory_get_widget ( GUI.item_factory, "/View/Keypad");
      keypad    = GTK_WIDGET(gtk_object_get_data (GTK_OBJECT (GUI.window), "keypad"));
      if(keypad != NULL)
	{
	  if (GTK_CHECK_MENU_ITEM (keypad_check)->active)
	    gtk_widget_show(keypad);
	  else
	    gtk_widget_hide(keypad);
	}
      break;
    case STATUSBAR:
      statusbar_check = gtk_item_factory_get_widget ( GUI.item_factory, "/View/Statusbar");
      statusbar = GTK_WIDGET(gtk_object_get_data (GTK_OBJECT (GUI.window), "statusbarbox"));
      if(statusbar != NULL)
	{
	  if (GTK_CHECK_MENU_ITEM (statusbar_check)->active)
	    gtk_widget_show(statusbar);
	  else
	    gtk_widget_hide(statusbar);
	}
      break;
    case PLAYLIST:
      playlist_check = gtk_item_factory_get_widget ( GUI.item_factory, "/View/Playlist");
      if(GUI.playlistwin == NULL)
	{
	  GUI.playlistwin = create_playlistwin();
	  gtk_widget_show(GUI.playlistwin);
	  set_playlist_size_and_position_cb(GUI.window);
	  load_playlist(NULL);
	}
      if (GTK_CHECK_MENU_ITEM (playlist_check)->active)
	gtk_widget_show(GUI.playlistwin);
      else
	gtk_widget_hide(GUI.playlistwin);
      break;
    default:
      break;
    }
}

void set_check_item_state()
{
  GtkWidget *toolbar_check = NULL, *seekbar_check = NULL,
    *keypad_check = NULL, *statusbar_check = NULL, *playlist_check = NULL;

  toolbar_check   = gtk_item_factory_get_widget ( GUI.item_factory, "/View/Toolbar");
  seekbar_check   = gtk_item_factory_get_widget ( GUI.item_factory, "/View/Slider");
  keypad_check    = gtk_item_factory_get_widget ( GUI.item_factory, "/View/Keypad");
  statusbar_check = gtk_item_factory_get_widget ( GUI.item_factory, "/View/Statusbar");
  playlist_check = gtk_item_factory_get_widget ( GUI.item_factory, "/View/Playlist");
  
  if(toolbar_check != NULL)
    {
      gtk_check_menu_item_set_show_toggle(GTK_CHECK_MENU_ITEM(toolbar_check), TRUE);
      gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (toolbar_check), TRUE);
    }
  if(seekbar_check != NULL)
    {
      gtk_check_menu_item_set_show_toggle(GTK_CHECK_MENU_ITEM(seekbar_check), TRUE);
      gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (seekbar_check), TRUE);
    }
  if(keypad_check != NULL)
    {
      gtk_check_menu_item_set_show_toggle(GTK_CHECK_MENU_ITEM(keypad_check), TRUE);
      gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (keypad_check), TRUE);
    }
  if(statusbar_check != NULL)
    {
      gtk_check_menu_item_set_show_toggle(GTK_CHECK_MENU_ITEM(statusbar_check), TRUE);
      gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (statusbar_check), TRUE);
    }
  if(playlist_check != NULL)
    {
      gtk_check_menu_item_set_show_toggle(GTK_CHECK_MENU_ITEM(playlist_check), TRUE);
      gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (playlist_check), FALSE);
    }
}

void toggle_player_mode(DWORD mode)
{
  GtkToggleButton *obj = NULL;
  GtkHandleBox *toolbar;

  toolbar = GTK_HANDLE_BOX(gtk_object_get_data (GTK_OBJECT (GUI.window),"toolbar"));
  
  switch(mode)
    {
      case  LOCALFILE:
	obj = GTK_TOGGLE_BUTTON(gtk_object_get_data (GTK_OBJECT (toolbar),"file_button"));
	break;
      case RTSPURL:
	obj = GTK_TOGGLE_BUTTON(gtk_object_get_data (GTK_OBJECT (toolbar),"rtsp_button"));
	break;
      case DRIVE_NUMBER:
	obj = GTK_TOGGLE_BUTTON(gtk_object_get_data (GTK_OBJECT (toolbar),"disc_button"));
	break;
    default:
      break;
    }
  
  if(obj != NULL)
    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (obj), TRUE);
  
}

gboolean change_player_mode_cb(GtkWidget *widget, gpointer data)
{
  BYTE bDiscType;
  bDiscType = (BYTE) GPOINTER_TO_UINT(data); 
  
  if (GTK_TOGGLE_BUTTON (widget)->active)
    {
      switch(bDiscType)
	{
	case LOCALFILE:
	  if(Info.bDiscType == RTSPURL)
	    {
	      close_cb(NULL,NULL);
	      initialize_info_structure_file(&Info);
	      /* load_localfile_config(&Info); */
	      reinitialize_gui_state(&ovl);
	      G_WARNING("Init data structures for FILE");
	    }
	  if(Info.bDiscType != LOCALFILE && Info.bDiscType != RTSPURL)
	    {
	      close_cb(NULL,NULL);
	      MPEGoff(&Info);
	      initialize_info_structure_file(&Info);
	      /* load_localfile_config(&Info); */
	      reinitialize_gui_state(&ovl);
	      MPEGon(&Info);
	    }
	  break;
	case RTSPURL:
	  if(Info.bDiscType == LOCALFILE)
	    {
	      close_cb(NULL,NULL);
	      initialize_info_structure_rtsp(&Info); 
	      reinitialize_gui_state(&ovl);
	      G_WARNING("Init data structures for RTSP");
	    }
	  if(Info.bDiscType != LOCALFILE && Info.bDiscType != RTSPURL)
	    {
	      close_cb(NULL,NULL);
	      MPEGoff(&Info);
	      initialize_info_structure_rtsp(&Info);
	      reinitialize_gui_state(&ovl);
	      MPEGon(&Info);
	    }
	  break;
	default:
	  if(Info.bDiscType == LOCALFILE || Info.bDiscType == RTSPURL)
	    {
	      close_cb(NULL,NULL);
	      MPEGoff(&Info);
	      reinitialize_info_structure_disc(&Info);
	      reinitialize_gui_state(&ovl);
	      MPEGon(&Info);
	    }
	  break;
	}
   }

  return TRUE;
}

void help_about_cb(GtkWidget *widget, gpointer data)
{
  TCHAR *sVersion, *sDate, *sCopyR;

  sVersion = g_new(TCHAR,20);
  sDate = g_new(TCHAR,20);
  sCopyR = g_new(TCHAR,80); 
  FMPAbout(sVersion,sDate,sCopyR);
  gtk_message_box("About",
		  "Sigma Designs Inc.",
		  g_strconcat("Version ", 
			      (gchar*)sVersion,
			      "\nCompiled on ",
			      (gchar*)sDate,
			      "\n",
			      (gchar*)sCopyR,
			      NULL),
		  TRUE,
		  NULL,
		  NULL);
  g_free(sVersion);
  g_free(sDate);
  g_free(sCopyR);
}

gboolean repeat_mode_cb(GtkWidget *widget, gpointer data)
{
  WORD repeat_mode = GPOINTER_TO_UINT(data);
  
  switch(repeat_mode)
    {
    case HANDLE_REPEAT:
      handle_Repeat_Mode_disc(&Info);
      break;
    case HANDLE_REPEATAB:
      handle_RepeatAB_Mode_disc(&Info);
      break;
    }

  return TRUE;
}

/*VODErrors code handling */

void alert(gchar* frame, gchar* message)
{
  gtk_message_box("Error",frame,message,TRUE,NULL,NULL);
}
