/*=========================================================================
  pgad.c - Gadget display functions.   These functions print out all of the
  structures.  It is an independent module and others can easily be
  added.
  
  Change History:
   11/16/86 - RRL added display stuff for a standard header, intuitexts
              and cleaned up some other stuff. The output should now
	      compile without errors. The gadget names are now used
              to provide symbolic names for borders, images, etc.
=========================================================================*/
 
/*  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 <stdio.h>
#include "gad.h"
 
/* constants */
 
/* external functions */
 
/* external variables */
 
extern char *gadnames[];
extern int nextgad;
extern struct Gadget *gads[];
extern char gadimage [MAX_PROPINFOS] [NAM_SIZ];
extern char selectimage [MAX_PROPINFOS] [NAM_SIZ];
extern struct IntuiText *ITMap[]; /* Pointers to the Gad's Intuitexts */
 
/* globals */
char quote = 34;
char upcasebuf[NAM_SIZ];
 
/*----------- Get name of gadget, given it's address ------------*/
char *getname(gad)
struct Gadget *gad;
{
    int i;
 
    for (i=0; i<nextgad; i++) {
       if (gads[i] == gad) return(gadnames[i]);
    }
    return("NULL");
}
 
/*--------------upname makes an upper case version of a string --RRL-*/
char *upname(str)
   char *str;
   {  char *c;
      
      strcpy(upcasebuf,str); 
      for (c = upcasebuf; *c = toupper(*c); c++);
      return(upcasebuf);
    }

/*---------startcomment prints a line of asterisks for comments --RRL-*/
startcomment(fd)
FILE *fd;
  { fprintf(fd,
"\n/**********************************************************************\n");
  }

/*---------endcomment prints a line of asterisks for comments -----RRL-*/
endcomment(fd)
FILE *fd;
  { fprintf(fd,
" **********************************************************************/\n\n");
  }

/*=======================================================================
  modename - Returns a pointer to a string containing the mode names
========================================================================*/
char *modename(mode)
UBYTE mode;
{
    if (mode = 0) return("JAM1");
    if (mode = 1) return("JAM2");
    if (mode = 2) return("COMPLEMENT");
    if (mode = 4) return("INVERSVID");
}
/*=======================================================================
  disp_border - Displays a Border structure.  The "num" is an
  identification number that distinguishes it from borders that other
  gadgets have.
========================================================================*/
disp_border(fd, border, num, name)  
FILE *fd;
struct Border *border;
int num;
char *name; {
    startcomment(fd);
    fprintf(fd," *  Border Definitions for %s gadget\n",name);
    endcomment(fd);

    disp_pairs(fd, border->XY, num, name);        /* display the border */
    fprintf(fd, "struct Border %s_bord_%d = {\n",name, num);
    fprintf(fd, "  %d,  %d,       /* LeftEdge, TopEdge */\n", 
 	            border->LeftEdge, border->TopEdge);
    fprintf(fd, "  %d,  %d,  %s,  /* FrontPen, BackPen, DrawMode*/\n",
              border->FrontPen, border->BackPen,
              modename(border->DrawMode));
    fprintf(fd, "  5,             /* Count of XY pairs */  \n");
    fprintf(fd, "  (SHORT *)&%s_Pairs_%d, /* XY pairs */\n",name, num);
    fprintf(fd, "  NULL           /* Next Border */\n");
    fprintf(fd, "};\n\n");
}
/*=======================================================================
 disp_render - Displays the "thingie" pointed to by the GadgetRender
 field.   This would be a border or an image.
=========================================================================*/
disp_render(fd, gad, rendnum, index)
FILE *fd;
struct Gadget *gad;
int rendnum;
int index;
{
    long   flags;
    struct Border *bord;
    struct Image *im;
    struct PropInfo *pi;
    
 
 
    flags = (long)gad->UserData;
 
    if ((flags & GAD_BORDER_TYPE) == GAD_BORDER_TYPE) {
       bord = (struct Border *)gad->GadgetRender;
       if (bord != NULL)
          disp_border(fd, bord, rendnum, gadnames[index]);
    }
    if ((flags & GAD_IMAGE_TYPE) == GAD_IMAGE_TYPE) {
       im = (struct Image *)gad->GadgetRender;
       if (im != NULL) {
          imagout(fd, im, gadimage[index]);
       }
    }
    if ((flags & SEL_IMAGE_TYPE) == SEL_IMAGE_TYPE) {
       im = (struct Image *)gad->SelectRender;
       if (im != NULL) {
          imagout(fd, im, selectimage[index]);
       }
    }

    if (gad->GadgetType == PROPGADGET) {
       pi = (struct PropInfo *)gad->SpecialInfo;
       if ((pi->Flags & AUTOKNOB) == AUTOKNOB) {
       startcomment(fd);
       fprintf(fd," * Image structure for %s proportional gadget knob.\n",
               gadnames[index]);
       endcomment(fd);
       fprintf(fd,"struct Image %s_knobim;\n",gadnames[index]);
       }
    }

    if (gad->GadgetText != NULL) 
       disp_texts(fd,gad,rendnum,gadnames[index]);
}

