1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
2<HTML
3><HEAD
4><TITLE
5> NCURSES Programming HOWTO </TITLE
6><META
7NAME="GENERATOR"
8CONTENT="Modular DocBook HTML Stylesheet Version 1.79"></HEAD
9><BODY
10CLASS="ARTICLE"
11BGCOLOR="#FFFFFF"
12TEXT="#000000"
13LINK="#0000FF"
14VLINK="#840084"
15ALINK="#0000FF"
16><DIV
17CLASS="ARTICLE"
18><DIV
19CLASS="TITLEPAGE"
20><H1
21CLASS="TITLE"
22><A
23NAME="AEN2"
24>NCURSES Programming HOWTO</A
25></H1
26><H3
27CLASS="AUTHOR"
28><A
29NAME="AEN4"
30> Pradeep   Padala </A
31></H3
32><DIV
33CLASS="AFFILIATION"
34><DIV
35CLASS="ADDRESS"
36><P
37CLASS="ADDRESS"
38><CODE
39CLASS="EMAIL"
40>&#60;<A
41HREF="mailto:ppadala@gmail.com"
42>ppadala@gmail.com</A
43>&#62;</CODE
44></P
45></DIV
46></DIV
47><P
48CLASS="PUBDATE"
49>v1.9, 2005-06-20<BR></P
50><DIV
51CLASS="REVHISTORY"
52><TABLE
53WIDTH="100%"
54BORDER="0"
55><TR
56><TH
57ALIGN="LEFT"
58VALIGN="TOP"
59COLSPAN="3"
60><B
61>Revision History</B
62></TH
63></TR
64><TR
65><TD
66ALIGN="LEFT"
67>Revision 1.9</TD
68><TD
69ALIGN="LEFT"
70>2005-06-20</TD
71><TD
72ALIGN="LEFT"
73>Revised by: ppadala</TD
74></TR
75><TR
76><TD
77ALIGN="LEFT"
78COLSPAN="3"
79>The license has been changed to the MIT-style license used
80        by NCURSES. Note that the programs are also re-licensed under this.</TD
81></TR
82><TR
83><TD
84ALIGN="LEFT"
85>Revision 1.8</TD
86><TD
87ALIGN="LEFT"
88>2005-06-17</TD
89><TD
90ALIGN="LEFT"
91>Revised by: ppadala</TD
92></TR
93><TR
94><TD
95ALIGN="LEFT"
96COLSPAN="3"
97>Lots of updates. Added references and perl examples.
98        Changes to examples. Many grammatical and stylistic changes to the
99        content. Changes to NCURSES history.</TD
100></TR
101><TR
102><TD
103ALIGN="LEFT"
104>Revision 1.7.1</TD
105><TD
106ALIGN="LEFT"
107>2002-06-25</TD
108><TD
109ALIGN="LEFT"
110>Revised by: ppadala</TD
111></TR
112><TR
113><TD
114ALIGN="LEFT"
115COLSPAN="3"
116>Added a README file for building and instructions
117        for building from source.</TD
118></TR
119><TR
120><TD
121ALIGN="LEFT"
122>Revision 1.7</TD
123><TD
124ALIGN="LEFT"
125>2002-06-25</TD
126><TD
127ALIGN="LEFT"
128>Revised by: ppadala</TD
129></TR
130><TR
131><TD
132ALIGN="LEFT"
133COLSPAN="3"
134>Added "Other formats" section and made a lot of fancy
135        changes to the programs. Inlining of programs is gone.</TD
136></TR
137><TR
138><TD
139ALIGN="LEFT"
140>Revision 1.6.1</TD
141><TD
142ALIGN="LEFT"
143>2002-02-24</TD
144><TD
145ALIGN="LEFT"
146>Revised by: ppadala</TD
147></TR
148><TR
149><TD
150ALIGN="LEFT"
151COLSPAN="3"
152>Removed the old Changelog section, cleaned the makefiles</TD
153></TR
154><TR
155><TD
156ALIGN="LEFT"
157>Revision 1.6</TD
158><TD
159ALIGN="LEFT"
160>2002-02-16</TD
161><TD
162ALIGN="LEFT"
163>Revised by: ppadala</TD
164></TR
165><TR
166><TD
167ALIGN="LEFT"
168COLSPAN="3"
169>Corrected a lot of spelling mistakes, added ACS variables
170        section</TD
171></TR
172><TR
173><TD
174ALIGN="LEFT"
175>Revision 1.5</TD
176><TD
177ALIGN="LEFT"
178>2002-01-05</TD
179><TD
180ALIGN="LEFT"
181>Revised by: ppadala</TD
182></TR
183><TR
184><TD
185ALIGN="LEFT"
186COLSPAN="3"
187>Changed structure to present proper TOC</TD
188></TR
189><TR
190><TD
191ALIGN="LEFT"
192>Revision 1.3.1</TD
193><TD
194ALIGN="LEFT"
195>2001-07-26</TD
196><TD
197ALIGN="LEFT"
198>Revised by: ppadala</TD
199></TR
200><TR
201><TD
202ALIGN="LEFT"
203COLSPAN="3"
204>Corrected maintainers paragraph, Corrected stable release number</TD
205></TR
206><TR
207><TD
208ALIGN="LEFT"
209>Revision 1.3</TD
210><TD
211ALIGN="LEFT"
212>2001-07-24</TD
213><TD
214ALIGN="LEFT"
215>Revised by: ppadala</TD
216></TR
217><TR
218><TD
219ALIGN="LEFT"
220COLSPAN="3"
221>Added copyright notices to main document (LDP license)
222        and programs (GPL), Corrected
223        printw_example.</TD
224></TR
225><TR
226><TD
227ALIGN="LEFT"
228>Revision 1.2</TD
229><TD
230ALIGN="LEFT"
231>2001-06-05</TD
232><TD
233ALIGN="LEFT"
234>Revised by: ppadala</TD
235></TR
236><TR
237><TD
238ALIGN="LEFT"
239COLSPAN="3"
240>Incorporated ravi's changes. Mainly to introduction, menu,
241        form, justforfun sections</TD
242></TR
243><TR
244><TD
245ALIGN="LEFT"
246>Revision 1.1</TD
247><TD
248ALIGN="LEFT"
249>2001-05-22</TD
250><TD
251ALIGN="LEFT"
252>Revised by: ppadala</TD
253></TR
254><TR
255><TD
256ALIGN="LEFT"
257COLSPAN="3"
258>Added "a word about window" section, Added scanw_example.</TD
259></TR
260></TABLE
261></DIV
262><DIV
263><DIV
264CLASS="ABSTRACT"
265><P
266></P
267><A
268NAME="AEN67"
269></A
270><P
271>    <SPAN
272CLASS="emphasis"
273><I
274CLASS="EMPHASIS"
275>This document is intended to be an "All in One" guide for programming with
276ncurses and its sister libraries. We graduate from a simple "Hello World"
277program to more complex form manipulation. No prior experience in ncurses is
278assumed. Send comments to <A
279HREF="mailto:ppadala@gmail.com"
280TARGET="_top"
281>this address</A
282>
283    </I
284></SPAN
285>
286    </P
287><P
288></P
289></DIV
290></DIV
291><HR></DIV
292><DIV
293CLASS="TOC"
294><DL
295><DT
296><B
297>Table of Contents</B
298></DT
299><DT
300>1. <A
301HREF="#INTRO"
302>Introduction</A
303></DT
304><DD
305><DL
306><DT
307>1.1. <A
308HREF="#WHATIS"
309>What is NCURSES?</A
310></DT
311><DT
312>1.2. <A
313HREF="#WHATCANWEDO"
314>What we can do with NCURSES</A
315></DT
316><DT
317>1.3. <A
318HREF="#WHERETOGETIT"
319>Where to get it</A
320></DT
321><DT
322>1.4. <A
323HREF="#PURPOSE"
324>Purpose/Scope of the document</A
325></DT
326><DT
327>1.5. <A
328HREF="#ABOUTPROGRAMS"
329>About the Programs</A
330></DT
331><DT
332>1.6. <A
333HREF="#OTHERFORMATS"
334>Other Formats of the document</A
335></DT
336><DD
337><DL
338><DT
339>1.6.1. <A
340HREF="#LISTFORMATS"
341>Readily available formats from tldp.org</A
342></DT
343><DT
344>1.6.2. <A
345HREF="#BUILDSOURCE"
346>Building from source</A
347></DT
348></DL
349></DD
350><DT
351>1.7. <A
352HREF="#CREDITS"
353>Credits</A
354></DT
355><DT
356>1.8. <A
357HREF="#WISHLIST"
358>Wish List</A
359></DT
360><DT
361>1.9. <A
362HREF="#COPYRIGHT"
363>Copyright</A
364></DT
365></DL
366></DD
367><DT
368>2. <A
369HREF="#HELLOWORLD"
370>Hello World !!!</A
371></DT
372><DD
373><DL
374><DT
375>2.1. <A
376HREF="#COMPILECURSES"
377>Compiling With the NCURSES Library</A
378></DT
379><DT
380>2.2. <A
381HREF="#DISSECTION"
382>Dissection</A
383></DT
384><DD
385><DL
386><DT
387>2.2.1. <A
388HREF="#ABOUT-INITSCR"
389>About initscr()</A
390></DT
391><DT
392>2.2.2. <A
393HREF="#MYST-REFRESH"
394>The mysterious refresh()</A
395></DT
396><DT
397>2.2.3. <A
398HREF="#ABOUT-ENDWIN"
399>About endwin()</A
400></DT
401></DL
402></DD
403></DL
404></DD
405><DT
406>3. <A
407HREF="#GORY"
408>The Gory Details</A
409></DT
410><DT
411>4. <A
412HREF="#INIT"
413>Initialization</A
414></DT
415><DD
416><DL
417><DT
418>4.1. <A
419HREF="#ABOUTINIT"
420>Initialization functions</A
421></DT
422><DT
423>4.2. <A
424HREF="#RAWCBREAK"
425>raw() and cbreak()</A
426></DT
427><DT
428>4.3. <A
429HREF="#ECHONOECHO"
430>echo() and noecho()</A
431></DT
432><DT
433>4.4. <A
434HREF="#KEYPAD"
435>keypad()</A
436></DT
437><DT
438>4.5. <A
439HREF="#HALFDELAY"
440>halfdelay()</A
441></DT
442><DT
443>4.6. <A
444HREF="#MISCINIT"
445>Miscellaneous Initialization functions</A
446></DT
447><DT
448>4.7. <A
449HREF="#INITEX"
450>An Example</A
451></DT
452></DL
453></DD
454><DT
455>5. <A
456HREF="#AWORDWINDOWS"
457>A Word about Windows</A
458></DT
459><DT
460>6. <A
461HREF="#PRINTW"
462>Output functions</A
463></DT
464><DD
465><DL
466><DT
467>6.1. <A
468HREF="#ADDCHCLASS"
469>addch() class of functions</A
470></DT
471><DT
472>6.2. <A
473HREF="#AEN298"
474>mvaddch(), waddch() and mvwaddch()</A
475></DT
476><DT
477>6.3. <A
478HREF="#PRINTWCLASS"
479>printw() class of functions</A
480></DT
481><DD
482><DL
483><DT
484>6.3.1. <A
485HREF="#PRINTWMVPRINTW"
486>printw() and mvprintw</A
487></DT
488><DT
489>6.3.2. <A
490HREF="#WPRINTWMVWPRINTW"
491>wprintw() and mvwprintw</A
492></DT
493><DT
494>6.3.3. <A
495HREF="#VWPRINTW"
496>vwprintw()</A
497></DT
498><DT
499>6.3.4. <A
500HREF="#SIMPLEPRINTWEX"
501>A Simple printw example</A
502></DT
503></DL
504></DD
505><DT
506>6.4. <A
507HREF="#ADDSTRCLASS"
508>addstr() class of functions</A
509></DT
510><DT
511>6.5. <A
512HREF="#ACAUTION"
513>A word of caution</A
514></DT
515></DL
516></DD
517><DT
518>7. <A
519HREF="#SCANW"
520>Input functions</A
521></DT
522><DD
523><DL
524><DT
525>7.1. <A
526HREF="#GETCHCLASS"
527>getch() class of functions</A
528></DT
529><DT
530>7.2. <A
531HREF="#SCANWCLASS"
532>scanw() class of functions</A
533></DT
534><DD
535><DL
536><DT
537>7.2.1. <A
538HREF="#SCANWMVSCANW"
539>scanw() and mvscanw</A
540></DT
541><DT
542>7.2.2. <A
543HREF="#WSCANWMVWSCANW"
544>wscanw() and mvwscanw()</A
545></DT
546><DT
547>7.2.3. <A
548HREF="#VWSCANW"
549>vwscanw()</A
550></DT
551></DL
552></DD
553><DT
554>7.3. <A
555HREF="#GETSTRCLASS"
556>getstr() class of functions</A
557></DT
558><DT
559>7.4. <A
560HREF="#GETSTREX"
561>Some examples</A
562></DT
563></DL
564></DD
565><DT
566>8. <A
567HREF="#ATTRIB"
568>Attributes</A
569></DT
570><DD
571><DL
572><DT
573>8.1. <A
574HREF="#ATTRIBDETAILS"
575>The details</A
576></DT
577><DT
578>8.2. <A
579HREF="#ATTRONVSATTRSET"
580>attron() vs attrset()</A
581></DT
582><DT
583>8.3. <A
584HREF="#ATTR_GET"
585>attr_get()</A
586></DT
587><DT
588>8.4. <A
589HREF="#ATTR_FUNCS"
590>attr_ functions</A
591></DT
592><DT
593>8.5. <A
594HREF="#WATTRFUNCS"
595>wattr functions</A
596></DT
597><DT
598>8.6. <A
599HREF="#CHGAT"
600>chgat() functions</A
601></DT
602></DL
603></DD
604><DT
605>9. <A
606HREF="#WINDOWS"
607>Windows</A
608></DT
609><DD
610><DL
611><DT
612>9.1. <A
613HREF="#WINDOWBASICS"
614>The basics</A
615></DT
616><DT
617>9.2. <A
618HREF="#LETBEWINDOW"
619>Let there be a Window !!!</A
620></DT
621><DT
622>9.3. <A
623HREF="#BORDEREXEXPL"
624>Explanation</A
625></DT
626><DT
627>9.4. <A
628HREF="#OTHERSTUFF"
629>The other stuff in the example</A
630></DT
631><DT
632>9.5. <A
633HREF="#OTHERBORDERFUNCS"
634>Other Border functions</A
635></DT
636></DL
637></DD
638><DT
639>10. <A
640HREF="#COLOR"
641>Colors</A
642></DT
643><DD
644><DL
645><DT
646>10.1. <A
647HREF="#COLORBASICS"
648>The basics</A
649></DT
650><DT
651>10.2. <A
652HREF="#CHANGECOLORDEFS"
653>Changing Color Definitions</A
654></DT
655><DT
656>10.3. <A
657HREF="#COLORCONTENT"
658>Color Content</A
659></DT
660></DL
661></DD
662><DT
663>11. <A
664HREF="#KEYS"
665>Interfacing with the key board</A
666></DT
667><DD
668><DL
669><DT
670>11.1. <A
671HREF="#KEYSBASICS"
672>The Basics</A
673></DT
674><DT
675>11.2. <A
676HREF="#SIMPLEKEYEX"
677>A Simple Key Usage example</A
678></DT
679></DL
680></DD
681><DT
682>12. <A
683HREF="#MOUSE"
684>Interfacing with the mouse</A
685></DT
686><DD
687><DL
688><DT
689>12.1. <A
690HREF="#MOUSEBASICS"
691>The Basics</A
692></DT
693><DT
694>12.2. <A
695HREF="#GETTINGEVENTS"
696>Getting the events</A
697></DT
698><DT
699>12.3. <A
700HREF="#MOUSETOGETHER"
701>Putting it all Together</A
702></DT
703><DT
704>12.4. <A
705HREF="#MISCMOUSEFUNCS"
706>Miscellaneous Functions</A
707></DT
708></DL
709></DD
710><DT
711>13. <A
712HREF="#SCREEN"
713>Screen Manipulation</A
714></DT
715><DD
716><DL
717><DT
718>13.1. <A
719HREF="#GETYX"
720>getyx() functions</A
721></DT
722><DT
723>13.2. <A
724HREF="#SCREENDUMP"
725>Screen Dumping</A
726></DT
727><DT
728>13.3. <A
729HREF="#WINDOWDUMP"
730>Window Dumping</A
731></DT
732></DL
733></DD
734><DT
735>14. <A
736HREF="#MISC"
737>Miscellaneous features</A
738></DT
739><DD
740><DL
741><DT
742>14.1. <A
743HREF="#CURSSET"
744>curs_set()</A
745></DT
746><DT
747>14.2. <A
748HREF="#TEMPLEAVE"
749>Temporarily Leaving Curses mode</A
750></DT
751><DT
752>14.3. <A
753HREF="#ACSVARS"
754>ACS_ variables</A
755></DT
756></DL
757></DD
758><DT
759>15. <A
760HREF="#OTHERLIB"
761>Other libraries</A
762></DT
763><DT
764>16. <A
765HREF="#PANELS"
766>Panel Library</A
767></DT
768><DD
769><DL
770><DT
771>16.1. <A
772HREF="#PANELBASICS"
773>The Basics</A
774></DT
775><DT
776>16.2. <A
777HREF="#COMPILEPANELS"
778>Compiling With the Panels Library</A
779></DT
780><DT
781>16.3. <A
782HREF="#PANELBROWSING"
783>Panel Window Browsing</A
784></DT
785><DT
786>16.4. <A
787HREF="#USERPTRUSING"
788>Using User Pointers</A
789></DT
790><DT
791>16.5. <A
792HREF="#PANELMOVERESIZE"
793>Moving and Resizing Panels</A
794></DT
795><DT
796>16.6. <A
797HREF="#PANELSHOWHIDE"
798>Hiding and Showing Panels</A
799></DT
800><DT
801>16.7. <A
802HREF="#PANELABOVE"
803>panel_above() and panel_below() Functions</A
804></DT
805></DL
806></DD
807><DT
808>17. <A
809HREF="#MENUS"
810>Menus Library</A
811></DT
812><DD
813><DL
814><DT
815>17.1. <A
816HREF="#MENUBASICS"
817>The Basics</A
818></DT
819><DT
820>17.2. <A
821HREF="#COMPILEMENUS"
822>Compiling With the Menu Library</A
823></DT
824><DT
825>17.3. <A
826HREF="#MENUDRIVER"
827>Menu Driver: The work horse of the menu system</A
828></DT
829><DT
830>17.4. <A
831HREF="#MENUWINDOWS"
832>Menu Windows</A
833></DT
834><DT
835>17.5. <A
836HREF="#SCROLLMENUS"
837>Scrolling Menus</A
838></DT
839><DT
840>17.6. <A
841HREF="#MULTICOLUMN"
842>Multi Columnar Menus</A
843></DT
844><DT
845>17.7. <A
846HREF="#MULTIVALUEMENUS"
847>Multi Valued Menus</A
848></DT
849><DT
850>17.8. <A
851HREF="#MENUOPT"
852>Menu Options</A
853></DT
854><DT
855>17.9. <A
856HREF="#MENUUSERPTR"
857>The useful User Pointer</A
858></DT
859></DL
860></DD
861><DT
862>18. <A
863HREF="#FORMS"
864>Forms Library</A
865></DT
866><DD
867><DL
868><DT
869>18.1. <A
870HREF="#FORMBASICS"
871>The Basics</A
872></DT
873><DT
874>18.2. <A
875HREF="#COMPILEFORMS"
876>Compiling With the Forms Library</A
877></DT
878><DT
879>18.3. <A
880HREF="#PLAYFIELDS"
881>Playing with Fields</A
882></DT
883><DD
884><DL
885><DT
886>18.3.1. <A
887HREF="#FETCHINFO"
888>Fetching Size and Location of Field</A
889></DT
890><DT
891>18.3.2. <A
892HREF="#MOVEFIELD"
893>Moving the field</A
894></DT
895><DT
896>18.3.3. <A
897HREF="#JUSTIFYFIELD"
898>Field Justification</A
899></DT
900><DT
901>18.3.4. <A
902HREF="#FIELDDISPATTRIB"
903>Field Display Attributes</A
904></DT
905><DT
906>18.3.5. <A
907HREF="#FIELDOPTIONBITS"
908>Field Option Bits</A
909></DT
910><DT
911>18.3.6. <A
912HREF="#FIELDSTATUS"
913>Field Status</A
914></DT
915><DT
916>18.3.7. <A
917HREF="#FIELDUSERPTR"
918>Field User Pointer</A
919></DT
920><DT
921>18.3.8. <A
922HREF="#VARIABLESIZEFIELDS"
923>Variable-Sized Fields</A
924></DT
925></DL
926></DD
927><DT
928>18.4. <A
929HREF="#FORMWINDOWS"
930>Form Windows</A
931></DT
932><DT
933>18.5. <A
934HREF="#FILEDVALIDATE"
935>Field Validation</A
936></DT
937><DT
938>18.6. <A
939HREF="#FORMDRIVER"
940>Form Driver: The work horse of the forms system</A
941></DT
942><DD
943><DL
944><DT
945>18.6.1. <A
946HREF="#PAGENAVREQ"
947>Page Navigation Requests</A
948></DT
949><DT
950>18.6.2. <A
951HREF="#INTERFIELDNAVREQ"
952>Inter-Field Navigation Requests</A
953></DT
954><DT
955>18.6.3. <A
956HREF="#INTRAFIELDNAVREQ"
957>Intra-Field Navigation Requests</A
958></DT
959><DT
960>18.6.4. <A
961HREF="#SCROLLREQ"
962>Scrolling Requests</A
963></DT
964><DT
965>18.6.5. <A
966HREF="#EDITREQ"
967>Editing Requests</A
968></DT
969><DT
970>18.6.6. <A
971HREF="#ORDERREQ"
972>Order Requests</A
973></DT
974><DT
975>18.6.7. <A
976HREF="#APPLICCOMMANDS"
977>Application Commands</A
978></DT
979></DL
980></DD
981></DL
982></DD
983><DT
984>19. <A
985HREF="#TOOLS"
986>Tools and Widget Libraries</A
987></DT
988><DD
989><DL
990><DT
991>19.1. <A
992HREF="#CDK"
993>CDK (Curses Development Kit)</A
994></DT
995><DD
996><DL
997><DT
998>19.1.1. <A
999HREF="#WIDGETLIST"
1000>Widget List</A
1001></DT
1002><DT
1003>19.1.2. <A
1004HREF="#CDKATTRACT"
1005>Some Attractive Features</A
1006></DT
1007><DT
1008>19.1.3. <A
1009HREF="#CDKCONCLUSION"
1010>Conclusion</A
1011></DT
1012></DL
1013></DD
1014><DT
1015>19.2. <A
1016HREF="#DIALOG"
1017>The dialog</A
1018></DT
1019><DT
1020>19.3. <A
1021HREF="#PERLCURSES"
1022>Perl Curses Modules CURSES::FORM and CURSES::WIDGETS</A
1023></DT
1024></DL
1025></DD
1026><DT
1027>20. <A
1028HREF="#JUSTFORFUN"
1029>Just For Fun !!!</A
1030></DT
1031><DD
1032><DL
1033><DT
1034>20.1. <A
1035HREF="#GAMEOFLIFE"
1036>The Game of Life</A
1037></DT
1038><DT
1039>20.2. <A
1040HREF="#MAGIC"
1041>Magic Square</A
1042></DT
1043><DT
1044>20.3. <A
1045HREF="#HANOI"
1046>Towers of Hanoi</A
1047></DT
1048><DT
1049>20.4. <A
1050HREF="#QUEENS"
1051>Queens Puzzle</A
1052></DT
1053><DT
1054>20.5. <A
1055HREF="#SHUFFLE"
1056>Shuffle</A
1057></DT
1058><DT
1059>20.6. <A
1060HREF="#TT"
1061>Typing Tutor</A
1062></DT
1063></DL
1064></DD
1065><DT
1066>21. <A
1067HREF="#REF"
1068>References</A
1069></DT
1070></DL
1071></DIV
1072><DIV
1073CLASS="SECT1"
1074><H2
1075CLASS="SECT1"
1076><A
1077NAME="INTRO"
1078>1. Introduction</A
1079></H2
1080><P
1081>In the olden days of teletype terminals, terminals were away from computers and
1082were connected to them through serial cables. The terminals could be configured
1083by sending a series of bytes. All the capabilities (such as
1084moving the cursor to a new location, erasing part of the screen, scrolling the
1085screen, changing modes etc.) of terminals could be accessed through these
1086series of bytes. These control seeuqnces are usually called escape sequences,
1087because they start
1088with an escape(0x1B) character. Even today, with proper emulation, we can send
1089escape sequences to the emulator and achieve the same effect on a terminal
1090window.</P
1091><P
1092>Suppose you wanted to print a line in color. Try typing this on your console.</P
1093><PRE
1094CLASS="PROGRAMLISTING"
1095>echo "^[[0;31;40mIn Color"</PRE
1096><P
1097>The first character is an escape character, which looks like two characters ^
1098and [. To be able to print it, you have to press CTRL+V and then the ESC key.
1099All the others are normal printable characters. You should be able to see the
1100string "In Color" in red. It stays that way and to revert back to the original
1101mode type this.</P
1102><PRE
1103CLASS="PROGRAMLISTING"
1104>echo "^[[0;37;40m"</PRE
1105><P
1106>Now, what do these magic characters mean? Difficult to comprehend? They might
1107even be different for different terminals. So the designers of UNIX invented a
1108mechanism named <TT
1109CLASS="LITERAL"
1110>termcap</TT
1111>. It is a file that
1112lists all the capabilities of a particular terminal, along with the escape
1113sequences needed to achieve a particular effect. In the later years, this was
1114replaced by <TT
1115CLASS="LITERAL"
1116>terminfo</TT
1117>. Without delving too
1118much into details, this mechanism allows application
1119programs to query the terminfo database and obtain the control characters to be
1120sent to a terminal or terminal emulator.</P
1121><DIV
1122CLASS="SECT2"
1123><HR><H3
1124CLASS="SECT2"
1125><A
1126NAME="WHATIS"
1127>1.1. What is NCURSES?</A
1128></H3
1129><P
1130>
1131You might be wondering, what the import of all this technical gibberish is.  In
1132the above scenario, every application program is supposed to query the terminfo
1133and perform the necessary stuff (sending control characters etc.).  It soon became
1134difficult to manage this complexity and this gave birth to 'CURSES'.  Curses is
1135a pun on the name "cursor optimization". The Curses library forms a wrapper
1136over working with raw terminal codes, and provides highly flexible and
1137efficient API (Application Programming Interface). It provides functions to
1138move the cursor, create windows, produce colors, play with mouse etc.  The
1139application programs need not worry about the underlying terminal capabilities.</P
1140><P
1141>So what is NCURSES? NCURSES is a clone of the original System V Release 4.0
1142(SVr4) curses. It is a freely distributable library, fully compatible with
1143older version of curses.  In short, it is a library of functions that manages
1144an application's display on character-cell terminals.  In the remainder of the
1145document, the terms curses and ncurses are used interchangeably.  </P
1146><P
1147>A detailed history of NCURSES can be found in the NEWS file from the source
1148distribution. The current package is maintained by
1149<A
1150HREF="mailto:dickey@his.com"
1151TARGET="_top"
1152>Thomas Dickey</A
1153>.
1154You can contact the maintainers at <A
1155HREF="mailto:bug-ncurses@gnu.org"
1156TARGET="_top"
1157>bug-ncurses@gnu.org</A
1158>.</P
1159></DIV
1160><DIV
1161CLASS="SECT2"
1162><HR><H3
1163CLASS="SECT2"
1164><A
1165NAME="WHATCANWEDO"
1166>1.2. What we can do with NCURSES</A
1167></H3
1168><P
1169>NCURSES not only creates a wrapper over terminal capabilities, but also gives a
1170robust framework to create nice looking UI (User Interface)s in text mode.  It
1171provides functions to create windows etc.  Its sister libraries panel, menu and
1172form provide an extension to the basic curses library. These libraries usually
1173come along with curses. One can create applications that contain multiple
1174windows, menus, panels and forms. Windows can be managed independently, can
1175provide 'scrollability' and even can be hidden.</P
1176><P
1177>
1178Menus provide the user with an easy command selection option.  Forms allow the
1179creation of easy-to-use data entry and display windows.  Panels extend the
1180capabilities of ncurses to deal with overlapping and stacked windows.</P
1181><P
1182>These are just some of the basic things we can do with ncurses. As we move
1183along, We will see all the capabilities of these libraries. </P
1184></DIV
1185><DIV
1186CLASS="SECT2"
1187><HR><H3
1188CLASS="SECT2"
1189><A
1190NAME="WHERETOGETIT"
1191>1.3. Where to get it</A
1192></H3
1193><P
1194>All right, now that you know what you can do with ncurses, you must be rearing
1195to get started. NCURSES is usually shipped with your installation. In case
1196you don't have the library or want to compile it on your own, read on.</P
1197><P
1198><SPAN
1199CLASS="emphasis"
1200><I
1201CLASS="EMPHASIS"
1202>Compiling the package</I
1203></SPAN
1204> </P
1205><P
1206>NCURSES can be obtained from <A
1207HREF="ftp://ftp.gnu.org/pub/gnu/ncurses/ncurses.tar.gz"
1208TARGET="_top"
1209>ftp://ftp.gnu.org/pub/gnu/ncurses/ncurses.tar.gz</A
1210> or any of the ftp
1211sites mentioned in <A
1212HREF="http://www.gnu.org/order/ftp.html"
1213TARGET="_top"
1214>http://www.gnu.org/order/ftp.html</A
1215>. </P
1216><P
1217>Read the README and INSTALL files for details on to how to install it. It
1218usually involves the following operations.</P
1219><PRE
1220CLASS="PROGRAMLISTING"
1221>    tar zxvf ncurses&lt;version&gt;.tar.gz  # unzip and untar the archive
1222    cd ncurses&lt;version&gt;               # cd to the directory
1223    ./configure                             # configure the build according to your
1224                                            # environment
1225    make                                    # make it
1226    su root                                 # become root
1227    make install                            # install it</PRE
1228><P
1229><SPAN
1230CLASS="emphasis"
1231><I
1232CLASS="EMPHASIS"
1233>Using the RPM </I
1234></SPAN
1235></P
1236><P
1237>NCURSES RPM can be found and downloaded from <A
1238HREF="http://rpmfind.net"
1239TARGET="_top"
1240>http://rpmfind.net </A
1241>. The RPM can be installed with the following
1242command after becoming root.</P
1243><PRE
1244CLASS="PROGRAMLISTING"
1245>    rpm -i &lt;downloaded rpm&gt;</PRE
1246></DIV
1247><DIV
1248CLASS="SECT2"
1249><HR><H3
1250CLASS="SECT2"
1251><A
1252NAME="PURPOSE"
1253>1.4. Purpose/Scope of the document</A
1254></H3
1255><P
1256>This document is intended to be a "All in One" guide for programming with
1257ncurses and its sister libraries. We graduate from a simple "Hello World"
1258program to more complex form manipulation. No prior experience in ncurses is
1259assumed. The writing is informal, but a lot of detail is provided for
1260each of the examples.</P
1261></DIV
1262><DIV
1263CLASS="SECT2"
1264><HR><H3
1265CLASS="SECT2"
1266><A
1267NAME="ABOUTPROGRAMS"
1268>1.5. About the Programs</A
1269></H3
1270><P
1271>All the programs in the document are available in zipped form
1272<A
1273HREF="http://www.tldp.org/HOWTO/NCURSES-Programming-HOWTO/ncurses_programs.tar.gz"
1274TARGET="_top"
1275>here</A
1276>. Unzip and untar it. The directory structure looks like this.</P
1277><PRE
1278CLASS="PROGRAMLISTING"
1279>ncurses
1280   |
1281   |----&gt; JustForFun     -- just for fun programs
1282   |----&gt; basics         -- basic programs
1283   |----&gt; demo           -- output files go into this directory after make
1284   |          |
1285   |          |----&gt; exe -- exe files of all example programs
1286   |----&gt; forms          -- programs related to form library
1287   |----&gt; menus          -- programs related to menus library
1288   |----&gt; panels         -- programs related to panels library
1289   |----&gt; perl           -- perl equivalents of the examples (contributed
1290   |                            by Anuradha Ratnaweera)
1291   |----&gt; Makefile       -- the top level Makefile
1292   |----&gt; README         -- the top level README file. contains instructions
1293   |----&gt; COPYING        -- copyright notice</PRE
1294><P
1295>The individual directories contain the following files.</P
1296><PRE
1297CLASS="PROGRAMLISTING"
1298>Description of files in each directory
1299--------------------------------------
1300JustForFun
1301    |
1302    |----&gt; hanoi.c   -- The Towers of Hanoi Solver
1303    |----&gt; life.c    -- The Game of Life demo
1304    |----&gt; magic.c   -- An Odd Order Magic Square builder
1305    |----&gt; queens.c  -- The famous N-Queens Solver
1306    |----&gt; shuffle.c -- A fun game, if you have time to kill
1307    |----&gt; tt.c      -- A very trivial typing tutor
1308
1309  basics
1310    |
1311    |----&gt; acs_vars.c            -- ACS_ variables example
1312    |----&gt; hello_world.c         -- Simple "Hello World" Program
1313    |----&gt; init_func_example.c   -- Initialization functions example
1314    |----&gt; key_code.c            -- Shows the scan code of the key pressed
1315    |----&gt; mouse_menu.c          -- A menu accessible by mouse
1316    |----&gt; other_border.c        -- Shows usage of other border functions apa
1317    |                               -- rt from box()
1318    |----&gt; printw_example.c      -- A very simple printw() example
1319    |----&gt; scanw_example.c       -- A very simple getstr() example
1320    |----&gt; simple_attr.c         -- A program that can print a c file with
1321    |                               -- comments in attribute
1322    |----&gt; simple_color.c        -- A simple example demonstrating colors
1323    |----&gt; simple_key.c          -- A menu accessible with keyboard UP, DOWN
1324    |                               -- arrows
1325    |----&gt; temp_leave.c          -- Demonstrates temporarily leaving curses mode
1326    |----&gt; win_border.c          -- Shows Creation of windows and borders
1327    |----&gt; with_chgat.c          -- chgat() usage example
1328
1329  forms
1330    |
1331    |----&gt; form_attrib.c     -- Usage of field attributes
1332    |----&gt; form_options.c    -- Usage of field options
1333    |----&gt; form_simple.c     -- A simple form example
1334    |----&gt; form_win.c        -- Demo of windows associated with forms
1335
1336  menus
1337    |
1338    |----&gt; menu_attrib.c     -- Usage of menu attributes
1339    |----&gt; menu_item_data.c  -- Usage of item_name() etc.. functions
1340    |----&gt; menu_multi_column.c    -- Creates multi columnar menus
1341    |----&gt; menu_scroll.c     -- Demonstrates scrolling capability of menus
1342    |----&gt; menu_simple.c     -- A simple menu accessed by arrow keys
1343    |----&gt; menu_toggle.c     -- Creates multi valued menus and explains
1344    |                           -- REQ_TOGGLE_ITEM
1345    |----&gt; menu_userptr.c    -- Usage of user pointer
1346    |----&gt; menu_win.c        -- Demo of windows associated with menus
1347
1348  panels
1349    |
1350    |----&gt; panel_browse.c    -- Panel browsing through tab. Usage of user
1351    |                           -- pointer
1352    |----&gt; panel_hide.c      -- Hiding and Un hiding of panels
1353    |----&gt; panel_resize.c    -- Moving and resizing of panels
1354    |----&gt; panel_simple.c    -- A simple panel example
1355
1356  perl
1357    |----&gt; 01-10.pl          -- Perl equivalents of first ten example programs</PRE
1358><P
1359>There is a top level Makefile included in the main directory. It builds all the
1360files and puts the ready-to-use exes in demo/exe directory. You can also
1361do selective make by going into the corresponding directory. Each directory
1362contains a README file explaining the purpose of each c file in the directory.</P
1363><P
1364>For every example, I have included path name for the file relative to the
1365examples directory. </P
1366><P
1367> If you prefer browsing individual programs, point your browser to
1368<A
1369HREF="http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/ncurses_programs/"
1370TARGET="_top"
1371>http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/ncurses_programs/</A
1372></P
1373><P
1374>All the programs are released under the same license that is used by ncurses
1375(MIT-style). This gives you the ability to do pretty much anything other than
1376claiming them as yours. Feel free to use them in your programs as appropriate.</P
1377></DIV
1378><DIV
1379CLASS="SECT2"
1380><HR><H3
1381CLASS="SECT2"
1382><A
1383NAME="OTHERFORMATS"
1384>1.6. Other Formats of the document</A
1385></H3
1386><P
1387>This howto is also availabe in various other formats on the tldp.org site.
1388Here are the links to other formats of this document.</P
1389><DIV
1390CLASS="SECT3"
1391><HR><H4
1392CLASS="SECT3"
1393><A
1394NAME="LISTFORMATS"
1395>1.6.1. Readily available formats from tldp.org</A
1396></H4
1397><P
1398></P
1399><UL
1400><LI
1401><P
1402><A
1403HREF="http://www.ibiblio.org/pub/Linux/docs/HOWTO/other-formats/pdf/NCURSES-Programming-HOWTO.pdf"
1404TARGET="_top"
1405>Acrobat PDF Format</A
1406></P
1407></LI
1408><LI
1409><P
1410><A
1411HREF="http://www.ibiblio.org/pub/Linux/docs/HOWTO/other-formats/ps/NCURSES-Programming-HOWTO.ps.gz"
1412TARGET="_top"
1413>PostScript Format</A
1414></P
1415></LI
1416><LI
1417><P
1418><A
1419HREF="http://www.ibiblio.org/pub/Linux/docs/HOWTO/other-formats/html/NCURSES-Programming-HOWTO-html.tar.gz"
1420TARGET="_top"
1421>In Multiple HTML pages</A
1422></P
1423></LI
1424><LI
1425><P
1426><A
1427HREF="http://www.ibiblio.org/pub/Linux/docs/HOWTO/other-formats/html_single/NCURSES-Programming-HOWTO.html"
1428TARGET="_top"
1429>In One big HTML format</A
1430></P
1431></LI
1432></UL
1433></DIV
1434><DIV
1435CLASS="SECT3"
1436><HR><H4
1437CLASS="SECT3"
1438><A
1439NAME="BUILDSOURCE"
1440>1.6.2. Building from source</A
1441></H4
1442><P
1443>If above links are broken or if you want to experiment with sgml read on.
1444<PRE
1445CLASS="PROGRAMLISTING"
1446>&#13;    Get both the source and the tar,gzipped programs, available at
1447        http://cvsview.tldp.org/index.cgi/LDP/howto/docbook/
1448        NCURSES-HOWTO/NCURSES-Programming-HOWTO.sgml
1449        http://cvsview.tldp.org/index.cgi/LDP/howto/docbook/
1450        NCURSES-HOWTO/ncurses_programs.tar.gz
1451
1452    Unzip ncurses_programs.tar.gz with
1453    tar zxvf ncurses_programs.tar.gz
1454
1455    Use jade to create various formats. For example if you just want to create
1456    the multiple html files, you would use
1457        jade -t sgml -i html -d &lt;path to docbook html stylesheet&gt;
1458        NCURSES-Programming-HOWTO.sgml
1459    to get pdf, first create a single html file of the HOWTO with
1460        jade -t sgml -i html -d &lt;path to docbook html stylesheet&gt; -V nochunks
1461        NCURSES-Programming-HOWTO.sgml &gt; NCURSES-ONE-BIG-FILE.html
1462    then use htmldoc to get pdf file with
1463        htmldoc --size universal -t pdf --firstpage p1 -f &lt;output file name.pdf&gt;
1464        NCURSES-ONE-BIG-FILE.html
1465    for ps, you would use
1466        htmldoc --size universal -t ps --firstpage p1 -f &lt;output file name.ps&gt;
1467        NCURSES-ONE-BIG-FILE.html</PRE
1468></P
1469><P
1470>See <A
1471HREF="http://www.tldp.org/LDP/LDP-Author-Guide/"
1472TARGET="_top"
1473>LDP Author guide</A
1474> for more details. If all else failes, mail me at
1475<A
1476HREF="ppadala@gmail.com"
1477TARGET="_top"
1478>ppadala@gmail.com</A
1479></P
1480></DIV
1481></DIV
1482><DIV
1483CLASS="SECT2"
1484><HR><H3
1485CLASS="SECT2"
1486><A
1487NAME="CREDITS"
1488>1.7. Credits</A
1489></H3
1490><P
1491>I thank <A
1492HREF="mailto:sharath_1@usa.net"
1493TARGET="_top"
1494>Sharath</A
1495> and Emre Akbas for
1496helping me with few sections. The introduction was initially written by sharath.
1497I rewrote it with few excerpts taken from his initial work. Emre helped in
1498writing printw and scanw sections.</P
1499><P
1500>Perl equivalents of the example programs are contributed by <A
1501HREF="mailto:Aratnaweera@virtusa.com"
1502TARGET="_top"
1503>Anuradha Ratnaweera</A
1504>. </P
1505><P
1506>Then comes <A
1507HREF="mailto:parimi@ece.arizona.edu"
1508TARGET="_top"
1509>Ravi Parimi</A
1510>, my
1511dearest friend, who has been on this project before even one line was written.
1512He constantly bombarded me with suggestions and patiently reviewed the whole
1513text.  He also checked each program on Linux and Solaris. </P
1514></DIV
1515><DIV
1516CLASS="SECT2"
1517><HR><H3
1518CLASS="SECT2"
1519><A
1520NAME="WISHLIST"
1521>1.8. Wish List</A
1522></H3
1523><P
1524>This is the wish list, in the order of priority. If you have a wish or you want
1525to work on completing the wish, mail <A
1526HREF="mailto:ppadala@gmail.com"
1527TARGET="_top"
1528>me</A
1529>. </P
1530><P
1531></P
1532><UL
1533><LI
1534><P
1535>Add examples to last parts of forms section.</P
1536></LI
1537><LI
1538><P
1539>Prepare a Demo showing all the programs and allow the user to browse through
1540description of each program. Let the user compile and see the program in action.
1541A dialog based interface is preferred.</P
1542></LI
1543><LI
1544><P
1545>Add debug info. _tracef, _tracemouse stuff.</P
1546></LI
1547><LI
1548><P
1549>Accessing termcap, terminfo using functions provided by ncurses
1550package.</P
1551></LI
1552><LI
1553><P
1554>Working on two terminals simultaneously.</P
1555></LI
1556><LI
1557><P
1558>Add more stuff to miscellaneous section.</P
1559></LI
1560></UL
1561></DIV
1562><DIV
1563CLASS="SECT2"
1564><HR><H3
1565CLASS="SECT2"
1566><A
1567NAME="COPYRIGHT"
1568>1.9. Copyright</A
1569></H3
1570><P
1571>Copyright &copy; 2001 by Pradeep Padala. </P
1572><P
1573>Permission is hereby granted, free of charge, to any person obtaining a copy
1574of this software and associated documentation files (the "Software"), to deal
1575in the Software without restriction, including without limitation the rights
1576to use, copy, modify, merge, publish, distribute, distribute with
1577modifications, sublicense, and/or sell copies of the Software, and to permit
1578persons to whom the Software is furnished to do so, subject to the following
1579conditions:</P
1580><P
1581>The above copyright notice and this permission notice shall be included in all
1582copies or substantial portions of the Software.</P
1583><P
1584>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1585IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1586FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1587ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
1588WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
1589IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.</P
1590><P
1591>Except as contained in this notice, the name(s) of the above copyright holders
1592shall not be used in advertising or otherwise to promote the sale, use or
1593other dealings in this Software without prior written authorization. </P
1594></DIV
1595></DIV
1596><DIV
1597CLASS="SECT1"
1598><HR><H2
1599CLASS="SECT1"
1600><A
1601NAME="HELLOWORLD"
1602>2. Hello World !!!</A
1603></H2
1604><P
1605>Welcome to the world of curses. Before we plunge into the library and look into
1606its various features, let's write a simple program and say
1607hello to the world. </P
1608><DIV
1609CLASS="SECT2"
1610><HR><H3
1611CLASS="SECT2"
1612><A
1613NAME="COMPILECURSES"
1614>2.1. Compiling With the NCURSES Library</A
1615></H3
1616><P
1617>To use ncurses library functions, you have to include ncurses.h in your
1618programs. To link the
1619program with ncurses the flag -lncurses should be added.</P
1620><PRE
1621CLASS="PROGRAMLISTING"
1622>    #include &lt;ncurses.h&gt;
1623    .
1624    .
1625    .
1626
1627    compile and link: gcc &lt;program file&gt; -lncurses</PRE
1628><DIV
1629CLASS="EXAMPLE"
1630><A
1631NAME="BHW"
1632></A
1633><P
1634><B
1635>Example 1.  The Hello World !!! Program </B
1636></P
1637><PRE
1638CLASS="PROGRAMLISTING"
1639><SPAN
1640CLASS="INLINEMEDIAOBJECT"
1641>#include &#60;ncurses.h&#62;
1642
1643int main()
1644{
1645	initscr();			/* Start curses mode 		  */
1646	printw("Hello World !!!");	/* Print Hello World		  */
1647	refresh();			/* Print it on to the real screen */
1648	getch();			/* Wait for user input */
1649	endwin();			/* End curses mode		  */
1650
1651	return 0;
1652}</SPAN
1653></PRE
1654></DIV
1655></DIV
1656><DIV
1657CLASS="SECT2"
1658><HR><H3
1659CLASS="SECT2"
1660><A
1661NAME="DISSECTION"
1662>2.2. Dissection</A
1663></H3
1664><P
1665>
1666The above program prints "Hello World !!!" to the screen and exits. This
1667program shows how to initialize curses and do screen manipulation and
1668end curses mode. Let's dissect it line by line. </P
1669><DIV
1670CLASS="SECT3"
1671><HR><H4
1672CLASS="SECT3"
1673><A
1674NAME="ABOUT-INITSCR"
1675>2.2.1. About initscr()</A
1676></H4
1677><P
1678>The function initscr() initializes the terminal in curses mode.  In some
1679implementations, it clears the screen and presents a blank screen. To do any
1680screen manipulation using curses package this has to be called first. This
1681function initializes the curses system and allocates memory for our present
1682window (called <TT
1683CLASS="LITERAL"
1684>stdscr</TT
1685>) and some other data-structures. Under extreme
1686cases this function might fail due to insufficient memory to allocate memory
1687for curses library's data structures. </P
1688><P
1689>
1690After this is done, we can do a variety of initializations to customize
1691our curses settings. These details will be explained <A
1692HREF="#INIT"
1693>later </A
1694>.</P
1695></DIV
1696><DIV
1697CLASS="SECT3"
1698><HR><H4
1699CLASS="SECT3"
1700><A
1701NAME="MYST-REFRESH"
1702>2.2.2. The mysterious refresh()</A
1703></H4
1704><P
1705>The next line printw prints the string "Hello World !!!" on to the screen. This
1706function is analogous to normal printf in all respects except that it prints
1707the data on a window called stdscr at the current (y,x) co-ordinates. Since our
1708present co-ordinates are at 0,0 the string is printed at the left hand corner
1709of the window.</P
1710><P
1711>This brings us to that mysterious refresh(). Well, when we called printw
1712the data is actually written to an imaginary window, which is not updated
1713on the screen yet. The job of printw is to update a few flags
1714and data structures and write the data to a buffer corresponding to stdscr.
1715In order to show it on the screen, we need to call refresh() and tell the
1716curses system to dump the contents on the screen.</P
1717><P
1718>The philosophy behind all this is to allow the programmer to do multiple updates
1719on the imaginary screen or windows and do a refresh once all his screen update
1720is done. refresh() checks the window and updates only the portion which has been
1721changed. This improves performance and offers greater flexibility too. But, it is
1722sometimes frustrating to beginners. A common mistake committed by beginners is
1723to forget to call refresh() after they did some update through printw() class of
1724functions. I still forget to add it sometimes :-) </P
1725></DIV
1726><DIV
1727CLASS="SECT3"
1728><HR><H4
1729CLASS="SECT3"
1730><A
1731NAME="ABOUT-ENDWIN"
1732>2.2.3. About endwin()</A
1733></H4
1734><P
1735>And finally don't forget to end the curses mode. Otherwise your terminal might
1736behave strangely after the program quits. endwin() frees the memory taken by
1737curses sub-system and its data structures and puts the terminal in normal
1738mode. This function must be called after you are done with the curses mode. </P
1739></DIV
1740></DIV
1741></DIV
1742><DIV
1743CLASS="SECT1"
1744><HR><H2
1745CLASS="SECT1"
1746><A
1747NAME="GORY"
1748>3. The Gory Details</A
1749></H2
1750><P
1751>Now that we have seen how to write a simple curses program let's get into the
1752details. There are many functions that help customize what you see on screen and
1753many features which can be put to full use. </P
1754><P
1755>Here we go...</P
1756></DIV
1757><DIV
1758CLASS="SECT1"
1759><HR><H2
1760CLASS="SECT1"
1761><A
1762NAME="INIT"
1763>4. Initialization</A
1764></H2
1765><P
1766>We now know that to initialize curses system the function initscr() has to be
1767called.  There are functions which can be called after this initialization to
1768customize our curses session. We may ask the curses system to set the terminal
1769in raw mode or initialize color or initialize the mouse etc.. Let's discuss some
1770of the functions that are normally called immediately after initscr();</P
1771><DIV
1772CLASS="SECT2"
1773><HR><H3
1774CLASS="SECT2"
1775><A
1776NAME="ABOUTINIT"
1777>4.1. Initialization functions</A
1778></H3
1779><P
1780> </P
1781></DIV
1782><DIV
1783CLASS="SECT2"
1784><HR><H3
1785CLASS="SECT2"
1786><A
1787NAME="RAWCBREAK"
1788>4.2. raw() and cbreak()</A
1789></H3
1790><P
1791>Normally the terminal driver buffers the characters a user types until a new
1792line or carriage return is encountered. But most programs require that the
1793characters be available as soon as the user types them. The above two functions
1794are used to disable line buffering. The difference between these two functions
1795is in the way control characters like suspend (CTRL-Z), interrupt and quit
1796(CTRL-C) are passed to the program. In the raw() mode these characters are
1797directly passed to the program without generating a signal. In the
1798<TT
1799CLASS="LITERAL"
1800>cbreak()</TT
1801> mode these control characters are
1802interpreted as any other character by the terminal driver. I personally prefer
1803to use raw() as I can exercise greater control over what the user does.</P
1804></DIV
1805><DIV
1806CLASS="SECT2"
1807><HR><H3
1808CLASS="SECT2"
1809><A
1810NAME="ECHONOECHO"
1811>4.3. echo() and noecho()</A
1812></H3
1813><P
1814>
1815These functions control the echoing of characters typed by the user to the
1816terminal. <TT
1817CLASS="LITERAL"
1818>noecho()</TT
1819> switches off echoing. The
1820reason you might want to do this is to gain more control over echoing or to
1821suppress unnecessary echoing while taking input from the user through the
1822getch() etc. functions. Most of the interactive programs call
1823<TT
1824CLASS="LITERAL"
1825>noecho()</TT
1826> at initialization and do the echoing
1827of characters in a controlled manner. It gives the programmer the flexibility
1828of echoing characters at any place in the window without updating current (y,x)
1829co-ordinates. </P
1830></DIV
1831><DIV
1832CLASS="SECT2"
1833><HR><H3
1834CLASS="SECT2"
1835><A
1836NAME="KEYPAD"
1837>4.4. keypad()</A
1838></H3
1839><P
1840>This is my favorite initialization function. It enables the reading of function
1841keys like F1, F2, arrow keys etc. Almost every interactive program enables this,
1842as arrow keys are a major part of any User Interface. Do
1843<TT
1844CLASS="LITERAL"
1845>keypad(stdscr, TRUE) </TT
1846> to enable this feature
1847for the regular screen (stdscr). You will learn more about key management in
1848later sections of this document.</P
1849></DIV
1850><DIV
1851CLASS="SECT2"
1852><HR><H3
1853CLASS="SECT2"
1854><A
1855NAME="HALFDELAY"
1856>4.5. halfdelay()</A
1857></H3
1858><P
1859>This function, though not used very often, is a useful one at times.
1860halfdelay()is called to enable the half-delay mode, which is similar to the
1861cbreak() mode in that characters typed are immediately available to program.
1862However, it waits for 'X' tenths of a second for input and then returns ERR, if
1863no input is available. 'X' is the timeout value passed to the function
1864halfdelay(). This function is useful when you want to ask the user for input,
1865and if he doesn't respond with in certain time, we can do some thing else. One
1866possible example is a timeout at the password prompt. </P
1867></DIV
1868><DIV
1869CLASS="SECT2"
1870><HR><H3
1871CLASS="SECT2"
1872><A
1873NAME="MISCINIT"
1874>4.6. Miscellaneous Initialization functions</A
1875></H3
1876><P
1877>There are few more functions which are called at initialization to
1878customize curses behavior. They are not used as extensively as those mentioned
1879above. Some of them are explained where appropriate.</P
1880></DIV
1881><DIV
1882CLASS="SECT2"
1883><HR><H3
1884CLASS="SECT2"
1885><A
1886NAME="INITEX"
1887>4.7. An Example</A
1888></H3
1889><P
1890>Let's write a program which will clarify the usage of these functions.</P
1891><DIV
1892CLASS="EXAMPLE"
1893><A
1894NAME="BINFU"
1895></A
1896><P
1897><B
1898>Example 2.  Initialization Function Usage example </B
1899></P
1900><PRE
1901CLASS="PROGRAMLISTING"
1902><SPAN
1903CLASS="INLINEMEDIAOBJECT"
1904>#include &#60;ncurses.h&#62;
1905
1906int main()
1907{	int ch;
1908
1909	initscr();			/* Start curses mode 		*/
1910	raw();				/* Line buffering disabled	*/
1911	keypad(stdscr, TRUE);		/* We get F1, F2 etc..		*/
1912	noecho();			/* Don't echo() while we do getch */
1913
1914    	printw("Type any character to see it in bold\n");
1915	ch = getch();			/* If raw() hadn't been called
1916					 * we have to press enter before it
1917					 * gets to the program 		*/
1918	if(ch == KEY_F(1))		/* Without keypad enabled this will */
1919		printw("F1 Key pressed");/*  not get to us either	*/
1920					/* Without noecho() some ugly escape
1921					 * charachters might have been printed
1922					 * on screen			*/
1923	else
1924	{	printw("The pressed key is ");
1925		attron(A_BOLD);
1926		printw("%c", ch);
1927		attroff(A_BOLD);
1928	}
1929	refresh();			/* Print it on to the real screen */
1930    	getch();			/* Wait for user input */
1931	endwin();			/* End curses mode		  */
1932
1933	return 0;
1934}</SPAN
1935></PRE
1936></DIV
1937><P
1938>This program is self-explanatory. But I used functions which aren't explained
1939yet. The function <TT
1940CLASS="LITERAL"
1941>getch()</TT
1942> is used to get a
1943character from user. It is equivalent to normal
1944<TT
1945CLASS="LITERAL"
1946>getchar()</TT
1947> except that we can disable the line
1948buffering to avoid &lt;enter&gt; after input. Look for more about
1949<TT
1950CLASS="LITERAL"
1951>getch()</TT
1952>and reading keys in the <A
1953HREF="#KEYS"
1954> key management section </A
1955>. The functions attron and attroff
1956are used to switch some attributes on and off respectively.  In the example I
1957used them to print the character in bold. These functions are explained in detail
1958later.</P
1959></DIV
1960></DIV
1961><DIV
1962CLASS="SECT1"
1963><HR><H2
1964CLASS="SECT1"
1965><A
1966NAME="AWORDWINDOWS"
1967>5. A Word about Windows</A
1968></H2
1969><P
1970>
1971Before we plunge into the myriad ncurses functions, let me clear few things
1972about windows. Windows are explained in detail in following <A
1973HREF="#WINDOWS"
1974> sections </A
1975></P
1976><P
1977>A Window is an imaginary screen defined by curses system. A window does not mean
1978a bordered window which you usually see on Win9X platforms. When curses is
1979initialized, it creates a default window named
1980<TT
1981CLASS="LITERAL"
1982>stdscr</TT
1983> which represents your 80x25 (or the size
1984of window in which you are running) screen.  If you are doing simple tasks like
1985printing few strings, reading input etc., you can safely use this single window
1986for all of your purposes. You can also create windows and call functions which
1987explicitly work on the specified window.</P
1988><P
1989>For example, if you call</P
1990><PRE
1991CLASS="PROGRAMLISTING"
1992>    printw("Hi There !!!");
1993    refresh();</PRE
1994><P
1995>It prints the string on stdscr at the present cursor position. Similarly the
1996call to refresh(), works on stdscr only. </P
1997><P
1998>Say you have created <A
1999HREF="#WINDOWS"
2000>windows</A
2001> then you have to
2002call a function with a 'w' added to the usual function.</P
2003><PRE
2004CLASS="PROGRAMLISTING"
2005>    wprintw(win, "Hi There !!!");
2006    wrefresh(win);</PRE
2007><P
2008>As you will see in the rest of the document, naming of functions follow the
2009same convention. For each function there usually are three more functions.</P
2010><PRE
2011CLASS="PROGRAMLISTING"
2012>    printw(string);        /* Print on stdscr at present cursor position */
2013    mvprintw(y, x, string);/* Move to (y, x) then print string     */
2014    wprintw(win, string);  /* Print on window win at present cursor position */
2015                           /* in the window */
2016    mvwprintw(win, y, x, string);   /* Move to (y, x) relative to window */
2017                                    /* co-ordinates and then print         */</PRE
2018><P
2019>Usually the w-less functions are macros which expand to corresponding w-function
2020with stdscr as the window parameter.</P
2021></DIV
2022><DIV
2023CLASS="SECT1"
2024><HR><H2
2025CLASS="SECT1"
2026><A
2027NAME="PRINTW"
2028>6. Output functions</A
2029></H2
2030><P
2031>I guess you can't wait any more to see some action. Back to our odyssey of
2032curses functions. Now that curses is initialized, let's interact with
2033world.</P
2034><P
2035>There are three classes of functions which you can use to do output on screen.
2036<P
2037></P
2038><OL
2039TYPE="1"
2040><LI
2041><P
2042>addch() class: Print single character with attributes </P
2043></LI
2044><LI
2045><P
2046>printw() class: Print formatted output similar to printf()</P
2047></LI
2048><LI
2049><P
2050>addstr() class: Print strings</P
2051></LI
2052></OL
2053></P
2054><P
2055>These functions can be used interchangeably and it's a matter of style as to
2056which class is used. Let's see each one in detail.</P
2057><DIV
2058CLASS="SECT2"
2059><HR><H3
2060CLASS="SECT2"
2061><A
2062NAME="ADDCHCLASS"
2063>6.1. addch() class of functions</A
2064></H3
2065><P
2066>These functions put a single character into the current cursor location and
2067advance the position of the cursor. You can give the character to be printed but
2068they usually are used to print a character with some attributes.  Attributes are
2069explained in detail in later <A
2070HREF="#ATTRIB"
2071> sections </A
2072> of the
2073document. If a character is associated with an attribute(bold, reverse video
2074etc.), when curses prints the character, it is printed in that attribute.</P
2075><P
2076>In order to combine a character with some attributes, you have two options:</P
2077><P
2078></P
2079><UL
2080><LI
2081><P
2082>By OR'ing a single character with the desired attribute macros. These attribute
2083macros could be found in the header file
2084<TT
2085CLASS="LITERAL"
2086>ncurses.h</TT
2087>. For example, you want to print a
2088character ch(of type char) bold and underlined, you would call addch() as below.
2089<PRE
2090CLASS="PROGRAMLISTING"
2091>    addch(ch | A_BOLD | A_UNDERLINE);</PRE
2092></P
2093></LI
2094><LI
2095><P
2096>By using functions like <TT
2097CLASS="LITERAL"
2098>attrset(),attron(),attroff()</TT
2099>. These functions are explained in the <A
2100HREF="#ATTRIB"
2101>Attributes</A
2102> section. Briefly, they manipulate the current attributes of
2103the given window. Once set, the character printed in the window are associated
2104with the attributes until it is turned off.</P
2105></LI
2106></UL
2107><P
2108>Additionally, <TT
2109CLASS="LITERAL"
2110>curses</TT
2111> provides some special
2112characters for character-based graphics. You can draw tables, horizontal or
2113vertical lines, etc. You can find all avaliable characters in the header file
2114<TT
2115CLASS="LITERAL"
2116>ncurses.h</TT
2117>.  Try looking for macros beginning
2118with <TT
2119CLASS="LITERAL"
2120>ACS_</TT
2121> in this file. </P
2122></DIV
2123><DIV
2124CLASS="SECT2"
2125><HR><H3
2126CLASS="SECT2"
2127><A
2128NAME="AEN298"
2129>6.2. mvaddch(), waddch() and mvwaddch()</A
2130></H3
2131><P
2132><TT
2133CLASS="LITERAL"
2134>mvaddch()</TT
2135> is used to move the cursor to a
2136given point, and then print. Thus, the calls:
2137<PRE
2138CLASS="PROGRAMLISTING"
2139>    move(row,col);    /* moves the cursor to row<SPAN
2140CLASS="emphasis"
2141><I
2142CLASS="EMPHASIS"
2143>th</I
2144></SPAN
2145> row and col<SPAN
2146CLASS="emphasis"
2147><I
2148CLASS="EMPHASIS"
2149>th</I
2150></SPAN
2151> column */
2152    addch(ch);</PRE
2153>
2154can be replaced by
2155<PRE
2156CLASS="PROGRAMLISTING"
2157>    mvaddch(row,col,ch);</PRE
2158></P
2159><P
2160><TT
2161CLASS="LITERAL"
2162>waddch()</TT
2163> is similar to
2164<TT
2165CLASS="LITERAL"
2166>addch()</TT
2167>, except that it adds a character into
2168the given window. (Note that <TT
2169CLASS="LITERAL"
2170>addch()</TT
2171> adds a
2172character into the window <TT
2173CLASS="LITERAL"
2174>stdscr</TT
2175>.)</P
2176><P
2177>In a similar fashion <TT
2178CLASS="LITERAL"
2179>mvwaddch()</TT
2180> function is
2181used to add a character into the given window at the given coordinates.</P
2182><P
2183>Now, we are familiar with the basic output function
2184<TT
2185CLASS="LITERAL"
2186>addch()</TT
2187>. But, if we want to print a string, it
2188would be very annoying to print it character by character. Fortunately,
2189<TT
2190CLASS="LITERAL"
2191>ncurses</TT
2192> provides <TT
2193CLASS="LITERAL"
2194>printf</TT
2195><SPAN
2196CLASS="emphasis"
2197><I
2198CLASS="EMPHASIS"
2199>-like</I
2200></SPAN
2201> or
2202<TT
2203CLASS="LITERAL"
2204>puts</TT
2205><SPAN
2206CLASS="emphasis"
2207><I
2208CLASS="EMPHASIS"
2209>-like</I
2210></SPAN
2211> functions.</P
2212></DIV
2213><DIV
2214CLASS="SECT2"
2215><HR><H3
2216CLASS="SECT2"
2217><A
2218NAME="PRINTWCLASS"
2219>6.3. printw() class of functions</A
2220></H3
2221><P
2222>These functions are similar to <TT
2223CLASS="LITERAL"
2224>printf()</TT
2225> with
2226the added capability of printing at any position on the screen. </P
2227><DIV
2228CLASS="SECT3"
2229><HR><H4
2230CLASS="SECT3"
2231><A
2232NAME="PRINTWMVPRINTW"
2233>6.3.1. printw() and mvprintw</A
2234></H4
2235><P
2236>These two functions work much like <TT
2237CLASS="LITERAL"
2238>printf()</TT
2239>.
2240<TT
2241CLASS="LITERAL"
2242>mvprintw()</TT
2243> can be used to move the cursor to a
2244position and then print. If you want to move the cursor first and then print
2245using <TT
2246CLASS="LITERAL"
2247>printw()</TT
2248> function, use
2249<TT
2250CLASS="LITERAL"
2251>move() </TT
2252> first and then use
2253<TT
2254CLASS="LITERAL"
2255>printw()</TT
2256> though I see no point why one should
2257avoid using <TT
2258CLASS="LITERAL"
2259>mvprintw()</TT
2260>, you have the
2261flexibility to manipulate. </P
2262></DIV
2263><DIV
2264CLASS="SECT3"
2265><HR><H4
2266CLASS="SECT3"
2267><A
2268NAME="WPRINTWMVWPRINTW"
2269>6.3.2. wprintw() and mvwprintw</A
2270></H4
2271><P
2272>These two functions are similar to above two except that they print in the
2273corresponding window given as argument. </P
2274></DIV
2275><DIV
2276CLASS="SECT3"
2277><HR><H4
2278CLASS="SECT3"
2279><A
2280NAME="VWPRINTW"
2281>6.3.3. vwprintw()</A
2282></H4
2283><P
2284>This function is similar to <TT
2285CLASS="LITERAL"
2286>vprintf()</TT
2287>. This can
2288be used when variable number of arguments are to be printed.</P
2289></DIV
2290><DIV
2291CLASS="SECT3"
2292><HR><H4
2293CLASS="SECT3"
2294><A
2295NAME="SIMPLEPRINTWEX"
2296>6.3.4. A Simple printw example</A
2297></H4
2298><DIV
2299CLASS="EXAMPLE"
2300><A
2301NAME="BPREX"
2302></A
2303><P
2304><B
2305>Example 3.  A Simple printw example </B
2306></P
2307><PRE
2308CLASS="PROGRAMLISTING"
2309><SPAN
2310CLASS="INLINEMEDIAOBJECT"
2311>#include &#60;ncurses.h&#62;			/* ncurses.h includes stdio.h */
2312#include &#60;string.h&#62;
2313
2314int main()
2315{
2316 char mesg[]="Just a string";		/* message to be appeared on the screen */
2317 int row,col;				/* to store the number of rows and *
2318					 * the number of colums of the screen */
2319 initscr();				/* start the curses mode */
2320 getmaxyx(stdscr,row,col);		/* get the number of rows and columns */
2321 mvprintw(row/2,(col-strlen(mesg))/2,"%s",mesg);
2322                                	/* print the message at the center of the screen */
2323 mvprintw(row-2,0,"This screen has %d rows and %d columns\n",row,col);
2324 printw("Try resizing your window(if possible) and then run this program again");
2325 refresh();
2326 getch();
2327 endwin();
2328
2329 return 0;
2330}</SPAN
2331></PRE
2332></DIV
2333><P
2334>Above program demonstrates how easy it is to use <TT
2335CLASS="LITERAL"
2336>printw</TT
2337>. You just feed the coordinates and the message to be appeared
2338on the screen, then it does what you want.</P
2339><P
2340>The above program introduces us to a new function
2341<TT
2342CLASS="LITERAL"
2343>getmaxyx()</TT
2344>, a macro defined in
2345<TT
2346CLASS="LITERAL"
2347>ncurses.h</TT
2348>. It gives the number of columns and
2349the number of rows in a given window.
2350<TT
2351CLASS="LITERAL"
2352>getmaxyx()</TT
2353> does this by updating the variables
2354given to it. Since <TT
2355CLASS="LITERAL"
2356>getmaxyx()</TT
2357> is not a function
2358we don't pass pointers to it, we just give two integer variables. </P
2359></DIV
2360></DIV
2361><DIV
2362CLASS="SECT2"
2363><HR><H3
2364CLASS="SECT2"
2365><A
2366NAME="ADDSTRCLASS"
2367>6.4. addstr() class of functions</A
2368></H3
2369><P
2370><TT
2371CLASS="LITERAL"
2372>addstr()</TT
2373> is used to put a character string into
2374a given window. This function is similar to calling
2375<TT
2376CLASS="LITERAL"
2377>addch()</TT
2378> once for each character in a given
2379string. This is true for all output functions. There are other functions from
2380this family such as <TT
2381CLASS="LITERAL"
2382>mvaddstr(),mvwaddstr()</TT
2383> and
2384<TT
2385CLASS="LITERAL"
2386>waddstr()</TT
2387>, which obey the naming convention of
2388curses.(e.g. mvaddstr() is similar to the respective calls move() and then
2389addstr().) Another function of this family is addnstr(), which takes an integer
2390parameter(say n) additionally. This function puts at most n characters into the
2391screen. If n is negative, then the entire string will be added. </P
2392></DIV
2393><DIV
2394CLASS="SECT2"
2395><HR><H3
2396CLASS="SECT2"
2397><A
2398NAME="ACAUTION"
2399>6.5. A word of caution</A
2400></H3
2401><P
2402>All these functions take y co-ordinate first and then x in their arguments.
2403A common mistake by beginners is to pass x,y in that order. If you are
2404doing too many manipulations of (y,x) co-ordinates, think of dividing the
2405screen into windows and manipulate each one separately. Windows are explained
2406in the <A
2407HREF="#WINDOWS"
2408> windows </A
2409> section.</P
2410></DIV
2411></DIV
2412><DIV
2413CLASS="SECT1"
2414><HR><H2
2415CLASS="SECT1"
2416><A
2417NAME="SCANW"
2418>7. Input functions</A
2419></H2
2420><P
2421>Well, printing without taking input, is boring. Let's see functions which
2422allow us to get input from user. These functions also can be divided into
2423three categories.</P
2424><P
2425></P
2426><OL
2427TYPE="1"
2428><LI
2429><P
2430>getch() class: Get a character</P
2431></LI
2432><LI
2433><P
2434>scanw() class: Get formatted input</P
2435></LI
2436><LI
2437><P
2438>getstr() class: Get strings</P
2439></LI
2440></OL
2441><DIV
2442CLASS="SECT2"
2443><HR><H3
2444CLASS="SECT2"
2445><A
2446NAME="GETCHCLASS"
2447>7.1. getch() class of functions</A
2448></H3
2449><P
2450>These functions read a single character from the terminal. But there are several
2451subtle facts to consider. For example if you don't use the function cbreak(),
2452curses will not read your input characters contiguously but will begin read them
2453only after a new line or an EOF is encountered. In order to avoid this, the
2454cbreak() function must used so that characters are immediately available to your
2455program. Another widely used function is noecho(). As the name suggests, when
2456this function is set (used), the characters that are keyed in by the user will
2457not show up on the screen. The two functions cbreak() and noecho() are typical
2458examples of key management.  Functions of this genre are explained in the
2459<A
2460HREF="#KEYS"
2461>key management section </A
2462>.</P
2463></DIV
2464><DIV
2465CLASS="SECT2"
2466><HR><H3
2467CLASS="SECT2"
2468><A
2469NAME="SCANWCLASS"
2470>7.2. scanw() class of functions</A
2471></H3
2472><P
2473>These functions are similar to <TT
2474CLASS="LITERAL"
2475>scanf()</TT
2476> with the
2477added capability of getting the input from any location on the screen.</P
2478><DIV
2479CLASS="SECT3"
2480><HR><H4
2481CLASS="SECT3"
2482><A
2483NAME="SCANWMVSCANW"
2484>7.2.1. scanw() and mvscanw</A
2485></H4
2486><P
2487>The usage of these functions is similar to that of
2488<TT
2489CLASS="LITERAL"
2490>sscanf()</TT
2491>, where the line to be scanned is
2492provided by <TT
2493CLASS="LITERAL"
2494>wgetstr()</TT
2495> function. That is, these
2496functions call to <TT
2497CLASS="LITERAL"
2498>wgetstr()</TT
2499> function(explained
2500below) and uses the resulting line for a scan. </P
2501></DIV
2502><DIV
2503CLASS="SECT3"
2504><HR><H4
2505CLASS="SECT3"
2506><A
2507NAME="WSCANWMVWSCANW"
2508>7.2.2. wscanw() and mvwscanw()</A
2509></H4
2510><P
2511>These are similar to above two functions except that they read from a window,
2512which is supplied as one of the arguments to these functions. </P
2513></DIV
2514><DIV
2515CLASS="SECT3"
2516><HR><H4
2517CLASS="SECT3"
2518><A
2519NAME="VWSCANW"
2520>7.2.3. vwscanw()</A
2521></H4
2522><P
2523>This function is similar to <TT
2524CLASS="LITERAL"
2525>vscanf()</TT
2526>. This can
2527be used when a variable number of arguments are to be scanned.</P
2528></DIV
2529></DIV
2530><DIV
2531CLASS="SECT2"
2532><HR><H3
2533CLASS="SECT2"
2534><A
2535NAME="GETSTRCLASS"
2536>7.3. getstr() class of functions</A
2537></H3
2538><P
2539>These functions are used to get strings from the terminal. In essence, this
2540function performs the same task as would be achieved by a series of calls to
2541<TT
2542CLASS="LITERAL"
2543>getch()</TT
2544> until a newline, carriage return, or
2545end-of-file is received. The resulting string of characters are pointed to by
2546<TT
2547CLASS="LITERAL"
2548>str</TT
2549>, which is a character pointer provided by
2550the user.</P
2551></DIV
2552><DIV
2553CLASS="SECT2"
2554><HR><H3
2555CLASS="SECT2"
2556><A
2557NAME="GETSTREX"
2558>7.4. Some examples</A
2559></H3
2560><DIV
2561CLASS="EXAMPLE"
2562><A
2563NAME="BSCEX"
2564></A
2565><P
2566><B
2567>Example 4.  A Simple scanw example </B
2568></P
2569><PRE
2570CLASS="PROGRAMLISTING"
2571><SPAN
2572CLASS="INLINEMEDIAOBJECT"
2573>#include &#60;ncurses.h&#62;			/* ncurses.h includes stdio.h */
2574#include &#60;string.h&#62;
2575
2576int main()
2577{
2578 char mesg[]="Enter a string: ";		/* message to be appeared on the screen */
2579 char str[80];
2580 int row,col;				/* to store the number of rows and *
2581					 * the number of colums of the screen */
2582 initscr();				/* start the curses mode */
2583 getmaxyx(stdscr,row,col);		/* get the number of rows and columns */
2584 mvprintw(row/2,(col-strlen(mesg))/2,"%s",mesg);
2585                     		/* print the message at the center of the screen */
2586 getstr(str);
2587 mvprintw(LINES - 2, 0, "You Entered: %s", str);
2588 getch();
2589 endwin();
2590
2591 return 0;
2592}</SPAN
2593></PRE
2594></DIV
2595></DIV
2596></DIV
2597><DIV
2598CLASS="SECT1"
2599><HR><H2
2600CLASS="SECT1"
2601><A
2602NAME="ATTRIB"
2603>8. Attributes</A
2604></H2
2605><P
2606>We have seen an example of how attributes can be used to print characters with
2607some special effects. Attributes, when set prudently, can present information in
2608an easy, understandable manner. The following program takes a C file as input
2609and prints the file with comments in bold. Scan through the code. </P
2610><DIV
2611CLASS="EXAMPLE"
2612><A
2613NAME="BSIAT"
2614></A
2615><P
2616><B
2617>Example 5.  A Simple Attributes example </B
2618></P
2619><PRE
2620CLASS="PROGRAMLISTING"
2621><SPAN
2622CLASS="INLINEMEDIAOBJECT"
2623>/* pager functionality by Joseph Spainhour" &#60;spainhou@bellsouth.net&#62; */
2624#include &#60;ncurses.h&#62;
2625#include &#60;stdlib.h&#62;
2626
2627int main(int argc, char *argv[])
2628{
2629  int ch, prev, row, col;
2630  prev = EOF;
2631  FILE *fp;
2632  int y, x;
2633
2634  if(argc != 2)
2635  {
2636    printf("Usage: %s &#60;a c file name&#62;\n", argv[0]);
2637    exit(1);
2638  }
2639  fp = fopen(argv[1], "r");
2640  if(fp == NULL)
2641  {
2642    perror("Cannot open input file");
2643    exit(1);
2644  }
2645  initscr();				/* Start curses mode */
2646  getmaxyx(stdscr, row, col);		/* find the boundaries of the screeen */
2647  while((ch = fgetc(fp)) != EOF)	/* read the file till we reach the end */
2648  {
2649    getyx(stdscr, y, x);		/* get the current curser position */
2650    if(y == (row - 1))			/* are we are at the end of the screen */
2651    {
2652      printw("&#60;-Press Any Key-&#62;");	/* tell the user to press a key */
2653      getch();
2654      clear();				/* clear the screen */
2655      move(0, 0);			/* start at the beginning of the screen */
2656    }
2657    if(prev == '/' &#38;&#38; ch == '*')    	/* If it is / and * then only
2658                                     	 * switch bold on */
2659    {
2660      attron(A_BOLD);			/* cut bold on */
2661      getyx(stdscr, y, x);		/* get the current curser position */
2662      move(y, x - 1);			/* back up one space */
2663      printw("%c%c", '/', ch); 		/* The actual printing is done here */
2664    }
2665    else
2666      printw("%c", ch);
2667    refresh();
2668    if(prev == '*' &#38;&#38; ch == '/')
2669      attroff(A_BOLD);        		/* Switch it off once we got *
2670                                 	 * and then / */
2671    prev = ch;
2672  }
2673  endwin();                       	/* End curses mode */
2674  fclose(fp);
2675  return 0;
2676}</SPAN
2677></PRE
2678></DIV
2679><P
2680>
2681Don't worry about all those initialization and other crap. Concentrate on
2682the while loop. It reads each character in the file and searches for the
2683pattern /*. Once it spots the pattern, it switches the BOLD attribute on with
2684<TT
2685CLASS="LITERAL"
2686> attron()</TT
2687> . When we get the pattern */ it is
2688switched off by <TT
2689CLASS="LITERAL"
2690> attroff()</TT
2691> .</P
2692><P
2693>
2694The above program also introduces us to two useful functions
2695<TT
2696CLASS="LITERAL"
2697>getyx() </TT
2698> and
2699<TT
2700CLASS="LITERAL"
2701>move()</TT
2702>. The first function gets the
2703co-ordinates of the present cursor into the variables y, x. Since getyx() is a
2704macro we don't have to pass pointers to variables. The function
2705<TT
2706CLASS="LITERAL"
2707>move()</TT
2708> moves the cursor to the co-ordinates
2709given to it. </P
2710><P
2711>
2712The above program is really a simple one which doesn't do much. On these lines
2713one could write a more useful program which reads a C file, parses it and prints
2714it in different colors. One could even extend it to other languages as well.</P
2715><DIV
2716CLASS="SECT2"
2717><HR><H3
2718CLASS="SECT2"
2719><A
2720NAME="ATTRIBDETAILS"
2721>8.1. The details</A
2722></H3
2723><P
2724>Let's get into more details of attributes. The functions <TT
2725CLASS="LITERAL"
2726>attron(), attroff(), attrset() </TT
2727>, and their sister functions
2728<TT
2729CLASS="LITERAL"
2730> attr_get()</TT
2731> etc..  can be used to switch
2732attributes on/off , get attributes and produce a colorful display.</P
2733><P
2734>The functions attron and attroff take a bit-mask of attributes and switch them
2735on or off, respectively. The following video attributes, which are defined in
2736&lt;curses.h&gt; can be passed to these functions. </P
2737><PRE
2738CLASS="PROGRAMLISTING"
2739>
2740    A_NORMAL        Normal display (no highlight)
2741    A_STANDOUT      Best highlighting mode of the terminal.
2742    A_UNDERLINE     Underlining
2743    A_REVERSE       Reverse video
2744    A_BLINK         Blinking
2745    A_DIM           Half bright
2746    A_BOLD          Extra bright or bold
2747    A_PROTECT       Protected mode
2748    A_INVIS         Invisible or blank mode
2749    A_ALTCHARSET    Alternate character set
2750    A_CHARTEXT      Bit-mask to extract a character
2751    COLOR_PAIR(n)   Color-pair number n
2752    </PRE
2753><P
2754>
2755The last one is the most colorful one :-) Colors are explained in the
2756<A
2757HREF="#color"
2758TARGET="_top"
2759>next sections</A
2760>.</P
2761><P
2762>We can OR(|) any number of above attributes to get a combined effect. If you
2763wanted reverse video with blinking characters you can use</P
2764><PRE
2765CLASS="PROGRAMLISTING"
2766>    attron(A_REVERSE | A_BLINK);</PRE
2767></DIV
2768><DIV
2769CLASS="SECT2"
2770><HR><H3
2771CLASS="SECT2"
2772><A
2773NAME="ATTRONVSATTRSET"
2774>8.2. attron() vs attrset()</A
2775></H3
2776><P
2777>Then what is the difference between attron() and attrset()? attrset sets the
2778attributes of window whereas attron just switches on the attribute given to it.
2779So attrset() fully overrides whatever attributes the window previously had and
2780sets it to the new attribute(s). Similarly attroff() just switches off the
2781attribute(s) given to it as an argument. This gives us the flexibility of
2782managing attributes easily.But if you use them carelessly you may loose track of
2783what attributes the window has and garble the display. This is especially true
2784while managing menus with colors and highlighting. So decide on a consistent
2785policy and stick to it. You can always use <TT
2786CLASS="LITERAL"
2787> standend()</TT
2788> which is equivalent to <TT
2789CLASS="LITERAL"
2790> attrset(A_NORMAL)</TT
2791> which turns off all attributes and brings you to normal mode.</P
2792></DIV
2793><DIV
2794CLASS="SECT2"
2795><HR><H3
2796CLASS="SECT2"
2797><A
2798NAME="ATTR_GET"
2799>8.3. attr_get()</A
2800></H3
2801><P
2802>&#13;The function attr_get() gets the current attributes and color pair of the
2803window. Though we might not use this as often as the above functions, this is
2804useful in scanning areas of screen. Say we wanted to do some complex update on
2805screen and we are not sure what attribute each character is associated with.
2806Then this function can be used with either attrset or attron to produce the
2807desired effect.&#13;</P
2808></DIV
2809><DIV
2810CLASS="SECT2"
2811><HR><H3
2812CLASS="SECT2"
2813><A
2814NAME="ATTR_FUNCS"
2815>8.4. attr_ functions</A
2816></H3
2817><P
2818>There are series of functions like attr_set(), attr_on etc.. These are similar
2819to above functions except that they take parameters of type
2820<TT
2821CLASS="LITERAL"
2822>attr_t</TT
2823>.</P
2824></DIV
2825><DIV
2826CLASS="SECT2"
2827><HR><H3
2828CLASS="SECT2"
2829><A
2830NAME="WATTRFUNCS"
2831>8.5. wattr functions</A
2832></H3
2833><P
2834>For each of the above functions we have a corresponding function with 'w' which
2835operates on a particular window. The above functions operate on stdscr. </P
2836></DIV
2837><DIV
2838CLASS="SECT2"
2839><HR><H3
2840CLASS="SECT2"
2841><A
2842NAME="CHGAT"
2843>8.6. chgat() functions</A
2844></H3
2845><P
2846>The function chgat() is listed in the end of the man page curs_attr. It actually
2847is a useful one. This function can be used to set attributes for a group of
2848characters without moving. I mean it !!! without moving the cursor :-) It
2849changes the attributes of a given number of characters starting at the current
2850cursor location.</P
2851><P
2852>We can give -1 as the character count to update till end of line. If you want to
2853change attributes of characters from current position to end of line, just use
2854this.</P
2855><PRE
2856CLASS="PROGRAMLISTING"
2857>    chgat(-1, A_REVERSE, 0, NULL);</PRE
2858><P
2859>
2860This function is useful when changing attributes for characters that are
2861already on the screen. Move to the character from which you want to change and
2862change the attribute. </P
2863><P
2864>Other functions wchgat(), mvchgat(), wchgat() behave similarly except that the w
2865functions operate on the particular window. The mv functions first move the
2866cursor then perform the work given to them. Actually chgat is a macro which is
2867replaced by a wchgat() with stdscr as the window. Most of the "w-less" functions
2868are macros.</P
2869><DIV
2870CLASS="EXAMPLE"
2871><A
2872NAME="BWICH"
2873></A
2874><P
2875><B
2876>Example 6.  Chgat() Usage example </B
2877></P
2878><PRE
2879CLASS="PROGRAMLISTING"
2880><SPAN
2881CLASS="INLINEMEDIAOBJECT"
2882>#include &#60;ncurses.h&#62;
2883
2884int main(int argc, char *argv[])
2885{	initscr();			/* Start curses mode 		*/
2886	start_color();			/* Start color functionality	*/
2887
2888	init_pair(1, COLOR_CYAN, COLOR_BLACK);
2889	printw("A Big string which i didn't care to type fully ");
2890	mvchgat(0, 0, -1, A_BLINK, 1, NULL);
2891	/*
2892	 * First two parameters specify the position at which to start
2893	 * Third parameter number of characters to update. -1 means till
2894	 * end of line
2895	 * Forth parameter is the normal attribute you wanted to give
2896	 * to the charcter
2897	 * Fifth is the color index. It is the index given during init_pair()
2898	 * use 0 if you didn't want color
2899	 * Sixth one is always NULL
2900	 */
2901	refresh();
2902    	getch();
2903	endwin();			/* End curses mode		  */
2904	return 0;
2905}</SPAN
2906></PRE
2907></DIV
2908><P
2909>This example also introduces us to the color world of curses. Colors will be
2910explained in detail later. Use 0 for no color.</P
2911></DIV
2912></DIV
2913><DIV
2914CLASS="SECT1"
2915><HR><H2
2916CLASS="SECT1"
2917><A
2918NAME="WINDOWS"
2919>9. Windows</A
2920></H2
2921><P
2922>Windows form the most important concept in curses. You have seen the standard
2923window stdscr above where all the functions implicitly operated on this window.
2924Now to make design even a simplest GUI, you need to resort to windows.  The main
2925reason you may want to use windows is to manipulate parts of the screen
2926separately, for better efficiency, by updating only the windows that need to be
2927changed and for a better design. I would say the last reason is the most
2928important in going for windows. You should always strive for a better and
2929easy-to-manage design in your programs. If you are writing big, complex GUIs
2930this is of pivotal importance before you start doing anything.</P
2931><DIV
2932CLASS="SECT2"
2933><HR><H3
2934CLASS="SECT2"
2935><A
2936NAME="WINDOWBASICS"
2937>9.1. The basics</A
2938></H3
2939><P
2940>A Window can be created by calling the function
2941<TT
2942CLASS="LITERAL"
2943>newwin()</TT
2944>. It doesn't create any thing on the
2945screen actually. It allocates memory for a structure to manipulate the window
2946and updates the structure with data regarding the window like it's size, beginy,
2947beginx etc.. Hence in curses, a window is just an abstraction of an imaginary
2948window, which can be manipulated independent of other parts of screen. The
2949function newwin() returns a pointer to structure WINDOW, which can be passed to
2950window related functions like wprintw() etc.. Finally the window can be
2951destroyed with delwin(). It will deallocate the memory associated with the
2952window structure.</P
2953></DIV
2954><DIV
2955CLASS="SECT2"
2956><HR><H3
2957CLASS="SECT2"
2958><A
2959NAME="LETBEWINDOW"
2960>9.2. Let there be a Window !!!</A
2961></H3
2962><P
2963>What fun is it, if a window is created and we can't see it. So the fun part
2964begins by displaying the window. The function
2965<TT
2966CLASS="LITERAL"
2967>box()</TT
2968> can be used to draw a border around the
2969window. Let's explore these functions in more detail in this example.</P
2970><DIV
2971CLASS="EXAMPLE"
2972><A
2973NAME="BWIBO"
2974></A
2975><P
2976><B
2977>Example 7. Window Border example </B
2978></P
2979><PRE
2980CLASS="PROGRAMLISTING"
2981><SPAN
2982CLASS="INLINEMEDIAOBJECT"
2983>#include &#60;ncurses.h&#62;
2984
2985
2986WINDOW *create_newwin(int height, int width, int starty, int startx);
2987void destroy_win(WINDOW *local_win);
2988
2989int main(int argc, char *argv[])
2990{	WINDOW *my_win;
2991	int startx, starty, width, height;
2992	int ch;
2993
2994	initscr();			/* Start curses mode 		*/
2995	cbreak();			/* Line buffering disabled, Pass on
2996					 * everty thing to me 		*/
2997	keypad(stdscr, TRUE);		/* I need that nifty F1 	*/
2998
2999	height = 3;
3000	width = 10;
3001	starty = (LINES - height) / 2;	/* Calculating for a center placement */
3002	startx = (COLS - width) / 2;	/* of the window		*/
3003	printw("Press F1 to exit");
3004	refresh();
3005	my_win = create_newwin(height, width, starty, startx);
3006
3007	while((ch = getch()) != KEY_F(1))
3008	{	switch(ch)
3009		{	case KEY_LEFT:
3010				destroy_win(my_win);
3011				my_win = create_newwin(height, width, starty,--startx);
3012				break;
3013			case KEY_RIGHT:
3014				destroy_win(my_win);
3015				my_win = create_newwin(height, width, starty,++startx);
3016				break;
3017			case KEY_UP:
3018				destroy_win(my_win);
3019				my_win = create_newwin(height, width, --starty,startx);
3020				break;
3021			case KEY_DOWN:
3022				destroy_win(my_win);
3023				my_win = create_newwin(height, width, ++starty,startx);
3024				break;
3025		}
3026	}
3027
3028	endwin();			/* End curses mode		  */
3029	return 0;
3030}
3031
3032WINDOW *create_newwin(int height, int width, int starty, int startx)
3033{	WINDOW *local_win;
3034
3035	local_win = newwin(height, width, starty, startx);
3036	box(local_win, 0 , 0);		/* 0, 0 gives default characters
3037					 * for the vertical and horizontal
3038					 * lines			*/
3039	wrefresh(local_win);		/* Show that box 		*/
3040
3041	return local_win;
3042}
3043
3044void destroy_win(WINDOW *local_win)
3045{
3046	/* box(local_win, ' ', ' '); : This won't produce the desired
3047	 * result of erasing the window. It will leave it's four corners
3048	 * and so an ugly remnant of window.
3049	 */
3050	wborder(local_win, ' ', ' ', ' ',' ',' ',' ',' ',' ');
3051	/* The parameters taken are
3052	 * 1. win: the window on which to operate
3053	 * 2. ls: character to be used for the left side of the window
3054	 * 3. rs: character to be used for the right side of the window
3055	 * 4. ts: character to be used for the top side of the window
3056	 * 5. bs: character to be used for the bottom side of the window
3057	 * 6. tl: character to be used for the top left corner of the window
3058	 * 7. tr: character to be used for the top right corner of the window
3059	 * 8. bl: character to be used for the bottom left corner of the window
3060	 * 9. br: character to be used for the bottom right corner of the window
3061	 */
3062	wrefresh(local_win);
3063	delwin(local_win);
3064}</SPAN
3065></PRE
3066></DIV
3067></DIV
3068><DIV
3069CLASS="SECT2"
3070><HR><H3
3071CLASS="SECT2"
3072><A
3073NAME="BORDEREXEXPL"
3074>9.3. Explanation</A
3075></H3
3076><P
3077>Don't scream. I know it's a big example. But I have to explain some important
3078things here :-). This program creates a rectangular window that can be moved
3079with left, right, up, down arrow keys. It repeatedly creates and destroys
3080windows as user press a key. Don't go beyond the screen limits. Checking for
3081those limits is left as an exercise for the reader. Let's dissect it by line by line.</P
3082><P
3083>The <TT
3084CLASS="LITERAL"
3085>create_newwin()</TT
3086> function creates a window
3087with <TT
3088CLASS="LITERAL"
3089>newwin() </TT
3090> and displays a border around it
3091with box. The function <TT
3092CLASS="LITERAL"
3093> destroy_win()</TT
3094> first
3095erases the window from screen by painting a border with ' ' character and then
3096calling <TT
3097CLASS="LITERAL"
3098>delwin()</TT
3099> to deallocate memory related
3100to it. Depending on the key the user presses, starty or startx is changed and a
3101new window is created.</P
3102><P
3103>In the destroy_win, as you can see, I used wborder instead of box. The reason is
3104written in the comments (You missed it. I know. Read the code :-)). wborder
3105draws a border around the window with the characters given to it as the 4 corner
3106points and the 4 lines. To put it clearly, if you have called wborder as below:
3107<PRE
3108CLASS="PROGRAMLISTING"
3109>    wborder(win, '|', '|', '-', '-', '+', '+', '+', '+');</PRE
3110></P
3111><P
3112>it produces some thing like </P
3113><PRE
3114CLASS="PROGRAMLISTING"
3115>    +------------+
3116    |            |
3117    |            |
3118    |            |
3119    |            |
3120    |            |
3121    |            |
3122    +------------+</PRE
3123></DIV
3124><DIV
3125CLASS="SECT2"
3126><HR><H3
3127CLASS="SECT2"
3128><A
3129NAME="OTHERSTUFF"
3130>9.4. The other stuff in the example</A
3131></H3
3132><P
3133>You can also see in the above examples, that I have used the variables COLS,
3134LINES which are initialized to the screen sizes after initscr(). They can be
3135useful in finding screen dimensions and finding the center co-ordinate of the
3136screen as above. The function <TT
3137CLASS="LITERAL"
3138>getch()</TT
3139> as usual
3140gets the key from keyboard and according to the key it does the corresponding
3141work. This type of switch- case is very common in any GUI based programs.</P
3142></DIV
3143><DIV
3144CLASS="SECT2"
3145><HR><H3
3146CLASS="SECT2"
3147><A
3148NAME="OTHERBORDERFUNCS"
3149>9.5. Other Border functions</A
3150></H3
3151><P
3152>Above program is grossly inefficient in that with each press of a key, a window
3153is destroyed and another is created. So let's write a more efficient program
3154which uses other border related functions.</P
3155><P
3156>The following program uses <TT
3157CLASS="LITERAL"
3158>mvhline()</TT
3159> and
3160<TT
3161CLASS="LITERAL"
3162>mvvline()</TT
3163> to achieve similar effect. These two
3164functions are simple. They create a horizontal or vertical line of the specified
3165length at the specified position.</P
3166><DIV
3167CLASS="EXAMPLE"
3168><A
3169NAME="BOTBO"
3170></A
3171><P
3172><B
3173>Example 8.  More border functions</B
3174></P
3175><PRE
3176CLASS="PROGRAMLISTING"
3177><SPAN
3178CLASS="INLINEMEDIAOBJECT"
3179>#include &#60;ncurses.h&#62;
3180
3181typedef struct _win_border_struct {
3182	chtype 	ls, rs, ts, bs,
3183	 	tl, tr, bl, br;
3184}WIN_BORDER;
3185
3186typedef struct _WIN_struct {
3187
3188	int startx, starty;
3189	int height, width;
3190	WIN_BORDER border;
3191}WIN;
3192
3193void init_win_params(WIN *p_win);
3194void print_win_params(WIN *p_win);
3195void create_box(WIN *win, bool flag);
3196
3197int main(int argc, char *argv[])
3198{	WIN win;
3199	int ch;
3200
3201	initscr();			/* Start curses mode 		*/
3202	start_color();			/* Start the color functionality */
3203	cbreak();			/* Line buffering disabled, Pass on
3204					 * everty thing to me 		*/
3205	keypad(stdscr, TRUE);		/* I need that nifty F1 	*/
3206	noecho();
3207	init_pair(1, COLOR_CYAN, COLOR_BLACK);
3208
3209	/* Initialize the window parameters */
3210	init_win_params(&#38;win);
3211	print_win_params(&#38;win);
3212
3213	attron(COLOR_PAIR(1));
3214	printw("Press F1 to exit");
3215	refresh();
3216	attroff(COLOR_PAIR(1));
3217
3218	create_box(&#38;win, TRUE);
3219	while((ch = getch()) != KEY_F(1))
3220	{	switch(ch)
3221		{	case KEY_LEFT:
3222				create_box(&#38;win, FALSE);
3223				--win.startx;
3224				create_box(&#38;win, TRUE);
3225				break;
3226			case KEY_RIGHT:
3227				create_box(&#38;win, FALSE);
3228				++win.startx;
3229				create_box(&#38;win, TRUE);
3230				break;
3231			case KEY_UP:
3232				create_box(&#38;win, FALSE);
3233				--win.starty;
3234				create_box(&#38;win, TRUE);
3235				break;
3236			case KEY_DOWN:
3237				create_box(&#38;win, FALSE);
3238				++win.starty;
3239				create_box(&#38;win, TRUE);
3240				break;
3241		}
3242	}
3243	endwin();			/* End curses mode		  */
3244	return 0;
3245}
3246void init_win_params(WIN *p_win)
3247{
3248	p_win-&#62;height = 3;
3249	p_win-&#62;width = 10;
3250	p_win-&#62;starty = (LINES - p_win-&#62;height)/2;
3251	p_win-&#62;startx = (COLS - p_win-&#62;width)/2;
3252
3253	p_win-&#62;border.ls = '|';
3254	p_win-&#62;border.rs = '|';
3255	p_win-&#62;border.ts = '-';
3256	p_win-&#62;border.bs = '-';
3257	p_win-&#62;border.tl = '+';
3258	p_win-&#62;border.tr = '+';
3259	p_win-&#62;border.bl = '+';
3260	p_win-&#62;border.br = '+';
3261
3262}
3263void print_win_params(WIN *p_win)
3264{
3265#ifdef _DEBUG
3266	mvprintw(25, 0, "%d %d %d %d", p_win-&#62;startx, p_win-&#62;starty,
3267				p_win-&#62;width, p_win-&#62;height);
3268	refresh();
3269#endif
3270}
3271void create_box(WIN *p_win, bool flag)
3272{	int i, j;
3273	int x, y, w, h;
3274
3275	x = p_win-&#62;startx;
3276	y = p_win-&#62;starty;
3277	w = p_win-&#62;width;
3278	h = p_win-&#62;height;
3279
3280	if(flag == TRUE)
3281	{	mvaddch(y, x, p_win-&#62;border.tl);
3282		mvaddch(y, x + w, p_win-&#62;border.tr);
3283		mvaddch(y + h, x, p_win-&#62;border.bl);
3284		mvaddch(y + h, x + w, p_win-&#62;border.br);
3285		mvhline(y, x + 1, p_win-&#62;border.ts, w - 1);
3286		mvhline(y + h, x + 1, p_win-&#62;border.bs, w - 1);
3287		mvvline(y + 1, x, p_win-&#62;border.ls, h - 1);
3288		mvvline(y + 1, x + w, p_win-&#62;border.rs, h - 1);
3289
3290	}
3291	else
3292		for(j = y; j &#60;= y + h; ++j)
3293			for(i = x; i &#60;= x + w; ++i)
3294				mvaddch(j, i, ' ');
3295
3296	refresh();
3297
3298}</SPAN
3299></PRE
3300></DIV
3301></DIV
3302></DIV
3303><DIV
3304CLASS="SECT1"
3305><HR><H2
3306CLASS="SECT1"
3307><A
3308NAME="COLOR"
3309>10. Colors</A
3310></H2
3311><DIV
3312CLASS="SECT2"
3313><H3
3314CLASS="SECT2"
3315><A
3316NAME="COLORBASICS"
3317>10.1. The basics</A
3318></H3
3319><P
3320>Life seems dull with no colors. Curses has a nice mechanism to handle colors.
3321Let's get into the thick of the things with a small program.</P
3322><DIV
3323CLASS="EXAMPLE"
3324><A
3325NAME="BSICO"
3326></A
3327><P
3328><B
3329>Example 9.  A Simple Color example </B
3330></P
3331><PRE
3332CLASS="PROGRAMLISTING"
3333><SPAN
3334CLASS="INLINEMEDIAOBJECT"
3335>#include &#60;ncurses.h&#62;
3336
3337void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string);
3338int main(int argc, char *argv[])
3339{	initscr();			/* Start curses mode 		*/
3340	if(has_colors() == FALSE)
3341	{	endwin();
3342		printf("Your terminal does not support color\n");
3343		exit(1);
3344	}
3345	start_color();			/* Start color 			*/
3346	init_pair(1, COLOR_RED, COLOR_BLACK);
3347
3348	attron(COLOR_PAIR(1));
3349	print_in_middle(stdscr, LINES / 2, 0, 0, "Viola !!! In color ...");
3350	attroff(COLOR_PAIR(1));
3351    	getch();
3352	endwin();
3353}
3354void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string)
3355{	int length, x, y;
3356	float temp;
3357
3358	if(win == NULL)
3359		win = stdscr;
3360	getyx(win, y, x);
3361	if(startx != 0)
3362		x = startx;
3363	if(starty != 0)
3364		y = starty;
3365	if(width == 0)
3366		width = 80;
3367
3368	length = strlen(string);
3369	temp = (width - length)/ 2;
3370	x = startx + (int)temp;
3371	mvwprintw(win, y, x, "%s", string);
3372	refresh();
3373}
3374</SPAN
3375></PRE
3376></DIV
3377><P
3378>As you can see, to start using color, you should first call the function
3379<TT
3380CLASS="LITERAL"
3381> start_color()</TT
3382>. After that, you can use color
3383capabilities of your terminals using various functions. To find out whether a
3384terminal has color capabilities or not, you can use
3385<TT
3386CLASS="LITERAL"
3387>has_colors()</TT
3388> function, which returns FALSE if
3389the terminal does not support color. </P
3390><P
3391>Curses initializes all the colors supported by terminal when start_color() is
3392called. These can be accessed by the define constants like
3393<TT
3394CLASS="LITERAL"
3395>COLOR_BLACK </TT
3396> etc. Now to actually start using
3397colors, you have to define pairs. Colors are always used in pairs. That means
3398you have to use the function <TT
3399CLASS="LITERAL"
3400>init_pair() </TT
3401> to
3402define the foreground and background for the pair number you give.  After that
3403that pair number can be used as a normal attribute with <TT
3404CLASS="LITERAL"
3405>COLOR_PAIR()</TT
3406>function. This may seem to be cumbersome at first.
3407But this elegant solution allows us to manage color pairs very easily. To
3408appreciate it, you have to look into the the source code of "dialog", a utility
3409for displaying dialog boxes from shell scripts. The developers have defined
3410foreground and background combinations for all the colors they might need and
3411initialized at the beginning. This makes it very easy to set attributes just by
3412accessing a pair which we already have defined as a constant.</P
3413><P
3414>The following colors are defined in <TT
3415CLASS="LITERAL"
3416>curses.h</TT
3417>.
3418You can use these as parameters for various color functions.
3419<PRE
3420CLASS="PROGRAMLISTING"
3421>        COLOR_BLACK   0
3422        COLOR_RED     1
3423        COLOR_GREEN   2
3424        COLOR_YELLOW  3
3425        COLOR_BLUE    4
3426        COLOR_MAGENTA 5
3427        COLOR_CYAN    6
3428        COLOR_WHITE   7</PRE
3429></P
3430></DIV
3431><DIV
3432CLASS="SECT2"
3433><HR><H3
3434CLASS="SECT2"
3435><A
3436NAME="CHANGECOLORDEFS"
3437>10.2. Changing Color Definitions</A
3438></H3
3439><P
3440>The function <TT
3441CLASS="LITERAL"
3442>init_color()</TT
3443>can be used to change
3444the rgb values for the colors defined by curses initially. Say you wanted to
3445lighten the intensity of red color by a minuscule. Then you can use this
3446function as</P
3447><PRE
3448CLASS="PROGRAMLISTING"
3449>    init_color(COLOR_RED, 700, 0, 0);
3450    /* param 1     : color name
3451     * param 2, 3, 4 : rgb content min = 0, max = 1000 */</PRE
3452><P
3453>If your terminal cannot change the color definitions, the function returns ERR.
3454The function <TT
3455CLASS="LITERAL"
3456>can_change_color()</TT
3457> can be used to
3458find out whether the terminal has the capability of changing color content or
3459not.  The rgb content is scaled from 0 to 1000. Initially RED color is defined
3460with content 1000(r), 0(g), 0(b). </P
3461></DIV
3462><DIV
3463CLASS="SECT2"
3464><HR><H3
3465CLASS="SECT2"
3466><A
3467NAME="COLORCONTENT"
3468>10.3. Color Content</A
3469></H3
3470><P
3471>The functions <TT
3472CLASS="LITERAL"
3473>color_content()</TT
3474> and
3475<TT
3476CLASS="LITERAL"
3477>pair_content()</TT
3478> can be used to find the color
3479content and foreground, background combination for the pair. </P
3480></DIV
3481></DIV
3482><DIV
3483CLASS="SECT1"
3484><HR><H2
3485CLASS="SECT1"
3486><A
3487NAME="KEYS"
3488>11. Interfacing with the key board</A
3489></H2
3490><DIV
3491CLASS="SECT2"
3492><H3
3493CLASS="SECT2"
3494><A
3495NAME="KEYSBASICS"
3496>11.1. The Basics</A
3497></H3
3498><P
3499>No GUI is complete without a strong user interface and to interact with the
3500user, a curses program should be sensitive to key presses or the mouse actions
3501done by the user. Let's deal with the keys first.</P
3502><P
3503>As you have seen in almost all of the above examples, it's very easy to get key
3504input from the user. A simple way of getting key presses is to use
3505<TT
3506CLASS="LITERAL"
3507>getch()</TT
3508> function. The cbreak mode should be
3509enabled to read keys when you are interested in reading individual key hits
3510rather than complete lines of text (which usually end with a carriage return).
3511keypad should be enabled to get the Functions keys, arrow keys etc.  See the
3512initialization section for details.</P
3513><P
3514><TT
3515CLASS="LITERAL"
3516>getch()</TT
3517> returns an integer corresponding to the
3518key pressed. If it is a normal character, the integer value will be equivalent
3519to the character. Otherwise it returns a number which can be matched with the
3520constants defined in <TT
3521CLASS="LITERAL"
3522>curses.h</TT
3523>.  For example if
3524the user presses F1, the integer returned is 265. This can be checked using the
3525macro KEY_F() defined in curses.h. This makes reading keys portable and easy to
3526manage.</P
3527><P
3528>For example, if you call getch() like this</P
3529><PRE
3530CLASS="PROGRAMLISTING"
3531>    int ch;
3532
3533    ch = getch();</PRE
3534><P
3535>getch() will wait for the user to press a key, (unless you specified a timeout)
3536and when user presses a key, the corresponding integer is returned. Then you can
3537check the value returned with the constants defined in curses.h to match against
3538the keys you want.</P
3539><P
3540>The following code piece will do that job.</P
3541><PRE
3542CLASS="PROGRAMLISTING"
3543>    if(ch == KEY_LEFT)
3544        printw("Left arrow is pressed\n");</PRE
3545><P
3546>Let's write a small program which creates a menu which can be navigated by up
3547and down arrows.</P
3548></DIV
3549><DIV
3550CLASS="SECT2"
3551><HR><H3
3552CLASS="SECT2"
3553><A
3554NAME="SIMPLEKEYEX"
3555>11.2. A Simple Key Usage example</A
3556></H3
3557><DIV
3558CLASS="EXAMPLE"
3559><A
3560NAME="BSIKE"
3561></A
3562><P
3563><B
3564>Example 10.  A Simple Key Usage example </B
3565></P
3566><PRE
3567CLASS="PROGRAMLISTING"
3568><SPAN
3569CLASS="INLINEMEDIAOBJECT"
3570>#include &#60;stdio.h&#62;
3571#include &#60;ncurses.h&#62;
3572
3573#define WIDTH 30
3574#define HEIGHT 10
3575
3576int startx = 0;
3577int starty = 0;
3578
3579char *choices[] = {
3580			"Choice 1",
3581			"Choice 2",
3582			"Choice 3",
3583			"Choice 4",
3584			"Exit",
3585		  };
3586int n_choices = sizeof(choices) / sizeof(char *);
3587void print_menu(WINDOW *menu_win, int highlight);
3588
3589int main()
3590{	WINDOW *menu_win;
3591	int highlight = 1;
3592	int choice = 0;
3593	int c;
3594
3595	initscr();
3596	clear();
3597	noecho();
3598	cbreak();	/* Line buffering disabled. pass on everything */
3599	startx = (80 - WIDTH) / 2;
3600	starty = (24 - HEIGHT) / 2;
3601
3602	menu_win = newwin(HEIGHT, WIDTH, starty, startx);
3603	keypad(menu_win, TRUE);
3604	mvprintw(0, 0, "Use arrow keys to go up and down, Press enter to select a choice");
3605	refresh();
3606	print_menu(menu_win, highlight);
3607	while(1)
3608	{	c = wgetch(menu_win);
3609		switch(c)
3610		{	case KEY_UP:
3611				if(highlight == 1)
3612					highlight = n_choices;
3613				else
3614					--highlight;
3615				break;
3616			case KEY_DOWN:
3617				if(highlight == n_choices)
3618					highlight = 1;
3619				else
3620					++highlight;
3621				break;
3622			case 10:
3623				choice = highlight;
3624				break;
3625			default:
3626				mvprintw(24, 0, "Charcter pressed is = %3d Hopefully it can be printed as '%c'", c, c);
3627				refresh();
3628				break;
3629		}
3630		print_menu(menu_win, highlight);
3631		if(choice != 0)	/* User did a choice come out of the infinite loop */
3632			break;
3633	}
3634	mvprintw(23, 0, "You chose choice %d with choice string %s\n", choice, choices[choice - 1]);
3635	clrtoeol();
3636	refresh();
3637	endwin();
3638	return 0;
3639}
3640
3641
3642void print_menu(WINDOW *menu_win, int highlight)
3643{
3644	int x, y, i;
3645
3646	x = 2;
3647	y = 2;
3648	box(menu_win, 0, 0);
3649	for(i = 0; i &#60; n_choices; ++i)
3650	{	if(highlight == i + 1) /* High light the present choice */
3651		{	wattron(menu_win, A_REVERSE);
3652			mvwprintw(menu_win, y, x, "%s", choices[i]);
3653			wattroff(menu_win, A_REVERSE);
3654		}
3655		else
3656			mvwprintw(menu_win, y, x, "%s", choices[i]);
3657		++y;
3658	}
3659	wrefresh(menu_win);
3660}
3661</SPAN
3662></PRE
3663></DIV
3664></DIV
3665></DIV
3666><DIV
3667CLASS="SECT1"
3668><HR><H2
3669CLASS="SECT1"
3670><A
3671NAME="MOUSE"
3672>12. Interfacing with the mouse</A
3673></H2
3674><P
3675>Now that you have seen how to get keys, lets do the same thing from mouse.
3676Usually each UI allows the user to interact with both keyboard and mouse. </P
3677><DIV
3678CLASS="SECT2"
3679><HR><H3
3680CLASS="SECT2"
3681><A
3682NAME="MOUSEBASICS"
3683>12.1. The Basics</A
3684></H3
3685><P
3686>Before you do any thing else, the events you want to receive have to be enabled
3687with <TT
3688CLASS="LITERAL"
3689>mousemask()</TT
3690>.</P
3691><PRE
3692CLASS="PROGRAMLISTING"
3693>    mousemask(  mmask_t newmask,    /* The events you want to listen to */
3694                mmask_t *oldmask)    /* The old events mask                */</PRE
3695><P
3696>The first parameter to above function is a bit mask of events you would like to
3697listen. By default, all the events are turned off. The bit mask <TT
3698CLASS="LITERAL"
3699> ALL_MOUSE_EVENTS</TT
3700> can be used to get all the events.</P
3701><P
3702>The following are all the event masks:</P
3703><PRE
3704CLASS="PROGRAMLISTING"
3705>    Name            Description
3706       ---------------------------------------------------------------------
3707       BUTTON1_PRESSED          mouse button 1 down
3708       BUTTON1_RELEASED         mouse button 1 up
3709       BUTTON1_CLICKED          mouse button 1 clicked
3710       BUTTON1_DOUBLE_CLICKED   mouse button 1 double clicked
3711       BUTTON1_TRIPLE_CLICKED   mouse button 1 triple clicked
3712       BUTTON2_PRESSED          mouse button 2 down
3713       BUTTON2_RELEASED         mouse button 2 up
3714       BUTTON2_CLICKED          mouse button 2 clicked
3715       BUTTON2_DOUBLE_CLICKED   mouse button 2 double clicked
3716       BUTTON2_TRIPLE_CLICKED   mouse button 2 triple clicked
3717       BUTTON3_PRESSED          mouse button 3 down
3718       BUTTON3_RELEASED         mouse button 3 up
3719       BUTTON3_CLICKED          mouse button 3 clicked
3720       BUTTON3_DOUBLE_CLICKED   mouse button 3 double clicked
3721       BUTTON3_TRIPLE_CLICKED   mouse button 3 triple clicked
3722       BUTTON4_PRESSED          mouse button 4 down
3723       BUTTON4_RELEASED         mouse button 4 up
3724       BUTTON4_CLICKED          mouse button 4 clicked
3725       BUTTON4_DOUBLE_CLICKED   mouse button 4 double clicked
3726       BUTTON4_TRIPLE_CLICKED   mouse button 4 triple clicked
3727       BUTTON_SHIFT             shift was down during button state change
3728       BUTTON_CTRL              control was down during button state change
3729       BUTTON_ALT               alt was down during button state change
3730       ALL_MOUSE_EVENTS         report all button state changes
3731       REPORT_MOUSE_POSITION    report mouse movement</PRE
3732></DIV
3733><DIV
3734CLASS="SECT2"
3735><HR><H3
3736CLASS="SECT2"
3737><A
3738NAME="GETTINGEVENTS"
3739>12.2. Getting the events</A
3740></H3
3741><P
3742>Once a class of mouse events have been enabled, getch() class of functions
3743return KEY_MOUSE every time some mouse event happens. Then the mouse event can
3744be retrieved with <TT
3745CLASS="LITERAL"
3746>getmouse()</TT
3747>.</P
3748><P
3749>The code approximately looks like this:</P
3750><PRE
3751CLASS="PROGRAMLISTING"
3752>    MEVENT event;
3753
3754    ch = getch();
3755    if(ch == KEY_MOUSE)
3756        if(getmouse(&amp;event) == OK)
3757            .    /* Do some thing with the event */
3758            .
3759            .</PRE
3760><P
3761>
3762getmouse() returns the event into the pointer given to it. It's a structure
3763which contains</P
3764><PRE
3765CLASS="PROGRAMLISTING"
3766>    typedef struct
3767    {
3768        short id;         /* ID to distinguish multiple devices */
3769        int x, y, z;      /* event coordinates */
3770        mmask_t bstate;   /* button state bits */
3771    }    </PRE
3772><P
3773>The <TT
3774CLASS="LITERAL"
3775>bstate</TT
3776> is the main variable we are
3777interested in. It tells the button state of the mouse.</P
3778><P
3779>Then with a code snippet like the following, we can find out what happened.</P
3780><PRE
3781CLASS="PROGRAMLISTING"
3782>    if(event.bstate &amp; BUTTON1_PRESSED)
3783        printw("Left Button Pressed");</PRE
3784></DIV
3785><DIV
3786CLASS="SECT2"
3787><HR><H3
3788CLASS="SECT2"
3789><A
3790NAME="MOUSETOGETHER"
3791>12.3. Putting it all Together</A
3792></H3
3793><P
3794>That's pretty much interfacing with mouse. Let's create the same menu and enable
3795mouse interaction. To make things simpler, key handling is removed.</P
3796><DIV
3797CLASS="EXAMPLE"
3798><A
3799NAME="BMOME"
3800></A
3801><P
3802><B
3803>Example 11.  Access the menu with mouse !!! </B
3804></P
3805><PRE
3806CLASS="PROGRAMLISTING"
3807><SPAN
3808CLASS="INLINEMEDIAOBJECT"
3809>#include &#60;ncurses.h&#62;
3810
3811#define WIDTH 30
3812#define HEIGHT 10
3813
3814int startx = 0;
3815int starty = 0;
3816
3817char *choices[] = { 	"Choice 1",
3818			"Choice 2",
3819			"Choice 3",
3820			"Choice 4",
3821			"Exit",
3822		  };
3823
3824int n_choices = sizeof(choices) / sizeof(char *);
3825
3826void print_menu(WINDOW *menu_win, int highlight);
3827void report_choice(int mouse_x, int mouse_y, int *p_choice);
3828
3829int main()
3830{	int c, choice = 0;
3831	WINDOW *menu_win;
3832	MEVENT event;
3833
3834	/* Initialize curses */
3835	initscr();
3836	clear();
3837	noecho();
3838	cbreak();	//Line buffering disabled. pass on everything
3839
3840	/* Try to put the window in the middle of screen */
3841	startx = (80 - WIDTH) / 2;
3842	starty = (24 - HEIGHT) / 2;
3843
3844	attron(A_REVERSE);
3845	mvprintw(23, 1, "Click on Exit to quit (Works best in a virtual console)");
3846	refresh();
3847	attroff(A_REVERSE);
3848
3849	/* Print the menu for the first time */
3850	menu_win = newwin(HEIGHT, WIDTH, starty, startx);
3851	print_menu(menu_win, 1);
3852	/* Get all the mouse events */
3853	mousemask(ALL_MOUSE_EVENTS, NULL);
3854
3855	while(1)
3856	{	c = wgetch(menu_win);
3857		switch(c)
3858		{	case KEY_MOUSE:
3859			if(getmouse(&#38;event) == OK)
3860			{	/* When the user clicks left mouse button */
3861				if(event.bstate &#38; BUTTON1_PRESSED)
3862				{	report_choice(event.x + 1, event.y + 1, &#38;choice);
3863					if(choice == -1) //Exit chosen
3864						goto end;
3865					mvprintw(22, 1, "Choice made is : %d String Chosen is \"%10s\"", choice, choices[choice - 1]);
3866					refresh();
3867				}
3868			}
3869			print_menu(menu_win, choice);
3870			break;
3871		}
3872	}
3873end:
3874	endwin();
3875	return 0;
3876}
3877
3878
3879void print_menu(WINDOW *menu_win, int highlight)
3880{
3881	int x, y, i;
3882
3883	x = 2;
3884	y = 2;
3885	box(menu_win, 0, 0);
3886	for(i = 0; i &#60; n_choices; ++i)
3887	{	if(highlight == i + 1)
3888		{	wattron(menu_win, A_REVERSE);
3889			mvwprintw(menu_win, y, x, "%s", choices[i]);
3890			wattroff(menu_win, A_REVERSE);
3891		}
3892		else
3893			mvwprintw(menu_win, y, x, "%s", choices[i]);
3894		++y;
3895	}
3896	wrefresh(menu_win);
3897}
3898
3899/* Report the choice according to mouse position */
3900void report_choice(int mouse_x, int mouse_y, int *p_choice)
3901{	int i,j, choice;
3902
3903	i = startx + 2;
3904	j = starty + 3;
3905
3906	for(choice = 0; choice &#60; n_choices; ++choice)
3907		if(mouse_y == j + choice &#38;&#38; mouse_x &#62;= i &#38;&#38; mouse_x &#60;= i + strlen(choices[choice]))
3908		{	if(choice == n_choices - 1)
3909				*p_choice = -1;
3910			else
3911				*p_choice = choice + 1;
3912			break;
3913		}
3914}</SPAN
3915></PRE
3916></DIV
3917></DIV
3918><DIV
3919CLASS="SECT2"
3920><HR><H3
3921CLASS="SECT2"
3922><A
3923NAME="MISCMOUSEFUNCS"
3924>12.4. Miscellaneous Functions</A
3925></H3
3926><P
3927>The functions mouse_trafo() and wmouse_trafo() can be used to convert to mouse
3928co-ordinates to screen relative co-ordinates. See curs_mouse(3X) man page for details.</P
3929><P
3930>The  mouseinterval  function sets the maximum time (in thousands of a
3931second) that can elapse between press and release events in order for
3932them to be recognized as a click.  This function returns the previous
3933interval value.  The default is one fifth of a second.</P
3934></DIV
3935></DIV
3936><DIV
3937CLASS="SECT1"
3938><HR><H2
3939CLASS="SECT1"
3940><A
3941NAME="SCREEN"
3942>13. Screen Manipulation</A
3943></H2
3944><P
3945>In this section, we will look into some functions, which allow us to manage the
3946screen efficiently and to write some fancy programs. This is especially
3947important in writing games. </P
3948><DIV
3949CLASS="SECT2"
3950><HR><H3
3951CLASS="SECT2"
3952><A
3953NAME="GETYX"
3954>13.1. getyx() functions</A
3955></H3
3956><P
3957>&#13;The function <TT
3958CLASS="LITERAL"
3959>getyx()</TT
3960> can be used to find out
3961the present cursor co-ordinates. It will fill the values of x and y co-ordinates
3962in the arguments given to it. Since getyx() is a macro you don't have to pass
3963the address of the variables. It can be called as</P
3964><PRE
3965CLASS="PROGRAMLISTING"
3966>    getyx(win, y, x);
3967    /* win: window pointer
3968     *   y, x: y, x co-ordinates will be put into this variables
3969     */</PRE
3970><P
3971>The function getparyx() gets the beginning co-ordinates of the sub window
3972relative to the main window. This is some times useful to update a sub window.
3973When designing fancy stuff like writing multiple menus, it becomes difficult to
3974store the menu positions, their first option co-ordinates etc. A simple solution
3975to this problem, is to create menus in sub windows and later find the starting
3976co-ordinates of the menus by using getparyx().</P
3977><P
3978>The functions getbegyx() and getmaxyx() store current window's beginning and
3979maximum co-ordinates. These functions are useful in the same way as above in
3980managing the windows and sub windows effectively.</P
3981></DIV
3982><DIV
3983CLASS="SECT2"
3984><HR><H3
3985CLASS="SECT2"
3986><A
3987NAME="SCREENDUMP"
3988>13.2. Screen Dumping</A
3989></H3
3990><P
3991>While writing games, some times it becomes necessary to store the state of the
3992screen and restore it back to the same state. The function scr_dump() can be
3993used to dump the screen contents to a file given as an argument. Later it can be
3994restored by scr_restore function. These two simple functions can be used
3995effectively to maintain a fast moving game with changing scenarios. </P
3996></DIV
3997><DIV
3998CLASS="SECT2"
3999><HR><H3
4000CLASS="SECT2"
4001><A
4002NAME="WINDOWDUMP"
4003>13.3. Window Dumping</A
4004></H3
4005><P
4006>To store and restore windows, the functions
4007<TT
4008CLASS="LITERAL"
4009>putwin()</TT
4010> and <TT
4011CLASS="LITERAL"
4012>getwin()</TT
4013> can be used. <TT
4014CLASS="LITERAL"
4015>putwin()</TT
4016> puts
4017the present window state into a file, which can be later restored by
4018<TT
4019CLASS="LITERAL"
4020>getwin()</TT
4021>.</P
4022><P
4023>
4024The function <TT
4025CLASS="LITERAL"
4026>copywin()</TT
4027> can be used to copy a
4028window completely onto another window. It takes the source and destination
4029windows as parameters and according to the rectangle specified, it copies the
4030rectangular region from source to destination window.  It's last parameter
4031specifies whether to overwrite or just overlay the contents on to the
4032destination window. If this argument is true, then the copying is
4033non-destructive.</P
4034></DIV
4035></DIV
4036><DIV
4037CLASS="SECT1"
4038><HR><H2
4039CLASS="SECT1"
4040><A
4041NAME="MISC"
4042>14. Miscellaneous features</A
4043></H2
4044><P
4045>Now you know enough features to write a good curses program, with all bells and
4046whistles. There are some miscellaneous functions which are useful in various
4047cases.  Let's go headlong into some of those.</P
4048><DIV
4049CLASS="SECT2"
4050><HR><H3
4051CLASS="SECT2"
4052><A
4053NAME="CURSSET"
4054>14.1. curs_set()</A
4055></H3
4056><P
4057>This function can be used to make the cursor invisible. The parameter to this
4058function should be </P
4059><PRE
4060CLASS="PROGRAMLISTING"
4061>    0 : invisible      or
4062    1 : normal    or
4063    2 : very visible.</PRE
4064></DIV
4065><DIV
4066CLASS="SECT2"
4067><HR><H3
4068CLASS="SECT2"
4069><A
4070NAME="TEMPLEAVE"
4071>14.2. Temporarily Leaving Curses mode</A
4072></H3
4073><P
4074>Some times you may want to get back to cooked mode (normal line buffering mode)
4075temporarily. In such a case you will first need to save the tty modes with a
4076call to <TT
4077CLASS="LITERAL"
4078>def_prog_mode()</TT
4079> and then call
4080<TT
4081CLASS="LITERAL"
4082>endwin()</TT
4083> to end the curses mode. This will
4084leave you in the original tty mode. To get back to curses once you are done,
4085call <TT
4086CLASS="LITERAL"
4087>reset_prog_mode() </TT
4088>. This function returns
4089the tty to the state stored by <TT
4090CLASS="LITERAL"
4091>def_prog_mode()</TT
4092>. Then do refresh(), and you are back to the curses mode. Here
4093is an example showing the sequence of things to be done.</P
4094><DIV
4095CLASS="EXAMPLE"
4096><A
4097NAME="BTELE"
4098></A
4099><P
4100><B
4101>Example 12.  Temporarily Leaving Curses Mode </B
4102></P
4103><PRE
4104CLASS="PROGRAMLISTING"
4105><SPAN
4106CLASS="INLINEMEDIAOBJECT"
4107>#include &#60;ncurses.h&#62;
4108
4109int main()
4110{
4111	initscr();			/* Start curses mode 		  */
4112	printw("Hello World !!!\n");	/* Print Hello World		  */
4113	refresh();			/* Print it on to the real screen */
4114	def_prog_mode();		/* Save the tty modes		  */
4115	endwin();			/* End curses mode temporarily	  */
4116	system("/bin/sh");		/* Do whatever you like in cooked mode */
4117	reset_prog_mode();		/* Return to the previous tty mode*/
4118					/* stored by def_prog_mode() 	  */
4119	refresh();			/* Do refresh() to restore the	  */
4120					/* Screen contents		  */
4121	printw("Another String\n");	/* Back to curses use the full    */
4122	refresh();			/* capabilities of curses	  */
4123	endwin();			/* End curses mode		  */
4124
4125	return 0;
4126}</SPAN
4127></PRE
4128></DIV
4129></DIV
4130><DIV
4131CLASS="SECT2"
4132><HR><H3
4133CLASS="SECT2"
4134><A
4135NAME="ACSVARS"
4136>14.3. ACS_ variables</A
4137></H3
4138><P
4139>If you have ever programmed in DOS, you know about those nifty characters in
4140extended character set. They are printable only on some terminals. NCURSES
4141functions like <TT
4142CLASS="LITERAL"
4143>box()</TT
4144> use these characters. All
4145these variables start with ACS meaning alternative character set. You might have
4146noticed me using these characters in some of the programs above. Here's an example
4147showing all the characters.</P
4148><DIV
4149CLASS="EXAMPLE"
4150><A
4151NAME="BACSVARS"
4152></A
4153><P
4154><B
4155>Example 13.  ACS Variables Example </B
4156></P
4157><PRE
4158CLASS="PROGRAMLISTING"
4159><SPAN
4160CLASS="INLINEMEDIAOBJECT"
4161>#include &#60;ncurses.h&#62;
4162
4163int main()
4164{
4165        initscr();
4166
4167        printw("Upper left corner           "); addch(ACS_ULCORNER); printw("\n");
4168        printw("Lower left corner           "); addch(ACS_LLCORNER); printw("\n");
4169        printw("Lower right corner          "); addch(ACS_LRCORNER); printw("\n");
4170        printw("Tee pointing right          "); addch(ACS_LTEE); printw("\n");
4171        printw("Tee pointing left           "); addch(ACS_RTEE); printw("\n");
4172        printw("Tee pointing up             "); addch(ACS_BTEE); printw("\n");
4173        printw("Tee pointing down           "); addch(ACS_TTEE); printw("\n");
4174        printw("Horizontal line             "); addch(ACS_HLINE); printw("\n");
4175        printw("Vertical line               "); addch(ACS_VLINE); printw("\n");
4176        printw("Large Plus or cross over    "); addch(ACS_PLUS); printw("\n");
4177        printw("Scan Line 1                 "); addch(ACS_S1); printw("\n");
4178        printw("Scan Line 3                 "); addch(ACS_S3); printw("\n");
4179        printw("Scan Line 7                 "); addch(ACS_S7); printw("\n");
4180        printw("Scan Line 9                 "); addch(ACS_S9); printw("\n");
4181        printw("Diamond                     "); addch(ACS_DIAMOND); printw("\n");
4182        printw("Checker board (stipple)     "); addch(ACS_CKBOARD); printw("\n");
4183        printw("Degree Symbol               "); addch(ACS_DEGREE); printw("\n");
4184        printw("Plus/Minus Symbol           "); addch(ACS_PLMINUS); printw("\n");
4185        printw("Bullet                      "); addch(ACS_BULLET); printw("\n");
4186        printw("Arrow Pointing Left         "); addch(ACS_LARROW); printw("\n");
4187        printw("Arrow Pointing Right        "); addch(ACS_RARROW); printw("\n");
4188        printw("Arrow Pointing Down         "); addch(ACS_DARROW); printw("\n");
4189        printw("Arrow Pointing Up           "); addch(ACS_UARROW); printw("\n");
4190        printw("Board of squares            "); addch(ACS_BOARD); printw("\n");
4191        printw("Lantern Symbol              "); addch(ACS_LANTERN); printw("\n");
4192        printw("Solid Square Block          "); addch(ACS_BLOCK); printw("\n");
4193        printw("Less/Equal sign             "); addch(ACS_LEQUAL); printw("\n");
4194        printw("Greater/Equal sign          "); addch(ACS_GEQUAL); printw("\n");
4195        printw("Pi                          "); addch(ACS_PI); printw("\n");
4196        printw("Not equal                   "); addch(ACS_NEQUAL); printw("\n");
4197        printw("UK pound sign               "); addch(ACS_STERLING); printw("\n");
4198
4199        refresh();
4200        getch();
4201        endwin();
4202
4203	return 0;
4204}</SPAN
4205></PRE
4206></DIV
4207></DIV
4208></DIV
4209><DIV
4210CLASS="SECT1"
4211><HR><H2
4212CLASS="SECT1"
4213><A
4214NAME="OTHERLIB"
4215>15. Other libraries</A
4216></H2
4217><P
4218>Apart from the curses library, there are few text mode libraries, which provide
4219more functionality and a lot of features. The following sections explain three
4220standard libraries which are usually distributed along with curses. </P
4221></DIV
4222><DIV
4223CLASS="SECT1"
4224><HR><H2
4225CLASS="SECT1"
4226><A
4227NAME="PANELS"
4228>16. Panel Library</A
4229></H2
4230><P
4231>Now that you are proficient in curses, you wanted to do some thing big. You
4232created a lot of overlapping windows to give a professional windows-type look.
4233Unfortunately, it soon becomes difficult to manage these. The multiple
4234refreshes, updates plunge you into a nightmare. The overlapping windows create
4235blotches, whenever you forget to refresh the windows in the proper order. </P
4236><P
4237>Don't despair. There's an elegant solution provided in panels library. In the
4238words of developers of ncurses </P
4239><P
4240><SPAN
4241CLASS="emphasis"
4242><I
4243CLASS="EMPHASIS"
4244>When your interface design is such that windows may dive deeper into the
4245visibility stack or pop to the top at runtime, the resulting book-keeping can be
4246tedious and difficult to get right. Hence the panels library.</I
4247></SPAN
4248></P
4249><P
4250>If you have lot of overlapping windows, then panels library is the way to go. It
4251obviates the need of doing series of wnoutrefresh(), doupdate() and relieves the
4252burden of doing it correctly(bottom up). The library maintains information about
4253the order of windows, their overlapping and update the screen properly. So why
4254wait? Let's take a close peek into panels.</P
4255><DIV
4256CLASS="SECT2"
4257><HR><H3
4258CLASS="SECT2"
4259><A
4260NAME="PANELBASICS"
4261>16.1. The Basics</A
4262></H3
4263><P
4264>Panel object is a window that is implicitly treated as part of a deck including
4265all other panel objects. The deck is treated as a stack with the top panel being
4266completely visible and the other panels may or may not be obscured according to
4267their positions. So the basic idea is to create a stack of overlapping panels
4268and use panels library to display them correctly. There is a function similar to
4269refresh() which, when called , displays panels in the correct order. Functions
4270are provided to hide or show panels, move panels, change its size etc.. The
4271overlapping problem is managed by the panels library during all the calls to
4272these functions. </P
4273><P
4274>The general flow of a panel program goes like this:
4275
4276<P
4277></P
4278><OL
4279TYPE="1"
4280><LI
4281><P
4282>Create the windows (with newwin()) to be attached to the panels.</P
4283></LI
4284><LI
4285><P
4286>Create panels with the chosen visibility order. Stack them up according to the
4287desired visibility. The function new_panel() is used to created panels.</P
4288></LI
4289><LI
4290><P
4291>Call update_panels() to write the panels to the virtual screen in correct
4292visibility order. Do a doupdate() to show it on the screen. </P
4293></LI
4294><LI
4295><P
4296>Mainpulate the panels with show_panel(), hide_panel(), move_panel() etc. Make
4297use of helper functions like panel_hidden() and panel_window(). Make use of user
4298pointer to store custom data for a panel. Use the functions set_panel_userptr()
4299and panel_userptr() to set and get the user pointer for a panel.</P
4300></LI
4301><LI
4302><P
4303>When you are done with the panel use del_panel() to delete the panel.</P
4304></LI
4305></OL
4306></P
4307><P
4308>Let's make the concepts clear, with some programs.  The following is a simple
4309program which creates 3 overlapping panels and shows them on the screen. </P
4310></DIV
4311><DIV
4312CLASS="SECT2"
4313><HR><H3
4314CLASS="SECT2"
4315><A
4316NAME="COMPILEPANELS"
4317>16.2. Compiling With the Panels Library</A
4318></H3
4319><P
4320>To use panels library functions, you have to include panel.h and to link the
4321program with panels library the flag -lpanel should be added along with
4322-lncurses in that order.</P
4323><PRE
4324CLASS="PROGRAMLISTING"
4325>    #include &lt;panel.h&gt;
4326    .
4327    .
4328    .
4329
4330    compile and link: gcc &lt;program file&gt; -lpanel -lncurses</PRE
4331><DIV
4332CLASS="EXAMPLE"
4333><A
4334NAME="PPASI"
4335></A
4336><P
4337><B
4338>Example 14.  Panel basics</B
4339></P
4340><PRE
4341CLASS="PROGRAMLISTING"
4342><SPAN
4343CLASS="INLINEMEDIAOBJECT"
4344>#include &#60;panel.h&#62;
4345
4346int main()
4347{	WINDOW *my_wins[3];
4348	PANEL  *my_panels[3];
4349	int lines = 10, cols = 40, y = 2, x = 4, i;
4350
4351	initscr();
4352	cbreak();
4353	noecho();
4354
4355	/* Create windows for the panels */
4356	my_wins[0] = newwin(lines, cols, y, x);
4357	my_wins[1] = newwin(lines, cols, y + 1, x + 5);
4358	my_wins[2] = newwin(lines, cols, y + 2, x + 10);
4359
4360	/*
4361	 * Create borders around the windows so that you can see the effect
4362	 * of panels
4363	 */
4364	for(i = 0; i &#60; 3; ++i)
4365		box(my_wins[i], 0, 0);
4366
4367	/* Attach a panel to each window */ 	/* Order is bottom up */
4368	my_panels[0] = new_panel(my_wins[0]); 	/* Push 0, order: stdscr-0 */
4369	my_panels[1] = new_panel(my_wins[1]); 	/* Push 1, order: stdscr-0-1 */
4370	my_panels[2] = new_panel(my_wins[2]); 	/* Push 2, order: stdscr-0-1-2 */
4371
4372	/* Update the stacking order. 2nd panel will be on top */
4373	update_panels();
4374
4375	/* Show it on the screen */
4376	doupdate();
4377
4378	getch();
4379	endwin();
4380}
4381</SPAN
4382></PRE
4383></DIV
4384><P
4385>As you can see, above program follows a simple flow as explained. The windows
4386are created with newwin() and then they are attached to panels with new_panel().
4387As we attach one panel after another, the stack of panels gets updated. To put
4388them on screen update_panels() and doupdate() are called.</P
4389></DIV
4390><DIV
4391CLASS="SECT2"
4392><HR><H3
4393CLASS="SECT2"
4394><A
4395NAME="PANELBROWSING"
4396>16.3. Panel Window Browsing</A
4397></H3
4398><P
4399>A slightly complicated example is given below. This program creates 3
4400windows which can be cycled through using tab. Have a look at the code.</P
4401><DIV
4402CLASS="EXAMPLE"
4403><A
4404NAME="PPABR"
4405></A
4406><P
4407><B
4408>Example 15.  Panel Window Browsing Example </B
4409></P
4410><PRE
4411CLASS="PROGRAMLISTING"
4412><SPAN
4413CLASS="INLINEMEDIAOBJECT"
4414>#include &#60;panel.h&#62;
4415
4416#define NLINES 10
4417#define NCOLS 40
4418
4419void init_wins(WINDOW **wins, int n);
4420void win_show(WINDOW *win, char *label, int label_color);
4421void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color);
4422
4423int main()
4424{	WINDOW *my_wins[3];
4425	PANEL  *my_panels[3];
4426	PANEL  *top;
4427	int ch;
4428
4429	/* Initialize curses */
4430	initscr();
4431	start_color();
4432	cbreak();
4433	noecho();
4434	keypad(stdscr, TRUE);
4435
4436	/* Initialize all the colors */
4437	init_pair(1, COLOR_RED, COLOR_BLACK);
4438	init_pair(2, COLOR_GREEN, COLOR_BLACK);
4439	init_pair(3, COLOR_BLUE, COLOR_BLACK);
4440	init_pair(4, COLOR_CYAN, COLOR_BLACK);
4441
4442	init_wins(my_wins, 3);
4443
4444	/* Attach a panel to each window */ 	/* Order is bottom up */
4445	my_panels[0] = new_panel(my_wins[0]); 	/* Push 0, order: stdscr-0 */
4446	my_panels[1] = new_panel(my_wins[1]); 	/* Push 1, order: stdscr-0-1 */
4447	my_panels[2] = new_panel(my_wins[2]); 	/* Push 2, order: stdscr-0-1-2 */
4448
4449	/* Set up the user pointers to the next panel */
4450	set_panel_userptr(my_panels[0], my_panels[1]);
4451	set_panel_userptr(my_panels[1], my_panels[2]);
4452	set_panel_userptr(my_panels[2], my_panels[0]);
4453
4454	/* Update the stacking order. 2nd panel will be on top */
4455	update_panels();
4456
4457	/* Show it on the screen */
4458	attron(COLOR_PAIR(4));
4459	mvprintw(LINES - 2, 0, "Use tab to browse through the windows (F1 to Exit)");
4460	attroff(COLOR_PAIR(4));
4461	doupdate();
4462
4463	top = my_panels[2];
4464	while((ch = getch()) != KEY_F(1))
4465	{	switch(ch)
4466		{	case 9:
4467				top = (PANEL *)panel_userptr(top);
4468				top_panel(top);
4469				break;
4470		}
4471		update_panels();
4472		doupdate();
4473	}
4474	endwin();
4475	return 0;
4476}
4477
4478/* Put all the windows */
4479void init_wins(WINDOW **wins, int n)
4480{	int x, y, i;
4481	char label[80];
4482
4483	y = 2;
4484	x = 10;
4485	for(i = 0; i &#60; n; ++i)
4486	{	wins[i] = newwin(NLINES, NCOLS, y, x);
4487		sprintf(label, "Window Number %d", i + 1);
4488		win_show(wins[i], label, i + 1);
4489		y += 3;
4490		x += 7;
4491	}
4492}
4493
4494/* Show the window with a border and a label */
4495void win_show(WINDOW *win, char *label, int label_color)
4496{	int startx, starty, height, width;
4497
4498	getbegyx(win, starty, startx);
4499	getmaxyx(win, height, width);
4500
4501	box(win, 0, 0);
4502	mvwaddch(win, 2, 0, ACS_LTEE);
4503	mvwhline(win, 2, 1, ACS_HLINE, width - 2);
4504	mvwaddch(win, 2, width - 1, ACS_RTEE);
4505
4506	print_in_middle(win, 1, 0, width, label, COLOR_PAIR(label_color));
4507}
4508
4509void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color)
4510{	int length, x, y;
4511	float temp;
4512
4513	if(win == NULL)
4514		win = stdscr;
4515	getyx(win, y, x);
4516	if(startx != 0)
4517		x = startx;
4518	if(starty != 0)
4519		y = starty;
4520	if(width == 0)
4521		width = 80;
4522
4523	length = strlen(string);
4524	temp = (width - length)/ 2;
4525	x = startx + (int)temp;
4526	wattron(win, color);
4527	mvwprintw(win, y, x, "%s", string);
4528	wattroff(win, color);
4529	refresh();
4530}</SPAN
4531></PRE
4532></DIV
4533></DIV
4534><DIV
4535CLASS="SECT2"
4536><HR><H3
4537CLASS="SECT2"
4538><A
4539NAME="USERPTRUSING"
4540>16.4. Using User Pointers</A
4541></H3
4542><P
4543>In the above example I used user pointers to find out the next window in the
4544cycle.  We can attach custom information to the panel by specifying a user
4545pointer, which can point to any information you want to store. In this case I
4546stored the pointer to the next panel in the cycle. User pointer for a panel can
4547be set with the function <TT
4548CLASS="LITERAL"
4549> set_panel_userptr()</TT
4550>.
4551It can be accessed using the function <TT
4552CLASS="LITERAL"
4553>panel_userptr()</TT
4554> which will return the user pointer for the panel given as
4555argument. After finding the next panel in the cycle It's brought to the top by
4556the function top_panel().  This function brings the panel given as argument to
4557the top of the panel stack.  </P
4558></DIV
4559><DIV
4560CLASS="SECT2"
4561><HR><H3
4562CLASS="SECT2"
4563><A
4564NAME="PANELMOVERESIZE"
4565>16.5. Moving and Resizing Panels</A
4566></H3
4567><P
4568>The function <TT
4569CLASS="LITERAL"
4570>move_panel()</TT
4571> can be used to move a
4572panel to the desired location. It does not change the position of the panel in
4573the stack. Make sure that you use move_panel() instead mvwin() on the window
4574associated with the panel.</P
4575><P
4576>Resizing a panel is slightly complex.  There is no straight forward function
4577just to resize the window associated with a panel. A solution to resize a panel
4578is to create a new window with the desired sizes, change the window associated
4579with the panel using replace_panel(). Don't forget to delete the old window. The
4580window associated with a panel can be found by using the function
4581panel_window().</P
4582><P
4583>The following program shows these concepts, in supposedly simple program. You
4584can cycle through the window with &lt;TAB&gt; as usual. To resize or move the
4585active panel press 'r' for resize 'm' for moving. Then use arrow keys to resize
4586or move it to the desired way and press enter to end your resizing or moving.
4587This example makes use of user data to get the required data to do the
4588operations. </P
4589><DIV
4590CLASS="EXAMPLE"
4591><A
4592NAME="PPARE"
4593></A
4594><P
4595><B
4596>Example 16.  Panel Moving and Resizing example </B
4597></P
4598><PRE
4599CLASS="PROGRAMLISTING"
4600><SPAN
4601CLASS="INLINEMEDIAOBJECT"
4602>#include &#60;panel.h&#62;
4603
4604typedef struct _PANEL_DATA {
4605	int x, y, w, h;
4606	char label[80];
4607	int label_color;
4608	PANEL *next;
4609}PANEL_DATA;
4610
4611#define NLINES 10
4612#define NCOLS 40
4613
4614void init_wins(WINDOW **wins, int n);
4615void win_show(WINDOW *win, char *label, int label_color);
4616void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color);
4617void set_user_ptrs(PANEL **panels, int n);
4618
4619int main()
4620{	WINDOW *my_wins[3];
4621	PANEL  *my_panels[3];
4622	PANEL_DATA  *top;
4623	PANEL *stack_top;
4624	WINDOW *temp_win, *old_win;
4625	int ch;
4626	int newx, newy, neww, newh;
4627	int size = FALSE, move = FALSE;
4628
4629	/* Initialize curses */
4630	initscr();
4631	start_color();
4632	cbreak();
4633	noecho();
4634	keypad(stdscr, TRUE);
4635
4636	/* Initialize all the colors */
4637	init_pair(1, COLOR_RED, COLOR_BLACK);
4638	init_pair(2, COLOR_GREEN, COLOR_BLACK);
4639	init_pair(3, COLOR_BLUE, COLOR_BLACK);
4640	init_pair(4, COLOR_CYAN, COLOR_BLACK);
4641
4642	init_wins(my_wins, 3);
4643
4644	/* Attach a panel to each window */ 	/* Order is bottom up */
4645	my_panels[0] = new_panel(my_wins[0]); 	/* Push 0, order: stdscr-0 */
4646	my_panels[1] = new_panel(my_wins[1]); 	/* Push 1, order: stdscr-0-1 */
4647	my_panels[2] = new_panel(my_wins[2]); 	/* Push 2, order: stdscr-0-1-2 */
4648
4649	set_user_ptrs(my_panels, 3);
4650	/* Update the stacking order. 2nd panel will be on top */
4651	update_panels();
4652
4653	/* Show it on the screen */
4654	attron(COLOR_PAIR(4));
4655	mvprintw(LINES - 3, 0, "Use 'm' for moving, 'r' for resizing");
4656	mvprintw(LINES - 2, 0, "Use tab to browse through the windows (F1 to Exit)");
4657	attroff(COLOR_PAIR(4));
4658	doupdate();
4659
4660	stack_top = my_panels[2];
4661	top = (PANEL_DATA *)panel_userptr(stack_top);
4662	newx = top-&#62;x;
4663	newy = top-&#62;y;
4664	neww = top-&#62;w;
4665	newh = top-&#62;h;
4666	while((ch = getch()) != KEY_F(1))
4667	{	switch(ch)
4668		{	case 9:		/* Tab */
4669				top = (PANEL_DATA *)panel_userptr(stack_top);
4670				top_panel(top-&#62;next);
4671				stack_top = top-&#62;next;
4672				top = (PANEL_DATA *)panel_userptr(stack_top);
4673				newx = top-&#62;x;
4674				newy = top-&#62;y;
4675				neww = top-&#62;w;
4676				newh = top-&#62;h;
4677				break;
4678			case 'r':	/* Re-Size*/
4679				size = TRUE;
4680				attron(COLOR_PAIR(4));
4681				mvprintw(LINES - 4, 0, "Entered Resizing :Use Arrow Keys to resize and press &#60;ENTER&#62; to end resizing");
4682				refresh();
4683				attroff(COLOR_PAIR(4));
4684				break;
4685			case 'm':	/* Move */
4686				attron(COLOR_PAIR(4));
4687				mvprintw(LINES - 4, 0, "Entered Moving: Use Arrow Keys to Move and press &#60;ENTER&#62; to end moving");
4688				refresh();
4689				attroff(COLOR_PAIR(4));
4690				move = TRUE;
4691				break;
4692			case KEY_LEFT:
4693				if(size == TRUE)
4694				{	--newx;
4695					++neww;
4696				}
4697				if(move == TRUE)
4698					--newx;
4699				break;
4700			case KEY_RIGHT:
4701				if(size == TRUE)
4702				{	++newx;
4703					--neww;
4704				}
4705				if(move == TRUE)
4706					++newx;
4707				break;
4708			case KEY_UP:
4709				if(size == TRUE)
4710				{	--newy;
4711					++newh;
4712				}
4713				if(move == TRUE)
4714					--newy;
4715				break;
4716			case KEY_DOWN:
4717				if(size == TRUE)
4718				{	++newy;
4719					--newh;
4720				}
4721				if(move == TRUE)
4722					++newy;
4723				break;
4724			case 10:	/* Enter */
4725				move(LINES - 4, 0);
4726				clrtoeol();
4727				refresh();
4728				if(size == TRUE)
4729				{	old_win = panel_window(stack_top);
4730					temp_win = newwin(newh, neww, newy, newx);
4731					replace_panel(stack_top, temp_win);
4732					win_show(temp_win, top-&#62;label, top-&#62;label_color);
4733					delwin(old_win);
4734					size = FALSE;
4735				}
4736				if(move == TRUE)
4737				{	move_panel(stack_top, newy, newx);
4738					move = FALSE;
4739				}
4740				break;
4741
4742		}
4743		attron(COLOR_PAIR(4));
4744		mvprintw(LINES - 3, 0, "Use 'm' for moving, 'r' for resizing");
4745	    	mvprintw(LINES - 2, 0, "Use tab to browse through the windows (F1 to Exit)");
4746	    	attroff(COLOR_PAIR(4));
4747	        refresh();
4748		update_panels();
4749		doupdate();
4750	}
4751	endwin();
4752	return 0;
4753}
4754
4755/* Put all the windows */
4756void init_wins(WINDOW **wins, int n)
4757{	int x, y, i;
4758	char label[80];
4759
4760	y = 2;
4761	x = 10;
4762	for(i = 0; i &#60; n; ++i)
4763	{	wins[i] = newwin(NLINES, NCOLS, y, x);
4764		sprintf(label, "Window Number %d", i + 1);
4765		win_show(wins[i], label, i + 1);
4766		y += 3;
4767		x += 7;
4768	}
4769}
4770
4771/* Set the PANEL_DATA structures for individual panels */
4772void set_user_ptrs(PANEL **panels, int n)
4773{	PANEL_DATA *ptrs;
4774	WINDOW *win;
4775	int x, y, w, h, i;
4776	char temp[80];
4777
4778	ptrs = (PANEL_DATA *)calloc(n, sizeof(PANEL_DATA));
4779
4780	for(i = 0;i &#60; n; ++i)
4781	{	win = panel_window(panels[i]);
4782		getbegyx(win, y, x);
4783		getmaxyx(win, h, w);
4784		ptrs[i].x = x;
4785		ptrs[i].y = y;
4786		ptrs[i].w = w;
4787		ptrs[i].h = h;
4788		sprintf(temp, "Window Number %d", i + 1);
4789		strcpy(ptrs[i].label, temp);
4790		ptrs[i].label_color = i + 1;
4791		if(i + 1 == n)
4792			ptrs[i].next = panels[0];
4793		else
4794			ptrs[i].next = panels[i + 1];
4795		set_panel_userptr(panels[i], &#38;ptrs[i]);
4796	}
4797}
4798
4799/* Show the window with a border and a label */
4800void win_show(WINDOW *win, char *label, int label_color)
4801{	int startx, starty, height, width;
4802
4803	getbegyx(win, starty, startx);
4804	getmaxyx(win, height, width);
4805
4806	box(win, 0, 0);
4807	mvwaddch(win, 2, 0, ACS_LTEE);
4808	mvwhline(win, 2, 1, ACS_HLINE, width - 2);
4809	mvwaddch(win, 2, width - 1, ACS_RTEE);
4810
4811	print_in_middle(win, 1, 0, width, label, COLOR_PAIR(label_color));
4812}
4813
4814void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color)
4815{	int length, x, y;
4816	float temp;
4817
4818	if(win == NULL)
4819		win = stdscr;
4820	getyx(win, y, x);
4821	if(startx != 0)
4822		x = startx;
4823	if(starty != 0)
4824		y = starty;
4825	if(width == 0)
4826		width = 80;
4827
4828	length = strlen(string);
4829	temp = (width - length)/ 2;
4830	x = startx + (int)temp;
4831	wattron(win, color);
4832	mvwprintw(win, y, x, "%s", string);
4833	wattroff(win, color);
4834	refresh();
4835}</SPAN
4836></PRE
4837></DIV
4838><P
4839>Concentrate on the main while loop. Once it finds out the type of key pressed,
4840it takes appropriate action. If 'r' is pressed resizing mode is started. After
4841this the new sizes are updated as the user presses the arrow keys. When the user
4842presses &lt;ENTER&gt; present selection ends and panel is resized by using the
4843concept explained.  While in resizing mode the program doesn't show how the
4844window is getting resized.  It's left as an exercise to the reader to print a
4845dotted border while it gets resized to a new position. </P
4846><P
4847>When the user presses 'm' the move mode starts. This is a bit simpler than
4848resizing.  As the arrow keys are pressed the new position is updated and
4849pressing of &lt;ENTER&gt; causes the panel to be moved by calling the function
4850move_panel().</P
4851><P
4852>In this program the user data which is represented as PANEL_DATA, plays very
4853important role in finding the associated information with a panel. As written in
4854the comments, the PANEL_DATA stores the panel sizes, label, label color and a
4855pointer to the next panel in the cycle.</P
4856></DIV
4857><DIV
4858CLASS="SECT2"
4859><HR><H3
4860CLASS="SECT2"
4861><A
4862NAME="PANELSHOWHIDE"
4863>16.6. Hiding and Showing Panels</A
4864></H3
4865><P
4866>A Panel can be hidden by using the function hide_panel(). This function merely
4867removes it form the stack of panels, thus hiding it on the screen once you do
4868update_panels() and doupdate(). It doesn't destroy the PANEL structure
4869associated with the hidden panel.  It can be shown again by using the
4870show_panel() function.</P
4871><P
4872>The following program shows the hiding of panels. Press 'a' or 'b' or 'c' to
4873show or hide first, second and third windows respectively. It uses a user data
4874with a small variable hide, which keeps track of whether the window is hidden or
4875not. For some reason the function
4876<TT
4877CLASS="LITERAL"
4878>panel_hidden()</TT
4879> which tells whether a panel is
4880hidden or not is not working.  A bug report was also presented by Michael Andres
4881<A
4882HREF="http://www.geocrawler.com/archives/3/344/1999/9/0/2643549/"
4883TARGET="_top"
4884> here</A
4885></P
4886><DIV
4887CLASS="EXAMPLE"
4888><A
4889NAME="PPAHI"
4890></A
4891><P
4892><B
4893>Example 17.  Panel Hiding and Showing example </B
4894></P
4895><PRE
4896CLASS="PROGRAMLISTING"
4897><SPAN
4898CLASS="INLINEMEDIAOBJECT"
4899>#include &#60;panel.h&#62;
4900
4901typedef struct _PANEL_DATA {
4902	int hide;	/* TRUE if panel is hidden */
4903}PANEL_DATA;
4904
4905#define NLINES 10
4906#define NCOLS 40
4907
4908void init_wins(WINDOW **wins, int n);
4909void win_show(WINDOW *win, char *label, int label_color);
4910void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color);
4911
4912int main()
4913{	WINDOW *my_wins[3];
4914	PANEL  *my_panels[3];
4915	PANEL_DATA panel_datas[3];
4916	PANEL_DATA *temp;
4917	int ch;
4918
4919	/* Initialize curses */
4920	initscr();
4921	start_color();
4922	cbreak();
4923	noecho();
4924	keypad(stdscr, TRUE);
4925
4926	/* Initialize all the colors */
4927	init_pair(1, COLOR_RED, COLOR_BLACK);
4928	init_pair(2, COLOR_GREEN, COLOR_BLACK);
4929	init_pair(3, COLOR_BLUE, COLOR_BLACK);
4930	init_pair(4, COLOR_CYAN, COLOR_BLACK);
4931
4932	init_wins(my_wins, 3);
4933
4934	/* Attach a panel to each window */ 	/* Order is bottom up */
4935	my_panels[0] = new_panel(my_wins[0]); 	/* Push 0, order: stdscr-0 */
4936	my_panels[1] = new_panel(my_wins[1]); 	/* Push 1, order: stdscr-0-1 */
4937	my_panels[2] = new_panel(my_wins[2]); 	/* Push 2, order: stdscr-0-1-2 */
4938
4939	/* Initialize panel datas saying that nothing is hidden */
4940	panel_datas[0].hide = FALSE;
4941	panel_datas[1].hide = FALSE;
4942	panel_datas[2].hide = FALSE;
4943
4944	set_panel_userptr(my_panels[0], &#38;panel_datas[0]);
4945	set_panel_userptr(my_panels[1], &#38;panel_datas[1]);
4946	set_panel_userptr(my_panels[2], &#38;panel_datas[2]);
4947
4948	/* Update the stacking order. 2nd panel will be on top */
4949	update_panels();
4950
4951	/* Show it on the screen */
4952	attron(COLOR_PAIR(4));
4953	mvprintw(LINES - 3, 0, "Show or Hide a window with 'a'(first window)  'b'(Second Window)  'c'(Third Window)");
4954	mvprintw(LINES - 2, 0, "F1 to Exit");
4955
4956	attroff(COLOR_PAIR(4));
4957	doupdate();
4958
4959	while((ch = getch()) != KEY_F(1))
4960	{	switch(ch)
4961		{	case 'a':
4962				temp = (PANEL_DATA *)panel_userptr(my_panels[0]);
4963				if(temp-&#62;hide == FALSE)
4964				{	hide_panel(my_panels[0]);
4965					temp-&#62;hide = TRUE;
4966				}
4967				else
4968				{	show_panel(my_panels[0]);
4969					temp-&#62;hide = FALSE;
4970				}
4971				break;
4972			case 'b':
4973				temp = (PANEL_DATA *)panel_userptr(my_panels[1]);
4974				if(temp-&#62;hide == FALSE)
4975				{	hide_panel(my_panels[1]);
4976					temp-&#62;hide = TRUE;
4977				}
4978				else
4979				{	show_panel(my_panels[1]);
4980					temp-&#62;hide = FALSE;
4981				}
4982				break;
4983			case 'c':
4984				temp = (PANEL_DATA *)panel_userptr(my_panels[2]);
4985				if(temp-&#62;hide == FALSE)
4986				{	hide_panel(my_panels[2]);
4987					temp-&#62;hide = TRUE;
4988				}
4989				else
4990				{	show_panel(my_panels[2]);
4991					temp-&#62;hide = FALSE;
4992				}
4993				break;
4994		}
4995		update_panels();
4996		doupdate();
4997	}
4998	endwin();
4999	return 0;
5000}
5001
5002/* Put all the windows */
5003void init_wins(WINDOW **wins, int n)
5004{	int x, y, i;
5005	char label[80];
5006
5007	y = 2;
5008	x = 10;
5009	for(i = 0; i &#60; n; ++i)
5010	{	wins[i] = newwin(NLINES, NCOLS, y, x);
5011		sprintf(label, "Window Number %d", i + 1);
5012		win_show(wins[i], label, i + 1);
5013		y += 3;
5014		x += 7;
5015	}
5016}
5017
5018/* Show the window with a border and a label */
5019void win_show(WINDOW *win, char *label, int label_color)
5020{	int startx, starty, height, width;
5021
5022	getbegyx(win, starty, startx);
5023	getmaxyx(win, height, width);
5024
5025	box(win, 0, 0);
5026	mvwaddch(win, 2, 0, ACS_LTEE);
5027	mvwhline(win, 2, 1, ACS_HLINE, width - 2);
5028	mvwaddch(win, 2, width - 1, ACS_RTEE);
5029
5030	print_in_middle(win, 1, 0, width, label, COLOR_PAIR(label_color));
5031}
5032
5033void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color)
5034{	int length, x, y;
5035	float temp;
5036
5037	if(win == NULL)
5038		win = stdscr;
5039	getyx(win, y, x);
5040	if(startx != 0)
5041		x = startx;
5042	if(starty != 0)
5043		y = starty;
5044	if(width == 0)
5045		width = 80;
5046
5047	length = strlen(string);
5048	temp = (width - length)/ 2;
5049	x = startx + (int)temp;
5050	wattron(win, color);
5051	mvwprintw(win, y, x, "%s", string);
5052	wattroff(win, color);
5053	refresh();
5054}</SPAN
5055></PRE
5056></DIV
5057></DIV
5058><DIV
5059CLASS="SECT2"
5060><HR><H3
5061CLASS="SECT2"
5062><A
5063NAME="PANELABOVE"
5064>16.7. panel_above() and panel_below() Functions</A
5065></H3
5066><P
5067>The functions <TT
5068CLASS="LITERAL"
5069>panel_above()</TT
5070> and
5071<TT
5072CLASS="LITERAL"
5073>panel_below()</TT
5074> can be used to find out the panel
5075above and below a panel. If the argument to these functions is NULL, then they
5076return a pointer to bottom panel and top panel respectively.</P
5077></DIV
5078></DIV
5079><DIV
5080CLASS="SECT1"
5081><HR><H2
5082CLASS="SECT1"
5083><A
5084NAME="MENUS"
5085>17. Menus Library</A
5086></H2
5087><P
5088>The menus library provides a nice extension to basic curses, through which you
5089can create menus. It provides a set of functions to create menus. But they have
5090to be customized to give a nicer look, with colors etc. Let's get into the
5091details.</P
5092><P
5093>A menu is a screen display that assists the user to choose some subset of a
5094given set of items. To put it simple, a menu is a collection of items from which
5095one or more items can be chosen. Some readers might not be aware of multiple
5096item selection capability.  Menu library provides functionality to write menus
5097from which the user can chose more than one item as the preferred choice. This
5098is dealt with in a later section. Now it is time for some rudiments.</P
5099><DIV
5100CLASS="SECT2"
5101><HR><H3
5102CLASS="SECT2"
5103><A
5104NAME="MENUBASICS"
5105>17.1. The Basics</A
5106></H3
5107><P
5108>To create menus, you first create items, and then post the menu to the display.
5109After that, all the processing of user responses is done in an elegant function
5110menu_driver() which is the work horse of any menu program. </P
5111><P
5112>The general flow of control of a menu program looks like this.
5113<P
5114></P
5115><OL
5116TYPE="1"
5117><LI
5118><P
5119>Initialize curses</P
5120></LI
5121><LI
5122><P
5123>Create items using new_item(). You can specify a name and description for the
5124items.</P
5125></LI
5126><LI
5127><P
5128>Create the menu with new_menu() by specifying the items to be attached with.</P
5129></LI
5130><LI
5131><P
5132>Post the menu with menu_post() and refresh the screen.</P
5133></LI
5134><LI
5135><P
5136>Process the user requests with a loop and do necessary updates to menu with
5137menu_driver.</P
5138></LI
5139><LI
5140><P
5141>Unpost the menu with menu_unpost()</P
5142></LI
5143><LI
5144><P
5145>Free the memory allocated to menu by free_menu()</P
5146></LI
5147><LI
5148><P
5149>Free the memory allocated to the items with free_item() </P
5150></LI
5151><LI
5152><P
5153>End curses </P
5154></LI
5155></OL
5156></P
5157><P
5158>Let's see a program which prints a simple menu and updates the current selection
5159with up, down arrows. </P
5160></DIV
5161><DIV
5162CLASS="SECT2"
5163><HR><H3
5164CLASS="SECT2"
5165><A
5166NAME="COMPILEMENUS"
5167>17.2. Compiling With the Menu Library</A
5168></H3
5169><P
5170>To use menu library functions, you have to include menu.h and to link the
5171program with menu library the flag -lmenu should be added along with -lncurses
5172in that order.</P
5173><PRE
5174CLASS="PROGRAMLISTING"
5175>    #include &lt;menu.h&gt;
5176    .
5177    .
5178    .
5179
5180    compile and link: gcc &lt;program file&gt; -lmenu -lncurses</PRE
5181><DIV
5182CLASS="EXAMPLE"
5183><A
5184NAME="MMESI"
5185></A
5186><P
5187><B
5188>Example 18. Menu Basics </B
5189></P
5190><PRE
5191CLASS="PROGRAMLISTING"
5192><SPAN
5193CLASS="INLINEMEDIAOBJECT"
5194>#include &#60;curses.h&#62;
5195#include &#60;menu.h&#62;
5196
5197#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
5198#define CTRLD 	4
5199
5200char *choices[] = {
5201                        "Choice 1",
5202                        "Choice 2",
5203                        "Choice 3",
5204                        "Choice 4",
5205                        "Exit",
5206                  };
5207
5208int main()
5209{	ITEM **my_items;
5210	int c;
5211	MENU *my_menu;
5212	int n_choices, i;
5213	ITEM *cur_item;
5214
5215
5216	initscr();
5217	cbreak();
5218	noecho();
5219	keypad(stdscr, TRUE);
5220
5221	n_choices = ARRAY_SIZE(choices);
5222	my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *));
5223
5224	for(i = 0; i &#60; n_choices; ++i)
5225	        my_items[i] = new_item(choices[i], choices[i]);
5226	my_items[n_choices] = (ITEM *)NULL;
5227
5228	my_menu = new_menu((ITEM **)my_items);
5229	mvprintw(LINES - 2, 0, "F1 to Exit");
5230	post_menu(my_menu);
5231	refresh();
5232
5233	while((c = getch()) != KEY_F(1))
5234	{   switch(c)
5235	    {	case KEY_DOWN:
5236		        menu_driver(my_menu, REQ_DOWN_ITEM);
5237				break;
5238			case KEY_UP:
5239				menu_driver(my_menu, REQ_UP_ITEM);
5240				break;
5241		}
5242	}
5243
5244	free_item(my_items[0]);
5245	free_item(my_items[1]);
5246	free_menu(my_menu);
5247	endwin();
5248}
5249	</SPAN
5250></PRE
5251></DIV
5252><P
5253>This program demonstrates the basic concepts involved in creating a menu using
5254menus library.  First we create the items using new_item() and then attach them
5255to the menu with new_menu() function. After posting the menu and refreshing the
5256screen, the main processing loop starts. It reads user input and takes
5257corresponding action. The function menu_driver() is the main work horse of the
5258menu system. The second parameter to this function tells what's to be done with
5259the menu. According to the parameter, menu_driver() does the corresponding task.
5260The value can be either a menu navigational request, an ascii character, or a
5261KEY_MOUSE special key associated with a mouse event.</P
5262><P
5263>The menu_driver accepts following navigational requests.
5264<PRE
5265CLASS="PROGRAMLISTING"
5266>&#13;     REQ_LEFT_ITEM         Move left to an item.
5267     REQ_RIGHT_ITEM      Move right to an item.
5268     REQ_UP_ITEM         Move up to an item.
5269     REQ_DOWN_ITEM       Move down to an item.
5270     REQ_SCR_ULINE       Scroll up a line.
5271     REQ_SCR_DLINE          Scroll down a line.
5272     REQ_SCR_DPAGE          Scroll down a page.
5273     REQ_SCR_UPAGE         Scroll up a page.
5274     REQ_FIRST_ITEM     Move to the first item.
5275     REQ_LAST_ITEM         Move to the last item.
5276     REQ_NEXT_ITEM         Move to the next item.
5277     REQ_PREV_ITEM         Move to the previous item.
5278     REQ_TOGGLE_ITEM     Select/deselect an item.
5279     REQ_CLEAR_PATTERN     Clear the menu pattern buffer.
5280     REQ_BACK_PATTERN      Delete the previous character from the pattern buffer.
5281     REQ_NEXT_MATCH     Move to the next item matching the pattern match.
5282     REQ_PREV_MATCH     Move to the previous item matching the pattern match.&#13;</PRE
5283></P
5284><P
5285>Don't get overwhelmed by the number of options. We will see them slowly one
5286after another. The options of interest in this example are REQ_UP_ITEM and
5287REQ_DOWN_ITEM.  These two options when passed to menu_driver, menu driver
5288updates the current item to one item up or down respectively.</P
5289></DIV
5290><DIV
5291CLASS="SECT2"
5292><HR><H3
5293CLASS="SECT2"
5294><A
5295NAME="MENUDRIVER"
5296>17.3. Menu Driver: The work horse of the menu system</A
5297></H3
5298><P
5299>As you have seen in the above example, menu_driver plays an important role in
5300updating the menu. It is very important to understand various options it takes
5301and what they do.  As explained above, the second parameter to menu_driver() can
5302be either a navigational request, a printable character or a KEY_MOUSE key.
5303Let's dissect the different navigational requests.</P
5304><P
5305></P
5306><UL
5307><LI
5308><P
5309><SPAN
5310CLASS="emphasis"
5311><I
5312CLASS="EMPHASIS"
5313>REQ_LEFT_ITEM and REQ_RIGHT_ITEM</I
5314></SPAN
5315></P
5316><P
5317>A Menu can be displayed with multiple columns for more than one item. This can
5318be done by using the <TT
5319CLASS="LITERAL"
5320>menu_format()</TT
5321>function.
5322When a multi columnar menu is displayed these requests cause the menu driver to
5323move the current selection to left or right.</P
5324></LI
5325><LI
5326><P
5327><SPAN
5328CLASS="emphasis"
5329><I
5330CLASS="EMPHASIS"
5331>REQ_UP_ITEM and REQ_DOWN_ITEM </I
5332></SPAN
5333> </P
5334><P
5335>These two options you have seen in the above example. These options when given,
5336makes the menu_driver to move the current selection to an item up or down.</P
5337></LI
5338><LI
5339><P
5340> <SPAN
5341CLASS="emphasis"
5342><I
5343CLASS="EMPHASIS"
5344>REQ_SCR_* options</I
5345></SPAN
5346> </P
5347><P
5348>The four options REQ_SCR_ULINE, REQ_SCR_DLINE, REQ_SCR_DPAGE, REQ_SCR_UPAGE are
5349related to scrolling. If all the items in the menu cannot be displayed in the
5350menu sub window, then the menu is scrollable. These requests can be given to the
5351menu_driver to do the scrolling either one line up, down or one page down or up
5352respectively. </P
5353></LI
5354><LI
5355><P
5356><SPAN
5357CLASS="emphasis"
5358><I
5359CLASS="EMPHASIS"
5360>REQ_FIRST_ITEM, REQ_LAST_ITEM, REQ_NEXT_ITEM and
5361REQ_PREV_ITEM </I
5362></SPAN
5363> </P
5364><P
5365>These requests are self explanatory.</P
5366></LI
5367><LI
5368><P
5369> <SPAN
5370CLASS="emphasis"
5371><I
5372CLASS="EMPHASIS"
5373>REQ_TOGGLE_ITEM</I
5374></SPAN
5375> </P
5376><P
5377>This request when given, toggles the present selection. This option is to be
5378used only in a multi valued menu. So to use this request the option O_ONEVALUE
5379must be off. This option can be made off or on with set_menu_opts().</P
5380></LI
5381><LI
5382><P
5383> <SPAN
5384CLASS="emphasis"
5385><I
5386CLASS="EMPHASIS"
5387>Pattern Requests </I
5388></SPAN
5389></P
5390><P
5391>Every menu has an associated pattern buffer, which is used to find the nearest
5392match to the ascii characters entered by the user. Whenever ascii characters are
5393given to menu_driver, it puts in to the pattern buffer. It also tries to find
5394the nearest match to the pattern in the items list and moves current selection
5395to that item. The request REQ_CLEAR_PATTERN clears the pattern buffer. The
5396request REQ_BACK_PATTERN deletes the previous character in the pattern buffer.
5397In case the pattern matches more than one item then the matched items can be
5398cycled through REQ_NEXT_MATCH and REQ_PREV_MATCH which move the current
5399selection to the next and previous matches respectively.</P
5400></LI
5401><LI
5402><P
5403> <SPAN
5404CLASS="emphasis"
5405><I
5406CLASS="EMPHASIS"
5407>Mouse Requests</I
5408></SPAN
5409></P
5410><P
5411>In case of KEY_MOUSE requests, according to the mouse position an action is
5412taken accordingly. The action to be taken is explained in the man page as, </P
5413><PRE
5414CLASS="PROGRAMLISTING"
5415><SPAN
5416CLASS="emphasis"
5417><I
5418CLASS="EMPHASIS"
5419>       If  the  second argument is the KEY_MOUSE special key, the
5420       associated mouse event is translated into one of the above
5421       pre-defined  requests.   Currently only clicks in the user
5422       window (e.g. inside the menu display area or  the  decora&shy;
5423       tion  window)  are handled. If you click above the display
5424       region of the menu, a REQ_SCR_ULINE is generated,  if  you
5425       doubleclick  a  REQ_SCR_UPAGE  is  generated  and  if  you
5426       tripleclick a REQ_FIRST_ITEM is generated.  If  you  click
5427       below  the  display region of the menu, a REQ_SCR_DLINE is
5428       generated, if you doubleclick a REQ_SCR_DPAGE is generated
5429       and  if  you  tripleclick a REQ_LAST_ITEM is generated. If
5430       you click at an item inside the display area of the  menu,
5431       the menu cursor is positioned to that item.</I
5432></SPAN
5433></PRE
5434></LI
5435></UL
5436><P
5437>Each of the above requests will be explained in the following lines with several
5438examples whenever appropriate.</P
5439></DIV
5440><DIV
5441CLASS="SECT2"
5442><HR><H3
5443CLASS="SECT2"
5444><A
5445NAME="MENUWINDOWS"
5446>17.4. Menu Windows</A
5447></H3
5448><P
5449>Every menu created is associated with a window and a sub window. The menu window
5450displays any title or border associated with the menu. The menu sub window
5451displays the menu items currently available for selection. But we didn't specify
5452any window or sub window in the simple example. When a window is not specified,
5453stdscr is taken as the main window, and then menu system calculates the sub
5454window size required for the display of items. Then items are displayed in the
5455calculated sub window. So let's play with these windows and display a menu with
5456a border and a title.</P
5457><DIV
5458CLASS="EXAMPLE"
5459><A
5460NAME="MMEWI"
5461></A
5462><P
5463><B
5464>Example 19.  Menu Windows Usage example </B
5465></P
5466><PRE
5467CLASS="PROGRAMLISTING"
5468><SPAN
5469CLASS="INLINEMEDIAOBJECT"
5470>#include &#60;menu.h&#62;
5471
5472#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
5473#define CTRLD 	4
5474
5475char *choices[] = {
5476                        "Choice 1",
5477                        "Choice 2",
5478                        "Choice 3",
5479                        "Choice 4",
5480                        "Exit",
5481                        (char *)NULL,
5482                  };
5483void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color);
5484
5485int main()
5486{	ITEM **my_items;
5487	int c;
5488	MENU *my_menu;
5489        WINDOW *my_menu_win;
5490        int n_choices, i;
5491
5492	/* Initialize curses */
5493	initscr();
5494	start_color();
5495        cbreak();
5496        noecho();
5497	keypad(stdscr, TRUE);
5498	init_pair(1, COLOR_RED, COLOR_BLACK);
5499
5500	/* Create items */
5501        n_choices = ARRAY_SIZE(choices);
5502        my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *));
5503        for(i = 0; i &#60; n_choices; ++i)
5504                my_items[i] = new_item(choices[i], choices[i]);
5505
5506	/* Crate menu */
5507	my_menu = new_menu((ITEM **)my_items);
5508
5509	/* Create the window to be associated with the menu */
5510        my_menu_win = newwin(10, 40, 4, 4);
5511        keypad(my_menu_win, TRUE);
5512
5513	/* Set main window and sub window */
5514        set_menu_win(my_menu, my_menu_win);
5515        set_menu_sub(my_menu, derwin(my_menu_win, 6, 38, 3, 1));
5516
5517	/* Set menu mark to the string " * " */
5518        set_menu_mark(my_menu, " * ");
5519
5520	/* Print a border around the main window and print a title */
5521        box(my_menu_win, 0, 0);
5522	print_in_middle(my_menu_win, 1, 0, 40, "My Menu", COLOR_PAIR(1));
5523	mvwaddch(my_menu_win, 2, 0, ACS_LTEE);
5524	mvwhline(my_menu_win, 2, 1, ACS_HLINE, 38);
5525	mvwaddch(my_menu_win, 2, 39, ACS_RTEE);
5526	mvprintw(LINES - 2, 0, "F1 to exit");
5527	refresh();
5528
5529	/* Post the menu */
5530	post_menu(my_menu);
5531	wrefresh(my_menu_win);
5532
5533	while((c = wgetch(my_menu_win)) != KEY_F(1))
5534	{       switch(c)
5535	        {	case KEY_DOWN:
5536				menu_driver(my_menu, REQ_DOWN_ITEM);
5537				break;
5538			case KEY_UP:
5539				menu_driver(my_menu, REQ_UP_ITEM);
5540				break;
5541		}
5542                wrefresh(my_menu_win);
5543	}
5544
5545	/* Unpost and free all the memory taken up */
5546        unpost_menu(my_menu);
5547        free_menu(my_menu);
5548        for(i = 0; i &#60; n_choices; ++i)
5549                free_item(my_items[i]);
5550	endwin();
5551}
5552
5553void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color)
5554{	int length, x, y;
5555	float temp;
5556
5557	if(win == NULL)
5558		win = stdscr;
5559	getyx(win, y, x);
5560	if(startx != 0)
5561		x = startx;
5562	if(starty != 0)
5563		y = starty;
5564	if(width == 0)
5565		width = 80;
5566
5567	length = strlen(string);
5568	temp = (width - length)/ 2;
5569	x = startx + (int)temp;
5570	wattron(win, color);
5571	mvwprintw(win, y, x, "%s", string);
5572	wattroff(win, color);
5573	refresh();
5574}</SPAN
5575></PRE
5576></DIV
5577><P
5578>This example creates a menu with a title, border, a fancy line separating title
5579and the items. As you can see, in order to attach a window to a menu the
5580function set_menu_win() has to be used. Then we attach the sub window also. This
5581displays the items in the sub window.  You can also set the mark string which
5582gets displayed to the left of the selected item with set_menu_mark().</P
5583></DIV
5584><DIV
5585CLASS="SECT2"
5586><HR><H3
5587CLASS="SECT2"
5588><A
5589NAME="SCROLLMENUS"
5590>17.5. Scrolling Menus</A
5591></H3
5592><P
5593>If the sub window given for a window is not big enough to show all the items,
5594then the menu will be scrollable. When you are on the last item in the present
5595list, if you send REQ_DOWN_ITEM, it gets translated into REQ_SCR_DLINE and the
5596menu scrolls by one item.  You can manually give REQ_SCR_ operations to do
5597scrolling. Let's see how it can be done.</P
5598><DIV
5599CLASS="EXAMPLE"
5600><A
5601NAME="MMESC"
5602></A
5603><P
5604><B
5605>Example 20.  Scrolling Menus example </B
5606></P
5607><PRE
5608CLASS="PROGRAMLISTING"
5609><SPAN
5610CLASS="INLINEMEDIAOBJECT"
5611>#include &#60;curses.h&#62;
5612#include &#60;menu.h&#62;
5613
5614#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
5615#define CTRLD 	4
5616
5617char *choices[] = {
5618                        "Choice 1",
5619                        "Choice 2",
5620                        "Choice 3",
5621                        "Choice 4",
5622			"Choice 5",
5623			"Choice 6",
5624			"Choice 7",
5625			"Choice 8",
5626			"Choice 9",
5627			"Choice 10",
5628                        "Exit",
5629                        (char *)NULL,
5630                  };
5631void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color);
5632
5633int main()
5634{	ITEM **my_items;
5635	int c;
5636	MENU *my_menu;
5637        WINDOW *my_menu_win;
5638        int n_choices, i;
5639
5640	/* Initialize curses */
5641	initscr();
5642	start_color();
5643        cbreak();
5644        noecho();
5645	keypad(stdscr, TRUE);
5646	init_pair(1, COLOR_RED, COLOR_BLACK);
5647	init_pair(2, COLOR_CYAN, COLOR_BLACK);
5648
5649	/* Create items */
5650        n_choices = ARRAY_SIZE(choices);
5651        my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *));
5652        for(i = 0; i &#60; n_choices; ++i)
5653                my_items[i] = new_item(choices[i], choices[i]);
5654
5655	/* Crate menu */
5656	my_menu = new_menu((ITEM **)my_items);
5657
5658	/* Create the window to be associated with the menu */
5659        my_menu_win = newwin(10, 40, 4, 4);
5660        keypad(my_menu_win, TRUE);
5661
5662	/* Set main window and sub window */
5663        set_menu_win(my_menu, my_menu_win);
5664        set_menu_sub(my_menu, derwin(my_menu_win, 6, 38, 3, 1));
5665	set_menu_format(my_menu, 5, 1);
5666
5667	/* Set menu mark to the string " * " */
5668        set_menu_mark(my_menu, " * ");
5669
5670	/* Print a border around the main window and print a title */
5671        box(my_menu_win, 0, 0);
5672	print_in_middle(my_menu_win, 1, 0, 40, "My Menu", COLOR_PAIR(1));
5673	mvwaddch(my_menu_win, 2, 0, ACS_LTEE);
5674	mvwhline(my_menu_win, 2, 1, ACS_HLINE, 38);
5675	mvwaddch(my_menu_win, 2, 39, ACS_RTEE);
5676
5677	/* Post the menu */
5678	post_menu(my_menu);
5679	wrefresh(my_menu_win);
5680
5681	attron(COLOR_PAIR(2));
5682	mvprintw(LINES - 2, 0, "Use PageUp and PageDown to scoll down or up a page of items");
5683	mvprintw(LINES - 1, 0, "Arrow Keys to navigate (F1 to Exit)");
5684	attroff(COLOR_PAIR(2));
5685	refresh();
5686
5687	while((c = wgetch(my_menu_win)) != KEY_F(1))
5688	{       switch(c)
5689	        {	case KEY_DOWN:
5690				menu_driver(my_menu, REQ_DOWN_ITEM);
5691				break;
5692			case KEY_UP:
5693				menu_driver(my_menu, REQ_UP_ITEM);
5694				break;
5695			case KEY_NPAGE:
5696				menu_driver(my_menu, REQ_SCR_DPAGE);
5697				break;
5698			case KEY_PPAGE:
5699				menu_driver(my_menu, REQ_SCR_UPAGE);
5700				break;
5701		}
5702                wrefresh(my_menu_win);
5703	}
5704
5705	/* Unpost and free all the memory taken up */
5706        unpost_menu(my_menu);
5707        free_menu(my_menu);
5708        for(i = 0; i &#60; n_choices; ++i)
5709                free_item(my_items[i]);
5710	endwin();
5711}
5712
5713void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color)
5714{	int length, x, y;
5715	float temp;
5716
5717	if(win == NULL)
5718		win = stdscr;
5719	getyx(win, y, x);
5720	if(startx != 0)
5721		x = startx;
5722	if(starty != 0)
5723		y = starty;
5724	if(width == 0)
5725		width = 80;
5726
5727	length = strlen(string);
5728	temp = (width - length)/ 2;
5729	x = startx + (int)temp;
5730	wattron(win, color);
5731	mvwprintw(win, y, x, "%s", string);
5732	wattroff(win, color);
5733	refresh();
5734}</SPAN
5735></PRE
5736></DIV
5737><P
5738>This program is self-explanatory. In this example the number of choices has been
5739increased to ten, which is larger than our sub window size which can hold 6
5740items.  This message has to be explicitly conveyed to the menu system with the
5741function set_menu_format(). In here we specify the number of rows and columns we
5742want to be displayed for a single page. We can specify any number of items to be
5743shown, in the rows variables, if it is less than the height of the sub window.
5744If the key pressed by the user is a PAGE UP or PAGE DOWN, the menu is scrolled a
5745page due to the requests (REQ_SCR_DPAGE and REQ_SCR_UPAGE) given to
5746menu_driver().</P
5747></DIV
5748><DIV
5749CLASS="SECT2"
5750><HR><H3
5751CLASS="SECT2"
5752><A
5753NAME="MULTICOLUMN"
5754>17.6. Multi Columnar Menus</A
5755></H3
5756><P
5757>In the above example you have seen how to use the function set_menu_format(). I
5758didn't mention what the cols variable (third parameter) does. Well, If your sub
5759window is wide enough, you can opt to display more than one item per row. This
5760can be specified in the cols variable. To make things simpler, the following
5761example doesn't show descriptions for the items.</P
5762><DIV
5763CLASS="EXAMPLE"
5764><A
5765NAME="MMEMUCO"
5766></A
5767><P
5768><B
5769>Example 21.  Milt Columnar Menus Example </B
5770></P
5771><PRE
5772CLASS="PROGRAMLISTING"
5773><SPAN
5774CLASS="INLINEMEDIAOBJECT"
5775>#include &#60;curses.h&#62;
5776#include &#60;menu.h&#62;
5777
5778#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
5779#define CTRLD 	4
5780
5781char *choices[] = {
5782                        "Choice 1", "Choice 2", "Choice 3", "Choice 4", "Choice 5",
5783			"Choice 6", "Choice 7", "Choice 8", "Choice 9", "Choice 10",
5784			"Choice 11", "Choice 12", "Choice 13", "Choice 14", "Choice 15",
5785			"Choice 16", "Choice 17", "Choice 18", "Choice 19", "Choice 20",
5786                        "Exit",
5787                        (char *)NULL,
5788                  };
5789
5790int main()
5791{	ITEM **my_items;
5792	int c;
5793	MENU *my_menu;
5794        WINDOW *my_menu_win;
5795        int n_choices, i;
5796
5797	/* Initialize curses */
5798	initscr();
5799	start_color();
5800        cbreak();
5801        noecho();
5802	keypad(stdscr, TRUE);
5803	init_pair(1, COLOR_RED, COLOR_BLACK);
5804	init_pair(2, COLOR_CYAN, COLOR_BLACK);
5805
5806	/* Create items */
5807        n_choices = ARRAY_SIZE(choices);
5808        my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *));
5809        for(i = 0; i &#60; n_choices; ++i)
5810                my_items[i] = new_item(choices[i], choices[i]);
5811
5812	/* Crate menu */
5813	my_menu = new_menu((ITEM **)my_items);
5814
5815	/* Set menu option not to show the description */
5816	menu_opts_off(my_menu, O_SHOWDESC);
5817
5818	/* Create the window to be associated with the menu */
5819        my_menu_win = newwin(10, 70, 4, 4);
5820        keypad(my_menu_win, TRUE);
5821
5822	/* Set main window and sub window */
5823        set_menu_win(my_menu, my_menu_win);
5824        set_menu_sub(my_menu, derwin(my_menu_win, 6, 68, 3, 1));
5825	set_menu_format(my_menu, 5, 3);
5826	set_menu_mark(my_menu, " * ");
5827
5828	/* Print a border around the main window and print a title */
5829        box(my_menu_win, 0, 0);
5830
5831	attron(COLOR_PAIR(2));
5832	mvprintw(LINES - 3, 0, "Use PageUp and PageDown to scroll");
5833	mvprintw(LINES - 2, 0, "Use Arrow Keys to navigate (F1 to Exit)");
5834	attroff(COLOR_PAIR(2));
5835	refresh();
5836
5837	/* Post the menu */
5838	post_menu(my_menu);
5839	wrefresh(my_menu_win);
5840
5841	while((c = wgetch(my_menu_win)) != KEY_F(1))
5842	{       switch(c)
5843	        {	case KEY_DOWN:
5844				menu_driver(my_menu, REQ_DOWN_ITEM);
5845				break;
5846			case KEY_UP:
5847				menu_driver(my_menu, REQ_UP_ITEM);
5848				break;
5849			case KEY_LEFT:
5850				menu_driver(my_menu, REQ_LEFT_ITEM);
5851				break;
5852			case KEY_RIGHT:
5853				menu_driver(my_menu, REQ_RIGHT_ITEM);
5854				break;
5855			case KEY_NPAGE:
5856				menu_driver(my_menu, REQ_SCR_DPAGE);
5857				break;
5858			case KEY_PPAGE:
5859				menu_driver(my_menu, REQ_SCR_UPAGE);
5860				break;
5861		}
5862                wrefresh(my_menu_win);
5863	}
5864
5865	/* Unpost and free all the memory taken up */
5866        unpost_menu(my_menu);
5867        free_menu(my_menu);
5868        for(i = 0; i &#60; n_choices; ++i)
5869                free_item(my_items[i]);
5870	endwin();
5871}</SPAN
5872></PRE
5873></DIV
5874><P
5875>Watch the function call to set_menu_format(). It specifies the number of columns
5876to be 3, thus displaying 3 items per row. We have also switched off the showing
5877descriptions with the function menu_opts_off(). There are couple of functions
5878set_menu_opts(),  menu_opts_on() and menu_opts() which can be used to manipulate
5879menu options. The following menu options can be specified.</P
5880><PRE
5881CLASS="PROGRAMLISTING"
5882>       O_ONEVALUE
5883            Only one item can be selected for this menu.
5884
5885       O_SHOWDESC
5886            Display  the  item  descriptions  when  the  menu  is
5887            posted.
5888
5889       O_ROWMAJOR
5890            Display the menu in row-major order.
5891
5892       O_IGNORECASE
5893            Ignore the case when pattern-matching.
5894
5895       O_SHOWMATCH
5896            Move the cursor to within the item  name  while  pat&shy;
5897            tern-matching.
5898
5899       O_NONCYCLIC
5900            Don't   wrap   around  next-item  and  previous-item,
5901            requests to the other end of the menu.</PRE
5902><P
5903>All options are on by default. You can switch specific attributes on or off with
5904menu_opts_on() and menu_opts_off() functions. You can also use set_menu_opts()
5905to directly specify the options. The argument to this function should be a OR ed
5906value of some of those above constants. The function menu_opts() can be used to
5907find out a menu's present options. </P
5908></DIV
5909><DIV
5910CLASS="SECT2"
5911><HR><H3
5912CLASS="SECT2"
5913><A
5914NAME="MULTIVALUEMENUS"
5915>17.7. Multi Valued Menus</A
5916></H3
5917><P
5918>You might be wondering what if you switch off the option O_ONEVALUE. Then the
5919menu becomes multi-valued. That means you can select more than one item. This
5920brings us to the request REQ_TOGGLE_ITEM. Let's see it in action.</P
5921><DIV
5922CLASS="EXAMPLE"
5923><A
5924NAME="MMETO"
5925></A
5926><P
5927><B
5928>Example 22.  Multi Valued Menus example </B
5929></P
5930><PRE
5931CLASS="PROGRAMLISTING"
5932><SPAN
5933CLASS="INLINEMEDIAOBJECT"
5934>#include &#60;curses.h&#62;
5935#include &#60;menu.h&#62;
5936
5937#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
5938#define CTRLD 	4
5939
5940char *choices[] = {
5941                        "Choice 1",
5942                        "Choice 2",
5943                        "Choice 3",
5944                        "Choice 4",
5945			"Choice 5",
5946			"Choice 6",
5947			"Choice 7",
5948                        "Exit",
5949                  };
5950
5951int main()
5952{	ITEM **my_items;
5953	int c;
5954	MENU *my_menu;
5955        int n_choices, i;
5956	ITEM *cur_item;
5957
5958	/* Initialize curses */
5959	initscr();
5960        cbreak();
5961        noecho();
5962	keypad(stdscr, TRUE);
5963
5964	/* Initialize items */
5965        n_choices = ARRAY_SIZE(choices);
5966        my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *));
5967        for(i = 0; i &#60; n_choices; ++i)
5968                my_items[i] = new_item(choices[i], choices[i]);
5969	my_items[n_choices] = (ITEM *)NULL;
5970
5971	my_menu = new_menu((ITEM **)my_items);
5972
5973	/* Make the menu multi valued */
5974	menu_opts_off(my_menu, O_ONEVALUE);
5975
5976	mvprintw(LINES - 3, 0, "Use &#60;SPACE&#62; to select or unselect an item.");
5977	mvprintw(LINES - 2, 0, "&#60;ENTER&#62; to see presently selected items(F1 to Exit)");
5978	post_menu(my_menu);
5979	refresh();
5980
5981	while((c = getch()) != KEY_F(1))
5982	{       switch(c)
5983	        {	case KEY_DOWN:
5984				menu_driver(my_menu, REQ_DOWN_ITEM);
5985				break;
5986			case KEY_UP:
5987				menu_driver(my_menu, REQ_UP_ITEM);
5988				break;
5989			case ' ':
5990				menu_driver(my_menu, REQ_TOGGLE_ITEM);
5991				break;
5992			case 10:	/* Enter */
5993			{	char temp[200];
5994				ITEM **items;
5995
5996				items = menu_items(my_menu);
5997				temp[0] = '\0';
5998				for(i = 0; i &#60; item_count(my_menu); ++i)
5999					if(item_value(items[i]) == TRUE)
6000					{	strcat(temp, item_name(items[i]));
6001						strcat(temp, " ");
6002					}
6003				move(20, 0);
6004				clrtoeol();
6005				mvprintw(20, 0, temp);
6006				refresh();
6007			}
6008			break;
6009		}
6010	}
6011
6012	free_item(my_items[0]);
6013        free_item(my_items[1]);
6014	free_menu(my_menu);
6015	endwin();
6016}
6017	</SPAN
6018></PRE
6019></DIV
6020><P
6021>Whew, A lot of new functions. Let's take them one after another. Firstly, the
6022REQ_TOGGLE_ITEM.  In a multi-valued menu, the user should be allowed to select
6023or un select more than one item. The request REQ_TOGGLE_ITEM toggles the present
6024selection. In this case when space is pressed REQ_TOGGLE_ITEM request is sent to
6025menu_driver to achieve the result.</P
6026><P
6027>Now when the user presses &lt;ENTER&gt; we show the items he presently selected.
6028First we find out the items associated with the menu using the function
6029menu_items(). Then we loop through the items to find out if the item is selected
6030or not. The function item_value() returns TRUE if an item is selected. The
6031function item_count() returns the number of items in the menu. The item name can
6032be found with item_name(). You can also find the description associated with an
6033item using item_description().</P
6034></DIV
6035><DIV
6036CLASS="SECT2"
6037><HR><H3
6038CLASS="SECT2"
6039><A
6040NAME="MENUOPT"
6041>17.8. Menu Options</A
6042></H3
6043><P
6044>Well, by this time you must be itching for some difference in your menu, with
6045lots of functionality. I know. You want Colors !!!. You want to create nice
6046menus similar to those text mode <A
6047HREF="http://www.jersey.net/~debinjoe/games/"
6048TARGET="_top"
6049>dos games</A
6050>. The functions
6051set_menu_fore() and set_menu_back() can be used to change the attribute of the
6052selected item and unselected item. The names are misleading. They don't change
6053menu's foreground or background which would have been useless. </P
6054><P
6055>The function set_menu_grey() can be used to set the display attribute for the
6056non-selectable items in the menu. This brings us to the interesting option for
6057an item the one and only O_SELECTABLE. We can turn it off by the function
6058item_opts_off() and after that that item is not selectable. It's like a grayed
6059item in those fancy windows menus. Let's put these concepts in practice with
6060this example</P
6061><DIV
6062CLASS="EXAMPLE"
6063><A
6064NAME="MMEAT"
6065></A
6066><P
6067><B
6068>Example 23.  Menu Options example </B
6069></P
6070><PRE
6071CLASS="PROGRAMLISTING"
6072><SPAN
6073CLASS="INLINEMEDIAOBJECT"
6074>#include &#60;menu.h&#62;
6075
6076#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
6077#define CTRLD 	4
6078
6079char *choices[] = {
6080                        "Choice 1",
6081                        "Choice 2",
6082                        "Choice 3",
6083                        "Choice 4",
6084			"Choice 5",
6085			"Choice 6",
6086			"Choice 7",
6087                        "Exit",
6088                  };
6089
6090int main()
6091{	ITEM **my_items;
6092	int c;
6093	MENU *my_menu;
6094        int n_choices, i;
6095	ITEM *cur_item;
6096
6097	/* Initialize curses */
6098	initscr();
6099	start_color();
6100        cbreak();
6101        noecho();
6102	keypad(stdscr, TRUE);
6103	init_pair(1, COLOR_RED, COLOR_BLACK);
6104	init_pair(2, COLOR_GREEN, COLOR_BLACK);
6105	init_pair(3, COLOR_MAGENTA, COLOR_BLACK);
6106
6107	/* Initialize items */
6108        n_choices = ARRAY_SIZE(choices);
6109        my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *));
6110        for(i = 0; i &#60; n_choices; ++i)
6111                my_items[i] = new_item(choices[i], choices[i]);
6112	my_items[n_choices] = (ITEM *)NULL;
6113	item_opts_off(my_items[3], O_SELECTABLE);
6114	item_opts_off(my_items[6], O_SELECTABLE);
6115
6116	/* Create menu */
6117	my_menu = new_menu((ITEM **)my_items);
6118
6119	/* Set fore ground and back ground of the menu */
6120	set_menu_fore(my_menu, COLOR_PAIR(1) | A_REVERSE);
6121	set_menu_back(my_menu, COLOR_PAIR(2));
6122	set_menu_grey(my_menu, COLOR_PAIR(3));
6123
6124	/* Post the menu */
6125	mvprintw(LINES - 3, 0, "Press &#60;ENTER&#62; to see the option selected");
6126	mvprintw(LINES - 2, 0, "Up and Down arrow keys to naviage (F1 to Exit)");
6127	post_menu(my_menu);
6128	refresh();
6129
6130	while((c = getch()) != KEY_F(1))
6131	{       switch(c)
6132	        {	case KEY_DOWN:
6133				menu_driver(my_menu, REQ_DOWN_ITEM);
6134				break;
6135			case KEY_UP:
6136				menu_driver(my_menu, REQ_UP_ITEM);
6137				break;
6138			case 10: /* Enter */
6139				move(20, 0);
6140				clrtoeol();
6141				mvprintw(20, 0, "Item selected is : %s",
6142						item_name(current_item(my_menu)));
6143				pos_menu_cursor(my_menu);
6144				break;
6145		}
6146	}
6147	unpost_menu(my_menu);
6148	for(i = 0; i &#60; n_choices; ++i)
6149		free_item(my_items[i]);
6150	free_menu(my_menu);
6151	endwin();
6152}
6153	</SPAN
6154></PRE
6155></DIV
6156></DIV
6157><DIV
6158CLASS="SECT2"
6159><HR><H3
6160CLASS="SECT2"
6161><A
6162NAME="MENUUSERPTR"
6163>17.9. The useful User Pointer</A
6164></H3
6165><P
6166>We can associate a user pointer with each item in the menu. It works the same
6167way as user pointer in panels. It's not touched by menu system. You can store
6168any thing you like in that. I usually use it to store the function to be
6169executed when the menu option is chosen (It's selected and may be the user
6170pressed &lt;ENTER&gt;);</P
6171><DIV
6172CLASS="EXAMPLE"
6173><A
6174NAME="MMEUS"
6175></A
6176><P
6177><B
6178>Example 24.  Menu User Pointer Usage </B
6179></P
6180><PRE
6181CLASS="PROGRAMLISTING"
6182><SPAN
6183CLASS="INLINEMEDIAOBJECT"
6184>#include &#60;curses.h&#62;
6185#include &#60;menu.h&#62;
6186
6187#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
6188#define CTRLD 	4
6189
6190char *choices[] = {
6191                        "Choice 1",
6192                        "Choice 2",
6193                        "Choice 3",
6194                        "Choice 4",
6195			"Choice 5",
6196			"Choice 6",
6197			"Choice 7",
6198                        "Exit",
6199                  };
6200void func(char *name);
6201
6202int main()
6203{	ITEM **my_items;
6204	int c;
6205	MENU *my_menu;
6206        int n_choices, i;
6207	ITEM *cur_item;
6208
6209	/* Initialize curses */
6210	initscr();
6211	start_color();
6212        cbreak();
6213        noecho();
6214	keypad(stdscr, TRUE);
6215	init_pair(1, COLOR_RED, COLOR_BLACK);
6216	init_pair(2, COLOR_GREEN, COLOR_BLACK);
6217	init_pair(3, COLOR_MAGENTA, COLOR_BLACK);
6218
6219	/* Initialize items */
6220        n_choices = ARRAY_SIZE(choices);
6221        my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *));
6222        for(i = 0; i &#60; n_choices; ++i)
6223	{       my_items[i] = new_item(choices[i], choices[i]);
6224		/* Set the user pointer */
6225		set_item_userptr(my_items[i], func);
6226	}
6227	my_items[n_choices] = (ITEM *)NULL;
6228
6229	/* Create menu */
6230	my_menu = new_menu((ITEM **)my_items);
6231
6232	/* Post the menu */
6233	mvprintw(LINES - 3, 0, "Press &#60;ENTER&#62; to see the option selected");
6234	mvprintw(LINES - 2, 0, "Up and Down arrow keys to naviage (F1 to Exit)");
6235	post_menu(my_menu);
6236	refresh();
6237
6238	while((c = getch()) != KEY_F(1))
6239	{       switch(c)
6240	        {	case KEY_DOWN:
6241				menu_driver(my_menu, REQ_DOWN_ITEM);
6242				break;
6243			case KEY_UP:
6244				menu_driver(my_menu, REQ_UP_ITEM);
6245				break;
6246			case 10: /* Enter */
6247			{	ITEM *cur;
6248				void (*p)(char *);
6249
6250				cur = current_item(my_menu);
6251				p = item_userptr(cur);
6252				p((char *)item_name(cur));
6253				pos_menu_cursor(my_menu);
6254				break;
6255			}
6256			break;
6257		}
6258	}
6259	unpost_menu(my_menu);
6260	for(i = 0; i &#60; n_choices; ++i)
6261		free_item(my_items[i]);
6262	free_menu(my_menu);
6263	endwin();
6264}
6265
6266void func(char *name)
6267{	move(20, 0);
6268	clrtoeol();
6269	mvprintw(20, 0, "Item selected is : %s", name);
6270}	</SPAN
6271></PRE
6272></DIV
6273></DIV
6274></DIV
6275><DIV
6276CLASS="SECT1"
6277><HR><H2
6278CLASS="SECT1"
6279><A
6280NAME="FORMS"
6281>18. Forms Library</A
6282></H2
6283><P
6284>Well. If you have seen those forms on web pages which take input from users and
6285do various kinds of things, you might be wondering how would any one create such
6286forms in text mode display. It's quite difficult to write those nifty forms in
6287plain ncurses. Forms library tries to provide a basic frame work to build and
6288maintain forms with ease. It has lot of features(functions) which manage
6289validation, dynamic expansion of fields etc.. Let's see it in full flow.</P
6290><P
6291>A form is a collection of fields; each field can be either a label(static text)
6292or a data-entry location. The forms also library provides functions to divide
6293forms into multiple pages. </P
6294><DIV
6295CLASS="SECT2"
6296><HR><H3
6297CLASS="SECT2"
6298><A
6299NAME="FORMBASICS"
6300>18.1. The Basics</A
6301></H3
6302><P
6303>Forms are created in much the same way as menus. First the fields related to the
6304form are created with new_field(). You can set options for the fields, so that
6305they can be displayed with some fancy attributes, validated before the field
6306looses focus etc.. Then the fields are attached to form. After this, the form
6307can be posted to display and is ready to receive inputs. On the similar lines to
6308menu_driver(), the form is manipulated with form_driver(). We can send requests
6309to form_driver to move focus to a certain field, move cursor to end of the field
6310etc..  After the user enters values in the fields and validation done, form can
6311be unposted and memory allocated can be freed.</P
6312><P
6313>The general flow of control of a forms program looks like this.
6314
6315<P
6316></P
6317><OL
6318TYPE="1"
6319><LI
6320><P
6321>Initialize curses</P
6322></LI
6323><LI
6324><P
6325>Create fields using new_field(). You can specify the height and
6326width of the field, and its position on the form.</P
6327></LI
6328><LI
6329><P
6330>Create the forms with new_form() by specifying the fields to be
6331attached with.</P
6332></LI
6333><LI
6334><P
6335>Post the form with form_post() and refresh the screen.</P
6336></LI
6337><LI
6338><P
6339>Process the user requests with a loop and do necessary updates
6340to form with form_driver.</P
6341></LI
6342><LI
6343><P
6344>Unpost the menu with form_unpost()</P
6345></LI
6346><LI
6347><P
6348>Free the memory allocated to menu by free_form()</P
6349></LI
6350><LI
6351><P
6352>Free the memory allocated to the items with free_field()</P
6353></LI
6354><LI
6355><P
6356>End curses</P
6357></LI
6358></OL
6359></P
6360><P
6361>As you can see, working with forms library is much similar to handling menu
6362library.  The following examples will explore various aspects of form
6363processing. Let's start the journey with a simple example.  first.</P
6364></DIV
6365><DIV
6366CLASS="SECT2"
6367><HR><H3
6368CLASS="SECT2"
6369><A
6370NAME="COMPILEFORMS"
6371>18.2. Compiling With the Forms Library</A
6372></H3
6373><P
6374>To use forms library functions, you have to include form.h and to link the
6375program with forms library the flag -lform should be added along with -lncurses
6376in that order.</P
6377><PRE
6378CLASS="PROGRAMLISTING"
6379>    #include &lt;form.h&gt;
6380    .
6381    .
6382    .
6383
6384    compile and link: gcc &lt;program file&gt; -lform -lncurses</PRE
6385><DIV
6386CLASS="EXAMPLE"
6387><A
6388NAME="FFOSI"
6389></A
6390><P
6391><B
6392>Example 25.  Forms Basics </B
6393></P
6394><PRE
6395CLASS="PROGRAMLISTING"
6396><SPAN
6397CLASS="INLINEMEDIAOBJECT"
6398>#include &#60;form.h&#62;
6399
6400int main()
6401{	FIELD *field[3];
6402	FORM  *my_form;
6403	int ch;
6404
6405	/* Initialize curses */
6406	initscr();
6407	cbreak();
6408	noecho();
6409	keypad(stdscr, TRUE);
6410
6411	/* Initialize the fields */
6412	field[0] = new_field(1, 10, 4, 18, 0, 0);
6413	field[1] = new_field(1, 10, 6, 18, 0, 0);
6414	field[2] = NULL;
6415
6416	/* Set field options */
6417	set_field_back(field[0], A_UNDERLINE); 	/* Print a line for the option 	*/
6418	field_opts_off(field[0], O_AUTOSKIP);  	/* Don't go to next field when this */
6419						/* Field is filled up 		*/
6420	set_field_back(field[1], A_UNDERLINE);
6421	field_opts_off(field[1], O_AUTOSKIP);
6422
6423	/* Create the form and post it */
6424	my_form = new_form(field);
6425	post_form(my_form);
6426	refresh();
6427
6428	mvprintw(4, 10, "Value 1:");
6429	mvprintw(6, 10, "Value 2:");
6430	refresh();
6431
6432	/* Loop through to get user requests */
6433	while((ch = getch()) != KEY_F(1))
6434	{	switch(ch)
6435		{	case KEY_DOWN:
6436				/* Go to next field */
6437				form_driver(my_form, REQ_NEXT_FIELD);
6438				/* Go to the end of the present buffer */
6439				/* Leaves nicely at the last character */
6440				form_driver(my_form, REQ_END_LINE);
6441				break;
6442			case KEY_UP:
6443				/* Go to previous field */
6444				form_driver(my_form, REQ_PREV_FIELD);
6445				form_driver(my_form, REQ_END_LINE);
6446				break;
6447			default:
6448				/* If this is a normal character, it gets */
6449				/* Printed				  */
6450				form_driver(my_form, ch);
6451				break;
6452		}
6453	}
6454
6455	/* Un post form and free the memory */
6456	unpost_form(my_form);
6457	free_form(my_form);
6458	free_field(field[0]);
6459	free_field(field[1]);
6460
6461	endwin();
6462	return 0;
6463}</SPAN
6464></PRE
6465></DIV
6466><P
6467>Above example is pretty straight forward. It creates two fields with
6468<TT
6469CLASS="LITERAL"
6470>new_field()</TT
6471>.  new_field() takes height, width,
6472starty, startx, number of offscreen rows and number of additional working
6473buffers. The fifth argument number of offscreen rows specifies how much of the
6474field to be shown. If it is zero, the entire field is always displayed otherwise
6475the form will be scrollable when the user accesses not displayed parts of the
6476field.  The forms library allocates one buffer per field to store the data user
6477enters. Using the last parameter to new_field() we can specify it to allocate
6478some additional buffers.  These can be used for any purpose you like.</P
6479><P
6480>After creating the fields, back ground attribute of both of them is set to an
6481underscore with set_field_back(). The AUTOSKIP option is turned off using
6482field_opts_off().  If this option is turned on, focus will move to the next
6483field in the form once the active field is filled up completely.</P
6484><P
6485>After attaching the fields to the form, it is posted. Here on, user inputs are
6486processed in the while loop, by making corresponding requests to form_driver.
6487The details of all the requests to the form_driver() are explained later.</P
6488></DIV
6489><DIV
6490CLASS="SECT2"
6491><HR><H3
6492CLASS="SECT2"
6493><A
6494NAME="PLAYFIELDS"
6495>18.3. Playing with Fields</A
6496></H3
6497><P
6498>Each form field is associated with a lot of attributes. They can be manipulated
6499to get the required effect and to have fun !!!. So why wait? </P
6500><DIV
6501CLASS="SECT3"
6502><HR><H4
6503CLASS="SECT3"
6504><A
6505NAME="FETCHINFO"
6506>18.3.1. Fetching Size and Location of Field</A
6507></H4
6508><P
6509>The parameters we have given at the time of creation of a field can be retrieved
6510with field_info(). It returns height, width, starty, startx, number of offscreen
6511rows, and number of additional buffers into the parameters given to it. It is a
6512sort of inverse of new_field().</P
6513><PRE
6514CLASS="PROGRAMLISTING"
6515>int field_info(     FIELD *field,              /* field from which to fetch */
6516                    int *height, *int width,   /* field size */
6517                    int *top, int *left,       /* upper left corner */
6518                    int *offscreen,            /* number of offscreen rows */
6519                    int *nbuf);                /* number of working buffers */</PRE
6520></DIV
6521><DIV
6522CLASS="SECT3"
6523><HR><H4
6524CLASS="SECT3"
6525><A
6526NAME="MOVEFIELD"
6527>18.3.2. Moving the field</A
6528></H4
6529><P
6530>The location of the field can be moved to a different position with
6531move_field().</P
6532><PRE
6533CLASS="PROGRAMLISTING"
6534>int move_field(    FIELD *field,              /* field to alter */
6535                   int top, int left);        /* new upper-left corner */</PRE
6536><P
6537>As usual, the changed position can be queried with field_infor().</P
6538></DIV
6539><DIV
6540CLASS="SECT3"
6541><HR><H4
6542CLASS="SECT3"
6543><A
6544NAME="JUSTIFYFIELD"
6545>18.3.3. Field Justification</A
6546></H4
6547><P
6548>The justification to be done for the field can be fixed using the function
6549set_field_just().</P
6550><PRE
6551CLASS="PROGRAMLISTING"
6552>    int set_field_just(FIELD *field,          /* field to alter */
6553               int justmode);         /* mode to set */
6554    int field_just(FIELD *field);          /* fetch justify mode of field */</PRE
6555><P
6556>The justification mode valued accepted and returned by these functions are
6557NO_JUSTIFICATION, JUSTIFY_RIGHT, JUSTIFY_LEFT, or JUSTIFY_CENTER.</P
6558></DIV
6559><DIV
6560CLASS="SECT3"
6561><HR><H4
6562CLASS="SECT3"
6563><A
6564NAME="FIELDDISPATTRIB"
6565>18.3.4. Field Display Attributes</A
6566></H4
6567><P
6568>As you have seen, in the above example, display attribute for the fields can be
6569set with set_field_fore() and setfield_back(). These functions set foreground
6570and background attribute of the fields. You can also specify a pad character
6571which will be filled in the unfilled portion of the field. The pad character is
6572set with a call to set_field_pad(). Default pad value is a space. The functions
6573field_fore(), field_back, field_pad() can be used to query the present
6574foreground, background attributes and pad character for the field. The following
6575list gives the usage of functions.</P
6576><PRE
6577CLASS="PROGRAMLISTING"
6578>&#13;int set_field_fore(FIELD *field,        /* field to alter */
6579                   chtype attr);        /* attribute to set */
6580
6581chtype field_fore(FIELD *field);        /* field to query */
6582                                        /* returns foreground attribute */
6583
6584int set_field_back(FIELD *field,        /* field to alter */
6585                   chtype attr);        /* attribute to set */
6586
6587chtype field_back(FIELD *field);        /* field to query */
6588                                        /* returns background attribute */
6589
6590int set_field_pad(FIELD *field,         /* field to alter */
6591                  int pad);             /* pad character to set */
6592
6593chtype field_pad(FIELD *field);         /* field to query */
6594                                        /* returns present pad character */&#13;</PRE
6595><P
6596>Though above functions seem quite simple, using colors with set_field_fore() may
6597be frustrating in the beginning. Let me first explain about foreground and
6598background attributes of a field. The foreground attribute is associated with
6599the character. That means a character in the field is printed with the attribute
6600you have set with set_field_fore(). Background attribute is the attribute used
6601to fill background of field, whether any character is there or not.  So what
6602about colors? Since colors are always defined in pairs, what is the right way to
6603display colored fields? Here's an example clarifying color attributes.</P
6604><DIV
6605CLASS="EXAMPLE"
6606><A
6607NAME="FFOAT"
6608></A
6609><P
6610><B
6611>Example 26.  Form Attributes example </B
6612></P
6613><PRE
6614CLASS="PROGRAMLISTING"
6615><SPAN
6616CLASS="INLINEMEDIAOBJECT"
6617>#include &#60;form.h&#62;
6618
6619int main()
6620{	FIELD *field[3];
6621	FORM  *my_form;
6622	int ch;
6623
6624	/* Initialize curses */
6625	initscr();
6626	start_color();
6627	cbreak();
6628	noecho();
6629	keypad(stdscr, TRUE);
6630
6631	/* Initialize few color pairs */
6632	init_pair(1, COLOR_WHITE, COLOR_BLUE);
6633	init_pair(2, COLOR_WHITE, COLOR_BLUE);
6634
6635	/* Initialize the fields */
6636	field[0] = new_field(1, 10, 4, 18, 0, 0);
6637	field[1] = new_field(1, 10, 6, 18, 0, 0);
6638	field[2] = NULL;
6639
6640	/* Set field options */
6641	set_field_fore(field[0], COLOR_PAIR(1));/* Put the field with blue background */
6642	set_field_back(field[0], COLOR_PAIR(2));/* and white foreground (characters */
6643						/* are printed in white 	*/
6644	field_opts_off(field[0], O_AUTOSKIP);  	/* Don't go to next field when this */
6645						/* Field is filled up 		*/
6646	set_field_back(field[1], A_UNDERLINE);
6647	field_opts_off(field[1], O_AUTOSKIP);
6648
6649	/* Create the form and post it */
6650	my_form = new_form(field);
6651	post_form(my_form);
6652	refresh();
6653
6654	set_current_field(my_form, field[0]); /* Set focus to the colored field */
6655	mvprintw(4, 10, "Value 1:");
6656	mvprintw(6, 10, "Value 2:");
6657	mvprintw(LINES - 2, 0, "Use UP, DOWN arrow keys to switch between fields");
6658	refresh();
6659
6660	/* Loop through to get user requests */
6661	while((ch = getch()) != KEY_F(1))
6662	{	switch(ch)
6663		{	case KEY_DOWN:
6664				/* Go to next field */
6665				form_driver(my_form, REQ_NEXT_FIELD);
6666				/* Go to the end of the present buffer */
6667				/* Leaves nicely at the last character */
6668				form_driver(my_form, REQ_END_LINE);
6669				break;
6670			case KEY_UP:
6671				/* Go to previous field */
6672				form_driver(my_form, REQ_PREV_FIELD);
6673				form_driver(my_form, REQ_END_LINE);
6674				break;
6675			default:
6676				/* If this is a normal character, it gets */
6677				/* Printed				  */
6678				form_driver(my_form, ch);
6679				break;
6680		}
6681	}
6682
6683	/* Un post form and free the memory */
6684	unpost_form(my_form);
6685	free_form(my_form);
6686	free_field(field[0]);
6687	free_field(field[1]);
6688
6689	endwin();
6690	return 0;
6691}</SPAN
6692></PRE
6693></DIV
6694><P
6695>Play with the color pairs and try to understand the foreground and background
6696attributes. In my programs using color attributes, I usually set only the
6697background with set_field_back(). Curses simply doesn't allow defining
6698individual color attributes. </P
6699></DIV
6700><DIV
6701CLASS="SECT3"
6702><HR><H4
6703CLASS="SECT3"
6704><A
6705NAME="FIELDOPTIONBITS"
6706>18.3.5. Field Option Bits</A
6707></H4
6708><P
6709>There is also a large collection of field option bits you can set to control
6710various aspects of forms processing. You can manipulate them with these
6711functions:</P
6712><PRE
6713CLASS="PROGRAMLISTING"
6714>int set_field_opts(FIELD *field,          /* field to alter */
6715                   int attr);             /* attribute to set */
6716
6717int field_opts_on(FIELD *field,           /* field to alter */
6718                  int attr);              /* attributes to turn on */
6719
6720int field_opts_off(FIELD *field,          /* field to alter */
6721                  int attr);              /* attributes to turn off */
6722
6723int field_opts(FIELD *field);             /* field to query */ </PRE
6724><P
6725>The function set_field_opts() can be used to directly set attributes of a field
6726or you can choose to switch a few attributes on and off with field_opts_on() and
6727field_opts_off() selectively. Anytime you can query the attributes of a field
6728with field_opts(). The following is the list of available options. By default,
6729all options are on.</P
6730><P
6731></P
6732><DIV
6733CLASS="VARIABLELIST"
6734><DL
6735><DT
6736>O_VISIBLE</DT
6737><DD
6738><P
6739>Controls whether the field is visible on the screen.  Can be used
6740during form processing to hide or pop up fields depending on the value
6741of parent fields.</P
6742></DD
6743><DT
6744>O_ACTIVE</DT
6745><DD
6746><P
6747>Controls whether the field is active during forms processing (i.e.
6748visited by form navigation keys).  Can be used to make labels or derived
6749fields with buffer values alterable by the forms application, not the user.</P
6750></DD
6751><DT
6752>O_PUBLIC</DT
6753><DD
6754><P
6755>Controls whether data is displayed during field entry.  If this option is
6756turned off on a field, the library will accept and edit data in that field,
6757but it will not be displayed and the visible field cursor will not move.
6758You can turn off the O_PUBLIC bit to define password fields.</P
6759></DD
6760><DT
6761>O_EDIT</DT
6762><DD
6763><P
6764>Controls whether the field's data can be modified.  When this option is
6765off, all editing requests except <TT
6766CLASS="LITERAL"
6767>REQ_PREV_CHOICE</TT
6768> and <TT
6769CLASS="LITERAL"
6770>REQ_NEXT_CHOICE</TT
6771>will
6772fail.  Such read-only fields may be useful for help messages.</P
6773></DD
6774><DT
6775>O_WRAP</DT
6776><DD
6777><P
6778>Controls word-wrapping in multi-line fields.  Normally, when any
6779character of a (blank-separated) word reaches the end of the current line, the
6780entire word is wrapped to the next line (assuming there is one).  When this
6781option is off, the word will be split across the line break.</P
6782></DD
6783><DT
6784>O_BLANK</DT
6785><DD
6786><P
6787>Controls field blanking.  When this option is on, entering a character at
6788the first field position erases the entire field (except for the just-entered
6789character).</P
6790></DD
6791><DT
6792>O_AUTOSKIP</DT
6793><DD
6794><P
6795>Controls automatic skip to next field when this one fills.  Normally,
6796when the forms user tries to type more data into a field than will fit,
6797the editing location jumps to next field.  When this option is off, the
6798user's cursor will hang at the end of the field.  This option is ignored
6799in dynamic fields that have not reached their size limit.</P
6800></DD
6801><DT
6802>O_NULLOK</DT
6803><DD
6804><P
6805>Controls whether validation is applied to
6806blank fields.  Normally, it is not; the user can leave a field blank
6807without invoking the usual validation check on exit.  If this option is
6808off on a field, exit from it will invoke a validation check.</P
6809></DD
6810><DT
6811>O_PASSOK</DT
6812><DD
6813><P
6814>Controls whether validation occurs on every exit, or only after
6815the field is modified.  Normally the latter is true.  Setting O_PASSOK
6816may be useful if your field's validation function may change during
6817forms processing.</P
6818></DD
6819><DT
6820>O_STATIC</DT
6821><DD
6822><P
6823>Controls whether the field is fixed to its initial dimensions.  If you
6824turn this off, the field becomes dynamic and will
6825stretch to fit entered data.</P
6826></DD
6827></DL
6828></DIV
6829><P
6830>A field's options cannot be changed while the field is currently selected.
6831However, options may be changed on posted fields that are not current. </P
6832><P
6833>The option values are bit-masks and can be composed with logical-or in
6834the obvious way. You have seen the usage of switching off O_AUTOSKIP option.
6835The following example clarifies usage of some more options. Other options
6836are explained where appropriate.</P
6837><DIV
6838CLASS="EXAMPLE"
6839><A
6840NAME="FFOOP"
6841></A
6842><P
6843><B
6844>Example 27.  Field Options Usage example </B
6845></P
6846><PRE
6847CLASS="PROGRAMLISTING"
6848><SPAN
6849CLASS="INLINEMEDIAOBJECT"
6850>#include &#60;form.h&#62;
6851
6852#define STARTX 15
6853#define STARTY 4
6854#define WIDTH 25
6855
6856#define N_FIELDS 3
6857
6858int main()
6859{	FIELD *field[N_FIELDS];
6860	FORM  *my_form;
6861	int ch, i;
6862
6863	/* Initialize curses */
6864	initscr();
6865	cbreak();
6866	noecho();
6867	keypad(stdscr, TRUE);
6868
6869	/* Initialize the fields */
6870	for(i = 0; i &#60; N_FIELDS - 1; ++i)
6871		field[i] = new_field(1, WIDTH, STARTY + i * 2, STARTX, 0, 0);
6872	field[N_FIELDS - 1] = NULL;
6873
6874	/* Set field options */
6875	set_field_back(field[1], A_UNDERLINE); 	/* Print a line for the option 	*/
6876
6877	field_opts_off(field[0], O_ACTIVE); /* This field is a static label */
6878	field_opts_off(field[1], O_PUBLIC); /* This filed is like a password field*/
6879	field_opts_off(field[1], O_AUTOSKIP); /* To avoid entering the same field */
6880					      /* after last character is entered */
6881
6882	/* Create the form and post it */
6883	my_form = new_form(field);
6884	post_form(my_form);
6885	refresh();
6886
6887	set_field_just(field[0], JUSTIFY_CENTER); /* Center Justification */
6888	set_field_buffer(field[0], 0, "This is a static Field");
6889						  /* Initialize the field  */
6890	mvprintw(STARTY, STARTX - 10, "Field 1:");
6891	mvprintw(STARTY + 2, STARTX - 10, "Field 2:");
6892	refresh();
6893
6894	/* Loop through to get user requests */
6895	while((ch = getch()) != KEY_F(1))
6896	{	switch(ch)
6897		{	case KEY_DOWN:
6898				/* Go to next field */
6899				form_driver(my_form, REQ_NEXT_FIELD);
6900				/* Go to the end of the present buffer */
6901				/* Leaves nicely at the last character */
6902				form_driver(my_form, REQ_END_LINE);
6903				break;
6904			case KEY_UP:
6905				/* Go to previous field */
6906				form_driver(my_form, REQ_PREV_FIELD);
6907				form_driver(my_form, REQ_END_LINE);
6908				break;
6909			default:
6910				/* If this is a normal character, it gets */
6911				/* Printed				  */
6912				form_driver(my_form, ch);
6913				break;
6914		}
6915	}
6916
6917	/* Un post form and free the memory */
6918	unpost_form(my_form);
6919	free_form(my_form);
6920	free_field(field[0]);
6921	free_field(field[1]);
6922
6923	endwin();
6924	return 0;
6925}</SPAN
6926></PRE
6927></DIV
6928><P
6929>This example, though useless, shows the usage of options. If used properly, they
6930can present information very effectively in a form. The second field being not
6931O_PUBLIC, does not show the characters you are typing.</P
6932></DIV
6933><DIV
6934CLASS="SECT3"
6935><HR><H4
6936CLASS="SECT3"
6937><A
6938NAME="FIELDSTATUS"
6939>18.3.6. Field Status</A
6940></H4
6941><P
6942>The field status specifies whether the field has got edited or not. It is
6943initially set to FALSE and when user enters something and the data buffer gets
6944modified it becomes TRUE. So a field's status can be queried to find out whether
6945it has been modified or not. The following functions can assist in those
6946operations.</P
6947><PRE
6948CLASS="PROGRAMLISTING"
6949>int set_field_status(FIELD *field,      /* field to alter */
6950                   int status);         /* status to set */
6951
6952int field_status(FIELD *field);         /* fetch status of field */</PRE
6953><P
6954>It's better to check the field's status only after after leaving the field, as
6955data buffer might not have been updated yet as the validation is still due. To
6956guarantee that right status is returned, call field_status() either (1) in the
6957field's exit validation check routine, (2) from the field's or form's
6958initialization or termination hooks, or (3) just after a REQ_VALIDATION request
6959has been processed by the forms driver</P
6960></DIV
6961><DIV
6962CLASS="SECT3"
6963><HR><H4
6964CLASS="SECT3"
6965><A
6966NAME="FIELDUSERPTR"
6967>18.3.7. Field User Pointer</A
6968></H4
6969><P
6970>Every field structure contains one pointer that can be used by the user for
6971various purposes. It is not touched by forms library and can be used for any
6972purpose by the user. The following functions set and fetch user pointer.</P
6973><PRE
6974CLASS="PROGRAMLISTING"
6975>int set_field_userptr(FIELD *field,
6976           char *userptr);      /* the user pointer you wish to associate */
6977                                /* with the field    */
6978
6979char *field_userptr(FIELD *field);      /* fetch user pointer of the field */</PRE
6980></DIV
6981><DIV
6982CLASS="SECT3"
6983><HR><H4
6984CLASS="SECT3"
6985><A
6986NAME="VARIABLESIZEFIELDS"
6987>18.3.8. Variable-Sized Fields</A
6988></H4
6989><P
6990>If you want a dynamically changing field with variable width, this is the
6991feature you want to put to full use. This will allow the user to enter more data
6992than the original size of the field and let the field grow. According to the
6993field orientation it will scroll horizontally or vertically to incorporate the
6994new data.</P
6995><P
6996>To make a field dynamically growable, the option O_STATIC should be turned off.
6997This can be done with a
6998<PRE
6999CLASS="PROGRAMLISTING"
7000>    field_opts_off(field_pointer, O_STATIC);</PRE
7001></P
7002><P
7003>But it's usually not advisable to allow a field to grow infinitely. You can set
7004a maximum limit to the growth of the field with
7005<PRE
7006CLASS="PROGRAMLISTING"
7007>int set_max_field(FIELD *field,    /* Field on which to operate */
7008                  int max_growth); /* maximum growth allowed for the field */</PRE
7009></P
7010><P
7011>The field info for a dynamically growable field can be retrieved by
7012<PRE
7013CLASS="PROGRAMLISTING"
7014>int dynamic_field_info( FIELD *field,     /* Field on which to operate */
7015            int   *prows,     /* number of rows will be filled in this */
7016            int   *pcols,     /* number of columns will be filled in this*/
7017            int   *pmax)      /* maximum allowable growth will be filled */
7018                              /* in this */</PRE
7019>
7020Though field_info work as usual, it is advisable to use this function to get the
7021proper attributes of a dynamically growable field.</P
7022><P
7023>Recall the library routine new_field; a new field created with height set to one
7024will be defined to be a one line field. A new field created with height greater
7025than one will be defined to be a multi line field. </P
7026><P
7027>A one line field with O_STATIC turned off (dynamically growable field)  will
7028contain a single fixed row, but the number of columns can increase if the user
7029enters more data than the initial field will hold. The number of columns
7030displayed will remain fixed and the additional data will scroll horizontally. </P
7031><P
7032>A multi line field with O_STATIC turned off (dynamically growable field) will
7033contain a fixed number of columns, but the number of rows can increase if the
7034user enters more data than the initial field will hold. The number of rows
7035displayed will remain fixed and the additional data will scroll vertically.</P
7036><P
7037>The above two paragraphs pretty much describe a dynamically growable field's
7038behavior. The way other parts of forms library behaves is described below:</P
7039><P
7040></P
7041><OL
7042TYPE="1"
7043><LI
7044><P
7045>The field option O_AUTOSKIP will be ignored if the option O_STATIC is off and
7046there is no maximum growth specified for the field. Currently, O_AUTOSKIP
7047generates an automatic REQ_NEXT_FIELD form driver request when the user types in
7048the last character position of a field. On a growable field with no maximum
7049growth specified, there is no last character position. If a maximum growth is
7050specified, the O_AUTOSKIP option will work as normal if the field has grown to
7051its maximum size. </P
7052></LI
7053><LI
7054><P
7055>The field justification will be ignored if the option O_STATIC is off.
7056Currently, set_field_just can be used to JUSTIFY_LEFT, JUSTIFY_RIGHT,
7057JUSTIFY_CENTER the contents of a one line field. A growable one line field will,
7058by definition, grow and scroll horizontally and may contain more data than can
7059be justified. The return from field_just will be unchanged. </P
7060></LI
7061><LI
7062><P
7063>The overloaded form driver request REQ_NEW_LINE will operate the same way
7064regardless of the O_NL_OVERLOAD form option if the field option O_STATIC is off
7065and there is no maximum growth specified for the field. Currently, if the form
7066option O_NL_OVERLOAD is on, REQ_NEW_LINE implicitly generates a REQ_NEXT_FIELD
7067if called from the last line of a field. If a field can grow without bound,
7068there is no last line, so REQ_NEW_LINE will never implicitly generate a
7069REQ_NEXT_FIELD. If a maximum growth limit is specified and the O_NL_OVERLOAD
7070form option is on, REQ_NEW_LINE will only implicitly generate REQ_NEXT_FIELD if
7071the field has grown to its maximum size and the user is on the last line. </P
7072></LI
7073><LI
7074><P
7075>The library call dup_field will work as usual; it will duplicate the field,
7076including the current buffer size and contents of the field being duplicated.
7077Any specified maximum growth will also be duplicated. </P
7078></LI
7079><LI
7080><P
7081>The library call link_field will work as usual; it will duplicate all field
7082attributes and share buffers with the field being linked. If the O_STATIC field
7083option is subsequently changed by a field sharing buffers, how the system reacts
7084to an attempt to enter more data into the field than the buffer will currently
7085hold will depend on the setting of the option in the current field. </P
7086></LI
7087><LI
7088><P
7089>The library call field_info will work as usual; the variable nrow will contain
7090the value of the original call to new_field. The user should use
7091dynamic_field_info, described above, to query the current size of the buffer.</P
7092></LI
7093></OL
7094><P
7095>Some of the above points make sense only after explaining form driver. We will
7096be looking into that in next few sections.</P
7097></DIV
7098></DIV
7099><DIV
7100CLASS="SECT2"
7101><HR><H3
7102CLASS="SECT2"
7103><A
7104NAME="FORMWINDOWS"
7105>18.4. Form Windows</A
7106></H3
7107><P
7108>The form windows concept is pretty much similar to menu windows. Every form is
7109associated with a main window and a sub window. The form main window displays
7110any title or border associated or whatever the user wishes. Then the sub window
7111contains all the fields and displays them according to their position. This
7112gives the flexibility of manipulating fancy form displaying very easily. </P
7113><P
7114>Since this is pretty much similar to menu windows, I am providing an example
7115with out much explanation. The functions are similar and they work the same way.</P
7116><DIV
7117CLASS="EXAMPLE"
7118><A
7119NAME="FFOWI"
7120></A
7121><P
7122><B
7123>Example 28.  Form Windows Example </B
7124></P
7125><PRE
7126CLASS="PROGRAMLISTING"
7127><SPAN
7128CLASS="INLINEMEDIAOBJECT"
7129>#include &#60;form.h&#62;
7130
7131void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color);
7132
7133int main()
7134{
7135	FIELD *field[3];
7136	FORM  *my_form;
7137	WINDOW *my_form_win;
7138	int ch, rows, cols;
7139
7140	/* Initialize curses */
7141	initscr();
7142	start_color();
7143	cbreak();
7144	noecho();
7145	keypad(stdscr, TRUE);
7146
7147	/* Initialize few color pairs */
7148   	init_pair(1, COLOR_RED, COLOR_BLACK);
7149
7150	/* Initialize the fields */
7151	field[0] = new_field(1, 10, 6, 1, 0, 0);
7152	field[1] = new_field(1, 10, 8, 1, 0, 0);
7153	field[2] = NULL;
7154
7155	/* Set field options */
7156	set_field_back(field[0], A_UNDERLINE);
7157	field_opts_off(field[0], O_AUTOSKIP); /* Don't go to next field when this */
7158					      /* Field is filled up 		*/
7159	set_field_back(field[1], A_UNDERLINE);
7160	field_opts_off(field[1], O_AUTOSKIP);
7161
7162	/* Create the form and post it */
7163	my_form = new_form(field);
7164
7165	/* Calculate the area required for the form */
7166	scale_form(my_form, &#38;rows, &#38;cols);
7167
7168	/* Create the window to be associated with the form */
7169        my_form_win = newwin(rows + 4, cols + 4, 4, 4);
7170        keypad(my_form_win, TRUE);
7171
7172	/* Set main window and sub window */
7173        set_form_win(my_form, my_form_win);
7174        set_form_sub(my_form, derwin(my_form_win, rows, cols, 2, 2));
7175
7176	/* Print a border around the main window and print a title */
7177        box(my_form_win, 0, 0);
7178	print_in_middle(my_form_win, 1, 0, cols + 4, "My Form", COLOR_PAIR(1));
7179
7180	post_form(my_form);
7181	wrefresh(my_form_win);
7182
7183	mvprintw(LINES - 2, 0, "Use UP, DOWN arrow keys to switch between fields");
7184	refresh();
7185
7186	/* Loop through to get user requests */
7187	while((ch = wgetch(my_form_win)) != KEY_F(1))
7188	{	switch(ch)
7189		{	case KEY_DOWN:
7190				/* Go to next field */
7191				form_driver(my_form, REQ_NEXT_FIELD);
7192				/* Go to the end of the present buffer */
7193				/* Leaves nicely at the last character */
7194				form_driver(my_form, REQ_END_LINE);
7195				break;
7196			case KEY_UP:
7197				/* Go to previous field */
7198				form_driver(my_form, REQ_PREV_FIELD);
7199				form_driver(my_form, REQ_END_LINE);
7200				break;
7201			default:
7202				/* If this is a normal character, it gets */
7203				/* Printed				  */
7204				form_driver(my_form, ch);
7205				break;
7206		}
7207	}
7208
7209	/* Un post form and free the memory */
7210	unpost_form(my_form);
7211	free_form(my_form);
7212	free_field(field[0]);
7213	free_field(field[1]);
7214
7215	endwin();
7216	return 0;
7217}
7218
7219void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color)
7220{	int length, x, y;
7221	float temp;
7222
7223	if(win == NULL)
7224		win = stdscr;
7225	getyx(win, y, x);
7226	if(startx != 0)
7227		x = startx;
7228	if(starty != 0)
7229		y = starty;
7230	if(width == 0)
7231		width = 80;
7232
7233	length = strlen(string);
7234	temp = (width - length)/ 2;
7235	x = startx + (int)temp;
7236	wattron(win, color);
7237	mvwprintw(win, y, x, "%s", string);
7238	wattroff(win, color);
7239	refresh();
7240}</SPAN
7241></PRE
7242></DIV
7243></DIV
7244><DIV
7245CLASS="SECT2"
7246><HR><H3
7247CLASS="SECT2"
7248><A
7249NAME="FILEDVALIDATE"
7250>18.5. Field Validation</A
7251></H3
7252><P
7253>By default, a field will accept any data input by the user. It is possible to
7254attach validation to the field. Then any attempt by the user to leave the field,
7255while it contains data that doesn't match the validation type will fail. Some
7256validation types also have a character-validity check for each time a character
7257is entered in the field.</P
7258><P
7259>Validation can be attached to a field with the following function.
7260<PRE
7261CLASS="PROGRAMLISTING"
7262>int set_field_type(FIELD *field,          /* field to alter */
7263                   FIELDTYPE *ftype,      /* type to associate */
7264                   ...);                  /* additional arguments*/</PRE
7265>
7266Once set, the validation type for a field can be queried with
7267<PRE
7268CLASS="PROGRAMLISTING"
7269>FIELDTYPE *field_type(FIELD *field);      /* field to query */</PRE
7270></P
7271><P
7272>The form driver validates the data in a field only when data is entered by the
7273end-user. Validation does not occur when </P
7274><P
7275></P
7276><UL
7277><LI
7278><P
7279>the application program changes the field value by calling set_field_buffer. </P
7280></LI
7281><LI
7282><P
7283>linked field values are changed indirectly -- by changing the field to which
7284they are linked</P
7285></LI
7286></UL
7287><P
7288>The following are the pre-defined validation types. You can also specify custom
7289validation, though it's a bit tricky and cumbersome.</P
7290><H1
7291CLASS="BRIDGEHEAD"
7292><A
7293NAME="AEN1069"
7294></A
7295>TYPE_ALPHA</H1
7296><P
7297>This field type accepts alphabetic data; no blanks, no digits, no special
7298characters (this is checked at character-entry time).  It is set up with: </P
7299><PRE
7300CLASS="PROGRAMLISTING"
7301>int set_field_type(FIELD *field,          /* field to alter */
7302                   TYPE_ALPHA,            /* type to associate */
7303                   int width);            /* maximum width of field */</PRE
7304><P
7305>The width argument sets a minimum width of data. The user has to enter at-least
7306width number of characters before he can leave the field. Typically
7307you'll want to set this to the field width; if it's greater than the
7308field width, the validation check will always fail.  A minimum width
7309of zero makes field completion optional. </P
7310><H1
7311CLASS="BRIDGEHEAD"
7312><A
7313NAME="AEN1073"
7314></A
7315>TYPE_ALNUM</H1
7316><P
7317>This field type accepts alphabetic data and digits; no blanks, no special
7318characters (this is checked at character-entry time).  It is set up with: </P
7319><PRE
7320CLASS="PROGRAMLISTING"
7321>int set_field_type(FIELD *field,          /* field to alter */
7322                   TYPE_ALNUM,            /* type to associate */
7323                   int width);            /* maximum width of field */</PRE
7324><P
7325>The width argument sets a minimum width of data.  As with
7326TYPE_ALPHA, typically you'll want to set this to the field width; if it's
7327greater than the field width, the validation check will always fail.  A
7328minimum width of zero makes field completion optional. </P
7329><H1
7330CLASS="BRIDGEHEAD"
7331><A
7332NAME="AEN1077"
7333></A
7334>TYPE_ENUM</H1
7335><P
7336>This type allows you to restrict a field's values to be among a specified
7337set of string values (for example, the two-letter postal codes for U.S.
7338states).  It is set up with: </P
7339><PRE
7340CLASS="PROGRAMLISTING"
7341>int set_field_type(FIELD *field,          /* field to alter */
7342                   TYPE_ENUM,             /* type to associate */
7343                   char **valuelist;      /* list of possible values */
7344                   int checkcase;         /* case-sensitive? */
7345                   int checkunique);      /* must specify uniquely? */</PRE
7346><P
7347>The valuelist parameter must point at a NULL-terminated list of
7348valid strings.  The checkcase argument, if true, makes comparison
7349with the string case-sensitive. </P
7350><P
7351>When the user exits a TYPE_ENUM field, the validation procedure tries to
7352complete the data in the buffer to a valid entry.  If a complete choice string
7353has been entered, it is of course valid.  But it is also possible to enter a
7354prefix of a valid string and have it completed for you. </P
7355><P
7356>By default, if you enter such a prefix and it matches more than one value
7357in the string list, the prefix will be completed to the first matching
7358value.  But the checkunique argument, if true, requires prefix
7359matches to be unique in order to be valid. </P
7360><P
7361>The REQ_NEXT_CHOICE and REQ_PREV_CHOICE input requests can be particularly
7362useful with these fields. </P
7363><H1
7364CLASS="BRIDGEHEAD"
7365><A
7366NAME="AEN1084"
7367></A
7368>TYPE_INTEGER</H1
7369><P
7370>This field type accepts an integer.  It is set up as follows: </P
7371><PRE
7372CLASS="PROGRAMLISTING"
7373>int set_field_type(FIELD *field,          /* field to alter */
7374                   TYPE_INTEGER,          /* type to associate */
7375                   int padding,           /* # places to zero-pad to */
7376                   int vmin, int vmax);   /* valid range */</PRE
7377><P
7378>Valid characters consist of an optional leading minus and digits.
7379The range check is performed on exit.  If the range maximum is less
7380than or equal to the minimum, the range is ignored. </P
7381><P
7382>If the value passes its range check, it is padded with as many leading
7383zero digits as necessary to meet the padding argument. </P
7384><P
7385>A TYPE_INTEGER value buffer can conveniently be interpreted with the C library
7386function atoi(3).</P
7387><H1
7388CLASS="BRIDGEHEAD"
7389><A
7390NAME="AEN1090"
7391></A
7392>TYPE_NUMERIC</H1
7393><P
7394>This field type accepts a decimal number.  It is set up as follows: </P
7395><PRE
7396CLASS="PROGRAMLISTING"
7397>int set_field_type(FIELD *field,          /* field to alter */
7398                   TYPE_NUMERIC,          /* type to associate */
7399                   int padding,           /* # places of precision */
7400                   int vmin, int vmax);   /* valid range */</PRE
7401><P
7402>Valid characters consist of an optional leading minus and digits. possibly
7403including a decimal point.  The range check is performed on exit.  If the
7404range maximum is less than or equal to the minimum, the range is
7405ignored. </P
7406><P
7407>If the value passes its range check, it is padded with as many trailing
7408zero digits as necessary to meet the padding argument. </P
7409><P
7410>A TYPE_NUMERIC value buffer can conveniently be interpreted with the C library
7411function atof(3).</P
7412><H1
7413CLASS="BRIDGEHEAD"
7414><A
7415NAME="AEN1096"
7416></A
7417>TYPE_REGEXP</H1
7418><P
7419>This field type accepts data matching a regular expression.  It is set up
7420as follows: </P
7421><PRE
7422CLASS="PROGRAMLISTING"
7423>int set_field_type(FIELD *field,          /* field to alter */
7424                   TYPE_REGEXP,           /* type to associate */
7425                   char *regexp);         /* expression to match */</PRE
7426><P
7427>The syntax for regular expressions is that of regcomp(3).
7428The check for regular-expression match is performed on exit.</P
7429></DIV
7430><DIV
7431CLASS="SECT2"
7432><HR><H3
7433CLASS="SECT2"
7434><A
7435NAME="FORMDRIVER"
7436>18.6. Form Driver: The work horse of the forms system</A
7437></H3
7438><P
7439>As in the menu system, form_driver() plays a very important role in forms
7440system.  All types of requests to forms system should be funneled through
7441form_driver().</P
7442><PRE
7443CLASS="PROGRAMLISTING"
7444>int form_driver(FORM *form,     /* form on which to operate     */
7445                int request)    /* form request code         */</PRE
7446><P
7447>As you have seen some of the examples above, you have to be in a loop looking
7448for user input and then decide whether it's a field data or a form request. The
7449form requests are then passed to form_driver() to do the work.</P
7450><P
7451>The requests roughly can be divided into following categories. Different
7452requests and their usage is explained below:</P
7453><DIV
7454CLASS="SECT3"
7455><HR><H4
7456CLASS="SECT3"
7457><A
7458NAME="PAGENAVREQ"
7459>18.6.1. Page Navigation Requests</A
7460></H4
7461><P
7462>These requests cause page-level moves through the form, triggering display of a
7463new form screen. A form can be made of multiple pages. If you have a big form
7464with lot of fields and logical sections, then you can divide the form into
7465pages. The function set_new_page() to set a new page at the field specified.</P
7466><PRE
7467CLASS="PROGRAMLISTING"
7468>int set_new_page(FIELD *field,/* Field at which page break to be set or unset */
7469         bool new_page_flag); /* should be TRUE to put a break */</PRE
7470><P
7471>The following requests allow you to move to different pages</P
7472><P
7473></P
7474><UL
7475><LI
7476><P
7477><SPAN
7478CLASS="emphasis"
7479><I
7480CLASS="EMPHASIS"
7481>REQ_NEXT_PAGE</I
7482></SPAN
7483> Move to the next form page.</P
7484></LI
7485><LI
7486><P
7487><SPAN
7488CLASS="emphasis"
7489><I
7490CLASS="EMPHASIS"
7491>REQ_PREV_PAGE</I
7492></SPAN
7493> Move to the previous
7494form page.</P
7495></LI
7496><LI
7497><P
7498><SPAN
7499CLASS="emphasis"
7500><I
7501CLASS="EMPHASIS"
7502>REQ_FIRST_PAGE</I
7503></SPAN
7504> Move to the first form page.</P
7505></LI
7506><LI
7507><P
7508><SPAN
7509CLASS="emphasis"
7510><I
7511CLASS="EMPHASIS"
7512>REQ_LAST_PAGE</I
7513></SPAN
7514> Move to the last form page. </P
7515></LI
7516></UL
7517><P
7518>These requests treat the list as cyclic; that is, REQ_NEXT_PAGE from the
7519last page goes to the first, and REQ_PREV_PAGE from the first page goes to
7520the last.</P
7521></DIV
7522><DIV
7523CLASS="SECT3"
7524><HR><H4
7525CLASS="SECT3"
7526><A
7527NAME="INTERFIELDNAVREQ"
7528>18.6.2. Inter-Field Navigation Requests</A
7529></H4
7530><P
7531>These requests handle navigation between fields on the same page.</P
7532><P
7533></P
7534><UL
7535><LI
7536><P
7537><SPAN
7538CLASS="emphasis"
7539><I
7540CLASS="EMPHASIS"
7541>REQ_NEXT_FIELD</I
7542></SPAN
7543>
7544    Move to next field.  </P
7545></LI
7546><LI
7547><P
7548><SPAN
7549CLASS="emphasis"
7550><I
7551CLASS="EMPHASIS"
7552>REQ_PREV_FIELD</I
7553></SPAN
7554>
7555    Move to previous field.  </P
7556></LI
7557><LI
7558><P
7559><SPAN
7560CLASS="emphasis"
7561><I
7562CLASS="EMPHASIS"
7563>REQ_FIRST_FIELD</I
7564></SPAN
7565>
7566    Move to the first field. </P
7567></LI
7568><LI
7569><P
7570><SPAN
7571CLASS="emphasis"
7572><I
7573CLASS="EMPHASIS"
7574>REQ_LAST_FIELD</I
7575></SPAN
7576>
7577    Move to the last field. </P
7578></LI
7579><LI
7580><P
7581><SPAN
7582CLASS="emphasis"
7583><I
7584CLASS="EMPHASIS"
7585>REQ_SNEXT_FIELD</I
7586></SPAN
7587>
7588    Move to sorted next field. </P
7589></LI
7590><LI
7591><P
7592><SPAN
7593CLASS="emphasis"
7594><I
7595CLASS="EMPHASIS"
7596>REQ_SPREV_FIELD</I
7597></SPAN
7598>
7599    Move to sorted previous field. </P
7600></LI
7601><LI
7602><P
7603><SPAN
7604CLASS="emphasis"
7605><I
7606CLASS="EMPHASIS"
7607>REQ_SFIRST_FIELD</I
7608></SPAN
7609>
7610    Move to the sorted first field. </P
7611></LI
7612><LI
7613><P
7614><SPAN
7615CLASS="emphasis"
7616><I
7617CLASS="EMPHASIS"
7618>REQ_SLAST_FIELD</I
7619></SPAN
7620>
7621    Move to the sorted last field. </P
7622></LI
7623><LI
7624><P
7625><SPAN
7626CLASS="emphasis"
7627><I
7628CLASS="EMPHASIS"
7629>REQ_LEFT_FIELD</I
7630></SPAN
7631>
7632    Move left to field.  </P
7633></LI
7634><LI
7635><P
7636><SPAN
7637CLASS="emphasis"
7638><I
7639CLASS="EMPHASIS"
7640>REQ_RIGHT_FIELD</I
7641></SPAN
7642>
7643    Move right to field. </P
7644></LI
7645><LI
7646><P
7647><SPAN
7648CLASS="emphasis"
7649><I
7650CLASS="EMPHASIS"
7651>REQ_UP_FIELD</I
7652></SPAN
7653>
7654    Move up to field.  </P
7655></LI
7656><LI
7657><P
7658><SPAN
7659CLASS="emphasis"
7660><I
7661CLASS="EMPHASIS"
7662>REQ_DOWN_FIELD</I
7663></SPAN
7664>
7665    Move down to field. </P
7666></LI
7667></UL
7668><P
7669>These requests treat the list of fields on a page as cyclic; that is,
7670REQ_NEXT_FIELD from the last field goes to the first, and REQ_PREV_FIELD
7671from the first field goes to the last. The order of the fields for these
7672(and the REQ_FIRST_FIELD and REQ_LAST_FIELD requests) is simply the order of
7673the field pointers in the form array (as set up by new_form() or
7674set_form_fields()</P
7675><P
7676>It is also possible to traverse the fields as if they had been sorted in
7677screen-position order, so the sequence goes left-to-right and top-to-bottom.
7678To do this, use the second group of four sorted-movement requests.</P
7679><P
7680>Finally, it is possible to move between fields using visual directions up,
7681down, right, and left. To accomplish this, use the third group of four
7682requests. Note, however, that the position of a form for purposes of these
7683requests is its upper-left corner.</P
7684><P
7685>For example, suppose you have a multi-line field B, and two single-line
7686fields A and C on the same line with B, with A to the left of B and C to the
7687right of B. A REQ_MOVE_RIGHT from A will go to B only if A, B, and C all
7688share the same first line; otherwise it will skip over B to C.</P
7689></DIV
7690><DIV
7691CLASS="SECT3"
7692><HR><H4
7693CLASS="SECT3"
7694><A
7695NAME="INTRAFIELDNAVREQ"
7696>18.6.3. Intra-Field Navigation Requests</A
7697></H4
7698><P
7699>These requests drive movement of the edit cursor within the currently
7700selected field.</P
7701><P
7702></P
7703><UL
7704><LI
7705><P
7706><SPAN
7707CLASS="emphasis"
7708><I
7709CLASS="EMPHASIS"
7710>REQ_NEXT_CHAR</I
7711></SPAN
7712>
7713    Move to next character.  </P
7714></LI
7715><LI
7716><P
7717><SPAN
7718CLASS="emphasis"
7719><I
7720CLASS="EMPHASIS"
7721>REQ_PREV_CHAR</I
7722></SPAN
7723>
7724    Move to previous character.  </P
7725></LI
7726><LI
7727><P
7728><SPAN
7729CLASS="emphasis"
7730><I
7731CLASS="EMPHASIS"
7732>REQ_NEXT_LINE</I
7733></SPAN
7734>
7735    Move to next line.  </P
7736></LI
7737><LI
7738><P
7739><SPAN
7740CLASS="emphasis"
7741><I
7742CLASS="EMPHASIS"
7743>REQ_PREV_LINE</I
7744></SPAN
7745>
7746    Move to previous line.  </P
7747></LI
7748><LI
7749><P
7750><SPAN
7751CLASS="emphasis"
7752><I
7753CLASS="EMPHASIS"
7754>REQ_NEXT_WORD</I
7755></SPAN
7756>
7757    Move to next word. </P
7758></LI
7759><LI
7760><P
7761><SPAN
7762CLASS="emphasis"
7763><I
7764CLASS="EMPHASIS"
7765>REQ_PREV_WORD</I
7766></SPAN
7767>
7768    Move to previous word. </P
7769></LI
7770><LI
7771><P
7772><SPAN
7773CLASS="emphasis"
7774><I
7775CLASS="EMPHASIS"
7776>REQ_BEG_FIELD</I
7777></SPAN
7778>
7779    Move to beginning of field. </P
7780></LI
7781><LI
7782><P
7783><SPAN
7784CLASS="emphasis"
7785><I
7786CLASS="EMPHASIS"
7787>REQ_END_FIELD</I
7788></SPAN
7789>
7790    Move to end of field. </P
7791></LI
7792><LI
7793><P
7794><SPAN
7795CLASS="emphasis"
7796><I
7797CLASS="EMPHASIS"
7798>REQ_BEG_LINE</I
7799></SPAN
7800>
7801    Move to beginning of line. </P
7802></LI
7803><LI
7804><P
7805><SPAN
7806CLASS="emphasis"
7807><I
7808CLASS="EMPHASIS"
7809>REQ_END_LINE</I
7810></SPAN
7811>
7812    Move to end of line.  </P
7813></LI
7814><LI
7815><P
7816><SPAN
7817CLASS="emphasis"
7818><I
7819CLASS="EMPHASIS"
7820>REQ_LEFT_CHAR</I
7821></SPAN
7822>
7823    Move left in field. </P
7824></LI
7825><LI
7826><P
7827><SPAN
7828CLASS="emphasis"
7829><I
7830CLASS="EMPHASIS"
7831>REQ_RIGHT_CHAR</I
7832></SPAN
7833>
7834    Move right in field.  </P
7835></LI
7836><LI
7837><P
7838><SPAN
7839CLASS="emphasis"
7840><I
7841CLASS="EMPHASIS"
7842>REQ_UP_CHAR</I
7843></SPAN
7844>
7845    Move up in field. </P
7846></LI
7847><LI
7848><P
7849><SPAN
7850CLASS="emphasis"
7851><I
7852CLASS="EMPHASIS"
7853>REQ_DOWN_CHAR</I
7854></SPAN
7855>
7856    Move down in field. </P
7857></LI
7858></UL
7859><P
7860>Each word is separated from the previous and next characters by whitespace.
7861The commands to move to beginning and end of line or field look for the
7862first or last non-pad character in their ranges.</P
7863></DIV
7864><DIV
7865CLASS="SECT3"
7866><HR><H4
7867CLASS="SECT3"
7868><A
7869NAME="SCROLLREQ"
7870>18.6.4. Scrolling Requests</A
7871></H4
7872><P
7873>Fields that are dynamic and have grown and fields explicitly created with
7874offscreen rows are scrollable. One-line fields scroll horizontally;
7875multi-line fields scroll vertically. Most scrolling is triggered by editing
7876and intra-field movement (the library scrolls the field to keep the cursor
7877visible). It is possible to explicitly request scrolling with the following
7878requests:</P
7879><P
7880></P
7881><UL
7882><LI
7883><P
7884><SPAN
7885CLASS="emphasis"
7886><I
7887CLASS="EMPHASIS"
7888>REQ_SCR_FLINE</I
7889></SPAN
7890>
7891    Scroll vertically forward a line.  </P
7892></LI
7893><LI
7894><P
7895><SPAN
7896CLASS="emphasis"
7897><I
7898CLASS="EMPHASIS"
7899>REQ_SCR_BLINE</I
7900></SPAN
7901>
7902    Scroll vertically backward a line.  </P
7903></LI
7904><LI
7905><P
7906><SPAN
7907CLASS="emphasis"
7908><I
7909CLASS="EMPHASIS"
7910>REQ_SCR_FPAGE</I
7911></SPAN
7912>
7913    Scroll vertically forward a page.  </P
7914></LI
7915><LI
7916><P
7917><SPAN
7918CLASS="emphasis"
7919><I
7920CLASS="EMPHASIS"
7921>REQ_SCR_BPAGE</I
7922></SPAN
7923>
7924    Scroll vertically backward a page. </P
7925></LI
7926><LI
7927><P
7928><SPAN
7929CLASS="emphasis"
7930><I
7931CLASS="EMPHASIS"
7932>REQ_SCR_FHPAGE</I
7933></SPAN
7934>
7935    Scroll vertically forward half a page. </P
7936></LI
7937><LI
7938><P
7939><SPAN
7940CLASS="emphasis"
7941><I
7942CLASS="EMPHASIS"
7943>REQ_SCR_BHPAGE</I
7944></SPAN
7945>
7946    Scroll vertically backward half a page. </P
7947></LI
7948><LI
7949><P
7950><SPAN
7951CLASS="emphasis"
7952><I
7953CLASS="EMPHASIS"
7954>REQ_SCR_FCHAR</I
7955></SPAN
7956>
7957    Scroll horizontally forward a character. </P
7958></LI
7959><LI
7960><P
7961><SPAN
7962CLASS="emphasis"
7963><I
7964CLASS="EMPHASIS"
7965>REQ_SCR_BCHAR</I
7966></SPAN
7967>
7968    Scroll horizontally backward a character. </P
7969></LI
7970><LI
7971><P
7972><SPAN
7973CLASS="emphasis"
7974><I
7975CLASS="EMPHASIS"
7976>REQ_SCR_HFLINE</I
7977></SPAN
7978>
7979    Scroll horizontally one field width forward. </P
7980></LI
7981><LI
7982><P
7983><SPAN
7984CLASS="emphasis"
7985><I
7986CLASS="EMPHASIS"
7987>REQ_SCR_HBLINE</I
7988></SPAN
7989>
7990    Scroll horizontally one field width backward. </P
7991></LI
7992><LI
7993><P
7994><SPAN
7995CLASS="emphasis"
7996><I
7997CLASS="EMPHASIS"
7998>REQ_SCR_HFHALF</I
7999></SPAN
8000>
8001    Scroll horizontally one half field width forward. </P
8002></LI
8003><LI
8004><P
8005><SPAN
8006CLASS="emphasis"
8007><I
8008CLASS="EMPHASIS"
8009>REQ_SCR_HBHALF</I
8010></SPAN
8011>
8012    Scroll horizontally one half field width backward. </P
8013></LI
8014></UL
8015><P
8016>For scrolling purposes, a page of a field is the height of its visible part.</P
8017></DIV
8018><DIV
8019CLASS="SECT3"
8020><HR><H4
8021CLASS="SECT3"
8022><A
8023NAME="EDITREQ"
8024>18.6.5. Editing Requests</A
8025></H4
8026><P
8027>When you pass the forms driver an ASCII character, it is treated as a
8028request to add the character to the field's data buffer. Whether this is an
8029insertion or a replacement depends on the field's edit mode (insertion is
8030the default.</P
8031><P
8032>The following requests support editing the field and changing the edit mode:</P
8033><P
8034></P
8035><UL
8036><LI
8037><P
8038><SPAN
8039CLASS="emphasis"
8040><I
8041CLASS="EMPHASIS"
8042>REQ_INS_MODE</I
8043></SPAN
8044>
8045    Set insertion mode. </P
8046></LI
8047><LI
8048><P
8049><SPAN
8050CLASS="emphasis"
8051><I
8052CLASS="EMPHASIS"
8053>REQ_OVL_MODE</I
8054></SPAN
8055>
8056    Set overlay mode. </P
8057></LI
8058><LI
8059><P
8060><SPAN
8061CLASS="emphasis"
8062><I
8063CLASS="EMPHASIS"
8064>REQ_NEW_LINE</I
8065></SPAN
8066>
8067    New line request (see below for explanation). </P
8068></LI
8069><LI
8070><P
8071><SPAN
8072CLASS="emphasis"
8073><I
8074CLASS="EMPHASIS"
8075>REQ_INS_CHAR</I
8076></SPAN
8077>
8078    Insert space at character location.  </P
8079></LI
8080><LI
8081><P
8082><SPAN
8083CLASS="emphasis"
8084><I
8085CLASS="EMPHASIS"
8086>REQ_INS_LINE</I
8087></SPAN
8088>
8089    Insert blank line at character location.  </P
8090></LI
8091><LI
8092><P
8093><SPAN
8094CLASS="emphasis"
8095><I
8096CLASS="EMPHASIS"
8097>REQ_DEL_CHAR</I
8098></SPAN
8099>
8100    Delete character at cursor.  </P
8101></LI
8102><LI
8103><P
8104><SPAN
8105CLASS="emphasis"
8106><I
8107CLASS="EMPHASIS"
8108>REQ_DEL_PREV</I
8109></SPAN
8110>
8111    Delete previous word at cursor. </P
8112></LI
8113><LI
8114><P
8115><SPAN
8116CLASS="emphasis"
8117><I
8118CLASS="EMPHASIS"
8119>REQ_DEL_LINE</I
8120></SPAN
8121>
8122    Delete line at cursor. </P
8123></LI
8124><LI
8125><P
8126><SPAN
8127CLASS="emphasis"
8128><I
8129CLASS="EMPHASIS"
8130>REQ_DEL_WORD</I
8131></SPAN
8132>
8133    Delete word at cursor. </P
8134></LI
8135><LI
8136><P
8137><SPAN
8138CLASS="emphasis"
8139><I
8140CLASS="EMPHASIS"
8141>REQ_CLR_EOL</I
8142></SPAN
8143>
8144    Clear to end of line. </P
8145></LI
8146><LI
8147><P
8148><SPAN
8149CLASS="emphasis"
8150><I
8151CLASS="EMPHASIS"
8152>REQ_CLR_EOF</I
8153></SPAN
8154>
8155    Clear to end of field. </P
8156></LI
8157><LI
8158><P
8159><SPAN
8160CLASS="emphasis"
8161><I
8162CLASS="EMPHASIS"
8163>REQ_CLR_FIELD</I
8164></SPAN
8165>
8166    Clear entire field. </P
8167></LI
8168></UL
8169><P
8170>The behavior of the REQ_NEW_LINE and REQ_DEL_PREV requests is complicated
8171and partly controlled by a pair of forms options. The special cases are
8172triggered when the cursor is at the beginning of a field, or on the last
8173line of the field.</P
8174><P
8175>First, we consider REQ_NEW_LINE:</P
8176><P
8177>The normal behavior of REQ_NEW_LINE in insert mode is to break the current
8178line at the position of the edit cursor, inserting the portion of the
8179current line after the cursor as a new line following the current and moving
8180the cursor to the beginning of that new line (you may think of this as
8181inserting a newline in the field buffer).</P
8182><P
8183>The normal behavior of REQ_NEW_LINE in overlay mode is to clear the current
8184line from the position of the edit cursor to end of line. The cursor is then
8185moved to the beginning of the next line.</P
8186><P
8187>However, REQ_NEW_LINE at the beginning of a field, or on the last line of a
8188field, instead does a REQ_NEXT_FIELD. O_NL_OVERLOAD option is off, this
8189special action is disabled.</P
8190><P
8191>Now, let us consider REQ_DEL_PREV:</P
8192><P
8193>The normal behavior of REQ_DEL_PREV is to delete the previous character. If
8194insert mode is on, and the cursor is at the start of a line, and the text on
8195that line will fit on the previous one, it instead appends the contents of
8196the current line to the previous one and deletes the current line (you may
8197think of this as deleting a newline from the field buffer).</P
8198><P
8199>However, REQ_DEL_PREV at the beginning of a field is instead treated as a
8200REQ_PREV_FIELD.</P
8201><P
8202>If the O_BS_OVERLOAD option is off, this special action is disabled and the
8203forms driver just returns E_REQUEST_DENIED.</P
8204></DIV
8205><DIV
8206CLASS="SECT3"
8207><HR><H4
8208CLASS="SECT3"
8209><A
8210NAME="ORDERREQ"
8211>18.6.6. Order Requests</A
8212></H4
8213><P
8214>If the type of your field is ordered, and has associated functions for
8215getting the next and previous values of the type from a given value, there
8216are requests that can fetch that value into the field buffer:</P
8217><P
8218></P
8219><UL
8220><LI
8221><P
8222><SPAN
8223CLASS="emphasis"
8224><I
8225CLASS="EMPHASIS"
8226>REQ_NEXT_CHOICE</I
8227></SPAN
8228>
8229    Place the successor value of the current value in the buffer.
8230    </P
8231></LI
8232><LI
8233><P
8234><SPAN
8235CLASS="emphasis"
8236><I
8237CLASS="EMPHASIS"
8238>REQ_PREV_CHOICE</I
8239></SPAN
8240>
8241    Place the predecessor value of the current value in the buffer.
8242    </P
8243></LI
8244></UL
8245><P
8246>Of the built-in field types, only TYPE_ENUM has built-in successor and
8247predecessor functions. When you define a field type of your own (see Custom
8248Validation Types), you can associate our own ordering functions.</P
8249></DIV
8250><DIV
8251CLASS="SECT3"
8252><HR><H4
8253CLASS="SECT3"
8254><A
8255NAME="APPLICCOMMANDS"
8256>18.6.7. Application Commands</A
8257></H4
8258><P
8259>Form requests are represented as integers above the curses value greater than
8260KEY_MAX and less than or equal to the constant MAX_COMMAND.  A value within this
8261range gets ignored by form_driver(). So this can be used for any purpose by the
8262application. It can be treated as an application specific action and take
8263corresponding action.</P
8264></DIV
8265></DIV
8266></DIV
8267><DIV
8268CLASS="SECT1"
8269><HR><H2
8270CLASS="SECT1"
8271><A
8272NAME="TOOLS"
8273>19. Tools and Widget Libraries</A
8274></H2
8275><P
8276>
8277Now that you have seen the capabilities of ncurses and its sister libraries, you
8278are rolling your sleeves up and gearing for a project that heavily manipulates
8279screen. But wait..  It can be pretty difficult to write and maintain complex GUI
8280widgets in plain ncurses or even with the additional libraries. There are some
8281ready-to-use tools and widget libraries that can be used instead of writing your
8282own widgets. You can use some of them, get ideas from the code, or even extend
8283them.</P
8284><DIV
8285CLASS="SECT2"
8286><HR><H3
8287CLASS="SECT2"
8288><A
8289NAME="CDK"
8290>19.1. CDK (Curses Development Kit)</A
8291></H3
8292><P
8293>In the author's words </P
8294><P
8295><SPAN
8296CLASS="emphasis"
8297><I
8298CLASS="EMPHASIS"
8299>
8300CDK stands for 'Curses Development Kit' and it currently contains 21 ready
8301to use widgets which facilitate the speedy development of full screen
8302curses programs. </I
8303></SPAN
8304></P
8305><P
8306>The kit provides some useful widgets, which can be used in your programs
8307directly. It's pretty well written and the documentation is very good. The
8308examples in the examples directory can be a good place to start for beginners.
8309The CDK can be downloaded from <A
8310HREF="http://invisible-island.net/cdk/"
8311TARGET="_top"
8312>http://invisible-island.net/cdk/</A
8313>
8314. Follow the instructions in
8315README file to install it.</P
8316><DIV
8317CLASS="SECT3"
8318><HR><H4
8319CLASS="SECT3"
8320><A
8321NAME="WIDGETLIST"
8322>19.1.1. Widget List</A
8323></H4
8324><P
8325>The following is the list of widgets provided with cdk and their description.</P
8326><PRE
8327CLASS="PROGRAMLISTING"
8328>Widget Type           Quick Description
8329===========================================================================
8330Alphalist             Allows a user to select from a list of words, with
8331                      the ability to narrow the search list by typing in a
8332                      few characters of the desired word.
8333Buttonbox             This creates a multiple button widget.
8334Calendar              Creates a little simple calendar widget.
8335Dialog                Prompts the user with a message, and the user
8336                      can pick an answer from the buttons provided.
8337Entry                 Allows the user to enter various types of information.
8338File Selector         A file selector built from Cdk base widgets. This
8339                      example shows how to create more complicated widgets
8340                      using the Cdk widget library.
8341Graph                 Draws a graph.
8342Histogram             Draws a histogram.
8343Item List             Creates a pop up field which allows the user to select
8344                      one of several choices in a small field. Very useful
8345                      for things like days of the week or month names.
8346Label                 Displays messages in a pop up box, or the label can be
8347                      considered part of the screen.
8348Marquee               Displays a message in a scrolling marquee.
8349Matrix                Creates a complex matrix with lots of options.
8350Menu                  Creates a pull-down menu interface.
8351Multiple Line Entry   A multiple line entry field. Very useful
8352                      for long fields. (like a description
8353                      field)
8354Radio List            Creates a radio button list.
8355Scale                 Creates a numeric scale. Used for allowing a user to
8356                      pick a numeric value and restrict them to a range of
8357                      values.
8358Scrolling List        Creates a scrolling list/menu list.
8359Scrolling Window      Creates a scrolling log file viewer. Can add
8360                      information into the window while its running.
8361                      A good widget for displaying the progress of
8362                      something. (akin to a console window)
8363Selection List        Creates a multiple option selection list.
8364Slider                Akin to the scale widget, this widget provides a
8365                      visual slide bar to represent the numeric value.
8366Template              Creates a entry field with character sensitive
8367                      positions. Used for pre-formatted fields like
8368                      dates and phone numbers.
8369Viewer                This is a file/information viewer. Very useful
8370                      when you need to display loads of information.
8371===========================================================================</PRE
8372><P
8373>A few of the widgets are modified by Thomas Dickey in recent versions.</P
8374></DIV
8375><DIV
8376CLASS="SECT3"
8377><HR><H4
8378CLASS="SECT3"
8379><A
8380NAME="CDKATTRACT"
8381>19.1.2. Some Attractive Features</A
8382></H4
8383><P
8384>Apart from making our life easier with readily usable widgets, cdk solves one
8385frustrating problem with printing multi colored strings, justified strings
8386elegantly.  Special formatting tags can be embedded in the strings which are
8387passed to CDK functions. For Example</P
8388><P
8389>If the string</P
8390><PRE
8391CLASS="PROGRAMLISTING"
8392>"&lt;/B/1&gt;This line should have a yellow foreground and a blue
8393background.&lt;!1&gt;"</PRE
8394><P
8395>given as a parameter to newCDKLabel(), it prints the line with yellow foreground
8396and blue background. There are other tags available for justifying string,
8397embedding special drawing characters etc.. Please refer to the man page
8398cdk_display(3X) for details. The man page explains the usage with nice examples.</P
8399></DIV
8400><DIV
8401CLASS="SECT3"
8402><HR><H4
8403CLASS="SECT3"
8404><A
8405NAME="CDKCONCLUSION"
8406>19.1.3. Conclusion</A
8407></H4
8408><P
8409>All in all, CDK is a well-written package of widgets, which if used properly can
8410form a strong frame work for developing complex GUI.</P
8411></DIV
8412></DIV
8413><DIV
8414CLASS="SECT2"
8415><HR><H3
8416CLASS="SECT2"
8417><A
8418NAME="DIALOG"
8419>19.2. The dialog</A
8420></H3
8421><P
8422>Long long ago, in September 1994, when few people knew linux, Jeff Tranter wrote
8423an <A
8424HREF="http://www2.linuxjournal.com/lj-issues/issue5/2807.html"
8425TARGET="_top"
8426>article</A
8427> on dialog in Linux Journal. He starts the article with these words..</P
8428><P
8429><SPAN
8430CLASS="emphasis"
8431><I
8432CLASS="EMPHASIS"
8433>Linux is based on the Unix operating system, but also features a number of
8434unique and useful kernel features and application programs that often go beyond
8435what is available under Unix. One little-known gem is "dialog", a utility for
8436creating professional-looking dialog boxes from within shell scripts. This
8437article presents a tutorial introduction to the dialog utility, and shows
8438examples of how and where it can be used</I
8439></SPAN
8440></P
8441><P
8442>
8443As he explains, dialog is a real gem in making professional-looking dialog boxes
8444with ease. It creates a variety of dialog boxes, menus, check lists etc.. It is
8445usually installed by default. If not, you can download it from <A
8446HREF="http://invisible-island.net/dialog/"
8447TARGET="_top"
8448>Thomas Dickey</A
8449>'s site. </P
8450><P
8451>The above-mentioned article gives a very good overview of its uses and
8452capabilites.  The man page has more details. It can be used in variety of
8453situations. One good example is building of linux kernel in text mode. Linux
8454kernel uses a modified version of dialog tailored for its needs. </P
8455><P
8456>dialog was initially designed to be used with shell scripts. If you want to use
8457its functionality in a c program, then you can use libdialog. The documentation
8458regarding this is sparse. Definitive reference is the dialog.h header file which
8459comes with the library. You may need to hack here and there to get the required
8460output. The source is easily customizable. I have used it on a number of
8461occasions by modifying the code.</P
8462></DIV
8463><DIV
8464CLASS="SECT2"
8465><HR><H3
8466CLASS="SECT2"
8467><A
8468NAME="PERLCURSES"
8469>19.3. Perl Curses Modules CURSES::FORM and CURSES::WIDGETS</A
8470></H3
8471><P
8472>The perl module Curses, Curses::Form and Curses::Widgets give access to curses
8473from perl.  If you have curses and basic perl is installed, you can get these
8474modules from <A
8475HREF="http://www.cpan.org/modules/01modules.index.html"
8476TARGET="_top"
8477> CPAN
8478All Modules page</A
8479>.  Get the three zipped modules in the Curses category.
8480Once installed you can use these modules from perl scripts like any other
8481module. For more information on perl modules see perlmod man page. The above
8482modules come with good documentation and they have some demo scripts to test the
8483functionality. Though the widgets provided are very rudimentary, these modules
8484provide good access to curses library from perl.</P
8485><P
8486>Some of my code examples are converted to perl by Anuradha Ratnaweera and they
8487are available in the <TT
8488CLASS="LITERAL"
8489>perl</TT
8490> directory.</P
8491><P
8492>
8493For more information see man pages Curses(3) , Curses::Form(3) and
8494Curses::Widgets(3).  These pages are installed only when the above modules are
8495acquired and installed.</P
8496></DIV
8497></DIV
8498><DIV
8499CLASS="SECT1"
8500><HR><H2
8501CLASS="SECT1"
8502><A
8503NAME="JUSTFORFUN"
8504>20. Just For Fun !!!</A
8505></H2
8506><P
8507>This section contains few programs written by me just for fun. They don't
8508signify a better programming practice or the best way of using ncurses. They are
8509provided here so as to allow beginners to get ideas and add more programs to
8510this section.  If you have written a couple of nice, simple programs in curses
8511and want them to included here, contact <A
8512HREF="mailto:ppadala@gmail.com"
8513TARGET="_top"
8514>me</A
8515>.</P
8516><DIV
8517CLASS="SECT2"
8518><HR><H3
8519CLASS="SECT2"
8520><A
8521NAME="GAMEOFLIFE"
8522>20.1. The Game of Life</A
8523></H3
8524><P
8525>Game of life is a wonder of math. In
8526<A
8527HREF="http://www.math.com/students/wonders/life/life.html"
8528TARGET="_top"
8529>Paul Callahan</A
8530>'s words</P
8531><PRE
8532CLASS="PROGRAMLISTING"
8533><SPAN
8534CLASS="emphasis"
8535><I
8536CLASS="EMPHASIS"
8537>The Game of Life (or simply Life) is not a game in the conventional sense. There
8538are no players, and no winning or losing. Once the "pieces" are placed in the
8539starting position, the rules determine everything that happens later.
8540Nevertheless, Life is full of surprises! In most cases, it is impossible to look
8541at a starting position (or pattern) and see what will happen in the future. The
8542only way to find out is to follow the rules of the game.</I
8543></SPAN
8544></PRE
8545><P
8546>This program starts with a simple inverted U pattern and shows how wonderful
8547life works. There is a lot of room for improvement in the program. You can let
8548the user enter pattern of his choice or even take input from a file. You can
8549also change rules and play with a lot of variations. Search on <A
8550HREF="http://www.google.com"
8551TARGET="_top"
8552>google</A
8553> for interesting information on game
8554of life.</P
8555><P
8556><SPAN
8557CLASS="emphasis"
8558><I
8559CLASS="EMPHASIS"
8560>File Path: JustForFun/life.c</I
8561></SPAN
8562></P
8563></DIV
8564><DIV
8565CLASS="SECT2"
8566><HR><H3
8567CLASS="SECT2"
8568><A
8569NAME="MAGIC"
8570>20.2. Magic Square</A
8571></H3
8572><P
8573>Magic Square, another wonder of math, is very simple to understand but very
8574difficult to make. In a magic square sum of the numbers in each row, each column
8575is equal.  Even diagnol sum can be equal. There are many variations which have
8576special properties.</P
8577><P
8578>This program creates a simple magic square of odd order.</P
8579><P
8580><SPAN
8581CLASS="emphasis"
8582><I
8583CLASS="EMPHASIS"
8584>File Path: JustForFun/magic.c</I
8585></SPAN
8586></P
8587></DIV
8588><DIV
8589CLASS="SECT2"
8590><HR><H3
8591CLASS="SECT2"
8592><A
8593NAME="HANOI"
8594>20.3. Towers of Hanoi</A
8595></H3
8596><P
8597>The famous towers of hanoi solver. The aim of the game is to move the disks on
8598the first peg to last peg, using middle peg as a temporary stay. The catch is
8599not to place a larger disk over a small disk at any time.</P
8600><P
8601><SPAN
8602CLASS="emphasis"
8603><I
8604CLASS="EMPHASIS"
8605>File Path: JustForFun/hanoi.c</I
8606></SPAN
8607></P
8608></DIV
8609><DIV
8610CLASS="SECT2"
8611><HR><H3
8612CLASS="SECT2"
8613><A
8614NAME="QUEENS"
8615>20.4. Queens Puzzle</A
8616></H3
8617><P
8618>The objective of the famous N-Queen puzzle is to put N queens on a N X N chess
8619board without attacking each other. </P
8620><P
8621>This program solves it with a simple backtracking technique.</P
8622><P
8623><SPAN
8624CLASS="emphasis"
8625><I
8626CLASS="EMPHASIS"
8627>File Path: JustForFun/queens.c</I
8628></SPAN
8629></P
8630></DIV
8631><DIV
8632CLASS="SECT2"
8633><HR><H3
8634CLASS="SECT2"
8635><A
8636NAME="SHUFFLE"
8637>20.5. Shuffle</A
8638></H3
8639><P
8640>A fun game, if you have time to kill. </P
8641><P
8642><SPAN
8643CLASS="emphasis"
8644><I
8645CLASS="EMPHASIS"
8646>File Path: JustForFun/shuffle.c</I
8647></SPAN
8648></P
8649></DIV
8650><DIV
8651CLASS="SECT2"
8652><HR><H3
8653CLASS="SECT2"
8654><A
8655NAME="TT"
8656>20.6. Typing Tutor</A
8657></H3
8658><P
8659>A simple typing tutor, I created more out of need than for ease of use. If you
8660know how to put your fingers correctly on the keyboard, but lack practice, this
8661can be helpful. </P
8662><P
8663><SPAN
8664CLASS="emphasis"
8665><I
8666CLASS="EMPHASIS"
8667>File Path: JustForFun/tt.c</I
8668></SPAN
8669></P
8670></DIV
8671></DIV
8672><DIV
8673CLASS="SECT1"
8674><HR><H2
8675CLASS="SECT1"
8676><A
8677NAME="REF"
8678>21. References</A
8679></H2
8680><P
8681></P
8682><UL
8683><LI
8684><P
8685>NCURSES man pages </P
8686></LI
8687><LI
8688><P
8689>NCURSES FAQ at <A
8690HREF="http://invisible-island.net/ncurses/ncurses.faq.html"
8691TARGET="_top"
8692>http://invisible-island.net/ncurses/ncurses.faq.html</A
8693>
8694    </P
8695></LI
8696><LI
8697><P
8698>Writing programs with NCURSES by Eric Raymond and Zeyd M.
8699Ben-Halim at
8700<A
8701HREF="http://invisible-island.net/ncurses/ncurses-intro.html"
8702TARGET="_top"
8703>http://invisible-island.net/ncurses/ncurses-intro.html</A
8704> - somewhat
8705obsolete. I was inspired by this document and the structure of this HOWTO
8706follows from the original document</P
8707></LI
8708></UL
8709></DIV
8710></DIV
8711></BODY
8712></HTML
8713>