/**
 *
 *
 *    OpenSReqWindow(1.0)    ARP Programmers Manual   OpenSReqWindow(1.0)
 *
 *
 *
 *    NAME
 *   OpenSReqWindow -- Open a simple requester window
 *
 *    SYNOPSIS
 *   WindowPtr = OpenSReqWindow(SimpleRequester, hsize, vsize,
 *    D0        A0               D0?    D1?
 *                              idcmpflags, flags)
 *
 *    FUNCTION
 *   This function allocates a NewWindow structure, opens a window,
 *   and RectFills a background color, given an initialized Simple-
 *   Requester structure and a width and height. Also, window flags
 *   and IDCMP flags have to be passed. It then deallocates
 *   the NewWindow structure. Notice that it only uses fields in the
 *   SimpleRequester structure that it needs, so this function can
 *   also be used for other than requester windows. Any error
 *   conditions are returned in the SimpleRequester structure.
 *
 *    INPUTS
 *   SimpleRequester -- A pointer to an initialized SimpleRequester
 *                      structure.
 *         hsize           -- The width of the window
 *   vsize           -- The height of the window
 *   idcmpflags      -- The IDCMP flags
 *   flags           -- The window flags
 *
 *    RESULT
 *   WindowPtr       -- NULL if an error was detected. In that case the
 *                      error value may be obtained from the
 *                      SimpleRequester structure. Otherwise a pointer
 *                      to the Window will be returned.
 *
 *    ADDITIONAL CONSIDERATIONS
 *   This routine assumes Intuition and Graphics are already open.
 *   See also the related higher level routines SimpleMsgWin() and
 *   UpdateMsgWin().
 *
 *    BUGS
 *   None reported sofar.
 *
 *    AUTHOR
 *   W.G.J. Langeveld (WGL)
 *
 *
**/
#include <exec/types.h>
#include <exec/io.h>
#include <exec/memory.h>
#include <libraries/dos.h>
#include <intuition/intuition.h>
#include <intuition/intuitionbase.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <functions.h>
#include <simpreq.h>
#include "simpdefs.h"

extern struct IntuitionBase *IntuitionBase;
extern struct GfxBase *GfxBase;

/**
 *
 *   This routine opens the SimpleRequester window
 *
**/
struct Window *OpenSReqWindow ( struct SimpleRequester *s, int hsize, int vsize, ULONG idcmpflags, ULONG flags )
{
	struct NewWindow *nw;
	struct Window     *w;
	ULONG              pen;
	
	s->sr_ErrorCode = SR_SUCCESS;
	/*
	 *   Set up a NewWindow structure.
	 */
	nw = AllocMem((long) sizeof(struct NewWindow), MEMF_CLEAR);
	if (nw == NULL) 
	{
		s->sr_ErrorCode = SR_OUTOFMEMORY;
		return(NULL);
	}
	
	nw->LeftEdge    = s->sr_x;
	nw->TopEdge     = s->sr_y;
	nw->Width       = hsize;
	nw->Height      = vsize;
	
	if (s->sr_PenList) 
	{
		nw->BlockPen    = s->sr_PenList->reqp_BlockPen;
		nw->DetailPen   = s->sr_PenList->reqp_DetailPen;
	}
	else
	{
		nw->BlockPen    = DEFWINBPEN;
		nw->DetailPen   = DEFWINDPEN;
	}
	
	nw->IDCMPFlags  = idcmpflags;
	nw->Flags       = flags;
	nw->MinWidth    = nw->MaxWidth  = nw->Width;
	nw->MinHeight   = nw->MaxHeight = nw->Height;
	nw->Type        = WBENCHSCREEN;
	/*
	 *   Modify the NewWindow structure if Custom Screen
	 */
	if (s->sr_Screen) 
	{
		nw->Type   = s->sr_Screen->Flags & SCREENTYPE;
		nw->Screen = s->sr_Screen;
	}
	/*
	 *   Open the window and free the NewWindow structure
	 */
	w = OpenWindow(nw);
	
	FreeMem(nw, (long) sizeof(struct NewWindow));
	
	if (w == NULL) 
	{
		s->sr_ErrorCode = SR_NOWINDOW;
		return(NULL);
	}
	
	/*
	 *   Color in the background
	 */
	if (s->sr_PenList) 
	{
		pen = s->sr_PenList->reqp_BackPen;
	}
	else
	{
		pen = DEFBACKPEN;
	}
	
	BlankSReqWin(w, pen);
	
	return(w);
}

void BlankSReqWin ( struct Window *w, ULONG pen)
{
	struct RastPort *rp;
	WORD             sp = 0;
	register long    x1,
	                 x2,
	                 y1,
	                 y2;
	
	if (w == NULL)
		return;
	
	rp = w->RPort;
	
	SetAPen(rp, (long) pen);
	SetDrMd(rp, JAM2);
	
	if (w->BorderTop > 4 && pen)
		sp = 1;
	
	x1 = (long) (            w->BorderLeft   + (sp << 1) - 2);
	y1 = (long) (            w->BorderTop    +  sp       - 1);
	x2 = (long) (w->Width  - w->BorderRight  - (sp << 1) + 1);
	y2 = (long) (w->Height - w->BorderBottom -  sp          );
	
	if (IntuitionBase->LibNode.lib_Version >= 36) 
	{
		y1 += 1;
		x2 -= 2;
		x1 += 2;
		y2 -= 1;
	}
	
	if ((x2 >= x1) && (y2 >= y1))
		RectFill(rp, x1, y1, x2, y2);
	return;
}
