// Copyright (C) 1999-2015
// Smithsonian Astrophysical Observatory, Cambridge, MA, USA
// For conditions of distribution and use, see copyright notice in "copyright"

#include <tcl.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>

#include "magnifiertrue.h"
#include "util.h"

// Tk Canvas Widget Function Declarations

int MagnifierTrueColorCreateProc(Tcl_Interp*, Tk_Canvas, Tk_Item*, int,
				 Tcl_Obj *const []);

// MagnifierTrueColor Specs

static Tk_CustomOption tagsOption = {
  Tk_CanvasTagsParseProc, Tk_CanvasTagsPrintProc, NULL
};

static Tk_ConfigSpec magnifierTrueColorSpecs[] = {

  {TK_CONFIG_STRING, (char*)"-command", NULL, NULL, "magnifier",
   Tk_Offset(WidgetOptions, cmdName), TK_CONFIG_OPTION_SPECIFIED, NULL},
  {TK_CONFIG_INT, (char*)"-x", NULL, NULL, "1",
   Tk_Offset(WidgetOptions, x), TK_CONFIG_OPTION_SPECIFIED, NULL},
  {TK_CONFIG_INT, (char*)"-y", NULL, NULL, "1",
   Tk_Offset(WidgetOptions, y), TK_CONFIG_OPTION_SPECIFIED, NULL},
  {TK_CONFIG_INT, (char*)"-width", NULL, NULL, "256",
   Tk_Offset(WidgetOptions, width), TK_CONFIG_OPTION_SPECIFIED, NULL},
  {TK_CONFIG_INT, (char*)"-height", NULL, NULL, "256",
   Tk_Offset(WidgetOptions, height), TK_CONFIG_OPTION_SPECIFIED, NULL},
  {TK_CONFIG_ANCHOR, (char*)"-anchor", NULL, NULL, "nw",
   Tk_Offset(WidgetOptions, anchor), 0, NULL},
  {TK_CONFIG_CUSTOM, (char*)"-tags", NULL, NULL, NULL,
   0, TK_CONFIG_NULL_OK, &tagsOption},

  {TK_CONFIG_STRING, (char*)"-helvetica", NULL, NULL, "helvetica",
   Tk_Offset(WidgetOptions, helvetica), 0, NULL},
  {TK_CONFIG_STRING, (char*)"-courier", NULL, NULL, "courier",
   Tk_Offset(WidgetOptions, courier), 0, NULL},
  {TK_CONFIG_STRING, (char*)"-times", NULL, NULL, "times",
   Tk_Offset(WidgetOptions, times), 0, NULL},

  {TK_CONFIG_END, NULL, NULL, NULL, NULL, 0, 0, NULL},
};

// Tk Static Structure

static Tk_ItemType magnifierTrueColorType = {
  (char*)"magnifiertruecolor",         // name
  sizeof(WidgetOptions),        // size
  MagnifierTrueColorCreateProc, // configProc
  magnifierTrueColorSpecs,      // configSpecs
  WidgetConfigProc,             // configProc
  WidgetCoordProc,              // coordProc
  WidgetDeleteProc,             // deleteProc
  WidgetDisplayProc,            // displayProc
  0,                            // alwaysRedraw
  WidgetPointProc,              // pointProc
  WidgetAreaProc,               // areaProc
  WidgetPostscriptProc,         // postscriptProc
  WidgetScaleProc,              // scaleProc
  WidgetTranslateProc,          // translateProc
  (Tk_ItemIndexProc*)NULL,      // indexProc
  (Tk_ItemCursorProc*)NULL,     // icursorProc
  (Tk_ItemSelectionProc*)NULL,  // selectionProc
  (Tk_ItemInsertProc*)NULL,     // insertProc
  (Tk_ItemDCharsProc*)NULL,     // dCharsProc
  (Tk_ItemType*)NULL            // nextPtr
};

// Non-Member Functions

int MagnifierTrueColor_Init(Tcl_Interp* interp)
{
  Tk_CreateItemType(&magnifierTrueColorType);
  return TCL_OK;
}

int MagnifierTrueColorCreateProc(Tcl_Interp* interp, Tk_Canvas canvas, 
				 Tk_Item* item, int argc, Tcl_Obj *const argv[])
{
  MagnifierTrueColor* magnifier = new MagnifierTrueColor(interp, canvas, item);

  // and set default configuration

  if (magnifier->configure(argc, (const char**)argv, 0) != TCL_OK) {
    delete magnifier;
    Tcl_AppendResult(interp, " error occured while creating magnifier.", NULL);
    return TCL_ERROR;
  }

  return TCL_OK;
}

MagnifierTrueColor::MagnifierTrueColor(Tcl_Interp* i, Tk_Canvas c, 
				       Tk_Item* item) : Magnifier(i, c, item)
{
  configSpecs = magnifierTrueColorSpecs;  // magnifier configure options
}

void MagnifierTrueColor::clearPixmap()
{
  XImage* xmap = XGetImage(display, pixmap, 0, 0, options->width, 
			   options->height, AllPlanes, ZPixmap);
  if (!xmap) {
    internalError("MagnifierTrueColor: Unable to Create XImage");
    return;
  }

  memset(xmap->data, 255, xmap->bytes_per_line * xmap->height);

  XPutImage(display, pixmap, gc, xmap, 0, 0, 0, 0, options->width, 
	    options->height);
  XDestroyImage(xmap);
}
