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

#include <visu_object.h>
#include <support.h>
#include <opengl.h>
#include <visu_extension.h>
#include <openGLFunctions/light.h>
#include <openGLFunctions/renderingMode.h>
#include <openGLFunctions/view.h>
#include <gtk_renderingWindowWidget.h>

int disableCallbacksOpenGL;

static GtkListStore *light_list_store;

#define OPENGL_FOLLOW_GLOBAL_RENDERING_MODE _("Follow global setting")

/* Local callbacks. */
void comboOpenGLRenderingChanged(GtkComboBox *combobox, gpointer data);
void addPresetOneLightClicked(GtkButton *button, gpointer data);
void addPresetFourLightsClicked(GtkButton *button, gpointer data);
static void selectionChanged(GtkTreeSelection *tree, gpointer data);
static void addNewLightClicked(GtkButton *button, gpointer data);
static void removeSelectedLightsClicked(GtkButton *button, gpointer data);
static void renderingModeEdited(GtkCellRendererText *cellrenderertext,
				gchar *path, gchar *text, gpointer user_data);

subPanel* initPanelOpenGL()
{
  char *cl = _("Set OpenGL parameters");
  char *tl = _("OpenGL");
  GtkWidget *child;

  child = createInteriorOpenGL();
  panelOpenGL = createSubPanelWithIconFromStock(cl, tl, GTK_STOCK_EXECUTE,
						child, refreshPanelOpenGL,
						(onChangePanelFunc)0);
  if (!panelOpenGL)
    return (subPanel*)0;

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

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

  /* Private parameters. */
  disableCallbacksOpenGL = 0;

  return panelOpenGL;
}

enum {
  LIGHT_POINTER_TO,
  LIGHT_ENABLED,
  LIGHT_POSITION0,
  LIGHT_POSITION1,
  LIGHT_POSITION2,
  LIGHT_MULTIPLIER,
  LIGHT_N_PARAMETERS
};

enum {
  RENDERING_POINTER_TO,
  RENDERING_NAME,
  RENDERING_MODE,
  RENDERING_N_PARAMETERS
};

void light_store_in_list_store(gpointer data, gpointer user_data)
{
  Light *light0 = data;
  GtkTreeIter iter;
  GtkListStore *list;

  g_return_if_fail(user_data);
  list = GTK_LIST_STORE(user_data);

  gtk_list_store_append(list, &iter);
  gtk_list_store_set(list, &iter,
		     LIGHT_POINTER_TO, (gpointer)light0,
		     LIGHT_ENABLED, light0->enabled,
		     LIGHT_POSITION0, light0->position[0],
		     LIGHT_POSITION1, light0->position[1],
		     LIGHT_POSITION2, light0->position[2], 
		     LIGHT_MULTIPLIER, light0->multiplier, -1);
}

void light_tree_show_hide(GtkCellRendererToggle *cell_renderer, 
			  gchar *string_path, gpointer user_data)
{
  GtkTreePath* path = gtk_tree_path_new_from_string(string_path);
  GtkTreeIter iter;
  gboolean status;
  Light *light0;
  GtkListStore *list;

  g_return_if_fail(user_data);
  list = GTK_LIST_STORE(user_data);

  if(gtk_tree_model_get_iter(GTK_TREE_MODEL(light_list_store), &iter, path) == FALSE)
    return;

  status = gtk_cell_renderer_toggle_get_active(cell_renderer) ? FALSE : TRUE;
  gtk_list_store_set(list, &iter,
		     LIGHT_ENABLED, status, -1);
  gtk_tree_model_get(GTK_TREE_MODEL(list), &iter,
		     LIGHT_POINTER_TO, &light0, -1);

  light0->enabled = status;

  status = lightEnvironnementApply(currentLights);
  if (status)
    g_signal_emit(visu, VISU_GET_CLASS (visu)->OpenGLAskForReDraw_signal_id, 0, NULL);
}

