#include "plplot.h"
#include <dos.h>
#include <stdio.h>
#include <exec/types.h>
#include <exec/ports.h>
#include <graphics/display.h>
#include <intuition/intuition.h>
#include <intuition/intuitionbase.h>
#include <intuition/screens.h>

struct NewScreen NewScreen = {
   0,		/* LeftEdge Position */
   0,		/* TopEdge */
   640,		/* Width (high-res) */
   400,		/* Height (interlace) */
   1,		/* Depth (1 color) */
   0,1,		/* DetailPen and BlockPen */
   HIRES|INTERLACE,
   CUSTOMSCREEN,
   NULL,		/* Use default font */
   NULL,		/* Don't want a title */
   NULL,		/* Gadgets?? */
   NULL		/* No CustomBitMap */
};
	
struct NewWindow NewWindow = {
   0,0,640,400,
   -1,-1,
   MENUPICK|VANILLAKEY,
   SMART_REFRESH|BACKDROP|BORDERLESS|ACTIVATE,
   NULL,	/* Window Gadget Pointer */
   NULL,	/* Checkmark image */
   NULL,	/* No Title */
   NULL,	/* My Screen Pointer (this is set below) */
   NULL, /* Not a SuperBitMap */
   0,0,0,0,
   CUSTOMSCREEN
};

struct IntuiText IText[] = {
   { 0, 1, JAM1, 0, 0, NULL, "Screen to Back" },
   { 0, 1, JAM1, 0, 0, NULL, "Clear Screen" },
   { 0, 1, JAM1, 0, 0, NULL, "Close Screen" },
   { 0, 1, JAM1, 0, 0, NULL, "Interrupt  <CTRL-C>"},
   { 0, 1, JAM1, 0, 0, NULL, "Continue   <Return>"}
};

struct MenuItem MenuItem[] = {
   {
      &MenuItem[1],
      0, 0, (140 + COMMWIDTH), 9,
      ITEMTEXT | COMMSEQ | ITEMENABLED | HIGHCOMP,
      0,
      (APTR)&IText[0],
      NULL,
      'F',
      NULL,
      NULL
   },
   {
      &MenuItem[2],
      0, 9, (140 + COMMWIDTH), 9,
      ITEMTEXT | COMMSEQ | HIGHCOMP,
      0,
      (APTR)&IText[1],
      NULL,
      'C',
      NULL,
      NULL
   },
   {
      NULL,
      0, 18, (140 + COMMWIDTH), 9,
      ITEMTEXT | COMMSEQ | HIGHCOMP,
      0,
      (APTR)&IText[2],
      NULL,
      'Q',
      NULL,
      NULL
   },
   {
      &MenuItem[4],
      0, 0, (140 + COMMWIDTH), 9,
      ITEMTEXT | ITEMENABLED | HIGHCOMP,
      0,
      (APTR)&IText[3],
      NULL,
      NULL,
      NULL,
      NULL
   },
   {
      NULL,
      0, 9, (140 + COMMWIDTH), 9,
      ITEMTEXT | HIGHCOMP,
      0,
      (APTR)&IText[4],
      NULL,
      NULL,
      NULL,
      NULL
   }
};
	
struct Menu Menu[] = {
   {
      &Menu[1],
      0, 0, 140, 0,
      MENUENABLED,
      "Screen Control",
      &MenuItem[0]
   },		
   {
      NULL,
      140, 0, 140, 0,
      MENUENABLED,
      "Graphics Control",
      &MenuItem[3]
   }
};

extern long IntuitionBase;
extern long GfxBase;
static struct Screen *Screen;
static struct Window *Window;
static struct Border Border;
static short xy[4];

/* Open Borderless Full Screen Window for Drawing */
void amiini()
{
   LONG OpenLibrary(), OpenScreen(), OpenWindow();

   IntuitionBase = OpenLibrary("intuition.library",0);
   if(IntuitionBase == NULL) {
      printf("Couldn't open intuition library");
      exit(1);
   }
	
   GfxBase = OpenLibrary("graphics.library",0);
   if(GfxBase == NULL) {
      printf("Couldn't open graphics library");
      exit(1);
   }
	
   if((Screen = (struct Screen *)OpenScreen(&NewScreen)) == NULL) {
      printf("Couldn't open custom screen");
      exit(1);
   }
	
   ShowTitle(Screen,FALSE);

   NewWindow.Screen = Screen;	/* Pointer to CustomScreen */
	
   if((Window = (struct Window *)OpenWindow(&NewWindow)) == NULL) {
      printf("Couldn't open window.\n");
      exit(1);
   }
	
   Border.LeftEdge = 0;
   Border.TopEdge = 0;
   Border.FrontPen = 1;
   Border.BackPen = 0;
   Border.DrawMode = JAM1;
   Border.Count = 2;
   Border.NextBorder = NULL;
   Border.XY = xy;
	
   SetMenuStrip(Window,Menu);
}