/*=======================================================================
  dispstring - Displays the Stringinfo buffer and the stringInfo
  structure.  A number "num" is passed that identifies it from another
  string gadget in the set of gadgets.   It is passed in from the
  "save as source" function in the filemen.c module
========================================================================*/
dispstring(fd, sinfo, num, size, name)
FILE *fd;
struct StringInfo *sinfo;
int num;                     /* Part of the structure name */
int size;                    /* Size is the length of the string buffer */
char *name;        
{   startcomment(fd);
    fprintf(fd, " *  String information for the %s string gadget.\n",name);
    endcomment(fd);
    fprintf(fd, "UBYTE %s_sbuf_%d[%d] = %c123456789%c;\n",
             name,num, size, quote, quote);
    fprintf(fd, "UBYTE %s_ubuf_%d[%d];\n\n",name, num, size);
    fprintf(fd, "struct StringInfo %s_txstr_%d = {\n", name,num);
    fprintf(fd, "  %s_sbuf_%d, %s_ubuf_%d, /* Buffer, UndoBuffer  */\n", 
             name, num, name, num);
    fprintf(fd, "  %d, %d, %d,  /* BufferPos, MaxChars, DispPos   */\n",
             sinfo->BufferPos, sinfo->MaxChars, sinfo->DispPos);
    fprintf(fd, "  %d, %d,      /* UndoPos, NumChars              */\n", 
             sinfo->UndoPos, sinfo->NumChars);
    fprintf(fd, "  %d, %d, %d,  /* DispCount, CLeft, CTop         */\n",
             sinfo->DispCount, sinfo->CLeft, sinfo->CTop);
    fprintf(fd, "  0x%lx, %ld   /* LayerPtr, LongInt              */\n", 
             sinfo->LayerPtr, sinfo->LongInt);
    fprintf(fd, "  0x%lx        /* AltKeyMap                      */\n", 
             sinfo->AltKeyMap);
    fprintf(fd, "};\n\n");
}
 
