/*
** Gadinfo - get full info on the system gadgets
**
** Version 1.0
**
** Author: Edward Hutchins
**
** This program will probably die a horrible death under 1.4!
*/

#define INTUITIONPRIVATE

#include <local/typedefs.h>
#include <intuition/intuition.h>
#include <intuition/intuitionbase.h>

/*
** Global variables
*/

IBASE			*IntuitionBase;
 
typedef struct
{
	WORD		Res;
	WORD		Index;
} GadList;

GadList			NewGads[] =
{
	{ HIRESGADGET, UPFRONTGADGET },
	{ HIRESGADGET, DOWNBACKGADGET },
	{ HIRESGADGET, SIZEGADGET },
	{ HIRESGADGET, CLOSEGADGET },
	{ HIRESGADGET, SUPFRONTGADGET },
	{ HIRESGADGET, SDOWNBACKGADGET },
	{ LOWRESGADGET, UPFRONTGADGET },
	{ LOWRESGADGET, DOWNBACKGADGET },
	{ LOWRESGADGET, SIZEGADGET },
	{ LOWRESGADGET, CLOSEGADGET },
	{ LOWRESGADGET, SUPFRONTGADGET },
	{ LOWRESGADGET, SDOWNBACKGADGET },
	{ RESCOUNT, 0 }
};

char			*GadNames[] =
{
"UPFRONTGADGET",
"DOWNBACKGADGET",
"SIZEGADGET",
"CLOSEGADGET",
"DRAGGADGET",
"SUPFRONTGADGET",
"SDOWNBACKGADGET",
"SDRAGGADGET"
};

/*
** printimage - print the contents of an image structure
*/

void printimage( IMAGE *img )
{
	int			x, y, w, h, d, bit;
	USHORT		*Bitplanes, *bitplane;
	char		Bits[6*16+1];
	char		*p;

	if (img == NULL)
	{
		printf( "Not an image.\n" );
		return;
	}
	
	printf( "Image at :%lx\n", (long)img );
	printf( "(%d,%d)-(%d,%d) × %d\n", img->TopEdge, img->LeftEdge, img->Width, img->Height, img->Depth );
	printf( "PlanePick: %x  PlaneOnOff: %x\n", img->PlanePick, img->PlaneOnOff );

	w = ((img->Width + 15) & ~15);
	h = (w / 16) * img->Height;
	Bitplanes = img->ImageData;

	for (y = 0; y < img->Height; ++y)
	{
		p = Bits;
		for (x = 0; x < w; ++x)
		{
			*p = '\0';
			bit = 1 << (15-(x & 15));
			bitplane = Bitplanes + (x / 16);
			for (d = 0; d < 2 /* img->Depth */; ++d)
			{
				*p |= (*bitplane & bit) ? (1 << d) : 0;
				bitplane += h;
			}
			*p += (*p < 10) ? '0' : 'A';
			++p;
		}
		*p = '\0';
		printf( ">%s\n", Bits );
		Bitplanes += (w / 16);
	}
}

/*
** printgad - print the specs on a gadget
*/

void printgad( GADGET *gad )
{
	printf( "Gadget at :%lx\n", (long)gad );
	printf( "(%d,%d)-(%d,%d)\n", gad->TopEdge, gad->LeftEdge, gad->Width, gad->Height );
	printf( "Flags: %x  Acitvation: %x  Type: %x\n", gad->Flags, gad->Activation, gad->GadgetType );
	printimage( gad->GadgetRender );
	printimage( gad->SelectRender );
}

/*
** main - just look for now
*/

void main( int argv, char *argc[] )
{
	GadList			*glist = NewGads;
	GADGET			*gad;
	int				t;

	IntuitionBase = OpenLibrary( "intuition.library", 33 );
	if (IntuitionBase == NULL) return;

	while (glist->Res != RESCOUNT)
	{
		gad = IntuitionBase->SysGadgets[glist->Res][glist->Index];
		printf( "\n%s mode: %d\n", GadNames[glist->Index], glist->Res );
		printgad( gad );
		++glist;		
	}

	printf( "\nSystem patterns:\nDrag Bar\n" );

	for (t = 0; t < 8; ++t)
		printf( ">%4x\n", IntuitionBase->apattern[t] );

	printf( "\nDots:\n" );

	for (t = 0; t < 4; ++t)
		printf( ">%4x\n", IntuitionBase->bpattern[t] );
}
