/***************************************************************
 *             -------     auto.c     -------                  *
 *  Here's a routine to automagically construct AutoRequests.  *
 *  BuildAutoRequest, workbench routine by Neil Katin 	       *
 *                                                             *
 *  Modified to stand alone by Andy Finkel 		       *
 *  And Jim Mackraz made the suggestion that it be broken out  *
 *                                                             *
 *  Modified 07/25/86 by Lisa Siracusa to work with Lattice C  *
 ***************************************************************/
 
# include "exec/types.h"
# include "exec/nodes.h"
# include "exec/lists.h"
# include "exec/memory.h"
# include "exec/alerts.h"
# include "clib/macros.h"
# include "graphics/rastport.h"
# include "libraries/dos.h"
# include "libraries/dosextens.h"
# include "intuition/intuition.h"

/****************************************************************
 *  BuildAutoRequest						*
 *  	 takes window pointer,					*
 *	 array of strings to be displayed,			*
 *	 text for positive gadget,				*
 *	 text for negative gadget,				*
 *	 positive flags (see AutoRequest, in Intuition manual)	*
 *	 negative flags (see AutoRequest, in Intuition manual)	*
 ****************************************************************/

BuildAutoRequest(w,textArray,posText,negText,posflags,negflags)
struct Window *w;
char   **textArray;
char   *posText;
char   *negText;
ULONG  posflags;
ULONG  negflags;
{
struct IntuiText *itArray,
                 *ita,
                 *itPos,
                 *itNeg;
SHORT  fontSize;
char   **sa;
int    height,
       width;
int    sizeArray;
int    result;

    /*  make sure there is at least one string  */
    if(! *textArray)
        return(0);

    /*  compute height and width  */
    width = textLength(w,posText) + textLength(w,negText) + 70;
    fontSize=(w->WScreen->Font->ta_YSize);
    height = fontSize * 2 + 22;

    for(sa = textArray; *sa; sa++) 
    {
	height += fontSize + 2;
	width = MAX(width,textLength(w,*sa) + 34);
    }

    sizeArray = ((sa - textArray) +2) * sizeof(struct IntuiText);

    /*  allocate intuitext structures  */
    itArray = (struct IntuiText *) AllocMem(sizeArray,MEMF_CLEAR);
    if(! itArray) 
       return(0);

    /*  now initialize the intuitext structures  */
    ita = itArray;
    sa = textArray;

    ita->TopEdge = 4;
    for(ita = itArray,sa = textArray; *sa; sa++, ita++)
	initIntuiText(fontSize,ita,*sa,&ita[1]);

    /*  keep the next one from pointing off into space  */
    ita[-1].NextText = NULL;
 
    itPos = ita++;
    initIntuiText(fontSize,itPos,posText,0);

    itNeg = ita;
    initIntuiText(fontSize,itNeg,negText,0);

    itPos->TopEdge = itNeg->TopEdge = AUTOTOPEDGE;
    itPos->LeftEdge = itNeg->LeftEdge = AUTOLEFTEDGE;

    result = AutoRequest(w,itArray,itPos,itNeg,
	                 posflags,negflags,width + 16,height);

    FreeMem(itArray,sizeArray);
    return(result);
}


initIntuiText(size,it,text,itNext)
SHORT  size;
struct IntuiText *it;
char   *text;
struct IntuiText *itNext;
{
    it->FrontPen = 0;
    it->BackPen  = 1;
    it->DrawMode = JAM1;
    it->IText    = text;

    if(itNext) 
    {
	it->NextText = itNext;
	itNext->TopEdge = it->TopEdge + size+2;
	it->LeftEdge = 12;
    }
}


textLength(w,string)
struct Window *w;
char   *string;
{
return(TextLength(w->RPort,string,stringlen(string)));
}


stringlen(s)
register char *s;
{
register i = 0;

while(*s++) 
      i++;
return(i);
}
