Newsgroups: comp.sys.amiga.programmer
Subject: Novice help with Int. interface
From: mneylon@jupiter.cse.utoledo.edu (Michael Neylon)
Date: 4 Oct 92 11:04:30 EST

The follow should be a flashcard program from english to german translation.
A text version of it works fine, so I am sure that the file reading and
the string checking is ok.  However, I am having problems setting a 
interface up for it.  The program should open a window, create a string gadget
in the center of it.  The english translation would appear above the gadget, 
activate it, allow the user to enter his guess, and display the correctness
of it just below it.  However, this has crashed in a number of ways for me.
Also, if I include the i++ line in main, the second engtext.IText field
is filled with 'J's.....I have no idea what to do...

-----------------

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

#include <intuition/intuition.h>

#define STRINGSIZE 30

struct IntuitionBase *IntuitionBase;

SHORT d_pnts[] = 
{
	-2,10,
	-2,-2,
	244,-2
};

SHORT l_pnts[] =
{
	-2,10,
	244,10,
	244,-2
};

/* dual-border */
struct Border l_brdr = 
{
	0,0,
	2,0,JAM1,
	3,
	l_pnts,
	NULL,
};

struct Border d_brdr =
{
	0,0,
	1,0,JAM1,
	3,
	d_pnts,
	&l_brdr,
};

UBYTE undo_b[STRINGSIZE];

UBYTE ans_b[STRINGSIZE];

struct IntuiText gad_title=
{
	1,0,JAM1,
	-60,0,
	NULL,
	"Answer:",
	NULL,
};

struct StringInfo strng_inf=
{
	ans_b, undo_b,
	0,30,0,
	0,0,0,0,0,NULL,NULL,NULL,
};

struct Gadget ans_g=
{
	NULL,
	80,50,
	244,12,
	GADGHCOMP,
	GADGIMMEDIATE|RELVERIFY,
	STRGADGET,
	(APTR) &d_brdr,
	NULL,
	&gad_title,
	NULL,
	(APTR) &strng_inf,
	0,
	NULL
};

struct Window *wind;

struct NewWindow new_wind=
{
	20,20,
	350,100,
	0,1,
	CLOSEWINDOW|GADGETDOWN|GADGETUP,
	SMART_REFRESH|WINDOWCLOSE|WINDOWDRAG|WINDOWDEPTH|ACTIVATE,
	&ans_g,
	NULL,
	"Flashcard",
	NULL,
	NULL,
	400,
	180,
	640,200,
	WBENCHSCREEN
};

char english[50][30], german[50][30];

struct IntuiText engtext = 
{
	2,0,JAM1,
	50,30,
	NULL,
	"default",
	NULL
};

struct IntuiText cortext =
{
	3,0,JAM1,
	150,80,
	NULL,
	"Correct!",
	NULL
};

struct IntuiText incortext =
{
	2,0,JAM1,
	50,80,
	NULL,
	"Default",
	NULL
};

int read_trans() 
{
	FILE *fp;
	int i=0;
	char line[80];

	fp=fopen("trans.dat","r");
	
	fgets(line,80,fp);
	while(!feof(fp)) {
		strcpy(german[i],(strchr(line,'|'))+1);
		strncpy(english[i],line,strlen(line)-strlen(german[i])-1);
		i++;
		fgets(line,80,fp);	
	}
	fclose(fp);
	return i;
}

main() 
{
	ULONG class;
	struct IntuiMessage *int_mess;
	char input[30];
	int flag=1,i=0,j,numitem;


	IntuitionBase = (struct IntuitionBase *)
	  OpenLibrary("intuition.library",0);
	if (IntuitionBase == NULL ) {
		exit();
	}
	
	wind = (struct Window *) OpenWindow( &new_wind);
	if(wind == NULL)
	{
		CloseLibrary( IntuitionBase );
		exit();
	}

	numitem = read_trans();

	while (flag) 
	{
		strng_inf.Buffer = '\0';
		ActivateGadget( &ans_g, wind, NULL);
		if (i == numitem-1) flag = 0;
		strcpy(engtext.IText,english[i]);
		printf("engtext.IText = %s\n",engtext.IText);
		strcpy(incortext.IText,german[i]);
		printf("incortext.IText = %s\n",incortext.IText);
		PrintIText(wind->RPort,&engtext,0,0);
		Wait(1 << wind->UserPort->mp_SigBit );
		int_mess = (struct IntuiMessage *) GetMsg(wind->UserPort);
		class = int_mess->Class;
		ReplyMsg(int_mess);
		switch(class)
		{
			case CLOSEWINDOW:
				flag = 0;
				break;
			case GADGETDOWN:
				break;
			case GADGETUP:
				strcpy(input,strng_inf.Buffer);
				if(strncmp(input,german[i],strlen(german[i])-1)==0)
					PrintIText(wind->RPort,&cortext,1,1);
				else
					PrintIText(wind->RPort,&incortext,1,1);
				for (j=0;j<=1000;j++) ;
				/* i++ */
				break;
		} /* switch */
	}

	CloseWindow( wind );
	CloseLibrary(IntuitionBase);
}

-- 
Michael Neylon   aka Masem the Great and Almighty Thermodynamics GOD!
      //         | Senior, Chemical Engineering, Univ. of Toledo
  \\ // Only the |   Summer Intern, NASA Lewis Research Center
\  \X/   AMIGA!  |         mneylon@jupiter.cse.utoledo.edu           /
Newsgroups: comp.sys.amiga.programmer