frm_driver.c 131 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863
  1. /****************************************************************************
  2. * Copyright (c) 1998 Free Software Foundation, Inc. *
  3. * *
  4. * Permission is hereby granted, free of charge, to any person obtaining a *
  5. * copy of this software and associated documentation files (the *
  6. * "Software"), to deal in the Software without restriction, including *
  7. * without limitation the rights to use, copy, modify, merge, publish, *
  8. * distribute, distribute with modifications, sublicense, and/or sell *
  9. * copies of the Software, and to permit persons to whom the Software is *
  10. * furnished to do so, subject to the following conditions: *
  11. * *
  12. * The above copyright notice and this permission notice shall be included *
  13. * in all copies or substantial portions of the Software. *
  14. * *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
  16. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
  18. * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
  19. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
  20. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
  21. * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
  22. * *
  23. * Except as contained in this notice, the name(s) of the above copyright *
  24. * holders shall not be used in advertising or otherwise to promote the *
  25. * sale, use or other dealings in this Software without prior written *
  26. * authorization. *
  27. ****************************************************************************/
  28. /****************************************************************************
  29. * Author: Juergen Pfeifer <[email protected]> 1995,1997 *
  30. ****************************************************************************/
  31. #if defined(__hpux)
  32. #define _XOPEN_SOURCE_EXTENDED
  33. #endif
  34. #include "form.priv.h"
  35. #if defined(__hpux)
  36. #undef _XOPEN_SOURCE_EXTENDED
  37. #endif
  38. /* AIX seems to define this */
  39. #undef lines
  40. #undef columns
  41. MODULE_ID("$Id$")
  42. /*----------------------------------------------------------------------------
  43. This is the core module of the form library. It contains the majority
  44. of the driver routines as well as the form_driver function.
  45. Essentially this module is nearly the whole library. This is because
  46. all the functions in this module depends on some others in the module,
  47. so it makes no sense to split them into separate files because they
  48. will always be linked together. The only acceptable concern is turnaround
  49. time for this module, but now we have all Pentiums or Riscs, so what!
  50. The driver routines are grouped into nine generic categories:
  51. a) Page Navigation ( all functions prefixed by PN_ )
  52. The current page of the form is left and some new page is
  53. entered.
  54. b) Inter-Field Navigation ( all functions prefixed by FN_ )
  55. The current field of the form is left and some new field is
  56. entered.
  57. c) Intra-Field Navigation ( all functions prefixed by IFN_ )
  58. The current position in the current field is changed.
  59. d) Vertical Scrolling ( all functions prefixed by VSC_ )
  60. Esseantially this is a specialization of Intra-Field navigation.
  61. It has to check for a multi-line field.
  62. e) Horizontal Scrolling ( all functions prefixed by HSC_ )
  63. Esseantially this is a specialization of Intra-Field navigation.
  64. It has to check for a single-line field.
  65. f) Field Editing ( all functions prefixed by FE_ )
  66. The content of the current field is changed
  67. g) Edit Mode requests ( all functions prefixed by EM_ )
  68. Switching between insert and overlay mode
  69. h) Field-Validation requests ( all functions prefixed by FV_ )
  70. Perform verifications of the field.
  71. i) Choice requests ( all functions prefixed by CR_ )
  72. Requests to enumerate possible field values
  73. --------------------------------------------------------------------------*/
  74. /*----------------------------------------------------------------------------
  75. Some remarks on the placements of assert() macros :
  76. I use them only on "strategic" places, i.e. top level entries where
  77. I want to make sure that things are set correctly. Throughout subordinate
  78. routines I omit them mostly.
  79. --------------------------------------------------------------------------*/
  80. /*
  81. Some options that may effect compatibility in behavior to SVr4 forms,
  82. but they are here to allow a more intuitive and user friendly behaviour of
  83. our form implementation. This doesn't affect the API, so we feel it is
  84. uncritical.
  85. The initial implementation tries to stay very close with the behaviour
  86. of the original SVr4 implementation, although in some areas it is quite
  87. clear that this isn't the most appropriate way. As far as possible this
  88. sources will allow you to build a forms lib that behaves quite similar
  89. to SVr4, but now and in the future we will give you better options.
  90. Perhaps at some time we will make this configurable at runtime.
  91. */
  92. /* Implement a more user-friendly previous/next word behaviour */
  93. #define FRIENDLY_PREV_NEXT_WORD (1)
  94. /* Fix the wrong behaviour for forms with all fields inactive */
  95. #define FIX_FORM_INACTIVE_BUG (1)
  96. /* Allow dynamic field growth also when navigating past the end */
  97. #define GROW_IF_NAVIGATE (1)
  98. /*----------------------------------------------------------------------------
  99. Forward references to some internally used static functions
  100. --------------------------------------------------------------------------*/
  101. static int Inter_Field_Navigation ( int (* const fct) (FORM *), FORM * form );
  102. static int FN_Next_Field (FORM * form);
  103. static int FN_Previous_Field (FORM * form);
  104. static int FE_New_Line(FORM *);
  105. static int FE_Delete_Previous(FORM *);
  106. /*----------------------------------------------------------------------------
  107. Macro Definitions.
  108. Some Remarks on that: I use the convention to use UPPERCASE for constants
  109. defined by Macros. If I provide a macro as a kind of inline routine to
  110. provide some logic, I use my Upper_Lower case style.
  111. --------------------------------------------------------------------------*/
  112. /* Calculate the position of a single row in a field buffer */
  113. #define Position_Of_Row_In_Buffer(field,row) ((row)*(field)->dcols)
  114. /* Calculate start address for the fields buffer# N */
  115. #define Address_Of_Nth_Buffer(field,N) \
  116. ((field)->buf + (N)*(1+Buffer_Length(field)))
  117. /* Calculate the start address of the row in the fields specified buffer# N */
  118. #define Address_Of_Row_In_Nth_Buffer(field,N,row) \
  119. (Address_Of_Nth_Buffer(field,N) + Position_Of_Row_In_Buffer(field,row))
  120. /* Calculate the start address of the row in the fields primary buffer */
  121. #define Address_Of_Row_In_Buffer(field,row) \
  122. Address_Of_Row_In_Nth_Buffer(field,0,row)
  123. /* Calculate the start address of the row in the forms current field
  124. buffer# N */
  125. #define Address_Of_Current_Row_In_Nth_Buffer(form,N) \
  126. Address_Of_Row_In_Nth_Buffer((form)->current,N,(form)->currow)
  127. /* Calculate the start address of the row in the forms current field
  128. primary buffer */
  129. #define Address_Of_Current_Row_In_Buffer(form) \
  130. Address_Of_Current_Row_In_Nth_Buffer(form,0)
  131. /* Calculate the address of the cursor in the forms current field
  132. primary buffer */
  133. #define Address_Of_Current_Position_In_Nth_Buffer(form,N) \
  134. (Address_Of_Current_Row_In_Nth_Buffer(form,N) + (form)->curcol)
  135. /* Calculate the address of the cursor in the forms current field
  136. buffer# N */
  137. #define Address_Of_Current_Position_In_Buffer(form) \
  138. Address_Of_Current_Position_In_Nth_Buffer(form,0)
  139. /* Logic to decide wether or not a field is actually a field with
  140. vertical or horizontal scrolling */
  141. #define Is_Scroll_Field(field) \
  142. (((field)->drows > (field)->rows) || \
  143. ((field)->dcols > (field)->cols))
  144. /* Logic to decide whether or not a field needs to have an individual window
  145. instead of a derived window because it contains invisible parts.
  146. This is true for non-public fields and for scrollable fields. */
  147. #define Has_Invisible_Parts(field) \
  148. (!((field)->opts & O_PUBLIC) || \
  149. Is_Scroll_Field(field))
  150. /* Logic to decide whether or not a field needs justification */
  151. #define Justification_Allowed(field) \
  152. (((field)->just != NO_JUSTIFICATION) && \
  153. (Single_Line_Field(field)) && \
  154. (((field)->dcols == (field)->cols) && \
  155. ((field)->opts & O_STATIC)) )
  156. /* Logic to determine whether or not a dynamic field may still grow */
  157. #define Growable(field) ((field)->status & _MAY_GROW)
  158. /* Macro to set the attributes for a fields window */
  159. #define Set_Field_Window_Attributes(field,win) \
  160. ( wbkgdset((win),(chtype)((field)->pad | (field)->back)), \
  161. wattrset((win),(field)->fore) )
  162. /* Logic to decide whether or not a field really appears on the form */
  163. #define Field_Really_Appears(field) \
  164. ((field->form) &&\
  165. (field->form->status & _POSTED) &&\
  166. (field->opts & O_VISIBLE) &&\
  167. (field->page == field->form->curpage))
  168. /* Logic to determine whether or not we are on the first position in the
  169. current field */
  170. #define First_Position_In_Current_Field(form) \
  171. (((form)->currow==0) && ((form)->curcol==0))
  172. #define Minimum(a,b) (((a)<=(b)) ? (a) : (b))
  173. #define Maximum(a,b) (((a)>=(b)) ? (a) : (b))
  174. /*---------------------------------------------------------------------------
  175. | Facility : libnform
  176. | Function : static char *Get_Start_Of_Data(char * buf, int blen)
  177. |
  178. | Description : Return pointer to first non-blank position in buffer.
  179. | If buffer is empty return pointer to buffer itself.
  180. |
  181. | Return Values : Pointer to first non-blank position in buffer
  182. +--------------------------------------------------------------------------*/
  183. INLINE static char *Get_Start_Of_Data(char * buf, int blen)
  184. {
  185. char *p = buf;
  186. char *end = &buf[blen];
  187. assert(buf && blen>=0);
  188. while( (p < end) && is_blank(*p) )
  189. p++;
  190. return( (p==end) ? buf : p );
  191. }
  192. /*---------------------------------------------------------------------------
  193. | Facility : libnform
  194. | Function : static char *After_End_Of_Data(char * buf, int blen)
  195. |
  196. | Description : Return pointer after last non-blank position in buffer.
  197. | If buffer is empty, return pointer to buffer itself.
  198. |
  199. | Return Values : Pointer to position after last non-blank position in
  200. | buffer.
  201. +--------------------------------------------------------------------------*/
  202. INLINE static char *After_End_Of_Data(char * buf,int blen)
  203. {
  204. char *p = &buf[blen];
  205. assert(buf && blen>=0);
  206. while( (p>buf) && is_blank(p[-1]) )
  207. p--;
  208. return( p );
  209. }
  210. /*---------------------------------------------------------------------------
  211. | Facility : libnform
  212. | Function : static char *Get_First_Whitespace_Character(
  213. | char * buf, int blen)
  214. |
  215. | Description : Position to the first whitespace character.
  216. |
  217. | Return Values : Pointer to first whitespace character in buffer.
  218. +--------------------------------------------------------------------------*/
  219. INLINE static char *Get_First_Whitespace_Character(char * buf, int blen)
  220. {
  221. char *p = buf;
  222. char *end = &p[blen];
  223. assert(buf && blen>=0);
  224. while( (p < end) && !is_blank(*p))
  225. p++;
  226. return( (p==end) ? buf : p );
  227. }
  228. /*---------------------------------------------------------------------------
  229. | Facility : libnform
  230. | Function : static char *After_Last_Whitespace_Character(
  231. | char * buf, int blen)
  232. |
  233. | Description : Get the position after the last whitespace character.
  234. |
  235. | Return Values : Pointer to position after last whitespace character in
  236. | buffer.
  237. +--------------------------------------------------------------------------*/
  238. INLINE static char *After_Last_Whitespace_Character(char * buf, int blen)
  239. {
  240. char *p = &buf[blen];
  241. assert(buf && blen>=0);
  242. while( (p>buf) && !is_blank(p[-1]) )
  243. p--;
  244. return( p );
  245. }
  246. /* Set this to 1 to use the div_t version. This is a good idea if your
  247. compiler has an intrinsic div() support. Unfortunately GNU-C has it
  248. not yet.
  249. N.B.: This only works if form->curcol follows immediately form->currow
  250. and both are of type int.
  251. */
  252. #define USE_DIV_T (0)
  253. /*---------------------------------------------------------------------------
  254. | Facility : libnform
  255. | Function : static void Adjust_Cursor_Position(
  256. | FORM * form, const char * pos)
  257. |
  258. | Description : Set current row and column of the form to values
  259. | corresponding to the buffer position.
  260. |
  261. | Return Values : -
  262. +--------------------------------------------------------------------------*/
  263. INLINE static void Adjust_Cursor_Position(FORM * form, const char * pos)
  264. {
  265. FIELD *field;
  266. int idx;
  267. field = form->current;
  268. assert( pos >= field->buf && field->dcols > 0);
  269. idx = (int)( pos - field->buf );
  270. #if USE_DIV_T
  271. *((div_t *)&(form->currow)) = div(idx,field->dcols);
  272. #else
  273. form->currow = idx / field->dcols;
  274. form->curcol = idx - field->cols * form->currow;
  275. #endif
  276. if ( field->drows < form->currow )
  277. form->currow = 0;
  278. }
  279. /*---------------------------------------------------------------------------
  280. | Facility : libnform
  281. | Function : static void Buffer_To_Window(
  282. | const FIELD * field,
  283. | WINDOW * win)
  284. |
  285. | Description : Copy the buffer to the window. If its a multiline
  286. | field, the buffer is split to the lines of the
  287. | window without any editing.
  288. |
  289. | Return Values : -
  290. +--------------------------------------------------------------------------*/
  291. static void Buffer_To_Window(const FIELD * field, WINDOW * win)
  292. {
  293. int width, height;
  294. int len;
  295. int row;
  296. char *pBuffer;
  297. assert(win && field);
  298. width = getmaxx(win);
  299. height = getmaxy(win);
  300. for(row=0, pBuffer=field->buf;
  301. row < height;
  302. row++, pBuffer += width )
  303. {
  304. if ((len = (int)( After_End_Of_Data( pBuffer, width ) - pBuffer )) > 0)
  305. {
  306. wmove( win, row, 0 );
  307. waddnstr( win, pBuffer, len );
  308. }
  309. }
  310. }
  311. /*---------------------------------------------------------------------------
  312. | Facility : libnform
  313. | Function : static void Window_To_Buffer(
  314. | WINDOW * win,
  315. | FIELD * field)
  316. |
  317. | Description : Copy the content of the window into the buffer.
  318. | The multiple lines of a window are simply
  319. | concatenated into the buffer. Pad characters in
  320. | the window will be replaced by blanks in the buffer.
  321. |
  322. | Return Values : -
  323. +--------------------------------------------------------------------------*/
  324. static void Window_To_Buffer(WINDOW * win, FIELD * field)
  325. {
  326. int pad;
  327. int len = 0;
  328. char *p;
  329. int row, height;
  330. assert(win && field && field->buf );
  331. pad = field->pad;
  332. p = field->buf;
  333. height = getmaxy(win);
  334. for(row=0; (row < height) && (row < field->drows); row++ )
  335. {
  336. wmove( win, row, 0 );
  337. len += winnstr( win, p+len, field->dcols );
  338. }
  339. p[len] = '\0';
  340. /* replace visual padding character by blanks in buffer */
  341. if (pad != C_BLANK)
  342. {
  343. int i;
  344. for(i=0; i<len; i++, p++)
  345. {
  346. if (*p==pad)
  347. *p = C_BLANK;
  348. }
  349. }
  350. }
  351. /*---------------------------------------------------------------------------
  352. | Facility : libnform
  353. | Function : static void Synchronize_Buffer(FORM * form)
  354. |
  355. | Description : If there was a change, copy the content of the
  356. | window into the buffer, so the buffer is synchronized
  357. | with the windows content. We have to indicate that the
  358. | buffer needs validation due to the change.
  359. |
  360. | Return Values : -
  361. +--------------------------------------------------------------------------*/
  362. INLINE static void Synchronize_Buffer(FORM * form)
  363. {
  364. if (form->status & _WINDOW_MODIFIED)
  365. {
  366. form->status &= ~_WINDOW_MODIFIED;
  367. form->status |= _FCHECK_REQUIRED;
  368. Window_To_Buffer(form->w,form->current);
  369. wmove(form->w,form->currow,form->curcol);
  370. }
  371. }
  372. /*---------------------------------------------------------------------------
  373. | Facility : libnform
  374. | Function : static bool Field_Grown( FIELD *field, int amount)
  375. |
  376. | Description : This function is called for growable dynamic fields
  377. | only. It has to increase the buffers and to allocate
  378. | a new window for this field.
  379. | This function has the side effect to set a new
  380. | field-buffer pointer, the dcols and drows values
  381. | as well as a new current Window for the field.
  382. |
  383. | Return Values : TRUE - field successfully increased
  384. | FALSE - there was some error
  385. +--------------------------------------------------------------------------*/
  386. static bool Field_Grown(FIELD * field, int amount)
  387. {
  388. bool result = FALSE;
  389. if (field && Growable(field))
  390. {
  391. bool single_line_field = Single_Line_Field(field);
  392. int old_buflen = Buffer_Length(field);
  393. int new_buflen;
  394. int old_dcols = field->dcols;
  395. int old_drows = field->drows;
  396. char *oldbuf = field->buf;
  397. char *newbuf;
  398. int growth;
  399. FORM *form = field->form;
  400. bool need_visual_update = ((form != (FORM *)0) &&
  401. (form->status & _POSTED) &&
  402. (form->current==field));
  403. if (need_visual_update)
  404. Synchronize_Buffer(form);
  405. if (single_line_field)
  406. {
  407. growth = field->cols * amount;
  408. if (field->maxgrow)
  409. growth = Minimum(field->maxgrow - field->dcols,growth);
  410. field->dcols += growth;
  411. if (field->dcols == field->maxgrow)
  412. field->status &= ~_MAY_GROW;
  413. }
  414. else
  415. {
  416. growth = (field->rows + field->nrow) * amount;
  417. if (field->maxgrow)
  418. growth = Minimum(field->maxgrow - field->drows,growth);
  419. field->drows += growth;
  420. if (field->drows == field->maxgrow)
  421. field->status &= ~_MAY_GROW;
  422. }
  423. /* drows, dcols changed, so we get really the new buffer length */
  424. new_buflen = Buffer_Length(field);
  425. newbuf=(char *)malloc((size_t)Total_Buffer_Size(field));
  426. if (!newbuf)
  427. { /* restore to previous state */
  428. field->dcols = old_dcols;
  429. field->drows = old_drows;
  430. if (( single_line_field && (field->dcols!=field->maxgrow)) ||
  431. (!single_line_field && (field->drows!=field->maxgrow)))
  432. field->status |= _MAY_GROW;
  433. return FALSE;
  434. }
  435. else
  436. { /* Copy all the buffers. This is the reason why we can't
  437. just use realloc().
  438. */
  439. int i;
  440. char *old_bp;
  441. char *new_bp;
  442. field->buf = newbuf;
  443. for(i=0;i<=field->nbuf;i++)
  444. {
  445. new_bp = Address_Of_Nth_Buffer(field,i);
  446. old_bp = oldbuf + i*(1+old_buflen);
  447. memcpy(new_bp,old_bp,(size_t)old_buflen);
  448. if (new_buflen > old_buflen)
  449. memset(new_bp + old_buflen,C_BLANK,
  450. (size_t)(new_buflen - old_buflen));
  451. *(new_bp + new_buflen) = '\0';
  452. }
  453. if (need_visual_update)
  454. {
  455. WINDOW *new_window = newpad(field->drows,field->dcols);
  456. if (!new_window)
  457. { /* restore old state */
  458. field->dcols = old_dcols;
  459. field->drows = old_drows;
  460. field->buf = oldbuf;
  461. if (( single_line_field &&
  462. (field->dcols!=field->maxgrow)) ||
  463. (!single_line_field &&
  464. (field->drows!=field->maxgrow)))
  465. field->status |= _MAY_GROW;
  466. free( newbuf );
  467. return FALSE;
  468. }
  469. assert(form!=(FORM *)0);
  470. delwin(form->w);
  471. form->w = new_window;
  472. Set_Field_Window_Attributes(field,form->w);
  473. werase(form->w);
  474. Buffer_To_Window(field,form->w);
  475. untouchwin(form->w);
  476. wmove(form->w,form->currow,form->curcol);
  477. }
  478. free(oldbuf);
  479. /* reflect changes in linked fields */
  480. if (field != field->link)
  481. {
  482. FIELD *linked_field;
  483. for(linked_field = field->link;
  484. linked_field!= field;
  485. linked_field = linked_field->link)
  486. {
  487. linked_field->buf = field->buf;
  488. linked_field->drows = field->drows;
  489. linked_field->dcols = field->dcols;
  490. }
  491. }
  492. result = TRUE;
  493. }
  494. }
  495. return(result);
  496. }
  497. /*---------------------------------------------------------------------------
  498. | Facility : libnform
  499. | Function : int _nc_Position_Form_Cursor(FORM * form)
  500. |
  501. | Description : Position the cursor in the window for the current
  502. | field to be in sync. with the currow and curcol
  503. | values.
  504. |
  505. | Return Values : E_OK - success
  506. | E_BAD_ARGUMENT - invalid form pointer
  507. | E_SYSTEM_ERROR - form has no current field or
  508. | field-window
  509. +--------------------------------------------------------------------------*/
  510. int
  511. _nc_Position_Form_Cursor(FORM * form)
  512. {
  513. FIELD *field;
  514. WINDOW *formwin;
  515. if (!form)
  516. return(E_BAD_ARGUMENT);
  517. if (!form->w || !form->current)
  518. return(E_SYSTEM_ERROR);
  519. field = form->current;
  520. formwin = Get_Form_Window(form);
  521. wmove( form->w, form->currow, form->curcol );
  522. if ( Has_Invisible_Parts(field) )
  523. {
  524. /* in this case fieldwin isn't derived from formwin, so we have
  525. to move the cursor in formwin by hand... */
  526. wmove(formwin,
  527. field->frow + form->currow - form->toprow,
  528. field->fcol + form->curcol - form->begincol);
  529. wcursyncup(formwin);
  530. }
  531. else
  532. wcursyncup(form->w);
  533. return(E_OK);
  534. }
  535. /*---------------------------------------------------------------------------
  536. | Facility : libnform
  537. | Function : int _nc_Refresh_Current_Field(FORM * form)
  538. |
  539. | Description : Propagate the changes in the fields window to the
  540. | window of the form.
  541. |
  542. | Return Values : E_OK - on success
  543. | E_BAD_ARGUMENT - invalid form pointer
  544. | E_SYSTEM_ERROR - general error
  545. +--------------------------------------------------------------------------*/
  546. int
  547. _nc_Refresh_Current_Field(FORM * form)
  548. {
  549. WINDOW *formwin;
  550. FIELD *field;
  551. if (!form)
  552. RETURN(E_BAD_ARGUMENT);
  553. if (!form->w || !form->current)
  554. RETURN(E_SYSTEM_ERROR);
  555. field = form->current;
  556. formwin = Get_Form_Window(form);
  557. if (field->opts & O_PUBLIC)
  558. {
  559. if (Is_Scroll_Field(field))
  560. {
  561. /* Again, in this case the fieldwin isn't derived from formwin,
  562. so we have to perform a copy operation. */
  563. if (Single_Line_Field(field))
  564. { /* horizontal scrolling */
  565. if (form->curcol < form->begincol)
  566. form->begincol = form->curcol;
  567. else
  568. {
  569. if (form->curcol >= (form->begincol + field->cols))
  570. form->begincol = form->curcol - field->cols + 1;
  571. }
  572. copywin(form->w,
  573. formwin,
  574. 0,
  575. form->begincol,
  576. field->frow,
  577. field->fcol,
  578. field->frow,
  579. field->cols + field->fcol - 1,
  580. 0);
  581. }
  582. else
  583. { /* A multiline, i.e. vertical scrolling field */
  584. int row_after_bottom,first_modified_row,first_unmodified_row;
  585. if (field->drows > field->rows)
  586. {
  587. row_after_bottom = form->toprow + field->rows;
  588. if (form->currow < form->toprow)
  589. {
  590. form->toprow = form->currow;
  591. field->status |= _NEWTOP;
  592. }
  593. if (form->currow >= row_after_bottom)
  594. {
  595. form->toprow = form->currow - field->rows + 1;
  596. field->status |= _NEWTOP;
  597. }
  598. if (field->status & _NEWTOP)
  599. { /* means we have to copy whole range */
  600. first_modified_row = form->toprow;
  601. first_unmodified_row = first_modified_row + field->rows;
  602. field->status &= ~_NEWTOP;
  603. }
  604. else
  605. { /* we try to optimize : finding the range of touched
  606. lines */
  607. first_modified_row = form->toprow;
  608. while(first_modified_row < row_after_bottom)
  609. {
  610. if (is_linetouched(form->w,first_modified_row))
  611. break;
  612. first_modified_row++;
  613. }
  614. first_unmodified_row = first_modified_row;
  615. while(first_unmodified_row < row_after_bottom)
  616. {
  617. if (!is_linetouched(form->w,first_unmodified_row))
  618. break;
  619. first_unmodified_row++;
  620. }
  621. }
  622. }
  623. else
  624. {
  625. first_modified_row = form->toprow;
  626. first_unmodified_row = first_modified_row + field->rows;
  627. }
  628. if (first_unmodified_row != first_modified_row)
  629. copywin(form->w,
  630. formwin,
  631. first_modified_row,
  632. 0,
  633. field->frow + first_modified_row - form->toprow,
  634. field->fcol,
  635. field->frow + first_unmodified_row - form->toprow - 1,
  636. field->cols + field->fcol - 1,
  637. 0);
  638. }
  639. wsyncup(formwin);
  640. }
  641. else
  642. { /* if the field-window is simply a derived window, i.e. contains
  643. no invisible parts, the whole thing is trivial
  644. */
  645. wsyncup(form->w);
  646. }
  647. }
  648. untouchwin(form->w);
  649. return _nc_Position_Form_Cursor(form);
  650. }
  651. /*---------------------------------------------------------------------------
  652. | Facility : libnform
  653. | Function : static void Perform_Justification(
  654. | FIELD * field,
  655. | WINDOW * win)
  656. |
  657. | Description : Output field with requested justification
  658. |
  659. | Return Values : -
  660. +--------------------------------------------------------------------------*/
  661. static void Perform_Justification(FIELD * field, WINDOW * win)
  662. {
  663. char *bp;
  664. int len;
  665. int col = 0;
  666. bp = Get_Start_Of_Data(field->buf,Buffer_Length(field));
  667. len = (int)(After_End_Of_Data(field->buf,Buffer_Length(field)) - bp);
  668. if (len>0)
  669. {
  670. assert(win && (field->drows == 1) && (field->dcols == field->cols));
  671. switch(field->just)
  672. {
  673. case JUSTIFY_LEFT:
  674. break;
  675. case JUSTIFY_CENTER:
  676. col = (field->cols - len)/2;
  677. break;
  678. case JUSTIFY_RIGHT:
  679. col = field->cols - len;
  680. break;
  681. default:
  682. break;
  683. }
  684. wmove(win,0,col);
  685. waddnstr(win,bp,len);
  686. }
  687. }
  688. /*---------------------------------------------------------------------------
  689. | Facility : libnform
  690. | Function : static void Undo_Justification(
  691. | FIELD * field,
  692. | WINDOW * win)
  693. |
  694. | Description : Display field without any justification, i.e.
  695. | left justified
  696. |
  697. | Return Values : -
  698. +--------------------------------------------------------------------------*/
  699. static void Undo_Justification(FIELD * field, WINDOW * win)
  700. {
  701. char *bp;
  702. int len;
  703. bp = Get_Start_Of_Data(field->buf,Buffer_Length(field));
  704. len = (int)(After_End_Of_Data(field->buf,Buffer_Length(field))-bp);
  705. if (len>0)
  706. {
  707. assert(win != 0);
  708. wmove(win,0,0);
  709. waddnstr(win,bp,len);
  710. }
  711. }
  712. /*---------------------------------------------------------------------------
  713. | Facility : libnform
  714. | Function : static bool Check_Char(
  715. | FIELDTYPE * typ,
  716. | int ch,
  717. | TypeArgument *argp)
  718. |
  719. | Description : Perform a single character check for character ch
  720. | according to the fieldtype instance.
  721. |
  722. | Return Values : TRUE - Character is valid
  723. | FALSE - Character is invalid
  724. +--------------------------------------------------------------------------*/
  725. static bool Check_Char(FIELDTYPE * typ, int ch, TypeArgument *argp)
  726. {
  727. if (typ)
  728. {
  729. if (typ->status & _LINKED_TYPE)
  730. {
  731. assert(argp != 0);
  732. return(
  733. Check_Char(typ->left ,ch,argp->left ) ||
  734. Check_Char(typ->right,ch,argp->right) );
  735. }
  736. else
  737. {
  738. if (typ->ccheck)
  739. return typ->ccheck(ch,(void *)argp);
  740. }
  741. }
  742. return (isprint((unsigned char)ch) ? TRUE : FALSE);
  743. }
  744. /*---------------------------------------------------------------------------
  745. | Facility : libnform
  746. | Function : static int Display_Or_Erase_Field(
  747. | FIELD * field,
  748. | bool bEraseFlag)
  749. |
  750. | Description : Create a subwindow for the field and display the
  751. | buffer contents (apply justification if required)
  752. | or simply erase the field.
  753. |
  754. | Return Values : E_OK - on success
  755. | E_SYSTEM_ERROR - some error (typical no memory)
  756. +--------------------------------------------------------------------------*/
  757. static int Display_Or_Erase_Field(FIELD * field, bool bEraseFlag)
  758. {
  759. WINDOW *win;
  760. WINDOW *fwin;
  761. if (!field)
  762. return E_SYSTEM_ERROR;
  763. fwin = Get_Form_Window(field->form);
  764. win = derwin(fwin,
  765. field->rows,field->cols,field->frow,field->fcol);
  766. if (!win)
  767. return E_SYSTEM_ERROR;
  768. else
  769. {
  770. if (field->opts & O_VISIBLE)
  771. Set_Field_Window_Attributes(field,win);
  772. else
  773. wattrset(win,getattrs(fwin));
  774. werase(win);
  775. }
  776. if (!bEraseFlag)
  777. {
  778. if (field->opts & O_PUBLIC)
  779. {
  780. if (Justification_Allowed(field))
  781. Perform_Justification(field,win);
  782. else
  783. Buffer_To_Window(field,win);
  784. }
  785. field->status &= ~_NEWTOP;
  786. }
  787. wsyncup(win);
  788. delwin(win);
  789. return E_OK;
  790. }
  791. /* Macros to preset the bEraseFlag */
  792. #define Display_Field(field) Display_Or_Erase_Field(field,FALSE)
  793. #define Erase_Field(field) Display_Or_Erase_Field(field,TRUE)
  794. /*---------------------------------------------------------------------------
  795. | Facility : libnform
  796. | Function : static int Synchronize_Field(FIELD * field)
  797. |
  798. | Description : Synchronize the windows content with the value in
  799. | the buffer.
  800. |
  801. | Return Values : E_OK - success
  802. | E_BAD_ARGUMENT - invalid field pointer
  803. | E_SYSTEM_ERROR - some severe basic error
  804. +--------------------------------------------------------------------------*/
  805. static int Synchronize_Field(FIELD * field)
  806. {
  807. FORM *form;
  808. int res = E_OK;
  809. if (!field)
  810. return(E_BAD_ARGUMENT);
  811. if (((form=field->form) != (FORM *)0)
  812. && Field_Really_Appears(field))
  813. {
  814. if (field == form->current)
  815. {
  816. form->currow = form->curcol = form->toprow = form->begincol = 0;
  817. werase(form->w);
  818. if ( (field->opts & O_PUBLIC) && Justification_Allowed(field) )
  819. Undo_Justification( field, form->w );
  820. else
  821. Buffer_To_Window( field, form->w );
  822. field->status |= _NEWTOP;
  823. res = _nc_Refresh_Current_Field( form );
  824. }
  825. else
  826. res = Display_Field( field );
  827. }
  828. field->status |= _CHANGED;
  829. return(res);
  830. }
  831. /*---------------------------------------------------------------------------
  832. | Facility : libnform
  833. | Function : static int Synchronize_Linked_Fields(FIELD * field)
  834. |
  835. | Description : Propagate the Synchronize_Field function to all linked
  836. | fields. The first error that occurs in the sequence
  837. | of updates is the returnvalue.
  838. |
  839. | Return Values : E_OK - success
  840. | E_BAD_ARGUMENT - invalid field pointer
  841. | E_SYSTEM_ERROR - some severe basic error
  842. +--------------------------------------------------------------------------*/
  843. static int Synchronize_Linked_Fields(FIELD * field)
  844. {
  845. FIELD *linked_field;
  846. int res = E_OK;
  847. int syncres;
  848. if (!field)
  849. return(E_BAD_ARGUMENT);
  850. if (!field->link)
  851. return(E_SYSTEM_ERROR);
  852. for(linked_field = field->link;
  853. linked_field!= field;
  854. linked_field = linked_field->link )
  855. {
  856. if (((syncres=Synchronize_Field(linked_field)) != E_OK) &&
  857. (res==E_OK))
  858. res = syncres;
  859. }
  860. return(res);
  861. }
  862. /*---------------------------------------------------------------------------
  863. | Facility : libnform
  864. | Function : int _nc_Synchronize_Attributes(FIELD * field)
  865. |
  866. | Description : If a fields visual attributes have changed, this
  867. | routine is called to propagate those changes to the
  868. | screen.
  869. |
  870. | Return Values : E_OK - success
  871. | E_BAD_ARGUMENT - invalid field pointer
  872. | E_SYSTEM_ERROR - some severe basic error
  873. +--------------------------------------------------------------------------*/
  874. int _nc_Synchronize_Attributes(FIELD * field)
  875. {
  876. FORM *form;
  877. int res = E_OK;
  878. WINDOW *formwin;
  879. if (!field)
  880. return(E_BAD_ARGUMENT);
  881. if (((form=field->form) != (FORM *)0)
  882. && Field_Really_Appears(field))
  883. {
  884. if (form->current==field)
  885. {
  886. Synchronize_Buffer(form);
  887. Set_Field_Window_Attributes(field,form->w);
  888. werase(form->w);
  889. if (field->opts & O_PUBLIC)
  890. {
  891. if (Justification_Allowed(field))
  892. Undo_Justification(field,form->w);
  893. else
  894. Buffer_To_Window(field,form->w);
  895. }
  896. else
  897. {
  898. formwin = Get_Form_Window(form);
  899. copywin(form->w,formwin,
  900. 0,0,
  901. field->frow,field->fcol,
  902. field->rows-1,field->cols-1,0);
  903. wsyncup(formwin);
  904. Buffer_To_Window(field,form->w);
  905. field->status |= _NEWTOP; /* fake refresh to paint all */
  906. _nc_Refresh_Current_Field(form);
  907. }
  908. }
  909. else
  910. {
  911. res = Display_Field(field);
  912. }
  913. }
  914. return(res);
  915. }
  916. /*---------------------------------------------------------------------------
  917. | Facility : libnform
  918. | Function : int _nc_Synchronize_Options(FIELD * field,
  919. | Field_Options newopts)
  920. |
  921. | Description : If a fields options have changed, this routine is
  922. | called to propagate these changes to the screen and
  923. | to really change the behaviour of the field.
  924. |
  925. | Return Values : E_OK - success
  926. | E_BAD_ARGUMENT - invalid field pointer
  927. | E_SYSTEM_ERROR - some severe basic error
  928. +--------------------------------------------------------------------------*/
  929. int
  930. _nc_Synchronize_Options(FIELD *field, Field_Options newopts)
  931. {
  932. Field_Options oldopts;
  933. Field_Options changed_opts;
  934. FORM *form;
  935. int res = E_OK;
  936. if (!field)
  937. return(E_BAD_ARGUMENT);
  938. oldopts = field->opts;
  939. changed_opts = oldopts ^ newopts;
  940. field->opts = newopts;
  941. form = field->form;
  942. if (form)
  943. {
  944. if (form->current == field)
  945. {
  946. field->opts = oldopts;
  947. return(E_CURRENT);
  948. }
  949. if (form->status & _POSTED)
  950. {
  951. if ((form->curpage == field->page))
  952. {
  953. if (changed_opts & O_VISIBLE)
  954. {
  955. if (newopts & O_VISIBLE)
  956. res = Display_Field(field);
  957. else
  958. res = Erase_Field(field);
  959. }
  960. else
  961. {
  962. if ((changed_opts & O_PUBLIC) &&
  963. (newopts & O_VISIBLE))
  964. res = Display_Field(field);
  965. }
  966. }
  967. }
  968. }
  969. if (changed_opts & O_STATIC)
  970. {
  971. bool single_line_field = Single_Line_Field(field);
  972. int res2 = E_OK;
  973. if (newopts & O_STATIC)
  974. { /* the field becomes now static */
  975. field->status &= ~_MAY_GROW;
  976. /* if actually we have no hidden columns, justification may
  977. occur again */
  978. if (single_line_field &&
  979. (field->cols == field->dcols) &&
  980. (field->just != NO_JUSTIFICATION) &&
  981. Field_Really_Appears(field))
  982. {
  983. res2 = Display_Field(field);
  984. }
  985. }
  986. else
  987. { /* field is no longer static */
  988. if ((field->maxgrow==0) ||
  989. ( single_line_field && (field->dcols < field->maxgrow)) ||
  990. (!single_line_field && (field->drows < field->maxgrow)))
  991. {
  992. field->status |= _MAY_GROW;
  993. /* a field with justification now changes its behaviour,
  994. so we must redisplay it */
  995. if (single_line_field &&
  996. (field->just != NO_JUSTIFICATION) &&
  997. Field_Really_Appears(field))
  998. {
  999. res2 = Display_Field(field);
  1000. }
  1001. }
  1002. }
  1003. if (res2 != E_OK)
  1004. res = res2;
  1005. }
  1006. return(res);
  1007. }
  1008. /*---------------------------------------------------------------------------
  1009. | Facility : libnform
  1010. | Function : int _nc_Set_Current_Field(FORM * form,
  1011. | FIELD * newfield)
  1012. |
  1013. | Description : Make the newfield the new current field.
  1014. |
  1015. | Return Values : E_OK - success
  1016. | E_BAD_ARGUMENT - invalid form or field pointer
  1017. | E_SYSTEM_ERROR - some severe basic error
  1018. +--------------------------------------------------------------------------*/
  1019. int
  1020. _nc_Set_Current_Field(FORM *form, FIELD *newfield)
  1021. {
  1022. FIELD *field;
  1023. WINDOW *new_window;
  1024. if (!form || !newfield || !form->current || (newfield->form!=form))
  1025. return(E_BAD_ARGUMENT);
  1026. if ( (form->status & _IN_DRIVER) )
  1027. return(E_BAD_STATE);
  1028. if (!(form->field))
  1029. return(E_NOT_CONNECTED);
  1030. field = form->current;
  1031. if ((field!=newfield) ||
  1032. !(form->status & _POSTED))
  1033. {
  1034. if ((form->w) &&
  1035. (field->opts & O_VISIBLE) &&
  1036. (field->form->curpage == field->page))
  1037. {
  1038. _nc_Refresh_Current_Field(form);
  1039. if (field->opts & O_PUBLIC)
  1040. {
  1041. if (field->drows > field->rows)
  1042. {
  1043. if (form->toprow==0)
  1044. field->status &= ~_NEWTOP;
  1045. else
  1046. field->status |= _NEWTOP;
  1047. }
  1048. else
  1049. {
  1050. if (Justification_Allowed(field))
  1051. {
  1052. Window_To_Buffer(form->w,field);
  1053. werase(form->w);
  1054. Perform_Justification(field,form->w);
  1055. wsyncup(form->w);
  1056. }
  1057. }
  1058. }
  1059. delwin(form->w);
  1060. }
  1061. field = newfield;
  1062. if (Has_Invisible_Parts(field))
  1063. new_window = newpad(field->drows,field->dcols);
  1064. else
  1065. new_window = derwin(Get_Form_Window(form),
  1066. field->rows,field->cols,field->frow,field->fcol);
  1067. if (!new_window)
  1068. return(E_SYSTEM_ERROR);
  1069. form->current = field;
  1070. form->w = new_window;
  1071. form->status &= ~_WINDOW_MODIFIED;
  1072. Set_Field_Window_Attributes(field,form->w);
  1073. if (Has_Invisible_Parts(field))
  1074. {
  1075. werase(form->w);
  1076. Buffer_To_Window(field,form->w);
  1077. }
  1078. else
  1079. {
  1080. if (Justification_Allowed(field))
  1081. {
  1082. werase(form->w);
  1083. Undo_Justification(field,form->w);
  1084. wsyncup(form->w);
  1085. }
  1086. }
  1087. untouchwin(form->w);
  1088. }
  1089. form->currow = form->curcol = form->toprow = form->begincol = 0;
  1090. return(E_OK);
  1091. }
  1092. /*----------------------------------------------------------------------------
  1093. Intra-Field Navigation routines
  1094. --------------------------------------------------------------------------*/
  1095. /*---------------------------------------------------------------------------
  1096. | Facility : libnform
  1097. | Function : static int IFN_Next_Character(FORM * form)
  1098. |
  1099. | Description : Move to the next character in the field. In a multiline
  1100. | field this wraps at the end of the line.
  1101. |
  1102. | Return Values : E_OK - success
  1103. | E_REQUEST_DENIED - at the rightmost position
  1104. +--------------------------------------------------------------------------*/
  1105. static int IFN_Next_Character(FORM * form)
  1106. {
  1107. FIELD *field = form->current;
  1108. if ((++(form->curcol))==field->dcols)
  1109. {
  1110. if ((++(form->currow))==field->drows)
  1111. {
  1112. #if GROW_IF_NAVIGATE
  1113. if (!Single_Line_Field(field) && Field_Grown(field,1)) {
  1114. form->curcol = 0;
  1115. return(E_OK);
  1116. }
  1117. #endif
  1118. form->currow--;
  1119. #if GROW_IF_NAVIGATE
  1120. if (Single_Line_Field(field) && Field_Grown(field,1))
  1121. return(E_OK);
  1122. #endif
  1123. form->curcol--;
  1124. return(E_REQUEST_DENIED);
  1125. }
  1126. form->curcol = 0;
  1127. }
  1128. return(E_OK);
  1129. }
  1130. /*---------------------------------------------------------------------------
  1131. | Facility : libnform
  1132. | Function : static int IFN_Previous_Character(FORM * form)
  1133. |
  1134. | Description : Move to the previous character in the field. In a
  1135. | multiline field this wraps and the beginning of the
  1136. | line.
  1137. |
  1138. | Return Values : E_OK - success
  1139. | E_REQUEST_DENIED - at the leftmost position
  1140. +--------------------------------------------------------------------------*/
  1141. static int IFN_Previous_Character(FORM * form)
  1142. {
  1143. if ((--(form->curcol))<0)
  1144. {
  1145. if ((--(form->currow))<0)
  1146. {
  1147. form->currow++;
  1148. form->curcol++;
  1149. return(E_REQUEST_DENIED);
  1150. }
  1151. form->curcol = form->current->dcols - 1;
  1152. }
  1153. return(E_OK);
  1154. }
  1155. /*---------------------------------------------------------------------------
  1156. | Facility : libnform
  1157. | Function : static int IFN_Next_Line(FORM * form)
  1158. |
  1159. | Description : Move to the beginning of the next line in the field
  1160. |
  1161. | Return Values : E_OK - success
  1162. | E_REQUEST_DENIED - at the last line
  1163. +--------------------------------------------------------------------------*/
  1164. static int IFN_Next_Line(FORM * form)
  1165. {
  1166. FIELD *field = form->current;
  1167. if ((++(form->currow))==field->drows)
  1168. {
  1169. #if GROW_IF_NAVIGATE
  1170. if (!Single_Line_Field(field) && Field_Grown(field,1))
  1171. return(E_OK);
  1172. #endif
  1173. form->currow--;
  1174. return(E_REQUEST_DENIED);
  1175. }
  1176. form->curcol = 0;
  1177. return(E_OK);
  1178. }
  1179. /*---------------------------------------------------------------------------
  1180. | Facility : libnform
  1181. | Function : static int IFN_Previous_Line(FORM * form)
  1182. |
  1183. | Description : Move to the beginning of the previous line in the field
  1184. |
  1185. | Return Values : E_OK - success
  1186. | E_REQUEST_DENIED - at the first line
  1187. +--------------------------------------------------------------------------*/
  1188. static int IFN_Previous_Line(FORM * form)
  1189. {
  1190. if ( (--(form->currow)) < 0 )
  1191. {
  1192. form->currow++;
  1193. return(E_REQUEST_DENIED);
  1194. }
  1195. form->curcol = 0;
  1196. return(E_OK);
  1197. }
  1198. /*---------------------------------------------------------------------------
  1199. | Facility : libnform
  1200. | Function : static int IFN_Next_Word(FORM * form)
  1201. |
  1202. | Description : Move to the beginning of the next word in the field.
  1203. |
  1204. | Return Values : E_OK - success
  1205. | E_REQUEST_DENIED - there is no next word
  1206. +--------------------------------------------------------------------------*/
  1207. static int IFN_Next_Word(FORM * form)
  1208. {
  1209. FIELD *field = form->current;
  1210. char *bp = Address_Of_Current_Position_In_Buffer(form);
  1211. char *s;
  1212. char *t;
  1213. /* We really need access to the data, so we have to synchronize */
  1214. Synchronize_Buffer(form);
  1215. /* Go to the first whitespace after the current position (including
  1216. current position). This is then the startpoint to look for the
  1217. next non-blank data */
  1218. s = Get_First_Whitespace_Character(bp,Buffer_Length(field) -
  1219. (int)(bp - field->buf));
  1220. /* Find the start of the next word */
  1221. t = Get_Start_Of_Data(s,Buffer_Length(field) -
  1222. (int)(s - field->buf));
  1223. #if !FRIENDLY_PREV_NEXT_WORD
  1224. if (s==t)
  1225. return(E_REQUEST_DENIED);
  1226. else
  1227. #endif
  1228. {
  1229. Adjust_Cursor_Position(form,t);
  1230. return(E_OK);
  1231. }
  1232. }
  1233. /*---------------------------------------------------------------------------
  1234. | Facility : libnform
  1235. | Function : static int IFN_Previous_Word(FORM * form)
  1236. |
  1237. | Description : Move to the beginning of the previous word in the field.
  1238. |
  1239. | Return Values : E_OK - success
  1240. | E_REQUEST_DENIED - there is no previous word
  1241. +--------------------------------------------------------------------------*/
  1242. static int IFN_Previous_Word(FORM * form)
  1243. {
  1244. FIELD *field = form->current;
  1245. char *bp = Address_Of_Current_Position_In_Buffer(form);
  1246. char *s;
  1247. char *t;
  1248. bool again = FALSE;
  1249. /* We really need access to the data, so we have to synchronize */
  1250. Synchronize_Buffer(form);
  1251. s = After_End_Of_Data(field->buf,(int)(bp-field->buf));
  1252. /* s points now right after the last non-blank in the buffer before bp.
  1253. If bp was in a word, s equals bp. In this case we must find the last
  1254. whitespace in the buffer before bp and repeat the game to really find
  1255. the previous word! */
  1256. if (s==bp)
  1257. again = TRUE;
  1258. /* And next call now goes backward to look for the last whitespace
  1259. before that, pointing right after this, so it points to the begin
  1260. of the previous word.
  1261. */
  1262. t = After_Last_Whitespace_Character(field->buf,(int)(s - field->buf));
  1263. #if !FRIENDLY_PREV_NEXT_WORD
  1264. if (s==t)
  1265. return(E_REQUEST_DENIED);
  1266. #endif
  1267. if (again)
  1268. { /* and do it again, replacing bp by t */
  1269. s = After_End_Of_Data(field->buf,(int)(t - field->buf));
  1270. t = After_Last_Whitespace_Character(field->buf,(int)(s - field->buf));
  1271. #if !FRIENDLY_PREV_NEXT_WORD
  1272. if (s==t)
  1273. return(E_REQUEST_DENIED);
  1274. #endif
  1275. }
  1276. Adjust_Cursor_Position(form,t);
  1277. return(E_OK);
  1278. }
  1279. /*---------------------------------------------------------------------------
  1280. | Facility : libnform
  1281. | Function : static int IFN_Beginning_Of_Field(FORM * form)
  1282. |
  1283. | Description : Place the cursor at the first non-pad character in
  1284. | the field.
  1285. |
  1286. | Return Values : E_OK - success
  1287. +--------------------------------------------------------------------------*/
  1288. static int IFN_Beginning_Of_Field(FORM * form)
  1289. {
  1290. FIELD *field = form->current;
  1291. Synchronize_Buffer(form);
  1292. Adjust_Cursor_Position(form,
  1293. Get_Start_Of_Data(field->buf,Buffer_Length(field)));
  1294. return(E_OK);
  1295. }
  1296. /*---------------------------------------------------------------------------
  1297. | Facility : libnform
  1298. | Function : static int IFN_End_Of_Field(FORM * form)
  1299. |
  1300. | Description : Place the cursor after the last non-pad character in
  1301. | the field. If the field occupies the last position in
  1302. | the buffer, the cursos is positioned on the last
  1303. | character.
  1304. |
  1305. | Return Values : E_OK - success
  1306. +--------------------------------------------------------------------------*/
  1307. static int IFN_End_Of_Field(FORM * form)
  1308. {
  1309. FIELD *field = form->current;
  1310. char *pos;
  1311. Synchronize_Buffer(form);
  1312. pos = After_End_Of_Data(field->buf,Buffer_Length(field));
  1313. if (pos==(field->buf + Buffer_Length(field)))
  1314. pos--;
  1315. Adjust_Cursor_Position(form,pos);
  1316. return(E_OK);
  1317. }
  1318. /*---------------------------------------------------------------------------
  1319. | Facility : libnform
  1320. | Function : static int IFN_Beginning_Of_Line(FORM * form)
  1321. |
  1322. | Description : Place the cursor on the first non-pad character in
  1323. | the current line of the field.
  1324. |
  1325. | Return Values : E_OK - success
  1326. +--------------------------------------------------------------------------*/
  1327. static int IFN_Beginning_Of_Line(FORM * form)
  1328. {
  1329. FIELD *field = form->current;
  1330. Synchronize_Buffer(form);
  1331. Adjust_Cursor_Position(form,
  1332. Get_Start_Of_Data(Address_Of_Current_Row_In_Buffer(form),
  1333. field->dcols));
  1334. return(E_OK);
  1335. }
  1336. /*---------------------------------------------------------------------------
  1337. | Facility : libnform
  1338. | Function : static int IFN_End_Of_Line(FORM * form)
  1339. |
  1340. | Description : Place the cursor after the last non-pad character in the
  1341. | current line of the field. If the field occupies the
  1342. | last column in the line, the cursor is positioned on the
  1343. | last character of the line.
  1344. |
  1345. | Return Values : E_OK - success
  1346. +--------------------------------------------------------------------------*/
  1347. static int IFN_End_Of_Line(FORM * form)
  1348. {
  1349. FIELD *field = form->current;
  1350. char *pos;
  1351. char *bp;
  1352. Synchronize_Buffer(form);
  1353. bp = Address_Of_Current_Row_In_Buffer(form);
  1354. pos = After_End_Of_Data(bp,field->dcols);
  1355. if (pos == (bp + field->dcols))
  1356. pos--;
  1357. Adjust_Cursor_Position(form,pos);
  1358. return(E_OK);
  1359. }
  1360. /*---------------------------------------------------------------------------
  1361. | Facility : libnform
  1362. | Function : static int IFN_Left_Character(FORM * form)
  1363. |
  1364. | Description : Move one character to the left in the current line.
  1365. | This doesn't cycle.
  1366. |
  1367. | Return Values : E_OK - success
  1368. | E_REQUEST_DENIED - already in first column
  1369. +--------------------------------------------------------------------------*/
  1370. static int IFN_Left_Character(FORM * form)
  1371. {
  1372. if ( (--(form->curcol)) < 0 )
  1373. {
  1374. form->curcol++;
  1375. return(E_REQUEST_DENIED);
  1376. }
  1377. return(E_OK);
  1378. }
  1379. /*---------------------------------------------------------------------------
  1380. | Facility : libnform
  1381. | Function : static int IFN_Right_Character(FORM * form)
  1382. |
  1383. | Description : Move one character to the right in the current line.
  1384. | This doesn't cycle.
  1385. |
  1386. | Return Values : E_OK - success
  1387. | E_REQUEST_DENIED - already in last column
  1388. +--------------------------------------------------------------------------*/
  1389. static int IFN_Right_Character(FORM * form)
  1390. {
  1391. if ( (++(form->curcol)) == form->current->dcols )
  1392. {
  1393. #if GROW_IF_NAVIGATE
  1394. FIELD *field = form->current;
  1395. if (Single_Line_Field(field) && Field_Grown(field,1))
  1396. return(E_OK);
  1397. #endif
  1398. --(form->curcol);
  1399. return(E_REQUEST_DENIED);
  1400. }
  1401. return(E_OK);
  1402. }
  1403. /*---------------------------------------------------------------------------
  1404. | Facility : libnform
  1405. | Function : static int IFN_Up_Character(FORM * form)
  1406. |
  1407. | Description : Move one line up. This doesn't cycle through the lines
  1408. | of the field.
  1409. |
  1410. | Return Values : E_OK - success
  1411. | E_REQUEST_DENIED - already in last column
  1412. +--------------------------------------------------------------------------*/
  1413. static int IFN_Up_Character(FORM * form)
  1414. {
  1415. if ( (--(form->currow)) < 0 )
  1416. {
  1417. form->currow++;
  1418. return(E_REQUEST_DENIED);
  1419. }
  1420. return(E_OK);
  1421. }
  1422. /*---------------------------------------------------------------------------
  1423. | Facility : libnform
  1424. | Function : static int IFN_Down_Character(FORM * form)
  1425. |
  1426. | Description : Move one line down. This doesn't cycle through the
  1427. | lines of the field.
  1428. |
  1429. | Return Values : E_OK - success
  1430. | E_REQUEST_DENIED - already in last column
  1431. +--------------------------------------------------------------------------*/
  1432. static int IFN_Down_Character(FORM * form)
  1433. {
  1434. FIELD *field = form->current;
  1435. if ( (++(form->currow)) == field->drows )
  1436. {
  1437. #if GROW_IF_NAVIGATE
  1438. if (!Single_Line_Field(field) && Field_Grown(field,1))
  1439. return(E_OK);
  1440. #endif
  1441. --(form->currow);
  1442. return(E_REQUEST_DENIED);
  1443. }
  1444. return(E_OK);
  1445. }
  1446. /*----------------------------------------------------------------------------
  1447. END of Intra-Field Navigation routines
  1448. --------------------------------------------------------------------------*/
  1449. /*----------------------------------------------------------------------------
  1450. Vertical scrolling helper routines
  1451. --------------------------------------------------------------------------*/
  1452. /*---------------------------------------------------------------------------
  1453. | Facility : libnform
  1454. | Function : static int VSC_Generic(FORM *form, int lines)
  1455. |
  1456. | Description : Scroll multi-line field forward (lines>0) or
  1457. | backward (lines<0) this many lines.
  1458. |
  1459. | Return Values : E_OK - success
  1460. | E_REQUEST_DENIED - can't scroll
  1461. +--------------------------------------------------------------------------*/
  1462. static int VSC_Generic(FORM *form, int lines)
  1463. {
  1464. FIELD *field = form->current;
  1465. int res = E_REQUEST_DENIED;
  1466. int rows_to_go = (lines > 0 ? lines : -lines);
  1467. if (lines > 0)
  1468. {
  1469. if ( (rows_to_go + form->toprow) > (field->drows - field->rows) )
  1470. rows_to_go = (field->drows - field->rows - form->toprow);
  1471. if (rows_to_go > 0)
  1472. {
  1473. form->currow += rows_to_go;
  1474. form->toprow += rows_to_go;
  1475. res = E_OK;
  1476. }
  1477. }
  1478. else
  1479. {
  1480. if (rows_to_go > form->toprow)
  1481. rows_to_go = form->toprow;
  1482. if (rows_to_go > 0)
  1483. {
  1484. form->currow -= rows_to_go;
  1485. form->toprow -= rows_to_go;
  1486. res = E_OK;
  1487. }
  1488. }
  1489. return(res);
  1490. }
  1491. /*----------------------------------------------------------------------------
  1492. End of Vertical scrolling helper routines
  1493. --------------------------------------------------------------------------*/
  1494. /*----------------------------------------------------------------------------
  1495. Vertical scrolling routines
  1496. --------------------------------------------------------------------------*/
  1497. /*---------------------------------------------------------------------------
  1498. | Facility : libnform
  1499. | Function : static int Vertical_Scrolling(
  1500. | int (* const fct) (FORM *),
  1501. | FORM * form)
  1502. |
  1503. | Description : Performs the generic vertical scrolling routines.
  1504. | This has to check for a multi-line field and to set
  1505. | the _NEWTOP flag if scrolling really occured.
  1506. |
  1507. | Return Values : Propagated error code from low-level driver calls
  1508. +--------------------------------------------------------------------------*/
  1509. static int Vertical_Scrolling(int (* const fct) (FORM *), FORM * form)
  1510. {
  1511. int res = E_REQUEST_DENIED;
  1512. if (!Single_Line_Field(form->current))
  1513. {
  1514. res = fct(form);
  1515. if (res == E_OK)
  1516. form->current->status |= _NEWTOP;
  1517. }
  1518. return(res);
  1519. }
  1520. /*---------------------------------------------------------------------------
  1521. | Facility : libnform
  1522. | Function : static int VSC_Scroll_Line_Forward(FORM * form)
  1523. |
  1524. | Description : Scroll multi-line field forward a line
  1525. |
  1526. | Return Values : E_OK - success
  1527. | E_REQUEST_DENIED - no data ahead
  1528. +--------------------------------------------------------------------------*/
  1529. static int VSC_Scroll_Line_Forward(FORM * form)
  1530. {
  1531. return VSC_Generic(form,1);
  1532. }
  1533. /*---------------------------------------------------------------------------
  1534. | Facility : libnform
  1535. | Function : static int VSC_Scroll_Line_Backward(FORM * form)
  1536. |
  1537. | Description : Scroll multi-line field backward a line
  1538. |
  1539. | Return Values : E_OK - success
  1540. | E_REQUEST_DENIED - no data behind
  1541. +--------------------------------------------------------------------------*/
  1542. static int VSC_Scroll_Line_Backward(FORM * form)
  1543. {
  1544. return VSC_Generic(form,-1);
  1545. }
  1546. /*---------------------------------------------------------------------------
  1547. | Facility : libnform
  1548. | Function : static int VSC_Scroll_Page_Forward(FORM * form)
  1549. |
  1550. | Description : Scroll a multi-line field forward a page
  1551. |
  1552. | Return Values : E_OK - success
  1553. | E_REQUEST_DENIED - no data ahead
  1554. +--------------------------------------------------------------------------*/
  1555. static int VSC_Scroll_Page_Forward(FORM * form)
  1556. {
  1557. return VSC_Generic(form,form->current->rows);
  1558. }
  1559. /*---------------------------------------------------------------------------
  1560. | Facility : libnform
  1561. | Function : static int VSC_Scroll_Half_Page_Forward(FORM * form)
  1562. |
  1563. | Description : Scroll a multi-line field forward half a page
  1564. |
  1565. | Return Values : E_OK - success
  1566. | E_REQUEST_DENIED - no data ahead
  1567. +--------------------------------------------------------------------------*/
  1568. static int VSC_Scroll_Half_Page_Forward(FORM * form)
  1569. {
  1570. return VSC_Generic(form,(form->current->rows + 1)/2);
  1571. }
  1572. /*---------------------------------------------------------------------------
  1573. | Facility : libnform
  1574. | Function : static int VSC_Scroll_Page_Backward(FORM * form)
  1575. |
  1576. | Description : Scroll a multi-line field backward a page
  1577. |
  1578. | Return Values : E_OK - success
  1579. | E_REQUEST_DENIED - no data behind
  1580. +--------------------------------------------------------------------------*/
  1581. static int VSC_Scroll_Page_Backward(FORM * form)
  1582. {
  1583. return VSC_Generic(form, -(form->current->rows));
  1584. }
  1585. /*---------------------------------------------------------------------------
  1586. | Facility : libnform
  1587. | Function : static int VSC_Scroll_Half_Page_Backward(FORM * form)
  1588. |
  1589. | Description : Scroll a multi-line field backward half a page
  1590. |
  1591. | Return Values : E_OK - success
  1592. | E_REQUEST_DENIED - no data behind
  1593. +--------------------------------------------------------------------------*/
  1594. static int VSC_Scroll_Half_Page_Backward(FORM * form)
  1595. {
  1596. return VSC_Generic(form, -((form->current->rows + 1)/2));
  1597. }
  1598. /*----------------------------------------------------------------------------
  1599. End of Vertical scrolling routines
  1600. --------------------------------------------------------------------------*/
  1601. /*----------------------------------------------------------------------------
  1602. Horizontal scrolling helper routines
  1603. --------------------------------------------------------------------------*/
  1604. /*---------------------------------------------------------------------------
  1605. | Facility : libnform
  1606. | Function : static int HSC_Generic(FORM *form, int columns)
  1607. |
  1608. | Description : Scroll single-line field forward (columns>0) or
  1609. | backward (columns<0) this many columns.
  1610. |
  1611. | Return Values : E_OK - success
  1612. | E_REQUEST_DENIED - can't scroll
  1613. +--------------------------------------------------------------------------*/
  1614. static int HSC_Generic(FORM *form, int columns)
  1615. {
  1616. FIELD *field = form->current;
  1617. int res = E_REQUEST_DENIED;
  1618. int cols_to_go = (columns > 0 ? columns : -columns);
  1619. if (columns > 0)
  1620. {
  1621. if ((cols_to_go + form->begincol) > (field->dcols - field->cols))
  1622. cols_to_go = field->dcols - field->cols - form->begincol;
  1623. if (cols_to_go > 0)
  1624. {
  1625. form->curcol += cols_to_go;
  1626. form->begincol += cols_to_go;
  1627. res = E_OK;
  1628. }
  1629. }
  1630. else
  1631. {
  1632. if ( cols_to_go > form->begincol )
  1633. cols_to_go = form->begincol;
  1634. if (cols_to_go > 0)
  1635. {
  1636. form->curcol -= cols_to_go;
  1637. form->begincol -= cols_to_go;
  1638. res = E_OK;
  1639. }
  1640. }
  1641. return(res);
  1642. }
  1643. /*----------------------------------------------------------------------------
  1644. End of Horizontal scrolling helper routines
  1645. --------------------------------------------------------------------------*/
  1646. /*----------------------------------------------------------------------------
  1647. Horizontal scrolling routines
  1648. --------------------------------------------------------------------------*/
  1649. /*---------------------------------------------------------------------------
  1650. | Facility : libnform
  1651. | Function : static int Horizontal_Scrolling(
  1652. | int (* const fct) (FORM *),
  1653. | FORM * form)
  1654. |
  1655. | Description : Performs the generic horizontal scrolling routines.
  1656. | This has to check for a single-line field.
  1657. |
  1658. | Return Values : Propagated error code from low-level driver calls
  1659. +--------------------------------------------------------------------------*/
  1660. static int Horizontal_Scrolling(int (* const fct) (FORM *), FORM * form)
  1661. {
  1662. if (Single_Line_Field(form->current))
  1663. return fct(form);
  1664. else
  1665. return(E_REQUEST_DENIED);
  1666. }
  1667. /*---------------------------------------------------------------------------
  1668. | Facility : libnform
  1669. | Function : static int HSC_Scroll_Char_Forward(FORM * form)
  1670. |
  1671. | Description : Scroll single-line field forward a character
  1672. |
  1673. | Return Values : E_OK - success
  1674. | E_REQUEST_DENIED - no data ahead
  1675. +--------------------------------------------------------------------------*/
  1676. static int HSC_Scroll_Char_Forward(FORM *form)
  1677. {
  1678. return HSC_Generic(form,1);
  1679. }
  1680. /*---------------------------------------------------------------------------
  1681. | Facility : libnform
  1682. | Function : static int HSC_Scroll_Char_Backward(FORM * form)
  1683. |
  1684. | Description : Scroll single-line field backward a character
  1685. |
  1686. | Return Values : E_OK - success
  1687. | E_REQUEST_DENIED - no data behind
  1688. +--------------------------------------------------------------------------*/
  1689. static int HSC_Scroll_Char_Backward(FORM *form)
  1690. {
  1691. return HSC_Generic(form,-1);
  1692. }
  1693. /*---------------------------------------------------------------------------
  1694. | Facility : libnform
  1695. | Function : static int HSC_Horizontal_Line_Forward(FORM* form)
  1696. |
  1697. | Description : Scroll single-line field forward a line
  1698. |
  1699. | Return Values : E_OK - success
  1700. | E_REQUEST_DENIED - no data ahead
  1701. +--------------------------------------------------------------------------*/
  1702. static int HSC_Horizontal_Line_Forward(FORM * form)
  1703. {
  1704. return HSC_Generic(form,form->current->cols);
  1705. }
  1706. /*---------------------------------------------------------------------------
  1707. | Facility : libnform
  1708. | Function : static int HSC_Horizontal_Half_Line_Forward(FORM* form)
  1709. |
  1710. | Description : Scroll single-line field forward half a line
  1711. |
  1712. | Return Values : E_OK - success
  1713. | E_REQUEST_DENIED - no data ahead
  1714. +--------------------------------------------------------------------------*/
  1715. static int HSC_Horizontal_Half_Line_Forward(FORM * form)
  1716. {
  1717. return HSC_Generic(form,(form->current->cols + 1)/2);
  1718. }
  1719. /*---------------------------------------------------------------------------
  1720. | Facility : libnform
  1721. | Function : static int HSC_Horizontal_Line_Backward(FORM* form)
  1722. |
  1723. | Description : Scroll single-line field backward a line
  1724. |
  1725. | Return Values : E_OK - success
  1726. | E_REQUEST_DENIED - no data behind
  1727. +--------------------------------------------------------------------------*/
  1728. static int HSC_Horizontal_Line_Backward(FORM * form)
  1729. {
  1730. return HSC_Generic(form,-(form->current->cols));
  1731. }
  1732. /*---------------------------------------------------------------------------
  1733. | Facility : libnform
  1734. | Function : static int HSC_Horizontal_Half_Line_Backward(FORM* form)
  1735. |
  1736. | Description : Scroll single-line field backward half a line
  1737. |
  1738. | Return Values : E_OK - success
  1739. | E_REQUEST_DENIED - no data behind
  1740. +--------------------------------------------------------------------------*/
  1741. static int HSC_Horizontal_Half_Line_Backward(FORM * form)
  1742. {
  1743. return HSC_Generic(form,-((form->current->cols + 1)/2));
  1744. }
  1745. /*----------------------------------------------------------------------------
  1746. End of Horizontal scrolling routines
  1747. --------------------------------------------------------------------------*/
  1748. /*----------------------------------------------------------------------------
  1749. Helper routines for Field Editing
  1750. --------------------------------------------------------------------------*/
  1751. /*---------------------------------------------------------------------------
  1752. | Facility : libnform
  1753. | Function : static bool Is_There_Room_For_A_Line(FORM * form)
  1754. |
  1755. | Description : Check whether or not there is enough room in the
  1756. | buffer to enter a whole line.
  1757. |
  1758. | Return Values : TRUE - there is enough space
  1759. | FALSE - there is not enough space
  1760. +--------------------------------------------------------------------------*/
  1761. INLINE static bool Is_There_Room_For_A_Line(FORM * form)
  1762. {
  1763. FIELD *field = form->current;
  1764. char *begin_of_last_line, *s;
  1765. Synchronize_Buffer(form);
  1766. begin_of_last_line = Address_Of_Row_In_Buffer(field,(field->drows-1));
  1767. s = After_End_Of_Data(begin_of_last_line,field->dcols);
  1768. return ((s==begin_of_last_line) ? TRUE : FALSE);
  1769. }
  1770. /*---------------------------------------------------------------------------
  1771. | Facility : libnform
  1772. | Function : static bool Is_There_Room_For_A_Char_In_Line(FORM * form)
  1773. |
  1774. | Description : Checks whether or not there is room for a new character
  1775. | in the current line.
  1776. |
  1777. | Return Values : TRUE - there is room
  1778. | FALSE - there is not enough room (line full)
  1779. +--------------------------------------------------------------------------*/
  1780. INLINE static bool Is_There_Room_For_A_Char_In_Line(FORM * form)
  1781. {
  1782. int last_char_in_line;
  1783. wmove(form->w,form->currow,form->current->dcols-1);
  1784. last_char_in_line = (int)(winch(form->w) & A_CHARTEXT);
  1785. wmove(form->w,form->currow,form->curcol);
  1786. return (((last_char_in_line == form->current->pad) ||
  1787. is_blank(last_char_in_line)) ? TRUE : FALSE);
  1788. }
  1789. #define There_Is_No_Room_For_A_Char_In_Line(f) \
  1790. !Is_There_Room_For_A_Char_In_Line(f)
  1791. /*---------------------------------------------------------------------------
  1792. | Facility : libnform
  1793. | Function : static int Insert_String(
  1794. | FORM * form,
  1795. | int row,
  1796. | char *txt,
  1797. | int len )
  1798. |
  1799. | Description : Insert the 'len' characters beginning at pointer 'txt'
  1800. | into the 'row' of the 'form'. The insertion occurs
  1801. | on the beginning of the row, all other characters are
  1802. | moved to the right. After the text a pad character will
  1803. | be inserted to separate the text from the rest. If
  1804. | necessary the insertion moves characters on the next
  1805. | line to make place for the requested insertion string.
  1806. |
  1807. | Return Values : E_OK - success
  1808. | E_REQUEST_DENIED -
  1809. | E_SYSTEM_ERROR - system error
  1810. +--------------------------------------------------------------------------*/
  1811. static int Insert_String(FORM *form, int row, char *txt, int len)
  1812. {
  1813. FIELD *field = form->current;
  1814. char *bp = Address_Of_Row_In_Buffer(field,row);
  1815. int datalen = (int)(After_End_Of_Data(bp,field->dcols) - bp);
  1816. int freelen = field->dcols - datalen;
  1817. int requiredlen = len+1;
  1818. char *split;
  1819. int result = E_REQUEST_DENIED;
  1820. char *Space;
  1821. Space = (char*)malloc(2*sizeof(char));
  1822. strcpy(Space, " ");
  1823. if (freelen >= requiredlen)
  1824. {
  1825. wmove(form->w,row,0);
  1826. winsnstr(form->w,txt,len);
  1827. wmove(form->w,row,len);
  1828. winsnstr(form->w,Space,1);
  1829. free(Space);
  1830. return E_OK;
  1831. }
  1832. else
  1833. { /* we have to move characters on the next line. If we are on the
  1834. last line this may work, if the field is growable */
  1835. if ((row == (field->drows - 1)) && Growable(field))
  1836. {
  1837. if (!Field_Grown(field,1))
  1838. {
  1839. free(Space);
  1840. return(E_SYSTEM_ERROR);
  1841. }
  1842. /* !!!Side-Effect : might be changed due to growth!!! */
  1843. bp = Address_Of_Row_In_Buffer(field,row);
  1844. }
  1845. if (row < (field->drows - 1))
  1846. {
  1847. split = After_Last_Whitespace_Character(bp,
  1848. (int)(Get_Start_Of_Data(bp + field->dcols - requiredlen ,
  1849. requiredlen) - bp));
  1850. /* split points now to the first character of the portion of the
  1851. line that must be moved to the next line */
  1852. datalen = (int)(split-bp); /* + freelen has to stay on this line */
  1853. freelen = field->dcols - (datalen + freelen); /* for the next line */
  1854. if ((result=Insert_String(form,row+1,split,freelen))==E_OK)
  1855. {
  1856. wmove(form->w,row,datalen);
  1857. wclrtoeol(form->w);
  1858. wmove(form->w,row,0);
  1859. winsnstr(form->w,txt,len);
  1860. wmove(form->w,row,len);
  1861. winsnstr(form->w,Space,1);
  1862. free(Space);
  1863. return E_OK;
  1864. }
  1865. }
  1866. free(Space);
  1867. return(result);
  1868. }
  1869. }
  1870. /*---------------------------------------------------------------------------
  1871. | Facility : libnform
  1872. | Function : static int Wrapping_Not_Necessary_Or_Wrapping_Ok(
  1873. | FORM * form)
  1874. |
  1875. | Description : If a character has been entered into a field, it may
  1876. | be that wrapping has to occur. This routine checks
  1877. | whether or not wrapping is required and if so, performs
  1878. | the wrapping.
  1879. |
  1880. | Return Values : E_OK - no wrapping required or wrapping
  1881. | was successfull
  1882. | E_REQUEST_DENIED -
  1883. | E_SYSTEM_ERROR - some system error
  1884. +--------------------------------------------------------------------------*/
  1885. static int Wrapping_Not_Necessary_Or_Wrapping_Ok(FORM * form)
  1886. {
  1887. FIELD *field = form->current;
  1888. int result = E_REQUEST_DENIED;
  1889. bool Last_Row = ((field->drows - 1) == form->currow);
  1890. if ( (field->opts & O_WRAP) && /* wrapping wanted */
  1891. (!Single_Line_Field(field)) && /* must be multi-line */
  1892. (There_Is_No_Room_For_A_Char_In_Line(form)) && /* line is full */
  1893. (!Last_Row || Growable(field)) ) /* there are more lines*/
  1894. {
  1895. char *bp;
  1896. char *split;
  1897. int chars_to_be_wrapped;
  1898. int chars_to_remain_on_line;
  1899. if (Last_Row)
  1900. { /* the above logic already ensures, that in this case the field
  1901. is growable */
  1902. if (!Field_Grown(field,1))
  1903. return E_SYSTEM_ERROR;
  1904. }
  1905. bp = Address_Of_Current_Row_In_Buffer(form);
  1906. Window_To_Buffer(form->w,field);
  1907. split = After_Last_Whitespace_Character(bp,field->dcols);
  1908. /* split points to the first character of the sequence to be brought
  1909. on the next line */
  1910. chars_to_remain_on_line = (int)(split - bp);
  1911. chars_to_be_wrapped = field->dcols - chars_to_remain_on_line;
  1912. if (chars_to_remain_on_line > 0)
  1913. {
  1914. if ((result=Insert_String(form,form->currow+1,split,
  1915. chars_to_be_wrapped)) == E_OK)
  1916. {
  1917. wmove(form->w,form->currow,chars_to_remain_on_line);
  1918. wclrtoeol(form->w);
  1919. if (form->curcol >= chars_to_remain_on_line)
  1920. {
  1921. form->currow++;
  1922. form->curcol -= chars_to_remain_on_line;
  1923. }
  1924. return E_OK;
  1925. }
  1926. }
  1927. else
  1928. return E_OK;
  1929. if (result!=E_OK)
  1930. {
  1931. wmove(form->w,form->currow,form->curcol);
  1932. wdelch(form->w);
  1933. Window_To_Buffer(form->w,field);
  1934. result = E_REQUEST_DENIED;
  1935. }
  1936. }
  1937. else
  1938. result = E_OK; /* wrapping was not necessary */
  1939. return(result);
  1940. }
  1941. /*----------------------------------------------------------------------------
  1942. Field Editing routines
  1943. --------------------------------------------------------------------------*/
  1944. /*---------------------------------------------------------------------------
  1945. | Facility : libnform
  1946. | Function : static int Field_Editing(
  1947. | int (* const fct) (FORM *),
  1948. | FORM * form)
  1949. |
  1950. | Description : Generic routine for field editing requests. The driver
  1951. | routines are only called for editable fields, the
  1952. | _WINDOW_MODIFIED flag is set if editing occured.
  1953. | This is somewhat special due to the overload semantics
  1954. | of the NEW_LINE and DEL_PREV requests.
  1955. |
  1956. | Return Values : Error code from low level drivers.
  1957. +--------------------------------------------------------------------------*/
  1958. static int Field_Editing(int (* const fct) (FORM *), FORM * form)
  1959. {
  1960. int res = E_REQUEST_DENIED;
  1961. /* We have to deal here with the specific case of the overloaded
  1962. behaviour of New_Line and Delete_Previous requests.
  1963. They may end up in navigational requests if we are on the first
  1964. character in a field. But navigation is also allowed on non-
  1965. editable fields.
  1966. */
  1967. if ((fct==FE_Delete_Previous) &&
  1968. (form->opts & O_BS_OVERLOAD) &&
  1969. First_Position_In_Current_Field(form) )
  1970. {
  1971. res = Inter_Field_Navigation(FN_Previous_Field,form);
  1972. }
  1973. else
  1974. {
  1975. if (fct==FE_New_Line)
  1976. {
  1977. if ((form->opts & O_NL_OVERLOAD) &&
  1978. First_Position_In_Current_Field(form))
  1979. {
  1980. res = Inter_Field_Navigation(FN_Next_Field,form);
  1981. }
  1982. else
  1983. /* FE_New_Line deals itself with the _WINDOW_MODIFIED flag */
  1984. res = fct(form);
  1985. }
  1986. else
  1987. {
  1988. /* From now on, everything must be editable */
  1989. if (form->current->opts & O_EDIT)
  1990. {
  1991. res = fct(form);
  1992. if (res==E_OK)
  1993. form->status |= _WINDOW_MODIFIED;
  1994. }
  1995. }
  1996. }
  1997. return res;
  1998. }
  1999. /*---------------------------------------------------------------------------
  2000. | Facility : libnform
  2001. | Function : static int FE_New_Line(FORM * form)
  2002. |
  2003. | Description : Perform a new line request. This is rather complex
  2004. | compared to other routines in this code due to the
  2005. | rather difficult to understand description in the
  2006. | manuals.
  2007. |
  2008. | Return Values : E_OK - success
  2009. | E_REQUEST_DENIED - new line not allowed
  2010. | E_SYSTEM_ERROR - system error
  2011. +--------------------------------------------------------------------------*/
  2012. static int FE_New_Line(FORM * form)
  2013. {
  2014. FIELD *field = form->current;
  2015. char *bp, *t;
  2016. bool Last_Row = ((field->drows - 1)==form->currow);
  2017. if (form->status & _OVLMODE)
  2018. {
  2019. if (Last_Row &&
  2020. (!(Growable(field) && !Single_Line_Field(field))))
  2021. {
  2022. if (!(form->opts & O_NL_OVERLOAD))
  2023. return(E_REQUEST_DENIED);
  2024. wclrtoeol(form->w);
  2025. /* we have to set this here, although it is also
  2026. handled in the generic routine. The reason is,
  2027. that FN_Next_Field may fail, but the form is
  2028. definitively changed */
  2029. form->status |= _WINDOW_MODIFIED;
  2030. return Inter_Field_Navigation(FN_Next_Field,form);
  2031. }
  2032. else
  2033. {
  2034. if (Last_Row && !Field_Grown(field,1))
  2035. { /* N.B.: due to the logic in the 'if', LastRow==TRUE
  2036. means here that the field is growable and not
  2037. a single-line field */
  2038. return(E_SYSTEM_ERROR);
  2039. }
  2040. wclrtoeol(form->w);
  2041. form->currow++;
  2042. form->curcol = 0;
  2043. form->status |= _WINDOW_MODIFIED;
  2044. return(E_OK);
  2045. }
  2046. }
  2047. else
  2048. { /* Insert Mode */
  2049. if (Last_Row &&
  2050. !(Growable(field) && !Single_Line_Field(field)))
  2051. {
  2052. if (!(form->opts & O_NL_OVERLOAD))
  2053. return(E_REQUEST_DENIED);
  2054. return Inter_Field_Navigation(FN_Next_Field,form);
  2055. }
  2056. else
  2057. {
  2058. bool May_Do_It = !Last_Row && Is_There_Room_For_A_Line(form);
  2059. if (!(May_Do_It || Growable(field)))
  2060. return(E_REQUEST_DENIED);
  2061. if (!May_Do_It && !Field_Grown(field,1))
  2062. return(E_SYSTEM_ERROR);
  2063. bp= Address_Of_Current_Position_In_Buffer(form);
  2064. t = After_End_Of_Data(bp,field->dcols - form->curcol);
  2065. wclrtoeol(form->w);
  2066. form->currow++;
  2067. form->curcol=0;
  2068. wmove(form->w,form->currow,form->curcol);
  2069. winsertln(form->w);
  2070. waddnstr(form->w,bp,(int)(t-bp));
  2071. form->status |= _WINDOW_MODIFIED;
  2072. return E_OK;
  2073. }
  2074. }
  2075. }
  2076. /*---------------------------------------------------------------------------
  2077. | Facility : libnform
  2078. | Function : static int FE_Insert_Character(FORM * form)
  2079. |
  2080. | Description : Insert blank character at the cursor position
  2081. |
  2082. | Return Values : E_OK
  2083. | E_REQUEST_DENIED
  2084. +--------------------------------------------------------------------------*/
  2085. static int FE_Insert_Character(FORM * form)
  2086. {
  2087. FIELD *field = form->current;
  2088. int result = E_REQUEST_DENIED;
  2089. if (Check_Char(field->type,(int)C_BLANK,(TypeArgument *)(field->arg)))
  2090. {
  2091. bool There_Is_Room = Is_There_Room_For_A_Char_In_Line(form);
  2092. if (There_Is_Room ||
  2093. ((Single_Line_Field(field) && Growable(field))))
  2094. {
  2095. if (!There_Is_Room && !Field_Grown(field,1))
  2096. result = E_SYSTEM_ERROR;
  2097. else
  2098. {
  2099. winsch(form->w,(chtype)C_BLANK);
  2100. result = Wrapping_Not_Necessary_Or_Wrapping_Ok(form);
  2101. }
  2102. }
  2103. }
  2104. return result;
  2105. }
  2106. /*---------------------------------------------------------------------------
  2107. | Facility : libnform
  2108. | Function : static int FE_Insert_Line(FORM * form)
  2109. |
  2110. | Description : Insert a blank line at the cursor position
  2111. |
  2112. | Return Values : E_OK - success
  2113. | E_REQUEST_DENIED - line can not be inserted
  2114. +--------------------------------------------------------------------------*/
  2115. static int FE_Insert_Line(FORM * form)
  2116. {
  2117. FIELD *field = form->current;
  2118. int result = E_REQUEST_DENIED;
  2119. if (Check_Char(field->type,(int)C_BLANK,(TypeArgument *)(field->arg)))
  2120. {
  2121. bool Maybe_Done = (form->currow!=(field->drows-1)) &&
  2122. Is_There_Room_For_A_Line(form);
  2123. if (!Single_Line_Field(field) &&
  2124. (Maybe_Done || Growable(field)))
  2125. {
  2126. if (!Maybe_Done && !Field_Grown(field,1))
  2127. result = E_SYSTEM_ERROR;
  2128. else
  2129. {
  2130. form->curcol = 0;
  2131. winsertln(form->w);
  2132. result = E_OK;
  2133. }
  2134. }
  2135. }
  2136. return result;
  2137. }
  2138. /*---------------------------------------------------------------------------
  2139. | Facility : libnform
  2140. | Function : static int FE_Delete_Character(FORM * form)
  2141. |
  2142. | Description : Delete character at the cursor position
  2143. |
  2144. | Return Values : E_OK - success
  2145. +--------------------------------------------------------------------------*/
  2146. static int FE_Delete_Character(FORM * form)
  2147. {
  2148. wdelch(form->w);
  2149. return E_OK;
  2150. }
  2151. /*---------------------------------------------------------------------------
  2152. | Facility : libnform
  2153. | Function : static int FE_Delete_Previous(FORM * form)
  2154. |
  2155. | Description : Delete character before cursor. Again this is a rather
  2156. | difficult piece compared to others due to the overloading
  2157. | semantics of backspace.
  2158. | N.B.: The case of overloaded BS on first field position
  2159. | is already handled in the generic routine.
  2160. |
  2161. | Return Values : E_OK - success
  2162. | E_REQUEST_DENIED - Character can't be deleted
  2163. +--------------------------------------------------------------------------*/
  2164. static int FE_Delete_Previous(FORM * form)
  2165. {
  2166. FIELD *field = form->current;
  2167. if (First_Position_In_Current_Field(form))
  2168. return E_REQUEST_DENIED;
  2169. if ( (--(form->curcol))<0 )
  2170. {
  2171. char *this_line, *prev_line, *prev_end, *this_end;
  2172. form->curcol++;
  2173. if (form->status & _OVLMODE)
  2174. return E_REQUEST_DENIED;
  2175. prev_line = Address_Of_Row_In_Buffer(field,(form->currow-1));
  2176. this_line = Address_Of_Row_In_Buffer(field,(form->currow));
  2177. Synchronize_Buffer(form);
  2178. prev_end = After_End_Of_Data(prev_line,field->dcols);
  2179. this_end = After_End_Of_Data(this_line,field->dcols);
  2180. if ((int)(this_end-this_line) >
  2181. (field->cols-(int)(prev_end-prev_line)))
  2182. return E_REQUEST_DENIED;
  2183. wdeleteln(form->w);
  2184. Adjust_Cursor_Position(form,prev_end);
  2185. wmove(form->w,form->currow,form->curcol);
  2186. waddnstr(form->w,this_line,(int)(this_end-this_line));
  2187. }
  2188. else
  2189. {
  2190. wmove(form->w,form->currow,form->curcol);
  2191. wdelch(form->w);
  2192. }
  2193. return E_OK;
  2194. }
  2195. /*---------------------------------------------------------------------------
  2196. | Facility : libnform
  2197. | Function : static int FE_Delete_Line(FORM * form)
  2198. |
  2199. | Description : Delete line at cursor position.
  2200. |
  2201. | Return Values : E_OK - success
  2202. +--------------------------------------------------------------------------*/
  2203. static int FE_Delete_Line(FORM * form)
  2204. {
  2205. form->curcol = 0;
  2206. wdeleteln(form->w);
  2207. return E_OK;
  2208. }
  2209. /*---------------------------------------------------------------------------
  2210. | Facility : libnform
  2211. | Function : static int FE_Delete_Word(FORM * form)
  2212. |
  2213. | Description : Delete word at cursor position
  2214. |
  2215. | Return Values : E_OK - success
  2216. | E_REQUEST_DENIED - failure
  2217. +--------------------------------------------------------------------------*/
  2218. static int FE_Delete_Word(FORM * form)
  2219. {
  2220. FIELD *field = form->current;
  2221. char *bp = Address_Of_Current_Row_In_Buffer(form);
  2222. char *ep = bp + field->dcols;
  2223. char *cp = bp + form->curcol;
  2224. char *s;
  2225. Synchronize_Buffer(form);
  2226. if (is_blank(*cp))
  2227. return E_REQUEST_DENIED; /* not in word */
  2228. /* move cursor to begin of word and erase to end of screen-line */
  2229. Adjust_Cursor_Position(form,
  2230. After_Last_Whitespace_Character(bp,form->curcol));
  2231. wmove(form->w,form->currow,form->curcol);
  2232. wclrtoeol(form->w);
  2233. /* skip over word in buffer */
  2234. s = Get_First_Whitespace_Character(cp,(int)(ep-cp));
  2235. /* to begin of next word */
  2236. s = Get_Start_Of_Data(s,(int)(ep - s));
  2237. if ( (s!=cp) && !is_blank(*s))
  2238. {
  2239. /* copy remaining line to window */
  2240. waddnstr(form->w,s,(int)(s - After_End_Of_Data(s,(int)(ep - s))));
  2241. }
  2242. return E_OK;
  2243. }
  2244. /*---------------------------------------------------------------------------
  2245. | Facility : libnform
  2246. | Function : static int FE_Clear_To_End_Of_Line(FORM * form)
  2247. |
  2248. | Description : Clear to end of current line.
  2249. |
  2250. | Return Values : E_OK - success
  2251. +--------------------------------------------------------------------------*/
  2252. static int FE_Clear_To_End_Of_Line(FORM * form)
  2253. {
  2254. wclrtoeol(form->w);
  2255. return E_OK;
  2256. }
  2257. /*---------------------------------------------------------------------------
  2258. | Facility : libnform
  2259. | Function : static int FE_Clear_To_End_Of_Form(FORM * form)
  2260. |
  2261. | Description : Clear to end of form.
  2262. |
  2263. | Return Values : E_OK - success
  2264. +--------------------------------------------------------------------------*/
  2265. static int FE_Clear_To_End_Of_Form(FORM * form)
  2266. {
  2267. wclrtobot(form->w);
  2268. return E_OK;
  2269. }
  2270. /*---------------------------------------------------------------------------
  2271. | Facility : libnform
  2272. | Function : static int FE_Clear_Field(FORM * form)
  2273. |
  2274. | Description : Clear entire field.
  2275. |
  2276. | Return Values : E_OK - success
  2277. +--------------------------------------------------------------------------*/
  2278. static int FE_Clear_Field(FORM * form)
  2279. {
  2280. form->currow = form->curcol = 0;
  2281. werase(form->w);
  2282. return E_OK;
  2283. }
  2284. /*----------------------------------------------------------------------------
  2285. END of Field Editing routines
  2286. --------------------------------------------------------------------------*/
  2287. /*----------------------------------------------------------------------------
  2288. Edit Mode routines
  2289. --------------------------------------------------------------------------*/
  2290. /*---------------------------------------------------------------------------
  2291. | Facility : libnform
  2292. | Function : static int EM_Overlay_Mode(FORM * form)
  2293. |
  2294. | Description : Switch to overlay mode.
  2295. |
  2296. | Return Values : E_OK - success
  2297. +--------------------------------------------------------------------------*/
  2298. static int EM_Overlay_Mode(FORM * form)
  2299. {
  2300. form->status |= _OVLMODE;
  2301. return E_OK;
  2302. }
  2303. /*---------------------------------------------------------------------------
  2304. | Facility : libnform
  2305. | Function : static int EM_Insert_Mode(FORM * form)
  2306. |
  2307. | Description : Switch to insert mode
  2308. |
  2309. | Return Values : E_OK - success
  2310. +--------------------------------------------------------------------------*/
  2311. static int EM_Insert_Mode(FORM * form)
  2312. {
  2313. form->status &= ~_OVLMODE;
  2314. return E_OK;
  2315. }
  2316. /*----------------------------------------------------------------------------
  2317. END of Edit Mode routines
  2318. --------------------------------------------------------------------------*/
  2319. /*----------------------------------------------------------------------------
  2320. Helper routines for Choice Requests
  2321. --------------------------------------------------------------------------*/
  2322. /*---------------------------------------------------------------------------
  2323. | Facility : libnform
  2324. | Function : static bool Next_Choice(
  2325. | FIELDTYPE * typ,
  2326. | FIELD * field,
  2327. | TypeArgument *argp)
  2328. |
  2329. | Description : Get the next field choice. For linked types this is
  2330. | done recursively.
  2331. |
  2332. | Return Values : TRUE - next choice successfully retrieved
  2333. | FALSE - couldn't retrieve next choice
  2334. +--------------------------------------------------------------------------*/
  2335. static bool Next_Choice(FIELDTYPE * typ, FIELD *field, TypeArgument *argp)
  2336. {
  2337. if (!typ || !(typ->status & _HAS_CHOICE))
  2338. return FALSE;
  2339. if (typ->status & _LINKED_TYPE)
  2340. {
  2341. assert(argp != 0);
  2342. return(
  2343. Next_Choice(typ->left ,field,argp->left) ||
  2344. Next_Choice(typ->right,field,argp->right) );
  2345. }
  2346. else
  2347. {
  2348. assert(typ->next != 0);
  2349. return typ->next(field,(void *)argp);
  2350. }
  2351. }
  2352. /*---------------------------------------------------------------------------
  2353. | Facility : libnform
  2354. | Function : static bool Previous_Choice(
  2355. | FIELDTYPE * typ,
  2356. | FIELD * field,
  2357. | TypeArgument *argp)
  2358. |
  2359. | Description : Get the previous field choice. For linked types this
  2360. | is done recursively.
  2361. |
  2362. | Return Values : TRUE - previous choice successfully retrieved
  2363. | FALSE - couldn't retrieve previous choice
  2364. +--------------------------------------------------------------------------*/
  2365. static bool Previous_Choice(FIELDTYPE *typ, FIELD *field, TypeArgument *argp)
  2366. {
  2367. if (!typ || !(typ->status & _HAS_CHOICE))
  2368. return FALSE;
  2369. if (typ->status & _LINKED_TYPE)
  2370. {
  2371. assert(argp != 0);
  2372. return(
  2373. Previous_Choice(typ->left ,field,argp->left) ||
  2374. Previous_Choice(typ->right,field,argp->right));
  2375. }
  2376. else
  2377. {
  2378. assert(typ->prev != 0);
  2379. return typ->prev(field,(void *)argp);
  2380. }
  2381. }
  2382. /*----------------------------------------------------------------------------
  2383. End of Helper routines for Choice Requests
  2384. --------------------------------------------------------------------------*/
  2385. /*----------------------------------------------------------------------------
  2386. Routines for Choice Requests
  2387. --------------------------------------------------------------------------*/
  2388. /*---------------------------------------------------------------------------
  2389. | Facility : libnform
  2390. | Function : static int CR_Next_Choice(FORM * form)
  2391. |
  2392. | Description : Get the next field choice.
  2393. |
  2394. | Return Values : E_OK - success
  2395. | E_REQUEST_DENIED - next choice couldn't be retrieved
  2396. +--------------------------------------------------------------------------*/
  2397. static int CR_Next_Choice(FORM * form)
  2398. {
  2399. FIELD *field = form->current;
  2400. Synchronize_Buffer(form);
  2401. return ((Next_Choice(field->type,field,(TypeArgument *)(field->arg))) ?
  2402. E_OK : E_REQUEST_DENIED);
  2403. }
  2404. /*---------------------------------------------------------------------------
  2405. | Facility : libnform
  2406. | Function : static int CR_Previous_Choice(FORM * form)
  2407. |
  2408. | Description : Get the previous field choice.
  2409. |
  2410. | Return Values : E_OK - success
  2411. | E_REQUEST_DENIED - prev. choice couldn't be retrieved
  2412. +--------------------------------------------------------------------------*/
  2413. static int CR_Previous_Choice(FORM * form)
  2414. {
  2415. FIELD *field = form->current;
  2416. Synchronize_Buffer(form);
  2417. return ((Previous_Choice(field->type,field,(TypeArgument *)(field->arg))) ?
  2418. E_OK : E_REQUEST_DENIED);
  2419. }
  2420. /*----------------------------------------------------------------------------
  2421. End of Routines for Choice Requests
  2422. --------------------------------------------------------------------------*/
  2423. /*----------------------------------------------------------------------------
  2424. Helper routines for Field Validations.
  2425. --------------------------------------------------------------------------*/
  2426. /*---------------------------------------------------------------------------
  2427. | Facility : libnform
  2428. | Function : static bool Check_Field(
  2429. | FIELDTYPE * typ,
  2430. | FIELD * field,
  2431. | TypeArgument * argp)
  2432. |
  2433. | Description : Check the field according to its fieldtype and its
  2434. | actual arguments. For linked fieldtypes this is done
  2435. | recursively.
  2436. |
  2437. | Return Values : TRUE - field is valid
  2438. | FALSE - field is invalid.
  2439. +--------------------------------------------------------------------------*/
  2440. static bool Check_Field(FIELDTYPE *typ, FIELD *field, TypeArgument *argp)
  2441. {
  2442. if (typ)
  2443. {
  2444. if (field->opts & O_NULLOK)
  2445. {
  2446. char *bp = field->buf;
  2447. assert(bp != 0);
  2448. while(is_blank(*bp))
  2449. { bp++; }
  2450. if (*bp == '\0')
  2451. return TRUE;
  2452. }
  2453. if (typ->status & _LINKED_TYPE)
  2454. {
  2455. assert(argp != 0);
  2456. return(
  2457. Check_Field(typ->left ,field,argp->left ) ||
  2458. Check_Field(typ->right,field,argp->right) );
  2459. }
  2460. else
  2461. {
  2462. if (typ->fcheck)
  2463. return typ->fcheck(field,(void *)argp);
  2464. }
  2465. }
  2466. return TRUE;
  2467. }
  2468. /*---------------------------------------------------------------------------
  2469. | Facility : libnform
  2470. | Function : bool _nc_Internal_Validation(FORM * form )
  2471. |
  2472. | Description : Validate the current field of the form.
  2473. |
  2474. | Return Values : TRUE - field is valid
  2475. | FALSE - field is invalid
  2476. +--------------------------------------------------------------------------*/
  2477. bool
  2478. _nc_Internal_Validation(FORM *form)
  2479. {
  2480. FIELD *field;
  2481. field = form->current;
  2482. Synchronize_Buffer(form);
  2483. if ((form->status & _FCHECK_REQUIRED) ||
  2484. (!(field->opts & O_PASSOK)))
  2485. {
  2486. if (!Check_Field(field->type,field,(TypeArgument *)(field->arg)))
  2487. return FALSE;
  2488. form->status &= ~_FCHECK_REQUIRED;
  2489. field->status |= _CHANGED;
  2490. Synchronize_Linked_Fields(field);
  2491. }
  2492. return TRUE;
  2493. }
  2494. /*----------------------------------------------------------------------------
  2495. End of Helper routines for Field Validations.
  2496. --------------------------------------------------------------------------*/
  2497. /*----------------------------------------------------------------------------
  2498. Routines for Field Validation.
  2499. --------------------------------------------------------------------------*/
  2500. /*---------------------------------------------------------------------------
  2501. | Facility : libnform
  2502. | Function : static int FV_Validation(FORM * form)
  2503. |
  2504. | Description : Validate the current field of the form.
  2505. |
  2506. | Return Values : E_OK - field valid
  2507. | E_INVALID_FIELD - field not valid
  2508. +--------------------------------------------------------------------------*/
  2509. static int FV_Validation(FORM * form)
  2510. {
  2511. if (_nc_Internal_Validation(form))
  2512. return E_OK;
  2513. else
  2514. return E_INVALID_FIELD;
  2515. }
  2516. /*----------------------------------------------------------------------------
  2517. End of routines for Field Validation.
  2518. --------------------------------------------------------------------------*/
  2519. /*----------------------------------------------------------------------------
  2520. Helper routines for Inter-Field Navigation
  2521. --------------------------------------------------------------------------*/
  2522. /*---------------------------------------------------------------------------
  2523. | Facility : libnform
  2524. | Function : static FIELD *Next_Field_On_Page(FIELD * field)
  2525. |
  2526. | Description : Get the next field after the given field on the current
  2527. | page. The order of fields is the one defined by the
  2528. | fields array. Only visible and active fields are
  2529. | counted.
  2530. |
  2531. | Return Values : Pointer to the next field.
  2532. +--------------------------------------------------------------------------*/
  2533. INLINE static FIELD *Next_Field_On_Page(FIELD * field)
  2534. {
  2535. FORM *form = field->form;
  2536. FIELD **field_on_page = &form->field[field->index];
  2537. FIELD **first_on_page = &form->field[form->page[form->curpage].pmin];
  2538. FIELD **last_on_page = &form->field[form->page[form->curpage].pmax];
  2539. do
  2540. {
  2541. field_on_page =
  2542. (field_on_page==last_on_page) ? first_on_page : field_on_page + 1;
  2543. if (Field_Is_Selectable(*field_on_page))
  2544. break;
  2545. } while(field!=(*field_on_page));
  2546. return(*field_on_page);
  2547. }
  2548. /*---------------------------------------------------------------------------
  2549. | Facility : libnform
  2550. | Function : FIELD* _nc_First_Active_Field(FORM * form)
  2551. |
  2552. | Description : Get the first active field on the current page,
  2553. | if there are such. If there are none, get the first
  2554. | visible field on the page. If there are also none,
  2555. | we return the first field on page and hope the best.
  2556. |
  2557. | Return Values : Pointer to calculated field.
  2558. +--------------------------------------------------------------------------*/
  2559. FIELD*
  2560. _nc_First_Active_Field(FORM * form)
  2561. {
  2562. FIELD **last_on_page = &form->field[form->page[form->curpage].pmax];
  2563. FIELD *proposed = Next_Field_On_Page(*last_on_page);
  2564. if (proposed == *last_on_page)
  2565. { /* there might be the special situation, where there is no
  2566. active and visible field on the current page. We then select
  2567. the first visible field on this readonly page
  2568. */
  2569. if (Field_Is_Not_Selectable(proposed))
  2570. {
  2571. FIELD **field = &form->field[proposed->index];
  2572. FIELD **first = &form->field[form->page[form->curpage].pmin];
  2573. do
  2574. {
  2575. field = (field==last_on_page) ? first : field + 1;
  2576. if (((*field)->opts & O_VISIBLE))
  2577. break;
  2578. } while(proposed!=(*field));
  2579. proposed = *field;
  2580. if ((proposed == *last_on_page) && !(proposed->opts&O_VISIBLE))
  2581. { /* This means, there is also no visible field on the page.
  2582. So we propose the first one and hope the very best...
  2583. Some very clever user has designed a readonly and invisible
  2584. page on this form.
  2585. */
  2586. proposed = *first;
  2587. }
  2588. }
  2589. }
  2590. return(proposed);
  2591. }
  2592. /*---------------------------------------------------------------------------
  2593. | Facility : libnform
  2594. | Function : static FIELD *Previous_Field_On_Page(FIELD * field)
  2595. |
  2596. | Description : Get the previous field before the given field on the
  2597. | current page. The order of fields is the one defined by
  2598. | the fields array. Only visible and active fields are
  2599. | counted.
  2600. |
  2601. | Return Values : Pointer to the previous field.
  2602. +--------------------------------------------------------------------------*/
  2603. INLINE static FIELD *Previous_Field_On_Page(FIELD * field)
  2604. {
  2605. FORM *form = field->form;
  2606. FIELD **field_on_page = &form->field[field->index];
  2607. FIELD **first_on_page = &form->field[form->page[form->curpage].pmin];
  2608. FIELD **last_on_page = &form->field[form->page[form->curpage].pmax];
  2609. do
  2610. {
  2611. field_on_page =
  2612. (field_on_page==first_on_page) ? last_on_page : field_on_page - 1;
  2613. if (Field_Is_Selectable(*field_on_page))
  2614. break;
  2615. } while(field!=(*field_on_page));
  2616. return (*field_on_page);
  2617. }
  2618. /*---------------------------------------------------------------------------
  2619. | Facility : libnform
  2620. | Function : static FIELD *Sorted_Next_Field(FIELD * field)
  2621. |
  2622. | Description : Get the next field after the given field on the current
  2623. | page. The order of fields is the one defined by the
  2624. | (row,column) geometry, rows are major.
  2625. |
  2626. | Return Values : Pointer to the next field.
  2627. +--------------------------------------------------------------------------*/
  2628. INLINE static FIELD *Sorted_Next_Field(FIELD * field)
  2629. {
  2630. FIELD *field_on_page = field;
  2631. do
  2632. {
  2633. field_on_page = field_on_page->snext;
  2634. if (Field_Is_Selectable(field_on_page))
  2635. break;
  2636. } while(field_on_page!=field);
  2637. return (field_on_page);
  2638. }
  2639. /*---------------------------------------------------------------------------
  2640. | Facility : libnform
  2641. | Function : static FIELD *Sorted_Previous_Field(FIELD * field)
  2642. |
  2643. | Description : Get the previous field before the given field on the
  2644. | current page. The order of fields is the one defined
  2645. | by the (row,column) geometry, rows are major.
  2646. |
  2647. | Return Values : Pointer to the previous field.
  2648. +--------------------------------------------------------------------------*/
  2649. INLINE static FIELD *Sorted_Previous_Field(FIELD * field)
  2650. {
  2651. FIELD *field_on_page = field;
  2652. do
  2653. {
  2654. field_on_page = field_on_page->sprev;
  2655. if (Field_Is_Selectable(field_on_page))
  2656. break;
  2657. } while(field_on_page!=field);
  2658. return (field_on_page);
  2659. }
  2660. /*---------------------------------------------------------------------------
  2661. | Facility : libnform
  2662. | Function : static FIELD *Left_Neighbour_Field(FIELD * field)
  2663. |
  2664. | Description : Get the left neighbour of the field on the same line
  2665. | and the same page. Cycles through the line.
  2666. |
  2667. | Return Values : Pointer to left neighbour field.
  2668. +--------------------------------------------------------------------------*/
  2669. INLINE static FIELD *Left_Neighbour_Field(FIELD * field)
  2670. {
  2671. FIELD *field_on_page = field;
  2672. /* For a field that has really a left neighbour, the while clause
  2673. immediately fails and the loop is left, positioned at the right
  2674. neighbour. Otherwise we cycle backwards through the sorted fieldlist
  2675. until we enter the same line (from the right end).
  2676. */
  2677. do
  2678. {
  2679. field_on_page = Sorted_Previous_Field(field_on_page);
  2680. } while(field_on_page->frow != field->frow);
  2681. return (field_on_page);
  2682. }
  2683. /*---------------------------------------------------------------------------
  2684. | Facility : libnform
  2685. | Function : static FIELD *Right_Neighbour_Field(FIELD * field)
  2686. |
  2687. | Description : Get the right neighbour of the field on the same line
  2688. | and the same page.
  2689. |
  2690. | Return Values : Pointer to right neighbour field.
  2691. +--------------------------------------------------------------------------*/
  2692. INLINE static FIELD *Right_Neighbour_Field(FIELD * field)
  2693. {
  2694. FIELD *field_on_page = field;
  2695. /* See the comments on Left_Neighbour_Field to understand how it works */
  2696. do
  2697. {
  2698. field_on_page = Sorted_Next_Field(field_on_page);
  2699. } while(field_on_page->frow != field->frow);
  2700. return (field_on_page);
  2701. }
  2702. /*---------------------------------------------------------------------------
  2703. | Facility : libnform
  2704. | Function : static FIELD *Upper_Neighbour_Field(FIELD * field)
  2705. |
  2706. | Description : Because of the row-major nature of sorting the fields,
  2707. | its more difficult to define whats the upper neighbour
  2708. | field really means. We define that it must be on a
  2709. | 'previous' line (cyclic order!) and is the rightmost
  2710. | field laying on the left side of the given field. If
  2711. | this set is empty, we take the first field on the line.
  2712. |
  2713. | Return Values : Pointer to the upper neighbour field.
  2714. +--------------------------------------------------------------------------*/
  2715. static FIELD *Upper_Neighbour_Field(FIELD * field)
  2716. {
  2717. FIELD *field_on_page = field;
  2718. int frow = field->frow;
  2719. int fcol = field->fcol;
  2720. /* Walk back to the 'previous' line. The second term in the while clause
  2721. just guarantees that we stop if we cycled through the line because
  2722. there might be no 'previous' line if the page has just one line.
  2723. */
  2724. do
  2725. {
  2726. field_on_page = Sorted_Previous_Field(field_on_page);
  2727. } while(field_on_page->frow==frow && field_on_page->fcol!=fcol);
  2728. if (field_on_page->frow!=frow)
  2729. { /* We really found a 'previous' line. We are positioned at the
  2730. rightmost field on this line */
  2731. frow = field_on_page->frow;
  2732. /* We walk to the left as long as we are really right of the
  2733. field. */
  2734. while(field_on_page->frow==frow && field_on_page->fcol>fcol)
  2735. field_on_page = Sorted_Previous_Field(field_on_page);
  2736. /* If we wrapped, just go to the right which is the first field on
  2737. the row */
  2738. if (field_on_page->frow!=frow)
  2739. field_on_page = Sorted_Next_Field(field_on_page);
  2740. }
  2741. return (field_on_page);
  2742. }
  2743. /*---------------------------------------------------------------------------
  2744. | Facility : libnform
  2745. | Function : static FIELD *Down_Neighbour_Field(FIELD * field)
  2746. |
  2747. | Description : Because of the row-major nature of sorting the fields,
  2748. | its more difficult to define whats the down neighbour
  2749. | field really means. We define that it must be on a
  2750. | 'next' line (cyclic order!) and is the leftmost
  2751. | field laying on the right side of the given field. If
  2752. | this set is empty, we take the last field on the line.
  2753. |
  2754. | Return Values : Pointer to the upper neighbour field.
  2755. +--------------------------------------------------------------------------*/
  2756. static FIELD *Down_Neighbour_Field(FIELD * field)
  2757. {
  2758. FIELD *field_on_page = field;
  2759. int frow = field->frow;
  2760. int fcol = field->fcol;
  2761. /* Walk forward to the 'next' line. The second term in the while clause
  2762. just guarantees that we stop if we cycled through the line because
  2763. there might be no 'next' line if the page has just one line.
  2764. */
  2765. do
  2766. {
  2767. field_on_page = Sorted_Next_Field(field_on_page);
  2768. } while(field_on_page->frow==frow && field_on_page->fcol!=fcol);
  2769. if (field_on_page->frow!=frow)
  2770. { /* We really found a 'next' line. We are positioned at the rightmost
  2771. field on this line */
  2772. frow = field_on_page->frow;
  2773. /* We walk to the right as long as we are really left of the
  2774. field. */
  2775. while(field_on_page->frow==frow && field_on_page->fcol<fcol)
  2776. field_on_page = Sorted_Next_Field(field_on_page);
  2777. /* If we wrapped, just go to the left which is the last field on
  2778. the row */
  2779. if (field_on_page->frow!=frow)
  2780. field_on_page = Sorted_Previous_Field(field_on_page);
  2781. }
  2782. return(field_on_page);
  2783. }
  2784. /*----------------------------------------------------------------------------
  2785. Inter-Field Navigation routines
  2786. --------------------------------------------------------------------------*/
  2787. /*---------------------------------------------------------------------------
  2788. | Facility : libnform
  2789. | Function : static int Inter_Field_Navigation(
  2790. | int (* const fct) (FORM *),
  2791. | FORM * form)
  2792. |
  2793. | Description : Generic behaviour for changing the current field, the
  2794. | field is left and a new field is entered. So the field
  2795. | must be validated and the field init/term hooks must
  2796. | be called.
  2797. |
  2798. | Return Values : E_OK - success
  2799. | E_INVALID_FIELD - field is invalid
  2800. | some other - error from subordinate call
  2801. +--------------------------------------------------------------------------*/
  2802. static int Inter_Field_Navigation(int (* const fct) (FORM *),FORM *form)
  2803. {
  2804. int res;
  2805. if (!_nc_Internal_Validation(form))
  2806. res = E_INVALID_FIELD;
  2807. else
  2808. {
  2809. Call_Hook(form,fieldterm);
  2810. res = fct(form);
  2811. Call_Hook(form,fieldinit);
  2812. }
  2813. return res;
  2814. }
  2815. /*---------------------------------------------------------------------------
  2816. | Facility : libnform
  2817. | Function : static int FN_Next_Field(FORM * form)
  2818. |
  2819. | Description : Move to the next field on the current page of the form
  2820. |
  2821. | Return Values : E_OK - success
  2822. | != E_OK - error from subordinate call
  2823. +--------------------------------------------------------------------------*/
  2824. static int FN_Next_Field(FORM * form)
  2825. {
  2826. return _nc_Set_Current_Field(form,
  2827. Next_Field_On_Page(form->current));
  2828. }
  2829. /*---------------------------------------------------------------------------
  2830. | Facility : libnform
  2831. | Function : static int FN_Previous_Field(FORM * form)
  2832. |
  2833. | Description : Move to the previous field on the current page of the
  2834. | form
  2835. |
  2836. | Return Values : E_OK - success
  2837. | != E_OK - error from subordinate call
  2838. +--------------------------------------------------------------------------*/
  2839. static int FN_Previous_Field(FORM * form)
  2840. {
  2841. return _nc_Set_Current_Field(form,
  2842. Previous_Field_On_Page(form->current));
  2843. }
  2844. /*---------------------------------------------------------------------------
  2845. | Facility : libnform
  2846. | Function : static int FN_First_Field(FORM * form)
  2847. |
  2848. | Description : Move to the first field on the current page of the form
  2849. |
  2850. | Return Values : E_OK - success
  2851. | != E_OK - error from subordinate call
  2852. +--------------------------------------------------------------------------*/
  2853. static int FN_First_Field(FORM * form)
  2854. {
  2855. return _nc_Set_Current_Field(form,
  2856. Next_Field_On_Page(form->field[form->page[form->curpage].pmax]));
  2857. }
  2858. /*---------------------------------------------------------------------------
  2859. | Facility : libnform
  2860. | Function : static int FN_Last_Field(FORM * form)
  2861. |
  2862. | Description : Move to the last field on the current page of the form
  2863. |
  2864. | Return Values : E_OK - success
  2865. | != E_OK - error from subordinate call
  2866. +--------------------------------------------------------------------------*/
  2867. static int FN_Last_Field(FORM * form)
  2868. {
  2869. return
  2870. _nc_Set_Current_Field(form,
  2871. Previous_Field_On_Page(form->field[form->page[form->curpage].pmin]));
  2872. }
  2873. /*---------------------------------------------------------------------------
  2874. | Facility : libnform
  2875. | Function : static int FN_Sorted_Next_Field(FORM * form)
  2876. |
  2877. | Description : Move to the sorted next field on the current page
  2878. | of the form.
  2879. |
  2880. | Return Values : E_OK - success
  2881. | != E_OK - error from subordinate call
  2882. +--------------------------------------------------------------------------*/
  2883. static int FN_Sorted_Next_Field(FORM * form)
  2884. {
  2885. return _nc_Set_Current_Field(form,
  2886. Sorted_Next_Field(form->current));
  2887. }
  2888. /*---------------------------------------------------------------------------
  2889. | Facility : libnform
  2890. | Function : static int FN_Sorted_Previous_Field(FORM * form)
  2891. |
  2892. | Description : Move to the sorted previous field on the current page
  2893. | of the form.
  2894. |
  2895. | Return Values : E_OK - success
  2896. | != E_OK - error from subordinate call
  2897. +--------------------------------------------------------------------------*/
  2898. static int FN_Sorted_Previous_Field(FORM * form)
  2899. {
  2900. return _nc_Set_Current_Field(form,
  2901. Sorted_Previous_Field(form->current));
  2902. }
  2903. /*---------------------------------------------------------------------------
  2904. | Facility : libnform
  2905. | Function : static int FN_Sorted_First_Field(FORM * form)
  2906. |
  2907. | Description : Move to the sorted first field on the current page
  2908. | of the form.
  2909. |
  2910. | Return Values : E_OK - success
  2911. | != E_OK - error from subordinate call
  2912. +--------------------------------------------------------------------------*/
  2913. static int FN_Sorted_First_Field(FORM * form)
  2914. {
  2915. return _nc_Set_Current_Field(form,
  2916. Sorted_Next_Field(form->field[form->page[form->curpage].smax]));
  2917. }
  2918. /*---------------------------------------------------------------------------
  2919. | Facility : libnform
  2920. | Function : static int FN_Sorted_Last_Field(FORM * form)
  2921. |
  2922. | Description : Move to the sorted last field on the current page
  2923. | of the form.
  2924. |
  2925. | Return Values : E_OK - success
  2926. | != E_OK - error from subordinate call
  2927. +--------------------------------------------------------------------------*/
  2928. static int FN_Sorted_Last_Field(FORM * form)
  2929. {
  2930. return _nc_Set_Current_Field(form,
  2931. Sorted_Previous_Field(form->field[form->page[form->curpage].smin]));
  2932. }
  2933. /*---------------------------------------------------------------------------
  2934. | Facility : libnform
  2935. | Function : static int FN_Left_Field(FORM * form)
  2936. |
  2937. | Description : Get the field on the left of the current field on the
  2938. | same line and the same page. Cycles through the line.
  2939. |
  2940. | Return Values : E_OK - success
  2941. | != E_OK - error from subordinate call
  2942. +--------------------------------------------------------------------------*/
  2943. static int FN_Left_Field(FORM * form)
  2944. {
  2945. return _nc_Set_Current_Field(form,
  2946. Left_Neighbour_Field(form->current));
  2947. }
  2948. /*---------------------------------------------------------------------------
  2949. | Facility : libnform
  2950. | Function : static int FN_Right_Field(FORM * form)
  2951. |
  2952. | Description : Get the field on the right of the current field on the
  2953. | same line and the same page. Cycles through the line.
  2954. |
  2955. | Return Values : E_OK - success
  2956. | != E_OK - error from subordinate call
  2957. +--------------------------------------------------------------------------*/
  2958. static int FN_Right_Field(FORM * form)
  2959. {
  2960. return _nc_Set_Current_Field(form,
  2961. Right_Neighbour_Field(form->current));
  2962. }
  2963. /*---------------------------------------------------------------------------
  2964. | Facility : libnform
  2965. | Function : static int FN_Up_Field(FORM * form)
  2966. |
  2967. | Description : Get the upper neighbour of the current field. This
  2968. | cycles through the page. See the comments of the
  2969. | Upper_Neighbour_Field function to understand how
  2970. | 'upper' is defined.
  2971. |
  2972. | Return Values : E_OK - success
  2973. | != E_OK - error from subordinate call
  2974. +--------------------------------------------------------------------------*/
  2975. static int FN_Up_Field(FORM * form)
  2976. {
  2977. return _nc_Set_Current_Field(form,
  2978. Upper_Neighbour_Field(form->current));
  2979. }
  2980. /*---------------------------------------------------------------------------
  2981. | Facility : libnform
  2982. | Function : static int FN_Down_Field(FORM * form)
  2983. |
  2984. | Description : Get the down neighbour of the current field. This
  2985. | cycles through the page. See the comments of the
  2986. | Down_Neighbour_Field function to understand how
  2987. | 'down' is defined.
  2988. |
  2989. | Return Values : E_OK - success
  2990. | != E_OK - error from subordinate call
  2991. +--------------------------------------------------------------------------*/
  2992. static int FN_Down_Field(FORM * form)
  2993. {
  2994. return _nc_Set_Current_Field(form,
  2995. Down_Neighbour_Field(form->current));
  2996. }
  2997. /*----------------------------------------------------------------------------
  2998. END of Field Navigation routines
  2999. --------------------------------------------------------------------------*/
  3000. /*----------------------------------------------------------------------------
  3001. Helper routines for Page Navigation
  3002. --------------------------------------------------------------------------*/
  3003. /*---------------------------------------------------------------------------
  3004. | Facility : libnform
  3005. | Function : int _nc_Set_Form_Page(FORM * form,
  3006. | int page,
  3007. | FIELD * field)
  3008. |
  3009. | Description : Make the given page nr. the current page and make
  3010. | the given field the current field on the page. If
  3011. | for the field NULL is given, make the first field on
  3012. | the page the current field. The routine acts only
  3013. | if the requested page is not the current page.
  3014. |
  3015. | Return Values : E_OK - success
  3016. | != E_OK - error from subordinate call
  3017. +--------------------------------------------------------------------------*/
  3018. int
  3019. _nc_Set_Form_Page(FORM * form, int page, FIELD * field)
  3020. {
  3021. int res = E_OK;
  3022. if ((form->curpage!=page))
  3023. {
  3024. FIELD *last_field, *field_on_page;
  3025. werase(Get_Form_Window(form));
  3026. form->curpage = page;
  3027. last_field = field_on_page = form->field[form->page[page].smin];
  3028. do
  3029. {
  3030. if (field_on_page->opts & O_VISIBLE)
  3031. if ((res=Display_Field(field_on_page))!=E_OK)
  3032. return(res);
  3033. field_on_page = field_on_page->snext;
  3034. } while(field_on_page != last_field);
  3035. if (field)
  3036. res = _nc_Set_Current_Field(form,field);
  3037. else
  3038. /* N.B.: we don't encapsulate this by Inter_Field_Navigation(),
  3039. because this is already executed in a page navigation
  3040. context that contains field navigation
  3041. */
  3042. res = FN_First_Field(form);
  3043. }
  3044. return(res);
  3045. }
  3046. /*---------------------------------------------------------------------------
  3047. | Facility : libnform
  3048. | Function : static int Next_Page_Number(const FORM * form)
  3049. |
  3050. | Description : Calculate the page number following the current page
  3051. | number. This cycles if the highest page number is
  3052. | reached.
  3053. |
  3054. | Return Values : The next page number
  3055. +--------------------------------------------------------------------------*/
  3056. INLINE static int Next_Page_Number(const FORM * form)
  3057. {
  3058. return (form->curpage + 1) % form->maxpage;
  3059. }
  3060. /*---------------------------------------------------------------------------
  3061. | Facility : libnform
  3062. | Function : static int Previous_Page_Number(const FORM * form)
  3063. |
  3064. | Description : Calculate the page number before the current page
  3065. | number. This cycles if the first page number is
  3066. | reached.
  3067. |
  3068. | Return Values : The previous page number
  3069. +--------------------------------------------------------------------------*/
  3070. INLINE static int Previous_Page_Number(const FORM * form)
  3071. {
  3072. return (form->curpage!=0 ? form->curpage - 1 : form->maxpage - 1);
  3073. }
  3074. /*----------------------------------------------------------------------------
  3075. Page Navigation routines
  3076. --------------------------------------------------------------------------*/
  3077. /*---------------------------------------------------------------------------
  3078. | Facility : libnform
  3079. | Function : static int Page_Navigation(
  3080. | int (* const fct) (FORM *),
  3081. | FORM * form)
  3082. |
  3083. | Description : Generic behaviour for changing a page. This means
  3084. | that the field is left and a new field is entered.
  3085. | So the field must be validated and the field init/term
  3086. | hooks must be called. Because also the page is changed,
  3087. | the forms init/term hooks must be called also.
  3088. |
  3089. | Return Values : E_OK - success
  3090. | E_INVALID_FIELD - field is invalid
  3091. | some other - error from subordinate call
  3092. +--------------------------------------------------------------------------*/
  3093. static int Page_Navigation(int (* const fct) (FORM *), FORM * form)
  3094. {
  3095. int res;
  3096. if (!_nc_Internal_Validation(form))
  3097. res = E_INVALID_FIELD;
  3098. else
  3099. {
  3100. Call_Hook(form,fieldterm);
  3101. Call_Hook(form,formterm);
  3102. res = fct(form);
  3103. Call_Hook(form,forminit);
  3104. Call_Hook(form,fieldinit);
  3105. }
  3106. return res;
  3107. }
  3108. /*---------------------------------------------------------------------------
  3109. | Facility : libnform
  3110. | Function : static int PN_Next_Page(FORM * form)
  3111. |
  3112. | Description : Move to the next page of the form
  3113. |
  3114. | Return Values : E_OK - success
  3115. | != E_OK - error from subordinate call
  3116. +--------------------------------------------------------------------------*/
  3117. static int PN_Next_Page(FORM * form)
  3118. {
  3119. return _nc_Set_Form_Page(form,Next_Page_Number(form),(FIELD *)0);
  3120. }
  3121. /*---------------------------------------------------------------------------
  3122. | Facility : libnform
  3123. | Function : static int PN_Previous_Page(FORM * form)
  3124. |
  3125. | Description : Move to the previous page of the form
  3126. |
  3127. | Return Values : E_OK - success
  3128. | != E_OK - error from subordinate call
  3129. +--------------------------------------------------------------------------*/
  3130. static int PN_Previous_Page(FORM * form)
  3131. {
  3132. return _nc_Set_Form_Page(form,Previous_Page_Number(form),(FIELD *)0);
  3133. }
  3134. /*---------------------------------------------------------------------------
  3135. | Facility : libnform
  3136. | Function : static int PN_First_Page(FORM * form)
  3137. |
  3138. | Description : Move to the first page of the form
  3139. |
  3140. | Return Values : E_OK - success
  3141. | != E_OK - error from subordinate call
  3142. +--------------------------------------------------------------------------*/
  3143. static int PN_First_Page(FORM * form)
  3144. {
  3145. return _nc_Set_Form_Page(form,0,(FIELD *)0);
  3146. }
  3147. /*---------------------------------------------------------------------------
  3148. | Facility : libnform
  3149. | Function : static int PN_Last_Page(FORM * form)
  3150. |
  3151. | Description : Move to the last page of the form
  3152. |
  3153. | Return Values : E_OK - success
  3154. | != E_OK - error from subordinate call
  3155. +--------------------------------------------------------------------------*/
  3156. static int PN_Last_Page(FORM * form)
  3157. {
  3158. return _nc_Set_Form_Page(form,form->maxpage-1,(FIELD *)0);
  3159. }
  3160. /*----------------------------------------------------------------------------
  3161. END of Field Navigation routines
  3162. --------------------------------------------------------------------------*/
  3163. /*----------------------------------------------------------------------------
  3164. Helper routines for the core form driver.
  3165. --------------------------------------------------------------------------*/
  3166. /*---------------------------------------------------------------------------
  3167. | Facility : libnform
  3168. | Function : static int Data_Entry(FORM * form,int c)
  3169. |
  3170. | Description : Enter character c into at the current position of the
  3171. | current field of the form.
  3172. |
  3173. | Return Values : E_OK -
  3174. | E_REQUEST_DENIED -
  3175. | E_SYSTEM_ERROR -
  3176. +--------------------------------------------------------------------------*/
  3177. static int Data_Entry(FORM * form, int c)
  3178. {
  3179. FIELD *field = form->current;
  3180. int result = E_REQUEST_DENIED;
  3181. if ( (field->opts & O_EDIT)
  3182. #if FIX_FORM_INACTIVE_BUG
  3183. && (field->opts & O_ACTIVE)
  3184. #endif
  3185. )
  3186. {
  3187. if ( (field->opts & O_BLANK) &&
  3188. First_Position_In_Current_Field(form) &&
  3189. !(form->status & _FCHECK_REQUIRED) &&
  3190. !(form->status & _WINDOW_MODIFIED) )
  3191. werase(form->w);
  3192. if (form->status & _OVLMODE)
  3193. {
  3194. waddch(form->w,(chtype)c);
  3195. }
  3196. else /* no _OVLMODE */
  3197. {
  3198. bool There_Is_Room = Is_There_Room_For_A_Char_In_Line(form);
  3199. if (!(There_Is_Room ||
  3200. ((Single_Line_Field(field) && Growable(field)))))
  3201. return E_REQUEST_DENIED;
  3202. if (!There_Is_Room && !Field_Grown(field,1))
  3203. return E_SYSTEM_ERROR;
  3204. winsch(form->w,(chtype)c);
  3205. }
  3206. if ((result=Wrapping_Not_Necessary_Or_Wrapping_Ok(form))==E_OK)
  3207. {
  3208. bool End_Of_Field= (((field->drows-1)==form->currow) &&
  3209. ((field->dcols-1)==form->curcol));
  3210. form->status |= _WINDOW_MODIFIED;
  3211. if (End_Of_Field && !Growable(field) && (field->opts & O_AUTOSKIP))
  3212. result = Inter_Field_Navigation(FN_Next_Field,form);
  3213. else
  3214. {
  3215. if (End_Of_Field && Growable(field) && !Field_Grown(field,1))
  3216. result = E_SYSTEM_ERROR;
  3217. else
  3218. {
  3219. IFN_Next_Character(form);
  3220. result = E_OK;
  3221. }
  3222. }
  3223. }
  3224. }
  3225. return result;
  3226. }
  3227. /* Structure to describe the binding of a request code to a function.
  3228. The member keycode codes the request value as well as the generic
  3229. routine to use for the request. The code for the generic routine
  3230. is coded in the upper 16 Bits while the request code is coded in
  3231. the lower 16 bits.
  3232. In terms of C++ you might think of a request as a class with a
  3233. virtual method "perform". The different types of request are
  3234. derived from this base class and overload (or not) the base class
  3235. implementation of perform.
  3236. */
  3237. typedef struct {
  3238. int keycode; /* must be at least 32 bit: hi:mode, lo: key */
  3239. int (*cmd)(FORM *); /* low level driver routine for this key */
  3240. } Binding_Info;
  3241. /* You may see this is the class-id of the request type class */
  3242. #define ID_PN (0x00000000) /* Page navigation */
  3243. #define ID_FN (0x00010000) /* Inter-Field navigation */
  3244. #define ID_IFN (0x00020000) /* Intra-Field navigation */
  3245. #define ID_VSC (0x00030000) /* Vertical Scrolling */
  3246. #define ID_HSC (0x00040000) /* Horizontal Scrolling */
  3247. #define ID_FE (0x00050000) /* Field Editing */
  3248. #define ID_EM (0x00060000) /* Edit Mode */
  3249. #define ID_FV (0x00070000) /* Field Validation */
  3250. #define ID_CH (0x00080000) /* Choice */
  3251. #define ID_Mask (0xffff0000)
  3252. #define Key_Mask (0x0000ffff)
  3253. #define ID_Shft (16)
  3254. /* This array holds all the Binding Infos */
  3255. static const Binding_Info bindings[MAX_FORM_COMMAND - MIN_FORM_COMMAND + 1] =
  3256. {
  3257. { REQ_NEXT_PAGE |ID_PN ,PN_Next_Page},
  3258. { REQ_PREV_PAGE |ID_PN ,PN_Previous_Page},
  3259. { REQ_FIRST_PAGE |ID_PN ,PN_First_Page},
  3260. { REQ_LAST_PAGE |ID_PN ,PN_Last_Page},
  3261. { REQ_NEXT_FIELD |ID_FN ,FN_Next_Field},
  3262. { REQ_PREV_FIELD |ID_FN ,FN_Previous_Field},
  3263. { REQ_FIRST_FIELD |ID_FN ,FN_First_Field},
  3264. { REQ_LAST_FIELD |ID_FN ,FN_Last_Field},
  3265. { REQ_SNEXT_FIELD |ID_FN ,FN_Sorted_Next_Field},
  3266. { REQ_SPREV_FIELD |ID_FN ,FN_Sorted_Previous_Field},
  3267. { REQ_SFIRST_FIELD |ID_FN ,FN_Sorted_First_Field},
  3268. { REQ_SLAST_FIELD |ID_FN ,FN_Sorted_Last_Field},
  3269. { REQ_LEFT_FIELD |ID_FN ,FN_Left_Field},
  3270. { REQ_RIGHT_FIELD |ID_FN ,FN_Right_Field},
  3271. { REQ_UP_FIELD |ID_FN ,FN_Up_Field},
  3272. { REQ_DOWN_FIELD |ID_FN ,FN_Down_Field},
  3273. { REQ_NEXT_CHAR |ID_IFN ,IFN_Next_Character},
  3274. { REQ_PREV_CHAR |ID_IFN ,IFN_Previous_Character},
  3275. { REQ_NEXT_LINE |ID_IFN ,IFN_Next_Line},
  3276. { REQ_PREV_LINE |ID_IFN ,IFN_Previous_Line},
  3277. { REQ_NEXT_WORD |ID_IFN ,IFN_Next_Word},
  3278. { REQ_PREV_WORD |ID_IFN ,IFN_Previous_Word},
  3279. { REQ_BEG_FIELD |ID_IFN ,IFN_Beginning_Of_Field},
  3280. { REQ_END_FIELD |ID_IFN ,IFN_End_Of_Field},
  3281. { REQ_BEG_LINE |ID_IFN ,IFN_Beginning_Of_Line},
  3282. { REQ_END_LINE |ID_IFN ,IFN_End_Of_Line},
  3283. { REQ_LEFT_CHAR |ID_IFN ,IFN_Left_Character},
  3284. { REQ_RIGHT_CHAR |ID_IFN ,IFN_Right_Character},
  3285. { REQ_UP_CHAR |ID_IFN ,IFN_Up_Character},
  3286. { REQ_DOWN_CHAR |ID_IFN ,IFN_Down_Character},
  3287. { REQ_NEW_LINE |ID_FE ,FE_New_Line},
  3288. { REQ_INS_CHAR |ID_FE ,FE_Insert_Character},
  3289. { REQ_INS_LINE |ID_FE ,FE_Insert_Line},
  3290. { REQ_DEL_CHAR |ID_FE ,FE_Delete_Character},
  3291. { REQ_DEL_PREV |ID_FE ,FE_Delete_Previous},
  3292. { REQ_DEL_LINE |ID_FE ,FE_Delete_Line},
  3293. { REQ_DEL_WORD |ID_FE ,FE_Delete_Word},
  3294. { REQ_CLR_EOL |ID_FE ,FE_Clear_To_End_Of_Line},
  3295. { REQ_CLR_EOF |ID_FE ,FE_Clear_To_End_Of_Form},
  3296. { REQ_CLR_FIELD |ID_FE ,FE_Clear_Field},
  3297. { REQ_OVL_MODE |ID_EM ,EM_Overlay_Mode},
  3298. { REQ_INS_MODE |ID_EM ,EM_Insert_Mode},
  3299. { REQ_SCR_FLINE |ID_VSC ,VSC_Scroll_Line_Forward},
  3300. { REQ_SCR_BLINE |ID_VSC ,VSC_Scroll_Line_Backward},
  3301. { REQ_SCR_FPAGE |ID_VSC ,VSC_Scroll_Page_Forward},
  3302. { REQ_SCR_BPAGE |ID_VSC ,VSC_Scroll_Page_Backward},
  3303. { REQ_SCR_FHPAGE |ID_VSC ,VSC_Scroll_Half_Page_Forward},
  3304. { REQ_SCR_BHPAGE |ID_VSC ,VSC_Scroll_Half_Page_Backward},
  3305. { REQ_SCR_FCHAR |ID_HSC ,HSC_Scroll_Char_Forward},
  3306. { REQ_SCR_BCHAR |ID_HSC ,HSC_Scroll_Char_Backward},
  3307. { REQ_SCR_HFLINE |ID_HSC ,HSC_Horizontal_Line_Forward},
  3308. { REQ_SCR_HBLINE |ID_HSC ,HSC_Horizontal_Line_Backward},
  3309. { REQ_SCR_HFHALF |ID_HSC ,HSC_Horizontal_Half_Line_Forward},
  3310. { REQ_SCR_HBHALF |ID_HSC ,HSC_Horizontal_Half_Line_Backward},
  3311. { REQ_VALIDATION |ID_FV ,FV_Validation},
  3312. { REQ_NEXT_CHOICE |ID_CH ,CR_Next_Choice},
  3313. { REQ_PREV_CHOICE |ID_CH ,CR_Previous_Choice}
  3314. };
  3315. /*---------------------------------------------------------------------------
  3316. | Facility : libnform
  3317. | Function : int form_driver(FORM * form,int c)
  3318. |
  3319. | Description : This is the workhorse of the forms system. It checks
  3320. | to determine whether the character c is a request or
  3321. | data. If it is a request, the form driver executes
  3322. | the request and returns the result. If it is data
  3323. | (printable character), it enters the data into the
  3324. | current position in the current field. If it is not
  3325. | recognized, the form driver assumes it is an application
  3326. | defined command and returns E_UNKNOWN_COMMAND.
  3327. | Application defined command should be defined relative
  3328. | to MAX_FORM_COMMAND, the maximum value of a request.
  3329. |
  3330. | Return Values : E_OK - success
  3331. | E_SYSTEM_ERROR - system error
  3332. | E_BAD_ARGUMENT - an argument is incorrect
  3333. | E_NOT_POSTED - form is not posted
  3334. | E_INVALID_FIELD - field contents are invalid
  3335. | E_BAD_STATE - called from inside a hook routine
  3336. | E_REQUEST_DENIED - request failed
  3337. | E_UNKNOWN_COMMAND - command not known
  3338. +--------------------------------------------------------------------------*/
  3339. int form_driver(FORM * form, int c)
  3340. {
  3341. const Binding_Info* BI = (Binding_Info *)0;
  3342. int res = E_UNKNOWN_COMMAND;
  3343. if (!form)
  3344. RETURN(E_BAD_ARGUMENT);
  3345. if (!(form->field))
  3346. RETURN(E_NOT_CONNECTED);
  3347. assert(form->page != 0);
  3348. if (c==FIRST_ACTIVE_MAGIC)
  3349. {
  3350. form->current = _nc_First_Active_Field(form);
  3351. return E_OK;
  3352. }
  3353. assert(form->current &&
  3354. form->current->buf &&
  3355. (form->current->form == form)
  3356. );
  3357. if ( form->status & _IN_DRIVER )
  3358. RETURN(E_BAD_STATE);
  3359. if ( !( form->status & _POSTED ) )
  3360. RETURN(E_NOT_POSTED);
  3361. if ((c>=MIN_FORM_COMMAND && c<=MAX_FORM_COMMAND) &&
  3362. ((bindings[c-MIN_FORM_COMMAND].keycode & Key_Mask) == c))
  3363. BI = &(bindings[c-MIN_FORM_COMMAND]);
  3364. if (BI)
  3365. {
  3366. typedef int (*Generic_Method)(int (* const)(FORM *),FORM *);
  3367. static const Generic_Method Generic_Methods[] =
  3368. {
  3369. Page_Navigation, /* overloaded to call field&form hooks */
  3370. Inter_Field_Navigation, /* overloaded to call field hooks */
  3371. NULL, /* Intra-Field is generic */
  3372. Vertical_Scrolling, /* Overloaded to check multi-line */
  3373. Horizontal_Scrolling, /* Overloaded to check single-line */
  3374. Field_Editing, /* Overloaded to mark modification */
  3375. NULL, /* Edit Mode is generic */
  3376. NULL, /* Field Validation is generic */
  3377. NULL /* Choice Request is generic */
  3378. };
  3379. size_t nMethods = (sizeof(Generic_Methods)/sizeof(Generic_Methods[0]));
  3380. size_t method = ((BI->keycode & ID_Mask) >> ID_Shft) & 0xffff;
  3381. if ( (method >= nMethods) || !(BI->cmd) )
  3382. res = E_SYSTEM_ERROR;
  3383. else
  3384. {
  3385. Generic_Method fct = Generic_Methods[method];
  3386. if (fct)
  3387. res = fct(BI->cmd,form);
  3388. else
  3389. res = (BI->cmd)(form);
  3390. }
  3391. }
  3392. else
  3393. {
  3394. if (!(c & (~(int)MAX_REGULAR_CHARACTER)) &&
  3395. isprint((unsigned char)c) &&
  3396. Check_Char(form->current->type,c,
  3397. (TypeArgument *)(form->current->arg)))
  3398. res = Data_Entry(form,c);
  3399. }
  3400. _nc_Refresh_Current_Field(form);
  3401. RETURN(res);
  3402. }
  3403. /*----------------------------------------------------------------------------
  3404. Field-Buffer manipulation routines.
  3405. The effects of setting a buffer is tightly coupled to the core of the form
  3406. driver logic. This is especially true in the case of growable fields.
  3407. So I don't separate this into an own module.
  3408. --------------------------------------------------------------------------*/
  3409. /*---------------------------------------------------------------------------
  3410. | Facility : libnform
  3411. | Function : int set_field_buffer(FIELD *field,
  3412. | int buffer, char *value)
  3413. |
  3414. | Description : Set the given buffer of the field to the given value.
  3415. | Buffer 0 stores the displayed content of the field.
  3416. | For dynamic fields this may grow the fieldbuffers if
  3417. | the length of the value exceeds the current buffer
  3418. | length. For buffer 0 only printable values are allowed.
  3419. | For static fields, the value needs not to be zero ter-
  3420. | minated. It is copied up to the length of the buffer.
  3421. |
  3422. | Return Values : E_OK - success
  3423. | E_BAD_ARGUMENT - invalid argument
  3424. | E_SYSTEM_ERROR - system error
  3425. +--------------------------------------------------------------------------*/
  3426. int set_field_buffer(FIELD * field, int buffer, const char * value)
  3427. {
  3428. char *s, *p;
  3429. int res = E_OK;
  3430. unsigned int len;
  3431. if ( !field || !value || ((buffer < 0)||(buffer > field->nbuf)) )
  3432. RETURN(E_BAD_ARGUMENT);
  3433. len = Buffer_Length(field);
  3434. if (buffer==0)
  3435. {
  3436. const char *v;
  3437. unsigned int i = 0;
  3438. for(v=value; *v && (i<len); v++,i++)
  3439. {
  3440. if (!isprint((unsigned char)*v))
  3441. RETURN(E_BAD_ARGUMENT);
  3442. }
  3443. }
  3444. if (Growable(field))
  3445. {
  3446. /* for a growable field we must assume zero terminated strings, because
  3447. somehow we have to detect the length of what should be copied.
  3448. */
  3449. unsigned int vlen = strlen(value);
  3450. if (vlen > len)
  3451. {
  3452. if (!Field_Grown(field,
  3453. (int)(1 + (vlen-len)/((field->rows+field->nrow)*field->cols))))
  3454. RETURN(E_SYSTEM_ERROR);
  3455. /* in this case we also have to check, wether or not the remaining
  3456. characters in value are also printable for buffer 0. */
  3457. if (buffer==0)
  3458. {
  3459. unsigned int i;
  3460. for(i=len; i<vlen; i++)
  3461. if (!isprint(value[i]))
  3462. RETURN(E_BAD_ARGUMENT);
  3463. }
  3464. len = vlen;
  3465. }
  3466. }
  3467. p = Address_Of_Nth_Buffer(field,buffer);
  3468. #if HAVE_MEMCCPY
  3469. s = memccpy(p,value,0,len);
  3470. #else
  3471. for(s=(char *)value; *s && (s < (value+len)); s++)
  3472. p[s-value] = *s;
  3473. if (s < (value+len))
  3474. {
  3475. int off = s-value;
  3476. p[off] = *s++;
  3477. s = p + (s-value);
  3478. }
  3479. else
  3480. s=(char *)0;
  3481. #endif
  3482. if (s)
  3483. { /* this means, value was null terminated and not greater than the
  3484. buffer. We have to pad with blanks. Please note that due to memccpy
  3485. logic s points after the terminating null. */
  3486. s--; /* now we point to the terminator. */
  3487. assert(len >= (unsigned int)(s-p));
  3488. if (len > (unsigned int)(s-p))
  3489. memset(s,C_BLANK,len-(unsigned int)(s-p));
  3490. }
  3491. if (buffer==0)
  3492. {
  3493. int syncres;
  3494. if (((syncres=Synchronize_Field( field ))!=E_OK) &&
  3495. (res==E_OK))
  3496. res = syncres;
  3497. if (((syncres=Synchronize_Linked_Fields(field ))!=E_OK) &&
  3498. (res==E_OK))
  3499. res = syncres;
  3500. }
  3501. RETURN(res);
  3502. }
  3503. /*---------------------------------------------------------------------------
  3504. | Facility : libnform
  3505. | Function : char *field_buffer(const FIELD *field,int buffer)
  3506. |
  3507. | Description : Return the address of the buffer for the field.
  3508. |
  3509. | Return Values : Pointer to buffer or NULL if arguments were invalid.
  3510. +--------------------------------------------------------------------------*/
  3511. char *field_buffer(const FIELD * field, int buffer)
  3512. {
  3513. if (field && (buffer >= 0) && (buffer <= field->nbuf))
  3514. return Address_Of_Nth_Buffer(field,buffer);
  3515. else
  3516. return (char *)0;
  3517. }
  3518. /* frm_driver.c ends here */