/* 
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 <iac.h>
#include "iowacore.h"
#include <applet.h>
#include "iowapack.h"
#include "iowaobject.h"
#include <appletmain.h>
#include "iowaiac.h"



static long objectcount;
static hdlobject objecthandle;
static long objectnumber;





boolean IACcleanobject (Handle objectname) {

	hdlobject hobject;

	if (namedobjecterror (objectname, &hobject))
		return (false);
	
	initmoveinfo (); /*in case things change*/
	
	cleanobject (hobject);
	
	invalobject (hobject); /*guaranteed to update*/
	
	invalfrommoveinfo (); /*finish process started by cleanobject*/
	
	iowaupdatenow ();
	
	return (true);
	} /*IACcleanobject*/
	
	
boolean IACrenameobject (Handle objectname, Handle newname) {

	hdlobject hobject;

	if (namedobjecterror (objectname, &hobject))
		return (false);
	
	setobjectname (hobject, newname);
	
	return (true);
	} /*IACrenameobject*/
	

boolean IACdeleteobject (Handle objectname) {

	hdlobject hobject;

	if (namedobjecterror (objectname, &hobject))
		return (false);
	
	if ((**iowadata).activetextobject == hobject)
		clearactivetextobject ();
		
	invalobject (hobject);
	
	unlinkfromselection (hobject);
	
	unlinkobject (hobject);
	
	disposeobject (hobject);
	
	return (true);
	} /*IACdeleteobject*/
	

boolean IACnewobject (short objecttype, Handle objectname, Handle objectvalue, Handle objectscript) {
	
	hdlcard hc = iowadata;
	short gridunits = (**hc).gridunits;
	tyobject obj;
	hdlobject h;
	
	if (nocarderror ()) 
		return (false);
	
	defaultobject (&obj);
	
	obj.objecttype = objecttype; 
	
	/*determine the object's rectangle*/ {
		
		Point pt = (**hc).defaultnewobjectlocation;
		Rect r;
		
		r.top = pt.v;
		
		r.bottom = r.top + gridunits;
		
		r.left = pt.h;
		
		r.right = r.left + gridunits;
		
		obj.objectrect = r;
		}
	
	callinitobject (&obj);
	
	if (!newobject (obj, &h))
		return (false);
		
	(**h).objectname = objectname;
	
	setobjectvalue (h, objectvalue);
		
	setobjectscript (h, objectscript);
		
	(**h).objecttmpbit = false; /*could have been set by callinitobject*/
	
	madechanges ();
	
	return (true);
	
	
	
	initmoveinfo (); 
	
	cleanobject (h);
	
	(**hc).defaultnewobjectlocation.v = makemultiple ((**h).objectrect.bottom + 1, gridunits);
	
	invalobject (h); 
	
	invalfrommoveinfo (); 
	
	/*iowaupdatenow ();*/
	
	return (true);
	} /*IACnewobject*/
	

static boolean countobjectsvisit (hdlobject h) {
	
	objectcount++;
	
	return (true);
	} /*countobjectsvisit*/
	
	
long IACcountobjects (void) {
	
	if (nocarderror ()) 
		return (false);
	
	objectcount = 0;
	
	visitobjects ((**iowadata).objectlist, &countobjectsvisit);
	
	return (objectcount);
	} /*IACcountobjects*/
	
	
long IACcountselectedobjects (void) {

	if (nocarderror ()) 
		return (false);
	
	objectcount = 0;
	
	flatvisitselectedobjects (&countobjectsvisit);
	
	return (objectcount);
	} /*IACcountselectedobjects*/
	
	
static boolean getnthobjectvisit (hdlobject h) {
	
	objectcount++;
	
	if (objectcount == objectnumber) {
		
		objecthandle = h;
		
		return (false);
		}
	
	return (true);
	} /*getnthobjectvisit*/
	
	
Handle IACgetnthobject (long n) { /*return the name of the nth object*/
	
	if (nocarderror ()) 
		return (false);
	
	objectcount = 0;
	
	objecthandle = nil;
	
	objectnumber = n;
	
	visitobjects ((**iowadata).objectlist, &getnthobjectvisit);
	
	if (objecthandle == nil)
		return (nil);
	
	return ((**objecthandle).objectname);
	} /*IACgetnthobject*/
	

Handle IACgetnthselectedobject (long n) { /*return the name of the nth selected object*/
	
	if (nocarderror ()) 
		return (false);
	
	objectcount = 0;
	
	objecthandle = nil;
	
	objectnumber = n;
	
	flatvisitselectedobjects (&getnthobjectvisit);
	
	if (objecthandle == nil)
		return (nil);
	
	return ((**objecthandle).objectname);
	} /*IACgetnthselectedobject*/
	

boolean IACupdateobject (Handle objectname) {

	hdlobject hobject;

	if (namedobjecterror (objectname, &hobject))
		return (false);
	
	invalobject (hobject);
	
	iowaupdatenow ();
	
	return (true);
	} /*IACupdateobject*/
	

boolean IACpackobject (Handle objectname, Handle *hpacked) {
		
	hdlcard hc = iowadata;
	hdlobject hobject;

	if (namedobjecterror (objectname, &hobject))
		return (false);
	
	*hpacked = nil;		/*1/22/96 dmb: was ==
	
	if (hobject == nil) /*nothing selected*/
		return (false);
		
	if (!newclearhandle ((long) 0, hpacked))
		return (false);
		
	if (!packobject (*hpacked, hobject, true))
		goto error;
		
	return (true);
	
	error:
	
	disposehandle (*hpacked);
	
	*hpacked = nil;
	
	return (false);
	} /*IACpackobject*/
	
	
static void runmodefilter (EventRecord *ev) {
	
	switch ((*ev).what) {
		
		case iowaButtonHitEvent:
			(*ev).what = iowaRunScriptEvent;
			
			break;
			
		default:
			appleteventhandler (ev);
			
			break;
		} /*switch*/
	} /*runmodefilter*/
	

boolean IACeditspecialattributes (Handle objectname) {

	hdlobject hobject;
	Handle hpackedcard;

	if (namedobjecterror (objectname, &hobject))
		return (false);
		
	callgetcard (hobject, &hpackedcard);
	
	runModalCard (hpackedcard, true, 0, 0, &runmodefilter);
	
	return (true);
	} /*IACeditspecialattributes*/
	

void FrontierJump (Handle hname) {
	
	AppleEvent event, reply;
	
	if (!IACnewverb ('LAND', 'IOWA', 'jump', &event))
		return;
		
	IACglobals.event = &event;
	
	if (!IACpushtextparam (hname, 'prm1'))
		goto error;
		
	IACsendverb (&event, &reply);
	
	error:
	
	AEDisposeDesc (&event);	
	} /*FrontierJump*/