home *** CD-ROM | disk | FTP | other *** search
/ Amiga Elysian Archive / AmigaElysianArchive.iso / comm / term23_2.lha / Source_Code / termSource / termFastMacros.c < prev    next >
C/C++ Source or Header  |  1992-08-18  |  11KB  |  541 lines

  1. /*
  2. **    $Id: termFastMacros.c,v 1.3 92/08/15 20:13:59 olsen Sta Locker: olsen $
  3. **    $Revision: 1.3 $
  4. **    $Date: 92/08/15 20:13:59 $
  5. **
  6. **    Fast! macros support routines
  7. **
  8. **    Copyright © 1990-1992 by Olaf `Olsen' Barthel & MXM
  9. **        All Rights Reserved
  10. */
  11.  
  12. #include "termGlobal.h"
  13.  
  14. STATIC WORD         Left = -1,Top = -1,Height = -1;
  15. STATIC struct Gadget    *GadgetList = NULL;
  16. STATIC struct IBox     Dims = { -1,-1,-1,-1 };
  17.  
  18. VOID
  19. RefreshFastWindow(WORD Height)
  20. {
  21.     if(FastWindow)
  22.     {
  23.         struct Gadget        *Gadget;
  24.         struct NewGadget     NewGadget;
  25.         WORD             TopEdge = Screen -> WBorTop + Screen -> Font -> ta_YSize + 1;
  26.  
  27.         memset(&NewGadget,0,sizeof(struct NewGadget));
  28.  
  29.         if(GadgetList)
  30.         {
  31.             RemoveGList(FastWindow,GadgetList,-1);
  32.  
  33.             SetAPen(FastWindow -> RPort,0);
  34.  
  35.             RectFill(FastWindow -> RPort,FastWindow -> BorderRight,FastWindow -> BorderTop,FastWindow -> Width - (FastWindow -> BorderLeft + 1),FastWindow -> Height - (FastWindow -> BorderBottom + 1));
  36.  
  37.             FreeGadgets(GadgetList);
  38.  
  39.             GadgetList = NULL;
  40.         }
  41.  
  42.         SZ_SizeSetup(Screen,&UserFont,TRUE);
  43.  
  44.         if(Gadget = CreateContext(&GadgetList))
  45.         {
  46.             NewGadget . ng_Width        = SZ_Width(LISTVIEW_KIND,NULL,19,NULL);
  47.             NewGadget . ng_Height        = Height - (TopEdge + 4 + 8);
  48.             NewGadget . ng_GadgetText    = "";
  49.             NewGadget . ng_GadgetID        = 0;
  50.             NewGadget . ng_LeftEdge        = 6;
  51.             NewGadget . ng_TopEdge        = 1 + TopEdge;
  52.             NewGadget . ng_TextAttr        = &UserFont;
  53.             NewGadget . ng_VisualInfo    = VisualInfo;
  54.     
  55.             FastGadget = Gadget = CreateGadget(LISTVIEW_KIND,Gadget,&NewGadget,
  56.                 GTLV_Labels,        &FastMacroList,
  57.                 GTLV_ScrollWidth,    2 + 2 * UserFontWidth + 2,
  58.             TAG_DONE);
  59.         }
  60.  
  61.         if(Gadget)
  62.         {
  63.             AddGList(FastWindow,GadgetList,(UWORD)-1,(UWORD)-1,NULL);
  64.             RefreshGList(GadgetList,FastWindow,NULL,(UWORD)-1);
  65.             GT_RefreshWindow(FastWindow,NULL);
  66.         }
  67.         else
  68.         {
  69.             FreeGadgets(GadgetList);
  70.  
  71.             GadgetList = NULL;
  72.         }
  73.     }
  74. }
  75.  
  76. VOID
  77. CloseFastWindow()
  78. {
  79.     struct MenuItem    *Item;
  80.  
  81.     if(Item = FindThisItem(MEN_FAST_MACROS_WINDOW))
  82.         Item -> Flags &= ~CHECKED;
  83.  
  84.     if(FastWindow)
  85.     {
  86.         Left    = FastWindow -> LeftEdge;
  87.         Top    = FastWindow -> TopEdge;
  88.         Height    = FastWindow -> Height;
  89.  
  90.         ClearMenuStrip(FastWindow);
  91.         CloseWindowSafely(FastWindow);
  92.  
  93.         FastWindow = NULL;
  94.     }
  95.  
  96.     if(GadgetList)
  97.     {
  98.         FreeGadgets(GadgetList);
  99.  
  100.         GadgetList = NULL;
  101.     }
  102. }
  103.  
  104. BYTE
  105. OpenFastWindow()
  106. {
  107.     LONG LocalWidth,LocalHeight;
  108.  
  109.     SZ_SizeSetup(Screen,&UserFont,TRUE);
  110.  
  111.     LocalWidth    = 6 + SZ_Width(LISTVIEW_KIND,NULL,19,NULL) + 6;
  112.     LocalHeight    = Screen -> WBorTop + Screen -> Font -> ta_YSize + 1 + 2 + SZ_Height(LISTVIEW_KIND,10,0) + 2 + 8;
  113.  
  114.     if(Height == -1)
  115.         Height = LocalHeight;
  116.  
  117.     if(Left == -1)
  118.         Left = Screen -> Width - LocalWidth;
  119.  
  120.     if(Top == -1)
  121.         Top = Screen -> WBorTop + Screen -> Font -> ta_YSize + 1;
  122.  
  123.     if(Dims . Left == -1)
  124.     {
  125.         Dims . Left    = 0;
  126.         Dims . Top    = 0;
  127.         Dims . Width    = LocalWidth;
  128.         Dims . Height    = Screen -> WBorTop + Screen -> Font -> ta_YSize + 1;
  129.     }
  130.  
  131.     if(FastWindow = OpenWindowTags(NULL,
  132.         WA_Width,        LocalWidth,
  133.         WA_Height,        Height,
  134.  
  135.         WA_Left,        Left,
  136.         WA_Top,            Top,
  137.  
  138.         WA_DragBar,        TRUE,
  139.         WA_DepthGadget,        TRUE,
  140.         WA_CloseGadget,        TRUE,
  141.         WA_RMBTrap,        TRUE,
  142.         WA_Zoom,        &Dims,
  143.         WA_NoCareRefresh,    TRUE,
  144.  
  145.         WA_SizeGadget,        TRUE,
  146.         WA_SizeBBottom,        TRUE,
  147.  
  148.         WA_MinWidth,        LocalWidth,
  149.         WA_MaxWidth,        LocalWidth,
  150.  
  151.         WA_MinHeight,        Screen -> WBorTop + Screen -> Font -> ta_YSize + 1 + 2 + SZ_Height(LISTVIEW_KIND,1,0) + 2 + 8,
  152.         WA_MaxHeight,        ~0,
  153.  
  154.         WA_Title,        LocaleString(MSG_FASTMACROS_FAST_MACROS_TXT),
  155.  
  156.         WA_NewLookMenus,    TRUE,
  157.  
  158.         WA_CustomScreen,    Screen,
  159.     TAG_DONE))
  160.     {
  161.         struct MenuItem    *Item;
  162.  
  163.         if(Item = FindThisItem(MEN_FAST_MACROS_WINDOW))
  164.             Item -> Flags |= CHECKED;
  165.  
  166.         FastWindow -> UserPort = Window -> UserPort;
  167.  
  168.         ModifyIDCMP(FastWindow,Window -> IDCMPFlags);
  169.  
  170.         SetMenuStrip(FastWindow,Menu);
  171.  
  172.         FastWindow -> Flags &= ~WFLG_RMBTRAP;
  173.  
  174.         RefreshFastWindow(Height);
  175.  
  176.         return(TRUE);
  177.     }
  178.  
  179.     return(FALSE);
  180. }
  181.  
  182. struct MacroNode *
  183. NewFastMacro(UBYTE *Macro,UBYTE *Code)
  184. {
  185.     struct MacroNode *Node;
  186.  
  187.     if(Node = (struct MacroNode *)AllocVec(sizeof(struct MacroNode) + 21 + 257,MEMF_ANY|MEMF_CLEAR))
  188.     {
  189.         Node -> mn_Macro    = (UBYTE *)(Node + 1);
  190.         Node -> mn_Code        = &Node -> mn_Macro[21];
  191.  
  192.         strcpy(Node -> mn_Macro,Macro);
  193.         strcpy(Node -> mn_Code ,Code);
  194.  
  195.         return(Node);
  196.     }
  197.  
  198.     return(NULL);
  199. }
  200.  
  201. VOID
  202. RemFastMacro(struct MacroNode *Node)
  203. {
  204.     Remove((struct Node *)Node);
  205.  
  206.     FreeVec(Node);
  207. }
  208.  
  209. struct MacroNode *
  210. GetFastMacro(LONG Offset)
  211. {
  212.     struct MacroNode    *Node;
  213.     LONG             i;
  214.  
  215.     Node = (struct MacroNode *)FastMacroList . lh_Head;
  216.  
  217.     for(i = 0 ; i < Offset ; i++)
  218.     {
  219.         if(!Node -> mn_Succ -> mn_Succ)
  220.             return(NULL);
  221.  
  222.         Node = Node -> mn_Succ;
  223.     }
  224.  
  225.     return(Node);
  226. }
  227.  
  228. VOID
  229. ClearFastMacroList(struct List *List)
  230. {
  231.     struct Node *Node,*NextNode;
  232.  
  233.     Node = List -> lh_Head;
  234.  
  235.     while(Node -> ln_Succ)
  236.     {
  237.         NextNode = Node -> ln_Succ;
  238.  
  239.         Remove(Node);
  240.  
  241.         FreeVec(Node);
  242.  
  243.         Node = NextNode;
  244.     }
  245. }
  246.  
  247. LONG
  248. GetFastMacroOffset(struct MacroNode *MacroNode)
  249. {
  250.     struct MacroNode    *Node;
  251.     LONG             Offset = 0;
  252.  
  253.     Node = (struct MacroNode *)FastMacroList . lh_Head;
  254.  
  255.     while(Node -> mn_Succ)
  256.     {
  257.         if(Node == MacroNode)
  258.             return(Offset);
  259.  
  260.         Offset++;
  261.  
  262.         Node = Node -> mn_Succ;
  263.     }
  264.  
  265.     return(~0);
  266. }
  267.  
  268. BYTE
  269. SaveFastMacros(UBYTE *Name)
  270. {
  271.     struct IFFHandle    *Handle;
  272.     BYTE             Success = FALSE;
  273.  
  274.     if(Handle = (struct IFFHandle *)AllocIFF())
  275.     {
  276.         if(Handle -> iff_Stream = Open(Name,MODE_NEWFILE))
  277.         {
  278.             InitIFFasDOS(Handle);
  279.  
  280.             if(!OpenIFF(Handle,IFFF_WRITE))
  281.             {
  282.                 if(!PushChunk(Handle,'TERM',ID_CAT,IFFSIZE_UNKNOWN))
  283.                 {
  284.                     if(!PushChunk(Handle,'TERM',ID_FORM,IFFSIZE_UNKNOWN))
  285.                     {
  286.                         if(!PushChunk(Handle,0,'VERS',IFFSIZE_UNKNOWN))
  287.                         {
  288.                             struct TermInfo TermInfo;
  289.  
  290.                             TermInfo . Version    = TermVersion;
  291.                             TermInfo . Revision    = TermRevision;
  292.  
  293.                             if(WriteChunkBytes(Handle,&TermInfo,sizeof(struct TermInfo)) == sizeof(struct TermInfo))
  294.                             {
  295.                                 if(PopChunk(Handle))
  296.                                     Success = FALSE;
  297.                                 else
  298.                                 {
  299.                                     if(!PushChunk(Handle,0,'WIND',sizeof(struct IBox)))
  300.                                     {
  301.                                         struct IBox SizeBox;
  302.  
  303.                                         if(FastWindow)
  304.                                         {
  305.                                             Left    = FastWindow -> LeftEdge;
  306.                                             Top    = FastWindow -> TopEdge;
  307.                                             Height    = FastWindow -> Height;
  308.                                         }
  309.  
  310.                                         if(Height == -1)
  311.                                             SizeBox . Height = Screen -> WBorTop + Screen -> Font -> ta_YSize + 1 + 2 + SZ_Height(LISTVIEW_KIND,10,0) + 2 + 8;
  312.                                         else
  313.                                             SizeBox . Height = Height;
  314.  
  315.                                         if(Left == -1)
  316.                                             SizeBox . Left = Screen -> Width - (6 + SZ_Width(LISTVIEW_KIND,NULL,19,NULL) + 6);
  317.                                         else
  318.                                             SizeBox . Left = Left;
  319.  
  320.                                         if(Top == -1)
  321.                                             SizeBox . Top = Screen -> WBorTop + Screen -> Font -> ta_YSize + 1;
  322.                                         else
  323.                                             SizeBox . Top = Top;
  324.  
  325.                                         if(WriteChunkBytes(Handle,&SizeBox,sizeof(struct IBox)) == sizeof(struct IBox))
  326.                                         {
  327.                                             if(PopChunk(Handle))
  328.                                                 Success = FALSE;
  329.                                             else
  330.                                             {
  331.                                                 struct MacroNode *Node;
  332.  
  333.                                                 Node = (struct MacroNode *)FastMacroList . lh_Head;
  334.  
  335.                                                 while(Node -> mn_Succ)
  336.                                                 {
  337.                                                     if(!PushChunk(Handle,'TERM',ID_FORM,IFFSIZE_UNKNOWN))
  338.                                                     {
  339.                                                         if(!PushChunk(Handle,0,'FAST',IFFSIZE_UNKNOWN))
  340.                                                         {
  341.                                                             if(WriteChunkBytes(Handle,Node -> mn_Macro,20) != 20)
  342.                                                             {
  343.                                                                 Success = FALSE;
  344.  
  345.                                                                 break;
  346.                                                             }
  347.                                                             else
  348.                                                             {
  349.                                                                 if(WriteChunkBytes(Handle,Node -> mn_Code,256) != 256)
  350.                                                                 {
  351.                                                                     Success = FALSE;
  352.  
  353.                                                                     break;
  354.                                                                 }
  355.                                                                 else
  356.                                                                 {
  357.                                                                     if(PopChunk(Handle))
  358.                                                                     {
  359.                                                                         Success = FALSE;
  360.  
  361.                                                                         break;
  362.                                                                     }
  363.                                                                     else
  364.                                                                         Success = TRUE;
  365.                                                                 }
  366.                                                             }
  367.                                                         }
  368.  
  369.                                                         if(Success)
  370.                                                         {
  371.                                                             if(PopChunk(Handle))
  372.                                                             {
  373.                                                                 Success = FALSE;
  374.  
  375.                                                                 break;
  376.                                                             }
  377.                                                         }
  378.                                                     }
  379.  
  380.                                                     Node = Node -> mn_Succ;
  381.                                                 }
  382.                                             }
  383.                                         }
  384.                                     }
  385.                                 }
  386.                             }
  387.                         }
  388.  
  389.                         if(PopChunk(Handle))
  390.                             Success = FALSE;
  391.                     }
  392.  
  393.                     if(PopChunk(Handle))
  394.                         Success = FALSE;
  395.                 }
  396.  
  397.                 CloseIFF(Handle);
  398.             }
  399.  
  400.             Close(Handle -> iff_Stream);
  401.         }
  402.  
  403.         FreeIFF(Handle);
  404.     }
  405.  
  406.     if(Success)
  407.         SetProtection(Name,FIBF_EXECUTE);
  408.     else
  409.         DeleteFile(Name);
  410.  
  411.     return(Success);
  412. }
  413.  
  414. VOID __regargs
  415. MoveList(struct List *From,struct List *To)
  416. {
  417.     struct Node *Node,*NextNode;
  418.  
  419.     Node = From -> lh_Head;
  420.  
  421.     while(NextNode = Node -> ln_Succ)
  422.     {
  423.         Remove(Node);
  424.  
  425.         AddTail(To,Node);
  426.  
  427.         Node = NextNode;
  428.     }
  429. }
  430.  
  431. BYTE
  432. LoadFastMacros(UBYTE *Name)
  433. {
  434.     STATIC ULONG Stops[6] =
  435.     {
  436.         'TERM','VERS',
  437.         'TERM','FAST',
  438.         'TERM','WIND'
  439.     };
  440.  
  441.     struct List __aligned     NewFastMacroList;
  442.     LONG             NewFastMacroCount = 0;
  443.     struct IFFHandle    *Handle;
  444.     BYTE             Success = FALSE;
  445.     struct MacroNode    *Node;
  446.     struct ContextNode    *Chunk;
  447.     struct IBox         SizeBox;
  448.  
  449.     NewList(&NewFastMacroList);
  450.  
  451.     if(Handle = AllocIFF())
  452.     {
  453.         if(Handle -> iff_Stream = Open(Name,MODE_OLDFILE))
  454.         {
  455.             InitIFFasDOS(Handle);
  456.  
  457.             if(!OpenIFF(Handle,IFFF_READ))
  458.             {
  459.                 if(!StopChunks(Handle,&Stops[0],3))
  460.                 {
  461.                     while(!ParseIFF(Handle,IFFPARSE_SCAN))
  462.                     {
  463.                         Chunk = CurrentChunk(Handle);
  464.  
  465.                         if(Chunk -> cn_ID == 'VERS')
  466.                         {
  467.                             struct TermInfo TermInfo;
  468.  
  469.                             if(ReadChunkBytes(Handle,&TermInfo,sizeof(struct TermInfo)) == sizeof(struct TermInfo))
  470.                             {
  471.                                 if((TermInfo . Version > TermVersion) || (TermInfo . Version == TermVersion && TermInfo . Revision > TermRevision) || (TermInfo . Version == 1 && TermInfo . Revision < 6))
  472.                                     break;
  473.                             }
  474.                             else
  475.                                 break;
  476.                         }
  477.  
  478.                         if(Chunk -> cn_ID == 'WIND')
  479.                         {
  480.                             if(ReadChunkBytes(Handle,&SizeBox,sizeof(struct IBox)) == sizeof(struct IBox))
  481.                             {
  482.                                 Left    = SizeBox . Left;
  483.                                 Top    = SizeBox . Top;
  484.                                 Height    = SizeBox . Height;
  485.                             }
  486.                             else
  487.                                 break;
  488.                         }
  489.  
  490.                         if(Chunk -> cn_ID == 'FAST')
  491.                         {
  492.                             if(Node = NewFastMacro("",""))
  493.                             {
  494.                                 if(ReadChunkBytes(Handle,Node -> mn_Macro,20) == 20)
  495.                                 {
  496.                                     if(ReadChunkBytes(Handle,Node -> mn_Code,256) == 256)
  497.                                     {
  498.                                         AddTail(&NewFastMacroList,(struct Node *)Node);
  499.  
  500.                                         NewFastMacroCount++;
  501.  
  502.                                         Success = TRUE;
  503.                                     }
  504.                                     else
  505.                                         break;
  506.                                 }
  507.                                 else
  508.                                     break;
  509.                             }
  510.                             else
  511.                                 break;
  512.                         }
  513.                     }
  514.  
  515.                     if(Success)
  516.                     {
  517.                         if(NewFastMacroList . lh_Head -> ln_Succ)
  518.                         {
  519.                             ClearFastMacroList(&FastMacroList);
  520.  
  521.                             MoveList(&NewFastMacroList,&FastMacroList);
  522.  
  523.                             FastMacroCount = NewFastMacroCount;
  524.                         }
  525.                     }
  526.                     else
  527.                         ClearFastMacroList(&NewFastMacroList);
  528.                 }
  529.  
  530.                 CloseIFF(Handle);
  531.             }
  532.  
  533.             Close(Handle -> iff_Stream);
  534.         }
  535.  
  536.         FreeIFF(Handle);
  537.     }
  538.  
  539.     return(Success);
  540. }
  541.