void light_update(GtkCellRendererText *cellrenderertext,
		  gchar *path, gchar *text, gpointer user_data)
{
  GtkTreeIter iter;
  float new_value = atof(text);
  Light *light0;
  gboolean status;

  if(gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(light_list_store),
					 &iter, path))
    {      
      gtk_list_store_set(light_list_store, &iter,
			 GPOINTER_TO_INT(user_data), new_value, -1);
      gtk_tree_model_get(GTK_TREE_MODEL(light_list_store), &iter, 
			 LIGHT_POINTER_TO, &light0, -1);
    }

  if (GPOINTER_TO_INT(user_data) == LIGHT_MULTIPLIER)
    light0->multiplier = new_value;
  else
    light0->position[GPOINTER_TO_INT(user_data)-LIGHT_POSITION0] = new_value;
			 
  status = lightEnvironnementApply(currentLights);
  if (status)
    g_signal_emit(visu, VISU_GET_CLASS (visu)->OpenGLAskForReDraw_signal_id, 0, NULL);
}

void light_sync_lists(GtkListStore *list)
{
  g_return_if_fail(list);

  gtk_list_store_clear(list);
  g_list_foreach(lightEnvironnementGet_lightList(currentLights),
		 light_store_in_list_store, (gpointer)list);  
}

GtkWidget* lights_make_tree_view()
{
  GtkTreeViewColumn* column; 
  GtkCellRenderer *cell_show = gtk_cell_renderer_toggle_new();
  GtkCellRenderer *cell_float;
  GtkWidget *light_tree_view;

  light_list_store =
    gtk_list_store_new(LIGHT_N_PARAMETERS, G_TYPE_POINTER,
		       G_TYPE_BOOLEAN, G_TYPE_FLOAT, G_TYPE_FLOAT,
		       G_TYPE_FLOAT, G_TYPE_FLOAT);
  light_tree_view =
    gtk_tree_view_new_with_model(GTK_TREE_MODEL(light_list_store));

  light_sync_lists(light_list_store);

  g_signal_connect(cell_show, "toggled", G_CALLBACK(light_tree_show_hide),
		   (gpointer)light_list_store);

  column = gtk_tree_view_column_new_with_attributes(_("Use"), cell_show,
						    "active", LIGHT_ENABLED, NULL);
  gtk_tree_view_column_set_expand(column, FALSE);
  gtk_tree_view_column_set_alignment(column, 0.5);
  gtk_tree_view_append_column (GTK_TREE_VIEW (light_tree_view), column);

  cell_float = gtk_cell_renderer_text_new();
  g_object_set(G_OBJECT(cell_float), "editable", TRUE, NULL);
  g_signal_connect(G_OBJECT(cell_float), "edited",
		   G_CALLBACK(light_update), GINT_TO_POINTER(LIGHT_POSITION0));
  column = gtk_tree_view_column_new_with_attributes(_("x"), cell_float,
						    "text", LIGHT_POSITION0, NULL);
  gtk_tree_view_append_column (GTK_TREE_VIEW (light_tree_view), column);
  
  cell_float = gtk_cell_renderer_text_new();
  g_object_set(G_OBJECT(cell_float), "editable", TRUE, NULL);
  g_signal_connect(G_OBJECT(cell_float), "edited",
		   G_CALLBACK(light_update), GINT_TO_POINTER(LIGHT_POSITION1));
  column = gtk_tree_view_column_new_with_attributes(_("y"), cell_float,
						    "text", LIGHT_POSITION1, NULL);
  gtk_tree_view_append_column (GTK_TREE_VIEW (light_tree_view), column);

  cell_float = gtk_cell_renderer_text_new();
  g_object_set(G_OBJECT(cell_float), "editable", TRUE, NULL);
  g_signal_connect(G_OBJECT(cell_float), "edited",
		   G_CALLBACK(light_update), GINT_TO_POINTER(LIGHT_POSITION2));
  column = gtk_tree_view_column_new_with_attributes(_("z"), cell_float,
						    "text", LIGHT_POSITION2, NULL);
  gtk_tree_view_append_column (GTK_TREE_VIEW (light_tree_view), column);

  cell_float = gtk_cell_renderer_text_new();
  g_object_set(G_OBJECT(cell_float), "editable", TRUE, NULL);
  g_signal_connect(G_OBJECT(cell_float), "edited",
		   G_CALLBACK(light_update), GINT_TO_POINTER(LIGHT_MULTIPLIER));
  column = gtk_tree_view_column_new_with_attributes(_("power"), cell_float,
						    "text", LIGHT_MULTIPLIER, NULL);
  gtk_tree_view_append_column (GTK_TREE_VIEW (light_tree_view), column);
  gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (light_tree_view), TRUE);

  gtk_tree_selection_set_mode(gtk_tree_view_get_selection(GTK_TREE_VIEW(light_tree_view)),
			      GTK_SELECTION_MULTIPLE);

  return light_tree_view;
}

