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