/* ******************************************************************** */
/* C code generated by:							*/
/* Visual Arts Version 2.1						*/
/* Copyright 1994-95 Danny Y. Wong  All rights reserved			*/
/* Calgary, Alberta (CANADA)                        			*/
/* Partial of the code is copyright by Jaba Development			*/
/* ******************************************************************** */

#include <VisualArts.h>
#include <clib/VisualArts_protos.h>

#define ID_ObjID7                         			0
#define ID_ObjID8                         			1

#define SuperBitMapDepth						2
#define SuperBitMapMaxX						640
#define SuperBitMapMaxY						200
#define ID_SuperBitMapLeft						2
#define ID_SuperBitMapRight					3
#define ID_SuperBitMapUp						4
#define ID_SuperBitMapDown						5
#define ID_SuperBitMapHoriz					6
#define ID_SuperBitMapVert						7

#define SuperBitMapNumGads						2

#include "SuperBitmap_func.c" 

#include "SuperBitmap_images.c" 

int ObjID7Obj(struct VAobject VAObject);
int ObjID8Obj(struct VAobject VAObject);

BOOL SetupSuperBitMapScrollerWindow(void);
void SuperBitMapScrollBitmap(ULONG x, ULONG y);
void SuperBitMapInitNewBitmap(void);
void SuperBitMapFreeBitmap(void);
void SuperBitMapdoNewSize(void);
int GetPubScreen(void);
void ClosePubScreen(void);
int OpenSuperBitMapWindow(char windtitle[]);
void CloseSuperBitMapWindow(void);
int SuperBitMapHandler(void);
int SuperBitMapMainHandler(void);
void DrawSuperBitMapObjs(void);
int main(int argc, char *argv[]);

Object *SuperBitMapSizeImage  = NULL,  *SuperBitMapLeftImage = NULL,
       *SuperBitMapRightImage = NULL,  *SuperBitMapUpImage   = NULL,
       *SuperBitMapDownImage  = NULL;
struct Gadget *SuperBitMapHorizGadget = NULL, *SuperBitMapVertGadget  = NULL,
              *SuperBitMapLeftGadget  = NULL, *SuperBitMapRightGadget = NULL,
              *SuperBitMapUpGadget    = NULL, *SuperBitMapDownGadget  = NULL;
struct BitMap			*SuperBitMapBitmap = NULL;
UBYTE				*PubScrName = "Workbench";
struct DrawInfo			*ScrDrawInfo = NULL;
APTR				 VisualInfo = NULL;
struct Screen			*Scr = NULL;
struct Window			*SuperBitMapWnd = NULL;
struct Gadget			*SuperBitMapGList = NULL;
struct Gadget			*SuperBitMapGadgets[SuperBitMapNumGads];
struct IntuiMessage		 SuperBitMapMsg;
UWORD				 SuperBitMapLeft = 97;
UWORD				 SuperBitMapTop = 46;
UWORD				 SuperBitMapWidth = 395;
UWORD				 SuperBitMapHeight = 129;
struct TextAttr topaz8 = { (STRPTR)"topaz.font", 8, 0x00, 0x01 };
struct TextAttr topaz800 = { (STRPTR)"topaz.font", 8, 0x00, 0x00 };

WORD SuperBitMapGadTypes[] = {
	BUTTON_KIND,
	CHECKBOX_KIND,
};

struct NewGadget SuperBitMapNGads[] = {
	269, 10, 91, 12, (UBYTE *)"Button",&topaz800, ID_ObjID7, PLACETEXT_IN, NULL, (APTR)ObjID7Obj,
	334, 89, 26, 11, (UBYTE *)"Cool!", &topaz800, ID_ObjID8, PLACETEXT_LEFT, NULL, (APTR)ObjID8Obj,
};

ULONG SuperBitMapNTags[] = {
	TAG_DONE,
	TAG_DONE,
};


int GetPubScreen(void)
{
  if (!(Scr = LockPubScreen(PubScrName)))
	return(1L);

  if (!(VisualInfo = GetVisualInfo(Scr, TAG_DONE)))
	return(2L);

  if (!(ScrDrawInfo = GetScreenDrawInfo(Scr)))
	return(3L);
  return(0L);
}

