#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 "Button.h"
#include "Couleur.h"
#include "ModuleFont.h"

Button::Button(void):Controle() {
  coulTexte = new Couleur();
  dx = 0;
  strcpy(titre,"");
}

Button::Button(int px,int py,int dx,Couleur *coul,Couleur *coulT,const char *titre,void (*finActivation)(void)):Controle(px,py,coul) {
  this->coulTexte = new Couleur(coulT);
  this->dx = dx;
  strcpy(this->titre,titre);
  this->finActivation = finActivation;
}

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

void Button::dessiner(void) {
  if ( !enabled )
    actif = 0;
  glDisable(GL_LIGHTING);
  glPushMatrix();
  glTranslatef(px,py,0.0F);
  setGLColor((float *) coul);
  { glPushMatrix();
    glBegin(GL_LINES);
    glVertex2f(0.0F,-15.0F);
    glVertex2f(0.0F,7.0F);
    glVertex2f(0.0F,-15.0F);
    glVertex2f(dx,-15.0F);
    glVertex2f(dx,-15.0F);
    glVertex2f(dx,7.0F);
    glVertex2f(0.0F,7.0F);
    glVertex2f(dx,7.0F);
    glEnd();
    glPopMatrix(); }
  if ( actif ) {
    setGLColor((float *) coul);
    glPushMatrix();
    glBegin(GL_QUADS);
    glVertex2f(1.0F,6.0F);
    glVertex2f(1.0F,-13.0F);
    glVertex2f(dx-2.0F,-13.0F);
    glVertex2f(dx-2.0F,6.0F);
    glEnd();
    glPopMatrix();
    actif = 0;
    glutPostRedisplay(); }
  setGLColor((float *) coulTexte);
  { setFont(GLUT_BITMAP_8_BY_13,CENTER);
    glPushMatrix();
    bitmapStringOutput(dx/2,0.0F,0.0F,titre);
    glPopMatrix(); }
  glPopMatrix();
}

void Button::sauve(void) {
}

void Button::annule(void) {
}

int Button::selection(int x,int y) {
  if ( enabled ) {
    int xi = px;
    int xf = px+dx;
    int yi = py-15;
    int yf = py+7;
    if ( ( x >= xi ) && ( x <= xf ) && ( y >= yi ) && ( y <= yf ) ) {
      actif = !actif;
      return(1); } }
  return(-1);
}
