ncurses-intro.doc 110 KB

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