/* $Revision Header * Header built automatically - do not edit! *************
 *
 *	(C) Copyright 1990 by Olaf 'Olsen' Barthel & MXM
 *
 *	Name .....: StringHook.c
 *	Created ..: Wednesday 06-Feb-91 12:35
 *	Revision .: 0
 *
 *	Date            Author          Comment
 *	=========       ========        ====================
 *	06-Feb-91       Olsen           Created this file!
 *
 * $Revision Header ********************************************************/

#include "TermGlobal.h"

	/* HookEntry():
	 *
	 *	Call a hook function.
	 */

ULONG __saveds __asm
HookEntry(register __a0 struct Hook *HookPtr,register __a2 APTR Object,register __a1 APTR Message)
{
	return(HookPtr -> h_SubEntry(HookPtr,Object,Message));
}

	/* InitHook(struct Hook *Hook,APTR HookCode):
	 *
	 *	Set up a call back routine (hook).
	 */

VOID
InitHook(struct Hook *Hook,APTR HookCode)
{
	Hook -> h_Entry		= HookEntry;
	Hook -> h_SubEntry	= HookCode;
	Hook -> h_Data		= 0;
}

	/* CommandKey(struct Hook *Hook,struct SGWork *Work,ULONG *Msg):
	 *
	 *	This routine is called whenever input passes through
	 *	a `term' string gadget. It releases the currently
	 *	active string gadget as soon as the Right-Amiga-key is
	 *	pressed (user is about to select a menu item).
	 */

VOID
CommandKey(struct Hook *Hook,struct SGWork *Work,ULONG *Msg)
{
	if(Msg[0] == SGH_KEY)
	{
		if(Work -> Actions & SGA_END)
			Work -> Actions |= SGA_NEXTACTIVE;

		if((Work -> IEvent -> ie_Qualifier & AMIGARIGHT) && Work -> IEvent -> ie_Code < 96)
		{
			if(!(Work -> IEvent -> ie_Qualifier & (IEQUALIFIER_LSHIFT|IEQUALIFIER_RSHIFT)) && (Work -> IEvent -> ie_Code == KEYCODE_X || Work -> IEvent -> ie_Code == KEYCODE_Q))
				return;
			else
			{
				Work -> Actions |= (SGA_END|SGA_REUSE);
				Work -> Actions &= ~(SGA_USE|SGA_BEEP);

				CommandWindow = Work -> GadgetInfo -> gi_Window;
				CommandGadget = Work -> Gadget;
			}
		}

			/* The user pressed the cursor-right key to
			 * move the cursor to the next word in the buffer.
			 */

		if(Work -> IEvent -> ie_Code == CURSORRIGHT && (Work -> IEvent -> ie_Qualifier & IEQUALIFIER_CONTROL))
		{
			if(Work -> BufferPos != Work -> NumChars)
			{
				SHORT i,Position = -1;

				for(i = Work -> BufferPos ; i < Work -> NumChars ; i++)
				{
					if(Work -> WorkBuffer[i] == ' ')
					{
						for( ; i < Work -> NumChars ; i++)
						{
							if(Work -> WorkBuffer[i] != ' ')
							{
								Position = i;
								break;
							}
						}

						break;
					}
				}

				if(Position != -1)
					Work -> BufferPos = Position;
				else
					Work -> BufferPos = Work -> NumChars;

				Work -> EditOp = EO_MOVECURSOR;
			}
		}

			/* The user pressed the cursor-right key to
			 * move the cursor to the previous word in the buffer.
			 */

		if(Work -> IEvent -> ie_Code == CURSORLEFT && (Work -> IEvent -> ie_Qualifier & IEQUALIFIER_CONTROL))
		{
			if(Work -> BufferPos)
			{
				SHORT i,Position = -1;

				for(i = Work -> BufferPos ; i >= 0 ; i--)
				{
					if(Work -> WorkBuffer[i] != ' ')
					{
						Position = i;
						break;
					}
				}

				if(Position == -1)
					Position = 0;

				if(Position)
				{
					i = Position;

					Position = -1;

					for( ; i >= 0 ; i--)
					{
						if(Work -> WorkBuffer[i] == ' ')
						{
							Position = i + 1;
							break;
						}
					}
				}

				if(Position != -1)
					Work -> BufferPos = Position;
				else
					Work -> BufferPos = 0;

				Work -> EditOp = EO_MOVECURSOR;
			}
		}
	}
}

	/* DubGadList(struct Gadget *Gadget):
	 *
	 *	Walk through a linked list of gadgets and modify
	 *	the string gadgets to use the private string gadget
	 *	editing hook.
	 */

VOID
DubGadList(struct Gadget *Gadget)
{
	while(Gadget)
	{
		if((Gadget -> GadgetType & GTYP_GTYPEMASK) == GTYP_STRGADGET)
		{
			SGEXT(Gadget,&CommandExtend);

			Gadget -> Activation |= GACT_IMMEDIATE;
		}

		Gadget = Gadget -> NextGadget;
	}
}
