/* Select a character from ASDG CygnusEd */
/* assign to function key for easy access */

/* Njål Fisketjøn 930604 */


#include <exec/types.h>
#include <exec/ports.h>
#include <exec/nodes.h>
#include <intuition/sghooks.h>
#include <intuition/intuition.h>
#include <intuition/gadgetclass.h>
#include <libraries/gadtools.h>

#include <clib/alib_protos.h>
#include <clib/exec_protos.h>
#include <clib/graphics_protos.h>
#include <clib/intuition_protos.h>
#include <clib/gadtools_protos.h>

#include <stdio.h>
#include <string.h>

#include "ced.h"

#ifdef LATTICE
int CXBRK(void)		{ return(0); }
int chkabort(void)	{ return(0); }
#endif

#define RXFB_RESULT 17

struct CedMsg TheMessage;
struct MsgPort *CedPort;
struct MsgPort *MyPort;
char TheCommand[80];

struct TextAttr Topaz80 = {"topaz.font", 8, 0, 0, } ;

struct IntuiText Copyright = {2, 0, JAM1, 300, 160, NULL, 
	"Copyright © 1993 N Fisketjøn", NULL};
 
struct IntuiText IText7 = {1, 0, JAM1, 14, 143,	NULL,
	"à á â ã ä å æ ç è é ê ë ì í î ï ð ñ ò ó ô õ ö ÷ ø ù ú û ü ý þ ÿ",
	&Copyright};

struct IntuiText IText6 = {1, 0, JAM1, 14, 127,	NULL,
	"À Á Â Ã Ä Å Æ Ç È É Ê Ë Ì Í Î Ï Ð Ñ Ò Ó Ô Õ Ö × Ø Ù Ú Û Ü Ý Þ ß", &IText7};

unsigned char string5b[]={0xa8,0x00};
struct IntuiText IText5b = {1, 0, JAM1, 142, 111, NULL, string5b, &IText6};

struct IntuiText IText5 = {1, 0, JAM1, 14, 111,	NULL,
	"  ¡ ¢ £ ¤ ¥ ¦ §   © ª « ¬ ­ ® ¯ ° ± ² ³ ´ µ ¶ · ¸ ¹ º » ¼ ½ ¾ ¿", &IText5b};

unsigned char string4[]={0x80,0x20,0x81,0x20,0x82,0x20,0x83,0x20,0x84,0x20,0x85,0x20,0x86,0x20,0x87,0x20,
				0x88,0x20,0x89,0x20,0x8a,0x20,0x8b,0x20,0x8c,0x20,0x8d,0x20,0x8e,0x20,0x8f,0x20,
				0x90,0x20,0x91,0x20,0x92,0x20,0x93,0x20,0x94,0x20,0x95,0x20,0x96,0x20,0x97,0x20,
				0x98,0x20,0x99,0x20,0x9a,0x20,0x9b,0x20,0x9c,0x20,0x9d,0x20,0x9e,0x20,0x9f,0x20,0x00};
struct IntuiText IText4 = {1, 0, JAM1, 14, 95,	NULL, string4, &IText5};

struct IntuiText IText3 = {1, 0, JAM1, 14, 79,	NULL,
	"` a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~ ", &IText4};

unsigned char string2b[]={0x5c,0x00};
struct IntuiText IText2b = {1, 0, JAM1, 462, 63, NULL, string2b, &IText3};

struct IntuiText IText2 = {1, 0, JAM1, 14, 63,	NULL,
	"@ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [   ] ^ _", &IText2b};

unsigned char string1b[]={0x22,0x00};
struct IntuiText IText1b = {1, 0, JAM1, 46, 47,	NULL, string1b, &IText2};

struct IntuiText IText1 = {1, 0, JAM1, 14, 47,	NULL,
    "  !   # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ?", &IText1b};

unsigned char string0[]={0x20,0x20,0x01,0x20,0x02,0x20,0x03,0x20,0x04,0x20,0x05,0x20,0x06,0x20,0x07,0x20,
				0x08,0x20,0x09,0x20,0x0a,0x20,0x0b,0x20,0x0c,0x20,0x0d,0x20,0x0e,0x20,0x0f,0x20,
				0x10,0x20,0x11,0x20,0x12,0x20,0x13,0x20,0x14,0x20,0x15,0x20,0x16,0x20,0x17,0x20,
				0x18,0x20,0x19,0x20,0x1a,0x20,0x1b,0x20,0x1c,0x20,0x1d,0x20,0x1e,0x20,0x1f,0x20,0x00};
struct IntuiText IText0 = {1, 0, JAM1, 14, 31,	NULL, string0, &IText1};

struct Library *IntuitionBase;
struct Library *GfxBase;
struct Library *GadToolsBase;

void errorMessage(STRPTR error)
{
if (error)
	printf("Error: %s\n",error);
}

struct Gadget *createAllGadgets(struct Gadget **glistptr, void *vi,
	UWORD topborder)
{
int i,j;

struct NewGadget ng;
struct Gadget *gad;

gad= CreateContext(glistptr);

ng.ng_Height	 = 16;
ng.ng_Width		 = 16;
ng.ng_GadgetText = "";
ng.ng_TextAttr	 = &Topaz80;
ng.ng_VisualInfo = vi;
ng.ng_Flags		 = NG_HIGHLABEL;

for(i=0; i<=31; i++)
{
	for(j=0; j<=7; j++)
	{
	ng.ng_TopEdge	= 10 + (j*ng.ng_Height) + topborder;
	ng.ng_LeftEdge	= 10 + (i*ng.ng_Width);
	ng.ng_GadgetID	= j*32 + i;
	gad = CreateGadget(BUTTON_KIND, gad, &ng, TAG_END);
	}
}

return(gad);
}

