/*   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.
*/
#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <gtk/gtk.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h> /* For the access markers R_OK, W_OK ... */

#include "callbacks.h"
#include "interface.h"
#include "support.h"

#include "visu_tools.h"
#include "visu_data.h"
#include "visu_basic.h"
#include "visu_rendering.h"
#include "visu_object.h"
#include "visu_configFile.h"
#include "visu_pickMesure.h"
#include "opengl.h"
#include "renderingBackend/visu_windowInterface.h"
#include "gtk_main.h"
#include "gtk_observePick.h"
#include "gtk_pairs.h"
#include "gtk_about.h"
#include "gtk_save.h"
#include "extraGtkFunctions/gtk_dumpDialogWidget.h"
#include "gtk_renderingWindowWidget.h"


GtkProgressBar *dumpBar;

void comboboxChanged(GtkComboBox *combo, gpointer data)
{
  int index;
  GtkTreeIter iter;
  gboolean selected;
  
  if (currentSubPanel->onLeave)
    g_idle_add(currentSubPanel->onLeave, (gpointer)0);

  index = gtk_combo_box_get_active(combo);
  selected = gtk_combo_box_get_active_iter(combo, &iter);
  g_return_if_fail(selected);

  gtk_tree_model_get(gtk_combo_box_get_model(combo), &iter,
		     GTKMAIN_COLUMN_POINTER_TO_DATA, &currentSubPanel,
		     -1);

  if (currentSubPanel->onEnter)
    g_idle_add(currentSubPanel->onEnter, (gpointer)0);

  /* disable the changed signal of the notebook
     before changing it. */
  g_signal_handler_block(G_OBJECT(data), tabSubPanelSignalChanged);
  gtk_notebook_set_current_page(GTK_NOTEBOOK(data), index);
  g_signal_handler_unblock(G_OBJECT(data), tabSubPanelSignalChanged);

  DBG_fprintf(stderr, "Main Gtk : changed combo '%s' -> %d\n",
	      currentSubPanel->comboLabel, index);
}
void subPanelChanged(GtkNotebook *book, GtkNotebookPage *page,
		     guint num, gpointer data)
{
  GtkTreeIter iter;
  gboolean selected;

  /* One can't call the function directly for obscur reasons
     dealing with the gtk working : it has to leave this
     callback before doing any other action. */
  if (currentSubPanel->onLeave)
    g_idle_add(currentSubPanel->onLeave, (gpointer)0);

  /* disable the changed signal of the combobox
     before changing it. */
  g_signal_handler_block(G_OBJECT(data), comboboxListeSubPanelSignalChanged);
  gtk_combo_box_set_active(GTK_COMBO_BOX(data), num);
  selected = gtk_combo_box_get_active_iter(GTK_COMBO_BOX(data), &iter);
  g_return_if_fail(selected);
  gtk_tree_model_get(gtk_combo_box_get_model(GTK_COMBO_BOX(data)), &iter,
		     GTKMAIN_COLUMN_POINTER_TO_DATA, &currentSubPanel,
		     -1);
  g_signal_handler_unblock(G_OBJECT(data), comboboxListeSubPanelSignalChanged);

  if (currentSubPanel->onEnter)
    g_idle_add(currentSubPanel->onEnter, (gpointer)0);
  
  DBG_fprintf(stderr, "Main Gtk : changed tab -> %d\n", (int)num);
}

void filterChanged(GtkEditable *entry, gpointer data)
{
  char *label;
  
  label = (char*)gtk_entry_get_text(GTK_ENTRY(entry));
  if (!label || label[0] == '\0')
    return;
  gtk_file_selection_complete(GTK_FILE_SELECTION(data), label);
}

void on_loadButton_clicked(GtkButton *button, gpointer user_data)
{
  renderingWindowLoad_file(RENDERING_WINDOW(visuRenderingWindowGet_current()));
}

void on_saveButton_clicked(GtkButton *button, gpointer user_data)
{
  gtkSaveInit();
}

void on_observeButton_clicked(GtkButton *button, gpointer user_data)
{
  /* Start new pick & observe session. */
  gtkObservePickInit_session();
}


void on_spinTheta_value_changed(GtkSpinButton *spinbutton, gpointer user_data)
{
  int reDrawNeeded;
  RenderingWindow *window;

  if (disableCallbacks)
    return;

  window = RENDERING_WINDOW(visuRenderingWindowGet_current());
  g_return_if_fail(window);

/*   g_signal_handler_block(G_OBJECT(visu), */
/* 			 signalCameraChangedId); */
  reDrawNeeded = visuDataSet_angleOfView(renderingWindowGet_visuData(window),
					 (float)gtk_spin_button_get_value(spinbutton),
					 0., 0., MASK_THETA);
/*   g_signal_handler_unblock(G_OBJECT(visu), */
/* 			   signalCameraChangedId); */
  if (reDrawNeeded)
    g_signal_emit (visu, VISU_GET_CLASS (visu)->OpenGLAskForReDraw_signal_id,
		   0 , NULL);
}

void on_spinOmega_value_changed(GtkSpinButton *spinbutton, gpointer user_data)
{
  int reDrawNeeded;
  RenderingWindow *window;

  if (disableCallbacks)
    return;

  window = RENDERING_WINDOW(visuRenderingWindowGet_current());
  g_return_if_fail(window);

/*   g_signal_handler_block(G_OBJECT(visu), */
/* 			 signalCameraChangedId); */
  reDrawNeeded = visuDataSet_angleOfView(renderingWindowGet_visuData(window),
					 0., 0., (float)gtk_spin_button_get_value(spinbutton),
					 MASK_OMEGA);
/*   g_signal_handler_unblock(G_OBJECT(visu), */
/* 			   signalCameraChangedId); */
  if (reDrawNeeded)
    g_signal_emit (visu, VISU_GET_CLASS (visu)->OpenGLAskForReDraw_signal_id,
		   0 , NULL);
}


