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

/*  Module PNLCV09C.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 / Miscellaneous
            Source Language : C

    Modification History (Push-Down List):  

            28.12.85        : Convert to table-driven operation
                            : Make menu function rotate

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

#include <pnl4.h>

/*  Routine number 9 is for use in a single column field created by the 
    PAMENU function.  It checks if the entered character matches the first
    character of any line of the field, and places the line number (numbered
    from 1) in menuno.  Else menuno = 0.  The return code of three causes the
    read to terminate normally.  No character is saved or echoed.

    The key validation routine for menu fields allows only cursor up, down, 
    cr, esc.

    Note that the field read routine within PANELF uses PASBAD instead of 
    PACURS to position the cursor for reading a field with CV09.  This 
    results in the cursor being hidden if possible.  This fact may be of
    significance if you plan to use CV09 for some other purpose.

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

static int menuno = 0;

PACV09()

{
    register int i;

    menuno = 0;
    for (i = 0; i < PACRF.dataheight; i++)
        {
        if (*PVINCH == *(PACRF.datablock + (i * UCI(PACRF.datawidth))))
            {
            menuno = i + 1;
            break;
            }
        }
    return(3);
}

PAKV09()

{
    switch (PVKARG) {
    case 0: case 1: case 2: case 6: case 7: return(0);
    default: return(1); }
}


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

int PAMENU(fp,st,nht,nhm)

/*  PAMENU displays a mutliline field *fp and the uses a highlighted bar
    with highlight type and modifier nht and nhm to select a menu item.
    The bar starts on line st (first line = 1) and may be moved up and 
    down with the arrow up and down keys, or the initial (capital) letter
    of one of the menu items may be keyed.

    Return code is -1 if the parameters are bad, 0 if escape was keyed,
    or the line number (starting from 1) of the selected line.

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

panfcb *fp;
int st,nht,nhm;

{
    panfcb wrk;
    register int i;
    int ret;
    int savescro;
    extern int PASCRO;

    if ( (fp->so <= PAOVTH) ||      /* cannot overlay this field */
         (PASSEC == 0) ||           /* screen save not enabled */
         (UCI(fp->sh) == 1) )       /* single line field, waste of time */
         return (-1);
    ret = PANELF(fdisp,fp);         /* display it */
    if (ret != 0) return(-1);       /* probably no memory */
    PABF(&wrk,sizeof(panfcb),0);    /* clear wrk fcb */
    PABM(fp,&wrk,sizeof(panafcb));  /* move in passed one */ 
    wrk.sh = '\1';                  /* make single line field */
    wrk.sa = upper;                 /* no border, xa, crreq, wipe, etc */
    wrk.charvaln = 9;               /* special char val no for menus */
    wrk.linevaln = 0;
    
    savescro = PASCRO;
    PASCRO = 0;
    i = st - 1;                     /* select starting line */
    if (i < 0) i = 0;               /* allow passed zero */
    while (1)
        {
        if (i < 0)  i = UCI(fp->sh) - 1;    /* rotate round from */
        if (i >= UCI(fp->sh)) i = 0;        /* bottom to top and vv */
        wrk.dj = i;                 /* use line i of data area */
        wrk.sy = i + UCI(fp->sy);   /* and display line i */
        PAOLDH = UCI(fp->hightype);
        wrk.hightype = nht;
        wrk.highmodi = nhm;
        PANELF(fhighc,&wrk);        /* change highlighting */
        wrk.sl = '\1';              /* make single column */
        ret = PANELF(freadu,&wrk);
        wrk.sl = fp->sl;            /* restore width */
        PAOLDH = nht;
        wrk.hightype = fp->hightype;
        wrk.highmodi = fp->highmodi;
        PANELF(fhighc,&wrk);        /* restore highlight */

        switch (ret) {
        case 0:                     /* data entered */
                if (menuno == 0) { PAFUNC(6); break; }
                wrk.dj = menuno-1;  /* use selected line of data area */
                wrk.hightype = nht;
                wrk.highmodi = nhm;
                PAOLDH = UCI(fp->hightype);
                PANELF(fhighc,&wrk);        /* flash selected line */
                goto done;
        case 1:                     /* cr keyed */
                menuno = i+1;
                goto done;
        case 2:                     /* escape */
                menuno = 0;
                goto done;
        case 4:                     /* up */
                i--;
                break;
        case 5: i++;
                break;
        default:                    /* anything else */
                PAFUNC(6);         
                break; }
        }
done:
    PANELF(funsh,fp);       /* lift off this field */      
    PASCRO = savescro;
    return(menuno);
}

