frm_driver.c 130 KB

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