/*  GadTools layout toolkit
**
**  Copyright © 1993-1995 by Olaf `Olsen' Barthel
**  Freely distributable.
*/

#include "gtlayout_global.h"

#ifdef DO_FRACTION_KIND
ULONG __asm __saveds
LTP_FracEditRoutine(register __a0 struct Hook *Hook,register __a2 struct SGWork *Work,register __a1 ULONG *Msg)
{
	if(*Msg == SGH_KEY)
	{
		if(Work -> IEvent -> ie_Code == 0x5F)
		{
			LayoutHandle *Handle = Hook -> h_Data;

			if(Handle -> HelpHook)
			{
				Work -> Code	= 0x5F;
				Work -> Actions	= (Work -> Actions & ~SGA_BEEP) | SGA_END | SGA_USE;

				LTP_AddHistory(Work);

				return(TRUE);
			}
		}

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

		if(Work -> EditOp == EO_ENTER)
		{
			LayoutHandle	*Handle = Hook -> h_Data;
			ObjectNode	*Node;
			BOOLEAN 	 Activate = TRUE;

			if(GETOBJECT(Work -> Gadget,Node))
			{
				if(Node -> Type == FRACTION_KIND && Node -> Special . String . LastGadget)
					Activate = FALSE;
			}

			if(!(Work -> IEvent -> ie_Qualifier & QUALIFIER_SHIFT))
			{
				if(Activate && Handle -> AutoActivate)
					Work -> Actions |= SGA_NEXTACTIVE;

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

		if(Work -> EditOp == EO_INSERTCHAR || Work -> EditOp == EO_REPLACECHAR)
		{
			UBYTE DecimalPoint = (LTP_Locale ? LTP_Locale -> loc_DecimalPoint[0] : '.');

			if(Work -> Code == DecimalPoint || (Work -> Code >= '0' && Work -> Code <= '9'))
			{
				if(Work -> Code == DecimalPoint)
				{
					BOOLEAN UseIt = TRUE;
					LONG	i;

					for (i = 0 ; UseIt && i < Work -> NumChars ; i++)
					{
						if(Work -> PrevBuffer[i] == DecimalPoint)
							UseIt = FALSE;
					}

					if(!UseIt)
					{
						Work -> Actions &= ~SGA_USE;
						Work -> Actions |=  SGA_BEEP;
					}
				}
			}
			else
			{
				Work -> Actions &= ~SGA_USE;
				Work -> Actions |=  SGA_BEEP;
			}
		}

		if(Work -> Actions & SGA_END)
			LTP_AddHistory(Work);
	}
	else
	{
		if(*Msg != SGH_CLICK)
			return(FALSE);
	}

	return(TRUE);
}
#endif
