#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

#include "Controle.h"
#include "CheckBox.h"
#include "Couleur.h"
#include "ModuleFont.h"

CheckBox::CheckBox(void):Controle() {
  coulBox = new Couleur();
  coulTexte = new Couleur();
  dx = 0;
  d = NULL;
  strcpy(titre,"");
}

CheckBox::CheckBox(int px,int py,int dx,Couleur *coul,Couleur *coulB,const char *titre,Couleur *coulT,int *d):Controle(px,py,coul) {
  this->coulBox = new Couleur(coulB);
  this->coulTexte = new Couleur(coulT);
  this->dx = dx;
  this->d = d;
  strcpy(this->titre,titre);
}

CheckBox::~CheckBox(void) {
  delete(this->coulTexte);
  delete(this->coulBox);
}

void CheckBox::dessiner(void) {
  if ( !enabled )
    actif = 0;
  glDisable(GL_LIGHTING);
  glPushMatrix();
  glTranslatef(px,py,0.0F);
  setGLColor((float *) coul);
  { glPushMatrix();
    glTranslatef(dx,0.0F,0.0F);
    glBegin(GL_LINE_LOOP);
    glVertex2f(0.0F,2.0F);
    glVertex2f(0.0F,-10.0F);
    glVertex2f(12.0F,-10.0F);
    glVertex2f(12.0F,2.0F);
    glEnd();
    glPopMatrix(); }
  if ( *d ) {
    setGLColor((float *) coulBox);
    glPushMatrix();
    glTranslatef(dx,0.0F,0.0F);
    glBegin(GL_QUADS);
    glVertex2f(0.0F,2.0F);
    glVertex2f(0.0F,-10.0F);
    glVertex2f(12.0F,-10.0F);
    glVertex2f(12.0F,2.0F);
    glEnd();
    glPopMatrix(); }
  setGLColor((float *) coulTexte);
  { setFont(GLUT_BITMAP_8_BY_13,LEFT);
    glPushMatrix();
    bitmapStringOutput(0.0F,0.0F,0.0F,titre);
    glPopMatrix(); }
  glPopMatrix();
}

void CheckBox::sauve(void) {
  if ( d )
    save = *d;
}

///
/// Restoration des valeurs sauvegardees liees au CheckBox.
///
/// @author Nicolas Janey
/// @author nicolas.janey@univ-fcomte.fr
/// @version 1.0, 25/02/10
///
void CheckBox::annule(void) {
  if ( d )
    *d = save;
}

///
/// Selection du CheckBox lie a la position (x,y).
///
/// @param x La coordonnee x.
/// @param y La coordonnee y.
/// @return Le booleen indiquant que la selection a bien ete geree.
/// @author Nicolas Janey
/// @author nicolas.janey@univ-fcomte.fr
/// @version 1.0, 25/02/10
///
int CheckBox::selection(int x,int y) {
  if ( enabled ) {
    int xi = px+dx;
    int xf = px+dx+12;
    int yi = py-10;
    int yf = py+2;
    if ( ( x >= xi ) && ( x <= xf ) && ( y >= yi ) && ( y <= yf ) ) {
      *d = !*d;
      return(1); } }
  return(-1);
}

///
/// Ajustement d'un ensemble de CheckBox auquel this appartient pour que seul this soit active.
///
/// @param n Le nombre de CheckBox de l'ensemble a gerer.
/// @param ctrl Le tableau des CheckBox.
/// @param v Le tableau des variables (pointeurs) gerees par les CheckBox.
/// @author Nicolas Janey
/// @author nicolas.janey@univ-fcomte.fr
/// @version 1.0, 25/02/10
///
void CheckBox::ajuste(int n,CheckBox **ctrl,int **v) {
  for ( int j = 0 ; j < n ; j++ )
    if ( this == ctrl[j] ) {
      if ( *(v[j]) ) {
        for ( int i = 0 ; i < n ; i++ )
          if ( i != j ) {
            *(v[i]) = 0; } }
        else {
        *(v[j]) = 1; }
      for ( int i = 0 ; i < n ; i++ ) {
        ctrl[i]->dessiner(); }
      return; }
}

void CheckBox::sauve(FILE *f) {
  if ( strcmp(name,"") )
    fprintf(f,"  <%s valeur=\"%d\"></%s>\n",name,*d,name);
}