/*=======================================================================
  disp_props - Display PropInfo structure
========================================================================*/
disp_props(fd, prop, num, name)
FILE *fd;
struct PropInfo *prop;
int num;
char *name;
{
    int   cnt = 0;
    startcomment(fd);
    fprintf(fd, " * Proportional gadget info for the %s gadget.\n",name);
    endcomment(fd);
    fprintf(fd, "struct PropInfo %s_prop_%d = {\n",name, num);
    fprintf(fd, "   /* Flags                     */\n");
    fprintf(fd, "   ");
    if (prop->Flags == 0) fprintf(fd, "NULL");
 
    if (prop->Flags & AUTOKNOB) {
       fprintf(fd, "AUTOKNOB"); cnt++;
    }
    if (prop->Flags & FREEHORIZ) {
      if (cnt > 0) fprintf(fd, " | ");
      fprintf(fd, "FREEHORIZ");
      cnt++;
    }
    if (prop->Flags & FREEVERT) {
      if (cnt > 0) fprintf(fd, " | ");
      fprintf(fd, "FREEVERT");
      cnt++;
    }
    if (prop->Flags & PROPBORDERLESS) {
      if (cnt > 0) fprintf(fd, " | ");
      fprintf(fd, "PROPBORDERLESS");
      cnt++;
    }
    if (prop->Flags & KNOBHIT) {
      if (cnt > 0) fprintf(fd, " | ");
      fprintf(fd, "KNOBHIT");
      cnt++;
    }
    fprintf(fd, ",\n"); 
    /* the following values should be set to zero */
    fprintf(fd, "  0x0000, 0x0000,   /* HorizPot, VertPot     */\n");
   fprintf(fd,  "  0x%04x, 0x%04x,   /* HorizBody, VertBody   */\n",
           prop->HorizBody, prop->VertBody);
    fprintf(fd, "  %4d, %4d,   /* CWidth, CHeight       */\n", 
           prop->CWidth, prop->CHeight);
    fprintf(fd, "  0x%04x, 0x%04x,   /* HPotRes, VPotRes      */\n", 
           prop->HPotRes, prop->VPotRes);
    fprintf(fd, "  %4d, %4d    /* LeftBorder, TopBorder */\n", 
           prop->LeftBorder,  prop->TopBorder);
    fprintf(fd, "};\n\n");
}
 
