home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / ncurses-1.9.9e-src.tgz / tar.out / fsf / ncurses / misc / ncurses-intro.doc < prev    next >
Text File  |  1996-09-28  |  110KB  |  2,579 lines

  1.  
  2.                          WRITING PROGRAMS WITH NCURSES
  3.                                        
  4.      by Eric S. Raymond and Zeyd M. Ben-Halim
  5.      
  6.                                    CONTENTS
  7.                                        
  8.      * Introduction
  9.           + A Brief History of Curses
  10.           + Scope of This Document
  11.           + Terminology
  12.      * The Curses Library
  13.           + An Overview of Curses
  14.                o Compiling Programs using Curses
  15.                o Updating the Screen
  16.                o Standard Windows and Function Naming Conventions
  17.                o Variables
  18.           + Using the Library
  19.                o Starting up
  20.                o Output
  21.                o Input
  22.                o Using Forms Characters
  23.                o Character Attributes and Color
  24.                o Mouse Interfacing
  25.                o Finishing Up
  26.           + Function Descriptions
  27.                o Initialization and Wrapup
  28.                o Causing Output to the Terminal
  29.                o Low-Level Capability Access
  30.                o Debugging
  31.           + Hints, Tips, and Tricks
  32.                o Some Notes of Caution
  33.                o Temporarily Leaving ncurses Mode
  34.                o Using ncurses under xterm
  35.                o Handling Multiple Terminal Screens
  36.                o Testing for Terminal Capabilities
  37.                o Tuning for Speed
  38.                o Special Features of ncurses
  39.           + Compatibility with Older Versions
  40.                o Refresh of Overlapping Windows
  41.                o Background Erase
  42.           + XSI Curses Conformance
  43.      * The Panels Library
  44.           + Compiling With the Panels Library
  45.           + Overview of Panels
  46.           + Panels, Input, and the Standard Screen
  47.           + Hiding Panels
  48.           + Miscellaneous Other Facilities
  49.      * The Menu Library
  50.           + Compiling with the menu Library
  51.           + Overview of Menus
  52.           + Selecting items
  53.           + Menu Display
  54.           + Menu Windows
  55.           + Processing Menu Input
  56.           + Miscellaneous Other Features
  57.      * The Forms Library
  58.           + Compiling with the forms Library
  59.           + Overview of Forms
  60.           + Creating and Freeing Fields and Forms
  61.           + Fetching and Changing Field Attributes
  62.                o Fetching Size and Location Data
  63.                o Changing the Field Location
  64.                o The Justification Attribute
  65.                o Field Display Attributes
  66.                o Field Option Bits
  67.                o Field Status
  68.                o Field User Pointer
  69.           + Variable-Sized Fields
  70.           + Field Validation
  71.                o TYPE_ALPHA
  72.                o TYPE_ALNUM
  73.                o TYPE_ENUM
  74.                o TYPE_INTEGER
  75.                o TYPE_NUMERIC
  76.                o TYPE_REGEXP
  77.           + Direct Field Buffer Manipulation
  78.      * Attributes of Forms
  79.      * Control of Form Display
  80.      * Input Processing in the Forms Driver
  81.           + Page Navigation Requests
  82.           + Inter-Field Navigation Requests
  83.           + Intra-Field Navigation Requests
  84.           + Scrolling Requests
  85.           + Field Editing Requests
  86.           + Order Requests
  87.           + Application Commands
  88.      * Field Change Hooks
  89.      * Field Change Commands
  90.      * Form Options
  91.      * Custom Validation Types
  92.           + Union Types
  93.           + New Field Types
  94.           + Validation Function Arguments
  95.           + Order Functions For Custom Types
  96.           + Avoiding Problems
  97.             
  98.    
  99.      _________________________________________________________________
  100.    
  101.                                  INTRODUCTION
  102.                                        
  103.    This document is an introduction to programming with curses. It is not
  104.    an exhaustive reference for the curses Application Programming
  105.    Interface (API); that role is filled by the curses manual pages.
  106.    Rather, it is intended to help C programmers ease into using the
  107.    package.
  108.    
  109.    This document is aimed at C applications programmers not yet
  110.    specifically familiar with ncurses. If you are already an experienced
  111.    curses programmer, you should nevertheless read the sections on Mouse
  112.    Interfacing, Debugging, Compatibility with Older Versions, and Hints,
  113.    Tips, and Tricks. These will bring you up to speed on the special
  114.    features and quirks of the ncurses implementation. If you are not so
  115.    experienced, keep reading.
  116.    
  117.    The curses package is a subroutine library for terminal-independent
  118.    screen-painting and input-event handling which presents a high level
  119.    screen model to the programmer, hiding differences between terminal
  120.    types and doing automatic optimization of output to change one
  121.    screenfull of text into another. Curses uses terminfo, which is a
  122.    database format that can describe the capabilities of thousands of
  123.    different terminals.
  124.    
  125.    The curses API may seem something of an archaism on UNIX desktops
  126.    increasingly dominated by X, Motif, and Tcl/Tk. Nevertheless, UNIX
  127.    still supports tty lines and X supports _xterm(1)_; the curses API has
  128.    the advantage of (a) back-portability to character-cell terminals, and
  129.    (b) simplicity. For an application that does not require bit-mapped
  130.    graphics and multiple fonts, an interface implementation using curses
  131.    will typically be a great deal simpler and less expensive than one
  132.    using an X toolkit.
  133.    
  134. A Brief History of Curses
  135.  
  136.    Historically, the first ancestor of curses was the routines written to
  137.    provide screen-handling for the game rogue; these used the already-
  138.    existing termcap database facility for describing terminal
  139.    capabilities. These routines were abstracted into a documented library
  140.    and first released with the early BSD UNIX versions.
  141.    
  142.    System III UNIX from Bell Labs featured a rewritten and much-improved
  143.    curses library. It introduced the terminfo format. Terminfo is based
  144.    on Berkeley's termcap database, but contains a number of improvements
  145.    and extensions. Parameterized capabilities strings were introduced,
  146.    making it possible to describe multiple video attributes, and colors
  147.    and to handle far more unusual terminals than possible with termcap.
  148.    In the later AT&T System V releases, curses evolved to use more
  149.    facilities and offer more capabilities, going far beyond BSD curses in
  150.    power and flexibility.
  151.    
  152. Scope of This Document
  153.  
  154.    This document describes ncurses, a freeware implementation of the
  155.    System V curses API with some clearly marked extensions. It includes
  156.    the following System V curses features:
  157.    
  158.      * Support for multiple screen highlights (BSD curses could only
  159.        handle one `standout' highlight, usually reverse-video).
  160.        
  161.      * Support for line- and box-drawing using forms characters.
  162.        
  163.      * Recognition of function keys on input.
  164.        
  165.      * Color support.
  166.        
  167.      * Support for pads (windows of larger than screen size on which the
  168.        screen or a subwindow defines a viewport).
  169.        
  170.    Also, this package makes use of the insert and delete line and
  171.    character features of terminals so equipped, and determines how to
  172.    optimally use these features with no help from the programmer. It
  173.    allows arbitrary combinations of video attributes to be displayed,
  174.    even on terminals that leave ``magic cookies'' on the screen to mark
  175.    changes in attributes.
  176.    
  177.    The ncurses package can also capture and use event reports from a
  178.    mouse in some environments (notably, xterm under the X window system).
  179.    This document includes tips for using the mouse. The ncurses package
  180.    was originated by Pavel Curtis. The primary maintainer of the package
  181.    is Zeyd Ben-Halim <zmbenhal@netcom.com>. Eric S. Raymond
  182.    <esr@snark.thyrsus.com> wrote many of the new features in versions
  183.    after 1.8.1 and wrote most of this introduction.
  184.    
  185.    This document also describes the extension library, similarly modeled
  186.    on the SVr4 panels facility. This library allows you to associate
  187.    backing store with each of a stack or deck of overlapping windows, and
  188.    provides operations for moving windows around in the stack that change
  189.    their visibility in the natural way (handling window overlaps).
  190.    
  191.    Finally, this document describes in detail the menus and forms
  192.    extension libraries, also cloned from System V, which support easy
  193.    construction and sequences of menus and fill-in forms. This code was
  194.    contributed to the project by Jrgen Pfeifer.
  195.    
  196. Terminology
  197.  
  198.    In this document, the following terminology is used with reasonable
  199.    consistency:
  200.    
  201.    window
  202.           A data structure describing a sub-rectangle of the screen
  203.           (possibly the entire screen). You can write to a window as
  204.           though it were a miniature screen, scrolling independently of
  205.           other windows on the physical screen.
  206.           
  207.    screens
  208.           A subset of windows which are as large as the terminal screen,
  209.           i.e., they start at the upper left hand corner and encompass
  210.           the lower right hand corner. One of these, stdscr, is
  211.           automatically provided for the programmer.
  212.           
  213.    terminal screen
  214.           The package's idea of what the terminal display currently looks
  215.           like, i.e., what the user sees now. This is a special screen.
  216.           
  217.                               THE CURSES LIBRARY
  218.                                        
  219. An Overview of Curses
  220.  
  221.   COMPILING PROGRAMS USING CURSES
  222.   
  223.    In order to use the library, it is necessary to have certain types and
  224.    variables defined. Therefore, the programmer must have a line:
  225.    
  226.  
  227.           #include <curses.h>
  228.  
  229.    at the top of the program source. The screen package uses the Standard
  230.    I/O library, so <curses.h> includes <stdio.h>. <curses.h> also
  231.    includes <termios.h>, <termio.h>, or <sgtty.h> depending on your
  232.    system. It is redundant (but harmless) for the programmer to do these
  233.    includes, too. In linking with curses you need to have -lncurses in
  234.    your LDFLAGS or on the command line. There is no need for any other
  235.    libraries.
  236.    
  237.   UPDATING THE SCREEN
  238.   
  239.    In order to update the screen optimally, it is necessary for the
  240.    routines to know what the screen currently looks like and what the
  241.    programmer wants it to look like next. For this purpose, a data type
  242.    (structure) named WINDOW is defined which describes a window image to
  243.    the routines, including its starting position on the screen (the (y,
  244.    x) coordinates of the upper left hand corner) and its size. One of
  245.    these (called curscr, for current screen) is a screen image of what
  246.    the terminal currently looks like. Another screen (called stdscr, for
  247.    standard screen) is provided by default to make changes on.
  248.    
  249.    A window is a purely internal representation. It is used to build and
  250.    store a potential image of a portion of the terminal. It doesn't bear
  251.    any necessary relation to what is really on the terminal screen; it's
  252.    more like a scratchpad or write buffer.
  253.    
  254.    To make the section of physical screen corresponding to a window
  255.    reflect the contents of the window structure, the routine refresh()
  256.    (or wrefresh() if the window is not stdscr) is called.
  257.    
  258.    A given physical screen section may be within the scope of any number
  259.    of overlapping windows. Also, changes can be made to windows in any
  260.    order, without regard to motion efficiency. Then, at will, the
  261.    programmer can effectively say ``make it look like this,'' and let the
  262.    package implementation determine the most efficient way to repaint the
  263.    screen.
  264.    
  265.   STANDARD WINDOWS AND FUNCTION NAMING CONVENTIONS
  266.   
  267.    As hinted above, the routines can use several windows, but two are
  268.    automatically given: curscr, which knows what the terminal looks like,
  269.    and stdscr, which is what the programmer wants the terminal to look
  270.    like next. The user should never actually access curscr directly.
  271.    Changes should be made to through the API, and then the routine
  272.    refresh() (or wrefresh()) called.
  273.    
  274.    Many functions are defined to use stdscr as a default screen. For
  275.    example, to add a character to stdscr, one calls addch() with the
  276.    desired character as argument. To write to a different window. use the
  277.    routine waddch() (for `w'indow-specific addch()) is provided. This
  278.    convention of prepending function names with a `w' when they are to be
  279.    applied to specific windows is consistent. The only routines which do
  280.    not follow it are those for which a window must always be specified.
  281.    
  282.    In order to move the current (y, x) coordinates from one point to
  283.    another, the routines move() and wmove() are provided. However, it is
  284.    often desirable to first move and then perform some I/O operation. In
  285.    order to avoid clumsiness, most I/O routines can be preceded by the
  286.    prefix 'mv' and the desired (y, x) coordinates prepended to the
  287.    arguments to the function. For example, the calls
  288.    
  289.  
  290.           move(y, x);
  291.           addch(ch);
  292.  
  293.    can be replaced by
  294.    
  295.  
  296.           mvaddch(y, x, ch);
  297.  
  298.    and
  299.    
  300.  
  301.           wmove(win, y, x);
  302.           waddch(win, ch);
  303.  
  304.    can be replaced by
  305.    
  306.  
  307.           mvwaddch(win, y, x, ch);
  308.  
  309.    Note that the window description pointer (win) comes before the added
  310.    (y, x) coordinates. If a function requires a window pointer, it is
  311.    always the first parameter passed.
  312.    
  313.   VARIABLES
  314.   
  315.    The curses library sets some variables describing the terminal
  316.    capabilities.
  317.    
  318.  
  319.       type   name      description
  320.       ------------------------------------------------------------------
  321.       int    LINES     number of lines on the terminal
  322.       int    COLS      number of columns on the terminal
  323.  
  324.    The curses.h also introduces some #define constants and types of
  325.    general usefulness:
  326.    
  327.    bool   boolean type, actually a `char' (e.g., bool doneit;)
  328.           
  329.    TRUE   boolean `true' flag (1).
  330.           
  331.    FALSE  boolean `false' flag (0).
  332.           
  333.    ERR    error flag returned by routines on a fail (-1).
  334.           
  335.    OK     error flag returned by routines when things go right.
  336.           
  337. Using the Library
  338.  
  339.    Now we describe how to actually use the screen package. In it, we
  340.    assume all updating, reading, etc. is applied to stdscr. These
  341.    instructions will work on any window, providing you change the
  342.    function names and parameters as mentioned above.
  343.    
  344.    Here is a sample program to motivate the discussion:
  345.    
  346.  
  347. #include <curses.h>
  348. #include <signal.h>
  349.  
  350. static void finish(int sig);
  351.  
  352. main(int argc, char *argv[])
  353. {
  354.     /* initialize your non-curses data structures here */
  355.  
  356.     (void) signal(SIGINT, finish);      /* arrange interrupts to terminate */
  357.  
  358.     (void) initscr();      /* initialize the curses library */
  359.     keypad(stdscr, TRUE);  /* enable keyboard mapping */
  360.     (void) nonl();         /* tell curses not to do NL->CR/NL on output */
  361.     (void) cbreak();       /* take input chars one at a time, no wait for \n */
  362.     (void) noecho();       /* don't echo input */
  363.  
  364.     if (has_colors())
  365.     {
  366.         start_color();
  367.  
  368.         /*
  369.          * Simple color assignment, often all we need.
  370.          */
  371.         init_pair(COLOR_BLACK, COLOR_BLACK, COLOR_BLACK);
  372.         init_pair(COLOR_GREEN, COLOR_GREEN, COLOR_BLACK);
  373.         init_pair(COLOR_RED, COLOR_RED, COLOR_BLACK);
  374.         init_pair(COLOR_CYAN, COLOR_CYAN, COLOR_BLACK);
  375.         init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK);
  376.         init_pair(COLOR_MAGENTA, COLOR_MAGENTA, COLOR_BLACK);
  377.         init_pair(COLOR_BLUE, COLOR_BLUE, COLOR_BLACK);
  378.         init_pair(COLOR_YELLOW, COLOR_YELLOW, COLOR_BLACK);
  379.     }
  380.  
  381.     for (;;)
  382.     {
  383.         int c = getch();     /* refresh, accept single keystroke of input */
  384.  
  385.         /* process the command keystroke */
  386.     }
  387.  
  388.     finish(0);               /* we're done */
  389. }
  390.  
  391. static void finish(int sig)
  392. {
  393.     endwin();
  394.  
  395.     /* do your non-curses wrapup here */
  396.  
  397.     exit(0);
  398. }
  399.  
  400.   STARTING UP
  401.   
  402.    In order to use the screen package, the routines must know about
  403.    terminal characteristics, and the space for curscr and stdscr must be
  404.    allocated. These function initscr() does both these things. Since it
  405.    must allocate space for the windows, it can overflow memory when
  406.    attempting to do so. On the rare occasions this happens, initscr()
  407.    will terminate the program with an error message. initscr() must
  408.    always be called before any of the routines which affect windows are
  409.    used. If it is not, the program will core dump as soon as either
  410.    curscr or stdscr are referenced. However, it is usually best to wait
  411.    to call it until after you are sure you will need it, like after
  412.    checking for startup errors. Terminal status changing routines like
  413.    nl() and cbreak() should be called after initscr().
  414.    
  415.    Once the screen windows have been allocated, you can set them up for
  416.    your program. If you want to, say, allow a screen to scroll, use
  417.    scrollok(). If you want the cursor to be left in place after the last
  418.    change, use leaveok(). If this isn't done, refresh() will move the
  419.    cursor to the window's current (y, x) coordinates after updating it.
  420.    
  421.    You can create new windows of your own using the functions newwin(),
  422.    derwin(), and subwin(). The routine delwin() will allow you to get rid
  423.    of old windows. All the options described above can be applied to any
  424.    window.
  425.    
  426.   OUTPUT
  427.   
  428.    Now that we have set things up, we will want to actually update the
  429.    terminal. The basic functions used to change what will go on a window
  430.    are addch() and move(). addch() adds a character at the current (y, x)
  431.    coordinates. move() changes the current (y, x) coordinates to whatever
  432.    you want them to be. It returns ERR if you try to move off the window.
  433.    As mentioned above, you can combine the two into mvaddch() to do both
  434.    things at once.
  435.    
  436.    The other output functions, such as addstr() and printw(), all call
  437.    addch() to add characters to the window.
  438.    
  439.    After you have put on the window what you want there, when you want
  440.    the portion of the terminal covered by the window to be made to look
  441.    like it, you must call refresh(). In order to optimize finding
  442.    changes, refresh() assumes that any part of the window not changed
  443.    since the last refresh() of that window has not been changed on the
  444.    terminal, i.e., that you have not refreshed a portion of the terminal
  445.    with an overlapping window. If this is not the case, the routine
  446.    touchwin() is provided to make it look like the entire window has been
  447.    changed, thus making refresh() check the whole subsection of the
  448.    terminal for changes.
  449.    
  450.    If you call wrefresh() with curscr as its argument, it will make the
  451.    screen look like curscr thinks it looks like. This is useful for
  452.    implementing a command which would redraw the screen in case it get
  453.    messed up.
  454.    
  455.   INPUT
  456.   
  457.    The complementary function to addch() is getch() which, if echo is
  458.    set, will call addch() to echo the character. Since the screen package
  459.    needs to know what is on the terminal at all times, if characters are
  460.    to be echoed, the tty must be in raw or cbreak mode. Since initially
  461.    the terminal has echoing enabled and is in ordinary ``cooked'' mode,
  462.    one or the other has to changed before calling getch(); otherwise, the
  463.    program's output will be unpredictable.
  464.    
  465.    When you need to accept line-oriented input in a window, the functions
  466.    wgetstr() and friends are available. There is even a wscanw() function
  467.    that can do scanf()(3)-style multi-field parsing on window input.
  468.    These pseudo-line-oriented functions turn on echoing while they
  469.    execute.
  470.    
  471.    The example code above uses the call keypad(stdscr, TRUE) to enable
  472.    support for function-key mapping. With this feature, the getch() code
  473.    watches the input stream for character sequences that correspond to
  474.    arrow and function keys. These sequences are returned as
  475.    pseudo-character values. The #define values returned are listed in the
  476.    curses.h The mapping from sequences to #define values is determined by
  477.    key_ capabilities in the terminal's terminfo entry.
  478.    
  479.   USING FORMS CHARACTERS
  480.   
  481.    The addch() function (and some others, including box() and border())
  482.    can accept some pseudo-character arguments which are specially defined
  483.    by ncurses. These are #define values set up in the curses.h header;
  484.    see there for a complete list (look for the prefix ACS_).
  485.    
  486.    The most useful of the ACS defines are the forms-drawing characters.
  487.    You can use these to draw boxes and simple graphs on the screen. If
  488.    the terminal does not have such characters, curses.h will map them to
  489.    a recognizable (though ugly) set of ASCII defaults.
  490.    
  491.   CHARACTER ATTRIBUTES AND COLOR
  492.   
  493.    The ncurses package supports screen highlights including standout,
  494.    reverse-video, underline, and blink. It also supports color, which is
  495.    treated as another kind of highlight.
  496.    
  497.    Highlights are encoded, internally, as high bits of the
  498.    pseudo-character type (chtype) that curses.h uses to represent the
  499.    contents of a screen cell. See the curses.h header file for a complete
  500.    list of highlight mask values (look for the prefix A_).
  501.    
  502.    There are two ways to make highlights. One is to logical-or the value
  503.    of the highlights you want into the character argument of an addch()
  504.    call, or any other output call that takes a chtype argument.
  505.    
  506.    The other is to set the current-highlight value. This is logical-or'ed
  507.    with any highlight you specify the first way. You do this with the
  508.    functions attron(), attroff(), and attrset(); see the manual pages for
  509.    details. Color is a special kind of highlight. The package actually
  510.    thinks in terms of color pairs, combinations of foreground and
  511.    background colors. The sample code above sets up eight color pairs,
  512.    all of the guaranteed-available colors on black. Note that each color
  513.    pair is, in effect, given the name of its foreground color. Any other
  514.    range of eight non-conflicting values could have been used as the
  515.    first arguments of the init_pair() values.
  516.    
  517.    Once you've done an init_pair() that creates color-pair N, you can use
  518.    COLOR_PAIR(N) as a highlight that invokes that particular color
  519.    combination. Note that COLOR_PAIR(N), for constant N, is itself a
  520.    compile-time constant and can be used in initializers.
  521.    
  522.   MOUSE INTERFACING
  523.   
  524.    The ncurses library also provides a mouse interface. Note: his
  525.    facility is original to ncurses, it is not part of either the XSI
  526.    Curses standard, nor of System V Release 4, nor BSD curses. Thus, we
  527.    recommend that you wrap mouse-related code in an #ifdef using the
  528.    feature macro NCURSES_MOUSE_VERSION so it will not be compiled and
  529.    linked on non-ncurses systems.
  530.    
  531.    Presently, mouse event reporting works only under xterm. In the
  532.    future, ncurses will detect the presence of \fBgpm\fR(1), Alessandro
  533.    Rubini's freeware mouse server for Linux systems, and accept mouse
  534.    reports through it.
  535.    
  536.    The mouse interface is very simple. To activate it, you use the
  537.    function mousemask(), passing it as first argument a bit-mask that
  538.    specifies what kinds of events you want your program to be able to
  539.    see. It will return the bit-mask of events that actually become
  540.    visible, which may differ from the argument if the mouse device is not
  541.    capable of reporting some of the event types you specify.
  542.    
  543.    Once the mouse is active, your application's command loop should watch
  544.    for a return value of KEY_MOUSE from wgetch(). When you see this, a
  545.    mouse event report has been queued. To pick it off the queue, use the
  546.    function getmouse() (you must do this before the next wgetch(),
  547.    otherwise another mouse event might come in and make the first one
  548.    inaccessible).
  549.    
  550.    Each call to getmouse() fills a structure (the address of which you'll
  551.    pass it) with mouse event data. The event data includes zero-origin,
  552.    screen-relative character-cell coordinates of the mouse pointer. It
  553.    also includes an event mask. Bits in this mask will be set,
  554.    corresponding to the event type being reported.
  555.    
  556.    The mouse structure contains two additional fields which may be
  557.    significant in the future as ncurses interfaces to new kinds of
  558.    pointing device. In addition to x and y coordinates, there is a slot
  559.    for a z coordinate; this might be useful with touchscreens that can
  560.    return a pressure or duration parameter. There is also a device ID
  561.    field, which could be used to distinguish between multiple pointing
  562.    devices.
  563.    
  564.    The class of visible events may be changed at any time via
  565.    mousemask(). Events that can be reported include presses, releases,
  566.    single-, double- and triple-clicks (you can set the maximum
  567.    button-down time for clicks). If you don't make clicks visible, they
  568.    will be reported as press-release pairs. In some environments, the
  569.    event mask may include bits reporting the state of shift, alt, and
  570.    ctrl keys on the keyboard during the event.
  571.    
  572.    A function to check whether a mouse event fell within a given window
  573.    is also supplied. You can use this to see whether a given window
  574.    should consider a mouse event relevant to it.
  575.    
  576.    Because mouse event reporting will not be available in all
  577.    environments, it would be unwise to build ncurses applications that
  578.    _require_ the use of a mouse. Rather, you should use the mouse as a
  579.    shortcut for point-and-shoot commands your application would normally
  580.    accept from the keyboard. Two of the test games in the ncurses
  581.    distribution (bs and knight) contain code that illustrates how this
  582.    can be done.
  583.    
  584.    See the manual page curs_mouse(3X) for full details of the
  585.    mouse-interface functions.
  586.    
  587.   FINISHING UP
  588.   
  589.    In order to clean up after the ncurses routines, the routine endwin()
  590.    is provided. It restores tty modes to what they were when initscr()
  591.    was first called, and moves the cursor down to the lower-left corner.
  592.    Thus, anytime after the call to initscr, endwin() should be called
  593.    before exiting.
  594.    
  595. Function Descriptions
  596.  
  597.    We describe the detailed behavior of some important curses functions
  598.    here, as a supplement to the manual page descriptions.
  599.    
  600.   INITIALIZATION AND WRAPUP
  601.   
  602.    initscr()
  603.           The first function called should almost always be initscr().
  604.           This will determine the terminal type and initialize curses
  605.           data structures. initscr() also arranges that the first call to
  606.           refresh() will clear the screen. If an error occurs a message
  607.           is written to standard error and the program exits. Otherwise
  608.           it returns a pointer to stdscr. A few functions may be called
  609.           before initscr (slk_init(), filter(), ripofflines(), use_env(),
  610.           and, if you are using multiple terminals, newterm().)
  611.           
  612.    endwin()
  613.           Your program should always call endwin() before exiting or
  614.           shelling out of the program. This function will restore tty
  615.           modes, move the cursor to the lower left corner of the screen,
  616.           reset the terminal into the proper non-visual mode. Calling
  617.           refresh() or doupdate() after a temporary escape from the
  618.           program will restore the ncurses screen from before the escape.
  619.           
  620.           
  621.    newterm(type, ofp, ifp)
  622.           A program which outputs to more than one terminal should use
  623.           newterm() instead of initscr(). newterm() should be called once
  624.           for each terminal. It returns a variable of type SCREEN * which
  625.           should be saved as a reference to that terminal. The arguments
  626.           are the type of the terminal (a string) and FILE pointers for
  627.           the output and input of the terminal. If type is NULL then the
  628.           environment variable $TERM is used. endwin() should called once
  629.           at wrapup time for each terminal opened using this function.
  630.           
  631.    set_term(new)
  632.           This function is used to switch to a different terminal
  633.           previously opened by newterm(). The screen reference for the
  634.           new terminal is passed as the parameter. The previous terminal
  635.           is returned by the function. All other calls affect only the
  636.           current terminal.
  637.           
  638.    delscreen(sp)
  639.           The inverse of newterm(); deallocates the data structures
  640.           associated with a given SCREEN reference.
  641.           
  642.   CAUSING OUTPUT TO THE TERMINAL
  643.   
  644.    refresh() and wrefresh(win)
  645.           These functions must be called to actually get any output on
  646.           the terminal, as other routines merely manipulate data
  647.           structures. wrefresh() copies the named window to the physi-
  648.           cal terminal screen, taking into account what is already there
  649.           in order to do optimizations. refresh() does a refresh of
  650.           stdscr(). Unless leaveok() has been enabled, the physical
  651.           cursor of the terminal is left at the location of the window's
  652.           cursor.
  653.           
  654.    doupdate() and wnoutrefresh(win)
  655.           These two functions allow multiple updates with more efficiency
  656.           than wrefresh. To use them, it is important to understand how
  657.           curses works. In addition to all the window structures, curses
  658.           keeps two data structures representing the terminal screen: a
  659.           physical screen, describing what is actually on the screen, and
  660.           a virtual screen, describing what the programmer wants to have
  661.           on the screen. wrefresh works by first copying the named window
  662.           to the virtual screen (wnoutrefresh()), and then calling the
  663.           routine to update the screen (doupdate()). If the programmer
  664.           wishes to output several windows at once, a series of calls to
  665.           wrefresh will result in alternating calls to wnoutrefresh() and
  666.           doupdate(), causing several bursts of output to the screen. By
  667.           calling wnoutrefresh() for each window, it is then possible to
  668.           call doupdate() once, resulting in only one burst of output,
  669.           with fewer total characters transmitted (this also avoids a
  670.           visually annoying flicker at each update).
  671.           
  672.   LOW-LEVEL CAPABILITY ACCESS
  673.   
  674.    setupterm(term, filenum, errret) This routine is called to initialize
  675.           a terminal's description, without setting up the curses screen
  676.           structures or changing the tty-driver mode bits. term is the
  677.           character string representing the name of the terminal being
  678.           used. filenum is the UNIX file descriptor of the terminal to be
  679.           used for output. errret is a pointer to an integer, in which a
  680.           success or failure indication is returned. The values returned
  681.           can be 1 (all is well), 0 (no such terminal), or -1 (some
  682.           problem locating the terminfo database).
  683.           
  684.           The value of term can be given as NULL, which will cause the
  685.           value of TERM in the environment to be used. The errret pointer
  686.           can also be given as NULL, meaning no error code is wanted. If
  687.           errret is defaulted, and something goes wrong, setupterm() will
  688.           print an appropriate error message and exit, rather than
  689.           returning. Thus, a simple program can call setupterm(0, 1, 0)
  690.           and not worry about initialization errors.
  691.           
  692.           After the call to setupterm(), the global variable cur_term is
  693.           set to point to the current structure of terminal capabilities.
  694.           By calling setupterm() for each terminal, and saving and
  695.           restoring cur_term, it is possible for a program to use two or
  696.           more terminals at once. Setupterm() also stores the names
  697.           section of the terminal description in the global character
  698.           array ttytype[]. Subsequent calls to setupterm() will overwrite
  699.           this array, so you'll have to save it yourself if need be.
  700.           
  701.           
  702.   DEBUGGING
  703.   
  704.    NOTE: These functions are not part of the standard curses SPI!
  705.    
  706.    trace()
  707.           This function can be used to explicitly set a trace level. If
  708.           the trace level is nonzero, execution of your program will
  709.           generate a file called `trace' in the current working directory
  710.           containing a report on the library's actions. Higher trace
  711.           levels enable more detailed (and verbose) reporting -- see
  712.           comments attached to TRACE_ defines in the curses.h file for
  713.           details. (It is also possible to set a trace level by assigning
  714.           a trace level value to the environment variable NCURSES_TRACE).
  715.           
  716.    _tracef()
  717.           This function can be used to output your own debugging
  718.           information. It is only available only if you link with
  719.           -lncurses_g. It can be used the same way as printf(), only it
  720.           outputs a newline after the end of arguments. The output goes
  721.           to a file called trace in the current directory.
  722.           
  723.    Trace logs can be difficult to interpret due to the sheer volume of
  724.    data dumped in them. There is a script called _tracemunch_ included
  725.    with the ncurses distribution that can alleviate this problem
  726.    somewhat; it compacts long sequences of similar operations into more
  727.    succinct single-line pseudo-operations. These pseudo-ops can be
  728.    distinguished by the fact that they are named in capital letters.
  729.    
  730. Hints, Tips, and Tricks
  731.  
  732.    The ncurses manual pages are a complete reference for this library. In
  733.    the remainder of this document, we discuss various useful methods that
  734.    may not be obvious from the manual page descriptions.
  735.    
  736.   SOME NOTES OF CAUTION
  737.   
  738.    If you find yourself thinking you need to use noraw() or nocbreak(),
  739.    think again and move carefully. It's probably better design to use
  740.    getstr() or one of its relatives to simulate cooked mode. The noraw()
  741.    and nocbreak() functions try to restore cooked mode, but they may end
  742.    up clobbering some control bits set before you started your
  743.    application. Also, they have always been poorly documented, and are
  744.    likely to hurt your application's usability with other curses
  745.    libraries.
  746.    
  747.    Bear in mind that refresh() is a synonym for wrefresh(stdscr), and
  748.    don't try to mix use of stdscr with use of windows declared by
  749.    newwin(); a refresh() call will blow them off the screen. The right
  750.    way to handle this is to use subwin(), or not touch stdscr at all and
  751.    tile your screen with declared windows which you then wnoutrefresh()
  752.    somewhere in your program event loop, with a single doupdate() call to
  753.    trigger actual repainting.
  754.    
  755.    You are much less likely to run into problems if you design your
  756.    screen layouts to use tiled rather than overlapping windows.
  757.    Historically, curses support for overlapping windows has been weak,
  758.    fragile, and poorly documented. The ncurses library is not yet an
  759.    exception to this rule.
  760.    
  761.    There is a freeware panels library included in the ncurses
  762.    distribution that does a pretty good job of strengthening the
  763.    overlapping-windows facilities.
  764.    
  765.    Try to avoid using the global variables LINES and COLS. Use getmaxyx()
  766.    on the stdscr context instead. Reason: your code may be ported to run
  767.    in an environment with window resizes, in which case several screens
  768.    could be open with different sizes.
  769.    
  770.   TEMPORARILY LEAVING NCURSES MODE
  771.   
  772.    Sometimes you will want to write a program that spends most of its
  773.    time in screen mode, but occasionally returns to ordinary `cooked'
  774.    mode. A common reason for this is to support shell-out. This behavior
  775.    is simple to arrange in ncurses.
  776.    
  777.    To leave ncurses mode, call endwin() as you would if you were
  778.    intending to terminate the program. This will take the screen back to
  779.    cooked mode; you can do your shell-out. When you want to return to
  780.    ncurses mode, simply call refresh() or doupdate(). This will repaint
  781.    the screen.
  782.    
  783.    There is a boolean function, isendwin(), which code can use to test
  784.    whether ncurses screen mode is active. It returns TRUE in the interval
  785.    between an endwin() call and the following refresh(), FALSE otherwise.
  786.    
  787.    
  788.    Here is some sample code for shellout:
  789.    
  790.  
  791.     addstr("Shelling out...");
  792.     def_prog_mode();           /* save current tty modes */
  793.     endwin();                  /* restore original tty modes */
  794.     system("sh");              /* run shell */
  795.     addstr("returned.\n");     /* prepare return message */
  796.     refresh();                 /* restore save modes, repaint screen */
  797.  
  798.   USING NCURSES UNDER XTERM
  799.   
  800.    A resize operation in X sends SIGWINCH to the application running
  801.    under xterm. The ncurses library does not catch this signal, because
  802.    it cannot in general know how you want the screen re-painted. You will
  803.    have to write the SIGWINCH handler yourself.
  804.    
  805.    The easiest way to code your SIGWINCH handler is to have it do an
  806.    endwin, followed by an initscr and a screen repaint you code yourself.
  807.    The initscr will pick up the new screen size from the xterm's
  808.    environment.
  809.    
  810.   HANDLING MULTIPLE TERMINAL SCREENS
  811.   
  812.    The initscr() function actually calls a function named newterm() to do
  813.    most of its work. If you are writing a program that opens multiple
  814.    terminals, use newterm() directly.
  815.    
  816.    For each call, you will have to specify a terminal type and a pair of
  817.    file pointers; each call will return a screen reference, and stdscr
  818.    will be set to the last one allocated. You will switch between screens
  819.    with the set_term call. Note that you will also have to call
  820.    def_shell_mode and def_prog_mode on each tty yourself.
  821.    
  822.   TESTING FOR TERMINAL CAPABILITIES
  823.   
  824.    Sometimes you may want to write programs that test for the presence of
  825.    various capabilities before deciding whether to go into ncurses mode.
  826.    An easy way to do this is to call setupterm(), then use the functions
  827.    tigetflag(), tigetnum(), and tigetstr() to do your testing.
  828.    
  829.    A particularly useful case of this often comes up when you want to
  830.    test whether a given terminal type should be treated as `smart'
  831.    (cursor-addressable) or `stupid'. The right way to test this is to see
  832.    if the return value of tigetstr("cup") is non-NULL. Alternatively, you
  833.    can include the term.h file and test the value of the macro
  834.    cursor_address.
  835.    
  836.   TUNING FOR SPEED
  837.   
  838.    Use the addchstr() family of functions for fast screen-painting of
  839.    text when you know the text doesn't contain any control characters.
  840.    Try to make attribute changes infrequent on your screens. Don't use
  841.    the immedok() option!
  842.    
  843.   SPECIAL FEATURES OF NCURSES
  844.   
  845.    When running on PC-clones, ncurses has enhanced support for the IBM
  846.    high-half and ROM characters. The A_ALTCHARSET highlight, enables
  847.    display of both high-half ACS graphics and the PC ROM graphics 0-31
  848.    that are normally interpreted as control characters.
  849.    
  850.    The wresize() function allows you to resize a window in place.
  851.    
  852. Compatibility with Older Versions
  853.  
  854.    Despite our best efforts, there are some differences between ncurses
  855.    and the (undocumented!) behavior of older curses implementations.
  856.    These arise from ambiguities or omissions in the documentation of the
  857.    API.
  858.    
  859.   REFRESH OF OVERLAPPING WINDOWS
  860.   
  861.    If you define two windows A and B that overlap, and then alternately
  862.    scribble on and refresh them, the changes made to the overlapping
  863.    region under historic curses versions were often not documented
  864.    precisely.
  865.    
  866.    To understand why this is a problem, remember that screen updates are
  867.    calculated between two representations of the _entire_ display. The
  868.    documentation says that when you refresh a window, it is first copied
  869.    to to the virtual screen, and then changes are calculated to update
  870.    the physical screen (and applied to the terminal). But "copied to" is
  871.    not very specific, and subtle differences in how copying works can
  872.    produce different behaviors in the case where two overlapping windows
  873.    are each being refreshed at unpredictable intervals.
  874.    
  875.    What happens to the overlapping region depends on what wnoutrefresh()
  876.    does with its argument -- what portions of the argument window it
  877.    copies to the virtual screen. Some implementations do "change copy",
  878.    copying down only locations in the window that have changed (or been
  879.    marked changed with wtouchln() and friends). Some implementations do
  880.    "entire copy", copying _all_ window locations to the virtual screen
  881.    whether or not they have changed.
  882.    
  883.    The ncurses library itself has not always been consistent on this
  884.    score. Due to a bug, versions 1.8.7 to 1.9.8a did entire copy.
  885.    Versions 1.8.6 and older, and versions 1.9.9 and newer, do change
  886.    copy.
  887.    
  888.    For most commercial curses implementations, it is not documented and
  889.    not known for sure (at least not to the ncurses maintainers) whether
  890.    they do change copy or entire copy. We know that System V release 3
  891.    curses has logic in it that looks like an attempt to do change copy,
  892.    but the surrounding logic and data representations are sufficiently
  893.    complex, and our knowledge sufficiently indirect, that it's hard to
  894.    know whether this is reliable. It is not clear what the SVr4
  895.    documentation and XSI standard intend. The XSI Curses standard barely
  896.    mentions wnoutrefresh(); the SVr4 documents seem to be describing
  897.    entire-copy, but it is possible with some effort and straining to read
  898.    them the other way.
  899.    
  900.    It might therefore be unwise to rely on either behavior in programs
  901.    that might have to be linked with other curses implementations.
  902.    Instead, you can do an explicit touchwin() before the wnoutrefresh()
  903.    call to guarantee an entire-contents copy anywhere.
  904.    
  905.    The really clean way to handle this is to use the panels library. If,
  906.    when you want a screen update, you do update_panels(), it will do all
  907.    the necessary wnoutrfresh() calls for whatever panel stacking order
  908.    you have defined. Then you can do one doupdate() and there will be a
  909.    _single_ burst of physical I/O that will do all your updates.
  910.    
  911.   BACKGROUND ERASE
  912.   
  913.    If you have been using a very old versions of ncurses (1.8.7 or older)
  914.    you may be surprised by the behavior of the erase functions. In older
  915.    versions, erased areas of a window were filled with a blank modified
  916.    by the window's current attribute (as set by _wattrset()_,
  917.    _wattron()_, _wattroff()_ and friends).
  918.    
  919.    In newer versions, this is not so. Instead, the attribute of erased
  920.    blanks is normal unless and until it is modified by the functions
  921.    bkgdset() or wbkgdset().
  922.    
  923.    This change in behavior conforms ncurses to System V Release 4 and the
  924.    XSI Curses standard.
  925.    
  926. XSI Curses Conformance
  927.  
  928.    The ncurses library is intended to be base-level conformant with the
  929.    XSI Curses standard from X/Open. May extended-level features (in fact,
  930.    almost all features not directly concerned with wide characters and
  931.    internationalization) are also supported.
  932.    
  933.    One effect of XSI conformance is the change in behavior described
  934.    under "Background Erase -- Compatibility with Old Versions".
  935.    
  936.    Also, ncurses meets the XSI requirement that every macro entry point
  937.    have a corresponding function which may be linked (and will be
  938.    prototype-checked) if the macro definition is disabled with #undef.
  939.    
  940.                               THE PANELS LIBRARY
  941.                                        
  942.    The ncurses library by itself provides good support for screen
  943.    displays in which the windows are tiled (non-overlapping). In the more
  944.    general case that windows may overlap, you have to use a series of
  945.    wnoutrefresh() calls followed by a doupdate(), and be careful about
  946.    the order you do the window refreshes in. It has to be bottom-upwards,
  947.    otherwise parts of windows that should be obscured will show through.
  948.    
  949.    When your interface design is such that windows may dive deeper into
  950.    the visibility stack or pop to the top at runtime, the resulting
  951.    book-keeping can be tedious and difficult to get right. Hence the
  952.    panels library.
  953.    
  954.    The panel library first appeared in AT&T System V. The version
  955.    documented here is the freeware panel code distributed with ncurses.
  956.    
  957. Compiling With the Panels Library
  958.  
  959.    Your panels-using modules must import the panels library declarations
  960.    with
  961.    
  962.  
  963.           #include <panel.h>
  964.  
  965.    and must be linked explicitly with the panels library using an -lpanel
  966.    argument. Note that they must also link the ncurses library with
  967.    -lncurses. Most modern linkers are two-pass and will accept either
  968.    order, but it is still good practice to put -lpanel first and
  969.    -lncurses second.
  970.    
  971. Overview of Panels
  972.  
  973.    A panel object is a window that is implicitly treated as part of a
  974.    deck including all other panel objects. The deck has an implicit
  975.    bottom-to-top visibility order. The panels library includes an update
  976.    function (analogous to refresh()) that displays all panels in the deck
  977.    in the proper order to resolve overlaps. The standard window, stdscr,
  978.    is considered below all panels.
  979.    
  980.    Details on the panels functions are available in the man pages. We'll
  981.    just hit the highlights here.
  982.    
  983.    You create a panel from a window by calling new_panel() on a window
  984.    pointer. It then becomes the top of the deck. The panel's window is
  985.    available as the value of panel_window() called with the panel pointer
  986.    as argument.
  987.    
  988.    You can delete a panel (removing it from the deck) with del_panel.
  989.    This will not deallocate the associated window; you have to do that
  990.    yourself. You can replace a panel's window with a different window by
  991.    calling replace_window. The new window may be of different size; the
  992.    panel code will re-compute all overlaps. This operation doesn't change
  993.    the panel's position in the deck.
  994.    
  995.    To move a panel's window, use move_panel(). The mvwin() function on
  996.    the panel's window isn't sufficient because it doesn't update the
  997.    panels library's representation of where the windows are. This
  998.    operation leaves the panel's depth, contents, and size unchanged.
  999.    
  1000.    Two functions (top_panel(), bottom_panel()) are provided for
  1001.    rearranging the deck. The first pops its argument window to the top of
  1002.    the deck; the second sends it to the bottom. Either operation leaves
  1003.    the panel's screen location, contents, and size unchanged.
  1004.    
  1005.    The function update_panels() does all the wnoutrefresh() calls needed
  1006.    to prepare for doupdate() (which you must call yourself, afterwards).
  1007.    
  1008.    Typically, you will want to call update_panels() and doupdate() just
  1009.    before accepting command input, once in each cycle of interaction with
  1010.    the user. If you call update_panels() after each and every panel
  1011.    write, youll generate a lot of unnecessary refresh activity and screen
  1012.    flicker.
  1013.    
  1014. Panels, Input, and the Standard Screen
  1015.  
  1016.    You shouldn't mix wnoutrefresh() or wrefresh() operations with panels
  1017.    code; this will work only if the argument window is either in the top
  1018.    panel or un-obscured by any other panels.
  1019.    
  1020.    The stsdcr window is a special case. It is considered below all
  1021.    panels. Because changes to panels may obscure parts of stdscr, though,
  1022.    you should call update_panels() before doupdate() even when you only
  1023.    change stdscr.
  1024.    
  1025.    Note that wgetch automatically calls wrefresh. Therefore, before
  1026.    requesting input from a panel window, you need to be sure that the
  1027.    panel is totally un-obscured.
  1028.    
  1029.    There is presently no way to display changes to one obscured panel
  1030.    without repainting all panels.
  1031.    
  1032. Hiding Panels
  1033.  
  1034.    It's possible to remove a panel from the deck temporarily; use
  1035.    hide_panel for this. You can un-hide a panel with show_panel(). The
  1036.    predicate function panel_hidden tests whether or not a panel is
  1037.    hidden.
  1038.    
  1039.    The panel_update code ignores hidden panels. You cannot do top_panel()
  1040.    or bottom_panel on a hidden panel(). Other panels operations are
  1041.    applicable.
  1042.    
  1043. Miscellaneous Other Facilities
  1044.  
  1045.    It's possible to navigate the deck using the functions panel_above()
  1046.    and panel_below. Handed a panel pointer, they return the panel above
  1047.    or below that panel. Handed NULL, they return the bottom-most or
  1048.    top-most panel.
  1049.    
  1050.    Every panel has an associated user pointer, not used by the panel
  1051.    code, to which you can attach application data. See the man page
  1052.    documentation of set_panel_userptr() and panel_userptr for details.
  1053.    
  1054.                                THE MENU LIBRARY
  1055.                                        
  1056.    A menu is a screen display that assists the user to choose some subset
  1057.    of a given set of items. The menu library is a curses extension that
  1058.    supports easy programming of menu hierarchies with a uniform but
  1059.    flexible interface.
  1060.    
  1061.    The menu library first appeared in AT&T System V. The version
  1062.    documented here is the freeware menu code distributed with ncurses.
  1063.    
  1064. Compiling With the menu Library
  1065.  
  1066.    Your menu-using modules must import the menu library declarations with
  1067.    
  1068.  
  1069.           #include <menu.h>
  1070.  
  1071.    and must be linked explicitly with the menus library using an -lmenu
  1072.    argument. Note that they must also link the ncurses library with
  1073.    -lncurses. Most modern linkers are two-pass and will accept either
  1074.    order, but it is still good practice to put -lmenu first and -lncurses
  1075.    second.
  1076.    
  1077. Overview of Menus
  1078.  
  1079.    The menus created by this library consist of collections of items
  1080.    including a name string part and a description string part. To make
  1081.    menus, you create groups of these items and connect them with menu
  1082.    frame objects.
  1083.    
  1084.    The menu can then by posted, that is written to an associated window.
  1085.    Actually, each menu has two associated windows; a containing window in
  1086.    which the programmer can scribble titles or borders, and a subwindow
  1087.    in which the menu items proper are displayed. If this subwindow is too
  1088.    small to display all the items, it will be a scrollable viewport on
  1089.    the collection of items.
  1090.    
  1091.    A menu may also be unposted (that is, undisplayed), and finally freed
  1092.    to make the storage associated with it and its items available for
  1093.    re-use.
  1094.    
  1095.    The general flow of control of a menu program looks like this:
  1096.     1. Initialize curses.
  1097.     2. Create the menu items, using new_item().
  1098.     3. Create the menu using new_menu().
  1099.     4. Post the menu using menu_post().
  1100.     5. Refresh the screen.
  1101.     6. Process user requests via an input loop.
  1102.     7. Unpost the menu using menu_unpost().
  1103.     8. Free the menu, using free_menu().
  1104.     9. Free the items using free_item().
  1105.    10. Terminate curses.
  1106.        
  1107. Selecting items
  1108.  
  1109.    Menus may be multi-valued or (the default) single-valued (see the
  1110.    manual page mitem_opts(3x) to see how to change the default). Both
  1111.    types always have a current item.
  1112.    
  1113.    From a single-valued menu you can read the selected value simply by
  1114.    looking at the current item. From a multi-valued menu, you get the
  1115.    selected set by looping through the items applying the item_value()
  1116.    predicate function. Your menu-processing code can use the function
  1117.    set_item_value() to flag the items in the select set.
  1118.    
  1119.    Menu items can be made un-selectable using set_item_opts() or
  1120.    item_opts_off() with the O_SELECTABLE argument. This is the only
  1121.    option so far defined for menus, but it is good practice to code as
  1122.    though other option bits might be on.
  1123.    
  1124. Menu Display
  1125.  
  1126.    The menu library calculates a minimum display size for your window,
  1127.    based on the following variables:
  1128.    
  1129.      * The number and maximum length of the menu items
  1130.      * Whether the O_ROWMAJOR option is enabled
  1131.      * Whether display of descriptions is enabled
  1132.      * Whatever menu format may have been set by the programmer
  1133.      * The length of the menu mark string used for highlighting selected
  1134.        items
  1135.        
  1136.    The function set_menu_format() allows you to set the maximum size of
  1137.    the viewport or menu page that will be used to display menu items. You
  1138.    can retrieve any format associated with a menu with menu_format(). The
  1139.    default format is rows=16, columns=1.
  1140.    
  1141.    The actual menu page may be smaller than the format size. This depends
  1142.    on the item number and size and whether O_ROWMAJOR is on. This option
  1143.    (on by default) causes menu items to be displayed in a `raster-scan'
  1144.    pattern, so that if more than one item will fit horizontally the first
  1145.    couple of items are side-by-side in the top row. The alternative is
  1146.    column-major display, which tries to put the first several items in
  1147.    the first column.
  1148.    
  1149.    As mentioned above, a menu format not large enough to allow all items
  1150.    to fit on-screen will result in a menu display that is vertically
  1151.    scrollable.
  1152.    
  1153.    You can scroll it with requests to the menu driver, which will be
  1154.    described in the section on menu input handling.
  1155.    
  1156.    Each menu has a mark string used to visually tag selected items; see
  1157.    the menu_mark(3x) manual page for details. The mark string length also
  1158.    influences the menu page size.
  1159.    
  1160.    The function scale_menu() returns the minimum display size that the
  1161.    menu code computes from all these factors. There are other menu
  1162.    display attributes including a select attribute, an attribute for
  1163.    selectable items, an attribute for unselectable items, and a pad
  1164.    character used to separate item name text from description text. These
  1165.    have reasonable defaults which the library allows you to change (see
  1166.    the menu_attribs(3x)manual page.
  1167.    
  1168. Menu Windows
  1169.  
  1170.    Each menu has, as mentioned previously, a pair of associated windows.
  1171.    Both these windows are painted when the menu is posted and erased when
  1172.    the menu is unposted.
  1173.    
  1174.    The outer or frame window is not otherwise touched by the menu
  1175.    routines. It exists so the programmer can associate a title, a border,
  1176.    or perhaps help text with the menu and have it properly refreshed or
  1177.    erased at post/unpost time. The inner window or subwindow is where the
  1178.    current menu page is displayed.
  1179.    
  1180.    By default, both windows are stdscr. You can set them with the
  1181.    functions in menu_win(3x).
  1182.    
  1183.    When you call menu_post(), you write the menu to its subwindow. When
  1184.    you call menu_unpost(), you erase the subwindow, However, neither of
  1185.    these actually modifies the screen. To do that, call wrefresh() or
  1186.    some equivalent.
  1187.    
  1188. Processing Menu Input
  1189.  
  1190.    The main loop of your menu-processing code should call menu_driver()
  1191.    repeatedly. The first argument of this routine is a menu pointer; the
  1192.    second is a menu command code. You should write an input-fetching
  1193.    routine that maps input characters to menu command codes, and pass its
  1194.    output to menu_driver(). The menu command codes are fully documented
  1195.    in menu_driver(3x).
  1196.    
  1197.    The simplest group of command codes is REQ_NEXT_ITEM, REQ_PREV_ITEM,
  1198.    REQ_FIRST_ITEM, REQ_LAST_ITEM, REQ_UP_ITEM, REQ_DOWN_ITEM,
  1199.    REQ_LEFT_ITEM, REQ_RIGHT_ITEM. These change the currently selected
  1200.    item. These requests may cause scrolling of the menu page if it only
  1201.    partially displayed.
  1202.    
  1203.    There are explicit requests for scrolling which also change the
  1204.    current item (because the select location does not change, but the
  1205.    item there does). These are REQ_SCR_DLINE, REQ_SCR_ULINE,
  1206.    REQ_SCR_DPAGE, and REQ_SCR_UPAGE.
  1207.    
  1208.    The REQ_TOGGLE_ITEM selects or deselects the current item. It is for
  1209.    use in multi-valued menus; if you use it with O_ONEVALUE on, you'll
  1210.    get an error return (E_REQUEST_DENIED).
  1211.    
  1212.    Each menu has an associated pattern buffer. The menu_driver() logic
  1213.    tries to accumulate printable ASCII characters passed in in that
  1214.    buffer; when it matches a prefix of an item name, that item (or the
  1215.    next matching item) is selected. If appending a character yields no
  1216.    new match, that character is deleted from the pattern buffer, and
  1217.    menu_driver() returns E_NO_MATCH.
  1218.    
  1219.    Some requests change the pattern buffer directly: REQ_CLEAR_PATTERN,
  1220.    REQ_BACK_PATTERN, REQ_NEXT_MATCH, REQ_PREV_MATCH. The latter two are
  1221.    useful when pattern buffer input matches more than one item in a
  1222.    multi-valued menu.
  1223.    
  1224.    Each successful scroll or item navigation request clears the pattern
  1225.    buffer. It is also possible to set the pattern buffer explicitly with
  1226.    set_menu_pattern().
  1227.    
  1228.    Finally, menu driver requests above the constant MAX_COMMAND are
  1229.    considered application-specific commands. The menu_driver() code
  1230.    ignores them and returns E_UNKNOWN_COMMAND.
  1231.    
  1232. Miscellaneous Other Features
  1233.  
  1234.    Various menu options can affect the processing and visual appearance
  1235.    and input processing of menus. See menu_opts(3x) for details.
  1236.    
  1237.    It is possible to change the current item from application code; this
  1238.    is useful if you want to write your own navigation requests. It is
  1239.    also possible to explicitly set the top row of the menu display. See
  1240.    mitem_current(3x). If your application needs to change the menu
  1241.    subwindow cursor for any reason, pos_menu_cursor() will restore it to
  1242.    the correct location for continuing menu driver processing.
  1243.    
  1244.    It is possible to set hooks to be called at menu initialization and
  1245.    wrapup time, and whenever the selected item changes. See
  1246.    menu_hook(3x).
  1247.    
  1248.    Each item, and each menu, has an associated user pointer on which you
  1249.    can hang application data. See mitem_userptr(3x) and menu_userptr(3x).
  1250.    
  1251.    
  1252.                                THE FORMS LIBRARY
  1253.                                        
  1254.    The form library is a curses extension that supports easy programming
  1255.    of on-screen forms for data entry and program control.
  1256.    
  1257.    The form library first appeared in AT&T System V. The version
  1258.    documented here is the freeware form code distributed with ncurses.
  1259.    
  1260. Compiling With the form Library
  1261.  
  1262.    Your form-using modules must import the form library declarations with
  1263.    
  1264.  
  1265.           #include <form.h>
  1266.  
  1267.    and must be linked explicitly with the forms library using an -lform
  1268.    argument. Note that they must also link the ncurses library with
  1269.    -lncurses. Most modern linkers are two-pass and will accept either
  1270.    order, but it is still good practice to put -lform first and -lncurses
  1271.    second.
  1272.    
  1273. Overview of Forms
  1274.  
  1275.    A form is a collection of fields; each field may be either a label
  1276.    (explanatory text) or a data-entry location. Long forms may be
  1277.    segmented into pages; each entry to a new page clears the screen.
  1278.    
  1279.    To make forms, you create groups of fields and connect them with form
  1280.    frame objects; the form library makes this relatively simple.
  1281.    
  1282.    Once defined, a form can be posted, that is written to an associated
  1283.    window. Actually, each form has two associated windows; a containing
  1284.    window in which the programmer can scribble titles or borders, and a
  1285.    subwindow in which the form fields proper are displayed.
  1286.    
  1287.    As the form user fills out the posted form, navigation and editing
  1288.    keys support movement between fields, editing keys support modifying
  1289.    field, and plain text adds to or changes data in a current field. The
  1290.    form library allows you (the forms designer) to bind each navigation
  1291.    and editing key to any keystroke accepted by curses Fields may have
  1292.    validation conditions on them, so that they check input data for type
  1293.    and value. The form library supplies a rich set of pre-defined field
  1294.    types, and makes it relatively easy to define new ones.
  1295.    
  1296.    Once its transaction is completed (or aborted), a form may be unposted
  1297.    (that is, undisplayed), and finally freed to make the storage
  1298.    associated with it and its items available for re-use.
  1299.    
  1300.    The general flow of control of a form program looks like this:
  1301.     1. Initialize curses.
  1302.     2. Create the form fields, using new_field().
  1303.     3. Create the form using new_form().
  1304.     4. Post the form using form_post().
  1305.     5. Refresh the screen.
  1306.     6. Process user requests via an input loop.
  1307.     7. Unpost the form using form_unpost().
  1308.     8. Free the form, using free_form().
  1309.     9. Free the fields using free_field().
  1310.    10. Terminate curses.
  1311.        
  1312.    Note that this looks much like a menu program; the form library
  1313.    handles tasks which are in many ways similar, and its interface was
  1314.    obviously designed to resemble that of the menu library wherever
  1315.    possible.
  1316.    
  1317.    In forms programs, however, the `process user requests' is somewhat
  1318.    more complicated than for menus. Besides menu-like navigation
  1319.    operations, the menu driver loop has to support field editing and data
  1320.    validation.
  1321.    
  1322. Creating and Freeing Fields and Forms
  1323.  
  1324.    The basic function for creating fields is new_field():
  1325.    
  1326.  
  1327. FIELD *new_field(int height, int width,   /* new field size */
  1328.                  int top, int left,       /* upper left corner */
  1329.                  int offscreen,           /* number of offscreen rows */
  1330.                  int nbuf);               /* number of working buffers */
  1331.  
  1332.    Menu items always occupy a single row, but forms fields may have
  1333.    multiple rows. So new_field() requires you to specify a width and
  1334.    height (the first two arguments, which mist both be greater than
  1335.    zero).
  1336.    
  1337.    You must also specify the location of the field's upper left corner on
  1338.    the screen (the third and fourth arguments, which must be zero or
  1339.    greater). Note that these coordinates are relative to the form
  1340.    subwindow, which will coincide with stdscr by default but need not be
  1341.    stdscr if you've done an explicit set_form_window() call.
  1342.    
  1343.    The fifth argument allows you to specify a number of off-screen rows.
  1344.    If this is zero, the entire field will always be displayed. If it is
  1345.    nonzero, the form will be scrollable, with only one screen-full
  1346.    (initially the top part) displayed at any given time. If you make a
  1347.    field dynamic and grow it so it will no longer fit on the screen, the
  1348.    form will become scrollable even if the \fBoffscreen\fR argument was
  1349.    initially zero.
  1350.    
  1351.    The forms library allocates one working buffer per field; the size of
  1352.    each buffer is ((height + offscreen)*width + 1, one character for each
  1353.    position in the field plus a NUL terminator. The sixth argument is the
  1354.    number of additional data buffers to allocate for the field; your
  1355.    application can use them for its own purposes.
  1356.    
  1357.  
  1358. FIELD *dup_field(FIELD *field,            /* field to copy */
  1359.                  int top, int left);      /* location of new copy */
  1360.  
  1361.    The function dup_field() duplicates an existing field at a new
  1362.    location. Size and buffering information are copied; some attribute
  1363.    flags and status bits are not (see the form_field_new(3X) for
  1364.    details).
  1365.    
  1366.  
  1367. FIELD *link_field(FIELD *field,           /* field to copy */
  1368.                   int top, int left);     /* location of new copy */
  1369.  
  1370.    The function link_field() also duplicates an existing field at a new
  1371.    location. The difference from dup_field() is that it arranges for the
  1372.    new field's buffer to be shared with the old one.
  1373.    
  1374.    Besides the obvious use in making a field editable from two different
  1375.    form pages, linked fields give you a way to hack in dynamic labels. If
  1376.    you declare several fields linked to an original, and then make them
  1377.    inactive, changes from the original will still be propagated to the
  1378.    linked fields.
  1379.    
  1380.    As with duplicated fields, linked fields have attribute bits separate
  1381.    from the original.
  1382.    
  1383.    As you might guess, all these field-allocations return NULL if the
  1384.    field allocation is not possible due to an out-of-memory error or
  1385.    out-of-bounds arguments.
  1386.    
  1387.    To connect fields to a form, use
  1388.    
  1389.  
  1390. FORM *new_form(FIELD **fields);
  1391.  
  1392.    This function expects to see a NULL-terminated array of field
  1393.    pointers. Said fields are connected to a newly-allocated form object;
  1394.    its address is returned (or else NULL if the allocation fails).
  1395.    
  1396.    Note that new_field() does _not_ copy the pointer array into private
  1397.    storage; if you modify the contents of the pointer array during forms
  1398.    processing, all manner of bizarre things might happen. Also note that
  1399.    any given field may only be connected to one form.
  1400.    
  1401.    The functions free_field() and free_form are available to free field
  1402.    and form objects. It is an error to attempt to free a field connected
  1403.    to a form, but not vice-versa; thus, you will generally free your form
  1404.    objects first.
  1405.    
  1406. Fetching and Changing Field Attributes
  1407.  
  1408.    Each form field has a number of location and size attributes
  1409.    associated with it. There are other field attributes used to control
  1410.    display and editing of the field. Some (for example, the
  1411.    \fBO_STATIC\fR bit) involve sufficient complications to be covered in
  1412.    sections of their own later on. We cover the functions used to get and
  1413.    set several basic attributes here.
  1414.    
  1415.    When a field is created, the attributes not specified by the new_field
  1416.    function are copied from an invisible system default field. In
  1417.    attribute-setting and -fetching functions, the argument NULL is taken
  1418.    to mean this field. Changes to it persist as defaults until your forms
  1419.    application terminates.
  1420.    
  1421.   FETCHING SIZE AND LOCATION DATA
  1422.   
  1423.    You can retrieve field sizes and locations through:
  1424.    
  1425.  
  1426. int field_info(FIELD *field,              /* field from which to fetch */
  1427.                int *height, *int width,   /* field size */
  1428.                int *top, int *left,       /* upper left corner */
  1429.                int *offscreen,            /* number of offscreen rows */
  1430.                int *nbuf);                /* number of working buffers */
  1431.  
  1432.    This function is a sort of inverse of new_field(); instead of setting
  1433.    size and location attributes of a new field, it fetches them from an
  1434.    existing one.
  1435.    
  1436.   CHANGING THE FIELD LOCATION
  1437.   
  1438.    If is possible to move a field's location on the screen:
  1439.    
  1440.  
  1441. int move_field(FIELD *field,              /* field to alter */
  1442.                int top, int left);        /* new upper-left corner */
  1443.  
  1444.    You can, of course. query the current location through field_info().
  1445.    
  1446.   THE JUSTIFICATION ATTRIBUTE
  1447.   
  1448.    One-line fields may be unjustified, justified right, justified left,
  1449.    or centered. Here is how you manipulate this attribute:
  1450.    
  1451.  
  1452. int set_field_just(FIELD *field,          /* field to alter */
  1453.                    int justmode);         /* mode to set */
  1454.  
  1455. int field_just(FIELD *field);             /* fetch mode of field */
  1456.  
  1457.    The mode values accepted and returned by this functions are
  1458.    preprocessor macros NO_JUSTIFICATION, JUSTIFY_RIGHT, JUSTIFY_LEFT, or
  1459.    JUSTIFY_CENTER.
  1460.    
  1461.   FIELD DISPLAY ATTRIBUTES
  1462.   
  1463.    For each field, you can set a foreground attribute for entered
  1464.    characters, a background attribute for the entire field, and a pad
  1465.    character for the unfilled portion of the field. You can also control
  1466.    pagination of the form.
  1467.    
  1468.    This group of four field attributes controls the visual appearance of
  1469.    the field on the screen, without affecting in any way the data in the
  1470.    field buffer.
  1471.    
  1472.  
  1473. int set_field_fore(FIELD *field,          /* field to alter */
  1474.                    chtype attr);          /* attribute to set */
  1475.  
  1476. chtype field_fore(FIELD *field);          /* field to query */
  1477.  
  1478. int set_field_back(FIELD *field,          /* field to alter */
  1479.                    chtype attr);          /* attribute to set */
  1480.  
  1481. chtype field_back(FIELD *field);          /* field to query */
  1482.  
  1483. int set_field_pad(FIELD *field,           /* field to alter */
  1484.                  int pad);                /* pad character to set */
  1485.  
  1486. chtype field_pad(FIELD *field);
  1487.  
  1488. int set_new_page(FIELD *field,            /* field to alter */
  1489.                  int flag);               /* TRUE to force new page */
  1490.  
  1491. chtype new_page(FIELD *field);            /* field to query */
  1492.  
  1493.    The attributes set and returned by the first four functions are normal
  1494.    curses(3x) display attribute values (A_STANDOUT, A_BOLD, A_REVERSE
  1495.    etc). The page bit of a field controls whether it is displayed at the
  1496.    start of a new form screen.
  1497.    
  1498.   FIELD OPTION BITS
  1499.   
  1500.    There is also a large collection of field option bits you can set to
  1501.    control various aspects of forms processing. You can manipulate them
  1502.    with these functions:
  1503.    
  1504.  
  1505. int set_field_opts(FIELD *field,          /* field to alter */
  1506.                    int attr);             /* attribute to set */
  1507.  
  1508. int field_opts_on(FIELD *field,           /* field to alter */
  1509.                   int attr);              /* attributes to turn on */
  1510.  
  1511. int field_opts_off(FIELD *field,          /* field to alter */
  1512.                    int attr);             /* attributes to turn off */
  1513.  
  1514. int field_opts(FIELD *field);             /* field to query */
  1515.  
  1516.    By default, all options are on. Here are the available option bits:
  1517.    
  1518.    O_VISIBLE
  1519.           Controls whether the field is visible on the screen. Can be
  1520.           used during form processing to hide or pop up fields depending
  1521.           on the value of parent fields.
  1522.           
  1523.    O_ACTIVE
  1524.           Controls whether the field is active during forms processing
  1525.           (i.e. visited by form navigation keys). Can be used to make
  1526.           labels or derived fields with buffer values alterable by the
  1527.           forms application, not the user.
  1528.           
  1529.    O_PUBLIC
  1530.           Controls whether data is displayed during field entry. If this
  1531.           option is turned off on a field, the library will accept and
  1532.           edit data in that field, but it will not be displayed and the
  1533.           visible field cursor will not move. You can turn off the
  1534.           O_PUBLIC bit to define password fields.
  1535.           
  1536.    O_EDIT
  1537.           Controls whether the field's data can be modified. When this
  1538.           option is off, all editing requests except REQ_PREV_CHOICE and
  1539.           REQ_NEXT_CHOICEwill fail. Such read-only fields may be useful
  1540.           for help messages.
  1541.           
  1542.    O_WRAP
  1543.           Controls word-wrapping in multi-line fields. Normally, when any
  1544.           character of a (blank-separated) word reaches the end of the
  1545.           current line, the entire word is wrapped to the next line
  1546.           (assuming there is one). When this option is off, the word will
  1547.           be split across the line break.
  1548.           
  1549.    O_BLANK
  1550.           Controls field blanking. When this option is on, entering a
  1551.           character at the first field position erases the entire field
  1552.           (except for the just-entered character).
  1553.           
  1554.    O_AUTOSKIP
  1555.           Controls automatic skip to next field when this one fills.
  1556.           Normally, when the forms user tries to type more data into a
  1557.           field than will fit, the editing location jumps to next field.
  1558.           When this option is off, the user's cursor will hang at the end
  1559.           of the field. This option is ignored in dynamic fields that
  1560.           have not reached their size limit.
  1561.           
  1562.    O_NULLOK
  1563.           Controls whether validation is applied to blank fields.
  1564.           Normally, it is not; the user can leave a field blank without
  1565.           invoking the usual validation check on exit. If this option is
  1566.           off on a field, exit from it will invoke a validation check.
  1567.           
  1568.    O_PASSOK
  1569.           Controls whether validation occurs on every exit, or only after
  1570.           the field is modified. Normally the latter is true. Setting
  1571.           O_PASSOK may be useful if your field's validation function may
  1572.           change during forms processing.
  1573.           
  1574.    O_STATIC
  1575.           Controls whether the field is fixed to its initial dimensions.
  1576.           If you turn this off, the field becomes dynamic and will
  1577.           stretch to fit entered data.
  1578.           
  1579.    A field's options cannot be changed while the field is currently
  1580.    selected. However, options may be changed on posted fields that are
  1581.    not current.
  1582.    
  1583.    The option values are bit-masks and can be composed with logical-or in
  1584.    the obvious way.
  1585.    
  1586. Field Status
  1587.  
  1588.    Every field has a status flag, which is set to FALSE when the field is
  1589.    created and TRUE when the value in field buffer 0 changes. This flag
  1590.    can be queried and set directly:
  1591.    
  1592.  
  1593. int set_field_status(FIELD *field,      /* field to alter */
  1594.                    int status);         /* mode to set */
  1595.  
  1596. int field_status(FIELD *field);         /* fetch mode of field */
  1597.  
  1598.    Setting this flag under program control can be useful if you use the
  1599.    same form repeatedly, looking for modified fields each time.
  1600.    
  1601.    Calling field_status() on a field not currently selected for input
  1602.    will return a correct value. Calling field_status() on a field that is
  1603.    currently selected for input may not necessarily give a correct field
  1604.    status value, because entered data isn't necessarily copied to buffer
  1605.    zero before the exit validation check. To guarantee that the returned
  1606.    status value reflects reality, call field_status() either (1) in the
  1607.    field's exit validation check routine, (2) from the field's or form's
  1608.    initialization or termination hooks, or (3) just after a
  1609.    REQ_VALIDATION request has been processed by the forms driver.
  1610.    
  1611. Field User Pointer
  1612.  
  1613.    Each field structure contains one character pointer slot that is not
  1614.    used by the forms library. It is intended to be used by applications
  1615.    to store private per-field data. You can manipulate it with:
  1616.    
  1617.  
  1618. int set_field_userptr(FIELD *field,       /* field to alter */
  1619.                    char *userptr);        /* mode to set */
  1620.  
  1621. char *field_userptr(FIELD *field);        /* fetch mode of field */
  1622.  
  1623.    (Properly, this user pointer field ought to have (void *) type. The
  1624.    (char *) type is retained for System V compatibility.)
  1625.    
  1626.    It is valid to set the user pointer of the default field (with a
  1627.    set_field_userptr() call passed a NULL field pointer.) When a new
  1628.    field is created, the default-field user pointer is copied to
  1629.    initialize the new field's user pointer.
  1630.    
  1631. Variable-Sized Fields
  1632.  
  1633.    Normally, a field is fixed at the size specified for it at creation
  1634.    time. If, however, you turn off its O_STATIC bit, it becomes dynamic
  1635.    and will automatically resize itself to accommodate data as it is
  1636.    entered. If the field has extra buffers associated with it, they will
  1637.    grow right along with the main input buffer.
  1638.    
  1639.    A one-line dynamic field will have a fixed height (1) but variable
  1640.    width, scrolling horizontally to display data within the field area as
  1641.    originally dimensioned and located. A multi-line dynamic field will
  1642.    have a fixed width, but variable height (number of rows), scrolling
  1643.    vertically to display data within the field area as originally
  1644.    dimensioned and located.
  1645.    
  1646.    Normally, a dynamic field is allowed to grow without limit. But it is
  1647.    possible to set an upper limit on the size of a dynamic field. You do
  1648.    it with this function:
  1649.    
  1650.  
  1651. int set_max_field(FIELD *field,     /* field to alter (may not be NULL) */
  1652.                    int max_size);   /* upper limit on field size */
  1653.  
  1654.    If the field is one-line, max_size is taken to be a column size limit;
  1655.    if it is multi-line, it is taken to be a line size limit. To disable
  1656.    any limit, use an argument of zero. The growth limit can be changed
  1657.    whether or not the O_STATIC bit is on, but has no effect until it is.
  1658.    
  1659.    The following properties of a field change when it becomes dynamic:
  1660.      * If there is no growth limit, there is no final position of the
  1661.        field; therefore O_AUTOSKIP and O_NL_OVERLOAD are ignored.
  1662.      * Field justification will be ignored (though whatever justification
  1663.        is set up will be retained internally and can be queried).
  1664.      * The dup_field() and link_field() calls copy dynamic-buffer sizes.
  1665.        If the O_STATIC option is set on one of a collection of links,
  1666.        buffer resizing will occur only when the field is edited through
  1667.        that link.
  1668.      * The call field_info() will retrieve the original static size of
  1669.        the field; use dynamic_field_info() to get the actual dynamic
  1670.        size.
  1671.        
  1672. Field Validation
  1673.  
  1674.    By default, a field will accept any data that will fit in its input
  1675.    buffer. However, it is possible to attach a validation type to a
  1676.    field. If you do this, any attempt to leave the field while it
  1677.    contains data that doesn't match the validation type will fail. Some
  1678.    validation types also have a character-validity check for each time a
  1679.    character is entered in the field.
  1680.    
  1681.    A field's validation check (if any) is not called when
  1682.    set_field_buffer() modifies the input buffer, nor when that buffer is
  1683.    changed through a linked field.
  1684.    
  1685.    The form library provides a rich set of pre-defined validation types,
  1686.    and gives you the capability to define custom ones of your own. You
  1687.    can examine and change field validation attributes with the following
  1688.    functions:
  1689.    
  1690.  
  1691. int set_field_type(FIELD *field,          /* field to alter */
  1692.                    FIELDTYPE *ftype,      /* type to associate */
  1693.                    ...);                  /* additional arguments*/
  1694.  
  1695. FIELDTYPE *field_type(FIELD *field);      /* field to query */
  1696.  
  1697.    The validation type of a field is considered an attribute of the
  1698.    field. As with other field attributes, Also, doing set_field_type()
  1699.    with a NULL field default will change the system default for
  1700.    validation of newly-created fields.
  1701.    
  1702.    Here are the pre-defined validation types:
  1703.    
  1704.   TYPE_ALPHA
  1705.   
  1706.    This field type accepts alphabetic data; no blanks, no digits, no
  1707.    special characters (this is checked at character-entry time). It is
  1708.    set up with:
  1709.    
  1710.  
  1711. int set_field_type(FIELD *field,          /* field to alter */
  1712.                    TYPE_ALPHA,            /* type to associate */
  1713.                    int width);            /* maximum width of field */
  1714.  
  1715.    The width argument sets a minimum width of data. Typically you'll want
  1716.    to set this to the field width; if it's greater than the field width,
  1717.    the validation check will always fail. A minimum width of zero makes
  1718.    field completion optional.
  1719.    
  1720.   TYPE_ALNUM
  1721.   
  1722.    This field type accepts alphabetic data and digits; no blanks, no
  1723.    special characters (this is checked at character-entry time). It is
  1724.    set up with:
  1725.    
  1726.  
  1727. int set_field_type(FIELD *field,          /* field to alter */
  1728.                    TYPE_ALNUM,            /* type to associate */
  1729.                    int width);            /* maximum width of field */
  1730.  
  1731.    The width argument sets a minimum width of data. As with TYPE_ALPHA,
  1732.    typically you'll want to set this to the field width; if it's greater
  1733.    than the field width, the validation check will always fail. A minimum
  1734.    width of zero makes field completion optional.
  1735.    
  1736.   TYPE_ENUM
  1737.   
  1738.    This type allows you to restrict a field's values to be among a
  1739.    specified set of string values (for example, the two-letter postal
  1740.    codes for U.S. states). It is set up with:
  1741.    
  1742.  
  1743. int set_field_type(FIELD *field,          /* field to alter */
  1744.                    TYPE_ENUM,             /* type to associate */
  1745.                    char **valuelist;      /* list of possible values */
  1746.                    int checkcase;         /* case-sensitive? */
  1747.                    int checkunique);      /* must specify uniquely? */
  1748.  
  1749.    The valuelist parameter must point at a NULL-terminated list of valid
  1750.    strings. The checkcase argument, if true, makes comparison with the
  1751.    string case-sensitive.
  1752.    
  1753.    When the user exits a TYPE_ENUM field, the validation procedure tries
  1754.    to complete the data in the buffer to a valid entry. If a complete
  1755.    choice string has been entered, it is of course valid. But it is also
  1756.    possible to enter a prefix of a valid string and have it completed for
  1757.    you.
  1758.    
  1759.    By default, if you enter such a prefix and it matches more than one
  1760.    value in the string list, the prefix will be completed to the first
  1761.    matching value. But the checkunique argument, if true, requires prefix
  1762.    matches to be unique in order to be valid.
  1763.    
  1764.    The REQ_NEXT_CHOICE and REQ_PREV_CHOICE input requests can be
  1765.    particularly useful with these fields.
  1766.    
  1767.   TYPE_INTEGER
  1768.   
  1769.    This field type accepts an integer. It is set up as follows:
  1770.    
  1771.  
  1772. int set_field_type(FIELD *field,          /* field to alter */
  1773.                    TYPE_INTEGER,          /* type to associate */
  1774.                    int padding,           /* # places to zero-pad to */
  1775.                    int vmin, int vmax);   /* valid range */
  1776.  
  1777.    Valid characters consist of an optional leading minus and digits. The
  1778.    range check is performed on exit. If the range maximum is less than or
  1779.    equal to the minimum, the range is ignored.
  1780.    
  1781.    If the value passes its range check, it is padded with as many leading
  1782.    zero digits as necessary to meet the padding argument.
  1783.    
  1784.    A TYPE_INTEGER value buffer can conveniently be interpreted with the C
  1785.    library function atoi(3).
  1786.    
  1787.   TYPE_NUMERIC
  1788.   
  1789.    This field type accepts a decimal number. It is set up as follows:
  1790.    
  1791.  
  1792. int set_field_type(FIELD *field,          /* field to alter */
  1793.                    TYPE_NUMERIC,          /* type to associate */
  1794.                    int padding,           /* # places of precision */
  1795.                    int vmin, int vmax);   /* valid range */
  1796.  
  1797.    Valid characters consist of an optional leading minus and digits.
  1798.    possibly including a decimal point. The range check is performed on
  1799.    exit. If the range maximum is less than or equal to the minimum, the
  1800.    range is ignored.
  1801.    
  1802.    If the value passes its range check, it is padded with as many
  1803.    trailing zero digits as necessary to meet the padding argument.
  1804.    
  1805.    A TYPE_NUMERIC value buffer can conveniently be interpreted with the C
  1806.    library function atof(3).
  1807.    
  1808.   TYPE_REGEXP
  1809.   
  1810.    This field type accepts data matching a regular expression. It is set
  1811.    up as follows:
  1812.    
  1813.  
  1814. int set_field_type(FIELD *field,          /* field to alter */
  1815.                    TYPE_REGEXP,           /* type to associate */
  1816.                    char *regexp);         /* expression to match */
  1817.  
  1818.    The syntax for regular expressions is that of regcomp(3). The check
  1819.    for regular-expression match is performed on exit.
  1820.    
  1821. Direct Field Buffer Manipulation
  1822.  
  1823.    The most central attribute of a field is its buffer contents. When a
  1824.    form has been completed, your application usually needs to know the
  1825.    state of each field buffer. You can find this out with:
  1826.    
  1827.  
  1828. char *field_buffer(FIELD *field,          /* field to query */
  1829.                    int bufindex);         /* number of buffer to query */
  1830.  
  1831.    Normally, the state of the zero-numbered buffer for each field is set
  1832.    by the user's editing actions on that field. It's sometimes useful to
  1833.    be able to set the value of the zero-numbered (or some other) buffer
  1834.    from your application:
  1835.    
  1836.  
  1837. int set_field_buffer(FIELD *field,        /* field to alter */
  1838.                    int bufindex,          /* number of buffer to alter */
  1839.                    char *value);          /* string value to set */
  1840.  
  1841.    If the field is not large enough and cannot be resized to a
  1842.    sufficiently large size to contain the specified value, the value will
  1843.    be truncated to fit.
  1844.    
  1845.    Calling field_buffer() with a null field pointer will raise an error.
  1846.    Calling field_buffer() on a field not currently selected for input
  1847.    will return a correct value. Calling field_buffer() on a field that is
  1848.    currently selected for input may not necessarily give a correct field
  1849.    buffer value, because entered data isn't necessarily copied to buffer
  1850.    zero before the exit validation check. To guarantee that the returned
  1851.    buffer value reflects on-screen reality, call field_buffer() either
  1852.    (1) in the field's exit validation check routine, (2) from the field's
  1853.    or form's initialization or termination hooks, or (3) just after a
  1854.    REQ_VALIDATION request has been processed by the forms driver.
  1855.    
  1856. Attributes of Forms
  1857.  
  1858.    As with field attributes, form attributes inherit a default from a
  1859.    system default form structure. These defaults can be queried or set by
  1860.    of these functions using a form-pointer argument of NULL.
  1861.    
  1862.    The most important attribute of a form is its field list. You can
  1863.    query and change this list with:
  1864.    
  1865.  
  1866. int set_form_fields(FORM *form,           /* form to alter */
  1867.                     FIELD **fields);      /* fields to connect */
  1868.  
  1869. char *form_fields(FORM *form);            /* fetch fields of form */
  1870.  
  1871. int field_count(FORM *form);              /* count connect fields */
  1872.  
  1873.    The second argument of set_form_fields() may be a NULL-terminated
  1874.    field pointer array like the one required by new_form(). In that case,
  1875.    the old fields of the form are disconnected but not freed (and
  1876.    eligible to be connected to other forms), then the new fields are
  1877.    connected.
  1878.    
  1879.    It may also be null, in which case the old fields are disconnected
  1880.    (and not freed) but no new ones are connected.
  1881.    
  1882.    The field_count() function simply counts the number of fields
  1883.    connected to a given from. It returns -1 if the form-pointer argument
  1884.    is NULL.
  1885.    
  1886. Control of Form Display
  1887.  
  1888.    In the overview section, you saw that to display a form you normally
  1889.    start by defining its size (and fields), posting it, and refreshing
  1890.    the screen. There is an hidden step before posting, which is the
  1891.    association of the form with a frame window (actually, a pair of
  1892.    windows) within which it will be displayed. By default, the forms
  1893.    library associates every form with the full-screen window stdscr.
  1894.    
  1895.    By making this step explicit, you can associate a form with a declared
  1896.    frame window on your screen display. This can be useful if you want to
  1897.    adapt the form display to different screen sizes, dynamically tile
  1898.    forms on the screen, or use a form as part of an interface layout
  1899.    managed by panels.
  1900.    
  1901.    The two windows associated with each form have the same functions as
  1902.    their analogues in the menu library. Both these windows are painted
  1903.    when the form is posted and erased when the form is unposted.
  1904.    
  1905.    The outer or frame window is not otherwise touched by the form
  1906.    routines. It exists so the programmer can associate a title, a border,
  1907.    or perhaps help text with the form and have it properly refreshed or
  1908.    erased at post/unpost time. The inner window or subwindow is where the
  1909.    current form page is actually displayed.
  1910.    
  1911.    In order to declare your own frame window for a form, you'll need to
  1912.    know the size of the form's bounding rectangle. You can get this
  1913.    information with:
  1914.    
  1915.  
  1916. int scale_form(FORM *form,                /* form to query */
  1917.                int *rows,                 /* form rows */
  1918.                int *cols);                /* form cols */
  1919.  
  1920.    The form dimensions are passed back in the locations pointed to by the
  1921.    arguments. Once you have this information, you can use it to declare
  1922.    of windows, then use one of these functions:
  1923.    
  1924.  
  1925. int set_form_win(FORM *form,              /* form to alter */
  1926.                  WINDOW *win);            /* frame window to connect */
  1927.  
  1928. WINDOW *form_win(FORM *form);             /* fetch frame window of form */
  1929.  
  1930. int set_form_sub(FORM *form,              /* form to alter */
  1931.                  WINDOW *win);            /* form subwindow to connect */
  1932.  
  1933. WINDOW *form_sub(FORM *form);             /* fetch form subwindow of form */
  1934.  
  1935.    Note that curses operations, including refresh(), on the form, should
  1936.    be done on the frame window, not the form subwindow.
  1937.    
  1938.    It is possible to check from your application whether all of a
  1939.    scrollable field is actually displayed within the menu subwindow. Use
  1940.    these functions:
  1941.    
  1942.  
  1943. int data_ahead(FORM *form);               /* form to be queried */
  1944.  
  1945. int data_behind(FORM *form);              /* form to be queried */
  1946.  
  1947.    The function data_ahead() returns TRUE if (a) the current field is
  1948.    one-line and has undisplayed data off to the right, (b) the current
  1949.    field is multi-line and there is data off-screen below it.
  1950.    
  1951.    The function data_behind() returns TRUE if the first (upper left hand)
  1952.    character position is off-screen (not being displayed).
  1953.    
  1954.    Finally, there is a function to restore the form window's cursor to
  1955.    the value expected by the forms driver:
  1956.    
  1957.  
  1958. int pos_form_cursor(FORM *)               /* form to be queried */
  1959.  
  1960.    If your application changes the form window cursor, call this function
  1961.    before handing control back to the forms driver in order to
  1962.    re-synchronize it.
  1963.    
  1964. Input Processing in the Forms Driver
  1965.  
  1966.    The function form_driver() handles virtualized input requests for form
  1967.    navigation, editing, and validation requests, just as menu_driver does
  1968.    for menus (see the section on menu input handling).
  1969.    
  1970.  
  1971. int form_driver(FORM *form,               /* form to pass input to */
  1972.                 int request);             /* form request code */
  1973.  
  1974.    Your input virtualization function needs to take input and then
  1975.    convert it to either an alphanumeric character (which is treated as
  1976.    data to be entered in the currently-selected field), or a forms
  1977.    processing request.
  1978.    
  1979.    The forms driver provides hooks (through input-validation and
  1980.    field-termination functions) with which your application code can
  1981.    check that the input taken by the driver matched what was expected.
  1982.    
  1983.   PAGE NAVIGATION REQUESTS
  1984.   
  1985.    These requests cause page-level moves through the form, triggering
  1986.    display of a new form screen.
  1987.    
  1988.    REQ_NEXT_PAGE
  1989.           Move to the next form page.
  1990.           
  1991.    REQ_PREV_PAGE
  1992.           Move to the previous form page.
  1993.           
  1994.    REQ_FIRST_PAGE
  1995.           Move to the first form page.
  1996.           
  1997.    REQ_LAST_PAGE
  1998.           Move to the last form page.
  1999.           
  2000.    These requests treat the list as cyclic; that is, REQ_NEXT_PAGE from
  2001.    the last page goes to the first, and REQ_PREV_PAGE from the first page
  2002.    goes to the last.
  2003.    
  2004.   INTER-FIELD NAVIGATION REQUESTS
  2005.   
  2006.    These requests handle navigation between fields on the same page.
  2007.    
  2008.    REQ_NEXT_FIELD
  2009.           Move to next field.
  2010.           
  2011.    REQ_PREV_FIELD
  2012.           Move to previous field.
  2013.           
  2014.    REQ_FIRST_FIELD
  2015.           Move to the first field.
  2016.           
  2017.    REQ_LAST_FIELD
  2018.           Move to the last field.
  2019.           
  2020.    REQ_SNEXT_FIELD
  2021.           Move to sorted next field.
  2022.           
  2023.    REQ_SPREV_FIELD
  2024.           Move to sorted previous field.
  2025.           
  2026.    REQ_SFIRST_FIELD
  2027.           Move to the sorted first field.
  2028.           
  2029.    REQ_SLAST_FIELD
  2030.           Move to the sorted last field.
  2031.           
  2032.    REQ_LEFT_FIELD
  2033.           Move left to field.
  2034.           
  2035.    REQ_RIGHT_FIELD
  2036.           Move right to field.
  2037.           
  2038.    REQ_UP_FIELD
  2039.           Move up to field.
  2040.           
  2041.    REQ_DOWN_FIELD
  2042.           Move down to field.
  2043.           
  2044.    These requests treat the list of fields on a page as cyclic; that is,
  2045.    REQ_NEXT_FIELD from the last field goes to the first, and
  2046.    REQ_PREV_FIELD from the first field goes to the last. The order of the
  2047.    fields for these (and the REQ_FIRST_FIELD and REQ_LAST_FIELD requests)
  2048.    is simply the order of the field pointers in the form array (as set up
  2049.    by new_form() or set_form_fields()
  2050.    
  2051.    It is also possible to traverse the fields as if they had been sorted
  2052.    in screen-position order, so the sequence goes left-to-right and
  2053.    top-to-bottom. To do this, use the second group of four
  2054.    sorted-movement requests.
  2055.    
  2056.    Finally, it is possible to move between fields using visual directions
  2057.    up, down, right, and left. To accomplish this, use the third group of
  2058.    four requests. Note, however, that the position of a form for purposes
  2059.    of these requests is its upper-left corner.
  2060.    
  2061.    For example, suppose you have a multi-line field B, and two
  2062.    single-line fields A and C on the same line with B, with A to the left
  2063.    of B and C to the right of B. A REQ_MOVE_RIGHT from A will go to B
  2064.    only if A, B, and C _all_ share the same first line; otherwise it will
  2065.    skip over B to C.
  2066.    
  2067.   INTRA-FIELD NAVIGATION REQUESTS
  2068.   
  2069.    These requests drive movement of the edit cursor within the currently
  2070.    selected field.
  2071.    
  2072.    REQ_NEXT_CHAR
  2073.           Move to next character.
  2074.           
  2075.    REQ_PREV_CHAR
  2076.           Move to previous character.
  2077.           
  2078.    REQ_NEXT_LINE
  2079.           Move to next line.
  2080.           
  2081.    REQ_PREV_LINE
  2082.           Move to previous line.
  2083.           
  2084.    REQ_NEXT_WORD
  2085.           Move to next word.
  2086.           
  2087.    REQ_PREV_WORD
  2088.           Move to previous word.
  2089.           
  2090.    REQ_BEG_FIELD
  2091.           Move to beginning of field.
  2092.           
  2093.    REQ_END_FIELD
  2094.           Move to end of field.
  2095.           
  2096.    REQ_BEG_LINE
  2097.           Move to beginning of line.
  2098.           
  2099.    REQ_END_LINE
  2100.           Move to end of line.
  2101.           
  2102.    REQ_LEFT_CHAR
  2103.           Move left in field.
  2104.           
  2105.    REQ_RIGHT_CHAR
  2106.           Move right in field.
  2107.           
  2108.    REQ_UP_CHAR
  2109.           Move up in field.
  2110.           
  2111.    REQ_DOWN_CHAR
  2112.           Move down in field.
  2113.           
  2114.    Each _word_ is separated from the previous and next characters by
  2115.    whitespace. The commands to move to beginning and end of line or field
  2116.    look for the first or last non-pad character in their ranges.
  2117.    
  2118.   SCROLLING REQUESTS
  2119.   
  2120.    Fields that are dynamic and have grown and fields explicitly created
  2121.    with offscreen rows are scrollable. One-line fields scroll
  2122.    horizontally; multi-line fields scroll vertically. Most scrolling is
  2123.    triggered by editing and intra-field movement (the library scrolls the
  2124.    field to keep the cursor visible). It is possible to explicitly
  2125.    request scrolling with the following requests:
  2126.    
  2127.    
  2128.    
  2129.    REQ_SCR_FLINE
  2130.           Scroll vertically forward a line.
  2131.           
  2132.    REQ_SCR_BLINE
  2133.           Scroll vertically backward a line.
  2134.           
  2135.    REQ_SCR_FPAGE
  2136.           Scroll vertically forward a page.
  2137.           
  2138.    REQ_SCR_BPAGE
  2139.           Scroll vertically backward a page.
  2140.           
  2141.    REQ_SCR_FHPAGE
  2142.           Scroll vertically forward half a page.
  2143.           
  2144.    REQ_SCR_BHPAGE
  2145.           Scroll vertically backward half a page.
  2146.           
  2147.    REQ_SCR_FCHAR
  2148.           Scroll horizontally forward a character.
  2149.           
  2150.    REQ_SCR_BCHAR
  2151.           Scroll horizontally backward a character.
  2152.           
  2153.    REQ_SCR_HFLINE
  2154.           Scroll horizontally one field width forward.
  2155.           
  2156.    REQ_SCR_HBLINE
  2157.           Scroll horizontally one field width backward.
  2158.           
  2159.    REQ_SCR_HFHALF
  2160.           Scroll horizontally one half field width forward.
  2161.           
  2162.    REQ_SCR_HBHALF
  2163.           Scroll horizontally one half field width backward.
  2164.           
  2165.    For scrolling purposes, a _page_ of a field is the height of its
  2166.    visible part.
  2167.    
  2168.   EDITING REQUESTS
  2169.   
  2170.    When you pass the forms driver an ASCII character, it is treated as a
  2171.    request to add the character to the field's data buffer. Whether this
  2172.    is an insertion or a replacement depends on the field's edit mode
  2173.    (insertion is the default.
  2174.    
  2175.    The following requests support editing the field and changing the edit
  2176.    mode:
  2177.    
  2178.    REQ_INS_MODE
  2179.           Set insertion mode.
  2180.           
  2181.    REQ_OVL_MODE
  2182.           Set overlay mode.
  2183.           
  2184.    REQ_NEW_LINE
  2185.           New line request (see below for explanation).
  2186.           
  2187.    REQ_INS_CHAR
  2188.           Insert space at character location.
  2189.           
  2190.    REQ_INS_LINE
  2191.           Insert blank line at character location.
  2192.           
  2193.    REQ_DEL_CHAR
  2194.           Delete character at cursor.
  2195.           
  2196.    REQ_DEL_PREV
  2197.           Delete previous word at cursor.
  2198.           
  2199.    REQ_DEL_LINE
  2200.           Delete line at cursor.
  2201.           
  2202.    REQ_DEL_WORD
  2203.           Delete word at cursor.
  2204.           
  2205.    REQ_CLR_EOL
  2206.           Clear to end of line.
  2207.           
  2208.    REQ_CLR_EOF
  2209.           Clear to end of field.
  2210.           
  2211.    REQ_CLEAR_FIELD
  2212.           Clear entire field.
  2213.           
  2214.    The behavior of the REQ_NEW_LINE and REQ_DEL_PREV requests is
  2215.    complicated and partly controlled by a pair of forms options. The
  2216.    special cases are triggered when the cursor is at the beginning of a
  2217.    field, or on the last line of the field.
  2218.    
  2219.    First, we consider REQ_NEW_LINE:
  2220.    
  2221.    The normal behavior of REQ_NEW_LINE in insert mode is to break the
  2222.    current line at the position of the edit cursor, inserting the portion
  2223.    of the current line after the cursor as a new line following the
  2224.    current and moving the cursor to the beginning of that new line (you
  2225.    may think of this as inserting a newline in the field buffer).
  2226.    
  2227.    The normal behavior of REQ_NEW_LINE in overlay mode is to clear the
  2228.    current line from the position of the edit cursor to end of line. The
  2229.    cursor is then moved to the beginning of the next line.
  2230.    
  2231.    However, REQ_NEW_LINE at the beginning of a field, or on the last line
  2232.    of a field, instead does a REQ_NEXT_FIELD. O_NL_OVERLOAD option is
  2233.    off, this special action is disabled.
  2234.    
  2235.    Now, let us consider REQ_DEL_PREV:
  2236.    
  2237.    The normal behavior of REQ_DEL_PREV is to delete the previous
  2238.    character. If insert mode is on, and the cursor is at the start of a
  2239.    line, and the text on that line will fit on the previous one, it
  2240.    instead appends the contents of the current line to the previous one
  2241.    and deletes the current line (you may think of this as deleting a
  2242.    newline from the field buffer).
  2243.    
  2244.    However, REQ_DEL_PREV at the beginning of a field is instead treated
  2245.    as a REQ_PREV_FIELD.
  2246.    
  2247.    If the O_BS_OVERLOAD option is off, this special action is disabled
  2248.    and the forms driver just returns E_REQUEST_DENIED.
  2249.    
  2250.    See Form Options for discussion of how to set and clear the overload
  2251.    options.
  2252.    
  2253.   ORDER REQUESTS
  2254.   
  2255.    If the type of your field is ordered, and has associated functions for
  2256.    getting the next and previous values of the type from a given value,
  2257.    there are requests that can fetch that value into the field buffer:
  2258.    
  2259.    REQ_NEXT_CHOICE
  2260.           Place the successor value of the current value in the buffer.
  2261.           
  2262.    REQ_PREV_CHOICE
  2263.           Place the predecessor value of the current value in the buffer.
  2264.           
  2265.    Of the built-in field types, only TYPE_ENUM has built-in successor and
  2266.    predecessor functions. When you define a field type of your own (see
  2267.    Custom Validation Types), you can associate our own ordering
  2268.    functions.
  2269.    
  2270.   APPLICATION COMMANDS
  2271.   
  2272.    Form requests are represented as integers above the curses value
  2273.    greater than KEY_MAX and less than or equal to the constant
  2274.    MAX_COMMAND. If your input-virtualization routine returns a value
  2275.    above MAX_COMMAND, the forms driver will ignore it.
  2276.    
  2277. Field Change Hooks
  2278.  
  2279.    It is possible to set function hooks to be executed whenever the
  2280.    current field or form changes. Here are the functions that support
  2281.    this:
  2282.    
  2283.  
  2284. typedef void    (*HOOK)();       /* pointer to function returning void */
  2285.  
  2286. int set_form_init(FORM *form,    /* form to alter */
  2287.                   HOOK hook);    /* initialization hook */
  2288.  
  2289. HOOK form_init(FORM *form);      /* form to query */
  2290.  
  2291. int set_form_term(FORM *form,    /* form to alter */
  2292.                   HOOK hook);    /* termination hook */
  2293.  
  2294. HOOK form_term(FORM *form);      /* form to query */
  2295.  
  2296. int set_field_init(FORM *form,   /* form to alter */
  2297.                   HOOK hook);    /* initialization hook */
  2298.  
  2299. HOOK field_init(FORM *form);     /* form to query */
  2300.  
  2301. int set_field_term(FORM *form,   /* form to alter */
  2302.                   HOOK hook);    /* termination hook */
  2303.  
  2304. HOOK field_term(FORM *form);     /* form to query */
  2305.  
  2306.    These functions allow you to either set or query four different hooks.
  2307.    In each of the set functions, the second argument should be the
  2308.    address of a hook function. These functions differ only in the timing
  2309.    of the hook call.
  2310.    
  2311.    form_init
  2312.           This hook is called when the form is posted; also, just after
  2313.           each page change operation.
  2314.           
  2315.    field_init
  2316.           This hook is called when the form is posted; also, just after
  2317.           each field change
  2318.           
  2319.    field_term
  2320.           This hook is called just after field validation; that is, just
  2321.           before the field is altered. It is also called when the form is
  2322.           unposted.
  2323.           
  2324.    form_term
  2325.           This hook is called when the form is unposted; also, just
  2326.           before each page change operation.
  2327.           
  2328.    Calls to these hooks may be triggered
  2329.     1. When user editing requests are processed by the forms driver
  2330.     2. When the current page is changed by set_current_field() call
  2331.     3. When the current field is changed by a set_form_page() call
  2332.        
  2333.    See Field Change Commands for discussion of the latter two cases.
  2334.    
  2335.    You can set a default hook for all fields by passing one of the set
  2336.    functions a NULL first argument.
  2337.    
  2338.    You can disable any of these hooks by (re)setting them to NULL, the
  2339.    default value.
  2340.    
  2341. Field Change Commands
  2342.  
  2343.    Normally, navigation through the form will be driven by the user's
  2344.    input requests. But sometimes it is useful to be able to move the
  2345.    focus for editing and viewing under control of your application, or
  2346.    ask which field it currently is in. The following functions help you
  2347.    accomplish this:
  2348.    
  2349.  
  2350. int set_current_field(FORM *form,         /* form to alter */
  2351.                       FIELD *field);      /* field to shift to */
  2352.  
  2353. FIELD *current_field(FORM *form);         /* form to query */
  2354.  
  2355. int field_index(FORM *form,               /* form to query */
  2356.                 FIELD *field);            /* field to get index of */
  2357.  
  2358.    The function field_index() returns the index of the given field in the
  2359.    given form's field array (the array passed to new_form() or
  2360.    set_form_fields()).
  2361.    
  2362.    The initial current field of a form is the first active field on the
  2363.    first page. The function set_form_fields() resets this.
  2364.    
  2365.    It is also possible to move around by pages.
  2366.    
  2367.  
  2368. int set_form_page(FORM *form,             /* form to alter */
  2369.                   int page);              /* page to go to (0-origin) */
  2370.  
  2371. int form_page(FORM *form);                /* return form's current page */
  2372.  
  2373.    The initial page of a newly-created form is 0. The function
  2374.    set_form_fields() resets this.
  2375.    
  2376. Form Options
  2377.  
  2378.    Like fields, forms may have control option bits. They can be changed
  2379.    or queried with these functions:
  2380.    
  2381.  
  2382.  
  2383. int set_form_opts(FORM *form,             /* form to alter */
  2384.                   int attr);              /* attribute to set */
  2385.  
  2386. int form_opts_on(FORM *form,              /* form to alter */
  2387.                  int attr);               /* attributes to turn on */
  2388.  
  2389. int form_opts_off(FORM *form,             /* form to alter */
  2390.                   int attr);              /* attributes to turn off */
  2391.  
  2392. int form_opts(FORM *form);                /* form to query */
  2393.  
  2394.  
  2395. By default, all options are on.  Here are the available option bits:
  2396.  
  2397.    O_NL_OVERLOAD
  2398.           Enable overloading of REQ_NEW_LINE as described in Editing
  2399.           Requests. The value of this option is ignored on dynamic fields
  2400.           that have not reached their size limit; these have no last
  2401.           line, so the circumstances for triggering a REQ_NEXT_FIELD
  2402.           never arise.
  2403.           
  2404.    O_BS_OVERLOAD
  2405.           Enable overloading of REQ_DEL_PREV as described in Editing
  2406.           Requests.
  2407.           
  2408.    The option values are bit-masks and can be composed with logical-or in
  2409.    the obvious way.
  2410.    
  2411. Custom Validation Types
  2412.  
  2413.    The form library gives you the capability to define custom validation
  2414.    types of your own. Further, the optional additional arguments of
  2415.    set_field_type effectively allow you to parameterize validation types.
  2416.    Most of the complications in the validation-type interface have to do
  2417.    with the handling of the additional arguments within custom validation
  2418.    functions.
  2419.    
  2420.   UNION TYPES
  2421.   
  2422.    The simplest way to create a custom data type is to compose it from
  2423.    two preexisting ones:
  2424.    
  2425.  
  2426. FIELD *link_fieldtype(FIELDTYPE *type1,
  2427.                       FIELDTYPE *type2);
  2428.  
  2429.    This function creates a field type that will accept any of the values
  2430.    legal for either of its argument field types (which may be either
  2431.    predefined or programmer-defined). If a set_field_type() call later
  2432.    requires arguments, the new composite type expects all arguments for
  2433.    the first type, than all arguments for the second. Order functions
  2434.    (see Order Requests) associated with the component types will work on
  2435.    the composite; what it does is check the validation function for the
  2436.    first type, then for the second, to figure what type the buffer
  2437.    contents should be treated as.
  2438.    
  2439.   NEW FIELD TYPES
  2440.   
  2441.    To create a field type from scratch, you need to specify one or both
  2442.    of the following things:
  2443.    
  2444.      * A character-validation function, to check each character as it is
  2445.        entered.
  2446.      * A field-validation function to be applied on exit from the field.
  2447.        
  2448.    Here's how you do that:
  2449.    
  2450.    typedef int (*HOOK)(); /* pointer to function returning int */
  2451.    
  2452.  
  2453. FIELDTYPE *new_fieldtype(HOOK f_validate, /* field validator */
  2454.                          HOOK c_validate) /* character validator */
  2455.  
  2456.  
  2457. int free_fieldtype(FIELDTYPE *ftype);     /* type to free */
  2458.  
  2459.    At least one of the arguments of new_fieldtype()must be non-NULL. The
  2460.    forms driver will automatically call the new type's validation
  2461.    functions at appropriate points in processing a field of the new type.
  2462.    
  2463.    
  2464.    The function free_fieldtype() deallocates the argument fieldtype,
  2465.    freeing all storage associated with it.
  2466.    
  2467.    Normally, a field validator is called when the user attempts to leave
  2468.    the field. Its first argument is a field fointer, from which it can
  2469.    get to field buffer 0 and test it. If the function returns TRUE, the
  2470.    operation succeeds; if it returns FALSE, the edit cursor stays in the
  2471.    field.
  2472.    
  2473.    A character validator gets the character passed in as a first
  2474.    argument. It too should return TRUE if the character is valid, FALSE
  2475.    otherwise.
  2476.    
  2477.   VALIDATION FUNCTION ARGUMENTS
  2478.   
  2479.    Your field- and character- validation functions will be passed a
  2480.    second argument as well. This second argument is the address of a
  2481.    structure (which we'll call a _pile_) built from any of the
  2482.    field-type-specific arguments passed to set_field_type(). If no such
  2483.    arguments are defined for the field type, this pile pointer argument
  2484.    will be NULL.
  2485.    
  2486.    In order to arrange for such arguments to be passed to your validation
  2487.    functions, you must associate a small set of storage-management
  2488.    functions with the type. The forms driver will use these to synthesize
  2489.    a pile from the trailing arguments of each set_field_type() argument,
  2490.    and a pointer to the pile will be passed to the validation functions.
  2491.    
  2492.    Here is how you make the association:
  2493.    
  2494.  
  2495. typedef char    *(*PTRHOOK)();    /* pointer to function returning (char *) */
  2496. typedef void    (*VOIDHOOK)();    /* pointer to function returning void */
  2497.  
  2498. int set_fieldtype_arg(FIELDTYPE *type,    /* type to alter */
  2499.                       PTRHOOK make_str,   /* make structure from args */
  2500.                       PTRHOOK copy_str,   /* make copy of structure */
  2501.                       VOIDHOOK free_str); /* free structure storage */
  2502.  
  2503.    Here is how the storage-management hooks are used:
  2504.    
  2505.    make_str
  2506.           This function is called by set_field_type(). It gets one
  2507.           argument, a va_list of the type-specific arguments passed to
  2508.           set_field_type(). It is expected to return a pile pointer to a
  2509.           data structure that encapsulates those arguments.
  2510.           
  2511.    copy_str
  2512.           This function is called by form library functions that allocate
  2513.           new field instances. It is expected to take a pile pointer,
  2514.           copy the pile to allocated storage, and return the address of
  2515.           the pile copy.
  2516.           
  2517.    free_str
  2518.           This function is called by field- and type-deallocation
  2519.           routines in the library. It takes a pile pointer argument, and
  2520.           is expected to free the storage of that pile.
  2521.           
  2522.    The make_str and copy_str functions may return NULL to signal
  2523.    allocation failure. The library routines will that call them will
  2524.    return error indication when this happens. Thus, your validation
  2525.    functions should never see a NULL pile pointer and need not check
  2526.    specially for it.
  2527.    
  2528.   ORDER FUNCTIONS FOR CUSTOM TYPES
  2529.   
  2530.    Some custom field types are simply ordered in the same well-defined
  2531.    way that TYPE_ENUM is. For such types, it is possible to define
  2532.    successor and predecessor functions to support the REQ_NEXT_CHOICE and
  2533.    REQ_PREV_CHOICE requests. Here's how:
  2534.    
  2535.  
  2536. typedef int     (*INTHOOK)();     /* pointer to function returning int */
  2537.  
  2538. int set_fieldtype_arg(FIELDTYPE *type,    /* type to alter */
  2539.                       INTHOOK succ,       /* get successor value */
  2540.                       INTHOOK pred);      /* get predecessor value */
  2541.  
  2542.    The successor and predecessor arguments will each be passed two
  2543.    arguments; a field pointer, and a pile pointer (as for the validation
  2544.    functions). They are expected to use the function field_buffer() to
  2545.    read the current value, and set_field_buffer() on buffer 0 to set the
  2546.    next or previous value. Either hook may return TRUE to indicate
  2547.    success (a legal next or previous value was set) or FALSE to indicate
  2548.    failure.
  2549.    
  2550.   AVOIDING PROBLEMS
  2551.   
  2552.    The interface for defining custom types is complicated and tricky.
  2553.    Rather than attempting to create a custom type entirely from scratch,
  2554.    you should start by studying the library source code for whichever of
  2555.    the pre-defined types seems to be closest to what you want.
  2556.    
  2557.    Use that code as a model, and evolve it towards what you really want.
  2558.    You will avoid many problems and annoyances that way. The code in the
  2559.    ncurses library has been specifically un-copyrighted to support this.
  2560.    
  2561.    If your custom type defines order functions, have do something
  2562.    intuitive with a blank field. A useful convention is to make the
  2563.    successor of a blank field the types minimum value, and its
  2564.    predecessor the maximum.
  2565.    
  2566.    
  2567.      _________________________________________________________________
  2568.    
  2569.    
  2570.    
  2571.    THIS DOCUMENT IS STILL UNDER CONSTRUCTION. Full descriptions of the
  2572.    form library entry points are available in the form_*.3x manual pages
  2573.    included with the ncurses distributions.
  2574.    
  2575.    
  2576.      _________________________________________________________________
  2577.    
  2578.     Eric S. Raymond <esr@snark.thyrsus.com>
  2579.