/*   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 <math.h>

#include "gtkAtomic.h"

#include <renderingMethods/renderingAtomic.h>
#include "visuElements.h"
#include "visuConfig.h"
#include <gtk_main.h>
#include <renderingBackend/visu_windowInterface.h>

#define SPIN_ATOMIC_RADIUS_UPPER (gdouble)5.
#define SPIN_ATOMIC_RADIUS_LOWER (gdouble)0.001
#define SPIN_ATOMIC_RADIUS_STEP  (gdouble)0.05
#define LABEL_RADIUS      _("Radius:")
#define LABEL_RADIUS_MULT _("Radius <span style=\"italic\" size=\"smaller\" foreground=\"red\">\303\227 mult.</span>: ")


/* the spin button to control the radius. */
static GtkWidget *spinRadius;
static GtkWidget *entryShape;
static GtkWidget *labelRadius;

/* Pointer on the currently selected element. */
VisuElement* currentElement;

GtkWidget* createAtomicSpecificOpenWidget(void);

/* Callbacks */
void onSpinBoundsChanged(GObject *obj, gfloat val, gpointer data);


/***************/
/* Public part */
/***************/

/* Initialise the specific area in the element panel
   for the atomic rendering method. */
void initAtomic_gtkPanel()
{
  setInterfaceMethods(pointerOnRenderingAtomicMethod,
		      onElementChange_atomicMethod,
		      createGtkInterfaceForAtomicMethod);
}

/* Initialise the gtk methods associated with
   the atomic rendering method. */
void initAtomic_gtkMain()
{
  gtkMainSet_renderingSpecificMethods(pointerOnRenderingAtomicMethod,
				      createAtomicSpecificOpenWidget,
				      (createGtkLoadWidgetFunc)0);
}

GtkWidget* createAtomicSpecificOpenWidget(void)
{
  GtkWidget *label;

  label = gtk_label_new("Coucou");
  gtk_widget_show(label);
  return label;
}




/* Create the gtk widgets (a hbox with a spin with
   positive values) and return it. */
GtkWidget* createGtkInterfaceForAtomicMethod()
{
  GtkWidget* hbox, *vbox;
  GtkWidget* label;
  GtkObject *adj;
  GtkWidget *comboShape;
  const char **names;
  int i;

  vbox = gtk_vbox_new(FALSE, 0);

  hbox = gtk_hbox_new(FALSE, 0);
  gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 5);
  gtk_widget_show(hbox);

  labelRadius = gtk_label_new("");
  if (panelConfigGet_spinBoundsValue() == 1.)
    gtk_label_set_text(GTK_LABEL(labelRadius), LABEL_RADIUS);
  else
    {
      gtk_label_set_text(GTK_LABEL(labelRadius), LABEL_RADIUS_MULT);
      gtk_label_set_use_markup(GTK_LABEL(labelRadius), TRUE);
    }
  gtk_box_pack_start(GTK_BOX(hbox), labelRadius, FALSE, FALSE, 1);
  gtk_widget_show(labelRadius);

  adj = gtk_adjustment_new(1.0, SPIN_ATOMIC_RADIUS_LOWER,
			   SPIN_ATOMIC_RADIUS_UPPER, SPIN_ATOMIC_RADIUS_STEP,
			   0.1, 0.1);
  spinRadius = gtk_spin_button_new(GTK_ADJUSTMENT(adj), 0.01, 3);
  gtk_box_pack_start(GTK_BOX(hbox), spinRadius, FALSE,FALSE, 3);
  gtk_widget_show(spinRadius);

  label = gtk_label_new(_("Shape: "));
  gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 1);
  gtk_misc_set_alignment(GTK_MISC(label), 1., 0.5);
  gtk_widget_show(label);

  comboShape = gtk_combo_box_new_text();
  names = renderingAtomicGet_allShapesI18n();
  if (names)
    for (i = 0; names[i]; i++)
      gtk_combo_box_append_text(GTK_COMBO_BOX(comboShape), names[i]);
  else
    gtk_combo_box_append_text(GTK_COMBO_BOX(comboShape),
			      (char*)renderingAtomicGet_shapeNameDefault());
  gtk_combo_box_set_active(GTK_COMBO_BOX(comboShape), 0);
  entryShape = comboShape;
  /* set callback for the combo button. */
  g_signal_connect(G_OBJECT(entryShape), "changed",
		   G_CALLBACK(shapeChanged), (gpointer)0);
  gtk_box_pack_start(GTK_BOX(hbox), comboShape, FALSE, FALSE, 3);
  gtk_widget_show(comboShape);

  /* set callback for the spin button. */
  g_signal_connect((gpointer)spinRadius, "value-changed",
		   G_CALLBACK(radiusChanged), (gpointer)0);

  g_signal_connect(G_OBJECT(visuGtkObject), "spinBoundsChanged",
		   G_CALLBACK(onSpinBoundsChanged), (gpointer)0);

  return vbox;
}