VOID SendCygnusEdMessageGetReply(char *TheCommand)

{
	TheMessage.cm_Args[0] = TheCommand;
	TheMessage.cm_Node.mn_Node.ln_Type = NT_MESSAGE;
	TheMessage.cm_Node.mn_Length = sizeof(struct CedMsg);
	TheMessage.rm_Action= 1l << RXFB_RESULT;
	TheMessage.rm_Result2 = 0;

	if (!(CedPort = FindPort("rexx_ced")))
		errorMessage(" Unable to find Ced rexx port");
	if (!(MyPort = CreatePort("SltChr", 0L)))
		errorMessage(" Unable to create message port");

	TheMessage.cm_Node.mn_ReplyPort	= MyPort;
	PutMsg(CedPort, &TheMessage);
	WaitPort(MyPort);
	DeletePort(MyPort);
}

VOID process_window_events(struct Window *mywin)
{
struct IntuiMessage *imsg;
ULONG imsgClass;
UWORD imsgCode;
struct Gadget *gad;
BOOL terminated = FALSE;

while (!terminated)
	{
	Wait (1 << mywin->UserPort->mp_SigBit);

	while ((!terminated) &&
		   (imsg = GT_GetIMsg(mywin->UserPort)))
		{
		gad = (struct Gadget *)imsg->IAddress;

		imsgClass = imsg->Class;
		imsgCode = imsg->Code;

		GT_ReplyIMsg(imsg);

		switch (imsgClass)
			{
			case IDCMP_GADGETDOWN:
			case IDCMP_MOUSEMOVE:
			case IDCMP_GADGETUP:
				PrintIText(mywin->RPort,&IText0,0,0);
				sprintf(TheCommand,"enter ascii %i",gad->GadgetID);
				SendCygnusEdMessageGetReply(TheCommand);
				break;
			case IDCMP_CLOSEWINDOW:
				terminated = TRUE;
				break;
			case IDCMP_REFRESHWINDOW:
				GT_BeginRefresh(mywin);
				GT_EndRefresh(mywin, TRUE);
				break;
			}
		}
	}
}

VOID gadtoolsWindow(void)
{
struct TextFont *font;
struct Screen	*mysc;
struct Window	*mywin;
struct Gadget	*glist;
void			*vi;
UWORD			topborder;

if (NULL == (font = OpenFont(&Topaz80)))
	errorMessage( "Failed to open Topaz 80");
else
	{
	if (NULL == (mysc = LockPubScreen(NULL)))
		errorMessage( "Couldn't lock default public screen");
	else
		{
		if (NULL == (vi = GetVisualInfo(mysc, TAG_END)))
			errorMessage( "GetVisualInfo() failed");
		else
			{
			topborder = mysc->WBorTop + (mysc->Font->ta_YSize + 1);

			if (NULL == createAllGadgets(&glist, vi, topborder))
				errorMessage( "createAllGadgets() failed");
			else
				{
				ScreenToFront(mysc);
				if (NULL == (mywin = OpenWindowTags(NULL,
						WA_Title,	  "Select character(s) and close window",
						WA_Gadgets,	  glist,	  WA_AutoAdjust,	TRUE,
						WA_Width,		532,	  WA_MinWidth,		   0,
						WA_InnerHeight, 165,	  WA_MinHeight,		   0,
						WA_DragBar,	   TRUE,	  WA_DepthGadget,  FALSE,
						WA_Activate,   TRUE,	  WA_CloseGadget,	TRUE,
						WA_SizeGadget, FALSE,	  WA_SimpleRefresh, TRUE,
						WA_IDCMP, IDCMP_CLOSEWINDOW | IDCMP_REFRESHWINDOW |
							BUTTONIDCMP ,
						WA_PubScreen, mysc,
						TAG_END)))
					errorMessage( "OpenWindow() failed");
				else
					{
					PrintIText(mywin->RPort,&IText0,0,0);
					GT_RefreshWindow(mywin, NULL);
					process_window_events(mywin);
					CloseWindow(mywin);
					}
				}
			FreeGadgets(glist);
			FreeVisualInfo(vi);
			}
		UnlockPubScreen(NULL, mysc);
		}
	CloseFont(font);
	}
}

VOID main(void)
{

if (NULL == (IntuitionBase = OpenLibrary("intuition.library", 37)))
	errorMessage( "Requires V37 intuition.library");
else
	{
	if (NULL == (GfxBase = OpenLibrary("graphics.library", 37)))
		errorMessage(" Requires V37 graphics.library");
	else
		{
		if (NULL == (GadToolsBase = OpenLibrary("gadtools.library", 37)))
			errorMessage(" Requires V37 gadtools.library");
		else
			{
			gadtoolsWindow();
			CloseLibrary(GadToolsBase);
			}
		CloseLibrary(GfxBase);
		}
	CloseLibrary(IntuitionBase);
	}
SendCygnusEdMessageGetReply("Cedtofront");
}
