ncurses-intro.html 119 KB

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