/** simpmsg.c
 *
 *
 *    SimpleMsgWindow(1.0)    ARP Programmers Manual   SimpleMsgWindow(1.0)
 *
 *
 *
 *    NAME
 *   SimpleMsgWindow -- Open a simple message window fitting a requester.
 *
 *    SYNOPSIS
 *   Windowptr = SimpleMsgWindow( SimpleRequester )
 *
 *    FUNCTION
 *   This function opens a window and displays a set of text lines.
 *   The window is auto-sizing: it determines its size based on the
 *   number of non-NULL strings. The window does not have an IDCMP
 *   and comes only with a drag bar.
 *
 *    INPUTS
 *   SimpleRequester -- A pointer to an initialized SimpleRequester
 *   structure. Only applicable fields are used.
 *
 *    RESULT
 *   Windowptr   -- A pointer to the window. NULL on error. (See
 *                  SimpleRequester structure for error code).
 *
 *    ADDITIONAL CONSIDERATIONS
 *   The window may be closed using the fuction CloseMsgWindow.
 *   Since the window does not have an IDCMP port, no special steps
 *   need to be taken.
 *
 *    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 <graphics/gfxbase.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;

struct Window *SimpleMsgWindow ( struct SimpleRequester *s )
{
	return(SimpleMsgWIDCMP(s, NULL));
}

struct Window *SimpleMsgWIDCMP ( struct SimpleRequester *s, long IDCMP )
{
	WORD		   vsize, i, hsize, temp;
	struct Window *w;
	/*
	 *   Return if no request, otherwise initialize
	 */
	if (s == NULL)
		return(NULL);
	
	s->sr_ErrorCode = SR_SUCCESS;
	/*
	 *   Get the dimensions of the window.
	 *   vsize is the height, hsize the width of the window.
	 *
	 *   First, make hsize the length of the longest prompt line, and increment
	 *   vsize along the way.
	 */
	hsize = MINWINWIDTH;
	for (i = 0, vsize = MINWINHEIGHT; s->sr_Prompt[i] && i < SR_MAXPROMPTS;
	i++, vsize += LINEHEIGHT) 
	{
		if ((temp = CHARWIDTH * strlen(s->sr_Prompt[i])) > hsize)
			hsize = temp;
	}
	/*
	 *   Add space for the vertical borders of the requester.
	 */
	hsize += 2 * VBORDER;
	/*
	 *   Add a bit more space, and we have the window height in vsize.
	 */
	vsize += VSKIP;
	/*
	 *   Open the window
	 */
	w = OpenSReqWindow(s, hsize, vsize, IDCMP, WINDOWDRAG+WINDOWDEPTH);
	
	if (w == NULL)
		goto cleanup;
	/*
	 *   Put up the prompts.
	 */
	UpdateMsgWindow(w, s);
	/*
	 *   Clean up the mess.
	 */
	cleanup:
	
	return(w);
}





