/* text_in.c  for inputting text in OpenGL       */
/*            see demo_test_in.c for example use */

#include <stdio.h>
#include <string.h>
#include <GL/glut.h>

static int getting_text = 0;
static char * the_text;
static void (*text_entered)();

void clear_text(char msg[])
{
  msg[0]='\0';
}

void get_text(char msg[], void (*call_with_text)())
{
  getting_text = 1;
  the_text = msg;
  text_entered = call_with_text;
}

int add_text(unsigned char key)
{
  char msg[] = "x";
  int len;
  
  if(!getting_text) return 0;
  if(key==8) /* backspace */
  {
    len = strlen(the_text);
    the_text[len-1] = '\0';
  }
  else if(key==13 || key==9) /* cr or tab ends */
  {
    getting_text = 0;
    text_entered(the_text);
  }
  else
  {
    msg[0] = key;
    strcat(the_text, msg);
  }
  glutPostRedisplay();
  return 1;
}

void stop_text(char msg[])
{
  getting_text = 0;
}

void show_text(GLfloat x, GLfloat y, char msg[])
{
  int len, i;

  glPushMatrix();
    glRasterPos2f(x, y);
    len = strlen(msg);
    for (i = 0; i<len; i++)
      glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, msg[i]);
  glPopMatrix();
} /* end show_text */

/* end text_in.c */
