#include "global.h"

#include <stdio.h>
#include <string.h>
#include <exec/memory.h>
#include <libraries/asl.h>

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


void WriteStruct(USHORT code)
{
	LinkNode		*nodePtr;
	GadgetObj	*gadgObj;
	
	USHORT		gadCount = 0,
					tagCount = 0,
					stringCount = 0,
					rectCount = 0,
					iTextCount = 0,
					itemNum, totItems;
	short			*newGadgetIndex = 0L,
					*tagItemIndex = 0L,
					*stringIndex = 0L,
					*rectIndex = 0L,
					*iTextIndex = 0L;
	BOOL			first = TRUE;
	
	totItems = NodeCount( (void **) &gObjList);
	if (!totItems)
		return;
	
	if (	!GetMem( (void **) &newGadgetIndex, (ULONG) totItems * (ULONG) sizeof(short) ) ||
			!GetMem( (void **) &tagItemIndex, (ULONG) totItems * (ULONG) sizeof(short) ) ||
			!GetMem( (void **) &stringIndex, (ULONG) totItems * (ULONG) sizeof(short) ) ||
			!GetMem( (void **) &rectIndex, (ULONG) totItems * (ULONG) sizeof(short) ) ||
			!GetMem( (void **) &iTextIndex, (ULONG) totItems * (ULONG) sizeof(short) ) )
		goto cleanup;
	
	for (itemNum=0; itemNum<totItems; itemNum++)
	{
		newGadgetIndex[itemNum] = -1;
		tagItemIndex[itemNum] = -1;
	}
	
	// first, do the NewGadget structures

	for (nodePtr=gObjList, itemNum=0; nodePtr; nodePtr=nodePtr->next, itemNum++)
	{
		if (GetGadgetData(nodePtr, 0L, 0L, 0L, 0L, 0L))
		{
			gadgObj = (GadgetObj *) nodePtr;
			
			newGadgetIndex[itemNum] = gadCount++;

			if (first)
			{
				printf("//  NewGadget structure for all gadgets\n\n");
				printf("struct NewGadget gResNewGadget[] =\n{\n");
				first = FALSE;
			}

//			printf("// ++++++++++++++++++++++++++++++++\n");
			printf("\t// Item Number %d (type is %s)\n", itemNum, objName[nodePtr->type]);
			printf("\t{\t%3d, %3d, %3d, %3d, \"%s\", 0L, %3d,\n",
							gadgObj->rect.minX, gadgObj->rect.minY,
							gadgObj->rect.maxX - gadgObj->rect.minX + 1,
							gadgObj->rect.maxY - gadgObj->rect.minY + 1,
							gadgObj->label, itemNum);
			printf("\t\t");
			if (gadgObj->flags == 0)
				printf("0L, ");
			else
			{
				if (gadgObj->flags & PLACETEXT_IN)
					printf("PLACETEXT_IN");
				else if (gadgObj->flags & PLACETEXT_ABOVE)
					printf("PLACETEXT_ABOVE");
				else if (gadgObj->flags & PLACETEXT_RIGHT)
					printf("PLACETEXT_RIGHT");
				else if (gadgObj->flags & PLACETEXT_LEFT)
					printf("PLACETEXT_LEFT");
				else if (gadgObj->flags & PLACETEXT_BELOW)
					printf("PLACETEXT_BELOW");
				
				printf(", ");
			}
			printf("gVI, 0L },\n");
		}
	}
	
	if (!first)
		printf("};\n\n");
		
	// second, do the tagitem arrays

	first = TRUE;
	for (nodePtr=gObjList, itemNum=0; nodePtr; nodePtr=nodePtr->next, itemNum++)
	{
		if (GetGadgetData(nodePtr, 0L, 0L, 0L, 0L, 0L))
		{
			gadgObj = (GadgetObj *) nodePtr;
			
			tagItemIndex[itemNum] = tagCount;

			if (first)
			{
				printf("//  TagItem array for all gadgets\n\n");
				printf("ULONG gResTags[] =\n{\n");
				first = FALSE;
			}

			printf("\t// Item Number %d (type is %s)\n", itemNum, objName[nodePtr->type]);
			
			switch(nodePtr->type)
			{
				case OTYPE_Button:
					PutTF(gadgObj->disable);
					printf(",\n");
					tagCount++;
					break;
				case OTYPE_Cycle:
					PutTF(gadgObj->disable);
					printf(", %d,\n", ( (CycleObj *) gadgObj)->activeItem);
					tagCount += 2;
					break;
				case OTYPE_MX:
					PutTF(gadgObj->disable);
					printf(", %d, %d\n", ( (MXObj *) gadgObj)->activeItem,
												( (MXObj *) gadgObj)->extraSpacing);
					tagCount += 3;
					break;
			}
		}
	}
	
	if (!first)
		printf("};\n\n");
		
		
	// now, do the strings

	first = TRUE;
	for (nodePtr=gObjList, itemNum=0; nodePtr; nodePtr=nodePtr->next, itemNum++)
	{
		if (nodePtr->type == OTYPE_Cycle || nodePtr->type == OTYPE_MX ||
				nodePtr->type == OTYPE_ListView)
		{
			gadgObj = (GadgetObj *) nodePtr;
			
			stringIndex[itemNum] = stringCount;

			if (first)
			{
				printf("//  String array for all gadgets\n\n");
				printf("ULONG gResStrings[] =\n{\n");
				first = FALSE;
			}

			printf("\t// Item Number %d (type is %s)\n", itemNum, objName[nodePtr->type]);
			
			switch(nodePtr->type)
			{
				case OTYPE_Button:
					break;
			}
		}
	}
	
	if (!first)
		printf("};\n\n");
		
cleanup:
	if (newGadgetIndex)
		DropMem( (void **) &newGadgetIndex, (ULONG) totItems * (ULONG) sizeof(short) );
	if (tagItemIndex)
		DropMem( (void **) &tagItemIndex, (ULONG) totItems * (ULONG) sizeof(short) );
	if (stringIndex)
		DropMem( (void **) &stringIndex, (ULONG) totItems * (ULONG) sizeof(short) );
	if (iTextIndex)
		DropMem( (void **) &iTextIndex, (ULONG) totItems * (ULONG) sizeof(short) );
	if (rectIndex)
		DropMem( (void **) &rectIndex, (ULONG) totItems * (ULONG) sizeof(short) );

}

void PutTF(BOOL flag)
{
	printf("%s", flag ? "TRUE" : "FALSE");
}

