1243327Sgrehan<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"> 2243327Sgrehan<!-- 3243327Sgrehan $Id: ncurses-intro.html,v 1.46 2013/05/17 23:29:27 tom Exp $ 4243327Sgrehan **************************************************************************** 5243327Sgrehan * Copyright (c) 1998-2012,2013 Free Software Foundation, Inc. * 6243327Sgrehan * * 7243327Sgrehan * Permission is hereby granted, free of charge, to any person obtaining a * 8243327Sgrehan * copy of this software and associated documentation files (the * 9243327Sgrehan * "Software"), to deal in the Software without restriction, including * 10243327Sgrehan * without limitation the rights to use, copy, modify, merge, publish, * 11243327Sgrehan * distribute, distribute with modifications, sublicense, and/or sell * 12243327Sgrehan * copies of the Software, and to permit persons to whom the Software is * 13243327Sgrehan * furnished to do so, subject to the following conditions: * 14243327Sgrehan * * 15243327Sgrehan * The above copyright notice and this permission notice shall be included * 16243327Sgrehan * in all copies or substantial portions of the Software. * 17243327Sgrehan * * 18243327Sgrehan * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * 19243327Sgrehan * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * 20243327Sgrehan * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * 21243327Sgrehan * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 22243327Sgrehan * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 23243327Sgrehan * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * 24243327Sgrehan * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * 25243327Sgrehan * * 26243327Sgrehan * Except as contained in this notice, the name(s) of the above copyright * 27243327Sgrehan * holders shall not be used in advertising or otherwise to promote the * 28243327Sgrehan * sale, use or other dealings in this Software without prior written * 29243327Sgrehan * authorization. * 30243327Sgrehan **************************************************************************** 31243327Sgrehan--> 32243327Sgrehan<HTML> 33243327Sgrehan<HEAD> 34243327Sgrehan<TITLE>Writing Programs with NCURSES</TITLE> 35243327Sgrehan<link rev="made" href="mailto:bugs-ncurses@gnu.org"> 36243327Sgrehan<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 37243327Sgrehan</HEAD> 38243327Sgrehan<BODY> 39243327Sgrehan 40243327Sgrehan<H1>Writing Programs with NCURSES</H1> 41243327Sgrehan 42267450Sjhb<BLOCKQUOTE> 43270159Sgrehanby Eric S. Raymond and Zeyd M. Ben-Halim<BR> 44270159Sgrehanupdates since release 1.9.9e by Thomas Dickey 45267450Sjhb</BLOCKQUOTE> 46267450Sjhb 47267450Sjhb<H1>Contents</H1> 48270159Sgrehan<UL> 49270159Sgrehan<LI><A HREF="#introduction">Introduction</A> 50267450Sjhb<UL> 51243327Sgrehan<LI><A HREF="#history">A Brief History of Curses</A> 52243327Sgrehan<LI><A HREF="#scope">Scope of This Document</A> 53243327Sgrehan<LI><A HREF="#terminology">Terminology</A> 54243327Sgrehan</UL> 55243327Sgrehan<LI><A HREF="#curses">The Curses Library</A> 56243327Sgrehan<UL> 57243327Sgrehan<LI><A HREF="#overview">An Overview of Curses</A> 58243327Sgrehan<UL> 59243327Sgrehan<LI><A HREF="#compiling">Compiling Programs using Curses</A> 60243327Sgrehan<LI><A HREF="#updating">Updating the Screen</A> 61261265Sjhb<LI><A HREF="#stdscr">Standard Windows and Function Naming Conventions</A> 62243327Sgrehan<LI><A HREF="#variables">Variables</A> 63243327Sgrehan</UL> 64243327Sgrehan<LI><A HREF="#using">Using the Library</A> 65243327Sgrehan<UL> 66243327Sgrehan<LI><A HREF="#starting">Starting up</A> 67261088Sjhb<LI><A HREF="#output">Output</A> 68261088Sjhb<LI><A HREF="#input">Input</A> 69261088Sjhb<LI><A HREF="#formschars">Using Forms Characters</A> 70244167Sgrehan<LI><A HREF="#attributes">Character Attributes and Color</A> 71243327Sgrehan<LI><A HREF="#mouse">Mouse Interfacing</A> 72261265Sjhb<LI><A HREF="#finishing">Finishing Up</A> 73243327Sgrehan</UL> 74243327Sgrehan<LI><A HREF="#functions">Function Descriptions</A> 75243327Sgrehan<UL> 76243327Sgrehan<LI><A HREF="#init">Initialization and Wrapup</A> 77243327Sgrehan<LI><A HREF="#flush">Causing Output to the Terminal</A> 78267450Sjhb<LI><A HREF="#lowlevel">Low-Level Capability Access</A> 79243327Sgrehan<LI><A HREF="#debugging">Debugging</A> 80243704Sgrehan</UL> 81243704Sgrehan<LI><A HREF="#hints">Hints, Tips, and Tricks</A> 82243327Sgrehan<UL> 83261088Sjhb<LI><A HREF="#caution">Some Notes of Caution</A> 84270159Sgrehan<LI><A HREF="#leaving">Temporarily Leaving ncurses Mode</A> 85270159Sgrehan<LI><A HREF="#xterm">Using <CODE>ncurses</CODE> under <CODE>xterm</CODE></A> 86243327Sgrehan<LI><A HREF="#screens">Handling Multiple Terminal Screens</A> 87243327Sgrehan<LI><A HREF="#testing">Testing for Terminal Capabilities</A> 88243327Sgrehan<LI><A HREF="#tuning">Tuning for Speed</A> 89243327Sgrehan<LI><A HREF="#special">Special Features of <CODE>ncurses</CODE></A> 90243327Sgrehan</UL> 91243327Sgrehan<LI><A HREF="#compat">Compatibility with Older Versions</A> 92243327Sgrehan<UL> 93243327Sgrehan<LI><A HREF="#refbug">Refresh of Overlapping Windows</A> 94243327Sgrehan<LI><A HREF="#backbug">Background Erase</A> 95243327Sgrehan</UL> 96261088Sjhb<LI><A HREF="#xsifuncs">XSI Curses Conformance</A> 97243327Sgrehan</UL> 98243327Sgrehan<LI><A HREF="#panels">The Panels Library</A> 99243327Sgrehan<UL> 100243327Sgrehan<LI><A HREF="#pcompile">Compiling With the Panels Library</A> 101243327Sgrehan<LI><A HREF="#poverview">Overview of Panels</A> 102243327Sgrehan<LI><A HREF="#pstdscr">Panels, Input, and the Standard Screen</A> 103243327Sgrehan<LI><A HREF="#hiding">Hiding Panels</A> 104243327Sgrehan<LI><A HREF="#pmisc">Miscellaneous Other Facilities</A> 105261265Sjhb</UL> 106261265Sjhb<LI><A HREF="#menu">The Menu Library</A> 107261265Sjhb<UL> 108261265Sjhb<LI><A HREF="#mcompile">Compiling with the menu Library</A> 109261265Sjhb<LI><A HREF="#moverview">Overview of Menus</A> 110261265Sjhb<LI><A HREF="#mselect">Selecting items</A> 111261265Sjhb<LI><A HREF="#mdisplay">Menu Display</A> 112243327Sgrehan<LI><A HREF="#mwindows">Menu Windows</A> 113243327Sgrehan<LI><A HREF="#minput">Processing Menu Input</A> 114243327Sgrehan<LI><A HREF="#mmisc">Miscellaneous Other Features</A> 115243327Sgrehan</UL> 116243327Sgrehan<LI><A HREF="#form">The Forms Library</A> 117243327Sgrehan<UL> 118243327Sgrehan<LI><A HREF="#fcompile">Compiling with the forms Library</A> 119243327Sgrehan<LI><A HREF="#foverview">Overview of Forms</A> 120243327Sgrehan<LI><A HREF="#fcreate">Creating and Freeing Fields and Forms</A> 121243327Sgrehan<LI><A HREF="#fattributes">Fetching and Changing Field Attributes</A> 122243327Sgrehan<UL> 123243327Sgrehan<LI><A HREF="#fsizes">Fetching Size and Location Data</A> 124243327Sgrehan<LI><A HREF="#flocation">Changing the Field Location</A> 125243327Sgrehan<LI><A HREF="#fjust">The Justification Attribute</A> 126243327Sgrehan<LI><A HREF="#fdispatts">Field Display Attributes</A> 127243327Sgrehan<LI><A HREF="#foptions">Field Option Bits</A> 128243327Sgrehan<LI><A HREF="#fstatus">Field Status</A> 129243327Sgrehan<LI><A HREF="#fuser">Field User Pointer</A> 130243327Sgrehan</UL> 131243327Sgrehan<LI><A HREF="#fdynamic">Variable-Sized Fields</A> 132243327Sgrehan<LI><A HREF="#fvalidation">Field Validation</A> 133243327Sgrehan<UL> 134243327Sgrehan<LI><A HREF="#ftype_alpha">TYPE_ALPHA</A> 135243327Sgrehan<LI><A HREF="#ftype_alnum">TYPE_ALNUM</A> 136243327Sgrehan<LI><A HREF="#ftype_enum">TYPE_ENUM</A> 137243327Sgrehan<LI><A HREF="#ftype_integer">TYPE_INTEGER</A> 138243327Sgrehan<LI><A HREF="#ftype_numeric">TYPE_NUMERIC</A> 139243327Sgrehan<LI><A HREF="#ftype_regexp">TYPE_REGEXP</A> 140243327Sgrehan</UL> 141243704Sgrehan<LI><A HREF="#fbuffer">Direct Field Buffer Manipulation</A> 142243704Sgrehan<LI><A HREF="#formattrs">Attributes of Forms</A> 143243327Sgrehan<LI><A HREF="#fdisplay">Control of Form Display</A> 144243327Sgrehan<LI><A HREF="#fdriver">Input Processing in the Forms Driver</A> 145243327Sgrehan<UL> 146243327Sgrehan<LI><A HREF="#fpage">Page Navigation Requests</A> 147243327Sgrehan<LI><A HREF="#ffield">Inter-Field Navigation Requests</A> 148243327Sgrehan<LI><A HREF="#fifield">Intra-Field Navigation Requests</A> 149243327Sgrehan<LI><A HREF="#fscroll">Scrolling Requests</A> 150243327Sgrehan<LI><A HREF="#fedit">Field Editing Requests</A> 151243327Sgrehan<LI><A HREF="#forder">Order Requests</A> 152243327Sgrehan<LI><A HREF="#fappcmds">Application Commands</A> 153243327Sgrehan</UL> 154243327Sgrehan<LI><A HREF="#fhooks">Field Change Hooks</A> 155243327Sgrehan<LI><A HREF="#ffocus">Field Change Commands</A> 156243327Sgrehan<LI><A HREF="#frmoptions">Form Options</A> 157243327Sgrehan<LI><A HREF="#fcustom">Custom Validation Types</A> 158243327Sgrehan<UL> 159243327Sgrehan<LI><A HREF="#flinktypes">Union Types</A> 160243327Sgrehan<LI><A HREF="#fnewtypes">New Field Types</A> 161243327Sgrehan<LI><A HREF="#fcheckargs">Validation Function Arguments</A> 162243327Sgrehan<LI><A HREF="#fcustorder">Order Functions For Custom Types</A> 163243327Sgrehan<LI><A HREF="#fcustprobs">Avoiding Problems</A> 164243327Sgrehan</UL> 165243327Sgrehan</UL> 166243327Sgrehan</UL> 167243327Sgrehan 168243327Sgrehan<HR> 169243327Sgrehan<H1><A NAME="introduction">Introduction</A></H1> 170243327Sgrehan 171243327SgrehanThis document is an introduction to programming with <CODE>curses</CODE>. It is 172243327Sgrehannot an exhaustive reference for the curses Application Programming Interface 173243327Sgrehan(API); that role is filled by the <CODE>curses</CODE> manual pages. Rather, it 174243327Sgrehanis intended to help C programmers ease into using the package. <P> 175243327Sgrehan 176261088SjhbThis document is aimed at C applications programmers not yet specifically 177243327Sgrehanfamiliar with ncurses. If you are already an experienced <CODE>curses</CODE> 178243327Sgrehanprogrammer, you should nevertheless read the sections on 179243327Sgrehan<A HREF="#mouse">Mouse Interfacing</A>, <A HREF="#debugging">Debugging</A>, 180243327Sgrehan<A HREF="#compat">Compatibility with Older Versions</A>, 181261088Sjhband <A HREF="#hints">Hints, Tips, and Tricks</A>. These will bring you up 182261088Sjhbto speed on the special features and quirks of the <CODE>ncurses</CODE> 183270159Sgrehanimplementation. If you are not so experienced, keep reading. <P> 184270159Sgrehan 185243327SgrehanThe <CODE>curses</CODE> package is a subroutine library for 186243327Sgrehanterminal-independent screen-painting and input-event handling which 187243327Sgrehanpresents a high level screen model to the programmer, hiding differences 188243327Sgrehanbetween terminal types and doing automatic optimization of output to change 189243327Sgrehanone screen full of text into another. <CODE>Curses</CODE> uses terminfo, which 190243327Sgrehanis a database format that can describe the capabilities of thousands of 191243327Sgrehandifferent terminals. <P> 192243327Sgrehan 193243327SgrehanThe <CODE>curses</CODE> API may seem something of an archaism on UNIX desktops 194243327Sgrehanincreasingly dominated by X, Motif, and Tcl/Tk. Nevertheless, UNIX still 195243704Sgrehansupports tty lines and X supports <EM>xterm(1)</EM>; the <CODE>curses</CODE> 196243704SgrehanAPI has the advantage of (a) back-portability to character-cell terminals, 197243704Sgrehanand (b) simplicity. For an application that does not require bit-mapped 198243704Sgrehangraphics and multiple fonts, an interface implementation using <CODE>curses</CODE> 199243704Sgrehanwill typically be a great deal simpler and less expensive than one using an 200243704SgrehanX toolkit. 201243704Sgrehan 202243704Sgrehan<H2><A NAME="history">A Brief History of Curses</A></H2> 203243704Sgrehan 204243704SgrehanHistorically, the first ancestor of <CODE>curses</CODE> was the routines written to 205243704Sgrehanprovide screen-handling for the game <CODE>rogue</CODE>; these used the 206243704Sgrehanalready-existing <CODE>termcap</CODE> database facility for describing terminal 207243704Sgrehancapabilities. These routines were abstracted into a documented library and 208243704Sgrehanfirst released with the early BSD UNIX versions. <P> 209243704Sgrehan 210243704SgrehanSystem III UNIX from Bell Labs featured a rewritten and much-improved 211243704Sgrehan<CODE>curses</CODE> library. It introduced the terminfo format. Terminfo is based 212243704Sgrehanon Berkeley's termcap database, but contains a number of improvements and 213243704Sgrehanextensions. Parameterized capabilities strings were introduced, making it 214243704Sgrehanpossible to describe multiple video attributes, and colors and to handle far 215243704Sgrehanmore unusual terminals than possible with termcap. In the later AT&T 216261088SjhbSystem V releases, <CODE>curses</CODE> evolved to use more facilities and offer 217243704Sgrehanmore capabilities, going far beyond BSD curses in power and flexibility. 218243704Sgrehan 219243704Sgrehan<H2><A NAME="scope">Scope of This Document</A></H2> 220243704Sgrehan 221261088SjhbThis document describes <CODE>ncurses</CODE>, a free implementation of 222261088Sjhbthe System V <CODE>curses</CODE> API with some clearly marked extensions. 223270159SgrehanIt includes the following System V curses features: 224270159Sgrehan<UL> 225243704Sgrehan<LI>Support for multiple screen highlights (BSD curses could only 226243704Sgrehanhandle one `standout' highlight, usually reverse-video). 227243704Sgrehan<LI>Support for line- and box-drawing using forms characters. 228243704Sgrehan<LI>Recognition of function keys on input. 229243704Sgrehan<LI>Color support. 230243704Sgrehan<LI>Support for pads (windows of larger than screen size on which the 231243704Sgrehanscreen or a subwindow defines a viewport). 232243704Sgrehan</UL> 233243704Sgrehan 234243704SgrehanAlso, this package makes use of the insert and delete line and character 235243327Sgrehanfeatures of terminals so equipped, and determines how to optimally use these 236243327Sgrehanfeatures with no help from the programmer. It allows arbitrary combinations of 237243327Sgrehanvideo attributes to be displayed, even on terminals that leave ``magic 238243327Sgrehancookies'' on the screen to mark changes in attributes. <P> 239243327Sgrehan 240243327SgrehanThe <CODE>ncurses</CODE> package can also capture and use event reports from a 241243327Sgrehanmouse in some environments (notably, xterm under the X window system). This 242243327Sgrehandocument includes tips for using the mouse. <P> 243243327Sgrehan 244243327SgrehanThe <CODE>ncurses</CODE> package was originated by Pavel Curtis. The original 245243327Sgrehanmaintainer of this package is 246243327Sgrehan<A HREF="mailto:zmbenhal@netcom.com">Zeyd Ben-Halim</A> 247243327Sgrehan<zmbenhal@netcom.com>. 248243327Sgrehan<A HREF="mailto:esr@snark.thyrsus.com">Eric S. Raymond</A> 249243327Sgrehan<esr@snark.thyrsus.com> 250243327Sgrehanwrote many of the new features in versions after 1.8.1 251243327Sgrehanand wrote most of this introduction. 252243327SgrehanJürgen Pfeifer 253243327Sgrehanwrote all of the menu and forms code as well as the 254243327Sgrehan<A HREF="http://www.adahome.com">Ada95</A> binding. 255243327SgrehanOngoing work is being done by 256243327Sgrehan<A HREF="mailto:dickey@invisible-island.net">Thomas Dickey</A> (maintainer). 257243327SgrehanContact the current maintainers at 258243327Sgrehan<A HREF="mailto:bug-ncurses@gnu.org">bug-ncurses@gnu.org</A>. 259243327Sgrehan<P> 260243327Sgrehan 261243327SgrehanThis document also describes the <A HREF="#panels">panels</A> extension library, 262243327Sgrehansimilarly modeled on the SVr4 panels facility. This library allows you to 263243327Sgrehanassociate backing store with each of a stack or deck of overlapping windows, 264243327Sgrehanand provides operations for moving windows around in the stack that change 265243327Sgrehantheir visibility in the natural way (handling window overlaps). <P> 266243327Sgrehan 267255438SgrehanFinally, this document describes in detail the <A HREF="#menu">menus</A> and <A 268255438SgrehanHREF="#form">forms</A> extension libraries, also cloned from System V, 269255438Sgrehanwhich support easy construction and sequences of menus and fill-in 270243327Sgrehanforms. 271243327Sgrehan 272243327Sgrehan 273243327Sgrehan<H2><A NAME="terminology">Terminology</A></H2> 274243327Sgrehan 275261088SjhbIn this document, the following terminology is used with reasonable 276243327Sgrehanconsistency: 277243327Sgrehan 278255438Sgrehan<DL> 279259301Sgrehan<DT> window 280243327Sgrehan<DD> 281243327SgrehanA data structure describing a sub-rectangle of the screen (possibly the 282243327Sgrehanentire screen). You can write to a window as though it were a miniature 283243327Sgrehanscreen, scrolling independently of other windows on the physical screen. 284243327Sgrehan<DT> screens 285259301Sgrehan<DD> 286243327SgrehanA subset of windows which are as large as the terminal screen, i.e., they start 287243327Sgrehanat the upper left hand corner and encompass the lower right hand corner. One 288243327Sgrehanof these, <CODE>stdscr</CODE>, is automatically provided for the programmer. 289259301Sgrehan<DT> terminal screen 290259301Sgrehan<DD> 291259301SgrehanThe package's idea of what the terminal display currently looks like, i.e., 292259301Sgrehanwhat the user sees now. This is a special screen. 293259301Sgrehan</DL> 294259301Sgrehan 295259301Sgrehan<H1><A NAME="curses">The Curses Library</A></H1> 296259301Sgrehan 297259301Sgrehan<H2><A NAME="overview">An Overview of Curses</A></H2> 298259301Sgrehan 299261090Sjhb<H3><A NAME="compiling">Compiling Programs using Curses</A></H3> 300261090Sjhb 301243327SgrehanIn order to use the library, it is necessary to have certain types and 302261090Sjhbvariables defined. Therefore, the programmer must have a line: 303261090Sjhb 304243327Sgrehan<PRE> 305243327Sgrehan #include <curses.h> 306262350Sjhb</PRE> 307262350Sjhb 308262350Sjhbat the top of the program source. The screen package uses the Standard I/O 309262350Sjhblibrary, so <CODE><curses.h></CODE> includes 310262350Sjhb<CODE><stdio.h></CODE>. <CODE><curses.h></CODE> also includes 311262350Sjhb<CODE><termios.h></CODE>, <CODE><termio.h></CODE>, or 312262350Sjhb<CODE><sgtty.h></CODE> depending on your system. It is redundant (but 313262350Sjhbharmless) for the programmer to do these includes, too. In linking with 314262350Sjhb<CODE>curses</CODE> you need to have <CODE>-lncurses</CODE> in your LDFLAGS or on the 315262350Sjhbcommand line. There is no need for any other libraries. 316243327Sgrehan 317243327Sgrehan<H3><A NAME="updating">Updating the Screen</A></H3> 318243327Sgrehan 319243327SgrehanIn order to update the screen optimally, it is necessary for the routines to 320243327Sgrehanknow what the screen currently looks like and what the programmer wants it to 321243327Sgrehanlook like next. For this purpose, a data type (structure) named WINDOW is 322243327Sgrehandefined which describes a window image to the routines, including its starting 323243327Sgrehanposition on the screen (the (y, x) coordinates of the upper left hand corner) 324243327Sgrehanand its size. One of these (called <CODE>curscr</CODE>, for current screen) is a 325243327Sgrehanscreen image of what the terminal currently looks like. Another screen (called 326243327Sgrehan<CODE>stdscr</CODE>, for standard screen) is provided by default to make changes 327243327Sgrehanon. <P> 328243327Sgrehan 329243327SgrehanA window is a purely internal representation. It is used to build and store a 330243327Sgrehanpotential image of a portion of the terminal. It doesn't bear any necessary 331243327Sgrehanrelation to what is really on the terminal screen; it's more like a 332243327Sgrehanscratchpad or write buffer. <P> 333243327Sgrehan 334243327SgrehanTo make the section of physical screen corresponding to a window reflect the 335243327Sgrehancontents of the window structure, the routine <CODE>refresh()</CODE> (or 336243327Sgrehan<CODE>wrefresh()</CODE> if the window is not <CODE>stdscr</CODE>) is called. <P> 337243327Sgrehan 338243327SgrehanA given physical screen section may be within the scope of any number of 339243327Sgrehanoverlapping windows. Also, changes can be made to windows in any order, 340243327Sgrehanwithout regard to motion efficiency. Then, at will, the programmer can 341243327Sgrehaneffectively say ``make it look like this,'' and let the package implementation 342243327Sgrehandetermine the most efficient way to repaint the screen. 343243327Sgrehan 344243327Sgrehan<H3><A NAME="stdscr">Standard Windows and Function Naming Conventions</A></H3> 345243327Sgrehan 346243327SgrehanAs hinted above, the routines can use several windows, but two are 347243327Sgrehanautomatically given: <CODE>curscr</CODE>, which knows what the terminal looks like, 348243327Sgrehanand <CODE>stdscr</CODE>, which is what the programmer wants the terminal to look 349243327Sgrehanlike next. The user should never actually access <CODE>curscr</CODE> directly. 350261090SjhbChanges should be made to through the API, and then the routine 351243327Sgrehan<CODE>refresh()</CODE> (or <CODE>wrefresh()</CODE>) called. <P> 352261090Sjhb 353261090SjhbMany functions are defined to use <CODE>stdscr</CODE> as a default screen. For 354261090Sjhbexample, to add a character to <CODE>stdscr</CODE>, one calls <CODE>addch()</CODE> with 355261090Sjhbthe desired character as argument. To write to a different window. use the 356261090Sjhbroutine <CODE>waddch()</CODE> (for `w'indow-specific addch()) is provided. This 357261090Sjhbconvention of prepending function names with a `w' when they are to be 358261090Sjhbapplied to specific windows is consistent. The only routines which do not 359261090Sjhbfollow it are those for which a window must always be specified. <P> 360243327Sgrehan 361243327SgrehanIn order to move the current (y, x) coordinates from one point to another, the 362261090Sjhbroutines <CODE>move()</CODE> and <CODE>wmove()</CODE> are provided. However, it is 363261090Sjhboften desirable to first move and then perform some I/O operation. In order to 364243327Sgrehanavoid clumsiness, most I/O routines can be preceded by the prefix 'mv' and 365261090Sjhbthe desired (y, x) coordinates prepended to the arguments to the function. For 366261090Sjhbexample, the calls 367243327Sgrehan 368243327Sgrehan<PRE> 369243327Sgrehan move(y, x); 370261090Sjhb addch(ch); 371243327Sgrehan</PRE> 372243327Sgrehan 373243327Sgrehancan be replaced by 374243327Sgrehan 375243327Sgrehan<PRE> 376243327Sgrehan mvaddch(y, x, ch); 377243327Sgrehan</PRE> 378243327Sgrehan 379243327Sgrehanand 380243327Sgrehan 381243327Sgrehan<PRE> 382243327Sgrehan wmove(win, y, x); 383243327Sgrehan waddch(win, ch); 384243327Sgrehan</PRE> 385243327Sgrehan 386243327Sgrehancan be replaced by 387243327Sgrehan 388243327Sgrehan<PRE> 389284899Sneel mvwaddch(win, y, x, ch); 390243327Sgrehan</PRE> 391243327Sgrehan 392243327SgrehanNote that the window description pointer (win) comes before the added (y, x) 393243327Sgrehancoordinates. If a function requires a window pointer, it is always the first 394243327Sgrehanparameter passed. 395243327Sgrehan 396243327Sgrehan<H3><A NAME="variables">Variables</A></H3> 397243327Sgrehan 398243327SgrehanThe <CODE>curses</CODE> library sets some variables describing the terminal 399243327Sgrehancapabilities. 400243327Sgrehan 401261090Sjhb<PRE> 402243327Sgrehan type name description 403261090Sjhb ------------------------------------------------------------------ 404243327Sgrehan int LINES number of lines on the terminal 405243327Sgrehan int COLS number of columns on the terminal 406243327Sgrehan</PRE> 407243327Sgrehan 408243327SgrehanThe <CODE>curses.h</CODE> also introduces some <CODE>#define</CODE> constants and types 409261090Sjhbof general usefulness: 410243327Sgrehan 411243327Sgrehan<DL> 412243327Sgrehan<DT> <CODE>bool</CODE> 413243327Sgrehan<DD> boolean type, actually a `char' (e.g., <CODE>bool doneit;</CODE>) 414243327Sgrehan<DT> <CODE>TRUE</CODE> 415243327Sgrehan<DD> boolean `true' flag (1). 416243327Sgrehan<DT> <CODE>FALSE</CODE> 417243327Sgrehan<DD> boolean `false' flag (0). 418243327Sgrehan<DT> <CODE>ERR</CODE> 419243327Sgrehan<DD> error flag returned by routines on a failure (-1). 420243327Sgrehan<DT> <CODE>OK</CODE> 421243327Sgrehan<DD> error flag returned by routines when things go right. 422243327Sgrehan</DL> 423243327Sgrehan 424243327Sgrehan<H2><A NAME="using">Using the Library</A></H2> 425243327Sgrehan 426243327SgrehanNow we describe how to actually use the screen package. In it, we assume all 427243327Sgrehanupdating, reading, etc. is applied to <CODE>stdscr</CODE>. These instructions will 428243327Sgrehanwork on any window, providing you change the function names and parameters as 429261090Sjhbmentioned above. <P> 430243327Sgrehan 431243327SgrehanHere is a sample program to motivate the discussion: 432261090Sjhb 433281134Sneel<PRE> 434281134Sneel#include <stdlib.h> 435281134Sneel#include <curses.h> 436281134Sneel#include <signal.h> 437243327Sgrehan 438243327Sgrehanstatic void finish(int sig); 439243327Sgrehan 440243327Sgrehanint 441243327Sgrehanmain(int argc, char *argv[]) 442243327Sgrehan{ 443243327Sgrehan int num = 0; 444243327Sgrehan 445243327Sgrehan /* initialize your non-curses data structures here */ 446243327Sgrehan 447261090Sjhb (void) signal(SIGINT, finish); /* arrange interrupts to terminate */ 448261090Sjhb 449243327Sgrehan (void) initscr(); /* initialize the curses library */ 450243327Sgrehan keypad(stdscr, TRUE); /* enable keyboard mapping */ 451243327Sgrehan (void) nonl(); /* tell curses not to do NL->CR/NL on output */ 452243327Sgrehan (void) cbreak(); /* take input chars one at a time, no wait for \n */ 453243327Sgrehan (void) echo(); /* echo input - in color */ 454243327Sgrehan 455243327Sgrehan if (has_colors()) 456243327Sgrehan { 457243327Sgrehan start_color(); 458243327Sgrehan 459243327Sgrehan /* 460243327Sgrehan * Simple color assignment, often all we need. Color pair 0 cannot 461243327Sgrehan * be redefined. This example uses the same value for the color 462243327Sgrehan * pair as for the foreground color, though of course that is not 463243327Sgrehan * necessary: 464243327Sgrehan */ 465243327Sgrehan init_pair(1, COLOR_RED, COLOR_BLACK); 466243327Sgrehan init_pair(2, COLOR_GREEN, COLOR_BLACK); 467261090Sjhb init_pair(3, COLOR_YELLOW, COLOR_BLACK); 468261090Sjhb init_pair(4, COLOR_BLUE, COLOR_BLACK); 469243327Sgrehan init_pair(5, COLOR_CYAN, COLOR_BLACK); 470243327Sgrehan init_pair(6, COLOR_MAGENTA, COLOR_BLACK); 471243327Sgrehan init_pair(7, COLOR_WHITE, COLOR_BLACK); 472243327Sgrehan } 473243327Sgrehan 474243327Sgrehan for (;;) 475243327Sgrehan { 476243327Sgrehan int c = getch(); /* refresh, accept single keystroke of input */ 477243327Sgrehan attrset(COLOR_PAIR(num % 8)); 478243327Sgrehan num++; 479243327Sgrehan 480243327Sgrehan /* process the command keystroke */ 481243327Sgrehan } 482243327Sgrehan 483243327Sgrehan finish(0); /* we're done */ 484243327Sgrehan} 485243327Sgrehan 486243327Sgrehanstatic void finish(int sig) 487243327Sgrehan{ 488243327Sgrehan endwin(); 489243327Sgrehan 490243327Sgrehan /* do your non-curses wrapup here */ 491243327Sgrehan 492243327Sgrehan exit(0); 493243327Sgrehan} 494243327Sgrehan</PRE> 495276349Sneel 496243327Sgrehan<H3><A NAME="starting">Starting up</A></H3> 497243327Sgrehan 498243327SgrehanIn order to use the screen package, the routines must know about terminal 499243327Sgrehancharacteristics, and the space for <CODE>curscr</CODE> and <CODE>stdscr</CODE> must be 500261090Sjhballocated. These function <CODE>initscr()</CODE> does both these things. Since it 501243327Sgrehanmust allocate space for the windows, it can overflow memory when attempting to 502243327Sgrehando so. On the rare occasions this happens, <CODE>initscr()</CODE> will terminate 503243327Sgrehanthe program with an error message. <CODE>initscr()</CODE> must always be called 504243327Sgrehanbefore any of the routines which affect windows are used. If it is not, the 505276349Sneelprogram will core dump as soon as either <CODE>curscr</CODE> or <CODE>stdscr</CODE> are 506243327Sgrehanreferenced. However, it is usually best to wait to call it until after you are 507243327Sgrehansure you will need it, like after checking for startup errors. Terminal status 508243327Sgrehanchanging routines like <CODE>nl()</CODE> and <CODE>cbreak()</CODE> should be called 509243327Sgrehanafter <CODE>initscr()</CODE>. <P> 510243327Sgrehan 511243327SgrehanOnce the screen windows have been allocated, you can set them up for 512243327Sgrehanyour program. If you want to, say, allow a screen to scroll, use 513243327Sgrehan<CODE>scrollok()</CODE>. If you want the cursor to be left in place after 514243327Sgrehanthe last change, use <CODE>leaveok()</CODE>. If this isn't done, 515243327Sgrehan<CODE>refresh()</CODE> will move the cursor to the window's current (y, x) 516243327Sgrehancoordinates after updating it. <P> 517243327Sgrehan 518243327SgrehanYou can create new windows of your own using the functions <CODE>newwin()</CODE>, 519243327Sgrehan<CODE>derwin()</CODE>, and <CODE>subwin()</CODE>. The routine <CODE>delwin()</CODE> will 520243327Sgrehanallow you to get rid of old windows. All the options described above can be 521243327Sgrehanapplied to any window. 522243327Sgrehan 523243327Sgrehan<H3><A NAME="output">Output</A></H3> 524243327Sgrehan 525243327SgrehanNow that we have set things up, we will want to actually update the terminal. 526243327SgrehanThe basic functions used to change what will go on a window are 527243327Sgrehan<CODE>addch()</CODE> and <CODE>move()</CODE>. <CODE>addch()</CODE> adds a character at the 528243327Sgrehancurrent (y, x) coordinates. <CODE>move()</CODE> changes the current (y, x) 529243327Sgrehancoordinates to whatever you want them to be. It returns <CODE>ERR</CODE> if you 530243327Sgrehantry to move off the window. As mentioned above, you can combine the two into 531243327Sgrehan<CODE>mvaddch()</CODE> to do both things at once. <P> 532243327Sgrehan 533243327SgrehanThe other output functions, such as <CODE>addstr()</CODE> and <CODE>printw()</CODE>, 534243327Sgrehanall call <CODE>addch()</CODE> to add characters to the window. <P> 535243327Sgrehan 536243327SgrehanAfter you have put on the window what you want there, when you want the portion 537243327Sgrehanof the terminal covered by the window to be made to look like it, you must call 538243327Sgrehan<CODE>refresh()</CODE>. In order to optimize finding changes, <CODE>refresh()</CODE> 539243327Sgrehanassumes that any part of the window not changed since the last 540243327Sgrehan<CODE>refresh()</CODE> of that window has not been changed on the terminal, i.e., 541243327Sgrehanthat you have not refreshed a portion of the terminal with an overlapping 542243327Sgrehanwindow. If this is not the case, the routine <CODE>touchwin()</CODE> is provided 543243327Sgrehanto make it look like the entire window has been changed, thus making 544243327Sgrehan<CODE>refresh()</CODE> check the whole subsection of the terminal for changes. <P> 545243327Sgrehan 546261088SjhbIf you call <CODE>wrefresh()</CODE> with <CODE>curscr</CODE> as its argument, it will 547261088Sjhbmake the screen look like <CODE>curscr</CODE> thinks it looks like. This is useful 548261088Sjhbfor implementing a command which would redraw the screen in case it get messed 549261088Sjhbup. 550261088Sjhb 551261088Sjhb<H3><A NAME="input">Input</A></H3> 552261088Sjhb 553261088SjhbThe complementary function to <CODE>addch()</CODE> is <CODE>getch()</CODE> which, if 554261088Sjhbecho is set, will call <CODE>addch()</CODE> to echo the character. Since the 555261088Sjhbscreen package needs to know what is on the terminal at all times, if 556261088Sjhbcharacters are to be echoed, the tty must be in raw or cbreak mode. Since 557261088Sjhbinitially the terminal has echoing enabled and is in ordinary ``cooked'' mode, 558261088Sjhbone or the other has to changed before calling <CODE>getch()</CODE>; otherwise, 559261088Sjhbthe program's output will be unpredictable. <P> 560261088Sjhb 561261088SjhbWhen you need to accept line-oriented input in a window, the functions 562261088Sjhb<CODE>wgetstr()</CODE> and friends are available. There is even a <CODE>wscanw()</CODE> 563261088Sjhbfunction that can do <CODE>scanf()</CODE>(3)-style multi-field parsing on window 564261088Sjhbinput. These pseudo-line-oriented functions turn on echoing while they 565261088Sjhbexecute. <P> 566261088Sjhb 567261088SjhbThe example code above uses the call <CODE>keypad(stdscr, TRUE)</CODE> to enable 568261088Sjhbsupport for function-key mapping. With this feature, the <CODE>getch()</CODE> code 569261088Sjhbwatches the input stream for character sequences that correspond to arrow and 570261088Sjhbfunction keys. These sequences are returned as pseudo-character values. The 571261088Sjhb<CODE>#define</CODE> values returned are listed in the <CODE>curses.h</CODE> The 572261088Sjhbmapping from sequences to <CODE>#define</CODE> values is determined by 573261088Sjhb<CODE>key_</CODE> capabilities in the terminal's terminfo entry. 574261088Sjhb 575261088Sjhb<H3><A NAME="formschars">Using Forms Characters</A></H3> 576261088Sjhb 577261088SjhbThe <CODE>addch()</CODE> function (and some others, including <CODE>box()</CODE> and 578261088Sjhb<CODE>border()</CODE>) can accept some pseudo-character arguments which are specially 579261088Sjhbdefined by <CODE>ncurses</CODE>. These are <CODE>#define</CODE> values set up in 580261088Sjhbthe <CODE>curses.h</CODE> header; see there for a complete list (look for 581261088Sjhbthe prefix <CODE>ACS_</CODE>). <P> 582261088Sjhb 583261088SjhbThe most useful of the ACS defines are the forms-drawing characters. You can 584261088Sjhbuse these to draw boxes and simple graphs on the screen. If the terminal 585261088Sjhbdoes not have such characters, <CODE>curses.h</CODE> will map them to a 586261088Sjhbrecognizable (though ugly) set of ASCII defaults. 587261088Sjhb 588261088Sjhb<H3><A NAME="attributes">Character Attributes and Color</A></H3> 589261088Sjhb 590261088SjhbThe <CODE>ncurses</CODE> package supports screen highlights including standout, 591261088Sjhbreverse-video, underline, and blink. It also supports color, which is treated 592261088Sjhbas another kind of highlight. <P> 593261088Sjhb 594261088SjhbHighlights are encoded, internally, as high bits of the pseudo-character type 595270159Sgrehan(<CODE>chtype</CODE>) that <CODE>curses.h</CODE> uses to represent the contents of a 596270159Sgrehanscreen cell. See the <CODE>curses.h</CODE> header file for a complete list of 597270159Sgrehanhighlight mask values (look for the prefix <CODE>A_</CODE>).<P> 598270159Sgrehan 599270159SgrehanThere are two ways to make highlights. One is to logical-or the value of the 600270159Sgrehanhighlights you want into the character argument of an <CODE>addch()</CODE> call, 601270159Sgrehanor any other output call that takes a <CODE>chtype</CODE> argument. <P> 602270159Sgrehan 603270159SgrehanThe other is to set the current-highlight value. This is logical-or'ed with 604270159Sgrehanany highlight you specify the first way. You do this with the functions 605270159Sgrehan<CODE>attron()</CODE>, <CODE>attroff()</CODE>, and <CODE>attrset()</CODE>; see the manual 606270159Sgrehanpages for details. 607270159Sgrehan 608270159SgrehanColor is a special kind of highlight. The package actually thinks in terms 609270159Sgrehanof color pairs, combinations of foreground and background colors. The sample 610270159Sgrehancode above sets up eight color pairs, all of the guaranteed-available colors 611270159Sgrehanon black. Note that each color pair is, in effect, given the name of its 612270159Sgrehanforeground color. Any other range of eight non-conflicting values could 613270159Sgrehanhave been used as the first arguments of the <CODE>init_pair()</CODE> values. <P> 614270159Sgrehan 615270159SgrehanOnce you've done an <CODE>init_pair()</CODE> that creates color-pair N, you can 616270159Sgrehanuse <CODE>COLOR_PAIR(N)</CODE> as a highlight that invokes that particular 617270159Sgrehancolor combination. Note that <CODE>COLOR_PAIR(N)</CODE>, for constant N, 618270159Sgrehanis itself a compile-time constant and can be used in initializers. 619270159Sgrehan 620270159Sgrehan<H3><A NAME="mouse">Mouse Interfacing</A></H3> 621270159Sgrehan 622270159SgrehanThe <CODE>ncurses</CODE> library also provides a mouse interface. 623270159Sgrehan<!-- The 'note' tag is not portable enough --> 624270159Sgrehan<blockquote> 625270159Sgrehan<strong>NOTE:</strong> this facility is specific to <CODE>ncurses</CODE>, it is not part of either 626270159Sgrehanthe XSI Curses standard, nor of System V Release 4, nor BSD curses. 627270159SgrehanSystem V Release 4 curses contains code with similar interface definitions, 628243327Sgrehanhowever it is not documented. Other than by disassembling the library, we 629243327Sgrehanhave no way to determine exactly how that mouse code works. 630243327SgrehanThus, we recommend that you wrap mouse-related code in an #ifdef using the 631243327Sgrehanfeature macro NCURSES_MOUSE_VERSION so it will not be compiled and linked 632243327Sgrehanon non-ncurses systems. 633243327Sgrehan</blockquote> 634243327Sgrehan 635243327SgrehanPresently, mouse event reporting works in the following environments: 636243327Sgrehan<ul> 637243327Sgrehan<li>xterm and similar programs such as rxvt. 638243327Sgrehan<li>Linux console, when configured with <CODE>gpm</CODE>(1), Alessandro 639243327SgrehanRubini's mouse server. 640243327Sgrehan<li>FreeBSD sysmouse (console) 641243327Sgrehan<li>OS/2 EMX 642243327Sgrehan</ul> 643243327Sgrehan<P> 644243327SgrehanThe mouse interface is very simple. To activate it, you use the function 645243327Sgrehan<CODE>mousemask()</CODE>, passing it as first argument a bit-mask that specifies 646243327Sgrehanwhat kinds of events you want your program to be able to see. It will 647243327Sgrehanreturn the bit-mask of events that actually become visible, which may differ 648243327Sgrehanfrom the argument if the mouse device is not capable of reporting some of 649243327Sgrehanthe event types you specify. <P> 650243327Sgrehan 651243327SgrehanOnce the mouse is active, your application's command loop should watch 652243327Sgrehanfor a return value of <CODE>KEY_MOUSE</CODE> from <CODE>wgetch()</CODE>. When 653243327Sgrehanyou see this, a mouse event report has been queued. To pick it off 654243327Sgrehanthe queue, use the function <CODE>getmouse()</CODE> (you must do this before 655243327Sgrehanthe next <CODE>wgetch()</CODE>, otherwise another mouse event might come 656243327Sgrehanin and make the first one inaccessible). <P> 657243327Sgrehan 658243327SgrehanEach call to <CODE>getmouse()</CODE> fills a structure (the address of which you'll 659243327Sgrehanpass it) with mouse event data. The event data includes zero-origin, 660261265Sjhbscreen-relative character-cell coordinates of the mouse pointer. It also 661261265Sjhbincludes an event mask. Bits in this mask will be set, corresponding 662261265Sjhbto the event type being reported. <P> 663261265Sjhb 664261265SjhbThe mouse structure contains two additional fields which may be 665261265Sjhbsignificant in the future as ncurses interfaces to new kinds of 666261265Sjhbpointing device. In addition to x and y coordinates, there is a slot 667261265Sjhbfor a z coordinate; this might be useful with touch-screens that can 668261265Sjhbreturn a pressure or duration parameter. There is also a device ID 669261265Sjhbfield, which could be used to distinguish between multiple pointing 670261265Sjhbdevices. <P> 671261265Sjhb 672261265SjhbThe class of visible events may be changed at any time via <CODE>mousemask()</CODE>. 673261265SjhbEvents that can be reported include presses, releases, single-, double- and 674261265Sjhbtriple-clicks (you can set the maximum button-down time for clicks). If 675261265Sjhbyou don't make clicks visible, they will be reported as press-release 676261265Sjhbpairs. In some environments, the event mask may include bits reporting 677261265Sjhbthe state of shift, alt, and ctrl keys on the keyboard during the event. <P> 678261265Sjhb 679261265SjhbA function to check whether a mouse event fell within a given window is 680261265Sjhbalso supplied. You can use this to see whether a given window should 681261265Sjhbconsider a mouse event relevant to it. <P> 682261265Sjhb 683261265SjhbBecause mouse event reporting will not be available in all 684261265Sjhbenvironments, it would be unwise to build <CODE>ncurses</CODE> 685261265Sjhbapplications that <EM>require</EM> the use of a mouse. Rather, you should 686261265Sjhbuse the mouse as a shortcut for point-and-shoot commands your application 687261265Sjhbwould normally accept from the keyboard. Two of the test games in the 688261265Sjhb<CODE>ncurses</CODE> distribution (<CODE>bs</CODE> and <CODE>knight</CODE>) contain 689261265Sjhbcode that illustrates how this can be done. <P> 690261265Sjhb 691261265SjhbSee the manual page <CODE>curs_mouse(3X)</CODE> for full details of the 692261265Sjhbmouse-interface functions. 693261265Sjhb 694261265Sjhb<H3><A NAME="finishing">Finishing Up</A></H3> 695261265Sjhb 696261265SjhbIn order to clean up after the <CODE>ncurses</CODE> routines, the routine 697261265Sjhb<CODE>endwin()</CODE> is provided. It restores tty modes to what they were when 698261265Sjhb<CODE>initscr()</CODE> was first called, and moves the cursor down to the 699261265Sjhblower-left corner. Thus, anytime after the call to initscr, <CODE>endwin()</CODE> 700261265Sjhbshould be called before exiting. 701261265Sjhb 702261265Sjhb<H2><A NAME="functions">Function Descriptions</A></H2> 703261265Sjhb 704261265SjhbWe describe the detailed behavior of some important curses functions here, as a 705261265Sjhbsupplement to the manual page descriptions. 706261265Sjhb 707261265Sjhb<H3><A NAME="init">Initialization and Wrapup</A></H3> 708261265Sjhb 709261265Sjhb<DL> 710261265Sjhb<DT> <CODE>initscr()</CODE> 711261265Sjhb<DD> The first function called should almost always be <CODE>initscr()</CODE>. 712261265SjhbThis will determine the terminal type and 713261265Sjhbinitialize curses data structures. <CODE>initscr()</CODE> also arranges that 714261265Sjhbthe first call to <CODE>refresh()</CODE> will clear the screen. If an error 715261265Sjhboccurs a message is written to standard error and the program 716261265Sjhbexits. Otherwise it returns a pointer to stdscr. A few functions may be 717261265Sjhbcalled before initscr (<CODE>slk_init()</CODE>, <CODE>filter()</CODE>, 718261265Sjhb<CODE>ripoffline()</CODE>, <CODE>use_env()</CODE>, and, if you are using multiple 719261265Sjhbterminals, <CODE>newterm()</CODE>.) 720261265Sjhb<DT> <CODE>endwin()</CODE> 721261265Sjhb<DD> Your program should always call <CODE>endwin()</CODE> before exiting or 722261265Sjhbshelling out of the program. This function will restore tty modes, 723261265Sjhbmove the cursor to the lower left corner of the screen, reset the 724261265Sjhbterminal into the proper non-visual mode. Calling <CODE>refresh()</CODE> 725261265Sjhbor <CODE>doupdate()</CODE> after a temporary escape from the program will 726261265Sjhbrestore the ncurses screen from before the escape. 727261265Sjhb<DT> <CODE>newterm(type, ofp, ifp)</CODE> 728261265Sjhb<DD> A program which outputs to more than one terminal should use 729261265Sjhb<CODE>newterm()</CODE> instead of <CODE>initscr()</CODE>. <CODE>newterm()</CODE> should 730261265Sjhbbe called once for each terminal. It returns a variable of type 731261265Sjhb<CODE>SCREEN *</CODE> which should be saved as a reference to that 732261265Sjhbterminal. 733243327Sgrehan(NOTE: a SCREEN variable is not a <em>screen</em> in the sense we 734243327Sgrehanare describing in this introduction, but a collection of 735243327Sgrehanparameters used to assist in optimizing the display.) 736243327SgrehanThe arguments are the type of the terminal (a string) and 737243327Sgrehan<CODE>FILE</CODE> pointers for the output and input of the terminal. If 738243327Sgrehantype is NULL then the environment variable <CODE>$TERM</CODE> is used. 739261265Sjhb<CODE>endwin()</CODE> should called once at wrapup time for each terminal 740261265Sjhbopened using this function. 741261265Sjhb<DT> <CODE>set_term(new)</CODE> 742243327Sgrehan<DD> This function is used to switch to a different terminal previously 743261265Sjhbopened by <CODE>newterm()</CODE>. The screen reference for the new terminal 744261265Sjhbis passed as the parameter. The previous terminal is returned by the 745261265Sjhbfunction. All other calls affect only the current terminal. 746261265Sjhb<DT> <CODE>delscreen(sp)</CODE> 747261265Sjhb<DD> The inverse of <CODE>newterm()</CODE>; deallocates the data structures 748261265Sjhbassociated with a given <CODE>SCREEN</CODE> reference. 749268972Sjhb</DL> 750261265Sjhb 751261265Sjhb<H3><A NAME="flush">Causing Output to the Terminal</A></H3> 752261265Sjhb 753261265Sjhb<DL> 754261088Sjhb<DT> <CODE>refresh()</CODE> and <CODE>wrefresh(win)</CODE> 755261265Sjhb<DD> These functions must be called to actually get any output on 756261088Sjhbthe terminal, as other routines merely manipulate data 757261265Sjhbstructures. <CODE>wrefresh()</CODE> copies the named window to the physical 758268887Sjhbterminal screen, taking into account what is already 759261265Sjhbthere in order to do optimizations. <CODE>refresh()</CODE> does a 760261265Sjhbrefresh of <CODE>stdscr</CODE>. Unless <CODE>leaveok()</CODE> has been 761261265Sjhbenabled, the physical cursor of the terminal is left at the 762261265Sjhblocation of the window's cursor. 763261265Sjhb<DT> <CODE>doupdate()</CODE> and <CODE>wnoutrefresh(win)</CODE> 764261265Sjhb<DD> These two functions allow multiple updates with more efficiency 765261265Sjhbthan wrefresh. To use them, it is important to understand how curses 766261265Sjhbworks. In addition to all the window structures, curses keeps two 767261265Sjhbdata structures representing the terminal screen: a physical screen, 768261265Sjhbdescribing what is actually on the screen, and a virtual screen, 769261265Sjhbdescribing what the programmer wants to have on the screen. wrefresh 770261265Sjhbworks by first copying the named window to the virtual screen 771261265Sjhb(<CODE>wnoutrefresh()</CODE>), and then calling the routine to update the 772261265Sjhbscreen (<CODE>doupdate()</CODE>). If the programmer wishes to output 773243327Sgrehanseveral windows at once, a series of calls to <CODE>wrefresh</CODE> will result 774261265Sjhbin alternating calls to <CODE>wnoutrefresh()</CODE> and <CODE>doupdate()</CODE>, 775261265Sjhbcausing several bursts of output to the screen. By calling 776261265Sjhb<CODE>wnoutrefresh()</CODE> for each window, it is then possible to call 777243327Sgrehan<CODE>doupdate()</CODE> once, resulting in only one burst of output, with 778243327Sgrehanfewer total characters transmitted (this also avoids a visually annoying 779243327Sgrehanflicker at each update). 780243327Sgrehan</DL> 781243327Sgrehan 782243327Sgrehan<H3><A NAME="lowlevel">Low-Level Capability Access</A></H3> 783243327Sgrehan 784243327Sgrehan<DL> 785243327Sgrehan<DT> <CODE>setupterm(term, filenum, errret)</CODE> 786243327Sgrehan<DD> This routine is called to initialize a terminal's description, without setting 787243327Sgrehanup the curses screen structures or changing the tty-driver mode bits. 788243327Sgrehan<CODE>term</CODE> is the character string representing the name of the terminal 789243327Sgrehanbeing used. <CODE>filenum</CODE> is the UNIX file descriptor of the terminal to 790243327Sgrehanbe used for output. <CODE>errret</CODE> is a pointer to an integer, in which a 791243327Sgrehansuccess or failure indication is returned. The values returned can be 1 (all 792243327Sgrehanis well), 0 (no such terminal), or -1 (some problem locating the terminfo 793288434Sdelphijdatabase). <P> 794243327Sgrehan 795243327SgrehanThe value of <CODE>term</CODE> can be given as NULL, which will cause the value of 796288434Sdelphij<CODE>TERM</CODE> in the environment to be used. The <CODE>errret</CODE> pointer can 797243327Sgrehanalso be given as NULL, meaning no error code is wanted. If <CODE>errret</CODE> is 798243327Sgrehandefaulted, and something goes wrong, <CODE>setupterm()</CODE> will print an 799243327Sgrehanappropriate error message and exit, rather than returning. Thus, a simple 800243327Sgrehanprogram can call setupterm(0, 1, 0) and not worry about initialization 801243327Sgrehanerrors. <P> 802243327Sgrehan 803243327SgrehanAfter the call to <CODE>setupterm()</CODE>, the global variable <CODE>cur_term</CODE> is 804243327Sgrehanset to point to the current structure of terminal capabilities. By calling 805243327Sgrehan<CODE>setupterm()</CODE> for each terminal, and saving and restoring 806243327Sgrehan<CODE>cur_term</CODE>, it is possible for a program to use two or more terminals at 807243327Sgrehanonce. <CODE>Setupterm()</CODE> also stores the names section of the terminal 808243327Sgrehandescription in the global character array <CODE>ttytype[]</CODE>. Subsequent calls 809243327Sgrehanto <CODE>setupterm()</CODE> will overwrite this array, so you'll have to save it 810243327Sgrehanyourself if need be. 811243327Sgrehan</DL> 812243327Sgrehan 813243327Sgrehan<H3><A NAME="debugging">Debugging</A></H3> 814243327Sgrehan 815243327Sgrehan<!-- The 'note' tag is not portable enough --> 816243327Sgrehan<blockquote> 817243327Sgrehan<strong>NOTE:</strong> These functions are not part of the standard curses API! 818243327Sgrehan</blockquote> 819243327Sgrehan 820243327Sgrehan<DL> 821243327Sgrehan<DT> <CODE>trace()</CODE> 822243327Sgrehan<DD> 823243327SgrehanThis function can be used to explicitly set a trace level. If the 824243327Sgrehantrace level is nonzero, execution of your program will generate a file 825243327Sgrehancalled `trace' in the current working directory containing a report on 826243327Sgrehanthe library's actions. Higher trace levels enable more detailed (and 827243327Sgrehanverbose) reporting -- see comments attached to <CODE>TRACE_</CODE> defines 828243327Sgrehanin the <CODE>curses.h</CODE> file for details. (It is also possible to set 829243327Sgrehana trace level by assigning a trace level value to the environment variable 830243327Sgrehan<CODE>NCURSES_TRACE</CODE>). 831243327Sgrehan<DT> <CODE>_tracef()</CODE> 832243327Sgrehan<DD> 833243327SgrehanThis function can be used to output your own debugging information. It is only 834243327Sgrehanavailable only if you link with -lncurses_g. It can be used the same way as 835243327Sgrehan<CODE>printf()</CODE>, only it outputs a newline after the end of arguments. 836243327SgrehanThe output goes to a file called <CODE>trace</CODE> in the current directory. 837243327Sgrehan</DL> 838243327Sgrehan 839243327SgrehanTrace logs can be difficult to interpret due to the sheer volume of 840243327Sgrehandata dumped in them. There is a script called <STRONG>tracemunch</STRONG> 841243327Sgrehanincluded with the <CODE>ncurses</CODE> distribution that can alleviate 842243327Sgrehanthis problem somewhat; it compacts long sequences of similar operations into 843243327Sgrehanmore succinct single-line pseudo-operations. These pseudo-ops can be 844243327Sgrehandistinguished by the fact that they are named in capital letters. 845243327Sgrehan 846243327Sgrehan<H2><A NAME="hints">Hints, Tips, and Tricks</A></H2> 847248477Sneel 848243327SgrehanThe <CODE>ncurses</CODE> manual pages are a complete reference for this library. 849248477SneelIn the remainder of this document, we discuss various useful methods that 850247523Sneelmay not be obvious from the manual page descriptions. 851243327Sgrehan 852248477Sneel<H3><A NAME="caution">Some Notes of Caution</A></H3> 853248477Sneel 854248477SneelIf you find yourself thinking you need to use <CODE>noraw()</CODE> or 855248477Sneel<CODE>nocbreak()</CODE>, think again and move carefully. It's probably 856248477Sneelbetter design to use <CODE>getstr()</CODE> or one of its relatives to 857248477Sneelsimulate cooked mode. The <CODE>noraw()</CODE> and <CODE>nocbreak()</CODE> 858243327Sgrehanfunctions try to restore cooked mode, but they may end up clobbering 859248477Sneelsome control bits set before you started your application. Also, they 860248477Sneelhave always been poorly documented, and are likely to hurt your 861248477Sneelapplication's usability with other curses libraries. <P> 862248477Sneel 863243327SgrehanBear in mind that <CODE>refresh()</CODE> is a synonym for <CODE>wrefresh(stdscr)</CODE>. 864243327SgrehanDon't try to mix use of <CODE>stdscr</CODE> with use of windows declared 865243327Sgrehanby <CODE>newwin()</CODE>; a <CODE>refresh()</CODE> call will blow them off the 866248477Sneelscreen. The right way to handle this is to use <CODE>subwin()</CODE>, or 867243327Sgrehannot touch <CODE>stdscr</CODE> at all and tile your screen with declared 868243327Sgrehanwindows which you then <CODE>wnoutrefresh()</CODE> somewhere in your program 869243327Sgrehanevent loop, with a single <CODE>doupdate()</CODE> call to trigger actual 870243327Sgrehanrepainting. <P> 871243327Sgrehan 872243327SgrehanYou are much less likely to run into problems if you design your screen 873243327Sgrehanlayouts to use tiled rather than overlapping windows. Historically, 874243327Sgrehancurses support for overlapping windows has been weak, fragile, and poorly 875243327Sgrehandocumented. The <CODE>ncurses</CODE> library is not yet an exception to this 876243327Sgrehanrule. <P> 877243327Sgrehan 878243327SgrehanThere is a panels library included in the <CODE>ncurses</CODE> 879243327Sgrehandistribution that does a pretty good job of strengthening the 880243327Sgrehanoverlapping-windows facilities. <P> 881243327Sgrehan 882243327SgrehanTry to avoid using the global variables LINES and COLS. Use 883243327Sgrehan<CODE>getmaxyx()</CODE> on the <CODE>stdscr</CODE> context instead. Reason: 884243327Sgrehanyour code may be ported to run in an environment with window resizes, 885243327Sgrehanin which case several screens could be open with different sizes. 886243327Sgrehan 887243327Sgrehan<H3><A NAME="leaving">Temporarily Leaving NCURSES Mode</A></H3> 888243327Sgrehan 889243327SgrehanSometimes you will want to write a program that spends most of its time in 890243327Sgrehanscreen mode, but occasionally returns to ordinary `cooked' mode. A common 891243327Sgrehanreason for this is to support shell-out. This behavior is simple to arrange 892243327Sgrehanin <CODE>ncurses</CODE>. <P> 893243327Sgrehan 894243327SgrehanTo leave <CODE>ncurses</CODE> mode, call <CODE>endwin()</CODE> as you would if you 895243327Sgrehanwere intending to terminate the program. This will take the screen back to 896243327Sgrehancooked mode; you can do your shell-out. When you want to return to 897243327Sgrehan<CODE>ncurses</CODE> mode, simply call <CODE>refresh()</CODE> or <CODE>doupdate()</CODE>. 898243327SgrehanThis will repaint the screen. <P> 899243327Sgrehan 900248477SneelThere is a boolean function, <CODE>isendwin()</CODE>, which code can use to 901243327Sgrehantest whether <CODE>ncurses</CODE> screen mode is active. It returns <CODE>TRUE</CODE> 902243327Sgrehanin the interval between an <CODE>endwin()</CODE> call and the following 903243327Sgrehan<CODE>refresh()</CODE>, <CODE>FALSE</CODE> otherwise. <P> 904243327Sgrehan 905243327SgrehanHere is some sample code for shellout: 906243327Sgrehan 907243327Sgrehan<PRE> 908243327Sgrehan addstr("Shelling out..."); 909243327Sgrehan def_prog_mode(); /* save current tty modes */ 910243327Sgrehan endwin(); /* restore original tty modes */ 911243327Sgrehan system("sh"); /* run shell */ 912243327Sgrehan addstr("returned.\n"); /* prepare return message */ 913243327Sgrehan refresh(); /* restore save modes, repaint screen */ 914243327Sgrehan</PRE> 915243327Sgrehan 916243327Sgrehan<H3><A NAME="xterm">Using NCURSES under XTERM</A></H3> 917243327Sgrehan 918243327SgrehanA resize operation in X sends <CODE>SIGWINCH</CODE> to the application running 919243327Sgrehanunder xterm. 920243327Sgrehan 921243327SgrehanThe easiest way to handle <CODE>SIGWINCH</CODE> 922243327Sgrehanis to do an <CODE>endwin</CODE>, 923243327Sgrehanfollowed by an <CODE>refresh</CODE> and a screen repaint you code 924243327Sgrehanyourself. 925243327SgrehanThe <CODE>refresh</CODE> will pick up the new screen size from the 926243327Sgrehanxterm's environment. <P> 927243327Sgrehan 928243327SgrehanThat is the standard way, of course (it even works with some vendor's curses 929243327Sgrehanimplementations). 930243327SgrehanIts drawback is that it clears the screen to reinitialize the display, and does 931243327Sgrehannot resize subwindows which must be shrunk. 932243327Sgrehan<CODE>Ncurses</CODE> provides an extension which works better, the 933243327Sgrehan<CODE>resizeterm</CODE> function. That function ensures that all windows 934243327Sgrehanare limited to the new screen dimensions, and pads <CODE>stdscr</CODE> 935243327Sgrehanwith blanks if the screen is larger. <P> 936243327Sgrehan 937243327SgrehanThe <CODE>ncurses</CODE> library provides a SIGWINCH signal handler, 938243327Sgrehanwhich pushes a <CODE>KEY_RESIZE</CODE> via the wgetch() calls. 939243327SgrehanWhen <CODE>ncurses</CODE> returns that code, 940243327Sgrehanit calls <code>resizeterm</CODE> 941243327Sgrehanto update the size of the standard screen's window, repainting that 942243327Sgrehan(filling with blanks or truncating as needed). 943243327SgrehanIt also resizes other windows, 944243327Sgrehanbut its effect may be less satisfactory because it cannot 945243327Sgrehanknow how you want the screen re-painted. 946243327SgrehanYou will usually have to write special-purpose code to handle 947243327Sgrehan<CODE>KEY_RESIZE</CODE> yourself. 948243327Sgrehan 949243327Sgrehan<H3><A NAME="screens">Handling Multiple Terminal Screens</A></H3> 950243327Sgrehan 951243327SgrehanThe <CODE>initscr()</CODE> function actually calls a function named 952243327Sgrehan<CODE>newterm()</CODE> to do most of its work. If you are writing a program that 953243327Sgrehanopens multiple terminals, use <CODE>newterm()</CODE> directly. <P> 954243327Sgrehan 955243327SgrehanFor each call, you will have to specify a terminal type and a pair of file 956243327Sgrehanpointers; each call will return a screen reference, and <CODE>stdscr</CODE> will be 957243327Sgrehanset to the last one allocated. You will switch between screens with the 958243327Sgrehan<CODE>set_term</CODE> call. Note that you will also have to call 959243327Sgrehan<CODE>def_shell_mode</CODE> and <CODE>def_prog_mode</CODE> on each tty yourself. 960243327Sgrehan 961243327Sgrehan<H3><A NAME="testing">Testing for Terminal Capabilities</A></H3> 962243704Sgrehan 963243327SgrehanSometimes you may want to write programs that test for the presence of various 964243327Sgrehancapabilities before deciding whether to go into <CODE>ncurses</CODE> mode. An easy 965261088Sjhbway to do this is to call <CODE>setupterm()</CODE>, then use the functions 966270159Sgrehan<CODE>tigetflag()</CODE>, <CODE>tigetnum()</CODE>, and <CODE>tigetstr()</CODE> to do your 967243327Sgrehantesting. <P> 968243327Sgrehan 969243327SgrehanA particularly useful case of this often comes up when you want to 970243327Sgrehantest whether a given terminal type should be treated as `smart' 971243327Sgrehan(cursor-addressable) or `stupid'. The right way to test this is to see 972243327Sgrehanif the return value of <CODE>tigetstr("cup")</CODE> is non-NULL. Alternatively, 973259301Sgrehanyou can include the <CODE>term.h</CODE> file and test the value of the 974243327Sgrehanmacro <CODE>cursor_address</CODE>. 975243327Sgrehan 976243327Sgrehan<H3><A NAME="tuning">Tuning for Speed</A></H3> 977243327Sgrehan 978243327SgrehanUse the <CODE>addchstr()</CODE> family of functions for fast 979243327Sgrehanscreen-painting of text when you know the text doesn't contain any 980261088Sjhbcontrol characters. Try to make attribute changes infrequent on your 981261088Sjhbscreens. Don't use the <CODE>immedok()</CODE> option! 982261088Sjhb 983261088Sjhb<H3><A NAME="special">Special Features of NCURSES</A></H3> 984243327Sgrehan 985243327SgrehanThe <CODE>wresize()</CODE> function allows you to resize a window in place. 986243327SgrehanThe associated <CODE>resizeterm()</CODE> function simplifies the construction 987243327Sgrehanof <a HREF="#xterm">SIGWINCH</a> handlers, for resizing all windows. <P> 988243327Sgrehan 989243327SgrehanThe <CODE>define_key()</CODE> function allows you 990243327Sgrehanto define at runtime function-key control sequences which are not in the 991243327Sgrehanterminal description. 992243327SgrehanThe <CODE>keyok()</CODE> function allows you to temporarily 993243327Sgrehanenable or disable interpretation of any function-key control sequence. <P> 994243327Sgrehan 995243327SgrehanThe <CODE>use_default_colors()</CODE> function allows you to construct 996243327Sgrehanapplications which can use the terminal's default foreground and 997243327Sgrehanbackground colors as an additional "default" color. 998243327SgrehanSeveral terminal emulators support this feature, which is based on ISO 6429. <P> 999243327Sgrehan 1000243327SgrehanNcurses supports up 16 colors, unlike SVr4 curses which defines only 8. 1001243327SgrehanWhile most terminals which provide color allow only 8 colors, about 1002243327Sgrehana quarter (including XFree86 xterm) support 16 colors. 1003243327Sgrehan 1004243327Sgrehan<H2><A NAME="compat">Compatibility with Older Versions</A></H2> 1005243327Sgrehan 1006248477SneelDespite our best efforts, there are some differences between <CODE>ncurses</CODE> 1007243327Sgrehanand the (undocumented!) behavior of older curses implementations. These arise 1008243327Sgrehanfrom ambiguities or omissions in the documentation of the API. 1009243327Sgrehan 1010243327Sgrehan<H3><A NAME="refbug">Refresh of Overlapping Windows</A></H3> 1011243327Sgrehan 1012243327SgrehanIf you define two windows A and B that overlap, and then alternately scribble 1013on and refresh them, the changes made to the overlapping region under historic 1014<CODE>curses</CODE> versions were often not documented precisely. <P> 1015 1016To understand why this is a problem, remember that screen updates are 1017calculated between two representations of the <EM>entire</EM> display. The 1018documentation says that when you refresh a window, it is first copied to the 1019virtual screen, and then changes are calculated to update the physical screen 1020(and applied to the terminal). But "copied to" is not very specific, and 1021subtle differences in how copying works can produce different behaviors in the 1022case where two overlapping windows are each being refreshed at unpredictable 1023intervals. <P> 1024 1025What happens to the overlapping region depends on what <CODE>wnoutrefresh()</CODE> 1026does with its argument -- what portions of the argument window it copies to the 1027virtual screen. Some implementations do "change copy", copying down only 1028locations in the window that have changed (or been marked changed with 1029<CODE>wtouchln()</CODE> and friends). Some implementations do "entire copy", 1030copying <EM>all</EM> window locations to the virtual screen whether or not 1031they have changed. <P> 1032 1033The <CODE>ncurses</CODE> library itself has not always been consistent on this 1034score. Due to a bug, versions 1.8.7 to 1.9.8a did entire copy. Versions 10351.8.6 and older, and versions 1.9.9 and newer, do change copy. <P> 1036 1037For most commercial curses implementations, it is not documented and not known 1038for sure (at least not to the <CODE>ncurses</CODE> maintainers) whether they do 1039change copy or entire copy. We know that System V release 3 curses has logic 1040in it that looks like an attempt to do change copy, but the surrounding logic 1041and data representations are sufficiently complex, and our knowledge 1042sufficiently indirect, that it's hard to know whether this is reliable. 1043 1044It is not clear what the SVr4 documentation and XSI standard intend. The XSI 1045Curses standard barely mentions wnoutrefresh(); the SVr4 documents seem to be 1046describing entire-copy, but it is possible with some effort and straining to 1047read them the other way. <P> 1048 1049It might therefore be unwise to rely on either behavior in programs that might 1050have to be linked with other curses implementations. Instead, you can do an 1051explicit <CODE>touchwin()</CODE> before the <CODE>wnoutrefresh()</CODE> call to 1052guarantee an entire-contents copy anywhere. <P> 1053 1054The really clean way to handle this is to use the panels library. If, 1055when you want a screen update, you do <CODE>update_panels()</CODE>, it will 1056do all the necessary <CODE>wnoutrefresh()</CODE> calls for whatever panel 1057stacking order you have defined. Then you can do one <CODE>doupdate()</CODE> 1058and there will be a <EM>single</EM> burst of physical I/O that will do 1059all your updates. 1060 1061<H3><A NAME="backbug">Background Erase</A></H3> 1062 1063If you have been using a very old versions of <CODE>ncurses</CODE> (1.8.7 or 1064older) you may be surprised by the behavior of the erase functions. In older 1065versions, erased areas of a window were filled with a blank modified by the 1066window's current attribute (as set by <STRONG>wattrset()</STRONG>, <STRONG>wattron()</STRONG>, 1067<STRONG>wattroff()</STRONG> and friends). <P> 1068 1069In newer versions, this is not so. Instead, the attribute of erased blanks 1070is normal unless and until it is modified by the functions <CODE>bkgdset()</CODE> 1071or <CODE>wbkgdset()</CODE>. <P> 1072 1073This change in behavior conforms <CODE>ncurses</CODE> to System V Release 4 and 1074the XSI Curses standard. 1075 1076<H2><A NAME="xsifuncs">XSI Curses Conformance</A></H2> 1077 1078The <CODE>ncurses</CODE> library is intended to be base-level conformant with the 1079XSI Curses standard from X/Open. Many extended-level features (in fact, almost 1080all features not directly concerned with wide characters and 1081internationalization) are also supported. <P> 1082 1083One effect of XSI conformance is the change in behavior described under 1084<A HREF="#backbug">"Background Erase -- Compatibility with Old Versions"</A>. <P> 1085 1086Also, <CODE>ncurses</CODE> meets the XSI requirement that every macro 1087entry point have a corresponding function which may be linked (and 1088will be prototype-checked) if the macro definition is disabled with 1089<CODE>#undef</CODE>. 1090 1091<H1><A NAME="panels">The Panels Library</A></H1> 1092 1093The <CODE>ncurses</CODE> library by itself provides good support for screen 1094displays in which the windows are tiled (non-overlapping). In the more 1095general case that windows may overlap, you have to use a series of 1096<CODE>wnoutrefresh()</CODE> calls followed by a <CODE>doupdate()</CODE>, and be 1097careful about the order you do the window refreshes in. It has to be 1098bottom-upwards, otherwise parts of windows that should be obscured will 1099show through. <P> 1100 1101When your interface design is such that windows may dive deeper into the 1102visibility stack or pop to the top at runtime, the resulting book-keeping 1103can be tedious and difficult to get right. Hence the panels library. <P> 1104 1105The <CODE>panel</CODE> library first appeared in AT&T System V. The 1106version documented here is the <CODE>panel</CODE> code distributed 1107with <CODE>ncurses</CODE>. 1108 1109<H2><A NAME="pcompile">Compiling With the Panels Library</A></H2> 1110 1111Your panels-using modules must import the panels library declarations with 1112 1113<PRE> 1114 #include <panel.h> 1115</PRE> 1116 1117and must be linked explicitly with the panels library using an 1118<CODE>-lpanel</CODE> argument. Note that they must also link the 1119<CODE>ncurses</CODE> library with <CODE>-lncurses</CODE>. Many linkers 1120are two-pass and will accept either order, but it is still good practice 1121to put <CODE>-lpanel</CODE> first and <CODE>-lncurses</CODE> second. 1122 1123<H2><A NAME="poverview">Overview of Panels</A></H2> 1124 1125A panel object is a window that is implicitly treated as part of a 1126<DFN>deck</DFN> including all other panel objects. The deck has an implicit 1127bottom-to-top visibility order. The panels library includes an update 1128function (analogous to <CODE>refresh()</CODE>) that displays all panels in the 1129deck in the proper order to resolve overlaps. The standard window, 1130<CODE>stdscr</CODE>, is considered below all panels. <P> 1131 1132Details on the panels functions are available in the man pages. We'll just 1133hit the highlights here. <P> 1134 1135You create a panel from a window by calling <CODE>new_panel()</CODE> on a 1136window pointer. It then becomes the top of the deck. The panel's window 1137is available as the value of <CODE>panel_window()</CODE> called with the 1138panel pointer as argument.<P> 1139 1140You can delete a panel (removing it from the deck) with <CODE>del_panel</CODE>. 1141This will not deallocate the associated window; you have to do that yourself. 1142 1143You can replace a panel's window with a different window by calling 1144<CODE>replace_window</CODE>. The new window may be of different size; 1145the panel code will re-compute all overlaps. This operation doesn't 1146change the panel's position in the deck. <P> 1147 1148To move a panel's window, use <CODE>move_panel()</CODE>. The 1149<CODE>mvwin()</CODE> function on the panel's window isn't sufficient because it 1150doesn't update the panels library's representation of where the windows are. 1151This operation leaves the panel's depth, contents, and size unchanged. <P> 1152 1153Two functions (<CODE>top_panel()</CODE>, <CODE>bottom_panel()</CODE>) are 1154provided for rearranging the deck. The first pops its argument window to the 1155top of the deck; the second sends it to the bottom. Either operation leaves 1156the panel's screen location, contents, and size unchanged. <P> 1157 1158The function <CODE>update_panels()</CODE> does all the 1159<CODE>wnoutrefresh()</CODE> calls needed to prepare for 1160<CODE>doupdate()</CODE> (which you must call yourself, afterwards). <P> 1161 1162Typically, you will want to call <CODE>update_panels()</CODE> and 1163<CODE>doupdate()</CODE> just before accepting command input, once in each cycle 1164of interaction with the user. If you call <CODE>update_panels()</CODE> after 1165each and every panel write, you'll generate a lot of unnecessary refresh 1166activity and screen flicker. 1167 1168<H2><A NAME="pstdscr">Panels, Input, and the Standard Screen</A></H2> 1169 1170You shouldn't mix <CODE>wnoutrefresh()</CODE> or <CODE>wrefresh()</CODE> 1171operations with panels code; this will work only if the argument window 1172is either in the top panel or unobscured by any other panels. <P> 1173 1174The <CODE>stsdcr</CODE> window is a special case. It is considered below all 1175panels. Because changes to panels may obscure parts of <CODE>stdscr</CODE>, 1176though, you should call <CODE>update_panels()</CODE> before 1177<CODE>doupdate()</CODE> even when you only change <CODE>stdscr</CODE>. <P> 1178 1179Note that <CODE>wgetch</CODE> automatically calls <CODE>wrefresh</CODE>. 1180Therefore, before requesting input from a panel window, you need to be sure 1181that the panel is totally unobscured. <P> 1182 1183There is presently no way to display changes to one obscured panel without 1184repainting all panels. 1185 1186<H2><A NAME="hiding">Hiding Panels</A></H2> 1187 1188It's possible to remove a panel from the deck temporarily; use 1189<CODE>hide_panel</CODE> for this. Use <CODE>show_panel()</CODE> to render it 1190visible again. The predicate function <CODE>panel_hidden</CODE> 1191tests whether or not a panel is hidden. <P> 1192 1193The <CODE>panel_update</CODE> code ignores hidden panels. You cannot do 1194<CODE>top_panel()</CODE> or <CODE>bottom_panel</CODE> on a hidden panel(). 1195Other panels operations are applicable. 1196 1197<H2><A NAME="pmisc">Miscellaneous Other Facilities</A></H2> 1198 1199It's possible to navigate the deck using the functions 1200<CODE>panel_above()</CODE> and <CODE>panel_below</CODE>. Handed a panel 1201pointer, they return the panel above or below that panel. Handed 1202<CODE>NULL</CODE>, they return the bottom-most or top-most panel. <P> 1203 1204Every panel has an associated user pointer, not used by the panel code, to 1205which you can attach application data. See the man page documentation 1206of <CODE>set_panel_userptr()</CODE> and <CODE>panel_userptr</CODE> for 1207details. 1208 1209<H1><A NAME="menu">The Menu Library</A></H1> 1210 1211A menu is a screen display that assists the user to choose some subset 1212of a given set of items. The <CODE>menu</CODE> library is a curses 1213extension that supports easy programming of menu hierarchies with a 1214uniform but flexible interface. <P> 1215 1216The <CODE>menu</CODE> library first appeared in AT&T System V. The 1217version documented here is the <CODE>menu</CODE> code distributed 1218with <CODE>ncurses</CODE>. 1219 1220<H2><A NAME="mcompile">Compiling With the menu Library</A></H2> 1221 1222Your menu-using modules must import the menu library declarations with 1223 1224<PRE> 1225 #include <menu.h> 1226</PRE> 1227 1228and must be linked explicitly with the menus library using an 1229<CODE>-lmenu</CODE> argument. Note that they must also link the 1230<CODE>ncurses</CODE> library with <CODE>-lncurses</CODE>. Many linkers 1231are two-pass and will accept either order, but it is still good practice 1232to put <CODE>-lmenu</CODE> first and <CODE>-lncurses</CODE> second. 1233 1234<H2><A NAME="moverview">Overview of Menus</A></H2> 1235 1236The menus created by this library consist of collections of 1237<DFN>items</DFN> including a name string part and a description string 1238part. To make menus, you create groups of these items and connect 1239them with menu frame objects. <P> 1240 1241The menu can then by <DFN>posted</DFN>, that is written to an 1242associated window. Actually, each menu has two associated windows; a 1243containing window in which the programmer can scribble titles or 1244borders, and a subwindow in which the menu items proper are displayed. 1245If this subwindow is too small to display all the items, it will be a 1246scrollable viewport on the collection of items. <P> 1247 1248A menu may also be <DFN>unposted</DFN> (that is, undisplayed), and finally 1249freed to make the storage associated with it and its items available for 1250re-use. <P> 1251 1252The general flow of control of a menu program looks like this: 1253 1254<OL> 1255<LI>Initialize <CODE>curses</CODE>. 1256<LI>Create the menu items, using <CODE>new_item()</CODE>. 1257<LI>Create the menu using <CODE>new_menu()</CODE>. 1258<LI>Post the menu using <CODE>post_menu()</CODE>. 1259<LI>Refresh the screen. 1260<LI>Process user requests via an input loop. 1261<LI>Unpost the menu using <CODE>unpost_menu()</CODE>. 1262<LI>Free the menu, using <CODE>free_menu()</CODE>. 1263<LI>Free the items using <CODE>free_item()</CODE>. 1264<LI>Terminate <CODE>curses</CODE>. 1265</OL> 1266 1267<H2><A NAME="mselect">Selecting items</A></H2> 1268 1269Menus may be multi-valued or (the default) single-valued (see the manual 1270page <CODE>menu_opts(3x)</CODE> to see how to change the default). 1271Both types always have a <DFN>current item</DFN>. <P> 1272 1273From a single-valued menu you can read the selected value simply by looking 1274at the current item. From a multi-valued menu, you get the selected set 1275by looping through the items applying the <CODE>item_value()</CODE> 1276predicate function. Your menu-processing code can use the function 1277<CODE>set_item_value()</CODE> to flag the items in the select set. <P> 1278 1279Menu items can be made unselectable using <CODE>set_item_opts()</CODE> 1280or <CODE>item_opts_off()</CODE> with the <CODE>O_SELECTABLE</CODE> 1281argument. This is the only option so far defined for menus, but it 1282is good practice to code as though other option bits might be on. 1283 1284<H2><A NAME="mdisplay">Menu Display</A></H2> 1285 1286The menu library calculates a minimum display size for your window, based 1287on the following variables: 1288 1289<UL> 1290<LI>The number and maximum length of the menu items 1291<LI>Whether the O_ROWMAJOR option is enabled 1292<LI>Whether display of descriptions is enabled 1293<LI>Whatever menu format may have been set by the programmer 1294<LI>The length of the menu mark string used for highlighting selected items 1295</UL> 1296 1297The function <CODE>set_menu_format()</CODE> allows you to set the 1298maximum size of the viewport or <DFN>menu page</DFN> that will be used 1299to display menu items. You can retrieve any format associated with a 1300menu with <CODE>menu_format()</CODE>. The default format is rows=16, 1301columns=1. <P> 1302 1303The actual menu page may be smaller than the format size. This depends 1304on the item number and size and whether O_ROWMAJOR is on. This option 1305(on by default) causes menu items to be displayed in a `raster-scan' 1306pattern, so that if more than one item will fit horizontally the first 1307couple of items are side-by-side in the top row. The alternative is 1308column-major display, which tries to put the first several items in 1309the first column. <P> 1310 1311As mentioned above, a menu format not large enough to allow all items to fit 1312on-screen will result in a menu display that is vertically scrollable. <P> 1313You can scroll it with requests to the menu driver, which will be described 1314in the section on <A HREF="#minput">menu input handling</A>. <P> 1315 1316Each menu has a <DFN>mark string</DFN> used to visually tag selected items; 1317see the <CODE>menu_mark(3x)</CODE> manual page for details. The mark 1318string length also influences the menu page size. <P> 1319 1320The function <CODE>scale_menu()</CODE> returns the minimum display size 1321that the menu code computes from all these factors. 1322 1323There are other menu display attributes including a select attribute, 1324an attribute for selectable items, an attribute for unselectable items, 1325and a pad character used to separate item name text from description 1326text. These have reasonable defaults which the library allows you to 1327change (see the <CODE>menu_attribs(3x)</CODE> manual page. 1328 1329<H2><A NAME="mwindows">Menu Windows</A></H2> 1330 1331Each menu has, as mentioned previously, a pair of associated windows. 1332Both these windows are painted when the menu is posted and erased when 1333the menu is unposted. <P> 1334 1335The outer or frame window is not otherwise touched by the menu 1336routines. It exists so the programmer can associate a title, a 1337border, or perhaps help text with the menu and have it properly 1338refreshed or erased at post/unpost time. The inner window or 1339<DFN>subwindow</DFN> is where the current menu page is displayed. <P> 1340 1341By default, both windows are <CODE>stdscr</CODE>. You can set them with the 1342functions in <CODE>menu_win(3x)</CODE>. <P> 1343 1344When you call <CODE>post_menu()</CODE>, you write the menu to its 1345subwindow. When you call <CODE>unpost_menu()</CODE>, you erase the 1346subwindow, However, neither of these actually modifies the screen. To 1347do that, call <CODE>wrefresh()</CODE> or some equivalent. 1348 1349<H2><A NAME="minput">Processing Menu Input</A></H2> 1350 1351The main loop of your menu-processing code should call 1352<CODE>menu_driver()</CODE> repeatedly. The first argument of this routine 1353is a menu pointer; the second is a menu command code. You should write an 1354input-fetching routine that maps input characters to menu command codes, and 1355pass its output to <CODE>menu_driver()</CODE>. The menu command codes are 1356fully documented in <CODE>menu_driver(3x)</CODE>. <P> 1357 1358The simplest group of command codes is <CODE>REQ_NEXT_ITEM</CODE>, 1359<CODE>REQ_PREV_ITEM</CODE>, <CODE>REQ_FIRST_ITEM</CODE>, 1360<CODE>REQ_LAST_ITEM</CODE>, <CODE>REQ_UP_ITEM</CODE>, 1361<CODE>REQ_DOWN_ITEM</CODE>, <CODE>REQ_LEFT_ITEM</CODE>, 1362<CODE>REQ_RIGHT_ITEM</CODE>. These change the currently selected 1363item. These requests may cause scrolling of the menu page if it only 1364partially displayed. <P> 1365 1366There are explicit requests for scrolling which also change the 1367current item (because the select location does not change, but the 1368item there does). These are <CODE>REQ_SCR_DLINE</CODE>, 1369<CODE>REQ_SCR_ULINE</CODE>, <CODE>REQ_SCR_DPAGE</CODE>, and 1370<CODE>REQ_SCR_UPAGE</CODE>. <P> 1371 1372The <CODE>REQ_TOGGLE_ITEM</CODE> selects or deselects the current item. 1373It is for use in multi-valued menus; if you use it with <CODE>O_ONEVALUE</CODE> 1374on, you'll get an error return (<CODE>E_REQUEST_DENIED</CODE>). <P> 1375 1376Each menu has an associated pattern buffer. The 1377<CODE>menu_driver()</CODE> logic tries to accumulate printable ASCII 1378characters passed in in that buffer; when it matches a prefix of an 1379item name, that item (or the next matching item) is selected. If 1380appending a character yields no new match, that character is deleted 1381from the pattern buffer, and <CODE>menu_driver()</CODE> returns 1382<CODE>E_NO_MATCH</CODE>. <P> 1383 1384Some requests change the pattern buffer directly: 1385<CODE>REQ_CLEAR_PATTERN</CODE>, <CODE>REQ_BACK_PATTERN</CODE>, 1386<CODE>REQ_NEXT_MATCH</CODE>, <CODE>REQ_PREV_MATCH</CODE>. The latter 1387two are useful when pattern buffer input matches more than one item 1388in a multi-valued menu. <P> 1389 1390Each successful scroll or item navigation request clears the pattern 1391buffer. It is also possible to set the pattern buffer explicitly 1392with <CODE>set_menu_pattern()</CODE>. <P> 1393 1394Finally, menu driver requests above the constant <CODE>MAX_COMMAND</CODE> 1395are considered application-specific commands. The <CODE>menu_driver()</CODE> 1396code ignores them and returns <CODE>E_UNKNOWN_COMMAND</CODE>. 1397 1398<H2><A NAME="mmisc">Miscellaneous Other Features</A></H2> 1399 1400Various menu options can affect the processing and visual appearance 1401and input processing of menus. See <CODE>menu_opts(3x) for 1402details.</CODE> <P> 1403 1404It is possible to change the current item from application code; this 1405is useful if you want to write your own navigation requests. It is 1406also possible to explicitly set the top row of the menu display. See 1407<CODE>mitem_current(3x)</CODE>. 1408 1409If your application needs to change the menu subwindow cursor for 1410any reason, <CODE>pos_menu_cursor()</CODE> will restore it to the 1411correct location for continuing menu driver processing. <P> 1412 1413It is possible to set hooks to be called at menu initialization and 1414wrapup time, and whenever the selected item changes. See 1415<CODE>menu_hook(3x)</CODE>. <P> 1416 1417Each item, and each menu, has an associated user pointer on which you 1418can hang application data. See <CODE>mitem_userptr(3x)</CODE> and 1419<CODE>menu_userptr(3x)</CODE>. 1420 1421<H1><A NAME="form">The Forms Library</A></H1> 1422 1423The <CODE>form</CODE> library is a curses extension that supports easy 1424programming of on-screen forms for data entry and program control. <P> 1425 1426The <CODE>form</CODE> library first appeared in AT&T System V. The 1427version documented here is the <CODE>form</CODE> code distributed 1428with <CODE>ncurses</CODE>. 1429 1430<H2><A NAME="fcompile">Compiling With the form Library</A></H2> 1431 1432Your form-using modules must import the form library declarations with 1433 1434<PRE> 1435 #include <form.h> 1436</PRE> 1437 1438and must be linked explicitly with the forms library using an 1439<CODE>-lform</CODE> argument. Note that they must also link the 1440<CODE>ncurses</CODE> library with <CODE>-lncurses</CODE>. Many linkers 1441are two-pass and will accept either order, but it is still good practice 1442to put <CODE>-lform</CODE> first and <CODE>-lncurses</CODE> second. 1443 1444<H2><A NAME="foverview">Overview of Forms</A></H2> 1445 1446A form is a collection of fields; each field may be either a label 1447(explanatory text) or a data-entry location. Long forms may be 1448segmented into pages; each entry to a new page clears the screen. <P> 1449To make forms, you create groups of fields and connect them with form 1450frame objects; the form library makes this relatively simple. <P> 1451 1452Once defined, a form can be <DFN>posted</DFN>, that is written to an 1453associated window. Actually, each form has two associated windows; a 1454containing window in which the programmer can scribble titles or 1455borders, and a subwindow in which the form fields proper are displayed. <P> 1456 1457As the form user fills out the posted form, navigation and editing 1458keys support movement between fields, editing keys support modifying 1459field, and plain text adds to or changes data in a current field. The 1460form library allows you (the forms designer) to bind each navigation 1461and editing key to any keystroke accepted by <CODE>curses</CODE> 1462 1463Fields may have validation conditions on them, so that they check input 1464data for type and value. The form library supplies a rich set of 1465pre-defined field types, and makes it relatively easy to define new ones. <P> 1466 1467Once its transaction is completed (or aborted), a form may be 1468<DFN>unposted</DFN> (that is, undisplayed), and finally freed to make 1469the storage associated with it and its items available for re-use. <P> 1470 1471The general flow of control of a form program looks like this: 1472 1473<OL> 1474<LI>Initialize <CODE>curses</CODE>. 1475<LI>Create the form fields, using <CODE>new_field()</CODE>. 1476<LI>Create the form using <CODE>new_form()</CODE>. 1477<LI>Post the form using <CODE>post_form()</CODE>. 1478<LI>Refresh the screen. 1479<LI>Process user requests via an input loop. 1480<LI>Unpost the form using <CODE>unpost_form()</CODE>. 1481<LI>Free the form, using <CODE>free_form()</CODE>. 1482<LI>Free the fields using <CODE>free_field()</CODE>. 1483<LI>Terminate <CODE>curses</CODE>. 1484</OL> 1485 1486Note that this looks much like a menu program; the form library handles 1487tasks which are in many ways similar, and its interface was obviously 1488designed to resemble that of the <A HREF="#menu">menu library</A> 1489wherever possible. <P> 1490 1491In forms programs, however, the `process user requests' is somewhat more 1492complicated than for menus. Besides menu-like navigation operations, 1493the menu driver loop has to support field editing and data validation. 1494 1495<H2><A NAME="fcreate">Creating and Freeing Fields and Forms</A></H2> 1496 1497The basic function for creating fields is <CODE>new_field()</CODE>: 1498 1499<PRE> 1500FIELD *new_field(int height, int width, /* new field size */ 1501 int top, int left, /* upper left corner */ 1502 int offscreen, /* number of offscreen rows */ 1503 int nbuf); /* number of working buffers */ 1504</PRE> 1505 1506Menu items always occupy a single row, but forms fields may have 1507multiple rows. So <CODE>new_field()</CODE> requires you to specify a 1508width and height (the first two arguments, which mist both be greater 1509than zero). <P> 1510 1511You must also specify the location of the field's upper left corner on 1512the screen (the third and fourth arguments, which must be zero or 1513greater). Note that these coordinates are relative to the form 1514subwindow, which will coincide with <CODE>stdscr</CODE> by default but 1515need not be <CODE>stdscr</CODE> if you've done an explicit 1516<CODE>set_form_win()</CODE> call. <P> 1517 1518The fifth argument allows you to specify a number of off-screen rows. If 1519this is zero, the entire field will always be displayed. If it is 1520nonzero, the form will be scrollable, with only one screen-full (initially 1521the top part) displayed at any given time. If you make a field dynamic 1522and grow it so it will no longer fit on the screen, the form will become 1523scrollable even if the <CODE>offscreen</CODE> argument was initially zero. <P> 1524 1525The forms library allocates one working buffer per field; the size of 1526each buffer is <CODE>((height + offscreen)*width + 1</CODE>, one character 1527for each position in the field plus a NUL terminator. The sixth 1528argument is the number of additional data buffers to allocate for the 1529field; your application can use them for its own purposes. 1530 1531<PRE> 1532FIELD *dup_field(FIELD *field, /* field to copy */ 1533 int top, int left); /* location of new copy */ 1534</PRE> 1535 1536The function <CODE>dup_field()</CODE> duplicates an existing field at a 1537new location. Size and buffering information are copied; some 1538attribute flags and status bits are not (see the 1539<CODE>form_field_new(3X)</CODE> for details). 1540 1541<PRE> 1542FIELD *link_field(FIELD *field, /* field to copy */ 1543 int top, int left); /* location of new copy */ 1544</PRE> 1545 1546The function <CODE>link_field()</CODE> also duplicates an existing field 1547at a new location. The difference from <CODE>dup_field()</CODE> is that 1548it arranges for the new field's buffer to be shared with the old one. <P> 1549 1550Besides the obvious use in making a field editable from two different 1551form pages, linked fields give you a way to hack in dynamic labels. If 1552you declare several fields linked to an original, and then make them 1553inactive, changes from the original will still be propagated to the 1554linked fields. <P> 1555 1556As with duplicated fields, linked fields have attribute bits separate 1557from the original. <P> 1558 1559As you might guess, all these field-allocations return <CODE>NULL</CODE> if 1560the field allocation is not possible due to an out-of-memory error or 1561out-of-bounds arguments. <P> 1562 1563To connect fields to a form, use 1564 1565<PRE> 1566FORM *new_form(FIELD **fields); 1567</PRE> 1568 1569This function expects to see a NULL-terminated array of field pointers. 1570Said fields are connected to a newly-allocated form object; its address 1571is returned (or else NULL if the allocation fails). <P> 1572 1573Note that <CODE>new_field()</CODE> does <EM>not</EM> copy the pointer array 1574into private storage; if you modify the contents of the pointer array 1575during forms processing, all manner of bizarre things might happen. Also 1576note that any given field may only be connected to one form. <P> 1577 1578The functions <CODE>free_field()</CODE> and <CODE>free_form</CODE> are available 1579to free field and form objects. It is an error to attempt to free a field 1580connected to a form, but not vice-versa; thus, you will generally free 1581your form objects first. 1582 1583<H2><A NAME="fattributes">Fetching and Changing Field Attributes</A></H2> 1584 1585Each form field has a number of location and size attributes 1586associated with it. There are other field attributes used to control 1587display and editing of the field. Some (for example, the <CODE>O_STATIC</CODE> bit) 1588involve sufficient complications to be covered in sections of their own 1589later on. We cover the functions used to get and set several basic 1590attributes here. <P> 1591 1592When a field is created, the attributes not specified by the 1593<CODE>new_field</CODE> function are copied from an invisible system 1594default field. In attribute-setting and -fetching functions, the 1595argument NULL is taken to mean this field. Changes to it persist 1596as defaults until your forms application terminates. 1597 1598<H3><A NAME="fsizes">Fetching Size and Location Data</A></H3> 1599 1600You can retrieve field sizes and locations through: 1601 1602<PRE> 1603int field_info(FIELD *field, /* field from which to fetch */ 1604 int *height, *int width, /* field size */ 1605 int *top, int *left, /* upper left corner */ 1606 int *offscreen, /* number of offscreen rows */ 1607 int *nbuf); /* number of working buffers */ 1608</PRE> 1609 1610This function is a sort of inverse of <CODE>new_field()</CODE>; instead of 1611setting size and location attributes of a new field, it fetches them 1612from an existing one. 1613 1614<H3><A NAME="flocation">Changing the Field Location</A></H3> 1615 1616It is possible to move a field's location on the screen: 1617 1618<PRE> 1619int move_field(FIELD *field, /* field to alter */ 1620 int top, int left); /* new upper-left corner */ 1621</PRE> 1622 1623You can, of course. query the current location through <CODE>field_info()</CODE>. 1624 1625<H3><A NAME="fjust">The Justification Attribute</A></H3> 1626 1627One-line fields may be unjustified, justified right, justified left, 1628or centered. Here is how you manipulate this attribute: 1629 1630<PRE> 1631int set_field_just(FIELD *field, /* field to alter */ 1632 int justmode); /* mode to set */ 1633 1634int field_just(FIELD *field); /* fetch mode of field */ 1635</PRE> 1636 1637The mode values accepted and returned by this functions are 1638preprocessor macros <CODE>NO_JUSTIFICATION</CODE>, <CODE>JUSTIFY_RIGHT</CODE>, 1639<CODE>JUSTIFY_LEFT</CODE>, or <CODE>JUSTIFY_CENTER</CODE>. 1640 1641<H3><A NAME="fdispatts">Field Display Attributes</A></H3> 1642 1643For each field, you can set a foreground attribute for entered 1644characters, a background attribute for the entire field, and a pad 1645character for the unfilled portion of the field. You can also 1646control pagination of the form. <P> 1647 1648This group of four field attributes controls the visual appearance 1649of the field on the screen, without affecting in any way the data 1650in the field buffer. 1651 1652<PRE> 1653int set_field_fore(FIELD *field, /* field to alter */ 1654 chtype attr); /* attribute to set */ 1655 1656chtype field_fore(FIELD *field); /* field to query */ 1657 1658int set_field_back(FIELD *field, /* field to alter */ 1659 chtype attr); /* attribute to set */ 1660 1661chtype field_back(FIELD *field); /* field to query */ 1662 1663int set_field_pad(FIELD *field, /* field to alter */ 1664 int pad); /* pad character to set */ 1665 1666chtype field_pad(FIELD *field); 1667 1668int set_new_page(FIELD *field, /* field to alter */ 1669 int flag); /* TRUE to force new page */ 1670 1671chtype new_page(FIELD *field); /* field to query */ 1672</PRE> 1673 1674The attributes set and returned by the first four functions are normal 1675<CODE>curses(3x)</CODE> display attribute values (<CODE>A_STANDOUT</CODE>, 1676<CODE>A_BOLD</CODE>, <CODE>A_REVERSE</CODE> etc). 1677 1678The page bit of a field controls whether it is displayed at the start of 1679a new form screen. 1680 1681<H3><A NAME="foptions">Field Option Bits</A></H3> 1682 1683There is also a large collection of field option bits you can set to control 1684various aspects of forms processing. You can manipulate them with these 1685functions: 1686 1687<PRE> 1688int set_field_opts(FIELD *field, /* field to alter */ 1689 int attr); /* attribute to set */ 1690 1691int field_opts_on(FIELD *field, /* field to alter */ 1692 int attr); /* attributes to turn on */ 1693 1694int field_opts_off(FIELD *field, /* field to alter */ 1695 int attr); /* attributes to turn off */ 1696 1697int field_opts(FIELD *field); /* field to query */ 1698</PRE> 1699 1700By default, all options are on. Here are the available option bits: 1701<DL> 1702<DT> O_VISIBLE 1703<DD> Controls whether the field is visible on the screen. Can be used 1704during form processing to hide or pop up fields depending on the value 1705of parent fields. 1706<DT> O_ACTIVE 1707<DD> Controls whether the field is active during forms processing (i.e. 1708visited by form navigation keys). Can be used to make labels or derived 1709fields with buffer values alterable by the forms application, not the user. 1710<DT> O_PUBLIC 1711<DD> Controls whether data is displayed during field entry. If this option is 1712turned off on a field, the library will accept and edit data in that field, 1713but it will not be displayed and the visible field cursor will not move. 1714You can turn off the O_PUBLIC bit to define password fields. 1715<DT> O_EDIT 1716<DD> Controls whether the field's data can be modified. When this option is 1717off, all editing requests except <CODE>REQ_PREV_CHOICE</CODE> and 1718<CODE>REQ_NEXT_CHOICE</CODE> will fail. Such read-only fields may be useful for 1719help messages. 1720<DT> O_WRAP 1721<DD> Controls word-wrapping in multi-line fields. Normally, when any 1722character of a (blank-separated) word reaches the end of the current line, the 1723entire word is wrapped to the next line (assuming there is one). When this 1724option is off, the word will be split across the line break. 1725<DT> O_BLANK 1726<DD> Controls field blanking. When this option is on, entering a character at 1727the first field position erases the entire field (except for the just-entered 1728character). 1729<DT> O_AUTOSKIP 1730<DD> Controls automatic skip to next field when this one fills. Normally, 1731when the forms user tries to type more data into a field than will fit, 1732the editing location jumps to next field. When this option is off, the 1733user's cursor will hang at the end of the field. This option is ignored 1734in dynamic fields that have not reached their size limit. 1735<DT> O_NULLOK 1736<DD> Controls whether <A HREF="#fvalidation">validation</A> is applied to 1737blank fields. Normally, it is not; the user can leave a field blank 1738without invoking the usual validation check on exit. If this option is 1739off on a field, exit from it will invoke a validation check. 1740<DT> O_PASSOK 1741<DD> Controls whether validation occurs on every exit, or only after 1742the field is modified. Normally the latter is true. Setting O_PASSOK 1743may be useful if your field's validation function may change during 1744forms processing. 1745<DT> O_STATIC 1746<DD> Controls whether the field is fixed to its initial dimensions. If you 1747turn this off, the field becomes <A HREF="#fdynamic">dynamic</A> and will 1748stretch to fit entered data. 1749</DL> 1750 1751A field's options cannot be changed while the field is currently selected. 1752However, options may be changed on posted fields that are not current. <P> 1753 1754The option values are bit-masks and can be composed with logical-or in 1755the obvious way. 1756 1757<H2><A NAME="fstatus">Field Status</A></H2> 1758 1759Every field has a status flag, which is set to FALSE when the field is 1760created and TRUE when the value in field buffer 0 changes. This flag can 1761be queried and set directly: 1762 1763<PRE> 1764int set_field_status(FIELD *field, /* field to alter */ 1765 int status); /* mode to set */ 1766 1767int field_status(FIELD *field); /* fetch mode of field */ 1768</PRE> 1769 1770Setting this flag under program control can be useful if you use the same 1771form repeatedly, looking for modified fields each time. <P> 1772 1773Calling <CODE>field_status()</CODE> on a field not currently selected 1774for input will return a correct value. Calling <CODE>field_status()</CODE> on a 1775field that is currently selected for input may not necessarily give a 1776correct field status value, because entered data isn't necessarily copied to 1777buffer zero before the exit validation check. 1778 1779To guarantee that the returned status value reflects reality, call 1780<CODE>field_status()</CODE> either (1) in the field's exit validation check 1781routine, (2) from the field's or form's initialization or termination 1782hooks, or (3) just after a <CODE>REQ_VALIDATION</CODE> request has been 1783processed by the forms driver. 1784 1785<H2><A NAME="fuser">Field User Pointer</A></H2> 1786 1787Each field structure contains one character pointer slot that is not used 1788by the forms library. It is intended to be used by applications to store 1789private per-field data. You can manipulate it with: 1790 1791<PRE> 1792int set_field_userptr(FIELD *field, /* field to alter */ 1793 char *userptr); /* mode to set */ 1794 1795char *field_userptr(FIELD *field); /* fetch mode of field */ 1796</PRE> 1797 1798(Properly, this user pointer field ought to have <CODE>(void *)</CODE> type. 1799The <CODE>(char *)</CODE> type is retained for System V compatibility.) <P> 1800 1801It is valid to set the user pointer of the default field (with a 1802<CODE>set_field_userptr()</CODE> call passed a NULL field pointer.) 1803When a new field is created, the default-field user pointer is copied 1804to initialize the new field's user pointer. 1805 1806<H2><A NAME="fdynamic">Variable-Sized Fields</A></H2> 1807 1808Normally, a field is fixed at the size specified for it at creation 1809time. If, however, you turn off its O_STATIC bit, it becomes 1810<DFN>dynamic</DFN> and will automatically resize itself to accommodate 1811data as it is entered. If the field has extra buffers associated with it, 1812they will grow right along with the main input buffer. <P> 1813 1814A one-line dynamic field will have a fixed height (1) but variable 1815width, scrolling horizontally to display data within the field area as 1816originally dimensioned and located. A multi-line dynamic field will 1817have a fixed width, but variable height (number of rows), scrolling 1818vertically to display data within the field area as originally 1819dimensioned and located. <P> 1820 1821Normally, a dynamic field is allowed to grow without limit. But it is 1822possible to set an upper limit on the size of a dynamic field. You do 1823it with this function: 1824 1825<PRE> 1826int set_max_field(FIELD *field, /* field to alter (may not be NULL) */ 1827 int max_size); /* upper limit on field size */ 1828</PRE> 1829 1830If the field is one-line, <CODE>max_size</CODE> is taken to be a column size 1831limit; if it is multi-line, it is taken to be a line size limit. To disable 1832any limit, use an argument of zero. The growth limit can be changed whether 1833or not the O_STATIC bit is on, but has no effect until it is. <P> 1834 1835The following properties of a field change when it becomes dynamic: 1836 1837<UL> 1838<LI>If there is no growth limit, there is no final position of the field; 1839therefore <CODE>O_AUTOSKIP</CODE> and <CODE>O_NL_OVERLOAD</CODE> are ignored. 1840<LI>Field justification will be ignored (though whatever justification is 1841set up will be retained internally and can be queried). 1842<LI>The <CODE>dup_field()</CODE> and <CODE>link_field()</CODE> calls copy 1843dynamic-buffer sizes. If the <CODE>O_STATIC</CODE> option is set on one of a 1844collection of links, buffer resizing will occur only when the field is 1845edited through that link. 1846<LI>The call <CODE>field_info()</CODE> will retrieve the original static size of 1847the field; use <CODE>dynamic_field_info()</CODE> to get the actual dynamic size. 1848</UL> 1849 1850<H2><A NAME="fvalidation">Field Validation</A></H2> 1851 1852By default, a field will accept any data that will fit in its input buffer. 1853However, it is possible to attach a validation type to a field. If you do 1854this, any attempt to leave the field while it contains data that doesn't 1855match the validation type will fail. Some validation types also have a 1856character-validity check for each time a character is entered in the field. <P> 1857 1858A field's validation check (if any) is not called when 1859<CODE>set_field_buffer()</CODE> modifies the input buffer, nor when that buffer 1860is changed through a linked field. <P> 1861 1862The <CODE>form</CODE> library provides a rich set of pre-defined validation 1863types, and gives you the capability to define custom ones of your own. You 1864can examine and change field validation attributes with the following 1865functions: 1866 1867<PRE> 1868int set_field_type(FIELD *field, /* field to alter */ 1869 FIELDTYPE *ftype, /* type to associate */ 1870 ...); /* additional arguments*/ 1871 1872FIELDTYPE *field_type(FIELD *field); /* field to query */ 1873</PRE> 1874 1875The validation type of a field is considered an attribute of the field. As 1876with other field attributes, Also, doing <CODE>set_field_type()</CODE> with a 1877<CODE>NULL</CODE> field default will change the system default for validation of 1878newly-created fields. <P> 1879 1880Here are the pre-defined validation types: 1881 1882<H3><A NAME="ftype_alpha">TYPE_ALPHA</A></H3> 1883 1884This field type accepts alphabetic data; no blanks, no digits, no special 1885characters (this is checked at character-entry time). It is set up with: 1886 1887<PRE> 1888int set_field_type(FIELD *field, /* field to alter */ 1889 TYPE_ALPHA, /* type to associate */ 1890 int width); /* maximum width of field */ 1891</PRE> 1892 1893The <CODE>width</CODE> argument sets a minimum width of data. Typically 1894you'll want to set this to the field width; if it's greater than the 1895field width, the validation check will always fail. A minimum width 1896of zero makes field completion optional. 1897 1898<H3><A NAME="ftype_alnum">TYPE_ALNUM</A></H3> 1899 1900This field type accepts alphabetic data and digits; no blanks, no special 1901characters (this is checked at character-entry time). It is set up with: 1902 1903<PRE> 1904int set_field_type(FIELD *field, /* field to alter */ 1905 TYPE_ALNUM, /* type to associate */ 1906 int width); /* maximum width of field */ 1907</PRE> 1908 1909The <CODE>width</CODE> argument sets a minimum width of data. As with 1910TYPE_ALPHA, typically you'll want to set this to the field width; if it's 1911greater than the field width, the validation check will always fail. A 1912minimum width of zero makes field completion optional. 1913 1914<H3><A NAME="ftype_enum">TYPE_ENUM</A></H3> 1915 1916This type allows you to restrict a field's values to be among a specified 1917set of string values (for example, the two-letter postal codes for U.S. 1918states). It is set up with: 1919 1920<PRE> 1921int set_field_type(FIELD *field, /* field to alter */ 1922 TYPE_ENUM, /* type to associate */ 1923 char **valuelist; /* list of possible values */ 1924 int checkcase; /* case-sensitive? */ 1925 int checkunique); /* must specify uniquely? */ 1926</PRE> 1927 1928The <CODE>valuelist</CODE> parameter must point at a NULL-terminated list of 1929valid strings. The <CODE>checkcase</CODE> argument, if true, makes comparison 1930with the string case-sensitive. <P> 1931 1932When the user exits a TYPE_ENUM field, the validation procedure tries to 1933complete the data in the buffer to a valid entry. If a complete choice string 1934has been entered, it is of course valid. But it is also possible to enter a 1935prefix of a valid string and have it completed for you. <P> 1936 1937By default, if you enter such a prefix and it matches more than one value 1938in the string list, the prefix will be completed to the first matching 1939value. But the <CODE>checkunique</CODE> argument, if true, requires prefix 1940matches to be unique in order to be valid. <P> 1941 1942The <CODE>REQ_NEXT_CHOICE</CODE> and <CODE>REQ_PREV_CHOICE</CODE> input requests 1943can be particularly useful with these fields. 1944 1945<H3><A NAME="ftype_integer">TYPE_INTEGER</A></H3> 1946 1947This field type accepts an integer. It is set up as follows: 1948 1949<PRE> 1950int set_field_type(FIELD *field, /* field to alter */ 1951 TYPE_INTEGER, /* type to associate */ 1952 int padding, /* # places to zero-pad to */ 1953 int vmin, int vmax); /* valid range */ 1954</PRE> 1955 1956Valid characters consist of an optional leading minus and digits. 1957The range check is performed on exit. If the range maximum is less 1958than or equal to the minimum, the range is ignored. <P> 1959 1960If the value passes its range check, it is padded with as many leading 1961zero digits as necessary to meet the padding argument. <P> 1962 1963A <CODE>TYPE_INTEGER</CODE> value buffer can conveniently be interpreted 1964with the C library function <CODE>atoi(3)</CODE>. 1965 1966<H3><A NAME="ftype_numeric">TYPE_NUMERIC</A></H3> 1967 1968This field type accepts a decimal number. It is set up as follows: 1969 1970<PRE> 1971int set_field_type(FIELD *field, /* field to alter */ 1972 TYPE_NUMERIC, /* type to associate */ 1973 int padding, /* # places of precision */ 1974 double vmin, double vmax); /* valid range */ 1975</PRE> 1976 1977Valid characters consist of an optional leading minus and digits. possibly 1978including a decimal point. If your system supports locale's, the decimal point 1979character used must be the one defined by your locale. The range check is 1980performed on exit. If the range maximum is less than or equal to the minimum, 1981the range is ignored. <P> 1982 1983If the value passes its range check, it is padded with as many trailing 1984zero digits as necessary to meet the padding argument. <P> 1985 1986A <CODE>TYPE_NUMERIC</CODE> value buffer can conveniently be interpreted 1987with the C library function <CODE>atof(3)</CODE>. 1988 1989<H3><A NAME="ftype_regexp">TYPE_REGEXP</A></H3> 1990 1991This field type accepts data matching a regular expression. It is set up 1992as follows: 1993 1994<PRE> 1995int set_field_type(FIELD *field, /* field to alter */ 1996 TYPE_REGEXP, /* type to associate */ 1997 char *regexp); /* expression to match */ 1998</PRE> 1999 2000The syntax for regular expressions is that of <CODE>regcomp(3)</CODE>. 2001The check for regular-expression match is performed on exit. 2002 2003<H2><A NAME="fbuffer">Direct Field Buffer Manipulation</A></H2> 2004 2005The chief attribute of a field is its buffer contents. When a form has 2006been completed, your application usually needs to know the state of each 2007field buffer. You can find this out with: 2008 2009<PRE> 2010char *field_buffer(FIELD *field, /* field to query */ 2011 int bufindex); /* number of buffer to query */ 2012</PRE> 2013 2014Normally, the state of the zero-numbered buffer for each field is set by 2015the user's editing actions on that field. It's sometimes useful to be able 2016to set the value of the zero-numbered (or some other) buffer from your 2017application: 2018 2019<PRE> 2020int set_field_buffer(FIELD *field, /* field to alter */ 2021 int bufindex, /* number of buffer to alter */ 2022 char *value); /* string value to set */ 2023</PRE> 2024 2025If the field is not large enough and cannot be resized to a sufficiently 2026large size to contain the specified value, the value will be truncated 2027to fit. <P> 2028 2029Calling <CODE>field_buffer()</CODE> with a null field pointer will raise an 2030error. Calling <CODE>field_buffer()</CODE> on a field not currently selected 2031for input will return a correct value. Calling <CODE>field_buffer()</CODE> on a 2032field that is currently selected for input may not necessarily give a 2033correct field buffer value, because entered data isn't necessarily copied to 2034buffer zero before the exit validation check. 2035 2036To guarantee that the returned buffer value reflects on-screen reality, 2037call <CODE>field_buffer()</CODE> either (1) in the field's exit validation 2038check routine, (2) from the field's or form's initialization or termination 2039hooks, or (3) just after a <CODE>REQ_VALIDATION</CODE> request has been processed 2040by the forms driver. 2041 2042<H2><A NAME="formattrs">Attributes of Forms</A></H2> 2043 2044As with field attributes, form attributes inherit a default from a 2045system default form structure. These defaults can be queried or set by 2046of these functions using a form-pointer argument of <CODE>NULL</CODE>. <P> 2047 2048The principal attribute of a form is its field list. You can query 2049and change this list with: 2050 2051<PRE> 2052int set_form_fields(FORM *form, /* form to alter */ 2053 FIELD **fields); /* fields to connect */ 2054 2055char *form_fields(FORM *form); /* fetch fields of form */ 2056 2057int field_count(FORM *form); /* count connect fields */ 2058</PRE> 2059 2060The second argument of <CODE>set_form_fields()</CODE> may be a 2061NULL-terminated field pointer array like the one required by 2062<CODE>new_form()</CODE>. In that case, the old fields of the form are 2063disconnected but not freed (and eligible to be connected to other 2064forms), then the new fields are connected. <P> 2065 2066It may also be null, in which case the old fields are disconnected 2067(and not freed) but no new ones are connected. <P> 2068 2069The <CODE>field_count()</CODE> function simply counts the number of fields 2070connected to a given from. It returns -1 if the form-pointer argument 2071is NULL. 2072 2073<H2><A NAME="fdisplay">Control of Form Display</A></H2> 2074 2075In the overview section, you saw that to display a form you normally 2076start by defining its size (and fields), posting it, and refreshing 2077the screen. There is an hidden step before posting, which is the 2078association of the form with a frame window (actually, a pair of 2079windows) within which it will be displayed. By default, the forms 2080library associates every form with the full-screen window 2081<CODE>stdscr</CODE>. <P> 2082 2083By making this step explicit, you can associate a form with a declared 2084frame window on your screen display. This can be useful if you want to 2085adapt the form display to different screen sizes, dynamically tile 2086forms on the screen, or use a form as part of an interface layout 2087managed by <A HREF="#panels">panels</A>. <P> 2088 2089The two windows associated with each form have the same functions as 2090their analogues in the <A HREF="#menu">menu library</A>. Both these 2091windows are painted when the form is posted and erased when the form 2092is unposted. <P> 2093 2094The outer or frame window is not otherwise touched by the form 2095routines. It exists so the programmer can associate a title, a 2096border, or perhaps help text with the form and have it properly 2097refreshed or erased at post/unpost time. The inner window or subwindow 2098is where the current form page is actually displayed. <P> 2099 2100In order to declare your own frame window for a form, you'll need to 2101know the size of the form's bounding rectangle. You can get this 2102information with: 2103 2104<PRE> 2105int scale_form(FORM *form, /* form to query */ 2106 int *rows, /* form rows */ 2107 int *cols); /* form cols */ 2108</PRE> 2109 2110The form dimensions are passed back in the locations pointed to by 2111the arguments. Once you have this information, you can use it to 2112declare of windows, then use one of these functions: 2113 2114<PRE> 2115int set_form_win(FORM *form, /* form to alter */ 2116 WINDOW *win); /* frame window to connect */ 2117 2118WINDOW *form_win(FORM *form); /* fetch frame window of form */ 2119 2120int set_form_sub(FORM *form, /* form to alter */ 2121 WINDOW *win); /* form subwindow to connect */ 2122 2123WINDOW *form_sub(FORM *form); /* fetch form subwindow of form */ 2124</PRE> 2125 2126Note that curses operations, including <CODE>refresh()</CODE>, on the form, 2127should be done on the frame window, not the form subwindow. <P> 2128 2129It is possible to check from your application whether all of a 2130scrollable field is actually displayed within the menu subwindow. Use 2131these functions: 2132 2133<PRE> 2134int data_ahead(FORM *form); /* form to be queried */ 2135 2136int data_behind(FORM *form); /* form to be queried */ 2137</PRE> 2138 2139The function <CODE>data_ahead()</CODE> returns TRUE if (a) the current 2140field is one-line and has undisplayed data off to the right, (b) the current 2141field is multi-line and there is data off-screen below it. <P> 2142 2143The function <CODE>data_behind()</CODE> returns TRUE if the first (upper 2144left hand) character position is off-screen (not being displayed). <P> 2145 2146Finally, there is a function to restore the form window's cursor to the 2147value expected by the forms driver: 2148 2149<PRE> 2150int pos_form_cursor(FORM *) /* form to be queried */ 2151</PRE> 2152 2153If your application changes the form window cursor, call this function before 2154handing control back to the forms driver in order to re-synchronize it. 2155 2156<H2><A NAME="fdriver">Input Processing in the Forms Driver</A></H2> 2157 2158The function <CODE>form_driver()</CODE> handles virtualized input requests 2159for form navigation, editing, and validation requests, just as 2160<CODE>menu_driver</CODE> does for menus (see the section on <A 2161HREF="#minput">menu input handling</A>). 2162 2163<PRE> 2164int form_driver(FORM *form, /* form to pass input to */ 2165 int request); /* form request code */ 2166</PRE> 2167 2168Your input virtualization function needs to take input and then convert it 2169to either an alphanumeric character (which is treated as data to be 2170entered in the currently-selected field), or a forms processing request. <P> 2171 2172The forms driver provides hooks (through input-validation and 2173field-termination functions) with which your application code can check 2174that the input taken by the driver matched what was expected. 2175 2176<H3><A NAME="fpage">Page Navigation Requests</A></H3> 2177 2178These requests cause page-level moves through the form, 2179triggering display of a new form screen. 2180 2181<DL> 2182<DT> <CODE>REQ_NEXT_PAGE</CODE> 2183<DD> Move to the next form page. 2184<DT> <CODE>REQ_PREV_PAGE</CODE> 2185<DD> Move to the previous form page. 2186<DT> <CODE>REQ_FIRST_PAGE</CODE> 2187<DD> Move to the first form page. 2188<DT> <CODE>REQ_LAST_PAGE</CODE> 2189<DD> Move to the last form page. 2190</DL> 2191 2192These requests treat the list as cyclic; that is, <CODE>REQ_NEXT_PAGE</CODE> 2193from the last page goes to the first, and <CODE>REQ_PREV_PAGE</CODE> from 2194the first page goes to the last. 2195 2196<H3><A NAME="ffield">Inter-Field Navigation Requests</A></H3> 2197 2198These requests handle navigation between fields on the same page. 2199 2200<DL> 2201<DT> <CODE>REQ_NEXT_FIELD</CODE> 2202<DD> Move to next field. 2203<DT> <CODE>REQ_PREV_FIELD</CODE> 2204<DD> Move to previous field. 2205<DT> <CODE>REQ_FIRST_FIELD</CODE> 2206<DD> Move to the first field. 2207<DT> <CODE>REQ_LAST_FIELD</CODE> 2208<DD> Move to the last field. 2209<DT> <CODE>REQ_SNEXT_FIELD</CODE> 2210<DD> Move to sorted next field. 2211<DT> <CODE>REQ_SPREV_FIELD</CODE> 2212<DD> Move to sorted previous field. 2213<DT> <CODE>REQ_SFIRST_FIELD</CODE> 2214<DD> Move to the sorted first field. 2215<DT> <CODE>REQ_SLAST_FIELD</CODE> 2216<DD> Move to the sorted last field. 2217<DT> <CODE>REQ_LEFT_FIELD</CODE> 2218<DD> Move left to field. 2219<DT> <CODE>REQ_RIGHT_FIELD</CODE> 2220<DD> Move right to field. 2221<DT> <CODE>REQ_UP_FIELD</CODE> 2222<DD> Move up to field. 2223<DT> <CODE>REQ_DOWN_FIELD</CODE> 2224<DD> Move down to field. 2225</DL> 2226 2227These requests treat the list of fields on a page as cyclic; that is, 2228<CODE>REQ_NEXT_FIELD</CODE> from the last field goes to the first, and 2229<CODE>REQ_PREV_FIELD</CODE> from the first field goes to the last. The 2230order of the fields for these (and the <CODE>REQ_FIRST_FIELD</CODE> and 2231<CODE>REQ_LAST_FIELD</CODE> requests) is simply the order of the field 2232pointers in the form array (as set up by <CODE>new_form()</CODE> or 2233<CODE>set_form_fields()</CODE> <P> 2234 2235It is also possible to traverse the fields as if they had been sorted in 2236screen-position order, so the sequence goes left-to-right and top-to-bottom. 2237To do this, use the second group of four sorted-movement requests. <P> 2238 2239Finally, it is possible to move between fields using visual directions up, 2240down, right, and left. To accomplish this, use the third group of four 2241requests. Note, however, that the position of a form for purposes of these 2242requests is its upper-left corner. <P> 2243 2244For example, suppose you have a multi-line field B, and two 2245single-line fields A and C on the same line with B, with A to the left 2246of B and C to the right of B. A <CODE>REQ_MOVE_RIGHT</CODE> from A will 2247go to B only if A, B, and C <EM>all</EM> share the same first line; 2248otherwise it will skip over B to C. 2249 2250<H3><A NAME="fifield">Intra-Field Navigation Requests</A></H3> 2251 2252These requests drive movement of the edit cursor within the currently 2253selected field. 2254 2255<DL> 2256<DT> <CODE>REQ_NEXT_CHAR</CODE> 2257<DD> Move to next character. 2258<DT> <CODE>REQ_PREV_CHAR</CODE> 2259<DD> Move to previous character. 2260<DT> <CODE>REQ_NEXT_LINE</CODE> 2261<DD> Move to next line. 2262<DT> <CODE>REQ_PREV_LINE</CODE> 2263<DD> Move to previous line. 2264<DT> <CODE>REQ_NEXT_WORD</CODE> 2265<DD> Move to next word. 2266<DT> <CODE>REQ_PREV_WORD</CODE> 2267<DD> Move to previous word. 2268<DT> <CODE>REQ_BEG_FIELD</CODE> 2269<DD> Move to beginning of field. 2270<DT> <CODE>REQ_END_FIELD</CODE> 2271<DD> Move to end of field. 2272<DT> <CODE>REQ_BEG_LINE</CODE> 2273<DD> Move to beginning of line. 2274<DT> <CODE>REQ_END_LINE</CODE> 2275<DD> Move to end of line. 2276<DT> <CODE>REQ_LEFT_CHAR</CODE> 2277<DD> Move left in field. 2278<DT> <CODE>REQ_RIGHT_CHAR</CODE> 2279<DD> Move right in field. 2280<DT> <CODE>REQ_UP_CHAR</CODE> 2281<DD> Move up in field. 2282<DT> <CODE>REQ_DOWN_CHAR</CODE> 2283<DD> Move down in field. 2284</DL> 2285 2286Each <EM>word</EM> is separated from the previous and next characters 2287by whitespace. The commands to move to beginning and end of line or field 2288look for the first or last non-pad character in their ranges. 2289 2290<H3><A NAME="fscroll">Scrolling Requests</A></H3> 2291 2292Fields that are dynamic and have grown and fields explicitly created 2293with offscreen rows are scrollable. One-line fields scroll horizontally; 2294multi-line fields scroll vertically. Most scrolling is triggered by 2295editing and intra-field movement (the library scrolls the field to keep the 2296cursor visible). It is possible to explicitly request scrolling with the 2297following requests: 2298 2299<DL> 2300<DT> <CODE>REQ_SCR_FLINE</CODE> 2301<DD> Scroll vertically forward a line. 2302<DT> <CODE>REQ_SCR_BLINE</CODE> 2303<DD> Scroll vertically backward a line. 2304<DT> <CODE>REQ_SCR_FPAGE</CODE> 2305<DD> Scroll vertically forward a page. 2306<DT> <CODE>REQ_SCR_BPAGE</CODE> 2307<DD> Scroll vertically backward a page. 2308<DT> <CODE>REQ_SCR_FHPAGE</CODE> 2309<DD> Scroll vertically forward half a page. 2310<DT> <CODE>REQ_SCR_BHPAGE</CODE> 2311<DD> Scroll vertically backward half a page. 2312<DT> <CODE>REQ_SCR_FCHAR</CODE> 2313<DD> Scroll horizontally forward a character. 2314<DT> <CODE>REQ_SCR_BCHAR</CODE> 2315<DD> Scroll horizontally backward a character. 2316<DT> <CODE>REQ_SCR_HFLINE</CODE> 2317<DD> Scroll horizontally one field width forward. 2318<DT> <CODE>REQ_SCR_HBLINE</CODE> 2319<DD> Scroll horizontally one field width backward. 2320<DT> <CODE>REQ_SCR_HFHALF</CODE> 2321<DD> Scroll horizontally one half field width forward. 2322<DT> <CODE>REQ_SCR_HBHALF</CODE> 2323<DD> Scroll horizontally one half field width backward. 2324</DL> 2325 2326For scrolling purposes, a <EM>page</EM> of a field is the height 2327of its visible part. 2328 2329<H3><A NAME="fedit">Editing Requests</A></H3> 2330 2331When you pass the forms driver an ASCII character, it is treated as a 2332request to add the character to the field's data buffer. Whether this 2333is an insertion or a replacement depends on the field's edit mode 2334(insertion is the default. <P> 2335 2336The following requests support editing the field and changing the edit 2337mode: 2338 2339<DL> 2340<DT> <CODE>REQ_INS_MODE</CODE> 2341<DD> Set insertion mode. 2342<DT> <CODE>REQ_OVL_MODE</CODE> 2343<DD> Set overlay mode. 2344<DT> <CODE>REQ_NEW_LINE</CODE> 2345<DD> New line request (see below for explanation). 2346<DT> <CODE>REQ_INS_CHAR</CODE> 2347<DD> Insert space at character location. 2348<DT> <CODE>REQ_INS_LINE</CODE> 2349<DD> Insert blank line at character location. 2350<DT> <CODE>REQ_DEL_CHAR</CODE> 2351<DD> Delete character at cursor. 2352<DT> <CODE>REQ_DEL_PREV</CODE> 2353<DD> Delete previous word at cursor. 2354<DT> <CODE>REQ_DEL_LINE</CODE> 2355<DD> Delete line at cursor. 2356<DT> <CODE>REQ_DEL_WORD</CODE> 2357<DD> Delete word at cursor. 2358<DT> <CODE>REQ_CLR_EOL</CODE> 2359<DD> Clear to end of line. 2360<DT> <CODE>REQ_CLR_EOF</CODE> 2361<DD> Clear to end of field. 2362<DT> <CODE>REQ_CLEAR_FIELD</CODE> 2363<DD> Clear entire field. 2364</DL> 2365 2366The behavior of the <CODE>REQ_NEW_LINE</CODE> and <CODE>REQ_DEL_PREV</CODE> requests 2367is complicated and partly controlled by a pair of forms options. 2368The special cases are triggered when the cursor is at the beginning of 2369a field, or on the last line of the field. <P> 2370 2371First, we consider <CODE>REQ_NEW_LINE</CODE>: <P> 2372 2373The normal behavior of <CODE>REQ_NEW_LINE</CODE> in insert mode is to break the 2374current line at the position of the edit cursor, inserting the portion of 2375the current line after the cursor as a new line following the current 2376and moving the cursor to the beginning of that new line (you may think 2377of this as inserting a newline in the field buffer). <P> 2378 2379The normal behavior of <CODE>REQ_NEW_LINE</CODE> in overlay mode is to clear the 2380current line from the position of the edit cursor to end of line. 2381The cursor is then moved to the beginning of the next line. <P> 2382 2383However, <CODE>REQ_NEW_LINE</CODE> at the beginning of a field, or on the 2384last line of a field, instead does a <CODE>REQ_NEXT_FIELD</CODE>. 2385<CODE>O_NL_OVERLOAD</CODE> option is off, this special action is 2386disabled. <P> 2387 2388Now, let us consider <CODE>REQ_DEL_PREV</CODE>: <P> 2389 2390The normal behavior of <CODE>REQ_DEL_PREV</CODE> is to delete the previous 2391character. If insert mode is on, and the cursor is at the start of a 2392line, and the text on that line will fit on the previous one, it 2393instead appends the contents of the current line to the previous one 2394and deletes the current line (you may think of this as deleting a 2395newline from the field buffer). <P> 2396 2397However, <CODE>REQ_DEL_PREV</CODE> at the beginning of a field is instead 2398treated as a <CODE>REQ_PREV_FIELD</CODE>. <P> If the 2399<CODE>O_BS_OVERLOAD</CODE> option is off, this special action is 2400disabled and the forms driver just returns <CODE>E_REQUEST_DENIED</CODE>. <P> 2401 2402See <A HREF="#frmoptions">Form Options</A> for discussion of how to set 2403and clear the overload options. 2404 2405<H3><A NAME="forder">Order Requests</A></H3> 2406 2407If the type of your field is ordered, and has associated functions 2408for getting the next and previous values of the type from a given value, 2409there are requests that can fetch that value into the field buffer: 2410 2411<DL> 2412<DT> <CODE>REQ_NEXT_CHOICE</CODE> 2413<DD> Place the successor value of the current value in the buffer. 2414<DT> <CODE>REQ_PREV_CHOICE</CODE> 2415<DD> Place the predecessor value of the current value in the buffer. 2416</DL> 2417 2418Of the built-in field types, only <CODE>TYPE_ENUM</CODE> has built-in successor 2419and predecessor functions. When you define a field type of your own 2420(see <A HREF="#fcustom">Custom Validation Types</A>), you can associate 2421our own ordering functions. 2422 2423<H3><A NAME="fappcmds">Application Commands</A></H3> 2424 2425Form requests are represented as integers above the <CODE>curses</CODE> value 2426greater than <CODE>KEY_MAX</CODE> and less than or equal to the constant 2427<CODE>MAX_COMMAND</CODE>. If your input-virtualization routine returns a 2428value above <CODE>MAX_COMMAND</CODE>, the forms driver will ignore it. 2429 2430<H2><A NAME="fhooks">Field Change Hooks</A></H2> 2431 2432It is possible to set function hooks to be executed whenever the 2433current field or form changes. Here are the functions that support this: 2434 2435<PRE> 2436typedef void (*HOOK)(); /* pointer to function returning void */ 2437 2438int set_form_init(FORM *form, /* form to alter */ 2439 HOOK hook); /* initialization hook */ 2440 2441HOOK form_init(FORM *form); /* form to query */ 2442 2443int set_form_term(FORM *form, /* form to alter */ 2444 HOOK hook); /* termination hook */ 2445 2446HOOK form_term(FORM *form); /* form to query */ 2447 2448int set_field_init(FORM *form, /* form to alter */ 2449 HOOK hook); /* initialization hook */ 2450 2451HOOK field_init(FORM *form); /* form to query */ 2452 2453int set_field_term(FORM *form, /* form to alter */ 2454 HOOK hook); /* termination hook */ 2455 2456HOOK field_term(FORM *form); /* form to query */ 2457</PRE> 2458 2459These functions allow you to either set or query four different hooks. 2460In each of the set functions, the second argument should be the 2461address of a hook function. These functions differ only in the timing 2462of the hook call. 2463 2464<DL> 2465<DT> form_init 2466<DD> This hook is called when the form is posted; also, just after 2467each page change operation. 2468<DT> field_init 2469<DD> This hook is called when the form is posted; also, just after 2470each field change 2471<DT> field_term 2472<DD> This hook is called just after field validation; that is, just before 2473the field is altered. It is also called when the form is unposted. 2474<DT> form_term 2475<DD> This hook is called when the form is unposted; also, just before 2476each page change operation. 2477</DL> 2478 2479Calls to these hooks may be triggered 2480<OL> 2481<LI>When user editing requests are processed by the forms driver 2482<LI>When the current page is changed by <CODE>set_current_field()</CODE> call 2483<LI>When the current field is changed by a <CODE>set_form_page()</CODE> call 2484</OL> 2485 2486See <A NAME="ffocus">Field Change Commands</A> for discussion of the latter 2487two cases. <P> 2488 2489You can set a default hook for all fields by passing one of the set functions 2490a NULL first argument. <P> 2491 2492You can disable any of these hooks by (re)setting them to NULL, the default 2493value. 2494 2495<H2><A HREF="#ffocus">Field Change Commands</A></H2> 2496 2497Normally, navigation through the form will be driven by the user's 2498input requests. But sometimes it is useful to be able to move the 2499focus for editing and viewing under control of your application, or 2500ask which field it currently is in. The following functions help you 2501accomplish this: 2502 2503<PRE> 2504int set_current_field(FORM *form, /* form to alter */ 2505 FIELD *field); /* field to shift to */ 2506 2507FIELD *current_field(FORM *form); /* form to query */ 2508 2509int field_index(FORM *form, /* form to query */ 2510 FIELD *field); /* field to get index of */ 2511</PRE> 2512 2513The function <CODE>field_index()</CODE> returns the index of the given field 2514in the given form's field array (the array passed to <CODE>new_form()</CODE> or 2515<CODE>set_form_fields()</CODE>). <P> 2516 2517The initial current field of a form is the first active field on the 2518first page. The function <CODE>set_form_fields()</CODE> resets this.<P> 2519 2520It is also possible to move around by pages. 2521 2522<PRE> 2523int set_form_page(FORM *form, /* form to alter */ 2524 int page); /* page to go to (0-origin) */ 2525 2526int form_page(FORM *form); /* return form's current page */ 2527</PRE> 2528 2529The initial page of a newly-created form is 0. The function 2530<CODE>set_form_fields()</CODE> resets this. 2531 2532<H2><A NAME="frmoptions">Form Options</A></H2> 2533 2534Like fields, forms may have control option bits. They can be changed 2535or queried with these functions: 2536 2537<PRE> 2538int set_form_opts(FORM *form, /* form to alter */ 2539 int attr); /* attribute to set */ 2540 2541int form_opts_on(FORM *form, /* form to alter */ 2542 int attr); /* attributes to turn on */ 2543 2544int form_opts_off(FORM *form, /* form to alter */ 2545 int attr); /* attributes to turn off */ 2546 2547int form_opts(FORM *form); /* form to query */ 2548</PRE> 2549 2550By default, all options are on. Here are the available option bits: 2551 2552<DL> 2553<DT> O_NL_OVERLOAD 2554<DD> Enable overloading of <CODE>REQ_NEW_LINE</CODE> as described in <A 2555href="#fedit">Editing Requests</A>. The value of this option is 2556ignored on dynamic fields that have not reached their size limit; 2557these have no last line, so the circumstances for triggering a 2558<CODE>REQ_NEXT_FIELD</CODE> never arise. 2559<DT> O_BS_OVERLOAD 2560<DD> Enable overloading of <CODE>REQ_DEL_PREV</CODE> as described in 2561<A href="#fedit">Editing Requests</A>. 2562</DL> 2563 2564The option values are bit-masks and can be composed with logical-or in 2565the obvious way. 2566 2567<H2><A NAME="fcustom">Custom Validation Types</A></H2> 2568 2569The <CODE>form</CODE> library gives you the capability to define custom 2570validation types of your own. Further, the optional additional arguments 2571of <CODE>set_field_type</CODE> effectively allow you to parameterize validation 2572types. Most of the complications in the validation-type interface have to 2573do with the handling of the additional arguments within custom validation 2574functions. 2575 2576<H3><A NAME="flinktypes">Union Types</A></H3> 2577 2578The simplest way to create a custom data type is to compose it from two 2579preexisting ones: 2580 2581<PRE> 2582FIELD *link_fieldtype(FIELDTYPE *type1, 2583 FIELDTYPE *type2); 2584</PRE> 2585 2586This function creates a field type that will accept any of the values 2587legal for either of its argument field types (which may be either 2588predefined or programmer-defined). 2589 2590If a <CODE>set_field_type()</CODE> call later requires arguments, the new 2591composite type expects all arguments for the first type, than all arguments 2592for the second. Order functions (see <A HREF="#forder">Order Requests</A>) 2593associated with the component types will work on the composite; what it does 2594is check the validation function for the first type, then for the second, to 2595figure what type the buffer contents should be treated as. 2596 2597<H3><A NAME="fnewtypes">New Field Types</A></H3> 2598 2599To create a field type from scratch, you need to specify one or both of the 2600following things: 2601 2602<UL> 2603<LI>A character-validation function, to check each character as it is entered. 2604<LI>A field-validation function to be applied on exit from the field. 2605</UL> 2606 2607Here's how you do that: 2608<PRE> 2609typedef int (*HOOK)(); /* pointer to function returning int */ 2610 2611FIELDTYPE *new_fieldtype(HOOK f_validate, /* field validator */ 2612 HOOK c_validate) /* character validator */ 2613 2614 2615int free_fieldtype(FIELDTYPE *ftype); /* type to free */ 2616</PRE> 2617 2618At least one of the arguments of <CODE>new_fieldtype()</CODE> must be 2619non-NULL. The forms driver will automatically call the new type's 2620validation functions at appropriate points in processing a field of 2621the new type. <P> 2622 2623The function <CODE>free_fieldtype()</CODE> deallocates the argument 2624fieldtype, freeing all storage associated with it. <P> 2625 2626Normally, a field validator is called when the user attempts to 2627leave the field. Its first argument is a field pointer, from which it 2628can get to field buffer 0 and test it. If the function returns TRUE, 2629the operation succeeds; if it returns FALSE, the edit cursor stays in 2630the field. <P> 2631 2632A character validator gets the character passed in as a first argument. 2633It too should return TRUE if the character is valid, FALSE otherwise. 2634 2635<H3><A NAME="fcheckargs">Validation Function Arguments</A></H3> 2636 2637Your field- and character- validation functions will be passed a 2638second argument as well. This second argument is the address of a 2639structure (which we'll call a <EM>pile</EM>) built from any of the 2640field-type-specific arguments passed to <CODE>set_field_type()</CODE>. If 2641no such arguments are defined for the field type, this pile pointer 2642argument will be NULL. <P> 2643 2644In order to arrange for such arguments to be passed to your validation 2645functions, you must associate a small set of storage-management functions 2646with the type. The forms driver will use these to synthesize a pile 2647from the trailing arguments of each <CODE>set_field_type()</CODE> argument, and 2648a pointer to the pile will be passed to the validation functions. <P> 2649 2650Here is how you make the association: 2651 2652<PRE> 2653typedef char *(*PTRHOOK)(); /* pointer to function returning (char *) */ 2654typedef void (*VOIDHOOK)(); /* pointer to function returning void */ 2655 2656int set_fieldtype_arg(FIELDTYPE *type, /* type to alter */ 2657 PTRHOOK make_str, /* make structure from args */ 2658 PTRHOOK copy_str, /* make copy of structure */ 2659 VOIDHOOK free_str); /* free structure storage */ 2660</PRE> 2661 2662Here is how the storage-management hooks are used: 2663 2664<DL> 2665<DT> <CODE>make_str</CODE> 2666<DD> This function is called by <CODE>set_field_type()</CODE>. It gets one 2667argument, a <CODE>va_list</CODE> of the type-specific arguments passed to 2668<CODE>set_field_type()</CODE>. It is expected to return a pile pointer to a data 2669structure that encapsulates those arguments. 2670<DT> <CODE>copy_str</CODE> 2671<DD> This function is called by form library functions that allocate new 2672field instances. It is expected to take a pile pointer, copy the pile 2673to allocated storage, and return the address of the pile copy. 2674<DT> <CODE>free_str</CODE> 2675<DD> This function is called by field- and type-deallocation routines in the 2676library. It takes a pile pointer argument, and is expected to free the 2677storage of that pile. 2678</DL> 2679 2680The <CODE>make_str</CODE> and <CODE>copy_str</CODE> functions may return NULL to 2681signal allocation failure. The library routines will that call them will 2682return error indication when this happens. Thus, your validation functions 2683should never see a NULL file pointer and need not check specially for it. 2684 2685<H3><A NAME="fcustorder">Order Functions For Custom Types</A></H3> 2686 2687Some custom field types are simply ordered in the same well-defined way 2688that <CODE>TYPE_ENUM</CODE> is. For such types, it is possible to define 2689successor and predecessor functions to support the <CODE>REQ_NEXT_CHOICE</CODE> 2690and <CODE>REQ_PREV_CHOICE</CODE> requests. Here's how: 2691 2692<PRE> 2693typedef int (*INTHOOK)(); /* pointer to function returning int */ 2694 2695int set_fieldtype_arg(FIELDTYPE *type, /* type to alter */ 2696 INTHOOK succ, /* get successor value */ 2697 INTHOOK pred); /* get predecessor value */ 2698</PRE> 2699 2700The successor and predecessor arguments will each be passed two arguments; 2701a field pointer, and a pile pointer (as for the validation functions). They 2702are expected to use the function <CODE>field_buffer()</CODE> to read the 2703current value, and <CODE>set_field_buffer()</CODE> on buffer 0 to set the next 2704or previous value. Either hook may return TRUE to indicate success (a 2705legal next or previous value was set) or FALSE to indicate failure. 2706 2707<H3><A NAME="fcustprobs">Avoiding Problems</A></H3> 2708 2709The interface for defining custom types is complicated and tricky. 2710Rather than attempting to create a custom type entirely from scratch, 2711you should start by studying the library source code for whichever of 2712the pre-defined types seems to be closest to what you want. <P> 2713 2714Use that code as a model, and evolve it towards what you really want. 2715You will avoid many problems and annoyances that way. The code 2716in the <CODE>ncurses</CODE> library has been specifically exempted from 2717the package copyright to support this. <P> 2718 2719If your custom type defines order functions, have do something intuitive 2720with a blank field. A useful convention is to make the successor of a 2721blank field the types minimum value, and its predecessor the maximum. 2722</BODY> 2723</HTML> 2724