/**************************************
*  TEST.C  08/05/90
*  Written by Timm Martin
*  This source code is public domain.
***************************************/

#include <exec/types.h>
#include <functions.h>
#include <intuition/intuition.h>
#include <intuition/intuitionbase.h>
#include "zz_pointer.h"

/********************
*  SHARED VARIABLES
*********************/

struct IntuitionBase *IntuitionBase = NULL;
struct Window        *win           = NULL;

/************************
*  NEW WINDOW STRUCTURE
*************************/

struct NewWindow new_window =
{
  /* SHORT           LeftEdge    */ 0,
  /* SHORT           TopEdge     */ 0,
  /* SHORT           Width       */ 640,
  /* SHORT           Height      */ 160,
  /* UBYTE           DetailPen   */ 0,
  /* UBYTE           BlockPen    */ 1,
  /* ULONG           IDCMPFlags  */ CLOSEWINDOW|MOUSEBUTTONS,
  /* ULONG           Flags       */ ACTIVATE|NOCAREREFRESH|SIMPLE_REFRESH|WINDOWCLOSE|WINDOWDEPTH|WINDOWDRAG|WINDOWSIZING,
  /* struct Gadget * FirstGadget */ NULL,
  /* struct Image *  CheckMark   */ NULL,
  /* UBYTE *         Title       */ (STRPTR)"Pointer Test Program -- click in the window!",
  /* struct Screen * Screen      */ NULL,
  /* struct BitMap * BitMap      */ NULL,
  /* SHORT           MinWidth    */ 230,
  /* SHORT           MinHeight   */ 20,
  /* USHORT          MaxWidth    */ 640,
  /* USHORT          MaxHeight   */ 200,
  /* USHORT          Type        */ WBENCHSCREEN,
};

/*************
*  FUNCTIONS
**************/

void main( void );
void program_end( void );
void program_begin( void );
void window_input( void );

/***********
*  M A I N
************/

void main( void )
{
  program_begin();
  window_input();
  program_end();
}

/*****************
*  PROGRAM BEGIN
******************/

/*
This procedure opens the Intuition library, the window, and the pointer.
If any of these things fail, the program ends.
*/

void program_begin( void )
{
  if (!(IntuitionBase = (struct IntuitionBase *)
                        OpenLibrary( "intuition.library", 0L )) ||
      !(win = OpenWindow( &new_window )) ||
      !zz_pointer_open())
    program_end();
}

/***************
*  PROGRAM END
****************/

/*
This procedure closes the pointer, the window, and the Intuition
library.
*/

void program_end( void )
{
  zz_pointer_close();

  if (win)           CloseWindow( win );
  if (IntuitionBase) CloseLibrary( IntuitionBase );
}

/****************
*  WINDOW INPUT
*****************/

/*
This procedure monitors for window input.  It returns when the user clicks on
the window close gadget.
*/

void window_input( void )
{
  BOOL finished;
  struct IntuiMessage *imessage;
  BOOL pointer;

  finished = pointer = FALSE;
  while (!finished)
  {
    /* wait for input from the window and the timer device */
    Wait( 1L<<win->UserPort->mp_SigBit );

    /* for each window input message */
    while (imessage = (struct IntuiMessage *)GetMsg( win->UserPort ))
    {
      switch (imessage->Class)
      {
        case CLOSEWINDOW:
          /* end the program */
          finished = TRUE;
          break;
        case MOUSEBUTTONS:
          /* if clicked down the left mouse button */
          if (imessage->Code == SELECTDOWN)
          {
            /* if the ZZ pointer is currently visible */
            if (pointer)
            {
              /* clear it */
              CLEAR_POINTER( win );
              pointer = FALSE;
            }
            else
            {
              /* else display it */
              ZZ_POINTER( win );
              pointer = TRUE;
            }
          }
          break;
      }
      /* reply to the message to free its memory */
      ReplyMsg( (struct Message *)imessage );
    }
  }
}