void ClosePubScreen(void)
{
  if (VisualInfo)
    FreeVisualInfo(VisualInfo);
  if (Scr)
    UnlockPubScreen(NULL,  Scr);
  if (ScrDrawInfo)
    FreeScreenDrawInfo(Scr,  ScrDrawInfo);
}

BOOL SetupSuperBitMapScrollerWindow(void)
{
  ULONG resolution = SysISize();
  WORD topborder = Scr->WBorTop + Scr->Font->ta_YSize + 1;
  WORD w = IM(SuperBitMapSizeImage)->Width;
  WORD h = IM(SuperBitMapSizeImage)->Height;
  WORD bw = resolution == SYSISIZE_LOWRES ? 1 : 2;
  WORD bh = resolution == SYSISIZE_HIRES ? 2 : 1;
  WORD rw = resolution == SYSISIZE_HIRES ? 3 : 2;
  WORD rh = resolution == SYSISIZE_HIRES ? 2 : 1;
  WORD gw = 0, gh = 0, gap = 0;

  gh = MAX(IM(SuperBitMapLeftImage)->Height, h);
  gh = MAX(IM(SuperBitMapRightImage)->Height, gh);
  gw = MAX(IM(SuperBitMapUpImage)->Width, w);
  gw = MAX(IM(SuperBitMapDownImage)->Width, gw);
 /* If you have gadgets in the left window border, set 'gap' to the
 	 * width of these gadgets. */
  gap = 1;

  SuperBitMapHorizGadget = NewPropObject(FREEHORIZ,
	GA_ID, ID_SuperBitMapHoriz,
	GA_Left, rw + gap,
	GA_RelBottom, bh - gh + 2,
	GA_RelWidth, -gw - gap - IM(SuperBitMapLeftImage)->Width - IM(SuperBitMapRightImage)->Width - rw - rw,
	GA_Height, gh - bh - bh - 2,
	GA_BottomBorder, TRUE,
	GA_Immediate, TRUE,
	GA_RelVerify, TRUE,
	GA_GZZGadget, TRUE,
	GA_UserData, NULL,
	ICA_TARGET, ICTARGET_IDCMP,
	TAG_DONE);

  SuperBitMapVertGadget = NewPropObject(FREEVERT,
	GA_ID, ID_SuperBitMapVert,
	GA_RelRight, bw - gw + 3,
	GA_Top, topborder + rh,
	GA_Width, gw - bw - bw - 4,
	GA_RelHeight, -topborder - h - IM(SuperBitMapUpImage)->Height - IM(SuperBitMapDownImage)->Height - rh - rh,
	GA_RightBorder, TRUE,
	GA_Previous, SuperBitMapHorizGadget,
	GA_Immediate, TRUE,
	GA_RelVerify, TRUE,
	GA_GZZGadget, TRUE,
	GA_UserData, NULL,
	ICA_TARGET, ICTARGET_IDCMP,
	TAG_DONE);

  SuperBitMapLeftGadget = NewButtonObject(SuperBitMapLeftImage,
	GA_ID, ID_SuperBitMapLeft,
	GA_RelRight, 1 - IM(SuperBitMapLeftImage)->Width - IM(SuperBitMapRightImage)->Width - gw,
	GA_RelBottom, 1 - IM(SuperBitMapLeftImage)->Height,
	GA_BottomBorder, TRUE,
	GA_Previous, SuperBitMapVertGadget,
	GA_Immediate, TRUE,
	GA_RelVerify, TRUE,
	GA_GZZGadget, TRUE,
	GA_UserData, NULL,
	ICA_TARGET, ICTARGET_IDCMP,
	TAG_DONE);

  SuperBitMapRightGadget = NewButtonObject(SuperBitMapRightImage,
	GA_ID, ID_SuperBitMapRight,
	GA_RelRight, 1 - IM(SuperBitMapRightImage)->Width - gw,
	GA_RelBottom, 1 - IM(SuperBitMapRightImage)->Height,
	GA_BottomBorder, TRUE,
	GA_Previous, SuperBitMapLeftGadget,
	GA_Immediate, TRUE,
	GA_RelVerify, TRUE,
	GA_GZZGadget, TRUE,
	GA_UserData, NULL,
	ICA_TARGET, ICTARGET_IDCMP,
	TAG_DONE);

  SuperBitMapUpGadget = NewButtonObject(SuperBitMapUpImage,
	GA_ID, ID_SuperBitMapUp,
	GA_RelRight, 1 - IM(SuperBitMapUpImage)->Width,
	GA_RelBottom, 1 - IM(SuperBitMapUpImage)->Height - IM(SuperBitMapDownImage)->Height - h,
	GA_RightBorder, TRUE,
	GA_Previous, SuperBitMapRightGadget,
	GA_Immediate, TRUE,
	GA_RelVerify, TRUE,
	GA_GZZGadget, TRUE,
	GA_UserData, NULL,
	ICA_TARGET, ICTARGET_IDCMP,
	TAG_DONE);

  SuperBitMapDownGadget = NewButtonObject(SuperBitMapDownImage,
	GA_ID, ID_SuperBitMapDown,
	GA_RelRight, 1 - IM(SuperBitMapDownImage)->Width,
	GA_RelBottom, 1 - IM(SuperBitMapDownImage)->Height - h,
	GA_RightBorder, TRUE,
	GA_Previous, SuperBitMapUpGadget,
	GA_Immediate, TRUE,
	GA_RelVerify, TRUE,
	GA_GZZGadget, TRUE,
	GA_UserData, NULL,
	ICA_TARGET, ICTARGET_IDCMP,
	TAG_DONE);

  if (!SuperBitMapSizeImage || !SuperBitMapLeftImage || !SuperBitMapRightImage || 
      !SuperBitMapUpImage   || !SuperBitMapDownImage)
	return(FALSE);
  if (SuperBitMapDownGadget)
	return(TRUE);
  return(FALSE);
}