GtkWidget* make_renderingTreeView()
{
#if GTK_MINOR_VERSION > 5
  GtkTreeViewColumn* column; 
  GtkCellRenderer *renderer;
  GtkWidget *renderingTreeView;
  GtkListStore *renderingListStore;
  GtkListStore *namesListStore;
  GtkTreeIter iter;
  const char** names;
  int i;
  GList *list;
  const char *modeName;

  /* Initialize the model that store the possibilities
     for the cellRendererComboBox. */
  namesListStore = gtk_list_store_new(1, G_TYPE_STRING);
  gtk_list_store_append(namesListStore, &iter);
  gtk_list_store_set(namesListStore, &iter,
		     0, OPENGL_FOLLOW_GLOBAL_RENDERING_MODE, -1);
  names = openGLGet_allRenderingModesI18n();
  for (i = 0; names[i]; i++)
    {
      gtk_list_store_append(namesListStore, &iter);
      gtk_list_store_set(namesListStore, &iter,
			 0, names[i], -1);
    }

  /* Initialize the model that stores the extensions. */
  renderingListStore =
    gtk_list_store_new(RENDERING_N_PARAMETERS, G_TYPE_POINTER,
		       G_TYPE_STRING, G_TYPE_STRING);
  list = OpenGLExtensionGet_list();
  while(list)
    {
      if (((OpenGLExtension*)list->data)->isSensitiveToRenderingMode)
	{
	  if (((OpenGLExtension*)list->data)->preferedRenderingMode == -1)
	    modeName = OPENGL_FOLLOW_GLOBAL_RENDERING_MODE;
	  else
	    modeName = names[((OpenGLExtension*)list->data)->preferedRenderingMode];
	  gtk_list_store_append(renderingListStore, &iter);
	  gtk_list_store_set(renderingListStore, &iter,
			     RENDERING_POINTER_TO, list->data,
			     RENDERING_NAME, ((OpenGLExtension*)list->data)->name,
			     RENDERING_MODE, modeName, -1);
	}
      list = g_list_next(list);
    }

  renderingTreeView =
    gtk_tree_view_new_with_model(GTK_TREE_MODEL(renderingListStore));

  renderer = gtk_cell_renderer_text_new();
  column = gtk_tree_view_column_new_with_attributes(_("name of extension"), renderer,
						    "text", RENDERING_NAME, NULL);
  gtk_tree_view_column_set_expand(column, TRUE);
  gtk_tree_view_column_set_alignment(column, 0.);
  gtk_tree_view_append_column(GTK_TREE_VIEW(renderingTreeView), column);

  renderer = gtk_cell_renderer_combo_new();
  g_object_set(G_OBJECT(renderer), "has-entry", FALSE,
	       "model", namesListStore, "text-column", 0,
	       "editable", TRUE, NULL);
  g_signal_connect(G_OBJECT(renderer), "edited",
		   G_CALLBACK(renderingModeEdited), (gpointer)renderingListStore);
  column = gtk_tree_view_column_new_with_attributes(_("mode"), renderer,
						    "text", RENDERING_MODE, NULL);
  gtk_tree_view_column_set_alignment(column, 0.5);
  gtk_tree_view_append_column(GTK_TREE_VIEW(renderingTreeView), column);
  
  return renderingTreeView;
#endif
  return (GtkWidget*)0;
}
  
