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