frm_driver.c 138 KB

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