/*
 * MandelVroom 2.0
 *
 * (c) Copyright 1987,1989  Kevin L. Clague, San Jose, CA
 *
 * All rights reserved.
 *
 * Permission is hereby granted to distribute this program's source
 * executable, and documentation for non-comercial purposes, so long as the
 * copyright notices are not removed from the sources, executable or
 * documentation.  This program may not be distributed for a profit without
 * the express written consent of the author Kevin L. Clague.
 *
 * This program is not in the public domain.
 *
 * Fred Fish is expressly granted permission to distribute this program's
 * source and executable as part of the "Fred Fish freely redistributable
 * Amiga software library."
 *
 * Permission is expressly granted for this program and it's source to be
 * distributed as part of the Amicus Amiga software disks, and the
 * First Amiga User Group's Hot Mix disks.
 *
 * contents: this file implements a requester like window to get an integer
 * value from the user.
 */

#include "mandp.h"

struct Window *GetIWind;

#define GIWIDTH  190
#define GIHEIGHT 50

static
struct NewWindow NewGetI = {
   (320-GIWIDTH)/2,(200-GIHEIGHT)/2, /* start position           */
   GIWIDTH,GIHEIGHT,                 /* width, height            */
   (UBYTE) 0, (UBYTE) 1,     /* detail pen, block pen    */
   NULL,                     /* IDCMP flags */
   SMART_REFRESH,            /* MandWind flags */
   (struct Gadget *) NULL,   /* first gadget             */
   (struct Image *) NULL,    /* user checkmark           */
   (UBYTE *) NULL,           /* window title             */
   (struct Screen *) NULL,   /* pointer to screen        */
   (struct BitMap *) NULL,   /* pointer to superbitmap   */
   80,80,80,80,              /* sizing                   */
   CUSTOMSCREEN              /* type of screen           */
};
#define GETINOTDONE 0
#define GETIDONEOK  1
#define GETICANCEL  2

UBYTE IDone;

char *IString;

/*
 * Allocate all the gadgets and things for the get int window
 */
struct Gadget *MakeGetInt( Title, IVal )
  char *Title;
  LONG IVal;
{

  register struct Gadget *NextGadget;
  register SHORT *Height;
  register struct StringInfo *StringInfo;
  register struct Gadget *FirstGadget, *OldGadget;

  char string[80];

  extern struct Gadget *MakeBool();
  extern struct Gadget *MakeString();
  extern struct IntuiText *MakeIntui();

  LONG i;

#define GETICANID  0xffff
#define GETIOKID   0xfffe
#define GETISTRID  0xfffd

  FirstGadget = MakeBool(127, 32, 54, 12, 1, GETICANID, NULL);
  if (FirstGadget == NULL) {
    goto error;
  }
  FirstGadget->GadgetText = ShadowIntui("Cancel", 4, 3);

  if ( FirstGadget->GadgetText == NULL ) goto error;

  NextGadget = OldGadget = MakeBool(6, 32, 54, 12, 1, GETIOKID, NULL);

  if (NextGadget == NULL) goto error;

  NextGadget->GadgetText = ShadowIntui("  OK", 4, 3);

  if ( NextGadget->GadgetText == NULL ) goto error;

  FirstGadget->NextGadget = NextGadget;

  sprintf( string,"%d",IVal );
  NextGadget = MakeString( 55, 16, 10, GETISTRID, string );

  if (NextGadget == NULL) goto error;

  NextGadget->GadgetText = ShadowIntui(Title,-10,-11);

  if ( NextGadget->GadgetText == NULL ) goto error;

/*i = IntuiTextLength(NextGadget->GadgetText);
  NextGadget->GadgetText->LeftEdge = (GIWIDTH-i)/2;  */

  OldGadget->NextGadget = NextGadget;

  StringInfo = (struct StringInfo *) NextGadget->SpecialInfo;
  IString = (char *) StringInfo->Buffer;

  return( FirstGadget );

error:
  FreeGadgets(FirstGadget);
  return((struct Gadget *) NULL);

} /* MakeGetInt */

static struct Gadget *GetGadgets;

/*
 * Get an Integr from the user
 */
GetInt( Title, IVal)
  char *Title;
  LONG *IVal;
{
  register struct Gadget *Gadgets;
  register struct Window *Window;
  register LONG   i;
  register struct RastPort *Rp;

  struct Window *OpenMyWind();

  IDone = GETINOTDONE;

  if (GetIWind == NULL ) {

    Gadgets = MakeGetInt( Title, *IVal );

    if (Gadgets == NULL) {
      DispErrMsg("Panic. No Gadgets\n",0);
      return( 0 );
    } else {
      Window = OpenMyWind( &NewGetI, screen, NULL, GIWIDTH, GIHEIGHT);

      GetIWind = Window;

      if (Window) {

        Rp = Window->RPort;

        CurWind = Window;

        SetAPen( Rp, NORMALPEN );
        RectFill( Rp,0,0,Window->Width,Window->Height);

        GetGadgets = Gadgets;

        AddGList( Window, Gadgets, -1, -1);

        RefreshGadgets( Gadgets, Window, NULL );

        SetAPen( Rp, HIGHLIGHTPEN );
        Move( Rp, 0,               Window->Height-1 );
        Draw( Rp, 0,               0 );
        Draw( Rp, Window->Width-1, 0 );

        SetAPen( Rp, SHADOWPEN );
        Draw( Rp, Window->Width-1, Window->Height-1 );
        Draw( Rp, 0,               Window->Height-1 );

        DoIGet();
        if (IDone == GETIDONEOK) {
          sscanf( IString,"%d", IVal);
        }
        CloseGetIWind();
      } else {
        return( 0 );
      }
    }
  }
  return( IDone == GETIDONEOK );
} /* OpenGetIWind */

/*
 * Close the get integer window
 */
CloseGetIWind()
{
  if (GetIWind != NULL) {
    CloseMyWind( GetIWind, GetGadgets );
    GetIWind = NULL;
  }
} /* CloseGetIWind */

/*
 * Wait and process a message
 */
DoIGet()
{
  while (IDone == GETINOTDONE) {

    Wait( 1 << BackWind->UserPort->mp_SigBit );

    DoIMsg();
  }
}

/*
 * if there is a message, process it
 */
DoIMsg()
{
  register ULONG Class;
  register struct IntuiMessage *message;
  register struct Gadget *gadget;

  while (message = (struct IntuiMessage *) GetMsg( BackWind->UserPort)) {

    Class = message->Class;
    gadget = (struct Gadget *) message->IAddress;

    /* got a message */

    ReplyMsg(message);

    CheckButtons( gadget, Class );
  }
} /* DoIMsg */

CheckButtons( gadget, class )
  struct Gadget *gadget;
  ULONG class;
{
  if (class == GADGETDOWN) {

    switch( gadget->GadgetID ) {

      case GETICANID:
           IDone = GETICANCEL;
           break;

      case GETIOKID:
           IDone = GETIDONEOK;
           break;
    }
  }
} /* CheckButtons */
