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

/*
 *      optionswincb.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"


void saveGuiState(FMPOVERLAYVDO *ovl)
{
  DWORD output_mode, standard, component;
  DWORD vol; long bal;
  DWORD audio_mode, audio_output;
  DWORD brt, sat, con;
  
  getOutputMode(&output_mode);
  getTvStandard(&standard);
  getComponentMode(&component);
  getAudioBalance(&vol, &bal);
  getAudioChannelMode(&audio_mode);
  getAudioAC3DTS(&audio_output);
  getFMPProperty(FMPI_BRIGHTNESS, &brt);
  getFMPProperty(FMPI_SATURATION, &sat);
  getFMPProperty(FMPI_CONTRAST, &con);
  
  ovl->video_output_mode = output_mode;
  ovl->tv_standard = standard;
  ovl->tv_component = component;
  ovl->volume = vol;
  ovl->balance = bal;
  ovl->audio_mode = audio_mode;
  ovl->audio_output = audio_output;
  ovl->brt = brt; 
  ovl->sat = sat; 
  ovl->con = con;
}

void restoreGuiState(FMPOVERLAYVDO ovl)
{
  setAudioBalance(ovl.volume, ovl.balance);
  setAudioChannelMode(ovl.audio_mode);
  setAudioAC3DTS(ovl.audio_output);
  setFMPProperty(FMPI_CONTRAST,ovl.con);
  setFMPProperty(FMPI_BRIGHTNESS, ovl.brt);
  setFMPProperty(FMPI_SATURATION, ovl.sat);
}

void resumeDisplayFromStop(FMPOVERLAYVDO *ovl)
{
  if(ovl->video_output_mode != FMPV_VIDEOOUT_VGA)
    {
      if(ovl->b_vga)
	{
	  switch_overlay_back(ovl);
	  minimizeGuiSize();
	}
      setOutputMode(ovl->video_output_mode);
      setTvStandard(ovl->tv_standard);
      setComponentMode(ovl->tv_component);
    }
  else 
    {
      switch_overlay_on(ovl);
      normalGuiSize();
    }
}

gboolean switch_digital_out_mode_cb(GtkWidget *widget, gpointer data)
{
  if (GTK_TOGGLE_BUTTON (widget)->active)
    {
      setAudioAC3DTS((DWORD) GPOINTER_TO_UINT(data)); 
    }
  return TRUE;
}

gboolean switch_TVcomponent_cb(GtkWidget *widget, gpointer data)
{
  if (GTK_TOGGLE_BUTTON (widget)->active)
    {
      setComponentMode((DWORD) GPOINTER_TO_UINT(data)); 
    }
  return TRUE;
}

gboolean switch_to_mute_mode(GtkWidget *widget, gpointer data)
{
  if (GTK_TOGGLE_BUTTON (widget)->active)
    {
    	setMute(TRUE);
	ovl.b_mute = TRUE;
    }
  return TRUE;
}

gboolean switch_audio_channel_mode(GtkWidget *widget, gpointer data)
{
  GtkWidget *top, *tvvol_frame;
  
  top = gtk_widget_get_toplevel(widget);
  tvvol_frame = GTK_WIDGET(gtk_object_get_data (GTK_OBJECT (top),"tvvol_frame"));
  if (GTK_TOGGLE_BUTTON (widget)->active)
    {
      if(ovl.b_mute)
	{
	  setMute(FALSE);
	  ovl.b_mute = FALSE;
	}
      setAudioChannelMode((DWORD) GPOINTER_TO_UINT(data));
      refresh_Volume_frame_cb(tvvol_frame);
    }
 return TRUE;
}

void minimizeGuiSize()
{
  if(ovl.b_vga)
    {
      gint width=0, height=0;
      gdk_window_get_size(GUI.window->window ,&width,&height);
      GUI.window_width = width;
      GUI.window_height = height;
    }
  
  gtk_window_set_policy(GTK_WINDOW(GUI.window), TRUE, FALSE, TRUE);
}

void normalGuiSize()
{ 
  gtk_window_set_policy(GTK_WINDOW(GUI.window), FALSE, TRUE, FALSE);
  gdk_window_resize(GUI.window->window, GUI.window_width, GUI.window_height);
  window_move_toplevel(GUI.window,NULL,NULL);
}

gboolean switch_TVout_cb(GtkWidget *widget, gpointer data)
{ 
  GtkWidget *top, *tvcomponent_frame;
  
  top = gtk_widget_get_toplevel(widget);
  tvcomponent_frame = GTK_WIDGET(gtk_object_get_data (GTK_OBJECT (top),"tvcomponent_frame"));
  
  if (GTK_TOGGLE_BUTTON (widget)->active)
    {
      if(ovl.b_vga)
	{
	  minimizeGuiSize();
	  switch_overlay_back(&ovl);
	}
      setOutputMode(FMPV_VIDEOOUT_TV);
      setTvStandard((DWORD) GPOINTER_TO_UINT(data));
      ovl.b_vga = FALSE;
      gtk_widget_set_sensitive(GTK_WIDGET(tvcomponent_frame),TRUE);
      refresh_TV_component_frame_cb( GTK_WIDGET(tvcomponent_frame) );
    } 
  return TRUE;
}

void prepareForFullScreen()
{
  if(!ovl.b_vga)
    normalGuiSize();
}

gboolean switch_VGAout_cb(GtkWidget *widget, gpointer data)
{
  GtkWidget *top, *tvcomponent_frame;
  
  top = gtk_widget_get_toplevel(widget);
  tvcomponent_frame = GTK_WIDGET(gtk_object_get_data (GTK_OBJECT (top),"tvcomponent_frame"));

  if (ovl.b_vga_cable_present && (GTK_TOGGLE_BUTTON (widget)->active) )
    {
      if(!ovl.b_vga)
	{
	  switch_overlay_on(&ovl);
	  normalGuiSize();
	  gtk_widget_set_sensitive(GTK_WIDGET(tvcomponent_frame),FALSE);  
	}
    }
  return TRUE;
}

void updateAudio(int volume, int balance)
{
  volume = VOL_MAX+VOL_MIN - volume;
  setAudioBalance((DWORD)volume, (long)balance);
}

void volume_changed( GtkAdjustment *volume, GtkAdjustment *balance)
{
  updateAudio((int)volume->value,(int)balance->value);
}


void balance_changed( GtkAdjustment *balance,GtkAdjustment *volume)
{
  updateAudio((int)volume->value,(int)balance->value);
}


void brightness_changed( GtkAdjustment *adj,gpointer   data )
{
	DWORD FMPreturn;
	FMPreturn=setFMPProperty(FMPI_BRIGHTNESS,(BRT_MIN+BRT_MAX)-(unsigned int)adj->value);
}



void contrast_changed( GtkAdjustment *adj,gpointer   data )
{
	DWORD FMPreturn;	
	FMPreturn=setFMPProperty(FMPI_CONTRAST,(CON_MIN+CON_MAX)-(unsigned int)adj->value);
}



void saturation_changed( GtkAdjustment *adj,gpointer   data )
{
	DWORD FMPreturn;
	FMPreturn=setFMPProperty(FMPI_SATURATION,(CSC_MIN+CSC_MAX)-(unsigned int)adj->value);
}

void refresh_BSC_frame_cb(GtkWidget *widget)
{
  GtkAdjustment	*obj;
  DWORD brt, sat, con;
  
  getFMPProperty(FMPI_BRIGHTNESS, &brt);
  getFMPProperty(FMPI_SATURATION, &sat);
  getFMPProperty(FMPI_CONTRAST, &con);
  
  if(widget != NULL)
    {
     obj = GTK_RANGE(gtk_object_get_data (GTK_OBJECT (widget),"brightness_scale"))->adjustment; 
     gtk_adjustment_set_value(obj,(short) (BRT_MIN+BRT_MAX - brt) );
     
     obj = GTK_RANGE(gtk_object_get_data (GTK_OBJECT (widget),"saturation_scale"))->adjustment; 
     gtk_adjustment_set_value(obj,(short) (CSC_MIN+CSC_MAX - sat) );

     obj = GTK_RANGE(gtk_object_get_data (GTK_OBJECT (widget),"contrast_scale"))->adjustment; 
     gtk_adjustment_set_value(obj,(short) (CON_MIN+CON_MAX - con) );
    }
}

void refresh_Volume_frame_cb(GtkWidget *widget)
{
  GtkAdjustment	*obj_vol, *obj_bal; 
  DWORD volume;
  long balance;
  
  obj_vol = GTK_RANGE(gtk_object_get_data (GTK_OBJECT (widget),"volume_scale"))->adjustment;
  obj_bal = GTK_RANGE(gtk_object_get_data (GTK_OBJECT (widget),"balance_scale"))->adjustment;
  
  getAudioBalance(&volume, &balance);

  gtk_adjustment_set_value(obj_vol,(short) (VOL_MIN+VOL_MAX - volume)); 
  gtk_adjustment_set_value(obj_bal,(short) balance); 
   
  
}

gint popup_vol_display_timeout(gpointer data)
{
  if(GUI.popup_vol != NULL)
    {
      if(GTK_WIDGET_VISIBLE(GUI.popup_vol))
	gtk_widget_hide( GTK_WIDGET(GUI.popup_vol) );
    }
  GUI.osd_delay_tag = 0;
  return FALSE;
}

gboolean volume_changed_popup_cb(GtkAdjustment *volume, gpointer data)
{
  if(Info.playing != CLOSED)

    {
      DWORD unused = 0;
      long balance = 0;
  
      getAudioBalance(&unused, &balance);
      updateAudio((int)volume->value,(int)balance);
      
      if(GUI.options != NULL)
	{
	  GtkWidget *obj2 = NULL;
	  obj2 = GTK_WIDGET(gtk_object_get_data (GTK_OBJECT (GUI.options),"tvvol_frame"));
	  refresh_Volume_frame_cb(obj2);
	}
    }
  
  if(GUI.popup_vol_fadeout_tag != 0)
    {
      gtk_timeout_remove(GUI.popup_vol_fadeout_tag);
      GUI.popup_vol_fadeout_tag = 0;
    }
  
  GUI.popup_vol_fadeout_tag = 
    gtk_timeout_add(POPUP_VOL_FADEOUT_DELAY,
		    (GtkFunction)popup_vol_display_timeout,
		    NULL);
  
  return TRUE;
}

gboolean vol_popup_cb(GtkWidget *widget, gpointer data)
{
  if(GUI.popup_vol == NULL)
      create_popup_vol_menu();
  
  if( GTK_WIDGET_VISIBLE(GUI.popup_vol) )
    gtk_widget_hide(GTK_WIDGET(GUI.popup_vol));
  else
    gtk_widget_show(GTK_WIDGET(GUI.popup_vol));
  
  return TRUE;
}

void refresh_TV_component_frame_cb( GtkWidget *widget )
{
  GtkToggleButton * obj;
  DWORD component;

  if(ovl.b_overlay_access) 
    {
      if(!ovl.b_vga)
	{
	  getComponentMode(&component);
	  
	  switch(component)
	    {
		case FMPV_VIDEOOUT_COMPOSITE:
		  obj = GTK_TOGGLE_BUTTON(gtk_object_get_data (GTK_OBJECT (widget),"radioCOMPOSITE"));
		  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (obj), TRUE);
		  break;
		case FMPV_VIDEOOUT_COMPONENT_RGB:
		  obj = GTK_TOGGLE_BUTTON(gtk_object_get_data (GTK_OBJECT (widget),"radioRGB"));
		  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (obj), TRUE);
		  break;
		case FMPV_VIDEOOUT_COMPONENT_YUV:
		  obj = GTK_TOGGLE_BUTTON(gtk_object_get_data (GTK_OBJECT (widget),"radioYUV"));
		  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (obj), TRUE);
		  break;
		default:
		  obj = GTK_TOGGLE_BUTTON(gtk_object_get_data (GTK_OBJECT (widget),"radioCOMPOSITE"));
		  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (obj), TRUE);
	    }
	}
    }
}

void refresh_TV_standard_frame_cb( GtkWidget *widget )
{
  GtkToggleButton * obj;
  DWORD standard;

  if(ovl.b_overlay_access) 
    {
      obj = GTK_TOGGLE_BUTTON(gtk_object_get_data (GTK_OBJECT (widget),"radioVGA"));
      if(!ovl.b_vga_cable_present)
	gtk_widget_set_sensitive(GTK_WIDGET(obj),FALSE);
      if(!ovl.b_vga)
	{
	  getTvStandard(&standard);
	  switch(standard)
		{
		case FMPV_VIDEOOUT_NTSC:
		  obj = GTK_TOGGLE_BUTTON(gtk_object_get_data (GTK_OBJECT (widget),"radioNTSC"));
		  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (obj), TRUE);
		  break;
		case FMPV_VIDEOOUT_PAL:
		  obj = GTK_TOGGLE_BUTTON(gtk_object_get_data (GTK_OBJECT (widget),"radioPAL"));
		  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (obj), TRUE);
		  break;
		case FMPV_VIDEOOUT_PAL60:
		  obj = GTK_TOGGLE_BUTTON(gtk_object_get_data (GTK_OBJECT (widget),"radioPAL60"));
		  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (obj), TRUE);
		  break;
		case FMPV_VIDEOOUT_PALM:
		  obj = GTK_TOGGLE_BUTTON(gtk_object_get_data (GTK_OBJECT (widget),"radioPALM"));
		  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (obj), TRUE);
		  break;
		default:
		  obj = GTK_TOGGLE_BUTTON(gtk_object_get_data (GTK_OBJECT (widget),"radioVGA"));
		  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (obj), FALSE);
		}
	    }
      else
	{
	  obj = GTK_TOGGLE_BUTTON(gtk_object_get_data (GTK_OBJECT (widget),"radioVGA"));
	  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (obj), TRUE);
	}
    }
}

void refresh_channel_frame_cb( GtkWidget *widget )
{
  GtkToggleButton * obj;
  DWORD channel;
  
  if(ovl.b_overlay_access) 
    {
      getMute(&(ovl.b_mute));
      if(ovl.b_mute)
	{
	  obj = GTK_TOGGLE_BUTTON(gtk_object_get_data (GTK_OBJECT (widget),"radioMUTE"));
	  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (obj), TRUE);
	}
      else
	{
	  getAudioChannelMode(&channel);
	  switch(channel)
	    {
	    case FMPV_AUDIO_MODE_STEREO:
	      obj = GTK_TOGGLE_BUTTON(gtk_object_get_data (GTK_OBJECT (widget),"radioSTEREO"));
	      gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (obj), TRUE);
	      break;
	    case FMPV_AUDIO_MODE_MONOMIX:
	      obj = GTK_TOGGLE_BUTTON(gtk_object_get_data (GTK_OBJECT (widget),"radioMONO"));
	      gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (obj), TRUE);
	      break;
	    case FMPV_AUDIO_MODE_LEFT_ONLY:
	      obj = GTK_TOGGLE_BUTTON(gtk_object_get_data (GTK_OBJECT (widget),"radioMONOLEFT"));
	      gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (obj), TRUE);
	      break;
	    case FMPV_AUDIO_MODE_RIGHT_ONLY:
	      obj = GTK_TOGGLE_BUTTON(gtk_object_get_data (GTK_OBJECT (widget),"radioMONORIGHT"));
	      gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (obj), TRUE);
	      break;
	    default:
	      g_print("/nY'a bleme dans refresh channel mode !!\n");
	      break;	      
	    }
	}
    }
}

void refresh_digital_frame_cb( GtkWidget *widget )
{
  GtkToggleButton * obj;
  DWORD digitalout;
  
  if(ovl.b_overlay_access) 
    {
      getAudioAC3DTS(&digitalout);
      switch(digitalout)
	{
	case PCM:
	  obj = GTK_TOGGLE_BUTTON(gtk_object_get_data (GTK_OBJECT (widget),"radioPCM"));
	  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (obj), TRUE);
	  break;
	case AC3DTS:
	  obj = GTK_TOGGLE_BUTTON(gtk_object_get_data (GTK_OBJECT (widget),"radioAC3DTS"));
	  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (obj), TRUE);
	  break;
	}
    }
}

void refresh_all_cb(GtkWidget *widget, gpointer   data)
{
  GtkWidget *obj1, *obj2, *obj3, *obj4, *obj5, *obj6;

  if(data != NULL)
    {
      obj1 = GTK_WIDGET(gtk_object_get_data (GTK_OBJECT (data),"tvstd_frame"));
      obj2 = GTK_WIDGET(gtk_object_get_data (GTK_OBJECT (data),"tvvol_frame"));
      obj3 = GTK_WIDGET(gtk_object_get_data (GTK_OBJECT (data),"tvBSC_frame"));
      obj4 = GTK_WIDGET(gtk_object_get_data (GTK_OBJECT (data),"tvcomponent_frame"));
      obj5 = GTK_WIDGET(gtk_object_get_data (GTK_OBJECT (data),"channel_frame"));
      obj6 = GTK_WIDGET(gtk_object_get_data (GTK_OBJECT (data),"digital_frame"));
      
      if(!ovl.b_overlay_access)
	{
	  gtk_widget_set_sensitive(obj1,FALSE);
	  gtk_widget_set_sensitive(obj2,FALSE);
	  gtk_widget_set_sensitive(obj3,FALSE);
	  if(ovl.b_vga)
	    gtk_widget_set_sensitive(obj4,FALSE);
	  gtk_widget_set_sensitive(obj5,FALSE);
	  gtk_widget_set_sensitive(obj6,FALSE);
	}
      else
	{
	  refresh_TV_standard_frame_cb(obj1);
	  refresh_Volume_frame_cb(obj2);
	  refresh_BSC_frame_cb(obj3);
	  refresh_TV_component_frame_cb(obj4);
	  refresh_channel_frame_cb(obj5);
	  refresh_digital_frame_cb(obj6);
	}
      /* volume popup window ... */
      if(GUI.popup_vol != NULL)
	{
	  gtk_widget_destroy(GTK_WIDGET(GUI.popup_vol));
	  GUI.popup_vol = NULL;
	}
    }
  else
    G_MESSAGE("Trouble with DATA !");
}

gboolean C_Ok_clicked( GtkWidget *widget, gpointer   data )
{
	// GtkAdjustment	*obj;
	GtkWidget *window;
	window = gtk_widget_get_toplevel(widget);

	if(Info.playing != CLOSED)
	  saveGuiState(&ovl);
	/* we used to save the settings, but things have changed ! */
	gtk_widget_destroy(window);
	GUI.options = NULL;
	return TRUE;
}

gboolean C_Cancel_clicked(GtkWidget *widget, gpointer   data)
{
	GtkWidget *window;
	window = gtk_widget_get_toplevel(widget);
	/* Restore settings */
	if(Info.playing != CLOSED)
	  {
	    resumeDisplayFromStop(&ovl);
	    restoreGuiState(ovl);
	  }
	
	gtk_widget_destroy(window);
	GUI.options = NULL;
	return TRUE;
}

