frm_driver.c 138 KB

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