/* This function is called whenever an element is changed. */
void onElementChange_atomicMethod(VisuElement *ele)
{
  float radius;
  int shape;
  gdouble mult;

  currentElement = ele;

  radius = renderingAtomicGet_radius(ele);
  mult = panelConfigGet_spinBoundsValue();
  if (radius >= 0.)
    gtk_spin_button_set_value(GTK_SPIN_BUTTON(spinRadius),
			      radius / mult);
  else
    {
      g_warning("Can't find a value for radius of element '%s'.\n", ele->name);
      gtk_spin_button_set_value(GTK_SPIN_BUTTON(spinRadius),
				renderingAtomicGet_radiusDefault() / mult);
    }
  shape = renderingAtomicGet_shape(ele);
  if (shape >= 0)
    gtk_combo_box_set_active(GTK_COMBO_BOX(entryShape), shape);
  else
    {
      g_warning("Can't find the shape of element '%s'.\n", ele->name);
      gtk_combo_box_set_active(GTK_COMBO_BOX(entryShape), renderingAtomicGet_shapeDefault());
    }
}




/****************/
/* Private part */
/****************/

void onSpinBoundsChanged(GObject *obj, gfloat mult, gpointer data)
{
  float val;
  int i;
  GenericRenderingWindow window;
  VisuData *dataObj;

  if (getRenderingMethodInUse() != pointerOnRenderingAtomicMethod)
    return;

  DBG_fprintf(stderr, "Panel Atomic : catch the 'spinBoundsChanged' signal,"
	      " should adjust the radius bounds (multiplier : %f).\n", mult);

  if (panelConfigGet_spinBoundsValue() == 1.)
    gtk_label_set_text(GTK_LABEL(labelRadius), LABEL_RADIUS);
  else
    {
      gtk_label_set_text(GTK_LABEL(labelRadius), LABEL_RADIUS_MULT);
      gtk_label_set_use_markup(GTK_LABEL(labelRadius), TRUE);
    }

  window = visuRenderingWindowGet_current();
  if (!window)
    return;
    
  dataObj = visuRenderingWindowGet_visuData(window);
  if (dataObj)
    for (i = 0; i < dataObj->ntype; i++)
      {
	val = renderingAtomicGet_radius(dataObj->fromIntToVisuElement[i]);
	if (renderingAtomicSet_radius(dataObj->fromIntToVisuElement[i],
				      val * mult))
	  renderingAtomic_createShape(visuDataGet_openGLView(dataObj),
				      dataObj->fromIntToVisuElement[i]);
      }
}
   
/* Callback when the value of the spin is changed. */
void radiusChanged(GtkSpinButton* button, gpointer data)
{
  float rad;
  gdouble mult;
  RenderingWindow *window;

  g_return_if_fail(currentElement);

  rad = (float)gtk_spin_button_get_value(GTK_SPIN_BUTTON(spinRadius));
  mult = panelConfigGet_spinBoundsValue();
  if (renderingAtomicSet_radius(currentElement, rad * mult))
    {
      window = RENDERING_WINDOW(visuRenderingWindowGet_current());
      g_return_if_fail(window);
      renderingAtomic_createShape(visuDataGet_openGLView(renderingWindowGet_visuData(window)),
				  currentElement);
      g_signal_emit (visu, VISU_GET_CLASS (visu)->OpenGLAskForReDraw_signal_id,
		     0 , NULL);
    }
}

void shapeChanged(GtkComboBox *box, gpointer data)
{
  int shape;
  RenderingWindow *window;

  g_return_if_fail(currentElement);

  shape = (int)gtk_combo_box_get_active(box);

  if (renderingAtomicSet_shape(currentElement, shape))
    {
      window = RENDERING_WINDOW(visuRenderingWindowGet_current());
      g_return_if_fail(window);
      renderingAtomic_createShape(visuDataGet_openGLView(renderingWindowGet_visuData(window)),
				  currentElement);
      g_signal_emit (visu, VISU_GET_CLASS (visu)->OpenGLAskForReDraw_signal_id,
		     0 , NULL);
    }
}
