home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 2: PC / frozenfish_august_1995.bin / bbs / d09xx / d0928.lha / TeXFormat / Non-MUI / TeXFormat.c < prev    next >
C/C++ Source or Header  |  1993-10-07  |  37KB  |  844 lines

  1. /***************************************************************************
  2.  *                                                                         *
  3.  *  Filename : TeXFormat.c                                                 *
  4.  *                                                                         *
  5.  *  Description : A front-end for the PasTeX TeX implementation.           *
  6.  *                The program looks for the format files found in the path *
  7.  *                TeX:Formats and enables the user to switch between the   *
  8.  *                available format files via a gadtools interface.         *
  9.  *                                                                         *
  10.  *  Return Codes :  10 - No format files available.                        *
  11.  *                  20 - Couldn't open window.                             *
  12.  *                  25 - Couldn't get visualinfo of the public screen.     *
  13.  *                  30 - Couldn't create gadget context.                   *
  14.  *                  40 - Couldn't create gadtools gadgets.                 *
  15.  *                                                                         *
  16.  ***************************************************************************
  17.  *                                                                         *
  18.  *                         Modification History                            *
  19.  *                                                                         *
  20.  *  Date      Author       Comments                                        *
  21.  * ----------------------------------------------------------------------  *
  22.  *  9.3.92    R.B÷di       Created.                                        *
  23.  *  8.6.92    R.B÷di       Fixed bug (first format entry couldn't be       *
  24.  *                         selected.)                                      *
  25.  * 12.9.93    R.B÷di       Added parameters for CLI and WB.                *
  26.  * 14.9.93    R.B÷di       Added 'Save' button.                            *
  27.  *                                                                         *
  28.  ***************************************************************************
  29.  *                                                                         *
  30.  * Copyright ⌐ 1992 Richard B÷di,  All rights reserved.                    *
  31.  *                                                                         *
  32.  ***************************************************************************/
  33.  
  34. /*----------------------------- INCLUDES ----------------------------------*/
  35.  
  36. #include "string.h"
  37. #include "stdio.h"
  38. #include "stdlib.h"
  39. #include "dos.h"
  40.  
  41. #define INTUITION_IOBSOLETE_H
  42.  
  43. #include "dos/dosasl.h"
  44. #include "graphics/displayinfo.h"
  45. #include "intuition/intuition.h"
  46. #include "intuition/screens.h"
  47. #include "intuition/gadgetclass.h"
  48. #include "libraries/gadtools.h"
  49. #include "workbench/workbench.h"
  50. #include "workbench/startup.h"
  51. #include "utility/tagitem.h"
  52.  
  53. #include "clib/exec_protos.h"
  54. #include "clib/dos_protos.h"
  55. #include "clib/graphics_protos.h"
  56. #include "clib/intuition_protos.h"
  57. #include "clib/gadtools_protos.h"
  58. #include "clib/wb_protos.h"
  59. #include "clib/icon_protos.h"
  60. #include "clib/utility_protos.h"
  61.  
  62. #include "pragmas/exec_pragmas.h"
  63. #include "pragmas/dos_pragmas.h"
  64. #include "pragmas/graphics_pragmas.h"
  65. #include "pragmas/intuition_pragmas.h"
  66. #include "pragmas/gadtools_pragmas.h"
  67. #include "pragmas/wb_pragmas.h"
  68. #include "pragmas/icon_pragmas.h"
  69. #include "pragmas/utility_pragmas.h"
  70.  
  71.  
  72. /*--------------------------- DEFINES -------------------------------------*/
  73.  
  74. #define  VERSION        " V2.00 "
  75.  
  76. #define  COMMENTLENGTH         82
  77. #define  CMDSTRLENGTH         512
  78.  
  79. #define  ID_FORMAT_SEL          1
  80. #define  ID_SAVE                2
  81. #define  ID_OK                  3
  82.  
  83. #define  max(a,b)    ((a) > (b) ? (a) : (b))
  84.  
  85. /*-------------------- STRUCTURE DEFINITIONS ------------------------------*/
  86.  
  87. enum ScanError  { SCAN_OK, NO_LOCK, NO_FORMAT, NO_ENV, NO_MATCH, NO_MEMORY };
  88. enum Keywords   { KW_PUBSCREEN, KW_CURRENTFORMAT, KW_FORMATPATH, KW_PATTERN, LAST_KW };
  89. enum ErrorIDs   { ERR_NOERROR, ERR_FORMATFILES = 10, ERR_OPENWINDOW = 20,
  90.                   ERR_NOVISUALINFO = 25, ERR_GADGETCONTEXT = 30, ERR_NOGADTOOLS = 40 };
  91.  
  92. struct FormatNode
  93.    {
  94.    struct FormatNode   *NextNode;
  95.    char                 Name[FNSIZE];
  96.    char                 Comment[COMMENTLENGTH];
  97.    int                  ID;
  98.    };
  99.  
  100. struct TeXFormat
  101.    {
  102.    struct FormatNode   *FirstNode;
  103.    char               **FormatArray;
  104.    struct FormatNode   *CurrentFormat;
  105.    int                  NoOfFormats;
  106.    int                  MaxNameLength;
  107.    int                  MaxCommentLength;
  108.    };
  109.  
  110. struct Settings
  111.    {
  112.    long     ArgPtr[LAST_KW];
  113.    char     ArgStr[LAST_KW][FMSIZE];              // FMSIZE see file dos.h
  114.    char     CurrentFormat[FNSIZE];
  115.    };
  116.  
  117. /*--------------------- FUNCTION PROTOTYPES -------------------------------*/
  118.  
  119. void              ParseShell (int, char **, struct Settings *);
  120. void              ParseWB (struct WBStartup *, struct Settings *);
  121. enum ScanError    ScanFormatPath (struct TeXFormat *, struct Settings *);
  122. void              SetWindowLimits (struct TeXFormat *, enum ScanError);
  123. BOOL              PrepareGadgets (struct TeXFormat *);
  124. void              DrawGraphics (enum ScanError, struct Settings *, struct TeXFormat *);
  125. void              HandleIDCMP (struct TeXFormat *, struct Settings *);
  126. void              Cleanup (char *, enum ErrorIDs);
  127.  
  128.  
  129. /*------------------------ EXTERNAL VARIABLES ----------------------------*/
  130.  
  131. extern struct Library         *SysBase;
  132. extern struct DOSBase         *DOSBase;
  133. extern struct GfxBase         *GfxBase;
  134. extern struct IntuitionBase   *IntuitionBase;
  135. extern struct GadToolsBase    *GadToolsBase;
  136. extern struct WorkbenchBase   *WorkbenchBase;
  137. extern struct IconBase        *IconBase;
  138. extern struct UtilityBase     *UtilityBase;
  139.  
  140.  
  141. /*-------------------------- GLOBAL DATA ----------------------------------*/
  142.  
  143. char  Version[]   = "$VER: TeXFormat  Release " VERSION " (" __DATE__ ")         ⌐ R.B÷di";
  144. char  Title[]     = "TeXFormat " VERSION "   ⌐ R.B÷di";
  145. char  *Keywords[] = { "PUBSCREEN", "CURRENTFORMAT", "FORMATPATH", "PATTERN" };
  146. char  *Switchs[]  = { "/K", "/K", "/K", "/K" };
  147.  
  148. struct Screen  *PubScreen;
  149. struct Window  *Window = NULL;
  150. struct Gadget  *ContextGad = NULL;
  151. void           *VisInfo = NULL;
  152.  
  153. UWORD           WinWidth, WinHeight;
  154.  
  155. STRPTR          ErrorMsg[] = { "",
  156.                                "Couldn't find directory",
  157.                                "Couldn't find a TeX format file in ",
  158.                                "Environment variable unset",
  159.                                "Couldn't find TeX format file defined by the environment variable",
  160.                                "Not enough memory available" };
  161.  
  162. struct TagItem    Window_TagList[] =
  163.         { { WA_Left, 100 },
  164.           { WA_Top, 100 },
  165.           { WA_Width, 30 },
  166.           { WA_Height, 30 },
  167.           { WA_IDCMP, IDCMP_CLOSEWINDOW | IDCMP_GADGETUP | IDCMP_IDCMPUPDATE | IDCMP_MENUPICK |
  168.                       MXIDCMP | CHECKBOXIDCMP | INTEGERIDCMP | BUTTONIDCMP },
  169.           { WA_Flags, WFLG_CLOSEGADGET | WFLG_DRAGBAR | WFLG_SMART_REFRESH | WFLG_ACTIVATE },
  170.           { WA_Title, (ULONG)Title },
  171.           { WA_PubScreenName, NULL },
  172.           { WA_PubScreenFallBack, TRUE },
  173.           { WA_MaxHeight, 400 },
  174.           { WA_MaxWidth, 500 },
  175.           { TAG_END, NULL } };
  176.  
  177. struct TagItem
  178.    Visual_TagList[] = { { TAG_END, NULL } };
  179.  
  180. struct EasyStruct
  181.    AboutReq   = { sizeof (struct EasyStruct), 0, "About",
  182.                   "\n            TeXFormat %s         \n\n     A Format File Selector for TeX \n\n        (c) 1993 by Richard B÷di\n\n Mathematisches Institut\n UniversitΣt Tⁿbingen\n Auf der Morgenstelle 10\n 72076 Tⁿbingen\n Germany\n\ne-mail :\n mmisa01@mailserv.zdv.uni-tuebingen.de\n",
  183.                   "OK" };
  184.  
  185. struct NewGadget
  186.    NG_FormatSel   = { 24, 44, 120, 27, NULL, NULL, ID_FORMAT_SEL,
  187.                       PLACETEXT_RIGHT, NULL, NULL },
  188.  
  189.    NG_Save        = { 0, 0, 60, 16, "Save", NULL, ID_SAVE,
  190.                       PLACETEXT_IN, NULL, NULL },
  191.  
  192.    NG_OK          = { 0, 0, 60, 16, "OK", NULL, ID_OK,
  193.                       PLACETEXT_IN, NULL, NULL };
  194.  
  195. struct IntuiText
  196.    Path_Text      = { 1, 0, JAM2, 35, 40, NULL, NULL, NULL },
  197.    NoFormat_Text  = { 1, 0, JAM2, 35, 25, NULL, NULL, &Path_Text },
  198.    Descript_Text  = { 1, 0, JAM2, 0, 27, NULL, "Description", NULL },
  199.    FormatSel_Text = { 1, 0, JAM2, 48, 27, NULL, "TeX Format ", &Descript_Text },
  200.    Comment_Text   = { 1, 0, JAM2, 0, 35, NULL, NULL, NULL };
  201.  
  202.  
  203. struct NewMenu NewMenu[] = { NM_TITLE, "File", NULL, 0, 0, NULL,
  204.                              NM_ITEM, "About",   NULL, 0, 0, NULL,
  205.                              NM_ITEM, NM_BARLABEL,  NULL, 0, 0, NULL,
  206.                              NM_ITEM, "Quit",    "Q",  0, 0, NULL,
  207.                              NM_END };
  208.  
  209. struct Menu   *Menu = NULL;
  210.  
  211.  
  212. /*------------------------------- CODE ------------------------------------*/
  213.  
  214. main (int argc, char *argv[])
  215.  
  216. {
  217. struct TeXFormat  TeXFormat;
  218. struct Settings   Settings;
  219. enum ScanError    FormatAvail;
  220. struct TagItem   *Tag = NULL;
  221.  
  222.  
  223. if (argc == 0) ParseWB ((struct WBStartup *)argv, &Settings);
  224. if (argc > 1)  ParseShell (argc, argv, &Settings);
  225.  
  226. FormatAvail   = ScanFormatPath (&TeXFormat, &Settings);
  227. Tag           = FindTagItem (WA_PubScreenName, Window_TagList);
  228. Tag->ti_Data  = (ULONG)Settings.ArgStr[KW_PUBSCREEN];
  229.  
  230. if (Window = OpenWindowTagList (NULL, Window_TagList))
  231.    {
  232.    PubScreen = Window->WScreen;
  233.    SetWindowLimits (&TeXFormat, FormatAvail);
  234.    Delay (25);
  235.  
  236.    if (VisInfo = GetVisualInfoA (PubScreen, Visual_TagList))
  237.       {
  238.       if (Menu = CreateMenus (NewMenu, TAG_DONE))
  239.          if (LayoutMenus (Menu, VisInfo, TAG_DONE))
  240.             SetMenuStrip (Window, Menu);
  241.  
  242.       if (CreateContext (&ContextGad))
  243.          {
  244.          if (FormatAvail == SCAN_OK)
  245.             {
  246.             if (PrepareGadgets (&TeXFormat))
  247.                {
  248.                DrawGraphics (FormatAvail, &Settings, &TeXFormat);
  249.                HandleIDCMP (&TeXFormat, &Settings);
  250.                Cleanup (NULL, ERR_NOERROR);
  251.                }
  252.             else
  253.                Cleanup ("Couldn't create gadtools gadgets.\n", ERR_NOGADTOOLS);
  254.             }
  255.          else
  256.             {
  257.             DrawGraphics (FormatAvail, &Settings, &TeXFormat);
  258.             HandleIDCMP (&TeXFormat, &Settings);
  259.             Cleanup (NULL, ERR_FORMATFILES);
  260.             }
  261.          }
  262.       else
  263.          Cleanup ("Couldn't create gadget context.\n", ERR_GADGETCONTEXT);
  264.       }
  265.    else
  266.       Cleanup ("Couldn't get visualinfo of the public screen.\n", ERR_NOVISUALINFO);
  267.    }
  268. else
  269.    Cleanup ("Couldn't open window.\n", ERR_OPENWINDOW);
  270. }
  271.  
  272.  
  273. /***************************************************************************
  274.  *                                                                         *
  275.  *  Function name : ParseWB                                                *
  276.  *                                                                         *
  277.  *  Description : Fetches and parses the ToolTypes from the TeXFormat icon.*
  278.  *                Allowed tooltypes are :                                  *
  279.  *                   PUBSCREEN (Specifies the public screen on which the   *
  280.  *                              program's window should open.              *
  281.  *                   CURRENTFORMAT (Specifies the name of the environment  *
  282.  *                                  variable in which the name of the      *
  283.  *                                  current TeX format file is stored.)    *
  284.  *                   FORMATPATH (Specifies the path for the TeX            *
  285.  *                               format files is stored.)                  *
  286.  *                   PATTERN (Specifies the file pattern for fromat files. *
  287.  *                            Typically it's #?.fmt.)                      *
  288.  *                                                                         *
  289.  ***************************************************************************
  290.  *                                                                         *
  291.  *  Synopsis : ParseWB (WBArgStr, Settings)                                *
  292.  *                                                                         *
  293.  *  Parameters :                                                           *
  294.  *    (struct WBStartup **) WBArgStr:                                      *
  295.  *          The WB argument string.                                        *
  296.  *    (struct Settings *) Settings:                                        *
  297.  *          The tags of theses structure are filled by this procedure.     *
  298.  *                                                                         *
  299.  *  Return value : None                                                    *
  300.  *                                                                         *
  301.  ***************************************************************************/
  302.  
  303. void  ParseWB (struct WBStartup *WBArgStr, struct Settings *Settings)
  304.  
  305. {
  306. struct DiskObject   *DiskObj;
  307. struct WBArg        *WBArg;
  308. char               **ToolArray;
  309.  
  310.  
  311. WBArg = WBArgStr->sm_ArgList;
  312.  
  313. if (WBArg->wa_Name)     // get tooltypes
  314.    if (DiskObj = GetDiskObject (WBArg->wa_Name))
  315.       {
  316.       ToolArray = (char **)DiskObj->do_ToolTypes;
  317.       stpcpy (Settings->ArgStr[KW_PUBSCREEN], FindToolType (ToolArray, Keywords[KW_PUBSCREEN]));
  318.       stpcpy (Settings->ArgStr[KW_CURRENTFORMAT], FindToolType (ToolArray, Keywords[KW_CURRENTFORMAT]));
  319.       stpcpy (Settings->ArgStr[KW_FORMATPATH], FindToolType (ToolArray, Keywords[KW_FORMATPATH]));
  320.       stpcpy (Settings->ArgStr[KW_PATTERN], FindToolType (ToolArray, Keywords[KW_PATTERN]));
  321.       FreeDiskObject (DiskObj);
  322.       }
  323.  
  324. GetVar (Settings->ArgStr[KW_CURRENTFORMAT], Settings->CurrentFormat, sizeof (Settings->CurrentFormat), GVF_GLOBAL_ONLY);
  325. }
  326.  
  327.  
  328.  
  329. /***************************************************************************
  330.  *                                                                         *
  331.  *  Function name : ParseShell                                             *
  332.  *                                                                         *
  333.  *  Description : Parses the commandline for parameters.                   *
  334.  *                Allowed parameters are :                                 *
  335.  *                   PUBSCREEN\K (Specifies the public screen on which the *
  336.  *                                program's window should open.            *
  337.  *                   CURRENTFORMAT/K (Specifies the name of the environment*
  338.  *                                    variable in which the name of the    *
  339.  *                                    current TeX format file is stored.   *
  340.  *                   FORMATPATH/K (Specifies the the path for the TeX      *
  341.  *                                 format files is stored.                 *
  342.  *                   PATTERN(K (Specifies the file pattern for fromat files*
  343.  *                              Typically it's #?.fmt.)                    *
  344.  *                                                                         *
  345.  ***************************************************************************
  346.  *                                                                         *
  347.  *  Synopsis : ParseShell (NoOfArgs, Arg, Settings)                        *
  348.  *                                                                         *
  349.  *  Parameters :                                                           *
  350.  *    (int) NoOfArgs:                                                      *
  351.  *          The number of arguments.                                       *
  352.  *    (char **) Arg:                                                       *
  353.  *          The shell argument string.                                     *
  354.  *    (struct Settings *) Settings:                                        *
  355.  *          The tags of theses structure are filled by this procedure.     *
  356.  *                                                                         *
  357.  *  Return value : None                                                    *
  358.  *                                                                         *
  359.  ***************************************************************************/
  360.  
  361. void  ParseShell (int NoOfArgs, char **Arg, struct Settings *Settings)
  362.  
  363. {
  364. int            kword;
  365. struct RDArgs *RDArgs;
  366. char           Template[CMDSTRLENGTH];
  367.  
  368.  
  369. for (kword = 0, Template[0] = 0; kword < LAST_KW; kword++)
  370.    {
  371.    if (Template[0]) strcat (Template, ",");
  372.    strcat (Template, Keywords[kword]);
  373.    strcat (Template, Switchs[kword]);
  374.    }
  375.  
  376. if (RDArgs = ReadArgs (Template, Settings->ArgPtr, NULL))
  377.    {
  378.    for (kword = 0; kword < LAST_KW; kword++)
  379.       if (Settings->ArgPtr[kword])
  380.          stpcpy (Settings->ArgStr[kword], (char *)Settings->ArgPtr[kword]);
  381.    }
  382. else
  383.    PrintFault (IoErr (), "Error reading arguments");
  384.  
  385. GetVar (Settings->ArgStr[KW_CURRENTFORMAT], Settings->CurrentFormat, sizeof (Settings->CurrentFormat), GVF_GLOBAL_ONLY);
  386.  
  387. FreeArgs (RDArgs);
  388. }
  389.  
  390.  
  391. /***************************************************************************
  392.  *                                                                         *
  393.  *  Function name : ScanFormatPath                                         *
  394.  *                                                                         *
  395.  *  Description : Scans the format path for TeX format files, creates      *
  396.  *                and fills for each file a struct FormatNode, updates     *
  397.  *                the TeXFormat structure and reads the Settings->Texformat*
  398.  *                environment variable.                                    *
  399.  *                                                                         *
  400.  ***************************************************************************
  401.  *                                                                         *
  402.  *  Synopsis : ScanFormatPath (TeXFormat, Settings)                        *
  403.  *                                                                         *
  404.  *  Parameters :                                                           *
  405.  *    (struct TeXFormat *) TeXFormat:                                      *
  406.  *          This structure will be filled with the names of the format     *
  407.  *          files found.                                                   *
  408.  *    (struct Settings *) Settings:                                        *
  409.  *          Contains the path and extension of the format files and the    *
  410.  *          name of the TEXFORMAT environment variable.                    *
  411.  *                                                                         *
  412.  *  Return value : (enum ScanError)                                        *
  413.  *    SCAN_OK, if at least one TeX format file was found and the content   *
  414.  *             this variable matches a scanned TeX format file.            *
  415.  *    NO_LOCK, if path couldn't be locked for examination.                 *
  416.  *  NO_FORMAT, if no TeX format file was found.                            *
  417.  *     NO_ENV, if one of the environment variables is unset.               *
  418.  *   NO_MATCH, if content of the CURRENTFORMAT environment variable        *
  419.  *             matches no scanned format file.                             *
  420.  *  NO_MEMORY, if no memory for the FormatNodes was available.             *
  421.  *                                                                         *
  422.  ***************************************************************************/
  423.  
  424. enum ScanError    ScanFormatPath (struct TeXFormat *TeXFormat, struct Settings *Settings)
  425.  
  426. {
  427. int                  Index;
  428. BPTR                 OldLock, NewLock;
  429. BOOL                 ActFormatFound = FALSE, Match, Error = FALSE;
  430. struct AnchorPath    APath;
  431. struct FormatNode   *NewNode, *LastNode = NULL;
  432. char                 FormatName[FNSIZE];
  433.  
  434.  
  435. APath.ap_BreakBits          = 0;
  436. APath.ap_Strlen             = 0;
  437. TeXFormat->FirstNode        = NULL;
  438.  
  439. if ((strlen (Settings->CurrentFormat) != 0) && (strlen (Settings->ArgStr[KW_FORMATPATH]) != 0))
  440.    {
  441.    if (NewLock = Lock (Settings->ArgStr[KW_FORMATPATH], ACCESS_READ))
  442.       {
  443.       OldLock = CurrentDir (NewLock);
  444.  
  445.       for (Index = 0, Match = MatchFirst (Settings->ArgStr[KW_PATTERN], &APath); !Match; Match = MatchNext (&APath), Index++)
  446.          {
  447.          SplitName (APath.ap_Info.fib_FileName, '.', FormatName, 0, sizeof (FormatName));
  448.          if (strlen (FormatName) == 0) Index--;
  449.          else
  450.             {
  451.             if (NewNode = (struct FormatNode *) calloc (sizeof (struct FormatNode), 1))
  452.                {
  453.                if (TeXFormat->FirstNode == NULL) TeXFormat->FirstNode = NewNode;
  454.                else                              LastNode->NextNode   = NewNode;
  455.  
  456.                sprintf (NewNode->Name, "%-s", FormatName);
  457.                stpcpy (NewNode->Comment, APath.ap_Info.fib_Comment);
  458.                LastNode = NewNode;
  459.  
  460.                if (!strcmpi (Settings->CurrentFormat, FormatName))
  461.                   {
  462.                   TeXFormat->CurrentFormat  = NewNode;
  463.                   ActFormatFound            = TRUE;
  464.                   }
  465.                }
  466.             else
  467.                {
  468.                Error = TRUE;
  469.                break;
  470.                }
  471.             }
  472.          }
  473.  
  474.       MatchEnd (&APath);
  475.       CurrentDir (OldLock);
  476.       UnLock (NewLock);
  477.  
  478.       TeXFormat->NoOfFormats  = Index;
  479.  
  480.       if (Error)                    return (NO_MEMORY);
  481.       if (!TeXFormat->NoOfFormats)  return (NO_FORMAT);
  482.       if (!ActFormatFound)          return (NO_MATCH);
  483.  
  484.       TeXFormat->FormatArray  = (char **)calloc (TeXFormat->NoOfFormats + 1, sizeof (TeXFormat->FormatArray[0]));
  485.  
  486.       if (TeXFormat->FormatArray)
  487.          {
  488.          for (Index = 0, NewNode = TeXFormat->FirstNode; Index < TeXFormat->NoOfFormats; Index++, NewNode = NewNode->NextNode)
  489.             TeXFormat->FormatArray[Index]  = NewNode->Name;
  490.  
  491.          strsrt (TeXFormat->FormatArray, TeXFormat->NoOfFormats);
  492.          TeXFormat->FormatArray[TeXFormat->NoOfFormats]  = NULL;
  493.  
  494.          for (NewNode = TeXFormat->FirstNode; NewNode != NULL; NewNode = NewNode->NextNode)
  495.             for (Index = 0; Index < TeXFormat->NoOfFormats; Index++)
  496.                if (TeXFormat->FormatArray[Index] == NewNode->Name)
  497.                   NewNode->ID = Index;
  498.          }
  499.       else
  500.          return (NO_MEMORY);
  501.       }
  502.    else
  503.       return (NO_LOCK);
  504.    }
  505. else
  506.    return (NO_ENV);
  507.  
  508. return (SCAN_OK);
  509. }
  510.  
  511.  
  512.  
  513. /***************************************************************************
  514.  *                                                                         *
  515.  *  Function name : SetWindowLimits                                        *
  516.  *                                                                         *
  517.  *  Description : Sets the size of the window corresponding to the number  *
  518.  *                and length of the TeX format file entries.               *
  519.  *                                                                         *
  520.  ***************************************************************************
  521.  *                                                                         *
  522.  *  Synopsis : SetWindowLimits (TeXFormat, Mode)                           *
  523.  *                                                                         *
  524.  *  Parameters :                                                           *
  525.  *    (struct TeXFormat *) TeXFormat:                                      *
  526.  *          This structure contains the names of the TeX formats available.*
  527.  *    (enum ScanError) Mode                                                *
  528.  *       Specifies whether or not a format file was                        *
  529.  *       found in the format path.                                         *
  530.  *                                                                         *
  531.  *  Return value : None                                                    *
  532.  *                                                                         *
  533.  ***************************************************************************/
  534.  
  535. void SetWindowLimits (struct TeXFormat *TeXFormat, enum ScanError Mode)
  536.  
  537. {
  538. struct IntuiText     LongestString;
  539. struct FormatNode   *Node;
  540. int                  MaxLen;
  541.  
  542. const   WidthOffset = 70, HeightOffset = 75, GroupSpacing = 10;
  543.  
  544.  
  545. switch (Mode)
  546.    {
  547.    case SCAN_OK:
  548.       LongestString.IText         = FormatSel_Text.IText;
  549.       TeXFormat->MaxNameLength    = IntuiTextLength (&LongestString);
  550.       LongestString.IText         = Descript_Text.IText;
  551.       TeXFormat->MaxCommentLength = IntuiTextLength (&LongestString);
  552.  
  553.       for (Node = TeXFormat->FirstNode; Node != NULL; Node = Node->NextNode)
  554.          {
  555.          LongestString.IText = Node->Name;
  556.          if (IntuiTextLength (&LongestString) > TeXFormat->MaxNameLength)
  557.             TeXFormat->MaxNameLength = IntuiTextLength (&LongestString);
  558.  
  559.          LongestString.IText = Node->Comment;
  560.          if (IntuiTextLength (&LongestString) > TeXFormat->MaxCommentLength)
  561.             TeXFormat->MaxCommentLength = IntuiTextLength (&LongestString);
  562.          }
  563.  
  564.       TeXFormat->MaxNameLength    += GroupSpacing;
  565.       TeXFormat->MaxCommentLength += GroupSpacing;
  566.       MaxLen = TeXFormat->MaxNameLength + TeXFormat->MaxCommentLength;
  567.       break;
  568.  
  569.    default:
  570.       LongestString.IText = ErrorMsg[Mode];
  571.       MaxLen = IntuiTextLength (&LongestString);
  572.       TeXFormat->NoOfFormats = 0;
  573.       break;
  574.    }
  575.  
  576. LongestString.ITextFont = PubScreen->Font;
  577.  
  578. WinWidth  = MaxLen + WidthOffset;
  579. WinHeight = (PubScreen->Font->ta_YSize + 2) * TeXFormat->NoOfFormats + HeightOffset;
  580.  
  581. SizeWindow (Window, WinWidth - Window->Width, WinHeight - Window->Height);
  582. }
  583.  
  584.  
  585. /***************************************************************************
  586.  *                                                                         *
  587.  *  Function name : PrepareGadgets                                         *
  588.  *                                                                         *
  589.  *  Description : Adds all gadgets to the window.                          *
  590.  *                                                                         *
  591.  ***************************************************************************
  592.  *                                                                         *
  593.  *  Synopsis : PrepareGadgets (TeXFormat)                                  *
  594.  *                                                                         *
  595.  *  Parameters :                                                           *
  596.  *    (struct TeXFormat *) TeXFormat:                                      *
  597.  *          This structure contains the names of the TeX formats available.*
  598.  *                                                                         *
  599.  *  Return value : (BOOL)                                                  *
  600.  *       TRUE,  if creation of gadget succeeded.                           *
  601.  *      FALSE,  otherwise.                                                 *
  602.  *                                                                         *
  603.  ***************************************************************************/
  604.  
  605. BOOL PrepareGadgets (struct TeXFormat *TeXFormat)
  606.  
  607. {
  608. struct Gadget       *NextGadget;
  609.  
  610.  
  611. NextGadget = ContextGad;
  612.  
  613. NG_FormatSel.ng_VisualInfo    = VisInfo;
  614. NG_Save.ng_VisualInfo         = VisInfo;
  615. NG_OK.ng_VisualInfo           = VisInfo;
  616.  
  617. NG_FormatSel.ng_TextAttr      = PubScreen->Font;
  618. NG_Save.ng_TextAttr           = PubScreen->Font;
  619. NG_OK.ng_TextAttr             = PubScreen->Font;
  620.  
  621. NG_OK.ng_LeftEdge    = WinWidth -  NG_OK.ng_Width  - 15;
  622. NG_OK.ng_TopEdge     = WinHeight - NG_OK.ng_Height -  8;
  623. NG_Save.ng_LeftEdge  = 15;
  624. NG_Save.ng_TopEdge   = NG_OK.ng_TopEdge;
  625.  
  626. NextGadget = CreateGadget (MX_KIND, NextGadget, &NG_FormatSel,
  627.                            GTMX_Labels, (ULONG)TeXFormat->FormatArray,
  628.                            GTMX_Spacing, 2,
  629.                            GTMX_Active, TeXFormat->CurrentFormat->ID,
  630.                            TAG_END);
  631.  
  632. NextGadget = CreateGadget (BUTTON_KIND, NextGadget, &NG_Save, TAG_END);
  633. NextGadget = CreateGadget (BUTTON_KIND, NextGadget, &NG_OK, TAG_END);
  634.  
  635. if (NextGadget)
  636.    {
  637.    AddGList (Window, ContextGad, 0, -1, NULL);
  638.    RefreshGadgets (ContextGad, Window, NULL);
  639.    GT_RefreshWindow (Window, NULL);
  640.    return (TRUE);
  641.    }
  642. else
  643.    return (FALSE);
  644. }
  645.  
  646.  
  647. /***************************************************************************
  648.  *                                                                         *
  649.  *  Function name : DrawGraphics                                           *
  650.  *                                                                         *
  651.  *  Description : Adds all additional graphics on the window.              *
  652.  *                                                                         *
  653.  ***************************************************************************
  654.  *                                                                         *
  655.  *  Synopsis : DrawGraphics (Mode, Settings, TeXFormat)                    *
  656.  *                                                                         *
  657.  *  Parameters :                                                           *
  658.  *    (enum ScanError) Mode                                                *
  659.  *       Specifies whether or not a format file was                        *
  660.  *       found in the format path.                                         *
  661.  *    (struct Settings *) Settings:                                        *
  662.  *          Contains the path and extension of the format files and the    *
  663.  *          name of the TEXFORMAT environment variable.                    *
  664.  *    (struct TeXFormat *) TeXFormat:                                      *
  665.  *          This structure contains the names of the TeX formats available.*
  666.  *                                                                         *
  667.  *  Return value : None                                                    *
  668.  *                                                                         *
  669.  ***************************************************************************/
  670.  
  671. void DrawGraphics (enum ScanError Mode, struct Settings *Settings, struct TeXFormat *TeXFormat)
  672.  
  673. {
  674. struct FormatNode   *FNode;
  675.  
  676. const    DeltaX = 15, DeltaY = 28, YOffset = 39, VSpacing = 10;
  677.  
  678.  
  679. switch (Mode)
  680.    {
  681.    case SCAN_OK:
  682.       DrawBevelBox (Window->RPort, DeltaX, YOffset, WinWidth - (DeltaX << 1), WinHeight - DeltaY - YOffset,
  683.                     GT_VisualInfo, (ULONG)VisInfo,
  684.                     GTBB_Recessed, TRUE,
  685.                     TAG_END);
  686.       Descript_Text.LeftEdge = FormatSel_Text.LeftEdge + max (TeXFormat->MaxNameLength, IntuiTextLength (&Descript_Text));
  687.       PrintIText (Window->RPort, &FormatSel_Text, 0, 0);
  688.  
  689.       Comment_Text.LeftEdge  = Descript_Text.LeftEdge;
  690.  
  691.       for (FNode = TeXFormat->FirstNode; FNode != NULL; FNode = FNode->NextNode)
  692.          {
  693.          Comment_Text.TopEdge += VSpacing;
  694.          Comment_Text.IText    = FNode->Comment;
  695.          PrintIText (Window->RPort, &Comment_Text, 0, 0);
  696.          }
  697.       break;
  698.  
  699.    default:
  700.       NoFormat_Text.IText = ErrorMsg[Mode];
  701.       if (Mode != NO_ENV) Path_Text.IText = Settings->ArgStr[KW_FORMATPATH];
  702.       else Path_Text.IText = NULL;
  703.       PrintIText (Window->RPort, &NoFormat_Text, 0, 0);
  704.       break;
  705.    }
  706. }
  707.  
  708.  
  709. /***************************************************************************
  710.  *                                                                         *
  711.  *  Function name : HandleIDCMP                                            *
  712.  *                                                                         *
  713.  *  Description : Tests IDCMP port for incoming messages.                  *
  714.  *                This function returns if the window close gaget or the   *
  715.  *                CANCEL gadget was activated.                             *
  716.  *                                                                         *
  717.  ***************************************************************************
  718.  *                                                                         *
  719.  *  Synopsis : HandleIDCMP (TeXFormat, Settings)                           *
  720.  *                                                                         *
  721.  *  Parameters :                                                           *
  722.  *    (struct TeXFormat *) TeXFormat:                                      *
  723.  *          This structure contains the names of the TeX formats available.*
  724.  *    (struct Settings *) Settings:                                        *
  725.  *          Contains the path and extension of the format files and the    *
  726.  *          name of the TEXFORMAT environment variable.                    *
  727.  *                                                                         *
  728.  *  Return value : None                                                    *
  729.  *                                                                         *
  730.  ***************************************************************************/
  731.  
  732. void HandleIDCMP (struct TeXFormat *TeXFormat, struct Settings *Settings)
  733.  
  734. {
  735. struct FormatNode   *FNode;
  736. struct IntuiMessage *Msg;
  737. struct Gadget       *Gadget;
  738. ULONG                IClass, IDCMPFlag = NULL;
  739. UWORD                Code;
  740. USHORT               MenuItem;
  741. int                  GadID;
  742. char                 CMDStrg[128];
  743. BOOL                 Exit = FALSE;
  744.  
  745.  
  746. while (!Exit)
  747.    {
  748.    if(!(Msg = GT_GetIMsg (Window->UserPort)))
  749.       {
  750.       WaitPort (Window->UserPort);
  751.       continue;
  752.       }
  753.  
  754.    IClass = Msg->Class;
  755.    Code   = Msg->Code;
  756.    GT_ReplyIMsg (Msg);
  757.  
  758.    switch (IClass)
  759.       {
  760.       case IDCMP_GADGETUP:
  761.       case IDCMP_GADGETDOWN:
  762.          Gadget = (struct Gadget *) Msg->IAddress;
  763.          GadID  = Gadget->GadgetID;
  764.  
  765.          switch (GadID)
  766.             {
  767.             case ID_FORMAT_SEL:
  768.                for (FNode = TeXFormat->FirstNode; FNode != NULL; FNode = FNode->NextNode)
  769.                   if (FNode->ID == Code) break;
  770.                TeXFormat->CurrentFormat = FNode;
  771.                SetVar (Settings->ArgStr[KW_CURRENTFORMAT], FNode->Name, strlen (FNode->Name), GVF_GLOBAL_ONLY);
  772.                break;
  773.  
  774.             case ID_SAVE:
  775.                sprintf (CMDStrg, "copy env:%s envarc:%s", Settings->ArgStr[KW_CURRENTFORMAT], Settings->ArgStr[KW_CURRENTFORMAT]);
  776.                system (CMDStrg);
  777.                break;
  778.  
  779.             case ID_OK:
  780.                Exit = TRUE;
  781.                break;
  782.             }
  783.          break;
  784.  
  785.       case IDCMP_MENUPICK:
  786.          MenuItem = ITEMNUM (Code);
  787.          switch (MenuItem)
  788.             {
  789.             case 0:
  790.                EasyRequest (Window, &AboutReq, &IDCMPFlag, VERSION);
  791.                break;
  792.             case 2:
  793.                Exit = TRUE;
  794.                break;
  795.             }
  796.          break;
  797.  
  798.       case IDCMP_CLOSEWINDOW:
  799.          Exit = TRUE;
  800.          break;
  801.       }
  802.    }
  803. }
  804.  
  805.  
  806. /***************************************************************************
  807.  *                                                                         *
  808.  *  Function name : Cleanup                                                *
  809.  *                                                                         *
  810.  *  Description : Frees all allocated memory, closes all used libraries,   *
  811.  *                closes all screens and windows.                          *
  812.  *                Possibly prints some error message and exits.            *
  813.  *                                                                         *
  814.  ***************************************************************************
  815.  *                                                                         *
  816.  *  Synopsis : Cleanup (Strg, ErrorID)                                     *
  817.  *                                                                         *
  818.  *  Parameters :                                                           *
  819.  *    (char *) Strg:                                                       *
  820.  *       Pointer to a message string.                                      *
  821.  *    (enum ErrorIDs) ErrorID:                                             *
  822.  *       Error code identifier.                                            *
  823.  *                                                                         *
  824.  ***************************************************************************/
  825.  
  826. void Cleanup (char *Strg, enum ErrorIDs ErrorID)
  827.  
  828. {
  829. if (Strg) puts (Strg);
  830.  
  831. if (Menu)
  832.    {
  833.    if (Window) ClearMenuStrip (Window);
  834.    FreeMenus (Menu);
  835.    }
  836.  
  837. if (Window)        CloseWindow (Window);
  838. if (ContextGad)    FreeGadgets (ContextGad);
  839. if (VisInfo)       FreeVisualInfo (VisInfo);
  840.  
  841. exit (ErrorID);
  842. }
  843.  
  844.