/* 
Copyright (c) 1991-2000 UserLand Software, Inc. 

Permission is hereby granted, free of charge, to any person obtaining a 
copy of this software and associated documentation files (the 
"Software"), to deal in the Software without restriction, including 
without limitation the rights to use, copy, modify, merge, publish, 
distribute, sublicense, and/or sell copies of the Software, and to 
permit persons to whom the Software is furnished to do so, subject to the 
following conditions: 

The above copyright notice and this permission notice shall be 
included in all copies or substantial portions of the Software. 

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
*/ 


#include "iowacore.h"
#include "iowaicons.h"
#include <appletresource.h>



short iconsinpalette = 2;


typedef struct tyiconrecord {
	
	Handle hsmallicon;
	
	long objectTypeID;
	} tyiconrecord;
	
	
static tyiconrecord **iconarray = nil; /*dynamic array of icon records*/


static short counticonsinarray (void) {
	
	if (iconarray == nil)
		return (0);
		
	return ((short) (GetHandleSize ((Handle) iconarray) / longsizeof (tyiconrecord)));
	} /*counticonsinarray*/
	
	
boolean addtoiconarray (Handle hsmallicon, long objectTypeID) {
	
	tyiconrecord rec;
	short ct;
	
	if (hsmallicon == nil) /*if it has no icon, it doesn't appear in palette*/
		return (true);
	
	rec.hsmallicon = hsmallicon;
	
	rec.objectTypeID = objectTypeID;
	
	if (iconarray == nil)
		return (newfilledhandle (&rec, longsizeof (tyiconrecord), (Handle *) &iconarray));
	
	ct = counticonsinarray ();
	
	SetHandleSize ((Handle) iconarray, (ct + 1) * longsizeof (tyiconrecord));

	if (MemError () != noErr)
		return (false);
	
	(*iconarray) [ct] = rec;
	
	return (true);
	} /*addtoiconarray*/
	
	
typedef struct tymoderecord {
	
	short ixpalette;
	
	long objectTypeID;
	} tymoderecord;
	
	
static tymoderecord **modearray = nil; /*dynamic array of modes*/


static short countmodes (void) {
	
	if (modearray == nil)
		return (0);
		
	return ((short) (GetHandleSize ((Handle) modearray) / longsizeof (tymoderecord)));
	} /*countmodes*/
	
	
static boolean addmode (short ixpalette, long objectTypeID) {

	tymoderecord rec;
	short ct;
	
	rec.ixpalette = ixpalette;
	
	rec.objectTypeID = objectTypeID;
	
	if (modearray == nil)
		return (newfilledhandle (&rec, longsizeof (tymoderecord), (Handle *) &modearray));
	
	ct = countmodes ();
	
	SetHandleSize ((Handle) modearray, (ct + 1) * longsizeof (tymoderecord));

	if (MemError () != noErr)
		return (false);
	
	(*modearray) [ct] = rec;
	
	return (true);
	} /*addmode*/
	
	
long modetoobjecttype (short ixpalette) {
	
	short ct, i;
	tymoderecord rec;
	
	if ((ixpalette == arrowmode) || (ixpalette == textmode)) /*no object created in this mode*/
		return (-1);
		
	ct = countmodes ();
	
	for (i = 0; i < ct; i++) {
		
		rec = (*modearray) [i];
		
		if (rec.ixpalette == ixpalette)
			return (rec.objectTypeID);
		} /*for*/
		
	return (-1); /*not found*/
	} /*modetoobjecttype*/
	

static boolean iconsearch (tyobjecttype type, Handle hlist) {

	short ct, i;
	tyiconrecord rec;

	ct = counticonsinarray ();
	
	for (i = 0; i < ct; i++) {
		
		rec = (*iconarray) [i];
		
		if (rec.objectTypeID == type) {
		
			pushhandleonhandle (rec.hsmallicon, hlist);
	
			disposehandle (rec.hsmallicon);
			
			(*iconarray) [i].hsmallicon = nil;
		
			addmode (iconsinpalette++, rec.objectTypeID);
			
			return (true);
			}
		} /*for*/
	
	return (false);
	} /*iconsearch*/
	
	
boolean converticonarray (void) {
	
	short ct, i;
	Handle hiconlist;
	tyiconrecord rec;
	
	iconsinpalette = 2;
	
	getresourcehandle (0, 'SICN', 130, &hiconlist); /*get the arrow and ibeam sicns*/
	
	iconsearch (statictexttype, hiconlist); /*pull out the standard icon types first...*/
	
	iconsearch (edittexttype, hiconlist); /*...in an order that makes sense*/
	
	iconsearch (checkboxtype, hiconlist);
	
	iconsearch (radiobuttontype, hiconlist);
	
	iconsearch (buttontype, hiconlist);
	
	iconsearch (popuptype, hiconlist);
	
	iconsearch (icontype, hiconlist);
	
	iconsearch (picturetype, hiconlist);
	
	iconsearch (formulatype, hiconlist);
	
	iconsearch (recttype, hiconlist);
	
	iconsearch (ovaltype, hiconlist);
	
	iconsearch (linetype, hiconlist);
	
	ct = counticonsinarray ();
	
	for (i = 0; i < ct; i++) { /*pull out the non-UserLand-supplied icons*/
		
		rec = (*iconarray) [i];
		
		if (rec.hsmallicon != nil) {
		
			pushhandleonhandle (rec.hsmallicon, hiconlist);
	
			disposehandle (rec.hsmallicon);
		
			addmode (iconsinpalette++, rec.objectTypeID);
			}
		} /*for*/
	
	putresourcehandle (0, 'SICN', 132, hiconlist);
	
	disposehandle (hiconlist);
	
	disposehandle ((Handle) iconarray);
	
	iconarray = nil;
	
	return (true);
	} /*converticonarray*/