/*=======================================================================
  disp_pairs - Displays the gadget pairs
========================================================================*/
disp_pairs(fd, pairs, num, name)
FILE *fd;
SHORT *pairs;
int num;
char *name;
{
    int   i;
 
    fprintf(fd, "SHORT %s_Pairs_%d[] = {\n",name, num);
    for (i=0; i<10; i++) {
        fprintf(fd, "  %d", *pairs++);
        if ( i != 9) fprintf(fd, ",   ");
        else         fprintf(fd, "    ");
        if (i & 1)   fprintf(fd, "\n");
    }
    fprintf(fd, "};\n");
 
}
 
 
/*=======================================================================
  $$$  DispGadget - Displays the Gadget in "struct" form thru serial device
  later will vector it anywhere
========================================================================*/
disp_gadget(fd, gad, ren_type, rnum, inum, name, ind)
FILE *fd;
struct Gadget *gad;
int ren_type;        /* Specifies Border or Image */
int rnum;            /* A unique number identifying border or image */
int inum;            /* A unique number as above to be displayed in source*/
char *name;
int ind;
{
    int cnt;          /* Number of flags set */
    char *gnam;       /* Gadget name that gets printed */
    long flags;
    struct PropInfo *pi;
 
    /*  Set gadget render type here */
    flags = (long)gad->UserData;
    if (name == NULL) gnam = "gad";
    else gnam = name;
 
    startcomment(fd);
    fprintf(fd," *  Gadget Structure definition for the %s gadget.\n",gnam);
    endcomment(fd);

    fprintf(fd, "struct Gadget %s = {\n", gnam);
    if (gad->NextGadget != NULL)
              fprintf(fd, " &");
    else fprintf(fd, "  ");
    fprintf(fd, "%s,     /* NextGadget pointer */\n",
               getname(gad->NextGadget));
    fprintf(fd, "  %d, %d,    /* LeftEdge, TopEdge  */\n",
               gad->LeftEdge, gad->TopEdge);
     fprintf(fd,"  %d, %d,    /* Width, Height      */\n",
               gad->Width, gad->Height);
               
     fprintf(fd, "  /* Gadget Flags */\n");
    cnt = 0;
    fprintf(fd, "  ");
    if (gad->Flags == 0) {
        fprintf(fd, "GADGHCOMP"); /* complement is zero */
        cnt++;
    }   
    if (gad->Flags & SELECTED) {
       if (cnt > 0) fprintf(fd, " | ");
       fprintf(fd, " SELECTED");
       cnt++;
    }
    if (gad->Flags & GADGHNONE) {
       if (cnt > 0) fprintf(fd, " | ");
       fprintf(fd, " GADGHNONE");
       cnt++;
    }
    if (gad->Flags & GADGHBOX) {
       if (cnt > 0) fprintf(fd, " | ");
       fprintf(fd, " GADGHBOX");
       cnt++;
    }
    if (gad->Flags & GADGHIMAGE) {
       if (cnt > 0) fprintf(fd, " | " );
       fprintf(fd, " GADGHIMAGE");
       cnt++;
    }
    if (gad->Flags & GADGIMAGE) {
       if (cnt > 0) fprintf(fd, " | ");
       fprintf(fd, " GADGIMAGE");
       cnt++;
    }
    if (gad->Flags & GRELBOTTOM) {
       if (cnt > 0) fprintf(fd, " | ");
       fprintf(fd, " GRELBOTTOM");
       cnt++;
    }
    if (gad->Flags & GRELRIGHT) {
       if (cnt > 0) fprintf(fd, " | ");
       fprintf(fd, " GRELRIGHT");
       cnt++;
    }
    if (gad->Flags & GRELWIDTH) {
       if (cnt > 0) fprintf(fd, " | ");
       fprintf(fd, " GRELWIDTH");
       cnt++;
    }
    if (gad->Flags & GRELHEIGHT) {
       if (cnt > 0) fprintf(fd, " | ");
       fprintf(fd, " GRELHEIGHT");
       cnt++;
    }
    if (gad->Flags & GADGDISABLED) {
       if (cnt > 0) fprintf(fd, " | ");
       fprintf(fd, " GADGDISABLED");
       cnt++;
    }
 
    fprintf(fd, ",\n");     /* put comma at end */
    fprintf(fd, "  /* Activation Flags */\n");
    cnt = 0;
    fprintf(fd, " ");
    if (gad->Activation == NULL) fprintf(fd, " NULL");
 
    if (gad->Activation & RELVERIFY ) {
       fprintf(fd, " RELVERIFY");
       cnt++;
    }
    if (gad->Activation & GADGIMMEDIATE ) {
        if (cnt > 0) fprintf(fd, " | ");
        fprintf(fd, "GADGIMMEDIATE");
        cnt++;
    }
    if (gad->Activation & ENDGADGET) {
        if (cnt > 0) fprintf(fd, " | ");
        fprintf(fd, "ENDGADGET");
        cnt++;
    }
    if (gad->Activation & FOLLOWMOUSE) {
        if (cnt > 0) fprintf(fd, " | ");
        fprintf(fd, "FOLLOWMOUSE");
        cnt++;
    }
    if (gad->Activation & TOGGLESELECT) {
        if (cnt > 0) fprintf(fd, " | ");
        fprintf(fd, "TOGGLESELECT");
        cnt++;
    }
    if (gad->Activation & LONGINT) {
        if (cnt > 0) fprintf(fd, " | ");
        fprintf(fd, "LONGINT");
        cnt++;
    }
    if (gad->Activation & RIGHTBORDER) {
        if (cnt > 0) fprintf(fd, " | ");
        fprintf(fd, "RIGHTBORDER");
        cnt++;
    }
    if (gad->Activation & LEFTBORDER) {
        if (cnt > 0) fprintf(fd, " | ");
        fprintf(fd, "LEFTBORDER");
        cnt++;
    }
    if (gad->Activation & TOPBORDER) {
        if (cnt > 0) fprintf(fd, " | ");
        fprintf(fd, "TOPBORDER");
        cnt++;
    }
    if (gad->Activation & BOTTOMBORDER) {
        if (cnt > 0) fprintf(fd, " | ");
        fprintf(fd, "BOTTOMBORDER");
        cnt++;
    }
    if (gad->Activation & STRINGCENTER) {
        if (cnt > 0) fprintf(fd, " | ");
        fprintf(fd, "STRINGCENTER");
        cnt++;
    }
    if (gad->Activation & STRINGRIGHT) {
        if (cnt > 0) fprintf(fd, " | ");
        fprintf(fd, "STRINGRIGHT");
        cnt++;
    }
    if (gad->Activation & ALTKEYMAP) {
        if (cnt > 0) fprintf(fd, " | ");
        fprintf(fd, "ALTKEYMAP");
        cnt++;
    }
 
 
    fprintf(fd, ",\n");     /* put comma at end */
    fprintf(fd, "  /* GadgetType */\n");
    switch (gad->GadgetType) {
        case BOOLGADGET: fprintf(fd, "  BOOLGADGET,\n"); break;
        case PROPGADGET: fprintf(fd, "  PROPGADGET,\n"); break;
        case STRGADGET : fprintf(fd, "  STRGADGET,\n"); break;
    }                    /* $$$ */
    
    fprintf(fd, "  ");
 
    if (gad->GadgetRender == NULL)
       fprintf(fd, "NULL, ");
    else {   /* We get the name of the select and print it */
           flags = (long)gad->UserData;
 
           if ((flags & GAD_BORDER_TYPE) == GAD_BORDER_TYPE)
              fprintf(fd, "(APTR)&%s_bord_%d,  ",gnam, rnum);
              
           if (gad->GadgetType == PROPGADGET)  {
              pi = (struct PropInfo *)gad->SpecialInfo;
              if ((pi->Flags & AUTOKNOB) == AUTOKNOB)
                 fprintf(fd, "(APTR)&%s_knobim,   ",gnam);
              else if ((flags & GAD_IMAGE_TYPE) == GAD_IMAGE_TYPE)
                 fprintf(fd, "(APTR)&%s,    ", gadimage[ind]);
           }
           else if ((flags & GAD_IMAGE_TYPE) == GAD_IMAGE_TYPE)
              fprintf(fd, "(APTR)&%s,   ", gadimage[ind]);
            
    }
    fprintf(fd," /*  GadgetRender */\n");
    
    fprintf(fd, "  ");

    if (gad->SelectRender == NULL)
       fprintf(fd, "NULL,  ");
 
    else {   /* ### We get name of SelectRender and print it */
           flags = (long)gad->UserData;
           if ((flags & SEL_BORDER_TYPE) == SEL_BORDER_TYPE)
              fprintf(fd, "(APTR)&%s_bord_%d,  ",gnam, rnum);
               
           if (gad->GadgetType == PROPGADGET)  {
              pi = (struct PropInfo *)gad->SpecialInfo;
              if ((pi->Flags & AUTOKNOB) == AUTOKNOB)
                fprintf(fd, "NULL,  ");
              else if ((flags & SEL_IMAGE_TYPE) == SEL_IMAGE_TYPE) 
                fprintf(fd, "(APTR)&%s,  ", selectimage[ind]);
              }
           else if ((flags & SEL_IMAGE_TYPE) == SEL_IMAGE_TYPE) 
              fprintf(fd, "(APTR)&%s,  ", selectimage[ind]);

    }
    fprintf(fd, "  /* SelectRender */\n ");
    if (gad->GadgetText == NULL)
       fprintf(fd, "  NULL,    /* GadgetText */\n");
    else {   /* We get name of IntuiText and print it */
       fprintf(fd, "  &%s_Text_0,  /* GadgetText */\n", gnam);
    }
    fprintf(fd, "  0x%lx,    /* MutualExclude */\n", gad->MutualExclude);
 
    switch(gad->GadgetType) {
       case BOOLGADGET: fprintf(fd, "  NULL,  ",
                               gad->SpecialInfo); 
            break;
       case STRGADGET:  fprintf(fd, "  (APTR)&%s_txstr_%d,  ",gnam, inum);
            break;
       case PROPGADGET: fprintf(fd, "  (APTR)&%s_prop_%d,  ",gnam, inum);  
           break;
    }
    fprintf(fd, " /* SpecialInfo */\n");
    fprintf(fd, "  %sGAD,    /* GadgetID         */\n", upname(gnam)); 
    fprintf(fd, "  0x%lx     /* UserData Pointer */\n",gad->UserData);
    fprintf(fd, "};\n\n");
 
}

