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