/** \file
 * \brief IupBackgroundBox control
 *
 * See Copyright Notice in "iup.h"
 */

#include <stdio.h>
#include <string.h>

#include "iup.h"
#include "iupgl.h"

#include "iup_object.h"
#include "iup_register.h"
#include "iup_stdcontrols.h"


void iupGlCanvasInitClass(Iclass* ic);

static Iclass* iGlCanvasNewClass(void)
{
  Iclass* ic = iupClassNew(iupRegisterFindClass("canvas"));

  ic->name = "glcanvas";
  ic->cons = "GLCanvas";
  ic->format = NULL; /* no parameters */
  ic->nativetype = IUP_TYPECANVAS;
  ic->childtype = IUP_CHILDNONE;
  ic->is_interactive = 1;

  ic->New = iGlCanvasNewClass;

  iupClassRegisterCallback(ic, "SWAPBUFFERS_CB", "");

  iupClassRegisterAttribute(ic, "BUFFER", NULL, NULL, IUPAF_SAMEASSYSTEM, "SINGLE", IUPAF_DEFAULT);
  iupClassRegisterAttribute(ic, "COLOR", NULL, NULL, IUPAF_SAMEASSYSTEM, "RGBA", IUPAF_DEFAULT);
  iupClassRegisterAttribute(ic, "ERROR", NULL, NULL, NULL, NULL, IUPAF_READONLY | IUPAF_NO_INHERIT);

  iupClassRegisterAttribute(ic, "CONTEXT", NULL, NULL, NULL, NULL, IUPAF_READONLY | IUPAF_NO_STRING);
  iupClassRegisterAttribute(ic, "COLORMAP", NULL, NULL, NULL, NULL, IUPAF_READONLY | IUPAF_NO_STRING);

  iupClassRegisterAttribute(ic, "CONTEXTFLAGS", NULL, NULL, NULL, NULL, IUPAF_NO_INHERIT);
  iupClassRegisterAttribute(ic, "CONTEXTPROFILE", NULL, NULL, NULL, NULL, IUPAF_NO_INHERIT);
  iupClassRegisterAttribute(ic, "CONTEXTVERSION", NULL, NULL, NULL, NULL, IUPAF_NO_INHERIT);
  iupClassRegisterAttribute(ic, "ARBCONTEXT", NULL, NULL, NULL, NULL, IUPAF_NO_INHERIT);

  iupGlCanvasInitClass(ic);

  return ic;
}

static Iclass* iGLBackgroundBoxNewClass(void)
{
  Iclass* ic = iupBackgroundBoxNewBaseClass(iupRegisterFindClass("glcanvas"));

  ic->name = "glbackgroundbox";
  ic->cons = "GLBackgroundBox";

  return ic;
}

IUPGL_API Ihandle* IupGLBackgroundBox(Ihandle* child)
{
  void *children[2];
  children[0] = (void*)child;
  children[1] = NULL;
  return IupCreatev("glbackgroundbox", children);
}

IUPGL_API Ihandle* IupGLCanvas(void)
{
  return IupCreate("glcanvas");
}

IUPGL_API void IupGLCanvasOpen(void)
{
  if (!IupIsOpened())
    return;

  if (!IupGetGlobal("_IUP_GLCANVAS_OPEN"))
  {
    iupRegisterClass(iGlCanvasNewClass());
    iupRegisterClass(iGLBackgroundBoxNewClass());

    IupSetGlobal("_IUP_GLCANVAS_OPEN", "1");
  }
}