void addNewLightClicked(GtkButton *button, gpointer data)
{
  Light *light0;
  GtkTreeIter iter;
  gboolean status;

  g_return_if_fail(data);

  if(gtk_tree_model_iter_n_children(GTK_TREE_MODEL(data), (GtkTreeIter*)0) >= GL_MAX_LIGHTS)
    {
      raiseAlertDialog(_("The maximm number of lights allowed by OpenGL"
			 " has been reached, can't add more."));
      return;
    }

  light0 = light_newDefault();
  status = lightEnvironnementAdd_light(currentLights, light0);

  if (!status)
    return;

  gtk_list_store_append(GTK_LIST_STORE(data), &iter);
  gtk_list_store_set(GTK_LIST_STORE(data), &iter,
		     LIGHT_POINTER_TO, (gpointer)light0,
		     LIGHT_ENABLED, light0->enabled,
		     LIGHT_POSITION0, light0->position[0],
		     LIGHT_POSITION1, light0->position[1],
		     LIGHT_POSITION2, light0->position[2], 
		     LIGHT_MULTIPLIER, light0->multiplier, -1);

  status = lightEnvironnementApply(currentLights);
  if (status)
    g_signal_emit(visu, VISU_GET_CLASS (visu)->OpenGLAskForReDraw_signal_id, 0, NULL);
}
void removeSelectedLightsClicked(GtkButton *button, gpointer data)
{
  GList *tmpLst, *selectedRows;
  GtkTreeIter iter;
  gboolean validIter, status;
  Light *light0;
  GtkTreeIter *removableIter;

  g_return_if_fail(GTK_TREE_SELECTION(data));

  selectedRows =
    gtk_tree_selection_get_selected_rows(GTK_TREE_SELECTION(data), NULL);
  tmpLst = selectedRows;
  while (tmpLst)
    {
      validIter = gtk_tree_model_get_iter(GTK_TREE_MODEL(light_list_store),
					  &iter, (GtkTreePath*)tmpLst->data);
      gtk_tree_path_free((GtkTreePath*)tmpLst->data);
      tmpLst->data = (gpointer)0;
      if (!validIter)
	g_warning("Wrong 'path' variable in 'removeSelectedLightsClicked' method.\n");
      else
	{
	  gtk_tree_model_get(GTK_TREE_MODEL(light_list_store), &iter,
			     LIGHT_POINTER_TO, &light0,
			     -1);
	  lightEnvironnementDelete_light(currentLights, light0);
	  removableIter = g_malloc(sizeof(GtkTreeIter));
	  *removableIter = iter;
	  tmpLst->data = (gpointer)removableIter;
	}
      tmpLst = g_list_next(tmpLst);
    }
  tmpLst = selectedRows;
  while (tmpLst)
    {
      if (tmpLst->data)
	{
	  gtk_list_store_remove(light_list_store, (GtkTreeIter*)tmpLst->data);
	  g_free(tmpLst->data);
	}
      tmpLst = g_list_next(tmpLst);
    }  
  g_list_free(selectedRows);

  status = lightEnvironnementApply(currentLights);
  if (status)
    g_signal_emit(visu, VISU_GET_CLASS (visu)->OpenGLAskForReDraw_signal_id, 0, NULL);
}
void selectionChanged(GtkTreeSelection *tree, gpointer data)
{
  gint nb;

  g_return_if_fail(GTK_WIDGET(data));

  nb = gtk_tree_selection_count_selected_rows(tree);
  if (nb == 0)
    gtk_widget_set_sensitive(GTK_WIDGET(data), FALSE);
  else
    gtk_widget_set_sensitive(GTK_WIDGET(data), TRUE);
}


