xref: /freebsd/contrib/ncurses/doc/html/ncurses-intro.html (revision 8d9900a313593adeeaae295b4aea982cb14cb8a5)
1<!--
2  $Id: ncurses-intro.html,v 1.57 2022/11/26 19:33:46 tom Exp $
3  ****************************************************************************
4  * Copyright 2019-2020,2022 Thomas E. Dickey                                *
5  * Copyright 2000-2013,2017 Free Software Foundation, Inc.                  *
6  *                                                                          *
7  * Permission is hereby granted, free of charge, to any person obtaining a  *
8  * copy of this software and associated documentation files (the            *
9  * "Software"), to deal in the Software without restriction, including      *
10  * without limitation the rights to use, copy, modify, merge, publish,      *
11  * distribute, distribute with modifications, sublicense, and/or sell       *
12  * copies of the Software, and to permit persons to whom the Software is    *
13  * furnished to do so, subject to the following conditions:                 *
14  *                                                                          *
15  * The above copyright notice and this permission notice shall be included  *
16  * in all copies or substantial portions of the Software.                   *
17  *                                                                          *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
21  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
22  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
23  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
24  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
25  *                                                                          *
26  * Except as contained in this notice, the name(s) of the above copyright   *
27  * holders shall not be used in advertising or otherwise to promote the     *
28  * sale, use or other dealings in this Software without prior written       *
29  * authorization.                                                           *
30  ****************************************************************************
31-->
32<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
33<html>
34<head>
35  <meta name="generator" content=
36  "HTML Tidy for HTML5 for Linux version 5.6.0">
37  <title>Writing Programs with NCURSES</title>
38  <link rel="author" href="mailto:bugs-ncurses@gnu.org">
39  <meta http-equiv="Content-Type" content=
40  "text/html; charset=us-ascii">
41</head>
42<body>
43  <h1 class="no-header">Writing Programs with NCURSES</h1>
44
45  <h2>Writing Programs with NCURSES</h2>
46
47  <blockquote>
48    by Eric S. Raymond and Zeyd M. Ben-Halim<br>
49    updates since release 1.9.9e by Thomas Dickey
50  </blockquote>
51
52  <div class="nav">
53    <h2>Contents</h2>
54
55    <ul>
56      <li>
57        <a href="#introduction">Introduction</a>
58        <ul>
59          <li><a href="#history">A Brief History of Curses</a></li>
60
61          <li><a href="#scope">Scope of This Document</a></li>
62
63          <li><a href="#terminology">Terminology</a></li>
64        </ul>
65      </li>
66
67      <li>
68        <a href="#curses">The Curses Library</a>
69        <ul>
70          <li>
71            <a href="#overview">An Overview of Curses</a>
72            <ul>
73              <li><a href="#compiling">Compiling Programs using
74              Curses</a></li>
75
76              <li><a href="#updating">Updating the Screen</a></li>
77
78              <li><a href="#stdscr">Standard Windows and Function
79              Naming Conventions</a></li>
80
81              <li><a href="#variables">Variables</a></li>
82            </ul>
83          </li>
84
85          <li>
86            <a href="#using">Using the Library</a>
87            <ul>
88              <li><a href="#starting">Starting up</a></li>
89
90              <li><a href="#output">Output</a></li>
91
92              <li><a href="#input">Input</a></li>
93
94              <li><a href="#formschars">Using Forms Characters</a></li>
95
96              <li><a href="#attributes">Character Attributes and
97              Color</a></li>
98
99              <li><a href="#mouse">Mouse Interfacing</a></li>
100
101              <li><a href="#finishing">Finishing Up</a></li>
102            </ul>
103          </li>
104
105          <li>
106            <a href="#functions">Function Descriptions</a>
107            <ul>
108              <li><a href="#init">Initialization and Wrapup</a></li>
109
110              <li><a href="#flush">Causing Output to the
111              Terminal</a></li>
112
113              <li><a href="#lowlevel">Low-Level Capability
114              Access</a></li>
115
116              <li><a href="#debugging">Debugging</a></li>
117            </ul>
118          </li>
119
120          <li>
121            <a href="#hints">Hints, Tips, and Tricks</a>
122            <ul>
123              <li><a href="#caution">Some Notes of Caution</a></li>
124
125              <li><a href="#leaving">Temporarily Leaving ncurses
126              Mode</a></li>
127
128              <li><a href="#xterm">Using <code>ncurses</code> under
129              <code>xterm</code></a></li>
130
131              <li><a href="#screens">Handling Multiple Terminal
132              Screens</a></li>
133
134              <li><a href="#testing">Testing for Terminal
135              Capabilities</a></li>
136
137              <li><a href="#tuning">Tuning for Speed</a></li>
138
139              <li><a href="#special">Special Features of
140              <code>ncurses</code></a></li>
141            </ul>
142          </li>
143
144          <li>
145            <a href="#compat">Compatibility with Older Versions</a>
146            <ul>
147              <li><a href="#refbug">Refresh of Overlapping
148              Windows</a></li>
149
150              <li><a href="#backbug">Background Erase</a></li>
151            </ul>
152          </li>
153
154          <li><a href="#xsifuncs">XSI Curses Conformance</a></li>
155        </ul>
156      </li>
157
158      <li>
159        <a href="#panels">The Panels Library</a>
160        <ul>
161          <li><a href="#pcompile">Compiling With the Panels
162          Library</a></li>
163
164          <li><a href="#poverview">Overview of Panels</a></li>
165
166          <li><a href="#pstdscr">Panels, Input, and the Standard
167          Screen</a></li>
168
169          <li><a href="#hiding">Hiding Panels</a></li>
170
171          <li><a href="#pmisc">Miscellaneous Other Facilities</a></li>
172        </ul>
173      </li>
174
175      <li>
176        <a href="#menu">The Menu Library</a>
177        <ul>
178          <li><a href="#mcompile">Compiling with the menu
179          Library</a></li>
180
181          <li><a href="#moverview">Overview of Menus</a></li>
182
183          <li><a href="#mselect">Selecting items</a></li>
184
185          <li><a href="#mdisplay">Menu Display</a></li>
186
187          <li><a href="#mwindows">Menu Windows</a></li>
188
189          <li><a href="#minput">Processing Menu Input</a></li>
190
191          <li><a href="#mmisc">Miscellaneous Other Features</a></li>
192        </ul>
193      </li>
194
195      <li>
196        <a href="#form">The Forms Library</a>
197        <ul>
198          <li><a href="#fcompile">Compiling with the forms
199          Library</a></li>
200
201          <li><a href="#foverview">Overview of Forms</a></li>
202
203          <li><a href="#fcreate">Creating and Freeing Fields and
204          Forms</a></li>
205
206          <li>
207            <a href="#fattributes">Fetching and Changing Field
208            Attributes</a>
209            <ul>
210              <li><a href="#fsizes">Fetching Size and Location
211              Data</a></li>
212
213              <li><a href="#flocation">Changing the Field
214              Location</a></li>
215
216              <li><a href="#fjust">The Justification Attribute</a></li>
217
218              <li><a href="#fdispatts">Field Display Attributes</a></li>
219
220              <li><a href="#foptions">Field Option Bits</a></li>
221
222              <li><a href="#fstatus">Field Status</a></li>
223
224              <li><a href="#fuser">Field User Pointer</a></li>
225            </ul>
226          </li>
227
228          <li><a href="#fdynamic">Variable-Sized Fields</a></li>
229
230          <li>
231            <a href="#fvalidation">Field Validation</a>
232            <ul>
233              <li><a href="#ftype_alpha">TYPE_ALPHA</a></li>
234
235              <li><a href="#ftype_alnum">TYPE_ALNUM</a></li>
236
237              <li><a href="#ftype_enum">TYPE_ENUM</a></li>
238
239              <li><a href="#ftype_integer">TYPE_INTEGER</a></li>
240
241              <li><a href="#ftype_numeric">TYPE_NUMERIC</a></li>
242
243              <li><a href="#ftype_regexp">TYPE_REGEXP</a></li>
244            </ul>
245          </li>
246
247          <li><a href="#fbuffer">Direct Field Buffer
248          Manipulation</a></li>
249
250          <li><a href="#formattrs">Attributes of Forms</a></li>
251
252          <li><a href="#fdisplay">Control of Form Display</a></li>
253
254          <li>
255            <a href="#fdriver">Input Processing in the Forms
256            Driver</a>
257            <ul>
258              <li><a href="#fpage">Page Navigation Requests</a></li>
259
260              <li><a href="#ffield">Inter-Field Navigation
261              Requests</a></li>
262
263              <li><a href="#fifield">Intra-Field Navigation
264              Requests</a></li>
265
266              <li><a href="#fscroll">Scrolling Requests</a></li>
267
268              <li><a href="#fedit">Field Editing Requests</a></li>
269
270              <li><a href="#forder">Order Requests</a></li>
271
272              <li><a href="#fappcmds">Application Commands</a></li>
273            </ul>
274          </li>
275
276          <li><a href="#fhooks">Field Change Hooks</a></li>
277
278          <li><a href="#ffocus">Field Change Commands</a></li>
279
280          <li><a href="#frmoptions">Form Options</a></li>
281
282          <li>
283            <a href="#fcustom">Custom Validation Types</a>
284            <ul>
285              <li><a href="#flinktypes">Union Types</a></li>
286
287              <li><a href="#fnewtypes">New Field Types</a></li>
288
289              <li><a href="#fcheckargs">Validation Function
290              Arguments</a></li>
291
292              <li><a href="#fcustorder">Order Functions For Custom
293              Types</a></li>
294
295              <li><a href="#fcustprobs">Avoiding Problems</a></li>
296            </ul>
297          </li>
298        </ul>
299      </li>
300    </ul>
301  </div>
302
303  <hr>
304
305  <h2><a name="introduction" id="introduction">Introduction</a></h2>
306
307  <p>This document is an introduction to programming with
308  <code>curses</code>. It is not an exhaustive reference for the
309  curses Application Programming Interface (API); that role is
310  filled by the <code>curses</code> manual pages. Rather, it is
311  intended to help C programmers ease into using the package.</p>
312
313  <p>This document is aimed at C applications programmers not yet
314  specifically familiar with ncurses. If you are already an
315  experienced <code>curses</code> programmer, you should
316  nevertheless read the sections on <a href="#mouse">Mouse
317  Interfacing</a>, <a href="#debugging">Debugging</a>, <a href=
318  "#compat">Compatibility with Older Versions</a>, and <a href=
319  "#hints">Hints, Tips, and Tricks</a>. These will bring you up to
320  speed on the special features and quirks of the
321  <code>ncurses</code> implementation. If you are not so
322  experienced, keep reading.</p>
323
324  <p>The <code>curses</code> package is a subroutine library for
325  terminal-independent screen-painting and input-event handling
326  which presents a high level screen model to the programmer,
327  hiding differences between terminal types and doing automatic
328  optimization of output to change one screen full of text into
329  another. <code>Curses</code> uses terminfo, which is a database
330  format that can describe the capabilities of thousands of
331  different terminals.</p>
332
333  <p>The <code>curses</code> API may seem something of an archaism
334  on UNIX desktops increasingly dominated by X, Motif, and Tcl/Tk.
335  Nevertheless, UNIX still supports tty lines and X supports
336  <em>xterm(1)</em>; the <code>curses</code> API has the advantage
337  of (a) back-portability to character-cell terminals, and (b)
338  simplicity. For an application that does not require bit-mapped
339  graphics and multiple fonts, an interface implementation using
340  <code>curses</code> will typically be a great deal simpler and
341  less expensive than one using an X toolkit.</p>
342
343  <h3><a name="history" id="history">A Brief History of Curses</a></h3>
344
345  <p>Historically, the first ancestor of <code>curses</code> was
346  the routines written to provide screen-handling for the
347  <code>vi</code> editor; these used the <code>termcap</code>
348  database facility (both released in 3BSD) for describing terminal
349  capabilities. These routines were abstracted into a documented
350  library and first released with the early BSD UNIX versions. All
351  of this work was done by students at the University of California
352  (Berkeley campus). The curses library was first published in
353  4.0BSD, a year after 3BSD (i.e., late 1980).</p>
354
355  <p>After graduation, one of those students went to work at
356  AT&amp;T Bell Labs, and made an improved <code>termcap</code>
357  library called <code>terminfo</code> (i.e.,
358  &ldquo;libterm&rdquo;), and adapted the curses library to use
359  this. That was subsequently released in System V Release 2 (early
360  1984). Thereafter, other developers added to the curses and
361  terminfo libraries. For instance, a student at Cornell University
362  wrote an improved terminfo library as well as a tool
363  (<code>tic</code>) to compile the terminal descriptions. As a
364  general rule, AT&amp;T did not identify the developers in the
365  source-code or documentation; the <code>tic</code> and
366  <code>infocmp</code> programs are the exceptions.</p>
367
368  <p>System V Release 3 (System III UNIX) from Bell Labs featured a
369  rewritten and much-improved <code>curses</code> library, along
370  with the <code>tic</code> program (late 1986).</p>
371
372  <p>To recap, terminfo is based on Berkeley's termcap database,
373  but contains a number of improvements and extensions.
374  Parameterized capabilities strings were introduced, making it
375  possible to describe multiple video attributes, and colors and to
376  handle far more unusual terminals than possible with termcap. In
377  the later AT&amp;T System V releases, <code>curses</code> evolved
378  to use more facilities and offer more capabilities, going far
379  beyond BSD curses in power and flexibility.</p>
380
381  <h3><a name="scope" id="scope">Scope of This Document</a></h3>
382
383  <p>This document describes <code>ncurses</code>, a free
384  implementation of the System V <code>curses</code> API with some
385  clearly marked extensions. It includes the following System V
386  curses features:</p>
387
388  <ul>
389    <li>Support for multiple screen highlights (BSD curses could
390    only handle one &ldquo;standout&rdquo; highlight, usually
391    reverse-video).</li>
392
393    <li>Support for line- and box-drawing using forms
394    characters.</li>
395
396    <li>Recognition of function keys on input.</li>
397
398    <li>Color support.</li>
399
400    <li>Support for pads (windows of larger than screen size on
401    which the screen or a subwindow defines a viewport).</li>
402  </ul>
403
404  <p>Also, this package makes use of the insert and delete line and
405  character features of terminals so equipped, and determines how
406  to optimally use these features with no help from the programmer.
407  It allows arbitrary combinations of video attributes to be
408  displayed, even on terminals that leave &ldquo;magic
409  cookies&rdquo; on the screen to mark changes in attributes.</p>
410
411  <p>The <code>ncurses</code> package can also capture and use
412  event reports from a mouse in some environments (notably, xterm
413  under the X window system). This document includes tips for using
414  the mouse.</p>
415
416  <p>The <code>ncurses</code> package was originated by Pavel
417  Curtis. The original maintainer of this package is <a href=
418  "mailto:zmbenhal@netcom.com">Zeyd Ben-Halim</a>
419  &lt;zmbenhal@netcom.com&gt;. <a href=
420  "mailto:esr@snark.thyrsus.com">Eric S. Raymond</a>
421  &lt;esr@snark.thyrsus.com&gt; wrote many of the new features in
422  versions after 1.8.1 and wrote most of this introduction.
423  J&uuml;rgen Pfeifer wrote all of the menu and forms code as well
424  as the <a href="http://www.adahome.com">Ada95</a> binding.
425  Ongoing work is being done by <a href=
426  "mailto:dickey@invisible-island.net">Thomas Dickey</a>
427  (maintainer). Contact the current maintainers at <a href=
428  "mailto:bug-ncurses@gnu.org">bug-ncurses@gnu.org</a>.</p>
429
430  <p>This document also describes the <a href="#panels">panels</a>
431  extension library, similarly modeled on the SVr4 panels facility.
432  This library allows you to associate backing store with each of a
433  stack or deck of overlapping windows, and provides operations for
434  moving windows around in the stack that change their visibility
435  in the natural way (handling window overlaps).</p>
436
437  <p>Finally, this document describes in detail the <a href=
438  "#menu">menus</a> and <a href="#form">forms</a> extension
439  libraries, also cloned from System V, which support easy
440  construction and sequences of menus and fill-in forms.</p>
441
442  <h3><a name="terminology" id="terminology">Terminology</a></h3>
443
444  <p>In this document, the following terminology is used with
445  reasonable consistency:</p>
446
447  <dl>
448    <dt>window</dt>
449
450    <dd>A data structure describing a sub-rectangle of the screen
451    (possibly the entire screen). You can write to a window as
452    though it were a miniature screen, scrolling independently of
453    other windows on the physical screen.</dd>
454
455    <dt>screens</dt>
456
457    <dd>A subset of windows which are as large as the terminal
458    screen, i.e., they start at the upper left hand corner and
459    encompass the lower right hand corner. One of these,
460    <code>stdscr</code>, is automatically provided for the
461    programmer.</dd>
462
463    <dt>terminal screen</dt>
464
465    <dd>The package's idea of what the terminal display currently
466    looks like, i.e., what the user sees now. This is a special
467    screen.</dd>
468  </dl>
469
470  <h2><a name="curses" id="curses">The Curses Library</a></h2>
471
472  <h3><a name="overview" id="overview">An Overview of Curses</a></h3>
473
474  <h4><a name="compiling" id="compiling">Compiling Programs using
475  Curses</a></h4>
476
477  <p>In order to use the library, it is necessary to have certain
478  types and variables defined. Therefore, the programmer must have
479  a line:</p>
480
481  <pre class="code-block">
482          #include &lt;curses.h&gt;
483</pre>
484  <p>at the top of the program source. The screen package uses the
485  Standard I/O library, so <code>&lt;curses.h&gt;</code> includes
486  <code>&lt;stdio.h&gt;</code>. <code>&lt;curses.h&gt;</code> also
487  includes <code>&lt;termios.h&gt;</code>,
488  <code>&lt;termio.h&gt;</code>, or <code>&lt;sgtty.h&gt;</code>
489  depending on your system. It is redundant (but harmless) for the
490  programmer to do these includes, too. In linking with
491  <code>curses</code> you need to have <code>-lncurses</code> in
492  your LDFLAGS or on the command line. There is no need for any
493  other libraries.</p>
494
495  <h4><a name="updating" id="updating">Updating the Screen</a></h4>
496
497  <p>In order to update the screen optimally, it is necessary for
498  the routines to know what the screen currently looks like and
499  what the programmer wants it to look like next. For this purpose,
500  a data type (structure) named WINDOW is defined which describes a
501  window image to the routines, including its starting position on
502  the screen (the (y, x) coordinates of the upper left hand corner)
503  and its size. One of these (called <code>curscr</code>, for
504  current screen) is a screen image of what the terminal currently
505  looks like. Another screen (called <code>stdscr</code>, for
506  standard screen) is provided by default to make changes on.</p>
507
508  <p>A window is a purely internal representation. It is used to
509  build and store a potential image of a portion of the terminal.
510  It does not bear any necessary relation to what is really on the
511  terminal screen; it is more like a scratchpad or write
512  buffer.</p>
513
514  <p>To make the section of physical screen corresponding to a
515  window reflect the contents of the window structure, the routine
516  <code>refresh()</code> (or <code>wrefresh()</code> if the window
517  is not <code>stdscr</code>) is called.</p>
518
519  <p>A given physical screen section may be within the scope of any
520  number of overlapping windows. Also, changes can be made to
521  windows in any order, without regard to motion efficiency. Then,
522  at will, the programmer can effectively say &ldquo;make it look
523  like this,&rdquo; and let the package implementation determine
524  the most efficient way to repaint the screen.</p>
525
526  <h4><a name="stdscr" id="stdscr">Standard Windows and Function
527  Naming Conventions</a></h4>
528
529  <p>As hinted above, the routines can use several windows, but two
530  are automatically given: <code>curscr</code>, which knows what
531  the terminal looks like, and <code>stdscr</code>, which is what
532  the programmer wants the terminal to look like next. The user
533  should never actually access <code>curscr</code> directly.
534  Changes should be made to through the API, and then the routine
535  <code>refresh()</code> (or <code>wrefresh()</code>) called.</p>
536
537  <p>Many functions are defined to use <code>stdscr</code> as a
538  default screen. For example, to add a character to
539  <code>stdscr</code>, one calls <code>addch()</code> with the
540  desired character as argument. To write to a different window.
541  use the routine <code>waddch()</code> (for
542  <strong>w</strong>indow-specific addch()) is provided. This
543  convention of prepending function names with a &ldquo;w&rdquo;
544  when they are to be applied to specific windows is consistent.
545  The only routines which do not follow it are those for which a
546  window must always be specified.</p>
547
548  <p>In order to move the current (y, x) coordinates from one point
549  to another, the routines <code>move()</code> and
550  <code>wmove()</code> are provided. However, it is often desirable
551  to first move and then perform some I/O operation. In order to
552  avoid clumsiness, most I/O routines can be preceded by the prefix
553  &ldquo;mv&rdquo; and the desired (y, x) coordinates prepended to
554  the arguments to the function. For example, the calls</p>
555
556  <pre class="code-block">
557          move(y, x);
558          addch(ch);
559</pre>
560  <p>can be replaced by</p>
561
562  <pre class="code-block">
563          mvaddch(y, x, ch);
564</pre>
565  <p>and</p>
566
567  <pre class="code-block">
568          wmove(win, y, x);
569          waddch(win, ch);
570</pre>
571  <p>can be replaced by</p>
572
573  <pre class="code-block">
574          mvwaddch(win, y, x, ch);
575</pre>
576  <p>Note that the window description pointer (win) comes before
577  the added (y, x) coordinates. If a function requires a window
578  pointer, it is always the first parameter passed.</p>
579
580  <h4><a name="variables" id="variables">Variables</a></h4>
581
582  <p>The <code>curses</code> library sets some variables describing
583  the terminal capabilities.</p>
584
585  <pre class="code-block">
586      type   name      description
587      ------------------------------------------------------------------
588      int    LINES     number of lines on the terminal
589      int    COLS      number of columns on the terminal
590</pre>
591  <p>The <code>curses.h</code> also introduces some
592  <code>#define</code> constants and types of general
593  usefulness:</p>
594
595  <dl>
596    <dt><code>bool</code>
597    </dt>
598
599    <dd>boolean type, actually a &ldquo;char&rdquo; (e.g.,
600    <code>bool doneit;</code>)</dd>
601
602    <dt><code>TRUE</code>
603    </dt>
604
605    <dd>boolean &ldquo;true&rdquo; flag (1).</dd>
606
607    <dt><code>FALSE</code>
608    </dt>
609
610    <dd>boolean &ldquo;false&rdquo; flag (0).</dd>
611
612    <dt><code>ERR</code>
613    </dt>
614
615    <dd>error flag returned by routines on a failure (-1).</dd>
616
617    <dt><code>OK</code>
618    </dt>
619
620    <dd>error flag returned by routines when things go right.</dd>
621  </dl>
622
623  <h3><a name="using" id="using">Using the Library</a></h3>
624
625  <p>Now we describe how to actually use the screen package. In it,
626  we assume all updating, reading, etc. is applied to
627  <code>stdscr</code>. These instructions will work on any window,
628  providing you change the function names and parameters as
629  mentioned above.</p>
630
631  <p>Here is a sample program to motivate the discussion:</p>
632
633  <pre class="code-block">
634#include &lt;stdlib.h&gt;
635#include &lt;curses.h&gt;
636#include &lt;signal.h&gt;
637
638static void finish(int sig);
639
640int
641main(int argc, char *argv[])
642{
643    int num = 0;
644
645    /* initialize your non-curses data structures here */
646
647    (void) signal(SIGINT, finish);      /* arrange interrupts to terminate */
648
649    (void) initscr();      /* initialize the curses library */
650    keypad(stdscr, TRUE);  /* enable keyboard mapping */
651    (void) nonl();         /* tell curses not to do NL-&gt;CR/NL on output */
652    (void) cbreak();       /* take input chars one at a time, no wait for \n */
653    (void) echo();         /* echo input - in color */
654
655    if (has_colors())
656    {
657        start_color();
658
659        /*
660         * Simple color assignment, often all we need.  Color pair 0 cannot
661         * be redefined.  This example uses the same value for the color
662         * pair as for the foreground color, though of course that is not
663         * necessary:
664         */
665        init_pair(1, COLOR_RED,     COLOR_BLACK);
666        init_pair(2, COLOR_GREEN,   COLOR_BLACK);
667        init_pair(3, COLOR_YELLOW,  COLOR_BLACK);
668        init_pair(4, COLOR_BLUE,    COLOR_BLACK);
669        init_pair(5, COLOR_CYAN,    COLOR_BLACK);
670        init_pair(6, COLOR_MAGENTA, COLOR_BLACK);
671        init_pair(7, COLOR_WHITE,   COLOR_BLACK);
672    }
673
674    for (;;)
675    {
676        int c = getch();     /* refresh, accept single keystroke of input */
677        attrset(COLOR_PAIR(num % 8));
678        num++;
679
680        /* process the command keystroke */
681    }
682
683    finish(0);               /* we are done */
684}
685
686static void finish(int sig)
687{
688    endwin();
689
690    /* do your non-curses wrapup here */
691
692    exit(0);
693}
694</pre>
695  <h4><a name="starting" id="starting">Starting up</a></h4>
696
697  <p>In order to use the screen package, the routines must know
698  about terminal characteristics, and the space for
699  <code>curscr</code> and <code>stdscr</code> must be allocated.
700  These function <code>initscr()</code> does both these things.
701  Since it must allocate space for the windows, it can overflow
702  memory when attempting to do so. On the rare occasions this
703  happens, <code>initscr()</code> will terminate the program with
704  an error message. <code>initscr()</code> must always be called
705  before any of the routines which affect windows are used. If it
706  is not, the program will core dump as soon as either
707  <code>curscr</code> or <code>stdscr</code> are referenced.
708  However, it is usually best to wait to call it until after you
709  are sure you will need it, like after checking for startup
710  errors. Terminal status changing routines like <code>nl()</code>
711  and <code>cbreak()</code> should be called after
712  <code>initscr()</code>.</p>
713
714  <p>Once the screen windows have been allocated, you can set them
715  up for your program. If you want to, say, allow a screen to
716  scroll, use <code>scrollok()</code>. If you want the cursor to be
717  left in place after the last change, use <code>leaveok()</code>.
718  If this is not done, <code>refresh()</code> will move the cursor
719  to the window's current (y, x) coordinates after updating it.</p>
720
721  <p>You can create new windows of your own using the functions
722  <code>newwin()</code>, <code>derwin()</code>, and
723  <code>subwin()</code>. The routine <code>delwin()</code> will
724  allow you to get rid of old windows. All the options described
725  above can be applied to any window.</p>
726
727  <h4><a name="output" id="output">Output</a></h4>
728
729  <p>Now that we have set things up, we will want to actually
730  update the terminal. The basic functions used to change what will
731  go on a window are <code>addch()</code> and <code>move()</code>.
732  <code>addch()</code> adds a character at the current (y, x)
733  coordinates. <code>move()</code> changes the current (y, x)
734  coordinates to whatever you want them to be. It returns
735  <code>ERR</code> if you try to move off the window. As mentioned
736  above, you can combine the two into <code>mvaddch()</code> to do
737  both things at once.</p>
738
739  <p>The other output functions, such as <code>addstr()</code> and
740  <code>printw()</code>, all call <code>addch()</code> to add
741  characters to the window.</p>
742
743  <p>After you have put on the window what you want there, when you
744  want the portion of the terminal covered by the window to be made
745  to look like it, you must call <code>refresh()</code>. In order
746  to optimize finding changes, <code>refresh()</code> assumes that
747  any part of the window not changed since the last
748  <code>refresh()</code> of that window has not been changed on the
749  terminal, i.e., that you have not refreshed a portion of the
750  terminal with an overlapping window. If this is not the case, the
751  routine <code>touchwin()</code> is provided to make it look like
752  the entire window has been changed, thus making
753  <code>refresh()</code> check the whole subsection of the terminal
754  for changes.</p>
755
756  <p>If you call <code>wrefresh()</code> with <code>curscr</code>
757  as its argument, it will make the screen look like
758  <code>curscr</code> thinks it looks like. This is useful for
759  implementing a command which would redraw the screen in case it
760  get messed up.</p>
761
762  <h4><a name="input" id="input">Input</a></h4>
763
764  <p>The complementary function to <code>addch()</code> is
765  <code>getch()</code> which, if echo is set, will call
766  <code>addch()</code> to echo the character. Since the screen
767  package needs to know what is on the terminal at all times, if
768  characters are to be echoed, the tty must be in raw or cbreak
769  mode. Since initially the terminal has echoing enabled and is in
770  ordinary &ldquo;cooked&rdquo; mode, one or the other has to
771  changed before calling <code>getch()</code>; otherwise, the
772  program's output will be unpredictable.</p>
773
774  <p>When you need to accept line-oriented input in a window, the
775  functions <code>wgetstr()</code> and friends are available. There
776  is even a <code>wscanw()</code> function that can do
777  <code>scanf()</code>(3)-style multi-field parsing on window
778  input. These pseudo-line-oriented functions turn on echoing while
779  they execute.</p>
780
781  <p>The example code above uses the call <code>keypad(stdscr,
782  TRUE)</code> to enable support for function-key mapping. With
783  this feature, the <code>getch()</code> code watches the input
784  stream for character sequences that correspond to arrow and
785  function keys. These sequences are returned as pseudo-character
786  values. The <code>#define</code> values returned are listed in
787  the <code>curses.h</code> The mapping from sequences to
788  <code>#define</code> values is determined by <code>key_</code>
789  capabilities in the terminal's terminfo entry.</p>
790
791  <h4><a name="formschars" id="formschars">Using Forms
792  Characters</a></h4>
793
794  <p>The <code>addch()</code> function (and some others, including
795  <code>box()</code> and <code>border()</code>) can accept some
796  pseudo-character arguments which are specially defined by
797  <code>ncurses</code>. These are <code>#define</code> values set
798  up in the <code>curses.h</code> header; see there for a complete
799  list (look for the prefix <code>ACS_</code>).</p>
800
801  <p>The most useful of the ACS defines are the forms-drawing
802  characters. You can use these to draw boxes and simple graphs on
803  the screen. If the terminal does not have such characters,
804  <code>curses.h</code> will map them to a recognizable (though
805  ugly) set of ASCII defaults.</p>
806
807  <h4><a name="attributes" id="attributes">Character Attributes and
808  Color</a></h4>
809
810  <p>The <code>ncurses</code> package supports screen highlights
811  including standout, reverse-video, underline, and blink. It also
812  supports color, which is treated as another kind of
813  highlight.</p>
814
815  <p>Highlights are encoded, internally, as high bits of the
816  pseudo-character type (<code>chtype</code>) that
817  <code>curses.h</code> uses to represent the contents of a screen
818  cell. See the <code>curses.h</code> header file for a complete
819  list of highlight mask values (look for the prefix
820  <code>A_</code>).</p>
821
822  <p>There are two ways to make highlights. One is to logical-or
823  the value of the highlights you want into the character argument
824  of an <code>addch()</code> call, or any other output call that
825  takes a <code>chtype</code> argument.</p>
826
827  <p>The other is to set the current-highlight value. This is
828  <em>logical-OR</em>ed with any highlight you specify the first
829  way. You do this with the functions <code>attron()</code>,
830  <code>attroff()</code>, and <code>attrset()</code>; see the
831  manual pages for details. Color is a special kind of highlight.
832  The package actually thinks in terms of color pairs, combinations
833  of foreground and background colors. The sample code above sets
834  up eight color pairs, all of the guaranteed-available colors on
835  black. Note that each color pair is, in effect, given the name of
836  its foreground color. Any other range of eight non-conflicting
837  values could have been used as the first arguments of the
838  <code>init_pair()</code> values.</p>
839
840  <p>Once you have done an <code>init_pair()</code> that creates
841  color-pair N, you can use <code>COLOR_PAIR(N)</code> as a
842  highlight that invokes that particular color combination. Note
843  that <code>COLOR_PAIR(N)</code>, for constant N, is itself a
844  compile-time constant and can be used in initializers.</p>
845
846  <h4><a name="mouse" id="mouse">Mouse Interfacing</a></h4>
847
848  <p>The <code>ncurses</code> library also provides a mouse
849  interface.</p>
850
851  <blockquote>
852    <strong>NOTE:</strong> this facility is specific to
853    <code>ncurses</code>, it is not part of either the XSI Curses
854    standard, nor of System V Release 4, nor BSD curses. System V
855    Release 4 curses contains code with similar interface
856    definitions, however it is not documented. Other than by
857    disassembling the library, we have no way to determine exactly
858    how that mouse code works. Thus, we recommend that you wrap
859    mouse-related code in an #ifdef using the feature macro
860    NCURSES_MOUSE_VERSION so it will not be compiled and linked on
861    non-ncurses systems.
862  </blockquote>
863
864  <p>Presently, mouse event reporting works in the following
865  environments:</p>
866
867  <ul>
868    <li>xterm and similar programs such as rxvt.</li>
869
870    <li>Linux console, when configured with <code>gpm</code>(1),
871    Alessandro Rubini's mouse server.</li>
872
873    <li>FreeBSD sysmouse (console)</li>
874
875    <li>OS/2 EMX</li>
876  </ul>
877
878  <p>The mouse interface is very simple. To activate it, you use
879  the function <code>mousemask()</code>, passing it as first
880  argument a bit-mask that specifies what kinds of events you want
881  your program to be able to see. It will return the bit-mask of
882  events that actually become visible, which may differ from the
883  argument if the mouse device is not capable of reporting some of
884  the event types you specify.</p>
885
886  <p>Once the mouse is active, your application's command loop
887  should watch for a return value of <code>KEY_MOUSE</code> from
888  <code>wgetch()</code>. When you see this, a mouse event report
889  has been queued. To pick it off the queue, use the function
890  <code>getmouse()</code> (you must do this before the next
891  <code>wgetch()</code>, otherwise another mouse event might come
892  in and make the first one inaccessible).</p>
893
894  <p>Each call to <code>getmouse()</code> fills a structure (the
895  address of which you will pass it) with mouse event data. The
896  event data includes zero-origin, screen-relative character-cell
897  coordinates of the mouse pointer. It also includes an event mask.
898  Bits in this mask will be set, corresponding to the event type
899  being reported.</p>
900
901  <p>The mouse structure contains two additional fields which may
902  be significant in the future as ncurses interfaces to new kinds
903  of pointing device. In addition to x and y coordinates, there is
904  a slot for a z coordinate; this might be useful with
905  touch-screens that can return a pressure or duration parameter.
906  There is also a device ID field, which could be used to
907  distinguish between multiple pointing devices.</p>
908
909  <p>The class of visible events may be changed at any time via
910  <code>mousemask()</code>. Events that can be reported include
911  presses, releases, single-, double- and triple-clicks (you can
912  set the maximum button-down time for clicks). If you do not make
913  clicks visible, they will be reported as press-release pairs. In
914  some environments, the event mask may include bits reporting the
915  state of shift, alt, and ctrl keys on the keyboard during the
916  event.</p>
917
918  <p>A function to check whether a mouse event fell within a given
919  window is also supplied. You can use this to see whether a given
920  window should consider a mouse event relevant to it.</p>
921
922  <p>Because mouse event reporting will not be available in all
923  environments, it would be unwise to build <code>ncurses</code>
924  applications that <em>require</em> the use of a mouse. Rather,
925  you should use the mouse as a shortcut for point-and-shoot
926  commands your application would normally accept from the
927  keyboard. Two of the test games in the <code>ncurses</code>
928  distribution (<code>bs</code> and <code>knight</code>) contain
929  code that illustrates how this can be done.</p>
930
931  <p>See the manual page <code>curs_mouse(3X)</code> for full
932  details of the mouse-interface functions.</p>
933
934  <h4><a name="finishing" id="finishing">Finishing Up</a></h4>
935
936  <p>In order to clean up after the <code>ncurses</code> routines,
937  the routine <code>endwin()</code> is provided. It restores tty
938  modes to what they were when <code>initscr()</code> was first
939  called, and moves the cursor down to the lower-left corner. Thus,
940  anytime after the call to initscr, <code>endwin()</code> should
941  be called before exiting.</p>
942
943  <h3><a name="functions" id="functions">Function Descriptions</a></h3>
944
945  <p>We describe the detailed behavior of some important curses
946  functions here, as a supplement to the manual page
947  descriptions.</p>
948
949  <h4><a name="init" id="init">Initialization and Wrapup</a></h4>
950
951  <dl>
952    <dt><code>initscr()</code>
953    </dt>
954
955    <dd>The first function called should almost always be
956    <code>initscr()</code>. This will determine the terminal type
957    and initialize curses data structures. <code>initscr()</code>
958    also arranges that the first call to <code>refresh()</code>
959    will clear the screen. If an error occurs a message is written
960    to standard error and the program exits. Otherwise it returns a
961    pointer to stdscr. A few functions may be called before initscr
962    (<code>slk_init()</code>, <code>filter()</code>,
963    <code>ripoffline()</code>, <code>use_env()</code>, and, if you
964    are using multiple terminals, <code>newterm()</code>.)</dd>
965
966    <dt><code>endwin()</code>
967    </dt>
968
969    <dd>Your program should always call <code>endwin()</code>
970    before exiting or shelling out of the program. This function
971    will restore tty modes, move the cursor to the lower left
972    corner of the screen, reset the terminal into the proper
973    non-visual mode. Calling <code>refresh()</code> or
974    <code>doupdate()</code> after a temporary escape from the
975    program will restore the ncurses screen from before the
976    escape.</dd>
977
978    <dt><code>newterm(type, ofp, ifp)</code>
979    </dt>
980
981    <dd>A program which outputs to more than one terminal should
982    use <code>newterm()</code> instead of <code>initscr()</code>.
983    <code>newterm()</code> should be called once for each terminal.
984    It returns a variable of type <code>SCREEN *</code> which
985    should be saved as a reference to that terminal. (NOTE: a
986    SCREEN variable is not a <em>screen</em> in the sense we are
987    describing in this introduction, but a collection of parameters
988    used to assist in optimizing the display.) The arguments are
989    the type of the terminal (a string) and <code>FILE</code>
990    pointers for the output and input of the terminal. If type is
991    NULL then the environment variable <code>$TERM</code> is used.
992    <code>endwin()</code> should called once at wrapup time for
993    each terminal opened using this function.</dd>
994
995    <dt><code>set_term(new)</code>
996    </dt>
997
998    <dd>This function is used to switch to a different terminal
999    previously opened by <code>newterm()</code>. The screen
1000    reference for the new terminal is passed as the parameter. The
1001    previous terminal is returned by the function. All other calls
1002    affect only the current terminal.</dd>
1003
1004    <dt><code>delscreen(sp)</code>
1005    </dt>
1006
1007    <dd>The inverse of <code>newterm()</code>; deallocates the data
1008    structures associated with a given <code>SCREEN</code>
1009    reference.</dd>
1010  </dl>
1011
1012  <h4><a name="flush" id="flush">Causing Output to the Terminal</a></h4>
1013
1014  <dl>
1015    <dt><code>refresh()</code> and <code>wrefresh(win)</code></dt>
1016
1017    <dd>These functions must be called to actually get any output
1018    on the terminal, as other routines merely manipulate data
1019    structures. <code>wrefresh()</code> copies the named window to
1020    the physical terminal screen, taking into account what is
1021    already there in order to do optimizations.
1022    <code>refresh()</code> does a refresh of <code>stdscr</code>.
1023    Unless <code>leaveok()</code> has been enabled, the physical
1024    cursor of the terminal is left at the location of the window's
1025    cursor.</dd>
1026
1027    <dt><code>doupdate()</code> and
1028    <code>wnoutrefresh(win)</code></dt>
1029
1030    <dd>These two functions allow multiple updates with more
1031    efficiency than wrefresh. To use them, it is important to
1032    understand how curses works. In addition to all the window
1033    structures, curses keeps two data structures representing the
1034    terminal screen: a physical screen, describing what is actually
1035    on the screen, and a virtual screen, describing what the
1036    programmer wants to have on the screen. wrefresh works by first
1037    copying the named window to the virtual screen
1038    (<code>wnoutrefresh()</code>), and then calling the routine to
1039    update the screen (<code>doupdate()</code>). If the programmer
1040    wishes to output several windows at once, a series of calls to
1041    <code>wrefresh</code> will result in alternating calls to
1042    <code>wnoutrefresh()</code> and <code>doupdate()</code>,
1043    causing several bursts of output to the screen. By calling
1044    <code>wnoutrefresh()</code> for each window, it is then
1045    possible to call <code>doupdate()</code> once, resulting in
1046    only one burst of output, with fewer total characters
1047    transmitted (this also avoids a visually annoying flicker at
1048    each update).</dd>
1049  </dl>
1050
1051  <h4><a name="lowlevel" id="lowlevel">Low-Level Capability
1052  Access</a></h4>
1053
1054  <dl>
1055    <dt><code>setupterm(term, filenum, errret)</code>
1056    </dt>
1057
1058    <dd>
1059      This routine is called to initialize a terminal's
1060      description, without setting up the curses screen structures
1061      or changing the tty-driver mode bits. <code>term</code> is
1062      the character string representing the name of the terminal
1063      being used. <code>filenum</code> is the UNIX file descriptor
1064      of the terminal to be used for output. <code>errret</code> is
1065      a pointer to an integer, in which a success or failure
1066      indication is returned. The values returned can be 1 (all is
1067      well), 0 (no such terminal), or -1 (some problem locating the
1068      terminfo database).
1069      <p>The value of <code>term</code> can be given as NULL, which
1070      will cause the value of <code>TERM</code> in the environment
1071      to be used. The <code>errret</code> pointer can also be given
1072      as NULL, meaning no error code is wanted. If
1073      <code>errret</code> is defaulted, and something goes wrong,
1074      <code>setupterm()</code> will print an appropriate error
1075      message and exit, rather than returning. Thus, a simple
1076      program can call setupterm(0, 1, 0) and not worry about
1077      initialization errors.</p>
1078
1079      <p>After the call to <code>setupterm()</code>, the global
1080      variable <code>cur_term</code> is set to point to the current
1081      structure of terminal capabilities. By calling
1082      <code>setupterm()</code> for each terminal, and saving and
1083      restoring <code>cur_term</code>, it is possible for a program
1084      to use two or more terminals at once.
1085      <code>Setupterm()</code> also stores the names section of the
1086      terminal description in the global character array
1087      <code>ttytype[]</code>. Subsequent calls to
1088      <code>setupterm()</code> will overwrite this array, so you
1089      will have to save it yourself if need be.</p>
1090    </dd>
1091  </dl>
1092
1093  <h4><a name="debugging" id="debugging">Debugging</a></h4>
1094
1095  <blockquote>
1096    <strong>NOTE:</strong> These functions are not part of the
1097    standard curses API!
1098  </blockquote>
1099
1100  <dl>
1101    <dt><code>trace()</code>
1102    </dt>
1103
1104    <dd>This function can be used to explicitly set a trace level.
1105    If the trace level is nonzero, execution of your program will
1106    generate a file called &ldquo;trace&rdquo; in the current
1107    working directory containing a report on the library's actions.
1108    Higher trace levels enable more detailed (and verbose)
1109    reporting -- see comments attached to <code>TRACE_</code>
1110    defines in the <code>curses.h</code> file for details. (It is
1111    also possible to set a trace level by assigning a trace level
1112    value to the environment variable
1113    <code>NCURSES_TRACE</code>).</dd>
1114
1115    <dt><code>_tracef()</code>
1116    </dt>
1117
1118    <dd>This function can be used to output your own debugging
1119    information. It is only available only if you link with
1120    -lncurses_g. It can be used the same way as
1121    <code>printf()</code>, only it outputs a newline after the end
1122    of arguments. The output goes to a file called
1123    <code>trace</code> in the current directory.</dd>
1124  </dl>
1125
1126  <p>Trace logs can be difficult to interpret due to the sheer
1127  volume of data dumped in them. There is a script called
1128  <strong>tracemunch</strong> included with the
1129  <code>ncurses</code> distribution that can alleviate this problem
1130  somewhat; it compacts long sequences of similar operations into
1131  more succinct single-line pseudo-operations. These pseudo-ops can
1132  be distinguished by the fact that they are named in capital
1133  letters.</p>
1134
1135  <h3><a name="hints" id="hints">Hints, Tips, and Tricks</a></h3>
1136
1137  <p>The <code>ncurses</code> manual pages are a complete reference
1138  for this library. In the remainder of this document, we discuss
1139  various useful methods that may not be obvious from the manual
1140  page descriptions.</p>
1141
1142  <h4><a name="caution" id="caution">Some Notes of Caution</a></h4>
1143
1144  <p>If you find yourself thinking you need to use
1145  <code>noraw()</code> or <code>nocbreak()</code>, think again and
1146  move carefully. It is probably better design to use
1147  <code>getstr()</code> or one of its relatives to simulate cooked
1148  mode. The <code>noraw()</code> and <code>nocbreak()</code>
1149  functions try to restore cooked mode, but they may end up
1150  clobbering some control bits set before you started your
1151  application. Also, they have always been poorly documented, and
1152  are likely to hurt your application's usability with other curses
1153  libraries.</p>
1154
1155  <p>Bear in mind that <code>refresh()</code> is a synonym for
1156  <code>wrefresh(stdscr)</code>. Do not try to mix use of
1157  <code>stdscr</code> with use of windows declared by
1158  <code>newwin()</code>; a <code>refresh()</code> call will blow
1159  them off the screen. The right way to handle this is to use
1160  <code>subwin()</code>, or not touch <code>stdscr</code> at all
1161  and tile your screen with declared windows which you then
1162  <code>wnoutrefresh()</code> somewhere in your program event loop,
1163  with a single <code>doupdate()</code> call to trigger actual
1164  repainting.</p>
1165
1166  <p>You are much less likely to run into problems if you design
1167  your screen layouts to use tiled rather than overlapping windows.
1168  Historically, curses support for overlapping windows has been
1169  weak, fragile, and poorly documented. The <code>ncurses</code>
1170  library is not yet an exception to this rule.</p>
1171
1172  <p>There is a panels library included in the <code>ncurses</code>
1173  distribution that does a pretty good job of strengthening the
1174  overlapping-windows facilities.</p>
1175
1176  <p>Try to avoid using the global variables LINES and COLS. Use
1177  <code>getmaxyx()</code> on the <code>stdscr</code> context
1178  instead. Reason: your code may be ported to run in an environment
1179  with window resizes, in which case several screens could be open
1180  with different sizes.</p>
1181
1182  <h4><a name="leaving" id="leaving">Temporarily Leaving NCURSES
1183  Mode</a></h4>
1184
1185  <p>Sometimes you will want to write a program that spends most of
1186  its time in screen mode, but occasionally returns to ordinary
1187  &ldquo;cooked&rdquo; mode. A common reason for this is to support
1188  shell-out. This behavior is simple to arrange in
1189  <code>ncurses</code>.</p>
1190
1191  <p>To leave <code>ncurses</code> mode, call <code>endwin()</code>
1192  as you would if you were intending to terminate the program. This
1193  will take the screen back to cooked mode; you can do your
1194  shell-out. When you want to return to <code>ncurses</code> mode,
1195  simply call <code>refresh()</code> or <code>doupdate()</code>.
1196  This will repaint the screen.</p>
1197
1198  <p>There is a boolean function, <code>isendwin()</code>, which
1199  code can use to test whether <code>ncurses</code> screen mode is
1200  active. It returns <code>TRUE</code> in the interval between an
1201  <code>endwin()</code> call and the following
1202  <code>refresh()</code>, <code>FALSE</code> otherwise.</p>
1203
1204  <p>Here is some sample code for shellout:</p>
1205
1206  <pre class="code-block">
1207    addstr("Shelling out...");
1208    def_prog_mode();           /* save current tty modes */
1209    endwin();                  /* restore original tty modes */
1210    system("sh");              /* run shell */
1211    addstr("returned.\n");     /* prepare return message */
1212    refresh();                 /* restore save modes, repaint screen */
1213</pre>
1214  <h4><a name="xterm" id="xterm">Using NCURSES under XTERM</a></h4>
1215
1216  <p>A resize operation in X sends <code>SIGWINCH</code> to the
1217  application running under xterm. The easiest way to handle
1218  <code>SIGWINCH</code> is to do an <code>endwin</code>, followed
1219  by an <code>refresh</code> and a screen repaint you code
1220  yourself. The <code>refresh</code> will pick up the new screen
1221  size from the xterm's environment.</p>
1222
1223  <p>That is the standard way, of course (it even works with some
1224  vendor's curses implementations). Its drawback is that it clears
1225  the screen to reinitialize the display, and does not resize
1226  subwindows which must be shrunk. <code>Ncurses</code> provides an
1227  extension which works better, the <code>resizeterm</code>
1228  function. That function ensures that all windows are limited to
1229  the new screen dimensions, and pads <code>stdscr</code> with
1230  blanks if the screen is larger.</p>
1231
1232  <p>The <code>ncurses</code> library provides a SIGWINCH signal
1233  handler, which pushes a <code>KEY_RESIZE</code> via the wgetch()
1234  calls. When <code>ncurses</code> returns that code, it calls
1235  <code>resizeterm</code> to update the size of the standard
1236  screen's window, repainting that (filling with blanks or
1237  truncating as needed). It also resizes other windows, but its
1238  effect may be less satisfactory because it cannot know how you
1239  want the screen re-painted. You will usually have to write
1240  special-purpose code to handle <code>KEY_RESIZE</code>
1241  yourself.</p>
1242
1243  <h4><a name="screens" id="screens">Handling Multiple Terminal
1244  Screens</a></h4>
1245
1246  <p>The <code>initscr()</code> function actually calls a function
1247  named <code>newterm()</code> to do most of its work. If you are
1248  writing a program that opens multiple terminals, use
1249  <code>newterm()</code> directly.</p>
1250
1251  <p>For each call, you will have to specify a terminal type and a
1252  pair of file pointers; each call will return a screen reference,
1253  and <code>stdscr</code> will be set to the last one allocated.
1254  You will switch between screens with the <code>set_term</code>
1255  call. Note that you will also have to call
1256  <code>def_shell_mode</code> and <code>def_prog_mode</code> on
1257  each tty yourself.</p>
1258
1259  <h4><a name="testing" id="testing">Testing for Terminal
1260  Capabilities</a></h4>
1261
1262  <p>Sometimes you may want to write programs that test for the
1263  presence of various capabilities before deciding whether to go
1264  into <code>ncurses</code> mode. An easy way to do this is to call
1265  <code>setupterm()</code>, then use the functions
1266  <code>tigetflag()</code>, <code>tigetnum()</code>, and
1267  <code>tigetstr()</code> to do your testing.</p>
1268
1269  <p>A particularly useful case of this often comes up when you
1270  want to test whether a given terminal type should be treated as
1271  &ldquo;smart&rdquo; (cursor-addressable) or &ldquo;stupid&rdquo;.
1272  The right way to test this is to see if the return value of
1273  <code>tigetstr("cup")</code> is non-NULL. Alternatively, you can
1274  include the <code>term.h</code> file and test the value of the
1275  macro <code>cursor_address</code>.</p>
1276
1277  <h4><a name="tuning" id="tuning">Tuning for Speed</a></h4>
1278
1279  <p>Use the <code>addchstr()</code> family of functions for fast
1280  screen-painting of text when you know the text does not contain
1281  any control characters. Try to make attribute changes infrequent
1282  on your screens. Do not use the <code>immedok()</code>
1283  option!</p>
1284
1285  <h4><a name="special" id="special">Special Features of
1286  NCURSES</a></h4>
1287
1288  <p>The <code>wresize()</code> function allows you to resize a
1289  window in place. The associated <code>resizeterm()</code>
1290  function simplifies the construction of <a href=
1291  "#xterm">SIGWINCH</a> handlers, for resizing all windows.</p>
1292
1293  <p>The <code>define_key()</code> function allows you to define at
1294  runtime function-key control sequences which are not in the
1295  terminal description. The <code>keyok()</code> function allows
1296  you to temporarily enable or disable interpretation of any
1297  function-key control sequence.</p>
1298
1299  <p>The <code>use_default_colors()</code> function allows you to
1300  construct applications which can use the terminal's default
1301  foreground and background colors as an additional "default"
1302  color. Several terminal emulators support this feature, which is
1303  based on ISO 6429.</p>
1304
1305  <p>Ncurses supports up 16 colors, unlike SVr4 curses which
1306  defines only 8. While most terminals which provide color allow
1307  only 8 colors, about a quarter (including XFree86 xterm) support
1308  16 colors.</p>
1309
1310  <h3><a name="compat" id="compat">Compatibility with Older
1311  Versions</a></h3>
1312
1313  <p>Despite our best efforts, there are some differences between
1314  <code>ncurses</code> and the (undocumented!) behavior of older
1315  curses implementations. These arise from ambiguities or omissions
1316  in the documentation of the API.</p>
1317
1318  <h4><a name="refbug" id="refbug">Refresh of Overlapping
1319  Windows</a></h4>
1320
1321  <p>If you define two windows A and B that overlap, and then
1322  alternately scribble on and refresh them, the changes made to the
1323  overlapping region under historic <code>curses</code> versions
1324  were often not documented precisely.</p>
1325
1326  <p>To understand why this is a problem, remember that screen
1327  updates are calculated between two representations of the
1328  <em>entire</em> display. The documentation says that when you
1329  refresh a window, it is first copied to the virtual screen, and
1330  then changes are calculated to update the physical screen (and
1331  applied to the terminal). But "copied to" is not very specific,
1332  and subtle differences in how copying works can produce different
1333  behaviors in the case where two overlapping windows are each
1334  being refreshed at unpredictable intervals.</p>
1335
1336  <p>What happens to the overlapping region depends on what
1337  <code>wnoutrefresh()</code> does with its argument -- what
1338  portions of the argument window it copies to the virtual screen.
1339  Some implementations do "change copy", copying down only
1340  locations in the window that have changed (or been marked changed
1341  with <code>wtouchln()</code> and friends). Some implementations
1342  do "entire copy", copying <em>all</em> window locations to the
1343  virtual screen whether or not they have changed.</p>
1344
1345  <p>The <code>ncurses</code> library itself has not always been
1346  consistent on this score. Due to a bug, versions 1.8.7 to 1.9.8a
1347  did entire copy. Versions 1.8.6 and older, and versions 1.9.9 and
1348  newer, do change copy.</p>
1349
1350  <p>For most commercial curses implementations, it is not
1351  documented and not known for sure (at least not to the
1352  <code>ncurses</code> maintainers) whether they do change copy or
1353  entire copy. We know that System V release 3 curses has logic in
1354  it that looks like an attempt to do change copy, but the
1355  surrounding logic and data representations are sufficiently
1356  complex, and our knowledge sufficiently indirect, that it is hard
1357  to know whether this is reliable. It is not clear what the SVr4
1358  documentation and XSI standard intend. The XSI Curses standard
1359  barely mentions wnoutrefresh(); the SVr4 documents seem to be
1360  describing entire-copy, but it is possible with some effort and
1361  straining to read them the other way.</p>
1362
1363  <p>It might therefore be unwise to rely on either behavior in
1364  programs that might have to be linked with other curses
1365  implementations. Instead, you can do an explicit
1366  <code>touchwin()</code> before the <code>wnoutrefresh()</code>
1367  call to guarantee an entire-contents copy anywhere.</p>
1368
1369  <p>The really clean way to handle this is to use the panels
1370  library. If, when you want a screen update, you do
1371  <code>update_panels()</code>, it will do all the necessary
1372  <code>wnoutrefresh()</code> calls for whatever panel stacking
1373  order you have defined. Then you can do one
1374  <code>doupdate()</code> and there will be a <em>single</em> burst
1375  of physical I/O that will do all your updates.</p>
1376
1377  <h4><a name="backbug" id="backbug">Background Erase</a></h4>
1378
1379  <p>If you have been using a very old versions of
1380  <code>ncurses</code> (1.8.7 or older) you may be surprised by the
1381  behavior of the erase functions. In older versions, erased areas
1382  of a window were filled with a blank modified by the window's
1383  current attribute (as set by <strong>wattrset()</strong>,
1384  <strong>wattron()</strong>, <strong>wattroff()</strong> and
1385  friends).</p>
1386
1387  <p>In newer versions, this is not so. Instead, the attribute of
1388  erased blanks is normal unless and until it is modified by the
1389  functions <code>bkgdset()</code> or <code>wbkgdset()</code>.</p>
1390
1391  <p>This change in behavior conforms <code>ncurses</code> to
1392  System V Release 4 and the XSI Curses standard.</p>
1393
1394  <h3><a name="xsifuncs" id="xsifuncs">XSI Curses Conformance</a></h3>
1395
1396  <p>The <code>ncurses</code> library is intended to be base-level
1397  conformant with the XSI Curses standard from X/Open. Many
1398  extended-level features (in fact, almost all features not
1399  directly concerned with wide characters and internationalization)
1400  are also supported.</p>
1401
1402  <p>One effect of XSI conformance is the change in behavior
1403  described under <a href="#backbug">"Background Erase --
1404  Compatibility with Old Versions"</a>.</p>
1405
1406  <p>Also, <code>ncurses</code> meets the XSI requirement that
1407  every macro entry point have a corresponding function which may
1408  be linked (and will be prototype-checked) if the macro definition
1409  is disabled with <code>#undef</code>.</p>
1410
1411  <h2><a name="panels" id="panels">The Panels Library</a></h2>
1412
1413  <p>The <code>ncurses</code> library by itself provides good
1414  support for screen displays in which the windows are tiled
1415  (non-overlapping). In the more general case that windows may
1416  overlap, you have to use a series of <code>wnoutrefresh()</code>
1417  calls followed by a <code>doupdate()</code>, and be careful about
1418  the order you do the window refreshes in. It has to be
1419  bottom-upwards, otherwise parts of windows that should be
1420  obscured will show through.</p>
1421
1422  <p>When your interface design is such that windows may dive
1423  deeper into the visibility stack or pop to the top at runtime,
1424  the resulting book-keeping can be tedious and difficult to get
1425  right. Hence the panels library.</p>
1426
1427  <p>The <code>panel</code> library first appeared in AT&amp;T
1428  System V. The version documented here is the <code>panel</code>
1429  code distributed with <code>ncurses</code>.</p>
1430
1431  <h3><a name="pcompile" id="pcompile">Compiling With the Panels
1432  Library</a></h3>
1433
1434  <p>Your panels-using modules must import the panels library
1435  declarations with</p>
1436
1437  <pre class="code-block">
1438          #include &lt;panel.h&gt;
1439</pre>
1440  <p>and must be linked explicitly with the panels library using an
1441  <code>-lpanel</code> argument. Note that they must also link the
1442  <code>ncurses</code> library with <code>-lncurses</code>. Many
1443  linkers are two-pass and will accept either order, but it is
1444  still good practice to put <code>-lpanel</code> first and
1445  <code>-lncurses</code> second.</p>
1446
1447  <h3><a name="poverview" id="poverview">Overview of Panels</a></h3>
1448
1449  <p>A panel object is a window that is implicitly treated as part
1450  of a <dfn>deck</dfn> including all other panel objects. The deck
1451  has an implicit bottom-to-top visibility order. The panels
1452  library includes an update function (analogous to
1453  <code>refresh()</code>) that displays all panels in the deck in
1454  the proper order to resolve overlaps. The standard window,
1455  <code>stdscr</code>, is considered below all panels.</p>
1456
1457  <p>Details on the panels functions are available in the man
1458  pages. We will just hit the highlights here.</p>
1459
1460  <p>You create a panel from a window by calling
1461  <code>new_panel()</code> on a window pointer. It then becomes the
1462  top of the deck. The panel's window is available as the value of
1463  <code>panel_window()</code> called with the panel pointer as
1464  argument.</p>
1465
1466  <p>You can delete a panel (removing it from the deck) with
1467  <code>del_panel</code>. This will not deallocate the associated
1468  window; you have to do that yourself. You can replace a panel's
1469  window with a different window by calling
1470  <code>replace_window</code>. The new window may be of different
1471  size; the panel code will re-compute all overlaps. This operation
1472  does not change the panel's position in the deck.</p>
1473
1474  <p>To move a panel's window, use <code>move_panel()</code>. The
1475  <code>mvwin()</code> function on the panel's window is not
1476  sufficient because it does not update the panels library's
1477  representation of where the windows are. This operation leaves
1478  the panel's depth, contents, and size unchanged.</p>
1479
1480  <p>Two functions (<code>top_panel()</code>,
1481  <code>bottom_panel()</code>) are provided for rearranging the
1482  deck. The first pops its argument window to the top of the deck;
1483  the second sends it to the bottom. Either operation leaves the
1484  panel's screen location, contents, and size unchanged.</p>
1485
1486  <p>The function <code>update_panels()</code> does all the
1487  <code>wnoutrefresh()</code> calls needed to prepare for
1488  <code>doupdate()</code> (which you must call yourself,
1489  afterwards).</p>
1490
1491  <p>Typically, you will want to call <code>update_panels()</code>
1492  and <code>doupdate()</code> just before accepting command input,
1493  once in each cycle of interaction with the user. If you call
1494  <code>update_panels()</code> after each and every panel write,
1495  you will generate a lot of unnecessary refresh activity and
1496  screen flicker.</p>
1497
1498  <h3><a name="pstdscr" id="pstdscr">Panels, Input, and the
1499  Standard Screen</a></h3>
1500
1501  <p>You should not mix <code>wnoutrefresh()</code> or
1502  <code>wrefresh()</code> operations with panels code; this will
1503  work only if the argument window is either in the top panel or
1504  unobscured by any other panels.</p>
1505
1506  <p>The <code>stsdcr</code> window is a special case. It is
1507  considered below all panels. Because changes to panels may
1508  obscure parts of <code>stdscr</code>, though, you should call
1509  <code>update_panels()</code> before <code>doupdate()</code> even
1510  when you only change <code>stdscr</code>.</p>
1511
1512  <p>Note that <code>wgetch</code> automatically calls
1513  <code>wrefresh</code>. Therefore, before requesting input from a
1514  panel window, you need to be sure that the panel is totally
1515  unobscured.</p>
1516
1517  <p>There is presently no way to display changes to one obscured
1518  panel without repainting all panels.</p>
1519
1520  <h3><a name="hiding" id="hiding">Hiding Panels</a></h3>
1521
1522  <p>It is possible to remove a panel from the deck temporarily;
1523  use <code>hide_panel</code> for this. Use
1524  <code>show_panel()</code> to render it visible again. The
1525  predicate function <code>panel_hidden</code> tests whether or not
1526  a panel is hidden.</p>
1527
1528  <p>The <code>panel_update</code> code ignores hidden panels. You
1529  cannot do <code>top_panel()</code> or <code>bottom_panel</code>
1530  on a hidden panel(). Other panels operations are applicable.</p>
1531
1532  <h3><a name="pmisc" id="pmisc">Miscellaneous Other Facilities</a></h3>
1533
1534  <p>It is possible to navigate the deck using the functions
1535  <code>panel_above()</code> and <code>panel_below</code>. Handed a
1536  panel pointer, they return the panel above or below that panel.
1537  Handed <code>NULL</code>, they return the bottom-most or top-most
1538  panel.</p>
1539
1540  <p>Every panel has an associated user pointer, not used by the
1541  panel code, to which you can attach application data. See the man
1542  page documentation of <code>set_panel_userptr()</code> and
1543  <code>panel_userptr</code> for details.</p>
1544
1545  <h2><a name="menu" id="menu">The Menu Library</a></h2>
1546
1547  <p>A menu is a screen display that assists the user to choose
1548  some subset of a given set of items. The <code>menu</code>
1549  library is a curses extension that supports easy programming of
1550  menu hierarchies with a uniform but flexible interface.</p>
1551
1552  <p>The <code>menu</code> library first appeared in AT&amp;T
1553  System V. The version documented here is the <code>menu</code>
1554  code distributed with <code>ncurses</code>.</p>
1555
1556  <h3><a name="mcompile" id="mcompile">Compiling With the menu
1557  Library</a></h3>
1558
1559  <p>Your menu-using modules must import the menu library
1560  declarations with</p>
1561
1562  <pre class="code-block">
1563          #include &lt;menu.h&gt;
1564</pre>
1565  <p>and must be linked explicitly with the menus library using an
1566  <code>-lmenu</code> argument. Note that they must also link the
1567  <code>ncurses</code> library with <code>-lncurses</code>. Many
1568  linkers are two-pass and will accept either order, but it is
1569  still good practice to put <code>-lmenu</code> first and
1570  <code>-lncurses</code> second.</p>
1571
1572  <h3><a name="moverview" id="moverview">Overview of Menus</a></h3>
1573
1574  <p>The menus created by this library consist of collections of
1575  <dfn>items</dfn> including a name string part and a description
1576  string part. To make menus, you create groups of these items and
1577  connect them with menu frame objects.</p>
1578
1579  <p>The menu can then by <dfn>posted</dfn>, that is written to an
1580  associated window. Actually, each menu has two associated
1581  windows; a containing window in which the programmer can scribble
1582  titles or borders, and a subwindow in which the menu items proper
1583  are displayed. If this subwindow is too small to display all the
1584  items, it will be a scrollable viewport on the collection of
1585  items.</p>
1586
1587  <p>A menu may also be <dfn>unposted</dfn> (that is, undisplayed),
1588  and finally freed to make the storage associated with it and its
1589  items available for re-use.</p>
1590
1591  <p>The general flow of control of a menu program looks like
1592  this:</p>
1593
1594  <ol>
1595    <li>Initialize <code>curses</code>.</li>
1596
1597    <li>Create the menu items, using <code>new_item()</code>.</li>
1598
1599    <li>Create the menu using <code>new_menu()</code>.</li>
1600
1601    <li>Post the menu using <code>post_menu()</code>.</li>
1602
1603    <li>Refresh the screen.</li>
1604
1605    <li>Process user requests via an input loop.</li>
1606
1607    <li>Unpost the menu using <code>unpost_menu()</code>.</li>
1608
1609    <li>Free the menu, using <code>free_menu()</code>.</li>
1610
1611    <li>Free the items using <code>free_item()</code>.</li>
1612
1613    <li>Terminate <code>curses</code>.</li>
1614  </ol>
1615
1616  <h3><a name="mselect" id="mselect">Selecting items</a></h3>
1617
1618  <p>Menus may be multi-valued or (the default) single-valued (see
1619  the manual page <code>menu_opts(3x)</code> to see how to change
1620  the default). Both types always have a <dfn>current
1621  item</dfn>.</p>
1622
1623  <p>From a single-valued menu you can read the selected value
1624  simply by looking at the current item. From a multi-valued menu,
1625  you get the selected set by looping through the items applying
1626  the <code>item_value()</code> predicate function. Your
1627  menu-processing code can use the function
1628  <code>set_item_value()</code> to flag the items in the select
1629  set.</p>
1630
1631  <p>Menu items can be made unselectable using
1632  <code>set_item_opts()</code> or <code>item_opts_off()</code> with
1633  the <code>O_SELECTABLE</code> argument. This is the only option
1634  so far defined for menus, but it is good practice to code as
1635  though other option bits might be on.</p>
1636
1637  <h3><a name="mdisplay" id="mdisplay">Menu Display</a></h3>
1638
1639  <p>The menu library calculates a minimum display size for your
1640  window, based on the following variables:</p>
1641
1642  <ul>
1643    <li>The number and maximum length of the menu items</li>
1644
1645    <li>Whether the O_ROWMAJOR option is enabled</li>
1646
1647    <li>Whether display of descriptions is enabled</li>
1648
1649    <li>Whatever menu format may have been set by the
1650    programmer</li>
1651
1652    <li>The length of the menu mark string used for highlighting
1653    selected items</li>
1654  </ul>
1655
1656  <p>The function <code>set_menu_format()</code> allows you to set
1657  the maximum size of the viewport or <dfn>menu page</dfn> that
1658  will be used to display menu items. You can retrieve any format
1659  associated with a menu with <code>menu_format()</code>. The
1660  default format is rows=16, columns=1.</p>
1661
1662  <p>The actual menu page may be smaller than the format size. This
1663  depends on the item number and size and whether O_ROWMAJOR is on.
1664  This option (on by default) causes menu items to be displayed in
1665  a &ldquo;raster-scan&rdquo; pattern, so that if more than one
1666  item will fit horizontally the first couple of items are
1667  side-by-side in the top row. The alternative is column-major
1668  display, which tries to put the first several items in the first
1669  column.</p>
1670
1671  <p>As mentioned above, a menu format not large enough to allow
1672  all items to fit on-screen will result in a menu display that is
1673  vertically scrollable.</p>
1674
1675  <p>You can scroll it with requests to the menu driver, which will
1676  be described in the section on <a href="#minput">menu input
1677  handling</a>.</p>
1678
1679  <p>Each menu has a <dfn>mark string</dfn> used to visually tag
1680  selected items; see the <code>menu_mark(3x)</code> manual page
1681  for details. The mark string length also influences the menu page
1682  size.</p>
1683
1684  <p>The function <code>scale_menu()</code> returns the minimum
1685  display size that the menu code computes from all these factors.
1686  There are other menu display attributes including a select
1687  attribute, an attribute for selectable items, an attribute for
1688  unselectable items, and a pad character used to separate item
1689  name text from description text. These have reasonable defaults
1690  which the library allows you to change (see the
1691  <code>menu_attribs(3x)</code> manual page.</p>
1692
1693  <h3><a name="mwindows" id="mwindows">Menu Windows</a></h3>
1694
1695  <p>Each menu has, as mentioned previously, a pair of associated
1696  windows. Both these windows are painted when the menu is posted
1697  and erased when the menu is unposted.</p>
1698
1699  <p>The outer or frame window is not otherwise touched by the menu
1700  routines. It exists so the programmer can associate a title, a
1701  border, or perhaps help text with the menu and have it properly
1702  refreshed or erased at post/unpost time. The inner window or
1703  <dfn>subwindow</dfn> is where the current menu page is
1704  displayed.</p>
1705
1706  <p>By default, both windows are <code>stdscr</code>. You can set
1707  them with the functions in <code>menu_win(3x)</code>.</p>
1708
1709  <p>When you call <code>post_menu()</code>, you write the menu to
1710  its subwindow. When you call <code>unpost_menu()</code>, you
1711  erase the subwindow, However, neither of these actually modifies
1712  the screen. To do that, call <code>wrefresh()</code> or some
1713  equivalent.</p>
1714
1715  <h3><a name="minput" id="minput">Processing Menu Input</a></h3>
1716
1717  <p>The main loop of your menu-processing code should call
1718  <code>menu_driver()</code> repeatedly. The first argument of this
1719  routine is a menu pointer; the second is a menu command code. You
1720  should write an input-fetching routine that maps input characters
1721  to menu command codes, and pass its output to
1722  <code>menu_driver()</code>. The menu command codes are fully
1723  documented in <code>menu_driver(3x)</code>.</p>
1724
1725  <p>The simplest group of command codes is
1726  <code>REQ_NEXT_ITEM</code>, <code>REQ_PREV_ITEM</code>,
1727  <code>REQ_FIRST_ITEM</code>, <code>REQ_LAST_ITEM</code>,
1728  <code>REQ_UP_ITEM</code>, <code>REQ_DOWN_ITEM</code>,
1729  <code>REQ_LEFT_ITEM</code>, <code>REQ_RIGHT_ITEM</code>. These
1730  change the currently selected item. These requests may cause
1731  scrolling of the menu page if it only partially displayed.</p>
1732
1733  <p>There are explicit requests for scrolling which also change
1734  the current item (because the select location does not change,
1735  but the item there does). These are <code>REQ_SCR_DLINE</code>,
1736  <code>REQ_SCR_ULINE</code>, <code>REQ_SCR_DPAGE</code>, and
1737  <code>REQ_SCR_UPAGE</code>.</p>
1738
1739  <p>The <code>REQ_TOGGLE_ITEM</code> selects or deselects the
1740  current item. It is for use in multi-valued menus; if you use it
1741  with <code>O_ONEVALUE</code> on, you will get an error return
1742  (<code>E_REQUEST_DENIED</code>).</p>
1743
1744  <p>Each menu has an associated pattern buffer. The
1745  <code>menu_driver()</code> logic tries to accumulate printable
1746  ASCII characters passed in in that buffer; when it matches a
1747  prefix of an item name, that item (or the next matching item) is
1748  selected. If appending a character yields no new match, that
1749  character is deleted from the pattern buffer, and
1750  <code>menu_driver()</code> returns <code>E_NO_MATCH</code>.</p>
1751
1752  <p>Some requests change the pattern buffer directly:
1753  <code>REQ_CLEAR_PATTERN</code>, <code>REQ_BACK_PATTERN</code>,
1754  <code>REQ_NEXT_MATCH</code>, <code>REQ_PREV_MATCH</code>. The
1755  latter two are useful when pattern buffer input matches more than
1756  one item in a multi-valued menu.</p>
1757
1758  <p>Each successful scroll or item navigation request clears the
1759  pattern buffer. It is also possible to set the pattern buffer
1760  explicitly with <code>set_menu_pattern()</code>.</p>
1761
1762  <p>Finally, menu driver requests above the constant
1763  <code>MAX_COMMAND</code> are considered application-specific
1764  commands. The <code>menu_driver()</code> code ignores them and
1765  returns <code>E_UNKNOWN_COMMAND</code>.</p>
1766
1767  <h3><a name="mmisc" id="mmisc">Miscellaneous Other Features</a></h3>
1768
1769  <p>Various menu options can affect the processing and visual
1770  appearance and input processing of menus. See <code>menu_opts(3x)
1771  for details.</code></p>
1772
1773  <p>It is possible to change the current item from application
1774  code; this is useful if you want to write your own navigation
1775  requests. It is also possible to explicitly set the top row of
1776  the menu display. See <code>mitem_current(3x)</code>. If your
1777  application needs to change the menu subwindow cursor for any
1778  reason, <code>pos_menu_cursor()</code> will restore it to the
1779  correct location for continuing menu driver processing.</p>
1780
1781  <p>It is possible to set hooks to be called at menu
1782  initialization and wrapup time, and whenever the selected item
1783  changes. See <code>menu_hook(3x)</code>.</p>
1784
1785  <p>Each item, and each menu, has an associated user pointer on
1786  which you can hang application data. See
1787  <code>mitem_userptr(3x)</code> and
1788  <code>menu_userptr(3x)</code>.</p>
1789
1790  <h2><a name="form" id="form">The Forms Library</a></h2>
1791
1792  <p>The <code>form</code> library is a curses extension that
1793  supports easy programming of on-screen forms for data entry and
1794  program control.</p>
1795
1796  <p>The <code>form</code> library first appeared in AT&amp;T
1797  System V. The version documented here is the <code>form</code>
1798  code distributed with <code>ncurses</code>.</p>
1799
1800  <h3><a name="fcompile" id="fcompile">Compiling With the form
1801  Library</a></h3>
1802
1803  <p>Your form-using modules must import the form library
1804  declarations with</p>
1805
1806  <pre class="code-block">
1807          #include &lt;form.h&gt;
1808</pre>
1809  <p>and must be linked explicitly with the forms library using an
1810  <code>-lform</code> argument. Note that they must also link the
1811  <code>ncurses</code> library with <code>-lncurses</code>. Many
1812  linkers are two-pass and will accept either order, but it is
1813  still good practice to put <code>-lform</code> first and
1814  <code>-lncurses</code> second.</p>
1815
1816  <h3><a name="foverview" id="foverview">Overview of Forms</a></h3>
1817
1818  <p>A form is a collection of fields; each field may be either a
1819  label (explanatory text) or a data-entry location. Long forms may
1820  be segmented into pages; each entry to a new page clears the
1821  screen.</p>
1822
1823  <p>To make forms, you create groups of fields and connect them
1824  with form frame objects; the form library makes this relatively
1825  simple.</p>
1826
1827  <p>Once defined, a form can be <dfn>posted</dfn>, that is written
1828  to an associated window. Actually, each form has two associated
1829  windows; a containing window in which the programmer can scribble
1830  titles or borders, and a subwindow in which the form fields
1831  proper are displayed.</p>
1832
1833  <p>As the form user fills out the posted form, navigation and
1834  editing keys support movement between fields, editing keys
1835  support modifying field, and plain text adds to or changes data
1836  in a current field. The form library allows you (the forms
1837  designer) to bind each navigation and editing key to any
1838  keystroke accepted by <code>curses</code> Fields may have
1839  validation conditions on them, so that they check input data for
1840  type and value. The form library supplies a rich set of
1841  pre-defined field types, and makes it relatively easy to define
1842  new ones.</p>
1843
1844  <p>Once its transaction is completed (or aborted), a form may be
1845  <dfn>unposted</dfn> (that is, undisplayed), and finally freed to
1846  make the storage associated with it and its items available for
1847  re-use.</p>
1848
1849  <p>The general flow of control of a form program looks like
1850  this:</p>
1851
1852  <ol>
1853    <li>Initialize <code>curses</code>.</li>
1854
1855    <li>Create the form fields, using
1856    <code>new_field()</code>.</li>
1857
1858    <li>Create the form using <code>new_form()</code>.</li>
1859
1860    <li>Post the form using <code>post_form()</code>.</li>
1861
1862    <li>Refresh the screen.</li>
1863
1864    <li>Process user requests via an input loop.</li>
1865
1866    <li>Unpost the form using <code>unpost_form()</code>.</li>
1867
1868    <li>Free the form, using <code>free_form()</code>.</li>
1869
1870    <li>Free the fields using <code>free_field()</code>.</li>
1871
1872    <li>Terminate <code>curses</code>.</li>
1873  </ol>
1874
1875  <p>Note that this looks much like a menu program; the form
1876  library handles tasks which are in many ways similar, and its
1877  interface was obviously designed to resemble that of the <a href=
1878  "#menu">menu library</a> wherever possible.</p>
1879
1880  <p>In forms programs, however, the &ldquo;process user
1881  requests&rdquo; is somewhat more complicated than for menus.
1882  Besides menu-like navigation operations, the menu driver loop has
1883  to support field editing and data validation.</p>
1884
1885  <h3><a name="fcreate" id="fcreate">Creating and Freeing Fields
1886  and Forms</a></h3>
1887
1888  <p>The basic function for creating fields is
1889  <code>new_field()</code>:</p>
1890
1891  <pre class="code-block">
1892FIELD *new_field(int height, int width,   /* new field size */
1893                 int top, int left,       /* upper left corner */
1894                 int offscreen,           /* number of offscreen rows */
1895                 int nbuf);               /* number of working buffers */
1896</pre>
1897  <p>Menu items always occupy a single row, but forms fields may
1898  have multiple rows. So <code>new_field()</code> requires you to
1899  specify a width and height (the first two arguments, which mist
1900  both be greater than zero).</p>
1901
1902  <p>You must also specify the location of the field's upper left
1903  corner on the screen (the third and fourth arguments, which must
1904  be zero or greater). Note that these coordinates are relative to
1905  the form subwindow, which will coincide with <code>stdscr</code>
1906  by default but need not be <code>stdscr</code> if you have done
1907  an explicit <code>set_form_win()</code> call.</p>
1908
1909  <p>The fifth argument allows you to specify a number of
1910  off-screen rows. If this is zero, the entire field will always be
1911  displayed. If it is nonzero, the form will be scrollable, with
1912  only one screen-full (initially the top part) displayed at any
1913  given time. If you make a field dynamic and grow it so it will no
1914  longer fit on the screen, the form will become scrollable even if
1915  the <code>offscreen</code> argument was initially zero.</p>
1916
1917  <p>The forms library allocates one working buffer per field; the
1918  size of each buffer is <code>((height + offscreen)*width +
1919  1</code>, one character for each position in the field plus a NUL
1920  terminator. The sixth argument is the number of additional data
1921  buffers to allocate for the field; your application can use them
1922  for its own purposes.</p>
1923
1924  <pre class="code-block">
1925FIELD *dup_field(FIELD *field,            /* field to copy */
1926                 int top, int left);      /* location of new copy */
1927</pre>
1928  <p>The function <code>dup_field()</code> duplicates an existing
1929  field at a new location. Size and buffering information are
1930  copied; some attribute flags and status bits are not (see the
1931  <code>form_field_new(3X)</code> for details).</p>
1932
1933  <pre class="code-block">
1934FIELD *link_field(FIELD *field,           /* field to copy */
1935                  int top, int left);     /* location of new copy */
1936</pre>
1937  <p>The function <code>link_field()</code> also duplicates an
1938  existing field at a new location. The difference from
1939  <code>dup_field()</code> is that it arranges for the new field's
1940  buffer to be shared with the old one.</p>
1941
1942  <p>Besides the obvious use in making a field editable from two
1943  different form pages, linked fields give you a way to hack in
1944  dynamic labels. If you declare several fields linked to an
1945  original, and then make them inactive, changes from the original
1946  will still be propagated to the linked fields.</p>
1947
1948  <p>As with duplicated fields, linked fields have attribute bits
1949  separate from the original.</p>
1950
1951  <p>As you might guess, all these field-allocations return
1952  <code>NULL</code> if the field allocation is not possible due to
1953  an out-of-memory error or out-of-bounds arguments.</p>
1954
1955  <p>To connect fields to a form, use</p>
1956
1957  <pre class="code-block">
1958FORM *new_form(FIELD **fields);
1959</pre>
1960  <p>This function expects to see a NULL-terminated array of field
1961  pointers. Said fields are connected to a newly-allocated form
1962  object; its address is returned (or else NULL if the allocation
1963  fails).</p>
1964
1965  <p>Note that <code>new_field()</code> does <em>not</em> copy the
1966  pointer array into private storage; if you modify the contents of
1967  the pointer array during forms processing, all manner of bizarre
1968  things might happen. Also note that any given field may only be
1969  connected to one form.</p>
1970
1971  <p>The functions <code>free_field()</code> and
1972  <code>free_form</code> are available to free field and form
1973  objects. It is an error to attempt to free a field connected to a
1974  form, but not vice-versa; thus, you will generally free your form
1975  objects first.</p>
1976
1977  <h3><a name="fattributes" id="fattributes">Fetching and Changing
1978  Field Attributes</a></h3>
1979
1980  <p>Each form field has a number of location and size attributes
1981  associated with it. There are other field attributes used to
1982  control display and editing of the field. Some (for example, the
1983  <code>O_STATIC</code> bit) involve sufficient complications to be
1984  covered in sections of their own later on. We cover the functions
1985  used to get and set several basic attributes here.</p>
1986
1987  <p>When a field is created, the attributes not specified by the
1988  <code>new_field</code> function are copied from an invisible
1989  system default field. In attribute-setting and -fetching
1990  functions, the argument NULL is taken to mean this field. Changes
1991  to it persist as defaults until your forms application
1992  terminates.</p>
1993
1994  <h4><a name="fsizes" id="fsizes">Fetching Size and Location
1995  Data</a></h4>
1996
1997  <p>You can retrieve field sizes and locations through:</p>
1998
1999  <pre class="code-block">
2000int field_info(FIELD *field,              /* field from which to fetch */
2001               int *height, *int width,   /* field size */
2002               int *top, int *left,       /* upper left corner */
2003               int *offscreen,            /* number of offscreen rows */
2004               int *nbuf);                /* number of working buffers */
2005</pre>
2006  <p>This function is a sort of inverse of
2007  <code>new_field()</code>; instead of setting size and location
2008  attributes of a new field, it fetches them from an existing
2009  one.</p>
2010
2011  <h4><a name="flocation" id="flocation">Changing the Field
2012  Location</a></h4>
2013
2014  <p>It is possible to move a field's location on the screen:</p>
2015
2016  <pre class="code-block">
2017int move_field(FIELD *field,              /* field to alter */
2018               int top, int left);        /* new upper-left corner */
2019</pre>
2020  <p>You can, of course. query the current location through
2021  <code>field_info()</code>.</p>
2022
2023  <h4><a name="fjust" id="fjust">The Justification Attribute</a></h4>
2024
2025  <p>One-line fields may be unjustified, justified right, justified
2026  left, or centered. Here is how you manipulate this attribute:</p>
2027
2028  <pre class="code-block">
2029int set_field_just(FIELD *field,          /* field to alter */
2030                   int justmode);         /* mode to set */
2031
2032int field_just(FIELD *field);             /* fetch mode of field */
2033</pre>
2034  <p>The mode values accepted and returned by this functions are
2035  preprocessor macros <code>NO_JUSTIFICATION</code>,
2036  <code>JUSTIFY_RIGHT</code>, <code>JUSTIFY_LEFT</code>, or
2037  <code>JUSTIFY_CENTER</code>.</p>
2038
2039  <h4><a name="fdispatts" id="fdispatts">Field Display
2040  Attributes</a></h4>
2041
2042  <p>For each field, you can set a foreground attribute for entered
2043  characters, a background attribute for the entire field, and a
2044  pad character for the unfilled portion of the field. You can also
2045  control pagination of the form.</p>
2046
2047  <p>This group of four field attributes controls the visual
2048  appearance of the field on the screen, without affecting in any
2049  way the data in the field buffer.</p>
2050
2051  <pre class="code-block">
2052int set_field_fore(FIELD *field,          /* field to alter */
2053                   chtype attr);          /* attribute to set */
2054
2055chtype field_fore(FIELD *field);          /* field to query */
2056
2057int set_field_back(FIELD *field,          /* field to alter */
2058                   chtype attr);          /* attribute to set */
2059
2060chtype field_back(FIELD *field);          /* field to query */
2061
2062int set_field_pad(FIELD *field,           /* field to alter */
2063                 int pad);                /* pad character to set */
2064
2065chtype field_pad(FIELD *field);
2066
2067int set_new_page(FIELD *field,            /* field to alter */
2068                 int flag);               /* TRUE to force new page */
2069
2070chtype new_page(FIELD *field);            /* field to query */
2071</pre>
2072  <p>The attributes set and returned by the first four functions
2073  are normal <code>curses(3x)</code> display attribute values
2074  (<code>A_STANDOUT</code>, <code>A_BOLD</code>,
2075  <code>A_REVERSE</code> etc). The page bit of a field controls
2076  whether it is displayed at the start of a new form screen.</p>
2077
2078  <h4><a name="foptions" id="foptions">Field Option Bits</a></h4>
2079
2080  <p>There is also a large collection of field option bits you can
2081  set to control various aspects of forms processing. You can
2082  manipulate them with these functions:</p>
2083
2084  <pre class="code-block">
2085int set_field_opts(FIELD *field,          /* field to alter */
2086                   int attr);             /* attribute to set */
2087
2088int field_opts_on(FIELD *field,           /* field to alter */
2089                  int attr);              /* attributes to turn on */
2090
2091int field_opts_off(FIELD *field,          /* field to alter */
2092                   int attr);             /* attributes to turn off */
2093
2094int field_opts(FIELD *field);             /* field to query */
2095</pre>
2096  <p>By default, all options are on. Here are the available option
2097  bits:</p>
2098
2099  <dl>
2100    <dt>O_VISIBLE</dt>
2101
2102    <dd>Controls whether the field is visible on the screen. Can be
2103    used during form processing to hide or pop up fields depending
2104    on the value of parent fields.</dd>
2105
2106    <dt>O_ACTIVE</dt>
2107
2108    <dd>Controls whether the field is active during forms
2109    processing (i.e. visited by form navigation keys). Can be used
2110    to make labels or derived fields with buffer values alterable
2111    by the forms application, not the user.</dd>
2112
2113    <dt>O_PUBLIC</dt>
2114
2115    <dd>Controls whether data is displayed during field entry. If
2116    this option is turned off on a field, the library will accept
2117    and edit data in that field, but it will not be displayed and
2118    the visible field cursor will not move. You can turn off the
2119    O_PUBLIC bit to define password fields.</dd>
2120
2121    <dt>O_EDIT</dt>
2122
2123    <dd>Controls whether the field's data can be modified. When
2124    this option is off, all editing requests except
2125    <code>REQ_PREV_CHOICE</code> and <code>REQ_NEXT_CHOICE</code>
2126    will fail. Such read-only fields may be useful for help
2127    messages.</dd>
2128
2129    <dt>O_WRAP</dt>
2130
2131    <dd>Controls word-wrapping in multi-line fields. Normally, when
2132    any character of a (blank-separated) word reaches the end of
2133    the current line, the entire word is wrapped to the next line
2134    (assuming there is one). When this option is off, the word will
2135    be split across the line break.</dd>
2136
2137    <dt>O_BLANK</dt>
2138
2139    <dd>Controls field blanking. When this option is on, entering a
2140    character at the first field position erases the entire field
2141    (except for the just-entered character).</dd>
2142
2143    <dt>O_AUTOSKIP</dt>
2144
2145    <dd>Controls automatic skip to next field when this one fills.
2146    Normally, when the forms user tries to type more data into a
2147    field than will fit, the editing location jumps to next field.
2148    When this option is off, the user's cursor will hang at the end
2149    of the field. This option is ignored in dynamic fields that
2150    have not reached their size limit.</dd>
2151
2152    <dt>O_NULLOK</dt>
2153
2154    <dd>Controls whether <a href="#fvalidation">validation</a> is
2155    applied to blank fields. Normally, it is not; the user can
2156    leave a field blank without invoking the usual validation check
2157    on exit. If this option is off on a field, exit from it will
2158    invoke a validation check.</dd>
2159
2160    <dt>O_PASSOK</dt>
2161
2162    <dd>Controls whether validation occurs on every exit, or only
2163    after the field is modified. Normally the latter is true.
2164    Setting O_PASSOK may be useful if your field's validation
2165    function may change during forms processing.</dd>
2166
2167    <dt>O_STATIC</dt>
2168
2169    <dd>Controls whether the field is fixed to its initial
2170    dimensions. If you turn this off, the field becomes <a href=
2171    "#fdynamic">dynamic</a> and will stretch to fit entered
2172    data.</dd>
2173  </dl>
2174
2175  <p>A field's options cannot be changed while the field is
2176  currently selected. However, options may be changed on posted
2177  fields that are not current.</p>
2178
2179  <p>The option values are bit-masks and can be composed with
2180  logical-or in the obvious way.</p>
2181
2182  <h3><a name="fstatus" id="fstatus">Field Status</a></h3>
2183
2184  <p>Every field has a status flag, which is set to FALSE when the
2185  field is created and TRUE when the value in field buffer 0
2186  changes. This flag can be queried and set directly:</p>
2187
2188  <pre class="code-block">
2189int set_field_status(FIELD *field,      /* field to alter */
2190                   int status);         /* mode to set */
2191
2192int field_status(FIELD *field);         /* fetch mode of field */
2193</pre>
2194  <p>Setting this flag under program control can be useful if you
2195  use the same form repeatedly, looking for modified fields each
2196  time.</p>
2197
2198  <p>Calling <code>field_status()</code> on a field not currently
2199  selected for input will return a correct value. Calling
2200  <code>field_status()</code> on a field that is currently selected
2201  for input may not necessarily give a correct field status value,
2202  because entered data is not necessarily copied to buffer zero
2203  before the exit validation check. To guarantee that the returned
2204  status value reflects reality, call <code>field_status()</code>
2205  either (1) in the field's exit validation check routine, (2) from
2206  the field's or form's initialization or termination hooks, or (3)
2207  just after a <code>REQ_VALIDATION</code> request has been
2208  processed by the forms driver.</p>
2209
2210  <h3><a name="fuser" id="fuser">Field User Pointer</a></h3>
2211
2212  <p>Each field structure contains one character pointer slot that
2213  is not used by the forms library. It is intended to be used by
2214  applications to store private per-field data. You can manipulate
2215  it with:</p>
2216
2217  <pre class="code-block">
2218int set_field_userptr(FIELD *field,       /* field to alter */
2219                   char *userptr);        /* mode to set */
2220
2221char *field_userptr(FIELD *field);        /* fetch mode of field */
2222</pre>(Properly, this user pointer field ought to have <code>(void
2223*)</code> type. The <code>(char *)</code> type is retained for
2224System V compatibility.)
2225  <p>It is valid to set the user pointer of the default field (with
2226  a <code>set_field_userptr()</code> call passed a NULL field
2227  pointer.) When a new field is created, the default-field user
2228  pointer is copied to initialize the new field's user pointer.</p>
2229
2230  <h3><a name="fdynamic" id="fdynamic">Variable-Sized Fields</a></h3>
2231
2232  <p>Normally, a field is fixed at the size specified for it at
2233  creation time. If, however, you turn off its O_STATIC bit, it
2234  becomes <dfn>dynamic</dfn> and will automatically resize itself
2235  to accommodate data as it is entered. If the field has extra
2236  buffers associated with it, they will grow right along with the
2237  main input buffer.</p>
2238
2239  <p>A one-line dynamic field will have a fixed height (1) but
2240  variable width, scrolling horizontally to display data within the
2241  field area as originally dimensioned and located. A multi-line
2242  dynamic field will have a fixed width, but variable height
2243  (number of rows), scrolling vertically to display data within the
2244  field area as originally dimensioned and located.</p>
2245
2246  <p>Normally, a dynamic field is allowed to grow without limit.
2247  But it is possible to set an upper limit on the size of a dynamic
2248  field. You do it with this function:</p>
2249
2250  <pre class="code-block">
2251int set_max_field(FIELD *field,     /* field to alter (may not be NULL) */
2252                   int max_size);   /* upper limit on field size */
2253</pre>
2254  <p>If the field is one-line, <code>max_size</code> is taken to be
2255  a column size limit; if it is multi-line, it is taken to be a
2256  line size limit. To disable any limit, use an argument of zero.
2257  The growth limit can be changed whether or not the O_STATIC bit
2258  is on, but has no effect until it is.</p>
2259
2260  <p>The following properties of a field change when it becomes
2261  dynamic:</p>
2262
2263  <ul>
2264    <li>If there is no growth limit, there is no final position of
2265    the field; therefore <code>O_AUTOSKIP</code> and
2266    <code>O_NL_OVERLOAD</code> are ignored.</li>
2267
2268    <li>Field justification will be ignored (though whatever
2269    justification is set up will be retained internally and can be
2270    queried).</li>
2271
2272    <li>The <code>dup_field()</code> and <code>link_field()</code>
2273    calls copy dynamic-buffer sizes. If the <code>O_STATIC</code>
2274    option is set on one of a collection of links, buffer resizing
2275    will occur only when the field is edited through that
2276    link.</li>
2277
2278    <li>The call <code>field_info()</code> will retrieve the
2279    original static size of the field; use
2280    <code>dynamic_field_info()</code> to get the actual dynamic
2281    size.</li>
2282  </ul>
2283
2284  <h3><a name="fvalidation" id="fvalidation">Field Validation</a></h3>
2285
2286  <p>By default, a field will accept any data that will fit in its
2287  input buffer. However, it is possible to attach a validation type
2288  to a field. If you do this, any attempt to leave the field while
2289  it contains data that does not match the validation type will
2290  fail. Some validation types also have a character-validity check
2291  for each time a character is entered in the field.</p>
2292
2293  <p>A field's validation check (if any) is not called when
2294  <code>set_field_buffer()</code> modifies the input buffer, nor
2295  when that buffer is changed through a linked field.</p>
2296
2297  <p>The <code>form</code> library provides a rich set of
2298  pre-defined validation types, and gives you the capability to
2299  define custom ones of your own. You can examine and change field
2300  validation attributes with the following functions:</p>
2301
2302  <pre class="code-block">
2303int set_field_type(FIELD *field,          /* field to alter */
2304                   FIELDTYPE *ftype,      /* type to associate */
2305                   ...);                  /* additional arguments*/
2306
2307FIELDTYPE *field_type(FIELD *field);      /* field to query */
2308</pre>
2309  <p>The validation type of a field is considered an attribute of
2310  the field. As with other field attributes, Also, doing
2311  <code>set_field_type()</code> with a <code>NULL</code> field
2312  default will change the system default for validation of
2313  newly-created fields.</p>
2314
2315  <p>Here are the pre-defined validation types:</p>
2316
2317  <h4><a name="ftype_alpha" id="ftype_alpha">TYPE_ALPHA</a></h4>
2318
2319  <p>This field type accepts alphabetic data; no blanks, no digits,
2320  no special characters (this is checked at character-entry time).
2321  It is set up with:</p>
2322
2323  <pre class="code-block">
2324int set_field_type(FIELD *field,          /* field to alter */
2325                   TYPE_ALPHA,            /* type to associate */
2326                   int width);            /* maximum width of field */
2327</pre>
2328  <p>The <code>width</code> argument sets a minimum width of data.
2329  Typically you will want to set this to the field width; if it is
2330  greater than the field width, the validation check will always
2331  fail. A minimum width of zero makes field completion
2332  optional.</p>
2333
2334  <h4><a name="ftype_alnum" id="ftype_alnum">TYPE_ALNUM</a></h4>
2335
2336  <p>This field type accepts alphabetic data and digits; no blanks,
2337  no special characters (this is checked at character-entry time).
2338  It is set up with:</p>
2339
2340  <pre class="code-block">
2341int set_field_type(FIELD *field,          /* field to alter */
2342                   TYPE_ALNUM,            /* type to associate */
2343                   int width);            /* maximum width of field */
2344</pre>
2345  <p>The <code>width</code> argument sets a minimum width of data.
2346  As with TYPE_ALPHA, typically you will want to set this to the
2347  field width; if it is greater than the field width, the
2348  validation check will always fail. A minimum width of zero makes
2349  field completion optional.</p>
2350
2351  <h4><a name="ftype_enum" id="ftype_enum">TYPE_ENUM</a></h4>
2352
2353  <p>This type allows you to restrict a field's values to be among
2354  a specified set of string values (for example, the two-letter
2355  postal codes for U.S. states). It is set up with:</p>
2356
2357  <pre class="code-block">
2358int set_field_type(FIELD *field,          /* field to alter */
2359                   TYPE_ENUM,             /* type to associate */
2360                   char **valuelist;      /* list of possible values */
2361                   int checkcase;         /* case-sensitive? */
2362                   int checkunique);      /* must specify uniquely? */
2363</pre>
2364  <p>The <code>valuelist</code> parameter must point at a
2365  NULL-terminated list of valid strings. The <code>checkcase</code>
2366  argument, if true, makes comparison with the string
2367  case-sensitive.</p>
2368
2369  <p>When the user exits a TYPE_ENUM field, the validation
2370  procedure tries to complete the data in the buffer to a valid
2371  entry. If a complete choice string has been entered, it is of
2372  course valid. But it is also possible to enter a prefix of a
2373  valid string and have it completed for you.</p>
2374
2375  <p>By default, if you enter such a prefix and it matches more
2376  than one value in the string list, the prefix will be completed
2377  to the first matching value. But the <code>checkunique</code>
2378  argument, if true, requires prefix matches to be unique in order
2379  to be valid.</p>
2380
2381  <p>The <code>REQ_NEXT_CHOICE</code> and
2382  <code>REQ_PREV_CHOICE</code> input requests can be particularly
2383  useful with these fields.</p>
2384
2385  <h4><a name="ftype_integer" id="ftype_integer">TYPE_INTEGER</a></h4>
2386
2387  <p>This field type accepts an integer. It is set up as
2388  follows:</p>
2389
2390  <pre class="code-block">
2391int set_field_type(FIELD *field,          /* field to alter */
2392                   TYPE_INTEGER,          /* type to associate */
2393                   int padding,           /* # places to zero-pad to */
2394                   int vmin, int vmax);   /* valid range */
2395</pre>
2396  <p>Valid characters consist of an optional leading minus and
2397  digits. The range check is performed on exit. If the range
2398  maximum is less than or equal to the minimum, the range is
2399  ignored.</p>
2400
2401  <p>If the value passes its range check, it is padded with as many
2402  leading zero digits as necessary to meet the padding
2403  argument.</p>
2404
2405  <p>A <code>TYPE_INTEGER</code> value buffer can conveniently be
2406  interpreted with the C library function <code>atoi(3)</code>.</p>
2407
2408  <h4><a name="ftype_numeric" id="ftype_numeric">TYPE_NUMERIC</a></h4>
2409
2410  <p>This field type accepts a decimal number. It is set up as
2411  follows:</p>
2412
2413  <pre class="code-block">
2414int set_field_type(FIELD *field,              /* field to alter */
2415                   TYPE_NUMERIC,              /* type to associate */
2416                   int padding,               /* # places of precision */
2417                   double vmin, double vmax); /* valid range */
2418</pre>
2419  <p>Valid characters consist of an optional leading minus and
2420  digits. possibly including a decimal point. If your system
2421  supports locale's, the decimal point character used must be the
2422  one defined by your locale. The range check is performed on exit.
2423  If the range maximum is less than or equal to the minimum, the
2424  range is ignored.</p>
2425
2426  <p>If the value passes its range check, it is padded with as many
2427  trailing zero digits as necessary to meet the padding
2428  argument.</p>
2429
2430  <p>A <code>TYPE_NUMERIC</code> value buffer can conveniently be
2431  interpreted with the C library function <code>atof(3)</code>.</p>
2432
2433  <h4><a name="ftype_regexp" id="ftype_regexp">TYPE_REGEXP</a></h4>
2434
2435  <p>This field type accepts data matching a regular expression. It
2436  is set up as follows:</p>
2437
2438  <pre class="code-block">
2439int set_field_type(FIELD *field,          /* field to alter */
2440                   TYPE_REGEXP,           /* type to associate */
2441                   char *regexp);         /* expression to match */
2442</pre>
2443  <p>The syntax for regular expressions is that of
2444  <code>regcomp(3)</code>. The check for regular-expression match
2445  is performed on exit.</p>
2446
2447  <h3><a name="fbuffer" id="fbuffer">Direct Field Buffer
2448  Manipulation</a></h3>
2449
2450  <p>The chief attribute of a field is its buffer contents. When a
2451  form has been completed, your application usually needs to know
2452  the state of each field buffer. You can find this out with:</p>
2453
2454  <pre class="code-block">
2455char *field_buffer(FIELD *field,          /* field to query */
2456                   int bufindex);         /* number of buffer to query */
2457</pre>
2458  <p>Normally, the state of the zero-numbered buffer for each field
2459  is set by the user's editing actions on that field. It is
2460  sometimes useful to be able to set the value of the zero-numbered
2461  (or some other) buffer from your application:</p>
2462
2463  <pre class="code-block">
2464int set_field_buffer(FIELD *field,        /* field to alter */
2465                   int bufindex,          /* number of buffer to alter */
2466                   char *value);          /* string value to set */
2467</pre>
2468  <p>If the field is not large enough and cannot be resized to a
2469  sufficiently large size to contain the specified value, the value
2470  will be truncated to fit.</p>
2471
2472  <p>Calling <code>field_buffer()</code> with a null field pointer
2473  will raise an error. Calling <code>field_buffer()</code> on a
2474  field not currently selected for input will return a correct
2475  value. Calling <code>field_buffer()</code> on a field that is
2476  currently selected for input may not necessarily give a correct
2477  field buffer value, because entered data is not necessarily
2478  copied to buffer zero before the exit validation check. To
2479  guarantee that the returned buffer value reflects on-screen
2480  reality, call <code>field_buffer()</code> either (1) in the
2481  field's exit validation check routine, (2) from the field's or
2482  form's initialization or termination hooks, or (3) just after a
2483  <code>REQ_VALIDATION</code> request has been processed by the
2484  forms driver.</p>
2485
2486  <h3><a name="formattrs" id="formattrs">Attributes of Forms</a></h3>
2487
2488  <p>As with field attributes, form attributes inherit a default
2489  from a system default form structure. These defaults can be
2490  queried or set by of these functions using a form-pointer
2491  argument of <code>NULL</code>.</p>
2492
2493  <p>The principal attribute of a form is its field list. You can
2494  query and change this list with:</p>
2495
2496  <pre class="code-block">
2497int set_form_fields(FORM *form,           /* form to alter */
2498                    FIELD **fields);      /* fields to connect */
2499
2500char *form_fields(FORM *form);            /* fetch fields of form */
2501
2502int field_count(FORM *form);              /* count connect fields */
2503</pre>
2504  <p>The second argument of <code>set_form_fields()</code> may be a
2505  NULL-terminated field pointer array like the one required by
2506  <code>new_form()</code>. In that case, the old fields of the form
2507  are disconnected but not freed (and eligible to be connected to
2508  other forms), then the new fields are connected.</p>
2509
2510  <p>It may also be null, in which case the old fields are
2511  disconnected (and not freed) but no new ones are connected.</p>
2512
2513  <p>The <code>field_count()</code> function simply counts the
2514  number of fields connected to a given from. It returns -1 if the
2515  form-pointer argument is NULL.</p>
2516
2517  <h3><a name="fdisplay" id="fdisplay">Control of Form Display</a></h3>
2518
2519  <p>In the overview section, you saw that to display a form you
2520  normally start by defining its size (and fields), posting it, and
2521  refreshing the screen. There is an hidden step before posting,
2522  which is the association of the form with a frame window
2523  (actually, a pair of windows) within which it will be displayed.
2524  By default, the forms library associates every form with the
2525  full-screen window <code>stdscr</code>.</p>
2526
2527  <p>By making this step explicit, you can associate a form with a
2528  declared frame window on your screen display. This can be useful
2529  if you want to adapt the form display to different screen sizes,
2530  dynamically tile forms on the screen, or use a form as part of an
2531  interface layout managed by <a href="#panels">panels</a>.</p>
2532
2533  <p>The two windows associated with each form have the same
2534  functions as their analogues in the <a href="#menu">menu
2535  library</a>. Both these windows are painted when the form is
2536  posted and erased when the form is unposted.</p>
2537
2538  <p>The outer or frame window is not otherwise touched by the form
2539  routines. It exists so the programmer can associate a title, a
2540  border, or perhaps help text with the form and have it properly
2541  refreshed or erased at post/unpost time. The inner window or
2542  subwindow is where the current form page is actually
2543  displayed.</p>
2544
2545  <p>In order to declare your own frame window for a form, you will
2546  need to know the size of the form's bounding rectangle. You can
2547  get this information with:</p>
2548
2549  <pre class="code-block">
2550int scale_form(FORM *form,                /* form to query */
2551               int *rows,                 /* form rows */
2552               int *cols);                /* form cols */
2553</pre>
2554  <p>The form dimensions are passed back in the locations pointed
2555  to by the arguments. Once you have this information, you can use
2556  it to declare of windows, then use one of these functions:</p>
2557
2558  <pre class="code-block">
2559int set_form_win(FORM *form,              /* form to alter */
2560                 WINDOW *win);            /* frame window to connect */
2561
2562WINDOW *form_win(FORM *form);             /* fetch frame window of form */
2563
2564int set_form_sub(FORM *form,              /* form to alter */
2565                 WINDOW *win);            /* form subwindow to connect */
2566
2567WINDOW *form_sub(FORM *form);             /* fetch form subwindow of form */
2568</pre>
2569  <p>Note that curses operations, including <code>refresh()</code>,
2570  on the form, should be done on the frame window, not the form
2571  subwindow.</p>
2572
2573  <p>It is possible to check from your application whether all of a
2574  scrollable field is actually displayed within the menu subwindow.
2575  Use these functions:</p>
2576
2577  <pre class="code-block">
2578int data_ahead(FORM *form);               /* form to be queried */
2579
2580int data_behind(FORM *form);              /* form to be queried */
2581</pre>
2582  <p>The function <code>data_ahead()</code> returns TRUE if (a) the
2583  current field is one-line and has undisplayed data off to the
2584  right, (b) the current field is multi-line and there is data
2585  off-screen below it.</p>
2586
2587  <p>The function <code>data_behind()</code> returns TRUE if the
2588  first (upper left hand) character position is off-screen (not
2589  being displayed).</p>
2590
2591  <p>Finally, there is a function to restore the form window's
2592  cursor to the value expected by the forms driver:</p>
2593
2594  <pre class="code-block">
2595int pos_form_cursor(FORM *)               /* form to be queried */
2596</pre>
2597  <p>If your application changes the form window cursor, call this
2598  function before handing control back to the forms driver in order
2599  to re-synchronize it.</p>
2600
2601  <h3><a name="fdriver" id="fdriver">Input Processing in the Forms
2602  Driver</a></h3>
2603
2604  <p>The function <code>form_driver()</code> handles virtualized
2605  input requests for form navigation, editing, and validation
2606  requests, just as <code>menu_driver</code> does for menus (see
2607  the section on <a href="#minput">menu input handling</a>).</p>
2608
2609  <pre class="code-block">
2610int form_driver(FORM *form,               /* form to pass input to */
2611                int request);             /* form request code */
2612</pre>
2613  <p>Your input virtualization function needs to take input and
2614  then convert it to either an alphanumeric character (which is
2615  treated as data to be entered in the currently-selected field),
2616  or a forms processing request.</p>
2617
2618  <p>The forms driver provides hooks (through input-validation and
2619  field-termination functions) with which your application code can
2620  check that the input taken by the driver matched what was
2621  expected.</p>
2622
2623  <h4><a name="fpage" id="fpage">Page Navigation Requests</a></h4>
2624
2625  <p>These requests cause page-level moves through the form,
2626  triggering display of a new form screen.</p>
2627
2628  <dl>
2629    <dt><code>REQ_NEXT_PAGE</code>
2630    </dt>
2631
2632    <dd>Move to the next form page.</dd>
2633
2634    <dt><code>REQ_PREV_PAGE</code>
2635    </dt>
2636
2637    <dd>Move to the previous form page.</dd>
2638
2639    <dt><code>REQ_FIRST_PAGE</code>
2640    </dt>
2641
2642    <dd>Move to the first form page.</dd>
2643
2644    <dt><code>REQ_LAST_PAGE</code>
2645    </dt>
2646
2647    <dd>Move to the last form page.</dd>
2648  </dl>
2649
2650  <p>These requests treat the list as cyclic; that is,
2651  <code>REQ_NEXT_PAGE</code> from the last page goes to the first,
2652  and <code>REQ_PREV_PAGE</code> from the first page goes to the
2653  last.</p>
2654
2655  <h4><a name="ffield" id="ffield">Inter-Field Navigation
2656  Requests</a></h4>
2657
2658  <p>These requests handle navigation between fields on the same
2659  page.</p>
2660
2661  <dl>
2662    <dt><code>REQ_NEXT_FIELD</code>
2663    </dt>
2664
2665    <dd>Move to next field.</dd>
2666
2667    <dt><code>REQ_PREV_FIELD</code>
2668    </dt>
2669
2670    <dd>Move to previous field.</dd>
2671
2672    <dt><code>REQ_FIRST_FIELD</code>
2673    </dt>
2674
2675    <dd>Move to the first field.</dd>
2676
2677    <dt><code>REQ_LAST_FIELD</code>
2678    </dt>
2679
2680    <dd>Move to the last field.</dd>
2681
2682    <dt><code>REQ_SNEXT_FIELD</code>
2683    </dt>
2684
2685    <dd>Move to sorted next field.</dd>
2686
2687    <dt><code>REQ_SPREV_FIELD</code>
2688    </dt>
2689
2690    <dd>Move to sorted previous field.</dd>
2691
2692    <dt><code>REQ_SFIRST_FIELD</code>
2693    </dt>
2694
2695    <dd>Move to the sorted first field.</dd>
2696
2697    <dt><code>REQ_SLAST_FIELD</code>
2698    </dt>
2699
2700    <dd>Move to the sorted last field.</dd>
2701
2702    <dt><code>REQ_LEFT_FIELD</code>
2703    </dt>
2704
2705    <dd>Move left to field.</dd>
2706
2707    <dt><code>REQ_RIGHT_FIELD</code>
2708    </dt>
2709
2710    <dd>Move right to field.</dd>
2711
2712    <dt><code>REQ_UP_FIELD</code>
2713    </dt>
2714
2715    <dd>Move up to field.</dd>
2716
2717    <dt><code>REQ_DOWN_FIELD</code>
2718    </dt>
2719
2720    <dd>Move down to field.</dd>
2721  </dl>
2722
2723  <p>These requests treat the list of fields on a page as cyclic;
2724  that is, <code>REQ_NEXT_FIELD</code> from the last field goes to
2725  the first, and <code>REQ_PREV_FIELD</code> from the first field
2726  goes to the last. The order of the fields for these (and the
2727  <code>REQ_FIRST_FIELD</code> and <code>REQ_LAST_FIELD</code>
2728  requests) is simply the order of the field pointers in the form
2729  array (as set up by <code>new_form()</code> or
2730  <code>set_form_fields()</code></p>
2731
2732  <p>It is also possible to traverse the fields as if they had been
2733  sorted in screen-position order, so the sequence goes
2734  left-to-right and top-to-bottom. To do this, use the second group
2735  of four sorted-movement requests.</p>
2736
2737  <p>Finally, it is possible to move between fields using visual
2738  directions up, down, right, and left. To accomplish this, use the
2739  third group of four requests. Note, however, that the position of
2740  a form for purposes of these requests is its upper-left
2741  corner.</p>
2742
2743  <p>For example, suppose you have a multi-line field B, and two
2744  single-line fields A and C on the same line with B, with A to the
2745  left of B and C to the right of B. A <code>REQ_MOVE_RIGHT</code>
2746  from A will go to B only if A, B, and C <em>all</em> share the
2747  same first line; otherwise it will skip over B to C.</p>
2748
2749  <h4><a name="fifield" id="fifield">Intra-Field Navigation
2750  Requests</a></h4>
2751
2752  <p>These requests drive movement of the edit cursor within the
2753  currently selected field.</p>
2754
2755  <dl>
2756    <dt><code>REQ_NEXT_CHAR</code>
2757    </dt>
2758
2759    <dd>Move to next character.</dd>
2760
2761    <dt><code>REQ_PREV_CHAR</code>
2762    </dt>
2763
2764    <dd>Move to previous character.</dd>
2765
2766    <dt><code>REQ_NEXT_LINE</code>
2767    </dt>
2768
2769    <dd>Move to next line.</dd>
2770
2771    <dt><code>REQ_PREV_LINE</code>
2772    </dt>
2773
2774    <dd>Move to previous line.</dd>
2775
2776    <dt><code>REQ_NEXT_WORD</code>
2777    </dt>
2778
2779    <dd>Move to next word.</dd>
2780
2781    <dt><code>REQ_PREV_WORD</code>
2782    </dt>
2783
2784    <dd>Move to previous word.</dd>
2785
2786    <dt><code>REQ_BEG_FIELD</code>
2787    </dt>
2788
2789    <dd>Move to beginning of field.</dd>
2790
2791    <dt><code>REQ_END_FIELD</code>
2792    </dt>
2793
2794    <dd>Move to end of field.</dd>
2795
2796    <dt><code>REQ_BEG_LINE</code>
2797    </dt>
2798
2799    <dd>Move to beginning of line.</dd>
2800
2801    <dt><code>REQ_END_LINE</code>
2802    </dt>
2803
2804    <dd>Move to end of line.</dd>
2805
2806    <dt><code>REQ_LEFT_CHAR</code>
2807    </dt>
2808
2809    <dd>Move left in field.</dd>
2810
2811    <dt><code>REQ_RIGHT_CHAR</code>
2812    </dt>
2813
2814    <dd>Move right in field.</dd>
2815
2816    <dt><code>REQ_UP_CHAR</code>
2817    </dt>
2818
2819    <dd>Move up in field.</dd>
2820
2821    <dt><code>REQ_DOWN_CHAR</code>
2822    </dt>
2823
2824    <dd>Move down in field.</dd>
2825  </dl>
2826
2827  <p>Each <em>word</em> is separated from the previous and next
2828  characters by whitespace. The commands to move to beginning and
2829  end of line or field look for the first or last non-pad character
2830  in their ranges.</p>
2831
2832  <h4><a name="fscroll" id="fscroll">Scrolling Requests</a></h4>
2833
2834  <p>Fields that are dynamic and have grown and fields explicitly
2835  created with offscreen rows are scrollable. One-line fields
2836  scroll horizontally; multi-line fields scroll vertically. Most
2837  scrolling is triggered by editing and intra-field movement (the
2838  library scrolls the field to keep the cursor visible). It is
2839  possible to explicitly request scrolling with the following
2840  requests:</p>
2841
2842  <dl>
2843    <dt><code>REQ_SCR_FLINE</code>
2844    </dt>
2845
2846    <dd>Scroll vertically forward a line.</dd>
2847
2848    <dt><code>REQ_SCR_BLINE</code>
2849    </dt>
2850
2851    <dd>Scroll vertically backward a line.</dd>
2852
2853    <dt><code>REQ_SCR_FPAGE</code>
2854    </dt>
2855
2856    <dd>Scroll vertically forward a page.</dd>
2857
2858    <dt><code>REQ_SCR_BPAGE</code>
2859    </dt>
2860
2861    <dd>Scroll vertically backward a page.</dd>
2862
2863    <dt><code>REQ_SCR_FHPAGE</code>
2864    </dt>
2865
2866    <dd>Scroll vertically forward half a page.</dd>
2867
2868    <dt><code>REQ_SCR_BHPAGE</code>
2869    </dt>
2870
2871    <dd>Scroll vertically backward half a page.</dd>
2872
2873    <dt><code>REQ_SCR_FCHAR</code>
2874    </dt>
2875
2876    <dd>Scroll horizontally forward a character.</dd>
2877
2878    <dt><code>REQ_SCR_BCHAR</code>
2879    </dt>
2880
2881    <dd>Scroll horizontally backward a character.</dd>
2882
2883    <dt><code>REQ_SCR_HFLINE</code>
2884    </dt>
2885
2886    <dd>Scroll horizontally one field width forward.</dd>
2887
2888    <dt><code>REQ_SCR_HBLINE</code>
2889    </dt>
2890
2891    <dd>Scroll horizontally one field width backward.</dd>
2892
2893    <dt><code>REQ_SCR_HFHALF</code>
2894    </dt>
2895
2896    <dd>Scroll horizontally one half field width forward.</dd>
2897
2898    <dt><code>REQ_SCR_HBHALF</code>
2899    </dt>
2900
2901    <dd>Scroll horizontally one half field width backward.</dd>
2902  </dl>
2903
2904  <p>For scrolling purposes, a <em>page</em> of a field is the
2905  height of its visible part.</p>
2906
2907  <h4><a name="fedit" id="fedit">Editing Requests</a></h4>
2908
2909  <p>When you pass the forms driver an ASCII character, it is
2910  treated as a request to add the character to the field's data
2911  buffer. Whether this is an insertion or a replacement depends on
2912  the field's edit mode (insertion is the default.</p>
2913
2914  <p>The following requests support editing the field and changing
2915  the edit mode:</p>
2916
2917  <dl>
2918    <dt><code>REQ_INS_MODE</code>
2919    </dt>
2920
2921    <dd>Set insertion mode.</dd>
2922
2923    <dt><code>REQ_OVL_MODE</code>
2924    </dt>
2925
2926    <dd>Set overlay mode.</dd>
2927
2928    <dt><code>REQ_NEW_LINE</code>
2929    </dt>
2930
2931    <dd>New line request (see below for explanation).</dd>
2932
2933    <dt><code>REQ_INS_CHAR</code>
2934    </dt>
2935
2936    <dd>Insert space at character location.</dd>
2937
2938    <dt><code>REQ_INS_LINE</code>
2939    </dt>
2940
2941    <dd>Insert blank line at character location.</dd>
2942
2943    <dt><code>REQ_DEL_CHAR</code>
2944    </dt>
2945
2946    <dd>Delete character at cursor.</dd>
2947
2948    <dt><code>REQ_DEL_PREV</code>
2949    </dt>
2950
2951    <dd>Delete previous word at cursor.</dd>
2952
2953    <dt><code>REQ_DEL_LINE</code>
2954    </dt>
2955
2956    <dd>Delete line at cursor.</dd>
2957
2958    <dt><code>REQ_DEL_WORD</code>
2959    </dt>
2960
2961    <dd>Delete word at cursor.</dd>
2962
2963    <dt><code>REQ_CLR_EOL</code>
2964    </dt>
2965
2966    <dd>Clear to end of line.</dd>
2967
2968    <dt><code>REQ_CLR_EOF</code>
2969    </dt>
2970
2971    <dd>Clear to end of field.</dd>
2972
2973    <dt><code>REQ_CLEAR_FIELD</code>
2974    </dt>
2975
2976    <dd>Clear entire field.</dd>
2977  </dl>
2978
2979  <p>The behavior of the <code>REQ_NEW_LINE</code> and
2980  <code>REQ_DEL_PREV</code> requests is complicated and partly
2981  controlled by a pair of forms options. The special cases are
2982  triggered when the cursor is at the beginning of a field, or on
2983  the last line of the field.</p>
2984
2985  <p>First, we consider <code>REQ_NEW_LINE</code>:</p>
2986
2987  <p>The normal behavior of <code>REQ_NEW_LINE</code> in insert
2988  mode is to break the current line at the position of the edit
2989  cursor, inserting the portion of the current line after the
2990  cursor as a new line following the current and moving the cursor
2991  to the beginning of that new line (you may think of this as
2992  inserting a newline in the field buffer).</p>
2993
2994  <p>The normal behavior of <code>REQ_NEW_LINE</code> in overlay
2995  mode is to clear the current line from the position of the edit
2996  cursor to end of line. The cursor is then moved to the beginning
2997  of the next line.</p>
2998
2999  <p>However, <code>REQ_NEW_LINE</code> at the beginning of a
3000  field, or on the last line of a field, instead does a
3001  <code>REQ_NEXT_FIELD</code>. <code>O_NL_OVERLOAD</code> option is
3002  off, this special action is disabled.</p>
3003
3004  <p>Now, let us consider <code>REQ_DEL_PREV</code>:</p>
3005
3006  <p>The normal behavior of <code>REQ_DEL_PREV</code> is to delete
3007  the previous character. If insert mode is on, and the cursor is
3008  at the start of a line, and the text on that line will fit on the
3009  previous one, it instead appends the contents of the current line
3010  to the previous one and deletes the current line (you may think
3011  of this as deleting a newline from the field buffer).</p>
3012
3013  <p>However, <code>REQ_DEL_PREV</code> at the beginning of a field
3014  is instead treated as a <code>REQ_PREV_FIELD</code>.</p>
3015
3016  <p>If the <code>O_BS_OVERLOAD</code> option is off, this special
3017  action is disabled and the forms driver just returns
3018  <code>E_REQUEST_DENIED</code>.</p>
3019
3020  <p>See <a href="#frmoptions">Form Options</a> for discussion of
3021  how to set and clear the overload options.</p>
3022
3023  <h4><a name="forder" id="forder">Order Requests</a></h4>
3024
3025  <p>If the type of your field is ordered, and has associated
3026  functions for getting the next and previous values of the type
3027  from a given value, there are requests that can fetch that value
3028  into the field buffer:</p>
3029
3030  <dl>
3031    <dt><code>REQ_NEXT_CHOICE</code>
3032    </dt>
3033
3034    <dd>Place the successor value of the current value in the
3035    buffer.</dd>
3036
3037    <dt><code>REQ_PREV_CHOICE</code>
3038    </dt>
3039
3040    <dd>Place the predecessor value of the current value in the
3041    buffer.</dd>
3042  </dl>
3043
3044  <p>Of the built-in field types, only <code>TYPE_ENUM</code> has
3045  built-in successor and predecessor functions. When you define a
3046  field type of your own (see <a href="#fcustom">Custom Validation
3047  Types</a>), you can associate our own ordering functions.</p>
3048
3049  <h4><a name="fappcmds" id="fappcmds">Application Commands</a></h4>
3050
3051  <p>Form requests are represented as integers above the
3052  <code>curses</code> value greater than <code>KEY_MAX</code> and
3053  less than or equal to the constant <code>MAX_COMMAND</code>. If
3054  your input-virtualization routine returns a value above
3055  <code>MAX_COMMAND</code>, the forms driver will ignore it.</p>
3056
3057  <h3><a name="fhooks" id="fhooks">Field Change Hooks</a></h3>
3058
3059  <p>It is possible to set function hooks to be executed whenever
3060  the current field or form changes. Here are the functions that
3061  support this:</p>
3062
3063  <pre class="code-block">
3064typedef void    (*HOOK)();       /* pointer to function returning void */
3065
3066int set_form_init(FORM *form,    /* form to alter */
3067                  HOOK hook);    /* initialization hook */
3068
3069HOOK form_init(FORM *form);      /* form to query */
3070
3071int set_form_term(FORM *form,    /* form to alter */
3072                  HOOK hook);    /* termination hook */
3073
3074HOOK form_term(FORM *form);      /* form to query */
3075
3076int set_field_init(FORM *form,   /* form to alter */
3077                  HOOK hook);    /* initialization hook */
3078
3079HOOK field_init(FORM *form);     /* form to query */
3080
3081int set_field_term(FORM *form,   /* form to alter */
3082                  HOOK hook);    /* termination hook */
3083
3084HOOK field_term(FORM *form);     /* form to query */
3085</pre>
3086  <p>These functions allow you to either set or query four
3087  different hooks. In each of the set functions, the second
3088  argument should be the address of a hook function. These
3089  functions differ only in the timing of the hook call.</p>
3090
3091  <dl>
3092    <dt>form_init</dt>
3093
3094    <dd>This hook is called when the form is posted; also, just
3095    after each page change operation.</dd>
3096
3097    <dt>field_init</dt>
3098
3099    <dd>This hook is called when the form is posted; also, just
3100    after each field change</dd>
3101
3102    <dt>field_term</dt>
3103
3104    <dd>This hook is called just after field validation; that is,
3105    just before the field is altered. It is also called when the
3106    form is unposted.</dd>
3107
3108    <dt>form_term</dt>
3109
3110    <dd>This hook is called when the form is unposted; also, just
3111    before each page change operation.</dd>
3112  </dl>
3113
3114  <p>Calls to these hooks may be triggered</p>
3115
3116  <ol>
3117    <li>When user editing requests are processed by the forms
3118    driver</li>
3119
3120    <li>When the current page is changed by
3121    <code>set_current_field()</code> call</li>
3122
3123    <li>When the current field is changed by a
3124    <code>set_form_page()</code> call</li>
3125  </ol>
3126
3127  <p>See <a name="ffocus" id="ffocus">Field Change Commands</a> for
3128  discussion of the latter two cases.</p>
3129
3130  <p>You can set a default hook for all fields by passing one of
3131  the set functions a NULL first argument.</p>
3132
3133  <p>You can disable any of these hooks by (re)setting them to
3134  NULL, the default value.</p>
3135
3136  <h3><a href="#ffocus">Field Change Commands</a></h3>
3137
3138  <p>Normally, navigation through the form will be driven by the
3139  user's input requests. But sometimes it is useful to be able to
3140  move the focus for editing and viewing under control of your
3141  application, or ask which field it currently is in. The following
3142  functions help you accomplish this:</p>
3143
3144  <pre class="code-block">
3145int set_current_field(FORM *form,         /* form to alter */
3146                      FIELD *field);      /* field to shift to */
3147
3148FIELD *current_field(FORM *form);         /* form to query */
3149
3150int field_index(FORM *form,               /* form to query */
3151                FIELD *field);            /* field to get index of */
3152</pre>
3153  <p>The function <code>field_index()</code> returns the index of
3154  the given field in the given form's field array (the array passed
3155  to <code>new_form()</code> or
3156  <code>set_form_fields()</code>).</p>
3157
3158  <p>The initial current field of a form is the first active field
3159  on the first page. The function <code>set_form_fields()</code>
3160  resets this.</p>
3161
3162  <p>It is also possible to move around by pages.</p>
3163
3164  <pre class="code-block">
3165int set_form_page(FORM *form,             /* form to alter */
3166                  int page);              /* page to go to (0-origin) */
3167
3168int form_page(FORM *form);                /* return form's current page */
3169</pre>
3170  <p>The initial page of a newly-created form is 0. The function
3171  <code>set_form_fields()</code> resets this.</p>
3172
3173  <h3><a name="frmoptions" id="frmoptions">Form Options</a></h3>
3174
3175  <p>Like fields, forms may have control option bits. They can be
3176  changed or queried with these functions:</p>
3177
3178  <pre class="code-block">
3179int set_form_opts(FORM *form,             /* form to alter */
3180                  int attr);              /* attribute to set */
3181
3182int form_opts_on(FORM *form,              /* form to alter */
3183                 int attr);               /* attributes to turn on */
3184
3185int form_opts_off(FORM *form,             /* form to alter */
3186                  int attr);              /* attributes to turn off */
3187
3188int form_opts(FORM *form);                /* form to query */
3189</pre>
3190  <p>By default, all options are on. Here are the available option
3191  bits:</p>
3192
3193  <dl>
3194    <dt>O_NL_OVERLOAD</dt>
3195
3196    <dd>Enable overloading of <code>REQ_NEW_LINE</code> as
3197    described in <a href="#fedit">Editing Requests</a>. The value
3198    of this option is ignored on dynamic fields that have not
3199    reached their size limit; these have no last line, so the
3200    circumstances for triggering a <code>REQ_NEXT_FIELD</code>
3201    never arise.</dd>
3202
3203    <dt>O_BS_OVERLOAD</dt>
3204
3205    <dd>Enable overloading of <code>REQ_DEL_PREV</code> as
3206    described in <a href="#fedit">Editing Requests</a>.</dd>
3207  </dl>
3208
3209  <p>The option values are bit-masks and can be composed with
3210  logical-or in the obvious way.</p>
3211
3212  <h3><a name="fcustom" id="fcustom">Custom Validation Types</a></h3>
3213
3214  <p>The <code>form</code> library gives you the capability to
3215  define custom validation types of your own. Further, the optional
3216  additional arguments of <code>set_field_type</code> effectively
3217  allow you to parameterize validation types. Most of the
3218  complications in the validation-type interface have to do with
3219  the handling of the additional arguments within custom validation
3220  functions.</p>
3221
3222  <h4><a name="flinktypes" id="flinktypes">Union Types</a></h4>
3223
3224  <p>The simplest way to create a custom data type is to compose it
3225  from two preexisting ones:</p>
3226
3227  <pre class="code-block">
3228FIELD *link_fieldtype(FIELDTYPE *type1,
3229                      FIELDTYPE *type2);
3230</pre>
3231  <p>This function creates a field type that will accept any of the
3232  values legal for either of its argument field types (which may be
3233  either predefined or programmer-defined). If a
3234  <code>set_field_type()</code> call later requires arguments, the
3235  new composite type expects all arguments for the first type, than
3236  all arguments for the second. Order functions (see <a href=
3237  "#forder">Order Requests</a>) associated with the component types
3238  will work on the composite; what it does is check the validation
3239  function for the first type, then for the second, to figure what
3240  type the buffer contents should be treated as.</p>
3241
3242  <h4><a name="fnewtypes" id="fnewtypes">New Field Types</a></h4>
3243
3244  <p>To create a field type from scratch, you need to specify one
3245  or both of the following things:</p>
3246
3247  <ul>
3248    <li>A character-validation function, to check each character as
3249    it is entered.</li>
3250
3251    <li>A field-validation function to be applied on exit from the
3252    field.</li>
3253  </ul>
3254
3255  <p>Here is how you do that:</p>
3256
3257  <pre class="code-block">
3258typedef int     (*HOOK)();       /* pointer to function returning int */
3259
3260FIELDTYPE *new_fieldtype(HOOK f_validate, /* field validator */
3261                         HOOK c_validate) /* character validator */
3262
3263int free_fieldtype(FIELDTYPE *ftype);     /* type to free */
3264</pre>
3265  <p>At least one of the arguments of <code>new_fieldtype()</code>
3266  must be non-NULL. The forms driver will automatically call the
3267  new type's validation functions at appropriate points in
3268  processing a field of the new type.</p>
3269
3270  <p>The function <code>free_fieldtype()</code> deallocates the
3271  argument fieldtype, freeing all storage associated with it.</p>
3272
3273  <p>Normally, a field validator is called when the user attempts
3274  to leave the field. Its first argument is a field pointer, from
3275  which it can get to field buffer 0 and test it. If the function
3276  returns TRUE, the operation succeeds; if it returns FALSE, the
3277  edit cursor stays in the field.</p>
3278
3279  <p>A character validator gets the character passed in as a first
3280  argument. It too should return TRUE if the character is valid,
3281  FALSE otherwise.</p>
3282
3283  <h4><a name="fcheckargs" id="fcheckargs">Validation Function
3284  Arguments</a></h4>
3285
3286  <p>Your field- and character- validation functions will be passed
3287  a second argument as well. This second argument is the address of
3288  a structure (which we will call a <em>pile</em>) built from any
3289  of the field-type-specific arguments passed to
3290  <code>set_field_type()</code>. If no such arguments are defined
3291  for the field type, this pile pointer argument will be NULL.</p>
3292
3293  <p>In order to arrange for such arguments to be passed to your
3294  validation functions, you must associate a small set of
3295  storage-management functions with the type. The forms driver will
3296  use these to synthesize a pile from the trailing arguments of
3297  each <code>set_field_type()</code> argument, and a pointer to the
3298  pile will be passed to the validation functions.</p>
3299
3300  <p>Here is how you make the association:</p>
3301
3302  <pre class="code-block">
3303typedef char    *(*PTRHOOK)();    /* pointer to function returning (char *) */
3304typedef void    (*VOIDHOOK)();    /* pointer to function returning void */
3305
3306int set_fieldtype_arg(FIELDTYPE *type,    /* type to alter */
3307                      PTRHOOK make_str,   /* make structure from args */
3308                      PTRHOOK copy_str,   /* make copy of structure */
3309                      VOIDHOOK free_str); /* free structure storage */
3310</pre>
3311  <p>Here is how the storage-management hooks are used:</p>
3312
3313  <dl>
3314    <dt><code>make_str</code>
3315    </dt>
3316
3317    <dd>This function is called by <code>set_field_type()</code>.
3318    It gets one argument, a <code>va_list</code> of the
3319    type-specific arguments passed to
3320    <code>set_field_type()</code>. It is expected to return a pile
3321    pointer to a data structure that encapsulates those
3322    arguments.</dd>
3323
3324    <dt><code>copy_str</code>
3325    </dt>
3326
3327    <dd>This function is called by form library functions that
3328    allocate new field instances. It is expected to take a pile
3329    pointer, copy the pile to allocated storage, and return the
3330    address of the pile copy.</dd>
3331
3332    <dt><code>free_str</code>
3333    </dt>
3334
3335    <dd>This function is called by field- and type-deallocation
3336    routines in the library. It takes a pile pointer argument, and
3337    is expected to free the storage of that pile.</dd>
3338  </dl>
3339
3340  <p>The <code>make_str</code> and <code>copy_str</code> functions
3341  may return NULL to signal allocation failure. The library
3342  routines will that call them will return error indication when
3343  this happens. Thus, your validation functions should never see a
3344  NULL file pointer and need not check specially for it.</p>
3345
3346  <h4><a name="fcustorder" id="fcustorder">Order Functions For
3347  Custom Types</a></h4>
3348
3349  <p>Some custom field types are simply ordered in the same
3350  well-defined way that <code>TYPE_ENUM</code> is. For such types,
3351  it is possible to define successor and predecessor functions to
3352  support the <code>REQ_NEXT_CHOICE</code> and
3353  <code>REQ_PREV_CHOICE</code> requests. Here is how:</p>
3354
3355  <pre class="code-block">
3356typedef int     (*INTHOOK)();     /* pointer to function returning int */
3357
3358int set_fieldtype_arg(FIELDTYPE *type,    /* type to alter */
3359                      INTHOOK succ,       /* get successor value */
3360                      INTHOOK pred);      /* get predecessor value */
3361</pre>
3362  <p>The successor and predecessor arguments will each be passed
3363  two arguments; a field pointer, and a pile pointer (as for the
3364  validation functions). They are expected to use the function
3365  <code>field_buffer()</code> to read the current value, and
3366  <code>set_field_buffer()</code> on buffer 0 to set the next or
3367  previous value. Either hook may return TRUE to indicate success
3368  (a legal next or previous value was set) or FALSE to indicate
3369  failure.</p>
3370
3371  <h4><a name="fcustprobs" id="fcustprobs">Avoiding Problems</a></h4>
3372
3373  <p>The interface for defining custom types is complicated and
3374  tricky. Rather than attempting to create a custom type entirely
3375  from scratch, you should start by studying the library source
3376  code for whichever of the pre-defined types seems to be closest
3377  to what you want.</p>
3378
3379  <p>Use that code as a model, and evolve it towards what you
3380  really want. You will avoid many problems and annoyances that
3381  way. The code in the <code>ncurses</code> library has been
3382  specifically exempted from the package copyright to support
3383  this.</p>
3384
3385  <p>If your custom type defines order functions, have do something
3386  intuitive with a blank field. A useful convention is to make the
3387  successor of a blank field the types minimum value, and its
3388  predecessor the maximum.</p>
3389</body>
3390</html>
3391