NCURSES-Programming-HOWTO.html 237 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4. <meta name="generator" content=
  5. "HTML Tidy for Linux/x86 (vers 1st December 2004), see www.w3.org">
  6. <title>NCURSES Programming HOWTO</title>
  7. <meta name="GENERATOR" content=
  8. "Modular DocBook HTML Stylesheet Version 1.7">
  9. </head>
  10. <body class="ARTICLE" bgcolor="#FFFFFF" text="#000000" link=
  11. "#0000FF" vlink="#840084" alink="#0000FF">
  12. <div class="ARTICLE">
  13. <div class="TITLEPAGE">
  14. <h1 class="TITLE"><a name="AEN2" id="AEN2">NCURSES Programming
  15. HOWTO</a></h1>
  16. <h3 class="AUTHOR"><a name="AEN4" id="AEN4">Pradeep Padala</a></h3>
  17. <div class="AFFILIATION">
  18. <div class="ADDRESS">
  19. <p class="ADDRESS"><code class="EMAIL">&lt;<a href=
  20. "mailto:ppadala@gmail.com">ppadala@gmail.com</a>&gt;</code></p>
  21. </div>
  22. </div>
  23. <p class="PUBDATE">v1.9, 2005-06-20<br></p>
  24. <div class="REVHISTORY">
  25. <table width="100%" border="0">
  26. <tr>
  27. <th align="left" valign="top" colspan="3"><b>Revision
  28. History</b></th>
  29. </tr>
  30. <tr>
  31. <td align="left">Revision 1.9</td>
  32. <td align="left">2005-06-20</td>
  33. <td align="left">Revised by: ppadala</td>
  34. </tr>
  35. <tr>
  36. <td align="left" colspan="3">The license has been changed to the
  37. MIT-style license used by NCURSES. Note that the programs are also
  38. re-licensed under this.</td>
  39. </tr>
  40. <tr>
  41. <td align="left">Revision 1.8</td>
  42. <td align="left">2005-06-17</td>
  43. <td align="left">Revised by: ppadala</td>
  44. </tr>
  45. <tr>
  46. <td align="left" colspan="3">Lots of updates. Added references and
  47. perl examples. Changes to examples. Many grammatical and stylistic
  48. changes to the content. Changes to NCURSES history.</td>
  49. </tr>
  50. <tr>
  51. <td align="left">Revision 1.7.1</td>
  52. <td align="left">2002-06-25</td>
  53. <td align="left">Revised by: ppadala</td>
  54. </tr>
  55. <tr>
  56. <td align="left" colspan="3">Added a README file for building and
  57. instructions for building from source.</td>
  58. </tr>
  59. <tr>
  60. <td align="left">Revision 1.7</td>
  61. <td align="left">2002-06-25</td>
  62. <td align="left">Revised by: ppadala</td>
  63. </tr>
  64. <tr>
  65. <td align="left" colspan="3">Added "Other formats" section and made
  66. a lot of fancy changes to the programs. Inlining of programs is
  67. gone.</td>
  68. </tr>
  69. <tr>
  70. <td align="left">Revision 1.6.1</td>
  71. <td align="left">2002-02-24</td>
  72. <td align="left">Revised by: ppadala</td>
  73. </tr>
  74. <tr>
  75. <td align="left" colspan="3">Removed the old Changelog section,
  76. cleaned the makefiles</td>
  77. </tr>
  78. <tr>
  79. <td align="left">Revision 1.6</td>
  80. <td align="left">2002-02-16</td>
  81. <td align="left">Revised by: ppadala</td>
  82. </tr>
  83. <tr>
  84. <td align="left" colspan="3">Corrected a lot of spelling mistakes,
  85. added ACS variables section</td>
  86. </tr>
  87. <tr>
  88. <td align="left">Revision 1.5</td>
  89. <td align="left">2002-01-05</td>
  90. <td align="left">Revised by: ppadala</td>
  91. </tr>
  92. <tr>
  93. <td align="left" colspan="3">Changed structure to present proper
  94. TOC</td>
  95. </tr>
  96. <tr>
  97. <td align="left">Revision 1.3.1</td>
  98. <td align="left">2001-07-26</td>
  99. <td align="left">Revised by: ppadala</td>
  100. </tr>
  101. <tr>
  102. <td align="left" colspan="3">Corrected maintainers paragraph,
  103. Corrected stable release number</td>
  104. </tr>
  105. <tr>
  106. <td align="left">Revision 1.3</td>
  107. <td align="left">2001-07-24</td>
  108. <td align="left">Revised by: ppadala</td>
  109. </tr>
  110. <tr>
  111. <td align="left" colspan="3">Added copyright notices to main
  112. document (LDP license) and programs (GPL), Corrected
  113. printw_example.</td>
  114. </tr>
  115. <tr>
  116. <td align="left">Revision 1.2</td>
  117. <td align="left">2001-06-05</td>
  118. <td align="left">Revised by: ppadala</td>
  119. </tr>
  120. <tr>
  121. <td align="left" colspan="3">Incorporated ravi's changes. Mainly to
  122. introduction, menu, form, justforfun sections</td>
  123. </tr>
  124. <tr>
  125. <td align="left">Revision 1.1</td>
  126. <td align="left">2001-05-22</td>
  127. <td align="left">Revised by: ppadala</td>
  128. </tr>
  129. <tr>
  130. <td align="left" colspan="3">Added "a word about window" section,
  131. Added scanw_example.</td>
  132. </tr>
  133. </table>
  134. </div>
  135. <div>
  136. <div class="ABSTRACT"><a name="AEN67" id="AEN67"></a>
  137. <p><em>This document is intended to be an "All in One" guide for
  138. programming with ncurses and its sister libraries. We graduate from
  139. a simple "Hello World" program to more complex form manipulation.
  140. No prior experience in ncurses is assumed. Send comments to
  141. <a href="mailto:ppadala@gmail.com" target="_top">this
  142. address</a></em></p>
  143. </div>
  144. </div>
  145. <hr></div>
  146. <div class="TOC">
  147. <dl>
  148. <dt><b>Table of Contents</b></dt>
  149. <dt>1. <a href="#INTRO">Introduction</a></dt>
  150. <dd>
  151. <dl>
  152. <dt>1.1. <a href="#WHATIS">What is NCURSES?</a></dt>
  153. <dt>1.2. <a href="#WHATCANWEDO">What we can do with
  154. NCURSES</a></dt>
  155. <dt>1.3. <a href="#WHERETOGETIT">Where to get it</a></dt>
  156. <dt>1.4. <a href="#PURPOSE">Purpose/Scope of the document</a></dt>
  157. <dt>1.5. <a href="#ABOUTPROGRAMS">About the Programs</a></dt>
  158. <dt>1.6. <a href="#OTHERFORMATS">Other Formats of the
  159. document</a></dt>
  160. <dt>1.7. <a href="#CREDITS">Credits</a></dt>
  161. <dt>1.8. <a href="#WISHLIST">Wish List</a></dt>
  162. <dt>1.9. <a href="#COPYRIGHT">Copyright</a></dt>
  163. </dl>
  164. </dd>
  165. <dt>2. <a href="#HELLOWORLD">Hello World !!!</a></dt>
  166. <dd>
  167. <dl>
  168. <dt>2.1. <a href="#COMPILECURSES">Compiling With the NCURSES
  169. Library</a></dt>
  170. <dt>2.2. <a href="#DISSECTION">Dissection</a></dt>
  171. </dl>
  172. </dd>
  173. <dt>3. <a href="#GORY">The Gory Details</a></dt>
  174. <dt>4. <a href="#INIT">Initialization</a></dt>
  175. <dd>
  176. <dl>
  177. <dt>4.1. <a href="#ABOUTINIT">Initialization functions</a></dt>
  178. <dt>4.2. <a href="#RAWCBREAK">raw() and cbreak()</a></dt>
  179. <dt>4.3. <a href="#ECHONOECHO">echo() and noecho()</a></dt>
  180. <dt>4.4. <a href="#KEYPAD">keypad()</a></dt>
  181. <dt>4.5. <a href="#HALFDELAY">halfdelay()</a></dt>
  182. <dt>4.6. <a href="#MISCINIT">Miscellaneous Initialization
  183. functions</a></dt>
  184. <dt>4.7. <a href="#INITEX">An Example</a></dt>
  185. </dl>
  186. </dd>
  187. <dt>5. <a href="#AWORDWINDOWS">A Word about Windows</a></dt>
  188. <dt>6. <a href="#PRINTW">Output functions</a></dt>
  189. <dd>
  190. <dl>
  191. <dt>6.1. <a href="#ADDCHCLASS">addch() class of functions</a></dt>
  192. <dt>6.2. <a href="#AEN298">mvaddch(), waddch() and
  193. mvwaddch()</a></dt>
  194. <dt>6.3. <a href="#PRINTWCLASS">printw() class of
  195. functions</a></dt>
  196. <dt>6.4. <a href="#ADDSTRCLASS">addstr() class of
  197. functions</a></dt>
  198. <dt>6.5. <a href="#ACAUTION">A word of caution</a></dt>
  199. </dl>
  200. </dd>
  201. <dt>7. <a href="#SCANW">Input functions</a></dt>
  202. <dd>
  203. <dl>
  204. <dt>7.1. <a href="#GETCHCLASS">getch() class of functions</a></dt>
  205. <dt>7.2. <a href="#SCANWCLASS">scanw() class of functions</a></dt>
  206. <dt>7.3. <a href="#GETSTRCLASS">getstr() class of
  207. functions</a></dt>
  208. <dt>7.4. <a href="#GETSTREX">Some examples</a></dt>
  209. </dl>
  210. </dd>
  211. <dt>8. <a href="#ATTRIB">Attributes</a></dt>
  212. <dd>
  213. <dl>
  214. <dt>8.1. <a href="#ATTRIBDETAILS">The details</a></dt>
  215. <dt>8.2. <a href="#ATTRONVSATTRSET">attron() vs attrset()</a></dt>
  216. <dt>8.3. <a href="#ATTR_GET">attr_get()</a></dt>
  217. <dt>8.4. <a href="#ATTR_FUNCS">attr_ functions</a></dt>
  218. <dt>8.5. <a href="#WATTRFUNCS">wattr functions</a></dt>
  219. <dt>8.6. <a href="#CHGAT">chgat() functions</a></dt>
  220. </dl>
  221. </dd>
  222. <dt>9. <a href="#WINDOWS">Windows</a></dt>
  223. <dd>
  224. <dl>
  225. <dt>9.1. <a href="#WINDOWBASICS">The basics</a></dt>
  226. <dt>9.2. <a href="#LETBEWINDOW">Let there be a Window !!!</a></dt>
  227. <dt>9.3. <a href="#BORDEREXEXPL">Explanation</a></dt>
  228. <dt>9.4. <a href="#OTHERSTUFF">The other stuff in the
  229. example</a></dt>
  230. <dt>9.5. <a href="#OTHERBORDERFUNCS">Other Border
  231. functions</a></dt>
  232. </dl>
  233. </dd>
  234. <dt>10. <a href="#COLOR">Colors</a></dt>
  235. <dd>
  236. <dl>
  237. <dt>10.1. <a href="#COLORBASICS">The basics</a></dt>
  238. <dt>10.2. <a href="#CHANGECOLORDEFS">Changing Color
  239. Definitions</a></dt>
  240. <dt>10.3. <a href="#COLORCONTENT">Color Content</a></dt>
  241. </dl>
  242. </dd>
  243. <dt>11. <a href="#KEYS">Interfacing with the key board</a></dt>
  244. <dd>
  245. <dl>
  246. <dt>11.1. <a href="#KEYSBASICS">The Basics</a></dt>
  247. <dt>11.2. <a href="#SIMPLEKEYEX">A Simple Key Usage
  248. example</a></dt>
  249. </dl>
  250. </dd>
  251. <dt>12. <a href="#MOUSE">Interfacing with the mouse</a></dt>
  252. <dd>
  253. <dl>
  254. <dt>12.1. <a href="#MOUSEBASICS">The Basics</a></dt>
  255. <dt>12.2. <a href="#GETTINGEVENTS">Getting the events</a></dt>
  256. <dt>12.3. <a href="#MOUSETOGETHER">Putting it all Together</a></dt>
  257. <dt>12.4. <a href="#MISCMOUSEFUNCS">Miscellaneous
  258. Functions</a></dt>
  259. </dl>
  260. </dd>
  261. <dt>13. <a href="#SCREEN">Screen Manipulation</a></dt>
  262. <dd>
  263. <dl>
  264. <dt>13.1. <a href="#GETYX">getyx() functions</a></dt>
  265. <dt>13.2. <a href="#SCREENDUMP">Screen Dumping</a></dt>
  266. <dt>13.3. <a href="#WINDOWDUMP">Window Dumping</a></dt>
  267. </dl>
  268. </dd>
  269. <dt>14. <a href="#MISC">Miscellaneous features</a></dt>
  270. <dd>
  271. <dl>
  272. <dt>14.1. <a href="#CURSSET">curs_set()</a></dt>
  273. <dt>14.2. <a href="#TEMPLEAVE">Temporarily Leaving Curses
  274. mode</a></dt>
  275. <dt>14.3. <a href="#ACSVARS">ACS_ variables</a></dt>
  276. </dl>
  277. </dd>
  278. <dt>15. <a href="#OTHERLIB">Other libraries</a></dt>
  279. <dt>16. <a href="#PANELS">Panel Library</a></dt>
  280. <dd>
  281. <dl>
  282. <dt>16.1. <a href="#PANELBASICS">The Basics</a></dt>
  283. <dt>16.2. <a href="#COMPILEPANELS">Compiling With the Panels
  284. Library</a></dt>
  285. <dt>16.3. <a href="#PANELBROWSING">Panel Window Browsing</a></dt>
  286. <dt>16.4. <a href="#USERPTRUSING">Using User Pointers</a></dt>
  287. <dt>16.5. <a href="#PANELMOVERESIZE">Moving and Resizing
  288. Panels</a></dt>
  289. <dt>16.6. <a href="#PANELSHOWHIDE">Hiding and Showing
  290. Panels</a></dt>
  291. <dt>16.7. <a href="#PANELABOVE">panel_above() and panel_below()
  292. Functions</a></dt>
  293. </dl>
  294. </dd>
  295. <dt>17. <a href="#MENUS">Menus Library</a></dt>
  296. <dd>
  297. <dl>
  298. <dt>17.1. <a href="#MENUBASICS">The Basics</a></dt>
  299. <dt>17.2. <a href="#COMPILEMENUS">Compiling With the Menu
  300. Library</a></dt>
  301. <dt>17.3. <a href="#MENUDRIVER">Menu Driver: The work horse of the
  302. menu system</a></dt>
  303. <dt>17.4. <a href="#MENUWINDOWS">Menu Windows</a></dt>
  304. <dt>17.5. <a href="#SCROLLMENUS">Scrolling Menus</a></dt>
  305. <dt>17.6. <a href="#MULTICOLUMN">Multi Columnar Menus</a></dt>
  306. <dt>17.7. <a href="#MULTIVALUEMENUS">Multi Valued Menus</a></dt>
  307. <dt>17.8. <a href="#MENUOPT">Menu Options</a></dt>
  308. <dt>17.9. <a href="#MENUUSERPTR">The useful User Pointer</a></dt>
  309. </dl>
  310. </dd>
  311. <dt>18. <a href="#FORMS">Forms Library</a></dt>
  312. <dd>
  313. <dl>
  314. <dt>18.1. <a href="#FORMBASICS">The Basics</a></dt>
  315. <dt>18.2. <a href="#COMPILEFORMS">Compiling With the Forms
  316. Library</a></dt>
  317. <dt>18.3. <a href="#PLAYFIELDS">Playing with Fields</a></dt>
  318. <dt>18.4. <a href="#FORMWINDOWS">Form Windows</a></dt>
  319. <dt>18.5. <a href="#FILEDVALIDATE">Field Validation</a></dt>
  320. <dt>18.6. <a href="#FORMDRIVER">Form Driver: The work horse of the
  321. forms system</a></dt>
  322. </dl>
  323. </dd>
  324. <dt>19. <a href="#TOOLS">Tools and Widget Libraries</a></dt>
  325. <dd>
  326. <dl>
  327. <dt>19.1. <a href="#CDK">CDK (Curses Development Kit)</a></dt>
  328. <dt>19.2. <a href="#DIALOG">The dialog</a></dt>
  329. <dt>19.3. <a href="#PERLCURSES">Perl Curses Modules CURSES::FORM
  330. and CURSES::WIDGETS</a></dt>
  331. </dl>
  332. </dd>
  333. <dt>20. <a href="#JUSTFORFUN">Just For Fun !!!</a></dt>
  334. <dd>
  335. <dl>
  336. <dt>20.1. <a href="#GAMEOFLIFE">The Game of Life</a></dt>
  337. <dt>20.2. <a href="#MAGIC">Magic Square</a></dt>
  338. <dt>20.3. <a href="#HANOI">Towers of Hanoi</a></dt>
  339. <dt>20.4. <a href="#QUEENS">Queens Puzzle</a></dt>
  340. <dt>20.5. <a href="#SHUFFLE">Shuffle</a></dt>
  341. <dt>20.6. <a href="#TT">Typing Tutor</a></dt>
  342. </dl>
  343. </dd>
  344. <dt>21. <a href="#REF">References</a></dt>
  345. </dl>
  346. </div>
  347. <div class="SECT1">
  348. <h2 class="SECT1"><a name="INTRO" id="INTRO">1.
  349. Introduction</a></h2>
  350. <p>In the olden days of teletype terminals, terminals were away
  351. from computers and were connected to them through serial cables.
  352. The terminals could be configured by sending a series of bytes. All
  353. the capabilities (such as moving the cursor to a new location,
  354. erasing part of the screen, scrolling the screen, changing modes
  355. etc.) of terminals could be accessed through these series of bytes.
  356. These control seeuqnces are usually called escape sequences,
  357. because they start with an escape(0x1B) character. Even today, with
  358. proper emulation, we can send escape sequences to the emulator and
  359. achieve the same effect on a terminal window.</p>
  360. <p>Suppose you wanted to print a line in color. Try typing this on
  361. your console.</p>
  362. <table border="0" bgcolor="#E0E0E0" width="100%">
  363. <tr>
  364. <td>
  365. <pre class="PROGRAMLISTING">
  366. <font color="#000000">echo "^[[0;31;40mIn Color"</font>
  367. </pre></td>
  368. </tr>
  369. </table>
  370. <p>The first character is an escape character, which looks like two
  371. characters ^ and [. To be able to print it, you have to press
  372. CTRL+V and then the ESC key. All the others are normal printable
  373. characters. You should be able to see the string "In Color" in red.
  374. It stays that way and to revert back to the original mode type
  375. this.</p>
  376. <table border="0" bgcolor="#E0E0E0" width="100%">
  377. <tr>
  378. <td>
  379. <pre class="PROGRAMLISTING">
  380. <font color="#000000">echo "^[[0;37;40m"</font>
  381. </pre></td>
  382. </tr>
  383. </table>
  384. <p>Now, what do these magic characters mean? Difficult to
  385. comprehend? They might even be different for different terminals.
  386. So the designers of UNIX invented a mechanism named <var class=
  387. "LITERAL">termcap</var>. It is a file that lists all the
  388. capabilities of a particular terminal, along with the escape
  389. sequences needed to achieve a particular effect. In the later
  390. years, this was replaced by <var class="LITERAL">terminfo</var>.
  391. Without delving too much into details, this mechanism allows
  392. application programs to query the terminfo database and obtain the
  393. control characters to be sent to a terminal or terminal
  394. emulator.</p>
  395. <div class="SECT2">
  396. <hr>
  397. <h3 class="SECT2"><a name="WHATIS" id="WHATIS">1.1. What is
  398. NCURSES?</a></h3>
  399. <p>You might be wondering, what the import of all this technical
  400. gibberish is. In the above scenario, every application program is
  401. supposed to query the terminfo and perform the necessary stuff
  402. (sending control characters etc.). It soon became difficult to
  403. manage this complexity and this gave birth to 'CURSES'. Curses is a
  404. pun on the name "cursor optimization". The Curses library forms a
  405. wrapper over working with raw terminal codes, and provides highly
  406. flexible and efficient API (Application Programming Interface). It
  407. provides functions to move the cursor, create windows, produce
  408. colors, play with mouse etc. The application programs need not
  409. worry about the underlying terminal capabilities.</p>
  410. <p>So what is NCURSES? NCURSES is a clone of the original System V
  411. Release 4.0 (SVr4) curses. It is a freely distributable library,
  412. fully compatible with older version of curses. In short, it is a
  413. library of functions that manages an application's display on
  414. character-cell terminals. In the remainder of the document, the
  415. terms curses and ncurses are used interchangeably.</p>
  416. <p>A detailed history of NCURSES can be found in the NEWS file from
  417. the source distribution. The current package is maintained by
  418. <a href="mailto:dickey@his.com" target="_top">Thomas Dickey</a>.
  419. You can contact the maintainers at <a href=
  420. "mailto:bug-ncurses@gnu.org" target=
  421. "_top">bug-ncurses@gnu.org</a>.</p>
  422. </div>
  423. <div class="SECT2">
  424. <hr>
  425. <h3 class="SECT2"><a name="WHATCANWEDO" id="WHATCANWEDO">1.2. What
  426. we can do with NCURSES</a></h3>
  427. <p>NCURSES not only creates a wrapper over terminal capabilities,
  428. but also gives a robust framework to create nice looking UI (User
  429. Interface)s in text mode. It provides functions to create windows
  430. etc. Its sister libraries panel, menu and form provide an extension
  431. to the basic curses library. These libraries usually come along
  432. with curses. One can create applications that contain multiple
  433. windows, menus, panels and forms. Windows can be managed
  434. independently, can provide 'scrollability' and even can be
  435. hidden.</p>
  436. <p>Menus provide the user with an easy command selection option.
  437. Forms allow the creation of easy-to-use data entry and display
  438. windows. Panels extend the capabilities of ncurses to deal with
  439. overlapping and stacked windows.</p>
  440. <p>These are just some of the basic things we can do with ncurses.
  441. As we move along, We will see all the capabilities of these
  442. libraries.</p>
  443. </div>
  444. <div class="SECT2">
  445. <hr>
  446. <h3 class="SECT2"><a name="WHERETOGETIT" id="WHERETOGETIT">1.3.
  447. Where to get it</a></h3>
  448. <p>All right, now that you know what you can do with ncurses, you
  449. must be rearing to get started. NCURSES is usually shipped with
  450. your installation. In case you don't have the library or want to
  451. compile it on your own, read on.</p>
  452. <p><em>Compiling the package</em></p>
  453. <p>NCURSES can be obtained from <a href=
  454. "ftp://ftp.gnu.org/pub/gnu/ncurses/ncurses.tar.gz" target=
  455. "_top">ftp://ftp.gnu.org/pub/gnu/ncurses/ncurses.tar.gz</a> or any
  456. of the ftp sites mentioned in <a href=
  457. "http://www.gnu.org/order/ftp.html" target=
  458. "_top">http://www.gnu.org/order/ftp.html</a>.</p>
  459. <p>Read the README and INSTALL files for details on to how to
  460. install it. It usually involves the following operations.</p>
  461. <table border="0" bgcolor="#E0E0E0" width="100%">
  462. <tr>
  463. <td>
  464. <pre class="PROGRAMLISTING">
  465. <font color=
  466. "#000000"> tar zxvf ncurses&lt;version&gt;.tar.gz # unzip and untar the archive
  467. cd ncurses&lt;version&gt; # cd to the directory
  468. ./configure # configure the build according to your
  469. # environment
  470. make # make it
  471. su root # become root
  472. make install # install it</font>
  473. </pre></td>
  474. </tr>
  475. </table>
  476. <p><em>Using the RPM</em></p>
  477. <p>NCURSES RPM can be found and downloaded from <a href=
  478. "http://rpmfind.net" target="_top">http://rpmfind.net</a> . The RPM
  479. can be installed with the following command after becoming
  480. root.</p>
  481. <table border="0" bgcolor="#E0E0E0" width="100%">
  482. <tr>
  483. <td>
  484. <pre class="PROGRAMLISTING">
  485. <font color="#000000"> rpm -i &lt;downloaded rpm&gt;</font>
  486. </pre></td>
  487. </tr>
  488. </table>
  489. </div>
  490. <div class="SECT2">
  491. <hr>
  492. <h3 class="SECT2"><a name="PURPOSE" id="PURPOSE">1.4. Purpose/Scope
  493. of the document</a></h3>
  494. <p>This document is intended to be a "All in One" guide for
  495. programming with ncurses and its sister libraries. We graduate from
  496. a simple "Hello World" program to more complex form manipulation.
  497. No prior experience in ncurses is assumed. The writing is informal,
  498. but a lot of detail is provided for each of the examples.</p>
  499. </div>
  500. <div class="SECT2">
  501. <hr>
  502. <h3 class="SECT2"><a name="ABOUTPROGRAMS" id="ABOUTPROGRAMS">1.5.
  503. About the Programs</a></h3>
  504. <p>All the programs in the document are available in zipped form
  505. <a href=
  506. "http://www.tldp.org/HOWTO/NCURSES-Programming-HOWTO/ncurses_programs.tar.gz"
  507. target="_top">here</a>. Unzip and untar it. The directory structure
  508. looks like this.</p>
  509. <table border="0" bgcolor="#E0E0E0" width="100%">
  510. <tr>
  511. <td>
  512. <pre class="PROGRAMLISTING">
  513. <font color="#000000">ncurses
  514. |
  515. |----&gt; JustForFun -- just for fun programs
  516. |----&gt; basics -- basic programs
  517. |----&gt; demo -- output files go into this directory after make
  518. | |
  519. | |----&gt; exe -- exe files of all example programs
  520. |----&gt; forms -- programs related to form library
  521. |----&gt; menus -- programs related to menus library
  522. |----&gt; panels -- programs related to panels library
  523. |----&gt; perl -- perl equivalents of the examples (contributed
  524. | by Anuradha Ratnaweera)
  525. |----&gt; Makefile -- the top level Makefile
  526. |----&gt; README -- the top level README file. contains instructions
  527. |----&gt; COPYING -- copyright notice</font>
  528. </pre></td>
  529. </tr>
  530. </table>
  531. <p>The individual directories contain the following files.</p>
  532. <table border="0" bgcolor="#E0E0E0" width="100%">
  533. <tr>
  534. <td>
  535. <pre class="PROGRAMLISTING">
  536. <font color="#000000">Description of files in each directory
  537. --------------------------------------
  538. JustForFun
  539. |
  540. |----&gt; hanoi.c -- The Towers of Hanoi Solver
  541. |----&gt; life.c -- The Game of Life demo
  542. |----&gt; magic.c -- An Odd Order Magic Square builder
  543. |----&gt; queens.c -- The famous N-Queens Solver
  544. |----&gt; shuffle.c -- A fun game, if you have time to kill
  545. |----&gt; tt.c -- A very trivial typing tutor
  546. basics
  547. |
  548. |----&gt; acs_vars.c -- ACS_ variables example
  549. |----&gt; hello_world.c -- Simple "Hello World" Program
  550. |----&gt; init_func_example.c -- Initialization functions example
  551. |----&gt; key_code.c -- Shows the scan code of the key pressed
  552. |----&gt; mouse_menu.c -- A menu accessible by mouse
  553. |----&gt; other_border.c -- Shows usage of other border functions apa
  554. | -- rt from box()
  555. |----&gt; printw_example.c -- A very simple printw() example
  556. |----&gt; scanw_example.c -- A very simple getstr() example
  557. |----&gt; simple_attr.c -- A program that can print a c file with
  558. | -- comments in attribute
  559. |----&gt; simple_color.c -- A simple example demonstrating colors
  560. |----&gt; simple_key.c -- A menu accessible with keyboard UP, DOWN
  561. | -- arrows
  562. |----&gt; temp_leave.c -- Demonstrates temporarily leaving curses mode
  563. |----&gt; win_border.c -- Shows Creation of windows and borders
  564. |----&gt; with_chgat.c -- chgat() usage example
  565. forms
  566. |
  567. |----&gt; form_attrib.c -- Usage of field attributes
  568. |----&gt; form_options.c -- Usage of field options
  569. |----&gt; form_simple.c -- A simple form example
  570. |----&gt; form_win.c -- Demo of windows associated with forms
  571. menus
  572. |
  573. |----&gt; menu_attrib.c -- Usage of menu attributes
  574. |----&gt; menu_item_data.c -- Usage of item_name() etc.. functions
  575. |----&gt; menu_multi_column.c -- Creates multi columnar menus
  576. |----&gt; menu_scroll.c -- Demonstrates scrolling capability of menus
  577. |----&gt; menu_simple.c -- A simple menu accessed by arrow keys
  578. |----&gt; menu_toggle.c -- Creates multi valued menus and explains
  579. | -- REQ_TOGGLE_ITEM
  580. |----&gt; menu_userptr.c -- Usage of user pointer
  581. |----&gt; menu_win.c -- Demo of windows associated with menus
  582. panels
  583. |
  584. |----&gt; panel_browse.c -- Panel browsing through tab. Usage of user
  585. | -- pointer
  586. |----&gt; panel_hide.c -- Hiding and Un hiding of panels
  587. |----&gt; panel_resize.c -- Moving and resizing of panels
  588. |----&gt; panel_simple.c -- A simple panel example
  589. perl
  590. |----&gt; 01-10.pl -- Perl equivalents of first ten example programs</font>
  591. </pre></td>
  592. </tr>
  593. </table>
  594. <p>There is a top level Makefile included in the main directory. It
  595. builds all the files and puts the ready-to-use exes in demo/exe
  596. directory. You can also do selective make by going into the
  597. corresponding directory. Each directory contains a README file
  598. explaining the purpose of each c file in the directory.</p>
  599. <p>For every example, I have included path name for the file
  600. relative to the examples directory.</p>
  601. <p>If you prefer browsing individual programs, point your browser
  602. to <a href=
  603. "http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/ncurses_programs/"
  604. target=
  605. "_top">http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/ncurses_programs/</a></p>
  606. <p>All the programs are released under the same license that is
  607. used by ncurses (MIT-style). This gives you the ability to do
  608. pretty much anything other than claiming them as yours. Feel free
  609. to use them in your programs as appropriate.</p>
  610. </div>
  611. <div class="SECT2">
  612. <hr>
  613. <h3 class="SECT2"><a name="OTHERFORMATS" id="OTHERFORMATS">1.6.
  614. Other Formats of the document</a></h3>
  615. <p>This howto is also availabe in various other formats on the
  616. tldp.org site. Here are the links to other formats of this
  617. document.</p>
  618. <div class="SECT3">
  619. <hr>
  620. <h4 class="SECT3"><a name="LISTFORMATS" id="LISTFORMATS">1.6.1.
  621. Readily available formats from tldp.org</a></h4>
  622. <ul>
  623. <li>
  624. <p><a href=
  625. "http://www.ibiblio.org/pub/Linux/docs/HOWTO/other-formats/pdf/NCURSES-Programming-HOWTO.pdf"
  626. target="_top">Acrobat PDF Format</a></p>
  627. </li>
  628. <li>
  629. <p><a href=
  630. "http://www.ibiblio.org/pub/Linux/docs/HOWTO/other-formats/ps/NCURSES-Programming-HOWTO.ps.gz"
  631. target="_top">PostScript Format</a></p>
  632. </li>
  633. <li>
  634. <p><a href=
  635. "http://www.ibiblio.org/pub/Linux/docs/HOWTO/other-formats/html/NCURSES-Programming-HOWTO-html.tar.gz"
  636. target="_top">In Multiple HTML pages</a></p>
  637. </li>
  638. <li>
  639. <p><a href=
  640. "http://www.ibiblio.org/pub/Linux/docs/HOWTO/other-formats/html_single/NCURSES-Programming-HOWTO.html"
  641. target="_top">In One big HTML format</a></p>
  642. </li>
  643. </ul>
  644. </div>
  645. <div class="SECT3">
  646. <hr>
  647. <h4 class="SECT3"><a name="BUILDSOURCE" id="BUILDSOURCE">1.6.2.
  648. Building from source</a></h4>
  649. <p>If above links are broken or if you want to experiment with sgml
  650. read on.</p>
  651. <table border="0" bgcolor="#E0E0E0" width="100%">
  652. <tr>
  653. <td>
  654. <pre class="PROGRAMLISTING">
  655. <font color=
  656. "#000000">&#13; Get both the source and the tar,gzipped programs, available at
  657. http://cvsview.tldp.org/index.cgi/LDP/howto/docbook/
  658. NCURSES-HOWTO/NCURSES-Programming-HOWTO.sgml
  659. http://cvsview.tldp.org/index.cgi/LDP/howto/docbook/
  660. NCURSES-HOWTO/ncurses_programs.tar.gz
  661. Unzip ncurses_programs.tar.gz with
  662. tar zxvf ncurses_programs.tar.gz
  663. Use jade to create various formats. For example if you just want to create
  664. the multiple html files, you would use
  665. jade -t sgml -i html -d &lt;path to docbook html stylesheet&gt;
  666. NCURSES-Programming-HOWTO.sgml
  667. to get pdf, first create a single html file of the HOWTO with
  668. jade -t sgml -i html -d &lt;path to docbook html stylesheet&gt; -V nochunks
  669. NCURSES-Programming-HOWTO.sgml &gt; NCURSES-ONE-BIG-FILE.html
  670. then use htmldoc to get pdf file with
  671. htmldoc --size universal -t pdf --firstpage p1 -f &lt;output file name.pdf&gt;
  672. NCURSES-ONE-BIG-FILE.html
  673. for ps, you would use
  674. htmldoc --size universal -t ps --firstpage p1 -f &lt;output file name.ps&gt;
  675. NCURSES-ONE-BIG-FILE.html</font>
  676. </pre></td>
  677. </tr>
  678. </table>
  679. <p>See <a href="http://www.tldp.org/LDP/LDP-Author-Guide/" target=
  680. "_top">LDP Author guide</a> for more details. If all else failes,
  681. mail me at <a href="ppadala@gmail.com" target=
  682. "_top">ppadala@gmail.com</a></p>
  683. </div>
  684. </div>
  685. <div class="SECT2">
  686. <hr>
  687. <h3 class="SECT2"><a name="CREDITS" id="CREDITS">1.7.
  688. Credits</a></h3>
  689. <p>I thank <a href="mailto:sharath_1@usa.net" target=
  690. "_top">Sharath</a> and Emre Akbas for helping me with few sections.
  691. The introduction was initially written by sharath. I rewrote it
  692. with few excerpts taken from his initial work. Emre helped in
  693. writing printw and scanw sections.</p>
  694. <p>Perl equivalents of the example programs are contributed by
  695. <a href="mailto:Aratnaweera@virtusa.com" target="_top">Anuradha
  696. Ratnaweera</a>.</p>
  697. <p>Then comes <a href="mailto:parimi@ece.arizona.edu" target=
  698. "_top">Ravi Parimi</a>, my dearest friend, who has been on this
  699. project before even one line was written. He constantly bombarded
  700. me with suggestions and patiently reviewed the whole text. He also
  701. checked each program on Linux and Solaris.</p>
  702. </div>
  703. <div class="SECT2">
  704. <hr>
  705. <h3 class="SECT2"><a name="WISHLIST" id="WISHLIST">1.8. Wish
  706. List</a></h3>
  707. <p>This is the wish list, in the order of priority. If you have a
  708. wish or you want to work on completing the wish, mail <a href=
  709. "mailto:ppadala@gmail.com" target="_top">me</a>.</p>
  710. <ul>
  711. <li>
  712. <p>Add examples to last parts of forms section.</p>
  713. </li>
  714. <li>
  715. <p>Prepare a Demo showing all the programs and allow the user to
  716. browse through description of each program. Let the user compile
  717. and see the program in action. A dialog based interface is
  718. preferred.</p>
  719. </li>
  720. <li>
  721. <p>Add debug info. _tracef, _tracemouse stuff.</p>
  722. </li>
  723. <li>
  724. <p>Accessing termcap, terminfo using functions provided by ncurses
  725. package.</p>
  726. </li>
  727. <li>
  728. <p>Working on two terminals simultaneously.</p>
  729. </li>
  730. <li>
  731. <p>Add more stuff to miscellaneous section.</p>
  732. </li>
  733. </ul>
  734. </div>
  735. <div class="SECT2">
  736. <hr>
  737. <h3 class="SECT2"><a name="COPYRIGHT" id="COPYRIGHT">1.9.
  738. Copyright</a></h3>
  739. <p>Copyright &copy; 2001 by Pradeep Padala.</p>
  740. <p>Permission is hereby granted, free of charge, to any person
  741. obtaining a copy of this software and associated documentation
  742. files (the "Software"), to deal in the Software without
  743. restriction, including without limitation the rights to use, copy,
  744. modify, merge, publish, distribute, distribute with modifications,
  745. sublicense, and/or sell copies of the Software, and to permit
  746. persons to whom the Software is furnished to do so, subject to the
  747. following conditions:</p>
  748. <p>The above copyright notice and this permission notice shall be
  749. included in all copies or substantial portions of the Software.</p>
  750. <p>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  751. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  752. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  753. NONINFRINGEMENT. IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE
  754. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  755. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  756. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  757. SOFTWARE.</p>
  758. <p>Except as contained in this notice, the name(s) of the above
  759. copyright holders shall not be used in advertising or otherwise to
  760. promote the sale, use or other dealings in this Software without
  761. prior written authorization.</p>
  762. </div>
  763. </div>
  764. <div class="SECT1">
  765. <hr>
  766. <h2 class="SECT1"><a name="HELLOWORLD" id="HELLOWORLD">2. Hello
  767. World !!!</a></h2>
  768. <p>Welcome to the world of curses. Before we plunge into the
  769. library and look into its various features, let's write a simple
  770. program and say hello to the world.</p>
  771. <div class="SECT2">
  772. <hr>
  773. <h3 class="SECT2"><a name="COMPILECURSES" id="COMPILECURSES">2.1.
  774. Compiling With the NCURSES Library</a></h3>
  775. <p>To use ncurses library functions, you have to include ncurses.h
  776. in your programs. To link the program with ncurses the flag
  777. -lncurses should be added.</p>
  778. <table border="0" bgcolor="#E0E0E0" width="100%">
  779. <tr>
  780. <td>
  781. <pre class="PROGRAMLISTING">
  782. <font color="#000000"> #include &lt;ncurses.h&gt;
  783. .
  784. .
  785. .
  786. compile and link: gcc &lt;program file&gt; -lncurses</font>
  787. </pre></td>
  788. </tr>
  789. </table>
  790. <div class="EXAMPLE"><a name="BHW" id="BHW"></a>
  791. <p><b>Example 1. The Hello World !!! Program</b></p>
  792. <table border="0" bgcolor="#E0E0E0" width="100%">
  793. <tr>
  794. <td>
  795. <pre class="PROGRAMLISTING">
  796. <font color="#000000"><span class=
  797. "INLINEMEDIAOBJECT">#include &lt;ncurses.h&gt;
  798. int main()
  799. {
  800. initscr(); /* Start curses mode */
  801. printw("Hello World !!!"); /* Print Hello World */
  802. refresh(); /* Print it on to the real screen */
  803. getch(); /* Wait for user input */
  804. endwin(); /* End curses mode */
  805. return 0;
  806. }</span></font>
  807. </pre></td>
  808. </tr>
  809. </table>
  810. </div>
  811. </div>
  812. <div class="SECT2">
  813. <hr>
  814. <h3 class="SECT2"><a name="DISSECTION" id="DISSECTION">2.2.
  815. Dissection</a></h3>
  816. <p>The above program prints "Hello World !!!" to the screen and
  817. exits. This program shows how to initialize curses and do screen
  818. manipulation and end curses mode. Let's dissect it line by
  819. line.</p>
  820. <div class="SECT3">
  821. <hr>
  822. <h4 class="SECT3"><a name="ABOUT-INITSCR" id="ABOUT-INITSCR">2.2.1.
  823. About initscr()</a></h4>
  824. <p>The function initscr() initializes the terminal in curses mode.
  825. In some implementations, it clears the screen and presents a blank
  826. screen. To do any screen manipulation using curses package this has
  827. to be called first. This function initializes the curses system and
  828. allocates memory for our present window (called <var class=
  829. "LITERAL">stdscr</var>) and some other data-structures. Under
  830. extreme cases this function might fail due to insufficient memory
  831. to allocate memory for curses library's data structures.</p>
  832. <p>After this is done, we can do a variety of initializations to
  833. customize our curses settings. These details will be explained
  834. <a href="#INIT">later</a> .</p>
  835. </div>
  836. <div class="SECT3">
  837. <hr>
  838. <h4 class="SECT3"><a name="MYST-REFRESH" id="MYST-REFRESH">2.2.2.
  839. The mysterious refresh()</a></h4>
  840. <p>The next line printw prints the string "Hello World !!!" on to
  841. the screen. This function is analogous to normal printf in all
  842. respects except that it prints the data on a window called stdscr
  843. at the current (y,x) co-ordinates. Since our present co-ordinates
  844. are at 0,0 the string is printed at the left hand corner of the
  845. window.</p>
  846. <p>This brings us to that mysterious refresh(). Well, when we
  847. called printw the data is actually written to an imaginary window,
  848. which is not updated on the screen yet. The job of printw is to
  849. update a few flags and data structures and write the data to a
  850. buffer corresponding to stdscr. In order to show it on the screen,
  851. we need to call refresh() and tell the curses system to dump the
  852. contents on the screen.</p>
  853. <p>The philosophy behind all this is to allow the programmer to do
  854. multiple updates on the imaginary screen or windows and do a
  855. refresh once all his screen update is done. refresh() checks the
  856. window and updates only the portion which has been changed. This
  857. improves performance and offers greater flexibility too. But, it is
  858. sometimes frustrating to beginners. A common mistake committed by
  859. beginners is to forget to call refresh() after they did some update
  860. through printw() class of functions. I still forget to add it
  861. sometimes :-)</p>
  862. </div>
  863. <div class="SECT3">
  864. <hr>
  865. <h4 class="SECT3"><a name="ABOUT-ENDWIN" id="ABOUT-ENDWIN">2.2.3.
  866. About endwin()</a></h4>
  867. <p>And finally don't forget to end the curses mode. Otherwise your
  868. terminal might behave strangely after the program quits. endwin()
  869. frees the memory taken by curses sub-system and its data structures
  870. and puts the terminal in normal mode. This function must be called
  871. after you are done with the curses mode.</p>
  872. </div>
  873. </div>
  874. </div>
  875. <div class="SECT1">
  876. <hr>
  877. <h2 class="SECT1"><a name="GORY" id="GORY">3. The Gory
  878. Details</a></h2>
  879. <p>Now that we have seen how to write a simple curses program let's
  880. get into the details. There are many functions that help customize
  881. what you see on screen and many features which can be put to full
  882. use.</p>
  883. <p>Here we go...</p>
  884. </div>
  885. <div class="SECT1">
  886. <hr>
  887. <h2 class="SECT1"><a name="INIT" id="INIT">4.
  888. Initialization</a></h2>
  889. <p>We now know that to initialize curses system the function
  890. initscr() has to be called. There are functions which can be called
  891. after this initialization to customize our curses session. We may
  892. ask the curses system to set the terminal in raw mode or initialize
  893. color or initialize the mouse etc.. Let's discuss some of the
  894. functions that are normally called immediately after initscr();</p>
  895. <div class="SECT2">
  896. <hr>
  897. <h3 class="SECT2"><a name="ABOUTINIT" id="ABOUTINIT">4.1.
  898. Initialization functions</a></h3>
  899. </div>
  900. <div class="SECT2">
  901. <hr>
  902. <h3 class="SECT2"><a name="RAWCBREAK" id="RAWCBREAK">4.2. raw() and
  903. cbreak()</a></h3>
  904. <p>Normally the terminal driver buffers the characters a user types
  905. until a new line or carriage return is encountered. But most
  906. programs require that the characters be available as soon as the
  907. user types them. The above two functions are used to disable line
  908. buffering. The difference between these two functions is in the way
  909. control characters like suspend (CTRL-Z), interrupt and quit
  910. (CTRL-C) are passed to the program. In the raw() mode these
  911. characters are directly passed to the program without generating a
  912. signal. In the <var class="LITERAL">cbreak()</var> mode these
  913. control characters are interpreted as any other character by the
  914. terminal driver. I personally prefer to use raw() as I can exercise
  915. greater control over what the user does.</p>
  916. </div>
  917. <div class="SECT2">
  918. <hr>
  919. <h3 class="SECT2"><a name="ECHONOECHO" id="ECHONOECHO">4.3. echo()
  920. and noecho()</a></h3>
  921. <p>These functions control the echoing of characters typed by the
  922. user to the terminal. <var class="LITERAL">noecho()</var> switches
  923. off echoing. The reason you might want to do this is to gain more
  924. control over echoing or to suppress unnecessary echoing while
  925. taking input from the user through the getch() etc. functions. Most
  926. of the interactive programs call <var class=
  927. "LITERAL">noecho()</var> at initialization and do the echoing of
  928. characters in a controlled manner. It gives the programmer the
  929. flexibility of echoing characters at any place in the window
  930. without updating current (y,x) co-ordinates.</p>
  931. </div>
  932. <div class="SECT2">
  933. <hr>
  934. <h3 class="SECT2"><a name="KEYPAD" id="KEYPAD">4.4.
  935. keypad()</a></h3>
  936. <p>This is my favorite initialization function. It enables the
  937. reading of function keys like F1, F2, arrow keys etc. Almost every
  938. interactive program enables this, as arrow keys are a major part of
  939. any User Interface. Do <var class="LITERAL">keypad(stdscr,
  940. TRUE)</var> to enable this feature for the regular screen (stdscr).
  941. You will learn more about key management in later sections of this
  942. document.</p>
  943. </div>
  944. <div class="SECT2">
  945. <hr>
  946. <h3 class="SECT2"><a name="HALFDELAY" id="HALFDELAY">4.5.
  947. halfdelay()</a></h3>
  948. <p>This function, though not used very often, is a useful one at
  949. times. halfdelay()is called to enable the half-delay mode, which is
  950. similar to the cbreak() mode in that characters typed are
  951. immediately available to program. However, it waits for 'X' tenths
  952. of a second for input and then returns ERR, if no input is
  953. available. 'X' is the timeout value passed to the function
  954. halfdelay(). This function is useful when you want to ask the user
  955. for input, and if he doesn't respond with in certain time, we can
  956. do some thing else. One possible example is a timeout at the
  957. password prompt.</p>
  958. </div>
  959. <div class="SECT2">
  960. <hr>
  961. <h3 class="SECT2"><a name="MISCINIT" id="MISCINIT">4.6.
  962. Miscellaneous Initialization functions</a></h3>
  963. <p>There are few more functions which are called at initialization
  964. to customize curses behavior. They are not used as extensively as
  965. those mentioned above. Some of them are explained where
  966. appropriate.</p>
  967. </div>
  968. <div class="SECT2">
  969. <hr>
  970. <h3 class="SECT2"><a name="INITEX" id="INITEX">4.7. An
  971. Example</a></h3>
  972. <p>Let's write a program which will clarify the usage of these
  973. functions.</p>
  974. <div class="EXAMPLE"><a name="BINFU" id="BINFU"></a>
  975. <p><b>Example 2. Initialization Function Usage example</b></p>
  976. <table border="0" bgcolor="#E0E0E0" width="100%">
  977. <tr>
  978. <td>
  979. <pre class="PROGRAMLISTING">
  980. <font color="#000000"><span class=
  981. "INLINEMEDIAOBJECT">#include &lt;ncurses.h&gt;
  982. int main()
  983. { int ch;
  984. initscr(); /* Start curses mode */
  985. raw(); /* Line buffering disabled */
  986. keypad(stdscr, TRUE); /* We get F1, F2 etc.. */
  987. noecho(); /* Don't echo() while we do getch */
  988. printw("Type any character to see it in bold\n");
  989. ch = getch(); /* If raw() hadn't been called
  990. * we have to press enter before it
  991. * gets to the program */
  992. if(ch == KEY_F(1)) /* Without keypad enabled this will */
  993. printw("F1 Key pressed");/* not get to us either */
  994. /* Without noecho() some ugly escape
  995. * charachters might have been printed
  996. * on screen */
  997. else
  998. { printw("The pressed key is ");
  999. attron(A_BOLD);
  1000. printw("%c", ch);
  1001. attroff(A_BOLD);
  1002. }
  1003. refresh(); /* Print it on to the real screen */
  1004. getch(); /* Wait for user input */
  1005. endwin(); /* End curses mode */
  1006. return 0;
  1007. }</span></font>
  1008. </pre></td>
  1009. </tr>
  1010. </table>
  1011. </div>
  1012. <p>This program is self-explanatory. But I used functions which
  1013. aren't explained yet. The function <var class=
  1014. "LITERAL">getch()</var> is used to get a character from user. It is
  1015. equivalent to normal <var class="LITERAL">getchar()</var> except
  1016. that we can disable the line buffering to avoid &lt;enter&gt; after
  1017. input. Look for more about <var class="LITERAL">getch()</var>and
  1018. reading keys in the <a href="#KEYS">key management section</a> .
  1019. The functions attron and attroff are used to switch some attributes
  1020. on and off respectively. In the example I used them to print the
  1021. character in bold. These functions are explained in detail
  1022. later.</p>
  1023. </div>
  1024. </div>
  1025. <div class="SECT1">
  1026. <hr>
  1027. <h2 class="SECT1"><a name="AWORDWINDOWS" id="AWORDWINDOWS">5. A
  1028. Word about Windows</a></h2>
  1029. <p>Before we plunge into the myriad ncurses functions, let me clear
  1030. few things about windows. Windows are explained in detail in
  1031. following <a href="#WINDOWS">sections</a></p>
  1032. <p>A Window is an imaginary screen defined by curses system. A
  1033. window does not mean a bordered window which you usually see on
  1034. Win9X platforms. When curses is initialized, it creates a default
  1035. window named <var class="LITERAL">stdscr</var> which represents
  1036. your 80x25 (or the size of window in which you are running) screen.
  1037. If you are doing simple tasks like printing few strings, reading
  1038. input etc., you can safely use this single window for all of your
  1039. purposes. You can also create windows and call functions which
  1040. explicitly work on the specified window.</p>
  1041. <p>For example, if you call</p>
  1042. <table border="0" bgcolor="#E0E0E0" width="100%">
  1043. <tr>
  1044. <td>
  1045. <pre class="PROGRAMLISTING">
  1046. <font color="#000000"> printw("Hi There !!!");
  1047. refresh();</font>
  1048. </pre></td>
  1049. </tr>
  1050. </table>
  1051. <p>It prints the string on stdscr at the present cursor position.
  1052. Similarly the call to refresh(), works on stdscr only.</p>
  1053. <p>Say you have created <a href="#WINDOWS">windows</a> then you
  1054. have to call a function with a 'w' added to the usual function.</p>
  1055. <table border="0" bgcolor="#E0E0E0" width="100%">
  1056. <tr>
  1057. <td>
  1058. <pre class="PROGRAMLISTING">
  1059. <font color="#000000"> wprintw(win, "Hi There !!!");
  1060. wrefresh(win);</font>
  1061. </pre></td>
  1062. </tr>
  1063. </table>
  1064. <p>As you will see in the rest of the document, naming of functions
  1065. follow the same convention. For each function there usually are
  1066. three more functions.</p>
  1067. <table border="0" bgcolor="#E0E0E0" width="100%">
  1068. <tr>
  1069. <td>
  1070. <pre class="PROGRAMLISTING">
  1071. <font color=
  1072. "#000000"> printw(string); /* Print on stdscr at present cursor position */
  1073. mvprintw(y, x, string);/* Move to (y, x) then print string */
  1074. wprintw(win, string); /* Print on window win at present cursor position */
  1075. /* in the window */
  1076. mvwprintw(win, y, x, string); /* Move to (y, x) relative to window */
  1077. /* co-ordinates and then print */</font>
  1078. </pre></td>
  1079. </tr>
  1080. </table>
  1081. <p>Usually the w-less functions are macros which expand to
  1082. corresponding w-function with stdscr as the window parameter.</p>
  1083. </div>
  1084. <div class="SECT1">
  1085. <hr>
  1086. <h2 class="SECT1"><a name="PRINTW" id="PRINTW">6. Output
  1087. functions</a></h2>
  1088. <p>I guess you can't wait any more to see some action. Back to our
  1089. odyssey of curses functions. Now that curses is initialized, let's
  1090. interact with world.</p>
  1091. <p>There are three classes of functions which you can use to do
  1092. output on screen.</p>
  1093. <ol type="1">
  1094. <li>
  1095. <p>addch() class: Print single character with attributes</p>
  1096. </li>
  1097. <li>
  1098. <p>printw() class: Print formatted output similar to printf()</p>
  1099. </li>
  1100. <li>
  1101. <p>addstr() class: Print strings</p>
  1102. </li>
  1103. </ol>
  1104. <p>These functions can be used interchangeably and it's a matter of
  1105. style as to which class is used. Let's see each one in detail.</p>
  1106. <div class="SECT2">
  1107. <hr>
  1108. <h3 class="SECT2"><a name="ADDCHCLASS" id="ADDCHCLASS">6.1. addch()
  1109. class of functions</a></h3>
  1110. <p>These functions put a single character into the current cursor
  1111. location and advance the position of the cursor. You can give the
  1112. character to be printed but they usually are used to print a
  1113. character with some attributes. Attributes are explained in detail
  1114. in later <a href="#ATTRIB">sections</a> of the document. If a
  1115. character is associated with an attribute(bold, reverse video
  1116. etc.), when curses prints the character, it is printed in that
  1117. attribute.</p>
  1118. <p>In order to combine a character with some attributes, you have
  1119. two options:</p>
  1120. <ul>
  1121. <li>
  1122. <p>By OR'ing a single character with the desired attribute macros.
  1123. These attribute macros could be found in the header file
  1124. <var class="LITERAL">ncurses.h</var>. For example, you want to
  1125. print a character ch(of type char) bold and underlined, you would
  1126. call addch() as below.</p>
  1127. <table border="0" bgcolor="#E0E0E0" width="90%">
  1128. <tr>
  1129. <td>
  1130. <pre class="PROGRAMLISTING">
  1131. <font color="#000000"> addch(ch | A_BOLD | A_UNDERLINE);</font>
  1132. </pre></td>
  1133. </tr>
  1134. </table>
  1135. </li>
  1136. <li>
  1137. <p>By using functions like <var class=
  1138. "LITERAL">attrset(),attron(),attroff()</var>. These functions are
  1139. explained in the <a href="#ATTRIB">Attributes</a> section. Briefly,
  1140. they manipulate the current attributes of the given window. Once
  1141. set, the character printed in the window are associated with the
  1142. attributes until it is turned off.</p>
  1143. </li>
  1144. </ul>
  1145. <p>Additionally, <var class="LITERAL">curses</var> provides some
  1146. special characters for character-based graphics. You can draw
  1147. tables, horizontal or vertical lines, etc. You can find all
  1148. avaliable characters in the header file <var class=
  1149. "LITERAL">ncurses.h</var>. Try looking for macros beginning with
  1150. <var class="LITERAL">ACS_</var> in this file.</p>
  1151. </div>
  1152. <div class="SECT2">
  1153. <hr>
  1154. <h3 class="SECT2"><a name="AEN298" id="AEN298">6.2. mvaddch(),
  1155. waddch() and mvwaddch()</a></h3>
  1156. <p><var class="LITERAL">mvaddch()</var> is used to move the cursor
  1157. to a given point, and then print. Thus, the calls:</p>
  1158. <table border="0" bgcolor="#E0E0E0" width="100%">
  1159. <tr>
  1160. <td>
  1161. <pre class="PROGRAMLISTING">
  1162. <font color=
  1163. "#000000"> move(row,col); /* moves the cursor to row<em>th</em> row and col<em>th</em> column */
  1164. addch(ch);</font>
  1165. </pre></td>
  1166. </tr>
  1167. </table>
  1168. can be replaced by
  1169. <table border="0" bgcolor="#E0E0E0" width="100%">
  1170. <tr>
  1171. <td>
  1172. <pre class="PROGRAMLISTING">
  1173. <font color="#000000"> mvaddch(row,col,ch);</font>
  1174. </pre></td>
  1175. </tr>
  1176. </table>
  1177. <p><var class="LITERAL">waddch()</var> is similar to <var class=
  1178. "LITERAL">addch()</var>, except that it adds a character into the
  1179. given window. (Note that <var class="LITERAL">addch()</var> adds a
  1180. character into the window <var class="LITERAL">stdscr</var>.)</p>
  1181. <p>In a similar fashion <var class="LITERAL">mvwaddch()</var>
  1182. function is used to add a character into the given window at the
  1183. given coordinates.</p>
  1184. <p>Now, we are familiar with the basic output function <var class=
  1185. "LITERAL">addch()</var>. But, if we want to print a string, it
  1186. would be very annoying to print it character by character.
  1187. Fortunately, <var class="LITERAL">ncurses</var> provides
  1188. <var class="LITERAL">printf</var><em>-like</em> or <var class=
  1189. "LITERAL">puts</var><em>-like</em> functions.</p>
  1190. </div>
  1191. <div class="SECT2">
  1192. <hr>
  1193. <h3 class="SECT2"><a name="PRINTWCLASS" id="PRINTWCLASS">6.3.
  1194. printw() class of functions</a></h3>
  1195. <p>These functions are similar to <var class=
  1196. "LITERAL">printf()</var> with the added capability of printing at
  1197. any position on the screen.</p>
  1198. <div class="SECT3">
  1199. <hr>
  1200. <h4 class="SECT3"><a name="PRINTWMVPRINTW" id=
  1201. "PRINTWMVPRINTW">6.3.1. printw() and mvprintw</a></h4>
  1202. <p>These two functions work much like <var class=
  1203. "LITERAL">printf()</var>. <var class="LITERAL">mvprintw()</var> can
  1204. be used to move the cursor to a position and then print. If you
  1205. want to move the cursor first and then print using <var class=
  1206. "LITERAL">printw()</var> function, use <var class=
  1207. "LITERAL">move()</var> first and then use <var class=
  1208. "LITERAL">printw()</var> though I see no point why one should avoid
  1209. using <var class="LITERAL">mvprintw()</var>, you have the
  1210. flexibility to manipulate.</p>
  1211. </div>
  1212. <div class="SECT3">
  1213. <hr>
  1214. <h4 class="SECT3"><a name="WPRINTWMVWPRINTW" id=
  1215. "WPRINTWMVWPRINTW">6.3.2. wprintw() and mvwprintw</a></h4>
  1216. <p>These two functions are similar to above two except that they
  1217. print in the corresponding window given as argument.</p>
  1218. </div>
  1219. <div class="SECT3">
  1220. <hr>
  1221. <h4 class="SECT3"><a name="VWPRINTW" id="VWPRINTW">6.3.3.
  1222. vwprintw()</a></h4>
  1223. <p>This function is similar to <var class=
  1224. "LITERAL">vprintf()</var>. This can be used when variable number of
  1225. arguments are to be printed.</p>
  1226. </div>
  1227. <div class="SECT3">
  1228. <hr>
  1229. <h4 class="SECT3"><a name="SIMPLEPRINTWEX" id=
  1230. "SIMPLEPRINTWEX">6.3.4. A Simple printw example</a></h4>
  1231. <div class="EXAMPLE"><a name="BPREX" id="BPREX"></a>
  1232. <p><b>Example 3. A Simple printw example</b></p>
  1233. <table border="0" bgcolor="#E0E0E0" width="100%">
  1234. <tr>
  1235. <td>
  1236. <pre class="PROGRAMLISTING">
  1237. <font color="#000000"><span class=
  1238. "INLINEMEDIAOBJECT">#include &lt;ncurses.h&gt; /* ncurses.h includes stdio.h */
  1239. #include &lt;string.h&gt;
  1240. int main()
  1241. {
  1242. char mesg[]="Just a string"; /* message to be appeared on the screen */
  1243. int row,col; /* to store the number of rows and *
  1244. * the number of colums of the screen */
  1245. initscr(); /* start the curses mode */
  1246. getmaxyx(stdscr,row,col); /* get the number of rows and columns */
  1247. mvprintw(row/2,(col-strlen(mesg))/2,"%s",mesg);
  1248. /* print the message at the center of the screen */
  1249. mvprintw(row-2,0,"This screen has %d rows and %d columns\n",row,col);
  1250. printw("Try resizing your window(if possible) and then run this program again");
  1251. refresh();
  1252. getch();
  1253. endwin();
  1254. return 0;
  1255. }</span></font>
  1256. </pre></td>
  1257. </tr>
  1258. </table>
  1259. </div>
  1260. <p>Above program demonstrates how easy it is to use <var class=
  1261. "LITERAL">printw</var>. You just feed the coordinates and the
  1262. message to be appeared on the screen, then it does what you
  1263. want.</p>
  1264. <p>The above program introduces us to a new function <var class=
  1265. "LITERAL">getmaxyx()</var>, a macro defined in <var class=
  1266. "LITERAL">ncurses.h</var>. It gives the number of columns and the
  1267. number of rows in a given window. <var class=
  1268. "LITERAL">getmaxyx()</var> does this by updating the variables
  1269. given to it. Since <var class="LITERAL">getmaxyx()</var> is not a
  1270. function we don't pass pointers to it, we just give two integer
  1271. variables.</p>
  1272. </div>
  1273. </div>
  1274. <div class="SECT2">
  1275. <hr>
  1276. <h3 class="SECT2"><a name="ADDSTRCLASS" id="ADDSTRCLASS">6.4.
  1277. addstr() class of functions</a></h3>
  1278. <p><var class="LITERAL">addstr()</var> is used to put a character
  1279. string into a given window. This function is similar to calling
  1280. <var class="LITERAL">addch()</var> once for each character in a
  1281. given string. This is true for all output functions. There are
  1282. other functions from this family such as <var class=
  1283. "LITERAL">mvaddstr(),mvwaddstr()</var> and <var class=
  1284. "LITERAL">waddstr()</var>, which obey the naming convention of
  1285. curses.(e.g. mvaddstr() is similar to the respective calls move()
  1286. and then addstr().) Another function of this family is addnstr(),
  1287. which takes an integer parameter(say n) additionally. This function
  1288. puts at most n characters into the screen. If n is negative, then
  1289. the entire string will be added.</p>
  1290. </div>
  1291. <div class="SECT2">
  1292. <hr>
  1293. <h3 class="SECT2"><a name="ACAUTION" id="ACAUTION">6.5. A word of
  1294. caution</a></h3>
  1295. <p>All these functions take y co-ordinate first and then x in their
  1296. arguments. A common mistake by beginners is to pass x,y in that
  1297. order. If you are doing too many manipulations of (y,x)
  1298. co-ordinates, think of dividing the screen into windows and
  1299. manipulate each one separately. Windows are explained in the
  1300. <a href="#WINDOWS">windows</a> section.</p>
  1301. </div>
  1302. </div>
  1303. <div class="SECT1">
  1304. <hr>
  1305. <h2 class="SECT1"><a name="SCANW" id="SCANW">7. Input
  1306. functions</a></h2>
  1307. <p>Well, printing without taking input, is boring. Let's see
  1308. functions which allow us to get input from user. These functions
  1309. also can be divided into three categories.</p>
  1310. <ol type="1">
  1311. <li>
  1312. <p>getch() class: Get a character</p>
  1313. </li>
  1314. <li>
  1315. <p>scanw() class: Get formatted input</p>
  1316. </li>
  1317. <li>
  1318. <p>getstr() class: Get strings</p>
  1319. </li>
  1320. </ol>
  1321. <div class="SECT2">
  1322. <hr>
  1323. <h3 class="SECT2"><a name="GETCHCLASS" id="GETCHCLASS">7.1. getch()
  1324. class of functions</a></h3>
  1325. <p>These functions read a single character from the terminal. But
  1326. there are several subtle facts to consider. For example if you
  1327. don't use the function cbreak(), curses will not read your input
  1328. characters contiguously but will begin read them only after a new
  1329. line or an EOF is encountered. In order to avoid this, the cbreak()
  1330. function must used so that characters are immediately available to
  1331. your program. Another widely used function is noecho(). As the name
  1332. suggests, when this function is set (used), the characters that are
  1333. keyed in by the user will not show up on the screen. The two
  1334. functions cbreak() and noecho() are typical examples of key
  1335. management. Functions of this genre are explained in the <a href=
  1336. "#KEYS">key management section</a> .</p>
  1337. </div>
  1338. <div class="SECT2">
  1339. <hr>
  1340. <h3 class="SECT2"><a name="SCANWCLASS" id="SCANWCLASS">7.2. scanw()
  1341. class of functions</a></h3>
  1342. <p>These functions are similar to <var class=
  1343. "LITERAL">scanf()</var> with the added capability of getting the
  1344. input from any location on the screen.</p>
  1345. <div class="SECT3">
  1346. <hr>
  1347. <h4 class="SECT3"><a name="SCANWMVSCANW" id="SCANWMVSCANW">7.2.1.
  1348. scanw() and mvscanw</a></h4>
  1349. <p>The usage of these functions is similar to that of <var class=
  1350. "LITERAL">sscanf()</var>, where the line to be scanned is provided
  1351. by <var class="LITERAL">wgetstr()</var> function. That is, these
  1352. functions call to <var class="LITERAL">wgetstr()</var>
  1353. function(explained below) and uses the resulting line for a
  1354. scan.</p>
  1355. </div>
  1356. <div class="SECT3">
  1357. <hr>
  1358. <h4 class="SECT3"><a name="WSCANWMVWSCANW" id=
  1359. "WSCANWMVWSCANW">7.2.2. wscanw() and mvwscanw()</a></h4>
  1360. <p>These are similar to above two functions except that they read
  1361. from a window, which is supplied as one of the arguments to these
  1362. functions.</p>
  1363. </div>
  1364. <div class="SECT3">
  1365. <hr>
  1366. <h4 class="SECT3"><a name="VWSCANW" id="VWSCANW">7.2.3.
  1367. vwscanw()</a></h4>
  1368. <p>This function is similar to <var class="LITERAL">vscanf()</var>.
  1369. This can be used when a variable number of arguments are to be
  1370. scanned.</p>
  1371. </div>
  1372. </div>
  1373. <div class="SECT2">
  1374. <hr>
  1375. <h3 class="SECT2"><a name="GETSTRCLASS" id="GETSTRCLASS">7.3.
  1376. getstr() class of functions</a></h3>
  1377. <p>These functions are used to get strings from the terminal. In
  1378. essence, this function performs the same task as would be achieved
  1379. by a series of calls to <var class="LITERAL">getch()</var> until a
  1380. newline, carriage return, or end-of-file is received. The resulting
  1381. string of characters are pointed to by <var class=
  1382. "LITERAL">str</var>, which is a character pointer provided by the
  1383. user.</p>
  1384. </div>
  1385. <div class="SECT2">
  1386. <hr>
  1387. <h3 class="SECT2"><a name="GETSTREX" id="GETSTREX">7.4. Some
  1388. examples</a></h3>
  1389. <div class="EXAMPLE"><a name="BSCEX" id="BSCEX"></a>
  1390. <p><b>Example 4. A Simple scanw example</b></p>
  1391. <table border="0" bgcolor="#E0E0E0" width="100%">
  1392. <tr>
  1393. <td>
  1394. <pre class="PROGRAMLISTING">
  1395. <font color="#000000"><span class=
  1396. "INLINEMEDIAOBJECT">#include &lt;ncurses.h&gt; /* ncurses.h includes stdio.h */
  1397. #include &lt;string.h&gt;
  1398. int main()
  1399. {
  1400. char mesg[]="Enter a string: "; /* message to be appeared on the screen */
  1401. char str[80];
  1402. int row,col; /* to store the number of rows and *
  1403. * the number of colums of the screen */
  1404. initscr(); /* start the curses mode */
  1405. getmaxyx(stdscr,row,col); /* get the number of rows and columns */
  1406. mvprintw(row/2,(col-strlen(mesg))/2,"%s",mesg);
  1407. /* print the message at the center of the screen */
  1408. getstr(str);
  1409. mvprintw(LINES - 2, 0, "You Entered: %s", str);
  1410. getch();
  1411. endwin();
  1412. return 0;
  1413. }</span></font>
  1414. </pre></td>
  1415. </tr>
  1416. </table>
  1417. </div>
  1418. </div>
  1419. </div>
  1420. <div class="SECT1">
  1421. <hr>
  1422. <h2 class="SECT1"><a name="ATTRIB" id="ATTRIB">8.
  1423. Attributes</a></h2>
  1424. <p>We have seen an example of how attributes can be used to print
  1425. characters with some special effects. Attributes, when set
  1426. prudently, can present information in an easy, understandable
  1427. manner. The following program takes a C file as input and prints
  1428. the file with comments in bold. Scan through the code.</p>
  1429. <div class="EXAMPLE"><a name="BSIAT" id="BSIAT"></a>
  1430. <p><b>Example 5. A Simple Attributes example</b></p>
  1431. <table border="0" bgcolor="#E0E0E0" width="100%">
  1432. <tr>
  1433. <td>
  1434. <pre class="PROGRAMLISTING">
  1435. <font color="#000000"><span class=
  1436. "INLINEMEDIAOBJECT">/* pager functionality by Joseph Spainhour" &lt;spainhou@bellsouth.net&gt; */
  1437. #include &lt;ncurses.h&gt;
  1438. #include &lt;stdlib.h&gt;
  1439. int main(int argc, char *argv[])
  1440. {
  1441. int ch, prev, row, col;
  1442. prev = EOF;
  1443. FILE *fp;
  1444. int y, x;
  1445. if(argc != 2)
  1446. {
  1447. printf("Usage: %s &lt;a c file name&gt;\n", argv[0]);
  1448. exit(1);
  1449. }
  1450. fp = fopen(argv[1], "r");
  1451. if(fp == NULL)
  1452. {
  1453. perror("Cannot open input file");
  1454. exit(1);
  1455. }
  1456. initscr(); /* Start curses mode */
  1457. getmaxyx(stdscr, row, col); /* find the boundaries of the screeen */
  1458. while((ch = fgetc(fp)) != EOF) /* read the file till we reach the end */
  1459. {
  1460. getyx(stdscr, y, x); /* get the current curser position */
  1461. if(y == (row - 1)) /* are we are at the end of the screen */
  1462. {
  1463. printw("&lt;-Press Any Key-&gt;"); /* tell the user to press a key */
  1464. getch();
  1465. clear(); /* clear the screen */
  1466. move(0, 0); /* start at the beginning of the screen */
  1467. }
  1468. if(prev == '/' &amp;&amp; ch == '*') /* If it is / and * then only
  1469. * switch bold on */
  1470. {
  1471. attron(A_BOLD); /* cut bold on */
  1472. getyx(stdscr, y, x); /* get the current curser position */
  1473. move(y, x - 1); /* back up one space */
  1474. printw("%c%c", '/', ch); /* The actual printing is done here */
  1475. }
  1476. else
  1477. printw("%c", ch);
  1478. refresh();
  1479. if(prev == '*' &amp;&amp; ch == '/')
  1480. attroff(A_BOLD); /* Switch it off once we got *
  1481. * and then / */
  1482. prev = ch;
  1483. }
  1484. endwin(); /* End curses mode */
  1485. fclose(fp);
  1486. return 0;
  1487. }</span></font>
  1488. </pre></td>
  1489. </tr>
  1490. </table>
  1491. </div>
  1492. <p>Don't worry about all those initialization and other crap.
  1493. Concentrate on the while loop. It reads each character in the file
  1494. and searches for the pattern /*. Once it spots the pattern, it
  1495. switches the BOLD attribute on with <var class=
  1496. "LITERAL">attron()</var> . When we get the pattern */ it is
  1497. switched off by <var class="LITERAL">attroff()</var> .</p>
  1498. <p>The above program also introduces us to two useful functions
  1499. <var class="LITERAL">getyx()</var> and <var class=
  1500. "LITERAL">move()</var>. The first function gets the co-ordinates of
  1501. the present cursor into the variables y, x. Since getyx() is a
  1502. macro we don't have to pass pointers to variables. The function
  1503. <var class="LITERAL">move()</var> moves the cursor to the
  1504. co-ordinates given to it.</p>
  1505. <p>The above program is really a simple one which doesn't do much.
  1506. On these lines one could write a more useful program which reads a
  1507. C file, parses it and prints it in different colors. One could even
  1508. extend it to other languages as well.</p>
  1509. <div class="SECT2">
  1510. <hr>
  1511. <h3 class="SECT2"><a name="ATTRIBDETAILS" id="ATTRIBDETAILS">8.1.
  1512. The details</a></h3>
  1513. <p>Let's get into more details of attributes. The functions
  1514. <var class="LITERAL">attron(), attroff(), attrset()</var> , and
  1515. their sister functions <var class="LITERAL">attr_get()</var> etc..
  1516. can be used to switch attributes on/off , get attributes and
  1517. produce a colorful display.</p>
  1518. <p>The functions attron and attroff take a bit-mask of attributes
  1519. and switch them on or off, respectively. The following video
  1520. attributes, which are defined in &lt;curses.h&gt; can be passed to
  1521. these functions.</p>
  1522. <table border="0" bgcolor="#E0E0E0" width="100%">
  1523. <tr>
  1524. <td>
  1525. <pre class="PROGRAMLISTING">
  1526. <font color="#000000">
  1527. A_NORMAL Normal display (no highlight)
  1528. A_STANDOUT Best highlighting mode of the terminal.
  1529. A_UNDERLINE Underlining
  1530. A_REVERSE Reverse video
  1531. A_BLINK Blinking
  1532. A_DIM Half bright
  1533. A_BOLD Extra bright or bold
  1534. A_PROTECT Protected mode
  1535. A_INVIS Invisible or blank mode
  1536. A_ALTCHARSET Alternate character set
  1537. A_CHARTEXT Bit-mask to extract a character
  1538. COLOR_PAIR(n) Color-pair number n
  1539. </font>
  1540. </pre></td>
  1541. </tr>
  1542. </table>
  1543. <p>The last one is the most colorful one :-) Colors are explained
  1544. in the <a href="#color" target="_top">next sections</a>.</p>
  1545. <p>We can OR(|) any number of above attributes to get a combined
  1546. effect. If you wanted reverse video with blinking characters you
  1547. can use</p>
  1548. <table border="0" bgcolor="#E0E0E0" width="100%">
  1549. <tr>
  1550. <td>
  1551. <pre class="PROGRAMLISTING">
  1552. <font color="#000000"> attron(A_REVERSE | A_BLINK);</font>
  1553. </pre></td>
  1554. </tr>
  1555. </table>
  1556. </div>
  1557. <div class="SECT2">
  1558. <hr>
  1559. <h3 class="SECT2"><a name="ATTRONVSATTRSET" id=
  1560. "ATTRONVSATTRSET">8.2. attron() vs attrset()</a></h3>
  1561. <p>Then what is the difference between attron() and attrset()?
  1562. attrset sets the attributes of window whereas attron just switches
  1563. on the attribute given to it. So attrset() fully overrides whatever
  1564. attributes the window previously had and sets it to the new
  1565. attribute(s). Similarly attroff() just switches off the
  1566. attribute(s) given to it as an argument. This gives us the
  1567. flexibility of managing attributes easily.But if you use them
  1568. carelessly you may loose track of what attributes the window has
  1569. and garble the display. This is especially true while managing
  1570. menus with colors and highlighting. So decide on a consistent
  1571. policy and stick to it. You can always use <var class=
  1572. "LITERAL">standend()</var> which is equivalent to <var class=
  1573. "LITERAL">attrset(A_NORMAL)</var> which turns off all attributes
  1574. and brings you to normal mode.</p>
  1575. </div>
  1576. <div class="SECT2">
  1577. <hr>
  1578. <h3 class="SECT2"><a name="ATTR_GET" id="ATTR_GET">8.3.
  1579. attr_get()</a></h3>
  1580. <p>The function attr_get() gets the current attributes and color
  1581. pair of the window. Though we might not use this as often as the
  1582. above functions, this is useful in scanning areas of screen. Say we
  1583. wanted to do some complex update on screen and we are not sure what
  1584. attribute each character is associated with. Then this function can
  1585. be used with either attrset or attron to produce the desired
  1586. effect.</p>
  1587. </div>
  1588. <div class="SECT2">
  1589. <hr>
  1590. <h3 class="SECT2"><a name="ATTR_FUNCS" id="ATTR_FUNCS">8.4. attr_
  1591. functions</a></h3>
  1592. <p>There are series of functions like attr_set(), attr_on etc..
  1593. These are similar to above functions except that they take
  1594. parameters of type <var class="LITERAL">attr_t</var>.</p>
  1595. </div>
  1596. <div class="SECT2">
  1597. <hr>
  1598. <h3 class="SECT2"><a name="WATTRFUNCS" id="WATTRFUNCS">8.5. wattr
  1599. functions</a></h3>
  1600. <p>For each of the above functions we have a corresponding function
  1601. with 'w' which operates on a particular window. The above functions
  1602. operate on stdscr.</p>
  1603. </div>
  1604. <div class="SECT2">
  1605. <hr>
  1606. <h3 class="SECT2"><a name="CHGAT" id="CHGAT">8.6. chgat()
  1607. functions</a></h3>
  1608. <p>The function chgat() is listed in the end of the man page
  1609. curs_attr. It actually is a useful one. This function can be used
  1610. to set attributes for a group of characters without moving. I mean
  1611. it !!! without moving the cursor :-) It changes the attributes of a
  1612. given number of characters starting at the current cursor
  1613. location.</p>
  1614. <p>We can give -1 as the character count to update till end of
  1615. line. If you want to change attributes of characters from current
  1616. position to end of line, just use this.</p>
  1617. <table border="0" bgcolor="#E0E0E0" width="100%">
  1618. <tr>
  1619. <td>
  1620. <pre class="PROGRAMLISTING">
  1621. <font color="#000000"> chgat(-1, A_REVERSE, 0, NULL);</font>
  1622. </pre></td>
  1623. </tr>
  1624. </table>
  1625. <p>This function is useful when changing attributes for characters
  1626. that are already on the screen. Move to the character from which
  1627. you want to change and change the attribute.</p>
  1628. <p>Other functions wchgat(), mvchgat(), wchgat() behave similarly
  1629. except that the w functions operate on the particular window. The
  1630. mv functions first move the cursor then perform the work given to
  1631. them. Actually chgat is a macro which is replaced by a wchgat()
  1632. with stdscr as the window. Most of the "w-less" functions are
  1633. macros.</p>
  1634. <div class="EXAMPLE"><a name="BWICH" id="BWICH"></a>
  1635. <p><b>Example 6. Chgat() Usage example</b></p>
  1636. <table border="0" bgcolor="#E0E0E0" width="100%">
  1637. <tr>
  1638. <td>
  1639. <pre class="PROGRAMLISTING">
  1640. <font color="#000000"><span class=
  1641. "INLINEMEDIAOBJECT">#include &lt;ncurses.h&gt;
  1642. int main(int argc, char *argv[])
  1643. { initscr(); /* Start curses mode */
  1644. start_color(); /* Start color functionality */
  1645. init_pair(1, COLOR_CYAN, COLOR_BLACK);
  1646. printw("A Big string which i didn't care to type fully ");
  1647. mvchgat(0, 0, -1, A_BLINK, 1, NULL);
  1648. /*
  1649. * First two parameters specify the position at which to start
  1650. * Third parameter number of characters to update. -1 means till
  1651. * end of line
  1652. * Forth parameter is the normal attribute you wanted to give
  1653. * to the charcter
  1654. * Fifth is the color index. It is the index given during init_pair()
  1655. * use 0 if you didn't want color
  1656. * Sixth one is always NULL
  1657. */
  1658. refresh();
  1659. getch();
  1660. endwin(); /* End curses mode */
  1661. return 0;
  1662. }</span></font>
  1663. </pre></td>
  1664. </tr>
  1665. </table>
  1666. </div>
  1667. <p>This example also introduces us to the color world of curses.
  1668. Colors will be explained in detail later. Use 0 for no color.</p>
  1669. </div>
  1670. </div>
  1671. <div class="SECT1">
  1672. <hr>
  1673. <h2 class="SECT1"><a name="WINDOWS" id="WINDOWS">9.
  1674. Windows</a></h2>
  1675. <p>Windows form the most important concept in curses. You have seen
  1676. the standard window stdscr above where all the functions implicitly
  1677. operated on this window. Now to make design even a simplest GUI,
  1678. you need to resort to windows. The main reason you may want to use
  1679. windows is to manipulate parts of the screen separately, for better
  1680. efficiency, by updating only the windows that need to be changed
  1681. and for a better design. I would say the last reason is the most
  1682. important in going for windows. You should always strive for a
  1683. better and easy-to-manage design in your programs. If you are
  1684. writing big, complex GUIs this is of pivotal importance before you
  1685. start doing anything.</p>
  1686. <div class="SECT2">
  1687. <hr>
  1688. <h3 class="SECT2"><a name="WINDOWBASICS" id="WINDOWBASICS">9.1. The
  1689. basics</a></h3>
  1690. <p>A Window can be created by calling the function <var class=
  1691. "LITERAL">newwin()</var>. It doesn't create any thing on the screen
  1692. actually. It allocates memory for a structure to manipulate the
  1693. window and updates the structure with data regarding the window
  1694. like it's size, beginy, beginx etc.. Hence in curses, a window is
  1695. just an abstraction of an imaginary window, which can be
  1696. manipulated independent of other parts of screen. The function
  1697. newwin() returns a pointer to structure WINDOW, which can be passed
  1698. to window related functions like wprintw() etc.. Finally the window
  1699. can be destroyed with delwin(). It will deallocate the memory
  1700. associated with the window structure.</p>
  1701. </div>
  1702. <div class="SECT2">
  1703. <hr>
  1704. <h3 class="SECT2"><a name="LETBEWINDOW" id="LETBEWINDOW">9.2. Let
  1705. there be a Window !!!</a></h3>
  1706. <p>What fun is it, if a window is created and we can't see it. So
  1707. the fun part begins by displaying the window. The function
  1708. <var class="LITERAL">box()</var> can be used to draw a border
  1709. around the window. Let's explore these functions in more detail in
  1710. this example.</p>
  1711. <div class="EXAMPLE"><a name="BWIBO" id="BWIBO"></a>
  1712. <p><b>Example 7. Window Border example</b></p>
  1713. <table border="0" bgcolor="#E0E0E0" width="100%">
  1714. <tr>
  1715. <td>
  1716. <pre class="PROGRAMLISTING">
  1717. <font color="#000000"><span class=
  1718. "INLINEMEDIAOBJECT">#include &lt;ncurses.h&gt;
  1719. WINDOW *create_newwin(int height, int width, int starty, int startx);
  1720. void destroy_win(WINDOW *local_win);
  1721. int main(int argc, char *argv[])
  1722. { WINDOW *my_win;
  1723. int startx, starty, width, height;
  1724. int ch;
  1725. initscr(); /* Start curses mode */
  1726. cbreak(); /* Line buffering disabled, Pass on
  1727. * everty thing to me */
  1728. keypad(stdscr, TRUE); /* I need that nifty F1 */
  1729. height = 3;
  1730. width = 10;
  1731. starty = (LINES - height) / 2; /* Calculating for a center placement */
  1732. startx = (COLS - width) / 2; /* of the window */
  1733. printw("Press F1 to exit");
  1734. refresh();
  1735. my_win = create_newwin(height, width, starty, startx);
  1736. while((ch = getch()) != KEY_F(1))
  1737. { switch(ch)
  1738. { case KEY_LEFT:
  1739. destroy_win(my_win);
  1740. my_win = create_newwin(height, width, starty,--startx);
  1741. break;
  1742. case KEY_RIGHT:
  1743. destroy_win(my_win);
  1744. my_win = create_newwin(height, width, starty,++startx);
  1745. break;
  1746. case KEY_UP:
  1747. destroy_win(my_win);
  1748. my_win = create_newwin(height, width, --starty,startx);
  1749. break;
  1750. case KEY_DOWN:
  1751. destroy_win(my_win);
  1752. my_win = create_newwin(height, width, ++starty,startx);
  1753. break;
  1754. }
  1755. }
  1756. endwin(); /* End curses mode */
  1757. return 0;
  1758. }
  1759. WINDOW *create_newwin(int height, int width, int starty, int startx)
  1760. { WINDOW *local_win;
  1761. local_win = newwin(height, width, starty, startx);
  1762. box(local_win, 0 , 0); /* 0, 0 gives default characters
  1763. * for the vertical and horizontal
  1764. * lines */
  1765. wrefresh(local_win); /* Show that box */
  1766. return local_win;
  1767. }
  1768. void destroy_win(WINDOW *local_win)
  1769. {
  1770. /* box(local_win, ' ', ' '); : This won't produce the desired
  1771. * result of erasing the window. It will leave it's four corners
  1772. * and so an ugly remnant of window.
  1773. */
  1774. wborder(local_win, ' ', ' ', ' ',' ',' ',' ',' ',' ');
  1775. /* The parameters taken are
  1776. * 1. win: the window on which to operate
  1777. * 2. ls: character to be used for the left side of the window
  1778. * 3. rs: character to be used for the right side of the window
  1779. * 4. ts: character to be used for the top side of the window
  1780. * 5. bs: character to be used for the bottom side of the window
  1781. * 6. tl: character to be used for the top left corner of the window
  1782. * 7. tr: character to be used for the top right corner of the window
  1783. * 8. bl: character to be used for the bottom left corner of the window
  1784. * 9. br: character to be used for the bottom right corner of the window
  1785. */
  1786. wrefresh(local_win);
  1787. delwin(local_win);
  1788. }</span></font>
  1789. </pre></td>
  1790. </tr>
  1791. </table>
  1792. </div>
  1793. </div>
  1794. <div class="SECT2">
  1795. <hr>
  1796. <h3 class="SECT2"><a name="BORDEREXEXPL" id="BORDEREXEXPL">9.3.
  1797. Explanation</a></h3>
  1798. <p>Don't scream. I know it's a big example. But I have to explain
  1799. some important things here :-). This program creates a rectangular
  1800. window that can be moved with left, right, up, down arrow keys. It
  1801. repeatedly creates and destroys windows as user press a key. Don't
  1802. go beyond the screen limits. Checking for those limits is left as
  1803. an exercise for the reader. Let's dissect it by line by line.</p>
  1804. <p>The <var class="LITERAL">create_newwin()</var> function creates
  1805. a window with <var class="LITERAL">newwin()</var> and displays a
  1806. border around it with box. The function <var class=
  1807. "LITERAL">destroy_win()</var> first erases the window from screen
  1808. by painting a border with ' ' character and then calling
  1809. <var class="LITERAL">delwin()</var> to deallocate memory related to
  1810. it. Depending on the key the user presses, starty or startx is
  1811. changed and a new window is created.</p>
  1812. <p>In the destroy_win, as you can see, I used wborder instead of
  1813. box. The reason is written in the comments (You missed it. I know.
  1814. Read the code :-)). wborder draws a border around the window with
  1815. the characters given to it as the 4 corner points and the 4 lines.
  1816. To put it clearly, if you have called wborder as below:</p>
  1817. <table border="0" bgcolor="#E0E0E0" width="100%">
  1818. <tr>
  1819. <td>
  1820. <pre class="PROGRAMLISTING">
  1821. <font color=
  1822. "#000000"> wborder(win, '|', '|', '-', '-', '+', '+', '+', '+');</font>
  1823. </pre></td>
  1824. </tr>
  1825. </table>
  1826. <p>it produces some thing like</p>
  1827. <table border="0" bgcolor="#E0E0E0" width="100%">
  1828. <tr>
  1829. <td>
  1830. <pre class="PROGRAMLISTING">
  1831. <font color="#000000"> +------------+
  1832. | |
  1833. | |
  1834. | |
  1835. | |
  1836. | |
  1837. | |
  1838. +------------+</font>
  1839. </pre></td>
  1840. </tr>
  1841. </table>
  1842. </div>
  1843. <div class="SECT2">
  1844. <hr>
  1845. <h3 class="SECT2"><a name="OTHERSTUFF" id="OTHERSTUFF">9.4. The
  1846. other stuff in the example</a></h3>
  1847. <p>You can also see in the above examples, that I have used the
  1848. variables COLS, LINES which are initialized to the screen sizes
  1849. after initscr(). They can be useful in finding screen dimensions
  1850. and finding the center co-ordinate of the screen as above. The
  1851. function <var class="LITERAL">getch()</var> as usual gets the key
  1852. from keyboard and according to the key it does the corresponding
  1853. work. This type of switch- case is very common in any GUI based
  1854. programs.</p>
  1855. </div>
  1856. <div class="SECT2">
  1857. <hr>
  1858. <h3 class="SECT2"><a name="OTHERBORDERFUNCS" id=
  1859. "OTHERBORDERFUNCS">9.5. Other Border functions</a></h3>
  1860. <p>Above program is grossly inefficient in that with each press of
  1861. a key, a window is destroyed and another is created. So let's write
  1862. a more efficient program which uses other border related
  1863. functions.</p>
  1864. <p>The following program uses <var class="LITERAL">mvhline()</var>
  1865. and <var class="LITERAL">mvvline()</var> to achieve similar effect.
  1866. These two functions are simple. They create a horizontal or
  1867. vertical line of the specified length at the specified
  1868. position.</p>
  1869. <div class="EXAMPLE"><a name="BOTBO" id="BOTBO"></a>
  1870. <p><b>Example 8. More border functions</b></p>
  1871. <table border="0" bgcolor="#E0E0E0" width="100%">
  1872. <tr>
  1873. <td>
  1874. <pre class="PROGRAMLISTING">
  1875. <font color="#000000"><span class=
  1876. "INLINEMEDIAOBJECT">#include &lt;ncurses.h&gt;
  1877. typedef struct _win_border_struct {
  1878. chtype ls, rs, ts, bs,
  1879. tl, tr, bl, br;
  1880. }WIN_BORDER;
  1881. typedef struct _WIN_struct {
  1882. int startx, starty;
  1883. int height, width;
  1884. WIN_BORDER border;
  1885. }WIN;
  1886. void init_win_params(WIN *p_win);
  1887. void print_win_params(WIN *p_win);
  1888. void create_box(WIN *win, bool flag);
  1889. int main(int argc, char *argv[])
  1890. { WIN win;
  1891. int ch;
  1892. initscr(); /* Start curses mode */
  1893. start_color(); /* Start the color functionality */
  1894. cbreak(); /* Line buffering disabled, Pass on
  1895. * everty thing to me */
  1896. keypad(stdscr, TRUE); /* I need that nifty F1 */
  1897. noecho();
  1898. init_pair(1, COLOR_CYAN, COLOR_BLACK);
  1899. /* Initialize the window parameters */
  1900. init_win_params(&amp;win);
  1901. print_win_params(&amp;win);
  1902. attron(COLOR_PAIR(1));
  1903. printw("Press F1 to exit");
  1904. refresh();
  1905. attroff(COLOR_PAIR(1));
  1906. create_box(&amp;win, TRUE);
  1907. while((ch = getch()) != KEY_F(1))
  1908. { switch(ch)
  1909. { case KEY_LEFT:
  1910. create_box(&amp;win, FALSE);
  1911. --win.startx;
  1912. create_box(&amp;win, TRUE);
  1913. break;
  1914. case KEY_RIGHT:
  1915. create_box(&amp;win, FALSE);
  1916. ++win.startx;
  1917. create_box(&amp;win, TRUE);
  1918. break;
  1919. case KEY_UP:
  1920. create_box(&amp;win, FALSE);
  1921. --win.starty;
  1922. create_box(&amp;win, TRUE);
  1923. break;
  1924. case KEY_DOWN:
  1925. create_box(&amp;win, FALSE);
  1926. ++win.starty;
  1927. create_box(&amp;win, TRUE);
  1928. break;
  1929. }
  1930. }
  1931. endwin(); /* End curses mode */
  1932. return 0;
  1933. }
  1934. void init_win_params(WIN *p_win)
  1935. {
  1936. p_win-&gt;height = 3;
  1937. p_win-&gt;width = 10;
  1938. p_win-&gt;starty = (LINES - p_win-&gt;height)/2;
  1939. p_win-&gt;startx = (COLS - p_win-&gt;width)/2;
  1940. p_win-&gt;border.ls = '|';
  1941. p_win-&gt;border.rs = '|';
  1942. p_win-&gt;border.ts = '-';
  1943. p_win-&gt;border.bs = '-';
  1944. p_win-&gt;border.tl = '+';
  1945. p_win-&gt;border.tr = '+';
  1946. p_win-&gt;border.bl = '+';
  1947. p_win-&gt;border.br = '+';
  1948. }
  1949. void print_win_params(WIN *p_win)
  1950. {
  1951. #ifdef _DEBUG
  1952. mvprintw(25, 0, "%d %d %d %d", p_win-&gt;startx, p_win-&gt;starty,
  1953. p_win-&gt;width, p_win-&gt;height);
  1954. refresh();
  1955. #endif
  1956. }
  1957. void create_box(WIN *p_win, bool flag)
  1958. { int i, j;
  1959. int x, y, w, h;
  1960. x = p_win-&gt;startx;
  1961. y = p_win-&gt;starty;
  1962. w = p_win-&gt;width;
  1963. h = p_win-&gt;height;
  1964. if(flag == TRUE)
  1965. { mvaddch(y, x, p_win-&gt;border.tl);
  1966. mvaddch(y, x + w, p_win-&gt;border.tr);
  1967. mvaddch(y + h, x, p_win-&gt;border.bl);
  1968. mvaddch(y + h, x + w, p_win-&gt;border.br);
  1969. mvhline(y, x + 1, p_win-&gt;border.ts, w - 1);
  1970. mvhline(y + h, x + 1, p_win-&gt;border.bs, w - 1);
  1971. mvvline(y + 1, x, p_win-&gt;border.ls, h - 1);
  1972. mvvline(y + 1, x + w, p_win-&gt;border.rs, h - 1);
  1973. }
  1974. else
  1975. for(j = y; j &lt;= y + h; ++j)
  1976. for(i = x; i &lt;= x + w; ++i)
  1977. mvaddch(j, i, ' ');
  1978. refresh();
  1979. }</span></font>
  1980. </pre></td>
  1981. </tr>
  1982. </table>
  1983. </div>
  1984. </div>
  1985. </div>
  1986. <div class="SECT1">
  1987. <hr>
  1988. <h2 class="SECT1"><a name="COLOR" id="COLOR">10. Colors</a></h2>
  1989. <div class="SECT2">
  1990. <h3 class="SECT2"><a name="COLORBASICS" id="COLORBASICS">10.1. The
  1991. basics</a></h3>
  1992. <p>Life seems dull with no colors. Curses has a nice mechanism to
  1993. handle colors. Let's get into the thick of the things with a small
  1994. program.</p>
  1995. <div class="EXAMPLE"><a name="BSICO" id="BSICO"></a>
  1996. <p><b>Example 9. A Simple Color example</b></p>
  1997. <table border="0" bgcolor="#E0E0E0" width="100%">
  1998. <tr>
  1999. <td>
  2000. <pre class="PROGRAMLISTING">
  2001. <font color="#000000"><span class=
  2002. "INLINEMEDIAOBJECT">#include &lt;ncurses.h&gt;
  2003. void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string);
  2004. int main(int argc, char *argv[])
  2005. { initscr(); /* Start curses mode */
  2006. if(has_colors() == FALSE)
  2007. { endwin();
  2008. printf("Your terminal does not support color\n");
  2009. exit(1);
  2010. }
  2011. start_color(); /* Start color */
  2012. init_pair(1, COLOR_RED, COLOR_BLACK);
  2013. attron(COLOR_PAIR(1));
  2014. print_in_middle(stdscr, LINES / 2, 0, 0, "Viola !!! In color ...");
  2015. attroff(COLOR_PAIR(1));
  2016. getch();
  2017. endwin();
  2018. }
  2019. void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string)
  2020. { int length, x, y;
  2021. float temp;
  2022. if(win == NULL)
  2023. win = stdscr;
  2024. getyx(win, y, x);
  2025. if(startx != 0)
  2026. x = startx;
  2027. if(starty != 0)
  2028. y = starty;
  2029. if(width == 0)
  2030. width = 80;
  2031. length = strlen(string);
  2032. temp = (width - length)/ 2;
  2033. x = startx + (int)temp;
  2034. mvwprintw(win, y, x, "%s", string);
  2035. refresh();
  2036. }
  2037. </span></font>
  2038. </pre></td>
  2039. </tr>
  2040. </table>
  2041. </div>
  2042. <p>As you can see, to start using color, you should first call the
  2043. function <var class="LITERAL">start_color()</var>. After that, you
  2044. can use color capabilities of your terminals using various
  2045. functions. To find out whether a terminal has color capabilities or
  2046. not, you can use <var class="LITERAL">has_colors()</var> function,
  2047. which returns FALSE if the terminal does not support color.</p>
  2048. <p>Curses initializes all the colors supported by terminal when
  2049. start_color() is called. These can be accessed by the define
  2050. constants like <var class="LITERAL">COLOR_BLACK</var> etc. Now to
  2051. actually start using colors, you have to define pairs. Colors are
  2052. always used in pairs. That means you have to use the function
  2053. <var class="LITERAL">init_pair()</var> to define the foreground and
  2054. background for the pair number you give. After that that pair
  2055. number can be used as a normal attribute with <var class=
  2056. "LITERAL">COLOR_PAIR()</var>function. This may seem to be
  2057. cumbersome at first. But this elegant solution allows us to manage
  2058. color pairs very easily. To appreciate it, you have to look into
  2059. the the source code of "dialog", a utility for displaying dialog
  2060. boxes from shell scripts. The developers have defined foreground
  2061. and background combinations for all the colors they might need and
  2062. initialized at the beginning. This makes it very easy to set
  2063. attributes just by accessing a pair which we already have defined
  2064. as a constant.</p>
  2065. <p>The following colors are defined in <var class=
  2066. "LITERAL">curses.h</var>. You can use these as parameters for
  2067. various color functions.</p>
  2068. <table border="0" bgcolor="#E0E0E0" width="100%">
  2069. <tr>
  2070. <td>
  2071. <pre class="PROGRAMLISTING">
  2072. <font color="#000000"> COLOR_BLACK 0
  2073. COLOR_RED 1
  2074. COLOR_GREEN 2
  2075. COLOR_YELLOW 3
  2076. COLOR_BLUE 4
  2077. COLOR_MAGENTA 5
  2078. COLOR_CYAN 6
  2079. COLOR_WHITE 7</font>
  2080. </pre></td>
  2081. </tr>
  2082. </table>
  2083. </div>
  2084. <div class="SECT2">
  2085. <hr>
  2086. <h3 class="SECT2"><a name="CHANGECOLORDEFS" id=
  2087. "CHANGECOLORDEFS">10.2. Changing Color Definitions</a></h3>
  2088. <p>The function <var class="LITERAL">init_color()</var>can be used
  2089. to change the rgb values for the colors defined by curses
  2090. initially. Say you wanted to lighten the intensity of red color by
  2091. a minuscule. Then you can use this function as</p>
  2092. <table border="0" bgcolor="#E0E0E0" width="100%">
  2093. <tr>
  2094. <td>
  2095. <pre class="PROGRAMLISTING">
  2096. <font color="#000000"> init_color(COLOR_RED, 700, 0, 0);
  2097. /* param 1 : color name
  2098. * param 2, 3, 4 : rgb content min = 0, max = 1000 */</font>
  2099. </pre></td>
  2100. </tr>
  2101. </table>
  2102. <p>If your terminal cannot change the color definitions, the
  2103. function returns ERR. The function <var class=
  2104. "LITERAL">can_change_color()</var> can be used to find out whether
  2105. the terminal has the capability of changing color content or not.
  2106. The rgb content is scaled from 0 to 1000. Initially RED color is
  2107. defined with content 1000(r), 0(g), 0(b).</p>
  2108. </div>
  2109. <div class="SECT2">
  2110. <hr>
  2111. <h3 class="SECT2"><a name="COLORCONTENT" id="COLORCONTENT">10.3.
  2112. Color Content</a></h3>
  2113. <p>The functions <var class="LITERAL">color_content()</var> and
  2114. <var class="LITERAL">pair_content()</var> can be used to find the
  2115. color content and foreground, background combination for the
  2116. pair.</p>
  2117. </div>
  2118. </div>
  2119. <div class="SECT1">
  2120. <hr>
  2121. <h2 class="SECT1"><a name="KEYS" id="KEYS">11. Interfacing with the
  2122. key board</a></h2>
  2123. <div class="SECT2">
  2124. <h3 class="SECT2"><a name="KEYSBASICS" id="KEYSBASICS">11.1. The
  2125. Basics</a></h3>
  2126. <p>No GUI is complete without a strong user interface and to
  2127. interact with the user, a curses program should be sensitive to key
  2128. presses or the mouse actions done by the user. Let's deal with the
  2129. keys first.</p>
  2130. <p>As you have seen in almost all of the above examples, it's very
  2131. easy to get key input from the user. A simple way of getting key
  2132. presses is to use <var class="LITERAL">getch()</var> function. The
  2133. cbreak mode should be enabled to read keys when you are interested
  2134. in reading individual key hits rather than complete lines of text
  2135. (which usually end with a carriage return). keypad should be
  2136. enabled to get the Functions keys, arrow keys etc. See the
  2137. initialization section for details.</p>
  2138. <p><var class="LITERAL">getch()</var> returns an integer
  2139. corresponding to the key pressed. If it is a normal character, the
  2140. integer value will be equivalent to the character. Otherwise it
  2141. returns a number which can be matched with the constants defined in
  2142. <var class="LITERAL">curses.h</var>. For example if the user
  2143. presses F1, the integer returned is 265. This can be checked using
  2144. the macro KEY_F() defined in curses.h. This makes reading keys
  2145. portable and easy to manage.</p>
  2146. <p>For example, if you call getch() like this</p>
  2147. <table border="0" bgcolor="#E0E0E0" width="100%">
  2148. <tr>
  2149. <td>
  2150. <pre class="PROGRAMLISTING">
  2151. <font color="#000000"> int ch;
  2152. ch = getch();</font>
  2153. </pre></td>
  2154. </tr>
  2155. </table>
  2156. <p>getch() will wait for the user to press a key, (unless you
  2157. specified a timeout) and when user presses a key, the corresponding
  2158. integer is returned. Then you can check the value returned with the
  2159. constants defined in curses.h to match against the keys you
  2160. want.</p>
  2161. <p>The following code piece will do that job.</p>
  2162. <table border="0" bgcolor="#E0E0E0" width="100%">
  2163. <tr>
  2164. <td>
  2165. <pre class="PROGRAMLISTING">
  2166. <font color="#000000"> if(ch == KEY_LEFT)
  2167. printw("Left arrow is pressed\n");</font>
  2168. </pre></td>
  2169. </tr>
  2170. </table>
  2171. <p>Let's write a small program which creates a menu which can be
  2172. navigated by up and down arrows.</p>
  2173. </div>
  2174. <div class="SECT2">
  2175. <hr>
  2176. <h3 class="SECT2"><a name="SIMPLEKEYEX" id="SIMPLEKEYEX">11.2. A
  2177. Simple Key Usage example</a></h3>
  2178. <div class="EXAMPLE"><a name="BSIKE" id="BSIKE"></a>
  2179. <p><b>Example 10. A Simple Key Usage example</b></p>
  2180. <table border="0" bgcolor="#E0E0E0" width="100%">
  2181. <tr>
  2182. <td>
  2183. <pre class="PROGRAMLISTING">
  2184. <font color="#000000"><span class=
  2185. "INLINEMEDIAOBJECT">#include &lt;stdio.h&gt;
  2186. #include &lt;ncurses.h&gt;
  2187. #define WIDTH 30
  2188. #define HEIGHT 10
  2189. int startx = 0;
  2190. int starty = 0;
  2191. char *choices[] = {
  2192. "Choice 1",
  2193. "Choice 2",
  2194. "Choice 3",
  2195. "Choice 4",
  2196. "Exit",
  2197. };
  2198. int n_choices = sizeof(choices) / sizeof(char *);
  2199. void print_menu(WINDOW *menu_win, int highlight);
  2200. int main()
  2201. { WINDOW *menu_win;
  2202. int highlight = 1;
  2203. int choice = 0;
  2204. int c;
  2205. initscr();
  2206. clear();
  2207. noecho();
  2208. cbreak(); /* Line buffering disabled. pass on everything */
  2209. startx = (80 - WIDTH) / 2;
  2210. starty = (24 - HEIGHT) / 2;
  2211. menu_win = newwin(HEIGHT, WIDTH, starty, startx);
  2212. keypad(menu_win, TRUE);
  2213. mvprintw(0, 0, "Use arrow keys to go up and down, Press enter to select a choice");
  2214. refresh();
  2215. print_menu(menu_win, highlight);
  2216. while(1)
  2217. { c = wgetch(menu_win);
  2218. switch(c)
  2219. { case KEY_UP:
  2220. if(highlight == 1)
  2221. highlight = n_choices;
  2222. else
  2223. --highlight;
  2224. break;
  2225. case KEY_DOWN:
  2226. if(highlight == n_choices)
  2227. highlight = 1;
  2228. else
  2229. ++highlight;
  2230. break;
  2231. case 10:
  2232. choice = highlight;
  2233. break;
  2234. default:
  2235. mvprintw(24, 0, "Charcter pressed is = %3d Hopefully it can be printed as '%c'", c, c);
  2236. refresh();
  2237. break;
  2238. }
  2239. print_menu(menu_win, highlight);
  2240. if(choice != 0) /* User did a choice come out of the infinite loop */
  2241. break;
  2242. }
  2243. mvprintw(23, 0, "You chose choice %d with choice string %s\n", choice, choices[choice - 1]);
  2244. clrtoeol();
  2245. refresh();
  2246. endwin();
  2247. return 0;
  2248. }
  2249. void print_menu(WINDOW *menu_win, int highlight)
  2250. {
  2251. int x, y, i;
  2252. x = 2;
  2253. y = 2;
  2254. box(menu_win, 0, 0);
  2255. for(i = 0; i &lt; n_choices; ++i)
  2256. { if(highlight == i + 1) /* High light the present choice */
  2257. { wattron(menu_win, A_REVERSE);
  2258. mvwprintw(menu_win, y, x, "%s", choices[i]);
  2259. wattroff(menu_win, A_REVERSE);
  2260. }
  2261. else
  2262. mvwprintw(menu_win, y, x, "%s", choices[i]);
  2263. ++y;
  2264. }
  2265. wrefresh(menu_win);
  2266. }
  2267. </span></font>
  2268. </pre></td>
  2269. </tr>
  2270. </table>
  2271. </div>
  2272. </div>
  2273. </div>
  2274. <div class="SECT1">
  2275. <hr>
  2276. <h2 class="SECT1"><a name="MOUSE" id="MOUSE">12. Interfacing with
  2277. the mouse</a></h2>
  2278. <p>Now that you have seen how to get keys, lets do the same thing
  2279. from mouse. Usually each UI allows the user to interact with both
  2280. keyboard and mouse.</p>
  2281. <div class="SECT2">
  2282. <hr>
  2283. <h3 class="SECT2"><a name="MOUSEBASICS" id="MOUSEBASICS">12.1. The
  2284. Basics</a></h3>
  2285. <p>Before you do any thing else, the events you want to receive
  2286. have to be enabled with <var class="LITERAL">mousemask()</var>.</p>
  2287. <table border="0" bgcolor="#E0E0E0" width="100%">
  2288. <tr>
  2289. <td>
  2290. <pre class="PROGRAMLISTING">
  2291. <font color=
  2292. "#000000"> mousemask( mmask_t newmask, /* The events you want to listen to */
  2293. mmask_t *oldmask) /* The old events mask */</font>
  2294. </pre></td>
  2295. </tr>
  2296. </table>
  2297. <p>The first parameter to above function is a bit mask of events
  2298. you would like to listen. By default, all the events are turned
  2299. off. The bit mask <var class="LITERAL">ALL_MOUSE_EVENTS</var> can
  2300. be used to get all the events.</p>
  2301. <p>The following are all the event masks:</p>
  2302. <table border="0" bgcolor="#E0E0E0" width="100%">
  2303. <tr>
  2304. <td>
  2305. <pre class="PROGRAMLISTING">
  2306. <font color="#000000"> Name Description
  2307. ---------------------------------------------------------------------
  2308. BUTTON1_PRESSED mouse button 1 down
  2309. BUTTON1_RELEASED mouse button 1 up
  2310. BUTTON1_CLICKED mouse button 1 clicked
  2311. BUTTON1_DOUBLE_CLICKED mouse button 1 double clicked
  2312. BUTTON1_TRIPLE_CLICKED mouse button 1 triple clicked
  2313. BUTTON2_PRESSED mouse button 2 down
  2314. BUTTON2_RELEASED mouse button 2 up
  2315. BUTTON2_CLICKED mouse button 2 clicked
  2316. BUTTON2_DOUBLE_CLICKED mouse button 2 double clicked
  2317. BUTTON2_TRIPLE_CLICKED mouse button 2 triple clicked
  2318. BUTTON3_PRESSED mouse button 3 down
  2319. BUTTON3_RELEASED mouse button 3 up
  2320. BUTTON3_CLICKED mouse button 3 clicked
  2321. BUTTON3_DOUBLE_CLICKED mouse button 3 double clicked
  2322. BUTTON3_TRIPLE_CLICKED mouse button 3 triple clicked
  2323. BUTTON4_PRESSED mouse button 4 down
  2324. BUTTON4_RELEASED mouse button 4 up
  2325. BUTTON4_CLICKED mouse button 4 clicked
  2326. BUTTON4_DOUBLE_CLICKED mouse button 4 double clicked
  2327. BUTTON4_TRIPLE_CLICKED mouse button 4 triple clicked
  2328. BUTTON_SHIFT shift was down during button state change
  2329. BUTTON_CTRL control was down during button state change
  2330. BUTTON_ALT alt was down during button state change
  2331. ALL_MOUSE_EVENTS report all button state changes
  2332. REPORT_MOUSE_POSITION report mouse movement</font>
  2333. </pre></td>
  2334. </tr>
  2335. </table>
  2336. </div>
  2337. <div class="SECT2">
  2338. <hr>
  2339. <h3 class="SECT2"><a name="GETTINGEVENTS" id="GETTINGEVENTS">12.2.
  2340. Getting the events</a></h3>
  2341. <p>Once a class of mouse events have been enabled, getch() class of
  2342. functions return KEY_MOUSE every time some mouse event happens.
  2343. Then the mouse event can be retrieved with <var class=
  2344. "LITERAL">getmouse()</var>.</p>
  2345. <p>The code approximately looks like this:</p>
  2346. <table border="0" bgcolor="#E0E0E0" width="100%">
  2347. <tr>
  2348. <td>
  2349. <pre class="PROGRAMLISTING">
  2350. <font color="#000000"> MEVENT event;
  2351. ch = getch();
  2352. if(ch == KEY_MOUSE)
  2353. if(getmouse(&amp;event) == OK)
  2354. . /* Do some thing with the event */
  2355. .
  2356. .</font>
  2357. </pre></td>
  2358. </tr>
  2359. </table>
  2360. <p>getmouse() returns the event into the pointer given to it. It's
  2361. a structure which contains</p>
  2362. <table border="0" bgcolor="#E0E0E0" width="100%">
  2363. <tr>
  2364. <td>
  2365. <pre class="PROGRAMLISTING">
  2366. <font color="#000000"> typedef struct
  2367. {
  2368. short id; /* ID to distinguish multiple devices */
  2369. int x, y, z; /* event coordinates */
  2370. mmask_t bstate; /* button state bits */
  2371. } </font>
  2372. </pre></td>
  2373. </tr>
  2374. </table>
  2375. <p>The <var class="LITERAL">bstate</var> is the main variable we
  2376. are interested in. It tells the button state of the mouse.</p>
  2377. <p>Then with a code snippet like the following, we can find out
  2378. what happened.</p>
  2379. <table border="0" bgcolor="#E0E0E0" width="100%">
  2380. <tr>
  2381. <td>
  2382. <pre class="PROGRAMLISTING">
  2383. <font color="#000000"> if(event.bstate &amp; BUTTON1_PRESSED)
  2384. printw("Left Button Pressed");</font>
  2385. </pre></td>
  2386. </tr>
  2387. </table>
  2388. </div>
  2389. <div class="SECT2">
  2390. <hr>
  2391. <h3 class="SECT2"><a name="MOUSETOGETHER" id="MOUSETOGETHER">12.3.
  2392. Putting it all Together</a></h3>
  2393. <p>That's pretty much interfacing with mouse. Let's create the same
  2394. menu and enable mouse interaction. To make things simpler, key
  2395. handling is removed.</p>
  2396. <div class="EXAMPLE"><a name="BMOME" id="BMOME"></a>
  2397. <p><b>Example 11. Access the menu with mouse !!!</b></p>
  2398. <table border="0" bgcolor="#E0E0E0" width="100%">
  2399. <tr>
  2400. <td>
  2401. <pre class="PROGRAMLISTING">
  2402. <font color="#000000"><span class=
  2403. "INLINEMEDIAOBJECT">#include &lt;ncurses.h&gt;
  2404. #define WIDTH 30
  2405. #define HEIGHT 10
  2406. int startx = 0;
  2407. int starty = 0;
  2408. char *choices[] = { "Choice 1",
  2409. "Choice 2",
  2410. "Choice 3",
  2411. "Choice 4",
  2412. "Exit",
  2413. };
  2414. int n_choices = sizeof(choices) / sizeof(char *);
  2415. void print_menu(WINDOW *menu_win, int highlight);
  2416. void report_choice(int mouse_x, int mouse_y, int *p_choice);
  2417. int main()
  2418. { int c, choice = 0;
  2419. WINDOW *menu_win;
  2420. MEVENT event;
  2421. /* Initialize curses */
  2422. initscr();
  2423. clear();
  2424. noecho();
  2425. cbreak(); //Line buffering disabled. pass on everything
  2426. /* Try to put the window in the middle of screen */
  2427. startx = (80 - WIDTH) / 2;
  2428. starty = (24 - HEIGHT) / 2;
  2429. attron(A_REVERSE);
  2430. mvprintw(23, 1, "Click on Exit to quit (Works best in a virtual console)");
  2431. refresh();
  2432. attroff(A_REVERSE);
  2433. /* Print the menu for the first time */
  2434. menu_win = newwin(HEIGHT, WIDTH, starty, startx);
  2435. print_menu(menu_win, 1);
  2436. /* Get all the mouse events */
  2437. mousemask(ALL_MOUSE_EVENTS, NULL);
  2438. while(1)
  2439. { c = wgetch(menu_win);
  2440. switch(c)
  2441. { case KEY_MOUSE:
  2442. if(getmouse(&amp;event) == OK)
  2443. { /* When the user clicks left mouse button */
  2444. if(event.bstate &amp; BUTTON1_PRESSED)
  2445. { report_choice(event.x + 1, event.y + 1, &amp;choice);
  2446. if(choice == -1) //Exit chosen
  2447. goto end;
  2448. mvprintw(22, 1, "Choice made is : %d String Chosen is \"%10s\"", choice, choices[choice - 1]);
  2449. refresh();
  2450. }
  2451. }
  2452. print_menu(menu_win, choice);
  2453. break;
  2454. }
  2455. }
  2456. end:
  2457. endwin();
  2458. return 0;
  2459. }
  2460. void print_menu(WINDOW *menu_win, int highlight)
  2461. {
  2462. int x, y, i;
  2463. x = 2;
  2464. y = 2;
  2465. box(menu_win, 0, 0);
  2466. for(i = 0; i &lt; n_choices; ++i)
  2467. { if(highlight == i + 1)
  2468. { wattron(menu_win, A_REVERSE);
  2469. mvwprintw(menu_win, y, x, "%s", choices[i]);
  2470. wattroff(menu_win, A_REVERSE);
  2471. }
  2472. else
  2473. mvwprintw(menu_win, y, x, "%s", choices[i]);
  2474. ++y;
  2475. }
  2476. wrefresh(menu_win);
  2477. }
  2478. /* Report the choice according to mouse position */
  2479. void report_choice(int mouse_x, int mouse_y, int *p_choice)
  2480. { int i,j, choice;
  2481. i = startx + 2;
  2482. j = starty + 3;
  2483. for(choice = 0; choice &lt; n_choices; ++choice)
  2484. if(mouse_y == j + choice &amp;&amp; mouse_x &gt;= i &amp;&amp; mouse_x &lt;= i + strlen(choices[choice]))
  2485. { if(choice == n_choices - 1)
  2486. *p_choice = -1;
  2487. else
  2488. *p_choice = choice + 1;
  2489. break;
  2490. }
  2491. }</span></font>
  2492. </pre></td>
  2493. </tr>
  2494. </table>
  2495. </div>
  2496. </div>
  2497. <div class="SECT2">
  2498. <hr>
  2499. <h3 class="SECT2"><a name="MISCMOUSEFUNCS" id=
  2500. "MISCMOUSEFUNCS">12.4. Miscellaneous Functions</a></h3>
  2501. <p>The functions mouse_trafo() and wmouse_trafo() can be used to
  2502. convert to mouse co-ordinates to screen relative co-ordinates. See
  2503. curs_mouse(3X) man page for details.</p>
  2504. <p>The mouseinterval function sets the maximum time (in thousands
  2505. of a second) that can elapse between press and release events in
  2506. order for them to be recognized as a click. This function returns
  2507. the previous interval value. The default is one fifth of a
  2508. second.</p>
  2509. </div>
  2510. </div>
  2511. <div class="SECT1">
  2512. <hr>
  2513. <h2 class="SECT1"><a name="SCREEN" id="SCREEN">13. Screen
  2514. Manipulation</a></h2>
  2515. <p>In this section, we will look into some functions, which allow
  2516. us to manage the screen efficiently and to write some fancy
  2517. programs. This is especially important in writing games.</p>
  2518. <div class="SECT2">
  2519. <hr>
  2520. <h3 class="SECT2"><a name="GETYX" id="GETYX">13.1. getyx()
  2521. functions</a></h3>
  2522. <p>The function <var class="LITERAL">getyx()</var> can be used to
  2523. find out the present cursor co-ordinates. It will fill the values
  2524. of x and y co-ordinates in the arguments given to it. Since getyx()
  2525. is a macro you don't have to pass the address of the variables. It
  2526. can be called as</p>
  2527. <table border="0" bgcolor="#E0E0E0" width="100%">
  2528. <tr>
  2529. <td>
  2530. <pre class="PROGRAMLISTING">
  2531. <font color="#000000"> getyx(win, y, x);
  2532. /* win: window pointer
  2533. * y, x: y, x co-ordinates will be put into this variables
  2534. */</font>
  2535. </pre></td>
  2536. </tr>
  2537. </table>
  2538. <p>The function getparyx() gets the beginning co-ordinates of the
  2539. sub window relative to the main window. This is some times useful
  2540. to update a sub window. When designing fancy stuff like writing
  2541. multiple menus, it becomes difficult to store the menu positions,
  2542. their first option co-ordinates etc. A simple solution to this
  2543. problem, is to create menus in sub windows and later find the
  2544. starting co-ordinates of the menus by using getparyx().</p>
  2545. <p>The functions getbegyx() and getmaxyx() store current window's
  2546. beginning and maximum co-ordinates. These functions are useful in
  2547. the same way as above in managing the windows and sub windows
  2548. effectively.</p>
  2549. </div>
  2550. <div class="SECT2">
  2551. <hr>
  2552. <h3 class="SECT2"><a name="SCREENDUMP" id="SCREENDUMP">13.2. Screen
  2553. Dumping</a></h3>
  2554. <p>While writing games, some times it becomes necessary to store
  2555. the state of the screen and restore it back to the same state. The
  2556. function scr_dump() can be used to dump the screen contents to a
  2557. file given as an argument. Later it can be restored by scr_restore
  2558. function. These two simple functions can be used effectively to
  2559. maintain a fast moving game with changing scenarios.</p>
  2560. </div>
  2561. <div class="SECT2">
  2562. <hr>
  2563. <h3 class="SECT2"><a name="WINDOWDUMP" id="WINDOWDUMP">13.3. Window
  2564. Dumping</a></h3>
  2565. <p>To store and restore windows, the functions <var class=
  2566. "LITERAL">putwin()</var> and <var class="LITERAL">getwin()</var>
  2567. can be used. <var class="LITERAL">putwin()</var> puts the present
  2568. window state into a file, which can be later restored by
  2569. <var class="LITERAL">getwin()</var>.</p>
  2570. <p>The function <var class="LITERAL">copywin()</var> can be used to
  2571. copy a window completely onto another window. It takes the source
  2572. and destination windows as parameters and according to the
  2573. rectangle specified, it copies the rectangular region from source
  2574. to destination window. It's last parameter specifies whether to
  2575. overwrite or just overlay the contents on to the destination
  2576. window. If this argument is true, then the copying is
  2577. non-destructive.</p>
  2578. </div>
  2579. </div>
  2580. <div class="SECT1">
  2581. <hr>
  2582. <h2 class="SECT1"><a name="MISC" id="MISC">14. Miscellaneous
  2583. features</a></h2>
  2584. <p>Now you know enough features to write a good curses program,
  2585. with all bells and whistles. There are some miscellaneous functions
  2586. which are useful in various cases. Let's go headlong into some of
  2587. those.</p>
  2588. <div class="SECT2">
  2589. <hr>
  2590. <h3 class="SECT2"><a name="CURSSET" id="CURSSET">14.1.
  2591. curs_set()</a></h3>
  2592. <p>This function can be used to make the cursor invisible. The
  2593. parameter to this function should be</p>
  2594. <table border="0" bgcolor="#E0E0E0" width="100%">
  2595. <tr>
  2596. <td>
  2597. <pre class="PROGRAMLISTING">
  2598. <font color="#000000"> 0 : invisible or
  2599. 1 : normal or
  2600. 2 : very visible.</font>
  2601. </pre></td>
  2602. </tr>
  2603. </table>
  2604. </div>
  2605. <div class="SECT2">
  2606. <hr>
  2607. <h3 class="SECT2"><a name="TEMPLEAVE" id="TEMPLEAVE">14.2.
  2608. Temporarily Leaving Curses mode</a></h3>
  2609. <p>Some times you may want to get back to cooked mode (normal line
  2610. buffering mode) temporarily. In such a case you will first need to
  2611. save the tty modes with a call to <var class=
  2612. "LITERAL">def_prog_mode()</var> and then call <var class=
  2613. "LITERAL">endwin()</var> to end the curses mode. This will leave
  2614. you in the original tty mode. To get back to curses once you are
  2615. done, call <var class="LITERAL">reset_prog_mode()</var> . This
  2616. function returns the tty to the state stored by <var class=
  2617. "LITERAL">def_prog_mode()</var>. Then do refresh(), and you are
  2618. back to the curses mode. Here is an example showing the sequence of
  2619. things to be done.</p>
  2620. <div class="EXAMPLE"><a name="BTELE" id="BTELE"></a>
  2621. <p><b>Example 12. Temporarily Leaving Curses Mode</b></p>
  2622. <table border="0" bgcolor="#E0E0E0" width="100%">
  2623. <tr>
  2624. <td>
  2625. <pre class="PROGRAMLISTING">
  2626. <font color="#000000"><span class=
  2627. "INLINEMEDIAOBJECT">#include &lt;ncurses.h&gt;
  2628. int main()
  2629. {
  2630. initscr(); /* Start curses mode */
  2631. printw("Hello World !!!\n"); /* Print Hello World */
  2632. refresh(); /* Print it on to the real screen */
  2633. def_prog_mode(); /* Save the tty modes */
  2634. endwin(); /* End curses mode temporarily */
  2635. system("/bin/sh"); /* Do whatever you like in cooked mode */
  2636. reset_prog_mode(); /* Return to the previous tty mode*/
  2637. /* stored by def_prog_mode() */
  2638. refresh(); /* Do refresh() to restore the */
  2639. /* Screen contents */
  2640. printw("Another String\n"); /* Back to curses use the full */
  2641. refresh(); /* capabilities of curses */
  2642. endwin(); /* End curses mode */
  2643. return 0;
  2644. }</span></font>
  2645. </pre></td>
  2646. </tr>
  2647. </table>
  2648. </div>
  2649. </div>
  2650. <div class="SECT2">
  2651. <hr>
  2652. <h3 class="SECT2"><a name="ACSVARS" id="ACSVARS">14.3. ACS_
  2653. variables</a></h3>
  2654. <p>If you have ever programmed in DOS, you know about those nifty
  2655. characters in extended character set. They are printable only on
  2656. some terminals. NCURSES functions like <var class=
  2657. "LITERAL">box()</var> use these characters. All these variables
  2658. start with ACS meaning alternative character set. You might have
  2659. noticed me using these characters in some of the programs above.
  2660. Here's an example showing all the characters.</p>
  2661. <div class="EXAMPLE"><a name="BACSVARS" id="BACSVARS"></a>
  2662. <p><b>Example 13. ACS Variables Example</b></p>
  2663. <table border="0" bgcolor="#E0E0E0" width="100%">
  2664. <tr>
  2665. <td>
  2666. <pre class="PROGRAMLISTING">
  2667. <font color="#000000"><span class=
  2668. "INLINEMEDIAOBJECT">#include &lt;ncurses.h&gt;
  2669. int main()
  2670. {
  2671. initscr();
  2672. printw("Upper left corner "); addch(ACS_ULCORNER); printw("\n");
  2673. printw("Lower left corner "); addch(ACS_LLCORNER); printw("\n");
  2674. printw("Lower right corner "); addch(ACS_LRCORNER); printw("\n");
  2675. printw("Tee pointing right "); addch(ACS_LTEE); printw("\n");
  2676. printw("Tee pointing left "); addch(ACS_RTEE); printw("\n");
  2677. printw("Tee pointing up "); addch(ACS_BTEE); printw("\n");
  2678. printw("Tee pointing down "); addch(ACS_TTEE); printw("\n");
  2679. printw("Horizontal line "); addch(ACS_HLINE); printw("\n");
  2680. printw("Vertical line "); addch(ACS_VLINE); printw("\n");
  2681. printw("Large Plus or cross over "); addch(ACS_PLUS); printw("\n");
  2682. printw("Scan Line 1 "); addch(ACS_S1); printw("\n");
  2683. printw("Scan Line 3 "); addch(ACS_S3); printw("\n");
  2684. printw("Scan Line 7 "); addch(ACS_S7); printw("\n");
  2685. printw("Scan Line 9 "); addch(ACS_S9); printw("\n");
  2686. printw("Diamond "); addch(ACS_DIAMOND); printw("\n");
  2687. printw("Checker board (stipple) "); addch(ACS_CKBOARD); printw("\n");
  2688. printw("Degree Symbol "); addch(ACS_DEGREE); printw("\n");
  2689. printw("Plus/Minus Symbol "); addch(ACS_PLMINUS); printw("\n");
  2690. printw("Bullet "); addch(ACS_BULLET); printw("\n");
  2691. printw("Arrow Pointing Left "); addch(ACS_LARROW); printw("\n");
  2692. printw("Arrow Pointing Right "); addch(ACS_RARROW); printw("\n");
  2693. printw("Arrow Pointing Down "); addch(ACS_DARROW); printw("\n");
  2694. printw("Arrow Pointing Up "); addch(ACS_UARROW); printw("\n");
  2695. printw("Board of squares "); addch(ACS_BOARD); printw("\n");
  2696. printw("Lantern Symbol "); addch(ACS_LANTERN); printw("\n");
  2697. printw("Solid Square Block "); addch(ACS_BLOCK); printw("\n");
  2698. printw("Less/Equal sign "); addch(ACS_LEQUAL); printw("\n");
  2699. printw("Greater/Equal sign "); addch(ACS_GEQUAL); printw("\n");
  2700. printw("Pi "); addch(ACS_PI); printw("\n");
  2701. printw("Not equal "); addch(ACS_NEQUAL); printw("\n");
  2702. printw("UK pound sign "); addch(ACS_STERLING); printw("\n");
  2703. refresh();
  2704. getch();
  2705. endwin();
  2706. return 0;
  2707. }</span></font>
  2708. </pre></td>
  2709. </tr>
  2710. </table>
  2711. </div>
  2712. </div>
  2713. </div>
  2714. <div class="SECT1">
  2715. <hr>
  2716. <h2 class="SECT1"><a name="OTHERLIB" id="OTHERLIB">15. Other
  2717. libraries</a></h2>
  2718. <p>Apart from the curses library, there are few text mode
  2719. libraries, which provide more functionality and a lot of features.
  2720. The following sections explain three standard libraries which are
  2721. usually distributed along with curses.</p>
  2722. </div>
  2723. <div class="SECT1">
  2724. <hr>
  2725. <h2 class="SECT1"><a name="PANELS" id="PANELS">16. Panel
  2726. Library</a></h2>
  2727. <p>Now that you are proficient in curses, you wanted to do some
  2728. thing big. You created a lot of overlapping windows to give a
  2729. professional windows-type look. Unfortunately, it soon becomes
  2730. difficult to manage these. The multiple refreshes, updates plunge
  2731. you into a nightmare. The overlapping windows create blotches,
  2732. whenever you forget to refresh the windows in the proper order.</p>
  2733. <p>Don't despair. There's an elegant solution provided in panels
  2734. library. In the words of developers of ncurses</p>
  2735. <p><em>When your interface design is such that windows may dive
  2736. deeper into the visibility stack or pop to the top at runtime, the
  2737. resulting book-keeping can be tedious and difficult to get right.
  2738. Hence the panels library.</em></p>
  2739. <p>If you have lot of overlapping windows, then panels library is
  2740. the way to go. It obviates the need of doing series of
  2741. wnoutrefresh(), doupdate() and relieves the burden of doing it
  2742. correctly(bottom up). The library maintains information about the
  2743. order of windows, their overlapping and update the screen properly.
  2744. So why wait? Let's take a close peek into panels.</p>
  2745. <div class="SECT2">
  2746. <hr>
  2747. <h3 class="SECT2"><a name="PANELBASICS" id="PANELBASICS">16.1. The
  2748. Basics</a></h3>
  2749. <p>Panel object is a window that is implicitly treated as part of a
  2750. deck including all other panel objects. The deck is treated as a
  2751. stack with the top panel being completely visible and the other
  2752. panels may or may not be obscured according to their positions. So
  2753. the basic idea is to create a stack of overlapping panels and use
  2754. panels library to display them correctly. There is a function
  2755. similar to refresh() which, when called , displays panels in the
  2756. correct order. Functions are provided to hide or show panels, move
  2757. panels, change its size etc.. The overlapping problem is managed by
  2758. the panels library during all the calls to these functions.</p>
  2759. <p>The general flow of a panel program goes like this:</p>
  2760. <ol type="1">
  2761. <li>
  2762. <p>Create the windows (with newwin()) to be attached to the
  2763. panels.</p>
  2764. </li>
  2765. <li>
  2766. <p>Create panels with the chosen visibility order. Stack them up
  2767. according to the desired visibility. The function new_panel() is
  2768. used to created panels.</p>
  2769. </li>
  2770. <li>
  2771. <p>Call update_panels() to write the panels to the virtual screen
  2772. in correct visibility order. Do a doupdate() to show it on the
  2773. screen.</p>
  2774. </li>
  2775. <li>
  2776. <p>Mainpulate the panels with show_panel(), hide_panel(),
  2777. move_panel() etc. Make use of helper functions like panel_hidden()
  2778. and panel_window(). Make use of user pointer to store custom data
  2779. for a panel. Use the functions set_panel_userptr() and
  2780. panel_userptr() to set and get the user pointer for a panel.</p>
  2781. </li>
  2782. <li>
  2783. <p>When you are done with the panel use del_panel() to delete the
  2784. panel.</p>
  2785. </li>
  2786. </ol>
  2787. <p>Let's make the concepts clear, with some programs. The following
  2788. is a simple program which creates 3 overlapping panels and shows
  2789. them on the screen.</p>
  2790. </div>
  2791. <div class="SECT2">
  2792. <hr>
  2793. <h3 class="SECT2"><a name="COMPILEPANELS" id="COMPILEPANELS">16.2.
  2794. Compiling With the Panels Library</a></h3>
  2795. <p>To use panels library functions, you have to include panel.h and
  2796. to link the program with panels library the flag -lpanel should be
  2797. added along with -lncurses in that order.</p>
  2798. <table border="0" bgcolor="#E0E0E0" width="100%">
  2799. <tr>
  2800. <td>
  2801. <pre class="PROGRAMLISTING">
  2802. <font color="#000000"> #include &lt;panel.h&gt;
  2803. .
  2804. .
  2805. .
  2806. compile and link: gcc &lt;program file&gt; -lpanel -lncurses</font>
  2807. </pre></td>
  2808. </tr>
  2809. </table>
  2810. <div class="EXAMPLE"><a name="PPASI" id="PPASI"></a>
  2811. <p><b>Example 14. Panel basics</b></p>
  2812. <table border="0" bgcolor="#E0E0E0" width="100%">
  2813. <tr>
  2814. <td>
  2815. <pre class="PROGRAMLISTING">
  2816. <font color="#000000"><span class=
  2817. "INLINEMEDIAOBJECT">#include &lt;panel.h&gt;
  2818. int main()
  2819. { WINDOW *my_wins[3];
  2820. PANEL *my_panels[3];
  2821. int lines = 10, cols = 40, y = 2, x = 4, i;
  2822. initscr();
  2823. cbreak();
  2824. noecho();
  2825. /* Create windows for the panels */
  2826. my_wins[0] = newwin(lines, cols, y, x);
  2827. my_wins[1] = newwin(lines, cols, y + 1, x + 5);
  2828. my_wins[2] = newwin(lines, cols, y + 2, x + 10);
  2829. /*
  2830. * Create borders around the windows so that you can see the effect
  2831. * of panels
  2832. */
  2833. for(i = 0; i &lt; 3; ++i)
  2834. box(my_wins[i], 0, 0);
  2835. /* Attach a panel to each window */ /* Order is bottom up */
  2836. my_panels[0] = new_panel(my_wins[0]); /* Push 0, order: stdscr-0 */
  2837. my_panels[1] = new_panel(my_wins[1]); /* Push 1, order: stdscr-0-1 */
  2838. my_panels[2] = new_panel(my_wins[2]); /* Push 2, order: stdscr-0-1-2 */
  2839. /* Update the stacking order. 2nd panel will be on top */
  2840. update_panels();
  2841. /* Show it on the screen */
  2842. doupdate();
  2843. getch();
  2844. endwin();
  2845. }
  2846. </span></font>
  2847. </pre></td>
  2848. </tr>
  2849. </table>
  2850. </div>
  2851. <p>As you can see, above program follows a simple flow as
  2852. explained. The windows are created with newwin() and then they are
  2853. attached to panels with new_panel(). As we attach one panel after
  2854. another, the stack of panels gets updated. To put them on screen
  2855. update_panels() and doupdate() are called.</p>
  2856. </div>
  2857. <div class="SECT2">
  2858. <hr>
  2859. <h3 class="SECT2"><a name="PANELBROWSING" id="PANELBROWSING">16.3.
  2860. Panel Window Browsing</a></h3>
  2861. <p>A slightly complicated example is given below. This program
  2862. creates 3 windows which can be cycled through using tab. Have a
  2863. look at the code.</p>
  2864. <div class="EXAMPLE"><a name="PPABR" id="PPABR"></a>
  2865. <p><b>Example 15. Panel Window Browsing Example</b></p>
  2866. <table border="0" bgcolor="#E0E0E0" width="100%">
  2867. <tr>
  2868. <td>
  2869. <pre class="PROGRAMLISTING">
  2870. <font color="#000000"><span class=
  2871. "INLINEMEDIAOBJECT">#include &lt;panel.h&gt;
  2872. #define NLINES 10
  2873. #define NCOLS 40
  2874. void init_wins(WINDOW **wins, int n);
  2875. void win_show(WINDOW *win, char *label, int label_color);
  2876. void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color);
  2877. int main()
  2878. { WINDOW *my_wins[3];
  2879. PANEL *my_panels[3];
  2880. PANEL *top;
  2881. int ch;
  2882. /* Initialize curses */
  2883. initscr();
  2884. start_color();
  2885. cbreak();
  2886. noecho();
  2887. keypad(stdscr, TRUE);
  2888. /* Initialize all the colors */
  2889. init_pair(1, COLOR_RED, COLOR_BLACK);
  2890. init_pair(2, COLOR_GREEN, COLOR_BLACK);
  2891. init_pair(3, COLOR_BLUE, COLOR_BLACK);
  2892. init_pair(4, COLOR_CYAN, COLOR_BLACK);
  2893. init_wins(my_wins, 3);
  2894. /* Attach a panel to each window */ /* Order is bottom up */
  2895. my_panels[0] = new_panel(my_wins[0]); /* Push 0, order: stdscr-0 */
  2896. my_panels[1] = new_panel(my_wins[1]); /* Push 1, order: stdscr-0-1 */
  2897. my_panels[2] = new_panel(my_wins[2]); /* Push 2, order: stdscr-0-1-2 */
  2898. /* Set up the user pointers to the next panel */
  2899. set_panel_userptr(my_panels[0], my_panels[1]);
  2900. set_panel_userptr(my_panels[1], my_panels[2]);
  2901. set_panel_userptr(my_panels[2], my_panels[0]);
  2902. /* Update the stacking order. 2nd panel will be on top */
  2903. update_panels();
  2904. /* Show it on the screen */
  2905. attron(COLOR_PAIR(4));
  2906. mvprintw(LINES - 2, 0, "Use tab to browse through the windows (F1 to Exit)");
  2907. attroff(COLOR_PAIR(4));
  2908. doupdate();
  2909. top = my_panels[2];
  2910. while((ch = getch()) != KEY_F(1))
  2911. { switch(ch)
  2912. { case 9:
  2913. top = (PANEL *)panel_userptr(top);
  2914. top_panel(top);
  2915. break;
  2916. }
  2917. update_panels();
  2918. doupdate();
  2919. }
  2920. endwin();
  2921. return 0;
  2922. }
  2923. /* Put all the windows */
  2924. void init_wins(WINDOW **wins, int n)
  2925. { int x, y, i;
  2926. char label[80];
  2927. y = 2;
  2928. x = 10;
  2929. for(i = 0; i &lt; n; ++i)
  2930. { wins[i] = newwin(NLINES, NCOLS, y, x);
  2931. sprintf(label, "Window Number %d", i + 1);
  2932. win_show(wins[i], label, i + 1);
  2933. y += 3;
  2934. x += 7;
  2935. }
  2936. }
  2937. /* Show the window with a border and a label */
  2938. void win_show(WINDOW *win, char *label, int label_color)
  2939. { int startx, starty, height, width;
  2940. getbegyx(win, starty, startx);
  2941. getmaxyx(win, height, width);
  2942. box(win, 0, 0);
  2943. mvwaddch(win, 2, 0, ACS_LTEE);
  2944. mvwhline(win, 2, 1, ACS_HLINE, width - 2);
  2945. mvwaddch(win, 2, width - 1, ACS_RTEE);
  2946. print_in_middle(win, 1, 0, width, label, COLOR_PAIR(label_color));
  2947. }
  2948. void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color)
  2949. { int length, x, y;
  2950. float temp;
  2951. if(win == NULL)
  2952. win = stdscr;
  2953. getyx(win, y, x);
  2954. if(startx != 0)
  2955. x = startx;
  2956. if(starty != 0)
  2957. y = starty;
  2958. if(width == 0)
  2959. width = 80;
  2960. length = strlen(string);
  2961. temp = (width - length)/ 2;
  2962. x = startx + (int)temp;
  2963. wattron(win, color);
  2964. mvwprintw(win, y, x, "%s", string);
  2965. wattroff(win, color);
  2966. refresh();
  2967. }</span></font>
  2968. </pre></td>
  2969. </tr>
  2970. </table>
  2971. </div>
  2972. </div>
  2973. <div class="SECT2">
  2974. <hr>
  2975. <h3 class="SECT2"><a name="USERPTRUSING" id="USERPTRUSING">16.4.
  2976. Using User Pointers</a></h3>
  2977. <p>In the above example I used user pointers to find out the next
  2978. window in the cycle. We can attach custom information to the panel
  2979. by specifying a user pointer, which can point to any information
  2980. you want to store. In this case I stored the pointer to the next
  2981. panel in the cycle. User pointer for a panel can be set with the
  2982. function <var class="LITERAL">set_panel_userptr()</var>. It can be
  2983. accessed using the function <var class=
  2984. "LITERAL">panel_userptr()</var> which will return the user pointer
  2985. for the panel given as argument. After finding the next panel in
  2986. the cycle It's brought to the top by the function top_panel(). This
  2987. function brings the panel given as argument to the top of the panel
  2988. stack.</p>
  2989. </div>
  2990. <div class="SECT2">
  2991. <hr>
  2992. <h3 class="SECT2"><a name="PANELMOVERESIZE" id=
  2993. "PANELMOVERESIZE">16.5. Moving and Resizing Panels</a></h3>
  2994. <p>The function <var class="LITERAL">move_panel()</var> can be used
  2995. to move a panel to the desired location. It does not change the
  2996. position of the panel in the stack. Make sure that you use
  2997. move_panel() instead mvwin() on the window associated with the
  2998. panel.</p>
  2999. <p>Resizing a panel is slightly complex. There is no straight
  3000. forward function just to resize the window associated with a panel.
  3001. A solution to resize a panel is to create a new window with the
  3002. desired sizes, change the window associated with the panel using
  3003. replace_panel(). Don't forget to delete the old window. The window
  3004. associated with a panel can be found by using the function
  3005. panel_window().</p>
  3006. <p>The following program shows these concepts, in supposedly simple
  3007. program. You can cycle through the window with &lt;TAB&gt; as
  3008. usual. To resize or move the active panel press 'r' for resize 'm'
  3009. for moving. Then use arrow keys to resize or move it to the desired
  3010. way and press enter to end your resizing or moving. This example
  3011. makes use of user data to get the required data to do the
  3012. operations.</p>
  3013. <div class="EXAMPLE"><a name="PPARE" id="PPARE"></a>
  3014. <p><b>Example 16. Panel Moving and Resizing example</b></p>
  3015. <table border="0" bgcolor="#E0E0E0" width="100%">
  3016. <tr>
  3017. <td>
  3018. <pre class="PROGRAMLISTING">
  3019. <font color="#000000"><span class=
  3020. "INLINEMEDIAOBJECT">#include &lt;panel.h&gt;
  3021. typedef struct _PANEL_DATA {
  3022. int x, y, w, h;
  3023. char label[80];
  3024. int label_color;
  3025. PANEL *next;
  3026. }PANEL_DATA;
  3027. #define NLINES 10
  3028. #define NCOLS 40
  3029. void init_wins(WINDOW **wins, int n);
  3030. void win_show(WINDOW *win, char *label, int label_color);
  3031. void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color);
  3032. void set_user_ptrs(PANEL **panels, int n);
  3033. int main()
  3034. { WINDOW *my_wins[3];
  3035. PANEL *my_panels[3];
  3036. PANEL_DATA *top;
  3037. PANEL *stack_top;
  3038. WINDOW *temp_win, *old_win;
  3039. int ch;
  3040. int newx, newy, neww, newh;
  3041. int size = FALSE, move = FALSE;
  3042. /* Initialize curses */
  3043. initscr();
  3044. start_color();
  3045. cbreak();
  3046. noecho();
  3047. keypad(stdscr, TRUE);
  3048. /* Initialize all the colors */
  3049. init_pair(1, COLOR_RED, COLOR_BLACK);
  3050. init_pair(2, COLOR_GREEN, COLOR_BLACK);
  3051. init_pair(3, COLOR_BLUE, COLOR_BLACK);
  3052. init_pair(4, COLOR_CYAN, COLOR_BLACK);
  3053. init_wins(my_wins, 3);
  3054. /* Attach a panel to each window */ /* Order is bottom up */
  3055. my_panels[0] = new_panel(my_wins[0]); /* Push 0, order: stdscr-0 */
  3056. my_panels[1] = new_panel(my_wins[1]); /* Push 1, order: stdscr-0-1 */
  3057. my_panels[2] = new_panel(my_wins[2]); /* Push 2, order: stdscr-0-1-2 */
  3058. set_user_ptrs(my_panels, 3);
  3059. /* Update the stacking order. 2nd panel will be on top */
  3060. update_panels();
  3061. /* Show it on the screen */
  3062. attron(COLOR_PAIR(4));
  3063. mvprintw(LINES - 3, 0, "Use 'm' for moving, 'r' for resizing");
  3064. mvprintw(LINES - 2, 0, "Use tab to browse through the windows (F1 to Exit)");
  3065. attroff(COLOR_PAIR(4));
  3066. doupdate();
  3067. stack_top = my_panels[2];
  3068. top = (PANEL_DATA *)panel_userptr(stack_top);
  3069. newx = top-&gt;x;
  3070. newy = top-&gt;y;
  3071. neww = top-&gt;w;
  3072. newh = top-&gt;h;
  3073. while((ch = getch()) != KEY_F(1))
  3074. { switch(ch)
  3075. { case 9: /* Tab */
  3076. top = (PANEL_DATA *)panel_userptr(stack_top);
  3077. top_panel(top-&gt;next);
  3078. stack_top = top-&gt;next;
  3079. top = (PANEL_DATA *)panel_userptr(stack_top);
  3080. newx = top-&gt;x;
  3081. newy = top-&gt;y;
  3082. neww = top-&gt;w;
  3083. newh = top-&gt;h;
  3084. break;
  3085. case 'r': /* Re-Size*/
  3086. size = TRUE;
  3087. attron(COLOR_PAIR(4));
  3088. mvprintw(LINES - 4, 0, "Entered Resizing :Use Arrow Keys to resize and press &lt;ENTER&gt; to end resizing");
  3089. refresh();
  3090. attroff(COLOR_PAIR(4));
  3091. break;
  3092. case 'm': /* Move */
  3093. attron(COLOR_PAIR(4));
  3094. mvprintw(LINES - 4, 0, "Entered Moving: Use Arrow Keys to Move and press &lt;ENTER&gt; to end moving");
  3095. refresh();
  3096. attroff(COLOR_PAIR(4));
  3097. move = TRUE;
  3098. break;
  3099. case KEY_LEFT:
  3100. if(size == TRUE)
  3101. { --newx;
  3102. ++neww;
  3103. }
  3104. if(move == TRUE)
  3105. --newx;
  3106. break;
  3107. case KEY_RIGHT:
  3108. if(size == TRUE)
  3109. { ++newx;
  3110. --neww;
  3111. }
  3112. if(move == TRUE)
  3113. ++newx;
  3114. break;
  3115. case KEY_UP:
  3116. if(size == TRUE)
  3117. { --newy;
  3118. ++newh;
  3119. }
  3120. if(move == TRUE)
  3121. --newy;
  3122. break;
  3123. case KEY_DOWN:
  3124. if(size == TRUE)
  3125. { ++newy;
  3126. --newh;
  3127. }
  3128. if(move == TRUE)
  3129. ++newy;
  3130. break;
  3131. case 10: /* Enter */
  3132. move(LINES - 4, 0);
  3133. clrtoeol();
  3134. refresh();
  3135. if(size == TRUE)
  3136. { old_win = panel_window(stack_top);
  3137. temp_win = newwin(newh, neww, newy, newx);
  3138. replace_panel(stack_top, temp_win);
  3139. win_show(temp_win, top-&gt;label, top-&gt;label_color);
  3140. delwin(old_win);
  3141. size = FALSE;
  3142. }
  3143. if(move == TRUE)
  3144. { move_panel(stack_top, newy, newx);
  3145. move = FALSE;
  3146. }
  3147. break;
  3148. }
  3149. attron(COLOR_PAIR(4));
  3150. mvprintw(LINES - 3, 0, "Use 'm' for moving, 'r' for resizing");
  3151. mvprintw(LINES - 2, 0, "Use tab to browse through the windows (F1 to Exit)");
  3152. attroff(COLOR_PAIR(4));
  3153. refresh();
  3154. update_panels();
  3155. doupdate();
  3156. }
  3157. endwin();
  3158. return 0;
  3159. }
  3160. /* Put all the windows */
  3161. void init_wins(WINDOW **wins, int n)
  3162. { int x, y, i;
  3163. char label[80];
  3164. y = 2;
  3165. x = 10;
  3166. for(i = 0; i &lt; n; ++i)
  3167. { wins[i] = newwin(NLINES, NCOLS, y, x);
  3168. sprintf(label, "Window Number %d", i + 1);
  3169. win_show(wins[i], label, i + 1);
  3170. y += 3;
  3171. x += 7;
  3172. }
  3173. }
  3174. /* Set the PANEL_DATA structures for individual panels */
  3175. void set_user_ptrs(PANEL **panels, int n)
  3176. { PANEL_DATA *ptrs;
  3177. WINDOW *win;
  3178. int x, y, w, h, i;
  3179. char temp[80];
  3180. ptrs = (PANEL_DATA *)calloc(n, sizeof(PANEL_DATA));
  3181. for(i = 0;i &lt; n; ++i)
  3182. { win = panel_window(panels[i]);
  3183. getbegyx(win, y, x);
  3184. getmaxyx(win, h, w);
  3185. ptrs[i].x = x;
  3186. ptrs[i].y = y;
  3187. ptrs[i].w = w;
  3188. ptrs[i].h = h;
  3189. sprintf(temp, "Window Number %d", i + 1);
  3190. strcpy(ptrs[i].label, temp);
  3191. ptrs[i].label_color = i + 1;
  3192. if(i + 1 == n)
  3193. ptrs[i].next = panels[0];
  3194. else
  3195. ptrs[i].next = panels[i + 1];
  3196. set_panel_userptr(panels[i], &amp;ptrs[i]);
  3197. }
  3198. }
  3199. /* Show the window with a border and a label */
  3200. void win_show(WINDOW *win, char *label, int label_color)
  3201. { int startx, starty, height, width;
  3202. getbegyx(win, starty, startx);
  3203. getmaxyx(win, height, width);
  3204. box(win, 0, 0);
  3205. mvwaddch(win, 2, 0, ACS_LTEE);
  3206. mvwhline(win, 2, 1, ACS_HLINE, width - 2);
  3207. mvwaddch(win, 2, width - 1, ACS_RTEE);
  3208. print_in_middle(win, 1, 0, width, label, COLOR_PAIR(label_color));
  3209. }
  3210. void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color)
  3211. { int length, x, y;
  3212. float temp;
  3213. if(win == NULL)
  3214. win = stdscr;
  3215. getyx(win, y, x);
  3216. if(startx != 0)
  3217. x = startx;
  3218. if(starty != 0)
  3219. y = starty;
  3220. if(width == 0)
  3221. width = 80;
  3222. length = strlen(string);
  3223. temp = (width - length)/ 2;
  3224. x = startx + (int)temp;
  3225. wattron(win, color);
  3226. mvwprintw(win, y, x, "%s", string);
  3227. wattroff(win, color);
  3228. refresh();
  3229. }</span></font>
  3230. </pre></td>
  3231. </tr>
  3232. </table>
  3233. </div>
  3234. <p>Concentrate on the main while loop. Once it finds out the type
  3235. of key pressed, it takes appropriate action. If 'r' is pressed
  3236. resizing mode is started. After this the new sizes are updated as
  3237. the user presses the arrow keys. When the user presses
  3238. &lt;ENTER&gt; present selection ends and panel is resized by using
  3239. the concept explained. While in resizing mode the program doesn't
  3240. show how the window is getting resized. It's left as an exercise to
  3241. the reader to print a dotted border while it gets resized to a new
  3242. position.</p>
  3243. <p>When the user presses 'm' the move mode starts. This is a bit
  3244. simpler than resizing. As the arrow keys are pressed the new
  3245. position is updated and pressing of &lt;ENTER&gt; causes the panel
  3246. to be moved by calling the function move_panel().</p>
  3247. <p>In this program the user data which is represented as
  3248. PANEL_DATA, plays very important role in finding the associated
  3249. information with a panel. As written in the comments, the
  3250. PANEL_DATA stores the panel sizes, label, label color and a pointer
  3251. to the next panel in the cycle.</p>
  3252. </div>
  3253. <div class="SECT2">
  3254. <hr>
  3255. <h3 class="SECT2"><a name="PANELSHOWHIDE" id="PANELSHOWHIDE">16.6.
  3256. Hiding and Showing Panels</a></h3>
  3257. <p>A Panel can be hidden by using the function hide_panel(). This
  3258. function merely removes it form the stack of panels, thus hiding it
  3259. on the screen once you do update_panels() and doupdate(). It
  3260. doesn't destroy the PANEL structure associated with the hidden
  3261. panel. It can be shown again by using the show_panel()
  3262. function.</p>
  3263. <p>The following program shows the hiding of panels. Press 'a' or
  3264. 'b' or 'c' to show or hide first, second and third windows
  3265. respectively. It uses a user data with a small variable hide, which
  3266. keeps track of whether the window is hidden or not. For some reason
  3267. the function <var class="LITERAL">panel_hidden()</var> which tells
  3268. whether a panel is hidden or not is not working. A bug report was
  3269. also presented by Michael Andres <a href=
  3270. "http://www.geocrawler.com/archives/3/344/1999/9/0/2643549/"
  3271. target="_top">here</a></p>
  3272. <div class="EXAMPLE"><a name="PPAHI" id="PPAHI"></a>
  3273. <p><b>Example 17. Panel Hiding and Showing example</b></p>
  3274. <table border="0" bgcolor="#E0E0E0" width="100%">
  3275. <tr>
  3276. <td>
  3277. <pre class="PROGRAMLISTING">
  3278. <font color="#000000"><span class=
  3279. "INLINEMEDIAOBJECT">#include &lt;panel.h&gt;
  3280. typedef struct _PANEL_DATA {
  3281. int hide; /* TRUE if panel is hidden */
  3282. }PANEL_DATA;
  3283. #define NLINES 10
  3284. #define NCOLS 40
  3285. void init_wins(WINDOW **wins, int n);
  3286. void win_show(WINDOW *win, char *label, int label_color);
  3287. void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color);
  3288. int main()
  3289. { WINDOW *my_wins[3];
  3290. PANEL *my_panels[3];
  3291. PANEL_DATA panel_datas[3];
  3292. PANEL_DATA *temp;
  3293. int ch;
  3294. /* Initialize curses */
  3295. initscr();
  3296. start_color();
  3297. cbreak();
  3298. noecho();
  3299. keypad(stdscr, TRUE);
  3300. /* Initialize all the colors */
  3301. init_pair(1, COLOR_RED, COLOR_BLACK);
  3302. init_pair(2, COLOR_GREEN, COLOR_BLACK);
  3303. init_pair(3, COLOR_BLUE, COLOR_BLACK);
  3304. init_pair(4, COLOR_CYAN, COLOR_BLACK);
  3305. init_wins(my_wins, 3);
  3306. /* Attach a panel to each window */ /* Order is bottom up */
  3307. my_panels[0] = new_panel(my_wins[0]); /* Push 0, order: stdscr-0 */
  3308. my_panels[1] = new_panel(my_wins[1]); /* Push 1, order: stdscr-0-1 */
  3309. my_panels[2] = new_panel(my_wins[2]); /* Push 2, order: stdscr-0-1-2 */
  3310. /* Initialize panel datas saying that nothing is hidden */
  3311. panel_datas[0].hide = FALSE;
  3312. panel_datas[1].hide = FALSE;
  3313. panel_datas[2].hide = FALSE;
  3314. set_panel_userptr(my_panels[0], &amp;panel_datas[0]);
  3315. set_panel_userptr(my_panels[1], &amp;panel_datas[1]);
  3316. set_panel_userptr(my_panels[2], &amp;panel_datas[2]);
  3317. /* Update the stacking order. 2nd panel will be on top */
  3318. update_panels();
  3319. /* Show it on the screen */
  3320. attron(COLOR_PAIR(4));
  3321. mvprintw(LINES - 3, 0, "Show or Hide a window with 'a'(first window) 'b'(Second Window) 'c'(Third Window)");
  3322. mvprintw(LINES - 2, 0, "F1 to Exit");
  3323. attroff(COLOR_PAIR(4));
  3324. doupdate();
  3325. while((ch = getch()) != KEY_F(1))
  3326. { switch(ch)
  3327. { case 'a':
  3328. temp = (PANEL_DATA *)panel_userptr(my_panels[0]);
  3329. if(temp-&gt;hide == FALSE)
  3330. { hide_panel(my_panels[0]);
  3331. temp-&gt;hide = TRUE;
  3332. }
  3333. else
  3334. { show_panel(my_panels[0]);
  3335. temp-&gt;hide = FALSE;
  3336. }
  3337. break;
  3338. case 'b':
  3339. temp = (PANEL_DATA *)panel_userptr(my_panels[1]);
  3340. if(temp-&gt;hide == FALSE)
  3341. { hide_panel(my_panels[1]);
  3342. temp-&gt;hide = TRUE;
  3343. }
  3344. else
  3345. { show_panel(my_panels[1]);
  3346. temp-&gt;hide = FALSE;
  3347. }
  3348. break;
  3349. case 'c':
  3350. temp = (PANEL_DATA *)panel_userptr(my_panels[2]);
  3351. if(temp-&gt;hide == FALSE)
  3352. { hide_panel(my_panels[2]);
  3353. temp-&gt;hide = TRUE;
  3354. }
  3355. else
  3356. { show_panel(my_panels[2]);
  3357. temp-&gt;hide = FALSE;
  3358. }
  3359. break;
  3360. }
  3361. update_panels();
  3362. doupdate();
  3363. }
  3364. endwin();
  3365. return 0;
  3366. }
  3367. /* Put all the windows */
  3368. void init_wins(WINDOW **wins, int n)
  3369. { int x, y, i;
  3370. char label[80];
  3371. y = 2;
  3372. x = 10;
  3373. for(i = 0; i &lt; n; ++i)
  3374. { wins[i] = newwin(NLINES, NCOLS, y, x);
  3375. sprintf(label, "Window Number %d", i + 1);
  3376. win_show(wins[i], label, i + 1);
  3377. y += 3;
  3378. x += 7;
  3379. }
  3380. }
  3381. /* Show the window with a border and a label */
  3382. void win_show(WINDOW *win, char *label, int label_color)
  3383. { int startx, starty, height, width;
  3384. getbegyx(win, starty, startx);
  3385. getmaxyx(win, height, width);
  3386. box(win, 0, 0);
  3387. mvwaddch(win, 2, 0, ACS_LTEE);
  3388. mvwhline(win, 2, 1, ACS_HLINE, width - 2);
  3389. mvwaddch(win, 2, width - 1, ACS_RTEE);
  3390. print_in_middle(win, 1, 0, width, label, COLOR_PAIR(label_color));
  3391. }
  3392. void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color)
  3393. { int length, x, y;
  3394. float temp;
  3395. if(win == NULL)
  3396. win = stdscr;
  3397. getyx(win, y, x);
  3398. if(startx != 0)
  3399. x = startx;
  3400. if(starty != 0)
  3401. y = starty;
  3402. if(width == 0)
  3403. width = 80;
  3404. length = strlen(string);
  3405. temp = (width - length)/ 2;
  3406. x = startx + (int)temp;
  3407. wattron(win, color);
  3408. mvwprintw(win, y, x, "%s", string);
  3409. wattroff(win, color);
  3410. refresh();
  3411. }</span></font>
  3412. </pre></td>
  3413. </tr>
  3414. </table>
  3415. </div>
  3416. </div>
  3417. <div class="SECT2">
  3418. <hr>
  3419. <h3 class="SECT2"><a name="PANELABOVE" id="PANELABOVE">16.7.
  3420. panel_above() and panel_below() Functions</a></h3>
  3421. <p>The functions <var class="LITERAL">panel_above()</var> and
  3422. <var class="LITERAL">panel_below()</var> can be used to find out
  3423. the panel above and below a panel. If the argument to these
  3424. functions is NULL, then they return a pointer to bottom panel and
  3425. top panel respectively.</p>
  3426. </div>
  3427. </div>
  3428. <div class="SECT1">
  3429. <hr>
  3430. <h2 class="SECT1"><a name="MENUS" id="MENUS">17. Menus
  3431. Library</a></h2>
  3432. <p>The menus library provides a nice extension to basic curses,
  3433. through which you can create menus. It provides a set of functions
  3434. to create menus. But they have to be customized to give a nicer
  3435. look, with colors etc. Let's get into the details.</p>
  3436. <p>A menu is a screen display that assists the user to choose some
  3437. subset of a given set of items. To put it simple, a menu is a
  3438. collection of items from which one or more items can be chosen.
  3439. Some readers might not be aware of multiple item selection
  3440. capability. Menu library provides functionality to write menus from
  3441. which the user can chose more than one item as the preferred
  3442. choice. This is dealt with in a later section. Now it is time for
  3443. some rudiments.</p>
  3444. <div class="SECT2">
  3445. <hr>
  3446. <h3 class="SECT2"><a name="MENUBASICS" id="MENUBASICS">17.1. The
  3447. Basics</a></h3>
  3448. <p>To create menus, you first create items, and then post the menu
  3449. to the display. After that, all the processing of user responses is
  3450. done in an elegant function menu_driver() which is the work horse
  3451. of any menu program.</p>
  3452. <p>The general flow of control of a menu program looks like
  3453. this.</p>
  3454. <ol type="1">
  3455. <li>
  3456. <p>Initialize curses</p>
  3457. </li>
  3458. <li>
  3459. <p>Create items using new_item(). You can specify a name and
  3460. description for the items.</p>
  3461. </li>
  3462. <li>
  3463. <p>Create the menu with new_menu() by specifying the items to be
  3464. attached with.</p>
  3465. </li>
  3466. <li>
  3467. <p>Post the menu with menu_post() and refresh the screen.</p>
  3468. </li>
  3469. <li>
  3470. <p>Process the user requests with a loop and do necessary updates
  3471. to menu with menu_driver.</p>
  3472. </li>
  3473. <li>
  3474. <p>Unpost the menu with menu_unpost()</p>
  3475. </li>
  3476. <li>
  3477. <p>Free the memory allocated to menu by free_menu()</p>
  3478. </li>
  3479. <li>
  3480. <p>Free the memory allocated to the items with free_item()</p>
  3481. </li>
  3482. <li>
  3483. <p>End curses</p>
  3484. </li>
  3485. </ol>
  3486. <p>Let's see a program which prints a simple menu and updates the
  3487. current selection with up, down arrows.</p>
  3488. </div>
  3489. <div class="SECT2">
  3490. <hr>
  3491. <h3 class="SECT2"><a name="COMPILEMENUS" id="COMPILEMENUS">17.2.
  3492. Compiling With the Menu Library</a></h3>
  3493. <p>To use menu library functions, you have to include menu.h and to
  3494. link the program with menu library the flag -lmenu should be added
  3495. along with -lncurses in that order.</p>
  3496. <table border="0" bgcolor="#E0E0E0" width="100%">
  3497. <tr>
  3498. <td>
  3499. <pre class="PROGRAMLISTING">
  3500. <font color="#000000"> #include &lt;menu.h&gt;
  3501. .
  3502. .
  3503. .
  3504. compile and link: gcc &lt;program file&gt; -lmenu -lncurses</font>
  3505. </pre></td>
  3506. </tr>
  3507. </table>
  3508. <div class="EXAMPLE"><a name="MMESI" id="MMESI"></a>
  3509. <p><b>Example 18. Menu Basics</b></p>
  3510. <table border="0" bgcolor="#E0E0E0" width="100%">
  3511. <tr>
  3512. <td>
  3513. <pre class="PROGRAMLISTING">
  3514. <font color="#000000"><span class=
  3515. "INLINEMEDIAOBJECT">#include &lt;curses.h&gt;
  3516. #include &lt;menu.h&gt;
  3517. #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
  3518. #define CTRLD 4
  3519. char *choices[] = {
  3520. "Choice 1",
  3521. "Choice 2",
  3522. "Choice 3",
  3523. "Choice 4",
  3524. "Exit",
  3525. };
  3526. int main()
  3527. { ITEM **my_items;
  3528. int c;
  3529. MENU *my_menu;
  3530. int n_choices, i;
  3531. ITEM *cur_item;
  3532. initscr();
  3533. cbreak();
  3534. noecho();
  3535. keypad(stdscr, TRUE);
  3536. n_choices = ARRAY_SIZE(choices);
  3537. my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *));
  3538. for(i = 0; i &lt; n_choices; ++i)
  3539. my_items[i] = new_item(choices[i], choices[i]);
  3540. my_items[n_choices] = (ITEM *)NULL;
  3541. my_menu = new_menu((ITEM **)my_items);
  3542. mvprintw(LINES - 2, 0, "F1 to Exit");
  3543. post_menu(my_menu);
  3544. refresh();
  3545. while((c = getch()) != KEY_F(1))
  3546. { switch(c)
  3547. { case KEY_DOWN:
  3548. menu_driver(my_menu, REQ_DOWN_ITEM);
  3549. break;
  3550. case KEY_UP:
  3551. menu_driver(my_menu, REQ_UP_ITEM);
  3552. break;
  3553. }
  3554. }
  3555. free_item(my_items[0]);
  3556. free_item(my_items[1]);
  3557. free_menu(my_menu);
  3558. endwin();
  3559. }
  3560. </span></font>
  3561. </pre></td>
  3562. </tr>
  3563. </table>
  3564. </div>
  3565. <p>This program demonstrates the basic concepts involved in
  3566. creating a menu using menus library. First we create the items
  3567. using new_item() and then attach them to the menu with new_menu()
  3568. function. After posting the menu and refreshing the screen, the
  3569. main processing loop starts. It reads user input and takes
  3570. corresponding action. The function menu_driver() is the main work
  3571. horse of the menu system. The second parameter to this function
  3572. tells what's to be done with the menu. According to the parameter,
  3573. menu_driver() does the corresponding task. The value can be either
  3574. a menu navigational request, an ascii character, or a KEY_MOUSE
  3575. special key associated with a mouse event.</p>
  3576. <p>The menu_driver accepts following navigational requests.</p>
  3577. <table border="0" bgcolor="#E0E0E0" width="100%">
  3578. <tr>
  3579. <td>
  3580. <pre class="PROGRAMLISTING">
  3581. <font color=
  3582. "#000000">&#13; REQ_LEFT_ITEM Move left to an item.
  3583. REQ_RIGHT_ITEM Move right to an item.
  3584. REQ_UP_ITEM Move up to an item.
  3585. REQ_DOWN_ITEM Move down to an item.
  3586. REQ_SCR_ULINE Scroll up a line.
  3587. REQ_SCR_DLINE Scroll down a line.
  3588. REQ_SCR_DPAGE Scroll down a page.
  3589. REQ_SCR_UPAGE Scroll up a page.
  3590. REQ_FIRST_ITEM Move to the first item.
  3591. REQ_LAST_ITEM Move to the last item.
  3592. REQ_NEXT_ITEM Move to the next item.
  3593. REQ_PREV_ITEM Move to the previous item.
  3594. REQ_TOGGLE_ITEM Select/deselect an item.
  3595. REQ_CLEAR_PATTERN Clear the menu pattern buffer.
  3596. REQ_BACK_PATTERN Delete the previous character from the pattern buffer.
  3597. REQ_NEXT_MATCH Move to the next item matching the pattern match.
  3598. REQ_PREV_MATCH Move to the previous item matching the pattern match.&#13;</font>
  3599. </pre></td>
  3600. </tr>
  3601. </table>
  3602. <p>Don't get overwhelmed by the number of options. We will see them
  3603. slowly one after another. The options of interest in this example
  3604. are REQ_UP_ITEM and REQ_DOWN_ITEM. These two options when passed to
  3605. menu_driver, menu driver updates the current item to one item up or
  3606. down respectively.</p>
  3607. </div>
  3608. <div class="SECT2">
  3609. <hr>
  3610. <h3 class="SECT2"><a name="MENUDRIVER" id="MENUDRIVER">17.3. Menu
  3611. Driver: The work horse of the menu system</a></h3>
  3612. <p>As you have seen in the above example, menu_driver plays an
  3613. important role in updating the menu. It is very important to
  3614. understand various options it takes and what they do. As explained
  3615. above, the second parameter to menu_driver() can be either a
  3616. navigational request, a printable character or a KEY_MOUSE key.
  3617. Let's dissect the different navigational requests.</p>
  3618. <ul>
  3619. <li>
  3620. <p><em>REQ_LEFT_ITEM and REQ_RIGHT_ITEM</em></p>
  3621. <p>A Menu can be displayed with multiple columns for more than one
  3622. item. This can be done by using the <var class=
  3623. "LITERAL">menu_format()</var>function. When a multi columnar menu
  3624. is displayed these requests cause the menu driver to move the
  3625. current selection to left or right.</p>
  3626. </li>
  3627. <li>
  3628. <p><em>REQ_UP_ITEM and REQ_DOWN_ITEM</em></p>
  3629. <p>These two options you have seen in the above example. These
  3630. options when given, makes the menu_driver to move the current
  3631. selection to an item up or down.</p>
  3632. </li>
  3633. <li>
  3634. <p><em>REQ_SCR_* options</em></p>
  3635. <p>The four options REQ_SCR_ULINE, REQ_SCR_DLINE, REQ_SCR_DPAGE,
  3636. REQ_SCR_UPAGE are related to scrolling. If all the items in the
  3637. menu cannot be displayed in the menu sub window, then the menu is
  3638. scrollable. These requests can be given to the menu_driver to do
  3639. the scrolling either one line up, down or one page down or up
  3640. respectively.</p>
  3641. </li>
  3642. <li>
  3643. <p><em>REQ_FIRST_ITEM, REQ_LAST_ITEM, REQ_NEXT_ITEM and
  3644. REQ_PREV_ITEM</em></p>
  3645. <p>These requests are self explanatory.</p>
  3646. </li>
  3647. <li>
  3648. <p><em>REQ_TOGGLE_ITEM</em></p>
  3649. <p>This request when given, toggles the present selection. This
  3650. option is to be used only in a multi valued menu. So to use this
  3651. request the option O_ONEVALUE must be off. This option can be made
  3652. off or on with set_menu_opts().</p>
  3653. </li>
  3654. <li>
  3655. <p><em>Pattern Requests</em></p>
  3656. <p>Every menu has an associated pattern buffer, which is used to
  3657. find the nearest match to the ascii characters entered by the user.
  3658. Whenever ascii characters are given to menu_driver, it puts in to
  3659. the pattern buffer. It also tries to find the nearest match to the
  3660. pattern in the items list and moves current selection to that item.
  3661. The request REQ_CLEAR_PATTERN clears the pattern buffer. The
  3662. request REQ_BACK_PATTERN deletes the previous character in the
  3663. pattern buffer. In case the pattern matches more than one item then
  3664. the matched items can be cycled through REQ_NEXT_MATCH and
  3665. REQ_PREV_MATCH which move the current selection to the next and
  3666. previous matches respectively.</p>
  3667. </li>
  3668. <li>
  3669. <p><em>Mouse Requests</em></p>
  3670. <p>In case of KEY_MOUSE requests, according to the mouse position
  3671. an action is taken accordingly. The action to be taken is explained
  3672. in the man page as,</p>
  3673. <table border="0" bgcolor="#E0E0E0" width="90%">
  3674. <tr>
  3675. <td>
  3676. <pre class="PROGRAMLISTING">
  3677. <font color=
  3678. "#000000"><em> If the second argument is the KEY_MOUSE special key, the
  3679. associated mouse event is translated into one of the above
  3680. pre-defined requests. Currently only clicks in the user
  3681. window (e.g. inside the menu display area or the decora&shy;
  3682. tion window) are handled. If you click above the display
  3683. region of the menu, a REQ_SCR_ULINE is generated, if you
  3684. doubleclick a REQ_SCR_UPAGE is generated and if you
  3685. tripleclick a REQ_FIRST_ITEM is generated. If you click
  3686. below the display region of the menu, a REQ_SCR_DLINE is
  3687. generated, if you doubleclick a REQ_SCR_DPAGE is generated
  3688. and if you tripleclick a REQ_LAST_ITEM is generated. If
  3689. you click at an item inside the display area of the menu,
  3690. the menu cursor is positioned to that item.</em></font>
  3691. </pre></td>
  3692. </tr>
  3693. </table>
  3694. </li>
  3695. </ul>
  3696. <p>Each of the above requests will be explained in the following
  3697. lines with several examples whenever appropriate.</p>
  3698. </div>
  3699. <div class="SECT2">
  3700. <hr>
  3701. <h3 class="SECT2"><a name="MENUWINDOWS" id="MENUWINDOWS">17.4. Menu
  3702. Windows</a></h3>
  3703. <p>Every menu created is associated with a window and a sub window.
  3704. The menu window displays any title or border associated with the
  3705. menu. The menu sub window displays the menu items currently
  3706. available for selection. But we didn't specify any window or sub
  3707. window in the simple example. When a window is not specified,
  3708. stdscr is taken as the main window, and then menu system calculates
  3709. the sub window size required for the display of items. Then items
  3710. are displayed in the calculated sub window. So let's play with
  3711. these windows and display a menu with a border and a title.</p>
  3712. <div class="EXAMPLE"><a name="MMEWI" id="MMEWI"></a>
  3713. <p><b>Example 19. Menu Windows Usage example</b></p>
  3714. <table border="0" bgcolor="#E0E0E0" width="100%">
  3715. <tr>
  3716. <td>
  3717. <pre class="PROGRAMLISTING">
  3718. <font color="#000000"><span class=
  3719. "INLINEMEDIAOBJECT">#include &lt;menu.h&gt;
  3720. #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
  3721. #define CTRLD 4
  3722. char *choices[] = {
  3723. "Choice 1",
  3724. "Choice 2",
  3725. "Choice 3",
  3726. "Choice 4",
  3727. "Exit",
  3728. (char *)NULL,
  3729. };
  3730. void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color);
  3731. int main()
  3732. { ITEM **my_items;
  3733. int c;
  3734. MENU *my_menu;
  3735. WINDOW *my_menu_win;
  3736. int n_choices, i;
  3737. /* Initialize curses */
  3738. initscr();
  3739. start_color();
  3740. cbreak();
  3741. noecho();
  3742. keypad(stdscr, TRUE);
  3743. init_pair(1, COLOR_RED, COLOR_BLACK);
  3744. /* Create items */
  3745. n_choices = ARRAY_SIZE(choices);
  3746. my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *));
  3747. for(i = 0; i &lt; n_choices; ++i)
  3748. my_items[i] = new_item(choices[i], choices[i]);
  3749. /* Crate menu */
  3750. my_menu = new_menu((ITEM **)my_items);
  3751. /* Create the window to be associated with the menu */
  3752. my_menu_win = newwin(10, 40, 4, 4);
  3753. keypad(my_menu_win, TRUE);
  3754. /* Set main window and sub window */
  3755. set_menu_win(my_menu, my_menu_win);
  3756. set_menu_sub(my_menu, derwin(my_menu_win, 6, 38, 3, 1));
  3757. /* Set menu mark to the string " * " */
  3758. set_menu_mark(my_menu, " * ");
  3759. /* Print a border around the main window and print a title */
  3760. box(my_menu_win, 0, 0);
  3761. print_in_middle(my_menu_win, 1, 0, 40, "My Menu", COLOR_PAIR(1));
  3762. mvwaddch(my_menu_win, 2, 0, ACS_LTEE);
  3763. mvwhline(my_menu_win, 2, 1, ACS_HLINE, 38);
  3764. mvwaddch(my_menu_win, 2, 39, ACS_RTEE);
  3765. mvprintw(LINES - 2, 0, "F1 to exit");
  3766. refresh();
  3767. /* Post the menu */
  3768. post_menu(my_menu);
  3769. wrefresh(my_menu_win);
  3770. while((c = wgetch(my_menu_win)) != KEY_F(1))
  3771. { switch(c)
  3772. { case KEY_DOWN:
  3773. menu_driver(my_menu, REQ_DOWN_ITEM);
  3774. break;
  3775. case KEY_UP:
  3776. menu_driver(my_menu, REQ_UP_ITEM);
  3777. break;
  3778. }
  3779. wrefresh(my_menu_win);
  3780. }
  3781. /* Unpost and free all the memory taken up */
  3782. unpost_menu(my_menu);
  3783. free_menu(my_menu);
  3784. for(i = 0; i &lt; n_choices; ++i)
  3785. free_item(my_items[i]);
  3786. endwin();
  3787. }
  3788. void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color)
  3789. { int length, x, y;
  3790. float temp;
  3791. if(win == NULL)
  3792. win = stdscr;
  3793. getyx(win, y, x);
  3794. if(startx != 0)
  3795. x = startx;
  3796. if(starty != 0)
  3797. y = starty;
  3798. if(width == 0)
  3799. width = 80;
  3800. length = strlen(string);
  3801. temp = (width - length)/ 2;
  3802. x = startx + (int)temp;
  3803. wattron(win, color);
  3804. mvwprintw(win, y, x, "%s", string);
  3805. wattroff(win, color);
  3806. refresh();
  3807. }</span></font>
  3808. </pre></td>
  3809. </tr>
  3810. </table>
  3811. </div>
  3812. <p>This example creates a menu with a title, border, a fancy line
  3813. separating title and the items. As you can see, in order to attach
  3814. a window to a menu the function set_menu_win() has to be used. Then
  3815. we attach the sub window also. This displays the items in the sub
  3816. window. You can also set the mark string which gets displayed to
  3817. the left of the selected item with set_menu_mark().</p>
  3818. </div>
  3819. <div class="SECT2">
  3820. <hr>
  3821. <h3 class="SECT2"><a name="SCROLLMENUS" id="SCROLLMENUS">17.5.
  3822. Scrolling Menus</a></h3>
  3823. <p>If the sub window given for a window is not big enough to show
  3824. all the items, then the menu will be scrollable. When you are on
  3825. the last item in the present list, if you send REQ_DOWN_ITEM, it
  3826. gets translated into REQ_SCR_DLINE and the menu scrolls by one
  3827. item. You can manually give REQ_SCR_ operations to do scrolling.
  3828. Let's see how it can be done.</p>
  3829. <div class="EXAMPLE"><a name="MMESC" id="MMESC"></a>
  3830. <p><b>Example 20. Scrolling Menus example</b></p>
  3831. <table border="0" bgcolor="#E0E0E0" width="100%">
  3832. <tr>
  3833. <td>
  3834. <pre class="PROGRAMLISTING">
  3835. <font color="#000000"><span class=
  3836. "INLINEMEDIAOBJECT">#include &lt;curses.h&gt;
  3837. #include &lt;menu.h&gt;
  3838. #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
  3839. #define CTRLD 4
  3840. char *choices[] = {
  3841. "Choice 1",
  3842. "Choice 2",
  3843. "Choice 3",
  3844. "Choice 4",
  3845. "Choice 5",
  3846. "Choice 6",
  3847. "Choice 7",
  3848. "Choice 8",
  3849. "Choice 9",
  3850. "Choice 10",
  3851. "Exit",
  3852. (char *)NULL,
  3853. };
  3854. void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color);
  3855. int main()
  3856. { ITEM **my_items;
  3857. int c;
  3858. MENU *my_menu;
  3859. WINDOW *my_menu_win;
  3860. int n_choices, i;
  3861. /* Initialize curses */
  3862. initscr();
  3863. start_color();
  3864. cbreak();
  3865. noecho();
  3866. keypad(stdscr, TRUE);
  3867. init_pair(1, COLOR_RED, COLOR_BLACK);
  3868. init_pair(2, COLOR_CYAN, COLOR_BLACK);
  3869. /* Create items */
  3870. n_choices = ARRAY_SIZE(choices);
  3871. my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *));
  3872. for(i = 0; i &lt; n_choices; ++i)
  3873. my_items[i] = new_item(choices[i], choices[i]);
  3874. /* Crate menu */
  3875. my_menu = new_menu((ITEM **)my_items);
  3876. /* Create the window to be associated with the menu */
  3877. my_menu_win = newwin(10, 40, 4, 4);
  3878. keypad(my_menu_win, TRUE);
  3879. /* Set main window and sub window */
  3880. set_menu_win(my_menu, my_menu_win);
  3881. set_menu_sub(my_menu, derwin(my_menu_win, 6, 38, 3, 1));
  3882. set_menu_format(my_menu, 5, 1);
  3883. /* Set menu mark to the string " * " */
  3884. set_menu_mark(my_menu, " * ");
  3885. /* Print a border around the main window and print a title */
  3886. box(my_menu_win, 0, 0);
  3887. print_in_middle(my_menu_win, 1, 0, 40, "My Menu", COLOR_PAIR(1));
  3888. mvwaddch(my_menu_win, 2, 0, ACS_LTEE);
  3889. mvwhline(my_menu_win, 2, 1, ACS_HLINE, 38);
  3890. mvwaddch(my_menu_win, 2, 39, ACS_RTEE);
  3891. /* Post the menu */
  3892. post_menu(my_menu);
  3893. wrefresh(my_menu_win);
  3894. attron(COLOR_PAIR(2));
  3895. mvprintw(LINES - 2, 0, "Use PageUp and PageDown to scoll down or up a page of items");
  3896. mvprintw(LINES - 1, 0, "Arrow Keys to navigate (F1 to Exit)");
  3897. attroff(COLOR_PAIR(2));
  3898. refresh();
  3899. while((c = wgetch(my_menu_win)) != KEY_F(1))
  3900. { switch(c)
  3901. { case KEY_DOWN:
  3902. menu_driver(my_menu, REQ_DOWN_ITEM);
  3903. break;
  3904. case KEY_UP:
  3905. menu_driver(my_menu, REQ_UP_ITEM);
  3906. break;
  3907. case KEY_NPAGE:
  3908. menu_driver(my_menu, REQ_SCR_DPAGE);
  3909. break;
  3910. case KEY_PPAGE:
  3911. menu_driver(my_menu, REQ_SCR_UPAGE);
  3912. break;
  3913. }
  3914. wrefresh(my_menu_win);
  3915. }
  3916. /* Unpost and free all the memory taken up */
  3917. unpost_menu(my_menu);
  3918. free_menu(my_menu);
  3919. for(i = 0; i &lt; n_choices; ++i)
  3920. free_item(my_items[i]);
  3921. endwin();
  3922. }
  3923. void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color)
  3924. { int length, x, y;
  3925. float temp;
  3926. if(win == NULL)
  3927. win = stdscr;
  3928. getyx(win, y, x);
  3929. if(startx != 0)
  3930. x = startx;
  3931. if(starty != 0)
  3932. y = starty;
  3933. if(width == 0)
  3934. width = 80;
  3935. length = strlen(string);
  3936. temp = (width - length)/ 2;
  3937. x = startx + (int)temp;
  3938. wattron(win, color);
  3939. mvwprintw(win, y, x, "%s", string);
  3940. wattroff(win, color);
  3941. refresh();
  3942. }</span></font>
  3943. </pre></td>
  3944. </tr>
  3945. </table>
  3946. </div>
  3947. <p>This program is self-explanatory. In this example the number of
  3948. choices has been increased to ten, which is larger than our sub
  3949. window size which can hold 6 items. This message has to be
  3950. explicitly conveyed to the menu system with the function
  3951. set_menu_format(). In here we specify the number of rows and
  3952. columns we want to be displayed for a single page. We can specify
  3953. any number of items to be shown, in the rows variables, if it is
  3954. less than the height of the sub window. If the key pressed by the
  3955. user is a PAGE UP or PAGE DOWN, the menu is scrolled a page due to
  3956. the requests (REQ_SCR_DPAGE and REQ_SCR_UPAGE) given to
  3957. menu_driver().</p>
  3958. </div>
  3959. <div class="SECT2">
  3960. <hr>
  3961. <h3 class="SECT2"><a name="MULTICOLUMN" id="MULTICOLUMN">17.6.
  3962. Multi Columnar Menus</a></h3>
  3963. <p>In the above example you have seen how to use the function
  3964. set_menu_format(). I didn't mention what the cols variable (third
  3965. parameter) does. Well, If your sub window is wide enough, you can
  3966. opt to display more than one item per row. This can be specified in
  3967. the cols variable. To make things simpler, the following example
  3968. doesn't show descriptions for the items.</p>
  3969. <div class="EXAMPLE"><a name="MMEMUCO" id="MMEMUCO"></a>
  3970. <p><b>Example 21. Milt Columnar Menus Example</b></p>
  3971. <table border="0" bgcolor="#E0E0E0" width="100%">
  3972. <tr>
  3973. <td>
  3974. <pre class="PROGRAMLISTING">
  3975. <font color="#000000"><span class=
  3976. "INLINEMEDIAOBJECT">#include &lt;curses.h&gt;
  3977. #include &lt;menu.h&gt;
  3978. #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
  3979. #define CTRLD 4
  3980. char *choices[] = {
  3981. "Choice 1", "Choice 2", "Choice 3", "Choice 4", "Choice 5",
  3982. "Choice 6", "Choice 7", "Choice 8", "Choice 9", "Choice 10",
  3983. "Choice 11", "Choice 12", "Choice 13", "Choice 14", "Choice 15",
  3984. "Choice 16", "Choice 17", "Choice 18", "Choice 19", "Choice 20",
  3985. "Exit",
  3986. (char *)NULL,
  3987. };
  3988. int main()
  3989. { ITEM **my_items;
  3990. int c;
  3991. MENU *my_menu;
  3992. WINDOW *my_menu_win;
  3993. int n_choices, i;
  3994. /* Initialize curses */
  3995. initscr();
  3996. start_color();
  3997. cbreak();
  3998. noecho();
  3999. keypad(stdscr, TRUE);
  4000. init_pair(1, COLOR_RED, COLOR_BLACK);
  4001. init_pair(2, COLOR_CYAN, COLOR_BLACK);
  4002. /* Create items */
  4003. n_choices = ARRAY_SIZE(choices);
  4004. my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *));
  4005. for(i = 0; i &lt; n_choices; ++i)
  4006. my_items[i] = new_item(choices[i], choices[i]);
  4007. /* Crate menu */
  4008. my_menu = new_menu((ITEM **)my_items);
  4009. /* Set menu option not to show the description */
  4010. menu_opts_off(my_menu, O_SHOWDESC);
  4011. /* Create the window to be associated with the menu */
  4012. my_menu_win = newwin(10, 70, 4, 4);
  4013. keypad(my_menu_win, TRUE);
  4014. /* Set main window and sub window */
  4015. set_menu_win(my_menu, my_menu_win);
  4016. set_menu_sub(my_menu, derwin(my_menu_win, 6, 68, 3, 1));
  4017. set_menu_format(my_menu, 5, 3);
  4018. set_menu_mark(my_menu, " * ");
  4019. /* Print a border around the main window and print a title */
  4020. box(my_menu_win, 0, 0);
  4021. attron(COLOR_PAIR(2));
  4022. mvprintw(LINES - 3, 0, "Use PageUp and PageDown to scroll");
  4023. mvprintw(LINES - 2, 0, "Use Arrow Keys to navigate (F1 to Exit)");
  4024. attroff(COLOR_PAIR(2));
  4025. refresh();
  4026. /* Post the menu */
  4027. post_menu(my_menu);
  4028. wrefresh(my_menu_win);
  4029. while((c = wgetch(my_menu_win)) != KEY_F(1))
  4030. { switch(c)
  4031. { case KEY_DOWN:
  4032. menu_driver(my_menu, REQ_DOWN_ITEM);
  4033. break;
  4034. case KEY_UP:
  4035. menu_driver(my_menu, REQ_UP_ITEM);
  4036. break;
  4037. case KEY_LEFT:
  4038. menu_driver(my_menu, REQ_LEFT_ITEM);
  4039. break;
  4040. case KEY_RIGHT:
  4041. menu_driver(my_menu, REQ_RIGHT_ITEM);
  4042. break;
  4043. case KEY_NPAGE:
  4044. menu_driver(my_menu, REQ_SCR_DPAGE);
  4045. break;
  4046. case KEY_PPAGE:
  4047. menu_driver(my_menu, REQ_SCR_UPAGE);
  4048. break;
  4049. }
  4050. wrefresh(my_menu_win);
  4051. }
  4052. /* Unpost and free all the memory taken up */
  4053. unpost_menu(my_menu);
  4054. free_menu(my_menu);
  4055. for(i = 0; i &lt; n_choices; ++i)
  4056. free_item(my_items[i]);
  4057. endwin();
  4058. }</span></font>
  4059. </pre></td>
  4060. </tr>
  4061. </table>
  4062. </div>
  4063. <p>Watch the function call to set_menu_format(). It specifies the
  4064. number of columns to be 3, thus displaying 3 items per row. We have
  4065. also switched off the showing descriptions with the function
  4066. menu_opts_off(). There are couple of functions set_menu_opts(),
  4067. menu_opts_on() and menu_opts() which can be used to manipulate menu
  4068. options. The following menu options can be specified.</p>
  4069. <table border="0" bgcolor="#E0E0E0" width="100%">
  4070. <tr>
  4071. <td>
  4072. <pre class="PROGRAMLISTING">
  4073. <font color="#000000"> O_ONEVALUE
  4074. Only one item can be selected for this menu.
  4075. O_SHOWDESC
  4076. Display the item descriptions when the menu is
  4077. posted.
  4078. O_ROWMAJOR
  4079. Display the menu in row-major order.
  4080. O_IGNORECASE
  4081. Ignore the case when pattern-matching.
  4082. O_SHOWMATCH
  4083. Move the cursor to within the item name while pat&shy;
  4084. tern-matching.
  4085. O_NONCYCLIC
  4086. Don't wrap around next-item and previous-item,
  4087. requests to the other end of the menu.</font>
  4088. </pre></td>
  4089. </tr>
  4090. </table>
  4091. <p>All options are on by default. You can switch specific
  4092. attributes on or off with menu_opts_on() and menu_opts_off()
  4093. functions. You can also use set_menu_opts() to directly specify the
  4094. options. The argument to this function should be a OR ed value of
  4095. some of those above constants. The function menu_opts() can be used
  4096. to find out a menu's present options.</p>
  4097. </div>
  4098. <div class="SECT2">
  4099. <hr>
  4100. <h3 class="SECT2"><a name="MULTIVALUEMENUS" id=
  4101. "MULTIVALUEMENUS">17.7. Multi Valued Menus</a></h3>
  4102. <p>You might be wondering what if you switch off the option
  4103. O_ONEVALUE. Then the menu becomes multi-valued. That means you can
  4104. select more than one item. This brings us to the request
  4105. REQ_TOGGLE_ITEM. Let's see it in action.</p>
  4106. <div class="EXAMPLE"><a name="MMETO" id="MMETO"></a>
  4107. <p><b>Example 22. Multi Valued Menus example</b></p>
  4108. <table border="0" bgcolor="#E0E0E0" width="100%">
  4109. <tr>
  4110. <td>
  4111. <pre class="PROGRAMLISTING">
  4112. <font color="#000000"><span class=
  4113. "INLINEMEDIAOBJECT">#include &lt;curses.h&gt;
  4114. #include &lt;menu.h&gt;
  4115. #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
  4116. #define CTRLD 4
  4117. char *choices[] = {
  4118. "Choice 1",
  4119. "Choice 2",
  4120. "Choice 3",
  4121. "Choice 4",
  4122. "Choice 5",
  4123. "Choice 6",
  4124. "Choice 7",
  4125. "Exit",
  4126. };
  4127. int main()
  4128. { ITEM **my_items;
  4129. int c;
  4130. MENU *my_menu;
  4131. int n_choices, i;
  4132. ITEM *cur_item;
  4133. /* Initialize curses */
  4134. initscr();
  4135. cbreak();
  4136. noecho();
  4137. keypad(stdscr, TRUE);
  4138. /* Initialize items */
  4139. n_choices = ARRAY_SIZE(choices);
  4140. my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *));
  4141. for(i = 0; i &lt; n_choices; ++i)
  4142. my_items[i] = new_item(choices[i], choices[i]);
  4143. my_items[n_choices] = (ITEM *)NULL;
  4144. my_menu = new_menu((ITEM **)my_items);
  4145. /* Make the menu multi valued */
  4146. menu_opts_off(my_menu, O_ONEVALUE);
  4147. mvprintw(LINES - 3, 0, "Use &lt;SPACE&gt; to select or unselect an item.");
  4148. mvprintw(LINES - 2, 0, "&lt;ENTER&gt; to see presently selected items(F1 to Exit)");
  4149. post_menu(my_menu);
  4150. refresh();
  4151. while((c = getch()) != KEY_F(1))
  4152. { switch(c)
  4153. { case KEY_DOWN:
  4154. menu_driver(my_menu, REQ_DOWN_ITEM);
  4155. break;
  4156. case KEY_UP:
  4157. menu_driver(my_menu, REQ_UP_ITEM);
  4158. break;
  4159. case ' ':
  4160. menu_driver(my_menu, REQ_TOGGLE_ITEM);
  4161. break;
  4162. case 10: /* Enter */
  4163. { char temp[200];
  4164. ITEM **items;
  4165. items = menu_items(my_menu);
  4166. temp[0] = '\0';
  4167. for(i = 0; i &lt; item_count(my_menu); ++i)
  4168. if(item_value(items[i]) == TRUE)
  4169. { strcat(temp, item_name(items[i]));
  4170. strcat(temp, " ");
  4171. }
  4172. move(20, 0);
  4173. clrtoeol();
  4174. mvprintw(20, 0, temp);
  4175. refresh();
  4176. }
  4177. break;
  4178. }
  4179. }
  4180. free_item(my_items[0]);
  4181. free_item(my_items[1]);
  4182. free_menu(my_menu);
  4183. endwin();
  4184. }
  4185. </span></font>
  4186. </pre></td>
  4187. </tr>
  4188. </table>
  4189. </div>
  4190. <p>Whew, A lot of new functions. Let's take them one after another.
  4191. Firstly, the REQ_TOGGLE_ITEM. In a multi-valued menu, the user
  4192. should be allowed to select or un select more than one item. The
  4193. request REQ_TOGGLE_ITEM toggles the present selection. In this case
  4194. when space is pressed REQ_TOGGLE_ITEM request is sent to
  4195. menu_driver to achieve the result.</p>
  4196. <p>Now when the user presses &lt;ENTER&gt; we show the items he
  4197. presently selected. First we find out the items associated with the
  4198. menu using the function menu_items(). Then we loop through the
  4199. items to find out if the item is selected or not. The function
  4200. item_value() returns TRUE if an item is selected. The function
  4201. item_count() returns the number of items in the menu. The item name
  4202. can be found with item_name(). You can also find the description
  4203. associated with an item using item_description().</p>
  4204. </div>
  4205. <div class="SECT2">
  4206. <hr>
  4207. <h3 class="SECT2"><a name="MENUOPT" id="MENUOPT">17.8. Menu
  4208. Options</a></h3>
  4209. <p>Well, by this time you must be itching for some difference in
  4210. your menu, with lots of functionality. I know. You want Colors !!!.
  4211. You want to create nice menus similar to those text mode <a href=
  4212. "http://www.jersey.net/~debinjoe/games/" target="_top">dos
  4213. games</a>. The functions set_menu_fore() and set_menu_back() can be
  4214. used to change the attribute of the selected item and unselected
  4215. item. The names are misleading. They don't change menu's foreground
  4216. or background which would have been useless.</p>
  4217. <p>The function set_menu_grey() can be used to set the display
  4218. attribute for the non-selectable items in the menu. This brings us
  4219. to the interesting option for an item the one and only
  4220. O_SELECTABLE. We can turn it off by the function item_opts_off()
  4221. and after that that item is not selectable. It's like a grayed item
  4222. in those fancy windows menus. Let's put these concepts in practice
  4223. with this example</p>
  4224. <div class="EXAMPLE"><a name="MMEAT" id="MMEAT"></a>
  4225. <p><b>Example 23. Menu Options example</b></p>
  4226. <table border="0" bgcolor="#E0E0E0" width="100%">
  4227. <tr>
  4228. <td>
  4229. <pre class="PROGRAMLISTING">
  4230. <font color="#000000"><span class=
  4231. "INLINEMEDIAOBJECT">#include &lt;menu.h&gt;
  4232. #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
  4233. #define CTRLD 4
  4234. char *choices[] = {
  4235. "Choice 1",
  4236. "Choice 2",
  4237. "Choice 3",
  4238. "Choice 4",
  4239. "Choice 5",
  4240. "Choice 6",
  4241. "Choice 7",
  4242. "Exit",
  4243. };
  4244. int main()
  4245. { ITEM **my_items;
  4246. int c;
  4247. MENU *my_menu;
  4248. int n_choices, i;
  4249. ITEM *cur_item;
  4250. /* Initialize curses */
  4251. initscr();
  4252. start_color();
  4253. cbreak();
  4254. noecho();
  4255. keypad(stdscr, TRUE);
  4256. init_pair(1, COLOR_RED, COLOR_BLACK);
  4257. init_pair(2, COLOR_GREEN, COLOR_BLACK);
  4258. init_pair(3, COLOR_MAGENTA, COLOR_BLACK);
  4259. /* Initialize items */
  4260. n_choices = ARRAY_SIZE(choices);
  4261. my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *));
  4262. for(i = 0; i &lt; n_choices; ++i)
  4263. my_items[i] = new_item(choices[i], choices[i]);
  4264. my_items[n_choices] = (ITEM *)NULL;
  4265. item_opts_off(my_items[3], O_SELECTABLE);
  4266. item_opts_off(my_items[6], O_SELECTABLE);
  4267. /* Create menu */
  4268. my_menu = new_menu((ITEM **)my_items);
  4269. /* Set fore ground and back ground of the menu */
  4270. set_menu_fore(my_menu, COLOR_PAIR(1) | A_REVERSE);
  4271. set_menu_back(my_menu, COLOR_PAIR(2));
  4272. set_menu_grey(my_menu, COLOR_PAIR(3));
  4273. /* Post the menu */
  4274. mvprintw(LINES - 3, 0, "Press &lt;ENTER&gt; to see the option selected");
  4275. mvprintw(LINES - 2, 0, "Up and Down arrow keys to naviage (F1 to Exit)");
  4276. post_menu(my_menu);
  4277. refresh();
  4278. while((c = getch()) != KEY_F(1))
  4279. { switch(c)
  4280. { case KEY_DOWN:
  4281. menu_driver(my_menu, REQ_DOWN_ITEM);
  4282. break;
  4283. case KEY_UP:
  4284. menu_driver(my_menu, REQ_UP_ITEM);
  4285. break;
  4286. case 10: /* Enter */
  4287. move(20, 0);
  4288. clrtoeol();
  4289. mvprintw(20, 0, "Item selected is : %s",
  4290. item_name(current_item(my_menu)));
  4291. pos_menu_cursor(my_menu);
  4292. break;
  4293. }
  4294. }
  4295. unpost_menu(my_menu);
  4296. for(i = 0; i &lt; n_choices; ++i)
  4297. free_item(my_items[i]);
  4298. free_menu(my_menu);
  4299. endwin();
  4300. }
  4301. </span></font>
  4302. </pre></td>
  4303. </tr>
  4304. </table>
  4305. </div>
  4306. </div>
  4307. <div class="SECT2">
  4308. <hr>
  4309. <h3 class="SECT2"><a name="MENUUSERPTR" id="MENUUSERPTR">17.9. The
  4310. useful User Pointer</a></h3>
  4311. <p>We can associate a user pointer with each item in the menu. It
  4312. works the same way as user pointer in panels. It's not touched by
  4313. menu system. You can store any thing you like in that. I usually
  4314. use it to store the function to be executed when the menu option is
  4315. chosen (It's selected and may be the user pressed
  4316. &lt;ENTER&gt;);</p>
  4317. <div class="EXAMPLE"><a name="MMEUS" id="MMEUS"></a>
  4318. <p><b>Example 24. Menu User Pointer Usage</b></p>
  4319. <table border="0" bgcolor="#E0E0E0" width="100%">
  4320. <tr>
  4321. <td>
  4322. <pre class="PROGRAMLISTING">
  4323. <font color="#000000"><span class=
  4324. "INLINEMEDIAOBJECT">#include &lt;curses.h&gt;
  4325. #include &lt;menu.h&gt;
  4326. #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
  4327. #define CTRLD 4
  4328. char *choices[] = {
  4329. "Choice 1",
  4330. "Choice 2",
  4331. "Choice 3",
  4332. "Choice 4",
  4333. "Choice 5",
  4334. "Choice 6",
  4335. "Choice 7",
  4336. "Exit",
  4337. };
  4338. void func(char *name);
  4339. int main()
  4340. { ITEM **my_items;
  4341. int c;
  4342. MENU *my_menu;
  4343. int n_choices, i;
  4344. ITEM *cur_item;
  4345. /* Initialize curses */
  4346. initscr();
  4347. start_color();
  4348. cbreak();
  4349. noecho();
  4350. keypad(stdscr, TRUE);
  4351. init_pair(1, COLOR_RED, COLOR_BLACK);
  4352. init_pair(2, COLOR_GREEN, COLOR_BLACK);
  4353. init_pair(3, COLOR_MAGENTA, COLOR_BLACK);
  4354. /* Initialize items */
  4355. n_choices = ARRAY_SIZE(choices);
  4356. my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *));
  4357. for(i = 0; i &lt; n_choices; ++i)
  4358. { my_items[i] = new_item(choices[i], choices[i]);
  4359. /* Set the user pointer */
  4360. set_item_userptr(my_items[i], func);
  4361. }
  4362. my_items[n_choices] = (ITEM *)NULL;
  4363. /* Create menu */
  4364. my_menu = new_menu((ITEM **)my_items);
  4365. /* Post the menu */
  4366. mvprintw(LINES - 3, 0, "Press &lt;ENTER&gt; to see the option selected");
  4367. mvprintw(LINES - 2, 0, "Up and Down arrow keys to naviage (F1 to Exit)");
  4368. post_menu(my_menu);
  4369. refresh();
  4370. while((c = getch()) != KEY_F(1))
  4371. { switch(c)
  4372. { case KEY_DOWN:
  4373. menu_driver(my_menu, REQ_DOWN_ITEM);
  4374. break;
  4375. case KEY_UP:
  4376. menu_driver(my_menu, REQ_UP_ITEM);
  4377. break;
  4378. case 10: /* Enter */
  4379. { ITEM *cur;
  4380. void (*p)(char *);
  4381. cur = current_item(my_menu);
  4382. p = item_userptr(cur);
  4383. p((char *)item_name(cur));
  4384. pos_menu_cursor(my_menu);
  4385. break;
  4386. }
  4387. break;
  4388. }
  4389. }
  4390. unpost_menu(my_menu);
  4391. for(i = 0; i &lt; n_choices; ++i)
  4392. free_item(my_items[i]);
  4393. free_menu(my_menu);
  4394. endwin();
  4395. }
  4396. void func(char *name)
  4397. { move(20, 0);
  4398. clrtoeol();
  4399. mvprintw(20, 0, "Item selected is : %s", name);
  4400. } </span></font>
  4401. </pre></td>
  4402. </tr>
  4403. </table>
  4404. </div>
  4405. </div>
  4406. </div>
  4407. <div class="SECT1">
  4408. <hr>
  4409. <h2 class="SECT1"><a name="FORMS" id="FORMS">18. Forms
  4410. Library</a></h2>
  4411. <p>Well. If you have seen those forms on web pages which take input
  4412. from users and do various kinds of things, you might be wondering
  4413. how would any one create such forms in text mode display. It's
  4414. quite difficult to write those nifty forms in plain ncurses. Forms
  4415. library tries to provide a basic frame work to build and maintain
  4416. forms with ease. It has lot of features(functions) which manage
  4417. validation, dynamic expansion of fields etc.. Let's see it in full
  4418. flow.</p>
  4419. <p>A form is a collection of fields; each field can be either a
  4420. label(static text) or a data-entry location. The forms also library
  4421. provides functions to divide forms into multiple pages.</p>
  4422. <div class="SECT2">
  4423. <hr>
  4424. <h3 class="SECT2"><a name="FORMBASICS" id="FORMBASICS">18.1. The
  4425. Basics</a></h3>
  4426. <p>Forms are created in much the same way as menus. First the
  4427. fields related to the form are created with new_field(). You can
  4428. set options for the fields, so that they can be displayed with some
  4429. fancy attributes, validated before the field looses focus etc..
  4430. Then the fields are attached to form. After this, the form can be
  4431. posted to display and is ready to receive inputs. On the similar
  4432. lines to menu_driver(), the form is manipulated with form_driver().
  4433. We can send requests to form_driver to move focus to a certain
  4434. field, move cursor to end of the field etc.. After the user enters
  4435. values in the fields and validation done, form can be unposted and
  4436. memory allocated can be freed.</p>
  4437. <p>The general flow of control of a forms program looks like
  4438. this.</p>
  4439. <ol type="1">
  4440. <li>
  4441. <p>Initialize curses</p>
  4442. </li>
  4443. <li>
  4444. <p>Create fields using new_field(). You can specify the height and
  4445. width of the field, and its position on the form.</p>
  4446. </li>
  4447. <li>
  4448. <p>Create the forms with new_form() by specifying the fields to be
  4449. attached with.</p>
  4450. </li>
  4451. <li>
  4452. <p>Post the form with form_post() and refresh the screen.</p>
  4453. </li>
  4454. <li>
  4455. <p>Process the user requests with a loop and do necessary updates
  4456. to form with form_driver.</p>
  4457. </li>
  4458. <li>
  4459. <p>Unpost the menu with form_unpost()</p>
  4460. </li>
  4461. <li>
  4462. <p>Free the memory allocated to menu by free_form()</p>
  4463. </li>
  4464. <li>
  4465. <p>Free the memory allocated to the items with free_field()</p>
  4466. </li>
  4467. <li>
  4468. <p>End curses</p>
  4469. </li>
  4470. </ol>
  4471. <p>As you can see, working with forms library is much similar to
  4472. handling menu library. The following examples will explore various
  4473. aspects of form processing. Let's start the journey with a simple
  4474. example. first.</p>
  4475. </div>
  4476. <div class="SECT2">
  4477. <hr>
  4478. <h3 class="SECT2"><a name="COMPILEFORMS" id="COMPILEFORMS">18.2.
  4479. Compiling With the Forms Library</a></h3>
  4480. <p>To use forms library functions, you have to include form.h and
  4481. to link the program with forms library the flag -lform should be
  4482. added along with -lncurses in that order.</p>
  4483. <table border="0" bgcolor="#E0E0E0" width="100%">
  4484. <tr>
  4485. <td>
  4486. <pre class="PROGRAMLISTING">
  4487. <font color="#000000"> #include &lt;form.h&gt;
  4488. .
  4489. .
  4490. .
  4491. compile and link: gcc &lt;program file&gt; -lform -lncurses</font>
  4492. </pre></td>
  4493. </tr>
  4494. </table>
  4495. <div class="EXAMPLE"><a name="FFOSI" id="FFOSI"></a>
  4496. <p><b>Example 25. Forms Basics</b></p>
  4497. <table border="0" bgcolor="#E0E0E0" width="100%">
  4498. <tr>
  4499. <td>
  4500. <pre class="PROGRAMLISTING">
  4501. <font color="#000000"><span class=
  4502. "INLINEMEDIAOBJECT">#include &lt;form.h&gt;
  4503. int main()
  4504. { FIELD *field[3];
  4505. FORM *my_form;
  4506. int ch;
  4507. /* Initialize curses */
  4508. initscr();
  4509. cbreak();
  4510. noecho();
  4511. keypad(stdscr, TRUE);
  4512. /* Initialize the fields */
  4513. field[0] = new_field(1, 10, 4, 18, 0, 0);
  4514. field[1] = new_field(1, 10, 6, 18, 0, 0);
  4515. field[2] = NULL;
  4516. /* Set field options */
  4517. set_field_back(field[0], A_UNDERLINE); /* Print a line for the option */
  4518. field_opts_off(field[0], O_AUTOSKIP); /* Don't go to next field when this */
  4519. /* Field is filled up */
  4520. set_field_back(field[1], A_UNDERLINE);
  4521. field_opts_off(field[1], O_AUTOSKIP);
  4522. /* Create the form and post it */
  4523. my_form = new_form(field);
  4524. post_form(my_form);
  4525. refresh();
  4526. mvprintw(4, 10, "Value 1:");
  4527. mvprintw(6, 10, "Value 2:");
  4528. refresh();
  4529. /* Loop through to get user requests */
  4530. while((ch = getch()) != KEY_F(1))
  4531. { switch(ch)
  4532. { case KEY_DOWN:
  4533. /* Go to next field */
  4534. form_driver(my_form, REQ_NEXT_FIELD);
  4535. /* Go to the end of the present buffer */
  4536. /* Leaves nicely at the last character */
  4537. form_driver(my_form, REQ_END_LINE);
  4538. break;
  4539. case KEY_UP:
  4540. /* Go to previous field */
  4541. form_driver(my_form, REQ_PREV_FIELD);
  4542. form_driver(my_form, REQ_END_LINE);
  4543. break;
  4544. default:
  4545. /* If this is a normal character, it gets */
  4546. /* Printed */
  4547. form_driver(my_form, ch);
  4548. break;
  4549. }
  4550. }
  4551. /* Un post form and free the memory */
  4552. unpost_form(my_form);
  4553. free_form(my_form);
  4554. free_field(field[0]);
  4555. free_field(field[1]);
  4556. endwin();
  4557. return 0;
  4558. }</span></font>
  4559. </pre></td>
  4560. </tr>
  4561. </table>
  4562. </div>
  4563. <p>Above example is pretty straight forward. It creates two fields
  4564. with <var class="LITERAL">new_field()</var>. new_field() takes
  4565. height, width, starty, startx, number of offscreen rows and number
  4566. of additional working buffers. The fifth argument number of
  4567. offscreen rows specifies how much of the field to be shown. If it
  4568. is zero, the entire field is always displayed otherwise the form
  4569. will be scrollable when the user accesses not displayed parts of
  4570. the field. The forms library allocates one buffer per field to
  4571. store the data user enters. Using the last parameter to new_field()
  4572. we can specify it to allocate some additional buffers. These can be
  4573. used for any purpose you like.</p>
  4574. <p>After creating the fields, back ground attribute of both of them
  4575. is set to an underscore with set_field_back(). The AUTOSKIP option
  4576. is turned off using field_opts_off(). If this option is turned on,
  4577. focus will move to the next field in the form once the active field
  4578. is filled up completely.</p>
  4579. <p>After attaching the fields to the form, it is posted. Here on,
  4580. user inputs are processed in the while loop, by making
  4581. corresponding requests to form_driver. The details of all the
  4582. requests to the form_driver() are explained later.</p>
  4583. </div>
  4584. <div class="SECT2">
  4585. <hr>
  4586. <h3 class="SECT2"><a name="PLAYFIELDS" id="PLAYFIELDS">18.3.
  4587. Playing with Fields</a></h3>
  4588. <p>Each form field is associated with a lot of attributes. They can
  4589. be manipulated to get the required effect and to have fun !!!. So
  4590. why wait?</p>
  4591. <div class="SECT3">
  4592. <hr>
  4593. <h4 class="SECT3"><a name="FETCHINFO" id="FETCHINFO">18.3.1.
  4594. Fetching Size and Location of Field</a></h4>
  4595. <p>The parameters we have given at the time of creation of a field
  4596. can be retrieved with field_info(). It returns height, width,
  4597. starty, startx, number of offscreen rows, and number of additional
  4598. buffers into the parameters given to it. It is a sort of inverse of
  4599. new_field().</p>
  4600. <table border="0" bgcolor="#E0E0E0" width="100%">
  4601. <tr>
  4602. <td>
  4603. <pre class="PROGRAMLISTING">
  4604. <font color=
  4605. "#000000">int field_info( FIELD *field, /* field from which to fetch */
  4606. int *height, *int width, /* field size */
  4607. int *top, int *left, /* upper left corner */
  4608. int *offscreen, /* number of offscreen rows */
  4609. int *nbuf); /* number of working buffers */</font>
  4610. </pre></td>
  4611. </tr>
  4612. </table>
  4613. </div>
  4614. <div class="SECT3">
  4615. <hr>
  4616. <h4 class="SECT3"><a name="MOVEFIELD" id="MOVEFIELD">18.3.2. Moving
  4617. the field</a></h4>
  4618. <p>The location of the field can be moved to a different position
  4619. with move_field().</p>
  4620. <table border="0" bgcolor="#E0E0E0" width="100%">
  4621. <tr>
  4622. <td>
  4623. <pre class="PROGRAMLISTING">
  4624. <font color=
  4625. "#000000">int move_field( FIELD *field, /* field to alter */
  4626. int top, int left); /* new upper-left corner */</font>
  4627. </pre></td>
  4628. </tr>
  4629. </table>
  4630. <p>As usual, the changed position can be queried with
  4631. field_infor().</p>
  4632. </div>
  4633. <div class="SECT3">
  4634. <hr>
  4635. <h4 class="SECT3"><a name="JUSTIFYFIELD" id="JUSTIFYFIELD">18.3.3.
  4636. Field Justification</a></h4>
  4637. <p>The justification to be done for the field can be fixed using
  4638. the function set_field_just().</p>
  4639. <table border="0" bgcolor="#E0E0E0" width="100%">
  4640. <tr>
  4641. <td>
  4642. <pre class="PROGRAMLISTING">
  4643. <font color=
  4644. "#000000"> int set_field_just(FIELD *field, /* field to alter */
  4645. int justmode); /* mode to set */
  4646. int field_just(FIELD *field); /* fetch justify mode of field */</font>
  4647. </pre></td>
  4648. </tr>
  4649. </table>
  4650. <p>The justification mode valued accepted and returned by these
  4651. functions are NO_JUSTIFICATION, JUSTIFY_RIGHT, JUSTIFY_LEFT, or
  4652. JUSTIFY_CENTER.</p>
  4653. </div>
  4654. <div class="SECT3">
  4655. <hr>
  4656. <h4 class="SECT3"><a name="FIELDDISPATTRIB" id=
  4657. "FIELDDISPATTRIB">18.3.4. Field Display Attributes</a></h4>
  4658. <p>As you have seen, in the above example, display attribute for
  4659. the fields can be set with set_field_fore() and setfield_back().
  4660. These functions set foreground and background attribute of the
  4661. fields. You can also specify a pad character which will be filled
  4662. in the unfilled portion of the field. The pad character is set with
  4663. a call to set_field_pad(). Default pad value is a space. The
  4664. functions field_fore(), field_back, field_pad() can be used to
  4665. query the present foreground, background attributes and pad
  4666. character for the field. The following list gives the usage of
  4667. functions.</p>
  4668. <table border="0" bgcolor="#E0E0E0" width="100%">
  4669. <tr>
  4670. <td>
  4671. <pre class="PROGRAMLISTING">
  4672. <font color=
  4673. "#000000">&#13;int set_field_fore(FIELD *field, /* field to alter */
  4674. chtype attr); /* attribute to set */
  4675. chtype field_fore(FIELD *field); /* field to query */
  4676. /* returns foreground attribute */
  4677. int set_field_back(FIELD *field, /* field to alter */
  4678. chtype attr); /* attribute to set */
  4679. chtype field_back(FIELD *field); /* field to query */
  4680. /* returns background attribute */
  4681. int set_field_pad(FIELD *field, /* field to alter */
  4682. int pad); /* pad character to set */
  4683. chtype field_pad(FIELD *field); /* field to query */
  4684. /* returns present pad character */&#13;</font>
  4685. </pre></td>
  4686. </tr>
  4687. </table>
  4688. <p>Though above functions seem quite simple, using colors with
  4689. set_field_fore() may be frustrating in the beginning. Let me first
  4690. explain about foreground and background attributes of a field. The
  4691. foreground attribute is associated with the character. That means a
  4692. character in the field is printed with the attribute you have set
  4693. with set_field_fore(). Background attribute is the attribute used
  4694. to fill background of field, whether any character is there or not.
  4695. So what about colors? Since colors are always defined in pairs,
  4696. what is the right way to display colored fields? Here's an example
  4697. clarifying color attributes.</p>
  4698. <div class="EXAMPLE"><a name="FFOAT" id="FFOAT"></a>
  4699. <p><b>Example 26. Form Attributes example</b></p>
  4700. <table border="0" bgcolor="#E0E0E0" width="100%">
  4701. <tr>
  4702. <td>
  4703. <pre class="PROGRAMLISTING">
  4704. <font color="#000000"><span class=
  4705. "INLINEMEDIAOBJECT">#include &lt;form.h&gt;
  4706. int main()
  4707. { FIELD *field[3];
  4708. FORM *my_form;
  4709. int ch;
  4710. /* Initialize curses */
  4711. initscr();
  4712. start_color();
  4713. cbreak();
  4714. noecho();
  4715. keypad(stdscr, TRUE);
  4716. /* Initialize few color pairs */
  4717. init_pair(1, COLOR_WHITE, COLOR_BLUE);
  4718. init_pair(2, COLOR_WHITE, COLOR_BLUE);
  4719. /* Initialize the fields */
  4720. field[0] = new_field(1, 10, 4, 18, 0, 0);
  4721. field[1] = new_field(1, 10, 6, 18, 0, 0);
  4722. field[2] = NULL;
  4723. /* Set field options */
  4724. set_field_fore(field[0], COLOR_PAIR(1));/* Put the field with blue background */
  4725. set_field_back(field[0], COLOR_PAIR(2));/* and white foreground (characters */
  4726. /* are printed in white */
  4727. field_opts_off(field[0], O_AUTOSKIP); /* Don't go to next field when this */
  4728. /* Field is filled up */
  4729. set_field_back(field[1], A_UNDERLINE);
  4730. field_opts_off(field[1], O_AUTOSKIP);
  4731. /* Create the form and post it */
  4732. my_form = new_form(field);
  4733. post_form(my_form);
  4734. refresh();
  4735. set_current_field(my_form, field[0]); /* Set focus to the colored field */
  4736. mvprintw(4, 10, "Value 1:");
  4737. mvprintw(6, 10, "Value 2:");
  4738. mvprintw(LINES - 2, 0, "Use UP, DOWN arrow keys to switch between fields");
  4739. refresh();
  4740. /* Loop through to get user requests */
  4741. while((ch = getch()) != KEY_F(1))
  4742. { switch(ch)
  4743. { case KEY_DOWN:
  4744. /* Go to next field */
  4745. form_driver(my_form, REQ_NEXT_FIELD);
  4746. /* Go to the end of the present buffer */
  4747. /* Leaves nicely at the last character */
  4748. form_driver(my_form, REQ_END_LINE);
  4749. break;
  4750. case KEY_UP:
  4751. /* Go to previous field */
  4752. form_driver(my_form, REQ_PREV_FIELD);
  4753. form_driver(my_form, REQ_END_LINE);
  4754. break;
  4755. default:
  4756. /* If this is a normal character, it gets */
  4757. /* Printed */
  4758. form_driver(my_form, ch);
  4759. break;
  4760. }
  4761. }
  4762. /* Un post form and free the memory */
  4763. unpost_form(my_form);
  4764. free_form(my_form);
  4765. free_field(field[0]);
  4766. free_field(field[1]);
  4767. endwin();
  4768. return 0;
  4769. }</span></font>
  4770. </pre></td>
  4771. </tr>
  4772. </table>
  4773. </div>
  4774. <p>Play with the color pairs and try to understand the foreground
  4775. and background attributes. In my programs using color attributes, I
  4776. usually set only the background with set_field_back(). Curses
  4777. simply doesn't allow defining individual color attributes.</p>
  4778. </div>
  4779. <div class="SECT3">
  4780. <hr>
  4781. <h4 class="SECT3"><a name="FIELDOPTIONBITS" id=
  4782. "FIELDOPTIONBITS">18.3.5. Field Option Bits</a></h4>
  4783. <p>There is also a large collection of field option bits you can
  4784. set to control various aspects of forms processing. You can
  4785. manipulate them with these functions:</p>
  4786. <table border="0" bgcolor="#E0E0E0" width="100%">
  4787. <tr>
  4788. <td>
  4789. <pre class="PROGRAMLISTING">
  4790. <font color=
  4791. "#000000">int set_field_opts(FIELD *field, /* field to alter */
  4792. int attr); /* attribute to set */
  4793. int field_opts_on(FIELD *field, /* field to alter */
  4794. int attr); /* attributes to turn on */
  4795. int field_opts_off(FIELD *field, /* field to alter */
  4796. int attr); /* attributes to turn off */
  4797. int field_opts(FIELD *field); /* field to query */ </font>
  4798. </pre></td>
  4799. </tr>
  4800. </table>
  4801. <p>The function set_field_opts() can be used to directly set
  4802. attributes of a field or you can choose to switch a few attributes
  4803. on and off with field_opts_on() and field_opts_off() selectively.
  4804. Anytime you can query the attributes of a field with field_opts().
  4805. The following is the list of available options. By default, all
  4806. options are on.</p>
  4807. <div class="VARIABLELIST">
  4808. <dl>
  4809. <dt>O_VISIBLE</dt>
  4810. <dd>
  4811. <p>Controls whether the field is visible on the screen. Can be used
  4812. during form processing to hide or pop up fields depending on the
  4813. value of parent fields.</p>
  4814. </dd>
  4815. <dt>O_ACTIVE</dt>
  4816. <dd>
  4817. <p>Controls whether the field is active during forms processing
  4818. (i.e. visited by form navigation keys). Can be used to make labels
  4819. or derived fields with buffer values alterable by the forms
  4820. application, not the user.</p>
  4821. </dd>
  4822. <dt>O_PUBLIC</dt>
  4823. <dd>
  4824. <p>Controls whether data is displayed during field entry. If this
  4825. option is turned off on a field, the library will accept and edit
  4826. data in that field, but it will not be displayed and the visible
  4827. field cursor will not move. You can turn off the O_PUBLIC bit to
  4828. define password fields.</p>
  4829. </dd>
  4830. <dt>O_EDIT</dt>
  4831. <dd>
  4832. <p>Controls whether the field's data can be modified. When this
  4833. option is off, all editing requests except <var class=
  4834. "LITERAL">REQ_PREV_CHOICE</var> and <var class=
  4835. "LITERAL">REQ_NEXT_CHOICE</var>will fail. Such read-only fields may
  4836. be useful for help messages.</p>
  4837. </dd>
  4838. <dt>O_WRAP</dt>
  4839. <dd>
  4840. <p>Controls word-wrapping in multi-line fields. Normally, when any
  4841. character of a (blank-separated) word reaches the end of the
  4842. current line, the entire word is wrapped to the next line (assuming
  4843. there is one). When this option is off, the word will be split
  4844. across the line break.</p>
  4845. </dd>
  4846. <dt>O_BLANK</dt>
  4847. <dd>
  4848. <p>Controls field blanking. When this option is on, entering a
  4849. character at the first field position erases the entire field
  4850. (except for the just-entered character).</p>
  4851. </dd>
  4852. <dt>O_AUTOSKIP</dt>
  4853. <dd>
  4854. <p>Controls automatic skip to next field when this one fills.
  4855. Normally, when the forms user tries to type more data into a field
  4856. than will fit, the editing location jumps to next field. When this
  4857. option is off, the user's cursor will hang at the end of the field.
  4858. This option is ignored in dynamic fields that have not reached
  4859. their size limit.</p>
  4860. </dd>
  4861. <dt>O_NULLOK</dt>
  4862. <dd>
  4863. <p>Controls whether validation is applied to blank fields.
  4864. Normally, it is not; the user can leave a field blank without
  4865. invoking the usual validation check on exit. If this option is off
  4866. on a field, exit from it will invoke a validation check.</p>
  4867. </dd>
  4868. <dt>O_PASSOK</dt>
  4869. <dd>
  4870. <p>Controls whether validation occurs on every exit, or only after
  4871. the field is modified. Normally the latter is true. Setting
  4872. O_PASSOK may be useful if your field's validation function may
  4873. change during forms processing.</p>
  4874. </dd>
  4875. <dt>O_STATIC</dt>
  4876. <dd>
  4877. <p>Controls whether the field is fixed to its initial dimensions.
  4878. If you turn this off, the field becomes dynamic and will stretch to
  4879. fit entered data.</p>
  4880. </dd>
  4881. </dl>
  4882. </div>
  4883. <p>A field's options cannot be changed while the field is currently
  4884. selected. However, options may be changed on posted fields that are
  4885. not current.</p>
  4886. <p>The option values are bit-masks and can be composed with
  4887. logical-or in the obvious way. You have seen the usage of switching
  4888. off O_AUTOSKIP option. The following example clarifies usage of
  4889. some more options. Other options are explained where
  4890. appropriate.</p>
  4891. <div class="EXAMPLE"><a name="FFOOP" id="FFOOP"></a>
  4892. <p><b>Example 27. Field Options Usage example</b></p>
  4893. <table border="0" bgcolor="#E0E0E0" width="100%">
  4894. <tr>
  4895. <td>
  4896. <pre class="PROGRAMLISTING">
  4897. <font color="#000000"><span class=
  4898. "INLINEMEDIAOBJECT">#include &lt;form.h&gt;
  4899. #define STARTX 15
  4900. #define STARTY 4
  4901. #define WIDTH 25
  4902. #define N_FIELDS 3
  4903. int main()
  4904. { FIELD *field[N_FIELDS];
  4905. FORM *my_form;
  4906. int ch, i;
  4907. /* Initialize curses */
  4908. initscr();
  4909. cbreak();
  4910. noecho();
  4911. keypad(stdscr, TRUE);
  4912. /* Initialize the fields */
  4913. for(i = 0; i &lt; N_FIELDS - 1; ++i)
  4914. field[i] = new_field(1, WIDTH, STARTY + i * 2, STARTX, 0, 0);
  4915. field[N_FIELDS - 1] = NULL;
  4916. /* Set field options */
  4917. set_field_back(field[1], A_UNDERLINE); /* Print a line for the option */
  4918. field_opts_off(field[0], O_ACTIVE); /* This field is a static label */
  4919. field_opts_off(field[1], O_PUBLIC); /* This filed is like a password field*/
  4920. field_opts_off(field[1], O_AUTOSKIP); /* To avoid entering the same field */
  4921. /* after last character is entered */
  4922. /* Create the form and post it */
  4923. my_form = new_form(field);
  4924. post_form(my_form);
  4925. refresh();
  4926. set_field_just(field[0], JUSTIFY_CENTER); /* Center Justification */
  4927. set_field_buffer(field[0], 0, "This is a static Field");
  4928. /* Initialize the field */
  4929. mvprintw(STARTY, STARTX - 10, "Field 1:");
  4930. mvprintw(STARTY + 2, STARTX - 10, "Field 2:");
  4931. refresh();
  4932. /* Loop through to get user requests */
  4933. while((ch = getch()) != KEY_F(1))
  4934. { switch(ch)
  4935. { case KEY_DOWN:
  4936. /* Go to next field */
  4937. form_driver(my_form, REQ_NEXT_FIELD);
  4938. /* Go to the end of the present buffer */
  4939. /* Leaves nicely at the last character */
  4940. form_driver(my_form, REQ_END_LINE);
  4941. break;
  4942. case KEY_UP:
  4943. /* Go to previous field */
  4944. form_driver(my_form, REQ_PREV_FIELD);
  4945. form_driver(my_form, REQ_END_LINE);
  4946. break;
  4947. default:
  4948. /* If this is a normal character, it gets */
  4949. /* Printed */
  4950. form_driver(my_form, ch);
  4951. break;
  4952. }
  4953. }
  4954. /* Un post form and free the memory */
  4955. unpost_form(my_form);
  4956. free_form(my_form);
  4957. free_field(field[0]);
  4958. free_field(field[1]);
  4959. endwin();
  4960. return 0;
  4961. }</span></font>
  4962. </pre></td>
  4963. </tr>
  4964. </table>
  4965. </div>
  4966. <p>This example, though useless, shows the usage of options. If
  4967. used properly, they can present information very effectively in a
  4968. form. The second field being not O_PUBLIC, does not show the
  4969. characters you are typing.</p>
  4970. </div>
  4971. <div class="SECT3">
  4972. <hr>
  4973. <h4 class="SECT3"><a name="FIELDSTATUS" id="FIELDSTATUS">18.3.6.
  4974. Field Status</a></h4>
  4975. <p>The field status specifies whether the field has got edited or
  4976. not. It is initially set to FALSE and when user enters something
  4977. and the data buffer gets modified it becomes TRUE. So a field's
  4978. status can be queried to find out whether it has been modified or
  4979. not. The following functions can assist in those operations.</p>
  4980. <table border="0" bgcolor="#E0E0E0" width="100%">
  4981. <tr>
  4982. <td>
  4983. <pre class="PROGRAMLISTING">
  4984. <font color=
  4985. "#000000">int set_field_status(FIELD *field, /* field to alter */
  4986. int status); /* status to set */
  4987. int field_status(FIELD *field); /* fetch status of field */</font>
  4988. </pre></td>
  4989. </tr>
  4990. </table>
  4991. <p>It's better to check the field's status only after after leaving
  4992. the field, as data buffer might not have been updated yet as the
  4993. validation is still due. To guarantee that right status is
  4994. returned, call field_status() either (1) in the field's exit
  4995. validation check routine, (2) from the field's or form's
  4996. initialization or termination hooks, or (3) just after a
  4997. REQ_VALIDATION request has been processed by the forms driver</p>
  4998. </div>
  4999. <div class="SECT3">
  5000. <hr>
  5001. <h4 class="SECT3"><a name="FIELDUSERPTR" id="FIELDUSERPTR">18.3.7.
  5002. Field User Pointer</a></h4>
  5003. <p>Every field structure contains one pointer that can be used by
  5004. the user for various purposes. It is not touched by forms library
  5005. and can be used for any purpose by the user. The following
  5006. functions set and fetch user pointer.</p>
  5007. <table border="0" bgcolor="#E0E0E0" width="100%">
  5008. <tr>
  5009. <td>
  5010. <pre class="PROGRAMLISTING">
  5011. <font color="#000000">int set_field_userptr(FIELD *field,
  5012. char *userptr); /* the user pointer you wish to associate */
  5013. /* with the field */
  5014. char *field_userptr(FIELD *field); /* fetch user pointer of the field */</font>
  5015. </pre></td>
  5016. </tr>
  5017. </table>
  5018. </div>
  5019. <div class="SECT3">
  5020. <hr>
  5021. <h4 class="SECT3"><a name="VARIABLESIZEFIELDS" id=
  5022. "VARIABLESIZEFIELDS">18.3.8. Variable-Sized Fields</a></h4>
  5023. <p>If you want a dynamically changing field with variable width,
  5024. this is the feature you want to put to full use. This will allow
  5025. the user to enter more data than the original size of the field and
  5026. let the field grow. According to the field orientation it will
  5027. scroll horizontally or vertically to incorporate the new data.</p>
  5028. <p>To make a field dynamically growable, the option O_STATIC should
  5029. be turned off. This can be done with a</p>
  5030. <table border="0" bgcolor="#E0E0E0" width="100%">
  5031. <tr>
  5032. <td>
  5033. <pre class="PROGRAMLISTING">
  5034. <font color=
  5035. "#000000"> field_opts_off(field_pointer, O_STATIC);</font>
  5036. </pre></td>
  5037. </tr>
  5038. </table>
  5039. <p>But it's usually not advisable to allow a field to grow
  5040. infinitely. You can set a maximum limit to the growth of the field
  5041. with</p>
  5042. <table border="0" bgcolor="#E0E0E0" width="100%">
  5043. <tr>
  5044. <td>
  5045. <pre class="PROGRAMLISTING">
  5046. <font color=
  5047. "#000000">int set_max_field(FIELD *field, /* Field on which to operate */
  5048. int max_growth); /* maximum growth allowed for the field */</font>
  5049. </pre></td>
  5050. </tr>
  5051. </table>
  5052. <p>The field info for a dynamically growable field can be retrieved
  5053. by</p>
  5054. <table border="0" bgcolor="#E0E0E0" width="100%">
  5055. <tr>
  5056. <td>
  5057. <pre class="PROGRAMLISTING">
  5058. <font color=
  5059. "#000000">int dynamic_field_info( FIELD *field, /* Field on which to operate */
  5060. int *prows, /* number of rows will be filled in this */
  5061. int *pcols, /* number of columns will be filled in this*/
  5062. int *pmax) /* maximum allowable growth will be filled */
  5063. /* in this */</font>
  5064. </pre></td>
  5065. </tr>
  5066. </table>
  5067. Though field_info work as usual, it is advisable to use this
  5068. function to get the proper attributes of a dynamically growable
  5069. field.
  5070. <p>Recall the library routine new_field; a new field created with
  5071. height set to one will be defined to be a one line field. A new
  5072. field created with height greater than one will be defined to be a
  5073. multi line field.</p>
  5074. <p>A one line field with O_STATIC turned off (dynamically growable
  5075. field) will contain a single fixed row, but the number of columns
  5076. can increase if the user enters more data than the initial field
  5077. will hold. The number of columns displayed will remain fixed and
  5078. the additional data will scroll horizontally.</p>
  5079. <p>A multi line field with O_STATIC turned off (dynamically
  5080. growable field) will contain a fixed number of columns, but the
  5081. number of rows can increase if the user enters more data than the
  5082. initial field will hold. The number of rows displayed will remain
  5083. fixed and the additional data will scroll vertically.</p>
  5084. <p>The above two paragraphs pretty much describe a dynamically
  5085. growable field's behavior. The way other parts of forms library
  5086. behaves is described below:</p>
  5087. <ol type="1">
  5088. <li>
  5089. <p>The field option O_AUTOSKIP will be ignored if the option
  5090. O_STATIC is off and there is no maximum growth specified for the
  5091. field. Currently, O_AUTOSKIP generates an automatic REQ_NEXT_FIELD
  5092. form driver request when the user types in the last character
  5093. position of a field. On a growable field with no maximum growth
  5094. specified, there is no last character position. If a maximum growth
  5095. is specified, the O_AUTOSKIP option will work as normal if the
  5096. field has grown to its maximum size.</p>
  5097. </li>
  5098. <li>
  5099. <p>The field justification will be ignored if the option O_STATIC
  5100. is off. Currently, set_field_just can be used to JUSTIFY_LEFT,
  5101. JUSTIFY_RIGHT, JUSTIFY_CENTER the contents of a one line field. A
  5102. growable one line field will, by definition, grow and scroll
  5103. horizontally and may contain more data than can be justified. The
  5104. return from field_just will be unchanged.</p>
  5105. </li>
  5106. <li>
  5107. <p>The overloaded form driver request REQ_NEW_LINE will operate the
  5108. same way regardless of the O_NL_OVERLOAD form option if the field
  5109. option O_STATIC is off and there is no maximum growth specified for
  5110. the field. Currently, if the form option O_NL_OVERLOAD is on,
  5111. REQ_NEW_LINE implicitly generates a REQ_NEXT_FIELD if called from
  5112. the last line of a field. If a field can grow without bound, there
  5113. is no last line, so REQ_NEW_LINE will never implicitly generate a
  5114. REQ_NEXT_FIELD. If a maximum growth limit is specified and the
  5115. O_NL_OVERLOAD form option is on, REQ_NEW_LINE will only implicitly
  5116. generate REQ_NEXT_FIELD if the field has grown to its maximum size
  5117. and the user is on the last line.</p>
  5118. </li>
  5119. <li>
  5120. <p>The library call dup_field will work as usual; it will duplicate
  5121. the field, including the current buffer size and contents of the
  5122. field being duplicated. Any specified maximum growth will also be
  5123. duplicated.</p>
  5124. </li>
  5125. <li>
  5126. <p>The library call link_field will work as usual; it will
  5127. duplicate all field attributes and share buffers with the field
  5128. being linked. If the O_STATIC field option is subsequently changed
  5129. by a field sharing buffers, how the system reacts to an attempt to
  5130. enter more data into the field than the buffer will currently hold
  5131. will depend on the setting of the option in the current field.</p>
  5132. </li>
  5133. <li>
  5134. <p>The library call field_info will work as usual; the variable
  5135. nrow will contain the value of the original call to new_field. The
  5136. user should use dynamic_field_info, described above, to query the
  5137. current size of the buffer.</p>
  5138. </li>
  5139. </ol>
  5140. <p>Some of the above points make sense only after explaining form
  5141. driver. We will be looking into that in next few sections.</p>
  5142. </div>
  5143. </div>
  5144. <div class="SECT2">
  5145. <hr>
  5146. <h3 class="SECT2"><a name="FORMWINDOWS" id="FORMWINDOWS">18.4. Form
  5147. Windows</a></h3>
  5148. <p>The form windows concept is pretty much similar to menu windows.
  5149. Every form is associated with a main window and a sub window. The
  5150. form main window displays any title or border associated or
  5151. whatever the user wishes. Then the sub window contains all the
  5152. fields and displays them according to their position. This gives
  5153. the flexibility of manipulating fancy form displaying very
  5154. easily.</p>
  5155. <p>Since this is pretty much similar to menu windows, I am
  5156. providing an example with out much explanation. The functions are
  5157. similar and they work the same way.</p>
  5158. <div class="EXAMPLE"><a name="FFOWI" id="FFOWI"></a>
  5159. <p><b>Example 28. Form Windows Example</b></p>
  5160. <table border="0" bgcolor="#E0E0E0" width="100%">
  5161. <tr>
  5162. <td>
  5163. <pre class="PROGRAMLISTING">
  5164. <font color="#000000"><span class=
  5165. "INLINEMEDIAOBJECT">#include &lt;form.h&gt;
  5166. void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color);
  5167. int main()
  5168. {
  5169. FIELD *field[3];
  5170. FORM *my_form;
  5171. WINDOW *my_form_win;
  5172. int ch, rows, cols;
  5173. /* Initialize curses */
  5174. initscr();
  5175. start_color();
  5176. cbreak();
  5177. noecho();
  5178. keypad(stdscr, TRUE);
  5179. /* Initialize few color pairs */
  5180. init_pair(1, COLOR_RED, COLOR_BLACK);
  5181. /* Initialize the fields */
  5182. field[0] = new_field(1, 10, 6, 1, 0, 0);
  5183. field[1] = new_field(1, 10, 8, 1, 0, 0);
  5184. field[2] = NULL;
  5185. /* Set field options */
  5186. set_field_back(field[0], A_UNDERLINE);
  5187. field_opts_off(field[0], O_AUTOSKIP); /* Don't go to next field when this */
  5188. /* Field is filled up */
  5189. set_field_back(field[1], A_UNDERLINE);
  5190. field_opts_off(field[1], O_AUTOSKIP);
  5191. /* Create the form and post it */
  5192. my_form = new_form(field);
  5193. /* Calculate the area required for the form */
  5194. scale_form(my_form, &amp;rows, &amp;cols);
  5195. /* Create the window to be associated with the form */
  5196. my_form_win = newwin(rows + 4, cols + 4, 4, 4);
  5197. keypad(my_form_win, TRUE);
  5198. /* Set main window and sub window */
  5199. set_form_win(my_form, my_form_win);
  5200. set_form_sub(my_form, derwin(my_form_win, rows, cols, 2, 2));
  5201. /* Print a border around the main window and print a title */
  5202. box(my_form_win, 0, 0);
  5203. print_in_middle(my_form_win, 1, 0, cols + 4, "My Form", COLOR_PAIR(1));
  5204. post_form(my_form);
  5205. wrefresh(my_form_win);
  5206. mvprintw(LINES - 2, 0, "Use UP, DOWN arrow keys to switch between fields");
  5207. refresh();
  5208. /* Loop through to get user requests */
  5209. while((ch = wgetch(my_form_win)) != KEY_F(1))
  5210. { switch(ch)
  5211. { case KEY_DOWN:
  5212. /* Go to next field */
  5213. form_driver(my_form, REQ_NEXT_FIELD);
  5214. /* Go to the end of the present buffer */
  5215. /* Leaves nicely at the last character */
  5216. form_driver(my_form, REQ_END_LINE);
  5217. break;
  5218. case KEY_UP:
  5219. /* Go to previous field */
  5220. form_driver(my_form, REQ_PREV_FIELD);
  5221. form_driver(my_form, REQ_END_LINE);
  5222. break;
  5223. default:
  5224. /* If this is a normal character, it gets */
  5225. /* Printed */
  5226. form_driver(my_form, ch);
  5227. break;
  5228. }
  5229. }
  5230. /* Un post form and free the memory */
  5231. unpost_form(my_form);
  5232. free_form(my_form);
  5233. free_field(field[0]);
  5234. free_field(field[1]);
  5235. endwin();
  5236. return 0;
  5237. }
  5238. void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color)
  5239. { int length, x, y;
  5240. float temp;
  5241. if(win == NULL)
  5242. win = stdscr;
  5243. getyx(win, y, x);
  5244. if(startx != 0)
  5245. x = startx;
  5246. if(starty != 0)
  5247. y = starty;
  5248. if(width == 0)
  5249. width = 80;
  5250. length = strlen(string);
  5251. temp = (width - length)/ 2;
  5252. x = startx + (int)temp;
  5253. wattron(win, color);
  5254. mvwprintw(win, y, x, "%s", string);
  5255. wattroff(win, color);
  5256. refresh();
  5257. }</span></font>
  5258. </pre></td>
  5259. </tr>
  5260. </table>
  5261. </div>
  5262. </div>
  5263. <div class="SECT2">
  5264. <hr>
  5265. <h3 class="SECT2"><a name="FILEDVALIDATE" id="FILEDVALIDATE">18.5.
  5266. Field Validation</a></h3>
  5267. <p>By default, a field will accept any data input by the user. It
  5268. is possible to attach validation to the field. Then any attempt by
  5269. the user to leave the field, while it contains data that doesn't
  5270. match the validation type will fail. Some validation types also
  5271. have a character-validity check for each time a character is
  5272. entered in the field.</p>
  5273. <p>Validation can be attached to a field with the following
  5274. function.</p>
  5275. <table border="0" bgcolor="#E0E0E0" width="100%">
  5276. <tr>
  5277. <td>
  5278. <pre class="PROGRAMLISTING">
  5279. <font color=
  5280. "#000000">int set_field_type(FIELD *field, /* field to alter */
  5281. FIELDTYPE *ftype, /* type to associate */
  5282. ...); /* additional arguments*/</font>
  5283. </pre></td>
  5284. </tr>
  5285. </table>
  5286. Once set, the validation type for a field can be queried with
  5287. <table border="0" bgcolor="#E0E0E0" width="100%">
  5288. <tr>
  5289. <td>
  5290. <pre class="PROGRAMLISTING">
  5291. <font color=
  5292. "#000000">FIELDTYPE *field_type(FIELD *field); /* field to query */</font>
  5293. </pre></td>
  5294. </tr>
  5295. </table>
  5296. <p>The form driver validates the data in a field only when data is
  5297. entered by the end-user. Validation does not occur when</p>
  5298. <ul>
  5299. <li>
  5300. <p>the application program changes the field value by calling
  5301. set_field_buffer.</p>
  5302. </li>
  5303. <li>
  5304. <p>linked field values are changed indirectly -- by changing the
  5305. field to which they are linked</p>
  5306. </li>
  5307. </ul>
  5308. <p>The following are the pre-defined validation types. You can also
  5309. specify custom validation, though it's a bit tricky and
  5310. cumbersome.</p>
  5311. <h1 class="BRIDGEHEAD"><a name="AEN1069" id=
  5312. "AEN1069"></a>TYPE_ALPHA</h1>
  5313. <p>This field type accepts alphabetic data; no blanks, no digits,
  5314. no special characters (this is checked at character-entry time). It
  5315. is set up with:</p>
  5316. <table border="0" bgcolor="#E0E0E0" width="100%">
  5317. <tr>
  5318. <td>
  5319. <pre class="PROGRAMLISTING">
  5320. <font color=
  5321. "#000000">int set_field_type(FIELD *field, /* field to alter */
  5322. TYPE_ALPHA, /* type to associate */
  5323. int width); /* maximum width of field */</font>
  5324. </pre></td>
  5325. </tr>
  5326. </table>
  5327. <p>The width argument sets a minimum width of data. The user has to
  5328. enter at-least width number of characters before he can leave the
  5329. field. Typically you'll want to set this to the field width; if
  5330. it's greater than the field width, the validation check will always
  5331. fail. A minimum width of zero makes field completion optional.</p>
  5332. <h1 class="BRIDGEHEAD"><a name="AEN1073" id=
  5333. "AEN1073"></a>TYPE_ALNUM</h1>
  5334. <p>This field type accepts alphabetic data and digits; no blanks,
  5335. no special characters (this is checked at character-entry time). It
  5336. is set up with:</p>
  5337. <table border="0" bgcolor="#E0E0E0" width="100%">
  5338. <tr>
  5339. <td>
  5340. <pre class="PROGRAMLISTING">
  5341. <font color=
  5342. "#000000">int set_field_type(FIELD *field, /* field to alter */
  5343. TYPE_ALNUM, /* type to associate */
  5344. int width); /* maximum width of field */</font>
  5345. </pre></td>
  5346. </tr>
  5347. </table>
  5348. <p>The width argument sets a minimum width of data. As with
  5349. TYPE_ALPHA, typically you'll want to set this to the field width;
  5350. if it's greater than the field width, the validation check will
  5351. always fail. A minimum width of zero makes field completion
  5352. optional.</p>
  5353. <h1 class="BRIDGEHEAD"><a name="AEN1077" id=
  5354. "AEN1077"></a>TYPE_ENUM</h1>
  5355. <p>This type allows you to restrict a field's values to be among a
  5356. specified set of string values (for example, the two-letter postal
  5357. codes for U.S. states). It is set up with:</p>
  5358. <table border="0" bgcolor="#E0E0E0" width="100%">
  5359. <tr>
  5360. <td>
  5361. <pre class="PROGRAMLISTING">
  5362. <font color=
  5363. "#000000">int set_field_type(FIELD *field, /* field to alter */
  5364. TYPE_ENUM, /* type to associate */
  5365. char **valuelist; /* list of possible values */
  5366. int checkcase; /* case-sensitive? */
  5367. int checkunique); /* must specify uniquely? */</font>
  5368. </pre></td>
  5369. </tr>
  5370. </table>
  5371. <p>The valuelist parameter must point at a NULL-terminated list of
  5372. valid strings. The checkcase argument, if true, makes comparison
  5373. with the string case-sensitive.</p>
  5374. <p>When the user exits a TYPE_ENUM field, the validation procedure
  5375. tries to complete the data in the buffer to a valid entry. If a
  5376. complete choice string has been entered, it is of course valid. But
  5377. it is also possible to enter a prefix of a valid string and have it
  5378. completed for you.</p>
  5379. <p>By default, if you enter such a prefix and it matches more than
  5380. one value in the string list, the prefix will be completed to the
  5381. first matching value. But the checkunique argument, if true,
  5382. requires prefix matches to be unique in order to be valid.</p>
  5383. <p>The REQ_NEXT_CHOICE and REQ_PREV_CHOICE input requests can be
  5384. particularly useful with these fields.</p>
  5385. <h1 class="BRIDGEHEAD"><a name="AEN1084" id=
  5386. "AEN1084"></a>TYPE_INTEGER</h1>
  5387. <p>This field type accepts an integer. It is set up as follows:</p>
  5388. <table border="0" bgcolor="#E0E0E0" width="100%">
  5389. <tr>
  5390. <td>
  5391. <pre class="PROGRAMLISTING">
  5392. <font color=
  5393. "#000000">int set_field_type(FIELD *field, /* field to alter */
  5394. TYPE_INTEGER, /* type to associate */
  5395. int padding, /* # places to zero-pad to */
  5396. int vmin, int vmax); /* valid range */</font>
  5397. </pre></td>
  5398. </tr>
  5399. </table>
  5400. <p>Valid characters consist of an optional leading minus and
  5401. digits. The range check is performed on exit. If the range maximum
  5402. is less than or equal to the minimum, the range is ignored.</p>
  5403. <p>If the value passes its range check, it is padded with as many
  5404. leading zero digits as necessary to meet the padding argument.</p>
  5405. <p>A TYPE_INTEGER value buffer can conveniently be interpreted with
  5406. the C library function atoi(3).</p>
  5407. <h1 class="BRIDGEHEAD"><a name="AEN1090" id=
  5408. "AEN1090"></a>TYPE_NUMERIC</h1>
  5409. <p>This field type accepts a decimal number. It is set up as
  5410. follows:</p>
  5411. <table border="0" bgcolor="#E0E0E0" width="100%">
  5412. <tr>
  5413. <td>
  5414. <pre class="PROGRAMLISTING">
  5415. <font color=
  5416. "#000000">int set_field_type(FIELD *field, /* field to alter */
  5417. TYPE_NUMERIC, /* type to associate */
  5418. int padding, /* # places of precision */
  5419. int vmin, int vmax); /* valid range */</font>
  5420. </pre></td>
  5421. </tr>
  5422. </table>
  5423. <p>Valid characters consist of an optional leading minus and
  5424. digits. possibly including a decimal point. The range check is
  5425. performed on exit. If the range maximum is less than or equal to
  5426. the minimum, the range is ignored.</p>
  5427. <p>If the value passes its range check, it is padded with as many
  5428. trailing zero digits as necessary to meet the padding argument.</p>
  5429. <p>A TYPE_NUMERIC value buffer can conveniently be interpreted with
  5430. the C library function atof(3).</p>
  5431. <h1 class="BRIDGEHEAD"><a name="AEN1096" id=
  5432. "AEN1096"></a>TYPE_REGEXP</h1>
  5433. <p>This field type accepts data matching a regular expression. It
  5434. is set up as follows:</p>
  5435. <table border="0" bgcolor="#E0E0E0" width="100%">
  5436. <tr>
  5437. <td>
  5438. <pre class="PROGRAMLISTING">
  5439. <font color=
  5440. "#000000">int set_field_type(FIELD *field, /* field to alter */
  5441. TYPE_REGEXP, /* type to associate */
  5442. char *regexp); /* expression to match */</font>
  5443. </pre></td>
  5444. </tr>
  5445. </table>
  5446. <p>The syntax for regular expressions is that of regcomp(3). The
  5447. check for regular-expression match is performed on exit.</p>
  5448. </div>
  5449. <div class="SECT2">
  5450. <hr>
  5451. <h3 class="SECT2"><a name="FORMDRIVER" id="FORMDRIVER">18.6. Form
  5452. Driver: The work horse of the forms system</a></h3>
  5453. <p>As in the menu system, form_driver() plays a very important role
  5454. in forms system. All types of requests to forms system should be
  5455. funneled through form_driver().</p>
  5456. <table border="0" bgcolor="#E0E0E0" width="100%">
  5457. <tr>
  5458. <td>
  5459. <pre class="PROGRAMLISTING">
  5460. <font color=
  5461. "#000000">int form_driver(FORM *form, /* form on which to operate */
  5462. int request) /* form request code */</font>
  5463. </pre></td>
  5464. </tr>
  5465. </table>
  5466. <p>As you have seen some of the examples above, you have to be in a
  5467. loop looking for user input and then decide whether it's a field
  5468. data or a form request. The form requests are then passed to
  5469. form_driver() to do the work.</p>
  5470. <p>The requests roughly can be divided into following categories.
  5471. Different requests and their usage is explained below:</p>
  5472. <div class="SECT3">
  5473. <hr>
  5474. <h4 class="SECT3"><a name="PAGENAVREQ" id="PAGENAVREQ">18.6.1. Page
  5475. Navigation Requests</a></h4>
  5476. <p>These requests cause page-level moves through the form,
  5477. triggering display of a new form screen. A form can be made of
  5478. multiple pages. If you have a big form with lot of fields and
  5479. logical sections, then you can divide the form into pages. The
  5480. function set_new_page() to set a new page at the field
  5481. specified.</p>
  5482. <table border="0" bgcolor="#E0E0E0" width="100%">
  5483. <tr>
  5484. <td>
  5485. <pre class="PROGRAMLISTING">
  5486. <font color=
  5487. "#000000">int set_new_page(FIELD *field,/* Field at which page break to be set or unset */
  5488. bool new_page_flag); /* should be TRUE to put a break */</font>
  5489. </pre></td>
  5490. </tr>
  5491. </table>
  5492. <p>The following requests allow you to move to different pages</p>
  5493. <ul>
  5494. <li>
  5495. <p><em>REQ_NEXT_PAGE</em> Move to the next form page.</p>
  5496. </li>
  5497. <li>
  5498. <p><em>REQ_PREV_PAGE</em> Move to the previous form page.</p>
  5499. </li>
  5500. <li>
  5501. <p><em>REQ_FIRST_PAGE</em> Move to the first form page.</p>
  5502. </li>
  5503. <li>
  5504. <p><em>REQ_LAST_PAGE</em> Move to the last form page.</p>
  5505. </li>
  5506. </ul>
  5507. <p>These requests treat the list as cyclic; that is, REQ_NEXT_PAGE
  5508. from the last page goes to the first, and REQ_PREV_PAGE from the
  5509. first page goes to the last.</p>
  5510. </div>
  5511. <div class="SECT3">
  5512. <hr>
  5513. <h4 class="SECT3"><a name="INTERFIELDNAVREQ" id=
  5514. "INTERFIELDNAVREQ">18.6.2. Inter-Field Navigation Requests</a></h4>
  5515. <p>These requests handle navigation between fields on the same
  5516. page.</p>
  5517. <ul>
  5518. <li>
  5519. <p><em>REQ_NEXT_FIELD</em> Move to next field.</p>
  5520. </li>
  5521. <li>
  5522. <p><em>REQ_PREV_FIELD</em> Move to previous field.</p>
  5523. </li>
  5524. <li>
  5525. <p><em>REQ_FIRST_FIELD</em> Move to the first field.</p>
  5526. </li>
  5527. <li>
  5528. <p><em>REQ_LAST_FIELD</em> Move to the last field.</p>
  5529. </li>
  5530. <li>
  5531. <p><em>REQ_SNEXT_FIELD</em> Move to sorted next field.</p>
  5532. </li>
  5533. <li>
  5534. <p><em>REQ_SPREV_FIELD</em> Move to sorted previous field.</p>
  5535. </li>
  5536. <li>
  5537. <p><em>REQ_SFIRST_FIELD</em> Move to the sorted first field.</p>
  5538. </li>
  5539. <li>
  5540. <p><em>REQ_SLAST_FIELD</em> Move to the sorted last field.</p>
  5541. </li>
  5542. <li>
  5543. <p><em>REQ_LEFT_FIELD</em> Move left to field.</p>
  5544. </li>
  5545. <li>
  5546. <p><em>REQ_RIGHT_FIELD</em> Move right to field.</p>
  5547. </li>
  5548. <li>
  5549. <p><em>REQ_UP_FIELD</em> Move up to field.</p>
  5550. </li>
  5551. <li>
  5552. <p><em>REQ_DOWN_FIELD</em> Move down to field.</p>
  5553. </li>
  5554. </ul>
  5555. <p>These requests treat the list of fields on a page as cyclic;
  5556. that is, REQ_NEXT_FIELD from the last field goes to the first, and
  5557. REQ_PREV_FIELD from the first field goes to the last. The order of
  5558. the fields for these (and the REQ_FIRST_FIELD and REQ_LAST_FIELD
  5559. requests) is simply the order of the field pointers in the form
  5560. array (as set up by new_form() or set_form_fields()</p>
  5561. <p>It is also possible to traverse the fields as if they had been
  5562. sorted in screen-position order, so the sequence goes left-to-right
  5563. and top-to-bottom. To do this, use the second group of four
  5564. sorted-movement requests.</p>
  5565. <p>Finally, it is possible to move between fields using visual
  5566. directions up, down, right, and left. To accomplish this, use the
  5567. third group of four requests. Note, however, that the position of a
  5568. form for purposes of these requests is its upper-left corner.</p>
  5569. <p>For example, suppose you have a multi-line field B, and two
  5570. single-line fields A and C on the same line with B, with A to the
  5571. left of B and C to the right of B. A REQ_MOVE_RIGHT from A will go
  5572. to B only if A, B, and C all share the same first line; otherwise
  5573. it will skip over B to C.</p>
  5574. </div>
  5575. <div class="SECT3">
  5576. <hr>
  5577. <h4 class="SECT3"><a name="INTRAFIELDNAVREQ" id=
  5578. "INTRAFIELDNAVREQ">18.6.3. Intra-Field Navigation Requests</a></h4>
  5579. <p>These requests drive movement of the edit cursor within the
  5580. currently selected field.</p>
  5581. <ul>
  5582. <li>
  5583. <p><em>REQ_NEXT_CHAR</em> Move to next character.</p>
  5584. </li>
  5585. <li>
  5586. <p><em>REQ_PREV_CHAR</em> Move to previous character.</p>
  5587. </li>
  5588. <li>
  5589. <p><em>REQ_NEXT_LINE</em> Move to next line.</p>
  5590. </li>
  5591. <li>
  5592. <p><em>REQ_PREV_LINE</em> Move to previous line.</p>
  5593. </li>
  5594. <li>
  5595. <p><em>REQ_NEXT_WORD</em> Move to next word.</p>
  5596. </li>
  5597. <li>
  5598. <p><em>REQ_PREV_WORD</em> Move to previous word.</p>
  5599. </li>
  5600. <li>
  5601. <p><em>REQ_BEG_FIELD</em> Move to beginning of field.</p>
  5602. </li>
  5603. <li>
  5604. <p><em>REQ_END_FIELD</em> Move to end of field.</p>
  5605. </li>
  5606. <li>
  5607. <p><em>REQ_BEG_LINE</em> Move to beginning of line.</p>
  5608. </li>
  5609. <li>
  5610. <p><em>REQ_END_LINE</em> Move to end of line.</p>
  5611. </li>
  5612. <li>
  5613. <p><em>REQ_LEFT_CHAR</em> Move left in field.</p>
  5614. </li>
  5615. <li>
  5616. <p><em>REQ_RIGHT_CHAR</em> Move right in field.</p>
  5617. </li>
  5618. <li>
  5619. <p><em>REQ_UP_CHAR</em> Move up in field.</p>
  5620. </li>
  5621. <li>
  5622. <p><em>REQ_DOWN_CHAR</em> Move down in field.</p>
  5623. </li>
  5624. </ul>
  5625. <p>Each word is separated from the previous and next characters by
  5626. whitespace. The commands to move to beginning and end of line or
  5627. field look for the first or last non-pad character in their
  5628. ranges.</p>
  5629. </div>
  5630. <div class="SECT3">
  5631. <hr>
  5632. <h4 class="SECT3"><a name="SCROLLREQ" id="SCROLLREQ">18.6.4.
  5633. Scrolling Requests</a></h4>
  5634. <p>Fields that are dynamic and have grown and fields explicitly
  5635. created with offscreen rows are scrollable. One-line fields scroll
  5636. horizontally; multi-line fields scroll vertically. Most scrolling
  5637. is triggered by editing and intra-field movement (the library
  5638. scrolls the field to keep the cursor visible). It is possible to
  5639. explicitly request scrolling with the following requests:</p>
  5640. <ul>
  5641. <li>
  5642. <p><em>REQ_SCR_FLINE</em> Scroll vertically forward a line.</p>
  5643. </li>
  5644. <li>
  5645. <p><em>REQ_SCR_BLINE</em> Scroll vertically backward a line.</p>
  5646. </li>
  5647. <li>
  5648. <p><em>REQ_SCR_FPAGE</em> Scroll vertically forward a page.</p>
  5649. </li>
  5650. <li>
  5651. <p><em>REQ_SCR_BPAGE</em> Scroll vertically backward a page.</p>
  5652. </li>
  5653. <li>
  5654. <p><em>REQ_SCR_FHPAGE</em> Scroll vertically forward half a
  5655. page.</p>
  5656. </li>
  5657. <li>
  5658. <p><em>REQ_SCR_BHPAGE</em> Scroll vertically backward half a
  5659. page.</p>
  5660. </li>
  5661. <li>
  5662. <p><em>REQ_SCR_FCHAR</em> Scroll horizontally forward a
  5663. character.</p>
  5664. </li>
  5665. <li>
  5666. <p><em>REQ_SCR_BCHAR</em> Scroll horizontally backward a
  5667. character.</p>
  5668. </li>
  5669. <li>
  5670. <p><em>REQ_SCR_HFLINE</em> Scroll horizontally one field width
  5671. forward.</p>
  5672. </li>
  5673. <li>
  5674. <p><em>REQ_SCR_HBLINE</em> Scroll horizontally one field width
  5675. backward.</p>
  5676. </li>
  5677. <li>
  5678. <p><em>REQ_SCR_HFHALF</em> Scroll horizontally one half field width
  5679. forward.</p>
  5680. </li>
  5681. <li>
  5682. <p><em>REQ_SCR_HBHALF</em> Scroll horizontally one half field width
  5683. backward.</p>
  5684. </li>
  5685. </ul>
  5686. <p>For scrolling purposes, a page of a field is the height of its
  5687. visible part.</p>
  5688. </div>
  5689. <div class="SECT3">
  5690. <hr>
  5691. <h4 class="SECT3"><a name="EDITREQ" id="EDITREQ">18.6.5. Editing
  5692. Requests</a></h4>
  5693. <p>When you pass the forms driver an ASCII character, it is treated
  5694. as a request to add the character to the field's data buffer.
  5695. Whether this is an insertion or a replacement depends on the
  5696. field's edit mode (insertion is the default.</p>
  5697. <p>The following requests support editing the field and changing
  5698. the edit mode:</p>
  5699. <ul>
  5700. <li>
  5701. <p><em>REQ_INS_MODE</em> Set insertion mode.</p>
  5702. </li>
  5703. <li>
  5704. <p><em>REQ_OVL_MODE</em> Set overlay mode.</p>
  5705. </li>
  5706. <li>
  5707. <p><em>REQ_NEW_LINE</em> New line request (see below for
  5708. explanation).</p>
  5709. </li>
  5710. <li>
  5711. <p><em>REQ_INS_CHAR</em> Insert space at character location.</p>
  5712. </li>
  5713. <li>
  5714. <p><em>REQ_INS_LINE</em> Insert blank line at character
  5715. location.</p>
  5716. </li>
  5717. <li>
  5718. <p><em>REQ_DEL_CHAR</em> Delete character at cursor.</p>
  5719. </li>
  5720. <li>
  5721. <p><em>REQ_DEL_PREV</em> Delete previous word at cursor.</p>
  5722. </li>
  5723. <li>
  5724. <p><em>REQ_DEL_LINE</em> Delete line at cursor.</p>
  5725. </li>
  5726. <li>
  5727. <p><em>REQ_DEL_WORD</em> Delete word at cursor.</p>
  5728. </li>
  5729. <li>
  5730. <p><em>REQ_CLR_EOL</em> Clear to end of line.</p>
  5731. </li>
  5732. <li>
  5733. <p><em>REQ_CLR_EOF</em> Clear to end of field.</p>
  5734. </li>
  5735. <li>
  5736. <p><em>REQ_CLR_FIELD</em> Clear entire field.</p>
  5737. </li>
  5738. </ul>
  5739. <p>The behavior of the REQ_NEW_LINE and REQ_DEL_PREV requests is
  5740. complicated and partly controlled by a pair of forms options. The
  5741. special cases are triggered when the cursor is at the beginning of
  5742. a field, or on the last line of the field.</p>
  5743. <p>First, we consider REQ_NEW_LINE:</p>
  5744. <p>The normal behavior of REQ_NEW_LINE in insert mode is to break
  5745. the current line at the position of the edit cursor, inserting the
  5746. portion of the current line after the cursor as a new line
  5747. following the current and moving the cursor to the beginning of
  5748. that new line (you may think of this as inserting a newline in the
  5749. field buffer).</p>
  5750. <p>The normal behavior of REQ_NEW_LINE in overlay mode is to clear
  5751. the current line from the position of the edit cursor to end of
  5752. line. The cursor is then moved to the beginning of the next
  5753. line.</p>
  5754. <p>However, REQ_NEW_LINE at the beginning of a field, or on the
  5755. last line of a field, instead does a REQ_NEXT_FIELD. O_NL_OVERLOAD
  5756. option is off, this special action is disabled.</p>
  5757. <p>Now, let us consider REQ_DEL_PREV:</p>
  5758. <p>The normal behavior of REQ_DEL_PREV is to delete the previous
  5759. character. If insert mode is on, and the cursor is at the start of
  5760. a line, and the text on that line will fit on the previous one, it
  5761. instead appends the contents of the current line to the previous
  5762. one and deletes the current line (you may think of this as deleting
  5763. a newline from the field buffer).</p>
  5764. <p>However, REQ_DEL_PREV at the beginning of a field is instead
  5765. treated as a REQ_PREV_FIELD.</p>
  5766. <p>If the O_BS_OVERLOAD option is off, this special action is
  5767. disabled and the forms driver just returns E_REQUEST_DENIED.</p>
  5768. </div>
  5769. <div class="SECT3">
  5770. <hr>
  5771. <h4 class="SECT3"><a name="ORDERREQ" id="ORDERREQ">18.6.6. Order
  5772. Requests</a></h4>
  5773. <p>If the type of your field is ordered, and has associated
  5774. functions for getting the next and previous values of the type from
  5775. a given value, there are requests that can fetch that value into
  5776. the field buffer:</p>
  5777. <ul>
  5778. <li>
  5779. <p><em>REQ_NEXT_CHOICE</em> Place the successor value of the
  5780. current value in the buffer.</p>
  5781. </li>
  5782. <li>
  5783. <p><em>REQ_PREV_CHOICE</em> Place the predecessor value of the
  5784. current value in the buffer.</p>
  5785. </li>
  5786. </ul>
  5787. <p>Of the built-in field types, only TYPE_ENUM has built-in
  5788. successor and predecessor functions. When you define a field type
  5789. of your own (see Custom Validation Types), you can associate our
  5790. own ordering functions.</p>
  5791. </div>
  5792. <div class="SECT3">
  5793. <hr>
  5794. <h4 class="SECT3"><a name="APPLICCOMMANDS" id=
  5795. "APPLICCOMMANDS">18.6.7. Application Commands</a></h4>
  5796. <p>Form requests are represented as integers above the curses value
  5797. greater than KEY_MAX and less than or equal to the constant
  5798. MAX_COMMAND. A value within this range gets ignored by
  5799. form_driver(). So this can be used for any purpose by the
  5800. application. It can be treated as an application specific action
  5801. and take corresponding action.</p>
  5802. </div>
  5803. </div>
  5804. </div>
  5805. <div class="SECT1">
  5806. <hr>
  5807. <h2 class="SECT1"><a name="TOOLS" id="TOOLS">19. Tools and Widget
  5808. Libraries</a></h2>
  5809. <p>Now that you have seen the capabilities of ncurses and its
  5810. sister libraries, you are rolling your sleeves up and gearing for a
  5811. project that heavily manipulates screen. But wait.. It can be
  5812. pretty difficult to write and maintain complex GUI widgets in plain
  5813. ncurses or even with the additional libraries. There are some
  5814. ready-to-use tools and widget libraries that can be used instead of
  5815. writing your own widgets. You can use some of them, get ideas from
  5816. the code, or even extend them.</p>
  5817. <div class="SECT2">
  5818. <hr>
  5819. <h3 class="SECT2"><a name="CDK" id="CDK">19.1. CDK (Curses
  5820. Development Kit)</a></h3>
  5821. <p>In the author's words</p>
  5822. <p><em>CDK stands for 'Curses Development Kit' and it currently
  5823. contains 21 ready to use widgets which facilitate the speedy
  5824. development of full screen curses programs.</em></p>
  5825. <p>The kit provides some useful widgets, which can be used in your
  5826. programs directly. It's pretty well written and the documentation
  5827. is very good. The examples in the examples directory can be a good
  5828. place to start for beginners. The CDK can be downloaded from
  5829. <a href="http://invisible-island.net/cdk/" target=
  5830. "_top">http://invisible-island.net/cdk/</a> . Follow the
  5831. instructions in README file to install it.</p>
  5832. <div class="SECT3">
  5833. <hr>
  5834. <h4 class="SECT3"><a name="WIDGETLIST" id="WIDGETLIST">19.1.1.
  5835. Widget List</a></h4>
  5836. <p>The following is the list of widgets provided with cdk and their
  5837. description.</p>
  5838. <table border="0" bgcolor="#E0E0E0" width="100%">
  5839. <tr>
  5840. <td>
  5841. <pre class="PROGRAMLISTING">
  5842. <font color="#000000">Widget Type Quick Description
  5843. ===========================================================================
  5844. Alphalist Allows a user to select from a list of words, with
  5845. the ability to narrow the search list by typing in a
  5846. few characters of the desired word.
  5847. Buttonbox This creates a multiple button widget.
  5848. Calendar Creates a little simple calendar widget.
  5849. Dialog Prompts the user with a message, and the user
  5850. can pick an answer from the buttons provided.
  5851. Entry Allows the user to enter various types of information.
  5852. File Selector A file selector built from Cdk base widgets. This
  5853. example shows how to create more complicated widgets
  5854. using the Cdk widget library.
  5855. Graph Draws a graph.
  5856. Histogram Draws a histogram.
  5857. Item List Creates a pop up field which allows the user to select
  5858. one of several choices in a small field. Very useful
  5859. for things like days of the week or month names.
  5860. Label Displays messages in a pop up box, or the label can be
  5861. considered part of the screen.
  5862. Marquee Displays a message in a scrolling marquee.
  5863. Matrix Creates a complex matrix with lots of options.
  5864. Menu Creates a pull-down menu interface.
  5865. Multiple Line Entry A multiple line entry field. Very useful
  5866. for long fields. (like a description
  5867. field)
  5868. Radio List Creates a radio button list.
  5869. Scale Creates a numeric scale. Used for allowing a user to
  5870. pick a numeric value and restrict them to a range of
  5871. values.
  5872. Scrolling List Creates a scrolling list/menu list.
  5873. Scrolling Window Creates a scrolling log file viewer. Can add
  5874. information into the window while its running.
  5875. A good widget for displaying the progress of
  5876. something. (akin to a console window)
  5877. Selection List Creates a multiple option selection list.
  5878. Slider Akin to the scale widget, this widget provides a
  5879. visual slide bar to represent the numeric value.
  5880. Template Creates a entry field with character sensitive
  5881. positions. Used for pre-formatted fields like
  5882. dates and phone numbers.
  5883. Viewer This is a file/information viewer. Very useful
  5884. when you need to display loads of information.
  5885. ===========================================================================</font>
  5886. </pre></td>
  5887. </tr>
  5888. </table>
  5889. <p>A few of the widgets are modified by Thomas Dickey in recent
  5890. versions.</p>
  5891. </div>
  5892. <div class="SECT3">
  5893. <hr>
  5894. <h4 class="SECT3"><a name="CDKATTRACT" id="CDKATTRACT">19.1.2. Some
  5895. Attractive Features</a></h4>
  5896. <p>Apart from making our life easier with readily usable widgets,
  5897. cdk solves one frustrating problem with printing multi colored
  5898. strings, justified strings elegantly. Special formatting tags can
  5899. be embedded in the strings which are passed to CDK functions. For
  5900. Example</p>
  5901. <p>If the string</p>
  5902. <table border="0" bgcolor="#E0E0E0" width="100%">
  5903. <tr>
  5904. <td>
  5905. <pre class="PROGRAMLISTING">
  5906. <font color=
  5907. "#000000">"&lt;/B/1&gt;This line should have a yellow foreground and a blue
  5908. background.&lt;!1&gt;"</font>
  5909. </pre></td>
  5910. </tr>
  5911. </table>
  5912. <p>given as a parameter to newCDKLabel(), it prints the line with
  5913. yellow foreground and blue background. There are other tags
  5914. available for justifying string, embedding special drawing
  5915. characters etc.. Please refer to the man page cdk_display(3X) for
  5916. details. The man page explains the usage with nice examples.</p>
  5917. </div>
  5918. <div class="SECT3">
  5919. <hr>
  5920. <h4 class="SECT3"><a name="CDKCONCLUSION" id=
  5921. "CDKCONCLUSION">19.1.3. Conclusion</a></h4>
  5922. <p>All in all, CDK is a well-written package of widgets, which if
  5923. used properly can form a strong frame work for developing complex
  5924. GUI.</p>
  5925. </div>
  5926. </div>
  5927. <div class="SECT2">
  5928. <hr>
  5929. <h3 class="SECT2"><a name="DIALOG" id="DIALOG">19.2. The
  5930. dialog</a></h3>
  5931. <p>Long long ago, in September 1994, when few people knew linux,
  5932. Jeff Tranter wrote an <a href=
  5933. "http://www2.linuxjournal.com/lj-issues/issue5/2807.html" target=
  5934. "_top">article</a> on dialog in Linux Journal. He starts the
  5935. article with these words..</p>
  5936. <p><em>Linux is based on the Unix operating system, but also
  5937. features a number of unique and useful kernel features and
  5938. application programs that often go beyond what is available under
  5939. Unix. One little-known gem is "dialog", a utility for creating
  5940. professional-looking dialog boxes from within shell scripts. This
  5941. article presents a tutorial introduction to the dialog utility, and
  5942. shows examples of how and where it can be used</em></p>
  5943. <p>As he explains, dialog is a real gem in making
  5944. professional-looking dialog boxes with ease. It creates a variety
  5945. of dialog boxes, menus, check lists etc.. It is usually installed
  5946. by default. If not, you can download it from <a href=
  5947. "http://invisible-island.net/dialog/" target="_top">Thomas
  5948. Dickey</a>'s site.</p>
  5949. <p>The above-mentioned article gives a very good overview of its
  5950. uses and capabilites. The man page has more details. It can be used
  5951. in variety of situations. One good example is building of linux
  5952. kernel in text mode. Linux kernel uses a modified version of dialog
  5953. tailored for its needs.</p>
  5954. <p>dialog was initially designed to be used with shell scripts. If
  5955. you want to use its functionality in a c program, then you can use
  5956. libdialog. The documentation regarding this is sparse. Definitive
  5957. reference is the dialog.h header file which comes with the library.
  5958. You may need to hack here and there to get the required output. The
  5959. source is easily customizable. I have used it on a number of
  5960. occasions by modifying the code.</p>
  5961. </div>
  5962. <div class="SECT2">
  5963. <hr>
  5964. <h3 class="SECT2"><a name="PERLCURSES" id="PERLCURSES">19.3. Perl
  5965. Curses Modules CURSES::FORM and CURSES::WIDGETS</a></h3>
  5966. <p>The perl module Curses, Curses::Form and Curses::Widgets give
  5967. access to curses from perl. If you have curses and basic perl is
  5968. installed, you can get these modules from <a href=
  5969. "http://www.cpan.org/modules/01modules.index.html" target=
  5970. "_top">CPAN All Modules page</a>. Get the three zipped modules in
  5971. the Curses category. Once installed you can use these modules from
  5972. perl scripts like any other module. For more information on perl
  5973. modules see perlmod man page. The above modules come with good
  5974. documentation and they have some demo scripts to test the
  5975. functionality. Though the widgets provided are very rudimentary,
  5976. these modules provide good access to curses library from perl.</p>
  5977. <p>Some of my code examples are converted to perl by Anuradha
  5978. Ratnaweera and they are available in the <var class=
  5979. "LITERAL">perl</var> directory.</p>
  5980. <p>For more information see man pages Curses(3) , Curses::Form(3)
  5981. and Curses::Widgets(3). These pages are installed only when the
  5982. above modules are acquired and installed.</p>
  5983. </div>
  5984. </div>
  5985. <div class="SECT1">
  5986. <hr>
  5987. <h2 class="SECT1"><a name="JUSTFORFUN" id="JUSTFORFUN">20. Just For
  5988. Fun !!!</a></h2>
  5989. <p>This section contains few programs written by me just for fun.
  5990. They don't signify a better programming practice or the best way of
  5991. using ncurses. They are provided here so as to allow beginners to
  5992. get ideas and add more programs to this section. If you have
  5993. written a couple of nice, simple programs in curses and want them
  5994. to included here, contact <a href="mailto:ppadala@gmail.com"
  5995. target="_top">me</a>.</p>
  5996. <div class="SECT2">
  5997. <hr>
  5998. <h3 class="SECT2"><a name="GAMEOFLIFE" id="GAMEOFLIFE">20.1. The
  5999. Game of Life</a></h3>
  6000. <p>Game of life is a wonder of math. In <a href=
  6001. "http://www.math.com/students/wonders/life/life.html" target=
  6002. "_top">Paul Callahan</a>'s words</p>
  6003. <table border="0" bgcolor="#E0E0E0" width="100%">
  6004. <tr>
  6005. <td>
  6006. <pre class="PROGRAMLISTING">
  6007. <font color=
  6008. "#000000"><em>The Game of Life (or simply Life) is not a game in the conventional sense. There
  6009. are no players, and no winning or losing. Once the "pieces" are placed in the
  6010. starting position, the rules determine everything that happens later.
  6011. Nevertheless, Life is full of surprises! In most cases, it is impossible to look
  6012. at a starting position (or pattern) and see what will happen in the future. The
  6013. only way to find out is to follow the rules of the game.</em></font>
  6014. </pre></td>
  6015. </tr>
  6016. </table>
  6017. <p>This program starts with a simple inverted U pattern and shows
  6018. how wonderful life works. There is a lot of room for improvement in
  6019. the program. You can let the user enter pattern of his choice or
  6020. even take input from a file. You can also change rules and play
  6021. with a lot of variations. Search on <a href="http://www.google.com"
  6022. target="_top">google</a> for interesting information on game of
  6023. life.</p>
  6024. <p><em>File Path: JustForFun/life.c</em></p>
  6025. </div>
  6026. <div class="SECT2">
  6027. <hr>
  6028. <h3 class="SECT2"><a name="MAGIC" id="MAGIC">20.2. Magic
  6029. Square</a></h3>
  6030. <p>Magic Square, another wonder of math, is very simple to
  6031. understand but very difficult to make. In a magic square sum of the
  6032. numbers in each row, each column is equal. Even diagnol sum can be
  6033. equal. There are many variations which have special properties.</p>
  6034. <p>This program creates a simple magic square of odd order.</p>
  6035. <p><em>File Path: JustForFun/magic.c</em></p>
  6036. </div>
  6037. <div class="SECT2">
  6038. <hr>
  6039. <h3 class="SECT2"><a name="HANOI" id="HANOI">20.3. Towers of
  6040. Hanoi</a></h3>
  6041. <p>The famous towers of hanoi solver. The aim of the game is to
  6042. move the disks on the first peg to last peg, using middle peg as a
  6043. temporary stay. The catch is not to place a larger disk over a
  6044. small disk at any time.</p>
  6045. <p><em>File Path: JustForFun/hanoi.c</em></p>
  6046. </div>
  6047. <div class="SECT2">
  6048. <hr>
  6049. <h3 class="SECT2"><a name="QUEENS" id="QUEENS">20.4. Queens
  6050. Puzzle</a></h3>
  6051. <p>The objective of the famous N-Queen puzzle is to put N queens on
  6052. a N X N chess board without attacking each other.</p>
  6053. <p>This program solves it with a simple backtracking technique.</p>
  6054. <p><em>File Path: JustForFun/queens.c</em></p>
  6055. </div>
  6056. <div class="SECT2">
  6057. <hr>
  6058. <h3 class="SECT2"><a name="SHUFFLE" id="SHUFFLE">20.5.
  6059. Shuffle</a></h3>
  6060. <p>A fun game, if you have time to kill.</p>
  6061. <p><em>File Path: JustForFun/shuffle.c</em></p>
  6062. </div>
  6063. <div class="SECT2">
  6064. <hr>
  6065. <h3 class="SECT2"><a name="TT" id="TT">20.6. Typing Tutor</a></h3>
  6066. <p>A simple typing tutor, I created more out of need than for ease
  6067. of use. If you know how to put your fingers correctly on the
  6068. keyboard, but lack practice, this can be helpful.</p>
  6069. <p><em>File Path: JustForFun/tt.c</em></p>
  6070. </div>
  6071. </div>
  6072. <div class="SECT1">
  6073. <hr>
  6074. <h2 class="SECT1"><a name="REF" id="REF">21. References</a></h2>
  6075. <ul>
  6076. <li>
  6077. <p>NCURSES man pages</p>
  6078. </li>
  6079. <li>
  6080. <p>NCURSES FAQ at <a href=
  6081. "http://invisible-island.net/ncurses/ncurses.faq.html" target=
  6082. "_top">http://invisible-island.net/ncurses/ncurses.faq.html</a></p>
  6083. </li>
  6084. <li>
  6085. <p>Writing programs with NCURSES by Eric Raymond and Zeyd M.
  6086. Ben-Halim at <a href=
  6087. "http://invisible-island.net/ncurses/ncurses-intro.html" target=
  6088. "_top">http://invisible-island.net/ncurses/ncurses-intro.html</a> -
  6089. somewhat obsolete. I was inspired by this document and the
  6090. structure of this HOWTO follows from the original document</p>
  6091. </li>
  6092. </ul>
  6093. </div>
  6094. </div>
  6095. </body>
  6096. </html>