GtkWidget *createInteriorOpenGL()
{
  GtkWidget *vbox, *hbox;
  GtkWidget *label;
  GtkWidget *table;
  GtkWidget *align;
  GtkObject *adj;
  GtkWidget *scrollLight, *scrollRendering;
  GtkWidget *button;
  GtkWidget *image;
  GtkWidget *scroll, *viewport;
  GtkWidget *expanderLight, *vbox2, *expanderRendering;
  GtkWidget *light_tree_view, *renderingTreeView;
  const char** names;
  int i;

  scroll = gtk_scrolled_window_new(NULL, NULL);
  gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll),
				 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);

  viewport = gtk_viewport_new(NULL, NULL);
  gtk_container_add(GTK_CONTAINER(scroll), viewport);

  vbox = gtk_vbox_new(FALSE, 0);
  gtk_container_add(GTK_CONTAINER(viewport), vbox);

  hbox = gtk_hbox_new(FALSE, 0);
  label = gtk_label_new(_("Redraw immediately after changes :"));
  gtk_widget_set_name(label, "label_head");
  gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 2);
  checkImmediateDrawing = gtk_check_button_new();
/*   gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkImmediateDrawing), */
/* 			       (gboolean)get); */
  gtk_box_pack_start(GTK_BOX(hbox), checkImmediateDrawing, FALSE, FALSE, 2);
  gtk_widget_show(checkImmediateDrawing);
  gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 5);

  table = gtk_table_new(2, 1, FALSE);
  gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 5);
  align = gtk_alignment_new(0., 0.5, 0., 0.);
  gtk_table_attach(GTK_TABLE(table), align, 0, 1, 0, 1, GTK_FILL | GTK_EXPAND, GTK_SHRINK, 2, 0);
  label = gtk_label_new(_("Choose a rendering option :"));
  gtk_widget_set_name(label, "label_head");
  gtk_container_add(GTK_CONTAINER(align), label);
  align = gtk_alignment_new(1.0, 0.5, 0., 0.);
  gtk_table_attach(GTK_TABLE(table), align, 0, 1, 1, 2, GTK_FILL | GTK_EXPAND, GTK_SHRINK, 2, 0);

  comboOpenGLRendering = gtk_combo_box_new_text();
  names = openGLGet_allRenderingModesI18n();
  if (names)
    for (i = 0; names[i]; i++)
      gtk_combo_box_append_text(GTK_COMBO_BOX(comboOpenGLRendering), names[i]);
  else
    g_warning("No OpenGL rendering mode available.");
  gtk_combo_box_set_active(GTK_COMBO_BOX(comboOpenGLRendering), 0);
  gtk_container_add(GTK_CONTAINER(align), comboOpenGLRendering);
  g_signal_connect(G_OBJECT(comboOpenGLRendering), "changed",
		   G_CALLBACK(comboOpenGLRenderingChanged), (gpointer)0);

  hbox = gtk_hbox_new(FALSE, 0);
  label = gtk_label_new(_("Precision (100 is normal) : "));
  gtk_widget_set_name(label, "label_head");
  gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 2);
  adj = gtk_adjustment_new(5., 10., 500., 5., 1.0, 1.0);
  spinPrecision = gtk_spin_button_new(GTK_ADJUSTMENT(adj), 0.01, 0);
  gtk_spin_button_set_value(GTK_SPIN_BUTTON(spinPrecision),
			    OpenGLViewGet_precision() * 100.);
  gtk_box_pack_start(GTK_BOX(hbox), spinPrecision, FALSE, FALSE, 2);
  gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 5);

  hbox = gtk_hbox_new(FALSE, 0);
  label = gtk_label_new(_("Antialiase lines :"));
  gtk_widget_set_name(label, "label_head");
  gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 2);
  checkAntialiasing = gtk_check_button_new();
  gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkAntialiasing),
			       (gboolean)getAntialias());
  gtk_box_pack_start(GTK_BOX(hbox), checkAntialiasing, FALSE, FALSE, 2);
  gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 5);

  /*********************/
  /* Light parameters. */
  /*********************/
  expanderLight = gtk_expander_new(_("Light sources"));
  label = gtk_expander_get_label_widget(GTK_EXPANDER(expanderLight));
  gtk_widget_set_name(label, "label_head");
  gtk_box_pack_start(GTK_BOX(vbox), expanderLight, TRUE, TRUE, 5);
  hbox = gtk_hbox_new(FALSE, 0);
  gtk_container_add(GTK_CONTAINER(expanderLight), hbox);
  scrollLight = gtk_scrolled_window_new (NULL, NULL);
  gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollLight),
				 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
  gtk_box_pack_start(GTK_BOX(hbox), scrollLight, TRUE, TRUE, 0);
  light_tree_view = lights_make_tree_view();
  gtk_container_add(GTK_CONTAINER(scrollLight), light_tree_view);
  vbox2 = gtk_vbox_new(FALSE, 0);
  gtk_box_pack_start(GTK_BOX(hbox), vbox2, FALSE, FALSE, 0);
  button = gtk_button_new();
  image = gtk_image_new_from_stock(GTK_STOCK_ADD,
				   GTK_ICON_SIZE_BUTTON);
  gtk_container_add(GTK_CONTAINER(button), image);
  g_signal_connect(G_OBJECT(button), "clicked",
		   G_CALLBACK(addNewLightClicked),
		   (gpointer)gtk_tree_view_get_model(GTK_TREE_VIEW(light_tree_view)));
  gtk_box_pack_start(GTK_BOX(vbox2), button, FALSE, FALSE, 1);
  button = gtk_button_new();
  gtk_widget_set_sensitive(button, FALSE);
  image = gtk_image_new_from_stock(GTK_STOCK_REMOVE,
				   GTK_ICON_SIZE_BUTTON);
  gtk_container_add(GTK_CONTAINER(button), image);
  g_signal_connect(G_OBJECT(button), "clicked",
		   G_CALLBACK(removeSelectedLightsClicked),
		   (gpointer)gtk_tree_view_get_selection(GTK_TREE_VIEW(light_tree_view)));
  gtk_box_pack_start(GTK_BOX(vbox2), button, FALSE, FALSE, 1);
  g_signal_connect(G_OBJECT(gtk_tree_view_get_selection(GTK_TREE_VIEW(light_tree_view))),
		   "changed", G_CALLBACK(selectionChanged), (gpointer)button);

  button = gtk_button_new();
  image = create_pixmap(mainWindow, "stock-one-light_20.png");
  gtk_container_add(GTK_CONTAINER(button), image);
  g_signal_connect(G_OBJECT(button), "clicked",
		   G_CALLBACK(addPresetOneLightClicked),
		   (gpointer)gtk_tree_view_get_model(GTK_TREE_VIEW(light_tree_view)));
  gtk_box_pack_end(GTK_BOX(vbox2), button, FALSE, FALSE, 1);
  button = gtk_button_new();
  image = create_pixmap(mainWindow, "stock-four-lights_20.png");
  gtk_container_add(GTK_CONTAINER(button), image);
  g_signal_connect(G_OBJECT(button), "clicked",
		   G_CALLBACK(addPresetFourLightsClicked),
		   (gpointer)gtk_tree_view_get_model(GTK_TREE_VIEW(light_tree_view)));
  gtk_box_pack_end(GTK_BOX(vbox2), button, FALSE, FALSE, 1);

  /******************************/
  /* Rendering mode parameters. */
  /******************************/
