/* dialog.c - Standard dialogs */

#define DEBUG 0

/*
**	Date written: 09/18/86
**	Author: Tim Holloway
**		Compuserve: 73026,2026
**		Bix: tholloway
**		Fido node: 112/3 (Casa Mi Amiga).
**
**	Version: 1.1
**
**	Copyright (C) 1986, by Tim Holloway.  This program may be
**	freely distributed for non-commercial use only.  Use for commercial
**	purposes without the express permission of the author is a violation
**	of copyright law.
**
**	Restrictions:
**
**		This version assumes that output is going to the Workbench
**		screen.  Intuition and graphics libraries are expected to
**		be open.
**
**	Description:
**	  Gripe():
**		displays a requester with up to three lines of text.
**
**	  AyeNay():
**		Like Gripe, but looks for a Yes/No answer.  Returns BOOL.
**
**	  About():
**		Opens a window to display IntuiTexts.  Used for
**		informational displays.
**
**	Change History:
**		1.1 Added AyeNay() and About(). 1/10/87, TFH
**
**
**------Usage: Gripe (line1, line2, line3);
**
**	All are TEXT *;
**
**	Quirks: Builds stuff that MUST be done in CHIP memory.  Use
**	ATOM or something to place the DATA segment in CHIP memory!
**	Intuition Library MUST be open prior to this call.
**	If you need less than 3 lines of text, use "", although I think
**	that NULL is OK by Commodore.
**
**	Doesn't make allowance for text overflowing window.  Only response
**	is "Rats!" - a future version may look for an external IntuiText
**	pointer named GripeCurse so you can have more variety in your
**	complaints (random curse generator, anyone?).
**
**------Usage: boolvar = AyeNay (line1, line2, line3);
**
**	As above, except that Yes and No options are supplied to the
**	request.
**
**------Usage: About(width, height, Itext);
**
**	Defines a window, width x height pixels, centered on the screen.
**
**	Itext is the first in a series of chained IntuiTexts.  Can be used
**	in any way desired.
**
**	Restrictions: allow 2 pixels in for a border, the lower left corner
**	for the "OK" gadget.
**
*/

/* Basic Amiga system definitions */

#include <exec/types.h>
#include <exec/alerts.h>
#include <exec/memory.h>
/* #include <libraries/dos.h> */
/* #include <libraries/dosextens.h> */
/* include <graphics/gfx.h> */
#include <intuition/intuition.h>

static struct IntuiText Itemplate =
{ AUTOFRONTPEN, AUTOBACKPEN, AUTODRAWMODE,
  AUTOLEFTEDGE, AUTOTOPEDGE, AUTOITEXTFONT,
  NULL,
  NULL};

#define MEMSIZE (sizeof(struct IntuiText))

static struct IntuiText *
getwrk(msg1, msg2, msg3, numits)
TEXT *msg1, *msg2, *msg3;
int numits;
{
	register struct IntuiText *mtx, *mtx1;

	if ( (mtx = (struct IntuiText *)
		AllocMem(MEMSIZE*numits, MEMF_CHIP)) == NULL)
	{
		DisplayAlert(AT_Recovery|AG_NoMemory,
		 "Short on memory!", 60);
		return NULL;
	}
	repmem (mtx, &Itemplate, sizeof(Itemplate), numits);
	mtx1 = mtx;

	mtx1->TopEdge = AUTOTOPEDGE+0;
	mtx1->IText = msg1;
	mtx1->NextText = mtx1+1;
	mtx1++;

	mtx1->TopEdge = AUTOTOPEDGE+8;
	mtx1->IText = msg2;
	mtx1->NextText = mtx1+1;
	mtx1++;

	mtx1->TopEdge = AUTOTOPEDGE+16;
	mtx1->IText = msg3;
	mtx1++;
	return mtx;
}

void Gripe (msg1, msg2, msg3)
TEXT *msg1, *msg2, *msg3;
{
	register struct IntuiText *mtx, *mtx1;

	mtx = getwrk(msg1, msg2, msg3, 4);
	if (mtx != NULL)
	{
		mtx1 = mtx+3;
		mtx1->TopEdge = AUTOTOPEDGE+0;
		mtx1->IText = "Rats!";

		(void) AutoRequest (NULL, mtx, NULL, mtx1,  0, 0, 300, 66);
		FreeMem(mtx, MEMSIZE*4);
	}
}

