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