void amitex()
{
   /* I don't have a text mode on the custom screen */
}

void amigra()
{
}

void amicol(color)
int color;
{
   /* No color support at present -- maybe in the next release */
}

/* Clear the screen */
void amiclr()
{
   SetRast(Window->RPort,0);
}		

/* Draw the line.  Also check for interrupts. */
void amilin(x1,y1,x2,y2)
int x1, y1, x2, y2;
{
   int ItemNumber, MenuNumber;
   ULONG class;
   USHORT code, qualifier;
   struct IntuiMessage *message;
   struct MenuItem *Item, *ItemAddress();
	struct Message *GetMsg();
   void beepw();

   xy[0] = x1;
   xy[1] = 399-y1;
   xy[2] = x2;
   xy[3] = 399-y2;
	
   if((xy[0] == xy[2]) && (xy[1] == xy[3]))
      WritePixel(Window->RPort,xy[0],xy[1]);
   else
      DrawBorder(Window->RPort,&Border,0,0);
	
   /* Check for user abort via CTRL-C or menu ABORT selection */
   /* and also Screen to Back Selection */
   /* All other messages are replied to but ignored. */
   while((message = (struct IntuiMessage *)
                     GetMsg(Window->UserPort))!=NULL) {
      class = message->Class;
      code = message->Code;
      qualifier = message->Qualifier;
      ReplyMsg(message);
      if(class == VANILLAKEY) {
         if(code == 3)
            beepw();
      }
      else if(class == MENUPICK) {
         while (code != MENUNULL) {
               Item = ItemAddress(Menu,code);
            MenuNumber = MENUNUM(code);
            ItemNumber = ITEMNUM(code);
            if((MenuNumber == 0) && (ItemNumber == 0))
               ScreenToBack(Screen);
            else if((MenuNumber == 1) && (ItemNumber == 0))
               beepw();
            code = Item->NextSelect;
         }
      }
   }
}

void amitid()
{
   ClearMenuStrip(Window);
   CloseWindow(Window);
   CloseScreen(Screen);
}

void beepw()
{
   int ItemNumber, MenuNumber;
   ULONG class;
   USHORT code, qualifier;
   struct IntuiMessage *message;
   struct MenuItem *Item, *ItemAddress();
	struct Message *GetMsg();

   OnMenu(Window,SHIFTMENU(0)+SHIFTITEM(1));
   OnMenu(Window,SHIFTMENU(0)+SHIFTITEM(2));
   OnMenu(Window,SHIFTMENU(1)+SHIFTITEM(1));
   OffMenu(Window,SHIFTMENU(1)+SHIFTITEM(0));
   for(;;) {
      Wait(1L << Window->UserPort->mp_SigBit);
      while((message = (struct IntuiMessage *)
                        GetMsg(Window->UserPort))!=NULL) {
         class = message->Class;
         code = message->Code;
         qualifier = message->Qualifier;
         ReplyMsg(message);
         if(class == VANILLAKEY) {
            if(code == 13) {
               OffMenu(Window,SHIFTMENU(0)+SHIFTITEM(1));
               OffMenu(Window,SHIFTMENU(0)+SHIFTITEM(2));
               OffMenu(Window,SHIFTMENU(1)+SHIFTITEM(1));
               OnMenu(Window,SHIFTMENU(1)+SHIFTITEM(0));
               return;
            }
         }
         else if(class == MENUPICK) {
            while (code != MENUNULL) {
               Item = ItemAddress(Menu,code);
               MenuNumber = MENUNUM(code);
               ItemNumber = ITEMNUM(code);
               if(MenuNumber == 0) {
                  if(ItemNumber == 0)
                     ScreenToBack(Screen);
                  else if(ItemNumber == 1)
                     SetRast(Window->RPort,0);
                  else if(ItemNumber == 2) {
                     ClearMenuStrip(Window);
                     CloseWindow(Window);
                     CloseScreen(Screen);
                     goto AllDone;
                  }
               }
               else if(MenuNumber == 1) {
                  if (ItemNumber == 1) {
                     OffMenu(Window,SHIFTMENU(0)+SHIFTITEM(1));
                     OffMenu(Window,SHIFTMENU(0)+SHIFTITEM(2));
                     OffMenu(Window,SHIFTMENU(1)+SHIFTITEM(1));
                     OnMenu(Window,SHIFTMENU(1)+SHIFTITEM(0));
                     return;
                  }
               }
               code = Item->NextSelect;
            }
         }
      }
   }

AllDone:
   plend();
   exit(1);
}


