/*
**	$VER: prefs.c 39.11 (3.2.95)
**
**	Preferences program for Sketch Driver v39+
**
**	(C) Copyright 1995 Miguel Fides (Dior/Darkness)
**	    All Rights Reserved
**
*/

#include <intuition/intuition.h>
#include <proto/intuition.h>
#include <proto/exec.h>
#include <sketch.h>

char	PortName[]="SketchPort";
struct	MsgPort	*DriverPort;
struct	MsgPort	*MyPort;

int	ChangeClipBox(void);
long	WaitSM(struct SketchMessage *);
long	WaitSMReq(struct SketchMessage *,struct EasyStruct *);

void main(void)
{
if (MyPort=CreateMsgPort())
	{
	if (DriverPort=FindPort(PortName))
		{
		ChangeClipBox();
		}
	DeleteMsgPort(MyPort);
	}
}

int ChangeClipBox()
{
struct	SketchMessage sm;
long	boxx,boxy,boxw,boxh;
long	clipmode;
struct	EasyStruct es1={
	sizeof(struct EasyStruct),
	0,"Clip Box Preferences",
	"Press in the top left corner\n"
	"of the new clipbox...",
	"Cancel"
	};
struct	EasyStruct es2={
	sizeof(struct EasyStruct),
	0,"Clip Box Preferences",
	"Press in the bottom right corner\n"
	"of the new clipbox...",
	"Cancel"
	};
struct	EasyStruct es3={
	sizeof(struct EasyStruct),
	0,"Clip Box Preferences",
	"Clip Box changed.\n\n"
	"Save new preferences?",
	"Yes|No"
	};


sm.Message.mn_ReplyPort=MyPort;
sm.Command=SM_GETCLIPMODE;
sm.Extension=NULL;
if (WaitSM(&sm)==SM_OK) clipmode=(sm.Value)? sm.Value : SCM_ABSOLUTE;
	else return(FALSE);

sm.Message.mn_ReplyPort=MyPort;
sm.Command=SM_SETCLIPMODE;
sm.Value=SCM_INHIBIT;
sm.Extension=NULL;
if (WaitSM(&sm)!=SM_OK) return (FALSE);

sm.Message.mn_ReplyPort=MyPort;
sm.Command=SM_CLICK;
sm.Extension=NULL;
if (WaitSMReq(&sm,&es1)==SM_OK)
	{
	boxx=sm.X;
	boxy=sm.Y;

	sm.Message.mn_ReplyPort=MyPort;
	sm.Command=SM_CLICK;
	sm.Extension=NULL;
	if (WaitSMReq(&sm,&es2)==SM_OK)
		{
		boxw=sm.X-boxx;
		boxh=sm.Y-boxy;

		if ((boxw>1000)&&(boxh>1000))	/* at least 1" x 1" is needed */
			{
			sm.Message.mn_ReplyPort=MyPort;
			sm.Command=SM_SETCLIPBOX;
			sm.X=boxx; sm.Y=boxy;
			sm.Width=boxw; sm.Height=boxh;
			sm.Extension=NULL;
			WaitSM(&sm);
			}
		}
	}

sm.Message.mn_ReplyPort=MyPort;
sm.Command=SM_SETCLIPMODE;
sm.Value=clipmode;
sm.Extension=NULL;
if (WaitSM(&sm)!=SM_OK) return (FALSE);

if (EasyRequestArgs(NULL,&es3,NULL,NULL))
	{
	sm.Message.mn_ReplyPort=MyPort;
	sm.Command=SM_SAVEPREFS;
	sm.Extension=NULL;
	WaitSM(&sm);
	}
return(TRUE);
}

long	WaitSM(struct SketchMessage *sm)
{
struct SketchMessage *rm=NULL;

PutMsg(DriverPort,(struct Message *)sm);
do	{
	WaitPort(MyPort);
	while (rm=(struct SketchMessage *)GetMsg(MyPort))
		if (rm==sm) break;
	} while (rm==NULL);
return(sm->Error);
}

long	WaitSMReq(struct SketchMessage *sm,struct EasyStruct *es)
{
int	status;
struct	Window	*win;
struct	SketchMessage *rm=NULL;

win=BuildEasyRequestArgs(NULL,es,IDCMP_INTUITICKS,NULL);
PutMsg(DriverPort,(struct Message *)sm);

while ((status=SysReqHandler(win,NULL,TRUE))<0)
	{
	while (rm=(struct SketchMessage *)GetMsg(MyPort))
		{
		if (rm==sm) break;
		}
	if (rm==sm) break;
	}
FreeSysRequest(win);
if (rm==sm) return(sm->Error);
Forbid();
Remove((struct Node *)sm);
Permit();
return(SM_ABORTED);
}
