#include <FL/Fl.H>
#include <FL/gl.h>
#include <FL/Fl_Gl_Window.H>
#include <FL/Fl_Choice.H>
#include <FL/Fl_Button.H>

#include "glut.h"
#include <stdio.h>


class openGL_window : public Fl_Gl_Window { // Create a OpenGL class in FLTK 
  void draw();            // Draw function. 
  void draw_overlay();    // Draw overlay function. 

  static void Timer_CB(void *userdata) {
        openGL_window *pb = (openGL_window*)userdata;
        pb->redraw();
        Fl::repeat_timeout(1.0/24.0, Timer_CB, userdata);
    }

public:
	openGL_window(int x,int y,int w,int h,const char *l=0);  // Class constructor 
	int frame;

};

openGL_window::openGL_window(int x,int y,int w,int h,const char *l) :
Fl_Gl_Window(x,y,w,h,l) 
{
	mode( FL_RGB|FL_ALPHA|FL_DOUBLE | FL_STENCIL );
	Fl::add_timeout(1.0/24.0, Timer_CB, (void*)this);
	frame = 0;
}


void openGL_window::draw() {
// the valid() property may be used to avoid reinitializing your
// GL transformation for each redraw:
    if (!valid()) {
      valid(1);
      glLoadIdentity();
      glViewport(0,0,w(),h());
    }

    // draw an amazing but slow graphic:--------------
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glBegin(GL_QUADS);
	glColor3f(1, 0, 0);glVertex2f(-1, -1);
	glColor3f(1, 1, 0);glVertex2f(-1,  1);
	glColor3f(1, 0, 1);glVertex2f( 1,  1);
	glColor3f(1, 1, 1);glVertex2f( 1, -1);
	glEnd();
	//--------------------------------------------------
	++frame;
}

void openGL_window::draw_overlay() {
// the valid() property may be used to avoid reinitializing your
// GL transformation for each redraw:
	if (
		!valid()) {valid(1);
		glLoadIdentity();
		glViewport(0,0,w(),h());
	  }
    // draw an amazing graphic:-------------

	//---------------------------------------
}

//-------Button callback funtion
void button1_cb(Fl_Widget *, void *){
	printf("Button1\n");
}
//


//--------Setting FL Choice function here
Fl_Choice *choice1=(Fl_Choice *)0;  
Fl_Menu_Item menu_choice1[] = {  // Create choice menu
 {"choice1 A", 0,  0, 0, 0, FL_NORMAL_LABEL, 0, 14, 0},
 {"choice1 B", 0,  0, 0, 0, FL_NORMAL_LABEL, 0, 14, 0},
 {0,0,0,0,0,0,0,0,0}
};

void choice1_cb(Fl_Widget *o, void *p)   // choice callback function
{
	int temp=int(((Fl_Choice*)o)->value());
	openGL_window *gl_win = (openGL_window *)p;
	gl_win->redraw();
	gl_win->redraw_overlay();
	switch(temp)
	{
	case 0:
		printf("choice1 A\n");
		break;
	case 1:
		printf("choice1 B\n");
		break;
	default:
		printf("Other\n");
		break;
	}
}


Fl_Choice *choice2=(Fl_Choice *)0;
Fl_Menu_Item menu_choice2[] = {      // Create choice menu
 {"choice2 X", 0,  0, 0, 0, FL_NORMAL_LABEL, 0, 14, 0},
 {"choice2 Y", 0,  0, 0, 0, FL_NORMAL_LABEL, 0, 14, 0},
 {"choice2 Z", 0,  0, 0, 0, FL_NORMAL_LABEL, 0, 14, 0},
 {0,0,0,0,0,0,0,0,0}
};

void choice2_cb(Fl_Widget *o, void *p)    // choice callback function
{
	int temp=int(((Fl_Choice*)o)->value());
	openGL_window *gl_win = (openGL_window *)p;
	gl_win->redraw();
	gl_win->redraw_overlay();
	switch(temp)
	{
	case 0:
		printf("choice2 X\n");
		break;
	case 1:
		printf("choice2 Y\n");
		break;
	case 2:
		printf("choice2 Z\n");
		break;
	default:
		printf("Other\n");
		break;
	}
}
//----------------------------------------------


//  main function
int main(int argc, char **argv) {

  Fl_Window window(640,480);  // Create a FLTK window. Resolution 600*400. 

  openGL_window gl_win(10, 10, 600 ,400);   // Add openGL window in position (10,10).Resolution 600*400
  window.resizable(&gl_win);

  Fl_Widget *obj;
  obj = new Fl_Button(10,420,100,20,"Button1");
  obj->callback(button1_cb);

  choice1 = new Fl_Choice(200, 420 , 100 , 20);    // Add Choice ui in position (200,420). width=100 height=20  
  choice1->down_box(FL_BORDER_BOX);
  choice1->menu(menu_choice1);
  choice1->callback(choice1_cb,&gl_win);

  choice2 = new Fl_Choice(200, 450 , 100 , 20);    // Add Choice ui in position (200,450). width=100 height=20  
  choice2->down_box(FL_BORDER_BOX);
  choice2->menu(menu_choice2);
  choice2->callback(choice2_cb,&gl_win);

  window.end();                  // End of FLTK windows setting. 
  window.show(argc,argv);        // Show the FLTK window
  
  gl_win.show();                 // Show the openGL window
  
  gl_win.redraw_overlay();       // redraw 
  return Fl::run();
}