int OpenSuperBitMapWindow(char windtitle[80])
{
  struct NewGadget	NewGad;
  struct Gadget   	*Gad;
  register UWORD i, j;
  UWORD offsetx = Scr->WBorLeft;
  UWORD offsety = Scr->WBorTop + Scr->Font->ta_YSize + 1;

  SuperBitMapInitNewBitmap();
  if (SuperBitMapBitmap == NULL)
	return(1L);  
  SuperBitMapSizeImage  = NewImageObject(SIZEIMAGE);
  SuperBitMapLeftImage  = NewImageObject(LEFTIMAGE);
  SuperBitMapRightImage = NewImageObject(RIGHTIMAGE);
  SuperBitMapUpImage    = NewImageObject(UPIMAGE);
  SuperBitMapDownImage  = NewImageObject(DOWNIMAGE);

  if (!SetupSuperBitMapScrollerWindow())
	return(1L);
  if (!(Gad = CreateContext(&SuperBitMapGList)))
  	return(1L);

  for (i=0, j=0; i < SuperBitMapNumGads; i++)
  {
    CopyMem((char *)&SuperBitMapNGads[i], (char *)&NewGad, (long)sizeof(struct NewGadget));

    NewGad.ng_VisualInfo = VisualInfo;
    NewGad.ng_LeftEdge += offsetx;
    NewGad.ng_TopEdge  += offsety;

    SuperBitMapGadgets[i] = Gad = CreateGadgetA((ULONG)SuperBitMapGadTypes[i], Gad, &NewGad,
	(struct TagItem *)&SuperBitMapNTags[j]);
    while (SuperBitMapNTags[j])
        j +=2;
    j++;
    if (!Gad)
       return(2L);
  }
  SuperBitMapGadgets[i-1]->NextGadget = SuperBitMapHorizGadget;
  if (!(SuperBitMapWnd = OpenWindowTags(NULL,
  	WA_Left,	SuperBitMapLeft,
  	WA_Top, 	SuperBitMapTop,
  	WA_Width,	SuperBitMapWidth,
  	WA_Height,	SuperBitMapHeight + kWindowOffSetY,
  	WA_NewLookMenus, TRUE,
  	WA_IDCMP,	IDCMP_CLOSEWINDOW |  IDCMP_NEWSIZE | IDCMP_GADGETUP |  IDCMP_IDCMPUPDATE ,
  	WA_Flags,	WFLG_CLOSEGADGET | WFLG_SIZEGADGET | WFLG_SIZEBBOTTOM |
			WFLG_SIZEBRIGHT | WFLG_DEPTHGADGET  |  WFLG_SMART_REFRESH | 
			 WFLG_RMBTRAP |  WFLG_ACTIVATE | 
			 WFLG_DRAGBAR | 
			WFLG_GIMMEZEROZERO | WFLG_SUPER_BITMAP ,
  	WA_Gadgets,	SuperBitMapGList,
  	WA_Title,	windtitle,
  	WA_ScreenTitle,	"Visual Arts V2.2 Copyright 1994-95 Danny Y. Wong  All Rights Reserved.",
  	WA_PubScreen,	Scr,
  	WA_MinWidth,	160,
  	WA_MinHeight,	50,
  	WA_MaxWidth,	640,
  	WA_MaxHeight,	200,
  	WA_SuperBitMap,	SuperBitMapBitmap,
  	TAG_DONE)))
  	    return(3L);

  InitTempArea();
  SuperBitMapWnd->RPort->TmpRas=&Temprast;
  SuperBitMapWnd->RPort->AreaInfo=&areaInfo;
  DrawSuperBitMapObjs();
  DrawImage(SuperBitMapWnd->RPort, &VALogo1Image, 68, 31 + kWindowOffSetY);
  SuperBitMapdoNewSize();
  GT_RefreshWindow(SuperBitMapWnd, NULL);
  RefreshGadgets(SuperBitMapGadgets[0], SuperBitMapWnd, NULL);
  return(0L);
}

