/* strhooks.c -- string gadget hooks	*/

#include "sysall.h"
#include "strgad.h"

struct IHook	tabhook;
struct IHook	cyclehook;

/* forward in this file	*/
VOID	tabh();
VOID	cycleh();

#define TABSCAN	(0x42)

#define SHIFTY (IEQUALIFIER_LSHIFT|IEQUALIFIER_RSHIFT)

#define D( x )	;	/* debugging: on 'x', off ';'	*/

initMyHooks()
{
    initHook( &tabhook, tabh );
    initHook( &cyclehook, cycleh );
}

initHook( hook, ccode )
struct IHook	*hook;
VOID		(*ccode)();
{
    ULONG	hookface();	/* assembler hook interface	*/
    hook->ih_Entry = hookface;
    hook->ih_SubEntry = (ULONG (*)()) ccode;
    hook->ih_Reserved = 0;
}


VOID
tabh( hook, sgw )
struct IHook	*hook;
struct SGWork	*sgw;
{
    D( kprintf("tabh: hook %lx, sgw: %lx\n", hook, sgw ) );

    if ( sgw->IEvent->ie_Code == TABSCAN )
    {
	sgw->Code = (sgw->IEvent->ie_Qualifier & SHIFTY)?
	    MYCODEBACKTAB: MYCODETAB;
	sgw->Actions |= SGA_END;
	sgw->Actions &= ~SGA_USE;
    }
}

UBYTE	*choices[] = {
    (UBYTE *) "choice 1",
    (UBYTE *) "choice 2",
    (UBYTE *) "choice 3",
    (UBYTE *) "choice 4",
    (UBYTE *) "choice 5",
    (UBYTE *) "choice 6",
};

#define NUMCHOICES	((sizeof (choices))/sizeof (UBYTE *))
#define UPARROW		(0x4c)
#define DOWNARROW	(0x4d)

VOID
cycleh( hook, sgw )
struct IHook	*hook;
struct SGWork	*sgw;
{
    static int	choice = 0;

    D( kprintf("cycleh: hook %lx, sgw: %lx\n", hook, sgw ) );
    switch ( sgw->IEvent->ie_Code )
    {
    case TABSCAN:
	sgw->Code = (sgw->IEvent->ie_Qualifier & SHIFTY)?
	    MYCODEBACKTAB: MYCODETAB;
	sgw->Actions |= SGA_END;
	sgw->Actions &= ~SGA_USE;
	break;

    case UPARROW:
	sgw->WorkBuffer = choices[ choice = (choice + 1)%NUMCHOICES ];
	sgw->NumChars = strlen( sgw->WorkBuffer );
	break;

    case DOWNARROW:
	sgw->WorkBuffer = choices[choice=(choice+(NUMCHOICES-1))%NUMCHOICES];
	sgw->NumChars = strlen( sgw->WorkBuffer );
	break;
    }
}

