frm_driver.c 147 KB

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