From: peter@cbmvax.commodore.com (Peter Cherna)
Newsgroups: comp.sys.amiga.programmer
Subject: Re: Edit hooks for gadtools' STRING_KIND/INTEGER_KIND
Date: 14 Apr 92 12:59:30 GMT
Reply-To: peter@cbmvax.commodore.com (Peter Cherna)
Organization: Commodore-Amiga, Inc.  West Chester, PA.

In article <1992Apr11.122231.1557@brandonu.ca> $brycerw@brandonu.ca writes:
>All the documentation I could find is a little vague on what this edit hook
>can and cannot do.  (ie. can it allow for elimination of certain characters -
>so I can make a "money gadget", or taking a character and replacing it - so
>for example, all characters are uppercase - I know there's other ways for this
>just and example)

The best documentation is currently in the sghooks.h include file.  Basically,
a string edit hook allows you to do what you like based on input to the gadget.
For example, you can restrict entry to allow floating point numbers of the
form
	(optional minus-sign)(some digits)(optional period)(more digits)

You could arrange to reject the minus sign in other than the leading
position, reject more than one minus-sign or dot, and reject all other
non-numeric keystrokes.

Another good example would be a telephone number gadget, or a hexadecimal
number gadget.

As well, you could add command keys that do different things, for example
move left or right one word, scroll up or down through a Shell-like history
of alternatives, etc.

I've enclosed a sample string editing hook that only allows hexadecimal
digits (which is useful), and also zeroes any digit you click on
(which is not useful, but I needed something to demonstrate the
click-processing).  You'll still need to build a Hook structure around
it and create a string gadget that uses it.

/*------------------------------------------------------------------------
** This is an example string editing hook, which shows the basics of
** creating a string editing function.
**
** This hook restricts entry to hexadecimal digits (0-9, A-F, a-f),
** while converting them to upper case.
**
** To demonstrate the processing of mouse-clicks, this hook detects
** clicking on a character, and converts it to a zero.
*/
ULONG str_hookRoutine(struct Hook *hook, struct SGWork *sgw, ULONG *msg)
{
UBYTE *work_ptr;
ULONG return_code;

/* Hook must return non-zero if command is supported.
** This will be changed to zero if the command is unsupported.
*/
return_code = ~0L;

if (*msg == SGH_KEY)
    {
    /* key hit -- could be any key (Shift, repeat, character, etc.) */

    /* allow only upper case characters to be entered.
    ** act only on modes that add or update characters in the buffer.
    */
    if ((sgw->EditOp == EO_REPLACECHAR) ||
        (sgw->EditOp == EO_INSERTCHAR))
        {
        /* Code contains the ASCII representation of the character
        ** entered, if it maps to a single byte.  We could also look
        ** into the work buffer to find the new character.
        **
        **     sgw->Code == sgw->WorkBuffer[sgw->BufferPos - 1]
        **
        ** If the character is not a legal hex digit, don't use
        ** the work buffer and beep the screen.
        */
        if (!IsHexDigit(sgw->Code))
            {
            sgw->Actions |= SGA_BEEP;
            sgw->Actions &= ~SGA_USE;
            }
        else
            {
            /* And make it upper-case, for nicety */
            sgw->WorkBuffer[sgw->BufferPos - 1] = ToUpper(sgw->Code);
            }
        }
    }
else if (*msg == SGH_CLICK)
    {
    /* mouse click
    ** zero the digit clicked on
    */
    if (sgw->BufferPos < sgw->NumChars)
        {
        work_ptr = sgw->WorkBuffer + sgw->BufferPos;
        *work_ptr = '0';
        }
    }
else
    {
    /* UNKNOWN COMMAND
    ** hook should return zero if the command is not supported.
    */
    return_code = 0;
    }

return(return_code);
}

/*------------------------------------------------------------------------
>Robert Bryce

     Peter
--
Peter Cherna, Operating Systems Development Group, Commodore-Amiga, Inc.
{uunet|rutgers}!cbmvax!peter    peter@cbmvax.commodore.com
My opinions do not necessarily represent the opinions of my employer.
"ROM wasn't built in a day."