/*=========================================================================
  disp_Header prints out all of the standard #includes and generates
  some #defines using the gadget names. It also prints the text attribute
  structures used for intuitexts.  RRL
=========================================================================*/
disp_Header(fd)
FILE *fd;
  { 
    int i;
    startcomment(fd);
    fprintf(fd," *                    Gadget Structure Definitions\n");
    fprintf(fd," * \n");
    fprintf(fd," * The following structures were defined using the Gadget Editor created \n");
    fprintf(fd," * by the Programmer's Network.\n");
    fprintf(fd," * The credits for the Gadget Editor are:\n");
    fprintf(fd," * \n");
    fprintf(fd," *     John Draper    - Initial design, coordination, and integration.\n");
    fprintf(fd," *     Ray Larson     - Images and Intuitext. \n");
    fprintf(fd," *     Brent Southard - Saving and restoring gadgets in binary form.\n");
    fprintf(fd," *     Dave Milligan  - Gadget Editor Main menu.\n");
    fprintf(fd," * \n");
    fprintf(fd," * \n");
    endcomment(fd);

 
    fprintf(fd,"/*  The header files needed for gadget definitions  */ \n");
    fprintf(fd,"#include <intuition/intuition.h> \n");
    fprintf(fd,"#include <intuition/intuitionbase.h> \n");
    fprintf(fd,"#include <libraries/dosextens.h> \n");
    fprintf(fd,"#include <graphics/gfxbase.h> \n");
    fprintf(fd,"#include <graphics/gfx.h> \n");
    fprintf(fd,"#include <graphics/display.h> \n\n");
    fprintf(fd,"#include <graphics/text.h> \n");
    fprintf(fd,"#include <ctype.h> \n");

    fprintf(fd,"/*  Definitions for Gadget ID numbers               */ \n");
    for(i=0; i < nextgad; i++)
       fprintf(fd,"#define  %sGAD   %d\n",upname(gadnames[i]),i);
       
    /* output text attribute structures */   
    startcomment(fd);
    fprintf(fd," *  Text attribute structures used in rendering IntuiTexts\n");
    endcomment(fd);


    fprintf(fd,"\nchar def_font[] =\"topaz.font\";\n");
    fprintf(fd,"\n");
    fprintf(fd,"struct TextAttr TxtAt_Plain = {	(UBYTE *)def_font, 8,\n"); 
    fprintf(fd,"        FS_NORMAL, FPF_ROMFONT};\n");
    fprintf(fd,"\n");
    fprintf(fd,"struct TextAttr TxtAt_BIU = {(UBYTE *)def_font, 8, \n");
    fprintf(fd,"        FSF_BOLD | FSF_ITALIC | FSF_UNDERLINED, FPF_ROMFONT};\n");
    fprintf(fd,"\n");
    fprintf(fd,"struct TextAttr TxtAt_BU = {(UBYTE *)def_font, 8, \n");
    fprintf(fd,"        FSF_BOLD | FSF_UNDERLINED, FPF_ROMFONT};\n");
    fprintf(fd,"\n");
    fprintf(fd,"struct TextAttr TxtAt_BI = {(UBYTE *)def_font, 8, \n");
    fprintf(fd,"       FSF_BOLD | FSF_ITALIC, FPF_ROMFONT};\n");
    fprintf(fd,"\n");
    fprintf(fd,"struct TextAttr TxtAt_B ={(UBYTE *)def_font, 8, \n");
    fprintf(fd,"       FSF_BOLD, FPF_ROMFONT};\n");
    fprintf(fd,"\n");
    fprintf(fd,"struct TextAttr TxtAt_IU ={(UBYTE *)def_font, 8,\n");
    fprintf(fd,"       FSF_ITALIC | FSF_UNDERLINED, FPF_ROMFONT};\n");
    fprintf(fd,"\n");
    fprintf(fd,"struct TextAttr TxtAt_I ={(UBYTE *)def_font, 8, \n");
    fprintf(fd,"       FSF_ITALIC, FPF_ROMFONT};\n");
    fprintf(fd,"\n");
    fprintf(fd,"struct TextAttr TxtAt_U ={(UBYTE *)def_font, 8, \n");
    fprintf(fd,"       FSF_UNDERLINED, FPF_ROMFONT};\n");
    fprintf(fd,"\n");
    fprintf(fd,"\n");
   
 /* that's it for the header file */
}