#if GTK_MINOR_VERSION > 5
  expanderRendering = gtk_expander_new(_("Per extension rendering mode"));
  label = gtk_expander_get_label_widget(GTK_EXPANDER(expanderRendering));
  gtk_widget_set_name(label, "label_head");
  gtk_box_pack_start(GTK_BOX(vbox), expanderRendering, TRUE, TRUE, 5);
  scrollRendering = gtk_scrolled_window_new(NULL, NULL);
  gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollRendering),
				 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
  gtk_container_add(GTK_CONTAINER(expanderRendering), scrollRendering);
  renderingTreeView = make_renderingTreeView();
  gtk_widget_set_size_request(renderingTreeView, -1, 120);
  gtk_container_add(GTK_CONTAINER(scrollRendering), renderingTreeView);
#endif
  
  /************************/
  /* Force redraw option. */
  /************************/
  hbox = gtk_hbox_new(FALSE, 0);
  label = gtk_label_new(_("Force redraw when unmasked:"));
  gtk_widget_set_name(label, "label_head");
  gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 2);
  checkFakeBackingStore = gtk_check_button_new();
  gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkFakeBackingStore),
			       (gboolean)getFakeBackingStore());
  gtk_box_pack_start(GTK_BOX(hbox), checkFakeBackingStore, FALSE, FALSE, 2);
  gtk_widget_show(checkFakeBackingStore);
  gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 5);

  gtk_widget_show_all(scroll);
  return scroll;
}

