/*=========================================================================
   misc.c - Misc Routines not going anywhere else.   If not sure where
   to put it,  then put it here
=========================================================================*/
 
 
/*  The usual header files to be inserted later  */
#include <intuition/intuition.h>
#include <libraries/dosextens.h>
#include <graphics/gfxbase.h>
#include <graphics/gfx.h>
#include <graphics/display.h>
#include <exec/memory.h>
#include <workbench/workbench.h>
#include <workbench/startup.h>
#include <devices/narrator.h>
#include <devices/audio.h>
#include <libraries/translator.h>
#include "gad.h"
 
/* constants */
 
/* global vars */
extern struct Window *w;
 
/* extern vars */
 
/* extern functions */
 
/*--------------------------------------
  findadr - Finds the address in a table
  of addresses, returns an index to it
--------------------------------------*/
int findadr(ptr, adr, len)
ULONG ptr[];
ULONG adr;
int len;
{
    int i;
 
    for (i=0; i<len; i++) {
        if (adr == (ULONG)ptr[i]) return(i);
    }
    return(-1);
}
/*----------------------------------------
  findnull - finds the first NULL address
  in a table of addresses.  Returns an
  index to the NULL address in the table
----------------------------------------*/
int findnull(ptr, len)
ULONG ptr[];
int len;
{
   int i;
 
   for (i=0; i<len; i++) {
       if (ptr[i] == NULL) return(i);
   }
   return(-1);
}
/*---------------------------------------
  freeprop - Called in gt.c to free the
  proportional gadget memory usage.  Pointer
  to gadget is passed,  returns nothing
---------------------------------------*/
freeprop(gad)
struct Gadget *gad;         /* pointer to gadget */
{
             free_renders(gad);   /* Free all the renders */
             if (gad->SpecialInfo != NULL ) {
               FreeMem(gad->SpecialInfo, (long)sizeof(struct PropInfo));
             }
}
/* Frees BOTH GadgetRender or SelectRender fields.  Depends on UserData
   being used to hold a flag that determines if a border or Image was
   allocated
*/

free_bords(gad,freeflag)
struct Gadget *gad;
long freeflag;
{
     struct Border *bord, *selbord;
     long size;
     long flags;
     BOOL bothsame;
     
     /* for testing - just return */
     return;

     if (gad->GadgetRender == gad->SelectRender) bothsame = TRUE; 
     
     flags = (long)gad->UserData;
         if((freeflag & GAD_BORDER_TYPE) == GAD_BORDER_TYPE){ 
             if (gad->GadgetRender != NULL ) {
                if ((flags & GAD_BORDER_TYPE) == GAD_BORDER_TYPE) {
                   bord = (struct Border *)gad->GadgetRender;
                   FreeMem(bord->XY, 20L);
                   FreeMem(bord, (long)sizeof(struct Border));
                   gad->GadgetRender = NULL;
                   flags &= ~GAD_BORDER_TYPE;   /* $$$ */
                   gad->UserData = (APTR)flags;
                }
             }
          }
          if ((freeflag & SEL_BORDER_TYPE) == SEL_BORDER_TYPE) {          
             if (gad->SelectRender != NULL ) {
                if ((flags & SEL_BORDER_TYPE) == SEL_BORDER_TYPE) {
                   selbord = (struct Border *)gad->SelectRender;
                   if (bothsame == FALSE) {
                       FreeMem(selbord->XY, 20L);
                       FreeMem(selbord, (long)sizeof(struct Border));
                   }    
                   gad->SelectRender = NULL;
                   flags &= ~SEL_BORDER_TYPE;  /* $$$ */
                   gad->UserData = (APTR)flags;
                }
             }
          }
          
          if (bothsame) { /* if the same border was used for both */
          		  /* gadget and select only one should be */
			  /* freed - but the other stuff must be  */
			  /* updated for both.			  */
             flags &= ~(SEL_BORDER_TYPE | GAD_BORDER_TYPE);
             gad->UserData = (APTR)flags;
             gad->GadgetRender = NULL;
             gad->SelectRender = NULL;
          }
}


