frm_driver.c 138 KB

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