void CloseSuperBitMapWindow(void)
{
  if (SuperBitMapWnd)
	CloseWindow(SuperBitMapWnd);
  if (SuperBitMapGList)
	FreeGadgets(SuperBitMapGList);
  if (SuperBitMapBitmap)
	SuperBitMapFreeBitmap();
  DisposeObject(SuperBitMapHorizGadget);
  DisposeObject(SuperBitMapVertGadget);
  DisposeObject(SuperBitMapLeftGadget);
  DisposeObject(SuperBitMapRightGadget);
  DisposeObject(SuperBitMapUpGadget);
  DisposeObject(SuperBitMapDownGadget);
  DisposeObject(SuperBitMapSizeImage);
  DisposeObject(SuperBitMapLeftImage);
  DisposeObject(SuperBitMapRightImage);
  DisposeObject(SuperBitMapUpImage);
  DisposeObject(SuperBitMapDownImage);
}

int SuperBitMapHandler(void)
{
  struct IntuiMessage	*msg;
  struct TagItem *TagList = NULL;
  ULONG temp=0, dx=0, dy=0;
  struct VAobject	VAObject;
  int running	= 1;
  int (*func)(struct VAobject VAObject);
  ULONG class;
  UWORD code;

  while (msg=GT_GetIMsg(SuperBitMapWnd->UserPort))
  {
    CopyMem((char *)msg, (char *)&SuperBitMapMsg, (long)sizeof(struct IntuiMessage));
    TagList = (struct TagItem *)msg->IAddress;
    class = msg->Class;
    code  = msg->Code;

    VAObject.va_Window = (struct Window *)SuperBitMapWnd;
    VAObject.va_Gadget = (struct Gadget *)msg->IAddress;
    VAObject.va_IntuiMsg = (struct IntuiMessage *)msg;
    VAObject.va_Flags = 0;
    VAObject.va_UserData = 0;

    GT_ReplyIMsg(msg);
    switch(class)
    {

	  case IDCMP_NEWSIZE:
	   SuperBitMapdoNewSize();
	   break;

	  case IDCMP_IDCMPUPDATE:
	    switch(GetTagData(GA_ID, 0, TagList))
	    {
		case ID_SuperBitMapHoriz:
		   temp = SuperBitMapMaxX - SuperBitMapWnd->GZZWidth;
		   temp = temp * ((struct PropInfo *)SuperBitMapHorizGadget->SpecialInfo)->HorizPot;
		   temp = temp / MAX_LEVEL;
		   dx   = temp - LAYERXOFFSET(SuperBitMapWnd);
		   if (dx)
		      SuperBitMapScrollBitmap(dx, 0);
		   break;

		case ID_SuperBitMapVert:
		   temp = SuperBitMapMaxY - SuperBitMapWnd->GZZHeight;
		   temp = temp * ((struct PropInfo *)SuperBitMapVertGadget->SpecialInfo)->VertPot;
		   temp = temp / MAX_LEVEL;
		   dy   = temp - LAYERYOFFSET(SuperBitMapWnd);
		   if (dy)
		      SuperBitMapScrollBitmap(0, dy);
		   break;

		case ID_SuperBitMapLeft:
		  if (LAYERXOFFSET(SuperBitMapWnd) > 4)
		  {
			 SuperBitMapScrollBitmap(-5, 0);
			NewModifyProp(SuperBitMapHorizGadget, SuperBitMapWnd, NULL, AUTOKNOB | FREEHORIZ,
			(MAX_LEVEL * LAYERXOFFSET(SuperBitMapWnd)) / 
			(SuperBitMapMaxX - SuperBitMapWnd->GZZWidth), NULL,
			(MAX_LEVEL * SuperBitMapWnd->GZZWidth) / SuperBitMapMaxX,
			MAX_LEVEL, 1);
		  }
		  break;
		case ID_SuperBitMapRight:
		  if ((SuperBitMapWnd->GZZWidth + LAYERXOFFSET(SuperBitMapWnd)) < SuperBitMapMaxX - 6)
		  {
		      SuperBitMapScrollBitmap(5, 0);
			NewModifyProp(SuperBitMapHorizGadget, SuperBitMapWnd, NULL, AUTOKNOB | FREEHORIZ,
			(MAX_LEVEL * LAYERXOFFSET(SuperBitMapWnd)) / 
			(SuperBitMapMaxX - SuperBitMapWnd->GZZWidth), NULL,
			(MAX_LEVEL * SuperBitMapWnd->GZZWidth) / SuperBitMapMaxX,
			MAX_LEVEL, 1);
		  }
		  break;
		case ID_SuperBitMapUp:
		  if (LAYERYOFFSET(SuperBitMapWnd) > 2)
		  {
		      SuperBitMapScrollBitmap(0, -3);
			NewModifyProp(SuperBitMapVertGadget, SuperBitMapWnd, NULL, AUTOKNOB | FREEVERT,
			NULL, (MAX_LEVEL * LAYERYOFFSET(SuperBitMapWnd)) / 
			(SuperBitMapMaxY - SuperBitMapWnd->GZZHeight), MAX_LEVEL,
			(MAX_LEVEL * SuperBitMapWnd->GZZHeight) / SuperBitMapMaxY,
			1);
		  }
		  break;
		case ID_SuperBitMapDown:
		  if ((SuperBitMapWnd->GZZHeight + LAYERYOFFSET(SuperBitMapWnd)) < SuperBitMapMaxY - 3)
		  {
		      SuperBitMapScrollBitmap(0, 3);
			NewModifyProp(SuperBitMapVertGadget, SuperBitMapWnd, NULL, AUTOKNOB | FREEVERT,
			NULL, (MAX_LEVEL * LAYERYOFFSET(SuperBitMapWnd)) / 
			(SuperBitMapMaxY - SuperBitMapWnd->GZZHeight), MAX_LEVEL,
			(MAX_LEVEL * SuperBitMapWnd->GZZHeight) / SuperBitMapMaxY,
			1);
		  }
		  break;
	    }
	    break;

	  case IDCMP_CLOSEWINDOW:
	     return(0);
	     break;

	  case IDCMP_GADGETUP:
	    func = (void *)((struct Gadget *)SuperBitMapMsg.IAddress)->UserData;
	    if (func != NULL)
	      running =  func(VAObject);
	    break;

    }
  }
  return(running);
}