BOOL AyeNay (msg1, msg2, msg3)
TEXT *msg1, *msg2, *msg3;
{
	register struct IntuiText *mtx, *mtx1;
	register BOOL rc;

	while ( (mtx = getwrk(msg1, msg2, msg3, 5)) == NULL)
		 /* keep trying - MUST get memory! */;

	{
		mtx1 = mtx+3;
		mtx1->TopEdge = AUTOTOPEDGE+0;
		mtx1->IText = "Yes";
		mtx1++;
		mtx1->TopEdge = AUTOTOPEDGE+0;
		mtx1->IText = "No";

		rc = AutoRequest (NULL, mtx, mtx+3, mtx+4,  0, 0, 300, 66);
		FreeMem(mtx, MEMSIZE*5);
		return rc;
	}
}

static struct NewWindow nw = {
   0, 0, 0, 0,		/* left, top, width, height */
   1, 0,		/* pens */
   GADGETUP | RAWKEY ,
   SMART_REFRESH | ACTIVATE,
   NULL, /* firstgadget */
   NULL, /* checkmark */
   NULL, /* title */
   NULL, /* screen*/
   NULL, /* bitmap */
   0, 0, 0, 0,
   WBENCHSCREEN      /*===*/
};

static UWORD about_pairs[] = {
	0,0, 2,0, 2,2, 0,2, 0, 0
};

static struct Border about_border = {
	0, 0, 0, 1, JAM2, 5, &about_pairs, NULL
};

void
About (width, height, Itext)
UWORD width, height;
struct IntuiText *Itext;
{
	register struct Window *win;
	register struct IntuiMessage *ms, *GetMsg();
	register int code, class;

	nw.LeftEdge = 320 - (width >> 1);
	nw.TopEdge = 100 - (height >> 1);
	nw.Width = width;
	nw.Height = height;
	if ( (win = (struct Window *) OpenWindow(&nw)) == NULL) return;

	SetRast (win->RPort, 1);
	about_pairs[2] = width-1;
	about_pairs[4] = width-1;
	about_pairs[5] = height-1;
	about_pairs[7] = height-1;
	DrawBorder (win->RPort, &about_border, 0, 0);
	PrintIText (win->RPort, Itext, 4, 4);
	FOREVER
	{
		Wait (1L << win->UserPort->mp_SigBit);
		if ( (ms = GetMsg(win->UserPort)) != NULL)
		{
			code = ms->Code;
			class = ms->Class;
			ReplyMsg(ms);
			if (class = RAWKEY)
			{
			    if (code & IECODE_UP_PREFIX) continue;
			    break;
			}
		}
	}
	CloseWindow(win);
	return;
}

#if DEBUG

struct Library *IntuitionBase, *GfxBase, *OpenLibrary();

struct IntuiText thisnext = {
  AUTOFRONTPEN, AUTOBACKPEN, AUTODRAWMODE,
  0, 11, AUTOITEXTFONT,
  "This is Line 2",
  NULL
};

struct IntuiText thistest = {
  AUTOFRONTPEN, AUTOBACKPEN, AUTODRAWMODE,
  0, 0, AUTOITEXTFONT,
  "This is an IntuiTest",
  &thisnext
};

main()
{
	if ( (IntuitionBase = OpenLibrary("intuition.library", 29)) == NULL)
	{
		printf ("Couldn't open Intuition library!\n");
		_exit(20);
	}

	if ( (GfxBase = OpenLibrary("graphics.library", 29)) == NULL)
	{
		printf ("Couldn't open graphics library!\n");
		_exit(20);
	}
	About (400, 100, &thistest);

	printf ("Ayenay test: %d\n", AyeNay("Did", "this", "go OK?"));

	Gripe ("This is a", "Gripe", "test");
	CloseLibrary (GfxBase);
	CloseLibrary(IntuitionBase);
}
#endif