/*=========================================================================
  font_name - takes a byte containing font style flags and returns 
  a pointer to a strint matching one of the font names.                 RRL
=========================================================================*/
char *font_name(style)
UBYTE style;
{  switch (style) 
    { 
     case 0: return("&TxtAt_Plain");  break;
     case 1: return("&TxtAt_U");      break;
     case 2: return("&TxtAt_B");      break;  
     case 3: return("&TxtAt_BU");     break;
     case 4: return("&TxtAt_I");      break;
     case 5: return("&TxtAt_IU");     break;
     case 6: return("&TxtAt_BI");     break;
     case 7: return("&TxtAt_BIU");    break;
     };
 
}
/*=========================================================================
  disp_texts prints out all of the IntuiTexts associated with a gadget. RRL
=========================================================================*/
disp_texts(fd,gad,num,name)
FILE *fd;
struct Gadget *gad;
int num;
char *name;
{
    int i;
    struct IntuiText *text;
    BOOL first = TRUE;

   if (gad->GadgetText == NULL) return;

   /* map the linked list of intuitexts into an array */ 
  ITMap[0] = gad->GadgetText;
  for(i = 0; i < 50 && ITMap[i] != NULL; i++)
     ITMap[i+1] = ITMap[i]->NextText;
  
  /* print out the intuitext structures */
    startcomment(fd);
    fprintf(fd," *  IntuiTexts for the %s gadget.\n",name);
    endcomment(fd);
    
  for (i--; i >= 0 ; i--) {
    text = ITMap[i];
    fprintf(fd,"struct IntuiText %s_Text_%d = {\n",name,i);
    fprintf(fd,"   %d, %d,     /* FrontPen, BackPen */\n",
             text->FrontPen,text->BackPen);
    fprintf(fd,"   %s,       /* DrawMode */\n",modename(text->DrawMode));
    fprintf(fd,"   %d, %d,     /* LeftEdge, TopEdge */\n", 
              text->LeftEdge,text->TopEdge);
     /* the following comes from the header file */
    fprintf(fd,"   %s, /* ITextFont Pointer */ \n",
            font_name(text->ITextFont->ta_Style));
            
    fprintf(fd,"   /* The IText */\n");
    fprintf(fd,"   (UBYTE *)\"%s\",\n",text->IText);
    if (first) { 
                 first = FALSE;
                 fprintf(fd,"   NULL\n");
               }  
    else fprintf(fd,"   &%s_Text_%d    /* NextText */\n", name, i+1);
    fprintf(fd," };\n");
    fprintf(fd,"\n\n");
   }

 
}