void createCallBacksOpenGL()
{
  g_signal_connect((gpointer)spinPrecision, "value-changed",
		   G_CALLBACK(precisionChanged), (gpointer)0);
  g_signal_connect((gpointer)checkAntialiasing, "toggled",
		   G_CALLBACK(antialisingCheckChanged), (gpointer)0);
  g_signal_connect((gpointer)checkFakeBackingStore, "toggled",
		   G_CALLBACK(fakeBakingSoreCheckChanged), (gpointer)0);
  g_signal_connect((gpointer)checkImmediateDrawing, "toggled",
		   G_CALLBACK(immediateDrawingCheckChanged), (gpointer)0);
}

/*************/
/* Callbacks */
/*************/
void immediateDrawingCheckChanged(GtkToggleButton *button, gpointer data)
{
  if (disableCallbacksOpenGL)
    return;

  setImmediateDrawing(gtk_toggle_button_get_active(button));
}
void comboOpenGLRenderingChanged(GtkComboBox *combobox, gpointer data)
{
  int mode;
  
  if (disableCallbacksOpenGL)
    return;

  mode = (int)gtk_combo_box_get_active(combobox);

  if (openGLSet_globalRenderingMode(mode))
    g_signal_emit (visu, VISU_GET_CLASS (visu)->OpenGLAskForReDraw_signal_id,
		   0 , NULL);
}
void antialisingCheckChanged(GtkToggleButton *button, gpointer data)
{
  if (disableCallbacksOpenGL)
    return;

  if (setAntialias(gtk_toggle_button_get_active(button)))
    g_signal_emit (visu, VISU_GET_CLASS (visu)->OpenGLAskForReDraw_signal_id,
		   0 , NULL);
}
void precisionChanged(GtkButton *button, gpointer user_data)
{
  float prec;
  RenderingWindow *window;
  VisuData *dataObj;

  if (disableCallbacksOpenGL)
    return;

  window = RENDERING_WINDOW(visuRenderingWindowGet_current());
  if (!window)
    return;

  prec = (float)gtk_spin_button_get_value(GTK_SPIN_BUTTON(spinPrecision));
  if (OpenGLViewSet_precision(prec / 100.))
    {
      visuDataEmit_facettesChanged(renderingWindowGet_visuData(window));
      visuData_createAllElements(renderingWindowGet_visuData(window)); 
      g_signal_emit (visu, VISU_GET_CLASS (visu)->OpenGLAskForReDraw_signal_id,
		     0 , NULL);
    }
}
void fakeBakingSoreCheckChanged(GtkToggleButton *button, gpointer data)
{
  if (disableCallbacksOpenGL)
    return;

  setFakeBackingStore(gtk_toggle_button_get_active(button));
}
gboolean refreshPanelOpenGL(gpointer data)
{
  disableCallbacksOpenGL = 1;
  gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkImmediateDrawing),
			       (gboolean)getImmediateDrawing());
  gtk_combo_box_set_active(GTK_COMBO_BOX(comboOpenGLRendering),
			   openGLGet_globalRenderingOption());
  gtk_spin_button_set_value(GTK_SPIN_BUTTON(spinPrecision),
			    OpenGLViewGet_precision() * 100.);
  gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkAntialiasing),
			       (gboolean)getAntialias());
  gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkFakeBackingStore),
			       (gboolean)getFakeBackingStore());
  disableCallbacksOpenGL = 0;

  return FALSE;
}
void addPresetOneLightClicked(GtkButton *button, gpointer data)
{
  Light *light;
  gboolean status;

  g_return_if_fail(data);

  status = lightEnvironnementEmpty_lightList(currentLights);

  light = light_newDefault();
  status = lightEnvironnementAdd_light(currentLights, light) || status;

  light_sync_lists(GTK_LIST_STORE(data)); 

  status = lightEnvironnementApply(currentLights) || status;
  if (status)
    g_signal_emit(visu, VISU_GET_CLASS (visu)->OpenGLAskForReDraw_signal_id, 0, NULL);
}
void addPresetFourLightsClicked(GtkButton *button, gpointer data)
{
  Light *light;
  gboolean status;

  g_return_if_fail(data);

  status = lightEnvironnementEmpty_lightList(currentLights);

  light = light_newDefault();
  light->multiplier = 0.25;
  status = lightEnvironnementAdd_light(currentLights, light) || status;

  light = light_newDefault();
  light->position[0] *= -1.;
  light->multiplier = 0.25;
  status = lightEnvironnementAdd_light(currentLights, light) || status;

  light = light_newDefault();
  light->position[1] *= -1.;
  light->multiplier = 0.25;
  status = lightEnvironnementAdd_light(currentLights, light) || status;

  light = light_newDefault();
  light->position[0] *= -1.;
  light->position[1] *= -1.;
  light->multiplier = 0.25;
  status = lightEnvironnementAdd_light(currentLights, light) || status;

  light_sync_lists(GTK_LIST_STORE(data)); 

  status = lightEnvironnementApply(currentLights) || status;
  if (status)
    g_signal_emit(visu, VISU_GET_CLASS (visu)->OpenGLAskForReDraw_signal_id, 0, NULL);
}
static void renderingModeEdited(GtkCellRendererText *cellrenderertext,
				gchar *path, gchar *text, gpointer user_data)
{
  GtkListStore *list;
  gboolean valid;
  GtkTreeIter iter;
  const char** names;
  int mode;
  OpenGLExtension *ext;

  list = GTK_LIST_STORE(user_data);
  g_return_if_fail(list);

  valid = gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(list),
					      &iter, path);
  g_return_if_fail(valid);

  if (!strcmp(text, OPENGL_FOLLOW_GLOBAL_RENDERING_MODE))
    {
      valid = TRUE;
      mode = -1;
    }
  else
    {
      valid = FALSE;
      names = openGLGet_allRenderingModesI18n();
      for (mode = 0; names[mode] && !valid; mode++)
	valid = !strcmp(text, names[mode]);
    }
  g_return_if_fail(valid);

  gtk_tree_model_get(GTK_TREE_MODEL(list), &iter, 
		     RENDERING_POINTER_TO, &ext, -1);
  valid = OpenGLExtensionSet_preferedRenderingMode(ext, mode - 1);

  if (mode == -1)
    gtk_list_store_set(list, &iter,
		       RENDERING_MODE, OPENGL_FOLLOW_GLOBAL_RENDERING_MODE, -1);
  else
    {
      names = openGLGet_allRenderingModesI18n();
      gtk_list_store_set(list, &iter,
			 RENDERING_MODE, names[mode - 1], -1);
    }

  if (valid)
    g_signal_emit(visu, VISU_GET_CLASS (visu)->OpenGLAskForReDraw_signal_id, 0, NULL);
}
