
/****************************************************************************/

/*  Module PNLCV07C.C 

    Copyright 1986 Roundhill Computer Systems Limited
    All rights reserved

    Use and disclosure of this code are prohibited unless in accordance
    with the terms of the PANEL software licence.

    ************************************************************************
    *** THE OBJECT CODE VERSION OF THIS MODULE IS INCLUDED IN THE PANEL  ***
    *** LIBRARY.  IF YOU AMEND AND COMPILE THIS MODULE AND LINK IT WITH  ***
    *** YOUR APPLICATION IT WILL SUPERSEDE THE LIBRARY VERSION.          ***
    ************************************************************************

    PANEL:  Type 4          : Validation
            Source Language : C

    Modification History (Push-Down List):  

            28.12.85        : Convert to table-driven operation

*****************************************************************************/

#include <pnl4.h>

/*  CHARVALN = 7 implements word-wrap on entry of a multiple line field.
    It is suggested that you use this feature only with an interrupt driven
    keyboard handler, since it may need to redisplay a previous line during
    the processing of one keystroke.

    To save space, the same static buffer is used as for numeric line
    validation.

    Note that this routine only works well on original entry, and does not 
    reformat on editing.  When editing a field line which has a completely
    full predecessor line, start by entering <sp> in the first position.
    A leading space at start of line will always inhibit the word-wrap.
    
    We hope that this routine, and your developments of it, will greatly 
    assist entry of free-form data, but please do not expect to find a 
    full-screen editor in the next few lines of code!

******************************************************************************/

static int wwo = 0, wwx = 0, wwy = 0;
static int wwg = 0;
static int wwt = 0;
static uchar *wwe = PANULL, *wwp = PANULL, *wwi = PANULL; 
static uchar str[133] = "";

PACV07()

{
    register int i,j;

    if (wwe != PANULL)              /* chars to emit */
        {
        if (*wwe)                   /* not finished string */
            *PVNINCH = *wwe++;      /* send the next one */
          else
            wwe = PANULL;           /* switch off emitting of chars */
        return(0);                  /* deal with this last one */
        }
    if (!PATDSP(*PVINCH)) return(1);    /* bad char */
    if ( (PACRF.sh == 1) || (PACRF.fi) || (!PACRF.fj) ) return(0); 
             /* no action if single-line or if not first char or if on line 1 */
    if (*PVINCH == ' ') {wwg++; return(2);}     /* ignore leading space */
    if (wwg) {wwg = 0; return(0);}  /* previous char was space: no action */
    wwt = UCI(PACRF.fj) + UCI(PACRF.dj) - 1;
    wwo = ( UCI(PACRF.datawidth) * wwt ) + UCI(PACRF.di) + UCI(PACRF.sl);
    wwp = PACRF.datablock + wwo - 1;    /* offset of prev line end */
    if ( (*wwp == ' ') || (*wwp == PACRF.sf)) return(0); /* no need for wrap */
    for (i = 0; i < UCI(PACRF.sl); )     /* scan back along line */
        {
        if (*(wwp - i++) == ' ') break;
        }                       /* i now has length + 1 of last word of line */
    if (i > UCI(PACRF.sl) - 1) return(0);   /* must be room on this line */
    if (i > 132) return(0);     /* exceeds our buffer */
    wwi = wwp - (i-2);          /* start of word to move */
    for (j = 0; j < i-1; j++) str[j] = *wwi++;  /* move it into buffer */
    str[i-1] = *PVINCH;         /* add this character */
    str[i] = '\0';              /* and terminator */
    wwe = &str[1];              /* switch to force emit next chars in buffer */
    wwp = wwp - (i-1);          /* point to space before moved word */
    wwx = UCI(PACRF.sx) + UCI(PACRF.sl) - i;
    wwy = UCI(PACRF.sy) + UCI(PACRF.fj) - 1;
    for (j = 0; j < i; j++)     /* blank out previous line */
        {
        PASBAD(wwx+j,wwy);      /* position cursor on previous line */
        PACOUT(*(wwp+j)=PACRF.sf);  /* write highlighted fill characters */
        }
    PACURS(UCI(PACRF.sx),wwy+1);    /* return cursor to where it was */
    *PVNINCH = str[0];              /* use first char of buffer next */
    return(2);                      /* ignore the current one (saved in buffer) */
}
