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