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

#include <math.h>

#include "magnifier.h"
#include "util.h"

// Parser Stuff
#undef yyFlexLexer
#define yyFlexLexer mgFlexLexer
#include <FlexLexer.h>

void* mglval;
extern int mgparse(Magnifier*, mgFlexLexer*);

int mglex(void* vval, mgFlexLexer* ll)
{
  mglval = vval;
  return ll ? ll->yylex() : 0;
}

void mgerror(Magnifier* mg, mgFlexLexer* ll, const char* m)
{
  mg->error(m);
  const char* cmd = ll ? ll->YYText() : (const char*)NULL;
  if (cmd && cmd[0] != '\n') {
    mg->error(": ");
    mg->error(cmd);
  }
}

// Public Member Functions

Magnifier::Magnifier(Tcl_Interp* i, Tk_Canvas c, Tk_Item* item) : Widget(i, c, item)
{
  thumbnail = 0;
  needsUpdate = 0;
}

int Magnifier::parse(istringstream& istr)
{
  result = TCL_OK;
  mgFlexLexer* ll = new mgFlexLexer(&istr);
  mgparse(this, ll);
  delete ll;

  return result;
}

void Magnifier::update()
{
  needsUpdate = 1;
  redrawNow();
}

// Required Virtual Functions

// UpdatePixmap. This function is responsable for creating a valid 
// pixmap the size of the current Magnifier

int Magnifier::updatePixmap(const BBox& bb)
{
  // create a valid pixmap if needed
  // bb is in canvas coords

  if (!pixmap)
    if (!(pixmap = Tk_GetPixmap(display, Tk_WindowId(tkwin),
				options->width, options->height, depth))) {
      internalError("Magnifier: Unable to Create Pixmap");
      return TCL_OK;
    }

  if (needsUpdate) {
    if (thumbnail) {
      XSetClipOrigin(display, gc, 0, 0);
      XCopyArea(display, thumbnail, pixmap, gc, 0, 0, 
		options->width, options->height, 0, 0);
    }
    else
      clearPixmap();

    needsUpdate = 0;
  }

  return TCL_OK;
}

void Magnifier::invalidPixmap()
{
  Widget::invalidPixmap();
  update();
}

// Command Functions

void Magnifier::getBBoxCmd()
{
  ostringstream str;
  str << options->width << " " << options->height << ends;
  Tcl_AppendResult(interp, str.str().c_str(), NULL);  
}

void Magnifier::updateCmd(void* p)
{
  thumbnail = (Pixmap)p;
  update();
}

