/*   EXTRAITS DE LA LICENCE
	Copyright CEA, contributeurs : Luc BILLARD et Damien
	CALISTE, laboratoire L_Sim, (2001-2006)
  
	Adresse mèl :
	BILLARD, non joignable par mèl ;
	CALISTE, damien P caliste AT cea P fr.

	Ce logiciel est un programme informatique servant à visualiser des
	structures atomiques dans un rendu pseudo-3D. 

	Ce logiciel est régi par la licence CeCILL soumise au droit français et
	respectant les principes de diffusion des logiciels libres. Vous pouvez
	utiliser, modifier et/ou redistribuer ce programme sous les conditions
	de la licence CeCILL telle que diffusée par le CEA, le CNRS et l'INRIA 
	sur le site "http://www.cecill.info".

	Le fait que vous puissiez accéder à cet en-tête signifie que vous avez 
	pris connaissance de la licence CeCILL, et que vous en avez accepté les
	termes (cf. le fichier Documentation/licence.fr.txt fourni avec ce logiciel).
*/

/*   LICENCE SUM UP
	Copyright CEA, contributors : Luc BILLARD et Damien
	CALISTE, laboratoire L_Sim, (2001-2006)

	E-mail address:
	BILLARD, not reachable any more ;
	CALISTE, damien P caliste AT cea P fr.

	This software is a computer program whose purpose is to visualize atomic
	configurations in 3D.

	This software is governed by the CeCILL  license under French law and
	abiding by the rules of distribution of free software.  You can  use, 
	modify and/ or redistribute the software under the terms of the CeCILL
	license as circulated by CEA, CNRS and INRIA at the following URL
	"http://www.cecill.info". 

	The fact that you are presently reading this means that you have had
	knowledge of the CeCILL license and that you accept its terms. You can
	find a copy of this licence shipped with this software at Documentation/licence.en.txt.
*/
#include "panelBoxAxes.h"

#include <string.h>

#include <visu_object.h>
#include <renderingBackend/visu_windowInterface.h>
#include <gtk_renderingWindowWidget.h>
#include <openGLFunctions/view.h>

int disableCallbacksBoxAxes;

subPanel* initPanelBoxAxes()
{
  /* Long description */
  char *cl = _("Box and axes");
  /* Short description */
  char *tl = _("Box & axes");
  GtkWidget *child;

  child = createInteriorBoxAxes();
  panelBoxAxes = createSubPanel(cl, tl, child, refreshPanelBoxAxes,
				(onChangePanelFunc)0);
  if (!panelBoxAxes)
    return (subPanel*)0;

  /* Create the callbacks of all the sensitive widgets. */
  createCallBacksBoxAxes();

  /* call the visu add routine */
  addSubPanel(panelBoxAxes);

  /* Private parameters. */
  disableCallbacksBoxAxes = 0;

  return panelBoxAxes;
}

