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 >
Wrap
C/C++ Source or Header
|
1993-01-04
|
12KB
|
589 lines
/*
** termFastMacros.c
**
** Fast! macros support routines
**
** Copyright © 1990-1993 by Olaf `Olsen' Barthel & MXM
** All Rights Reserved
*/
#include "termGlobal.h"
STATIC WORD FastWindowLeft = -1,FastWindowTop = -1,FastWindowHeight = -1;
STATIC struct Gadget *GadgetList = NULL;
STATIC struct IBox Dims = { -1,-1,-1,-1 };
/* RefreshFastWindow(WORD FastWindowHeight):
*
* Refresh the contents of the fast! macro window.
*/
VOID
RefreshFastWindow(WORD FastWindowHeight)
{
if(FastWindow)
{
struct Gadget *Gadget;
struct NewGadget NewGadget;
WORD TopEdge = Window -> WScreen -> WBorTop + Window -> WScreen -> Font -> ta_YSize + 1;
memset(&NewGadget,0,sizeof(struct NewGadget));
if(GadgetList)
{
RemoveGList(FastWindow,GadgetList,-1);
SetAPen(FastWindow -> RPort,0);
RectFill(FastWindow -> RPort,FastWindow -> BorderRight,FastWindow -> BorderTop,FastWindow -> Width - (FastWindow -> BorderLeft + 1),FastWindow -> Height - (FastWindow -> BorderBottom + 1));
FreeGadgets(GadgetList);
GadgetList = NULL;
}
SZ_SizeSetup(Window -> WScreen,&UserFont,TRUE);
if(Gadget = CreateContext(&GadgetList))
{
NewGadget . ng_Width = SZ_Width(LISTVIEW_KIND,NULL,19,NULL);
NewGadget . ng_Height = FastWindowHeight - (TopEdge + 4 + 8);
NewGadget . ng_GadgetText = "";
NewGadget . ng_GadgetID = 0;
NewGadget . ng_LeftEdge = 6;
NewGadget . ng_TopEdge = 1 + TopEdge;
NewGadget . ng_TextAttr = &UserFont;
NewGadget . ng_VisualInfo = VisualInfo;
FastGadget = Gadget = CreateGadget(LISTVIEW_KIND,Gadget,&NewGadget,
GTLV_Labels, &FastMacroList,
GTLV_ScrollWidth, 2 + 2 * UserFontWidth + 2,
TAG_DONE);
}
if(Gadget)
{
AddGList(FastWindow,GadgetList,(UWORD)-1,(UWORD)-1,NULL);
RefreshGList(GadgetList,FastWindow,NULL,(UWORD)-1);
GT_RefreshWindow(FastWindow,NULL);
}
else
{
FreeGadgets(GadgetList);
GadgetList = NULL;
}
}
}
/* CloseFastWindow():
*
* Close the fast! macro window and free the associated resources.
*/
VOID
CloseFastWindow()
{
CheckItem(MEN_FAST_MACROS_WINDOW,FALSE);
if(FastWindow)
{
FastWindowLeft = FastWindow -> LeftEdge;
FastWindowTop = FastWindow -> TopEdge;
FastWindowHeight = FastWindow -> Height;
ClearMenuStrip(FastWindow);
CloseWindowSafely(FastWindow);
FastWindow = NULL;
}
if(GadgetList)
{
FreeGadgets(GadgetList);
GadgetList = NULL;
}
}
/* OpenFastWindow():
*
* Open the fast! macro window.
*/
BYTE
OpenFastWindow()
{
LONG LocalWidth,LocalHeight;
SZ_SizeSetup(Window -> WScreen,&UserFont,TRUE);
LocalWidth = 6 + SZ_Width(LISTVIEW_KIND,NULL,19,NULL) + 6;
LocalHeight = Window -> WScreen -> WBorTop + Window -> WScreen -> Font -> ta_YSize + 1 + 2 + SZ_Height(LISTVIEW_KIND,10,0) + 2 + 8;
if(FastWindowHeight == -1)
FastWindowHeight = LocalHeight;
if(FastWindowLeft == -1)
FastWindowLeft = Window -> WScreen -> Width - LocalWidth;
if(FastWindowTop == -1)
FastWindowTop = Window -> WScreen -> WBorTop + Window -> WScreen -> Font -> ta_YSize + 1;
if(Dims . Left == -1)
{
Dims . Left = 0;
Dims . Top = 0;
Dims . Width = LocalWidth;
Dims . Height = Window -> WScreen -> WBorTop + Window -> WScreen -> Font -> ta_YSize + 1;
}
if(FastWindow = OpenWindowTags(NULL,
WA_Width, LocalWidth,
WA_Height, FastWindowHeight,
WA_Left, FastWindowLeft,
WA_Top, FastWindowTop,
WA_DragBar, TRUE,
WA_DepthGadget, TRUE,
WA_CloseGadget, TRUE,
WA_RMBTrap, TRUE,
WA_Zoom, &Dims,
WA_NoCareRefresh, TRUE,
WA_SizeGadget, TRUE,
WA_SizeBBottom, TRUE,
WA_MinWidth, LocalWidth,
WA_MaxWidth, LocalWidth,
WA_MinHeight, Window -> WScreen -> WBorTop + Window -> WScreen -> Font -> ta_YSize + 1 + 2 + SZ_Height(LISTVIEW_KIND,1,0) + 2 + 8,
WA_MaxHeight, ~0,
WA_Title, LocaleString(MSG_FASTMACROS_FAST_MACROS_TXT),
WA_NewLookMenus, TRUE,
WA_CustomScreen, Window -> WScreen,
TAG_DONE))
{
FastWindow -> UserPort = Window -> UserPort;
ModifyIDCMP(FastWindow,Window -> IDCMPFlags);
SetMenuStrip(FastWindow,Menu);
FastWindow -> Flags &= ~WFLG_RMBTRAP;
RefreshFastWindow(FastWindowHeight);
CheckItem(MEN_FAST_MACROS_WINDOW,TRUE);
return(TRUE);
}
return(FALSE);
}
/* NewFastMacro(STRPTR Macro,STRPTR Code):
*
* Create a new fast! macro node.
*/
struct MacroNode *
NewFastMacro(STRPTR Macro,STRPTR Code)
{
struct MacroNode *Node;
if(Node = (struct MacroNode *)AllocVec(sizeof(struct MacroNode) + 21 + 257,MEMF_ANY|MEMF_CLEAR))
{
Node -> mn_Macro = (STRPTR)(Node + 1);
Node -> mn_Code = &Node -> mn_Macro[21];
strcpy(Node -> mn_Macro,Macro);
strcpy(Node -> mn_Code ,Code);
return(Node);
}
return(NULL);
}
/* RemFastMacro(struct MacroNode *Node):
*
* Remove and deallocate a fast! macro node.
*/
VOID
RemFastMacro(struct MacroNode *Node)
{
Remove((struct Node *)Node);
FreeVec(Node);
}
/* GetFastMacro(LONG Offset):
*
* Get a fast! macro node from the global list by
* its index number.
*/
struct MacroNode *
GetFastMacro(LONG Offset)
{
struct MacroNode *Node;
LONG i;
Node = (struct MacroNode *)FastMacroList . lh_Head;
for(i = 0 ; i < Offset ; i++)
{
if(!Node -> mn_Succ -> mn_Succ)
return(NULL);
Node = Node -> mn_Succ;
}
return(Node);
}
/* ClearFastMacroList(struct List *List):
*
* Remove all nodes from the global fast! macro list
* and free them on the way.
*/
VOID
ClearFastMacroList(struct List *List)
{
struct Node *Node,*NextNode;
Node = List -> lh_Head;
while(NextNode = Node -> ln_Succ)
{
Remove(Node);
FreeVec(Node);
Node = NextNode;
}
}
/* GetFastMacroOffset(struct MacroNode *MacroNode):
*
* Get the index number of a given node in the
* global fast! macro list.
*/
LONG
GetFastMacroOffset(struct MacroNode *MacroNode)
{
struct MacroNode *Node;
LONG Offset = 0;
Node = (struct MacroNode *)FastMacroList . lh_Head;
while(Node -> mn_Succ)
{
if(Node == MacroNode)
return(Offset);
Offset++;
Node = Node -> mn_Succ;
}
return(~0);
}
/* MoveList(struct List *From,struct List *To):
*
* Move the contents of a list to a different list.
*/
VOID __regargs
MoveList(struct List *From,struct List *To)
{
struct Node *Node,*NextNode;
Node = From -> lh_Head;
while(NextNode = Node -> ln_Succ)
{
Remove(Node);
AddTail(To,Node);
Node = NextNode;
}
}
/* SaveFastMacros(STRPTR Name):
*
* Save the fast! macro list to a file.
*/
BYTE
SaveFastMacros(STRPTR Name)
{
struct IFFHandle *Handle;
BYTE Success = FALSE;
if(Handle = (struct IFFHandle *)AllocIFF())
{
if(Handle -> iff_Stream = Open(Name,MODE_NEWFILE))
{
InitIFFasDOS(Handle);
if(!OpenIFF(Handle,IFFF_WRITE))
{
if(!PushChunk(Handle,ID_TERM,ID_CAT,IFFSIZE_UNKNOWN))
{
if(!PushChunk(Handle,ID_TERM,ID_FORM,IFFSIZE_UNKNOWN))
{
if(!PushChunk(Handle,0,ID_VERS,IFFSIZE_UNKNOWN))
{
struct TermInfo TermInfo;
TermInfo . Version = TermVersion;
TermInfo . Revision = TermRevision;
if(WriteChunkBytes(Handle,&TermInfo,sizeof(struct TermInfo)) == sizeof(struct TermInfo))
{
if(PopChunk(Handle))
Success = FALSE;
else
{
if(!PushChunk(Handle,0,ID_WIND,sizeof(struct IBox)))
{
struct IBox SizeBox;
if(FastWindow)
{
FastWindowLeft = FastWindow -> LeftEdge;
FastWindowTop = FastWindow -> TopEdge;
FastWindowHeight = FastWindow -> Height;
}
if(FastWindowHeight == -1)
SizeBox . Height = Window -> WScreen -> WBorTop + Window -> WScreen -> Font -> ta_YSize + 1 + 2 + SZ_Height(LISTVIEW_KIND,10,0) + 2 + 8;
else
SizeBox . Height = FastWindowHeight;
if(FastWindowLeft == -1)
SizeBox . Left = Window -> WScreen -> Width - (6 + SZ_Width(LISTVIEW_KIND,NULL,19,NULL) + 6);
else
SizeBox . Left = FastWindowLeft;
if(FastWindowTop == -1)
SizeBox . Top = Window -> WScreen -> WBorTop + Window -> WScreen -> Font -> ta_YSize + 1;
else
SizeBox . Top = FastWindowTop;
if(WriteChunkBytes(Handle,&SizeBox,sizeof(struct IBox)) == sizeof(struct IBox))
{
if(PopChunk(Handle))
Success = FALSE;
else
{
struct MacroNode *Node;
Node = (struct MacroNode *)FastMacroList . lh_Head;
while(Node -> mn_Succ)
{
if(!PushChunk(Handle,ID_TERM,ID_FORM,IFFSIZE_UNKNOWN))
{
if(!PushChunk(Handle,0,ID_FAST,IFFSIZE_UNKNOWN))
{
if(WriteChunkBytes(Handle,Node -> mn_Macro,20) != 20)
{
Success = FALSE;
break;
}
else
{
if(WriteChunkBytes(Handle,Node -> mn_Code,256) != 256)
{
Success = FALSE;
break;
}
else
{
if(PopChunk(Handle))
{
Success = FALSE;
break;
}
else
Success = TRUE;
}
}
}
if(Success)
{
if(PopChunk(Handle))
{
Success = FALSE;
break;
}
}
}
Node = Node -> mn_Succ;
}
}
}
}
}
}
}
if(PopChunk(Handle))
Success = FALSE;
}
if(PopChunk(Handle))
Success = FALSE;
}
CloseIFF(Handle);
}
Close(Handle -> iff_Stream);
}
FreeIFF(Handle);
}
if(Success)
SetProtection(Name,FIBF_EXECUTE);
else
DeleteFile(Name);
return(Success);
}
/* LoadFastMacros(STRPTR Name):
*
* Restore the fast! macro list from a file.
*/
BYTE
LoadFastMacros(STRPTR Name)
{
STATIC ULONG Stops[6] =
{
ID_TERM,ID_VERS,
ID_TERM,ID_FAST,
ID_TERM,ID_WIND
};
struct List __aligned NewFastMacroList;
LONG NewFastMacroCount = 0;
struct IFFHandle *Handle;
BYTE Success = FALSE;
struct MacroNode *Node;
struct ContextNode *Chunk;
struct IBox SizeBox;
NewList(&NewFastMacroList);
if(Handle = AllocIFF())
{
if(Handle -> iff_Stream = Open(Name,MODE_OLDFILE))
{
InitIFFasDOS(Handle);
if(!OpenIFF(Handle,IFFF_READ))
{
if(!StopChunks(Handle,Stops,3))
{
while(!ParseIFF(Handle,IFFPARSE_SCAN))
{
Chunk = CurrentChunk(Handle);
if(Chunk -> cn_ID == ID_VERS)
{
struct TermInfo TermInfo;
if(ReadChunkBytes(Handle,&TermInfo,sizeof(struct TermInfo)) == sizeof(struct TermInfo))
{
if((TermInfo . Version > TermVersion) || (TermInfo . Version == TermVersion && TermInfo . Revision > TermRevision) || (TermInfo . Version == 1 && TermInfo . Revision < 6))
break;
}
else
break;
}
if(Chunk -> cn_ID == ID_WIND)
{
if(ReadChunkBytes(Handle,&SizeBox,sizeof(struct IBox)) == sizeof(struct IBox))
{
FastWindowLeft = SizeBox . Left;
FastWindowTop = SizeBox . Top;
FastWindowHeight = SizeBox . Height;
}
else
break;
}
if(Chunk -> cn_ID == ID_FAST)
{
if(Node = NewFastMacro("",""))
{
if(ReadChunkBytes(Handle,Node -> mn_Macro,20) == 20)
{
if(ReadChunkBytes(Handle,Node -> mn_Code,256) == 256)
{
AddTail(&NewFastMacroList,(struct Node *)Node);
NewFastMacroCount++;
Success = TRUE;
}
else
break;
}
else
break;
}
else
break;
}
}
if(Success)
{
if(NewFastMacroList . lh_Head -> ln_Succ)
{
ClearFastMacroList(&FastMacroList);
MoveList(&NewFastMacroList,&FastMacroList);
FastMacroCount = NewFastMacroCount;
}
}
else
ClearFastMacroList(&NewFastMacroList);
}
CloseIFF(Handle);
}
Close(Handle -> iff_Stream);
}
FreeIFF(Handle);
}
return(Success);
}