
/* doslbnc.ld  - Copyright 1988 Commodore-Amiga, Inc. */

#include <exec/types.h>
#include <exec/memory.h>
#include <graphics/gfx.h>
#include <graphics/gfxbase.h>
#include <graphics/clip.h>
#include <graphics/rastport.h>
#include <graphics/layers.h>
#include <intuition/intuition.h>

#define DEPTH	2

/*#define DEBUG*/

/* #define KPRINTF */

#ifdef KPRINTF
#define printf kprintf
#endif

extern int GfxBase;

int HEIGHT = 512;
int WIDTH = 1024-16;

#define RBG_ID 0x0001
#define BBG_ID 0x0002

int usable_width(w)
struct Window *w;
{
   return (w->Width - w->BorderLeft - w->BorderRight);
}

int usable_height(w)
struct Window *w;
{
   return(w->Height - w->BorderTop - w->BorderBottom);
}

do_gadget_down(msg)
struct IntuiMessage *msg;
{
LONG error = FALSE;

#ifdef DEBUG
    printf("do_gadget_down: entering...\n");
#endif

    /* stub */

#ifdef DEBUG
    printf("do_gadget_down: exiting...\n");
#endif

    return(error);
}

do_gadget_up(msg)
struct IntuiMessage *msg;
{
LONG error = FALSE;
struct Window *w;
struct RastPort *rp;
struct Layer *l;
struct LayerInfo *li;
struct Gadget *g;
UWORD gid;
struct PropInfo *pi;

#ifdef DEBUG
    printf("do_gadget_up: entering...\n");
#endif

    if(msg)
    {
        if(g = (struct Gadget *)msg->IAddress)
		{

			gid = g->GadgetID;
			switch(gid)
			{
				case(RBG_ID): 
				{
					/* scroll vertically */
#ifdef DEBUG
					printf("do_gadget_up: scroll vertically...\n");
#endif

					if(w = msg->IDCMPWindow)
					{
						if(rp = w->RPort)
						{
							if(l = rp->Layer)
							{
								if(li = l->LayerInfo)
								{
									WORD abs_scroll;
									WORD delta_scroll;
									WORD max_scroll;
									WORD sb_height;

									pi = g->SpecialInfo;
							
									sb_height = l->SuperBitMap->Rows;

									abs_scroll = (((pi->VertPot + 1) * (sb_height - usable_height(w))) >> 16);

									delta_scroll = abs_scroll - l->Scroll_Y;

									ScrollLayer(li,l,0,delta_scroll);
								}
							}
						}
					}
					break;
				}

				case(BBG_ID): 
				{
					/* scroll horizontally */
#ifdef DEBUG
					printf("do_gadget_up: scroll horizontally...\n");
#endif
					if(w = msg->IDCMPWindow)
					{
						if(rp = w->RPort)
						{
							if(l = rp->Layer)
							{
								if(li = l->LayerInfo)
								{
									WORD abs_scroll;
									WORD delta_scroll;
									WORD max_scroll;
									WORD sb_width;

									pi = g->SpecialInfo;
						
									sb_width = l->SuperBitMap->BytesPerRow << 3;

									abs_scroll = (((pi->HorizPot + 1) * (sb_width - usable_width(w))) >> 16);

									delta_scroll = abs_scroll - l->Scroll_X;

									ScrollLayer(li,l,delta_scroll,0);
								}
							}
						}
					}
					break;
				}

				default:
				{
					/* unknown gid */
#ifdef DEBUG
					printf("do_gadget_up: unknown gadget_id...ERROR = TRUE!\n");
#endif
					error = TRUE;
					break;
				}
			}
		}
    	else
    	{
    	    error = TRUE;
    	} 
    } 
    else
    {
	error = TRUE;
    } 

#ifdef DEBUG
    printf("do_gadget_up: exiting...\n");
#endif

    return(error);
}

dots(vp,rp)
struct ViewPort *vp;
struct RastPort *rp;
{
#ifdef DEBUG
	printf("rp=%lx\n",rp);
#endif
	lbnc(rp);
}

struct BitMap bm;	/* assume initialized to 0 */

main()
{
	int i;

	GfxBase = OpenLibrary("graphics.library",32);

	if (AvailMem(MEMF_CHIP) < 100000)	/* for eentsy machines */
	{
		HEIGHT = 256;
		WIDTH = 640;
	}

	InitBitMap(&bm,DEPTH,WIDTH,HEIGHT);

	for(i = 0; i< DEPTH ; i++)
	{
		if ( (bm.Planes[i] = (PLANEPTR)AllocRaster(WIDTH,HEIGHT)) == 0)
		{

#ifdef DEBUG
			printf("no mem for bitmap\n");
#endif
			for (i = 0; i< DEPTH; i++)
			{
				if (bm.Planes[i])	
				{
					FreeRaster(bm.Planes[i],WIDTH,HEIGHT);
				}
			}
			CloseLibrary(GfxBase);
			exit();
		}
		else	
		{
			BltClear(bm.Planes[i],RASSIZE(WIDTH,HEIGHT));
		}
	}

	startgfx(200,100,50,100,DEPTH,0,dots,"Lines Window",
		GIMMEZEROZERO|SMART_REFRESH|SUPER_BITMAP,&bm);

	for(i = 0; i< DEPTH ; i++)
	{
		FreeRaster(bm.Planes[i],WIDTH,HEIGHT);
	}

	CloseLibrary(GfxBase);

}