GtkWidget *createInteriorBoxAxes()
{
  GtkWidget *vbox, *hbox;
  GtkWidget *label;
  GtkWidget *table;
  GtkObject *adj;
#define RED_LABEL   _("R:")
#define GREEN_LABEL _("G:")
#define BLUE_LABEL  _("B:")
  char *rgb[3];
  char *rgbName[3] = {"scroll_r", "scroll_g", "scroll_b"};
  int i;

  rgb[0] = RED_LABEL;
  rgb[1] = GREEN_LABEL;
  rgb[2] = BLUE_LABEL;

  vbox = gtk_vbox_new(FALSE, 0);

  hbox = gtk_hbox_new(FALSE, 0);
  label = gtk_label_new(_("Draw a box :"));
  gtk_widget_set_name(label, "label_head");
  gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 2);
  gtk_widget_show(label);
  checkBoxIsOn = gtk_check_button_new();
  gtk_box_pack_start(GTK_BOX(hbox), checkBoxIsOn, FALSE, FALSE, 2);
  gtk_widget_show(checkBoxIsOn);
  gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 5);
  gtk_widget_show(hbox);

  hbox = gtk_hbox_new(FALSE, 0);
  label = gtk_label_new(_("Draw the axes :"));
  gtk_widget_set_name(label, "label_head");
  gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 2);
  gtk_widget_show(label);
  checkAxesAreOn = gtk_check_button_new();
  gtk_box_pack_start(GTK_BOX(hbox), checkAxesAreOn, FALSE, FALSE, 2);
  gtk_widget_show(checkAxesAreOn);
  gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 5);
  gtk_widget_show(hbox);

  table = gtk_table_new(3, 2, FALSE);
  gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 2);
  gtk_widget_show(table);
  for (i = 0; i < 3; i++)
    {
      label = gtk_label_new(rgb[i]);
      gtk_table_attach(GTK_TABLE(table), label, 0, 1, i, i + 1,
		       GTK_SHRINK, GTK_SHRINK, 5, 0);
      gtk_widget_show(label);
      rgbBoxAndAxes[i] = gtk_hscale_new_with_range(0., 1., 0.001);
      gtk_scale_set_value_pos(GTK_SCALE(rgbBoxAndAxes[i]),
			      GTK_POS_RIGHT);
      gtk_widget_set_name(rgbBoxAndAxes[i], rgbName[i]);
      gtk_table_attach(GTK_TABLE(table), rgbBoxAndAxes[i],
		       1, 2, i, i + 1,
		       GTK_EXPAND | GTK_FILL, GTK_SHRINK, 5, 0);
      gtk_widget_show(rgbBoxAndAxes[i]);
    }


  hbox = gtk_hbox_new(FALSE, 0);
  label = gtk_label_new(_("Line width (1 to 10) : "));
  gtk_widget_set_name(label, "label_head");
  gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 2);
  gtk_widget_show(label);
  adj = gtk_adjustment_new(1., 1., 10., 1., 2.0, 2.0);
  spinLineWidth = gtk_spin_button_new(GTK_ADJUSTMENT(adj), 0, 0);
  gtk_box_pack_start(GTK_BOX(hbox), spinLineWidth, FALSE, FALSE, 2);
  gtk_widget_show(spinLineWidth);
  gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 5);
  gtk_widget_show(hbox);


  gtk_widget_show(vbox);

  return vbox;
}

void createCallBacksBoxAxes()
{
  int i;

  g_signal_connect((gpointer)spinLineWidth, "value-changed",
		   G_CALLBACK(lineWidthChanged), (gpointer)0);
  g_signal_connect((gpointer)checkBoxIsOn, "toggled",
		   G_CALLBACK(boxCheckChanged), (gpointer)0);
  g_signal_connect((gpointer)checkAxesAreOn, "toggled",
		   G_CALLBACK(axesCheckChanged), (gpointer)0);
  for (i = 0; i < 3; i++)
    g_signal_connect((gpointer)rgbBoxAndAxes[i], "value-changed",
		     G_CALLBACK(changedRGBBoxAxes), GINT_TO_POINTER(i));
}

