/***************************************************************
 Author: Joshua New (www.cs.utk.edu/~new)
 Affiliation: Seelab, The University of Tennessee at Knoxville
 Lab Director: Dr. Jian Huang
 Description: Direct manipulation of numbered ranges inspired
              by Nate's OpenGL Tutorials API
              http://www.xmission.com/~nate/opengl.html
 Contact: If you experience any platform-dependent modifications,
          please notify me so the original source code can also
		  be updated: new@cs.utk.edu
****************************************************************/

#ifndef _JEDIT_H
#define _JEDIT_H

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

#if defined(__APPLE__) || defined(MACOSX)
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

#define MAX_QUERY_LENGTH 2048
#define MAX_QUERIES 32
#define INVALID -1
#define CLOSED   0
#define OPEN     1
#define INUSE    2

#define H_OFFSET 5
#define V_OFFSET 3

//Adjust for font/format changes here
//<CF> - Change to encapsulate cell ('b' key)
// Need to adjust all <CF> to use:
//     glutBitmapLength(void *font, char *string); 
/*#define CELL_TOP    (cell->y - 4*font_size/5)
#define CELL_BOTTOM (cell->y + font_size/5)
#define CELL_LEFT   (cell->x)
#define CELL_RIGHT  (cell->x + 2*font_size)
*/

#define DELIMITER "%se"

#define SE_BACKGROUND -2
#define SE_TEXT       -3
#define SE_INFO       -4
#define SE_BBOX       -5

//////////////////////
//  Global structs  //
//////////////////////

//
// Main manipulator
//
typedef struct _cell {
    int id;
    int x, y;
    float min, max;
    float* value;
    float step;
    char* info;
    char* format;
	int top;
	int bottom;
	int left;
	int right;
} cell;

//
// Main query holder
//
typedef struct _seq {
	int mode;
	float color[3];
	int numVars;
	float* vars;
	cell* cells;
	char* queryText;
} seq;


#ifdef __cplusplus
extern "C" {
#endif

	//
	//Main Functions
	//

	//Must be called when initializing OpenGL
	int seinit(const int winID,
			   const int x, const int y,
			   const int width, const int height);

	//Use this to add query lines to the viewport
	int seadd(char* str, int numVars, float* varArray);

	//Must be placed at the beginning of the display callback
	void sedraw();

	//Use this to perform some action when a value changes
	void registerCallback(int (* seQueryChange)());

	//Editing functions:
	//Set the location(s) of the interactive variable(s)
	int seloc(int queryID, int variableID, int x, int y);
   int selocspace(int queryID, int variableID,
               const char* str, const char* dFormat, float maxVal);
	int selocguess(int queryID, int variableID,
					const char* str, const char* dFormat, float maxVal);
	//Set the minimum and maximum range of the variable(s)
	int seset(int queryID, int variableID,
		  float min, float max, float step, const char* format);
	int sesetall(int queryID,
		  float min, float max, float step, const char* format);
	//Use this to add info to a variable of a query
	int seinfo(int queryID, int variableID, const char* str);
	//Set the minimum and maximum range of the variable(s)
	int seminmax(int queryID, int variableID, float min, float max);
	int seminmaxall(int queryID, float min, float max);
	//Set the step of the variable(s)
	int sestep(int queryID, int variableID, float step);
	int sestepall(int queryID, float step);
	//Set the format for the displayed number(s)
	int seformat(int queryID, int variableID, const char* format);
	int seformatall(int queryID, const char* format);
	int seformatdefault(const char* format);
	//Set the color of a query
	int secolor(int queryID, float r, float g, float b);

	//
	//Utility functions
	//
	//<CR> - changing the font will necessitate more changes in the API
	void sesetfont(char* name, int size);
	//Draw a string at location x,y
	void sedrawstr(GLuint x, GLuint y, char* format, ...);
	//Get a new string by finding and replacing substrings of a string
   char* sesubstr(const char* search, const char* replace,const char* str);
   //Build the query string drawn behind the active cells
	char* sebuildquery(const char* str, const char* dFormat, float maxVal);

#ifdef __cplusplus
}
#endif
#endif //_JEDIT_H
