a
/* TestText zum Testen des Editors */
a
/*#FOLD: Includes */
a
/************
a
 *
a
 * Includes:
a
 *
a
 ************/
a

a
#include <exec/types.h>
a
#include <intuition/intuition.h>
a
#include "src/Editor.h"
a
/*#ENDFD Includes */
a
/*#FOLD: Defines */
a
/***********
a
 *
a
 * Defines:
a
 *
a
 ***********/
a

a
#define REV 33L
a
#define MAXINPUTLEN 128L
a
/*#ENDFD Defines */
a
/*#FOLD: External Functions */
a
/**********************
a
 *
a
 * External Functions:
a
 *
a
 **********************/
a

a
void Test(),print();
a

a
struct Library *OpenLibrary();
a
struct Window *OpenWindow();
a
void CloseLibrary(),NewList(),AddTail(),CloseWindow(),free();
a
void DeletePort(),ModifyIDCMP(),ReplyMsg(),freeMemoryblock();
a
void initWindowSize(),SetDrMd(),SetAPen(),SetBPen(),printAll();
a
void handleKeys(),Cursor();
a
struct Editor *malloc(),*RemHead();
a
struct MsgPort *CreatePort();
a
struct IntuiMessage *GetMsg();
a
ULONG Wait(),OpenDevice();
a
SHORT RawKeyConvert();
a

a
/*#ENDFD External Functions */
a
/*#FOLD: Global Variables */
a
/*********************
a
 *
a
 * Global Variables:
a
 *
a
 *********************/ 
a

a
struct Library *IntuitionBase = NULL, *GfxBase = NULL, *DosBase = NULL;
a
struct Device *ConsoleDevice = NULL;
a

a
struct EList editorList;
a
struct Editor *actualEditor;
a

a
struct NewWindow newEdWindow =
a
{
a
	100,50,440,156,
a
	AUTOFRONTPEN,AUTOBACKPEN,
a
	REFRESHWINDOW | MOUSEBUTTONS | RAWKEY | CLOSEWINDOW | NEWSIZE,
a
	WINDOWSIZING | WINDOWDRAG | WINDOWDEPTH | WINDOWCLOSE | SIZEBBOTTOM
a
	| SIMPLE_REFRESH,
a
	NULL,NULL,
a
	(UBYTE *)"Editor",
a
	NULL,NULL,
a
	100,50,640,256,
a
	WBENCHSCREEN
a
};
a

a
struct MsgPort *edUserPort = NULL;
a

a
struct IOStdReq ioStdReq;
a

a
UBYTE inputBuffer[MAXINPUTLEN];
a
UWORD inputLen = 0;
a

a
struct InputEvent inputEvent =
a
{
a
	0,
a
	IECLASS_RAWKEY,0,
a
	0,0
a
};
a

a
/*#ENDFD Global Variables */
a
/*#FOLD: Functions */
a
/***************
a
 *					*
a
 * Functions: *
a
 *					*
a
 ***************/
a

a
/*#FOLD: nextLine */
a
/*********************************************
a
 *
a
 * nextLine(line,pnr)
a
 *
a
 * Returns pointer to next line,
a
 * and executes folding
a
 * Zero, if no next line.
a
 *
a
 * line ^ Zline-structure.
a
 * pnr ^ to line number.
a
 *
a
 *********************************************/
a

a
struct Zline *nextLine(line,pnr)
a
register struct Zline *line;
a
register UWORD					 *pnr;
a
{
a
	register struct Zline *z;
a
	register UWORD folding = actualEditor->folding;
a

a
	if (z = line)
a
		if (z->succ)
a
			do
a
			{
a
				if ((z = z->succ)->succ)
a
					*pnr += 1;
a
				else
a
				{
a
					z = NULL;
a
					break;
a
				}
a
			}
a
			while ((z->flags & ZLF_FOLD) > folding);
a
		else
a
			z = NULL;
a

a
	return (z);
a
}
a

a
/*#ENDFD nextLine */
a
/*#FOLD: prevLine */
a
/*******************************************
a
 *
a
 * prevLine(line,pnr)
a
 *
a
 * Returns pointer to previous line,
a
 * and executes folding
a
 * Zero, if no previous line.
a
 *
a
 * line ^ Zline-structure.
a
 * pnr ^ to line number.
a
 *
a
 *******************************************/