/*************/
/* Callbacks */
/*************/
void boxCheckChanged(GtkToggleButton *button, gpointer data)
{
  RenderingWindow *window;

  if (disableCallbacksBoxAxes)
    return;

  if (boxSet_isOn(gtk_toggle_button_get_active(button)))
    {
      window = RENDERING_WINDOW(visuRenderingWindowGet_current());
      g_return_if_fail(window);
      if (!renderingWindowGet_visuData(window))
	return;
      boxDraw(renderingWindowGet_visuData(window));
    }

  g_signal_emit (visu, VISU_GET_CLASS (visu)->OpenGLAskForReDraw_signal_id,
		 0 , NULL); 
}
void axesCheckChanged(GtkToggleButton *button, gpointer data)
{
  RenderingWindow *window;

  if (disableCallbacksBoxAxes)
    return;

  if (axesSet_areOn(gtk_toggle_button_get_active(button)))
    {
      window = RENDERING_WINDOW(visuRenderingWindowGet_current());
      g_return_if_fail(window);
      if (!renderingWindowGet_visuData(window))
	return;
      axesDraw(renderingWindowGet_visuData(window));
    }

  g_signal_emit (visu, VISU_GET_CLASS (visu)->OpenGLAskForReDraw_signal_id,
		 0 , NULL); 
}
void lineWidthChanged(GtkSpinButton *spin, gpointer user_data)
{
  int res;
  RenderingWindow *window;

  if (disableCallbacksBoxAxes)
    return;

  window = RENDERING_WINDOW(visuRenderingWindowGet_current());
  g_return_if_fail(window);
  res = 0;
  if (boxSet_lineWidth((float)gtk_spin_button_get_value(spin)))
    {
      if (renderingWindowGet_visuData(window))
	{
	  boxDraw(renderingWindowGet_visuData(window));
	  res = 1;
	}
    }
  if (axesSet_lineWidth((float)gtk_spin_button_get_value(spin)))
    {
      if (renderingWindowGet_visuData(window))
	{
	  axesDraw(renderingWindowGet_visuData(window));
	  res = 1;
	}
    }
  if (res)
    g_signal_emit (visu, VISU_GET_CLASS (visu)->OpenGLAskForReDraw_signal_id,
		   0 , NULL); 
}
void changedRGBBoxAxes(GtkRange *rg, gpointer data)
{
  GtkAdjustment *adj;
  int rgbMask[3] = {MASK_RGB_R, MASK_RGB_G, MASK_RGB_B};
  float rgb[3];
  int res;
  RenderingWindow *window;

  if (disableCallbacksBoxAxes)
    return;

  adj = gtk_range_get_adjustment(rg);
  if (GPOINTER_TO_INT(data) < 0 || GPOINTER_TO_INT(data) > 2)
    {
      fprintf(stderr, "INTERNAL ERROR! The numbering of color"
	      " (%d) is inconsistent. It must be in[|0;2|].\n", GPOINTER_TO_INT(data));
      return;
    }
  rgb[GPOINTER_TO_INT(data)] = (float)gtk_adjustment_get_value(adj);
  
  window = RENDERING_WINDOW(visuRenderingWindowGet_current());
  g_return_if_fail(window);
  res = 0;
  if (boxSet_RGBValues(rgb, rgbMask[GPOINTER_TO_INT(data)]))
    {
      if (renderingWindowGet_visuData(window))
	{
	  boxDraw(renderingWindowGet_visuData(window));
	  res = 1;
	}
    }
  if (axesSet_RGBValues(rgb, rgbMask[GPOINTER_TO_INT(data)]))
    {
      if (renderingWindowGet_visuData(window))
	{
	  axesDraw(renderingWindowGet_visuData(window));
	  res = 1;
	}
    }
  if (res)
    g_signal_emit (visu, VISU_GET_CLASS (visu)->OpenGLAskForReDraw_signal_id,
		   0 , NULL); 
}
gboolean refreshPanelBoxAxes(gpointer data)
{
  int i;
  float rgb[3];

  disableCallbacksBoxAxes = 1;
  gtk_spin_button_set_value(GTK_SPIN_BUTTON(spinLineWidth),
			    boxGet_lineWidth());
  gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkBoxIsOn),
			       (gboolean)boxGet_isOn());
  gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkAxesAreOn),
			       (gboolean)axesGet_areOn());
  rgb[0] = boxGet_redValue();
  rgb[1] = boxGet_greenValue();
  rgb[2] = boxGet_blueValue();
  for (i = 0; i < 3; i++)
    gtk_range_set_value(GTK_RANGE(rgbBoxAndAxes[i]), (gdouble)rgb[i]);
  disableCallbacksBoxAxes = 0;

  return FALSE;
}
