/*
 * Graphics Window interface to MetaFont for Amiga
 */

#define	EXTERN	extern
#include "mfd.h"


#include <proto/intuition.h>
#include <proto/graphics.h>
#include <proto/exec.h>


struct Screen *Scr;
struct Window *Win;



void close_all(char *reason)
{
  printf("%s\n", reason);

  if (Win)           CloseWindow(Win);
  if (GfxBase)       CloseLibrary((struct Library *)GfxBase);
  if (IntuitionBase) CloseLibrary((struct Library *)IntuitionBase);

} /* close_all() */



boolean mf_ami_initscreen()
{
  static struct TagItem win_tags[] = {
    {WA_Width,     500},  /* default in plain.mf */
    {WA_Height,    400},  /* default in plain.mf */
    {WA_Flags,     WFLG_SIZEGADGET|WFLG_DRAGBAR|WFLG_DEPTHGADGET},
    {WA_Title,     (UBYTE *)"MetaFont"},
    {WA_MinWidth,  80},
    {WA_MinHeight, 20},
    {WA_MaxWidth,  -1},
    {WA_MaxHeight, -1},
    {WA_PubScreen,  0},
    {TAG_END }};

#if DEBUG
  printf("initscreen()\n");
#endif

  if (NOT(IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",0)))
  {
    close_all("can't open intuition library\n");
    return(FALSE);
  }

  if (NOT(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 0)))
  {
    close_all("can't open intuition library\n");
    return(FALSE);
  }

  if (NOT(Scr = LockPubScreen(0)))
  {
    close_all("can't get public screen\n");
    return(FALSE);
  }

  win_tags[8].ti_Data = (ULONG)Scr;

  if (NOT(Win = (struct Window *)OpenWindowTagList(0, win_tags)))
  {
    close_all("can't open window\n");
    return(FALSE);
  }

  UnlockPubScreen(0, Scr);

#if DEBUG
   printf("initscreen() ok\n");
#endif

   return(TRUE);

} /* mf_ami_initscreen() */



void mf_ami_updatescreen()
{

} /* mf_ami_update_screen() */



/*
 * blankrectangle: reset rectangle bounded by ([left,right],[top,bottom])
 *			to background color
 */

void mf_ami_blankrectangle(screencol left, screencol right, screenrow top, screenrow bottom)
{
#if DEBUG
  printf("blankrectangle() - left: %d right: %d top: %d bottom: %d\n", left, right, top, bottom);
#endif

  SetAPen (Win->RPort, 0);
  RectFill(Win->RPort, (SHORT)(Win->BorderLeft), (SHORT)(Win->BorderTop),
                       (SHORT)(Win->Width - Win->BorderRight - 1),
                       (SHORT)(Win->Height - Win->BorderLeft - 1));

} /* mf_ami_blankrectangle() */



/*
 * paintrow -- paint "row" starting with color "init_color",  up to next
 *		transition specified by "transition_vector", switch colors,
 *		and continue for "vector_size" transitions.
 */

void mf_ami_paintrow(screenrow row, pixelcolor init_color, 
                     transspec transition_vector, screencol vector_size)
{
  int col, color;

  color = (0 == init_color) ? 0 : 1;
  do
  {
    col = *transition_vector++;
    SetAPen (Win->RPort, color);
    Move (Win->RPort, col + Win->BorderLeft, row + Win->BorderTop);
    Draw (Win->RPort, (*transition_vector) - 1 + Win->BorderLeft, row + Win->BorderTop);

 #if DEBUG
    printf("move col: %d row: %d ", col, row);
    printf("draw col: %d row: %d\n", (*transition_vector)-1, row);
#endif

    color = 1 - color;
  }
  while (--vector_size);

} /* mf_ami_paintrow() */