void DrawSuperBitMapObjs(void)
{
  SuperBitMapDrawRects(SuperBitMapWnd);
  SuperBitMapDrawCircles(SuperBitMapWnd);
  SuperBitMapDrawLine(SuperBitMapWnd);

}

int SuperBitMapMainHandler(void)
{
  int running = 1;
  ULONG windsig, signals;

  windsig = 1L << SuperBitMapWnd->UserPort->mp_SigBit;

  while (running == 1)
  {
    signals = Wait( windsig );
    if (signals & windsig)
    {
	  running = SuperBitMapHandler();
    }
  }
  return(running);
}

int main(int argc, char *argv[])
{
  int rc;

  if (!(GetPubScreen()))
  {
	if (!(OpenSuperBitMapWindow("SuperBitMap Demo")))
	{
	  rc = SuperBitMapMainHandler();
	  CloseSuperBitMapWindow();
	}
	ClosePubScreen();
  }
  return(0L);
}

void SuperBitMapdoNewSize(void)
{
     ULONG temp;

     temp = LAYERXOFFSET(SuperBitMapWnd) + SuperBitMapWnd->GZZWidth;
     if (temp >= SuperBitMapMaxX)
	   SuperBitMapScrollBitmap(SuperBitMapMaxX - temp, 0);

     NewModifyProp(SuperBitMapHorizGadget, SuperBitMapWnd, NULL, AUTOKNOB | FREEHORIZ,
		(MAX_LEVEL * LAYERXOFFSET(SuperBitMapWnd)) / 
		(SuperBitMapMaxX - SuperBitMapWnd->GZZWidth),
		 NULL,
		(MAX_LEVEL * SuperBitMapWnd->GZZWidth) / SuperBitMapMaxX,
		 MAX_LEVEL, 1);

     temp = LAYERYOFFSET(SuperBitMapWnd) + SuperBitMapWnd->GZZHeight;
     if (temp >= SuperBitMapMaxY)
	  SuperBitMapScrollBitmap(0, SuperBitMapMaxY - temp);

     NewModifyProp(SuperBitMapVertGadget, SuperBitMapWnd, NULL, AUTOKNOB | FREEVERT,
		 NULL,
		(MAX_LEVEL * LAYERYOFFSET(SuperBitMapWnd)) / 
		(SuperBitMapMaxY - SuperBitMapWnd->GZZHeight),
		 MAX_LEVEL,
		(MAX_LEVEL * SuperBitMapWnd->GZZHeight) / SuperBitMapMaxY,
		 1);
}

