/* ScreenShift 1.0
 * Anson Mah
 * June 4, 1987
 * Completely in the Public Domain
 * Written for Lattice C

 * Reworked by Ĝlli
 * 3.2.90
 * for Lettuce-C V5.04
 * Removed that nasty "SS" ->
 *  sorry, but I couldn't bear the name
 *  esspecially regarding current events
 *  in germany...

 */


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

#define PROPGADWIDTH	200	/* gadget proportions */
#define PROPGADHEIGHT	50
#define XJUMP	512		/* add to Vert/HorizPot when */
#define YJUMP	1024		/*   moving with cursor keys */
#define UP	0x4c		/* cursor key RAWKEYS */
#define DOWN	0x4d		/* also defined in intuition.h, */
#define LEFT	0x4f		/*   but included for clarity */
#define RIGHT	0x4e
#define CR	0x44		/* RETURN key */

extern UWORD xoffset(BYTE), yoffset(BYTE);      /* the math.s routines */
extern BYTE xpot(UWORD), ypot(UWORD);

UWORD piData[60] = {
   0x0000,0x0000,0x0000,
   0x7FFF,0xFFFF,0xFE00,
   0x7FFF,0xB7DF,0xFE00,
   0x7FFB,0xDBDF,0xFE00,
   0x7FFD,0xFDDF,0xFE00,
   0x7FFF,0xDADF,0xFE00,
   0x7FFB,0xDBDF,0xFE00,
   0x7FFF,0xBBDF,0xFE00,
   0x7FFF,0xFFFF,0xFE00,
   0x0000,0x0000,0x0000,
   0xFFFF,0xFFFF,0xFF00,
   0xFFFF,0xFFFF,0xFF00,
   0xFFF0,0x6FBF,0xFF00,
   0xFFE7,0x273F,0xFF00,
   0xFFE3,0xE23F,0xFF00,
   0xFFFE,0x253F,0xFF00,
   0xFFE7,0x273F,0xFF00,
   0xFFF0,0x673F,0xFF00,
   0xFFFF,0xFFFF,0xFF00,
   0xFFFF,0xFFFF,0xFF00
};

struct Image piImage = {
	0, 0,		/* LeftEdge, TopEdge */
	40, 10, 2,	/* Width, Height, Depth */
	&piData[0],	/* ImageData */
	0x03, 0x00,	/* PlanePick, PlaneOnOff */
	NULL		/* NextImage */
};



struct PropInfo pinfo = { FREEVERT | FREEHORIZ, 0,0, MAXBODY/5,MAXBODY/5, 0,0,0,0,0 };
struct Gadget pgadget = {
	NULL, 0, 10, PROPGADWIDTH, PROPGADHEIGHT, GADGIMAGE, FOLLOWMOUSE,
	PROPGADGET, (APTR)&piImage, NULL, NULL, 0, (APTR)&pinfo,
	0, NULL
};

struct NewWindow pwindef = {
	220, 70,
	PROPGADWIDTH, PROPGADHEIGHT + 10,
	0, 3,
	CLOSEWINDOW | MOUSEMOVE | RAWKEY,
	WINDOWDEPTH | WINDOWCLOSE | WINDOWDRAG | ACTIVATE | SMART_REFRESH | RMBTRAP,
	&pgadget,
	NULL,
	"ScreenMove",
	NULL, NULL,
	0,0, 0,0,
	WBENCHSCREEN
};

void _main(void);
void XCEXIT(int);

void _main()
{
	struct IntuiMessage *msg;
	ULONG class, code;
	BYTE c_xoffset, c_yoffset;
	struct IntuitionBase *IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 33);
	struct Preferences pref;
	struct Window *pwin;

	/* get current preference setting */
	(void)GetPrefs(&pref, sizeof(struct Preferences));
	/* set up prop gadget with current View settings */
	pinfo.HorizPot = xoffset(pref.ViewXOffset);
	pinfo.VertPot = yoffset(pref.ViewYOffset);
	if (!(pwin = (struct Window *)OpenWindow(&pwindef)))
	    goto exit;

	for (;;) {
		WaitPort(pwin->UserPort);
		msg = (struct IntuiMessage *)GetMsg(pwin->UserPort);
		class = msg->Class;
		code = msg->Code;
		ReplyMsg((struct Message*)msg);

		switch (class) {
			case CLOSEWINDOW:
			    goto exit;
			case RAWKEY:
			  switch (code) {
				case UP:
					if (pinfo.VertPot >= YJUMP)
						pinfo.VertPot -= YJUMP;
					else	pinfo.VertPot = 0;
					break;
				case DOWN:
					if (pinfo.VertPot < MAXBODY - YJUMP)
						pinfo.VertPot += YJUMP;
					else	pinfo.VertPot = MAXBODY;
					break;
				case LEFT:
					if (pinfo.HorizPot >= XJUMP)
						pinfo.HorizPot -= XJUMP;
					else	pinfo.HorizPot = 0;
					break;
				case RIGHT:
					if (pinfo.HorizPot < MAXBODY - XJUMP)
						pinfo.HorizPot += XJUMP;
					else	pinfo.HorizPot = MAXBODY;
					break;
				case CR:	/* carriage return */
					goto exit;
			  }
			  RefreshGadgets(&pgadget, pwin, NULL);
			  /* fall through */
			case MOUSEMOVE:
				c_xoffset = xpot(pinfo.HorizPot);
				c_yoffset = ypot(pinfo.VertPot);
				if (pref.ViewXOffset != c_xoffset || pref.ViewYOffset != c_yoffset) {
					pref.ViewXOffset = c_xoffset;
					pref.ViewYOffset = c_yoffset;
					(void)SetPrefs(&pref, sizeof(struct Preferences), FALSE);
				}
				break;
		}
	}
exit:
	if(pwin) CloseWindow(pwin);
	XCEXIT(0);
}

