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

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

VOID
LTP_HandleHistory(struct SGWork *Work)
{
	ObjectNode *Node;

	if(GETOBJECT(Work->Gadget,Node))
	{
		if(Node->Special.String.HistoryHook)
		{
			struct Node	*Choice = NULL;
			struct MinList	*List	= Node->Special.String.HistoryHook->h_Data;

			if(Work->IEvent->ie_Qualifier & QUALIFIER_SHIFT)
			{
				if(Work->IEvent->ie_Code == CURSORUP)
				{
					if(List->mlh_Head->mln_Succ)
						Choice = (struct Node *)List->mlh_Head;
				}
				else
				{
					if(List->mlh_Head->mln_Succ)
						Choice = (struct Node *)List->mlh_TailPred;
				}
			}
			else
			{
				struct Node *Current = Node->Special.String.CurrentNode;

				if(Work->IEvent->ie_Code == CURSORUP)
				{
					if(Current)
					{
						if(Current->ln_Pred->ln_Pred)
							Choice = Current->ln_Pred;
						else
							Choice = Current;
					}
					else
					{
						if(List->mlh_Head->mln_Succ)
							Choice = (struct Node *)List->mlh_TailPred;
					}
				}
				else
				{
					if(Current)
					{
						if(Current->ln_Succ->ln_Succ)
							Choice = Current->ln_Succ;
					}
				}
			}

			if(Choice != Node->Special.String.CurrentNode)
			{
				if(Choice)
				{
					strcpy(Work->WorkBuffer,Choice->ln_Name);

					Work->NumChars = Work->BufferPos = strlen(Work->WorkBuffer);
				}
				else
				{
					Work->WorkBuffer[0]	= 0;
					Work->NumChars	= 0;
					Work->BufferPos	= 0;
				}

				if(Node->Type == INTEGER_KIND)
				{
#ifdef DO_HEXHOOK
					LTP_ConvertNum((Node->Min < 0),Work->WorkBuffer,&Work->LongInt);
#else
					Work->LongInt = LTP_Atol(Work->WorkBuffer);
#endif
				}

				Node->Special.String.CurrentNode = Choice;

				Work->Actions = (Work->Actions & ~SGA_BEEP) | SGA_USE | SGA_REDISPLAY;
			}
		}
	}
}
