xref: /freebsd/contrib/ncurses/doc/ncurses-intro.doc (revision 4a1a95108dd76c4259fe6c37c4471f7969b17983)
17a69bbfbSPeter Wemm                         Writing Programs with NCURSES
27a69bbfbSPeter Wemm
37a69bbfbSPeter Wemm     by Eric S. Raymond and Zeyd M. Ben-Halim
47a69bbfbSPeter Wemm     updates since release 1.9.9e by Thomas Dickey
57a69bbfbSPeter Wemm
67a69bbfbSPeter Wemm                                   Contents
77a69bbfbSPeter Wemm
87a69bbfbSPeter Wemm     * Introduction
97a69bbfbSPeter Wemm          + A Brief History of Curses
107a69bbfbSPeter Wemm          + Scope of This Document
117a69bbfbSPeter Wemm          + Terminology
127a69bbfbSPeter Wemm     * The Curses Library
137a69bbfbSPeter Wemm          + An Overview of Curses
147a69bbfbSPeter Wemm               o Compiling Programs using Curses
157a69bbfbSPeter Wemm               o Updating the Screen
167a69bbfbSPeter Wemm               o Standard Windows and Function Naming Conventions
177a69bbfbSPeter Wemm               o Variables
187a69bbfbSPeter Wemm          + Using the Library
197a69bbfbSPeter Wemm               o Starting up
207a69bbfbSPeter Wemm               o Output
217a69bbfbSPeter Wemm               o Input
227a69bbfbSPeter Wemm               o Using Forms Characters
237a69bbfbSPeter Wemm               o Character Attributes and Color
247a69bbfbSPeter Wemm               o Mouse Interfacing
257a69bbfbSPeter Wemm               o Finishing Up
267a69bbfbSPeter Wemm          + Function Descriptions
277a69bbfbSPeter Wemm               o Initialization and Wrapup
287a69bbfbSPeter Wemm               o Causing Output to the Terminal
297a69bbfbSPeter Wemm               o Low-Level Capability Access
307a69bbfbSPeter Wemm               o Debugging
317a69bbfbSPeter Wemm          + Hints, Tips, and Tricks
327a69bbfbSPeter Wemm               o Some Notes of Caution
337a69bbfbSPeter Wemm               o Temporarily Leaving ncurses Mode
347a69bbfbSPeter Wemm               o Using ncurses under xterm
357a69bbfbSPeter Wemm               o Handling Multiple Terminal Screens
367a69bbfbSPeter Wemm               o Testing for Terminal Capabilities
377a69bbfbSPeter Wemm               o Tuning for Speed
387a69bbfbSPeter Wemm               o Special Features of ncurses
397a69bbfbSPeter Wemm          + Compatibility with Older Versions
407a69bbfbSPeter Wemm               o Refresh of Overlapping Windows
417a69bbfbSPeter Wemm               o Background Erase
427a69bbfbSPeter Wemm          + XSI Curses Conformance
437a69bbfbSPeter Wemm     * The Panels Library
447a69bbfbSPeter Wemm          + Compiling With the Panels Library
457a69bbfbSPeter Wemm          + Overview of Panels
467a69bbfbSPeter Wemm          + Panels, Input, and the Standard Screen
477a69bbfbSPeter Wemm          + Hiding Panels
487a69bbfbSPeter Wemm          + Miscellaneous Other Facilities
497a69bbfbSPeter Wemm     * The Menu Library
507a69bbfbSPeter Wemm          + Compiling with the menu Library
517a69bbfbSPeter Wemm          + Overview of Menus
527a69bbfbSPeter Wemm          + Selecting items
537a69bbfbSPeter Wemm          + Menu Display
547a69bbfbSPeter Wemm          + Menu Windows
557a69bbfbSPeter Wemm          + Processing Menu Input
567a69bbfbSPeter Wemm          + Miscellaneous Other Features
577a69bbfbSPeter Wemm     * The Forms Library
587a69bbfbSPeter Wemm          + Compiling with the forms Library
597a69bbfbSPeter Wemm          + Overview of Forms
607a69bbfbSPeter Wemm          + Creating and Freeing Fields and Forms
617a69bbfbSPeter Wemm          + Fetching and Changing Field Attributes
627a69bbfbSPeter Wemm               o Fetching Size and Location Data
637a69bbfbSPeter Wemm               o Changing the Field Location
647a69bbfbSPeter Wemm               o The Justification Attribute
657a69bbfbSPeter Wemm               o Field Display Attributes
667a69bbfbSPeter Wemm               o Field Option Bits
677a69bbfbSPeter Wemm               o Field Status
687a69bbfbSPeter Wemm               o Field User Pointer
697a69bbfbSPeter Wemm          + Variable-Sized Fields
707a69bbfbSPeter Wemm          + Field Validation
717a69bbfbSPeter Wemm               o TYPE_ALPHA
727a69bbfbSPeter Wemm               o TYPE_ALNUM
737a69bbfbSPeter Wemm               o TYPE_ENUM
747a69bbfbSPeter Wemm               o TYPE_INTEGER
757a69bbfbSPeter Wemm               o TYPE_NUMERIC
767a69bbfbSPeter Wemm               o TYPE_REGEXP
777a69bbfbSPeter Wemm          + Direct Field Buffer Manipulation
787a69bbfbSPeter Wemm          + Attributes of Forms
797a69bbfbSPeter Wemm          + Control of Form Display
807a69bbfbSPeter Wemm          + Input Processing in the Forms Driver
817a69bbfbSPeter Wemm               o Page Navigation Requests
827a69bbfbSPeter Wemm               o Inter-Field Navigation Requests
837a69bbfbSPeter Wemm               o Intra-Field Navigation Requests
847a69bbfbSPeter Wemm               o Scrolling Requests
857a69bbfbSPeter Wemm               o Field Editing Requests
867a69bbfbSPeter Wemm               o Order Requests
877a69bbfbSPeter Wemm               o Application Commands
887a69bbfbSPeter Wemm          + Field Change Hooks
897a69bbfbSPeter Wemm          + Field Change Commands
907a69bbfbSPeter Wemm          + Form Options
917a69bbfbSPeter Wemm          + Custom Validation Types
927a69bbfbSPeter Wemm               o Union Types
937a69bbfbSPeter Wemm               o New Field Types
947a69bbfbSPeter Wemm               o Validation Function Arguments
957a69bbfbSPeter Wemm               o Order Functions For Custom Types
967a69bbfbSPeter Wemm               o Avoiding Problems
977a69bbfbSPeter Wemm     _________________________________________________________________
987a69bbfbSPeter Wemm
997a69bbfbSPeter Wemm                                 Introduction
1007a69bbfbSPeter Wemm
1017a69bbfbSPeter Wemm   This document is an introduction to programming with curses. It is not
1027a69bbfbSPeter Wemm   an   exhaustive  reference  for  the  curses  Application  Programming
1037a69bbfbSPeter Wemm   Interface  (API);  that  role  is  filled  by the curses manual pages.
1047a69bbfbSPeter Wemm   Rather,  it  is  intended  to  help  C programmers ease into using the
1057a69bbfbSPeter Wemm   package.
1067a69bbfbSPeter Wemm
1077a69bbfbSPeter Wemm   This   document  is  aimed  at  C  applications  programmers  not  yet
1087a69bbfbSPeter Wemm   specifically  familiar with ncurses. If you are already an experienced
1097a69bbfbSPeter Wemm   curses  programmer, you should nevertheless read the sections on Mouse
1107a69bbfbSPeter Wemm   Interfacing,  Debugging, Compatibility with Older Versions, and Hints,
1117a69bbfbSPeter Wemm   Tips,  and  Tricks.  These  will  bring you up to speed on the special
1127a69bbfbSPeter Wemm   features  and  quirks of the ncurses implementation. If you are not so
1137a69bbfbSPeter Wemm   experienced, keep reading.
1147a69bbfbSPeter Wemm
1157a69bbfbSPeter Wemm   The  curses  package  is a subroutine library for terminal-independent
1167a69bbfbSPeter Wemm   screen-painting  and  input-event handling which presents a high level
1177a69bbfbSPeter Wemm   screen  model  to  the programmer, hiding differences between terminal
1187a69bbfbSPeter Wemm   types  and doing automatic optimization of output to change one screen
1197a69bbfbSPeter Wemm   full  of  text into another. Curses uses terminfo, which is a database
1207a69bbfbSPeter Wemm   format  that  can  describe the capabilities of thousands of different
1217a69bbfbSPeter Wemm   terminals.
1227a69bbfbSPeter Wemm
1237a69bbfbSPeter Wemm   The  curses  API  may  seem  something of an archaism on UNIX desktops
1247a69bbfbSPeter Wemm   increasingly  dominated  by  X,  Motif, and Tcl/Tk. Nevertheless, UNIX
1257a69bbfbSPeter Wemm   still  supports  tty lines and X supports xterm(1); the curses API has
1267a69bbfbSPeter Wemm   the advantage of (a) back-portability to character-cell terminals, and
1277a69bbfbSPeter Wemm   (b)  simplicity.  For  an application that does not require bit-mapped
1287a69bbfbSPeter Wemm   graphics  and multiple fonts, an interface implementation using curses
1297a69bbfbSPeter Wemm   will  typically  be  a  great deal simpler and less expensive than one
1307a69bbfbSPeter Wemm   using an X toolkit.
1317a69bbfbSPeter Wemm
1327a69bbfbSPeter WemmA Brief History of Curses
1337a69bbfbSPeter Wemm
1347a69bbfbSPeter Wemm   Historically, the first ancestor of curses was the routines written to
1357a69bbfbSPeter Wemm   provide   screen-handling   for   the   game  rogue;  these  used  the
1367a69bbfbSPeter Wemm   already-existing  termcap  database  facility  for describing terminal
1377a69bbfbSPeter Wemm   capabilities. These routines were abstracted into a documented library
1387a69bbfbSPeter Wemm   and first released with the early BSD UNIX versions.
1397a69bbfbSPeter Wemm
1407a69bbfbSPeter Wemm   System  III UNIX from Bell Labs featured a rewritten and much-improved
1417a69bbfbSPeter Wemm   curses  library.  It introduced the terminfo format. Terminfo is based
1427a69bbfbSPeter Wemm   on  Berkeley's termcap database, but contains a number of improvements
1437a69bbfbSPeter Wemm   and  extensions.  Parameterized  capabilities strings were introduced,
1447a69bbfbSPeter Wemm   making  it  possible to describe multiple video attributes, and colors
1457a69bbfbSPeter Wemm   and  to  handle far more unusual terminals than possible with termcap.
1467a69bbfbSPeter Wemm   In  the  later  AT&T  System  V  releases,  curses evolved to use more
1477a69bbfbSPeter Wemm   facilities and offer more capabilities, going far beyond BSD curses in
1487a69bbfbSPeter Wemm   power and flexibility.
1497a69bbfbSPeter Wemm
1507a69bbfbSPeter WemmScope of This Document
1517a69bbfbSPeter Wemm
1527a69bbfbSPeter Wemm   This document describes ncurses, a free implementation of the System V
1537a69bbfbSPeter Wemm   curses  API  with  some  clearly  marked  extensions.  It includes the
1547a69bbfbSPeter Wemm   following System V curses features:
1557a69bbfbSPeter Wemm     * Support  for  multiple  screen  highlights  (BSD curses could only
1567a69bbfbSPeter Wemm       handle one `standout' highlight, usually reverse-video).
1577a69bbfbSPeter Wemm     * Support for line- and box-drawing using forms characters.
1587a69bbfbSPeter Wemm     * Recognition of function keys on input.
1597a69bbfbSPeter Wemm     * Color support.
1607a69bbfbSPeter Wemm     * Support  for pads (windows of larger than screen size on which the
1617a69bbfbSPeter Wemm       screen or a subwindow defines a viewport).
1627a69bbfbSPeter Wemm
1637a69bbfbSPeter Wemm   Also,  this  package  makes  use  of  the  insert  and delete line and
1647a69bbfbSPeter Wemm   character  features  of  terminals  so equipped, and determines how to
1657a69bbfbSPeter Wemm   optimally  use  these  features  with  no help from the programmer. It
1667a69bbfbSPeter Wemm   allows  arbitrary  combinations  of  video attributes to be displayed,
1677a69bbfbSPeter Wemm   even  on  terminals that leave ``magic cookies'' on the screen to mark
1687a69bbfbSPeter Wemm   changes in attributes.
1697a69bbfbSPeter Wemm
1707a69bbfbSPeter Wemm   The  ncurses  package  can  also  capture and use event reports from a
1717a69bbfbSPeter Wemm   mouse in some environments (notably, xterm under the X window system).
1727a69bbfbSPeter Wemm   This document includes tips for using the mouse.
1737a69bbfbSPeter Wemm
1747a69bbfbSPeter Wemm   The  ncurses  package  was  originated  by  Pavel Curtis. The original
1757a69bbfbSPeter Wemm   maintainer  of  this  package is Zeyd Ben-Halim <zmbenhal@netcom.com>.
1767a69bbfbSPeter Wemm   Eric S. Raymond <esr@snark.thyrsus.com> wrote many of the new features
1777a69bbfbSPeter Wemm   in  versions  after  1.8.1 and wrote most of this introduction. J�rgen
1787a69bbfbSPeter Wemm   Pfeifer  wrote  all  of  the  menu and forms code as well as the Ada95
1794a1a9510SRong-En Fan   binding.  Ongoing  work  is  being done by Thomas Dickey (maintainer).
1804a1a9510SRong-En Fan   Contact the current maintainers at bug-ncurses@gnu.org.
1817a69bbfbSPeter Wemm
1827a69bbfbSPeter Wemm   This  document  also describes the panels extension library, similarly
1837a69bbfbSPeter Wemm   modeled  on  the  SVr4  panels  facility.  This  library allows you to
1847a69bbfbSPeter Wemm   associate  backing  store  with each of a stack or deck of overlapping
1857a69bbfbSPeter Wemm   windows,  and  provides  operations  for  moving windows around in the
1867a69bbfbSPeter Wemm   stack that change their visibility in the natural way (handling window
1877a69bbfbSPeter Wemm   overlaps).
1887a69bbfbSPeter Wemm
1897a69bbfbSPeter Wemm   Finally,  this  document  describes  in  detail  the  menus  and forms
1907a69bbfbSPeter Wemm   extension  libraries,  also  cloned  from System V, which support easy
1917a69bbfbSPeter Wemm   construction and sequences of menus and fill-in forms.
1927a69bbfbSPeter Wemm
1937a69bbfbSPeter WemmTerminology
1947a69bbfbSPeter Wemm
1957a69bbfbSPeter Wemm   In  this  document,  the following terminology is used with reasonable
1967a69bbfbSPeter Wemm   consistency:
1977a69bbfbSPeter Wemm
1987a69bbfbSPeter Wemm   window
1997a69bbfbSPeter Wemm          A  data  structure  describing  a  sub-rectangle  of the screen
2007a69bbfbSPeter Wemm          (possibly  the  entire  screen).  You  can write to a window as
2017a69bbfbSPeter Wemm          though  it  were a miniature screen, scrolling independently of
2027a69bbfbSPeter Wemm          other windows on the physical screen.
2037a69bbfbSPeter Wemm
2047a69bbfbSPeter Wemm   screens
2057a69bbfbSPeter Wemm          A  subset of windows which are as large as the terminal screen,
2067a69bbfbSPeter Wemm          i.e.,  they  start  at the upper left hand corner and encompass
2077a69bbfbSPeter Wemm          the   lower  right  hand  corner.  One  of  these,  stdscr,  is
2087a69bbfbSPeter Wemm          automatically provided for the programmer.
2097a69bbfbSPeter Wemm
2107a69bbfbSPeter Wemm   terminal screen
2117a69bbfbSPeter Wemm          The package's idea of what the terminal display currently looks
2127a69bbfbSPeter Wemm          like, i.e., what the user sees now. This is a special screen.
2137a69bbfbSPeter Wemm
2147a69bbfbSPeter Wemm                              The Curses Library
2157a69bbfbSPeter Wemm
2167a69bbfbSPeter WemmAn Overview of Curses
2177a69bbfbSPeter Wemm
2187a69bbfbSPeter Wemm  Compiling Programs using Curses
2197a69bbfbSPeter Wemm
2207a69bbfbSPeter Wemm   In order to use the library, it is necessary to have certain types and
2217a69bbfbSPeter Wemm   variables defined. Therefore, the programmer must have a line:
2227a69bbfbSPeter Wemm          #include <curses.h>
2237a69bbfbSPeter Wemm
2247a69bbfbSPeter Wemm   at the top of the program source. The screen package uses the Standard
2257a69bbfbSPeter Wemm   I/O   library,  so  <curses.h>  includes  <stdio.h>.  <curses.h>  also
2267a69bbfbSPeter Wemm   includes  <termios.h>,  <termio.h>,  or  <sgtty.h>  depending  on your
2277a69bbfbSPeter Wemm   system.  It is redundant (but harmless) for the programmer to do these
2287a69bbfbSPeter Wemm   includes,  too.  In  linking with curses you need to have -lncurses in
2297a69bbfbSPeter Wemm   your  LDFLAGS  or  on the command line. There is no need for any other
2307a69bbfbSPeter Wemm   libraries.
2317a69bbfbSPeter Wemm
2327a69bbfbSPeter Wemm  Updating the Screen
2337a69bbfbSPeter Wemm
2347a69bbfbSPeter Wemm   In  order  to  update  the  screen  optimally, it is necessary for the
2357a69bbfbSPeter Wemm   routines  to  know  what  the screen currently looks like and what the
2367a69bbfbSPeter Wemm   programmer  wants  it to look like next. For this purpose, a data type
2377a69bbfbSPeter Wemm   (structure)  named WINDOW is defined which describes a window image to
2387a69bbfbSPeter Wemm   the  routines,  including its starting position on the screen (the (y,
2397a69bbfbSPeter Wemm   x)  coordinates  of  the  upper left hand corner) and its size. One of
2407a69bbfbSPeter Wemm   these  (called  curscr,  for current screen) is a screen image of what
2417a69bbfbSPeter Wemm   the  terminal currently looks like. Another screen (called stdscr, for
2427a69bbfbSPeter Wemm   standard screen) is provided by default to make changes on.
2437a69bbfbSPeter Wemm
2447a69bbfbSPeter Wemm   A  window is a purely internal representation. It is used to build and
2457a69bbfbSPeter Wemm   store  a potential image of a portion of the terminal. It doesn't bear
2467a69bbfbSPeter Wemm   any  necessary relation to what is really on the terminal screen; it's
2477a69bbfbSPeter Wemm   more like a scratchpad or write buffer.
2487a69bbfbSPeter Wemm
2497a69bbfbSPeter Wemm   To  make  the  section  of  physical  screen corresponding to a window
2507a69bbfbSPeter Wemm   reflect  the  contents  of the window structure, the routine refresh()
2517a69bbfbSPeter Wemm   (or wrefresh() if the window is not stdscr) is called.
2527a69bbfbSPeter Wemm
2537a69bbfbSPeter Wemm   A  given physical screen section may be within the scope of any number
2547a69bbfbSPeter Wemm   of  overlapping  windows.  Also, changes can be made to windows in any
2557a69bbfbSPeter Wemm   order,  without  regard  to  motion  efficiency.  Then,  at  will, the
2567a69bbfbSPeter Wemm   programmer can effectively say ``make it look like this,'' and let the
2577a69bbfbSPeter Wemm   package implementation determine the most efficient way to repaint the
2587a69bbfbSPeter Wemm   screen.
2597a69bbfbSPeter Wemm
2607a69bbfbSPeter Wemm  Standard Windows and Function Naming Conventions
2617a69bbfbSPeter Wemm
2627a69bbfbSPeter Wemm   As  hinted  above,  the  routines can use several windows, but two are
2637a69bbfbSPeter Wemm   automatically given: curscr, which knows what the terminal looks like,
2647a69bbfbSPeter Wemm   and  stdscr,  which  is what the programmer wants the terminal to look
2657a69bbfbSPeter Wemm   like  next.  The  user  should  never actually access curscr directly.
2667a69bbfbSPeter Wemm   Changes  should  be  made  to  through  the  API, and then the routine
2677a69bbfbSPeter Wemm   refresh() (or wrefresh()) called.
2687a69bbfbSPeter Wemm
2697a69bbfbSPeter Wemm   Many  functions  are  defined  to  use stdscr as a default screen. For
2707a69bbfbSPeter Wemm   example,  to  add  a  character  to stdscr, one calls addch() with the
2717a69bbfbSPeter Wemm   desired character as argument. To write to a different window. use the
2727a69bbfbSPeter Wemm   routine  waddch()  (for  `w'indow-specific  addch()) is provided. This
2737a69bbfbSPeter Wemm   convention of prepending function names with a `w' when they are to be
2747a69bbfbSPeter Wemm   applied  to specific windows is consistent. The only routines which do
2757a69bbfbSPeter Wemm   not follow it are those for which a window must always be specified.
2767a69bbfbSPeter Wemm
2777a69bbfbSPeter Wemm   In  order  to  move  the  current (y, x) coordinates from one point to
2787a69bbfbSPeter Wemm   another,  the routines move() and wmove() are provided. However, it is
2797a69bbfbSPeter Wemm   often  desirable to first move and then perform some I/O operation. In
2807a69bbfbSPeter Wemm   order  to  avoid  clumsiness, most I/O routines can be preceded by the
2817a69bbfbSPeter Wemm   prefix  'mv'  and  the  desired  (y,  x)  coordinates prepended to the
2827a69bbfbSPeter Wemm   arguments to the function. For example, the calls
2837a69bbfbSPeter Wemm          move(y, x);
2847a69bbfbSPeter Wemm          addch(ch);
2857a69bbfbSPeter Wemm
2867a69bbfbSPeter Wemm   can be replaced by
2877a69bbfbSPeter Wemm          mvaddch(y, x, ch);
2887a69bbfbSPeter Wemm
2897a69bbfbSPeter Wemm   and
2907a69bbfbSPeter Wemm          wmove(win, y, x);
2917a69bbfbSPeter Wemm          waddch(win, ch);
2927a69bbfbSPeter Wemm
2937a69bbfbSPeter Wemm   can be replaced by
2947a69bbfbSPeter Wemm          mvwaddch(win, y, x, ch);
2957a69bbfbSPeter Wemm
2967a69bbfbSPeter Wemm   Note  that the window description pointer (win) comes before the added
2977a69bbfbSPeter Wemm   (y,  x)  coordinates.  If  a function requires a window pointer, it is
2987a69bbfbSPeter Wemm   always the first parameter passed.
2997a69bbfbSPeter Wemm
3007a69bbfbSPeter Wemm  Variables
3017a69bbfbSPeter Wemm
3027a69bbfbSPeter Wemm   The  curses  library  sets  some  variables  describing  the  terminal
3037a69bbfbSPeter Wemm   capabilities.
3047a69bbfbSPeter Wemm      type   name      description
3057a69bbfbSPeter Wemm      ------------------------------------------------------------------
3067a69bbfbSPeter Wemm      int    LINES     number of lines on the terminal
3077a69bbfbSPeter Wemm      int    COLS      number of columns on the terminal
3087a69bbfbSPeter Wemm
3097a69bbfbSPeter Wemm   The  curses.h  also  introduces  some  #define  constants and types of
3107a69bbfbSPeter Wemm   general usefulness:
3117a69bbfbSPeter Wemm
3127a69bbfbSPeter Wemm   bool
3137a69bbfbSPeter Wemm          boolean type, actually a `char' (e.g., bool doneit;)
3147a69bbfbSPeter Wemm
3157a69bbfbSPeter Wemm   TRUE
3167a69bbfbSPeter Wemm          boolean `true' flag (1).
3177a69bbfbSPeter Wemm
3187a69bbfbSPeter Wemm   FALSE
3197a69bbfbSPeter Wemm          boolean `false' flag (0).
3207a69bbfbSPeter Wemm
3217a69bbfbSPeter Wemm   ERR
3227a69bbfbSPeter Wemm          error flag returned by routines on a failure (-1).
3237a69bbfbSPeter Wemm
3247a69bbfbSPeter Wemm   OK
3257a69bbfbSPeter Wemm          error flag returned by routines when things go right.
3267a69bbfbSPeter Wemm
3277a69bbfbSPeter WemmUsing the Library
3287a69bbfbSPeter Wemm
3297a69bbfbSPeter Wemm   Now  we  describe  how  to  actually use the screen package. In it, we
3307a69bbfbSPeter Wemm   assume  all  updating,  reading,  etc.  is  applied  to  stdscr. These
3317a69bbfbSPeter Wemm   instructions  will  work  on  any  window,  providing  you  change the
3327a69bbfbSPeter Wemm   function names and parameters as mentioned above.
3337a69bbfbSPeter Wemm
3347a69bbfbSPeter Wemm   Here is a sample program to motivate the discussion:
3357a69bbfbSPeter Wemm#include <curses.h>
3367a69bbfbSPeter Wemm#include <signal.h>
3377a69bbfbSPeter Wemm
3387a69bbfbSPeter Wemmstatic void finish(int sig);
3397a69bbfbSPeter Wemm
3407a69bbfbSPeter Wemmint
3417a69bbfbSPeter Wemmmain(int argc, char *argv[])
3427a69bbfbSPeter Wemm{
3437a69bbfbSPeter Wemm    int num = 0;
3447a69bbfbSPeter Wemm
3457a69bbfbSPeter Wemm    /* initialize your non-curses data structures here */
3467a69bbfbSPeter Wemm
3477a69bbfbSPeter Wemm    (void) signal(SIGINT, finish);      /* arrange interrupts to terminate */
3487a69bbfbSPeter Wemm
3497a69bbfbSPeter Wemm    (void) initscr();      /* initialize the curses library */
3507a69bbfbSPeter Wemm    keypad(stdscr, TRUE);  /* enable keyboard mapping */
3517a69bbfbSPeter Wemm    (void) nonl();         /* tell curses not to do NL->CR/NL on output */
3527a69bbfbSPeter Wemm    (void) cbreak();       /* take input chars one at a time, no wait for \n */
3537a69bbfbSPeter Wemm    (void) echo();         /* echo input - in color */
3547a69bbfbSPeter Wemm
3557a69bbfbSPeter Wemm    if (has_colors())
3567a69bbfbSPeter Wemm    {
3577a69bbfbSPeter Wemm        start_color();
3587a69bbfbSPeter Wemm
3597a69bbfbSPeter Wemm        /*
3607a69bbfbSPeter Wemm         * Simple color assignment, often all we need.  Color pair 0 cannot
3617a69bbfbSPeter Wemm         * be redefined.  This example uses the same value for the color
3627a69bbfbSPeter Wemm         * pair as for the foreground color, though of course that is not
3637a69bbfbSPeter Wemm         * necessary:
3647a69bbfbSPeter Wemm         */
3657a69bbfbSPeter Wemm        init_pair(1, COLOR_RED,     COLOR_BLACK);
3667a69bbfbSPeter Wemm        init_pair(2, COLOR_GREEN,   COLOR_BLACK);
3677a69bbfbSPeter Wemm        init_pair(3, COLOR_YELLOW,  COLOR_BLACK);
3687a69bbfbSPeter Wemm        init_pair(4, COLOR_BLUE,    COLOR_BLACK);
3697a69bbfbSPeter Wemm        init_pair(5, COLOR_CYAN,    COLOR_BLACK);
3707a69bbfbSPeter Wemm        init_pair(6, COLOR_MAGENTA, COLOR_BLACK);
3717a69bbfbSPeter Wemm        init_pair(7, COLOR_WHITE,   COLOR_BLACK);
3727a69bbfbSPeter Wemm    }
3737a69bbfbSPeter Wemm
3747a69bbfbSPeter Wemm    for (;;)
3757a69bbfbSPeter Wemm    {
3767a69bbfbSPeter Wemm        int c = getch();     /* refresh, accept single keystroke of input */
3777a69bbfbSPeter Wemm        attrset(COLOR_PAIR(num % 8));
3787a69bbfbSPeter Wemm        num++;
3797a69bbfbSPeter Wemm
3807a69bbfbSPeter Wemm        /* process the command keystroke */
3817a69bbfbSPeter Wemm    }
3827a69bbfbSPeter Wemm
3837a69bbfbSPeter Wemm    finish(0);               /* we're done */
3847a69bbfbSPeter Wemm}
3857a69bbfbSPeter Wemm
3867a69bbfbSPeter Wemmstatic void finish(int sig)
3877a69bbfbSPeter Wemm{
3887a69bbfbSPeter Wemm    endwin();
3897a69bbfbSPeter Wemm
3907a69bbfbSPeter Wemm    /* do your non-curses wrapup here */
3917a69bbfbSPeter Wemm
3927a69bbfbSPeter Wemm    exit(0);
3937a69bbfbSPeter Wemm}
3947a69bbfbSPeter Wemm
3957a69bbfbSPeter Wemm  Starting up
3967a69bbfbSPeter Wemm
3977a69bbfbSPeter Wemm   In  order  to  use  the  screen  package, the routines must know about
3987a69bbfbSPeter Wemm   terminal  characteristics, and the space for curscr and stdscr must be
3997a69bbfbSPeter Wemm   allocated.  These  function initscr() does both these things. Since it
4007a69bbfbSPeter Wemm   must  allocate  space  for  the  windows,  it can overflow memory when
4017a69bbfbSPeter Wemm   attempting  to  do  so.  On the rare occasions this happens, initscr()
4027a69bbfbSPeter Wemm   will  terminate  the  program  with  an  error message. initscr() must
4037a69bbfbSPeter Wemm   always  be  called before any of the routines which affect windows are
4047a69bbfbSPeter Wemm   used.  If  it  is  not,  the  program will core dump as soon as either
4057a69bbfbSPeter Wemm   curscr  or  stdscr are referenced. However, it is usually best to wait
4067a69bbfbSPeter Wemm   to  call  it  until  after  you  are sure you will need it, like after
4077a69bbfbSPeter Wemm   checking  for  startup  errors. Terminal status changing routines like
4087a69bbfbSPeter Wemm   nl() and cbreak() should be called after initscr().
4097a69bbfbSPeter Wemm
4107a69bbfbSPeter Wemm   Once  the  screen windows have been allocated, you can set them up for
4117a69bbfbSPeter Wemm   your  program.  If  you  want  to,  say, allow a screen to scroll, use
4127a69bbfbSPeter Wemm   scrollok().  If you want the cursor to be left in place after the last
4137a69bbfbSPeter Wemm   change,  use  leaveok().  If  this isn't done, refresh() will move the
4147a69bbfbSPeter Wemm   cursor to the window's current (y, x) coordinates after updating it.
4157a69bbfbSPeter Wemm
4167a69bbfbSPeter Wemm   You  can  create new windows of your own using the functions newwin(),
4177a69bbfbSPeter Wemm   derwin(), and subwin(). The routine delwin() will allow you to get rid
4187a69bbfbSPeter Wemm   of  old windows. All the options described above can be applied to any
4197a69bbfbSPeter Wemm   window.
4207a69bbfbSPeter Wemm
4217a69bbfbSPeter Wemm  Output
4227a69bbfbSPeter Wemm
4237a69bbfbSPeter Wemm   Now  that  we  have set things up, we will want to actually update the
4247a69bbfbSPeter Wemm   terminal.  The basic functions used to change what will go on a window
4257a69bbfbSPeter Wemm   are addch() and move(). addch() adds a character at the current (y, x)
4267a69bbfbSPeter Wemm   coordinates. move() changes the current (y, x) coordinates to whatever
4277a69bbfbSPeter Wemm   you want them to be. It returns ERR if you try to move off the window.
4287a69bbfbSPeter Wemm   As  mentioned above, you can combine the two into mvaddch() to do both
4297a69bbfbSPeter Wemm   things at once.
4307a69bbfbSPeter Wemm
4317a69bbfbSPeter Wemm   The  other  output  functions, such as addstr() and printw(), all call
4327a69bbfbSPeter Wemm   addch() to add characters to the window.
4337a69bbfbSPeter Wemm
4347a69bbfbSPeter Wemm   After  you  have  put on the window what you want there, when you want
4357a69bbfbSPeter Wemm   the  portion  of the terminal covered by the window to be made to look
4367a69bbfbSPeter Wemm   like  it,  you  must  call  refresh().  In  order  to optimize finding
4377a69bbfbSPeter Wemm   changes,  refresh()  assumes  that  any part of the window not changed
4387a69bbfbSPeter Wemm   since  the  last  refresh() of that window has not been changed on the
4397a69bbfbSPeter Wemm   terminal,  i.e., that you have not refreshed a portion of the terminal
4407a69bbfbSPeter Wemm   with  an  overlapping  window.  If  this  is not the case, the routine
4417a69bbfbSPeter Wemm   touchwin() is provided to make it look like the entire window has been
4427a69bbfbSPeter Wemm   changed,  thus  making  refresh()  check  the  whole subsection of the
4437a69bbfbSPeter Wemm   terminal for changes.
4447a69bbfbSPeter Wemm
4457a69bbfbSPeter Wemm   If  you  call wrefresh() with curscr as its argument, it will make the
4467a69bbfbSPeter Wemm   screen  look  like  curscr  thinks  it  looks like. This is useful for
4477a69bbfbSPeter Wemm   implementing  a  command  which would redraw the screen in case it get
4487a69bbfbSPeter Wemm   messed up.
4497a69bbfbSPeter Wemm
4507a69bbfbSPeter Wemm  Input
4517a69bbfbSPeter Wemm
4527a69bbfbSPeter Wemm   The  complementary  function  to  addch() is getch() which, if echo is
4537a69bbfbSPeter Wemm   set, will call addch() to echo the character. Since the screen package
4547a69bbfbSPeter Wemm   needs  to know what is on the terminal at all times, if characters are
4557a69bbfbSPeter Wemm   to  be  echoed, the tty must be in raw or cbreak mode. Since initially
4567a69bbfbSPeter Wemm   the  terminal  has echoing enabled and is in ordinary ``cooked'' mode,
4577a69bbfbSPeter Wemm   one or the other has to changed before calling getch(); otherwise, the
4587a69bbfbSPeter Wemm   program's output will be unpredictable.
4597a69bbfbSPeter Wemm
4607a69bbfbSPeter Wemm   When you need to accept line-oriented input in a window, the functions
4617a69bbfbSPeter Wemm   wgetstr() and friends are available. There is even a wscanw() function
4627a69bbfbSPeter Wemm   that  can  do  scanf()(3)-style  multi-field  parsing on window input.
4637a69bbfbSPeter Wemm   These  pseudo-line-oriented  functions  turn  on  echoing  while  they
4647a69bbfbSPeter Wemm   execute.
4657a69bbfbSPeter Wemm
4667a69bbfbSPeter Wemm   The  example  code  above uses the call keypad(stdscr, TRUE) to enable
4677a69bbfbSPeter Wemm   support  for function-key mapping. With this feature, the getch() code
4687a69bbfbSPeter Wemm   watches  the  input  stream for character sequences that correspond to
4697a69bbfbSPeter Wemm   arrow   and   function   keys.   These   sequences   are  returned  as
4707a69bbfbSPeter Wemm   pseudo-character values. The #define values returned are listed in the
4717a69bbfbSPeter Wemm   curses.h The mapping from sequences to #define values is determined by
4727a69bbfbSPeter Wemm   key_ capabilities in the terminal's terminfo entry.
4737a69bbfbSPeter Wemm
4747a69bbfbSPeter Wemm  Using Forms Characters
4757a69bbfbSPeter Wemm
4767a69bbfbSPeter Wemm   The  addch()  function (and some others, including box() and border())
4777a69bbfbSPeter Wemm   can accept some pseudo-character arguments which are specially defined
4787a69bbfbSPeter Wemm   by  ncurses.  These  are #define values set up in the curses.h header;
4797a69bbfbSPeter Wemm   see there for a complete list (look for the prefix ACS_).
4807a69bbfbSPeter Wemm
4817a69bbfbSPeter Wemm   The  most  useful of the ACS defines are the forms-drawing characters.
4827a69bbfbSPeter Wemm   You  can  use  these to draw boxes and simple graphs on the screen. If
4837a69bbfbSPeter Wemm   the  terminal does not have such characters, curses.h will map them to
4847a69bbfbSPeter Wemm   a recognizable (though ugly) set of ASCII defaults.
4857a69bbfbSPeter Wemm
4867a69bbfbSPeter Wemm  Character Attributes and Color
4877a69bbfbSPeter Wemm
4887a69bbfbSPeter Wemm   The  ncurses  package  supports  screen highlights including standout,
4897a69bbfbSPeter Wemm   reverse-video,  underline, and blink. It also supports color, which is
4907a69bbfbSPeter Wemm   treated as another kind of highlight.
4917a69bbfbSPeter Wemm
4927a69bbfbSPeter Wemm   Highlights   are   encoded,   internally,   as   high   bits   of  the
4937a69bbfbSPeter Wemm   pseudo-character  type  (chtype)  that  curses.h uses to represent the
4947a69bbfbSPeter Wemm   contents of a screen cell. See the curses.h header file for a complete
4957a69bbfbSPeter Wemm   list of highlight mask values (look for the prefix A_).
4967a69bbfbSPeter Wemm
4977a69bbfbSPeter Wemm   There  are two ways to make highlights. One is to logical-or the value
4987a69bbfbSPeter Wemm   of  the  highlights you want into the character argument of an addch()
4997a69bbfbSPeter Wemm   call, or any other output call that takes a chtype argument.
5007a69bbfbSPeter Wemm
5017a69bbfbSPeter Wemm   The other is to set the current-highlight value. This is logical-or'ed
5027a69bbfbSPeter Wemm   with  any  highlight  you  specify the first way. You do this with the
5037a69bbfbSPeter Wemm   functions attron(), attroff(), and attrset(); see the manual pages for
5047a69bbfbSPeter Wemm   details.  Color  is  a special kind of highlight. The package actually
5057a69bbfbSPeter Wemm   thinks  in  terms  of  color  pairs,  combinations  of  foreground and
5067a69bbfbSPeter Wemm   background  colors.  The  sample code above sets up eight color pairs,
5077a69bbfbSPeter Wemm   all  of the guaranteed-available colors on black. Note that each color
5087a69bbfbSPeter Wemm   pair  is, in effect, given the name of its foreground color. Any other
5097a69bbfbSPeter Wemm   range  of  eight  non-conflicting  values  could have been used as the
5107a69bbfbSPeter Wemm   first arguments of the init_pair() values.
5117a69bbfbSPeter Wemm
5127a69bbfbSPeter Wemm   Once you've done an init_pair() that creates color-pair N, you can use
5137a69bbfbSPeter Wemm   COLOR_PAIR(N)  as  a  highlight  that  invokes  that  particular color
5147a69bbfbSPeter Wemm   combination.  Note  that  COLOR_PAIR(N),  for  constant N, is itself a
5157a69bbfbSPeter Wemm   compile-time constant and can be used in initializers.
5167a69bbfbSPeter Wemm
5177a69bbfbSPeter Wemm  Mouse Interfacing
5187a69bbfbSPeter Wemm
5197a69bbfbSPeter Wemm   The ncurses library also provides a mouse interface.
5207a69bbfbSPeter Wemm
5217a69bbfbSPeter Wemm     NOTE:  this  facility  is  specific  to  ncurses, it is not part of
5227a69bbfbSPeter Wemm     either  the XSI Curses standard, nor of System V Release 4, nor BSD
5237a69bbfbSPeter Wemm     curses.  System  V  Release  4  curses  contains  code with similar
5247a69bbfbSPeter Wemm     interface  definitions, however it is not documented. Other than by
5257a69bbfbSPeter Wemm     disassembling  the library, we have no way to determine exactly how
5267a69bbfbSPeter Wemm     that   mouse   code   works.  Thus,  we  recommend  that  you  wrap
5277a69bbfbSPeter Wemm     mouse-related   code   in   an   #ifdef  using  the  feature  macro
5287a69bbfbSPeter Wemm     NCURSES_MOUSE_VERSION  so  it  will  not  be compiled and linked on
5297a69bbfbSPeter Wemm     non-ncurses systems.
5307a69bbfbSPeter Wemm
5317a69bbfbSPeter Wemm   Presently, mouse event reporting works in the following environments:
5327a69bbfbSPeter Wemm     * xterm and similar programs such as rxvt.
5337a69bbfbSPeter Wemm     * Linux  console,  when  configured with gpm(1), Alessandro Rubini's
5347a69bbfbSPeter Wemm       mouse server.
5354a1a9510SRong-En Fan     * FreeBSD sysmouse (console)
5367a69bbfbSPeter Wemm     * OS/2 EMX
5377a69bbfbSPeter Wemm
5387a69bbfbSPeter Wemm   The  mouse  interface  is  very  simple.  To  activate it, you use the
5397a69bbfbSPeter Wemm   function  mousemask(),  passing  it  as first argument a bit-mask that
5407a69bbfbSPeter Wemm   specifies  what  kinds  of  events you want your program to be able to
5417a69bbfbSPeter Wemm   see.  It  will  return  the  bit-mask  of  events that actually become
5427a69bbfbSPeter Wemm   visible, which may differ from the argument if the mouse device is not
5437a69bbfbSPeter Wemm   capable of reporting some of the event types you specify.
5447a69bbfbSPeter Wemm
5457a69bbfbSPeter Wemm   Once the mouse is active, your application's command loop should watch
5467a69bbfbSPeter Wemm   for  a  return  value of KEY_MOUSE from wgetch(). When you see this, a
5477a69bbfbSPeter Wemm   mouse  event report has been queued. To pick it off the queue, use the
5487a69bbfbSPeter Wemm   function  getmouse()  (you  must  do  this  before  the next wgetch(),
5497a69bbfbSPeter Wemm   otherwise  another  mouse  event  might come in and make the first one
5507a69bbfbSPeter Wemm   inaccessible).
5517a69bbfbSPeter Wemm
5527a69bbfbSPeter Wemm   Each call to getmouse() fills a structure (the address of which you'll
5537a69bbfbSPeter Wemm   pass  it)  with mouse event data. The event data includes zero-origin,
5547a69bbfbSPeter Wemm   screen-relative  character-cell  coordinates  of the mouse pointer. It
5557a69bbfbSPeter Wemm   also  includes  an  event  mask.  Bits  in  this  mask  will  be  set,
5567a69bbfbSPeter Wemm   corresponding to the event type being reported.
5577a69bbfbSPeter Wemm
5587a69bbfbSPeter Wemm   The  mouse  structure  contains  two  additional  fields  which may be
5597a69bbfbSPeter Wemm   significant  in  the  future  as  ncurses  interfaces  to new kinds of
5607a69bbfbSPeter Wemm   pointing  device.  In addition to x and y coordinates, there is a slot
5617a69bbfbSPeter Wemm   for  a  z coordinate; this might be useful with touch-screens that can
5627a69bbfbSPeter Wemm   return  a  pressure  or  duration parameter. There is also a device ID
5637a69bbfbSPeter Wemm   field,  which  could  be used to distinguish between multiple pointing
5647a69bbfbSPeter Wemm   devices.
5657a69bbfbSPeter Wemm
5667a69bbfbSPeter Wemm   The   class  of  visible  events  may  be  changed  at  any  time  via
5677a69bbfbSPeter Wemm   mousemask().  Events  that  can be reported include presses, releases,
5687a69bbfbSPeter Wemm   single-,   double-   and   triple-clicks  (you  can  set  the  maximum
5697a69bbfbSPeter Wemm   button-down  time  for clicks). If you don't make clicks visible, they
5707a69bbfbSPeter Wemm   will  be  reported  as  press-release pairs. In some environments, the
5717a69bbfbSPeter Wemm   event  mask  may  include  bits reporting the state of shift, alt, and
5727a69bbfbSPeter Wemm   ctrl keys on the keyboard during the event.
5737a69bbfbSPeter Wemm
5747a69bbfbSPeter Wemm   A  function  to check whether a mouse event fell within a given window
5757a69bbfbSPeter Wemm   is  also  supplied.  You  can  use  this to see whether a given window
5767a69bbfbSPeter Wemm   should consider a mouse event relevant to it.
5777a69bbfbSPeter Wemm
5787a69bbfbSPeter Wemm   Because   mouse   event   reporting  will  not  be  available  in  all
5797a69bbfbSPeter Wemm   environments,  it  would  be unwise to build ncurses applications that
5807a69bbfbSPeter Wemm   require  the  use  of  a  mouse. Rather, you should use the mouse as a
5817a69bbfbSPeter Wemm   shortcut  for point-and-shoot commands your application would normally
5827a69bbfbSPeter Wemm   accept  from  the  keyboard.  Two  of  the  test  games in the ncurses
5837a69bbfbSPeter Wemm   distribution  (bs  and  knight) contain code that illustrates how this
5847a69bbfbSPeter Wemm   can be done.
5857a69bbfbSPeter Wemm
5867a69bbfbSPeter Wemm   See   the   manual   page  curs_mouse(3X)  for  full  details  of  the
5877a69bbfbSPeter Wemm   mouse-interface functions.
5887a69bbfbSPeter Wemm
5897a69bbfbSPeter Wemm  Finishing Up
5907a69bbfbSPeter Wemm
5917a69bbfbSPeter Wemm   In  order to clean up after the ncurses routines, the routine endwin()
5927a69bbfbSPeter Wemm   is  provided.  It  restores tty modes to what they were when initscr()
5937a69bbfbSPeter Wemm   was  first called, and moves the cursor down to the lower-left corner.
5947a69bbfbSPeter Wemm   Thus,  anytime  after  the  call to initscr, endwin() should be called
5957a69bbfbSPeter Wemm   before exiting.
5967a69bbfbSPeter Wemm
5977a69bbfbSPeter WemmFunction Descriptions
5987a69bbfbSPeter Wemm
5997a69bbfbSPeter Wemm   We  describe  the detailed behavior of some important curses functions
6007a69bbfbSPeter Wemm   here, as a supplement to the manual page descriptions.
6017a69bbfbSPeter Wemm
6027a69bbfbSPeter Wemm  Initialization and Wrapup
6037a69bbfbSPeter Wemm
6047a69bbfbSPeter Wemm   initscr()
6057a69bbfbSPeter Wemm          The  first  function  called should almost always be initscr().
6067a69bbfbSPeter Wemm          This  will  determine  the  terminal type and initialize curses
6077a69bbfbSPeter Wemm          data structures. initscr() also arranges that the first call to
6087a69bbfbSPeter Wemm          refresh()  will  clear the screen. If an error occurs a message
6097a69bbfbSPeter Wemm          is  written  to standard error and the program exits. Otherwise
6107a69bbfbSPeter Wemm          it  returns  a pointer to stdscr. A few functions may be called
6114a1a9510SRong-En Fan          before  initscr (slk_init(), filter(), ripoffline(), use_env(),
6127a69bbfbSPeter Wemm          and, if you are using multiple terminals, newterm().)
6137a69bbfbSPeter Wemm
6147a69bbfbSPeter Wemm   endwin()
6157a69bbfbSPeter Wemm          Your  program  should  always  call  endwin() before exiting or
6167a69bbfbSPeter Wemm          shelling  out  of  the  program. This function will restore tty
6177a69bbfbSPeter Wemm          modes,  move the cursor to the lower left corner of the screen,
6187a69bbfbSPeter Wemm          reset  the  terminal  into  the proper non-visual mode. Calling
6197a69bbfbSPeter Wemm          refresh()  or  doupdate()  after  a  temporary  escape from the
6207a69bbfbSPeter Wemm          program will restore the ncurses screen from before the escape.
6217a69bbfbSPeter Wemm
6227a69bbfbSPeter Wemm   newterm(type, ofp, ifp)
6237a69bbfbSPeter Wemm          A  program  which  outputs to more than one terminal should use
6247a69bbfbSPeter Wemm          newterm() instead of initscr(). newterm() should be called once
6257a69bbfbSPeter Wemm          for each terminal. It returns a variable of type SCREEN * which
6264a1a9510SRong-En Fan          should  be  saved  as  a  reference  to that terminal. (NOTE: a
6274a1a9510SRong-En Fan          SCREEN  variable is not a screen in the sense we are describing
6284a1a9510SRong-En Fan          in  this  introduction,  but a collection of parameters used to
6294a1a9510SRong-En Fan          assist  in  optimizing the display.) The arguments are the type
6304a1a9510SRong-En Fan          of the terminal (a string) and FILE pointers for the output and
6314a1a9510SRong-En Fan          input  of  the  terminal.  If type is NULL then the environment
6324a1a9510SRong-En Fan          variable  $TERM  is used. endwin() should called once at wrapup
6334a1a9510SRong-En Fan          time for each terminal opened using this function.
6347a69bbfbSPeter Wemm
6357a69bbfbSPeter Wemm   set_term(new)
6367a69bbfbSPeter Wemm          This  function  is  used  to  switch  to  a  different terminal
6377a69bbfbSPeter Wemm          previously  opened  by  newterm(). The screen reference for the
6387a69bbfbSPeter Wemm          new  terminal is passed as the parameter. The previous terminal
6397a69bbfbSPeter Wemm          is  returned  by  the function. All other calls affect only the
6407a69bbfbSPeter Wemm          current terminal.
6417a69bbfbSPeter Wemm
6427a69bbfbSPeter Wemm   delscreen(sp)
6437a69bbfbSPeter Wemm          The  inverse  of  newterm();  deallocates  the  data structures
6447a69bbfbSPeter Wemm          associated with a given SCREEN reference.
6457a69bbfbSPeter Wemm
6467a69bbfbSPeter Wemm  Causing Output to the Terminal
6477a69bbfbSPeter Wemm
6487a69bbfbSPeter Wemm   refresh() and wrefresh(win)
6497a69bbfbSPeter Wemm          These  functions  must  be called to actually get any output on
6507a69bbfbSPeter Wemm          the   terminal,   as  other  routines  merely  manipulate  data
6517a69bbfbSPeter Wemm          structures.  wrefresh() copies the named window to the physical
6527a69bbfbSPeter Wemm          terminal  screen,  taking into account what is already there in
6534a1a9510SRong-En Fan          order  to do optimizations. refresh() does a refresh of stdscr.
6544a1a9510SRong-En Fan          Unless  leaveok()  has been enabled, the physical cursor of the
6554a1a9510SRong-En Fan          terminal is left at the location of the window's cursor.
6567a69bbfbSPeter Wemm
6577a69bbfbSPeter Wemm   doupdate() and wnoutrefresh(win)
6587a69bbfbSPeter Wemm          These two functions allow multiple updates with more efficiency
6597a69bbfbSPeter Wemm          than  wrefresh.  To use them, it is important to understand how
6607a69bbfbSPeter Wemm          curses  works. In addition to all the window structures, curses
6617a69bbfbSPeter Wemm          keeps  two  data structures representing the terminal screen: a
6627a69bbfbSPeter Wemm          physical screen, describing what is actually on the screen, and
6637a69bbfbSPeter Wemm          a  virtual screen, describing what the programmer wants to have
6647a69bbfbSPeter Wemm          on the screen. wrefresh works by first copying the named window
6657a69bbfbSPeter Wemm          to  the  virtual  screen (wnoutrefresh()), and then calling the
6667a69bbfbSPeter Wemm          routine  to  update  the screen (doupdate()). If the programmer
6677a69bbfbSPeter Wemm          wishes  to output several windows at once, a series of calls to
6687a69bbfbSPeter Wemm          wrefresh will result in alternating calls to wnoutrefresh() and
6697a69bbfbSPeter Wemm          doupdate(),  causing several bursts of output to the screen. By
6707a69bbfbSPeter Wemm          calling  wnoutrefresh() for each window, it is then possible to
6717a69bbfbSPeter Wemm          call  doupdate()  once,  resulting in only one burst of output,
6727a69bbfbSPeter Wemm          with  fewer  total  characters  transmitted (this also avoids a
6737a69bbfbSPeter Wemm          visually annoying flicker at each update).
6747a69bbfbSPeter Wemm
6757a69bbfbSPeter Wemm  Low-Level Capability Access
6767a69bbfbSPeter Wemm
6777a69bbfbSPeter Wemm   setupterm(term, filenum, errret)
6787a69bbfbSPeter Wemm          This  routine is called to initialize a terminal's description,
6797a69bbfbSPeter Wemm          without setting up the curses screen structures or changing the
6807a69bbfbSPeter Wemm          tty-driver mode bits. term is the character string representing
6817a69bbfbSPeter Wemm          the  name  of the terminal being used. filenum is the UNIX file
6827a69bbfbSPeter Wemm          descriptor  of  the terminal to be used for output. errret is a
6837a69bbfbSPeter Wemm          pointer to an integer, in which a success or failure indication
6847a69bbfbSPeter Wemm          is  returned. The values returned can be 1 (all is well), 0 (no
6857a69bbfbSPeter Wemm          such  terminal),  or  -1  (some  problem  locating the terminfo
6867a69bbfbSPeter Wemm          database).
6877a69bbfbSPeter Wemm
6887a69bbfbSPeter Wemm          The  value  of  term can be given as NULL, which will cause the
6897a69bbfbSPeter Wemm          value of TERM in the environment to be used. The errret pointer
6907a69bbfbSPeter Wemm          can  also be given as NULL, meaning no error code is wanted. If
6917a69bbfbSPeter Wemm          errret is defaulted, and something goes wrong, setupterm() will
6927a69bbfbSPeter Wemm          print  an  appropriate  error  message  and  exit,  rather than
6937a69bbfbSPeter Wemm          returning.  Thus,  a simple program can call setupterm(0, 1, 0)
6947a69bbfbSPeter Wemm          and not worry about initialization errors.
6957a69bbfbSPeter Wemm
6967a69bbfbSPeter Wemm          After  the call to setupterm(), the global variable cur_term is
6977a69bbfbSPeter Wemm          set to point to the current structure of terminal capabilities.
6987a69bbfbSPeter Wemm          By  calling  setupterm()  for  each  terminal,  and  saving and
6997a69bbfbSPeter Wemm          restoring  cur_term, it is possible for a program to use two or
7007a69bbfbSPeter Wemm          more  terminals  at  once.  Setupterm()  also  stores the names
7017a69bbfbSPeter Wemm          section  of  the  terminal  description in the global character
7027a69bbfbSPeter Wemm          array ttytype[]. Subsequent calls to setupterm() will overwrite
7037a69bbfbSPeter Wemm          this array, so you'll have to save it yourself if need be.
7047a69bbfbSPeter Wemm
7057a69bbfbSPeter Wemm  Debugging
7067a69bbfbSPeter Wemm
7077a69bbfbSPeter Wemm     NOTE: These functions are not part of the standard curses API!
7087a69bbfbSPeter Wemm
7097a69bbfbSPeter Wemm   trace()
7107a69bbfbSPeter Wemm          This  function  can be used to explicitly set a trace level. If
7117a69bbfbSPeter Wemm          the  trace  level  is  nonzero,  execution of your program will
7127a69bbfbSPeter Wemm          generate a file called `trace' in the current working directory
7137a69bbfbSPeter Wemm          containing  a  report  on  the  library's actions. Higher trace
7147a69bbfbSPeter Wemm          levels  enable  more  detailed  (and  verbose) reporting -- see
7157a69bbfbSPeter Wemm          comments  attached  to  TRACE_ defines in the curses.h file for
7167a69bbfbSPeter Wemm          details. (It is also possible to set a trace level by assigning
7177a69bbfbSPeter Wemm          a trace level value to the environment variable NCURSES_TRACE).
7187a69bbfbSPeter Wemm
7197a69bbfbSPeter Wemm   _tracef()
7207a69bbfbSPeter Wemm          This  function  can  be  used  to  output  your  own  debugging
7217a69bbfbSPeter Wemm          information.  It  is  only  available  only  if  you  link with
7227a69bbfbSPeter Wemm          -lncurses_g.  It  can be used the same way as printf(), only it
7237a69bbfbSPeter Wemm          outputs  a  newline after the end of arguments. The output goes
7247a69bbfbSPeter Wemm          to a file called trace in the current directory.
7257a69bbfbSPeter Wemm
7267a69bbfbSPeter Wemm   Trace  logs  can  be difficult to interpret due to the sheer volume of
7277a69bbfbSPeter Wemm   data dumped in them. There is a script called tracemunch included with
7287a69bbfbSPeter Wemm   the  ncurses distribution that can alleviate this problem somewhat; it
7297a69bbfbSPeter Wemm   compacts  long  sequences  of  similar  operations  into more succinct
7307a69bbfbSPeter Wemm   single-line  pseudo-operations.  These pseudo-ops can be distinguished
7317a69bbfbSPeter Wemm   by the fact that they are named in capital letters.
7327a69bbfbSPeter Wemm
7337a69bbfbSPeter WemmHints, Tips, and Tricks
7347a69bbfbSPeter Wemm
7357a69bbfbSPeter Wemm   The ncurses manual pages are a complete reference for this library. In
7367a69bbfbSPeter Wemm   the remainder of this document, we discuss various useful methods that
7377a69bbfbSPeter Wemm   may not be obvious from the manual page descriptions.
7387a69bbfbSPeter Wemm
7397a69bbfbSPeter Wemm  Some Notes of Caution
7407a69bbfbSPeter Wemm
7417a69bbfbSPeter Wemm   If  you  find yourself thinking you need to use noraw() or nocbreak(),
7427a69bbfbSPeter Wemm   think  again  and  move  carefully. It's probably better design to use
7437a69bbfbSPeter Wemm   getstr()  or one of its relatives to simulate cooked mode. The noraw()
7447a69bbfbSPeter Wemm   and  nocbreak() functions try to restore cooked mode, but they may end
7457a69bbfbSPeter Wemm   up   clobbering   some  control  bits  set  before  you  started  your
7467a69bbfbSPeter Wemm   application.  Also,  they  have always been poorly documented, and are
7477a69bbfbSPeter Wemm   likely   to  hurt  your  application's  usability  with  other  curses
7487a69bbfbSPeter Wemm   libraries.
7497a69bbfbSPeter Wemm
7507a69bbfbSPeter Wemm   Bear  in  mind that refresh() is a synonym for wrefresh(stdscr). Don't
7517a69bbfbSPeter Wemm   try  to  mix use of stdscr with use of windows declared by newwin(); a
7527a69bbfbSPeter Wemm   refresh()  call will blow them off the screen. The right way to handle
7537a69bbfbSPeter Wemm   this  is  to  use  subwin(),  or not touch stdscr at all and tile your
7547a69bbfbSPeter Wemm   screen  with  declared windows which you then wnoutrefresh() somewhere
7557a69bbfbSPeter Wemm   in  your  program event loop, with a single doupdate() call to trigger
7567a69bbfbSPeter Wemm   actual repainting.
7577a69bbfbSPeter Wemm
7587a69bbfbSPeter Wemm   You  are  much  less  likely  to  run into problems if you design your
7597a69bbfbSPeter Wemm   screen   layouts   to  use  tiled  rather  than  overlapping  windows.
7607a69bbfbSPeter Wemm   Historically,  curses  support  for overlapping windows has been weak,
7617a69bbfbSPeter Wemm   fragile,  and  poorly  documented.  The  ncurses library is not yet an
7627a69bbfbSPeter Wemm   exception to this rule.
7637a69bbfbSPeter Wemm
7647a69bbfbSPeter Wemm   There  is  a  panels library included in the ncurses distribution that
7657a69bbfbSPeter Wemm   does  a  pretty  good  job  of  strengthening  the overlapping-windows
7667a69bbfbSPeter Wemm   facilities.
7677a69bbfbSPeter Wemm
7687a69bbfbSPeter Wemm   Try to avoid using the global variables LINES and COLS. Use getmaxyx()
7697a69bbfbSPeter Wemm   on  the stdscr context instead. Reason: your code may be ported to run
7707a69bbfbSPeter Wemm   in  an  environment with window resizes, in which case several screens
7717a69bbfbSPeter Wemm   could be open with different sizes.
7727a69bbfbSPeter Wemm
7737a69bbfbSPeter Wemm  Temporarily Leaving NCURSES Mode
7747a69bbfbSPeter Wemm
7757a69bbfbSPeter Wemm   Sometimes  you  will  want  to write a program that spends most of its
7767a69bbfbSPeter Wemm   time  in  screen  mode,  but occasionally returns to ordinary `cooked'
7777a69bbfbSPeter Wemm   mode.  A common reason for this is to support shell-out. This behavior
7787a69bbfbSPeter Wemm   is simple to arrange in ncurses.
7797a69bbfbSPeter Wemm
7807a69bbfbSPeter Wemm   To  leave  ncurses  mode,  call  endwin()  as  you  would  if you were
7817a69bbfbSPeter Wemm   intending  to terminate the program. This will take the screen back to
7827a69bbfbSPeter Wemm   cooked  mode;  you  can  do your shell-out. When you want to return to
7837a69bbfbSPeter Wemm   ncurses  mode,  simply call refresh() or doupdate(). This will repaint
7847a69bbfbSPeter Wemm   the screen.
7857a69bbfbSPeter Wemm
7867a69bbfbSPeter Wemm   There  is  a  boolean function, isendwin(), which code can use to test
7877a69bbfbSPeter Wemm   whether ncurses screen mode is active. It returns TRUE in the interval
7887a69bbfbSPeter Wemm   between an endwin() call and the following refresh(), FALSE otherwise.
7897a69bbfbSPeter Wemm
7907a69bbfbSPeter Wemm   Here is some sample code for shellout:
7917a69bbfbSPeter Wemm    addstr("Shelling out...");
7927a69bbfbSPeter Wemm    def_prog_mode();           /* save current tty modes */
7937a69bbfbSPeter Wemm    endwin();                  /* restore original tty modes */
7947a69bbfbSPeter Wemm    system("sh");              /* run shell */
7957a69bbfbSPeter Wemm    addstr("returned.\n");     /* prepare return message */
7967a69bbfbSPeter Wemm    refresh();                 /* restore save modes, repaint screen */
7977a69bbfbSPeter Wemm
7987a69bbfbSPeter Wemm  Using NCURSES under XTERM
7997a69bbfbSPeter Wemm
8007a69bbfbSPeter Wemm   A  resize  operation  in  X  sends SIGWINCH to the application running
8017a69bbfbSPeter Wemm   under  xterm.  The  ncurses  library  provides  an experimental signal
8027a69bbfbSPeter Wemm   handler,  but in general does not catch this signal, because it cannot
8037a69bbfbSPeter Wemm   know  how  you  want  the  screen re-painted. You will usually have to
8047a69bbfbSPeter Wemm   write the SIGWINCH handler yourself. Ncurses can give you some help.
8057a69bbfbSPeter Wemm
8067a69bbfbSPeter Wemm   The  easiest  way  to  code  your SIGWINCH handler is to have it do an
8077a69bbfbSPeter Wemm   endwin, followed by an refresh and a screen repaint you code yourself.
8087a69bbfbSPeter Wemm   The  refresh  will  pick  up  the  new  screen  size  from the xterm's
8097a69bbfbSPeter Wemm   environment.
8107a69bbfbSPeter Wemm
8117a69bbfbSPeter Wemm   That  is the standard way, of course (it even works with some vendor's
8127a69bbfbSPeter Wemm   curses  implementations). Its drawback is that it clears the screen to
8137a69bbfbSPeter Wemm   reinitialize the display, and does not resize subwindows which must be
8147a69bbfbSPeter Wemm   shrunk.   Ncurses  provides  an  extension  which  works  better,  the
8157a69bbfbSPeter Wemm   resizeterm  function.  That  function  ensures  that  all  windows are
8167a69bbfbSPeter Wemm   limited  to  the new screen dimensions, and pads stdscr with blanks if
8177a69bbfbSPeter Wemm   the screen is larger.
8187a69bbfbSPeter Wemm
8197a69bbfbSPeter Wemm   Finally,  ncurses  can  be  configured  to  provide  its  own SIGWINCH
8207a69bbfbSPeter Wemm   handler, based on resizeterm.
8217a69bbfbSPeter Wemm
8227a69bbfbSPeter Wemm  Handling Multiple Terminal Screens
8237a69bbfbSPeter Wemm
8247a69bbfbSPeter Wemm   The initscr() function actually calls a function named newterm() to do
8257a69bbfbSPeter Wemm   most  of  its  work.  If you are writing a program that opens multiple
8267a69bbfbSPeter Wemm   terminals, use newterm() directly.
8277a69bbfbSPeter Wemm
8287a69bbfbSPeter Wemm   For  each call, you will have to specify a terminal type and a pair of
8297a69bbfbSPeter Wemm   file  pointers;  each  call will return a screen reference, and stdscr
8307a69bbfbSPeter Wemm   will be set to the last one allocated. You will switch between screens
8317a69bbfbSPeter Wemm   with  the  set_term  call.  Note  that  you  will  also  have  to call
8327a69bbfbSPeter Wemm   def_shell_mode and def_prog_mode on each tty yourself.
8337a69bbfbSPeter Wemm
8347a69bbfbSPeter Wemm  Testing for Terminal Capabilities
8357a69bbfbSPeter Wemm
8367a69bbfbSPeter Wemm   Sometimes you may want to write programs that test for the presence of
8377a69bbfbSPeter Wemm   various  capabilities before deciding whether to go into ncurses mode.
8387a69bbfbSPeter Wemm   An  easy way to do this is to call setupterm(), then use the functions
8397a69bbfbSPeter Wemm   tigetflag(), tigetnum(), and tigetstr() to do your testing.
8407a69bbfbSPeter Wemm
8417a69bbfbSPeter Wemm   A  particularly  useful  case  of this often comes up when you want to
8427a69bbfbSPeter Wemm   test  whether  a  given  terminal  type  should  be treated as `smart'
8437a69bbfbSPeter Wemm   (cursor-addressable) or `stupid'. The right way to test this is to see
8447a69bbfbSPeter Wemm   if the return value of tigetstr("cup") is non-NULL. Alternatively, you
8457a69bbfbSPeter Wemm   can  include  the  term.h  file  and  test  the  value  of  the  macro
8467a69bbfbSPeter Wemm   cursor_address.
8477a69bbfbSPeter Wemm
8487a69bbfbSPeter Wemm  Tuning for Speed
8497a69bbfbSPeter Wemm
8507a69bbfbSPeter Wemm   Use  the  addchstr()  family  of functions for fast screen-painting of
8517a69bbfbSPeter Wemm   text  when  you  know the text doesn't contain any control characters.
8527a69bbfbSPeter Wemm   Try  to  make  attribute changes infrequent on your screens. Don't use
8537a69bbfbSPeter Wemm   the immedok() option!
8547a69bbfbSPeter Wemm
8557a69bbfbSPeter Wemm  Special Features of NCURSES
8567a69bbfbSPeter Wemm
8577a69bbfbSPeter Wemm   The  wresize()  function  allows  you to resize a window in place. The
8587a69bbfbSPeter Wemm   associated   resizeterm()  function  simplifies  the  construction  of
8597a69bbfbSPeter Wemm   SIGWINCH handlers, for resizing all windows.
8607a69bbfbSPeter Wemm
8617a69bbfbSPeter Wemm   The define_key() function allows you to define at runtime function-key
8627a69bbfbSPeter Wemm   control  sequences  which  are  not  in  the terminal description. The
8637a69bbfbSPeter Wemm   keyok()   function   allows  you  to  temporarily  enable  or  disable
8647a69bbfbSPeter Wemm   interpretation of any function-key control sequence.
8657a69bbfbSPeter Wemm
8667a69bbfbSPeter Wemm   The use_default_colors() function allows you to construct applications
8677a69bbfbSPeter Wemm   which  can use the terminal's default foreground and background colors
8687a69bbfbSPeter Wemm   as  an  additional "default" color. Several terminal emulators support
8697a69bbfbSPeter Wemm   this feature, which is based on ISO 6429.
8707a69bbfbSPeter Wemm
8717a69bbfbSPeter Wemm   Ncurses  supports  up 16 colors, unlike SVr4 curses which defines only
8727a69bbfbSPeter Wemm   8. While most terminals which provide color allow only 8 colors, about
8737a69bbfbSPeter Wemm   a quarter (including XFree86 xterm) support 16 colors.
8747a69bbfbSPeter Wemm
8757a69bbfbSPeter WemmCompatibility with Older Versions
8767a69bbfbSPeter Wemm
8777a69bbfbSPeter Wemm   Despite  our  best efforts, there are some differences between ncurses
8787a69bbfbSPeter Wemm   and  the  (undocumented!)  behavior  of  older curses implementations.
8797a69bbfbSPeter Wemm   These  arise from ambiguities or omissions in the documentation of the
8807a69bbfbSPeter Wemm   API.
8817a69bbfbSPeter Wemm
8827a69bbfbSPeter Wemm  Refresh of Overlapping Windows
8837a69bbfbSPeter Wemm
8847a69bbfbSPeter Wemm   If  you  define two windows A and B that overlap, and then alternately
8857a69bbfbSPeter Wemm   scribble  on  and  refresh  them,  the changes made to the overlapping
8867a69bbfbSPeter Wemm   region  under  historic  curses  versions  were  often  not documented
8877a69bbfbSPeter Wemm   precisely.
8887a69bbfbSPeter Wemm
8897a69bbfbSPeter Wemm   To  understand why this is a problem, remember that screen updates are
8907a69bbfbSPeter Wemm   calculated  between  two  representations  of  the entire display. The
8917a69bbfbSPeter Wemm   documentation  says that when you refresh a window, it is first copied
8924a1a9510SRong-En Fan   to  the  virtual screen,  and  then  changes  are calculated to update
8937a69bbfbSPeter Wemm   the  physical screen (and applied to the terminal). But "copied to" is
8947a69bbfbSPeter Wemm   not  very  specific,  and  subtle differences in how copying works can
8957a69bbfbSPeter Wemm   produce  different behaviors in the case where two overlapping windows
8967a69bbfbSPeter Wemm   are each being refreshed at unpredictable intervals.
8977a69bbfbSPeter Wemm
8987a69bbfbSPeter Wemm   What  happens to the overlapping region depends on what wnoutrefresh()
8997a69bbfbSPeter Wemm   does  with  its  argument  --  what portions of the argument window it
9007a69bbfbSPeter Wemm   copies  to  the virtual screen. Some implementations do "change copy",
9017a69bbfbSPeter Wemm   copying  down  only locations in the window that have changed (or been
9027a69bbfbSPeter Wemm   marked  changed  with wtouchln() and friends). Some implementations do
9037a69bbfbSPeter Wemm   "entire  copy",  copying  all  window  locations to the virtual screen
9047a69bbfbSPeter Wemm   whether or not they have changed.
9057a69bbfbSPeter Wemm
9067a69bbfbSPeter Wemm   The  ncurses  library  itself  has  not always been consistent on this
9077a69bbfbSPeter Wemm   score.  Due  to  a  bug,  versions  1.8.7  to  1.9.8a did entire copy.
9087a69bbfbSPeter Wemm   Versions  1.8.6  and  older,  and  versions 1.9.9 and newer, do change
9097a69bbfbSPeter Wemm   copy.
9107a69bbfbSPeter Wemm
9117a69bbfbSPeter Wemm   For  most  commercial curses implementations, it is not documented and
9127a69bbfbSPeter Wemm   not  known  for sure (at least not to the ncurses maintainers) whether
9137a69bbfbSPeter Wemm   they  do  change  copy or entire copy. We know that System V release 3
9147a69bbfbSPeter Wemm   curses  has  logic in it that looks like an attempt to do change copy,
9157a69bbfbSPeter Wemm   but  the  surrounding  logic and data representations are sufficiently
9167a69bbfbSPeter Wemm   complex,  and  our  knowledge sufficiently indirect, that it's hard to
9177a69bbfbSPeter Wemm   know  whether  this  is  reliable.  It  is  not  clear  what  the SVr4
9187a69bbfbSPeter Wemm   documentation  and XSI standard intend. The XSI Curses standard barely
9197a69bbfbSPeter Wemm   mentions  wnoutrefresh();  the  SVr4  documents  seem to be describing
9207a69bbfbSPeter Wemm   entire-copy, but it is possible with some effort and straining to read
9217a69bbfbSPeter Wemm   them the other way.
9227a69bbfbSPeter Wemm
9237a69bbfbSPeter Wemm   It  might  therefore  be unwise to rely on either behavior in programs
9247a69bbfbSPeter Wemm   that  might  have  to  be  linked  with  other curses implementations.
9257a69bbfbSPeter Wemm   Instead,  you  can do an explicit touchwin() before the wnoutrefresh()
9267a69bbfbSPeter Wemm   call to guarantee an entire-contents copy anywhere.
9277a69bbfbSPeter Wemm
9287a69bbfbSPeter Wemm   The  really clean way to handle this is to use the panels library. If,
9297a69bbfbSPeter Wemm   when  you want a screen update, you do update_panels(), it will do all
9304a1a9510SRong-En Fan   the  necessary  wnoutrefresh() calls for whatever panel stacking order
9317a69bbfbSPeter Wemm   you  have  defined. Then you can do one doupdate() and there will be a
9327a69bbfbSPeter Wemm   single burst of physical I/O that will do all your updates.
9337a69bbfbSPeter Wemm
9347a69bbfbSPeter Wemm  Background Erase
9357a69bbfbSPeter Wemm
9367a69bbfbSPeter Wemm   If you have been using a very old versions of ncurses (1.8.7 or older)
9377a69bbfbSPeter Wemm   you  may be surprised by the behavior of the erase functions. In older
9387a69bbfbSPeter Wemm   versions,  erased  areas of a window were filled with a blank modified
9397a69bbfbSPeter Wemm   by  the  window's  current attribute (as set by wattrset(), wattron(),
9407a69bbfbSPeter Wemm   wattroff() and friends).
9417a69bbfbSPeter Wemm
9427a69bbfbSPeter Wemm   In  newer  versions,  this is not so. Instead, the attribute of erased
9437a69bbfbSPeter Wemm   blanks  is  normal  unless  and  until it is modified by the functions
9447a69bbfbSPeter Wemm   bkgdset() or wbkgdset().
9457a69bbfbSPeter Wemm
9467a69bbfbSPeter Wemm   This change in behavior conforms ncurses to System V Release 4 and the
9477a69bbfbSPeter Wemm   XSI Curses standard.
9487a69bbfbSPeter Wemm
9497a69bbfbSPeter WemmXSI Curses Conformance
9507a69bbfbSPeter Wemm
9517a69bbfbSPeter Wemm   The  ncurses  library is intended to be base-level conformant with the
9527a69bbfbSPeter Wemm   XSI  Curses  standard  from  X/Open.  Many extended-level features (in
9537a69bbfbSPeter Wemm   fact,  almost all features not directly concerned with wide characters
9547a69bbfbSPeter Wemm   and internationalization) are also supported.
9557a69bbfbSPeter Wemm
9567a69bbfbSPeter Wemm   One  effect  of  XSI  conformance  is the change in behavior described
9577a69bbfbSPeter Wemm   under "Background Erase -- Compatibility with Old Versions".
9587a69bbfbSPeter Wemm
9597a69bbfbSPeter Wemm   Also,  ncurses  meets the XSI requirement that every macro entry point
9607a69bbfbSPeter Wemm   have  a  corresponding  function  which  may  be  linked  (and will be
9617a69bbfbSPeter Wemm   prototype-checked) if the macro definition is disabled with #undef.
9627a69bbfbSPeter Wemm
9637a69bbfbSPeter Wemm                              The Panels Library
9647a69bbfbSPeter Wemm
9657a69bbfbSPeter Wemm   The  ncurses  library  by  itself  provides  good  support  for screen
9667a69bbfbSPeter Wemm   displays in which the windows are tiled (non-overlapping). In the more
9677a69bbfbSPeter Wemm   general  case  that  windows  may overlap, you have to use a series of
9687a69bbfbSPeter Wemm   wnoutrefresh()  calls  followed  by a doupdate(), and be careful about
9697a69bbfbSPeter Wemm   the order you do the window refreshes in. It has to be bottom-upwards,
9707a69bbfbSPeter Wemm   otherwise parts of windows that should be obscured will show through.
9717a69bbfbSPeter Wemm
9727a69bbfbSPeter Wemm   When  your  interface design is such that windows may dive deeper into
9737a69bbfbSPeter Wemm   the  visibility  stack  or  pop  to  the top at runtime, the resulting
9747a69bbfbSPeter Wemm   book-keeping  can  be  tedious  and  difficult to get right. Hence the
9757a69bbfbSPeter Wemm   panels library.
9767a69bbfbSPeter Wemm
9777a69bbfbSPeter Wemm   The  panel  library  first  appeared  in  AT&T  System  V. The version
9787a69bbfbSPeter Wemm   documented here is the panel code distributed with ncurses.
9797a69bbfbSPeter Wemm
9807a69bbfbSPeter WemmCompiling With the Panels Library
9817a69bbfbSPeter Wemm
9827a69bbfbSPeter Wemm   Your  panels-using modules must import the panels library declarations
9837a69bbfbSPeter Wemm   with
9847a69bbfbSPeter Wemm          #include <panel.h>
9857a69bbfbSPeter Wemm
9867a69bbfbSPeter Wemm   and must be linked explicitly with the panels library using an -lpanel
9877a69bbfbSPeter Wemm   argument.  Note  that  they  must  also  link the ncurses library with
9887a69bbfbSPeter Wemm   -lncurses. Many linkers are two-pass and will accept either order, but
9897a69bbfbSPeter Wemm   it is still good practice to put -lpanel first and -lncurses second.
9907a69bbfbSPeter Wemm
9917a69bbfbSPeter WemmOverview of Panels
9927a69bbfbSPeter Wemm
9937a69bbfbSPeter Wemm   A  panel  object  is  a window that is implicitly treated as part of a
9947a69bbfbSPeter Wemm   deck  including  all  other  panel  objects.  The deck has an implicit
9957a69bbfbSPeter Wemm   bottom-to-top  visibility order. The panels library includes an update
9967a69bbfbSPeter Wemm   function (analogous to refresh()) that displays all panels in the deck
9977a69bbfbSPeter Wemm   in  the proper order to resolve overlaps. The standard window, stdscr,
9987a69bbfbSPeter Wemm   is considered below all panels.
9997a69bbfbSPeter Wemm
10007a69bbfbSPeter Wemm   Details  on the panels functions are available in the man pages. We'll
10017a69bbfbSPeter Wemm   just hit the highlights here.
10027a69bbfbSPeter Wemm
10037a69bbfbSPeter Wemm   You  create  a  panel from a window by calling new_panel() on a window
10047a69bbfbSPeter Wemm   pointer.  It  then  becomes the top of the deck. The panel's window is
10057a69bbfbSPeter Wemm   available as the value of panel_window() called with the panel pointer
10067a69bbfbSPeter Wemm   as argument.
10077a69bbfbSPeter Wemm
10087a69bbfbSPeter Wemm   You  can  delete  a  panel (removing it from the deck) with del_panel.
10097a69bbfbSPeter Wemm   This  will  not  deallocate the associated window; you have to do that
10107a69bbfbSPeter Wemm   yourself.  You can replace a panel's window with a different window by
10117a69bbfbSPeter Wemm   calling  replace_window.  The new window may be of different size; the
10127a69bbfbSPeter Wemm   panel code will re-compute all overlaps. This operation doesn't change
10137a69bbfbSPeter Wemm   the panel's position in the deck.
10147a69bbfbSPeter Wemm
10157a69bbfbSPeter Wemm   To  move  a  panel's window, use move_panel(). The mvwin() function on
10167a69bbfbSPeter Wemm   the  panel's  window  isn't  sufficient  because it doesn't update the
10177a69bbfbSPeter Wemm   panels  library's  representation  of  where  the  windows  are.  This
10187a69bbfbSPeter Wemm   operation leaves the panel's depth, contents, and size unchanged.
10197a69bbfbSPeter Wemm
10207a69bbfbSPeter Wemm   Two   functions   (top_panel(),   bottom_panel())   are  provided  for
10217a69bbfbSPeter Wemm   rearranging the deck. The first pops its argument window to the top of
10227a69bbfbSPeter Wemm   the  deck;  the second sends it to the bottom. Either operation leaves
10237a69bbfbSPeter Wemm   the panel's screen location, contents, and size unchanged.
10247a69bbfbSPeter Wemm
10257a69bbfbSPeter Wemm   The  function update_panels() does all the wnoutrefresh() calls needed
10267a69bbfbSPeter Wemm   to prepare for doupdate() (which you must call yourself, afterwards).
10277a69bbfbSPeter Wemm
10287a69bbfbSPeter Wemm   Typically,  you  will want to call update_panels() and doupdate() just
10297a69bbfbSPeter Wemm   before accepting command input, once in each cycle of interaction with
10307a69bbfbSPeter Wemm   the  user.  If  you  call  update_panels()  after each and every panel
10317a69bbfbSPeter Wemm   write,  you'll  generate  a  lot  of  unnecessary refresh activity and
10327a69bbfbSPeter Wemm   screen flicker.
10337a69bbfbSPeter Wemm
10347a69bbfbSPeter WemmPanels, Input, and the Standard Screen
10357a69bbfbSPeter Wemm
10367a69bbfbSPeter Wemm   You  shouldn't mix wnoutrefresh() or wrefresh() operations with panels
10377a69bbfbSPeter Wemm   code;  this will work only if the argument window is either in the top
10387a69bbfbSPeter Wemm   panel or unobscured by any other panels.
10397a69bbfbSPeter Wemm
10407a69bbfbSPeter Wemm   The  stsdcr  window  is  a  special  case.  It is considered below all
10417a69bbfbSPeter Wemm   panels. Because changes to panels may obscure parts of stdscr, though,
10427a69bbfbSPeter Wemm   you  should  call update_panels() before doupdate() even when you only
10437a69bbfbSPeter Wemm   change stdscr.
10447a69bbfbSPeter Wemm
10457a69bbfbSPeter Wemm   Note  that  wgetch  automatically  calls  wrefresh.  Therefore, before
10467a69bbfbSPeter Wemm   requesting  input  from  a  panel window, you need to be sure that the
10477a69bbfbSPeter Wemm   panel is totally unobscured.
10487a69bbfbSPeter Wemm
10497a69bbfbSPeter Wemm   There  is  presently  no  way to display changes to one obscured panel
10507a69bbfbSPeter Wemm   without repainting all panels.
10517a69bbfbSPeter Wemm
10527a69bbfbSPeter WemmHiding Panels
10537a69bbfbSPeter Wemm
10547a69bbfbSPeter Wemm   It's  possible  to  remove  a  panel  from  the  deck temporarily; use
10557a69bbfbSPeter Wemm   hide_panel  for this. Use show_panel() to render it visible again. The
10567a69bbfbSPeter Wemm   predicate  function  panel_hidden  tests  whether  or  not  a panel is
10577a69bbfbSPeter Wemm   hidden.
10587a69bbfbSPeter Wemm
10597a69bbfbSPeter Wemm   The panel_update code ignores hidden panels. You cannot do top_panel()
10607a69bbfbSPeter Wemm   or  bottom_panel  on  a  hidden  panel().  Other panels operations are
10617a69bbfbSPeter Wemm   applicable.
10627a69bbfbSPeter Wemm
10637a69bbfbSPeter WemmMiscellaneous Other Facilities
10647a69bbfbSPeter Wemm
10657a69bbfbSPeter Wemm   It's  possible  to navigate the deck using the functions panel_above()
10667a69bbfbSPeter Wemm   and  panel_below.  Handed a panel pointer, they return the panel above
10677a69bbfbSPeter Wemm   or  below  that  panel.  Handed  NULL,  they return the bottom-most or
10687a69bbfbSPeter Wemm   top-most panel.
10697a69bbfbSPeter Wemm
10707a69bbfbSPeter Wemm   Every  panel  has  an  associated  user pointer, not used by the panel
10717a69bbfbSPeter Wemm   code,  to  which  you  can  attach  application data. See the man page
10727a69bbfbSPeter Wemm   documentation of set_panel_userptr() and panel_userptr for details.
10737a69bbfbSPeter Wemm
10747a69bbfbSPeter Wemm                               The Menu Library
10757a69bbfbSPeter Wemm
10767a69bbfbSPeter Wemm   A menu is a screen display that assists the user to choose some subset
10777a69bbfbSPeter Wemm   of  a  given set of items. The menu library is a curses extension that
10787a69bbfbSPeter Wemm   supports  easy  programming  of  menu  hierarchies  with a uniform but
10797a69bbfbSPeter Wemm   flexible interface.
10807a69bbfbSPeter Wemm
10817a69bbfbSPeter Wemm   The  menu  library  first  appeared  in  AT&T  System  V.  The version
10827a69bbfbSPeter Wemm   documented here is the menu code distributed with ncurses.
10837a69bbfbSPeter Wemm
10847a69bbfbSPeter WemmCompiling With the menu Library
10857a69bbfbSPeter Wemm
10867a69bbfbSPeter Wemm   Your menu-using modules must import the menu library declarations with
10877a69bbfbSPeter Wemm          #include <menu.h>
10887a69bbfbSPeter Wemm
10897a69bbfbSPeter Wemm   and  must  be linked explicitly with the menus library using an -lmenu
10907a69bbfbSPeter Wemm   argument.  Note  that  they  must  also  link the ncurses library with
10917a69bbfbSPeter Wemm   -lncurses. Many linkers are two-pass and will accept either order, but
10927a69bbfbSPeter Wemm   it is still good practice to put -lmenu first and -lncurses second.
10937a69bbfbSPeter Wemm
10947a69bbfbSPeter WemmOverview of Menus
10957a69bbfbSPeter Wemm
10967a69bbfbSPeter Wemm   The  menus  created  by  this  library consist of collections of items
10977a69bbfbSPeter Wemm   including  a  name  string part and a description string part. To make
10987a69bbfbSPeter Wemm   menus,  you  create  groups  of these items and connect them with menu
10997a69bbfbSPeter Wemm   frame objects.
11007a69bbfbSPeter Wemm
11017a69bbfbSPeter Wemm   The  menu can then by posted, that is written to an associated window.
11027a69bbfbSPeter Wemm   Actually, each menu has two associated windows; a containing window in
11037a69bbfbSPeter Wemm   which  the  programmer can scribble titles or borders, and a subwindow
11047a69bbfbSPeter Wemm   in which the menu items proper are displayed. If this subwindow is too
11057a69bbfbSPeter Wemm   small  to  display  all the items, it will be a scrollable viewport on
11067a69bbfbSPeter Wemm   the collection of items.
11077a69bbfbSPeter Wemm
11087a69bbfbSPeter Wemm   A  menu may also be unposted (that is, undisplayed), and finally freed
11097a69bbfbSPeter Wemm   to  make  the  storage  associated with it and its items available for
11107a69bbfbSPeter Wemm   re-use.
11117a69bbfbSPeter Wemm
11127a69bbfbSPeter Wemm   The general flow of control of a menu program looks like this:
11137a69bbfbSPeter Wemm    1. Initialize curses.
11147a69bbfbSPeter Wemm    2. Create the menu items, using new_item().
11157a69bbfbSPeter Wemm    3. Create the menu using new_menu().
11164a1a9510SRong-En Fan    4. Post the menu using post_menu().
11177a69bbfbSPeter Wemm    5. Refresh the screen.
11187a69bbfbSPeter Wemm    6. Process user requests via an input loop.
11194a1a9510SRong-En Fan    7. Unpost the menu using unpost_menu().
11207a69bbfbSPeter Wemm    8. Free the menu, using free_menu().
11217a69bbfbSPeter Wemm    9. Free the items using free_item().
11227a69bbfbSPeter Wemm   10. Terminate curses.
11237a69bbfbSPeter Wemm
11247a69bbfbSPeter WemmSelecting items
11257a69bbfbSPeter Wemm
11267a69bbfbSPeter Wemm   Menus  may  be  multi-valued  or  (the default) single-valued (see the
11277a69bbfbSPeter Wemm   manual  page  menu_opts(3x)  to  see  how to change the default). Both
11287a69bbfbSPeter Wemm   types always have a current item.
11297a69bbfbSPeter Wemm
11307a69bbfbSPeter Wemm   From  a  single-valued  menu you can read the selected value simply by
11317a69bbfbSPeter Wemm   looking  at  the  current  item. From a multi-valued menu, you get the
11327a69bbfbSPeter Wemm   selected  set  by  looping through the items applying the item_value()
11337a69bbfbSPeter Wemm   predicate  function.  Your  menu-processing  code can use the function
11347a69bbfbSPeter Wemm   set_item_value() to flag the items in the select set.
11357a69bbfbSPeter Wemm
11367a69bbfbSPeter Wemm   Menu   items   can  be  made  unselectable  using  set_item_opts()  or
11377a69bbfbSPeter Wemm   item_opts_off()  with  the  O_SELECTABLE  argument.  This  is the only
11387a69bbfbSPeter Wemm   option  so  far  defined for menus, but it is good practice to code as
11397a69bbfbSPeter Wemm   though other option bits might be on.
11407a69bbfbSPeter Wemm
11417a69bbfbSPeter WemmMenu Display
11427a69bbfbSPeter Wemm
11437a69bbfbSPeter Wemm   The  menu  library  calculates a minimum display size for your window,
11447a69bbfbSPeter Wemm   based on the following variables:
11457a69bbfbSPeter Wemm     * The number and maximum length of the menu items
11467a69bbfbSPeter Wemm     * Whether the O_ROWMAJOR option is enabled
11477a69bbfbSPeter Wemm     * Whether display of descriptions is enabled
11487a69bbfbSPeter Wemm     * Whatever menu format may have been set by the programmer
11497a69bbfbSPeter Wemm     * The  length of the menu mark string used for highlighting selected
11507a69bbfbSPeter Wemm       items
11517a69bbfbSPeter Wemm
11527a69bbfbSPeter Wemm   The  function  set_menu_format() allows you to set the maximum size of
11537a69bbfbSPeter Wemm   the viewport or menu page that will be used to display menu items. You
11547a69bbfbSPeter Wemm   can retrieve any format associated with a menu with menu_format(). The
11557a69bbfbSPeter Wemm   default format is rows=16, columns=1.
11567a69bbfbSPeter Wemm
11577a69bbfbSPeter Wemm   The actual menu page may be smaller than the format size. This depends
11587a69bbfbSPeter Wemm   on  the item number and size and whether O_ROWMAJOR is on. This option
11597a69bbfbSPeter Wemm   (on  by  default) causes menu items to be displayed in a `raster-scan'
11607a69bbfbSPeter Wemm   pattern, so that if more than one item will fit horizontally the first
11617a69bbfbSPeter Wemm   couple  of  items  are side-by-side in the top row. The alternative is
11627a69bbfbSPeter Wemm   column-major  display,  which  tries to put the first several items in
11637a69bbfbSPeter Wemm   the first column.
11647a69bbfbSPeter Wemm
11657a69bbfbSPeter Wemm   As  mentioned above, a menu format not large enough to allow all items
11667a69bbfbSPeter Wemm   to  fit  on-screen  will  result  in a menu display that is vertically
11677a69bbfbSPeter Wemm   scrollable.
11687a69bbfbSPeter Wemm
11697a69bbfbSPeter Wemm   You  can  scroll  it  with  requests to the menu driver, which will be
11707a69bbfbSPeter Wemm   described in the section on menu input handling.
11717a69bbfbSPeter Wemm
11727a69bbfbSPeter Wemm   Each  menu  has a mark string used to visually tag selected items; see
11737a69bbfbSPeter Wemm   the menu_mark(3x) manual page for details. The mark string length also
11747a69bbfbSPeter Wemm   influences the menu page size.
11757a69bbfbSPeter Wemm
11767a69bbfbSPeter Wemm   The  function  scale_menu()  returns the minimum display size that the
11777a69bbfbSPeter Wemm   menu  code  computes  from  all  these  factors.  There are other menu
11787a69bbfbSPeter Wemm   display  attributes  including  a  select  attribute, an attribute for
11797a69bbfbSPeter Wemm   selectable  items,  an  attribute  for  unselectable  items, and a pad
11807a69bbfbSPeter Wemm   character used to separate item name text from description text. These
11817a69bbfbSPeter Wemm   have  reasonable  defaults which the library allows you to change (see
11827a69bbfbSPeter Wemm   the menu_attribs(3x) manual page.
11837a69bbfbSPeter Wemm
11847a69bbfbSPeter WemmMenu Windows
11857a69bbfbSPeter Wemm
11867a69bbfbSPeter Wemm   Each  menu has, as mentioned previously, a pair of associated windows.
11877a69bbfbSPeter Wemm   Both these windows are painted when the menu is posted and erased when
11887a69bbfbSPeter Wemm   the menu is unposted.
11897a69bbfbSPeter Wemm
11907a69bbfbSPeter Wemm   The  outer  or  frame  window  is  not  otherwise  touched by the menu
11917a69bbfbSPeter Wemm   routines. It exists so the programmer can associate a title, a border,
11927a69bbfbSPeter Wemm   or  perhaps  help text with the menu and have it properly refreshed or
11937a69bbfbSPeter Wemm   erased at post/unpost time. The inner window or subwindow is where the
11947a69bbfbSPeter Wemm   current menu page is displayed.
11957a69bbfbSPeter Wemm
11967a69bbfbSPeter Wemm   By  default,  both  windows  are  stdscr.  You  can  set them with the
11977a69bbfbSPeter Wemm   functions in menu_win(3x).
11987a69bbfbSPeter Wemm
11994a1a9510SRong-En Fan   When  you  call post_menu(), you write the menu to its subwindow. When
12004a1a9510SRong-En Fan   you  call  unpost_menu(), you erase the subwindow, However, neither of
12017a69bbfbSPeter Wemm   these  actually  modifies  the  screen. To do that, call wrefresh() or
12027a69bbfbSPeter Wemm   some equivalent.
12037a69bbfbSPeter Wemm
12047a69bbfbSPeter WemmProcessing Menu Input
12057a69bbfbSPeter Wemm
12067a69bbfbSPeter Wemm   The  main  loop of your menu-processing code should call menu_driver()
12077a69bbfbSPeter Wemm   repeatedly.  The first argument of this routine is a menu pointer; the
12087a69bbfbSPeter Wemm   second  is  a  menu  command  code. You should write an input-fetching
12097a69bbfbSPeter Wemm   routine that maps input characters to menu command codes, and pass its
12107a69bbfbSPeter Wemm   output  to  menu_driver(). The menu command codes are fully documented
12117a69bbfbSPeter Wemm   in menu_driver(3x).
12127a69bbfbSPeter Wemm
12137a69bbfbSPeter Wemm   The  simplest  group of command codes is REQ_NEXT_ITEM, REQ_PREV_ITEM,
12147a69bbfbSPeter Wemm   REQ_FIRST_ITEM,     REQ_LAST_ITEM,     REQ_UP_ITEM,     REQ_DOWN_ITEM,
12157a69bbfbSPeter Wemm   REQ_LEFT_ITEM,  REQ_RIGHT_ITEM.  These  change  the currently selected
12167a69bbfbSPeter Wemm   item.  These  requests may cause scrolling of the menu page if it only
12177a69bbfbSPeter Wemm   partially displayed.
12187a69bbfbSPeter Wemm
12197a69bbfbSPeter Wemm   There  are  explicit  requests  for  scrolling  which  also change the
12207a69bbfbSPeter Wemm   current  item  (because  the  select location does not change, but the
12217a69bbfbSPeter Wemm   item    there   does).   These   are   REQ_SCR_DLINE,   REQ_SCR_ULINE,
12227a69bbfbSPeter Wemm   REQ_SCR_DPAGE, and REQ_SCR_UPAGE.
12237a69bbfbSPeter Wemm
12247a69bbfbSPeter Wemm   The  REQ_TOGGLE_ITEM  selects or deselects the current item. It is for
12257a69bbfbSPeter Wemm   use  in  multi-valued  menus; if you use it with O_ONEVALUE on, you'll
12267a69bbfbSPeter Wemm   get an error return (E_REQUEST_DENIED).
12277a69bbfbSPeter Wemm
12287a69bbfbSPeter Wemm   Each  menu  has  an associated pattern buffer. The menu_driver() logic
12297a69bbfbSPeter Wemm   tries  to  accumulate  printable  ASCII  characters  passed in in that
12307a69bbfbSPeter Wemm   buffer;  when  it  matches a prefix of an item name, that item (or the
12317a69bbfbSPeter Wemm   next  matching  item)  is selected. If appending a character yields no
12327a69bbfbSPeter Wemm   new  match,  that  character  is  deleted from the pattern buffer, and
12337a69bbfbSPeter Wemm   menu_driver() returns E_NO_MATCH.
12347a69bbfbSPeter Wemm
12357a69bbfbSPeter Wemm   Some  requests  change the pattern buffer directly: REQ_CLEAR_PATTERN,
12367a69bbfbSPeter Wemm   REQ_BACK_PATTERN,  REQ_NEXT_MATCH,  REQ_PREV_MATCH. The latter two are
12377a69bbfbSPeter Wemm   useful  when  pattern  buffer  input  matches  more than one item in a
12387a69bbfbSPeter Wemm   multi-valued menu.
12397a69bbfbSPeter Wemm
12407a69bbfbSPeter Wemm   Each  successful  scroll or item navigation request clears the pattern
12417a69bbfbSPeter Wemm   buffer.  It is also possible to set the pattern buffer explicitly with
12427a69bbfbSPeter Wemm   set_menu_pattern().
12437a69bbfbSPeter Wemm
12447a69bbfbSPeter Wemm   Finally,  menu  driver  requests  above  the  constant MAX_COMMAND are
12457a69bbfbSPeter Wemm   considered   application-specific  commands.  The  menu_driver()  code
12467a69bbfbSPeter Wemm   ignores them and returns E_UNKNOWN_COMMAND.
12477a69bbfbSPeter Wemm
12487a69bbfbSPeter WemmMiscellaneous Other Features
12497a69bbfbSPeter Wemm
12507a69bbfbSPeter Wemm   Various  menu  options can affect the processing and visual appearance
12517a69bbfbSPeter Wemm   and input processing of menus. See menu_opts(3x) for details.
12527a69bbfbSPeter Wemm
12537a69bbfbSPeter Wemm   It  is possible to change the current item from application code; this
12547a69bbfbSPeter Wemm   is  useful  if  you  want to write your own navigation requests. It is
12557a69bbfbSPeter Wemm   also  possible  to explicitly set the top row of the menu display. See
12567a69bbfbSPeter Wemm   mitem_current(3x).  If  your  application  needs  to  change  the menu
12577a69bbfbSPeter Wemm   subwindow  cursor for any reason, pos_menu_cursor() will restore it to
12587a69bbfbSPeter Wemm   the correct location for continuing menu driver processing.
12597a69bbfbSPeter Wemm
12607a69bbfbSPeter Wemm   It  is  possible  to set hooks to be called at menu initialization and
12617a69bbfbSPeter Wemm   wrapup   time,   and   whenever   the   selected   item  changes.  See
12627a69bbfbSPeter Wemm   menu_hook(3x).
12637a69bbfbSPeter Wemm
12647a69bbfbSPeter Wemm   Each  item, and each menu, has an associated user pointer on which you
12657a69bbfbSPeter Wemm   can hang application data. See mitem_userptr(3x) and menu_userptr(3x).
12667a69bbfbSPeter Wemm
12677a69bbfbSPeter Wemm                               The Forms Library
12687a69bbfbSPeter Wemm
12697a69bbfbSPeter Wemm   The  form library is a curses extension that supports easy programming
12707a69bbfbSPeter Wemm   of on-screen forms for data entry and program control.
12717a69bbfbSPeter Wemm
12727a69bbfbSPeter Wemm   The  form  library  first  appeared  in  AT&T  System  V.  The version
12737a69bbfbSPeter Wemm   documented here is the form code distributed with ncurses.
12747a69bbfbSPeter Wemm
12757a69bbfbSPeter WemmCompiling With the form Library
12767a69bbfbSPeter Wemm
12777a69bbfbSPeter Wemm   Your form-using modules must import the form library declarations with
12787a69bbfbSPeter Wemm          #include <form.h>
12797a69bbfbSPeter Wemm
12807a69bbfbSPeter Wemm   and  must  be linked explicitly with the forms library using an -lform
12817a69bbfbSPeter Wemm   argument.  Note  that  they  must  also  link the ncurses library with
12827a69bbfbSPeter Wemm   -lncurses. Many linkers are two-pass and will accept either order, but
12837a69bbfbSPeter Wemm   it is still good practice to put -lform first and -lncurses second.
12847a69bbfbSPeter Wemm
12857a69bbfbSPeter WemmOverview of Forms
12867a69bbfbSPeter Wemm
12877a69bbfbSPeter Wemm   A  form  is  a  collection of fields; each field may be either a label
12887a69bbfbSPeter Wemm   (explanatory  text)  or  a  data-entry  location.  Long  forms  may be
12897a69bbfbSPeter Wemm   segmented into pages; each entry to a new page clears the screen.
12907a69bbfbSPeter Wemm
12917a69bbfbSPeter Wemm   To  make forms, you create groups of fields and connect them with form
12927a69bbfbSPeter Wemm   frame objects; the form library makes this relatively simple.
12937a69bbfbSPeter Wemm
12947a69bbfbSPeter Wemm   Once  defined,  a form can be posted, that is written to an associated
12957a69bbfbSPeter Wemm   window.  Actually,  each form has two associated windows; a containing
12967a69bbfbSPeter Wemm   window  in  which the programmer can scribble titles or borders, and a
12977a69bbfbSPeter Wemm   subwindow in which the form fields proper are displayed.
12987a69bbfbSPeter Wemm
12997a69bbfbSPeter Wemm   As  the  form  user  fills out the posted form, navigation and editing
13007a69bbfbSPeter Wemm   keys  support  movement between fields, editing keys support modifying
13017a69bbfbSPeter Wemm   field,  and plain text adds to or changes data in a current field. The
13027a69bbfbSPeter Wemm   form  library  allows you (the forms designer) to bind each navigation
13037a69bbfbSPeter Wemm   and  editing  key  to any keystroke accepted by curses Fields may have
13047a69bbfbSPeter Wemm   validation  conditions on them, so that they check input data for type
13057a69bbfbSPeter Wemm   and  value.  The form library supplies a rich set of pre-defined field
13067a69bbfbSPeter Wemm   types, and makes it relatively easy to define new ones.
13077a69bbfbSPeter Wemm
13087a69bbfbSPeter Wemm   Once its transaction is completed (or aborted), a form may be unposted
13097a69bbfbSPeter Wemm   (that  is,  undisplayed),  and  finally  freed  to  make  the  storage
13107a69bbfbSPeter Wemm   associated with it and its items available for re-use.
13117a69bbfbSPeter Wemm
13127a69bbfbSPeter Wemm   The general flow of control of a form program looks like this:
13137a69bbfbSPeter Wemm    1. Initialize curses.
13147a69bbfbSPeter Wemm    2. Create the form fields, using new_field().
13157a69bbfbSPeter Wemm    3. Create the form using new_form().
13164a1a9510SRong-En Fan    4. Post the form using post_form().
13177a69bbfbSPeter Wemm    5. Refresh the screen.
13187a69bbfbSPeter Wemm    6. Process user requests via an input loop.
13194a1a9510SRong-En Fan    7. Unpost the form using unpost_form().
13207a69bbfbSPeter Wemm    8. Free the form, using free_form().
13217a69bbfbSPeter Wemm    9. Free the fields using free_field().
13227a69bbfbSPeter Wemm   10. Terminate curses.
13237a69bbfbSPeter Wemm
13247a69bbfbSPeter Wemm   Note  that  this  looks  much  like  a  menu program; the form library
13257a69bbfbSPeter Wemm   handles  tasks  which  are in many ways similar, and its interface was
13267a69bbfbSPeter Wemm   obviously  designed  to  resemble  that  of  the menu library wherever
13277a69bbfbSPeter Wemm   possible.
13287a69bbfbSPeter Wemm
13297a69bbfbSPeter Wemm   In  forms  programs,  however, the `process user requests' is somewhat
13307a69bbfbSPeter Wemm   more   complicated   than  for  menus.  Besides  menu-like  navigation
13317a69bbfbSPeter Wemm   operations, the menu driver loop has to support field editing and data
13327a69bbfbSPeter Wemm   validation.
13337a69bbfbSPeter Wemm
13347a69bbfbSPeter WemmCreating and Freeing Fields and Forms
13357a69bbfbSPeter Wemm
13367a69bbfbSPeter Wemm   The basic function for creating fields is new_field():
13377a69bbfbSPeter WemmFIELD *new_field(int height, int width,   /* new field size */
13387a69bbfbSPeter Wemm                 int top, int left,       /* upper left corner */
13397a69bbfbSPeter Wemm                 int offscreen,           /* number of offscreen rows */
13407a69bbfbSPeter Wemm                 int nbuf);               /* number of working buffers */
13417a69bbfbSPeter Wemm
13427a69bbfbSPeter Wemm   Menu  items  always  occupy  a  single  row, but forms fields may have
13437a69bbfbSPeter Wemm   multiple  rows.  So  new_field()  requires  you to specify a width and
13447a69bbfbSPeter Wemm   height  (the  first  two  arguments,  which  mist both be greater than
13457a69bbfbSPeter Wemm   zero).
13467a69bbfbSPeter Wemm
13477a69bbfbSPeter Wemm   You must also specify the location of the field's upper left corner on
13487a69bbfbSPeter Wemm   the  screen  (the  third  and  fourth arguments, which must be zero or
13497a69bbfbSPeter Wemm   greater).  Note  that  these  coordinates  are  relative  to  the form
13507a69bbfbSPeter Wemm   subwindow,  which will coincide with stdscr by default but need not be
13514a1a9510SRong-En Fan   stdscr if you've done an explicit set_form_win() call.
13527a69bbfbSPeter Wemm
13537a69bbfbSPeter Wemm   The  fifth argument allows you to specify a number of off-screen rows.
13547a69bbfbSPeter Wemm   If  this  is zero, the entire field will always be displayed. If it is
13557a69bbfbSPeter Wemm   nonzero,  the  form  will  be  scrollable,  with  only one screen-full
13567a69bbfbSPeter Wemm   (initially  the  top  part) displayed at any given time. If you make a
13577a69bbfbSPeter Wemm   field  dynamic and grow it so it will no longer fit on the screen, the
13587a69bbfbSPeter Wemm   form  will  become  scrollable  even  if  the  offscreen  argument was
13597a69bbfbSPeter Wemm   initially zero.
13607a69bbfbSPeter Wemm
13617a69bbfbSPeter Wemm   The  forms library allocates one working buffer per field; the size of
13627a69bbfbSPeter Wemm   each buffer is ((height + offscreen)*width + 1, one character for each
13637a69bbfbSPeter Wemm   position in the field plus a NUL terminator. The sixth argument is the
13647a69bbfbSPeter Wemm   number  of  additional  data  buffers  to allocate for the field; your
13657a69bbfbSPeter Wemm   application can use them for its own purposes.
13667a69bbfbSPeter WemmFIELD *dup_field(FIELD *field,            /* field to copy */
13677a69bbfbSPeter Wemm                 int top, int left);      /* location of new copy */
13687a69bbfbSPeter Wemm
13697a69bbfbSPeter Wemm   The  function  dup_field()  duplicates  an  existing  field  at  a new
13707a69bbfbSPeter Wemm   location.  Size  and  buffering information are copied; some attribute
13717a69bbfbSPeter Wemm   flags  and  status  bits  are  not  (see  the  form_field_new(3X)  for
13727a69bbfbSPeter Wemm   details).
13737a69bbfbSPeter WemmFIELD *link_field(FIELD *field,           /* field to copy */
13747a69bbfbSPeter Wemm                  int top, int left);     /* location of new copy */
13757a69bbfbSPeter Wemm
13767a69bbfbSPeter Wemm   The  function  link_field() also duplicates an existing field at a new
13777a69bbfbSPeter Wemm   location.  The difference from dup_field() is that it arranges for the
13787a69bbfbSPeter Wemm   new field's buffer to be shared with the old one.
13797a69bbfbSPeter Wemm
13807a69bbfbSPeter Wemm   Besides  the obvious use in making a field editable from two different
13817a69bbfbSPeter Wemm   form pages, linked fields give you a way to hack in dynamic labels. If
13827a69bbfbSPeter Wemm   you  declare  several fields linked to an original, and then make them
13837a69bbfbSPeter Wemm   inactive,  changes  from  the original will still be propagated to the
13847a69bbfbSPeter Wemm   linked fields.
13857a69bbfbSPeter Wemm
13867a69bbfbSPeter Wemm   As  with duplicated fields, linked fields have attribute bits separate
13877a69bbfbSPeter Wemm   from the original.
13887a69bbfbSPeter Wemm
13897a69bbfbSPeter Wemm   As  you  might  guess,  all these field-allocations return NULL if the
13907a69bbfbSPeter Wemm   field  allocation  is  not  possible  due to an out-of-memory error or
13917a69bbfbSPeter Wemm   out-of-bounds arguments.
13927a69bbfbSPeter Wemm
13937a69bbfbSPeter Wemm   To connect fields to a form, use
13947a69bbfbSPeter WemmFORM *new_form(FIELD **fields);
13957a69bbfbSPeter Wemm
13967a69bbfbSPeter Wemm   This  function  expects  to  see  a  NULL-terminated  array  of  field
13977a69bbfbSPeter Wemm   pointers.  Said fields are connected to a newly-allocated form object;
13987a69bbfbSPeter Wemm   its address is returned (or else NULL if the allocation fails).
13997a69bbfbSPeter Wemm
14007a69bbfbSPeter Wemm   Note  that  new_field()  does  not copy the pointer array into private
14017a69bbfbSPeter Wemm   storage;  if you modify the contents of the pointer array during forms
14027a69bbfbSPeter Wemm   processing,  all manner of bizarre things might happen. Also note that
14037a69bbfbSPeter Wemm   any given field may only be connected to one form.
14047a69bbfbSPeter Wemm
14057a69bbfbSPeter Wemm   The  functions  free_field() and free_form are available to free field
14067a69bbfbSPeter Wemm   and  form objects. It is an error to attempt to free a field connected
14077a69bbfbSPeter Wemm   to a form, but not vice-versa; thus, you will generally free your form
14087a69bbfbSPeter Wemm   objects first.
14097a69bbfbSPeter Wemm
14107a69bbfbSPeter WemmFetching and Changing Field Attributes
14117a69bbfbSPeter Wemm
14127a69bbfbSPeter Wemm   Each  form  field  has  a  number  of  location  and  size  attributes
14137a69bbfbSPeter Wemm   associated  with  it. There are other field attributes used to control
14147a69bbfbSPeter Wemm   display and editing of the field. Some (for example, the O_STATIC bit)
14157a69bbfbSPeter Wemm   involve  sufficient  complications  to be covered in sections of their
14167a69bbfbSPeter Wemm   own later on. We cover the functions used to get and set several basic
14177a69bbfbSPeter Wemm   attributes here.
14187a69bbfbSPeter Wemm
14197a69bbfbSPeter Wemm   When a field is created, the attributes not specified by the new_field
14207a69bbfbSPeter Wemm   function  are  copied  from  an  invisible  system  default  field. In
14217a69bbfbSPeter Wemm   attribute-setting  and -fetching functions, the argument NULL is taken
14227a69bbfbSPeter Wemm   to mean this field. Changes to it persist as defaults until your forms
14237a69bbfbSPeter Wemm   application terminates.
14247a69bbfbSPeter Wemm
14257a69bbfbSPeter Wemm  Fetching Size and Location Data
14267a69bbfbSPeter Wemm
14277a69bbfbSPeter Wemm   You can retrieve field sizes and locations through:
14287a69bbfbSPeter Wemmint field_info(FIELD *field,              /* field from which to fetch */
14297a69bbfbSPeter Wemm               int *height, *int width,   /* field size */
14307a69bbfbSPeter Wemm               int *top, int *left,       /* upper left corner */
14317a69bbfbSPeter Wemm               int *offscreen,            /* number of offscreen rows */
14327a69bbfbSPeter Wemm               int *nbuf);                /* number of working buffers */
14337a69bbfbSPeter Wemm
14347a69bbfbSPeter Wemm   This  function is a sort of inverse of new_field(); instead of setting
14357a69bbfbSPeter Wemm   size  and  location attributes of a new field, it fetches them from an
14367a69bbfbSPeter Wemm   existing one.
14377a69bbfbSPeter Wemm
14387a69bbfbSPeter Wemm  Changing the Field Location
14397a69bbfbSPeter Wemm
14407a69bbfbSPeter Wemm   It is possible to move a field's location on the screen:
14417a69bbfbSPeter Wemmint move_field(FIELD *field,              /* field to alter */
14427a69bbfbSPeter Wemm               int top, int left);        /* new upper-left corner */
14437a69bbfbSPeter Wemm
14447a69bbfbSPeter Wemm   You can, of course. query the current location through field_info().
14457a69bbfbSPeter Wemm
14467a69bbfbSPeter Wemm  The Justification Attribute
14477a69bbfbSPeter Wemm
14487a69bbfbSPeter Wemm   One-line  fields  may be unjustified, justified right, justified left,
14497a69bbfbSPeter Wemm   or centered. Here is how you manipulate this attribute:
14507a69bbfbSPeter Wemmint set_field_just(FIELD *field,          /* field to alter */
14517a69bbfbSPeter Wemm                   int justmode);         /* mode to set */
14527a69bbfbSPeter Wemm
14537a69bbfbSPeter Wemmint field_just(FIELD *field);             /* fetch mode of field */
14547a69bbfbSPeter Wemm
14557a69bbfbSPeter Wemm   The   mode   values  accepted  and  returned  by  this  functions  are
14567a69bbfbSPeter Wemm   preprocessor  macros NO_JUSTIFICATION, JUSTIFY_RIGHT, JUSTIFY_LEFT, or
14577a69bbfbSPeter Wemm   JUSTIFY_CENTER.
14587a69bbfbSPeter Wemm
14597a69bbfbSPeter Wemm  Field Display Attributes
14607a69bbfbSPeter Wemm
14617a69bbfbSPeter Wemm   For  each  field,  you  can  set  a  foreground  attribute for entered
14627a69bbfbSPeter Wemm   characters,  a  background  attribute  for the entire field, and a pad
14637a69bbfbSPeter Wemm   character  for the unfilled portion of the field. You can also control
14647a69bbfbSPeter Wemm   pagination of the form.
14657a69bbfbSPeter Wemm
14667a69bbfbSPeter Wemm   This  group of four field attributes controls the visual appearance of
14677a69bbfbSPeter Wemm   the  field on the screen, without affecting in any way the data in the
14687a69bbfbSPeter Wemm   field buffer.
14697a69bbfbSPeter Wemmint set_field_fore(FIELD *field,          /* field to alter */
14707a69bbfbSPeter Wemm                   chtype attr);          /* attribute to set */
14717a69bbfbSPeter Wemm
14727a69bbfbSPeter Wemmchtype field_fore(FIELD *field);          /* field to query */
14737a69bbfbSPeter Wemm
14747a69bbfbSPeter Wemmint set_field_back(FIELD *field,          /* field to alter */
14757a69bbfbSPeter Wemm                   chtype attr);          /* attribute to set */
14767a69bbfbSPeter Wemm
14777a69bbfbSPeter Wemmchtype field_back(FIELD *field);          /* field to query */
14787a69bbfbSPeter Wemm
14797a69bbfbSPeter Wemmint set_field_pad(FIELD *field,           /* field to alter */
14807a69bbfbSPeter Wemm                 int pad);                /* pad character to set */
14817a69bbfbSPeter Wemm
14827a69bbfbSPeter Wemmchtype field_pad(FIELD *field);
14837a69bbfbSPeter Wemm
14847a69bbfbSPeter Wemmint set_new_page(FIELD *field,            /* field to alter */
14857a69bbfbSPeter Wemm                 int flag);               /* TRUE to force new page */
14867a69bbfbSPeter Wemm
14877a69bbfbSPeter Wemmchtype new_page(FIELD *field);            /* field to query */
14887a69bbfbSPeter Wemm
14897a69bbfbSPeter Wemm   The attributes set and returned by the first four functions are normal
14907a69bbfbSPeter Wemm   curses(3x)  display  attribute  values  (A_STANDOUT, A_BOLD, A_REVERSE
14917a69bbfbSPeter Wemm   etc).  The page bit of a field controls whether it is displayed at the
14927a69bbfbSPeter Wemm   start of a new form screen.
14937a69bbfbSPeter Wemm
14947a69bbfbSPeter Wemm  Field Option Bits
14957a69bbfbSPeter Wemm
14967a69bbfbSPeter Wemm   There  is  also a large collection of field option bits you can set to
14977a69bbfbSPeter Wemm   control  various  aspects of forms processing. You can manipulate them
14987a69bbfbSPeter Wemm   with these functions:
14997a69bbfbSPeter Wemmint set_field_opts(FIELD *field,          /* field to alter */
15007a69bbfbSPeter Wemm                   int attr);             /* attribute to set */
15017a69bbfbSPeter Wemm
15027a69bbfbSPeter Wemmint field_opts_on(FIELD *field,           /* field to alter */
15037a69bbfbSPeter Wemm                  int attr);              /* attributes to turn on */
15047a69bbfbSPeter Wemm
15057a69bbfbSPeter Wemmint field_opts_off(FIELD *field,          /* field to alter */
15067a69bbfbSPeter Wemm                   int attr);             /* attributes to turn off */
15077a69bbfbSPeter Wemm
15087a69bbfbSPeter Wemmint field_opts(FIELD *field);             /* field to query */
15097a69bbfbSPeter Wemm
15107a69bbfbSPeter Wemm   By default, all options are on. Here are the available option bits:
15117a69bbfbSPeter Wemm
15127a69bbfbSPeter Wemm   O_VISIBLE
15137a69bbfbSPeter Wemm          Controls  whether  the  field  is visible on the screen. Can be
15147a69bbfbSPeter Wemm          used  during form processing to hide or pop up fields depending
15157a69bbfbSPeter Wemm          on the value of parent fields.
15167a69bbfbSPeter Wemm
15177a69bbfbSPeter Wemm   O_ACTIVE
15187a69bbfbSPeter Wemm          Controls  whether  the  field is active during forms processing
15197a69bbfbSPeter Wemm          (i.e.  visited  by  form  navigation keys). Can be used to make
15207a69bbfbSPeter Wemm          labels  or  derived  fields with buffer values alterable by the
15217a69bbfbSPeter Wemm          forms application, not the user.
15227a69bbfbSPeter Wemm
15237a69bbfbSPeter Wemm   O_PUBLIC
15247a69bbfbSPeter Wemm          Controls  whether data is displayed during field entry. If this
15257a69bbfbSPeter Wemm          option  is  turned  off on a field, the library will accept and
15267a69bbfbSPeter Wemm          edit  data  in that field, but it will not be displayed and the
15277a69bbfbSPeter Wemm          visible  field  cursor  will  not  move.  You  can turn off the
15287a69bbfbSPeter Wemm          O_PUBLIC bit to define password fields.
15297a69bbfbSPeter Wemm
15307a69bbfbSPeter Wemm   O_EDIT
15317a69bbfbSPeter Wemm          Controls  whether  the  field's data can be modified. When this
15327a69bbfbSPeter Wemm          option  is off, all editing requests except REQ_PREV_CHOICE and
15337a69bbfbSPeter Wemm          REQ_NEXT_CHOICE  will fail. Such read-only fields may be useful
15347a69bbfbSPeter Wemm          for help messages.
15357a69bbfbSPeter Wemm
15367a69bbfbSPeter Wemm   O_WRAP
15377a69bbfbSPeter Wemm          Controls word-wrapping in multi-line fields. Normally, when any
15387a69bbfbSPeter Wemm          character  of  a  (blank-separated) word reaches the end of the
15397a69bbfbSPeter Wemm          current  line,  the  entire  word  is  wrapped to the next line
15407a69bbfbSPeter Wemm          (assuming there is one). When this option is off, the word will
15417a69bbfbSPeter Wemm          be split across the line break.
15427a69bbfbSPeter Wemm
15437a69bbfbSPeter Wemm   O_BLANK
15447a69bbfbSPeter Wemm          Controls  field  blanking.  When  this option is on, entering a
15457a69bbfbSPeter Wemm          character  at  the first field position erases the entire field
15467a69bbfbSPeter Wemm          (except for the just-entered character).
15477a69bbfbSPeter Wemm
15487a69bbfbSPeter Wemm   O_AUTOSKIP
15497a69bbfbSPeter Wemm          Controls  automatic  skip  to  next  field when this one fills.
15507a69bbfbSPeter Wemm          Normally,  when  the  forms user tries to type more data into a
15517a69bbfbSPeter Wemm          field  than will fit, the editing location jumps to next field.
15527a69bbfbSPeter Wemm          When this option is off, the user's cursor will hang at the end
15537a69bbfbSPeter Wemm          of  the  field.  This  option is ignored in dynamic fields that
15547a69bbfbSPeter Wemm          have not reached their size limit.
15557a69bbfbSPeter Wemm
15567a69bbfbSPeter Wemm   O_NULLOK
15577a69bbfbSPeter Wemm          Controls   whether  validation  is  applied  to  blank  fields.
15587a69bbfbSPeter Wemm          Normally,  it  is not; the user can leave a field blank without
15597a69bbfbSPeter Wemm          invoking  the usual validation check on exit. If this option is
15607a69bbfbSPeter Wemm          off on a field, exit from it will invoke a validation check.
15617a69bbfbSPeter Wemm
15627a69bbfbSPeter Wemm   O_PASSOK
15637a69bbfbSPeter Wemm          Controls whether validation occurs on every exit, or only after
15647a69bbfbSPeter Wemm          the  field  is  modified.  Normally the latter is true. Setting
15657a69bbfbSPeter Wemm          O_PASSOK  may be useful if your field's validation function may
15667a69bbfbSPeter Wemm          change during forms processing.
15677a69bbfbSPeter Wemm
15687a69bbfbSPeter Wemm   O_STATIC
15697a69bbfbSPeter Wemm          Controls  whether the field is fixed to its initial dimensions.
15707a69bbfbSPeter Wemm          If  you  turn  this  off,  the  field  becomes dynamic and will
15717a69bbfbSPeter Wemm          stretch to fit entered data.
15727a69bbfbSPeter Wemm
15737a69bbfbSPeter Wemm   A  field's  options  cannot  be  changed  while the field is currently
15747a69bbfbSPeter Wemm   selected.  However,  options  may be changed on posted fields that are
15757a69bbfbSPeter Wemm   not current.
15767a69bbfbSPeter Wemm
15777a69bbfbSPeter Wemm   The option values are bit-masks and can be composed with logical-or in
15787a69bbfbSPeter Wemm   the obvious way.
15797a69bbfbSPeter Wemm
15807a69bbfbSPeter WemmField Status
15817a69bbfbSPeter Wemm
15827a69bbfbSPeter Wemm   Every field has a status flag, which is set to FALSE when the field is
15837a69bbfbSPeter Wemm   created  and  TRUE when the value in field buffer 0 changes. This flag
15847a69bbfbSPeter Wemm   can be queried and set directly:
15857a69bbfbSPeter Wemmint set_field_status(FIELD *field,      /* field to alter */
15867a69bbfbSPeter Wemm                   int status);         /* mode to set */
15877a69bbfbSPeter Wemm
15887a69bbfbSPeter Wemmint field_status(FIELD *field);         /* fetch mode of field */
15897a69bbfbSPeter Wemm
15907a69bbfbSPeter Wemm   Setting  this  flag under program control can be useful if you use the
15917a69bbfbSPeter Wemm   same form repeatedly, looking for modified fields each time.
15927a69bbfbSPeter Wemm
15937a69bbfbSPeter Wemm   Calling  field_status()  on  a  field not currently selected for input
15947a69bbfbSPeter Wemm   will return a correct value. Calling field_status() on a field that is
15957a69bbfbSPeter Wemm   currently  selected for input may not necessarily give a correct field
15967a69bbfbSPeter Wemm   status  value, because entered data isn't necessarily copied to buffer
15977a69bbfbSPeter Wemm   zero  before the exit validation check. To guarantee that the returned
15987a69bbfbSPeter Wemm   status  value  reflects reality, call field_status() either (1) in the
15997a69bbfbSPeter Wemm   field's  exit validation check routine, (2) from the field's or form's
16007a69bbfbSPeter Wemm   initialization   or   termination   hooks,   or   (3)   just  after  a
16017a69bbfbSPeter Wemm   REQ_VALIDATION request has been processed by the forms driver.
16027a69bbfbSPeter Wemm
16037a69bbfbSPeter WemmField User Pointer
16047a69bbfbSPeter Wemm
16057a69bbfbSPeter Wemm   Each  field  structure contains one character pointer slot that is not
16067a69bbfbSPeter Wemm   used  by  the forms library. It is intended to be used by applications
16077a69bbfbSPeter Wemm   to store private per-field data. You can manipulate it with:
16087a69bbfbSPeter Wemmint set_field_userptr(FIELD *field,       /* field to alter */
16097a69bbfbSPeter Wemm                   char *userptr);        /* mode to set */
16107a69bbfbSPeter Wemm
16117a69bbfbSPeter Wemmchar *field_userptr(FIELD *field);        /* fetch mode of field */
16127a69bbfbSPeter Wemm
16137a69bbfbSPeter Wemm   (Properly,  this  user  pointer field ought to have (void *) type. The
16147a69bbfbSPeter Wemm   (char *) type is retained for System V compatibility.)
16157a69bbfbSPeter Wemm
16167a69bbfbSPeter Wemm   It  is  valid  to  set  the  user pointer of the default field (with a
16177a69bbfbSPeter Wemm   set_field_userptr()  call  passed  a  NULL  field pointer.) When a new
16187a69bbfbSPeter Wemm   field  is  created,  the  default-field  user  pointer  is  copied  to
16197a69bbfbSPeter Wemm   initialize the new field's user pointer.
16207a69bbfbSPeter Wemm
16217a69bbfbSPeter WemmVariable-Sized Fields
16227a69bbfbSPeter Wemm
16237a69bbfbSPeter Wemm   Normally,  a  field  is fixed at the size specified for it at creation
16247a69bbfbSPeter Wemm   time.  If,  however, you turn off its O_STATIC bit, it becomes dynamic
16257a69bbfbSPeter Wemm   and  will  automatically  resize  itself  to accommodate data as it is
16267a69bbfbSPeter Wemm   entered.  If the field has extra buffers associated with it, they will
16277a69bbfbSPeter Wemm   grow right along with the main input buffer.
16287a69bbfbSPeter Wemm
16297a69bbfbSPeter Wemm   A  one-line  dynamic  field  will have a fixed height (1) but variable
16307a69bbfbSPeter Wemm   width, scrolling horizontally to display data within the field area as
16317a69bbfbSPeter Wemm   originally  dimensioned  and  located. A multi-line dynamic field will
16327a69bbfbSPeter Wemm   have  a  fixed  width, but variable height (number of rows), scrolling
16337a69bbfbSPeter Wemm   vertically  to  display  data  within  the  field  area  as originally
16347a69bbfbSPeter Wemm   dimensioned and located.
16357a69bbfbSPeter Wemm
16367a69bbfbSPeter Wemm   Normally,  a dynamic field is allowed to grow without limit. But it is
16377a69bbfbSPeter Wemm   possible  to set an upper limit on the size of a dynamic field. You do
16387a69bbfbSPeter Wemm   it with this function:
16397a69bbfbSPeter Wemmint set_max_field(FIELD *field,     /* field to alter (may not be NULL) */
16407a69bbfbSPeter Wemm                   int max_size);   /* upper limit on field size */
16417a69bbfbSPeter Wemm
16427a69bbfbSPeter Wemm   If the field is one-line, max_size is taken to be a column size limit;
16437a69bbfbSPeter Wemm   if  it  is multi-line, it is taken to be a line size limit. To disable
16447a69bbfbSPeter Wemm   any  limit,  use  an argument of zero. The growth limit can be changed
16457a69bbfbSPeter Wemm   whether or not the O_STATIC bit is on, but has no effect until it is.
16467a69bbfbSPeter Wemm
16477a69bbfbSPeter Wemm   The following properties of a field change when it becomes dynamic:
16487a69bbfbSPeter Wemm     * If  there  is  no  growth limit, there is no final position of the
16497a69bbfbSPeter Wemm       field; therefore O_AUTOSKIP and O_NL_OVERLOAD are ignored.
16507a69bbfbSPeter Wemm     * Field justification will be ignored (though whatever justification
16517a69bbfbSPeter Wemm       is set up will be retained internally and can be queried).
16527a69bbfbSPeter Wemm     * The  dup_field() and link_field() calls copy dynamic-buffer sizes.
16537a69bbfbSPeter Wemm       If  the  O_STATIC  option  is set on one of a collection of links,
16547a69bbfbSPeter Wemm       buffer  resizing  will occur only when the field is edited through
16557a69bbfbSPeter Wemm       that link.
16567a69bbfbSPeter Wemm     * The  call  field_info()  will retrieve the original static size of
16577a69bbfbSPeter Wemm       the  field;  use  dynamic_field_info()  to  get the actual dynamic
16587a69bbfbSPeter Wemm       size.
16597a69bbfbSPeter Wemm
16607a69bbfbSPeter WemmField Validation
16617a69bbfbSPeter Wemm
16627a69bbfbSPeter Wemm   By  default,  a  field will accept any data that will fit in its input
16637a69bbfbSPeter Wemm   buffer.  However,  it  is  possible  to  attach a validation type to a
16647a69bbfbSPeter Wemm   field.  If  you  do  this,  any  attempt  to  leave the field while it
16657a69bbfbSPeter Wemm   contains  data  that doesn't match the validation type will fail. Some
16667a69bbfbSPeter Wemm   validation  types also have a character-validity check for each time a
16677a69bbfbSPeter Wemm   character is entered in the field.
16687a69bbfbSPeter Wemm
16697a69bbfbSPeter Wemm   A   field's   validation   check   (if   any)   is   not  called  when
16707a69bbfbSPeter Wemm   set_field_buffer()  modifies the input buffer, nor when that buffer is
16717a69bbfbSPeter Wemm   changed through a linked field.
16727a69bbfbSPeter Wemm
16737a69bbfbSPeter Wemm   The  form library provides a rich set of pre-defined validation types,
16747a69bbfbSPeter Wemm   and  gives  you  the capability to define custom ones of your own. You
16757a69bbfbSPeter Wemm   can  examine and change field validation attributes with the following
16767a69bbfbSPeter Wemm   functions:
16777a69bbfbSPeter Wemmint set_field_type(FIELD *field,          /* field to alter */
16787a69bbfbSPeter Wemm                   FIELDTYPE *ftype,      /* type to associate */
16797a69bbfbSPeter Wemm                   ...);                  /* additional arguments*/
16807a69bbfbSPeter Wemm
16817a69bbfbSPeter WemmFIELDTYPE *field_type(FIELD *field);      /* field to query */
16827a69bbfbSPeter Wemm
16837a69bbfbSPeter Wemm   The  validation  type  of  a  field  is considered an attribute of the
16847a69bbfbSPeter Wemm   field.  As  with  other field attributes, Also, doing set_field_type()
16857a69bbfbSPeter Wemm   with  a  NULL  field  default  will  change  the  system  default  for
16867a69bbfbSPeter Wemm   validation of newly-created fields.
16877a69bbfbSPeter Wemm
16887a69bbfbSPeter Wemm   Here are the pre-defined validation types:
16897a69bbfbSPeter Wemm
16907a69bbfbSPeter Wemm  TYPE_ALPHA
16917a69bbfbSPeter Wemm
16927a69bbfbSPeter Wemm   This  field  type  accepts  alphabetic  data; no blanks, no digits, no
16937a69bbfbSPeter Wemm   special  characters  (this  is checked at character-entry time). It is
16947a69bbfbSPeter Wemm   set up with:
16957a69bbfbSPeter Wemmint set_field_type(FIELD *field,          /* field to alter */
16967a69bbfbSPeter Wemm                   TYPE_ALPHA,            /* type to associate */
16977a69bbfbSPeter Wemm                   int width);            /* maximum width of field */
16987a69bbfbSPeter Wemm
16997a69bbfbSPeter Wemm   The width argument sets a minimum width of data. Typically you'll want
17007a69bbfbSPeter Wemm   to  set this to the field width; if it's greater than the field width,
17017a69bbfbSPeter Wemm   the  validation  check will always fail. A minimum width of zero makes
17027a69bbfbSPeter Wemm   field completion optional.
17037a69bbfbSPeter Wemm
17047a69bbfbSPeter Wemm  TYPE_ALNUM
17057a69bbfbSPeter Wemm
17067a69bbfbSPeter Wemm   This  field  type  accepts  alphabetic  data and digits; no blanks, no
17077a69bbfbSPeter Wemm   special  characters  (this  is checked at character-entry time). It is
17087a69bbfbSPeter Wemm   set up with:
17097a69bbfbSPeter Wemmint set_field_type(FIELD *field,          /* field to alter */
17107a69bbfbSPeter Wemm                   TYPE_ALNUM,            /* type to associate */
17117a69bbfbSPeter Wemm                   int width);            /* maximum width of field */
17127a69bbfbSPeter Wemm
17137a69bbfbSPeter Wemm   The  width  argument sets a minimum width of data. As with TYPE_ALPHA,
17147a69bbfbSPeter Wemm   typically  you'll want to set this to the field width; if it's greater
17157a69bbfbSPeter Wemm   than the field width, the validation check will always fail. A minimum
17167a69bbfbSPeter Wemm   width of zero makes field completion optional.
17177a69bbfbSPeter Wemm
17187a69bbfbSPeter Wemm  TYPE_ENUM
17197a69bbfbSPeter Wemm
17207a69bbfbSPeter Wemm   This  type  allows  you  to  restrict  a  field's values to be among a
17217a69bbfbSPeter Wemm   specified  set  of  string  values (for example, the two-letter postal
17227a69bbfbSPeter Wemm   codes for U.S. states). It is set up with:
17237a69bbfbSPeter Wemmint set_field_type(FIELD *field,          /* field to alter */
17247a69bbfbSPeter Wemm                   TYPE_ENUM,             /* type to associate */
17257a69bbfbSPeter Wemm                   char **valuelist;      /* list of possible values */
17267a69bbfbSPeter Wemm                   int checkcase;         /* case-sensitive? */
17277a69bbfbSPeter Wemm                   int checkunique);      /* must specify uniquely? */
17287a69bbfbSPeter Wemm
17297a69bbfbSPeter Wemm   The  valuelist parameter must point at a NULL-terminated list of valid
17307a69bbfbSPeter Wemm   strings.  The  checkcase  argument, if true, makes comparison with the
17317a69bbfbSPeter Wemm   string case-sensitive.
17327a69bbfbSPeter Wemm
17337a69bbfbSPeter Wemm   When  the user exits a TYPE_ENUM field, the validation procedure tries
17347a69bbfbSPeter Wemm   to  complete  the  data  in the buffer to a valid entry. If a complete
17357a69bbfbSPeter Wemm   choice  string has been entered, it is of course valid. But it is also
17367a69bbfbSPeter Wemm   possible to enter a prefix of a valid string and have it completed for
17377a69bbfbSPeter Wemm   you.
17387a69bbfbSPeter Wemm
17397a69bbfbSPeter Wemm   By  default,  if  you enter such a prefix and it matches more than one
17407a69bbfbSPeter Wemm   value  in  the  string list, the prefix will be completed to the first
17417a69bbfbSPeter Wemm   matching value. But the checkunique argument, if true, requires prefix
17427a69bbfbSPeter Wemm   matches to be unique in order to be valid.
17437a69bbfbSPeter Wemm
17447a69bbfbSPeter Wemm   The   REQ_NEXT_CHOICE   and  REQ_PREV_CHOICE  input  requests  can  be
17457a69bbfbSPeter Wemm   particularly useful with these fields.
17467a69bbfbSPeter Wemm
17477a69bbfbSPeter Wemm  TYPE_INTEGER
17487a69bbfbSPeter Wemm
17497a69bbfbSPeter Wemm   This field type accepts an integer. It is set up as follows:
17507a69bbfbSPeter Wemmint set_field_type(FIELD *field,          /* field to alter */
17517a69bbfbSPeter Wemm                   TYPE_INTEGER,          /* type to associate */
17527a69bbfbSPeter Wemm                   int padding,           /* # places to zero-pad to */
17537a69bbfbSPeter Wemm                   int vmin, int vmax);   /* valid range */
17547a69bbfbSPeter Wemm
17557a69bbfbSPeter Wemm   Valid  characters consist of an optional leading minus and digits. The
17567a69bbfbSPeter Wemm   range check is performed on exit. If the range maximum is less than or
17577a69bbfbSPeter Wemm   equal to the minimum, the range is ignored.
17587a69bbfbSPeter Wemm
17597a69bbfbSPeter Wemm   If the value passes its range check, it is padded with as many leading
17607a69bbfbSPeter Wemm   zero digits as necessary to meet the padding argument.
17617a69bbfbSPeter Wemm
17627a69bbfbSPeter Wemm   A TYPE_INTEGER value buffer can conveniently be interpreted with the C
17637a69bbfbSPeter Wemm   library function atoi(3).
17647a69bbfbSPeter Wemm
17657a69bbfbSPeter Wemm  TYPE_NUMERIC
17667a69bbfbSPeter Wemm
17677a69bbfbSPeter Wemm   This field type accepts a decimal number. It is set up as follows:
17687a69bbfbSPeter Wemmint set_field_type(FIELD *field,              /* field to alter */
17697a69bbfbSPeter Wemm                   TYPE_NUMERIC,              /* type to associate */
17707a69bbfbSPeter Wemm                   int padding,               /* # places of precision */
17717a69bbfbSPeter Wemm                   double vmin, double vmax); /* valid range */
17727a69bbfbSPeter Wemm
17737a69bbfbSPeter Wemm   Valid  characters  consist  of  an  optional leading minus and digits.
17747a69bbfbSPeter Wemm   possibly  including a decimal point. If your system supports locale's,
17757a69bbfbSPeter Wemm   the  decimal  point  character  used  must  be the one defined by your
17767a69bbfbSPeter Wemm   locale.  The range check is performed on exit. If the range maximum is
17777a69bbfbSPeter Wemm   less than or equal to the minimum, the range is ignored.
17787a69bbfbSPeter Wemm
17797a69bbfbSPeter Wemm   If  the  value  passes  its  range  check,  it  is padded with as many
17807a69bbfbSPeter Wemm   trailing zero digits as necessary to meet the padding argument.
17817a69bbfbSPeter Wemm
17827a69bbfbSPeter Wemm   A TYPE_NUMERIC value buffer can conveniently be interpreted with the C
17837a69bbfbSPeter Wemm   library function atof(3).
17847a69bbfbSPeter Wemm
17857a69bbfbSPeter Wemm  TYPE_REGEXP
17867a69bbfbSPeter Wemm
17877a69bbfbSPeter Wemm   This  field type accepts data matching a regular expression. It is set
17887a69bbfbSPeter Wemm   up as follows:
17897a69bbfbSPeter Wemmint set_field_type(FIELD *field,          /* field to alter */
17907a69bbfbSPeter Wemm                   TYPE_REGEXP,           /* type to associate */
17917a69bbfbSPeter Wemm                   char *regexp);         /* expression to match */
17927a69bbfbSPeter Wemm
17937a69bbfbSPeter Wemm   The  syntax  for  regular expressions is that of regcomp(3). The check
17947a69bbfbSPeter Wemm   for regular-expression match is performed on exit.
17957a69bbfbSPeter Wemm
17967a69bbfbSPeter WemmDirect Field Buffer Manipulation
17977a69bbfbSPeter Wemm
17987a69bbfbSPeter Wemm   The chief attribute of a field is its buffer contents. When a form has
17997a69bbfbSPeter Wemm   been  completed,  your  application usually needs to know the state of
18007a69bbfbSPeter Wemm   each field buffer. You can find this out with:
18017a69bbfbSPeter Wemmchar *field_buffer(FIELD *field,          /* field to query */
18027a69bbfbSPeter Wemm                   int bufindex);         /* number of buffer to query */
18037a69bbfbSPeter Wemm
18047a69bbfbSPeter Wemm   Normally,  the state of the zero-numbered buffer for each field is set
18057a69bbfbSPeter Wemm   by  the user's editing actions on that field. It's sometimes useful to
18067a69bbfbSPeter Wemm   be  able  to set the value of the zero-numbered (or some other) buffer
18077a69bbfbSPeter Wemm   from your application:
18087a69bbfbSPeter Wemmint set_field_buffer(FIELD *field,        /* field to alter */
18097a69bbfbSPeter Wemm                   int bufindex,          /* number of buffer to alter */
18107a69bbfbSPeter Wemm                   char *value);          /* string value to set */
18117a69bbfbSPeter Wemm
18127a69bbfbSPeter Wemm   If  the  field  is  not  large  enough  and  cannot  be  resized  to a
18137a69bbfbSPeter Wemm   sufficiently large size to contain the specified value, the value will
18147a69bbfbSPeter Wemm   be truncated to fit.
18157a69bbfbSPeter Wemm
18167a69bbfbSPeter Wemm   Calling  field_buffer() with a null field pointer will raise an error.
18177a69bbfbSPeter Wemm   Calling  field_buffer()  on  a  field not currently selected for input
18187a69bbfbSPeter Wemm   will return a correct value. Calling field_buffer() on a field that is
18197a69bbfbSPeter Wemm   currently  selected for input may not necessarily give a correct field
18207a69bbfbSPeter Wemm   buffer  value, because entered data isn't necessarily copied to buffer
18217a69bbfbSPeter Wemm   zero  before the exit validation check. To guarantee that the returned
18227a69bbfbSPeter Wemm   buffer  value  reflects  on-screen reality, call field_buffer() either
18237a69bbfbSPeter Wemm   (1) in the field's exit validation check routine, (2) from the field's
18247a69bbfbSPeter Wemm   or  form's  initialization  or  termination hooks, or (3) just after a
18257a69bbfbSPeter Wemm   REQ_VALIDATION request has been processed by the forms driver.
18267a69bbfbSPeter Wemm
18277a69bbfbSPeter WemmAttributes of Forms
18287a69bbfbSPeter Wemm
18297a69bbfbSPeter Wemm   As  with  field  attributes,  form attributes inherit a default from a
18307a69bbfbSPeter Wemm   system default form structure. These defaults can be queried or set by
18317a69bbfbSPeter Wemm   of these functions using a form-pointer argument of NULL.
18327a69bbfbSPeter Wemm
18337a69bbfbSPeter Wemm   The principal attribute of a form is its field list. You can query and
18347a69bbfbSPeter Wemm   change this list with:
18357a69bbfbSPeter Wemmint set_form_fields(FORM *form,           /* form to alter */
18367a69bbfbSPeter Wemm                    FIELD **fields);      /* fields to connect */
18377a69bbfbSPeter Wemm
18387a69bbfbSPeter Wemmchar *form_fields(FORM *form);            /* fetch fields of form */
18397a69bbfbSPeter Wemm
18407a69bbfbSPeter Wemmint field_count(FORM *form);              /* count connect fields */
18417a69bbfbSPeter Wemm
18427a69bbfbSPeter Wemm   The  second  argument  of  set_form_fields()  may be a NULL-terminated
18437a69bbfbSPeter Wemm   field pointer array like the one required by new_form(). In that case,
18447a69bbfbSPeter Wemm   the  old  fields  of  the  form  are  disconnected  but not freed (and
18457a69bbfbSPeter Wemm   eligible  to  be  connected  to  other forms), then the new fields are
18467a69bbfbSPeter Wemm   connected.
18477a69bbfbSPeter Wemm
18487a69bbfbSPeter Wemm   It  may  also  be  null, in which case the old fields are disconnected
18497a69bbfbSPeter Wemm   (and not freed) but no new ones are connected.
18507a69bbfbSPeter Wemm
18517a69bbfbSPeter Wemm   The   field_count()  function  simply  counts  the  number  of  fields
18527a69bbfbSPeter Wemm   connected  to a given from. It returns -1 if the form-pointer argument
18537a69bbfbSPeter Wemm   is NULL.
18547a69bbfbSPeter Wemm
18557a69bbfbSPeter WemmControl of Form Display
18567a69bbfbSPeter Wemm
18577a69bbfbSPeter Wemm   In  the  overview section, you saw that to display a form you normally
18587a69bbfbSPeter Wemm   start  by  defining  its size (and fields), posting it, and refreshing
18597a69bbfbSPeter Wemm   the  screen.  There  is  an  hidden  step before posting, which is the
18607a69bbfbSPeter Wemm   association  of  the  form  with  a  frame window (actually, a pair of
18617a69bbfbSPeter Wemm   windows)  within  which  it  will  be displayed. By default, the forms
18627a69bbfbSPeter Wemm   library associates every form with the full-screen window stdscr.
18637a69bbfbSPeter Wemm
18647a69bbfbSPeter Wemm   By making this step explicit, you can associate a form with a declared
18657a69bbfbSPeter Wemm   frame window on your screen display. This can be useful if you want to
18667a69bbfbSPeter Wemm   adapt  the  form  display  to different screen sizes, dynamically tile
18677a69bbfbSPeter Wemm   forms  on  the  screen,  or  use a form as part of an interface layout
18687a69bbfbSPeter Wemm   managed by panels.
18697a69bbfbSPeter Wemm
18707a69bbfbSPeter Wemm   The  two  windows associated with each form have the same functions as
18717a69bbfbSPeter Wemm   their  analogues  in  the menu library. Both these windows are painted
18727a69bbfbSPeter Wemm   when the form is posted and erased when the form is unposted.
18737a69bbfbSPeter Wemm
18747a69bbfbSPeter Wemm   The  outer  or  frame  window  is  not  otherwise  touched by the form
18757a69bbfbSPeter Wemm   routines. It exists so the programmer can associate a title, a border,
18767a69bbfbSPeter Wemm   or  perhaps  help text with the form and have it properly refreshed or
18777a69bbfbSPeter Wemm   erased at post/unpost time. The inner window or subwindow is where the
18787a69bbfbSPeter Wemm   current form page is actually displayed.
18797a69bbfbSPeter Wemm
18807a69bbfbSPeter Wemm   In  order  to declare your own frame window for a form, you'll need to
18817a69bbfbSPeter Wemm   know  the  size  of  the  form's  bounding rectangle. You can get this
18827a69bbfbSPeter Wemm   information with:
18837a69bbfbSPeter Wemmint scale_form(FORM *form,                /* form to query */
18847a69bbfbSPeter Wemm               int *rows,                 /* form rows */
18857a69bbfbSPeter Wemm               int *cols);                /* form cols */
18867a69bbfbSPeter Wemm
18877a69bbfbSPeter Wemm   The form dimensions are passed back in the locations pointed to by the
18887a69bbfbSPeter Wemm   arguments.  Once  you have this information, you can use it to declare
18897a69bbfbSPeter Wemm   of windows, then use one of these functions:
18907a69bbfbSPeter Wemmint set_form_win(FORM *form,              /* form to alter */
18917a69bbfbSPeter Wemm                 WINDOW *win);            /* frame window to connect */
18927a69bbfbSPeter Wemm
18937a69bbfbSPeter WemmWINDOW *form_win(FORM *form);             /* fetch frame window of form */
18947a69bbfbSPeter Wemm
18957a69bbfbSPeter Wemmint set_form_sub(FORM *form,              /* form to alter */
18967a69bbfbSPeter Wemm                 WINDOW *win);            /* form subwindow to connect */
18977a69bbfbSPeter Wemm
18987a69bbfbSPeter WemmWINDOW *form_sub(FORM *form);             /* fetch form subwindow of form */
18997a69bbfbSPeter Wemm
19007a69bbfbSPeter Wemm   Note  that curses operations, including refresh(), on the form, should
19017a69bbfbSPeter Wemm   be done on the frame window, not the form subwindow.
19027a69bbfbSPeter Wemm
19037a69bbfbSPeter Wemm   It  is  possible  to  check  from  your  application  whether all of a
19047a69bbfbSPeter Wemm   scrollable  field is actually displayed within the menu subwindow. Use
19057a69bbfbSPeter Wemm   these functions:
19067a69bbfbSPeter Wemmint data_ahead(FORM *form);               /* form to be queried */
19077a69bbfbSPeter Wemm
19087a69bbfbSPeter Wemmint data_behind(FORM *form);              /* form to be queried */
19097a69bbfbSPeter Wemm
19107a69bbfbSPeter Wemm   The  function  data_ahead()  returns  TRUE if (a) the current field is
19117a69bbfbSPeter Wemm   one-line  and  has  undisplayed data off to the right, (b) the current
19127a69bbfbSPeter Wemm   field is multi-line and there is data off-screen below it.
19137a69bbfbSPeter Wemm
19147a69bbfbSPeter Wemm   The function data_behind() returns TRUE if the first (upper left hand)
19157a69bbfbSPeter Wemm   character position is off-screen (not being displayed).
19167a69bbfbSPeter Wemm
19177a69bbfbSPeter Wemm   Finally,  there  is  a function to restore the form window's cursor to
19187a69bbfbSPeter Wemm   the value expected by the forms driver:
19197a69bbfbSPeter Wemmint pos_form_cursor(FORM *)               /* form to be queried */
19207a69bbfbSPeter Wemm
19217a69bbfbSPeter Wemm   If your application changes the form window cursor, call this function
19227a69bbfbSPeter Wemm   before   handing  control  back  to  the  forms  driver  in  order  to
19237a69bbfbSPeter Wemm   re-synchronize it.
19247a69bbfbSPeter Wemm
19257a69bbfbSPeter WemmInput Processing in the Forms Driver
19267a69bbfbSPeter Wemm
19277a69bbfbSPeter Wemm   The function form_driver() handles virtualized input requests for form
19287a69bbfbSPeter Wemm   navigation, editing, and validation requests, just as menu_driver does
19297a69bbfbSPeter Wemm   for menus (see the section on menu input handling).
19307a69bbfbSPeter Wemmint form_driver(FORM *form,               /* form to pass input to */
19317a69bbfbSPeter Wemm                int request);             /* form request code */
19327a69bbfbSPeter Wemm
19337a69bbfbSPeter Wemm   Your  input  virtualization  function  needs  to  take  input and then
19347a69bbfbSPeter Wemm   convert  it  to  either an alphanumeric character (which is treated as
19357a69bbfbSPeter Wemm   data  to  be  entered  in  the  currently-selected  field), or a forms
19367a69bbfbSPeter Wemm   processing request.
19377a69bbfbSPeter Wemm
19387a69bbfbSPeter Wemm   The   forms   driver  provides  hooks  (through  input-validation  and
19397a69bbfbSPeter Wemm   field-termination  functions)  with  which  your  application code can
19407a69bbfbSPeter Wemm   check that the input taken by the driver matched what was expected.
19417a69bbfbSPeter Wemm
19427a69bbfbSPeter Wemm  Page Navigation Requests
19437a69bbfbSPeter Wemm
19447a69bbfbSPeter Wemm   These  requests  cause  page-level  moves through the form, triggering
19457a69bbfbSPeter Wemm   display of a new form screen.
19467a69bbfbSPeter Wemm
19477a69bbfbSPeter Wemm   REQ_NEXT_PAGE
19487a69bbfbSPeter Wemm          Move to the next form page.
19497a69bbfbSPeter Wemm
19507a69bbfbSPeter Wemm   REQ_PREV_PAGE
19517a69bbfbSPeter Wemm          Move to the previous form page.
19527a69bbfbSPeter Wemm
19537a69bbfbSPeter Wemm   REQ_FIRST_PAGE
19547a69bbfbSPeter Wemm          Move to the first form page.
19557a69bbfbSPeter Wemm
19567a69bbfbSPeter Wemm   REQ_LAST_PAGE
19577a69bbfbSPeter Wemm          Move to the last form page.
19587a69bbfbSPeter Wemm
19597a69bbfbSPeter Wemm   These  requests  treat the list as cyclic; that is, REQ_NEXT_PAGE from
19607a69bbfbSPeter Wemm   the last page goes to the first, and REQ_PREV_PAGE from the first page
19617a69bbfbSPeter Wemm   goes to the last.
19627a69bbfbSPeter Wemm
19637a69bbfbSPeter Wemm  Inter-Field Navigation Requests
19647a69bbfbSPeter Wemm
19657a69bbfbSPeter Wemm   These requests handle navigation between fields on the same page.
19667a69bbfbSPeter Wemm
19677a69bbfbSPeter Wemm   REQ_NEXT_FIELD
19687a69bbfbSPeter Wemm          Move to next field.
19697a69bbfbSPeter Wemm
19707a69bbfbSPeter Wemm   REQ_PREV_FIELD
19717a69bbfbSPeter Wemm          Move to previous field.
19727a69bbfbSPeter Wemm
19737a69bbfbSPeter Wemm   REQ_FIRST_FIELD
19747a69bbfbSPeter Wemm          Move to the first field.
19757a69bbfbSPeter Wemm
19767a69bbfbSPeter Wemm   REQ_LAST_FIELD
19777a69bbfbSPeter Wemm          Move to the last field.
19787a69bbfbSPeter Wemm
19797a69bbfbSPeter Wemm   REQ_SNEXT_FIELD
19807a69bbfbSPeter Wemm          Move to sorted next field.
19817a69bbfbSPeter Wemm
19827a69bbfbSPeter Wemm   REQ_SPREV_FIELD
19837a69bbfbSPeter Wemm          Move to sorted previous field.
19847a69bbfbSPeter Wemm
19857a69bbfbSPeter Wemm   REQ_SFIRST_FIELD
19867a69bbfbSPeter Wemm          Move to the sorted first field.
19877a69bbfbSPeter Wemm
19887a69bbfbSPeter Wemm   REQ_SLAST_FIELD
19897a69bbfbSPeter Wemm          Move to the sorted last field.
19907a69bbfbSPeter Wemm
19917a69bbfbSPeter Wemm   REQ_LEFT_FIELD
19927a69bbfbSPeter Wemm          Move left to field.
19937a69bbfbSPeter Wemm
19947a69bbfbSPeter Wemm   REQ_RIGHT_FIELD
19957a69bbfbSPeter Wemm          Move right to field.
19967a69bbfbSPeter Wemm
19977a69bbfbSPeter Wemm   REQ_UP_FIELD
19987a69bbfbSPeter Wemm          Move up to field.
19997a69bbfbSPeter Wemm
20007a69bbfbSPeter Wemm   REQ_DOWN_FIELD
20017a69bbfbSPeter Wemm          Move down to field.
20027a69bbfbSPeter Wemm
20037a69bbfbSPeter Wemm   These  requests treat the list of fields on a page as cyclic; that is,
20047a69bbfbSPeter Wemm   REQ_NEXT_FIELD   from   the   last   field  goes  to  the  first,  and
20057a69bbfbSPeter Wemm   REQ_PREV_FIELD from the first field goes to the last. The order of the
20067a69bbfbSPeter Wemm   fields for these (and the REQ_FIRST_FIELD and REQ_LAST_FIELD requests)
20077a69bbfbSPeter Wemm   is simply the order of the field pointers in the form array (as set up
20087a69bbfbSPeter Wemm   by new_form() or set_form_fields()
20097a69bbfbSPeter Wemm
20107a69bbfbSPeter Wemm   It  is also possible to traverse the fields as if they had been sorted
20117a69bbfbSPeter Wemm   in  screen-position  order,  so  the  sequence  goes left-to-right and
20127a69bbfbSPeter Wemm   top-to-bottom.   To   do   this,   use   the   second  group  of  four
20137a69bbfbSPeter Wemm   sorted-movement requests.
20147a69bbfbSPeter Wemm
20157a69bbfbSPeter Wemm   Finally, it is possible to move between fields using visual directions
20167a69bbfbSPeter Wemm   up,  down, right, and left. To accomplish this, use the third group of
20177a69bbfbSPeter Wemm   four requests. Note, however, that the position of a form for purposes
20187a69bbfbSPeter Wemm   of these requests is its upper-left corner.
20197a69bbfbSPeter Wemm
20207a69bbfbSPeter Wemm   For   example,  suppose  you  have  a  multi-line  field  B,  and  two
20217a69bbfbSPeter Wemm   single-line fields A and C on the same line with B, with A to the left
20227a69bbfbSPeter Wemm   of  B  and  C  to the right of B. A REQ_MOVE_RIGHT from A will go to B
20237a69bbfbSPeter Wemm   only  if  A, B, and C all share the same first line; otherwise it will
20247a69bbfbSPeter Wemm   skip over B to C.
20257a69bbfbSPeter Wemm
20267a69bbfbSPeter Wemm  Intra-Field Navigation Requests
20277a69bbfbSPeter Wemm
20287a69bbfbSPeter Wemm   These  requests drive movement of the edit cursor within the currently
20297a69bbfbSPeter Wemm   selected field.
20307a69bbfbSPeter Wemm
20317a69bbfbSPeter Wemm   REQ_NEXT_CHAR
20327a69bbfbSPeter Wemm          Move to next character.
20337a69bbfbSPeter Wemm
20347a69bbfbSPeter Wemm   REQ_PREV_CHAR
20357a69bbfbSPeter Wemm          Move to previous character.
20367a69bbfbSPeter Wemm
20377a69bbfbSPeter Wemm   REQ_NEXT_LINE
20387a69bbfbSPeter Wemm          Move to next line.
20397a69bbfbSPeter Wemm
20407a69bbfbSPeter Wemm   REQ_PREV_LINE
20417a69bbfbSPeter Wemm          Move to previous line.
20427a69bbfbSPeter Wemm
20437a69bbfbSPeter Wemm   REQ_NEXT_WORD
20447a69bbfbSPeter Wemm          Move to next word.
20457a69bbfbSPeter Wemm
20467a69bbfbSPeter Wemm   REQ_PREV_WORD
20477a69bbfbSPeter Wemm          Move to previous word.
20487a69bbfbSPeter Wemm
20497a69bbfbSPeter Wemm   REQ_BEG_FIELD
20507a69bbfbSPeter Wemm          Move to beginning of field.
20517a69bbfbSPeter Wemm
20527a69bbfbSPeter Wemm   REQ_END_FIELD
20537a69bbfbSPeter Wemm          Move to end of field.
20547a69bbfbSPeter Wemm
20557a69bbfbSPeter Wemm   REQ_BEG_LINE
20567a69bbfbSPeter Wemm          Move to beginning of line.
20577a69bbfbSPeter Wemm
20587a69bbfbSPeter Wemm   REQ_END_LINE
20597a69bbfbSPeter Wemm          Move to end of line.
20607a69bbfbSPeter Wemm
20617a69bbfbSPeter Wemm   REQ_LEFT_CHAR
20627a69bbfbSPeter Wemm          Move left in field.
20637a69bbfbSPeter Wemm
20647a69bbfbSPeter Wemm   REQ_RIGHT_CHAR
20657a69bbfbSPeter Wemm          Move right in field.
20667a69bbfbSPeter Wemm
20677a69bbfbSPeter Wemm   REQ_UP_CHAR
20687a69bbfbSPeter Wemm          Move up in field.
20697a69bbfbSPeter Wemm
20707a69bbfbSPeter Wemm   REQ_DOWN_CHAR
20717a69bbfbSPeter Wemm          Move down in field.
20727a69bbfbSPeter Wemm
20737a69bbfbSPeter Wemm   Each  word  is  separated  from  the  previous  and next characters by
20747a69bbfbSPeter Wemm   whitespace. The commands to move to beginning and end of line or field
20757a69bbfbSPeter Wemm   look for the first or last non-pad character in their ranges.
20767a69bbfbSPeter Wemm
20777a69bbfbSPeter Wemm  Scrolling Requests
20787a69bbfbSPeter Wemm
20797a69bbfbSPeter Wemm   Fields  that  are dynamic and have grown and fields explicitly created
20807a69bbfbSPeter Wemm   with   offscreen   rows   are   scrollable.   One-line  fields  scroll
20817a69bbfbSPeter Wemm   horizontally;  multi-line  fields scroll vertically. Most scrolling is
20827a69bbfbSPeter Wemm   triggered by editing and intra-field movement (the library scrolls the
20837a69bbfbSPeter Wemm   field  to  keep  the  cursor  visible).  It  is possible to explicitly
20847a69bbfbSPeter Wemm   request scrolling with the following requests:
20857a69bbfbSPeter Wemm
20867a69bbfbSPeter Wemm   REQ_SCR_FLINE
20877a69bbfbSPeter Wemm          Scroll vertically forward a line.
20887a69bbfbSPeter Wemm
20897a69bbfbSPeter Wemm   REQ_SCR_BLINE
20907a69bbfbSPeter Wemm          Scroll vertically backward a line.
20917a69bbfbSPeter Wemm
20927a69bbfbSPeter Wemm   REQ_SCR_FPAGE
20937a69bbfbSPeter Wemm          Scroll vertically forward a page.
20947a69bbfbSPeter Wemm
20957a69bbfbSPeter Wemm   REQ_SCR_BPAGE
20967a69bbfbSPeter Wemm          Scroll vertically backward a page.
20977a69bbfbSPeter Wemm
20987a69bbfbSPeter Wemm   REQ_SCR_FHPAGE
20997a69bbfbSPeter Wemm          Scroll vertically forward half a page.
21007a69bbfbSPeter Wemm
21017a69bbfbSPeter Wemm   REQ_SCR_BHPAGE
21027a69bbfbSPeter Wemm          Scroll vertically backward half a page.
21037a69bbfbSPeter Wemm
21047a69bbfbSPeter Wemm   REQ_SCR_FCHAR
21057a69bbfbSPeter Wemm          Scroll horizontally forward a character.
21067a69bbfbSPeter Wemm
21077a69bbfbSPeter Wemm   REQ_SCR_BCHAR
21087a69bbfbSPeter Wemm          Scroll horizontally backward a character.
21097a69bbfbSPeter Wemm
21107a69bbfbSPeter Wemm   REQ_SCR_HFLINE
21117a69bbfbSPeter Wemm          Scroll horizontally one field width forward.
21127a69bbfbSPeter Wemm
21137a69bbfbSPeter Wemm   REQ_SCR_HBLINE
21147a69bbfbSPeter Wemm          Scroll horizontally one field width backward.
21157a69bbfbSPeter Wemm
21167a69bbfbSPeter Wemm   REQ_SCR_HFHALF
21177a69bbfbSPeter Wemm          Scroll horizontally one half field width forward.
21187a69bbfbSPeter Wemm
21197a69bbfbSPeter Wemm   REQ_SCR_HBHALF
21207a69bbfbSPeter Wemm          Scroll horizontally one half field width backward.
21217a69bbfbSPeter Wemm
21227a69bbfbSPeter Wemm   For scrolling purposes, a page of a field is the height of its visible
21237a69bbfbSPeter Wemm   part.
21247a69bbfbSPeter Wemm
21257a69bbfbSPeter Wemm  Editing Requests
21267a69bbfbSPeter Wemm
21277a69bbfbSPeter Wemm   When  you pass the forms driver an ASCII character, it is treated as a
21287a69bbfbSPeter Wemm   request  to add the character to the field's data buffer. Whether this
21297a69bbfbSPeter Wemm   is  an  insertion  or  a  replacement depends on the field's edit mode
21307a69bbfbSPeter Wemm   (insertion is the default.
21317a69bbfbSPeter Wemm
21327a69bbfbSPeter Wemm   The following requests support editing the field and changing the edit
21337a69bbfbSPeter Wemm   mode:
21347a69bbfbSPeter Wemm
21357a69bbfbSPeter Wemm   REQ_INS_MODE
21367a69bbfbSPeter Wemm          Set insertion mode.
21377a69bbfbSPeter Wemm
21387a69bbfbSPeter Wemm   REQ_OVL_MODE
21397a69bbfbSPeter Wemm          Set overlay mode.
21407a69bbfbSPeter Wemm
21417a69bbfbSPeter Wemm   REQ_NEW_LINE
21427a69bbfbSPeter Wemm          New line request (see below for explanation).
21437a69bbfbSPeter Wemm
21447a69bbfbSPeter Wemm   REQ_INS_CHAR
21457a69bbfbSPeter Wemm          Insert space at character location.
21467a69bbfbSPeter Wemm
21477a69bbfbSPeter Wemm   REQ_INS_LINE
21487a69bbfbSPeter Wemm          Insert blank line at character location.
21497a69bbfbSPeter Wemm
21507a69bbfbSPeter Wemm   REQ_DEL_CHAR
21517a69bbfbSPeter Wemm          Delete character at cursor.
21527a69bbfbSPeter Wemm
21537a69bbfbSPeter Wemm   REQ_DEL_PREV
21547a69bbfbSPeter Wemm          Delete previous word at cursor.
21557a69bbfbSPeter Wemm
21567a69bbfbSPeter Wemm   REQ_DEL_LINE
21577a69bbfbSPeter Wemm          Delete line at cursor.
21587a69bbfbSPeter Wemm
21597a69bbfbSPeter Wemm   REQ_DEL_WORD
21607a69bbfbSPeter Wemm          Delete word at cursor.
21617a69bbfbSPeter Wemm
21627a69bbfbSPeter Wemm   REQ_CLR_EOL
21637a69bbfbSPeter Wemm          Clear to end of line.
21647a69bbfbSPeter Wemm
21657a69bbfbSPeter Wemm   REQ_CLR_EOF
21667a69bbfbSPeter Wemm          Clear to end of field.
21677a69bbfbSPeter Wemm
21687a69bbfbSPeter Wemm   REQ_CLEAR_FIELD
21697a69bbfbSPeter Wemm          Clear entire field.
21707a69bbfbSPeter Wemm
21717a69bbfbSPeter Wemm   The   behavior  of  the  REQ_NEW_LINE  and  REQ_DEL_PREV  requests  is
21727a69bbfbSPeter Wemm   complicated  and  partly  controlled  by  a pair of forms options. The
21737a69bbfbSPeter Wemm   special  cases  are triggered when the cursor is at the beginning of a
21747a69bbfbSPeter Wemm   field, or on the last line of the field.
21757a69bbfbSPeter Wemm
21767a69bbfbSPeter Wemm   First, we consider REQ_NEW_LINE:
21777a69bbfbSPeter Wemm
21787a69bbfbSPeter Wemm   The  normal  behavior  of  REQ_NEW_LINE in insert mode is to break the
21797a69bbfbSPeter Wemm   current line at the position of the edit cursor, inserting the portion
21807a69bbfbSPeter Wemm   of  the  current  line  after  the  cursor as a new line following the
21817a69bbfbSPeter Wemm   current  and  moving the cursor to the beginning of that new line (you
21827a69bbfbSPeter Wemm   may think of this as inserting a newline in the field buffer).
21837a69bbfbSPeter Wemm
21847a69bbfbSPeter Wemm   The  normal  behavior  of REQ_NEW_LINE in overlay mode is to clear the
21857a69bbfbSPeter Wemm   current  line from the position of the edit cursor to end of line. The
21867a69bbfbSPeter Wemm   cursor is then moved to the beginning of the next line.
21877a69bbfbSPeter Wemm
21887a69bbfbSPeter Wemm   However, REQ_NEW_LINE at the beginning of a field, or on the last line
21897a69bbfbSPeter Wemm   of  a  field,  instead  does a REQ_NEXT_FIELD. O_NL_OVERLOAD option is
21907a69bbfbSPeter Wemm   off, this special action is disabled.
21917a69bbfbSPeter Wemm
21927a69bbfbSPeter Wemm   Now, let us consider REQ_DEL_PREV:
21937a69bbfbSPeter Wemm
21947a69bbfbSPeter Wemm   The  normal  behavior  of  REQ_DEL_PREV  is  to  delete  the  previous
21957a69bbfbSPeter Wemm   character.  If  insert mode is on, and the cursor is at the start of a
21967a69bbfbSPeter Wemm   line,  and  the  text  on  that  line will fit on the previous one, it
21977a69bbfbSPeter Wemm   instead  appends  the contents of the current line to the previous one
21987a69bbfbSPeter Wemm   and  deletes  the  current  line  (you may think of this as deleting a
21997a69bbfbSPeter Wemm   newline from the field buffer).
22007a69bbfbSPeter Wemm
22017a69bbfbSPeter Wemm   However,  REQ_DEL_PREV  at the beginning of a field is instead treated
22027a69bbfbSPeter Wemm   as a REQ_PREV_FIELD.
22037a69bbfbSPeter Wemm
22047a69bbfbSPeter Wemm   If  the  O_BS_OVERLOAD  option is off, this special action is disabled
22057a69bbfbSPeter Wemm   and the forms driver just returns E_REQUEST_DENIED.
22067a69bbfbSPeter Wemm
22077a69bbfbSPeter Wemm   See  Form  Options for discussion of how to set and clear the overload
22087a69bbfbSPeter Wemm   options.
22097a69bbfbSPeter Wemm
22107a69bbfbSPeter Wemm  Order Requests
22117a69bbfbSPeter Wemm
22127a69bbfbSPeter Wemm   If the type of your field is ordered, and has associated functions for
22137a69bbfbSPeter Wemm   getting  the  next and previous values of the type from a given value,
22147a69bbfbSPeter Wemm   there are requests that can fetch that value into the field buffer:
22157a69bbfbSPeter Wemm
22167a69bbfbSPeter Wemm   REQ_NEXT_CHOICE
22177a69bbfbSPeter Wemm          Place the successor value of the current value in the buffer.
22187a69bbfbSPeter Wemm
22197a69bbfbSPeter Wemm   REQ_PREV_CHOICE
22207a69bbfbSPeter Wemm          Place the predecessor value of the current value in the buffer.
22217a69bbfbSPeter Wemm
22227a69bbfbSPeter Wemm   Of the built-in field types, only TYPE_ENUM has built-in successor and
22237a69bbfbSPeter Wemm   predecessor  functions.  When you define a field type of your own (see
22247a69bbfbSPeter Wemm   Custom   Validation   Types),  you  can  associate  our  own  ordering
22257a69bbfbSPeter Wemm   functions.
22267a69bbfbSPeter Wemm
22277a69bbfbSPeter Wemm  Application Commands
22287a69bbfbSPeter Wemm
22297a69bbfbSPeter Wemm   Form  requests  are  represented  as  integers  above the curses value
22307a69bbfbSPeter Wemm   greater   than  KEY_MAX  and  less  than  or  equal  to  the  constant
22317a69bbfbSPeter Wemm   MAX_COMMAND.  If  your  input-virtualization  routine  returns a value
22327a69bbfbSPeter Wemm   above MAX_COMMAND, the forms driver will ignore it.
22337a69bbfbSPeter Wemm
22347a69bbfbSPeter WemmField Change Hooks
22357a69bbfbSPeter Wemm
22367a69bbfbSPeter Wemm   It  is  possible  to  set  function  hooks to be executed whenever the
22377a69bbfbSPeter Wemm   current  field  or  form  changes. Here are the functions that support
22387a69bbfbSPeter Wemm   this:
22397a69bbfbSPeter Wemmtypedef void    (*HOOK)();       /* pointer to function returning void */
22407a69bbfbSPeter Wemm
22417a69bbfbSPeter Wemmint set_form_init(FORM *form,    /* form to alter */
22427a69bbfbSPeter Wemm                  HOOK hook);    /* initialization hook */
22437a69bbfbSPeter Wemm
22447a69bbfbSPeter WemmHOOK form_init(FORM *form);      /* form to query */
22457a69bbfbSPeter Wemm
22467a69bbfbSPeter Wemmint set_form_term(FORM *form,    /* form to alter */
22477a69bbfbSPeter Wemm                  HOOK hook);    /* termination hook */
22487a69bbfbSPeter Wemm
22497a69bbfbSPeter WemmHOOK form_term(FORM *form);      /* form to query */
22507a69bbfbSPeter Wemm
22517a69bbfbSPeter Wemmint set_field_init(FORM *form,   /* form to alter */
22527a69bbfbSPeter Wemm                  HOOK hook);    /* initialization hook */
22537a69bbfbSPeter Wemm
22547a69bbfbSPeter WemmHOOK field_init(FORM *form);     /* form to query */
22557a69bbfbSPeter Wemm
22567a69bbfbSPeter Wemmint set_field_term(FORM *form,   /* form to alter */
22577a69bbfbSPeter Wemm                  HOOK hook);    /* termination hook */
22587a69bbfbSPeter Wemm
22597a69bbfbSPeter WemmHOOK field_term(FORM *form);     /* form to query */
22607a69bbfbSPeter Wemm
22617a69bbfbSPeter Wemm   These functions allow you to either set or query four different hooks.
22627a69bbfbSPeter Wemm   In  each  of  the  set  functions,  the  second argument should be the
22637a69bbfbSPeter Wemm   address  of a hook function. These functions differ only in the timing
22647a69bbfbSPeter Wemm   of the hook call.
22657a69bbfbSPeter Wemm
22667a69bbfbSPeter Wemm   form_init
22677a69bbfbSPeter Wemm          This  hook  is called when the form is posted; also, just after
22687a69bbfbSPeter Wemm          each page change operation.
22697a69bbfbSPeter Wemm
22707a69bbfbSPeter Wemm   field_init
22717a69bbfbSPeter Wemm          This  hook  is called when the form is posted; also, just after
22727a69bbfbSPeter Wemm          each field change
22737a69bbfbSPeter Wemm
22747a69bbfbSPeter Wemm   field_term
22757a69bbfbSPeter Wemm          This  hook is called just after field validation; that is, just
22767a69bbfbSPeter Wemm          before the field is altered. It is also called when the form is
22777a69bbfbSPeter Wemm          unposted.
22787a69bbfbSPeter Wemm
22797a69bbfbSPeter Wemm   form_term
22807a69bbfbSPeter Wemm          This  hook  is  called  when  the  form is unposted; also, just
22817a69bbfbSPeter Wemm          before each page change operation.
22827a69bbfbSPeter Wemm
22837a69bbfbSPeter Wemm   Calls to these hooks may be triggered
22847a69bbfbSPeter Wemm    1. When user editing requests are processed by the forms driver
22857a69bbfbSPeter Wemm    2. When the current page is changed by set_current_field() call
22867a69bbfbSPeter Wemm    3. When the current field is changed by a set_form_page() call
22877a69bbfbSPeter Wemm
22887a69bbfbSPeter Wemm   See Field Change Commands for discussion of the latter two cases.
22897a69bbfbSPeter Wemm
22907a69bbfbSPeter Wemm   You  can  set  a default hook for all fields by passing one of the set
22917a69bbfbSPeter Wemm   functions a NULL first argument.
22927a69bbfbSPeter Wemm
22937a69bbfbSPeter Wemm   You  can  disable  any of these hooks by (re)setting them to NULL, the
22947a69bbfbSPeter Wemm   default value.
22957a69bbfbSPeter Wemm
22967a69bbfbSPeter WemmField Change Commands
22977a69bbfbSPeter Wemm
22987a69bbfbSPeter Wemm   Normally,  navigation  through  the  form will be driven by the user's
22997a69bbfbSPeter Wemm   input  requests.  But  sometimes  it  is useful to be able to move the
23007a69bbfbSPeter Wemm   focus  for  editing  and viewing under control of your application, or
23017a69bbfbSPeter Wemm   ask  which  field it currently is in. The following functions help you
23027a69bbfbSPeter Wemm   accomplish this:
23037a69bbfbSPeter Wemmint set_current_field(FORM *form,         /* form to alter */
23047a69bbfbSPeter Wemm                      FIELD *field);      /* field to shift to */
23057a69bbfbSPeter Wemm
23067a69bbfbSPeter WemmFIELD *current_field(FORM *form);         /* form to query */
23077a69bbfbSPeter Wemm
23087a69bbfbSPeter Wemmint field_index(FORM *form,               /* form to query */
23097a69bbfbSPeter Wemm                FIELD *field);            /* field to get index of */
23107a69bbfbSPeter Wemm
23117a69bbfbSPeter Wemm   The function field_index() returns the index of the given field in the
23127a69bbfbSPeter Wemm   given   form's   field  array  (the  array  passed  to  new_form()  or
23137a69bbfbSPeter Wemm   set_form_fields()).
23147a69bbfbSPeter Wemm
23157a69bbfbSPeter Wemm   The  initial  current field of a form is the first active field on the
23167a69bbfbSPeter Wemm   first page. The function set_form_fields() resets this.
23177a69bbfbSPeter Wemm
23187a69bbfbSPeter Wemm   It is also possible to move around by pages.
23197a69bbfbSPeter Wemmint set_form_page(FORM *form,             /* form to alter */
23207a69bbfbSPeter Wemm                  int page);              /* page to go to (0-origin) */
23217a69bbfbSPeter Wemm
23227a69bbfbSPeter Wemmint form_page(FORM *form);                /* return form's current page */
23237a69bbfbSPeter Wemm
23247a69bbfbSPeter Wemm   The   initial  page  of  a  newly-created  form  is  0.  The  function
23257a69bbfbSPeter Wemm   set_form_fields() resets this.
23267a69bbfbSPeter Wemm
23277a69bbfbSPeter WemmForm Options
23287a69bbfbSPeter Wemm
23297a69bbfbSPeter Wemm   Like  fields,  forms may have control option bits. They can be changed
23307a69bbfbSPeter Wemm   or queried with these functions:
23317a69bbfbSPeter Wemmint set_form_opts(FORM *form,             /* form to alter */
23327a69bbfbSPeter Wemm                  int attr);              /* attribute to set */
23337a69bbfbSPeter Wemm
23347a69bbfbSPeter Wemmint form_opts_on(FORM *form,              /* form to alter */
23357a69bbfbSPeter Wemm                 int attr);               /* attributes to turn on */
23367a69bbfbSPeter Wemm
23377a69bbfbSPeter Wemmint form_opts_off(FORM *form,             /* form to alter */
23387a69bbfbSPeter Wemm                  int attr);              /* attributes to turn off */
23397a69bbfbSPeter Wemm
23407a69bbfbSPeter Wemmint form_opts(FORM *form);                /* form to query */
23417a69bbfbSPeter Wemm
23427a69bbfbSPeter Wemm   By default, all options are on. Here are the available option bits:
23437a69bbfbSPeter Wemm
23447a69bbfbSPeter Wemm   O_NL_OVERLOAD
23457a69bbfbSPeter Wemm          Enable  overloading  of  REQ_NEW_LINE  as  described in Editing
23467a69bbfbSPeter Wemm          Requests. The value of this option is ignored on dynamic fields
23477a69bbfbSPeter Wemm          that  have  not  reached  their  size limit; these have no last
23487a69bbfbSPeter Wemm          line,  so  the  circumstances  for  triggering a REQ_NEXT_FIELD
23497a69bbfbSPeter Wemm          never arise.
23507a69bbfbSPeter Wemm
23517a69bbfbSPeter Wemm   O_BS_OVERLOAD
23527a69bbfbSPeter Wemm          Enable  overloading  of  REQ_DEL_PREV  as  described in Editing
23537a69bbfbSPeter Wemm          Requests.
23547a69bbfbSPeter Wemm
23557a69bbfbSPeter Wemm   The option values are bit-masks and can be composed with logical-or in
23567a69bbfbSPeter Wemm   the obvious way.
23577a69bbfbSPeter Wemm
23587a69bbfbSPeter WemmCustom Validation Types
23597a69bbfbSPeter Wemm
23607a69bbfbSPeter Wemm   The  form library gives you the capability to define custom validation
23617a69bbfbSPeter Wemm   types  of  your  own.  Further,  the  optional additional arguments of
23627a69bbfbSPeter Wemm   set_field_type effectively allow you to parameterize validation types.
23637a69bbfbSPeter Wemm   Most  of the complications in the validation-type interface have to do
23647a69bbfbSPeter Wemm   with the handling of the additional arguments within custom validation
23657a69bbfbSPeter Wemm   functions.
23667a69bbfbSPeter Wemm
23677a69bbfbSPeter Wemm  Union Types
23687a69bbfbSPeter Wemm
23697a69bbfbSPeter Wemm   The  simplest  way  to create a custom data type is to compose it from
23707a69bbfbSPeter Wemm   two preexisting ones:
23717a69bbfbSPeter WemmFIELD *link_fieldtype(FIELDTYPE *type1,
23727a69bbfbSPeter Wemm                      FIELDTYPE *type2);
23737a69bbfbSPeter Wemm
23747a69bbfbSPeter Wemm   This  function creates a field type that will accept any of the values
23757a69bbfbSPeter Wemm   legal  for  either  of  its  argument field types (which may be either
23767a69bbfbSPeter Wemm   predefined  or  programmer-defined).  If a set_field_type() call later
23777a69bbfbSPeter Wemm   requires  arguments,  the new composite type expects all arguments for
23787a69bbfbSPeter Wemm   the  first  type,  than  all arguments for the second. Order functions
23797a69bbfbSPeter Wemm   (see  Order Requests) associated with the component types will work on
23807a69bbfbSPeter Wemm   the  composite;  what it does is check the validation function for the
23817a69bbfbSPeter Wemm   first  type,  then  for  the  second,  to  figure what type the buffer
23827a69bbfbSPeter Wemm   contents should be treated as.
23837a69bbfbSPeter Wemm
23847a69bbfbSPeter Wemm  New Field Types
23857a69bbfbSPeter Wemm
23867a69bbfbSPeter Wemm   To  create  a field type from scratch, you need to specify one or both
23877a69bbfbSPeter Wemm   of the following things:
23887a69bbfbSPeter Wemm     * A  character-validation function, to check each character as it is
23897a69bbfbSPeter Wemm       entered.
23907a69bbfbSPeter Wemm     * A field-validation function to be applied on exit from the field.
23917a69bbfbSPeter Wemm
23927a69bbfbSPeter Wemm   Here's how you do that:
23937a69bbfbSPeter Wemmtypedef int     (*HOOK)();       /* pointer to function returning int */
23947a69bbfbSPeter Wemm
23957a69bbfbSPeter WemmFIELDTYPE *new_fieldtype(HOOK f_validate, /* field validator */
23967a69bbfbSPeter Wemm                         HOOK c_validate) /* character validator */
23977a69bbfbSPeter Wemm
23987a69bbfbSPeter Wemm
23997a69bbfbSPeter Wemmint free_fieldtype(FIELDTYPE *ftype);     /* type to free */
24007a69bbfbSPeter Wemm
24017a69bbfbSPeter Wemm   At least one of the arguments of new_fieldtype() must be non-NULL. The
24027a69bbfbSPeter Wemm   forms  driver  will  automatically  call  the  new  type's  validation
24037a69bbfbSPeter Wemm   functions at appropriate points in processing a field of the new type.
24047a69bbfbSPeter Wemm
24057a69bbfbSPeter Wemm   The  function  free_fieldtype()  deallocates  the  argument fieldtype,
24067a69bbfbSPeter Wemm   freeing all storage associated with it.
24077a69bbfbSPeter Wemm
24087a69bbfbSPeter Wemm   Normally,  a field validator is called when the user attempts to leave
24097a69bbfbSPeter Wemm   the  field.  Its  first argument is a field pointer, from which it can
24107a69bbfbSPeter Wemm   get  to  field buffer 0 and test it. If the function returns TRUE, the
24117a69bbfbSPeter Wemm   operation  succeeds; if it returns FALSE, the edit cursor stays in the
24127a69bbfbSPeter Wemm   field.
24137a69bbfbSPeter Wemm
24147a69bbfbSPeter Wemm   A  character  validator  gets  the  character  passed  in  as  a first
24157a69bbfbSPeter Wemm   argument.  It  too should return TRUE if the character is valid, FALSE
24167a69bbfbSPeter Wemm   otherwise.
24177a69bbfbSPeter Wemm
24187a69bbfbSPeter Wemm  Validation Function Arguments
24197a69bbfbSPeter Wemm
24207a69bbfbSPeter Wemm   Your  field-  and  character-  validation  functions  will be passed a
24217a69bbfbSPeter Wemm   second  argument  as  well.  This  second argument is the address of a
24227a69bbfbSPeter Wemm   structure   (which   we'll   call  a  pile)  built  from  any  of  the
24237a69bbfbSPeter Wemm   field-type-specific  arguments  passed to set_field_type(). If no such
24247a69bbfbSPeter Wemm   arguments  are  defined for the field type, this pile pointer argument
24257a69bbfbSPeter Wemm   will be NULL.
24267a69bbfbSPeter Wemm
24277a69bbfbSPeter Wemm   In order to arrange for such arguments to be passed to your validation
24287a69bbfbSPeter Wemm   functions,  you  must  associate  a  small  set  of storage-management
24297a69bbfbSPeter Wemm   functions with the type. The forms driver will use these to synthesize
24307a69bbfbSPeter Wemm   a  pile from the trailing arguments of each set_field_type() argument,
24317a69bbfbSPeter Wemm   and a pointer to the pile will be passed to the validation functions.
24327a69bbfbSPeter Wemm
24337a69bbfbSPeter Wemm   Here is how you make the association:
24347a69bbfbSPeter Wemmtypedef char    *(*PTRHOOK)();    /* pointer to function returning (char *) */
24357a69bbfbSPeter Wemmtypedef void    (*VOIDHOOK)();    /* pointer to function returning void */
24367a69bbfbSPeter Wemm
24377a69bbfbSPeter Wemmint set_fieldtype_arg(FIELDTYPE *type,    /* type to alter */
24387a69bbfbSPeter Wemm                      PTRHOOK make_str,   /* make structure from args */
24397a69bbfbSPeter Wemm                      PTRHOOK copy_str,   /* make copy of structure */
24407a69bbfbSPeter Wemm                      VOIDHOOK free_str); /* free structure storage */
24417a69bbfbSPeter Wemm
24427a69bbfbSPeter Wemm   Here is how the storage-management hooks are used:
24437a69bbfbSPeter Wemm
24447a69bbfbSPeter Wemm   make_str
24457a69bbfbSPeter Wemm          This  function  is  called  by  set_field_type().  It  gets one
24467a69bbfbSPeter Wemm          argument,  a  va_list  of the type-specific arguments passed to
24477a69bbfbSPeter Wemm          set_field_type().  It is expected to return a pile pointer to a
24487a69bbfbSPeter Wemm          data structure that encapsulates those arguments.
24497a69bbfbSPeter Wemm
24507a69bbfbSPeter Wemm   copy_str
24517a69bbfbSPeter Wemm          This function is called by form library functions that allocate
24527a69bbfbSPeter Wemm          new  field  instances.  It  is expected to take a pile pointer,
24537a69bbfbSPeter Wemm          copy  the  pile to allocated storage, and return the address of
24547a69bbfbSPeter Wemm          the pile copy.
24557a69bbfbSPeter Wemm
24567a69bbfbSPeter Wemm   free_str
24577a69bbfbSPeter Wemm          This   function  is  called  by  field-  and  type-deallocation
24587a69bbfbSPeter Wemm          routines  in the library. It takes a pile pointer argument, and
24597a69bbfbSPeter Wemm          is expected to free the storage of that pile.
24607a69bbfbSPeter Wemm
24617a69bbfbSPeter Wemm   The  make_str  and  copy_str  functions  may  return  NULL  to  signal
24627a69bbfbSPeter Wemm   allocation  failure.  The  library  routines  will that call them will
24637a69bbfbSPeter Wemm   return  error  indication  when  this  happens.  Thus, your validation
24647a69bbfbSPeter Wemm   functions  should  never  see  a  NULL file pointer and need not check
24657a69bbfbSPeter Wemm   specially for it.
24667a69bbfbSPeter Wemm
24677a69bbfbSPeter Wemm  Order Functions For Custom Types
24687a69bbfbSPeter Wemm
24697a69bbfbSPeter Wemm   Some  custom  field  types are simply ordered in the same well-defined
24707a69bbfbSPeter Wemm   way  that  TYPE_ENUM  is.  For  such  types,  it is possible to define
24717a69bbfbSPeter Wemm   successor and predecessor functions to support the REQ_NEXT_CHOICE and
24727a69bbfbSPeter Wemm   REQ_PREV_CHOICE requests. Here's how:
24737a69bbfbSPeter Wemmtypedef int     (*INTHOOK)();     /* pointer to function returning int */
24747a69bbfbSPeter Wemm
24757a69bbfbSPeter Wemmint set_fieldtype_arg(FIELDTYPE *type,    /* type to alter */
24767a69bbfbSPeter Wemm                      INTHOOK succ,       /* get successor value */
24777a69bbfbSPeter Wemm                      INTHOOK pred);      /* get predecessor value */
24787a69bbfbSPeter Wemm
24797a69bbfbSPeter Wemm   The  successor  and  predecessor  arguments  will  each  be passed two
24807a69bbfbSPeter Wemm   arguments;  a field pointer, and a pile pointer (as for the validation
24817a69bbfbSPeter Wemm   functions).  They  are  expected to use the function field_buffer() to
24827a69bbfbSPeter Wemm   read  the current value, and set_field_buffer() on buffer 0 to set the
24837a69bbfbSPeter Wemm   next  or  previous  value.  Either  hook  may  return TRUE to indicate
24847a69bbfbSPeter Wemm   success  (a legal next or previous value was set) or FALSE to indicate
24857a69bbfbSPeter Wemm   failure.
24867a69bbfbSPeter Wemm
24877a69bbfbSPeter Wemm  Avoiding Problems
24887a69bbfbSPeter Wemm
24897a69bbfbSPeter Wemm   The  interface  for  defining  custom types is complicated and tricky.
24907a69bbfbSPeter Wemm   Rather  than attempting to create a custom type entirely from scratch,
24917a69bbfbSPeter Wemm   you  should start by studying the library source code for whichever of
24927a69bbfbSPeter Wemm   the pre-defined types seems to be closest to what you want.
24937a69bbfbSPeter Wemm
24947a69bbfbSPeter Wemm   Use  that code as a model, and evolve it towards what you really want.
24957a69bbfbSPeter Wemm   You  will avoid many problems and annoyances that way. The code in the
24967a69bbfbSPeter Wemm   ncurses  library  has  been  specifically  exempted  from  the package
24977a69bbfbSPeter Wemm   copyright to support this.
24987a69bbfbSPeter Wemm
24997a69bbfbSPeter Wemm   If  your  custom  type  defines  order  functions,  have  do something
25007a69bbfbSPeter Wemm   intuitive  with  a  blank  field.  A  useful convention is to make the
25017a69bbfbSPeter Wemm   successor   of  a  blank  field  the  types  minimum  value,  and  its
25027a69bbfbSPeter Wemm   predecessor the maximum.
2503