/*
**	GadTools layout toolkit
**
**	Copyright © 1993-1996 by Olaf `Olsen' Barthel
**		Freely distributable.
**
**	:ts=4
*/

#ifndef _GTLAYOUT_GLOBAL_H
#include "gtlayout_global.h"
#endif

#ifdef DO_HEXHOOK
ULONG __saveds __asm
LTP_HexEditRoutine(REG(a0) struct Hook *hook,REG(a2) struct SGWork *sgw,REG(a1) ULONG *msg)
{
	ULONG result = FALSE;

	if(*msg == SGH_KEY)
	{
		LayoutHandle *handle;
		ObjectNode *node;
		BOOL activate;

		handle = hook->h_Data;
		activate = TRUE;

		if(sgw->IEvent->ie_Code == 0x5F && handle->HelpHook)
		{
			sgw->Code		= 0x5F;
			sgw->Actions	= (sgw->Actions & ~SGA_BEEP) | SGA_END | SGA_USE;

			LTP_AddHistory(sgw);

			return(TRUE);
		}

		if(sgw->IEvent->ie_Code == CURSORUP || sgw->IEvent->ie_Code == CURSORDOWN)
			LTP_HandleHistory(sgw);

		if(GETOBJECT(sgw->Gadget,node))
		{
			if(node->Type == INTEGER_KIND)
			{
				if(node->Special.Integer.LastGadget)
					activate = FALSE;

				result = TRUE;

				if(!LTP_ConvertNum((node->Min < 0),sgw->WorkBuffer,(LONG *)&sgw->StringInfo->LongInt))
				{
					sgw->EditOp		= EO_BADFORMAT;
					sgw->Actions	= SGA_BEEP;
				}

				if(node->Special.Integer.ValidateHook)
				{
					if(!CallHookPkt(node->Special.Integer.ValidateHook,sgw,msg))
					{
						sgw->EditOp		= EO_BADFORMAT;
						sgw->Actions	= SGA_BEEP;
					}
				}
			}
		}

		if(sgw->EditOp == EO_ENTER && !(sgw->IEvent->ie_Qualifier & QUALIFIER_SHIFT))
		{
			if(activate && handle->AutoActivate)
				sgw->Actions |= SGA_NEXTACTIVE;

			if(!(sgw->Actions & SGA_NEXTACTIVE))
				sgw->Code = '\r';
		}

		if(sgw->Actions & SGA_END)
			LTP_AddHistory(sgw);
	}

	return(result);
}
#endif
