home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d8xx / d832 / term.lha / Term / term-3.1-Source.lha / termFastMacros.c < prev    next >
C/C++ Source or Header  |  1993-01-04  |  12KB  |  589 lines

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