/**
 *
 *
 *    UpdateMsgWindow(1.0)    ARP Programmers Manual   UpdateMsgWindow(1.0)
 *
 *
 *
 *    NAME
 *   UpdateMsgWindow -- Update a simple message window.
 *
 *    SYNOPSIS
 *   result = UpdateMsgWindow(WindowPtr, SimpleRequester)
 *    D0        A0        A1
 *
 *    FUNCTION
 *   This function updates a window with a set of text lines.
 *   The window should have been opened previously with
 *   SimpleMsgWindow() and it is up to you to make sure that the
 *   new set of lines will fit in the window.
 *
 *    INPUTS
 *   Windowptr       -- A pointer to a previously opened window
 *   SimpleRequester -- A pointer to an initialized SimpleRequester
 *                      structure. Only applicable fields are used.
 *
 *    RESULT
 *   result -- 0 if an error was detected. In that case the error
 *             value may be obtained from the SimpleRequester structure.
 *             1 on success.
 *
 *    ADDITIONAL CONSIDERATIONS
 *
 *    BUGS
 *   None reported sofar.
 *
 *    AUTHOR
 *   W.G.J. Langeveld (WGL)
 *
 *
**/
long UpdateMsgWindow ( struct Window *w, struct SimpleRequester *s )
{
	struct RastPort *rp;
	struct TextFont *font;
	long             result = 0L;
	WORD             vpos,
					 i,
					 j;
	ULONG			 apen,
					 bpen;
	LONG			 splen;
	char			*spaces = NULL;
	
	if (w == NULL)
		return(result);
	
	result = 1L;
	/*
	 *   rp is the RastPort
	 */
	rp = w->RPort;
	/*
	 *   Set font.
	 */
	font = (struct TextFont *) OpenFont(Topaz80Equiv());
	if (font == NULL) 
	{
		s->sr_ErrorCode = SR_NOFONT;
		result = 0L;
		goto cleanup;
	}
	
	SetFont(rp, font);
	/*
	 *   Get the pens.
	 */
	if (s->sr_PenList) 
	{
		apen = s->sr_PenList->reqp_PromptPen;
		bpen = s->sr_PenList->reqp_BackPen;
	}
	else
	{
		apen = DEFPROMPEN;
		bpen = DEFBACKPEN;
	}
	/*
	 *   Add the prompts
	 */
	SetAPen(rp, (long) apen);
	SetBPen(rp, (long) bpen);
	SetDrMd(rp, JAM2);
	
	splen = (w->Width - 2 * VBORDER) / 8 + 1;
	
	spaces = AllocMem(256L, MEMF_CLEAR);
	if (spaces == NULL) 
	{
		s->sr_ErrorCode = SR_OUTOFMEMORY;
		result = 0L;
		goto cleanup;
	}
	
	vpos = FIRSTLINEY;
	for (i = 0; s->sr_Prompt[i] && i < SR_MAXPROMPTS; i++) 
	{
		Move(rp, (long) VBORDER, (long) vpos);
		
		for (j = 0; j < splen; j++)
			spaces[j] = ' ';
		strcpy(spaces, s->sr_Prompt[i]);
		
		j = strlen(s->sr_Prompt[i]);
		if (j) 
		{
			spaces[j] = ' ';
			Text(rp, spaces, (long) (splen - 1) );
		}
		
		vpos += LINEHEIGHT;
	}
	/*
	 *   Clean up the mess.
	 */
	cleanup:
	if (font)
		CloseFont(font);
	if (spaces)
		FreeMem(spaces, 256L);
	
	return(result);
}

/**
 *
 *
 *    CloseMsgWindow(1.0)    ARP Programmers Manual   CloseMsgWindow(1.0)
 *
 *
 *
 *    NAME
 *   CloseMsgWindow -- Close a simple message window.
 *
 *    SYNOPSIS
 *   result = CloseMsgWindow(WindowPtr)
 *    D0        A0
 *
 *    FUNCTION
 *   This function closes a window.
 *
 *    INPUTS
 *   Windowptr       -- A pointer to a previously opened window
 *
 *    RESULT
 *   result -- NULL if success, Pointer to Window if failure.
 *
 *    ADDITIONAL CONSIDERATIONS
 *   Provided to avoid having to open the libraries
 *
 *    BUGS
 *   None reported sofar.
 *
 *    AUTHOR
 *   W.G.J. Langeveld (WGL)
 *
 *
**/
struct Window *CloseMsgWindow ( struct Window *w )
{
	/*
	 *   Well, close it.
	 */
	CloseWindow(w);
	w = NULL;
	
	return(w);
}

/**
 *
 *   See if the default GfxBase font is "Topaz-80 compatible". If so,
 *   return a pointer to that font's TextAttr structure to the caller.
 *
**/
struct TextAttr *Topaz80Equiv ( void )
{
	static char   fontname[40] = "topaz.font";
	static struct TextAttr plaintext = 
	{
		fontname, 8, 0, 0
	};
	
	struct TextFont *font;
	/*
	 *   Make sure the font pointer won't change during this operation
	 */
	Forbid();
	font = GfxBase->DefaultFont;
	if (font) 
	{
		/*
		 *   Now check if it has intolerable flags or style or size
		 */
		if (font->tf_Style & (FSF_UNDERLINED | FSF_BOLD | FSF_ITALIC |
			FSF_EXTENDED   | FSF_COLORFONT)) font = NULL;
		if (font->tf_Flags & (FPF_REVPATH | FPF_PROPORTIONAL))
			font = NULL;
		if ((font->tf_YSize != 8) || (font->tf_XSize != 8))
			font = NULL;
	}
	/*
	 *   If we have a font and it is of the right type, copy it into the
	 *   TextAttr struct for future use.
	 */
	if (font)
		strcpy(fontname, font->tf_Message.mn_Node.ln_Name);
	Permit();
	/*
	 *   Now load the font into memory.
	 */
	font = OpenFont(&plaintext);
	if (font == NULL)
		OpenDiskFont(&plaintext);
	if (font)
		CloseFont(font);
	
	return(&plaintext);
}
