
#ifndef DEMO

#include "Toolmaker.h"
#include "Externs.h"

#include <string.h>

#include <clib/dos_protos.h>
#include <clib/exec_protos.h>
#include <clib/intuition_protos.h>
#include <clib/alib_protos.h>

/****************************************************************************/

BOOL GenerateText(void)
  {
  int    i;
  char   **stringpointer;
  struct WindowNode    *windownode;
  struct TagNode       *tagnode;
  struct StringNode    *stringnode;
  struct StringNode    *labelnode;
  struct NewGadgetNode *newgadgetnode;
  struct AvailableTags *availabletags;
  struct MenuNode      *menunode;
  struct ItemNode      *itemnode;
  struct SubNode       *subnode;
  FILE   *fp;
  BOOL   st=TRUE;

  DEBUGENTER("GenerateText", NULL);

  strcpy(fullname, filename);
  strcat(fullname, "_tm_text.h");

  if(fp = fopen(fullname, "wb"))
    {
    /***************** Title ******************/

    st = GenerateString("", fp, st, NULL);
    sprintf(string, "/* Text include file generated by %s V%s */", title, version);
    st = GenerateString(string, fp, st, NULL);
    st = GenerateString("", fp, st, NULL);

    /* ARexx text */

    if(arexxflag)
      {
      sprintf(string, "#define AREXXNAME\t\"%s\"", savefilename);
      st = GenerateString(string, fp, st, NULL);
      }

    /* Screen text */

    if(!screennode.Workbench)
      {
      sprintf(string, "#define SCREENTEXT_%s\t\"%s\"", screennode.SourceLabel, screennode.Title);
      st = GenerateString(string, fp, st, NULL);

      for(tagnode = (struct TagNode *) screennode.TagList.lh_Head; tagnode->Node.ln_Succ;
          tagnode = (struct TagNode *) tagnode->Node.ln_Succ)
        {
        switch(AvailableScreenTags[tagnode->Node.ln_Pri].Type)
          {
          case TYPE_STRING:
            sprintf(string, "#define %s_%s\t\"%s\"", AvailableScreenTags[tagnode->Node.ln_Pri].Name, screennode.SourceLabel, tagnode->Data);
            st = GenerateString(string, fp, st, NULL);
            break;
          }
        }
      }

    /* Window text */

    for(windownode = (struct WindowNode *) WindowList.lh_Head; windownode->Node.ln_Succ;
        windownode = (struct WindowNode *) windownode->Node.ln_Succ)
      {
      if(windownode->Title[0] != '\0')
        {
        sprintf(string, "#define WINDOWTEXT_%s\t\"%s\"", windownode->SourceLabel, windownode->Title);
        st = GenerateString(string, fp, st, NULL);
        }

      for(tagnode = (struct TagNode *) windownode->TagList.lh_Head; tagnode->Node.ln_Succ;
          tagnode = (struct TagNode *) tagnode->Node.ln_Succ)
        {
        switch(AvailableWindowTags[tagnode->Node.ln_Pri].Type)
          {
          case TYPE_STRING:
            sprintf(string, "#define %s_%s\t\"%s\"", AvailableWindowTags[tagnode->Node.ln_Pri].Name, windownode->SourceLabel, tagnode->Data);
            st = GenerateString(string, fp, st, NULL);
            break;
          }
        }

      /* Menu text */

      for(menunode = (struct MenuNode *) windownode->MenuList.lh_Head; menunode->Node.ln_Succ;
          menunode = (struct MenuNode *) menunode->Node.ln_Succ)
        {
        sprintf(string, "#define MENUTEXT_%s\t\"%s\"", menunode->SourceLabel, menunode->MenuText);
        st = GenerateString(string, fp, st, NULL);

        for(itemnode = (struct ItemNode *) menunode->ItemList.lh_Head; itemnode->Node.ln_Succ;
            itemnode = (struct ItemNode *) itemnode->Node.ln_Succ)
          {
          if(itemnode->ItemText[0] != '-')
            {
            sprintf(string, "#define ITEMTEXT_%s\t\"%s\"", itemnode->SourceLabel, itemnode->ItemText);
            st = GenerateString(string, fp, st, NULL);
            }

          for(subnode = (struct SubNode *) itemnode->SubList.lh_Head; subnode->Node.ln_Succ;
              subnode = (struct SubNode *) subnode->Node.ln_Succ)
            {
            if(subnode->SubText[0] != '-')
              {
              sprintf(string, "#define SUBTEXT_%s\t\"%s\"", subnode->SourceLabel, subnode->SubText);
              st = GenerateString(string, fp, st, NULL);
              }
            }
          }
        }

      /* Gadget text */

      for(newgadgetnode = (struct NewGadgetNode *) windownode->NewGadgetList.lh_Head; newgadgetnode->Node.ln_Succ;
          newgadgetnode = (struct NewGadgetNode *) newgadgetnode->Node.ln_Succ)
        {
        sprintf(string, "#define GADGETTEXT_%s\t\"%s\"", newgadgetnode->SourceLabel, newgadgetnode->NewGadget.ng_GadgetText);
        st = GenerateString(string, fp, st, NULL);

        switch(newgadgetnode->Kind)
          {
          case BUTTON_KIND:
            availabletags = &AvailableBUGadgetTags[0];
            break;
          case CHECKBOX_KIND:
            availabletags = &AvailableCBGadgetTags[0];
            break;
          case CYCLE_KIND:
            availabletags = &AvailableCYGadgetTags[0];
            break;
          case INTEGER_KIND:
            availabletags = &AvailableINGadgetTags[0];
            break;
          case LISTVIEW_KIND:
            availabletags = &AvailableLVGadgetTags[0];
            break;
          case MX_KIND:
            availabletags = &AvailableMXGadgetTags[0];
            break;
          case NUMBER_KIND:
            availabletags = &AvailableNMGadgetTags[0];
            break;
          case PALETTE_KIND:
            availabletags = &AvailablePAGadgetTags[0];
            break;
          case SCROLLER_KIND:
            availabletags = &AvailableSCGadgetTags[0];
            break;
          case SLIDER_KIND:
            availabletags = &AvailableSLGadgetTags[0];
            break;
          case STRING_KIND:
            availabletags = &AvailableSTGadgetTags[0];
            break;
          case TEXT_KIND:
            availabletags = &AvailableTXGadgetTags[0];
            break;
          }

        for(tagnode = (struct TagNode *) newgadgetnode->TagList.lh_Head; tagnode->Node.ln_Succ;
            tagnode = (struct TagNode *) tagnode->Node.ln_Succ)
          {
          switch(availabletags[tagnode->Node.ln_Pri].Type)
            {
            case TYPE_STRINGLIST:
              stringpointer = (char **) tagnode->Data;
              labelnode = (struct StringNode *) tagnode->SourceLabelList.lh_Head;
              for(i=0; i<tagnode->DataCount; i++)
                {
                sprintf(string, "#define %s_%s_%s\t\"%s\"", availabletags[tagnode->Node.ln_Pri].Name, newgadgetnode->SourceLabel, labelnode->String, *stringpointer);
                st = GenerateString(string, fp, st, NULL);
                stringpointer++;
                labelnode = (struct StringNode *) ((struct Node *) labelnode)->ln_Succ;
                }
              break;

            case TYPE_LINKEDLIST:
              labelnode = (struct StringNode *) tagnode->SourceLabelList.lh_Head;
              for(stringnode = (struct StringNode *) ((struct List *) tagnode->Data)->lh_Head; stringnode->Node.ln_Succ;
                  stringnode = (struct StringNode *) stringnode->Node.ln_Succ)
                {
                sprintf(string, "#define %s_%s_%s\t\"%s\"", availabletags[tagnode->Node.ln_Pri].Name, newgadgetnode->SourceLabel, labelnode->String, stringnode->String);
                st = GenerateString(string, fp, st, NULL);
                labelnode = (struct StringNode *) ((struct Node *) labelnode)->ln_Succ;
                }
              break;

            case TYPE_STRING:
              sprintf(string, "#define %s_%s\t\"%s\"", availabletags[tagnode->Node.ln_Pri].Name, newgadgetnode->SourceLabel, tagnode->Data);
              st = GenerateString(string, fp, st, NULL);
              break;
            }
          }
        }
      }
    st = GenerateString("", fp, st, NULL);

    fclose(fp);

    st = UpdateIcon(fullname, "h", st);

    if(!st)
      {
      DeleteFile(fullname);
      DeleteIcon(fullname);
      }
    }
  else
    {
    st = FALSE;
    }

  DEBUGEXIT(TRUE, (ULONG) st);
  return(st);
  }

#endif