free_images(gad,freeflag)
struct Gadget *gad;
long freeflag;
{
     struct Image *imag;
     long size;
     long flags;
 
return;
     flags = (long)gad->UserData;
          if ((freeflag & GAD_IMAGE_TYPE) == GAD_IMAGE_TYPE) {
             if (gad->GadgetRender != NULL ) {
                if ((flags & GAD_IMAGE_TYPE) == GAD_IMAGE_TYPE) {
                   FreeImage(gad->GadgetRender); /* defined in imagfunc.c */
                   gad->GadgetRender = NULL;
                   flags &= ~GAD_IMAGE_TYPE;   /* $$$ */
                   gad->UserData = (APTR)flags;
                }                                     /* $$$ */
             }
	   }
          if ((freeflag & SEL_IMAGE_TYPE) == SEL_IMAGE_TYPE) {
             if (gad->SelectRender != NULL ) {
                if ((flags & SEL_IMAGE_TYPE) == SEL_IMAGE_TYPE) {
                   FreeImage(gad->SelectRender);
                   gad->SelectRender = NULL;
                   flags &= ~SEL_IMAGE_TYPE;   /* $$$ */
                   gad->UserData = (APTR)flags;
                }
             }
          }
          
          if ((gad->GadgetRender != NULL) && (gad->GadgetType == PROPGADGET)){
             if ((flags & KNOB_IMAGE_TYPE) == KNOB_IMAGE_TYPE) {
                /* AUTOKNOB structures have an image struct but no data */
                FreeMem(gad->GadgetRender, (long)sizeof(struct Image));
                flags &= ~KNOB_IMAGE_TYPE; 
                gad->UserData = (APTR)flags;
             }   
          }  

}


/*---------------------------------------------------------------- 
   this routine frees the list of intuitexts allocated to a gadget
   it is recursive
---------------------------------------------------------------- */
free_itxt(text)
struct IntuiText *text;
{
    if (text == NULL) return;
    free_itxt(text->NextText);
    FreeMem(text, (LONG)sizeof(struct IntuiText));
}

/* -----------------------------------------------------------------
   free_renders gets rid of allocated images, borders and intuitexts
------------------------------------------------------------------ */
free_renders(gad)
struct Gadget *gad;
{
    LONG freeflags;
return;

    freeflags = GAD_BORDER_TYPE | GAD_IMAGE_TYPE | SEL_BORDER_TYPE 
               | SEL_IMAGE_TYPE;
    free_images(gad,freeflags); /* try to free images first */
    free_bords(gad,freeflags);
    free_itxt(gad->GadgetText);
}
/*---------------------------------------
   freestrgad - Frees the string gadget
   which is allocated by addgadget, called
   from gt.c
----------------------------------------*/
freestrgad(gad)
struct Gadget *gad;    /* gadget pointer */
{
   struct StringInfo *si;     /* StringInfo structure */
 
   /* Get pointer to StringInfo */
   si = (struct StringInfo *)gad->SpecialInfo;
 
   if (si) {
        /* De-allocate the Buffer */
        if (si->Buffer)  FreeMem(si->Buffer, BUFSIZE);
 
        /* And the Stringinfo structures */
        FreeMem(si, (long)sizeof(struct StringInfo));
   }
   free_renders(gad);     /* Frees select render's etc */
}

/* IntuiText structures for message autorequesters */

  struct IntuiText autotxt1 = {2,1,JAM2,5,5,NULL,NULL,NULL};
  struct IntuiText autotxt2 = {2,1,JAM2,5,2,NULL,NULL,NULL};
  struct IntuiText autotxt3 = {2,1,JAM2,5,2,NULL,NULL,NULL};
    

/*============================================================*/
/* autoreq uses the AutoRequest routine to do simple messages */
/* and yes/no requests to the user.                           */
/*============================================================*/
autoreq(bodtext,postext,negtext)
    char *bodtext, *postext, *negtext;
    {
      long reqwidth, reqheight;
      SHORT txtlength;
      BOOL result;
      
      txtlength = strlen(bodtext)*10;
      if (txtlength < 60) txtlength = 60;
      reqwidth = (LONG)txtlength + 16;
      reqheight = (LONG)55;

      autotxt1.IText = (UBYTE *)bodtext;
      autotxt2.IText = (UBYTE *)postext;
      autotxt3.IText = (UBYTE *)negtext;

      result = AutoRequest(w,&autotxt1,&autotxt2,&autotxt3,
               0L,0L,reqwidth,reqheight);
               
      autotxt1.IText = NULL;
      autotxt2.IText = NULL;
      autotxt3.IText = NULL;
      return(result);

}     