a

a
struct Zline *prevLine(line,pnr)
a
register struct Zline *line;
a
register UWORD					 *pnr;
a
{
a
	register struct Zline *z;
a
	register UWORD folding = actualEditor->folding;
a

a
	if (z = line)
a
		if (z->pred)
a
			do
a
			{
a
				if ((z = z->pred)->pred)
a
					*pnr -= 1;
a
				else
a
				{
a
					z = NULL;
a
					break;
a
				}
a
			}
a
			while ((z->flags & ZLF_FOLD) > folding);
a
		else
a
			z = NULL;
a

a
	return (z);
a
}
a

a
/*#ENDFD prevLine */
a
/*#FOLD: OpenEditor */
a
/***********************************
a
 *
a
 * OpenEditor()
a
 *
a
 * Open Editor window and
a
 * initalize Editor structure
a
 *
a
 * Returns pointer to Editor structure
a
 * or is zero in case of an error
a
 *
a
 ***********************************/
a

a
struct Editor *OpenEditor()
a
{
a
	register struct Editor *ed = NULL;
a
	register struct Window *wd;
a
	register struct RastPort *rp;
a
	register ULONG flags;
a
	register UBYTE *ptr;
a
	register struct Zline **zptr;
a
	register UWORD n,*pnr;
a

a
	/* IDCMPFlags saved, sets it to zero in structure
a
		=> use your own UserPort! */
a
	flags = newEdWindow.IDCMPFlags;
a
	newEdWindow.IDCMPFlags = NULL;
a

a
	/* Memory for editor structure: */
a
	if (ed = malloc(sizeof(struct Editor)))
a
		/* Window open: */
a
		if (wd = OpenWindow(&newEdWindow))
a
		{
a
/*#FOLD: Parameter initialization */
a
			ed->window = wd;
a
			ed->rp = (rp = wd->RPort);
a

a
			/* Write mode set: */
a
			SetDrMd(rp,JAM2);
a
			SetAPen(rp,FGPEN);
a
			SetBPen(rp,BGPEN);
a

a
			/* UserPort established: */
a
			wd->UserPort = edUserPort;
a
			ModifyIDCMP(wd,flags);
a

a
			/* Parameter initialization: */
a
			NewList(&(ed->block));
a
			NewList(&(ed->zlines));
a
			ed->num_lines = 0;
a
			ed->actual = NULL;
a
			ed->toppos = 0;
a
			ed->leftpos = 0;
a
			ed->xpos = 1;
a
			ed->ypos = 1;
a
			ed->wdy	= 0;
a
			ed->changed = 0;
a
			ed->insert = 1;
a
			ed->folding = 0;
a

a
			/* zlinesptr/nr-Array initialization: */
a
			for (n = 0, zptr = ed->zlinesptr, pnr = ed->zlinesnr;
a
					n < MAXHEIGHT; n++, zptr++, pnr++)
a
			{
a
				*zptr = NULL;
a
				*pnr	= 1;
a
			}
a

a
			/* Tab initialization: */
a
			ptr = ed->tabstring;
a
			*ptr++ = 1;
a
			for (n = 1; n < MAXWIDTH; n++)
a
				if (n % 3)
a
					*ptr++ = 1;
a
				else
a
					*ptr++ = 0;
a

a
			ed->wch = 0;
a
			initWindowSize(ed);
a
/*#ENDFD Parameter initialization */
a
		}
a
		else
a
		{
a
			free(ed);
a
			ed = NULL;
a
		}
a

a
	newEdWindow.IDCMPFlags = flags;
a
	return (ed);
a
}
a

a
/*#ENDFD OpenEditor */
a
/*#FOLD: CloseEditor */
a
/***************************
a
 *
a
 * CloseEditor(ed)
a
 *
a
 * Close editor window.
a
 *
a
 * ed ^ Editor structure.
a
 *
a
 ***************************/
a

a
void CloseEditor(ed)
a
struct Editor	 *ed;
a
{
a
	register struct Memoryblock *blk;
a

a
	/* Free first memory block: */
a
	while (blk = (struct Memoryblock *)RemHead(&(ed->block)))
a
		freeMemoryblock(blk);
a

a
	/* Then Close window: */
a
	ed->window->UserPort = NULL;
a
	CloseWindow(ed->window);
a
	free(ed);
a
}
a

