/*************** Example G1 ******************************************/
#include "EZ.h"

static void eventHandler(EZ_Widget *, void *, int, XEvent *);
static void draw(EZ_Widget *);

static EZ_GLImage *testImage;
int                imageW, imageH;

main(int argc, char **argv)
{
  EZ_Widget *frame, *canvas;
  /* 
   * Initialize EZWGL, don't forget to 
   * initialize the graphics library also.
   */
  EZ_Initialize(argc, argv, 1);      /* notice the 1        */
  /*
   * canvases do not resize propertly as toplevel
   * widgets (bug!) It is recommended to put it
   * under a frame at least for now.
   */
  frame = EZ_CreateWidget(EZ_WIDGET_FRAME, NULL, 
                         EZ_FILL_MODE, EZ_FILL_BOTH,
                         0);
  canvas = EZ_CreateWidget(EZ_WIDGET_3D_CANVAS, frame,
                           EZ_WIDTH_HINT,  320, 
                           EZ_HEIGHT_HINT, 320,
			   EZ_BORDER_WIDTH, 10,
			   EZ_BORDER_TYPE, EZ_BORDER_SUNKEN,
                           EZ_EVENT_HANDLE, eventHandler, NULL,
                           0);
  /*
   * now display the canvas. One must first display
   * a 3DCanvas before calling any GL functions !!!
   */
  EZ_DisplayWidget(canvas);         
  {
    EZ_Pixmap  *testPixmap;
    testPixmap = EZ_CreateLabelPixmapFromXpmFile("twotails.xpm");
    testImage = EZ_CreateGLImage(testPixmap);
    EZ_FreeLabelPixmap(testPixmap);
    EZ_GetGLImageInfo(testImage, NULL, NULL, &imageW, &imageH);
  }
  
  /* now setup global GL attributes */
  EZ_RGBMode();                 /* we are using RGB mode  */
  EZ_ShadeModel(EZ_SMOOTH);     /* turn on smooth shading */
  EZ_AutoSelectBackBuffer();    /* select a back buffer   */
  EZ_DrawBuffer(EZ_BACK);/* always draw into the back buf */
  /*
   * We'll draw a 2D polygon, so we don't have to setup
   * a complex projection matrix. The default matrix mode
   * is EZ_MODELVIEW. We just set the projection matrix
   * on this default matrix stack.
   */
  EZ_LoadIdentity();            /* clear the top mtx 2 id */     
  EZ_Ortho2(-1.0,1.0,-1.0,1.0); /* project the unit cube  */
  
  draw(canvas);                 /* draw the polygon       */
  EZ_EventMainLoop();           /* handle events          */
}

static void draw(EZ_Widget *canvas)
{
  EZ_Clear(EZ_COLOR_BUFFER_BIT); /* clear the frame buffer */

  EZ_Begin(EZ_POLYGON);          /* start a polygon        */
  EZ_Color3f(1.0,0.0,0.0);       /* each vertex has its clr*/
  EZ_Vertex2f(1.0,0.0);
  EZ_Color3f(1.0,1.0,0.0);
  EZ_Vertex2f(0.5,0.866);
  EZ_Color3f(0.0,1.0,0.0);
  EZ_Vertex2f(-0.5,0.866);
  EZ_Color3f(0.0,1.0,1.0);
  EZ_Vertex2f(-1.0,0.0);
  EZ_Color3f(0.0,0.0,1.0);
  EZ_Vertex2f(-0.5,-0.866);
  EZ_Color3f(1.0,0.0,1.0);
  EZ_Vertex2f(0.5,-0.866);
  EZ_End();                     /* the end of the polygon */
}
/* 
 * the event handler. It should handle at least the
 * EZ_REDRAW event and the EZ_RESIZE.
 */
static void eventHandler(EZ_Widget *canvas, void *data,
                         int eventType, XEvent *xevent)
{
  switch(eventType) 
    {
    case EZ_REDRAW:
    case EZ_RESIZE:
      draw(canvas);
      EZ_SwapBuffers();
      break;
    case EZ_KEY_PRESS:
      if(EZ_PressedKey == EZ_ESCAPE_KEY) exit(0);
      break;
    case EZ_BUTTON1_PRESS:
      {
	XEvent xevent;
	draw(canvas);
	EZ_PutGLImageXWindow(testImage, 0, 0, 304,304,
			    EZ_PointerCoordinates[0] - imageW/2,
			    EZ_PointerCoordinates[1] - imageH/2);
	EZ_SwapBuffers();
	while(1)
	  {
	    int device = EZ_GLNextEvent(canvas, &xevent);	    
	    if(device == EZ_POINTER_MOTION)
	      {
		draw(canvas);
		EZ_PutGLImageXWindow(testImage, 0, 0, 304,304,
				     EZ_PointerCoordinates[0] - imageW/2,
				     EZ_PointerCoordinates[1] - imageH/2);
		EZ_SwapBuffers();
	      }
	    else if(device == EZ_BUTTON1_RELEASE)
	      break;
	  }
      }
    break;
    default:
      break;
    }
}
/*************** Example G1 ******************************************/  
