
/* This is just a quick and nasty example of using
 * requesters in OpalPaint. This will bring up
 * a few requesters and then load a gradient
 * into OpalPaint.
 * To use this loader, you must select it from the
 * load format requester (Right mouse button on load gadget).
 */

#include <proto/exec.h>
#include <proto/dos.h>
#include <opal/opallib.h>
#include <opal/loadsave.h>
#include <opal/opalpaint.h>
#include <opal/macros.h>
#include <proto/intuition.h>
#include <proto/graphics.h>
#include <graphics/gfxmacros.h>
#include <intuition/intuition.h>
#include <exec/types.h>
#include <exec/ports.h>
#include <exec/nodes.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>


/* NOTE: if your using callback functions in Build_Request()
 * disable stack checking and enable SAVEDS (SAS/C).
 */

#define WIDTH 320	/* Size of gradient that we'll generate */
#define HEIGHT 256

struct OpalBase *OpalBase;


struct MsgPort *LoadPort;
BOOL Do_Grad (void);

char LoaderName[] = "Gradient";
char PortName[] = "Gradient_Loader";

void main (void)
{
   struct LSIMessage *Mesg;

	LoadPort = CreatePort (PortName,0);
	if (LoadPort==NULL)
		exit (10);
	OpalBase = (struct OpalBase *)OpenLibrary ("opal.library",0);
	if (OpalBase==NULL)
		{ DeletePort (LoadPort);
		  exit (10);
		}

	if (!AddOVLoader (LoaderName,LoadPort,OVLF_ALLOWIMAGE))
		{ DeletePort (LoadPort);
		  CloseLibrary ((struct Library *)OpalBase);
		  exit (10);
		}
	CloseLibrary ((struct Library *)OpalBase);	/* library must be closed */
	OpalBase = NULL;

	while (1)
		{ WaitPort (LoadPort);
		  while (Mesg = (struct LSIMessage *)GetMsg (LoadPort))
			{ switch (Mesg->lsi_Type)
				{ case OVCMD_LOADIMAGE:
					ReplyMsg ((struct Message *)Mesg);
					Do_Grad();
					break;
				  case OVCMD_EXPUNGE:
					if (!OpalBase)
						OpalBase = (struct OpalBase *)OpenLibrary ("opal.library",0);
					RemOVLoader (LoaderName);
					if (OpalBase)
						CloseLibrary ((struct Library *)OpalBase);
					DeletePort (LoadPort);
					ReplyMsg ((struct Message *)Mesg);
					exit (0);
					break;
				  case OVCMD_FORMATCHECK:
					Mesg->lsi_Result = TRUE;
					Mesg->lsi_Width = WIDTH;
					Mesg->lsi_Height = HEIGHT;
					Mesg->lsi_Depth = 24;
					Mesg->lsi_Flags = LSIF_HASIMAGE;
					ReplyMsg ((struct Message *)Mesg);
					break;
				}
			}
		}
}


UBYTE Red[WIDTH],Green[WIDTH],Blue[WIDTH];


struct TextAttr Font = { (STRPTR)"topaz.font",8,0,0 };
MakeBorder (GadBd,GadVec,0,0,200,25);
MakeIBorder2 (GadIBd,GadVec,0,0,200,25);

MakeDoubleText (JPegLoadText,90,12,"Loading JPEG Image",NULL);
MakeWText (JPSmoothText,46,10,"Smoothing On ");
MakeButton (JPSmoothGad,NULL,66,60,200,25,JPSmoothText,GadBd,GadIBd,104);

struct Requester *ActiveReq;	/* The requester		*/
struct RastPort *ReqRP;		/* The requester's raster port	*/
struct GfxBase *GfxBase;
struct IntuitionBase *IntuitionBase;
BOOL BlockSmooth=TRUE;


/* This init function simply saves the pointers */

__saveds static void SmoothReq_Init (struct RastPort *RP,struct Requester *Req)
{
	ReqRP = RP;
	ActiveReq = Req;
}

/* Handler function toggles the gadget text */