void SuperBitMapFreeBitmap(void)
{
	short x;

	for (x=0; x < SuperBitMapDepth; x++)
	{
	     if (SuperBitMapBitmap->Planes[x] != NULL)
		FreeRaster(SuperBitMapBitmap->Planes[x], SuperBitMapMaxX, SuperBitMapMaxY);
	}
	FreeMem(SuperBitMapBitmap, sizeof(struct BitMap));
	SuperBitMapBitmap = NULL;
}


void SuperBitMapInitNewBitmap(void)
{
	short x;

	if (SuperBitMapBitmap = AllocMem(sizeof(struct BitMap), MEMF_PUBLIC | MEMF_CLEAR))
	{
	   InitBitMap(SuperBitMapBitmap, SuperBitMapDepth, SuperBitMapMaxX, SuperBitMapMaxY);
	   for (x=0; x < SuperBitMapDepth; x++)
	   {
	     if ((SuperBitMapBitmap->Planes[x] = AllocRaster(SuperBitMapMaxX, SuperBitMapMaxY)) == NULL)
		return;
	     BltClear(SuperBitMapBitmap->Planes[x], RASSIZE(SuperBitMapMaxX, SuperBitMapMaxY), 0);
	   }
	}
}


void SuperBitMapScrollBitmap(ULONG x, ULONG y)
{
	ScrollLayer(0, SuperBitMapWnd->RPort->Layer, x ,y);
	WaitTOF();
}