a
/*#ENDFD CloseEditor */
a
/*#ENDFD Functions */
a
/*#FOLD: Main program */
a
/*****************
a
 *
a
 * Main program:
a
 *
a
 *****************/
a

a
main()
a
{
a
	register struct Editor *ed;
a
	BOOL running = TRUE;
a
	register struct IntuiMessage *imsg;
a
	ULONG signal,class;
a
	UWORD code,qualifier;
a
	APTR iaddress;
a
	register UWORD n = 1;
a

a
	/* Libraries open: */
a
	if ( !(IntuitionBase = OpenLibrary("intuition.library",REV)))
a
		goto Ende;
a
	if ( !(GfxBase = OpenLibrary("graphics.library",REV)))
a
		goto Ende;
a
	if ( !(DosBase = OpenLibrary("dos.library",REV)))
a
		goto Ende;
a
	if ( !(OpenDevice("console.device",-1L,&ioStdReq,0L)))
a
		ConsoleDevice = ioStdReq.io_Device;
a
	else
a
		goto Ende;
a

a
	/* UserPort open */
a
	if (! (edUserPort = CreatePort(NULL,0L))) goto Ende;
a

a
	/* Editor list initialization: */
a
	NewList(&editorList);
a

a
	/* Open first editor window: */
a
	if (ed = OpenEditor())
a
	{
a
		AddTail(&editorList,ed);
a
		actualEditor = ed;
a
	}
a
	else
a
		goto Ende;
a

a
	Test();
a

a
	do
a
	{
a
/*#FOLD: Main loop */
a
		/* Set cursor: */
a
		Cursor();
a

a
		signal = Wait(1L << edUserPort->mp_SigBit);
a

a
		/* erase cursor again: */
a
		Cursor();
a

a
		while (imsg = GetMsg(edUserPort))
a
		{
a
			class			= imsg->Class;
a
			code			= imsg->Code;
a
			qualifier	= imsg->Qualifier;
a
			iaddress		= imsg->IAddress;
a

a
			ReplyMsg(imsg);
a

a
/*#FOLD: Event processing */
a
			/* Event processing: */
a
			switch (class)
a
			{
a
				case RAWKEY:
a
					if (! (code & IECODE_UP_PREFIX))
a
					{
a
						inputEvent.ie_Code		= code;
a
						inputEvent.ie_Qualifier = qualifier;
a
						if ((inputLen = RawKeyConvert(
a
									&inputEvent,inputBuffer,MAXINPUTLEN,NULL)
a
									) >= 0)
a
							handleKeys(inputBuffer,inputLen);
a
					}
a
					break;
a

a
				case NEWSIZE:
a
					initWindowSize(actualEditor);
a

a
					/* check if cursor still in window! */
a
					if (actualEditor->xpos > actualEditor->leftpos
a
														+actualEditor->wcw)
a
						actualEditor->xpos = actualEditor->leftpos
a
													 + actualEditor->wcw;
a

a
					if (actualEditor->wdy >= actualEditor->wch)
a
						actualEditor->ypos = actualEditor->zlinesnr
a
							[(actualEditor->wdy  = actualEditor->wch - 1)];
a

a
print();
a

a
					break;
a

a
				case REFRESHWINDOW:
a
					printAll();
a
					break;
a

a
				case CLOSEWINDOW:
a
					running = FALSE;
a
					break;
a

a
				default:
a
					printf("Not a processable event: %lx\n",class);
a

a
			} /* of case */
a
/*#ENDFD Event processing */
a
		} /* of while (GetMsg()) */
a
/*#ENDFD Main loop */
a
	} while (running);
a

a
Ende:
a
	/* Close all editor windows: */
a
	while (ed = RemHead(&editorList))
a
		CloseEditor(ed);
a

a
	/* Close UserPort: */
a
	if (edUserPort) DeletePort(edUserPort);
a

a
	/* Close Libraries: */
a
	if (DosBase) CloseLibrary(DosBase);
a
	if (GfxBase) CloseLibrary(GfxBase);
a
	if (IntuitionBase) CloseLibrary(IntuitionBase);
a
}
a
/*#ENDFD Main program */