__saveds static void SmoothReq_Handler (void)
{
	if (BlockSmooth)
		{ BlockSmooth = FALSE;
		  JPSmoothText.IText[11]='f';
		  JPSmoothText.IText[12]='f';
		}
	else
		{ BlockSmooth = TRUE;
		  JPSmoothText.IText[11]='n';
		  JPSmoothText.IText[12]=' ';
		}
	SetAPen (ReqRP,OPGREY);
	SetOPen (ReqRP,OPGREY);
	RectFill (ReqRP,70,63,260,82);
	RefreshGList (&JPSmoothGad,ActiveReq->RWindow,ActiveReq,1L);
}


	/* Tags for Build_Req_Tags() */

struct TagItem BRTags[] =
	{ {OPBR_Width,330},
	  {OPBR_Height,170},
	  {OPBR_Flags,BRF_CANCEL},
	  {OPBR_IText,(ULONG)JPegLoadText},
	  {OPBR_Gadgets,(ULONG)&JPSmoothGad},
	  {OPBR_InitFunc,(ULONG)SmoothReq_Init},
	  {OPBR_GadFunc,(ULONG)SmoothReq_Handler},
	  {OPBR_Flags,BRF_CANCEL|BRF_DRAGABLE},
	  {TAG_END,0}
	};

BOOL Do_Grad (void)
{
   int i,j;
   struct MsgPort *MasterPort;
   struct LSIMessage Mesg;
   BOOL Res;

	MasterPort = FindPort (OVLOADERPORT);
	if (MasterPort==NULL) return (FALSE);

	IntuitionBase = (struct IntuitionBase *)OpenLibrary ("intuition.library",0L);
	GfxBase = (struct GfxBase *)OpenLibrary ("graphics.library",0L);

	memset (&Mesg,0,sizeof(struct LSIMessage));
	Mesg.lsi_Node.mn_Node.ln_Type = NT_MESSAGE;
	Mesg.lsi_Node.mn_ReplyPort = LoadPort;
	Mesg.lsi_Node.mn_Length = sizeof (struct LSIMessage);

	Mesg.lsi_Type = OVCMD_BUILDREQUEST;
	Mesg.lsi_Address = (APTR)BRTags;
	LSICmd (MasterPort,LoadPort,&Mesg);

	Mesg.lsi_Type = OVCMD_ASKUSER;
	Mesg.lsi_Address = (APTR)"This is OVCMD_ASKUSER\\nHit ok or Cancel";
	LSICmd (MasterPort,LoadPort,&Mesg);

	Res = Mesg.lsi_Result;

	Mesg.lsi_Type = OVCMD_USERMESSAGE;
	if (Res)
		Mesg.lsi_Address = (APTR)"OKAY!!";
	else
		Mesg.lsi_Address = (APTR)"CANCEL!!";
	LSICmd (MasterPort,LoadPort,&Mesg);


	for (j=0; j<HEIGHT; j++)
		{ for (i=0; i<WIDTH; i++)
			{ Red[i] = i;
			  Green[i] = j;
			  Blue[i] = (i+j)/2;
			}
		  Mesg.lsi_Planes[0] = Red;
		  Mesg.lsi_Planes[1] = Green;
		  Mesg.lsi_Planes[2] = Blue;
		  Mesg.lsi_Width = WIDTH;
		  Mesg.lsi_Height = 1;
		  Mesg.lsi_X = 0;
		  Mesg.lsi_Y = j;
		  Mesg.lsi_Type = OVCMD_SENDDATA;
		  Mesg.lsi_SubType = OVDF_RGB;

		  LSICmd (MasterPort,LoadPort,&Mesg);
		  if ((j & 0xF)==0)
			{ Mesg.lsi_Type = OVCMD_PERCENTAGE;
			  Mesg.lsi_Address = "Grad ";
			  Mesg.lsi_Result = (j*100)/HEIGHT;
			  LSICmd (MasterPort,LoadPort,&Mesg);
			}
		}

	Mesg.lsi_Type = OVCMD_DONE;
	LSICmd (MasterPort,LoadPort,&Mesg);

	CloseLibrary ((struct Library *)IntuitionBase);
	CloseLibrary ((struct Library *)GfxBase);
	return (TRUE);
}

