frm_driver.c 131 KB

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