home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d5xx / d583 / tpledit.lha / TplEdit / src.lzh / savec.c < prev    next >
C/C++ Source or Header  |  1991-12-03  |  30KB  |  1,008 lines

  1.  /*
  2.  * SAVEC.C
  3.  */
  4.  
  5. #include "defs.h"
  6.  
  7. Prototype void SaveProjectAsSrc(char *);
  8. Prototype void DeclareHeaders(FILE *);
  9. Prototype void DeclareNG(FILE *, short);
  10. Prototype void DeclareWIN(FILE *);
  11. Prototype void DeclareBOX(FILE *);
  12. Prototype void DeclareRG(FILE *, TplRegion *);
  13. Prototype void DeclareInitGads(FILE *);
  14. Prototype void DeclareFreeGads(FILE *);
  15. Prototype void DeclareDispFunc(FILE *);
  16. Prototype void DeclareSelectGad(FILE *);
  17. Prototype void DeclareDrawBorders(FILE *);
  18. Prototype void DeclareMain(FILE *);
  19.  
  20. /*****************************************************/
  21.  
  22. const char *NG_Flags[] = {
  23.   "NULL",
  24.   "PLACETEXT_LEFT",
  25.   "PLACETEXT_RIGHT", "",
  26.   "PLACETEXT_ABOVE", "", "", "",
  27.   "PLACETEXT_BELOW", "", "", "", "", "", "", "",
  28.   "PLACETEXT_IN"
  29. };
  30.  
  31. static char *RgtKindAry[] = {
  32.   " GENERIC_KIND",      /*  0 invisible */
  33.   "  BUTTON_KIND",      /*  1 */
  34.   "CHECKBOX_KIND",      /*  2 */
  35.   " INTEGER_KIND",      /*  3 */
  36.   "LISTVIEW_KIND",      /*  4 */
  37.   "      MX_KIND",      /*  5 */
  38.   "  NUMBER_KIND",      /*  6 */
  39.   "   CYCLE_KIND",      /*  7 */
  40.   " PALETTE_KIND",      /*  8 */
  41.   "SCROLLER_KIND",      /*  9 */
  42.   "",                   /*  10 reserved = BEVELBOX  */
  43.   "  SLIDER_KIND",      /*  11*/
  44.   "  STRING_KIND",      /*  12*/
  45.   "    TEXT_KIND"       /*  13*/
  46. };
  47.  
  48. short callback = FALSE;
  49. short exclude  = FALSE;
  50. short border   = FALSE;
  51.  
  52.  
  53. /***********************  SaveProjectAsSrc(name)  ********************/
  54. void
  55. SaveProjectAsSrc(name)
  56.   char  *name;
  57. {
  58.   FILE  *fo;
  59.   TplRegion *rg;
  60.  
  61.  
  62.   char *defname;
  63.   defname = zalloc(strlen(name) + 8);
  64.   strcpy(defname, name);
  65.   char *d = defname;
  66.  
  67.   while (*d++ != '.');
  68.  
  69.   *--d = '\0';
  70.  
  71.   strcat(defname, "_defs.h");
  72.  
  73.   char *maindefname = "defs.h";
  74.  
  75.   if (fo = fopen(name, "r"))
  76.   {
  77.     fclose(fo);
  78.     if (!(do_request( "Sourcefile \"%s\" already exists !",
  79.                       "Replace it|Cancel", name)))
  80.       return;
  81.   }
  82.   if ((fo = fopen(name, "w")) == NULL)
  83.     return;
  84.  
  85.   fprintf(fo,
  86.           "\n"
  87.           "/*\n"
  88.           " * %s\n"
  89.           " *\n"
  90.           " * MACHINE GENERATED\n"
  91.           " * %s %s\n"
  92.           " */\n\n"
  93.           "#include \"%s\"\n\n"
  94.           "Prototype void    FreeGads(void);\n",
  95.           name, __DATE__, __TIME__, (savemain) ? defname : maindefname);
  96.  
  97.   if (savemain)
  98.     fputs("Prototype Gadget *InitGads(Screen *);\n", fo);
  99.   else
  100.     fputs("Prototype int     InitGads(void);\n", fo);
  101.  
  102.   short  numRG = 0;
  103.  
  104.   for (rg = GetHead(&RegList); rg; rg = GetSucc(&rg->rg_Node))
  105.   {
  106.     switch (rg->rg_Type)
  107.     {
  108.     case RGT_BEVELBOX:
  109.       border = TRUE;
  110.  
  111.     case RGT_INVISIBLE:
  112.       break;
  113.  
  114.     case RGT_SLIDER:
  115.       callback = TRUE;
  116.  
  117.     default:
  118.       if (rg->rg_ExclGroup)
  119.         exclude = TRUE;
  120.  
  121.       ++numRG;
  122.       break;
  123.     }
  124.   }
  125.  
  126.   if (exclude)
  127.   {
  128.     fputs("Prototype void    selectgad(Gadget *);\n"
  129.           "Prototype void    DisableExcl(Gadget *);\n"
  130.           "Prototype void    ReleaseExcl(Gadget *);\n", fo);
  131.   }
  132.  
  133.   if (border)
  134.     fputs("Prototype void    drawborders(void);\n", fo);
  135.  
  136.   if (callback)
  137.     fputs("Prototype __geta4 long DispFunc(Gadget *, WORD);\n", fo);
  138.  
  139.   fputs("\n", fo);
  140.  
  141.   for (rg = GetHead(&RegList); rg; rg = GetSucc(&rg->rg_Node))
  142.   {
  143.     if ((rg->rg_Type != RGT_INVISIBLE) &&
  144.         (rg->rg_Type != RGT_BEVELBOX))
  145.       fprintf(fo, "Prototype Gadget *Gad_%s;\n", rg->rg_Node.ln_Name);
  146.     if (rg->rg_Type == RGT_STRING)
  147.       fprintf(fo, "Prototype char   *Buf_%s;\n", rg->rg_Node.ln_Name);
  148.     if (rg->rg_Type == RGT_LISTVIEW ||
  149.         rg->rg_Type == RGT_CYCLE ||
  150.         rg->rg_Type == RGT_MX )
  151.       fprintf(fo, "Prototype List   List_%s;\n", rg->rg_Node.ln_Name);
  152.   }
  153.  
  154.   fprintf(fo, "\nPrototype NewGadget NGAry[%d];\n",  numRG);
  155.   fprintf(fo, "Prototype Gadget    *EGList;\n"
  156.               "Prototype Gadget    *Egadlist[%d];\n", numRG);
  157.   fprintf(fo, "Prototype Window    *EWin;\n"
  158.               "Prototype struct    VisualInfo *VisInfo;\n"
  159.               "Gadget    *Egadlist[%d];\n", numRG);
  160.  
  161.   if (savemain)
  162.     fputs("struct    VisualInfo *VisInfo;\n"
  163.           "Window    *EWin;\n" , fo);
  164.  
  165.  
  166.   DeclareNG(fo, numRG);
  167.  
  168.   for (rg = GetHead(&RegList); rg; rg = GetSucc(&rg->rg_Node))
  169.   {
  170.     if ((rg->rg_Type != RGT_INVISIBLE) &&
  171.         (rg->rg_Type != RGT_BEVELBOX))
  172.       DeclareRG(fo, rg);
  173.   }
  174.   DeclareInitGads(fo);
  175.   if (savemain)
  176.     DeclareMain(fo);
  177.  
  178.   if (callback)
  179.     DeclareDispFunc(fo);
  180.  
  181.   if (exclude)
  182.     DeclareSelectGad(fo);
  183.  
  184.   if (border)
  185.     DeclareDrawBorders(fo);
  186.  
  187.   DeclareFreeGads(fo);
  188.   fclose(fo);
  189.  
  190. /*********************** tpl_defs.h   ******************************************/
  191.  
  192.   FILE  *fo;
  193.   TplRegion *rg;
  194.  
  195.  
  196.   if (fo = fopen(defname, "w"))
  197.   {
  198.     fprintf(fo, "\n"
  199.           "/*\n"
  200.           " * %s\n"
  201.           " *\n"
  202.           " * MACHINE GENERATED\n"
  203.           " * %s %s\n"
  204.           " */\n\n",
  205.           defname, __DATE__, __TIME__);
  206.  
  207.  
  208.     if (savemain)
  209.       DeclareHeaders(fo);
  210.  
  211.     fputs("#define NULPTR    ((void *)0L)\n"
  212.           "#define Prototype extern       /* DICE special */\n", fo);
  213.  
  214.  
  215.     fputs("typedef struct Gadget        Gadget;\n"
  216.           "typedef struct Menu          Menu;\n"
  217.           "typedef struct MenuItem      MenuItem;\n"
  218.           "typedef struct Window        Window;\n"
  219.           "typedef struct Screen        Screen;\n"
  220.           "typedef struct NewGadget     NewGadget;\n"
  221.           "typedef struct NewMenu       NewMenu;\n"
  222.           "typedef struct IntuiMessage  IMsg;\n"
  223.           "typedef struct List          MaxList;\n"
  224.           "typedef struct Node          MaxNode;\n"
  225.           "typedef struct TextFont      TextFont;\n"
  226.           "typedef struct TextAttr      TextAttr;\n"
  227.           "typedef struct MsgPort       MsgPort;\n"
  228.           "typedef struct RastPort      RastPort;\n"
  229.           "typedef struct StringInfo    StringInfo;\n"
  230.           "typedef struct FileRequester FileRequester;\n"
  231.           "typedef struct MinList       List;\n"
  232.           "typedef struct MinNode       Node;\n"
  233.           "\n"
  234.           "typedef unsigned char  ubyte;\n"
  235.           "typedef unsigned short uword;\n"
  236.           "typedef unsigned long  ulong;\n\n", fo);
  237.  
  238.     DeclareWIN(fo);
  239.     DeclareBOX(fo);
  240.  
  241.     short i = 0;
  242.     for (rg = GetHead(&RegList); rg; rg = GetSucc(&rg->rg_Node))
  243.     {
  244.       if ((rg->rg_Type != RGT_INVISIBLE) &&
  245.           (rg->rg_Type != RGT_BEVELBOX))
  246.         fprintf(fo, "#define GAD_%s\t%3d\n", rg->rg_Node.ln_Name, i++);
  247.  
  248.     }
  249.     fprintf(fo, "\n#define ALLGADS\t%3d\n\n", i);
  250.  
  251.     for (i = 0, rg = GetHead(&RegList); rg; rg = GetSucc(&rg->rg_Node))
  252.     {
  253.       if ((rg->rg_Type != RGT_INVISIBLE) &&
  254.           (rg->rg_Type != RGT_BEVELBOX))
  255.         fprintf(fo, "#define NG_%s\t(NGAry + GAD_%s)\n", rg->rg_Node.ln_Name,
  256.                                                          rg->rg_Node.ln_Name);
  257.     }
  258.     fputs("\n", fo);
  259.  
  260.     fclose(fo);
  261.   }
  262. }
  263.  
  264. /***********************  DeclareNG(fo, ******************************/
  265. void
  266. DeclareNG(fo, nrg)
  267.   FILE  *fo;
  268.   short  nrg;
  269. {
  270.   TplRegion *rg;
  271.  
  272.   fprintf(fo,       "\nNewGadget NGAry[%d] = {\n", nrg);
  273.  
  274.   short  i;
  275.   for (i = 0, rg = GetHead(&RegList); rg; rg = GetSucc(&rg->rg_Node))
  276.   {
  277.     if ((rg->rg_Type != RGT_INVISIBLE) &&
  278.         (rg->rg_Type != RGT_BEVELBOX))
  279.     {
  280.       fprintf(fo,   "  {\n"
  281.                     "    GLEF_%d, GTOP_%d, GWID_%d, GHEI_%d,\n",i,i,i,i);
  282.  
  283.       if (rg->rg_LabText)
  284.         fprintf(fo, "    \"%s\",", rg->rg_LabText);
  285.       else
  286.         fprintf(fo, "    NULPTR,");
  287.  
  288.       fputs(        "\t\t/* UBYTE\t*ng_GadgetText\t*/\n", fo);
  289.  
  290.       fputs(        "    NULPTR,\t\t/* struct TextAttr *ng_TextAttr\t*/\n", fo);
  291.  
  292.       if (rg->rg_Node.ln_Name)
  293.         fprintf(fo, "    GAD_%s,", rg->rg_Node.ln_Name);
  294.       else
  295.         fprintf(fo, "    NULPTR,");
  296.  
  297.       fputs(        "\t/* UWORD\tng_GadgetID\t*/\n", fo);
  298.  
  299.       char *highlabel = (rg->rg_SpecFlags & SPEC_HIGHLABEL) ?
  300.                                           " | NG_HIGHLABEL" : "";
  301.  
  302.       char *flags = ((NG_Flags[rg->rg_Flags]) ?
  303.                      NG_Flags[rg->rg_Flags]  :
  304.                      NG_Flags[0]);
  305.  
  306.  
  307.       fprintf(fo,   "    %s%s,\t/* ULONG\tng_Flags\t*/\n", flags, highlabel);
  308.  
  309.       fputs(        "    NULPTR,\t\t/* APTR  \tng_VisualInfo\t*/\n", fo);
  310.       fputs(        "    NULPTR,\t\t/* APTR  \tng_UserData\t*/\n", fo);
  311.  
  312.       fprintf(fo,   "  }%s\n", (GetSucc(&rg->rg_Node) ? "," : ""));
  313.       ++i;
  314.     }
  315.   }
  316.   fputs("};\n", fo);
  317. }
  318.  
  319.  
  320. /***********************  DeclareInitGads(fo) ************************/
  321. void
  322. DeclareInitGads(fo)
  323.   FILE  *fo;
  324. {
  325.   TplRegion *rg;
  326.  
  327.   fputs("\n\nGadget *EGList;\n\n"
  328.         "/* ***** InitGads ***** */\n" , fo);
  329.  
  330.   if (savemain)
  331.     fputs(
  332.         "Gadget *\n"
  333.         "InitGads(Scr)\n"
  334.         "  Screen *Scr;\n", fo);
  335.   else
  336.     fputs(
  337.         "int\n"
  338.         "InitGads(void)\n", fo);
  339.  
  340.   fputs("{\n"
  341.         "  Gadget *gad;\n"
  342.         "  short i;\n\n"
  343.         "  EGList = NULPTR;\n", fo);
  344.  
  345. /*
  346.  * Step 1, allocate VisInfo
  347.  */
  348.   if (savemain)
  349.   fputs("  if ((VisInfo = GetVisualInfo(Scr, TAG_END)) == NULL)\n"
  350.         "    return(NULPTR);\n", fo);
  351.  
  352. /*
  353.  * Step 2, apply to NGAry ng_VisualInfo & ng_TextAttr (from screen)
  354.  */
  355.  
  356.   fputs("\n  NewGadget *ng;\n"
  357.         "  for (i = 0, ng = NGAry;\n"
  358.         "       i < sizeof(NGAry)/sizeof(NGAry[0]);\n"
  359.         "       ++i, ++ng)\n"
  360.         "  {\n"
  361.         "    ng->ng_VisualInfo = VisInfo;\n"
  362.         "    ng->ng_TextAttr   = Scr->Font;\n"
  363.         "  }\n", fo);
  364.  
  365. /*
  366.  * Step 3, allocate EGList
  367.  */
  368.  
  369.   fputs("  if ((gad = CreateContext(&EGList)) == NULL)\n", fo);
  370.   if (savemain)
  371.     fputs(
  372.         "    return(NULPTR);\n\n", fo);
  373.   else
  374.     fputs(
  375.         "    return(NULL);\n\n", fo);
  376.  
  377. /*
  378.  * Step 4, allocate gadgets
  379.  *
  380.  * Allocation is a two pass process.  The first pass deals with those gadgets
  381.  * that do not depend on other gadgets.  The second pass deals with those
  382.  * gadgets that DO depend on other gadgets.
  383.  */
  384.  
  385.   short  i = 0;
  386.  
  387.   for (short j = 0; j < 2; ++j)
  388.   {
  389.     for (rg = GetHead(&RegList); rg; rg = GetSucc(&rg->rg_Node))
  390.     {
  391.       if ((rg->rg_Type != RGT_INVISIBLE) &&
  392.           (rg->rg_Type != RGT_BEVELBOX))
  393.       {
  394.         if (j == 0)
  395.         {
  396.           if (rg->rg_LockedTo && (rg->rg_Type == RGT_LISTVIEW))
  397.             continue;
  398.         }
  399.         else
  400.         {
  401.           if (!(rg->rg_LockedTo && (rg->rg_Type == RGT_LISTVIEW)))
  402.             continue;
  403.         }
  404.  
  405.         char *name  = rg->rg_Node.ln_Name;
  406.  
  407.         fprintf(fo,     "  Gad_%s\t= gad = Egadlist[GAD_%s] =\n"
  408.                         "\tCreateGadget(%s, gad, NG_%s,\n",
  409.                           name, name, RgtKindAry[rg->rg_Type], name);
  410.  
  411.         switch (rg->rg_Type)
  412.         {
  413.         case RGT_STRING:
  414.           if (rg->rg_SpecFlags & SPEC_DISABLED)
  415.             fputs(      "\tGA_Disabled,       TRUE,\n",   fo);
  416.           fprintf(fo,   "\tGTST_MaxChars,     %d,\n",     rg->rg_BufSpace);
  417.           if (rg->rg_DefText[0])
  418.             fprintf(fo, "\tGTST_String,       \"%s\",\n", rg->rg_DefText);
  419.           break;
  420.  
  421.         case RGT_INTEGER:
  422.           if (rg->rg_SpecFlags & SPEC_DISABLED)
  423.             fputs(      "\tGA_Disabled,       TRUE,\n",   fo);
  424.           fprintf(fo,   "\tGTIN_MaxChars,     %d,\n",     rg->rg_BufSpace);
  425.           fprintf(fo,   "\tGTIN_Number,       %d,\n",     rg->rg_DefVal);
  426.           break;
  427.  
  428.         case RGT_SLIDER:
  429.           if (rg->rg_SpecFlags & SPEC_DISABLED)
  430.             fputs(      "\tGA_Disabled,       TRUE,\n",   fo);
  431.           fprintf(fo,   "\tGTSL_Max,          %d,\n",     rg->rg_MaxVal);
  432.           fprintf(fo,   "\tGTSL_Level,        %d,\n",     rg->rg_DefVal);
  433.           fprintf(fo,   "\tGTSL_MaxLevelLen,  %d,\n",    (rg->rg_BufSpace) ?
  434.                                                           rg->rg_BufSpace  : 4);
  435.           fprintf(fo,   "\tGTSL_LevelFormat,  \"%s\",\n",(rg->rg_LabText)   ?
  436.                                                           rg->rg_LabText    : "%4ld");
  437.           fprintf(fo,   "\tGTSL_LevelPlace,   %s,\n",     NG_Flags[rg->rg_Flags]);
  438.           fprintf(fo,   "\tGTSL_DispFunc,     DispFunc,\n");
  439.           fprintf(fo,   "\tPGA_Freedom,       %s,\n",    (rg->rg_SpecFlags  &
  440.                                                           SPEC_VERTICAL)    ?
  441.                                                          "LORIENT_VERT"     :
  442.                                                          "LORIENT_HORIZ");
  443.           break;
  444.  
  445.         case RGT_SCROLLER:
  446.           Box box = rg->rg_Box;
  447.           short x = box.bo_Xe - box.bo_Xs;
  448.           short y = box.bo_Ye - box.bo_Ys;
  449.           x = (x >  7) ?  8 : x;
  450.           y = (y > 15) ? 16 : y;
  451.  
  452.           fprintf(fo,   "\tGTSC_Total,        %d,\n",     rg->rg_MaxVal);
  453.           fprintf(fo,   "\tGTSC_Top,          %d,\n",     rg->rg_DefVal);
  454.           fprintf(fo,   "\tGTSC_Visible,      %d,\n",     rg->rg_BufSpace);
  455.           fprintf(fo,   "\tGTSC_Arrows,       %d,\n",    (rg->rg_SpecFlags   &
  456.                                                           SPEC_VERTICAL) ? y : x);
  457.           fprintf(fo,   "\tPGA_Freedom,       %s,\n",    (rg->rg_SpecFlags   &
  458.                                                           SPEC_VERTICAL)     ?
  459.                                                          "LORIENT_VERT"      :
  460.                                                          "LORIENT_HORIZ");
  461.           if (rg->rg_SpecFlags & SPEC_DISABLED)
  462.             fputs(      "\tGA_Disabled,       TRUE,\n",   fo);
  463.           break;
  464.  
  465.         case RGT_LISTVIEW:
  466.           fprintf(fo,   "\tGTLV_Labels,       &List_%s,\n", name);
  467.           fprintf(fo,   "\tGTLV_Selected,     %d,\n",     rg->rg_DefVal);
  468.           if (rg->rg_LockedTo)
  469.             fprintf(fo, "\tGTLV_ShowSelected, Gad_%s,\n",
  470.                                            rg->rg_LockedTo->rg_Node.ln_Name);
  471.           else
  472.             if (rg->rg_SpecFlags & SPEC_READONLY)
  473.               fputs(    "\tGTLV_ShowSelected, NULL,\n",   fo);
  474.  
  475.           fprintf(fo,   "\tLAYOUTA_Spacing,   %d,\n",     rg->rg_BufSpace);
  476.           break;
  477.  
  478.         case RGT_CYCLE:
  479.           if (rg->rg_SpecFlags & SPEC_DISABLED)
  480.             fputs(      "\tGA_Disabled,       TRUE,\n",   fo);
  481.           fprintf(fo,   "\tGTCY_Labels,       Ary_%s,\n",   name);
  482.           fprintf(fo,   "\tGTCY_Active,       %d,\n",     rg->rg_DefVal);
  483.           break;
  484.  
  485.         case RGT_MX:
  486.           fprintf(fo,   "\tGTMX_Spacing,      %d,\n",     rg->rg_BufSpace);
  487.           fprintf(fo,   "\tGTMX_Labels,       Ary_%s,\n", name);
  488.           fprintf(fo,   "\tGTMX_Active,       %d,\n",     rg->rg_DefVal);
  489.           break;
  490.  
  491.         case RGT_CHECKBOX:
  492.           if (rg->rg_SpecFlags & SPEC_DISABLED)
  493.             fputs(      "\tGA_Disabled,       TRUE,\n",   fo);
  494.           fprintf(fo,   "\tGTCB_Checked,      %d,\n",    (rg->rg_SpecFlags &
  495.                                                           SPEC_CHECKED));
  496.           break;
  497.  
  498.  
  499.         case RGT_NUMBER:
  500.           fprintf(fo,   "\tGTNM_Number,       %d,\n",     rg->rg_DefVal);
  501.           fprintf(fo,   "\tGTNM_Border,       %d,\n",    (rg->rg_SpecFlags &
  502.                                                           SPEC_BORDER));
  503.           break;
  504.  
  505.         case RGT_TEXT:
  506.           if (rg->rg_DefText[0])
  507.           {
  508.             fprintf(fo, "\tGTTX_Text,         \"%s\",\n"
  509.                         "\tGTTX_CopyText,     1,\n",      rg->rg_DefText);
  510.           }
  511.           fprintf(fo,   "\tGTTX_Border,       %d,\n",    (rg->rg_SpecFlags &
  512.                                                           SPEC_BORDER));
  513.           break;
  514.  
  515.         case RGT_PALETTE:
  516.           Box box = rg->rg_Box;
  517.           short x = box.bo_Xe - box.bo_Xs;
  518.           short y = box.bo_Ye - box.bo_Ys;
  519.           x = (x >> 1) - 4;
  520.           if ((y + 4) < x)
  521.             x = y + 4;
  522.  
  523.           short val = (rg->rg_MaxVal > 15) ? 5 :
  524.                       (rg->rg_MaxVal >  7) ? 4 :
  525.                       (rg->rg_MaxVal >  3) ? 3 :
  526.                       (rg->rg_MaxVal >  1) ? 2 : 1;
  527.  
  528.           if (rg->rg_SpecFlags & SPEC_DISABLED)
  529.             fputs(      "\tGA_Disabled,       TRUE,\n",     fo);
  530.           fprintf(fo,   "\tGTPA_Depth,          %d,\n",     val);
  531.           fprintf(fo,   "\tGTPA_Color,          %d,\n",     rg->rg_DefVal);
  532.           fprintf(fo,   "\tGTPA_ColorOffset,    %d,\n",     rg->rg_BufSpace);
  533.           fprintf(fo,   "\tGTPA_IndicatorWidth, %d,\n",     x);
  534.           fprintf(fo,   "\tGTPA_IndicatorHeight,%d,\n",     y);
  535.  
  536.         default:
  537.           break;
  538.  
  539.         }
  540.         fputs(          "\tTAG_END);\n\n"
  541.                         "  if (gad == NULL)\n", fo);
  542.         if (savemain)
  543.           fputs(        "    return(NULPTR);\n\n", fo);
  544.         else
  545.           fputs(        "    return(NULL);\n\n", fo);
  546.  
  547.         switch (rg->rg_Type)
  548.         {
  549.         case RGT_BUTTON:
  550.           if (rg->rg_SpecFlags & SPEC_CHECKED)
  551.             fputs(      "  gad->Flags |= GFLG_SELECTED;\n", fo);
  552.           break;
  553.  
  554.         case RGT_STRING:
  555.           fprintf(fo,   "  Buf_%s\t= ((StringInfo *)gad->SpecialInfo)->Buffer;\n",
  556.                                                             name);
  557.         default:
  558.           break;
  559.         }
  560.  
  561.         fprintf(fo,     "  (long)gad->UserData = %s;\n",    RgtKindAry[rg->rg_Type]);
  562.         if (rg->rg_ExclGroup)
  563.           fprintf(fo,   "  gad->MutualExclude  = 0x%X;\n",  rg->rg_ExclGroup);
  564.  
  565.         fputs(          "\n", fo);
  566.         ++i;
  567.       }
  568.     }
  569.   }
  570.  
  571. /*
  572.  * return EGList
  573.  */
  574.  
  575.   fputs(                "  return(EGList);\n}\n", fo);
  576. }
  577.  
  578. /***********************  DeclareDrawBorders(FILE **********************/
  579. void
  580. DeclareDrawBorders(FILE *fo)
  581. {
  582.   TplRegion *rg;
  583.  
  584.   fputs("\n/* ***** Do BevelBorders ***** */\n"
  585.         "void\n"
  586.         "drawborders(void)\n"
  587.         "{\n", fo);
  588.  
  589.   short i = 0;
  590.   for (rg = GetHead(&RegList); rg; rg = GetSucc(&rg->rg_Node))
  591.   {
  592.     if (rg->rg_Type == RGT_BEVELBOX)
  593.     {
  594.       fprintf(fo, "/* *** %s *** */\n",  rg->rg_Node.ln_Name);
  595.       fprintf(fo, "  DrawBevelBox(EWin->RPort, "
  596.                   "BLEF_%d, BTOP_%d, BWID_%d, BHEI_%d,\n", i,i,i,i);
  597.  
  598.       fputs(    "\tGT_VisualInfo, VisInfo,\n", fo);
  599.       if (rg->rg_SpecFlags & SPEC_RECESSED)
  600.         fputs(  "\tGTBB_Recessed, TRUE,\n", fo);
  601.       fputs(    "\tTAG_END);\n", fo);
  602.       ++i;
  603.     }
  604.   }
  605.   fputs("}\n", fo);
  606. }
  607.  
  608.  
  609. /***********************  DeclareRG(fo, ******************************/
  610. void
  611. DeclareRG(fo, rg)
  612.   FILE  *fo;
  613.   TplRegion *rg;
  614. {
  615.   char *name = rg->rg_Node.ln_Name;
  616.  
  617.   fprintf(fo,           "\nGadget *Gad_%s;\n", name);
  618.  
  619.   switch (rg->rg_Type)
  620.   {
  621.   case RGT_STRING:
  622.     fprintf(  fo,       "char   *Buf_%s;\n", name);
  623.     break;
  624.  
  625.   case RGT_LISTVIEW:
  626.     {
  627.       MaxNode *mn;
  628.       short  i;
  629.       short  cnt;
  630.  
  631.       for (cnt = 0, mn = GetHead(&rg->rg_ListSet); mn; ++cnt, mn = GetSucc(mn)) ;
  632.  
  633.       if (cnt)
  634.       {
  635.         fprintf(fo,     "struct Node NodeAry_%s[%d] = {\n", name, cnt);
  636.  
  637.         for (i = 0, mn = GetHead(&rg->rg_ListSet); mn; ++i, mn = GetSucc(mn))
  638.         {
  639.           fprintf(fo,   "  {\n");
  640.  
  641.           if (GetSucc(mn))
  642.             fprintf(fo, "    NodeAry_%s + %d,\n", name, i + 1);
  643.           else
  644.             fprintf(fo, "    (struct Node *)&List_%s.mlh_Tail,\n", name);
  645.  
  646.           if (GetPred(mn))
  647.             fprintf(fo, "    NodeAry_%s + %d,\n", name, i - 1);
  648.           else
  649.             fprintf(fo, "    (struct Node *)&List_%s.mlh_Head,\n", name);
  650.  
  651.           fprintf(fo,   "    0, 0,\n"
  652.                         "    \"%s\"\n"
  653.                         "  }%s\n", mn->ln_Name, (i == cnt - 1) ? "" : ",");
  654.         }
  655.  
  656.         fprintf(fo,     "};\n\n");
  657.       }
  658.  
  659.       fprintf(fo,       "List List_%s = {\n", name);
  660.  
  661.       if (cnt)
  662.         fprintf(fo,     "  &NodeAry_%s[0],\n", name);
  663.       else
  664.         fprintf(fo,     "  (struct MinNode *)&List_%s.mlh_Tail,\n", name);
  665.  
  666.       fprintf(fo,       "  NULPTR,\n");
  667.  
  668.       if (cnt)
  669.         fprintf(fo,     "  &NodeAry_%s[%d]\n", name, cnt - 1);
  670.       else
  671.         fprintf(fo,     "  (struct MinNode *)&List_%s.mlh_Head\n", name);
  672.  
  673.       fputs(            "};\n", fo);
  674.     }
  675.     break;
  676.  
  677.   case RGT_CYCLE:
  678.   case RGT_MX:
  679.     short  i = 0;
  680.  
  681.     fprintf(fo,         "char   *StatAry_%s[] = {", name);
  682.     if (rg->rg_CycleAry)
  683.     {
  684.       char  *ptr;
  685.  
  686.       for (; ptr = rg->rg_CycleAry[i]; ++i)
  687.         fprintf(fo,     "\n  \"%s\",", ptr);
  688.     }
  689.     fprintf(fo,         "\n  NULPTR\n};\n");
  690.     fprintf(fo,         "char **Ary_%s\t= StatAry_%s;\n", name, name);
  691.  
  692.     break;
  693.  
  694.   default:
  695.     break;
  696.   }
  697. }
  698.  
  699.  
  700. /***********************  DeclareFreeGads(fo) ************************/
  701. void
  702. DeclareFreeGads(fo)
  703.   FILE  *fo;
  704. {
  705.   fputs("\n/* ***** Free the Gadlist and return ***** */\n"
  706.         "void\n"
  707.         "FreeGads(void)\n"
  708.         "{\n"
  709.         "  FreeGadgets(EGList);\n", fo);
  710.  
  711.   if (savemain)
  712.     fputs(
  713.         "  if (VisInfo)\n"
  714.         "    FreeVisualInfo(VisInfo);\n"
  715.         "  VisInfo = NULL;\n", fo);
  716.  
  717.   fputs("  EGList  = NULL;\n"
  718.         "}\n", fo);
  719. }
  720.  
  721.  
  722. /***********************  DeclareHeaders(fo)  ************************/
  723. void
  724. DeclareHeaders(fo)
  725.   FILE  *fo;
  726. {
  727.   fputs("#include <exec/types.h>\n"
  728.         "#include <exec/lists.h>\n"
  729.         "#include <intuition/intuition.h>\n"
  730.         "#include <intuition/screens.h>\n"
  731.         "#include <intuition/gadgetclass.h>\n"
  732.         "#include <libraries/gadtools.h>\n"
  733.         "#include <clib/gadtools_protos.h>\n"
  734.         "\n", fo);
  735. }
  736.  
  737.  
  738. /***********************  DeclareBOX(fo)  ****************************/
  739. void
  740. DeclareBOX(fo)
  741.   FILE  *fo;
  742. {
  743.   short bboxes = FALSE;
  744.   TplRegion *rg;
  745.   short  i;
  746.  
  747.   for (i = 0, rg = GetHead(&RegList); rg; rg = GetSucc(&rg->rg_Node))
  748.   {
  749.     if (rg->rg_Type == RGT_BEVELBOX)
  750.     {
  751.       bboxes = TRUE;
  752.       continue;
  753.     }
  754.  
  755.     if (rg->rg_Type != RGT_INVISIBLE)
  756.     {
  757.       fprintf(fo, "/* *** Gadget %s *** */\n", rg->rg_Node.ln_Name);
  758.       fprintf(fo, "#define GLEF_%d\t%3d\n",   i,rg->rg_Box.bo_Xs);
  759.       fprintf(fo, "#define GTOP_%d\t%3d\n",   i,rg->rg_Box.bo_Ys);
  760.       fprintf(fo, "#define GWID_%d\t%3d\n",   i,rg->rg_Box.bo_Xe -
  761.                                                rg->rg_Box.bo_Xs + 1);
  762.       fprintf(fo, "#define GHEI_%d\t%3d\n\n", i,rg->rg_Box.bo_Ye -
  763.                                                rg->rg_Box.bo_Ys + 1);
  764.       ++i;
  765.     }
  766.   }
  767.   fputs("\n", fo);
  768.  
  769.   if (bboxes)
  770.   {
  771.     for (i = 0, rg = GetHead(&RegList); rg; rg = GetSucc(&rg->rg_Node))
  772.     {
  773.       if (rg->rg_Type == RGT_BEVELBOX)
  774.       {
  775.         fprintf(fo, "/* *** BevelBox %s *** */\n",  rg->rg_Node.ln_Name);
  776.         fprintf(fo, "#define BLEF_%d\t%3d\n",   i,rg->rg_Box.bo_Xs);
  777.         fprintf(fo, "#define BTOP_%d\t%3d\n",   i,rg->rg_Box.bo_Ys);
  778.         fprintf(fo, "#define BWID_%d\t%3d\n",   i,rg->rg_Box.bo_Xe -
  779.                                                   rg->rg_Box.bo_Xs + 1);
  780.         fprintf(fo, "#define BHEI_%d\t%3d\n\n", i,rg->rg_Box.bo_Ye -
  781.                                                   rg->rg_Box.bo_Ys + 1);
  782.         ++i;
  783.       }
  784.     }
  785.     fputs("\n", fo);
  786.   }
  787. }
  788.  
  789. /***********************  DeclareWIN(fo)  ****************************/
  790. void
  791. DeclareWIN(fo)
  792.   FILE  *fo;
  793. {
  794.   fprintf(fo, "/* *** Window pos & size *** */\n");
  795.   fprintf(fo, "#define WLEF\t%3d\n", Ph.ph_WinDim.bo_Xs);
  796.   fprintf(fo, "#define WTOP\t%3d\n", Ph.ph_WinDim.bo_Ys);
  797.   fprintf(fo, "#define WWID\t%3d\n", Ph.ph_WinDim.bo_Xe - Ph.ph_WinDim.bo_Xs);
  798.   fprintf(fo, "#define WHEI\t%3d\n", Ph.ph_WinDim.bo_Ye - Ph.ph_WinDim.bo_Ys);
  799.   fprintf(fo, "\n");
  800. }
  801.  
  802. /***********************  DeclareMain(fo) ****************************/
  803. void
  804. DeclareMain(fo)
  805.   FILE  *fo;
  806. {
  807.   fputs("/*  main function to test gadgets  */\n"
  808.         "int\n"
  809.         "main()\n"
  810.         "{\n"
  811.         "  Screen *Scr;\n"
  812.         "  Gadget *gadgets;\n"
  813.         "\n"
  814.         "  if (Scr = (Screen *)LockPubScreen(NULL))\n"
  815.         "  {\n"
  816.         "    if (gadgets = InitGads(Scr))\n"
  817.         "    {\n"
  818.         "      EWin = (Window *)OpenWindowTags(NULL,\n"
  819.         "             WA_Flags,     WFLG_DRAGBAR|\n"
  820.         "                           WFLG_DEPTHGADGET|\n"
  821.         "                           WFLG_CLOSEGADGET|\n"
  822.         "                           WFLG_SIZEGADGET|\n"
  823.         "                           WFLG_SMART_REFRESH|\n"
  824.         "                           WFLG_REPORTMOUSE|\n"
  825.         "                           WFLG_ACTIVATE,\n"
  826.         "\n"
  827.         "             WA_IDCMP,     LISTVIEWIDCMP|\n"
  828.         "                           IDCMP_NEWSIZE|\n"
  829.         "                           IDCMP_MENUPICK|\n"
  830.         "                           IDCMP_VANILLAKEY|\n"
  831.         "                           IDCMP_RAWKEY|\n"
  832.         "                           IDCMP_CHANGEWINDOW|\n"
  833.         "                           IDCMP_CLOSEWINDOW|\n"
  834.         "                           IDCMP_REFRESHWINDOW,\n"
  835.         "\n"
  836.         "             WA_Left,      WLEF,\n"
  837.         "             WA_Top,       WTOP,\n"
  838.         "             WA_Width,     WWID,\n"
  839.         "             WA_Height,    WHEI,\n"
  840.         "             WA_Title,     \"TPLTEST\",\n"
  841.         "             WA_MinWidth,  32,\n"
  842.         "             WA_MinHeight, 32,\n"
  843.         "             WA_MaxWidth,  -1,\n"
  844.         "             WA_MaxHeight, -1,\n"
  845.         "             WA_AutoAdjust, 1,\n"
  846.         "             WA_PubScreen, NULL,\n"
  847.         "             WA_PubScreenFallBack, 1,\n"
  848.         "             WA_Gadgets,   gadgets,\n"
  849.         "             TAG_END );\n"
  850.         "\n"
  851.         "\n"
  852.         "      if (EWin)\n"
  853.         "      {\n", fo);
  854.  
  855.   if (border)
  856.   fputs("        drawborders();\n", fo);
  857.  
  858.   fputs("        IMsg *im;\n"
  859.         "        short notDone = 1;\n"
  860.         "\n"
  861.         "        GT_RefreshWindow(EWin, NULL);\n"
  862.         "        while (notDone)\n"
  863.         "        {\n"
  864.         "          WaitPort(EWin->UserPort);\n"
  865.         "          while (im = GT_GetIMsg(EWin->UserPort))\n"
  866.         "          {\n"
  867.         "            switch (im->Class)\n"
  868.         "            {\n"
  869.         "            case IDCMP_CLOSEWINDOW:\n"
  870.         "              notDone = 0;\n"
  871.         "              break;\n"
  872.         "            case IDCMP_REFRESHWINDOW:\n"
  873.         "              GT_BeginRefresh(EWin);\n"
  874.         "              GT_EndRefresh(EWin, TRUE);\n"
  875.         "              break;\n", fo);
  876.  
  877.   if (exclude)
  878.   fputs("            case IDCMP_GADGETDOWN:\n"
  879.         "            case IDCMP_GADGETUP:\n"
  880.         "              Gadget *gad = im->IAddress;\n"
  881.         "              if (gad->MutualExclude)\n"
  882.         "              {\n"
  883.         "                switch((long)gad->UserData)\n"
  884.         "                {\n"
  885.         "                case  CHECKBOX_KIND:\n"
  886.         "                  if (gad->Flags & GFLG_SELECTED)\n"
  887.         "                    DisableExcl(gad);\n"
  888.         "                  break;\n"
  889.         "                case  BUTTON_KIND:\n"
  890.         "                  selectgad(gad);\n"
  891.         "                  break;\n"
  892.         "                default:\n"
  893.         "                  DisableExcl(gad);\n"
  894.         "                  break;\n"
  895.         "                }\n"
  896.         "              }\n", fo);
  897.  
  898.   fputs("            default:\n"
  899.         "              break;\n"
  900.         "            }\n"
  901.         "            GT_ReplyIMsg(im);\n"
  902.         "          }\n"
  903.         "        }\n"
  904.         "        CloseWindow(EWin);\n"
  905.         "      }\n"
  906.         "    }\n"
  907.         "    FreeGads();\n"
  908.         "  }\n"
  909.         "  return (0);\n"
  910.         "}\n"
  911.         "\n", fo);
  912.  
  913. }
  914. /***********************  DeclareDispFunc(FILE  **********************/
  915. void
  916. DeclareDispFunc(FILE *fo)
  917. {
  918.   fputs("\n/* CallBack function to Sliders & Scrollers */\n"
  919.         "__geta4 long\n"
  920.         "DispFunc(Gadget *gad, WORD Level)\n"
  921.         "{\n  return((long)Level);\n}\n", fo);
  922. }
  923.  
  924. /***********************  DeclareSelectGad(FILE **********************/
  925. void
  926. DeclareSelectGad(FILE *fo)
  927. {
  928.   fputs("\n/* Mutual Exclusion for Buttons & Boxes */\n"
  929.         "void\n"
  930.         "ReleaseExcl(Gadget *gadget)\n"
  931.         "{\n"
  932.         "  for (short i = 0; i < ALLGADS; ++i)\n"
  933.         "  {\n"
  934.         "    Gadget *gad  = Egadlist[i];\n"
  935.         "    if (gad != gadget)\n"
  936.         "    {\n"
  937.         "      if (gadget->MutualExclude & gad->MutualExclude)\n"
  938.         "        GT_SetGadgetAttrs(gad, EWin, NULL,\n"
  939.         "                          GA_Disabled, 0,\n"
  940.         "                          TAG_END);\n"
  941.         "    }\n"
  942.         "  }\n"
  943.         "}\n"
  944.         "\n"
  945.         "\n"
  946.         "void\n"
  947.         "DisableExcl(Gadget *gadget)\n"
  948.         "{\n"
  949.         "  for (short i = 0; i < ALLGADS; ++i)\n"
  950.         "  {\n"
  951.         "    Gadget *gad  = Egadlist[i];\n"
  952.         "    if (gad == gadget)\n"
  953.         "      continue;\n"
  954.         "    if (gadget->MutualExclude & gad->MutualExclude)\n"
  955.         "    {\n"
  956.         "      switch((long)gad->UserData)\n"
  957.         "      {\n"
  958.         "      case BUTTON_KIND:\n"
  959.         "      case CHECKBOX_KIND:\n"
  960.         "        if (gad->Flags & GFLG_SELECTED)\n"
  961.         "        {\n"
  962.         "          short gad_pos;\n"
  963.         "          gad_pos     = RemoveGadget(EWin, gad);\n"
  964.         "          gad->Flags -= GFLG_SELECTED;\n"
  965.         "          AddGadget(EWin, gad, gad_pos);\n"
  966.         "          RefreshGList(gad, EWin, NULL, 1);\n"
  967.         "        }\n"
  968.         "        break;\n"
  969.         "\n"
  970.         "      default:\n"
  971.         "        GT_SetGadgetAttrs(gad, EWin, NULL,\n"
  972.         "                          GA_Disabled, 1,\n"
  973.         "                          TAG_END);\n"
  974.         "        break;\n"
  975.         "      }\n"
  976.         "    }\n"
  977.         "  }\n"
  978.         "}\n"
  979.         "\n"
  980.         "\n"
  981.         "void\n"
  982.         "selectgad(Gadget *gadget)\n"
  983.         "{\n"
  984.         "  short release = FALSE;\n"
  985.         "  short gad_pos = RemoveGadget(EWin, gadget);\n"
  986.         "  if (gadget->Flags & GFLG_SELECTED)\n"
  987.         "  {\n"
  988.         "    gadget->Flags -= GFLG_SELECTED;\n"
  989.         "    release = TRUE;\n"
  990.         "  }\n"
  991.         "  else\n"
  992.         "    gadget->Flags |= GFLG_SELECTED;\n"
  993.         "\n"
  994.         "  AddGadget(EWin, gadget, gad_pos);\n"
  995.         "  RefreshGList(gadget, EWin, NULL, 1);\n"
  996.         "\n"
  997.         "  if (gadget->MutualExclude)\n"
  998.         "  {\n"
  999.         "    if (release)\n"
  1000.         "      ReleaseExcl(gadget);\n"
  1001.         "    else\n"
  1002.         "      DisableExcl(gadget);\n"
  1003.         "  }\n"
  1004.         "}\n\n\n", fo);
  1005. }
  1006.  
  1007.  
  1008.