#include <exec/types.h>
#include <exec/exec.h>
#include <exec/execbase.h>
#include <hardware/blit.h>
#include <graphics/copper.h>
#include <graphics/regions.h>
#include <graphics/rastport.h>
#include <graphics/gfxbase.h>
#include <graphics/gfxmacros.h>
#include <graphics/gels.h>
#include <graphics/display.h>
#include <intuition/intuition.h>

struct IntuitionBase *IntuitionBase = NULL;
struct GfxBase *GfxBase = NULL;

#define MAXX   640

struct NewScreen MyScreen =
{ 0,0,MAXX,400,4,0,1,HIRES | INTERLACE, CUSTOMSCREEN, NULL, "Graphics", 0,0,};

struct NewWindow DrawWindow = {
  0,0,MAXX,400,
  0,1,
  MENUPICK,
  BORDERLESS | BACKDROP | ACTIVATE,
  NULL,
  NULL,
  NULL,
  NULL,
  NULL,
  0,0,0,0,
  CUSTOMSCREEN,
};

struct Screen *Screen = NULL;
struct Window *Backdrop = NULL;
struct RastPort *DrawRP;
struct ViewPort *DrawVP;
struct IntuiMessage  *message;

struct MenuItem OnlyMenuItems[3];
struct IntuiText OnlyMenuText[3];
struct Menu OnlyMenu[1];

#define MAXLINES  125
#define ERASE     0

initialise()
{
  if (!(IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",0)))
    exit(1);

  if(!(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",0))) {
    cleanitup();
    exit(2);
  }

  if(!(Screen = (struct Screen *)OpenScreen(&MyScreen))) {
    cleanitup();
    exit(3);
  }

  DrawWindow.Screen = Screen;

  if(!(Backdrop = (struct Window *)OpenWindow(&DrawWindow))) {
    cleanitup();
    exit(4);
  }

  DrawRP = Backdrop->RPort;     /* Draw into backdrop window */
  DrawVP = &Screen->ViewPort;   /* Set colors in Screens VP  */

  initmenuitems();
  initmenu();
  SetMenuStrip(Backdrop, &OnlyMenu[0]);

  ShowTitle(Screen,FALSE);

  define_color( 0,  0,  0,  0);
  define_color( 1, 15, 15, 15);
  define_color( 2, 15,  0,  0);
  define_color( 3,  7,  0,  0);
  define_color( 4,  0, 15,  0);
  define_color( 5,  0,  7,  0);
  define_color( 6,  0,  0, 15);
  define_color( 7,  0,  0,  7);
  define_color( 8, 15, 15,  0);
  define_color( 9,  7,  7,  0);
  define_color(10, 15,  0, 15);
  define_color(11,  7,  0,  7);
  define_color(12,  0, 15, 15);
  define_color(13,  0,  7,  7);
  define_color(14,  7,  7,  7);
  define_color(15,  3,  3,  3);
}

terminate()
{
  ShowTitle(Screen,TRUE);
/*  while(check_user_action() != -1); */
  cleanitup();
}

cleanitup()    /* release allocated resources */
{
  if (Backdrop)
    CloseWindow(Backdrop);
  if (Screen)
    CloseScreen(Screen);
  if (GfxBase)
    CloseLibrary(GfxBase);
  if (IntuitionBase)
    CloseLibrary(IntuitionBase);
}

draw(x1, y1, x2, y2, color)
  int x1, x2;
  int y1, y2;
  BYTE color;
{
  SetAPen(DrawRP, color);
  SetDrMd(DrawRP, JAM1);
  Move(DrawRP, x1, y1);
  Draw(DrawRP, x2, y2);
}

define_color(color, r, g, b)
  int color, r, g, b;
{
  SetRGB4(DrawVP, color, r, g, b);
}

initmenuitems()
{
  short n;

  for(n = 0; n < 3; n++) {  /* One struct for each item */
    OnlyMenuItems[n].NextItem = &OnlyMenuItems[n + 1]; /* next item */
    OnlyMenuItems[n].LeftEdge = 0;
    OnlyMenuItems[n].TopEdge = 10 * n;
    OnlyMenuItems[n].Width = 112;
    OnlyMenuItems[n].Height = 10;
    OnlyMenuItems[n].Flags = ITEMTEXT | ITEMENABLED | HIGHCOMP;
    OnlyMenuItems[n].MutualExclude = 0;
    OnlyMenuItems[n].ItemFill = (APTR)&OnlyMenuText[n];
    OnlyMenuItems[n].SelectFill = NULL;
    OnlyMenuItems[n].Command = 0;
    OnlyMenuItems[n].SubItem = NULL;
    OnlyMenuItems[n].NextSelect = 0;

    OnlyMenuText[n].FrontPen = 0;
    OnlyMenuText[n].BackPen = 1;
    OnlyMenuText[n].DrawMode = JAM2;
    OnlyMenuText[n].LeftEdge = 0;
    OnlyMenuText[n].TopEdge = 1;
    OnlyMenuText[n].ITextFont = NULL;
    OnlyMenuText[n].NextText = NULL;
  }
  OnlyMenuItems[2].NextItem = NULL; /* Last item */

  OnlyMenuText[0].IText = (UBYTE *)"Hide Title Bar";
  OnlyMenuText[1].IText = (UBYTE *)"Show Title Bar";
  OnlyMenuText[2].IText = (UBYTE *)"QUIT!";
}

initmenu()
{
  OnlyMenu[0].NextMenu = NULL;                 /* No more menus */
  OnlyMenu[0].LeftEdge = 0;
  OnlyMenu[0].TopEdge = 0;
  OnlyMenu[0].Width = 85;
  OnlyMenu[0].Height = 10;
  OnlyMenu[0].Flags = MENUENABLED;             /* All items selectable */
  OnlyMenu[0].MenuName = "Actions";
  OnlyMenu[0].FirstItem = &OnlyMenuItems[0];   /* Pointer to first item */
}

check_user_action()
{
  ULONG class;
  USHORT code, ItemNum;
  int action;

  action = 0;

  while(message = (struct IntuiMessage *)GetMsg(Backdrop->UserPort)) {
    class = message->Class;
    code = message->Code;
    ReplyMsg(message);

    if (class == MENUPICK && code != MENUNULL) {
      ItemNum = ITEMNUM( code );
      switch (ItemNum) {
        case 0:
          ShowTitle(Screen, FALSE);
          break;
        case 1:
          ShowTitle(Screen, TRUE);
          break;
        case 2:
/*          ClearMenuStrip(Backdrop); */
          action = -1;
          return(action);
      }
    }
  }
  return (action);
}

put_screen()
{
/*  ScreenToBack(Screen); */
  ShowTitle(Screen,TRUE);
}

get_screen()
{
  ScreenToFront(Screen);
  ShowTitle(Screen,TRUE);
}