void on_spinDx_value_changed(GtkSpinButton *spinbutton, gpointer user_data)
{
  int reDrawNeeded;
  RenderingWindow *window;

  if (disableCallbacks)
    return;

  window = RENDERING_WINDOW(visuRenderingWindowGet_current());
  g_return_if_fail(window);

/*   g_signal_handler_block(G_OBJECT(visu), */
/* 			 signalCameraChangedId); */
  reDrawNeeded = visuDataSet_positionOfView(renderingWindowGet_visuData(window),
					    (float)gtk_spin_button_get_value(spinbutton),
					    0., MASK_XS);
/*   g_signal_handler_unblock(G_OBJECT(visu), */
/* 			   signalCameraChangedId); */
  if (reDrawNeeded)
    g_signal_emit (visu, VISU_GET_CLASS (visu)->OpenGLAskForReDraw_signal_id,
		   0 , NULL);
}


void on_spinGross_value_changed(GtkSpinButton *spinbutton, gpointer user_data)
{
  int reDrawNeeded;
  RenderingWindow *window;

  if (disableCallbacks)
    return;

  window = RENDERING_WINDOW(visuRenderingWindowGet_current());
  g_return_if_fail(window);

/*   g_signal_handler_block(G_OBJECT(visu), */
/* 			 signalCameraChangedId); */
  reDrawNeeded = visuDataSet_zoomOfView(renderingWindowGet_visuData(window),
					(float)gtk_spin_button_get_value(spinbutton));
/*   g_signal_handler_unblock(G_OBJECT(visu), */
/* 			   signalCameraChangedId); */
  if (reDrawNeeded)
    g_signal_emit (visu, VISU_GET_CLASS (visu)->OpenGLAskForReDraw_signal_id,
		   0 , NULL);
}


void on_spinPhi_value_changed(GtkSpinButton *spinbutton, gpointer user_data)
{
  int reDrawNeeded;
  RenderingWindow *window;

  if (disableCallbacks)
    return;

  window = RENDERING_WINDOW(visuRenderingWindowGet_current());
  g_return_if_fail(window);

/*   g_signal_handler_block(G_OBJECT(visu), */
/* 			 signalCameraChangedId); */
  reDrawNeeded = visuDataSet_angleOfView(renderingWindowGet_visuData(window), 0.,
					 (float)gtk_spin_button_get_value(spinbutton),
					 0., MASK_PHI);
/*   g_signal_handler_unblock(G_OBJECT(visu), */
/* 			   signalCameraChangedId); */
  if (reDrawNeeded)
    g_signal_emit (visu, VISU_GET_CLASS (visu)->OpenGLAskForReDraw_signal_id,
		   0 , NULL);
}


void on_spinDy_value_changed(GtkSpinButton *spinbutton, gpointer user_data)
{
  int reDrawNeeded;
  RenderingWindow *window;

  if (disableCallbacks)
    return;

  window = RENDERING_WINDOW(visuRenderingWindowGet_current());
  g_return_if_fail(window);

/*   g_signal_handler_block(G_OBJECT(visu), */
/* 			 signalCameraChangedId); */
  reDrawNeeded = visuDataSet_positionOfView(renderingWindowGet_visuData(window), 0.,
					    (float)gtk_spin_button_get_value(spinbutton), MASK_YS);
/*   g_signal_handler_unblock(G_OBJECT(visu), */
/* 			   signalCameraChangedId); */
  if (reDrawNeeded)
    g_signal_emit (visu, VISU_GET_CLASS (visu)->OpenGLAskForReDraw_signal_id,
		   0 , NULL);
}


void on_spinPersp_value_changed(GtkSpinButton *spinbutton, gpointer user_data)
{
  int reDrawNeeded;
  RenderingWindow *window;

  if (disableCallbacks)
    return;

  window = RENDERING_WINDOW(visuRenderingWindowGet_current());
  g_return_if_fail(window);

/*   g_signal_handler_block(G_OBJECT(visu), */
/* 			 signalCameraChangedId); */
  reDrawNeeded = visuDataSet_perspectiveOfView(renderingWindowGet_visuData(window),
					       (float)gtk_spin_button_get_value(spinbutton));
/*   g_signal_handler_unblock(G_OBJECT(visu), */
/* 			   signalCameraChangedId); */
  if (reDrawNeeded)
    g_signal_emit (visu, VISU_GET_CLASS (visu)->OpenGLAskForReDraw_signal_id,
		   0 , NULL);
}


void on_visuAbout_clicked(GtkButton *button, gpointer user_data)
{
  if (!aboutWindow)
    buildAboutWindow();
  gtk_widget_show(aboutWindow);
}

void on_pairsButton_clicked(GtkButton *button, gpointer user_data)
{
  if (!pairsDialog)
    buildPairsDialog();

  pairsIsHide = 0;
  if (pairsIsOutOfDate)
    gtkPairsBuild_viewport();

  visuShowWindow(GTK_WINDOW(pairsDialog));
}


void on_closebutton3_clicked(GtkButton *button, gpointer user_data)
{
  visuHideWindow(GTK_WINDOW(pairsDialog));
  pairsIsHide = 1;
}

gboolean on_pairsDialog_delete_event(GtkWidget *widget, GdkEvent *event, gpointer user_data)
{
  pairsDialog = (GtkWidget*)0;
  return FALSE;
}
