1166124Srafan<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 2166124Srafan<html> 3166124Srafan<head> 4166124Srafan<meta name="generator" content= 5166124Srafan"HTML Tidy for Linux/x86 (vers 1st December 2004), see www.w3.org"> 6166124Srafan<title>NCURSES Programming HOWTO</title> 7166124Srafan<meta name="GENERATOR" content= 8166124Srafan"Modular DocBook HTML Stylesheet Version 1.7"> 9166124Srafan</head> 10166124Srafan<body class="ARTICLE" bgcolor="#FFFFFF" text="#000000" link= 11166124Srafan"#0000FF" vlink="#840084" alink="#0000FF"> 12166124Srafan<div class="ARTICLE"> 13166124Srafan<div class="TITLEPAGE"> 14166124Srafan<h1 class="TITLE"><a name="AEN2" id="AEN2">NCURSES Programming 15166124SrafanHOWTO</a></h1> 16166124Srafan<h3 class="AUTHOR"><a name="AEN4" id="AEN4">Pradeep Padala</a></h3> 17166124Srafan<div class="AFFILIATION"> 18166124Srafan<div class="ADDRESS"> 19166124Srafan<p class="ADDRESS"><code class="EMAIL"><<a href= 20166124Srafan"mailto:ppadala@gmail.com">ppadala@gmail.com</a>></code></p> 21166124Srafan</div> 22166124Srafan</div> 23166124Srafan<p class="PUBDATE">v1.9, 2005-06-20<br></p> 24166124Srafan<div class="REVHISTORY"> 25166124Srafan<table width="100%" border="0"> 26166124Srafan<tr> 27166124Srafan<th align="left" valign="top" colspan="3"><b>Revision 28166124SrafanHistory</b></th> 29166124Srafan</tr> 30166124Srafan<tr> 31166124Srafan<td align="left">Revision 1.9</td> 32166124Srafan<td align="left">2005-06-20</td> 33166124Srafan<td align="left">Revised by: ppadala</td> 34166124Srafan</tr> 35166124Srafan<tr> 36166124Srafan<td align="left" colspan="3">The license has been changed to the 37166124SrafanMIT-style license used by NCURSES. Note that the programs are also 38166124Srafanre-licensed under this.</td> 39166124Srafan</tr> 40166124Srafan<tr> 41166124Srafan<td align="left">Revision 1.8</td> 42166124Srafan<td align="left">2005-06-17</td> 43166124Srafan<td align="left">Revised by: ppadala</td> 44166124Srafan</tr> 45166124Srafan<tr> 46166124Srafan<td align="left" colspan="3">Lots of updates. Added references and 47166124Srafanperl examples. Changes to examples. Many grammatical and stylistic 48166124Srafanchanges to the content. Changes to NCURSES history.</td> 49166124Srafan</tr> 50166124Srafan<tr> 51166124Srafan<td align="left">Revision 1.7.1</td> 52166124Srafan<td align="left">2002-06-25</td> 53166124Srafan<td align="left">Revised by: ppadala</td> 54166124Srafan</tr> 55166124Srafan<tr> 56166124Srafan<td align="left" colspan="3">Added a README file for building and 57166124Srafaninstructions for building from source.</td> 58166124Srafan</tr> 59166124Srafan<tr> 60166124Srafan<td align="left">Revision 1.7</td> 61166124Srafan<td align="left">2002-06-25</td> 62166124Srafan<td align="left">Revised by: ppadala</td> 63166124Srafan</tr> 64166124Srafan<tr> 65166124Srafan<td align="left" colspan="3">Added "Other formats" section and made 66166124Srafana lot of fancy changes to the programs. Inlining of programs is 67166124Srafangone.</td> 68166124Srafan</tr> 69166124Srafan<tr> 70166124Srafan<td align="left">Revision 1.6.1</td> 71166124Srafan<td align="left">2002-02-24</td> 72166124Srafan<td align="left">Revised by: ppadala</td> 73166124Srafan</tr> 74166124Srafan<tr> 75166124Srafan<td align="left" colspan="3">Removed the old Changelog section, 76166124Srafancleaned the makefiles</td> 77166124Srafan</tr> 78166124Srafan<tr> 79166124Srafan<td align="left">Revision 1.6</td> 80166124Srafan<td align="left">2002-02-16</td> 81166124Srafan<td align="left">Revised by: ppadala</td> 82166124Srafan</tr> 83166124Srafan<tr> 84166124Srafan<td align="left" colspan="3">Corrected a lot of spelling mistakes, 85166124Srafanadded ACS variables section</td> 86166124Srafan</tr> 87166124Srafan<tr> 88166124Srafan<td align="left">Revision 1.5</td> 89166124Srafan<td align="left">2002-01-05</td> 90166124Srafan<td align="left">Revised by: ppadala</td> 91166124Srafan</tr> 92166124Srafan<tr> 93166124Srafan<td align="left" colspan="3">Changed structure to present proper 94166124SrafanTOC</td> 95166124Srafan</tr> 96166124Srafan<tr> 97166124Srafan<td align="left">Revision 1.3.1</td> 98166124Srafan<td align="left">2001-07-26</td> 99166124Srafan<td align="left">Revised by: ppadala</td> 100166124Srafan</tr> 101166124Srafan<tr> 102166124Srafan<td align="left" colspan="3">Corrected maintainers paragraph, 103166124SrafanCorrected stable release number</td> 104166124Srafan</tr> 105166124Srafan<tr> 106166124Srafan<td align="left">Revision 1.3</td> 107166124Srafan<td align="left">2001-07-24</td> 108166124Srafan<td align="left">Revised by: ppadala</td> 109166124Srafan</tr> 110166124Srafan<tr> 111166124Srafan<td align="left" colspan="3">Added copyright notices to main 112166124Srafandocument (LDP license) and programs (GPL), Corrected 113166124Srafanprintw_example.</td> 114166124Srafan</tr> 115166124Srafan<tr> 116166124Srafan<td align="left">Revision 1.2</td> 117166124Srafan<td align="left">2001-06-05</td> 118166124Srafan<td align="left">Revised by: ppadala</td> 119166124Srafan</tr> 120166124Srafan<tr> 121166124Srafan<td align="left" colspan="3">Incorporated ravi's changes. Mainly to 122166124Srafanintroduction, menu, form, justforfun sections</td> 123166124Srafan</tr> 124166124Srafan<tr> 125166124Srafan<td align="left">Revision 1.1</td> 126166124Srafan<td align="left">2001-05-22</td> 127166124Srafan<td align="left">Revised by: ppadala</td> 128166124Srafan</tr> 129166124Srafan<tr> 130166124Srafan<td align="left" colspan="3">Added "a word about window" section, 131166124SrafanAdded scanw_example.</td> 132166124Srafan</tr> 133166124Srafan</table> 134166124Srafan</div> 135166124Srafan<div> 136166124Srafan<div class="ABSTRACT"><a name="AEN67" id="AEN67"></a> 137166124Srafan<p><em>This document is intended to be an "All in One" guide for 138166124Srafanprogramming with ncurses and its sister libraries. We graduate from 139166124Srafana simple "Hello World" program to more complex form manipulation. 140166124SrafanNo prior experience in ncurses is assumed. Send comments to 141166124Srafan<a href="mailto:ppadala@gmail.com" target="_top">this 142166124Srafanaddress</a></em></p> 143166124Srafan</div> 144166124Srafan</div> 145166124Srafan<hr></div> 146166124Srafan<div class="TOC"> 147166124Srafan<dl> 148166124Srafan<dt><b>Table of Contents</b></dt> 149166124Srafan<dt>1. <a href="#INTRO">Introduction</a></dt> 150166124Srafan<dd> 151166124Srafan<dl> 152166124Srafan<dt>1.1. <a href="#WHATIS">What is NCURSES?</a></dt> 153166124Srafan<dt>1.2. <a href="#WHATCANWEDO">What we can do with 154166124SrafanNCURSES</a></dt> 155166124Srafan<dt>1.3. <a href="#WHERETOGETIT">Where to get it</a></dt> 156166124Srafan<dt>1.4. <a href="#PURPOSE">Purpose/Scope of the document</a></dt> 157166124Srafan<dt>1.5. <a href="#ABOUTPROGRAMS">About the Programs</a></dt> 158166124Srafan<dt>1.6. <a href="#OTHERFORMATS">Other Formats of the 159166124Srafandocument</a></dt> 160166124Srafan<dt>1.7. <a href="#CREDITS">Credits</a></dt> 161166124Srafan<dt>1.8. <a href="#WISHLIST">Wish List</a></dt> 162166124Srafan<dt>1.9. <a href="#COPYRIGHT">Copyright</a></dt> 163166124Srafan</dl> 164166124Srafan</dd> 165166124Srafan<dt>2. <a href="#HELLOWORLD">Hello World !!!</a></dt> 166166124Srafan<dd> 167166124Srafan<dl> 168166124Srafan<dt>2.1. <a href="#COMPILECURSES">Compiling With the NCURSES 169166124SrafanLibrary</a></dt> 170166124Srafan<dt>2.2. <a href="#DISSECTION">Dissection</a></dt> 171166124Srafan</dl> 172166124Srafan</dd> 173166124Srafan<dt>3. <a href="#GORY">The Gory Details</a></dt> 174166124Srafan<dt>4. <a href="#INIT">Initialization</a></dt> 175166124Srafan<dd> 176166124Srafan<dl> 177166124Srafan<dt>4.1. <a href="#ABOUTINIT">Initialization functions</a></dt> 178166124Srafan<dt>4.2. <a href="#RAWCBREAK">raw() and cbreak()</a></dt> 179166124Srafan<dt>4.3. <a href="#ECHONOECHO">echo() and noecho()</a></dt> 180166124Srafan<dt>4.4. <a href="#KEYPAD">keypad()</a></dt> 181166124Srafan<dt>4.5. <a href="#HALFDELAY">halfdelay()</a></dt> 182166124Srafan<dt>4.6. <a href="#MISCINIT">Miscellaneous Initialization 183166124Srafanfunctions</a></dt> 184166124Srafan<dt>4.7. <a href="#INITEX">An Example</a></dt> 185166124Srafan</dl> 186166124Srafan</dd> 187166124Srafan<dt>5. <a href="#AWORDWINDOWS">A Word about Windows</a></dt> 188166124Srafan<dt>6. <a href="#PRINTW">Output functions</a></dt> 189166124Srafan<dd> 190166124Srafan<dl> 191166124Srafan<dt>6.1. <a href="#ADDCHCLASS">addch() class of functions</a></dt> 192166124Srafan<dt>6.2. <a href="#AEN298">mvaddch(), waddch() and 193166124Srafanmvwaddch()</a></dt> 194166124Srafan<dt>6.3. <a href="#PRINTWCLASS">printw() class of 195166124Srafanfunctions</a></dt> 196166124Srafan<dt>6.4. <a href="#ADDSTRCLASS">addstr() class of 197166124Srafanfunctions</a></dt> 198166124Srafan<dt>6.5. <a href="#ACAUTION">A word of caution</a></dt> 199166124Srafan</dl> 200166124Srafan</dd> 201166124Srafan<dt>7. <a href="#SCANW">Input functions</a></dt> 202166124Srafan<dd> 203166124Srafan<dl> 204166124Srafan<dt>7.1. <a href="#GETCHCLASS">getch() class of functions</a></dt> 205166124Srafan<dt>7.2. <a href="#SCANWCLASS">scanw() class of functions</a></dt> 206166124Srafan<dt>7.3. <a href="#GETSTRCLASS">getstr() class of 207166124Srafanfunctions</a></dt> 208166124Srafan<dt>7.4. <a href="#GETSTREX">Some examples</a></dt> 209166124Srafan</dl> 210166124Srafan</dd> 211166124Srafan<dt>8. <a href="#ATTRIB">Attributes</a></dt> 212166124Srafan<dd> 213166124Srafan<dl> 214166124Srafan<dt>8.1. <a href="#ATTRIBDETAILS">The details</a></dt> 215166124Srafan<dt>8.2. <a href="#ATTRONVSATTRSET">attron() vs attrset()</a></dt> 216166124Srafan<dt>8.3. <a href="#ATTR_GET">attr_get()</a></dt> 217166124Srafan<dt>8.4. <a href="#ATTR_FUNCS">attr_ functions</a></dt> 218166124Srafan<dt>8.5. <a href="#WATTRFUNCS">wattr functions</a></dt> 219166124Srafan<dt>8.6. <a href="#CHGAT">chgat() functions</a></dt> 220166124Srafan</dl> 221166124Srafan</dd> 222166124Srafan<dt>9. <a href="#WINDOWS">Windows</a></dt> 223166124Srafan<dd> 224166124Srafan<dl> 225166124Srafan<dt>9.1. <a href="#WINDOWBASICS">The basics</a></dt> 226166124Srafan<dt>9.2. <a href="#LETBEWINDOW">Let there be a Window !!!</a></dt> 227166124Srafan<dt>9.3. <a href="#BORDEREXEXPL">Explanation</a></dt> 228166124Srafan<dt>9.4. <a href="#OTHERSTUFF">The other stuff in the 229166124Srafanexample</a></dt> 230166124Srafan<dt>9.5. <a href="#OTHERBORDERFUNCS">Other Border 231166124Srafanfunctions</a></dt> 232166124Srafan</dl> 233166124Srafan</dd> 234166124Srafan<dt>10. <a href="#COLOR">Colors</a></dt> 235166124Srafan<dd> 236166124Srafan<dl> 237166124Srafan<dt>10.1. <a href="#COLORBASICS">The basics</a></dt> 238166124Srafan<dt>10.2. <a href="#CHANGECOLORDEFS">Changing Color 239166124SrafanDefinitions</a></dt> 240166124Srafan<dt>10.3. <a href="#COLORCONTENT">Color Content</a></dt> 241166124Srafan</dl> 242166124Srafan</dd> 243166124Srafan<dt>11. <a href="#KEYS">Interfacing with the key board</a></dt> 244166124Srafan<dd> 245166124Srafan<dl> 246166124Srafan<dt>11.1. <a href="#KEYSBASICS">The Basics</a></dt> 247166124Srafan<dt>11.2. <a href="#SIMPLEKEYEX">A Simple Key Usage 248166124Srafanexample</a></dt> 249166124Srafan</dl> 250166124Srafan</dd> 251166124Srafan<dt>12. <a href="#MOUSE">Interfacing with the mouse</a></dt> 252166124Srafan<dd> 253166124Srafan<dl> 254166124Srafan<dt>12.1. <a href="#MOUSEBASICS">The Basics</a></dt> 255166124Srafan<dt>12.2. <a href="#GETTINGEVENTS">Getting the events</a></dt> 256166124Srafan<dt>12.3. <a href="#MOUSETOGETHER">Putting it all Together</a></dt> 257166124Srafan<dt>12.4. <a href="#MISCMOUSEFUNCS">Miscellaneous 258166124SrafanFunctions</a></dt> 259166124Srafan</dl> 260166124Srafan</dd> 261166124Srafan<dt>13. <a href="#SCREEN">Screen Manipulation</a></dt> 262166124Srafan<dd> 263166124Srafan<dl> 264166124Srafan<dt>13.1. <a href="#GETYX">getyx() functions</a></dt> 265166124Srafan<dt>13.2. <a href="#SCREENDUMP">Screen Dumping</a></dt> 266166124Srafan<dt>13.3. <a href="#WINDOWDUMP">Window Dumping</a></dt> 267166124Srafan</dl> 268166124Srafan</dd> 269166124Srafan<dt>14. <a href="#MISC">Miscellaneous features</a></dt> 270166124Srafan<dd> 271166124Srafan<dl> 272166124Srafan<dt>14.1. <a href="#CURSSET">curs_set()</a></dt> 273166124Srafan<dt>14.2. <a href="#TEMPLEAVE">Temporarily Leaving Curses 274166124Srafanmode</a></dt> 275166124Srafan<dt>14.3. <a href="#ACSVARS">ACS_ variables</a></dt> 276166124Srafan</dl> 277166124Srafan</dd> 278166124Srafan<dt>15. <a href="#OTHERLIB">Other libraries</a></dt> 279166124Srafan<dt>16. <a href="#PANELS">Panel Library</a></dt> 280166124Srafan<dd> 281166124Srafan<dl> 282166124Srafan<dt>16.1. <a href="#PANELBASICS">The Basics</a></dt> 283166124Srafan<dt>16.2. <a href="#COMPILEPANELS">Compiling With the Panels 284166124SrafanLibrary</a></dt> 285166124Srafan<dt>16.3. <a href="#PANELBROWSING">Panel Window Browsing</a></dt> 286166124Srafan<dt>16.4. <a href="#USERPTRUSING">Using User Pointers</a></dt> 287166124Srafan<dt>16.5. <a href="#PANELMOVERESIZE">Moving and Resizing 288166124SrafanPanels</a></dt> 289166124Srafan<dt>16.6. <a href="#PANELSHOWHIDE">Hiding and Showing 290166124SrafanPanels</a></dt> 291166124Srafan<dt>16.7. <a href="#PANELABOVE">panel_above() and panel_below() 292166124SrafanFunctions</a></dt> 293166124Srafan</dl> 294166124Srafan</dd> 295166124Srafan<dt>17. <a href="#MENUS">Menus Library</a></dt> 296166124Srafan<dd> 297166124Srafan<dl> 298166124Srafan<dt>17.1. <a href="#MENUBASICS">The Basics</a></dt> 299166124Srafan<dt>17.2. <a href="#COMPILEMENUS">Compiling With the Menu 300166124SrafanLibrary</a></dt> 301166124Srafan<dt>17.3. <a href="#MENUDRIVER">Menu Driver: The work horse of the 302166124Srafanmenu system</a></dt> 303166124Srafan<dt>17.4. <a href="#MENUWINDOWS">Menu Windows</a></dt> 304166124Srafan<dt>17.5. <a href="#SCROLLMENUS">Scrolling Menus</a></dt> 305166124Srafan<dt>17.6. <a href="#MULTICOLUMN">Multi Columnar Menus</a></dt> 306166124Srafan<dt>17.7. <a href="#MULTIVALUEMENUS">Multi Valued Menus</a></dt> 307166124Srafan<dt>17.8. <a href="#MENUOPT">Menu Options</a></dt> 308166124Srafan<dt>17.9. <a href="#MENUUSERPTR">The useful User Pointer</a></dt> 309166124Srafan</dl> 310166124Srafan</dd> 311166124Srafan<dt>18. <a href="#FORMS">Forms Library</a></dt> 312166124Srafan<dd> 313166124Srafan<dl> 314166124Srafan<dt>18.1. <a href="#FORMBASICS">The Basics</a></dt> 315166124Srafan<dt>18.2. <a href="#COMPILEFORMS">Compiling With the Forms 316166124SrafanLibrary</a></dt> 317166124Srafan<dt>18.3. <a href="#PLAYFIELDS">Playing with Fields</a></dt> 318166124Srafan<dt>18.4. <a href="#FORMWINDOWS">Form Windows</a></dt> 319166124Srafan<dt>18.5. <a href="#FILEDVALIDATE">Field Validation</a></dt> 320166124Srafan<dt>18.6. <a href="#FORMDRIVER">Form Driver: The work horse of the 321166124Srafanforms system</a></dt> 322166124Srafan</dl> 323166124Srafan</dd> 324166124Srafan<dt>19. <a href="#TOOLS">Tools and Widget Libraries</a></dt> 325166124Srafan<dd> 326166124Srafan<dl> 327166124Srafan<dt>19.1. <a href="#CDK">CDK (Curses Development Kit)</a></dt> 328166124Srafan<dt>19.2. <a href="#DIALOG">The dialog</a></dt> 329166124Srafan<dt>19.3. <a href="#PERLCURSES">Perl Curses Modules CURSES::FORM 330166124Srafanand CURSES::WIDGETS</a></dt> 331166124Srafan</dl> 332166124Srafan</dd> 333166124Srafan<dt>20. <a href="#JUSTFORFUN">Just For Fun !!!</a></dt> 334166124Srafan<dd> 335166124Srafan<dl> 336166124Srafan<dt>20.1. <a href="#GAMEOFLIFE">The Game of Life</a></dt> 337166124Srafan<dt>20.2. <a href="#MAGIC">Magic Square</a></dt> 338166124Srafan<dt>20.3. <a href="#HANOI">Towers of Hanoi</a></dt> 339166124Srafan<dt>20.4. <a href="#QUEENS">Queens Puzzle</a></dt> 340166124Srafan<dt>20.5. <a href="#SHUFFLE">Shuffle</a></dt> 341166124Srafan<dt>20.6. <a href="#TT">Typing Tutor</a></dt> 342166124Srafan</dl> 343166124Srafan</dd> 344166124Srafan<dt>21. <a href="#REF">References</a></dt> 345166124Srafan</dl> 346166124Srafan</div> 347166124Srafan<div class="SECT1"> 348166124Srafan<h2 class="SECT1"><a name="INTRO" id="INTRO">1. 349166124SrafanIntroduction</a></h2> 350166124Srafan<p>In the olden days of teletype terminals, terminals were away 351166124Srafanfrom computers and were connected to them through serial cables. 352166124SrafanThe terminals could be configured by sending a series of bytes. All 353166124Srafanthe capabilities (such as moving the cursor to a new location, 354166124Srafanerasing part of the screen, scrolling the screen, changing modes 355166124Srafanetc.) of terminals could be accessed through these series of bytes. 356166124SrafanThese control seeuqnces are usually called escape sequences, 357166124Srafanbecause they start with an escape(0x1B) character. Even today, with 358166124Srafanproper emulation, we can send escape sequences to the emulator and 359166124Srafanachieve the same effect on a terminal window.</p> 360166124Srafan<p>Suppose you wanted to print a line in color. Try typing this on 361166124Srafanyour console.</p> 362166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 363166124Srafan<tr> 364166124Srafan<td> 365166124Srafan<pre class="PROGRAMLISTING"> 366166124Srafan<font color="#000000">echo "^[[0;31;40mIn Color"</font> 367166124Srafan</pre></td> 368166124Srafan</tr> 369166124Srafan</table> 370166124Srafan<p>The first character is an escape character, which looks like two 371166124Srafancharacters ^ and [. To be able to print it, you have to press 372166124SrafanCTRL+V and then the ESC key. All the others are normal printable 373166124Srafancharacters. You should be able to see the string "In Color" in red. 374166124SrafanIt stays that way and to revert back to the original mode type 375166124Srafanthis.</p> 376166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 377166124Srafan<tr> 378166124Srafan<td> 379166124Srafan<pre class="PROGRAMLISTING"> 380166124Srafan<font color="#000000">echo "^[[0;37;40m"</font> 381166124Srafan</pre></td> 382166124Srafan</tr> 383166124Srafan</table> 384166124Srafan<p>Now, what do these magic characters mean? Difficult to 385166124Srafancomprehend? They might even be different for different terminals. 386166124SrafanSo the designers of UNIX invented a mechanism named <var class= 387166124Srafan"LITERAL">termcap</var>. It is a file that lists all the 388166124Srafancapabilities of a particular terminal, along with the escape 389166124Srafansequences needed to achieve a particular effect. In the later 390166124Srafanyears, this was replaced by <var class="LITERAL">terminfo</var>. 391166124SrafanWithout delving too much into details, this mechanism allows 392166124Srafanapplication programs to query the terminfo database and obtain the 393166124Srafancontrol characters to be sent to a terminal or terminal 394166124Srafanemulator.</p> 395166124Srafan<div class="SECT2"> 396166124Srafan<hr> 397166124Srafan<h3 class="SECT2"><a name="WHATIS" id="WHATIS">1.1. What is 398166124SrafanNCURSES?</a></h3> 399166124Srafan<p>You might be wondering, what the import of all this technical 400166124Srafangibberish is. In the above scenario, every application program is 401166124Srafansupposed to query the terminfo and perform the necessary stuff 402166124Srafan(sending control characters etc.). It soon became difficult to 403166124Srafanmanage this complexity and this gave birth to 'CURSES'. Curses is a 404166124Srafanpun on the name "cursor optimization". The Curses library forms a 405166124Srafanwrapper over working with raw terminal codes, and provides highly 406166124Srafanflexible and efficient API (Application Programming Interface). It 407166124Srafanprovides functions to move the cursor, create windows, produce 408166124Srafancolors, play with mouse etc. The application programs need not 409166124Srafanworry about the underlying terminal capabilities.</p> 410166124Srafan<p>So what is NCURSES? NCURSES is a clone of the original System V 411166124SrafanRelease 4.0 (SVr4) curses. It is a freely distributable library, 412166124Srafanfully compatible with older version of curses. In short, it is a 413166124Srafanlibrary of functions that manages an application's display on 414166124Srafancharacter-cell terminals. In the remainder of the document, the 415166124Srafanterms curses and ncurses are used interchangeably.</p> 416166124Srafan<p>A detailed history of NCURSES can be found in the NEWS file from 417166124Srafanthe source distribution. The current package is maintained by 418166124Srafan<a href="mailto:dickey@his.com" target="_top">Thomas Dickey</a>. 419166124SrafanYou can contact the maintainers at <a href= 420166124Srafan"mailto:bug-ncurses@gnu.org" target= 421166124Srafan"_top">bug-ncurses@gnu.org</a>.</p> 422166124Srafan</div> 423166124Srafan<div class="SECT2"> 424166124Srafan<hr> 425166124Srafan<h3 class="SECT2"><a name="WHATCANWEDO" id="WHATCANWEDO">1.2. What 426166124Srafanwe can do with NCURSES</a></h3> 427166124Srafan<p>NCURSES not only creates a wrapper over terminal capabilities, 428166124Srafanbut also gives a robust framework to create nice looking UI (User 429166124SrafanInterface)s in text mode. It provides functions to create windows 430166124Srafanetc. Its sister libraries panel, menu and form provide an extension 431166124Srafanto the basic curses library. These libraries usually come along 432166124Srafanwith curses. One can create applications that contain multiple 433166124Srafanwindows, menus, panels and forms. Windows can be managed 434166124Srafanindependently, can provide 'scrollability' and even can be 435166124Srafanhidden.</p> 436166124Srafan<p>Menus provide the user with an easy command selection option. 437166124SrafanForms allow the creation of easy-to-use data entry and display 438166124Srafanwindows. Panels extend the capabilities of ncurses to deal with 439166124Srafanoverlapping and stacked windows.</p> 440166124Srafan<p>These are just some of the basic things we can do with ncurses. 441166124SrafanAs we move along, We will see all the capabilities of these 442166124Srafanlibraries.</p> 443166124Srafan</div> 444166124Srafan<div class="SECT2"> 445166124Srafan<hr> 446166124Srafan<h3 class="SECT2"><a name="WHERETOGETIT" id="WHERETOGETIT">1.3. 447166124SrafanWhere to get it</a></h3> 448166124Srafan<p>All right, now that you know what you can do with ncurses, you 449166124Srafanmust be rearing to get started. NCURSES is usually shipped with 450166124Srafanyour installation. In case you don't have the library or want to 451166124Srafancompile it on your own, read on.</p> 452166124Srafan<p><em>Compiling the package</em></p> 453166124Srafan<p>NCURSES can be obtained from <a href= 454166124Srafan"ftp://ftp.gnu.org/pub/gnu/ncurses/ncurses.tar.gz" target= 455166124Srafan"_top">ftp://ftp.gnu.org/pub/gnu/ncurses/ncurses.tar.gz</a> or any 456166124Srafanof the ftp sites mentioned in <a href= 457166124Srafan"http://www.gnu.org/order/ftp.html" target= 458166124Srafan"_top">http://www.gnu.org/order/ftp.html</a>.</p> 459166124Srafan<p>Read the README and INSTALL files for details on to how to 460166124Srafaninstall it. It usually involves the following operations.</p> 461166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 462166124Srafan<tr> 463166124Srafan<td> 464166124Srafan<pre class="PROGRAMLISTING"> 465166124Srafan<font color= 466166124Srafan"#000000"> tar zxvf ncurses<version>.tar.gz # unzip and untar the archive 467166124Srafan cd ncurses<version> # cd to the directory 468166124Srafan ./configure # configure the build according to your 469166124Srafan # environment 470166124Srafan make # make it 471166124Srafan su root # become root 472166124Srafan make install # install it</font> 473166124Srafan</pre></td> 474166124Srafan</tr> 475166124Srafan</table> 476166124Srafan<p><em>Using the RPM</em></p> 477166124Srafan<p>NCURSES RPM can be found and downloaded from <a href= 478166124Srafan"http://rpmfind.net" target="_top">http://rpmfind.net</a> . The RPM 479166124Srafancan be installed with the following command after becoming 480166124Srafanroot.</p> 481166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 482166124Srafan<tr> 483166124Srafan<td> 484166124Srafan<pre class="PROGRAMLISTING"> 485166124Srafan<font color="#000000"> rpm -i <downloaded rpm></font> 486166124Srafan</pre></td> 487166124Srafan</tr> 488166124Srafan</table> 489166124Srafan</div> 490166124Srafan<div class="SECT2"> 491166124Srafan<hr> 492166124Srafan<h3 class="SECT2"><a name="PURPOSE" id="PURPOSE">1.4. Purpose/Scope 493166124Srafanof the document</a></h3> 494166124Srafan<p>This document is intended to be a "All in One" guide for 495166124Srafanprogramming with ncurses and its sister libraries. We graduate from 496166124Srafana simple "Hello World" program to more complex form manipulation. 497166124SrafanNo prior experience in ncurses is assumed. The writing is informal, 498166124Srafanbut a lot of detail is provided for each of the examples.</p> 499166124Srafan</div> 500166124Srafan<div class="SECT2"> 501166124Srafan<hr> 502166124Srafan<h3 class="SECT2"><a name="ABOUTPROGRAMS" id="ABOUTPROGRAMS">1.5. 503166124SrafanAbout the Programs</a></h3> 504166124Srafan<p>All the programs in the document are available in zipped form 505166124Srafan<a href= 506166124Srafan"http://www.tldp.org/HOWTO/NCURSES-Programming-HOWTO/ncurses_programs.tar.gz" 507166124Srafantarget="_top">here</a>. Unzip and untar it. The directory structure 508166124Srafanlooks like this.</p> 509166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 510166124Srafan<tr> 511166124Srafan<td> 512166124Srafan<pre class="PROGRAMLISTING"> 513166124Srafan<font color="#000000">ncurses 514166124Srafan | 515166124Srafan |----> JustForFun -- just for fun programs 516166124Srafan |----> basics -- basic programs 517166124Srafan |----> demo -- output files go into this directory after make 518166124Srafan | | 519166124Srafan | |----> exe -- exe files of all example programs 520166124Srafan |----> forms -- programs related to form library 521166124Srafan |----> menus -- programs related to menus library 522166124Srafan |----> panels -- programs related to panels library 523166124Srafan |----> perl -- perl equivalents of the examples (contributed 524166124Srafan | by Anuradha Ratnaweera) 525166124Srafan |----> Makefile -- the top level Makefile 526166124Srafan |----> README -- the top level README file. contains instructions 527166124Srafan |----> COPYING -- copyright notice</font> 528166124Srafan</pre></td> 529166124Srafan</tr> 530166124Srafan</table> 531166124Srafan<p>The individual directories contain the following files.</p> 532166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 533166124Srafan<tr> 534166124Srafan<td> 535166124Srafan<pre class="PROGRAMLISTING"> 536166124Srafan<font color="#000000">Description of files in each directory 537166124Srafan-------------------------------------- 538166124SrafanJustForFun 539166124Srafan | 540166124Srafan |----> hanoi.c -- The Towers of Hanoi Solver 541166124Srafan |----> life.c -- The Game of Life demo 542166124Srafan |----> magic.c -- An Odd Order Magic Square builder 543166124Srafan |----> queens.c -- The famous N-Queens Solver 544166124Srafan |----> shuffle.c -- A fun game, if you have time to kill 545166124Srafan |----> tt.c -- A very trivial typing tutor 546166124Srafan 547166124Srafan basics 548166124Srafan | 549166124Srafan |----> acs_vars.c -- ACS_ variables example 550166124Srafan |----> hello_world.c -- Simple "Hello World" Program 551166124Srafan |----> init_func_example.c -- Initialization functions example 552166124Srafan |----> key_code.c -- Shows the scan code of the key pressed 553166124Srafan |----> mouse_menu.c -- A menu accessible by mouse 554166124Srafan |----> other_border.c -- Shows usage of other border functions apa 555166124Srafan | -- rt from box() 556166124Srafan |----> printw_example.c -- A very simple printw() example 557166124Srafan |----> scanw_example.c -- A very simple getstr() example 558166124Srafan |----> simple_attr.c -- A program that can print a c file with 559166124Srafan | -- comments in attribute 560166124Srafan |----> simple_color.c -- A simple example demonstrating colors 561166124Srafan |----> simple_key.c -- A menu accessible with keyboard UP, DOWN 562166124Srafan | -- arrows 563166124Srafan |----> temp_leave.c -- Demonstrates temporarily leaving curses mode 564166124Srafan |----> win_border.c -- Shows Creation of windows and borders 565166124Srafan |----> with_chgat.c -- chgat() usage example 566166124Srafan 567166124Srafan forms 568166124Srafan | 569166124Srafan |----> form_attrib.c -- Usage of field attributes 570166124Srafan |----> form_options.c -- Usage of field options 571166124Srafan |----> form_simple.c -- A simple form example 572166124Srafan |----> form_win.c -- Demo of windows associated with forms 573166124Srafan 574166124Srafan menus 575166124Srafan | 576166124Srafan |----> menu_attrib.c -- Usage of menu attributes 577166124Srafan |----> menu_item_data.c -- Usage of item_name() etc.. functions 578166124Srafan |----> menu_multi_column.c -- Creates multi columnar menus 579166124Srafan |----> menu_scroll.c -- Demonstrates scrolling capability of menus 580166124Srafan |----> menu_simple.c -- A simple menu accessed by arrow keys 581166124Srafan |----> menu_toggle.c -- Creates multi valued menus and explains 582166124Srafan | -- REQ_TOGGLE_ITEM 583166124Srafan |----> menu_userptr.c -- Usage of user pointer 584166124Srafan |----> menu_win.c -- Demo of windows associated with menus 585166124Srafan 586166124Srafan panels 587166124Srafan | 588166124Srafan |----> panel_browse.c -- Panel browsing through tab. Usage of user 589166124Srafan | -- pointer 590166124Srafan |----> panel_hide.c -- Hiding and Un hiding of panels 591166124Srafan |----> panel_resize.c -- Moving and resizing of panels 592166124Srafan |----> panel_simple.c -- A simple panel example 593166124Srafan 594166124Srafan perl 595166124Srafan |----> 01-10.pl -- Perl equivalents of first ten example programs</font> 596166124Srafan</pre></td> 597166124Srafan</tr> 598166124Srafan</table> 599166124Srafan<p>There is a top level Makefile included in the main directory. It 600166124Srafanbuilds all the files and puts the ready-to-use exes in demo/exe 601166124Srafandirectory. You can also do selective make by going into the 602166124Srafancorresponding directory. Each directory contains a README file 603166124Srafanexplaining the purpose of each c file in the directory.</p> 604166124Srafan<p>For every example, I have included path name for the file 605166124Srafanrelative to the examples directory.</p> 606166124Srafan<p>If you prefer browsing individual programs, point your browser 607166124Srafanto <a href= 608166124Srafan"http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/ncurses_programs/" 609166124Srafantarget= 610166124Srafan"_top">http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/ncurses_programs/</a></p> 611166124Srafan<p>All the programs are released under the same license that is 612166124Srafanused by ncurses (MIT-style). This gives you the ability to do 613166124Srafanpretty much anything other than claiming them as yours. Feel free 614166124Srafanto use them in your programs as appropriate.</p> 615166124Srafan</div> 616166124Srafan<div class="SECT2"> 617166124Srafan<hr> 618166124Srafan<h3 class="SECT2"><a name="OTHERFORMATS" id="OTHERFORMATS">1.6. 619166124SrafanOther Formats of the document</a></h3> 620166124Srafan<p>This howto is also availabe in various other formats on the 621166124Srafantldp.org site. Here are the links to other formats of this 622166124Srafandocument.</p> 623166124Srafan<div class="SECT3"> 624166124Srafan<hr> 625166124Srafan<h4 class="SECT3"><a name="LISTFORMATS" id="LISTFORMATS">1.6.1. 626166124SrafanReadily available formats from tldp.org</a></h4> 627166124Srafan<ul> 628166124Srafan<li> 629166124Srafan<p><a href= 630166124Srafan"http://www.ibiblio.org/pub/Linux/docs/HOWTO/other-formats/pdf/NCURSES-Programming-HOWTO.pdf" 631166124Srafantarget="_top">Acrobat PDF Format</a></p> 632166124Srafan</li> 633166124Srafan<li> 634166124Srafan<p><a href= 635166124Srafan"http://www.ibiblio.org/pub/Linux/docs/HOWTO/other-formats/ps/NCURSES-Programming-HOWTO.ps.gz" 636166124Srafantarget="_top">PostScript Format</a></p> 637166124Srafan</li> 638166124Srafan<li> 639166124Srafan<p><a href= 640166124Srafan"http://www.ibiblio.org/pub/Linux/docs/HOWTO/other-formats/html/NCURSES-Programming-HOWTO-html.tar.gz" 641166124Srafantarget="_top">In Multiple HTML pages</a></p> 642166124Srafan</li> 643166124Srafan<li> 644166124Srafan<p><a href= 645166124Srafan"http://www.ibiblio.org/pub/Linux/docs/HOWTO/other-formats/html_single/NCURSES-Programming-HOWTO.html" 646166124Srafantarget="_top">In One big HTML format</a></p> 647166124Srafan</li> 648166124Srafan</ul> 649166124Srafan</div> 650166124Srafan<div class="SECT3"> 651166124Srafan<hr> 652166124Srafan<h4 class="SECT3"><a name="BUILDSOURCE" id="BUILDSOURCE">1.6.2. 653166124SrafanBuilding from source</a></h4> 654166124Srafan<p>If above links are broken or if you want to experiment with sgml 655166124Srafanread on.</p> 656166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 657166124Srafan<tr> 658166124Srafan<td> 659166124Srafan<pre class="PROGRAMLISTING"> 660166124Srafan<font color= 661166124Srafan"#000000"> Get both the source and the tar,gzipped programs, available at 662166124Srafan http://cvsview.tldp.org/index.cgi/LDP/howto/docbook/ 663166124Srafan NCURSES-HOWTO/NCURSES-Programming-HOWTO.sgml 664166124Srafan http://cvsview.tldp.org/index.cgi/LDP/howto/docbook/ 665166124Srafan NCURSES-HOWTO/ncurses_programs.tar.gz 666166124Srafan 667166124Srafan Unzip ncurses_programs.tar.gz with 668166124Srafan tar zxvf ncurses_programs.tar.gz 669166124Srafan 670166124Srafan Use jade to create various formats. For example if you just want to create 671166124Srafan the multiple html files, you would use 672166124Srafan jade -t sgml -i html -d <path to docbook html stylesheet> 673166124Srafan NCURSES-Programming-HOWTO.sgml 674166124Srafan to get pdf, first create a single html file of the HOWTO with 675166124Srafan jade -t sgml -i html -d <path to docbook html stylesheet> -V nochunks 676166124Srafan NCURSES-Programming-HOWTO.sgml > NCURSES-ONE-BIG-FILE.html 677166124Srafan then use htmldoc to get pdf file with 678166124Srafan htmldoc --size universal -t pdf --firstpage p1 -f <output file name.pdf> 679166124Srafan NCURSES-ONE-BIG-FILE.html 680166124Srafan for ps, you would use 681166124Srafan htmldoc --size universal -t ps --firstpage p1 -f <output file name.ps> 682166124Srafan NCURSES-ONE-BIG-FILE.html</font> 683166124Srafan</pre></td> 684166124Srafan</tr> 685166124Srafan</table> 686166124Srafan<p>See <a href="http://www.tldp.org/LDP/LDP-Author-Guide/" target= 687166124Srafan"_top">LDP Author guide</a> for more details. If all else failes, 688166124Srafanmail me at <a href="ppadala@gmail.com" target= 689166124Srafan"_top">ppadala@gmail.com</a></p> 690166124Srafan</div> 691166124Srafan</div> 692166124Srafan<div class="SECT2"> 693166124Srafan<hr> 694166124Srafan<h3 class="SECT2"><a name="CREDITS" id="CREDITS">1.7. 695166124SrafanCredits</a></h3> 696166124Srafan<p>I thank <a href="mailto:sharath_1@usa.net" target= 697166124Srafan"_top">Sharath</a> and Emre Akbas for helping me with few sections. 698166124SrafanThe introduction was initially written by sharath. I rewrote it 699166124Srafanwith few excerpts taken from his initial work. Emre helped in 700166124Srafanwriting printw and scanw sections.</p> 701166124Srafan<p>Perl equivalents of the example programs are contributed by 702166124Srafan<a href="mailto:Aratnaweera@virtusa.com" target="_top">Anuradha 703166124SrafanRatnaweera</a>.</p> 704166124Srafan<p>Then comes <a href="mailto:parimi@ece.arizona.edu" target= 705166124Srafan"_top">Ravi Parimi</a>, my dearest friend, who has been on this 706166124Srafanproject before even one line was written. He constantly bombarded 707166124Srafanme with suggestions and patiently reviewed the whole text. He also 708166124Srafanchecked each program on Linux and Solaris.</p> 709166124Srafan</div> 710166124Srafan<div class="SECT2"> 711166124Srafan<hr> 712166124Srafan<h3 class="SECT2"><a name="WISHLIST" id="WISHLIST">1.8. Wish 713166124SrafanList</a></h3> 714166124Srafan<p>This is the wish list, in the order of priority. If you have a 715166124Srafanwish or you want to work on completing the wish, mail <a href= 716166124Srafan"mailto:ppadala@gmail.com" target="_top">me</a>.</p> 717166124Srafan<ul> 718166124Srafan<li> 719166124Srafan<p>Add examples to last parts of forms section.</p> 720166124Srafan</li> 721166124Srafan<li> 722166124Srafan<p>Prepare a Demo showing all the programs and allow the user to 723166124Srafanbrowse through description of each program. Let the user compile 724166124Srafanand see the program in action. A dialog based interface is 725166124Srafanpreferred.</p> 726166124Srafan</li> 727166124Srafan<li> 728166124Srafan<p>Add debug info. _tracef, _tracemouse stuff.</p> 729166124Srafan</li> 730166124Srafan<li> 731166124Srafan<p>Accessing termcap, terminfo using functions provided by ncurses 732166124Srafanpackage.</p> 733166124Srafan</li> 734166124Srafan<li> 735166124Srafan<p>Working on two terminals simultaneously.</p> 736166124Srafan</li> 737166124Srafan<li> 738166124Srafan<p>Add more stuff to miscellaneous section.</p> 739166124Srafan</li> 740166124Srafan</ul> 741166124Srafan</div> 742166124Srafan<div class="SECT2"> 743166124Srafan<hr> 744166124Srafan<h3 class="SECT2"><a name="COPYRIGHT" id="COPYRIGHT">1.9. 745166124SrafanCopyright</a></h3> 746166124Srafan<p>Copyright © 2001 by Pradeep Padala.</p> 747166124Srafan<p>Permission is hereby granted, free of charge, to any person 748166124Srafanobtaining a copy of this software and associated documentation 749166124Srafanfiles (the "Software"), to deal in the Software without 750166124Srafanrestriction, including without limitation the rights to use, copy, 751166124Srafanmodify, merge, publish, distribute, distribute with modifications, 752166124Srafansublicense, and/or sell copies of the Software, and to permit 753166124Srafanpersons to whom the Software is furnished to do so, subject to the 754166124Srafanfollowing conditions:</p> 755166124Srafan<p>The above copyright notice and this permission notice shall be 756166124Srafanincluded in all copies or substantial portions of the Software.</p> 757166124Srafan<p>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 758166124SrafanEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 759166124SrafanMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 760166124SrafanNONINFRINGEMENT. IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE 761166124SrafanLIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 762166124SrafanACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 763166124SrafanCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 764166124SrafanSOFTWARE.</p> 765166124Srafan<p>Except as contained in this notice, the name(s) of the above 766166124Srafancopyright holders shall not be used in advertising or otherwise to 767166124Srafanpromote the sale, use or other dealings in this Software without 768166124Srafanprior written authorization.</p> 769166124Srafan</div> 770166124Srafan</div> 771166124Srafan<div class="SECT1"> 772166124Srafan<hr> 773166124Srafan<h2 class="SECT1"><a name="HELLOWORLD" id="HELLOWORLD">2. Hello 774166124SrafanWorld !!!</a></h2> 775166124Srafan<p>Welcome to the world of curses. Before we plunge into the 776166124Srafanlibrary and look into its various features, let's write a simple 777166124Srafanprogram and say hello to the world.</p> 778166124Srafan<div class="SECT2"> 779166124Srafan<hr> 780166124Srafan<h3 class="SECT2"><a name="COMPILECURSES" id="COMPILECURSES">2.1. 781166124SrafanCompiling With the NCURSES Library</a></h3> 782166124Srafan<p>To use ncurses library functions, you have to include ncurses.h 783166124Srafanin your programs. To link the program with ncurses the flag 784166124Srafan-lncurses should be added.</p> 785166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 786166124Srafan<tr> 787166124Srafan<td> 788166124Srafan<pre class="PROGRAMLISTING"> 789166124Srafan<font color="#000000"> #include <ncurses.h> 790166124Srafan . 791166124Srafan . 792166124Srafan . 793166124Srafan 794166124Srafan compile and link: gcc <program file> -lncurses</font> 795166124Srafan</pre></td> 796166124Srafan</tr> 797166124Srafan</table> 798166124Srafan<div class="EXAMPLE"><a name="BHW" id="BHW"></a> 799166124Srafan<p><b>Example 1. The Hello World !!! Program</b></p> 800166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 801166124Srafan<tr> 802166124Srafan<td> 803166124Srafan<pre class="PROGRAMLISTING"> 804166124Srafan<font color="#000000"><span class= 805166124Srafan"INLINEMEDIAOBJECT">#include <ncurses.h> 806166124Srafan 807166124Srafanint main() 808166124Srafan{ 809166124Srafan initscr(); /* Start curses mode */ 810166124Srafan printw("Hello World !!!"); /* Print Hello World */ 811166124Srafan refresh(); /* Print it on to the real screen */ 812166124Srafan getch(); /* Wait for user input */ 813166124Srafan endwin(); /* End curses mode */ 814166124Srafan 815166124Srafan return 0; 816166124Srafan}</span></font> 817166124Srafan</pre></td> 818166124Srafan</tr> 819166124Srafan</table> 820166124Srafan</div> 821166124Srafan</div> 822166124Srafan<div class="SECT2"> 823166124Srafan<hr> 824166124Srafan<h3 class="SECT2"><a name="DISSECTION" id="DISSECTION">2.2. 825166124SrafanDissection</a></h3> 826166124Srafan<p>The above program prints "Hello World !!!" to the screen and 827166124Srafanexits. This program shows how to initialize curses and do screen 828166124Srafanmanipulation and end curses mode. Let's dissect it line by 829166124Srafanline.</p> 830166124Srafan<div class="SECT3"> 831166124Srafan<hr> 832166124Srafan<h4 class="SECT3"><a name="ABOUT-INITSCR" id="ABOUT-INITSCR">2.2.1. 833166124SrafanAbout initscr()</a></h4> 834166124Srafan<p>The function initscr() initializes the terminal in curses mode. 835166124SrafanIn some implementations, it clears the screen and presents a blank 836166124Srafanscreen. To do any screen manipulation using curses package this has 837166124Srafanto be called first. This function initializes the curses system and 838166124Srafanallocates memory for our present window (called <var class= 839166124Srafan"LITERAL">stdscr</var>) and some other data-structures. Under 840166124Srafanextreme cases this function might fail due to insufficient memory 841166124Srafanto allocate memory for curses library's data structures.</p> 842166124Srafan<p>After this is done, we can do a variety of initializations to 843166124Srafancustomize our curses settings. These details will be explained 844166124Srafan<a href="#INIT">later</a> .</p> 845166124Srafan</div> 846166124Srafan<div class="SECT3"> 847166124Srafan<hr> 848166124Srafan<h4 class="SECT3"><a name="MYST-REFRESH" id="MYST-REFRESH">2.2.2. 849166124SrafanThe mysterious refresh()</a></h4> 850166124Srafan<p>The next line printw prints the string "Hello World !!!" on to 851166124Srafanthe screen. This function is analogous to normal printf in all 852166124Srafanrespects except that it prints the data on a window called stdscr 853166124Srafanat the current (y,x) co-ordinates. Since our present co-ordinates 854166124Srafanare at 0,0 the string is printed at the left hand corner of the 855166124Srafanwindow.</p> 856166124Srafan<p>This brings us to that mysterious refresh(). Well, when we 857166124Srafancalled printw the data is actually written to an imaginary window, 858166124Srafanwhich is not updated on the screen yet. The job of printw is to 859166124Srafanupdate a few flags and data structures and write the data to a 860166124Srafanbuffer corresponding to stdscr. In order to show it on the screen, 861166124Srafanwe need to call refresh() and tell the curses system to dump the 862166124Srafancontents on the screen.</p> 863166124Srafan<p>The philosophy behind all this is to allow the programmer to do 864166124Srafanmultiple updates on the imaginary screen or windows and do a 865166124Srafanrefresh once all his screen update is done. refresh() checks the 866166124Srafanwindow and updates only the portion which has been changed. This 867166124Srafanimproves performance and offers greater flexibility too. But, it is 868166124Srafansometimes frustrating to beginners. A common mistake committed by 869166124Srafanbeginners is to forget to call refresh() after they did some update 870166124Srafanthrough printw() class of functions. I still forget to add it 871166124Srafansometimes :-)</p> 872166124Srafan</div> 873166124Srafan<div class="SECT3"> 874166124Srafan<hr> 875166124Srafan<h4 class="SECT3"><a name="ABOUT-ENDWIN" id="ABOUT-ENDWIN">2.2.3. 876166124SrafanAbout endwin()</a></h4> 877166124Srafan<p>And finally don't forget to end the curses mode. Otherwise your 878166124Srafanterminal might behave strangely after the program quits. endwin() 879166124Srafanfrees the memory taken by curses sub-system and its data structures 880166124Srafanand puts the terminal in normal mode. This function must be called 881166124Srafanafter you are done with the curses mode.</p> 882166124Srafan</div> 883166124Srafan</div> 884166124Srafan</div> 885166124Srafan<div class="SECT1"> 886166124Srafan<hr> 887166124Srafan<h2 class="SECT1"><a name="GORY" id="GORY">3. The Gory 888166124SrafanDetails</a></h2> 889166124Srafan<p>Now that we have seen how to write a simple curses program let's 890166124Srafanget into the details. There are many functions that help customize 891166124Srafanwhat you see on screen and many features which can be put to full 892166124Srafanuse.</p> 893166124Srafan<p>Here we go...</p> 894166124Srafan</div> 895166124Srafan<div class="SECT1"> 896166124Srafan<hr> 897166124Srafan<h2 class="SECT1"><a name="INIT" id="INIT">4. 898166124SrafanInitialization</a></h2> 899166124Srafan<p>We now know that to initialize curses system the function 900166124Srafaninitscr() has to be called. There are functions which can be called 901166124Srafanafter this initialization to customize our curses session. We may 902166124Srafanask the curses system to set the terminal in raw mode or initialize 903166124Srafancolor or initialize the mouse etc.. Let's discuss some of the 904166124Srafanfunctions that are normally called immediately after initscr();</p> 905166124Srafan<div class="SECT2"> 906166124Srafan<hr> 907166124Srafan<h3 class="SECT2"><a name="ABOUTINIT" id="ABOUTINIT">4.1. 908166124SrafanInitialization functions</a></h3> 909166124Srafan</div> 910166124Srafan<div class="SECT2"> 911166124Srafan<hr> 912166124Srafan<h3 class="SECT2"><a name="RAWCBREAK" id="RAWCBREAK">4.2. raw() and 913166124Srafancbreak()</a></h3> 914166124Srafan<p>Normally the terminal driver buffers the characters a user types 915166124Srafanuntil a new line or carriage return is encountered. But most 916166124Srafanprograms require that the characters be available as soon as the 917166124Srafanuser types them. The above two functions are used to disable line 918166124Srafanbuffering. The difference between these two functions is in the way 919166124Srafancontrol characters like suspend (CTRL-Z), interrupt and quit 920166124Srafan(CTRL-C) are passed to the program. In the raw() mode these 921166124Srafancharacters are directly passed to the program without generating a 922166124Srafansignal. In the <var class="LITERAL">cbreak()</var> mode these 923166124Srafancontrol characters are interpreted as any other character by the 924166124Srafanterminal driver. I personally prefer to use raw() as I can exercise 925166124Srafangreater control over what the user does.</p> 926166124Srafan</div> 927166124Srafan<div class="SECT2"> 928166124Srafan<hr> 929166124Srafan<h3 class="SECT2"><a name="ECHONOECHO" id="ECHONOECHO">4.3. echo() 930166124Srafanand noecho()</a></h3> 931166124Srafan<p>These functions control the echoing of characters typed by the 932166124Srafanuser to the terminal. <var class="LITERAL">noecho()</var> switches 933166124Srafanoff echoing. The reason you might want to do this is to gain more 934166124Srafancontrol over echoing or to suppress unnecessary echoing while 935166124Srafantaking input from the user through the getch() etc. functions. Most 936166124Srafanof the interactive programs call <var class= 937166124Srafan"LITERAL">noecho()</var> at initialization and do the echoing of 938166124Srafancharacters in a controlled manner. It gives the programmer the 939166124Srafanflexibility of echoing characters at any place in the window 940166124Srafanwithout updating current (y,x) co-ordinates.</p> 941166124Srafan</div> 942166124Srafan<div class="SECT2"> 943166124Srafan<hr> 944166124Srafan<h3 class="SECT2"><a name="KEYPAD" id="KEYPAD">4.4. 945166124Srafankeypad()</a></h3> 946166124Srafan<p>This is my favorite initialization function. It enables the 947166124Srafanreading of function keys like F1, F2, arrow keys etc. Almost every 948166124Srafaninteractive program enables this, as arrow keys are a major part of 949166124Srafanany User Interface. Do <var class="LITERAL">keypad(stdscr, 950166124SrafanTRUE)</var> to enable this feature for the regular screen (stdscr). 951166124SrafanYou will learn more about key management in later sections of this 952166124Srafandocument.</p> 953166124Srafan</div> 954166124Srafan<div class="SECT2"> 955166124Srafan<hr> 956166124Srafan<h3 class="SECT2"><a name="HALFDELAY" id="HALFDELAY">4.5. 957166124Srafanhalfdelay()</a></h3> 958166124Srafan<p>This function, though not used very often, is a useful one at 959166124Srafantimes. halfdelay()is called to enable the half-delay mode, which is 960166124Srafansimilar to the cbreak() mode in that characters typed are 961166124Srafanimmediately available to program. However, it waits for 'X' tenths 962166124Srafanof a second for input and then returns ERR, if no input is 963166124Srafanavailable. 'X' is the timeout value passed to the function 964166124Srafanhalfdelay(). This function is useful when you want to ask the user 965166124Srafanfor input, and if he doesn't respond with in certain time, we can 966166124Srafando some thing else. One possible example is a timeout at the 967166124Srafanpassword prompt.</p> 968166124Srafan</div> 969166124Srafan<div class="SECT2"> 970166124Srafan<hr> 971166124Srafan<h3 class="SECT2"><a name="MISCINIT" id="MISCINIT">4.6. 972166124SrafanMiscellaneous Initialization functions</a></h3> 973166124Srafan<p>There are few more functions which are called at initialization 974166124Srafanto customize curses behavior. They are not used as extensively as 975166124Srafanthose mentioned above. Some of them are explained where 976166124Srafanappropriate.</p> 977166124Srafan</div> 978166124Srafan<div class="SECT2"> 979166124Srafan<hr> 980166124Srafan<h3 class="SECT2"><a name="INITEX" id="INITEX">4.7. An 981166124SrafanExample</a></h3> 982166124Srafan<p>Let's write a program which will clarify the usage of these 983166124Srafanfunctions.</p> 984166124Srafan<div class="EXAMPLE"><a name="BINFU" id="BINFU"></a> 985166124Srafan<p><b>Example 2. Initialization Function Usage example</b></p> 986166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 987166124Srafan<tr> 988166124Srafan<td> 989166124Srafan<pre class="PROGRAMLISTING"> 990166124Srafan<font color="#000000"><span class= 991166124Srafan"INLINEMEDIAOBJECT">#include <ncurses.h> 992166124Srafan 993166124Srafanint main() 994166124Srafan{ int ch; 995166124Srafan 996166124Srafan initscr(); /* Start curses mode */ 997166124Srafan raw(); /* Line buffering disabled */ 998166124Srafan keypad(stdscr, TRUE); /* We get F1, F2 etc.. */ 999166124Srafan noecho(); /* Don't echo() while we do getch */ 1000166124Srafan 1001166124Srafan printw("Type any character to see it in bold\n"); 1002166124Srafan ch = getch(); /* If raw() hadn't been called 1003166124Srafan * we have to press enter before it 1004166124Srafan * gets to the program */ 1005166124Srafan if(ch == KEY_F(1)) /* Without keypad enabled this will */ 1006166124Srafan printw("F1 Key pressed");/* not get to us either */ 1007166124Srafan /* Without noecho() some ugly escape 1008166124Srafan * charachters might have been printed 1009166124Srafan * on screen */ 1010166124Srafan else 1011166124Srafan { printw("The pressed key is "); 1012166124Srafan attron(A_BOLD); 1013166124Srafan printw("%c", ch); 1014166124Srafan attroff(A_BOLD); 1015166124Srafan } 1016166124Srafan refresh(); /* Print it on to the real screen */ 1017166124Srafan getch(); /* Wait for user input */ 1018166124Srafan endwin(); /* End curses mode */ 1019166124Srafan 1020166124Srafan return 0; 1021166124Srafan}</span></font> 1022166124Srafan</pre></td> 1023166124Srafan</tr> 1024166124Srafan</table> 1025166124Srafan</div> 1026166124Srafan<p>This program is self-explanatory. But I used functions which 1027166124Srafanaren't explained yet. The function <var class= 1028166124Srafan"LITERAL">getch()</var> is used to get a character from user. It is 1029166124Srafanequivalent to normal <var class="LITERAL">getchar()</var> except 1030166124Srafanthat we can disable the line buffering to avoid <enter> after 1031166124Srafaninput. Look for more about <var class="LITERAL">getch()</var>and 1032166124Srafanreading keys in the <a href="#KEYS">key management section</a> . 1033166124SrafanThe functions attron and attroff are used to switch some attributes 1034166124Srafanon and off respectively. In the example I used them to print the 1035166124Srafancharacter in bold. These functions are explained in detail 1036166124Srafanlater.</p> 1037166124Srafan</div> 1038166124Srafan</div> 1039166124Srafan<div class="SECT1"> 1040166124Srafan<hr> 1041166124Srafan<h2 class="SECT1"><a name="AWORDWINDOWS" id="AWORDWINDOWS">5. A 1042166124SrafanWord about Windows</a></h2> 1043166124Srafan<p>Before we plunge into the myriad ncurses functions, let me clear 1044166124Srafanfew things about windows. Windows are explained in detail in 1045166124Srafanfollowing <a href="#WINDOWS">sections</a></p> 1046166124Srafan<p>A Window is an imaginary screen defined by curses system. A 1047166124Srafanwindow does not mean a bordered window which you usually see on 1048166124SrafanWin9X platforms. When curses is initialized, it creates a default 1049166124Srafanwindow named <var class="LITERAL">stdscr</var> which represents 1050166124Srafanyour 80x25 (or the size of window in which you are running) screen. 1051166124SrafanIf you are doing simple tasks like printing few strings, reading 1052166124Srafaninput etc., you can safely use this single window for all of your 1053166124Srafanpurposes. You can also create windows and call functions which 1054166124Srafanexplicitly work on the specified window.</p> 1055166124Srafan<p>For example, if you call</p> 1056166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 1057166124Srafan<tr> 1058166124Srafan<td> 1059166124Srafan<pre class="PROGRAMLISTING"> 1060166124Srafan<font color="#000000"> printw("Hi There !!!"); 1061166124Srafan refresh();</font> 1062166124Srafan</pre></td> 1063166124Srafan</tr> 1064166124Srafan</table> 1065166124Srafan<p>It prints the string on stdscr at the present cursor position. 1066166124SrafanSimilarly the call to refresh(), works on stdscr only.</p> 1067166124Srafan<p>Say you have created <a href="#WINDOWS">windows</a> then you 1068166124Srafanhave to call a function with a 'w' added to the usual function.</p> 1069166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 1070166124Srafan<tr> 1071166124Srafan<td> 1072166124Srafan<pre class="PROGRAMLISTING"> 1073166124Srafan<font color="#000000"> wprintw(win, "Hi There !!!"); 1074166124Srafan wrefresh(win);</font> 1075166124Srafan</pre></td> 1076166124Srafan</tr> 1077166124Srafan</table> 1078166124Srafan<p>As you will see in the rest of the document, naming of functions 1079166124Srafanfollow the same convention. For each function there usually are 1080166124Srafanthree more functions.</p> 1081166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 1082166124Srafan<tr> 1083166124Srafan<td> 1084166124Srafan<pre class="PROGRAMLISTING"> 1085166124Srafan<font color= 1086166124Srafan"#000000"> printw(string); /* Print on stdscr at present cursor position */ 1087166124Srafan mvprintw(y, x, string);/* Move to (y, x) then print string */ 1088166124Srafan wprintw(win, string); /* Print on window win at present cursor position */ 1089166124Srafan /* in the window */ 1090166124Srafan mvwprintw(win, y, x, string); /* Move to (y, x) relative to window */ 1091166124Srafan /* co-ordinates and then print */</font> 1092166124Srafan</pre></td> 1093166124Srafan</tr> 1094166124Srafan</table> 1095166124Srafan<p>Usually the w-less functions are macros which expand to 1096166124Srafancorresponding w-function with stdscr as the window parameter.</p> 1097166124Srafan</div> 1098166124Srafan<div class="SECT1"> 1099166124Srafan<hr> 1100166124Srafan<h2 class="SECT1"><a name="PRINTW" id="PRINTW">6. Output 1101166124Srafanfunctions</a></h2> 1102166124Srafan<p>I guess you can't wait any more to see some action. Back to our 1103166124Srafanodyssey of curses functions. Now that curses is initialized, let's 1104166124Srafaninteract with world.</p> 1105166124Srafan<p>There are three classes of functions which you can use to do 1106166124Srafanoutput on screen.</p> 1107166124Srafan<ol type="1"> 1108166124Srafan<li> 1109166124Srafan<p>addch() class: Print single character with attributes</p> 1110166124Srafan</li> 1111166124Srafan<li> 1112166124Srafan<p>printw() class: Print formatted output similar to printf()</p> 1113166124Srafan</li> 1114166124Srafan<li> 1115166124Srafan<p>addstr() class: Print strings</p> 1116166124Srafan</li> 1117166124Srafan</ol> 1118166124Srafan<p>These functions can be used interchangeably and it's a matter of 1119166124Srafanstyle as to which class is used. Let's see each one in detail.</p> 1120166124Srafan<div class="SECT2"> 1121166124Srafan<hr> 1122166124Srafan<h3 class="SECT2"><a name="ADDCHCLASS" id="ADDCHCLASS">6.1. addch() 1123166124Srafanclass of functions</a></h3> 1124166124Srafan<p>These functions put a single character into the current cursor 1125166124Srafanlocation and advance the position of the cursor. You can give the 1126166124Srafancharacter to be printed but they usually are used to print a 1127166124Srafancharacter with some attributes. Attributes are explained in detail 1128166124Srafanin later <a href="#ATTRIB">sections</a> of the document. If a 1129166124Srafancharacter is associated with an attribute(bold, reverse video 1130166124Srafanetc.), when curses prints the character, it is printed in that 1131166124Srafanattribute.</p> 1132166124Srafan<p>In order to combine a character with some attributes, you have 1133166124Srafantwo options:</p> 1134166124Srafan<ul> 1135166124Srafan<li> 1136166124Srafan<p>By OR'ing a single character with the desired attribute macros. 1137166124SrafanThese attribute macros could be found in the header file 1138166124Srafan<var class="LITERAL">ncurses.h</var>. For example, you want to 1139166124Srafanprint a character ch(of type char) bold and underlined, you would 1140166124Srafancall addch() as below.</p> 1141166124Srafan<table border="0" bgcolor="#E0E0E0" width="90%"> 1142166124Srafan<tr> 1143166124Srafan<td> 1144166124Srafan<pre class="PROGRAMLISTING"> 1145166124Srafan<font color="#000000"> addch(ch | A_BOLD | A_UNDERLINE);</font> 1146166124Srafan</pre></td> 1147166124Srafan</tr> 1148166124Srafan</table> 1149166124Srafan</li> 1150166124Srafan<li> 1151166124Srafan<p>By using functions like <var class= 1152166124Srafan"LITERAL">attrset(),attron(),attroff()</var>. These functions are 1153166124Srafanexplained in the <a href="#ATTRIB">Attributes</a> section. Briefly, 1154166124Srafanthey manipulate the current attributes of the given window. Once 1155166124Srafanset, the character printed in the window are associated with the 1156166124Srafanattributes until it is turned off.</p> 1157166124Srafan</li> 1158166124Srafan</ul> 1159166124Srafan<p>Additionally, <var class="LITERAL">curses</var> provides some 1160166124Srafanspecial characters for character-based graphics. You can draw 1161166124Srafantables, horizontal or vertical lines, etc. You can find all 1162166124Srafanavaliable characters in the header file <var class= 1163166124Srafan"LITERAL">ncurses.h</var>. Try looking for macros beginning with 1164166124Srafan<var class="LITERAL">ACS_</var> in this file.</p> 1165166124Srafan</div> 1166166124Srafan<div class="SECT2"> 1167166124Srafan<hr> 1168166124Srafan<h3 class="SECT2"><a name="AEN298" id="AEN298">6.2. mvaddch(), 1169166124Srafanwaddch() and mvwaddch()</a></h3> 1170166124Srafan<p><var class="LITERAL">mvaddch()</var> is used to move the cursor 1171166124Srafanto a given point, and then print. Thus, the calls:</p> 1172166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 1173166124Srafan<tr> 1174166124Srafan<td> 1175166124Srafan<pre class="PROGRAMLISTING"> 1176166124Srafan<font color= 1177166124Srafan"#000000"> move(row,col); /* moves the cursor to row<em>th</em> row and col<em>th</em> column */ 1178166124Srafan addch(ch);</font> 1179166124Srafan</pre></td> 1180166124Srafan</tr> 1181166124Srafan</table> 1182166124Srafancan be replaced by 1183166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 1184166124Srafan<tr> 1185166124Srafan<td> 1186166124Srafan<pre class="PROGRAMLISTING"> 1187166124Srafan<font color="#000000"> mvaddch(row,col,ch);</font> 1188166124Srafan</pre></td> 1189166124Srafan</tr> 1190166124Srafan</table> 1191166124Srafan<p><var class="LITERAL">waddch()</var> is similar to <var class= 1192166124Srafan"LITERAL">addch()</var>, except that it adds a character into the 1193166124Srafangiven window. (Note that <var class="LITERAL">addch()</var> adds a 1194166124Srafancharacter into the window <var class="LITERAL">stdscr</var>.)</p> 1195166124Srafan<p>In a similar fashion <var class="LITERAL">mvwaddch()</var> 1196166124Srafanfunction is used to add a character into the given window at the 1197166124Srafangiven coordinates.</p> 1198166124Srafan<p>Now, we are familiar with the basic output function <var class= 1199166124Srafan"LITERAL">addch()</var>. But, if we want to print a string, it 1200166124Srafanwould be very annoying to print it character by character. 1201166124SrafanFortunately, <var class="LITERAL">ncurses</var> provides 1202166124Srafan<var class="LITERAL">printf</var><em>-like</em> or <var class= 1203166124Srafan"LITERAL">puts</var><em>-like</em> functions.</p> 1204166124Srafan</div> 1205166124Srafan<div class="SECT2"> 1206166124Srafan<hr> 1207166124Srafan<h3 class="SECT2"><a name="PRINTWCLASS" id="PRINTWCLASS">6.3. 1208166124Srafanprintw() class of functions</a></h3> 1209166124Srafan<p>These functions are similar to <var class= 1210166124Srafan"LITERAL">printf()</var> with the added capability of printing at 1211166124Srafanany position on the screen.</p> 1212166124Srafan<div class="SECT3"> 1213166124Srafan<hr> 1214166124Srafan<h4 class="SECT3"><a name="PRINTWMVPRINTW" id= 1215166124Srafan"PRINTWMVPRINTW">6.3.1. printw() and mvprintw</a></h4> 1216166124Srafan<p>These two functions work much like <var class= 1217166124Srafan"LITERAL">printf()</var>. <var class="LITERAL">mvprintw()</var> can 1218166124Srafanbe used to move the cursor to a position and then print. If you 1219166124Srafanwant to move the cursor first and then print using <var class= 1220166124Srafan"LITERAL">printw()</var> function, use <var class= 1221166124Srafan"LITERAL">move()</var> first and then use <var class= 1222166124Srafan"LITERAL">printw()</var> though I see no point why one should avoid 1223166124Srafanusing <var class="LITERAL">mvprintw()</var>, you have the 1224166124Srafanflexibility to manipulate.</p> 1225166124Srafan</div> 1226166124Srafan<div class="SECT3"> 1227166124Srafan<hr> 1228166124Srafan<h4 class="SECT3"><a name="WPRINTWMVWPRINTW" id= 1229166124Srafan"WPRINTWMVWPRINTW">6.3.2. wprintw() and mvwprintw</a></h4> 1230166124Srafan<p>These two functions are similar to above two except that they 1231166124Srafanprint in the corresponding window given as argument.</p> 1232166124Srafan</div> 1233166124Srafan<div class="SECT3"> 1234166124Srafan<hr> 1235166124Srafan<h4 class="SECT3"><a name="VWPRINTW" id="VWPRINTW">6.3.3. 1236166124Srafanvwprintw()</a></h4> 1237166124Srafan<p>This function is similar to <var class= 1238166124Srafan"LITERAL">vprintf()</var>. This can be used when variable number of 1239166124Srafanarguments are to be printed.</p> 1240166124Srafan</div> 1241166124Srafan<div class="SECT3"> 1242166124Srafan<hr> 1243166124Srafan<h4 class="SECT3"><a name="SIMPLEPRINTWEX" id= 1244166124Srafan"SIMPLEPRINTWEX">6.3.4. A Simple printw example</a></h4> 1245166124Srafan<div class="EXAMPLE"><a name="BPREX" id="BPREX"></a> 1246166124Srafan<p><b>Example 3. A Simple printw example</b></p> 1247166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 1248166124Srafan<tr> 1249166124Srafan<td> 1250166124Srafan<pre class="PROGRAMLISTING"> 1251166124Srafan<font color="#000000"><span class= 1252166124Srafan"INLINEMEDIAOBJECT">#include <ncurses.h> /* ncurses.h includes stdio.h */ 1253166124Srafan#include <string.h> 1254166124Srafan 1255166124Srafanint main() 1256166124Srafan{ 1257166124Srafan char mesg[]="Just a string"; /* message to be appeared on the screen */ 1258166124Srafan int row,col; /* to store the number of rows and * 1259166124Srafan * the number of colums of the screen */ 1260166124Srafan initscr(); /* start the curses mode */ 1261166124Srafan getmaxyx(stdscr,row,col); /* get the number of rows and columns */ 1262166124Srafan mvprintw(row/2,(col-strlen(mesg))/2,"%s",mesg); 1263166124Srafan /* print the message at the center of the screen */ 1264166124Srafan mvprintw(row-2,0,"This screen has %d rows and %d columns\n",row,col); 1265166124Srafan printw("Try resizing your window(if possible) and then run this program again"); 1266166124Srafan refresh(); 1267166124Srafan getch(); 1268166124Srafan endwin(); 1269166124Srafan 1270166124Srafan return 0; 1271166124Srafan}</span></font> 1272166124Srafan</pre></td> 1273166124Srafan</tr> 1274166124Srafan</table> 1275166124Srafan</div> 1276166124Srafan<p>Above program demonstrates how easy it is to use <var class= 1277166124Srafan"LITERAL">printw</var>. You just feed the coordinates and the 1278166124Srafanmessage to be appeared on the screen, then it does what you 1279166124Srafanwant.</p> 1280166124Srafan<p>The above program introduces us to a new function <var class= 1281166124Srafan"LITERAL">getmaxyx()</var>, a macro defined in <var class= 1282166124Srafan"LITERAL">ncurses.h</var>. It gives the number of columns and the 1283166124Srafannumber of rows in a given window. <var class= 1284166124Srafan"LITERAL">getmaxyx()</var> does this by updating the variables 1285166124Srafangiven to it. Since <var class="LITERAL">getmaxyx()</var> is not a 1286166124Srafanfunction we don't pass pointers to it, we just give two integer 1287166124Srafanvariables.</p> 1288166124Srafan</div> 1289166124Srafan</div> 1290166124Srafan<div class="SECT2"> 1291166124Srafan<hr> 1292166124Srafan<h3 class="SECT2"><a name="ADDSTRCLASS" id="ADDSTRCLASS">6.4. 1293166124Srafanaddstr() class of functions</a></h3> 1294166124Srafan<p><var class="LITERAL">addstr()</var> is used to put a character 1295166124Srafanstring into a given window. This function is similar to calling 1296166124Srafan<var class="LITERAL">addch()</var> once for each character in a 1297166124Srafangiven string. This is true for all output functions. There are 1298166124Srafanother functions from this family such as <var class= 1299166124Srafan"LITERAL">mvaddstr(),mvwaddstr()</var> and <var class= 1300166124Srafan"LITERAL">waddstr()</var>, which obey the naming convention of 1301166124Srafancurses.(e.g. mvaddstr() is similar to the respective calls move() 1302166124Srafanand then addstr().) Another function of this family is addnstr(), 1303166124Srafanwhich takes an integer parameter(say n) additionally. This function 1304166124Srafanputs at most n characters into the screen. If n is negative, then 1305166124Srafanthe entire string will be added.</p> 1306166124Srafan</div> 1307166124Srafan<div class="SECT2"> 1308166124Srafan<hr> 1309166124Srafan<h3 class="SECT2"><a name="ACAUTION" id="ACAUTION">6.5. A word of 1310166124Srafancaution</a></h3> 1311166124Srafan<p>All these functions take y co-ordinate first and then x in their 1312166124Srafanarguments. A common mistake by beginners is to pass x,y in that 1313166124Srafanorder. If you are doing too many manipulations of (y,x) 1314166124Srafanco-ordinates, think of dividing the screen into windows and 1315166124Srafanmanipulate each one separately. Windows are explained in the 1316166124Srafan<a href="#WINDOWS">windows</a> section.</p> 1317166124Srafan</div> 1318166124Srafan</div> 1319166124Srafan<div class="SECT1"> 1320166124Srafan<hr> 1321166124Srafan<h2 class="SECT1"><a name="SCANW" id="SCANW">7. Input 1322166124Srafanfunctions</a></h2> 1323166124Srafan<p>Well, printing without taking input, is boring. Let's see 1324166124Srafanfunctions which allow us to get input from user. These functions 1325166124Srafanalso can be divided into three categories.</p> 1326166124Srafan<ol type="1"> 1327166124Srafan<li> 1328166124Srafan<p>getch() class: Get a character</p> 1329166124Srafan</li> 1330166124Srafan<li> 1331166124Srafan<p>scanw() class: Get formatted input</p> 1332166124Srafan</li> 1333166124Srafan<li> 1334166124Srafan<p>getstr() class: Get strings</p> 1335166124Srafan</li> 1336166124Srafan</ol> 1337166124Srafan<div class="SECT2"> 1338166124Srafan<hr> 1339166124Srafan<h3 class="SECT2"><a name="GETCHCLASS" id="GETCHCLASS">7.1. getch() 1340166124Srafanclass of functions</a></h3> 1341166124Srafan<p>These functions read a single character from the terminal. But 1342166124Srafanthere are several subtle facts to consider. For example if you 1343166124Srafandon't use the function cbreak(), curses will not read your input 1344166124Srafancharacters contiguously but will begin read them only after a new 1345166124Srafanline or an EOF is encountered. In order to avoid this, the cbreak() 1346166124Srafanfunction must used so that characters are immediately available to 1347166124Srafanyour program. Another widely used function is noecho(). As the name 1348166124Srafansuggests, when this function is set (used), the characters that are 1349166124Srafankeyed in by the user will not show up on the screen. The two 1350166124Srafanfunctions cbreak() and noecho() are typical examples of key 1351166124Srafanmanagement. Functions of this genre are explained in the <a href= 1352166124Srafan"#KEYS">key management section</a> .</p> 1353166124Srafan</div> 1354166124Srafan<div class="SECT2"> 1355166124Srafan<hr> 1356166124Srafan<h3 class="SECT2"><a name="SCANWCLASS" id="SCANWCLASS">7.2. scanw() 1357166124Srafanclass of functions</a></h3> 1358166124Srafan<p>These functions are similar to <var class= 1359166124Srafan"LITERAL">scanf()</var> with the added capability of getting the 1360166124Srafaninput from any location on the screen.</p> 1361166124Srafan<div class="SECT3"> 1362166124Srafan<hr> 1363166124Srafan<h4 class="SECT3"><a name="SCANWMVSCANW" id="SCANWMVSCANW">7.2.1. 1364166124Srafanscanw() and mvscanw</a></h4> 1365166124Srafan<p>The usage of these functions is similar to that of <var class= 1366166124Srafan"LITERAL">sscanf()</var>, where the line to be scanned is provided 1367166124Srafanby <var class="LITERAL">wgetstr()</var> function. That is, these 1368166124Srafanfunctions call to <var class="LITERAL">wgetstr()</var> 1369166124Srafanfunction(explained below) and uses the resulting line for a 1370166124Srafanscan.</p> 1371166124Srafan</div> 1372166124Srafan<div class="SECT3"> 1373166124Srafan<hr> 1374166124Srafan<h4 class="SECT3"><a name="WSCANWMVWSCANW" id= 1375166124Srafan"WSCANWMVWSCANW">7.2.2. wscanw() and mvwscanw()</a></h4> 1376166124Srafan<p>These are similar to above two functions except that they read 1377166124Srafanfrom a window, which is supplied as one of the arguments to these 1378166124Srafanfunctions.</p> 1379166124Srafan</div> 1380166124Srafan<div class="SECT3"> 1381166124Srafan<hr> 1382166124Srafan<h4 class="SECT3"><a name="VWSCANW" id="VWSCANW">7.2.3. 1383166124Srafanvwscanw()</a></h4> 1384166124Srafan<p>This function is similar to <var class="LITERAL">vscanf()</var>. 1385166124SrafanThis can be used when a variable number of arguments are to be 1386166124Srafanscanned.</p> 1387166124Srafan</div> 1388166124Srafan</div> 1389166124Srafan<div class="SECT2"> 1390166124Srafan<hr> 1391166124Srafan<h3 class="SECT2"><a name="GETSTRCLASS" id="GETSTRCLASS">7.3. 1392166124Srafangetstr() class of functions</a></h3> 1393166124Srafan<p>These functions are used to get strings from the terminal. In 1394166124Srafanessence, this function performs the same task as would be achieved 1395166124Srafanby a series of calls to <var class="LITERAL">getch()</var> until a 1396166124Srafannewline, carriage return, or end-of-file is received. The resulting 1397166124Srafanstring of characters are pointed to by <var class= 1398166124Srafan"LITERAL">str</var>, which is a character pointer provided by the 1399166124Srafanuser.</p> 1400166124Srafan</div> 1401166124Srafan<div class="SECT2"> 1402166124Srafan<hr> 1403166124Srafan<h3 class="SECT2"><a name="GETSTREX" id="GETSTREX">7.4. Some 1404166124Srafanexamples</a></h3> 1405166124Srafan<div class="EXAMPLE"><a name="BSCEX" id="BSCEX"></a> 1406166124Srafan<p><b>Example 4. A Simple scanw example</b></p> 1407166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 1408166124Srafan<tr> 1409166124Srafan<td> 1410166124Srafan<pre class="PROGRAMLISTING"> 1411166124Srafan<font color="#000000"><span class= 1412166124Srafan"INLINEMEDIAOBJECT">#include <ncurses.h> /* ncurses.h includes stdio.h */ 1413166124Srafan#include <string.h> 1414166124Srafan 1415166124Srafanint main() 1416166124Srafan{ 1417166124Srafan char mesg[]="Enter a string: "; /* message to be appeared on the screen */ 1418166124Srafan char str[80]; 1419166124Srafan int row,col; /* to store the number of rows and * 1420166124Srafan * the number of colums of the screen */ 1421166124Srafan initscr(); /* start the curses mode */ 1422166124Srafan getmaxyx(stdscr,row,col); /* get the number of rows and columns */ 1423166124Srafan mvprintw(row/2,(col-strlen(mesg))/2,"%s",mesg); 1424166124Srafan /* print the message at the center of the screen */ 1425166124Srafan getstr(str); 1426166124Srafan mvprintw(LINES - 2, 0, "You Entered: %s", str); 1427166124Srafan getch(); 1428166124Srafan endwin(); 1429166124Srafan 1430166124Srafan return 0; 1431166124Srafan}</span></font> 1432166124Srafan</pre></td> 1433166124Srafan</tr> 1434166124Srafan</table> 1435166124Srafan</div> 1436166124Srafan</div> 1437166124Srafan</div> 1438166124Srafan<div class="SECT1"> 1439166124Srafan<hr> 1440166124Srafan<h2 class="SECT1"><a name="ATTRIB" id="ATTRIB">8. 1441166124SrafanAttributes</a></h2> 1442166124Srafan<p>We have seen an example of how attributes can be used to print 1443166124Srafancharacters with some special effects. Attributes, when set 1444166124Srafanprudently, can present information in an easy, understandable 1445166124Srafanmanner. The following program takes a C file as input and prints 1446166124Srafanthe file with comments in bold. Scan through the code.</p> 1447166124Srafan<div class="EXAMPLE"><a name="BSIAT" id="BSIAT"></a> 1448166124Srafan<p><b>Example 5. A Simple Attributes example</b></p> 1449166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 1450166124Srafan<tr> 1451166124Srafan<td> 1452166124Srafan<pre class="PROGRAMLISTING"> 1453166124Srafan<font color="#000000"><span class= 1454166124Srafan"INLINEMEDIAOBJECT">/* pager functionality by Joseph Spainhour" <spainhou@bellsouth.net> */ 1455166124Srafan#include <ncurses.h> 1456166124Srafan#include <stdlib.h> 1457166124Srafan 1458166124Srafanint main(int argc, char *argv[]) 1459166124Srafan{ 1460166124Srafan int ch, prev, row, col; 1461166124Srafan prev = EOF; 1462166124Srafan FILE *fp; 1463166124Srafan int y, x; 1464166124Srafan 1465166124Srafan if(argc != 2) 1466166124Srafan { 1467166124Srafan printf("Usage: %s <a c file name>\n", argv[0]); 1468166124Srafan exit(1); 1469166124Srafan } 1470166124Srafan fp = fopen(argv[1], "r"); 1471166124Srafan if(fp == NULL) 1472166124Srafan { 1473166124Srafan perror("Cannot open input file"); 1474166124Srafan exit(1); 1475166124Srafan } 1476166124Srafan initscr(); /* Start curses mode */ 1477166124Srafan getmaxyx(stdscr, row, col); /* find the boundaries of the screeen */ 1478166124Srafan while((ch = fgetc(fp)) != EOF) /* read the file till we reach the end */ 1479166124Srafan { 1480166124Srafan getyx(stdscr, y, x); /* get the current curser position */ 1481166124Srafan if(y == (row - 1)) /* are we are at the end of the screen */ 1482166124Srafan { 1483166124Srafan printw("<-Press Any Key->"); /* tell the user to press a key */ 1484166124Srafan getch(); 1485166124Srafan clear(); /* clear the screen */ 1486166124Srafan move(0, 0); /* start at the beginning of the screen */ 1487166124Srafan } 1488166124Srafan if(prev == '/' && ch == '*') /* If it is / and * then only 1489166124Srafan * switch bold on */ 1490166124Srafan { 1491166124Srafan attron(A_BOLD); /* cut bold on */ 1492166124Srafan getyx(stdscr, y, x); /* get the current curser position */ 1493166124Srafan move(y, x - 1); /* back up one space */ 1494166124Srafan printw("%c%c", '/', ch); /* The actual printing is done here */ 1495166124Srafan } 1496166124Srafan else 1497166124Srafan printw("%c", ch); 1498166124Srafan refresh(); 1499166124Srafan if(prev == '*' && ch == '/') 1500166124Srafan attroff(A_BOLD); /* Switch it off once we got * 1501166124Srafan * and then / */ 1502166124Srafan prev = ch; 1503166124Srafan } 1504166124Srafan endwin(); /* End curses mode */ 1505166124Srafan fclose(fp); 1506166124Srafan return 0; 1507166124Srafan}</span></font> 1508166124Srafan</pre></td> 1509166124Srafan</tr> 1510166124Srafan</table> 1511166124Srafan</div> 1512166124Srafan<p>Don't worry about all those initialization and other crap. 1513166124SrafanConcentrate on the while loop. It reads each character in the file 1514166124Srafanand searches for the pattern /*. Once it spots the pattern, it 1515166124Srafanswitches the BOLD attribute on with <var class= 1516166124Srafan"LITERAL">attron()</var> . When we get the pattern */ it is 1517166124Srafanswitched off by <var class="LITERAL">attroff()</var> .</p> 1518166124Srafan<p>The above program also introduces us to two useful functions 1519166124Srafan<var class="LITERAL">getyx()</var> and <var class= 1520166124Srafan"LITERAL">move()</var>. The first function gets the co-ordinates of 1521166124Srafanthe present cursor into the variables y, x. Since getyx() is a 1522166124Srafanmacro we don't have to pass pointers to variables. The function 1523166124Srafan<var class="LITERAL">move()</var> moves the cursor to the 1524166124Srafanco-ordinates given to it.</p> 1525166124Srafan<p>The above program is really a simple one which doesn't do much. 1526166124SrafanOn these lines one could write a more useful program which reads a 1527166124SrafanC file, parses it and prints it in different colors. One could even 1528166124Srafanextend it to other languages as well.</p> 1529166124Srafan<div class="SECT2"> 1530166124Srafan<hr> 1531166124Srafan<h3 class="SECT2"><a name="ATTRIBDETAILS" id="ATTRIBDETAILS">8.1. 1532166124SrafanThe details</a></h3> 1533166124Srafan<p>Let's get into more details of attributes. The functions 1534166124Srafan<var class="LITERAL">attron(), attroff(), attrset()</var> , and 1535166124Srafantheir sister functions <var class="LITERAL">attr_get()</var> etc.. 1536166124Srafancan be used to switch attributes on/off , get attributes and 1537166124Srafanproduce a colorful display.</p> 1538166124Srafan<p>The functions attron and attroff take a bit-mask of attributes 1539166124Srafanand switch them on or off, respectively. The following video 1540166124Srafanattributes, which are defined in <curses.h> can be passed to 1541166124Srafanthese functions.</p> 1542166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 1543166124Srafan<tr> 1544166124Srafan<td> 1545166124Srafan<pre class="PROGRAMLISTING"> 1546166124Srafan<font color="#000000"> 1547166124Srafan A_NORMAL Normal display (no highlight) 1548166124Srafan A_STANDOUT Best highlighting mode of the terminal. 1549166124Srafan A_UNDERLINE Underlining 1550166124Srafan A_REVERSE Reverse video 1551166124Srafan A_BLINK Blinking 1552166124Srafan A_DIM Half bright 1553166124Srafan A_BOLD Extra bright or bold 1554166124Srafan A_PROTECT Protected mode 1555166124Srafan A_INVIS Invisible or blank mode 1556166124Srafan A_ALTCHARSET Alternate character set 1557166124Srafan A_CHARTEXT Bit-mask to extract a character 1558166124Srafan COLOR_PAIR(n) Color-pair number n 1559166124Srafan </font> 1560166124Srafan</pre></td> 1561166124Srafan</tr> 1562166124Srafan</table> 1563166124Srafan<p>The last one is the most colorful one :-) Colors are explained 1564166124Srafanin the <a href="#color" target="_top">next sections</a>.</p> 1565166124Srafan<p>We can OR(|) any number of above attributes to get a combined 1566166124Srafaneffect. If you wanted reverse video with blinking characters you 1567166124Srafancan use</p> 1568166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 1569166124Srafan<tr> 1570166124Srafan<td> 1571166124Srafan<pre class="PROGRAMLISTING"> 1572166124Srafan<font color="#000000"> attron(A_REVERSE | A_BLINK);</font> 1573166124Srafan</pre></td> 1574166124Srafan</tr> 1575166124Srafan</table> 1576166124Srafan</div> 1577166124Srafan<div class="SECT2"> 1578166124Srafan<hr> 1579166124Srafan<h3 class="SECT2"><a name="ATTRONVSATTRSET" id= 1580166124Srafan"ATTRONVSATTRSET">8.2. attron() vs attrset()</a></h3> 1581166124Srafan<p>Then what is the difference between attron() and attrset()? 1582166124Srafanattrset sets the attributes of window whereas attron just switches 1583166124Srafanon the attribute given to it. So attrset() fully overrides whatever 1584166124Srafanattributes the window previously had and sets it to the new 1585166124Srafanattribute(s). Similarly attroff() just switches off the 1586166124Srafanattribute(s) given to it as an argument. This gives us the 1587166124Srafanflexibility of managing attributes easily.But if you use them 1588166124Srafancarelessly you may loose track of what attributes the window has 1589166124Srafanand garble the display. This is especially true while managing 1590166124Srafanmenus with colors and highlighting. So decide on a consistent 1591166124Srafanpolicy and stick to it. You can always use <var class= 1592166124Srafan"LITERAL">standend()</var> which is equivalent to <var class= 1593166124Srafan"LITERAL">attrset(A_NORMAL)</var> which turns off all attributes 1594166124Srafanand brings you to normal mode.</p> 1595166124Srafan</div> 1596166124Srafan<div class="SECT2"> 1597166124Srafan<hr> 1598166124Srafan<h3 class="SECT2"><a name="ATTR_GET" id="ATTR_GET">8.3. 1599166124Srafanattr_get()</a></h3> 1600166124Srafan<p>The function attr_get() gets the current attributes and color 1601166124Srafanpair of the window. Though we might not use this as often as the 1602166124Srafanabove functions, this is useful in scanning areas of screen. Say we 1603166124Srafanwanted to do some complex update on screen and we are not sure what 1604166124Srafanattribute each character is associated with. Then this function can 1605166124Srafanbe used with either attrset or attron to produce the desired 1606166124Srafaneffect.</p> 1607166124Srafan</div> 1608166124Srafan<div class="SECT2"> 1609166124Srafan<hr> 1610166124Srafan<h3 class="SECT2"><a name="ATTR_FUNCS" id="ATTR_FUNCS">8.4. attr_ 1611166124Srafanfunctions</a></h3> 1612166124Srafan<p>There are series of functions like attr_set(), attr_on etc.. 1613166124SrafanThese are similar to above functions except that they take 1614166124Srafanparameters of type <var class="LITERAL">attr_t</var>.</p> 1615166124Srafan</div> 1616166124Srafan<div class="SECT2"> 1617166124Srafan<hr> 1618166124Srafan<h3 class="SECT2"><a name="WATTRFUNCS" id="WATTRFUNCS">8.5. wattr 1619166124Srafanfunctions</a></h3> 1620166124Srafan<p>For each of the above functions we have a corresponding function 1621166124Srafanwith 'w' which operates on a particular window. The above functions 1622166124Srafanoperate on stdscr.</p> 1623166124Srafan</div> 1624166124Srafan<div class="SECT2"> 1625166124Srafan<hr> 1626166124Srafan<h3 class="SECT2"><a name="CHGAT" id="CHGAT">8.6. chgat() 1627166124Srafanfunctions</a></h3> 1628166124Srafan<p>The function chgat() is listed in the end of the man page 1629166124Srafancurs_attr. It actually is a useful one. This function can be used 1630166124Srafanto set attributes for a group of characters without moving. I mean 1631166124Srafanit !!! without moving the cursor :-) It changes the attributes of a 1632166124Srafangiven number of characters starting at the current cursor 1633166124Srafanlocation.</p> 1634166124Srafan<p>We can give -1 as the character count to update till end of 1635166124Srafanline. If you want to change attributes of characters from current 1636166124Srafanposition to end of line, just use this.</p> 1637166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 1638166124Srafan<tr> 1639166124Srafan<td> 1640166124Srafan<pre class="PROGRAMLISTING"> 1641166124Srafan<font color="#000000"> chgat(-1, A_REVERSE, 0, NULL);</font> 1642166124Srafan</pre></td> 1643166124Srafan</tr> 1644166124Srafan</table> 1645166124Srafan<p>This function is useful when changing attributes for characters 1646166124Srafanthat are already on the screen. Move to the character from which 1647166124Srafanyou want to change and change the attribute.</p> 1648166124Srafan<p>Other functions wchgat(), mvchgat(), wchgat() behave similarly 1649166124Srafanexcept that the w functions operate on the particular window. The 1650166124Srafanmv functions first move the cursor then perform the work given to 1651166124Srafanthem. Actually chgat is a macro which is replaced by a wchgat() 1652166124Srafanwith stdscr as the window. Most of the "w-less" functions are 1653166124Srafanmacros.</p> 1654166124Srafan<div class="EXAMPLE"><a name="BWICH" id="BWICH"></a> 1655166124Srafan<p><b>Example 6. Chgat() Usage example</b></p> 1656166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 1657166124Srafan<tr> 1658166124Srafan<td> 1659166124Srafan<pre class="PROGRAMLISTING"> 1660166124Srafan<font color="#000000"><span class= 1661166124Srafan"INLINEMEDIAOBJECT">#include <ncurses.h> 1662166124Srafan 1663166124Srafanint main(int argc, char *argv[]) 1664166124Srafan{ initscr(); /* Start curses mode */ 1665166124Srafan start_color(); /* Start color functionality */ 1666166124Srafan 1667166124Srafan init_pair(1, COLOR_CYAN, COLOR_BLACK); 1668166124Srafan printw("A Big string which i didn't care to type fully "); 1669166124Srafan mvchgat(0, 0, -1, A_BLINK, 1, NULL); 1670166124Srafan /* 1671166124Srafan * First two parameters specify the position at which to start 1672166124Srafan * Third parameter number of characters to update. -1 means till 1673166124Srafan * end of line 1674166124Srafan * Forth parameter is the normal attribute you wanted to give 1675166124Srafan * to the charcter 1676166124Srafan * Fifth is the color index. It is the index given during init_pair() 1677166124Srafan * use 0 if you didn't want color 1678166124Srafan * Sixth one is always NULL 1679166124Srafan */ 1680166124Srafan refresh(); 1681166124Srafan getch(); 1682166124Srafan endwin(); /* End curses mode */ 1683166124Srafan return 0; 1684166124Srafan}</span></font> 1685166124Srafan</pre></td> 1686166124Srafan</tr> 1687166124Srafan</table> 1688166124Srafan</div> 1689166124Srafan<p>This example also introduces us to the color world of curses. 1690166124SrafanColors will be explained in detail later. Use 0 for no color.</p> 1691166124Srafan</div> 1692166124Srafan</div> 1693166124Srafan<div class="SECT1"> 1694166124Srafan<hr> 1695166124Srafan<h2 class="SECT1"><a name="WINDOWS" id="WINDOWS">9. 1696166124SrafanWindows</a></h2> 1697166124Srafan<p>Windows form the most important concept in curses. You have seen 1698166124Srafanthe standard window stdscr above where all the functions implicitly 1699166124Srafanoperated on this window. Now to make design even a simplest GUI, 1700166124Srafanyou need to resort to windows. The main reason you may want to use 1701166124Srafanwindows is to manipulate parts of the screen separately, for better 1702166124Srafanefficiency, by updating only the windows that need to be changed 1703166124Srafanand for a better design. I would say the last reason is the most 1704166124Srafanimportant in going for windows. You should always strive for a 1705166124Srafanbetter and easy-to-manage design in your programs. If you are 1706166124Srafanwriting big, complex GUIs this is of pivotal importance before you 1707166124Srafanstart doing anything.</p> 1708166124Srafan<div class="SECT2"> 1709166124Srafan<hr> 1710166124Srafan<h3 class="SECT2"><a name="WINDOWBASICS" id="WINDOWBASICS">9.1. The 1711166124Srafanbasics</a></h3> 1712166124Srafan<p>A Window can be created by calling the function <var class= 1713166124Srafan"LITERAL">newwin()</var>. It doesn't create any thing on the screen 1714166124Srafanactually. It allocates memory for a structure to manipulate the 1715166124Srafanwindow and updates the structure with data regarding the window 1716166124Srafanlike it's size, beginy, beginx etc.. Hence in curses, a window is 1717166124Srafanjust an abstraction of an imaginary window, which can be 1718166124Srafanmanipulated independent of other parts of screen. The function 1719166124Srafannewwin() returns a pointer to structure WINDOW, which can be passed 1720166124Srafanto window related functions like wprintw() etc.. Finally the window 1721166124Srafancan be destroyed with delwin(). It will deallocate the memory 1722166124Srafanassociated with the window structure.</p> 1723166124Srafan</div> 1724166124Srafan<div class="SECT2"> 1725166124Srafan<hr> 1726166124Srafan<h3 class="SECT2"><a name="LETBEWINDOW" id="LETBEWINDOW">9.2. Let 1727166124Srafanthere be a Window !!!</a></h3> 1728166124Srafan<p>What fun is it, if a window is created and we can't see it. So 1729166124Srafanthe fun part begins by displaying the window. The function 1730166124Srafan<var class="LITERAL">box()</var> can be used to draw a border 1731166124Srafanaround the window. Let's explore these functions in more detail in 1732166124Srafanthis example.</p> 1733166124Srafan<div class="EXAMPLE"><a name="BWIBO" id="BWIBO"></a> 1734166124Srafan<p><b>Example 7. Window Border example</b></p> 1735166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 1736166124Srafan<tr> 1737166124Srafan<td> 1738166124Srafan<pre class="PROGRAMLISTING"> 1739166124Srafan<font color="#000000"><span class= 1740166124Srafan"INLINEMEDIAOBJECT">#include <ncurses.h> 1741166124Srafan 1742166124Srafan 1743166124SrafanWINDOW *create_newwin(int height, int width, int starty, int startx); 1744166124Srafanvoid destroy_win(WINDOW *local_win); 1745166124Srafan 1746166124Srafanint main(int argc, char *argv[]) 1747166124Srafan{ WINDOW *my_win; 1748166124Srafan int startx, starty, width, height; 1749166124Srafan int ch; 1750166124Srafan 1751166124Srafan initscr(); /* Start curses mode */ 1752166124Srafan cbreak(); /* Line buffering disabled, Pass on 1753166124Srafan * everty thing to me */ 1754166124Srafan keypad(stdscr, TRUE); /* I need that nifty F1 */ 1755166124Srafan 1756166124Srafan height = 3; 1757166124Srafan width = 10; 1758166124Srafan starty = (LINES - height) / 2; /* Calculating for a center placement */ 1759166124Srafan startx = (COLS - width) / 2; /* of the window */ 1760166124Srafan printw("Press F1 to exit"); 1761166124Srafan refresh(); 1762166124Srafan my_win = create_newwin(height, width, starty, startx); 1763166124Srafan 1764166124Srafan while((ch = getch()) != KEY_F(1)) 1765166124Srafan { switch(ch) 1766166124Srafan { case KEY_LEFT: 1767166124Srafan destroy_win(my_win); 1768166124Srafan my_win = create_newwin(height, width, starty,--startx); 1769166124Srafan break; 1770166124Srafan case KEY_RIGHT: 1771166124Srafan destroy_win(my_win); 1772166124Srafan my_win = create_newwin(height, width, starty,++startx); 1773166124Srafan break; 1774166124Srafan case KEY_UP: 1775166124Srafan destroy_win(my_win); 1776166124Srafan my_win = create_newwin(height, width, --starty,startx); 1777166124Srafan break; 1778166124Srafan case KEY_DOWN: 1779166124Srafan destroy_win(my_win); 1780166124Srafan my_win = create_newwin(height, width, ++starty,startx); 1781166124Srafan break; 1782166124Srafan } 1783166124Srafan } 1784166124Srafan 1785166124Srafan endwin(); /* End curses mode */ 1786166124Srafan return 0; 1787166124Srafan} 1788166124Srafan 1789166124SrafanWINDOW *create_newwin(int height, int width, int starty, int startx) 1790166124Srafan{ WINDOW *local_win; 1791166124Srafan 1792166124Srafan local_win = newwin(height, width, starty, startx); 1793166124Srafan box(local_win, 0 , 0); /* 0, 0 gives default characters 1794166124Srafan * for the vertical and horizontal 1795166124Srafan * lines */ 1796166124Srafan wrefresh(local_win); /* Show that box */ 1797166124Srafan 1798166124Srafan return local_win; 1799166124Srafan} 1800166124Srafan 1801166124Srafanvoid destroy_win(WINDOW *local_win) 1802166124Srafan{ 1803166124Srafan /* box(local_win, ' ', ' '); : This won't produce the desired 1804166124Srafan * result of erasing the window. It will leave it's four corners 1805166124Srafan * and so an ugly remnant of window. 1806166124Srafan */ 1807166124Srafan wborder(local_win, ' ', ' ', ' ',' ',' ',' ',' ',' '); 1808166124Srafan /* The parameters taken are 1809166124Srafan * 1. win: the window on which to operate 1810166124Srafan * 2. ls: character to be used for the left side of the window 1811166124Srafan * 3. rs: character to be used for the right side of the window 1812166124Srafan * 4. ts: character to be used for the top side of the window 1813166124Srafan * 5. bs: character to be used for the bottom side of the window 1814166124Srafan * 6. tl: character to be used for the top left corner of the window 1815166124Srafan * 7. tr: character to be used for the top right corner of the window 1816166124Srafan * 8. bl: character to be used for the bottom left corner of the window 1817166124Srafan * 9. br: character to be used for the bottom right corner of the window 1818166124Srafan */ 1819166124Srafan wrefresh(local_win); 1820166124Srafan delwin(local_win); 1821166124Srafan}</span></font> 1822166124Srafan</pre></td> 1823166124Srafan</tr> 1824166124Srafan</table> 1825166124Srafan</div> 1826166124Srafan</div> 1827166124Srafan<div class="SECT2"> 1828166124Srafan<hr> 1829166124Srafan<h3 class="SECT2"><a name="BORDEREXEXPL" id="BORDEREXEXPL">9.3. 1830166124SrafanExplanation</a></h3> 1831166124Srafan<p>Don't scream. I know it's a big example. But I have to explain 1832166124Srafansome important things here :-). This program creates a rectangular 1833166124Srafanwindow that can be moved with left, right, up, down arrow keys. It 1834166124Srafanrepeatedly creates and destroys windows as user press a key. Don't 1835166124Srafango beyond the screen limits. Checking for those limits is left as 1836166124Srafanan exercise for the reader. Let's dissect it by line by line.</p> 1837166124Srafan<p>The <var class="LITERAL">create_newwin()</var> function creates 1838166124Srafana window with <var class="LITERAL">newwin()</var> and displays a 1839166124Srafanborder around it with box. The function <var class= 1840166124Srafan"LITERAL">destroy_win()</var> first erases the window from screen 1841166124Srafanby painting a border with ' ' character and then calling 1842166124Srafan<var class="LITERAL">delwin()</var> to deallocate memory related to 1843166124Srafanit. Depending on the key the user presses, starty or startx is 1844166124Srafanchanged and a new window is created.</p> 1845166124Srafan<p>In the destroy_win, as you can see, I used wborder instead of 1846166124Srafanbox. The reason is written in the comments (You missed it. I know. 1847166124SrafanRead the code :-)). wborder draws a border around the window with 1848166124Srafanthe characters given to it as the 4 corner points and the 4 lines. 1849166124SrafanTo put it clearly, if you have called wborder as below:</p> 1850166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 1851166124Srafan<tr> 1852166124Srafan<td> 1853166124Srafan<pre class="PROGRAMLISTING"> 1854166124Srafan<font color= 1855166124Srafan"#000000"> wborder(win, '|', '|', '-', '-', '+', '+', '+', '+');</font> 1856166124Srafan</pre></td> 1857166124Srafan</tr> 1858166124Srafan</table> 1859166124Srafan<p>it produces some thing like</p> 1860166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 1861166124Srafan<tr> 1862166124Srafan<td> 1863166124Srafan<pre class="PROGRAMLISTING"> 1864166124Srafan<font color="#000000"> +------------+ 1865166124Srafan | | 1866166124Srafan | | 1867166124Srafan | | 1868166124Srafan | | 1869166124Srafan | | 1870166124Srafan | | 1871166124Srafan +------------+</font> 1872166124Srafan</pre></td> 1873166124Srafan</tr> 1874166124Srafan</table> 1875166124Srafan</div> 1876166124Srafan<div class="SECT2"> 1877166124Srafan<hr> 1878166124Srafan<h3 class="SECT2"><a name="OTHERSTUFF" id="OTHERSTUFF">9.4. The 1879166124Srafanother stuff in the example</a></h3> 1880166124Srafan<p>You can also see in the above examples, that I have used the 1881166124Srafanvariables COLS, LINES which are initialized to the screen sizes 1882166124Srafanafter initscr(). They can be useful in finding screen dimensions 1883166124Srafanand finding the center co-ordinate of the screen as above. The 1884166124Srafanfunction <var class="LITERAL">getch()</var> as usual gets the key 1885166124Srafanfrom keyboard and according to the key it does the corresponding 1886166124Srafanwork. This type of switch- case is very common in any GUI based 1887166124Srafanprograms.</p> 1888166124Srafan</div> 1889166124Srafan<div class="SECT2"> 1890166124Srafan<hr> 1891166124Srafan<h3 class="SECT2"><a name="OTHERBORDERFUNCS" id= 1892166124Srafan"OTHERBORDERFUNCS">9.5. Other Border functions</a></h3> 1893166124Srafan<p>Above program is grossly inefficient in that with each press of 1894166124Srafana key, a window is destroyed and another is created. So let's write 1895166124Srafana more efficient program which uses other border related 1896166124Srafanfunctions.</p> 1897166124Srafan<p>The following program uses <var class="LITERAL">mvhline()</var> 1898166124Srafanand <var class="LITERAL">mvvline()</var> to achieve similar effect. 1899166124SrafanThese two functions are simple. They create a horizontal or 1900166124Srafanvertical line of the specified length at the specified 1901166124Srafanposition.</p> 1902166124Srafan<div class="EXAMPLE"><a name="BOTBO" id="BOTBO"></a> 1903166124Srafan<p><b>Example 8. More border functions</b></p> 1904166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 1905166124Srafan<tr> 1906166124Srafan<td> 1907166124Srafan<pre class="PROGRAMLISTING"> 1908166124Srafan<font color="#000000"><span class= 1909166124Srafan"INLINEMEDIAOBJECT">#include <ncurses.h> 1910166124Srafan 1911166124Srafantypedef struct _win_border_struct { 1912166124Srafan chtype ls, rs, ts, bs, 1913166124Srafan tl, tr, bl, br; 1914166124Srafan}WIN_BORDER; 1915166124Srafan 1916166124Srafantypedef struct _WIN_struct { 1917166124Srafan 1918166124Srafan int startx, starty; 1919166124Srafan int height, width; 1920166124Srafan WIN_BORDER border; 1921166124Srafan}WIN; 1922166124Srafan 1923166124Srafanvoid init_win_params(WIN *p_win); 1924166124Srafanvoid print_win_params(WIN *p_win); 1925166124Srafanvoid create_box(WIN *win, bool flag); 1926166124Srafan 1927166124Srafanint main(int argc, char *argv[]) 1928166124Srafan{ WIN win; 1929166124Srafan int ch; 1930166124Srafan 1931166124Srafan initscr(); /* Start curses mode */ 1932166124Srafan start_color(); /* Start the color functionality */ 1933166124Srafan cbreak(); /* Line buffering disabled, Pass on 1934166124Srafan * everty thing to me */ 1935166124Srafan keypad(stdscr, TRUE); /* I need that nifty F1 */ 1936166124Srafan noecho(); 1937166124Srafan init_pair(1, COLOR_CYAN, COLOR_BLACK); 1938166124Srafan 1939166124Srafan /* Initialize the window parameters */ 1940166124Srafan init_win_params(&win); 1941166124Srafan print_win_params(&win); 1942166124Srafan 1943166124Srafan attron(COLOR_PAIR(1)); 1944166124Srafan printw("Press F1 to exit"); 1945166124Srafan refresh(); 1946166124Srafan attroff(COLOR_PAIR(1)); 1947166124Srafan 1948166124Srafan create_box(&win, TRUE); 1949166124Srafan while((ch = getch()) != KEY_F(1)) 1950166124Srafan { switch(ch) 1951166124Srafan { case KEY_LEFT: 1952166124Srafan create_box(&win, FALSE); 1953166124Srafan --win.startx; 1954166124Srafan create_box(&win, TRUE); 1955166124Srafan break; 1956166124Srafan case KEY_RIGHT: 1957166124Srafan create_box(&win, FALSE); 1958166124Srafan ++win.startx; 1959166124Srafan create_box(&win, TRUE); 1960166124Srafan break; 1961166124Srafan case KEY_UP: 1962166124Srafan create_box(&win, FALSE); 1963166124Srafan --win.starty; 1964166124Srafan create_box(&win, TRUE); 1965166124Srafan break; 1966166124Srafan case KEY_DOWN: 1967166124Srafan create_box(&win, FALSE); 1968166124Srafan ++win.starty; 1969166124Srafan create_box(&win, TRUE); 1970166124Srafan break; 1971166124Srafan } 1972166124Srafan } 1973166124Srafan endwin(); /* End curses mode */ 1974166124Srafan return 0; 1975166124Srafan} 1976166124Srafanvoid init_win_params(WIN *p_win) 1977166124Srafan{ 1978166124Srafan p_win->height = 3; 1979166124Srafan p_win->width = 10; 1980166124Srafan p_win->starty = (LINES - p_win->height)/2; 1981166124Srafan p_win->startx = (COLS - p_win->width)/2; 1982166124Srafan 1983166124Srafan p_win->border.ls = '|'; 1984166124Srafan p_win->border.rs = '|'; 1985166124Srafan p_win->border.ts = '-'; 1986166124Srafan p_win->border.bs = '-'; 1987166124Srafan p_win->border.tl = '+'; 1988166124Srafan p_win->border.tr = '+'; 1989166124Srafan p_win->border.bl = '+'; 1990166124Srafan p_win->border.br = '+'; 1991166124Srafan 1992166124Srafan} 1993166124Srafanvoid print_win_params(WIN *p_win) 1994166124Srafan{ 1995166124Srafan#ifdef _DEBUG 1996166124Srafan mvprintw(25, 0, "%d %d %d %d", p_win->startx, p_win->starty, 1997166124Srafan p_win->width, p_win->height); 1998166124Srafan refresh(); 1999166124Srafan#endif 2000166124Srafan} 2001166124Srafanvoid create_box(WIN *p_win, bool flag) 2002166124Srafan{ int i, j; 2003166124Srafan int x, y, w, h; 2004166124Srafan 2005166124Srafan x = p_win->startx; 2006166124Srafan y = p_win->starty; 2007166124Srafan w = p_win->width; 2008166124Srafan h = p_win->height; 2009166124Srafan 2010166124Srafan if(flag == TRUE) 2011166124Srafan { mvaddch(y, x, p_win->border.tl); 2012166124Srafan mvaddch(y, x + w, p_win->border.tr); 2013166124Srafan mvaddch(y + h, x, p_win->border.bl); 2014166124Srafan mvaddch(y + h, x + w, p_win->border.br); 2015166124Srafan mvhline(y, x + 1, p_win->border.ts, w - 1); 2016166124Srafan mvhline(y + h, x + 1, p_win->border.bs, w - 1); 2017166124Srafan mvvline(y + 1, x, p_win->border.ls, h - 1); 2018166124Srafan mvvline(y + 1, x + w, p_win->border.rs, h - 1); 2019166124Srafan 2020166124Srafan } 2021166124Srafan else 2022166124Srafan for(j = y; j <= y + h; ++j) 2023166124Srafan for(i = x; i <= x + w; ++i) 2024166124Srafan mvaddch(j, i, ' '); 2025166124Srafan 2026166124Srafan refresh(); 2027166124Srafan 2028166124Srafan}</span></font> 2029166124Srafan</pre></td> 2030166124Srafan</tr> 2031166124Srafan</table> 2032166124Srafan</div> 2033166124Srafan</div> 2034166124Srafan</div> 2035166124Srafan<div class="SECT1"> 2036166124Srafan<hr> 2037166124Srafan<h2 class="SECT1"><a name="COLOR" id="COLOR">10. Colors</a></h2> 2038166124Srafan<div class="SECT2"> 2039166124Srafan<h3 class="SECT2"><a name="COLORBASICS" id="COLORBASICS">10.1. The 2040166124Srafanbasics</a></h3> 2041166124Srafan<p>Life seems dull with no colors. Curses has a nice mechanism to 2042166124Srafanhandle colors. Let's get into the thick of the things with a small 2043166124Srafanprogram.</p> 2044166124Srafan<div class="EXAMPLE"><a name="BSICO" id="BSICO"></a> 2045166124Srafan<p><b>Example 9. A Simple Color example</b></p> 2046166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 2047166124Srafan<tr> 2048166124Srafan<td> 2049166124Srafan<pre class="PROGRAMLISTING"> 2050166124Srafan<font color="#000000"><span class= 2051166124Srafan"INLINEMEDIAOBJECT">#include <ncurses.h> 2052166124Srafan 2053166124Srafanvoid print_in_middle(WINDOW *win, int starty, int startx, int width, char *string); 2054166124Srafanint main(int argc, char *argv[]) 2055166124Srafan{ initscr(); /* Start curses mode */ 2056166124Srafan if(has_colors() == FALSE) 2057166124Srafan { endwin(); 2058166124Srafan printf("Your terminal does not support color\n"); 2059166124Srafan exit(1); 2060166124Srafan } 2061166124Srafan start_color(); /* Start color */ 2062166124Srafan init_pair(1, COLOR_RED, COLOR_BLACK); 2063166124Srafan 2064166124Srafan attron(COLOR_PAIR(1)); 2065166124Srafan print_in_middle(stdscr, LINES / 2, 0, 0, "Viola !!! In color ..."); 2066166124Srafan attroff(COLOR_PAIR(1)); 2067166124Srafan getch(); 2068166124Srafan endwin(); 2069166124Srafan} 2070166124Srafanvoid print_in_middle(WINDOW *win, int starty, int startx, int width, char *string) 2071166124Srafan{ int length, x, y; 2072166124Srafan float temp; 2073166124Srafan 2074166124Srafan if(win == NULL) 2075166124Srafan win = stdscr; 2076166124Srafan getyx(win, y, x); 2077166124Srafan if(startx != 0) 2078166124Srafan x = startx; 2079166124Srafan if(starty != 0) 2080166124Srafan y = starty; 2081166124Srafan if(width == 0) 2082166124Srafan width = 80; 2083166124Srafan 2084166124Srafan length = strlen(string); 2085166124Srafan temp = (width - length)/ 2; 2086166124Srafan x = startx + (int)temp; 2087166124Srafan mvwprintw(win, y, x, "%s", string); 2088166124Srafan refresh(); 2089166124Srafan} 2090166124Srafan</span></font> 2091166124Srafan</pre></td> 2092166124Srafan</tr> 2093166124Srafan</table> 2094166124Srafan</div> 2095166124Srafan<p>As you can see, to start using color, you should first call the 2096166124Srafanfunction <var class="LITERAL">start_color()</var>. After that, you 2097166124Srafancan use color capabilities of your terminals using various 2098166124Srafanfunctions. To find out whether a terminal has color capabilities or 2099166124Srafannot, you can use <var class="LITERAL">has_colors()</var> function, 2100166124Srafanwhich returns FALSE if the terminal does not support color.</p> 2101166124Srafan<p>Curses initializes all the colors supported by terminal when 2102166124Srafanstart_color() is called. These can be accessed by the define 2103166124Srafanconstants like <var class="LITERAL">COLOR_BLACK</var> etc. Now to 2104166124Srafanactually start using colors, you have to define pairs. Colors are 2105166124Srafanalways used in pairs. That means you have to use the function 2106166124Srafan<var class="LITERAL">init_pair()</var> to define the foreground and 2107166124Srafanbackground for the pair number you give. After that that pair 2108166124Srafannumber can be used as a normal attribute with <var class= 2109166124Srafan"LITERAL">COLOR_PAIR()</var>function. This may seem to be 2110166124Srafancumbersome at first. But this elegant solution allows us to manage 2111166124Srafancolor pairs very easily. To appreciate it, you have to look into 2112166124Srafanthe the source code of "dialog", a utility for displaying dialog 2113166124Srafanboxes from shell scripts. The developers have defined foreground 2114166124Srafanand background combinations for all the colors they might need and 2115166124Srafaninitialized at the beginning. This makes it very easy to set 2116166124Srafanattributes just by accessing a pair which we already have defined 2117166124Srafanas a constant.</p> 2118166124Srafan<p>The following colors are defined in <var class= 2119166124Srafan"LITERAL">curses.h</var>. You can use these as parameters for 2120166124Srafanvarious color functions.</p> 2121166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 2122166124Srafan<tr> 2123166124Srafan<td> 2124166124Srafan<pre class="PROGRAMLISTING"> 2125166124Srafan<font color="#000000"> COLOR_BLACK 0 2126166124Srafan COLOR_RED 1 2127166124Srafan COLOR_GREEN 2 2128166124Srafan COLOR_YELLOW 3 2129166124Srafan COLOR_BLUE 4 2130166124Srafan COLOR_MAGENTA 5 2131166124Srafan COLOR_CYAN 6 2132166124Srafan COLOR_WHITE 7</font> 2133166124Srafan</pre></td> 2134166124Srafan</tr> 2135166124Srafan</table> 2136166124Srafan</div> 2137166124Srafan<div class="SECT2"> 2138166124Srafan<hr> 2139166124Srafan<h3 class="SECT2"><a name="CHANGECOLORDEFS" id= 2140166124Srafan"CHANGECOLORDEFS">10.2. Changing Color Definitions</a></h3> 2141166124Srafan<p>The function <var class="LITERAL">init_color()</var>can be used 2142166124Srafanto change the rgb values for the colors defined by curses 2143166124Srafaninitially. Say you wanted to lighten the intensity of red color by 2144166124Srafana minuscule. Then you can use this function as</p> 2145166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 2146166124Srafan<tr> 2147166124Srafan<td> 2148166124Srafan<pre class="PROGRAMLISTING"> 2149166124Srafan<font color="#000000"> init_color(COLOR_RED, 700, 0, 0); 2150166124Srafan /* param 1 : color name 2151166124Srafan * param 2, 3, 4 : rgb content min = 0, max = 1000 */</font> 2152166124Srafan</pre></td> 2153166124Srafan</tr> 2154166124Srafan</table> 2155166124Srafan<p>If your terminal cannot change the color definitions, the 2156166124Srafanfunction returns ERR. The function <var class= 2157166124Srafan"LITERAL">can_change_color()</var> can be used to find out whether 2158166124Srafanthe terminal has the capability of changing color content or not. 2159166124SrafanThe rgb content is scaled from 0 to 1000. Initially RED color is 2160166124Srafandefined with content 1000(r), 0(g), 0(b).</p> 2161166124Srafan</div> 2162166124Srafan<div class="SECT2"> 2163166124Srafan<hr> 2164166124Srafan<h3 class="SECT2"><a name="COLORCONTENT" id="COLORCONTENT">10.3. 2165166124SrafanColor Content</a></h3> 2166166124Srafan<p>The functions <var class="LITERAL">color_content()</var> and 2167166124Srafan<var class="LITERAL">pair_content()</var> can be used to find the 2168166124Srafancolor content and foreground, background combination for the 2169166124Srafanpair.</p> 2170166124Srafan</div> 2171166124Srafan</div> 2172166124Srafan<div class="SECT1"> 2173166124Srafan<hr> 2174166124Srafan<h2 class="SECT1"><a name="KEYS" id="KEYS">11. Interfacing with the 2175166124Srafankey board</a></h2> 2176166124Srafan<div class="SECT2"> 2177166124Srafan<h3 class="SECT2"><a name="KEYSBASICS" id="KEYSBASICS">11.1. The 2178166124SrafanBasics</a></h3> 2179166124Srafan<p>No GUI is complete without a strong user interface and to 2180166124Srafaninteract with the user, a curses program should be sensitive to key 2181166124Srafanpresses or the mouse actions done by the user. Let's deal with the 2182166124Srafankeys first.</p> 2183166124Srafan<p>As you have seen in almost all of the above examples, it's very 2184166124Srafaneasy to get key input from the user. A simple way of getting key 2185166124Srafanpresses is to use <var class="LITERAL">getch()</var> function. The 2186166124Srafancbreak mode should be enabled to read keys when you are interested 2187166124Srafanin reading individual key hits rather than complete lines of text 2188166124Srafan(which usually end with a carriage return). keypad should be 2189166124Srafanenabled to get the Functions keys, arrow keys etc. See the 2190166124Srafaninitialization section for details.</p> 2191166124Srafan<p><var class="LITERAL">getch()</var> returns an integer 2192166124Srafancorresponding to the key pressed. If it is a normal character, the 2193166124Srafaninteger value will be equivalent to the character. Otherwise it 2194166124Srafanreturns a number which can be matched with the constants defined in 2195166124Srafan<var class="LITERAL">curses.h</var>. For example if the user 2196166124Srafanpresses F1, the integer returned is 265. This can be checked using 2197166124Srafanthe macro KEY_F() defined in curses.h. This makes reading keys 2198166124Srafanportable and easy to manage.</p> 2199166124Srafan<p>For example, if you call getch() like this</p> 2200166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 2201166124Srafan<tr> 2202166124Srafan<td> 2203166124Srafan<pre class="PROGRAMLISTING"> 2204166124Srafan<font color="#000000"> int ch; 2205166124Srafan 2206166124Srafan ch = getch();</font> 2207166124Srafan</pre></td> 2208166124Srafan</tr> 2209166124Srafan</table> 2210166124Srafan<p>getch() will wait for the user to press a key, (unless you 2211166124Srafanspecified a timeout) and when user presses a key, the corresponding 2212166124Srafaninteger is returned. Then you can check the value returned with the 2213166124Srafanconstants defined in curses.h to match against the keys you 2214166124Srafanwant.</p> 2215166124Srafan<p>The following code piece will do that job.</p> 2216166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 2217166124Srafan<tr> 2218166124Srafan<td> 2219166124Srafan<pre class="PROGRAMLISTING"> 2220166124Srafan<font color="#000000"> if(ch == KEY_LEFT) 2221166124Srafan printw("Left arrow is pressed\n");</font> 2222166124Srafan</pre></td> 2223166124Srafan</tr> 2224166124Srafan</table> 2225166124Srafan<p>Let's write a small program which creates a menu which can be 2226166124Srafannavigated by up and down arrows.</p> 2227166124Srafan</div> 2228166124Srafan<div class="SECT2"> 2229166124Srafan<hr> 2230166124Srafan<h3 class="SECT2"><a name="SIMPLEKEYEX" id="SIMPLEKEYEX">11.2. A 2231166124SrafanSimple Key Usage example</a></h3> 2232166124Srafan<div class="EXAMPLE"><a name="BSIKE" id="BSIKE"></a> 2233166124Srafan<p><b>Example 10. A Simple Key Usage example</b></p> 2234166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 2235166124Srafan<tr> 2236166124Srafan<td> 2237166124Srafan<pre class="PROGRAMLISTING"> 2238166124Srafan<font color="#000000"><span class= 2239166124Srafan"INLINEMEDIAOBJECT">#include <stdio.h> 2240166124Srafan#include <ncurses.h> 2241166124Srafan 2242166124Srafan#define WIDTH 30 2243166124Srafan#define HEIGHT 10 2244166124Srafan 2245166124Srafanint startx = 0; 2246166124Srafanint starty = 0; 2247166124Srafan 2248166124Srafanchar *choices[] = { 2249166124Srafan "Choice 1", 2250166124Srafan "Choice 2", 2251166124Srafan "Choice 3", 2252166124Srafan "Choice 4", 2253166124Srafan "Exit", 2254166124Srafan }; 2255166124Srafanint n_choices = sizeof(choices) / sizeof(char *); 2256166124Srafanvoid print_menu(WINDOW *menu_win, int highlight); 2257166124Srafan 2258166124Srafanint main() 2259166124Srafan{ WINDOW *menu_win; 2260166124Srafan int highlight = 1; 2261166124Srafan int choice = 0; 2262166124Srafan int c; 2263166124Srafan 2264166124Srafan initscr(); 2265166124Srafan clear(); 2266166124Srafan noecho(); 2267166124Srafan cbreak(); /* Line buffering disabled. pass on everything */ 2268166124Srafan startx = (80 - WIDTH) / 2; 2269166124Srafan starty = (24 - HEIGHT) / 2; 2270166124Srafan 2271166124Srafan menu_win = newwin(HEIGHT, WIDTH, starty, startx); 2272166124Srafan keypad(menu_win, TRUE); 2273166124Srafan mvprintw(0, 0, "Use arrow keys to go up and down, Press enter to select a choice"); 2274166124Srafan refresh(); 2275166124Srafan print_menu(menu_win, highlight); 2276166124Srafan while(1) 2277166124Srafan { c = wgetch(menu_win); 2278166124Srafan switch(c) 2279166124Srafan { case KEY_UP: 2280166124Srafan if(highlight == 1) 2281166124Srafan highlight = n_choices; 2282166124Srafan else 2283166124Srafan --highlight; 2284166124Srafan break; 2285166124Srafan case KEY_DOWN: 2286166124Srafan if(highlight == n_choices) 2287166124Srafan highlight = 1; 2288166124Srafan else 2289166124Srafan ++highlight; 2290166124Srafan break; 2291166124Srafan case 10: 2292166124Srafan choice = highlight; 2293166124Srafan break; 2294166124Srafan default: 2295166124Srafan mvprintw(24, 0, "Charcter pressed is = %3d Hopefully it can be printed as '%c'", c, c); 2296166124Srafan refresh(); 2297166124Srafan break; 2298166124Srafan } 2299166124Srafan print_menu(menu_win, highlight); 2300166124Srafan if(choice != 0) /* User did a choice come out of the infinite loop */ 2301166124Srafan break; 2302166124Srafan } 2303166124Srafan mvprintw(23, 0, "You chose choice %d with choice string %s\n", choice, choices[choice - 1]); 2304166124Srafan clrtoeol(); 2305166124Srafan refresh(); 2306166124Srafan endwin(); 2307166124Srafan return 0; 2308166124Srafan} 2309166124Srafan 2310166124Srafan 2311166124Srafanvoid print_menu(WINDOW *menu_win, int highlight) 2312166124Srafan{ 2313166124Srafan int x, y, i; 2314166124Srafan 2315166124Srafan x = 2; 2316166124Srafan y = 2; 2317166124Srafan box(menu_win, 0, 0); 2318166124Srafan for(i = 0; i < n_choices; ++i) 2319166124Srafan { if(highlight == i + 1) /* High light the present choice */ 2320166124Srafan { wattron(menu_win, A_REVERSE); 2321166124Srafan mvwprintw(menu_win, y, x, "%s", choices[i]); 2322166124Srafan wattroff(menu_win, A_REVERSE); 2323166124Srafan } 2324166124Srafan else 2325166124Srafan mvwprintw(menu_win, y, x, "%s", choices[i]); 2326166124Srafan ++y; 2327166124Srafan } 2328166124Srafan wrefresh(menu_win); 2329166124Srafan} 2330166124Srafan</span></font> 2331166124Srafan</pre></td> 2332166124Srafan</tr> 2333166124Srafan</table> 2334166124Srafan</div> 2335166124Srafan</div> 2336166124Srafan</div> 2337166124Srafan<div class="SECT1"> 2338166124Srafan<hr> 2339166124Srafan<h2 class="SECT1"><a name="MOUSE" id="MOUSE">12. Interfacing with 2340166124Srafanthe mouse</a></h2> 2341166124Srafan<p>Now that you have seen how to get keys, lets do the same thing 2342166124Srafanfrom mouse. Usually each UI allows the user to interact with both 2343166124Srafankeyboard and mouse.</p> 2344166124Srafan<div class="SECT2"> 2345166124Srafan<hr> 2346166124Srafan<h3 class="SECT2"><a name="MOUSEBASICS" id="MOUSEBASICS">12.1. The 2347166124SrafanBasics</a></h3> 2348166124Srafan<p>Before you do any thing else, the events you want to receive 2349166124Srafanhave to be enabled with <var class="LITERAL">mousemask()</var>.</p> 2350166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 2351166124Srafan<tr> 2352166124Srafan<td> 2353166124Srafan<pre class="PROGRAMLISTING"> 2354166124Srafan<font color= 2355166124Srafan"#000000"> mousemask( mmask_t newmask, /* The events you want to listen to */ 2356166124Srafan mmask_t *oldmask) /* The old events mask */</font> 2357166124Srafan</pre></td> 2358166124Srafan</tr> 2359166124Srafan</table> 2360166124Srafan<p>The first parameter to above function is a bit mask of events 2361166124Srafanyou would like to listen. By default, all the events are turned 2362166124Srafanoff. The bit mask <var class="LITERAL">ALL_MOUSE_EVENTS</var> can 2363166124Srafanbe used to get all the events.</p> 2364166124Srafan<p>The following are all the event masks:</p> 2365166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 2366166124Srafan<tr> 2367166124Srafan<td> 2368166124Srafan<pre class="PROGRAMLISTING"> 2369166124Srafan<font color="#000000"> Name Description 2370166124Srafan --------------------------------------------------------------------- 2371166124Srafan BUTTON1_PRESSED mouse button 1 down 2372166124Srafan BUTTON1_RELEASED mouse button 1 up 2373166124Srafan BUTTON1_CLICKED mouse button 1 clicked 2374166124Srafan BUTTON1_DOUBLE_CLICKED mouse button 1 double clicked 2375166124Srafan BUTTON1_TRIPLE_CLICKED mouse button 1 triple clicked 2376166124Srafan BUTTON2_PRESSED mouse button 2 down 2377166124Srafan BUTTON2_RELEASED mouse button 2 up 2378166124Srafan BUTTON2_CLICKED mouse button 2 clicked 2379166124Srafan BUTTON2_DOUBLE_CLICKED mouse button 2 double clicked 2380166124Srafan BUTTON2_TRIPLE_CLICKED mouse button 2 triple clicked 2381166124Srafan BUTTON3_PRESSED mouse button 3 down 2382166124Srafan BUTTON3_RELEASED mouse button 3 up 2383166124Srafan BUTTON3_CLICKED mouse button 3 clicked 2384166124Srafan BUTTON3_DOUBLE_CLICKED mouse button 3 double clicked 2385166124Srafan BUTTON3_TRIPLE_CLICKED mouse button 3 triple clicked 2386166124Srafan BUTTON4_PRESSED mouse button 4 down 2387166124Srafan BUTTON4_RELEASED mouse button 4 up 2388166124Srafan BUTTON4_CLICKED mouse button 4 clicked 2389166124Srafan BUTTON4_DOUBLE_CLICKED mouse button 4 double clicked 2390166124Srafan BUTTON4_TRIPLE_CLICKED mouse button 4 triple clicked 2391166124Srafan BUTTON_SHIFT shift was down during button state change 2392166124Srafan BUTTON_CTRL control was down during button state change 2393166124Srafan BUTTON_ALT alt was down during button state change 2394166124Srafan ALL_MOUSE_EVENTS report all button state changes 2395166124Srafan REPORT_MOUSE_POSITION report mouse movement</font> 2396166124Srafan</pre></td> 2397166124Srafan</tr> 2398166124Srafan</table> 2399166124Srafan</div> 2400166124Srafan<div class="SECT2"> 2401166124Srafan<hr> 2402166124Srafan<h3 class="SECT2"><a name="GETTINGEVENTS" id="GETTINGEVENTS">12.2. 2403166124SrafanGetting the events</a></h3> 2404166124Srafan<p>Once a class of mouse events have been enabled, getch() class of 2405166124Srafanfunctions return KEY_MOUSE every time some mouse event happens. 2406166124SrafanThen the mouse event can be retrieved with <var class= 2407166124Srafan"LITERAL">getmouse()</var>.</p> 2408166124Srafan<p>The code approximately looks like this:</p> 2409166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 2410166124Srafan<tr> 2411166124Srafan<td> 2412166124Srafan<pre class="PROGRAMLISTING"> 2413166124Srafan<font color="#000000"> MEVENT event; 2414166124Srafan 2415166124Srafan ch = getch(); 2416166124Srafan if(ch == KEY_MOUSE) 2417166124Srafan if(getmouse(&event) == OK) 2418166124Srafan . /* Do some thing with the event */ 2419166124Srafan . 2420166124Srafan .</font> 2421166124Srafan</pre></td> 2422166124Srafan</tr> 2423166124Srafan</table> 2424166124Srafan<p>getmouse() returns the event into the pointer given to it. It's 2425166124Srafana structure which contains</p> 2426166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 2427166124Srafan<tr> 2428166124Srafan<td> 2429166124Srafan<pre class="PROGRAMLISTING"> 2430166124Srafan<font color="#000000"> typedef struct 2431166124Srafan { 2432166124Srafan short id; /* ID to distinguish multiple devices */ 2433166124Srafan int x, y, z; /* event coordinates */ 2434166124Srafan mmask_t bstate; /* button state bits */ 2435166124Srafan } </font> 2436166124Srafan</pre></td> 2437166124Srafan</tr> 2438166124Srafan</table> 2439166124Srafan<p>The <var class="LITERAL">bstate</var> is the main variable we 2440166124Srafanare interested in. It tells the button state of the mouse.</p> 2441166124Srafan<p>Then with a code snippet like the following, we can find out 2442166124Srafanwhat happened.</p> 2443166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 2444166124Srafan<tr> 2445166124Srafan<td> 2446166124Srafan<pre class="PROGRAMLISTING"> 2447166124Srafan<font color="#000000"> if(event.bstate & BUTTON1_PRESSED) 2448166124Srafan printw("Left Button Pressed");</font> 2449166124Srafan</pre></td> 2450166124Srafan</tr> 2451166124Srafan</table> 2452166124Srafan</div> 2453166124Srafan<div class="SECT2"> 2454166124Srafan<hr> 2455166124Srafan<h3 class="SECT2"><a name="MOUSETOGETHER" id="MOUSETOGETHER">12.3. 2456166124SrafanPutting it all Together</a></h3> 2457166124Srafan<p>That's pretty much interfacing with mouse. Let's create the same 2458166124Srafanmenu and enable mouse interaction. To make things simpler, key 2459166124Srafanhandling is removed.</p> 2460166124Srafan<div class="EXAMPLE"><a name="BMOME" id="BMOME"></a> 2461166124Srafan<p><b>Example 11. Access the menu with mouse !!!</b></p> 2462166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 2463166124Srafan<tr> 2464166124Srafan<td> 2465166124Srafan<pre class="PROGRAMLISTING"> 2466166124Srafan<font color="#000000"><span class= 2467166124Srafan"INLINEMEDIAOBJECT">#include <ncurses.h> 2468166124Srafan 2469166124Srafan#define WIDTH 30 2470166124Srafan#define HEIGHT 10 2471166124Srafan 2472166124Srafanint startx = 0; 2473166124Srafanint starty = 0; 2474166124Srafan 2475166124Srafanchar *choices[] = { "Choice 1", 2476166124Srafan "Choice 2", 2477166124Srafan "Choice 3", 2478166124Srafan "Choice 4", 2479166124Srafan "Exit", 2480166124Srafan }; 2481166124Srafan 2482166124Srafanint n_choices = sizeof(choices) / sizeof(char *); 2483166124Srafan 2484166124Srafanvoid print_menu(WINDOW *menu_win, int highlight); 2485166124Srafanvoid report_choice(int mouse_x, int mouse_y, int *p_choice); 2486166124Srafan 2487166124Srafanint main() 2488166124Srafan{ int c, choice = 0; 2489166124Srafan WINDOW *menu_win; 2490166124Srafan MEVENT event; 2491166124Srafan 2492166124Srafan /* Initialize curses */ 2493166124Srafan initscr(); 2494166124Srafan clear(); 2495166124Srafan noecho(); 2496166124Srafan cbreak(); //Line buffering disabled. pass on everything 2497166124Srafan 2498166124Srafan /* Try to put the window in the middle of screen */ 2499166124Srafan startx = (80 - WIDTH) / 2; 2500166124Srafan starty = (24 - HEIGHT) / 2; 2501166124Srafan 2502166124Srafan attron(A_REVERSE); 2503166124Srafan mvprintw(23, 1, "Click on Exit to quit (Works best in a virtual console)"); 2504166124Srafan refresh(); 2505166124Srafan attroff(A_REVERSE); 2506166124Srafan 2507166124Srafan /* Print the menu for the first time */ 2508166124Srafan menu_win = newwin(HEIGHT, WIDTH, starty, startx); 2509166124Srafan print_menu(menu_win, 1); 2510166124Srafan /* Get all the mouse events */ 2511166124Srafan mousemask(ALL_MOUSE_EVENTS, NULL); 2512166124Srafan 2513166124Srafan while(1) 2514166124Srafan { c = wgetch(menu_win); 2515166124Srafan switch(c) 2516166124Srafan { case KEY_MOUSE: 2517166124Srafan if(getmouse(&event) == OK) 2518166124Srafan { /* When the user clicks left mouse button */ 2519166124Srafan if(event.bstate & BUTTON1_PRESSED) 2520166124Srafan { report_choice(event.x + 1, event.y + 1, &choice); 2521166124Srafan if(choice == -1) //Exit chosen 2522166124Srafan goto end; 2523166124Srafan mvprintw(22, 1, "Choice made is : %d String Chosen is \"%10s\"", choice, choices[choice - 1]); 2524166124Srafan refresh(); 2525166124Srafan } 2526166124Srafan } 2527166124Srafan print_menu(menu_win, choice); 2528166124Srafan break; 2529166124Srafan } 2530166124Srafan } 2531166124Srafanend: 2532166124Srafan endwin(); 2533166124Srafan return 0; 2534166124Srafan} 2535166124Srafan 2536166124Srafan 2537166124Srafanvoid print_menu(WINDOW *menu_win, int highlight) 2538166124Srafan{ 2539166124Srafan int x, y, i; 2540166124Srafan 2541166124Srafan x = 2; 2542166124Srafan y = 2; 2543166124Srafan box(menu_win, 0, 0); 2544166124Srafan for(i = 0; i < n_choices; ++i) 2545166124Srafan { if(highlight == i + 1) 2546166124Srafan { wattron(menu_win, A_REVERSE); 2547166124Srafan mvwprintw(menu_win, y, x, "%s", choices[i]); 2548166124Srafan wattroff(menu_win, A_REVERSE); 2549166124Srafan } 2550166124Srafan else 2551166124Srafan mvwprintw(menu_win, y, x, "%s", choices[i]); 2552166124Srafan ++y; 2553166124Srafan } 2554166124Srafan wrefresh(menu_win); 2555166124Srafan} 2556166124Srafan 2557166124Srafan/* Report the choice according to mouse position */ 2558166124Srafanvoid report_choice(int mouse_x, int mouse_y, int *p_choice) 2559166124Srafan{ int i,j, choice; 2560166124Srafan 2561166124Srafan i = startx + 2; 2562166124Srafan j = starty + 3; 2563166124Srafan 2564166124Srafan for(choice = 0; choice < n_choices; ++choice) 2565166124Srafan if(mouse_y == j + choice && mouse_x >= i && mouse_x <= i + strlen(choices[choice])) 2566166124Srafan { if(choice == n_choices - 1) 2567166124Srafan *p_choice = -1; 2568166124Srafan else 2569166124Srafan *p_choice = choice + 1; 2570166124Srafan break; 2571166124Srafan } 2572166124Srafan}</span></font> 2573166124Srafan</pre></td> 2574166124Srafan</tr> 2575166124Srafan</table> 2576166124Srafan</div> 2577166124Srafan</div> 2578166124Srafan<div class="SECT2"> 2579166124Srafan<hr> 2580166124Srafan<h3 class="SECT2"><a name="MISCMOUSEFUNCS" id= 2581166124Srafan"MISCMOUSEFUNCS">12.4. Miscellaneous Functions</a></h3> 2582166124Srafan<p>The functions mouse_trafo() and wmouse_trafo() can be used to 2583166124Srafanconvert to mouse co-ordinates to screen relative co-ordinates. See 2584166124Srafancurs_mouse(3X) man page for details.</p> 2585166124Srafan<p>The mouseinterval function sets the maximum time (in thousands 2586166124Srafanof a second) that can elapse between press and release events in 2587166124Srafanorder for them to be recognized as a click. This function returns 2588166124Srafanthe previous interval value. The default is one fifth of a 2589166124Srafansecond.</p> 2590166124Srafan</div> 2591166124Srafan</div> 2592166124Srafan<div class="SECT1"> 2593166124Srafan<hr> 2594166124Srafan<h2 class="SECT1"><a name="SCREEN" id="SCREEN">13. Screen 2595166124SrafanManipulation</a></h2> 2596166124Srafan<p>In this section, we will look into some functions, which allow 2597166124Srafanus to manage the screen efficiently and to write some fancy 2598166124Srafanprograms. This is especially important in writing games.</p> 2599166124Srafan<div class="SECT2"> 2600166124Srafan<hr> 2601166124Srafan<h3 class="SECT2"><a name="GETYX" id="GETYX">13.1. getyx() 2602166124Srafanfunctions</a></h3> 2603166124Srafan<p>The function <var class="LITERAL">getyx()</var> can be used to 2604166124Srafanfind out the present cursor co-ordinates. It will fill the values 2605166124Srafanof x and y co-ordinates in the arguments given to it. Since getyx() 2606166124Srafanis a macro you don't have to pass the address of the variables. It 2607166124Srafancan be called as</p> 2608166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 2609166124Srafan<tr> 2610166124Srafan<td> 2611166124Srafan<pre class="PROGRAMLISTING"> 2612166124Srafan<font color="#000000"> getyx(win, y, x); 2613166124Srafan /* win: window pointer 2614166124Srafan * y, x: y, x co-ordinates will be put into this variables 2615166124Srafan */</font> 2616166124Srafan</pre></td> 2617166124Srafan</tr> 2618166124Srafan</table> 2619166124Srafan<p>The function getparyx() gets the beginning co-ordinates of the 2620166124Srafansub window relative to the main window. This is some times useful 2621166124Srafanto update a sub window. When designing fancy stuff like writing 2622166124Srafanmultiple menus, it becomes difficult to store the menu positions, 2623166124Srafantheir first option co-ordinates etc. A simple solution to this 2624166124Srafanproblem, is to create menus in sub windows and later find the 2625166124Srafanstarting co-ordinates of the menus by using getparyx().</p> 2626166124Srafan<p>The functions getbegyx() and getmaxyx() store current window's 2627166124Srafanbeginning and maximum co-ordinates. These functions are useful in 2628166124Srafanthe same way as above in managing the windows and sub windows 2629166124Srafaneffectively.</p> 2630166124Srafan</div> 2631166124Srafan<div class="SECT2"> 2632166124Srafan<hr> 2633166124Srafan<h3 class="SECT2"><a name="SCREENDUMP" id="SCREENDUMP">13.2. Screen 2634166124SrafanDumping</a></h3> 2635166124Srafan<p>While writing games, some times it becomes necessary to store 2636166124Srafanthe state of the screen and restore it back to the same state. The 2637166124Srafanfunction scr_dump() can be used to dump the screen contents to a 2638166124Srafanfile given as an argument. Later it can be restored by scr_restore 2639166124Srafanfunction. These two simple functions can be used effectively to 2640166124Srafanmaintain a fast moving game with changing scenarios.</p> 2641166124Srafan</div> 2642166124Srafan<div class="SECT2"> 2643166124Srafan<hr> 2644166124Srafan<h3 class="SECT2"><a name="WINDOWDUMP" id="WINDOWDUMP">13.3. Window 2645166124SrafanDumping</a></h3> 2646166124Srafan<p>To store and restore windows, the functions <var class= 2647166124Srafan"LITERAL">putwin()</var> and <var class="LITERAL">getwin()</var> 2648166124Srafancan be used. <var class="LITERAL">putwin()</var> puts the present 2649166124Srafanwindow state into a file, which can be later restored by 2650166124Srafan<var class="LITERAL">getwin()</var>.</p> 2651166124Srafan<p>The function <var class="LITERAL">copywin()</var> can be used to 2652166124Srafancopy a window completely onto another window. It takes the source 2653166124Srafanand destination windows as parameters and according to the 2654166124Srafanrectangle specified, it copies the rectangular region from source 2655166124Srafanto destination window. It's last parameter specifies whether to 2656166124Srafanoverwrite or just overlay the contents on to the destination 2657166124Srafanwindow. If this argument is true, then the copying is 2658166124Srafannon-destructive.</p> 2659166124Srafan</div> 2660166124Srafan</div> 2661166124Srafan<div class="SECT1"> 2662166124Srafan<hr> 2663166124Srafan<h2 class="SECT1"><a name="MISC" id="MISC">14. Miscellaneous 2664166124Srafanfeatures</a></h2> 2665166124Srafan<p>Now you know enough features to write a good curses program, 2666166124Srafanwith all bells and whistles. There are some miscellaneous functions 2667166124Srafanwhich are useful in various cases. Let's go headlong into some of 2668166124Srafanthose.</p> 2669166124Srafan<div class="SECT2"> 2670166124Srafan<hr> 2671166124Srafan<h3 class="SECT2"><a name="CURSSET" id="CURSSET">14.1. 2672166124Srafancurs_set()</a></h3> 2673166124Srafan<p>This function can be used to make the cursor invisible. The 2674166124Srafanparameter to this function should be</p> 2675166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 2676166124Srafan<tr> 2677166124Srafan<td> 2678166124Srafan<pre class="PROGRAMLISTING"> 2679166124Srafan<font color="#000000"> 0 : invisible or 2680166124Srafan 1 : normal or 2681166124Srafan 2 : very visible.</font> 2682166124Srafan</pre></td> 2683166124Srafan</tr> 2684166124Srafan</table> 2685166124Srafan</div> 2686166124Srafan<div class="SECT2"> 2687166124Srafan<hr> 2688166124Srafan<h3 class="SECT2"><a name="TEMPLEAVE" id="TEMPLEAVE">14.2. 2689166124SrafanTemporarily Leaving Curses mode</a></h3> 2690166124Srafan<p>Some times you may want to get back to cooked mode (normal line 2691166124Srafanbuffering mode) temporarily. In such a case you will first need to 2692166124Srafansave the tty modes with a call to <var class= 2693166124Srafan"LITERAL">def_prog_mode()</var> and then call <var class= 2694166124Srafan"LITERAL">endwin()</var> to end the curses mode. This will leave 2695166124Srafanyou in the original tty mode. To get back to curses once you are 2696166124Srafandone, call <var class="LITERAL">reset_prog_mode()</var> . This 2697166124Srafanfunction returns the tty to the state stored by <var class= 2698166124Srafan"LITERAL">def_prog_mode()</var>. Then do refresh(), and you are 2699166124Srafanback to the curses mode. Here is an example showing the sequence of 2700166124Srafanthings to be done.</p> 2701166124Srafan<div class="EXAMPLE"><a name="BTELE" id="BTELE"></a> 2702166124Srafan<p><b>Example 12. Temporarily Leaving Curses Mode</b></p> 2703166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 2704166124Srafan<tr> 2705166124Srafan<td> 2706166124Srafan<pre class="PROGRAMLISTING"> 2707166124Srafan<font color="#000000"><span class= 2708166124Srafan"INLINEMEDIAOBJECT">#include <ncurses.h> 2709166124Srafan 2710166124Srafanint main() 2711166124Srafan{ 2712166124Srafan initscr(); /* Start curses mode */ 2713166124Srafan printw("Hello World !!!\n"); /* Print Hello World */ 2714166124Srafan refresh(); /* Print it on to the real screen */ 2715166124Srafan def_prog_mode(); /* Save the tty modes */ 2716166124Srafan endwin(); /* End curses mode temporarily */ 2717166124Srafan system("/bin/sh"); /* Do whatever you like in cooked mode */ 2718166124Srafan reset_prog_mode(); /* Return to the previous tty mode*/ 2719166124Srafan /* stored by def_prog_mode() */ 2720166124Srafan refresh(); /* Do refresh() to restore the */ 2721166124Srafan /* Screen contents */ 2722166124Srafan printw("Another String\n"); /* Back to curses use the full */ 2723166124Srafan refresh(); /* capabilities of curses */ 2724166124Srafan endwin(); /* End curses mode */ 2725166124Srafan 2726166124Srafan return 0; 2727166124Srafan}</span></font> 2728166124Srafan</pre></td> 2729166124Srafan</tr> 2730166124Srafan</table> 2731166124Srafan</div> 2732166124Srafan</div> 2733166124Srafan<div class="SECT2"> 2734166124Srafan<hr> 2735166124Srafan<h3 class="SECT2"><a name="ACSVARS" id="ACSVARS">14.3. ACS_ 2736166124Srafanvariables</a></h3> 2737166124Srafan<p>If you have ever programmed in DOS, you know about those nifty 2738166124Srafancharacters in extended character set. They are printable only on 2739166124Srafansome terminals. NCURSES functions like <var class= 2740166124Srafan"LITERAL">box()</var> use these characters. All these variables 2741166124Srafanstart with ACS meaning alternative character set. You might have 2742166124Srafannoticed me using these characters in some of the programs above. 2743166124SrafanHere's an example showing all the characters.</p> 2744166124Srafan<div class="EXAMPLE"><a name="BACSVARS" id="BACSVARS"></a> 2745166124Srafan<p><b>Example 13. ACS Variables Example</b></p> 2746166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 2747166124Srafan<tr> 2748166124Srafan<td> 2749166124Srafan<pre class="PROGRAMLISTING"> 2750166124Srafan<font color="#000000"><span class= 2751166124Srafan"INLINEMEDIAOBJECT">#include <ncurses.h> 2752166124Srafan 2753166124Srafanint main() 2754166124Srafan{ 2755166124Srafan initscr(); 2756166124Srafan 2757166124Srafan printw("Upper left corner "); addch(ACS_ULCORNER); printw("\n"); 2758166124Srafan printw("Lower left corner "); addch(ACS_LLCORNER); printw("\n"); 2759166124Srafan printw("Lower right corner "); addch(ACS_LRCORNER); printw("\n"); 2760166124Srafan printw("Tee pointing right "); addch(ACS_LTEE); printw("\n"); 2761166124Srafan printw("Tee pointing left "); addch(ACS_RTEE); printw("\n"); 2762166124Srafan printw("Tee pointing up "); addch(ACS_BTEE); printw("\n"); 2763166124Srafan printw("Tee pointing down "); addch(ACS_TTEE); printw("\n"); 2764166124Srafan printw("Horizontal line "); addch(ACS_HLINE); printw("\n"); 2765166124Srafan printw("Vertical line "); addch(ACS_VLINE); printw("\n"); 2766166124Srafan printw("Large Plus or cross over "); addch(ACS_PLUS); printw("\n"); 2767166124Srafan printw("Scan Line 1 "); addch(ACS_S1); printw("\n"); 2768166124Srafan printw("Scan Line 3 "); addch(ACS_S3); printw("\n"); 2769166124Srafan printw("Scan Line 7 "); addch(ACS_S7); printw("\n"); 2770166124Srafan printw("Scan Line 9 "); addch(ACS_S9); printw("\n"); 2771166124Srafan printw("Diamond "); addch(ACS_DIAMOND); printw("\n"); 2772166124Srafan printw("Checker board (stipple) "); addch(ACS_CKBOARD); printw("\n"); 2773166124Srafan printw("Degree Symbol "); addch(ACS_DEGREE); printw("\n"); 2774166124Srafan printw("Plus/Minus Symbol "); addch(ACS_PLMINUS); printw("\n"); 2775166124Srafan printw("Bullet "); addch(ACS_BULLET); printw("\n"); 2776166124Srafan printw("Arrow Pointing Left "); addch(ACS_LARROW); printw("\n"); 2777166124Srafan printw("Arrow Pointing Right "); addch(ACS_RARROW); printw("\n"); 2778166124Srafan printw("Arrow Pointing Down "); addch(ACS_DARROW); printw("\n"); 2779166124Srafan printw("Arrow Pointing Up "); addch(ACS_UARROW); printw("\n"); 2780166124Srafan printw("Board of squares "); addch(ACS_BOARD); printw("\n"); 2781166124Srafan printw("Lantern Symbol "); addch(ACS_LANTERN); printw("\n"); 2782166124Srafan printw("Solid Square Block "); addch(ACS_BLOCK); printw("\n"); 2783166124Srafan printw("Less/Equal sign "); addch(ACS_LEQUAL); printw("\n"); 2784166124Srafan printw("Greater/Equal sign "); addch(ACS_GEQUAL); printw("\n"); 2785166124Srafan printw("Pi "); addch(ACS_PI); printw("\n"); 2786166124Srafan printw("Not equal "); addch(ACS_NEQUAL); printw("\n"); 2787166124Srafan printw("UK pound sign "); addch(ACS_STERLING); printw("\n"); 2788166124Srafan 2789166124Srafan refresh(); 2790166124Srafan getch(); 2791166124Srafan endwin(); 2792166124Srafan 2793166124Srafan return 0; 2794166124Srafan}</span></font> 2795166124Srafan</pre></td> 2796166124Srafan</tr> 2797166124Srafan</table> 2798166124Srafan</div> 2799166124Srafan</div> 2800166124Srafan</div> 2801166124Srafan<div class="SECT1"> 2802166124Srafan<hr> 2803166124Srafan<h2 class="SECT1"><a name="OTHERLIB" id="OTHERLIB">15. Other 2804166124Srafanlibraries</a></h2> 2805166124Srafan<p>Apart from the curses library, there are few text mode 2806166124Srafanlibraries, which provide more functionality and a lot of features. 2807166124SrafanThe following sections explain three standard libraries which are 2808166124Srafanusually distributed along with curses.</p> 2809166124Srafan</div> 2810166124Srafan<div class="SECT1"> 2811166124Srafan<hr> 2812166124Srafan<h2 class="SECT1"><a name="PANELS" id="PANELS">16. Panel 2813166124SrafanLibrary</a></h2> 2814166124Srafan<p>Now that you are proficient in curses, you wanted to do some 2815166124Srafanthing big. You created a lot of overlapping windows to give a 2816166124Srafanprofessional windows-type look. Unfortunately, it soon becomes 2817166124Srafandifficult to manage these. The multiple refreshes, updates plunge 2818166124Srafanyou into a nightmare. The overlapping windows create blotches, 2819166124Srafanwhenever you forget to refresh the windows in the proper order.</p> 2820166124Srafan<p>Don't despair. There's an elegant solution provided in panels 2821166124Srafanlibrary. In the words of developers of ncurses</p> 2822166124Srafan<p><em>When your interface design is such that windows may dive 2823166124Srafandeeper into the visibility stack or pop to the top at runtime, the 2824166124Srafanresulting book-keeping can be tedious and difficult to get right. 2825166124SrafanHence the panels library.</em></p> 2826166124Srafan<p>If you have lot of overlapping windows, then panels library is 2827166124Srafanthe way to go. It obviates the need of doing series of 2828166124Srafanwnoutrefresh(), doupdate() and relieves the burden of doing it 2829166124Srafancorrectly(bottom up). The library maintains information about the 2830166124Srafanorder of windows, their overlapping and update the screen properly. 2831166124SrafanSo why wait? Let's take a close peek into panels.</p> 2832166124Srafan<div class="SECT2"> 2833166124Srafan<hr> 2834166124Srafan<h3 class="SECT2"><a name="PANELBASICS" id="PANELBASICS">16.1. The 2835166124SrafanBasics</a></h3> 2836166124Srafan<p>Panel object is a window that is implicitly treated as part of a 2837166124Srafandeck including all other panel objects. The deck is treated as a 2838166124Srafanstack with the top panel being completely visible and the other 2839166124Srafanpanels may or may not be obscured according to their positions. So 2840166124Srafanthe basic idea is to create a stack of overlapping panels and use 2841166124Srafanpanels library to display them correctly. There is a function 2842166124Srafansimilar to refresh() which, when called , displays panels in the 2843166124Srafancorrect order. Functions are provided to hide or show panels, move 2844166124Srafanpanels, change its size etc.. The overlapping problem is managed by 2845166124Srafanthe panels library during all the calls to these functions.</p> 2846166124Srafan<p>The general flow of a panel program goes like this:</p> 2847166124Srafan<ol type="1"> 2848166124Srafan<li> 2849166124Srafan<p>Create the windows (with newwin()) to be attached to the 2850166124Srafanpanels.</p> 2851166124Srafan</li> 2852166124Srafan<li> 2853166124Srafan<p>Create panels with the chosen visibility order. Stack them up 2854166124Srafanaccording to the desired visibility. The function new_panel() is 2855166124Srafanused to created panels.</p> 2856166124Srafan</li> 2857166124Srafan<li> 2858166124Srafan<p>Call update_panels() to write the panels to the virtual screen 2859166124Srafanin correct visibility order. Do a doupdate() to show it on the 2860166124Srafanscreen.</p> 2861166124Srafan</li> 2862166124Srafan<li> 2863166124Srafan<p>Mainpulate the panels with show_panel(), hide_panel(), 2864166124Srafanmove_panel() etc. Make use of helper functions like panel_hidden() 2865166124Srafanand panel_window(). Make use of user pointer to store custom data 2866166124Srafanfor a panel. Use the functions set_panel_userptr() and 2867166124Srafanpanel_userptr() to set and get the user pointer for a panel.</p> 2868166124Srafan</li> 2869166124Srafan<li> 2870166124Srafan<p>When you are done with the panel use del_panel() to delete the 2871166124Srafanpanel.</p> 2872166124Srafan</li> 2873166124Srafan</ol> 2874166124Srafan<p>Let's make the concepts clear, with some programs. The following 2875166124Srafanis a simple program which creates 3 overlapping panels and shows 2876166124Srafanthem on the screen.</p> 2877166124Srafan</div> 2878166124Srafan<div class="SECT2"> 2879166124Srafan<hr> 2880166124Srafan<h3 class="SECT2"><a name="COMPILEPANELS" id="COMPILEPANELS">16.2. 2881166124SrafanCompiling With the Panels Library</a></h3> 2882166124Srafan<p>To use panels library functions, you have to include panel.h and 2883166124Srafanto link the program with panels library the flag -lpanel should be 2884166124Srafanadded along with -lncurses in that order.</p> 2885166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 2886166124Srafan<tr> 2887166124Srafan<td> 2888166124Srafan<pre class="PROGRAMLISTING"> 2889166124Srafan<font color="#000000"> #include <panel.h> 2890166124Srafan . 2891166124Srafan . 2892166124Srafan . 2893166124Srafan 2894166124Srafan compile and link: gcc <program file> -lpanel -lncurses</font> 2895166124Srafan</pre></td> 2896166124Srafan</tr> 2897166124Srafan</table> 2898166124Srafan<div class="EXAMPLE"><a name="PPASI" id="PPASI"></a> 2899166124Srafan<p><b>Example 14. Panel basics</b></p> 2900166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 2901166124Srafan<tr> 2902166124Srafan<td> 2903166124Srafan<pre class="PROGRAMLISTING"> 2904166124Srafan<font color="#000000"><span class= 2905166124Srafan"INLINEMEDIAOBJECT">#include <panel.h> 2906166124Srafan 2907166124Srafanint main() 2908166124Srafan{ WINDOW *my_wins[3]; 2909166124Srafan PANEL *my_panels[3]; 2910166124Srafan int lines = 10, cols = 40, y = 2, x = 4, i; 2911166124Srafan 2912166124Srafan initscr(); 2913166124Srafan cbreak(); 2914166124Srafan noecho(); 2915166124Srafan 2916166124Srafan /* Create windows for the panels */ 2917166124Srafan my_wins[0] = newwin(lines, cols, y, x); 2918166124Srafan my_wins[1] = newwin(lines, cols, y + 1, x + 5); 2919166124Srafan my_wins[2] = newwin(lines, cols, y + 2, x + 10); 2920166124Srafan 2921166124Srafan /* 2922166124Srafan * Create borders around the windows so that you can see the effect 2923166124Srafan * of panels 2924166124Srafan */ 2925166124Srafan for(i = 0; i < 3; ++i) 2926166124Srafan box(my_wins[i], 0, 0); 2927166124Srafan 2928166124Srafan /* Attach a panel to each window */ /* Order is bottom up */ 2929166124Srafan my_panels[0] = new_panel(my_wins[0]); /* Push 0, order: stdscr-0 */ 2930166124Srafan my_panels[1] = new_panel(my_wins[1]); /* Push 1, order: stdscr-0-1 */ 2931166124Srafan my_panels[2] = new_panel(my_wins[2]); /* Push 2, order: stdscr-0-1-2 */ 2932166124Srafan 2933166124Srafan /* Update the stacking order. 2nd panel will be on top */ 2934166124Srafan update_panels(); 2935166124Srafan 2936166124Srafan /* Show it on the screen */ 2937166124Srafan doupdate(); 2938166124Srafan 2939166124Srafan getch(); 2940166124Srafan endwin(); 2941166124Srafan} 2942166124Srafan</span></font> 2943166124Srafan</pre></td> 2944166124Srafan</tr> 2945166124Srafan</table> 2946166124Srafan</div> 2947166124Srafan<p>As you can see, above program follows a simple flow as 2948166124Srafanexplained. The windows are created with newwin() and then they are 2949166124Srafanattached to panels with new_panel(). As we attach one panel after 2950166124Srafananother, the stack of panels gets updated. To put them on screen 2951166124Srafanupdate_panels() and doupdate() are called.</p> 2952166124Srafan</div> 2953166124Srafan<div class="SECT2"> 2954166124Srafan<hr> 2955166124Srafan<h3 class="SECT2"><a name="PANELBROWSING" id="PANELBROWSING">16.3. 2956166124SrafanPanel Window Browsing</a></h3> 2957166124Srafan<p>A slightly complicated example is given below. This program 2958166124Srafancreates 3 windows which can be cycled through using tab. Have a 2959166124Srafanlook at the code.</p> 2960166124Srafan<div class="EXAMPLE"><a name="PPABR" id="PPABR"></a> 2961166124Srafan<p><b>Example 15. Panel Window Browsing Example</b></p> 2962166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 2963166124Srafan<tr> 2964166124Srafan<td> 2965166124Srafan<pre class="PROGRAMLISTING"> 2966166124Srafan<font color="#000000"><span class= 2967166124Srafan"INLINEMEDIAOBJECT">#include <panel.h> 2968166124Srafan 2969166124Srafan#define NLINES 10 2970166124Srafan#define NCOLS 40 2971166124Srafan 2972166124Srafanvoid init_wins(WINDOW **wins, int n); 2973166124Srafanvoid win_show(WINDOW *win, char *label, int label_color); 2974166124Srafanvoid print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); 2975166124Srafan 2976166124Srafanint main() 2977166124Srafan{ WINDOW *my_wins[3]; 2978166124Srafan PANEL *my_panels[3]; 2979166124Srafan PANEL *top; 2980166124Srafan int ch; 2981166124Srafan 2982166124Srafan /* Initialize curses */ 2983166124Srafan initscr(); 2984166124Srafan start_color(); 2985166124Srafan cbreak(); 2986166124Srafan noecho(); 2987166124Srafan keypad(stdscr, TRUE); 2988166124Srafan 2989166124Srafan /* Initialize all the colors */ 2990166124Srafan init_pair(1, COLOR_RED, COLOR_BLACK); 2991166124Srafan init_pair(2, COLOR_GREEN, COLOR_BLACK); 2992166124Srafan init_pair(3, COLOR_BLUE, COLOR_BLACK); 2993166124Srafan init_pair(4, COLOR_CYAN, COLOR_BLACK); 2994166124Srafan 2995166124Srafan init_wins(my_wins, 3); 2996166124Srafan 2997166124Srafan /* Attach a panel to each window */ /* Order is bottom up */ 2998166124Srafan my_panels[0] = new_panel(my_wins[0]); /* Push 0, order: stdscr-0 */ 2999166124Srafan my_panels[1] = new_panel(my_wins[1]); /* Push 1, order: stdscr-0-1 */ 3000166124Srafan my_panels[2] = new_panel(my_wins[2]); /* Push 2, order: stdscr-0-1-2 */ 3001166124Srafan 3002166124Srafan /* Set up the user pointers to the next panel */ 3003166124Srafan set_panel_userptr(my_panels[0], my_panels[1]); 3004166124Srafan set_panel_userptr(my_panels[1], my_panels[2]); 3005166124Srafan set_panel_userptr(my_panels[2], my_panels[0]); 3006166124Srafan 3007166124Srafan /* Update the stacking order. 2nd panel will be on top */ 3008166124Srafan update_panels(); 3009166124Srafan 3010166124Srafan /* Show it on the screen */ 3011166124Srafan attron(COLOR_PAIR(4)); 3012166124Srafan mvprintw(LINES - 2, 0, "Use tab to browse through the windows (F1 to Exit)"); 3013166124Srafan attroff(COLOR_PAIR(4)); 3014166124Srafan doupdate(); 3015166124Srafan 3016166124Srafan top = my_panels[2]; 3017166124Srafan while((ch = getch()) != KEY_F(1)) 3018166124Srafan { switch(ch) 3019166124Srafan { case 9: 3020166124Srafan top = (PANEL *)panel_userptr(top); 3021166124Srafan top_panel(top); 3022166124Srafan break; 3023166124Srafan } 3024166124Srafan update_panels(); 3025166124Srafan doupdate(); 3026166124Srafan } 3027166124Srafan endwin(); 3028166124Srafan return 0; 3029166124Srafan} 3030166124Srafan 3031166124Srafan/* Put all the windows */ 3032166124Srafanvoid init_wins(WINDOW **wins, int n) 3033166124Srafan{ int x, y, i; 3034166124Srafan char label[80]; 3035166124Srafan 3036166124Srafan y = 2; 3037166124Srafan x = 10; 3038166124Srafan for(i = 0; i < n; ++i) 3039166124Srafan { wins[i] = newwin(NLINES, NCOLS, y, x); 3040166124Srafan sprintf(label, "Window Number %d", i + 1); 3041166124Srafan win_show(wins[i], label, i + 1); 3042166124Srafan y += 3; 3043166124Srafan x += 7; 3044166124Srafan } 3045166124Srafan} 3046166124Srafan 3047166124Srafan/* Show the window with a border and a label */ 3048166124Srafanvoid win_show(WINDOW *win, char *label, int label_color) 3049166124Srafan{ int startx, starty, height, width; 3050166124Srafan 3051166124Srafan getbegyx(win, starty, startx); 3052166124Srafan getmaxyx(win, height, width); 3053166124Srafan 3054166124Srafan box(win, 0, 0); 3055166124Srafan mvwaddch(win, 2, 0, ACS_LTEE); 3056166124Srafan mvwhline(win, 2, 1, ACS_HLINE, width - 2); 3057166124Srafan mvwaddch(win, 2, width - 1, ACS_RTEE); 3058166124Srafan 3059166124Srafan print_in_middle(win, 1, 0, width, label, COLOR_PAIR(label_color)); 3060166124Srafan} 3061166124Srafan 3062166124Srafanvoid print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) 3063166124Srafan{ int length, x, y; 3064166124Srafan float temp; 3065166124Srafan 3066166124Srafan if(win == NULL) 3067166124Srafan win = stdscr; 3068166124Srafan getyx(win, y, x); 3069166124Srafan if(startx != 0) 3070166124Srafan x = startx; 3071166124Srafan if(starty != 0) 3072166124Srafan y = starty; 3073166124Srafan if(width == 0) 3074166124Srafan width = 80; 3075166124Srafan 3076166124Srafan length = strlen(string); 3077166124Srafan temp = (width - length)/ 2; 3078166124Srafan x = startx + (int)temp; 3079166124Srafan wattron(win, color); 3080166124Srafan mvwprintw(win, y, x, "%s", string); 3081166124Srafan wattroff(win, color); 3082166124Srafan refresh(); 3083166124Srafan}</span></font> 3084166124Srafan</pre></td> 3085166124Srafan</tr> 3086166124Srafan</table> 3087166124Srafan</div> 3088166124Srafan</div> 3089166124Srafan<div class="SECT2"> 3090166124Srafan<hr> 3091166124Srafan<h3 class="SECT2"><a name="USERPTRUSING" id="USERPTRUSING">16.4. 3092166124SrafanUsing User Pointers</a></h3> 3093166124Srafan<p>In the above example I used user pointers to find out the next 3094166124Srafanwindow in the cycle. We can attach custom information to the panel 3095166124Srafanby specifying a user pointer, which can point to any information 3096166124Srafanyou want to store. In this case I stored the pointer to the next 3097166124Srafanpanel in the cycle. User pointer for a panel can be set with the 3098166124Srafanfunction <var class="LITERAL">set_panel_userptr()</var>. It can be 3099166124Srafanaccessed using the function <var class= 3100166124Srafan"LITERAL">panel_userptr()</var> which will return the user pointer 3101166124Srafanfor the panel given as argument. After finding the next panel in 3102166124Srafanthe cycle It's brought to the top by the function top_panel(). This 3103166124Srafanfunction brings the panel given as argument to the top of the panel 3104166124Srafanstack.</p> 3105166124Srafan</div> 3106166124Srafan<div class="SECT2"> 3107166124Srafan<hr> 3108166124Srafan<h3 class="SECT2"><a name="PANELMOVERESIZE" id= 3109166124Srafan"PANELMOVERESIZE">16.5. Moving and Resizing Panels</a></h3> 3110166124Srafan<p>The function <var class="LITERAL">move_panel()</var> can be used 3111166124Srafanto move a panel to the desired location. It does not change the 3112166124Srafanposition of the panel in the stack. Make sure that you use 3113166124Srafanmove_panel() instead mvwin() on the window associated with the 3114166124Srafanpanel.</p> 3115166124Srafan<p>Resizing a panel is slightly complex. There is no straight 3116166124Srafanforward function just to resize the window associated with a panel. 3117166124SrafanA solution to resize a panel is to create a new window with the 3118166124Srafandesired sizes, change the window associated with the panel using 3119166124Srafanreplace_panel(). Don't forget to delete the old window. The window 3120166124Srafanassociated with a panel can be found by using the function 3121166124Srafanpanel_window().</p> 3122166124Srafan<p>The following program shows these concepts, in supposedly simple 3123166124Srafanprogram. You can cycle through the window with <TAB> as 3124166124Srafanusual. To resize or move the active panel press 'r' for resize 'm' 3125166124Srafanfor moving. Then use arrow keys to resize or move it to the desired 3126166124Srafanway and press enter to end your resizing or moving. This example 3127166124Srafanmakes use of user data to get the required data to do the 3128166124Srafanoperations.</p> 3129166124Srafan<div class="EXAMPLE"><a name="PPARE" id="PPARE"></a> 3130166124Srafan<p><b>Example 16. Panel Moving and Resizing example</b></p> 3131166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 3132166124Srafan<tr> 3133166124Srafan<td> 3134166124Srafan<pre class="PROGRAMLISTING"> 3135166124Srafan<font color="#000000"><span class= 3136166124Srafan"INLINEMEDIAOBJECT">#include <panel.h> 3137166124Srafan 3138166124Srafantypedef struct _PANEL_DATA { 3139166124Srafan int x, y, w, h; 3140166124Srafan char label[80]; 3141166124Srafan int label_color; 3142166124Srafan PANEL *next; 3143166124Srafan}PANEL_DATA; 3144166124Srafan 3145166124Srafan#define NLINES 10 3146166124Srafan#define NCOLS 40 3147166124Srafan 3148166124Srafanvoid init_wins(WINDOW **wins, int n); 3149166124Srafanvoid win_show(WINDOW *win, char *label, int label_color); 3150166124Srafanvoid print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); 3151166124Srafanvoid set_user_ptrs(PANEL **panels, int n); 3152166124Srafan 3153166124Srafanint main() 3154166124Srafan{ WINDOW *my_wins[3]; 3155166124Srafan PANEL *my_panels[3]; 3156166124Srafan PANEL_DATA *top; 3157166124Srafan PANEL *stack_top; 3158166124Srafan WINDOW *temp_win, *old_win; 3159166124Srafan int ch; 3160166124Srafan int newx, newy, neww, newh; 3161166124Srafan int size = FALSE, move = FALSE; 3162166124Srafan 3163166124Srafan /* Initialize curses */ 3164166124Srafan initscr(); 3165166124Srafan start_color(); 3166166124Srafan cbreak(); 3167166124Srafan noecho(); 3168166124Srafan keypad(stdscr, TRUE); 3169166124Srafan 3170166124Srafan /* Initialize all the colors */ 3171166124Srafan init_pair(1, COLOR_RED, COLOR_BLACK); 3172166124Srafan init_pair(2, COLOR_GREEN, COLOR_BLACK); 3173166124Srafan init_pair(3, COLOR_BLUE, COLOR_BLACK); 3174166124Srafan init_pair(4, COLOR_CYAN, COLOR_BLACK); 3175166124Srafan 3176166124Srafan init_wins(my_wins, 3); 3177166124Srafan 3178166124Srafan /* Attach a panel to each window */ /* Order is bottom up */ 3179166124Srafan my_panels[0] = new_panel(my_wins[0]); /* Push 0, order: stdscr-0 */ 3180166124Srafan my_panels[1] = new_panel(my_wins[1]); /* Push 1, order: stdscr-0-1 */ 3181166124Srafan my_panels[2] = new_panel(my_wins[2]); /* Push 2, order: stdscr-0-1-2 */ 3182166124Srafan 3183166124Srafan set_user_ptrs(my_panels, 3); 3184166124Srafan /* Update the stacking order. 2nd panel will be on top */ 3185166124Srafan update_panels(); 3186166124Srafan 3187166124Srafan /* Show it on the screen */ 3188166124Srafan attron(COLOR_PAIR(4)); 3189166124Srafan mvprintw(LINES - 3, 0, "Use 'm' for moving, 'r' for resizing"); 3190166124Srafan mvprintw(LINES - 2, 0, "Use tab to browse through the windows (F1 to Exit)"); 3191166124Srafan attroff(COLOR_PAIR(4)); 3192166124Srafan doupdate(); 3193166124Srafan 3194166124Srafan stack_top = my_panels[2]; 3195166124Srafan top = (PANEL_DATA *)panel_userptr(stack_top); 3196166124Srafan newx = top->x; 3197166124Srafan newy = top->y; 3198166124Srafan neww = top->w; 3199166124Srafan newh = top->h; 3200166124Srafan while((ch = getch()) != KEY_F(1)) 3201166124Srafan { switch(ch) 3202166124Srafan { case 9: /* Tab */ 3203166124Srafan top = (PANEL_DATA *)panel_userptr(stack_top); 3204166124Srafan top_panel(top->next); 3205166124Srafan stack_top = top->next; 3206166124Srafan top = (PANEL_DATA *)panel_userptr(stack_top); 3207166124Srafan newx = top->x; 3208166124Srafan newy = top->y; 3209166124Srafan neww = top->w; 3210166124Srafan newh = top->h; 3211166124Srafan break; 3212166124Srafan case 'r': /* Re-Size*/ 3213166124Srafan size = TRUE; 3214166124Srafan attron(COLOR_PAIR(4)); 3215166124Srafan mvprintw(LINES - 4, 0, "Entered Resizing :Use Arrow Keys to resize and press <ENTER> to end resizing"); 3216166124Srafan refresh(); 3217166124Srafan attroff(COLOR_PAIR(4)); 3218166124Srafan break; 3219166124Srafan case 'm': /* Move */ 3220166124Srafan attron(COLOR_PAIR(4)); 3221166124Srafan mvprintw(LINES - 4, 0, "Entered Moving: Use Arrow Keys to Move and press <ENTER> to end moving"); 3222166124Srafan refresh(); 3223166124Srafan attroff(COLOR_PAIR(4)); 3224166124Srafan move = TRUE; 3225166124Srafan break; 3226166124Srafan case KEY_LEFT: 3227166124Srafan if(size == TRUE) 3228166124Srafan { --newx; 3229166124Srafan ++neww; 3230166124Srafan } 3231166124Srafan if(move == TRUE) 3232166124Srafan --newx; 3233166124Srafan break; 3234166124Srafan case KEY_RIGHT: 3235166124Srafan if(size == TRUE) 3236166124Srafan { ++newx; 3237166124Srafan --neww; 3238166124Srafan } 3239166124Srafan if(move == TRUE) 3240166124Srafan ++newx; 3241166124Srafan break; 3242166124Srafan case KEY_UP: 3243166124Srafan if(size == TRUE) 3244166124Srafan { --newy; 3245166124Srafan ++newh; 3246166124Srafan } 3247166124Srafan if(move == TRUE) 3248166124Srafan --newy; 3249166124Srafan break; 3250166124Srafan case KEY_DOWN: 3251166124Srafan if(size == TRUE) 3252166124Srafan { ++newy; 3253166124Srafan --newh; 3254166124Srafan } 3255166124Srafan if(move == TRUE) 3256166124Srafan ++newy; 3257166124Srafan break; 3258166124Srafan case 10: /* Enter */ 3259166124Srafan move(LINES - 4, 0); 3260166124Srafan clrtoeol(); 3261166124Srafan refresh(); 3262166124Srafan if(size == TRUE) 3263166124Srafan { old_win = panel_window(stack_top); 3264166124Srafan temp_win = newwin(newh, neww, newy, newx); 3265166124Srafan replace_panel(stack_top, temp_win); 3266166124Srafan win_show(temp_win, top->label, top->label_color); 3267166124Srafan delwin(old_win); 3268166124Srafan size = FALSE; 3269166124Srafan } 3270166124Srafan if(move == TRUE) 3271166124Srafan { move_panel(stack_top, newy, newx); 3272166124Srafan move = FALSE; 3273166124Srafan } 3274166124Srafan break; 3275166124Srafan 3276166124Srafan } 3277166124Srafan attron(COLOR_PAIR(4)); 3278166124Srafan mvprintw(LINES - 3, 0, "Use 'm' for moving, 'r' for resizing"); 3279166124Srafan mvprintw(LINES - 2, 0, "Use tab to browse through the windows (F1 to Exit)"); 3280166124Srafan attroff(COLOR_PAIR(4)); 3281166124Srafan refresh(); 3282166124Srafan update_panels(); 3283166124Srafan doupdate(); 3284166124Srafan } 3285166124Srafan endwin(); 3286166124Srafan return 0; 3287166124Srafan} 3288166124Srafan 3289166124Srafan/* Put all the windows */ 3290166124Srafanvoid init_wins(WINDOW **wins, int n) 3291166124Srafan{ int x, y, i; 3292166124Srafan char label[80]; 3293166124Srafan 3294166124Srafan y = 2; 3295166124Srafan x = 10; 3296166124Srafan for(i = 0; i < n; ++i) 3297166124Srafan { wins[i] = newwin(NLINES, NCOLS, y, x); 3298166124Srafan sprintf(label, "Window Number %d", i + 1); 3299166124Srafan win_show(wins[i], label, i + 1); 3300166124Srafan y += 3; 3301166124Srafan x += 7; 3302166124Srafan } 3303166124Srafan} 3304166124Srafan 3305166124Srafan/* Set the PANEL_DATA structures for individual panels */ 3306166124Srafanvoid set_user_ptrs(PANEL **panels, int n) 3307166124Srafan{ PANEL_DATA *ptrs; 3308166124Srafan WINDOW *win; 3309166124Srafan int x, y, w, h, i; 3310166124Srafan char temp[80]; 3311166124Srafan 3312166124Srafan ptrs = (PANEL_DATA *)calloc(n, sizeof(PANEL_DATA)); 3313166124Srafan 3314166124Srafan for(i = 0;i < n; ++i) 3315166124Srafan { win = panel_window(panels[i]); 3316166124Srafan getbegyx(win, y, x); 3317166124Srafan getmaxyx(win, h, w); 3318166124Srafan ptrs[i].x = x; 3319166124Srafan ptrs[i].y = y; 3320166124Srafan ptrs[i].w = w; 3321166124Srafan ptrs[i].h = h; 3322166124Srafan sprintf(temp, "Window Number %d", i + 1); 3323166124Srafan strcpy(ptrs[i].label, temp); 3324166124Srafan ptrs[i].label_color = i + 1; 3325166124Srafan if(i + 1 == n) 3326166124Srafan ptrs[i].next = panels[0]; 3327166124Srafan else 3328166124Srafan ptrs[i].next = panels[i + 1]; 3329166124Srafan set_panel_userptr(panels[i], &ptrs[i]); 3330166124Srafan } 3331166124Srafan} 3332166124Srafan 3333166124Srafan/* Show the window with a border and a label */ 3334166124Srafanvoid win_show(WINDOW *win, char *label, int label_color) 3335166124Srafan{ int startx, starty, height, width; 3336166124Srafan 3337166124Srafan getbegyx(win, starty, startx); 3338166124Srafan getmaxyx(win, height, width); 3339166124Srafan 3340166124Srafan box(win, 0, 0); 3341166124Srafan mvwaddch(win, 2, 0, ACS_LTEE); 3342166124Srafan mvwhline(win, 2, 1, ACS_HLINE, width - 2); 3343166124Srafan mvwaddch(win, 2, width - 1, ACS_RTEE); 3344166124Srafan 3345166124Srafan print_in_middle(win, 1, 0, width, label, COLOR_PAIR(label_color)); 3346166124Srafan} 3347166124Srafan 3348166124Srafanvoid print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) 3349166124Srafan{ int length, x, y; 3350166124Srafan float temp; 3351166124Srafan 3352166124Srafan if(win == NULL) 3353166124Srafan win = stdscr; 3354166124Srafan getyx(win, y, x); 3355166124Srafan if(startx != 0) 3356166124Srafan x = startx; 3357166124Srafan if(starty != 0) 3358166124Srafan y = starty; 3359166124Srafan if(width == 0) 3360166124Srafan width = 80; 3361166124Srafan 3362166124Srafan length = strlen(string); 3363166124Srafan temp = (width - length)/ 2; 3364166124Srafan x = startx + (int)temp; 3365166124Srafan wattron(win, color); 3366166124Srafan mvwprintw(win, y, x, "%s", string); 3367166124Srafan wattroff(win, color); 3368166124Srafan refresh(); 3369166124Srafan}</span></font> 3370166124Srafan</pre></td> 3371166124Srafan</tr> 3372166124Srafan</table> 3373166124Srafan</div> 3374166124Srafan<p>Concentrate on the main while loop. Once it finds out the type 3375166124Srafanof key pressed, it takes appropriate action. If 'r' is pressed 3376166124Srafanresizing mode is started. After this the new sizes are updated as 3377166124Srafanthe user presses the arrow keys. When the user presses 3378166124Srafan<ENTER> present selection ends and panel is resized by using 3379166124Srafanthe concept explained. While in resizing mode the program doesn't 3380166124Srafanshow how the window is getting resized. It's left as an exercise to 3381166124Srafanthe reader to print a dotted border while it gets resized to a new 3382166124Srafanposition.</p> 3383166124Srafan<p>When the user presses 'm' the move mode starts. This is a bit 3384166124Srafansimpler than resizing. As the arrow keys are pressed the new 3385166124Srafanposition is updated and pressing of <ENTER> causes the panel 3386166124Srafanto be moved by calling the function move_panel().</p> 3387166124Srafan<p>In this program the user data which is represented as 3388166124SrafanPANEL_DATA, plays very important role in finding the associated 3389166124Srafaninformation with a panel. As written in the comments, the 3390166124SrafanPANEL_DATA stores the panel sizes, label, label color and a pointer 3391166124Srafanto the next panel in the cycle.</p> 3392166124Srafan</div> 3393166124Srafan<div class="SECT2"> 3394166124Srafan<hr> 3395166124Srafan<h3 class="SECT2"><a name="PANELSHOWHIDE" id="PANELSHOWHIDE">16.6. 3396166124SrafanHiding and Showing Panels</a></h3> 3397166124Srafan<p>A Panel can be hidden by using the function hide_panel(). This 3398166124Srafanfunction merely removes it form the stack of panels, thus hiding it 3399166124Srafanon the screen once you do update_panels() and doupdate(). It 3400166124Srafandoesn't destroy the PANEL structure associated with the hidden 3401166124Srafanpanel. It can be shown again by using the show_panel() 3402166124Srafanfunction.</p> 3403166124Srafan<p>The following program shows the hiding of panels. Press 'a' or 3404166124Srafan'b' or 'c' to show or hide first, second and third windows 3405166124Srafanrespectively. It uses a user data with a small variable hide, which 3406166124Srafankeeps track of whether the window is hidden or not. For some reason 3407166124Srafanthe function <var class="LITERAL">panel_hidden()</var> which tells 3408166124Srafanwhether a panel is hidden or not is not working. A bug report was 3409166124Srafanalso presented by Michael Andres <a href= 3410166124Srafan"http://www.geocrawler.com/archives/3/344/1999/9/0/2643549/" 3411166124Srafantarget="_top">here</a></p> 3412166124Srafan<div class="EXAMPLE"><a name="PPAHI" id="PPAHI"></a> 3413166124Srafan<p><b>Example 17. Panel Hiding and Showing example</b></p> 3414166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 3415166124Srafan<tr> 3416166124Srafan<td> 3417166124Srafan<pre class="PROGRAMLISTING"> 3418166124Srafan<font color="#000000"><span class= 3419166124Srafan"INLINEMEDIAOBJECT">#include <panel.h> 3420166124Srafan 3421166124Srafantypedef struct _PANEL_DATA { 3422166124Srafan int hide; /* TRUE if panel is hidden */ 3423166124Srafan}PANEL_DATA; 3424166124Srafan 3425166124Srafan#define NLINES 10 3426166124Srafan#define NCOLS 40 3427166124Srafan 3428166124Srafanvoid init_wins(WINDOW **wins, int n); 3429166124Srafanvoid win_show(WINDOW *win, char *label, int label_color); 3430166124Srafanvoid print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); 3431166124Srafan 3432166124Srafanint main() 3433166124Srafan{ WINDOW *my_wins[3]; 3434166124Srafan PANEL *my_panels[3]; 3435166124Srafan PANEL_DATA panel_datas[3]; 3436166124Srafan PANEL_DATA *temp; 3437166124Srafan int ch; 3438166124Srafan 3439166124Srafan /* Initialize curses */ 3440166124Srafan initscr(); 3441166124Srafan start_color(); 3442166124Srafan cbreak(); 3443166124Srafan noecho(); 3444166124Srafan keypad(stdscr, TRUE); 3445166124Srafan 3446166124Srafan /* Initialize all the colors */ 3447166124Srafan init_pair(1, COLOR_RED, COLOR_BLACK); 3448166124Srafan init_pair(2, COLOR_GREEN, COLOR_BLACK); 3449166124Srafan init_pair(3, COLOR_BLUE, COLOR_BLACK); 3450166124Srafan init_pair(4, COLOR_CYAN, COLOR_BLACK); 3451166124Srafan 3452166124Srafan init_wins(my_wins, 3); 3453166124Srafan 3454166124Srafan /* Attach a panel to each window */ /* Order is bottom up */ 3455166124Srafan my_panels[0] = new_panel(my_wins[0]); /* Push 0, order: stdscr-0 */ 3456166124Srafan my_panels[1] = new_panel(my_wins[1]); /* Push 1, order: stdscr-0-1 */ 3457166124Srafan my_panels[2] = new_panel(my_wins[2]); /* Push 2, order: stdscr-0-1-2 */ 3458166124Srafan 3459166124Srafan /* Initialize panel datas saying that nothing is hidden */ 3460166124Srafan panel_datas[0].hide = FALSE; 3461166124Srafan panel_datas[1].hide = FALSE; 3462166124Srafan panel_datas[2].hide = FALSE; 3463166124Srafan 3464166124Srafan set_panel_userptr(my_panels[0], &panel_datas[0]); 3465166124Srafan set_panel_userptr(my_panels[1], &panel_datas[1]); 3466166124Srafan set_panel_userptr(my_panels[2], &panel_datas[2]); 3467166124Srafan 3468166124Srafan /* Update the stacking order. 2nd panel will be on top */ 3469166124Srafan update_panels(); 3470166124Srafan 3471166124Srafan /* Show it on the screen */ 3472166124Srafan attron(COLOR_PAIR(4)); 3473166124Srafan mvprintw(LINES - 3, 0, "Show or Hide a window with 'a'(first window) 'b'(Second Window) 'c'(Third Window)"); 3474166124Srafan mvprintw(LINES - 2, 0, "F1 to Exit"); 3475166124Srafan 3476166124Srafan attroff(COLOR_PAIR(4)); 3477166124Srafan doupdate(); 3478166124Srafan 3479166124Srafan while((ch = getch()) != KEY_F(1)) 3480166124Srafan { switch(ch) 3481166124Srafan { case 'a': 3482166124Srafan temp = (PANEL_DATA *)panel_userptr(my_panels[0]); 3483166124Srafan if(temp->hide == FALSE) 3484166124Srafan { hide_panel(my_panels[0]); 3485166124Srafan temp->hide = TRUE; 3486166124Srafan } 3487166124Srafan else 3488166124Srafan { show_panel(my_panels[0]); 3489166124Srafan temp->hide = FALSE; 3490166124Srafan } 3491166124Srafan break; 3492166124Srafan case 'b': 3493166124Srafan temp = (PANEL_DATA *)panel_userptr(my_panels[1]); 3494166124Srafan if(temp->hide == FALSE) 3495166124Srafan { hide_panel(my_panels[1]); 3496166124Srafan temp->hide = TRUE; 3497166124Srafan } 3498166124Srafan else 3499166124Srafan { show_panel(my_panels[1]); 3500166124Srafan temp->hide = FALSE; 3501166124Srafan } 3502166124Srafan break; 3503166124Srafan case 'c': 3504166124Srafan temp = (PANEL_DATA *)panel_userptr(my_panels[2]); 3505166124Srafan if(temp->hide == FALSE) 3506166124Srafan { hide_panel(my_panels[2]); 3507166124Srafan temp->hide = TRUE; 3508166124Srafan } 3509166124Srafan else 3510166124Srafan { show_panel(my_panels[2]); 3511166124Srafan temp->hide = FALSE; 3512166124Srafan } 3513166124Srafan break; 3514166124Srafan } 3515166124Srafan update_panels(); 3516166124Srafan doupdate(); 3517166124Srafan } 3518166124Srafan endwin(); 3519166124Srafan return 0; 3520166124Srafan} 3521166124Srafan 3522166124Srafan/* Put all the windows */ 3523166124Srafanvoid init_wins(WINDOW **wins, int n) 3524166124Srafan{ int x, y, i; 3525166124Srafan char label[80]; 3526166124Srafan 3527166124Srafan y = 2; 3528166124Srafan x = 10; 3529166124Srafan for(i = 0; i < n; ++i) 3530166124Srafan { wins[i] = newwin(NLINES, NCOLS, y, x); 3531166124Srafan sprintf(label, "Window Number %d", i + 1); 3532166124Srafan win_show(wins[i], label, i + 1); 3533166124Srafan y += 3; 3534166124Srafan x += 7; 3535166124Srafan } 3536166124Srafan} 3537166124Srafan 3538166124Srafan/* Show the window with a border and a label */ 3539166124Srafanvoid win_show(WINDOW *win, char *label, int label_color) 3540166124Srafan{ int startx, starty, height, width; 3541166124Srafan 3542166124Srafan getbegyx(win, starty, startx); 3543166124Srafan getmaxyx(win, height, width); 3544166124Srafan 3545166124Srafan box(win, 0, 0); 3546166124Srafan mvwaddch(win, 2, 0, ACS_LTEE); 3547166124Srafan mvwhline(win, 2, 1, ACS_HLINE, width - 2); 3548166124Srafan mvwaddch(win, 2, width - 1, ACS_RTEE); 3549166124Srafan 3550166124Srafan print_in_middle(win, 1, 0, width, label, COLOR_PAIR(label_color)); 3551166124Srafan} 3552166124Srafan 3553166124Srafanvoid print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) 3554166124Srafan{ int length, x, y; 3555166124Srafan float temp; 3556166124Srafan 3557166124Srafan if(win == NULL) 3558166124Srafan win = stdscr; 3559166124Srafan getyx(win, y, x); 3560166124Srafan if(startx != 0) 3561166124Srafan x = startx; 3562166124Srafan if(starty != 0) 3563166124Srafan y = starty; 3564166124Srafan if(width == 0) 3565166124Srafan width = 80; 3566166124Srafan 3567166124Srafan length = strlen(string); 3568166124Srafan temp = (width - length)/ 2; 3569166124Srafan x = startx + (int)temp; 3570166124Srafan wattron(win, color); 3571166124Srafan mvwprintw(win, y, x, "%s", string); 3572166124Srafan wattroff(win, color); 3573166124Srafan refresh(); 3574166124Srafan}</span></font> 3575166124Srafan</pre></td> 3576166124Srafan</tr> 3577166124Srafan</table> 3578166124Srafan</div> 3579166124Srafan</div> 3580166124Srafan<div class="SECT2"> 3581166124Srafan<hr> 3582166124Srafan<h3 class="SECT2"><a name="PANELABOVE" id="PANELABOVE">16.7. 3583166124Srafanpanel_above() and panel_below() Functions</a></h3> 3584166124Srafan<p>The functions <var class="LITERAL">panel_above()</var> and 3585166124Srafan<var class="LITERAL">panel_below()</var> can be used to find out 3586166124Srafanthe panel above and below a panel. If the argument to these 3587166124Srafanfunctions is NULL, then they return a pointer to bottom panel and 3588166124Srafantop panel respectively.</p> 3589166124Srafan</div> 3590166124Srafan</div> 3591166124Srafan<div class="SECT1"> 3592166124Srafan<hr> 3593166124Srafan<h2 class="SECT1"><a name="MENUS" id="MENUS">17. Menus 3594166124SrafanLibrary</a></h2> 3595166124Srafan<p>The menus library provides a nice extension to basic curses, 3596166124Srafanthrough which you can create menus. It provides a set of functions 3597166124Srafanto create menus. But they have to be customized to give a nicer 3598166124Srafanlook, with colors etc. Let's get into the details.</p> 3599166124Srafan<p>A menu is a screen display that assists the user to choose some 3600166124Srafansubset of a given set of items. To put it simple, a menu is a 3601166124Srafancollection of items from which one or more items can be chosen. 3602166124SrafanSome readers might not be aware of multiple item selection 3603166124Srafancapability. Menu library provides functionality to write menus from 3604166124Srafanwhich the user can chose more than one item as the preferred 3605166124Srafanchoice. This is dealt with in a later section. Now it is time for 3606166124Srafansome rudiments.</p> 3607166124Srafan<div class="SECT2"> 3608166124Srafan<hr> 3609166124Srafan<h3 class="SECT2"><a name="MENUBASICS" id="MENUBASICS">17.1. The 3610166124SrafanBasics</a></h3> 3611166124Srafan<p>To create menus, you first create items, and then post the menu 3612166124Srafanto the display. After that, all the processing of user responses is 3613166124Srafandone in an elegant function menu_driver() which is the work horse 3614166124Srafanof any menu program.</p> 3615166124Srafan<p>The general flow of control of a menu program looks like 3616166124Srafanthis.</p> 3617166124Srafan<ol type="1"> 3618166124Srafan<li> 3619166124Srafan<p>Initialize curses</p> 3620166124Srafan</li> 3621166124Srafan<li> 3622166124Srafan<p>Create items using new_item(). You can specify a name and 3623166124Srafandescription for the items.</p> 3624166124Srafan</li> 3625166124Srafan<li> 3626166124Srafan<p>Create the menu with new_menu() by specifying the items to be 3627166124Srafanattached with.</p> 3628166124Srafan</li> 3629166124Srafan<li> 3630166124Srafan<p>Post the menu with menu_post() and refresh the screen.</p> 3631166124Srafan</li> 3632166124Srafan<li> 3633166124Srafan<p>Process the user requests with a loop and do necessary updates 3634166124Srafanto menu with menu_driver.</p> 3635166124Srafan</li> 3636166124Srafan<li> 3637166124Srafan<p>Unpost the menu with menu_unpost()</p> 3638166124Srafan</li> 3639166124Srafan<li> 3640166124Srafan<p>Free the memory allocated to menu by free_menu()</p> 3641166124Srafan</li> 3642166124Srafan<li> 3643166124Srafan<p>Free the memory allocated to the items with free_item()</p> 3644166124Srafan</li> 3645166124Srafan<li> 3646166124Srafan<p>End curses</p> 3647166124Srafan</li> 3648166124Srafan</ol> 3649166124Srafan<p>Let's see a program which prints a simple menu and updates the 3650166124Srafancurrent selection with up, down arrows.</p> 3651166124Srafan</div> 3652166124Srafan<div class="SECT2"> 3653166124Srafan<hr> 3654166124Srafan<h3 class="SECT2"><a name="COMPILEMENUS" id="COMPILEMENUS">17.2. 3655166124SrafanCompiling With the Menu Library</a></h3> 3656166124Srafan<p>To use menu library functions, you have to include menu.h and to 3657166124Srafanlink the program with menu library the flag -lmenu should be added 3658166124Srafanalong with -lncurses in that order.</p> 3659166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 3660166124Srafan<tr> 3661166124Srafan<td> 3662166124Srafan<pre class="PROGRAMLISTING"> 3663166124Srafan<font color="#000000"> #include <menu.h> 3664166124Srafan . 3665166124Srafan . 3666166124Srafan . 3667166124Srafan 3668166124Srafan compile and link: gcc <program file> -lmenu -lncurses</font> 3669166124Srafan</pre></td> 3670166124Srafan</tr> 3671166124Srafan</table> 3672166124Srafan<div class="EXAMPLE"><a name="MMESI" id="MMESI"></a> 3673166124Srafan<p><b>Example 18. Menu Basics</b></p> 3674166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 3675166124Srafan<tr> 3676166124Srafan<td> 3677166124Srafan<pre class="PROGRAMLISTING"> 3678166124Srafan<font color="#000000"><span class= 3679166124Srafan"INLINEMEDIAOBJECT">#include <curses.h> 3680166124Srafan#include <menu.h> 3681166124Srafan 3682166124Srafan#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) 3683166124Srafan#define CTRLD 4 3684166124Srafan 3685166124Srafanchar *choices[] = { 3686166124Srafan "Choice 1", 3687166124Srafan "Choice 2", 3688166124Srafan "Choice 3", 3689166124Srafan "Choice 4", 3690166124Srafan "Exit", 3691166124Srafan }; 3692166124Srafan 3693166124Srafanint main() 3694166124Srafan{ ITEM **my_items; 3695166124Srafan int c; 3696166124Srafan MENU *my_menu; 3697166124Srafan int n_choices, i; 3698166124Srafan ITEM *cur_item; 3699166124Srafan 3700166124Srafan 3701166124Srafan initscr(); 3702166124Srafan cbreak(); 3703166124Srafan noecho(); 3704166124Srafan keypad(stdscr, TRUE); 3705166124Srafan 3706166124Srafan n_choices = ARRAY_SIZE(choices); 3707166124Srafan my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); 3708166124Srafan 3709166124Srafan for(i = 0; i < n_choices; ++i) 3710166124Srafan my_items[i] = new_item(choices[i], choices[i]); 3711166124Srafan my_items[n_choices] = (ITEM *)NULL; 3712166124Srafan 3713166124Srafan my_menu = new_menu((ITEM **)my_items); 3714166124Srafan mvprintw(LINES - 2, 0, "F1 to Exit"); 3715166124Srafan post_menu(my_menu); 3716166124Srafan refresh(); 3717166124Srafan 3718166124Srafan while((c = getch()) != KEY_F(1)) 3719166124Srafan { switch(c) 3720166124Srafan { case KEY_DOWN: 3721166124Srafan menu_driver(my_menu, REQ_DOWN_ITEM); 3722166124Srafan break; 3723166124Srafan case KEY_UP: 3724166124Srafan menu_driver(my_menu, REQ_UP_ITEM); 3725166124Srafan break; 3726166124Srafan } 3727166124Srafan } 3728166124Srafan 3729166124Srafan free_item(my_items[0]); 3730166124Srafan free_item(my_items[1]); 3731166124Srafan free_menu(my_menu); 3732166124Srafan endwin(); 3733166124Srafan} 3734166124Srafan </span></font> 3735166124Srafan</pre></td> 3736166124Srafan</tr> 3737166124Srafan</table> 3738166124Srafan</div> 3739166124Srafan<p>This program demonstrates the basic concepts involved in 3740166124Srafancreating a menu using menus library. First we create the items 3741166124Srafanusing new_item() and then attach them to the menu with new_menu() 3742166124Srafanfunction. After posting the menu and refreshing the screen, the 3743166124Srafanmain processing loop starts. It reads user input and takes 3744166124Srafancorresponding action. The function menu_driver() is the main work 3745166124Srafanhorse of the menu system. The second parameter to this function 3746166124Srafantells what's to be done with the menu. According to the parameter, 3747166124Srafanmenu_driver() does the corresponding task. The value can be either 3748166124Srafana menu navigational request, an ascii character, or a KEY_MOUSE 3749166124Srafanspecial key associated with a mouse event.</p> 3750166124Srafan<p>The menu_driver accepts following navigational requests.</p> 3751166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 3752166124Srafan<tr> 3753166124Srafan<td> 3754166124Srafan<pre class="PROGRAMLISTING"> 3755166124Srafan<font color= 3756166124Srafan"#000000"> REQ_LEFT_ITEM Move left to an item. 3757166124Srafan REQ_RIGHT_ITEM Move right to an item. 3758166124Srafan REQ_UP_ITEM Move up to an item. 3759166124Srafan REQ_DOWN_ITEM Move down to an item. 3760166124Srafan REQ_SCR_ULINE Scroll up a line. 3761166124Srafan REQ_SCR_DLINE Scroll down a line. 3762166124Srafan REQ_SCR_DPAGE Scroll down a page. 3763166124Srafan REQ_SCR_UPAGE Scroll up a page. 3764166124Srafan REQ_FIRST_ITEM Move to the first item. 3765166124Srafan REQ_LAST_ITEM Move to the last item. 3766166124Srafan REQ_NEXT_ITEM Move to the next item. 3767166124Srafan REQ_PREV_ITEM Move to the previous item. 3768166124Srafan REQ_TOGGLE_ITEM Select/deselect an item. 3769166124Srafan REQ_CLEAR_PATTERN Clear the menu pattern buffer. 3770166124Srafan REQ_BACK_PATTERN Delete the previous character from the pattern buffer. 3771166124Srafan REQ_NEXT_MATCH Move to the next item matching the pattern match. 3772166124Srafan REQ_PREV_MATCH Move to the previous item matching the pattern match. </font> 3773166124Srafan</pre></td> 3774166124Srafan</tr> 3775166124Srafan</table> 3776166124Srafan<p>Don't get overwhelmed by the number of options. We will see them 3777166124Srafanslowly one after another. The options of interest in this example 3778166124Srafanare REQ_UP_ITEM and REQ_DOWN_ITEM. These two options when passed to 3779166124Srafanmenu_driver, menu driver updates the current item to one item up or 3780166124Srafandown respectively.</p> 3781166124Srafan</div> 3782166124Srafan<div class="SECT2"> 3783166124Srafan<hr> 3784166124Srafan<h3 class="SECT2"><a name="MENUDRIVER" id="MENUDRIVER">17.3. Menu 3785166124SrafanDriver: The work horse of the menu system</a></h3> 3786166124Srafan<p>As you have seen in the above example, menu_driver plays an 3787166124Srafanimportant role in updating the menu. It is very important to 3788166124Srafanunderstand various options it takes and what they do. As explained 3789166124Srafanabove, the second parameter to menu_driver() can be either a 3790166124Srafannavigational request, a printable character or a KEY_MOUSE key. 3791166124SrafanLet's dissect the different navigational requests.</p> 3792166124Srafan<ul> 3793166124Srafan<li> 3794166124Srafan<p><em>REQ_LEFT_ITEM and REQ_RIGHT_ITEM</em></p> 3795166124Srafan<p>A Menu can be displayed with multiple columns for more than one 3796166124Srafanitem. This can be done by using the <var class= 3797166124Srafan"LITERAL">menu_format()</var>function. When a multi columnar menu 3798166124Srafanis displayed these requests cause the menu driver to move the 3799166124Srafancurrent selection to left or right.</p> 3800166124Srafan</li> 3801166124Srafan<li> 3802166124Srafan<p><em>REQ_UP_ITEM and REQ_DOWN_ITEM</em></p> 3803166124Srafan<p>These two options you have seen in the above example. These 3804166124Srafanoptions when given, makes the menu_driver to move the current 3805166124Srafanselection to an item up or down.</p> 3806166124Srafan</li> 3807166124Srafan<li> 3808166124Srafan<p><em>REQ_SCR_* options</em></p> 3809166124Srafan<p>The four options REQ_SCR_ULINE, REQ_SCR_DLINE, REQ_SCR_DPAGE, 3810166124SrafanREQ_SCR_UPAGE are related to scrolling. If all the items in the 3811166124Srafanmenu cannot be displayed in the menu sub window, then the menu is 3812166124Srafanscrollable. These requests can be given to the menu_driver to do 3813166124Srafanthe scrolling either one line up, down or one page down or up 3814166124Srafanrespectively.</p> 3815166124Srafan</li> 3816166124Srafan<li> 3817166124Srafan<p><em>REQ_FIRST_ITEM, REQ_LAST_ITEM, REQ_NEXT_ITEM and 3818166124SrafanREQ_PREV_ITEM</em></p> 3819166124Srafan<p>These requests are self explanatory.</p> 3820166124Srafan</li> 3821166124Srafan<li> 3822166124Srafan<p><em>REQ_TOGGLE_ITEM</em></p> 3823166124Srafan<p>This request when given, toggles the present selection. This 3824166124Srafanoption is to be used only in a multi valued menu. So to use this 3825166124Srafanrequest the option O_ONEVALUE must be off. This option can be made 3826166124Srafanoff or on with set_menu_opts().</p> 3827166124Srafan</li> 3828166124Srafan<li> 3829166124Srafan<p><em>Pattern Requests</em></p> 3830166124Srafan<p>Every menu has an associated pattern buffer, which is used to 3831166124Srafanfind the nearest match to the ascii characters entered by the user. 3832166124SrafanWhenever ascii characters are given to menu_driver, it puts in to 3833166124Srafanthe pattern buffer. It also tries to find the nearest match to the 3834166124Srafanpattern in the items list and moves current selection to that item. 3835166124SrafanThe request REQ_CLEAR_PATTERN clears the pattern buffer. The 3836166124Srafanrequest REQ_BACK_PATTERN deletes the previous character in the 3837166124Srafanpattern buffer. In case the pattern matches more than one item then 3838166124Srafanthe matched items can be cycled through REQ_NEXT_MATCH and 3839166124SrafanREQ_PREV_MATCH which move the current selection to the next and 3840166124Srafanprevious matches respectively.</p> 3841166124Srafan</li> 3842166124Srafan<li> 3843166124Srafan<p><em>Mouse Requests</em></p> 3844166124Srafan<p>In case of KEY_MOUSE requests, according to the mouse position 3845166124Srafanan action is taken accordingly. The action to be taken is explained 3846166124Srafanin the man page as,</p> 3847166124Srafan<table border="0" bgcolor="#E0E0E0" width="90%"> 3848166124Srafan<tr> 3849166124Srafan<td> 3850166124Srafan<pre class="PROGRAMLISTING"> 3851166124Srafan<font color= 3852166124Srafan"#000000"><em> If the second argument is the KEY_MOUSE special key, the 3853166124Srafan associated mouse event is translated into one of the above 3854166124Srafan pre-defined requests. Currently only clicks in the user 3855166124Srafan window (e.g. inside the menu display area or the decora­ 3856166124Srafan tion window) are handled. If you click above the display 3857166124Srafan region of the menu, a REQ_SCR_ULINE is generated, if you 3858166124Srafan doubleclick a REQ_SCR_UPAGE is generated and if you 3859166124Srafan tripleclick a REQ_FIRST_ITEM is generated. If you click 3860166124Srafan below the display region of the menu, a REQ_SCR_DLINE is 3861166124Srafan generated, if you doubleclick a REQ_SCR_DPAGE is generated 3862166124Srafan and if you tripleclick a REQ_LAST_ITEM is generated. If 3863166124Srafan you click at an item inside the display area of the menu, 3864166124Srafan the menu cursor is positioned to that item.</em></font> 3865166124Srafan</pre></td> 3866166124Srafan</tr> 3867166124Srafan</table> 3868166124Srafan</li> 3869166124Srafan</ul> 3870166124Srafan<p>Each of the above requests will be explained in the following 3871166124Srafanlines with several examples whenever appropriate.</p> 3872166124Srafan</div> 3873166124Srafan<div class="SECT2"> 3874166124Srafan<hr> 3875166124Srafan<h3 class="SECT2"><a name="MENUWINDOWS" id="MENUWINDOWS">17.4. Menu 3876166124SrafanWindows</a></h3> 3877166124Srafan<p>Every menu created is associated with a window and a sub window. 3878166124SrafanThe menu window displays any title or border associated with the 3879166124Srafanmenu. The menu sub window displays the menu items currently 3880166124Srafanavailable for selection. But we didn't specify any window or sub 3881166124Srafanwindow in the simple example. When a window is not specified, 3882166124Srafanstdscr is taken as the main window, and then menu system calculates 3883166124Srafanthe sub window size required for the display of items. Then items 3884166124Srafanare displayed in the calculated sub window. So let's play with 3885166124Srafanthese windows and display a menu with a border and a title.</p> 3886166124Srafan<div class="EXAMPLE"><a name="MMEWI" id="MMEWI"></a> 3887166124Srafan<p><b>Example 19. Menu Windows Usage example</b></p> 3888166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 3889166124Srafan<tr> 3890166124Srafan<td> 3891166124Srafan<pre class="PROGRAMLISTING"> 3892166124Srafan<font color="#000000"><span class= 3893166124Srafan"INLINEMEDIAOBJECT">#include <menu.h> 3894166124Srafan 3895166124Srafan#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) 3896166124Srafan#define CTRLD 4 3897166124Srafan 3898166124Srafanchar *choices[] = { 3899166124Srafan "Choice 1", 3900166124Srafan "Choice 2", 3901166124Srafan "Choice 3", 3902166124Srafan "Choice 4", 3903166124Srafan "Exit", 3904166124Srafan (char *)NULL, 3905166124Srafan }; 3906166124Srafanvoid print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); 3907166124Srafan 3908166124Srafanint main() 3909166124Srafan{ ITEM **my_items; 3910166124Srafan int c; 3911166124Srafan MENU *my_menu; 3912166124Srafan WINDOW *my_menu_win; 3913166124Srafan int n_choices, i; 3914166124Srafan 3915166124Srafan /* Initialize curses */ 3916166124Srafan initscr(); 3917166124Srafan start_color(); 3918166124Srafan cbreak(); 3919166124Srafan noecho(); 3920166124Srafan keypad(stdscr, TRUE); 3921166124Srafan init_pair(1, COLOR_RED, COLOR_BLACK); 3922166124Srafan 3923166124Srafan /* Create items */ 3924166124Srafan n_choices = ARRAY_SIZE(choices); 3925166124Srafan my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *)); 3926166124Srafan for(i = 0; i < n_choices; ++i) 3927166124Srafan my_items[i] = new_item(choices[i], choices[i]); 3928166124Srafan 3929166124Srafan /* Crate menu */ 3930166124Srafan my_menu = new_menu((ITEM **)my_items); 3931166124Srafan 3932166124Srafan /* Create the window to be associated with the menu */ 3933166124Srafan my_menu_win = newwin(10, 40, 4, 4); 3934166124Srafan keypad(my_menu_win, TRUE); 3935166124Srafan 3936166124Srafan /* Set main window and sub window */ 3937166124Srafan set_menu_win(my_menu, my_menu_win); 3938166124Srafan set_menu_sub(my_menu, derwin(my_menu_win, 6, 38, 3, 1)); 3939166124Srafan 3940166124Srafan /* Set menu mark to the string " * " */ 3941166124Srafan set_menu_mark(my_menu, " * "); 3942166124Srafan 3943166124Srafan /* Print a border around the main window and print a title */ 3944166124Srafan box(my_menu_win, 0, 0); 3945166124Srafan print_in_middle(my_menu_win, 1, 0, 40, "My Menu", COLOR_PAIR(1)); 3946166124Srafan mvwaddch(my_menu_win, 2, 0, ACS_LTEE); 3947166124Srafan mvwhline(my_menu_win, 2, 1, ACS_HLINE, 38); 3948166124Srafan mvwaddch(my_menu_win, 2, 39, ACS_RTEE); 3949166124Srafan mvprintw(LINES - 2, 0, "F1 to exit"); 3950166124Srafan refresh(); 3951166124Srafan 3952166124Srafan /* Post the menu */ 3953166124Srafan post_menu(my_menu); 3954166124Srafan wrefresh(my_menu_win); 3955166124Srafan 3956166124Srafan while((c = wgetch(my_menu_win)) != KEY_F(1)) 3957166124Srafan { switch(c) 3958166124Srafan { case KEY_DOWN: 3959166124Srafan menu_driver(my_menu, REQ_DOWN_ITEM); 3960166124Srafan break; 3961166124Srafan case KEY_UP: 3962166124Srafan menu_driver(my_menu, REQ_UP_ITEM); 3963166124Srafan break; 3964166124Srafan } 3965166124Srafan wrefresh(my_menu_win); 3966166124Srafan } 3967166124Srafan 3968166124Srafan /* Unpost and free all the memory taken up */ 3969166124Srafan unpost_menu(my_menu); 3970166124Srafan free_menu(my_menu); 3971166124Srafan for(i = 0; i < n_choices; ++i) 3972166124Srafan free_item(my_items[i]); 3973166124Srafan endwin(); 3974166124Srafan} 3975166124Srafan 3976166124Srafanvoid print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) 3977166124Srafan{ int length, x, y; 3978166124Srafan float temp; 3979166124Srafan 3980166124Srafan if(win == NULL) 3981166124Srafan win = stdscr; 3982166124Srafan getyx(win, y, x); 3983166124Srafan if(startx != 0) 3984166124Srafan x = startx; 3985166124Srafan if(starty != 0) 3986166124Srafan y = starty; 3987166124Srafan if(width == 0) 3988166124Srafan width = 80; 3989166124Srafan 3990166124Srafan length = strlen(string); 3991166124Srafan temp = (width - length)/ 2; 3992166124Srafan x = startx + (int)temp; 3993166124Srafan wattron(win, color); 3994166124Srafan mvwprintw(win, y, x, "%s", string); 3995166124Srafan wattroff(win, color); 3996166124Srafan refresh(); 3997166124Srafan}</span></font> 3998166124Srafan</pre></td> 3999166124Srafan</tr> 4000166124Srafan</table> 4001166124Srafan</div> 4002166124Srafan<p>This example creates a menu with a title, border, a fancy line 4003166124Srafanseparating title and the items. As you can see, in order to attach 4004166124Srafana window to a menu the function set_menu_win() has to be used. Then 4005166124Srafanwe attach the sub window also. This displays the items in the sub 4006166124Srafanwindow. You can also set the mark string which gets displayed to 4007166124Srafanthe left of the selected item with set_menu_mark().</p> 4008166124Srafan</div> 4009166124Srafan<div class="SECT2"> 4010166124Srafan<hr> 4011166124Srafan<h3 class="SECT2"><a name="SCROLLMENUS" id="SCROLLMENUS">17.5. 4012166124SrafanScrolling Menus</a></h3> 4013166124Srafan<p>If the sub window given for a window is not big enough to show 4014166124Srafanall the items, then the menu will be scrollable. When you are on 4015166124Srafanthe last item in the present list, if you send REQ_DOWN_ITEM, it 4016166124Srafangets translated into REQ_SCR_DLINE and the menu scrolls by one 4017166124Srafanitem. You can manually give REQ_SCR_ operations to do scrolling. 4018166124SrafanLet's see how it can be done.</p> 4019166124Srafan<div class="EXAMPLE"><a name="MMESC" id="MMESC"></a> 4020166124Srafan<p><b>Example 20. Scrolling Menus example</b></p> 4021166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 4022166124Srafan<tr> 4023166124Srafan<td> 4024166124Srafan<pre class="PROGRAMLISTING"> 4025166124Srafan<font color="#000000"><span class= 4026166124Srafan"INLINEMEDIAOBJECT">#include <curses.h> 4027166124Srafan#include <menu.h> 4028166124Srafan 4029166124Srafan#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) 4030166124Srafan#define CTRLD 4 4031166124Srafan 4032166124Srafanchar *choices[] = { 4033166124Srafan "Choice 1", 4034166124Srafan "Choice 2", 4035166124Srafan "Choice 3", 4036166124Srafan "Choice 4", 4037166124Srafan "Choice 5", 4038166124Srafan "Choice 6", 4039166124Srafan "Choice 7", 4040166124Srafan "Choice 8", 4041166124Srafan "Choice 9", 4042166124Srafan "Choice 10", 4043166124Srafan "Exit", 4044166124Srafan (char *)NULL, 4045166124Srafan }; 4046166124Srafanvoid print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); 4047166124Srafan 4048166124Srafanint main() 4049166124Srafan{ ITEM **my_items; 4050166124Srafan int c; 4051166124Srafan MENU *my_menu; 4052166124Srafan WINDOW *my_menu_win; 4053166124Srafan int n_choices, i; 4054166124Srafan 4055166124Srafan /* Initialize curses */ 4056166124Srafan initscr(); 4057166124Srafan start_color(); 4058166124Srafan cbreak(); 4059166124Srafan noecho(); 4060166124Srafan keypad(stdscr, TRUE); 4061166124Srafan init_pair(1, COLOR_RED, COLOR_BLACK); 4062166124Srafan init_pair(2, COLOR_CYAN, COLOR_BLACK); 4063166124Srafan 4064166124Srafan /* Create items */ 4065166124Srafan n_choices = ARRAY_SIZE(choices); 4066166124Srafan my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *)); 4067166124Srafan for(i = 0; i < n_choices; ++i) 4068166124Srafan my_items[i] = new_item(choices[i], choices[i]); 4069166124Srafan 4070166124Srafan /* Crate menu */ 4071166124Srafan my_menu = new_menu((ITEM **)my_items); 4072166124Srafan 4073166124Srafan /* Create the window to be associated with the menu */ 4074166124Srafan my_menu_win = newwin(10, 40, 4, 4); 4075166124Srafan keypad(my_menu_win, TRUE); 4076166124Srafan 4077166124Srafan /* Set main window and sub window */ 4078166124Srafan set_menu_win(my_menu, my_menu_win); 4079166124Srafan set_menu_sub(my_menu, derwin(my_menu_win, 6, 38, 3, 1)); 4080166124Srafan set_menu_format(my_menu, 5, 1); 4081166124Srafan 4082166124Srafan /* Set menu mark to the string " * " */ 4083166124Srafan set_menu_mark(my_menu, " * "); 4084166124Srafan 4085166124Srafan /* Print a border around the main window and print a title */ 4086166124Srafan box(my_menu_win, 0, 0); 4087166124Srafan print_in_middle(my_menu_win, 1, 0, 40, "My Menu", COLOR_PAIR(1)); 4088166124Srafan mvwaddch(my_menu_win, 2, 0, ACS_LTEE); 4089166124Srafan mvwhline(my_menu_win, 2, 1, ACS_HLINE, 38); 4090166124Srafan mvwaddch(my_menu_win, 2, 39, ACS_RTEE); 4091166124Srafan 4092166124Srafan /* Post the menu */ 4093166124Srafan post_menu(my_menu); 4094166124Srafan wrefresh(my_menu_win); 4095166124Srafan 4096166124Srafan attron(COLOR_PAIR(2)); 4097166124Srafan mvprintw(LINES - 2, 0, "Use PageUp and PageDown to scoll down or up a page of items"); 4098166124Srafan mvprintw(LINES - 1, 0, "Arrow Keys to navigate (F1 to Exit)"); 4099166124Srafan attroff(COLOR_PAIR(2)); 4100166124Srafan refresh(); 4101166124Srafan 4102166124Srafan while((c = wgetch(my_menu_win)) != KEY_F(1)) 4103166124Srafan { switch(c) 4104166124Srafan { case KEY_DOWN: 4105166124Srafan menu_driver(my_menu, REQ_DOWN_ITEM); 4106166124Srafan break; 4107166124Srafan case KEY_UP: 4108166124Srafan menu_driver(my_menu, REQ_UP_ITEM); 4109166124Srafan break; 4110166124Srafan case KEY_NPAGE: 4111166124Srafan menu_driver(my_menu, REQ_SCR_DPAGE); 4112166124Srafan break; 4113166124Srafan case KEY_PPAGE: 4114166124Srafan menu_driver(my_menu, REQ_SCR_UPAGE); 4115166124Srafan break; 4116166124Srafan } 4117166124Srafan wrefresh(my_menu_win); 4118166124Srafan } 4119166124Srafan 4120166124Srafan /* Unpost and free all the memory taken up */ 4121166124Srafan unpost_menu(my_menu); 4122166124Srafan free_menu(my_menu); 4123166124Srafan for(i = 0; i < n_choices; ++i) 4124166124Srafan free_item(my_items[i]); 4125166124Srafan endwin(); 4126166124Srafan} 4127166124Srafan 4128166124Srafanvoid print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) 4129166124Srafan{ int length, x, y; 4130166124Srafan float temp; 4131166124Srafan 4132166124Srafan if(win == NULL) 4133166124Srafan win = stdscr; 4134166124Srafan getyx(win, y, x); 4135166124Srafan if(startx != 0) 4136166124Srafan x = startx; 4137166124Srafan if(starty != 0) 4138166124Srafan y = starty; 4139166124Srafan if(width == 0) 4140166124Srafan width = 80; 4141166124Srafan 4142166124Srafan length = strlen(string); 4143166124Srafan temp = (width - length)/ 2; 4144166124Srafan x = startx + (int)temp; 4145166124Srafan wattron(win, color); 4146166124Srafan mvwprintw(win, y, x, "%s", string); 4147166124Srafan wattroff(win, color); 4148166124Srafan refresh(); 4149166124Srafan}</span></font> 4150166124Srafan</pre></td> 4151166124Srafan</tr> 4152166124Srafan</table> 4153166124Srafan</div> 4154166124Srafan<p>This program is self-explanatory. In this example the number of 4155166124Srafanchoices has been increased to ten, which is larger than our sub 4156166124Srafanwindow size which can hold 6 items. This message has to be 4157166124Srafanexplicitly conveyed to the menu system with the function 4158166124Srafanset_menu_format(). In here we specify the number of rows and 4159166124Srafancolumns we want to be displayed for a single page. We can specify 4160166124Srafanany number of items to be shown, in the rows variables, if it is 4161166124Srafanless than the height of the sub window. If the key pressed by the 4162166124Srafanuser is a PAGE UP or PAGE DOWN, the menu is scrolled a page due to 4163166124Srafanthe requests (REQ_SCR_DPAGE and REQ_SCR_UPAGE) given to 4164166124Srafanmenu_driver().</p> 4165166124Srafan</div> 4166166124Srafan<div class="SECT2"> 4167166124Srafan<hr> 4168166124Srafan<h3 class="SECT2"><a name="MULTICOLUMN" id="MULTICOLUMN">17.6. 4169166124SrafanMulti Columnar Menus</a></h3> 4170166124Srafan<p>In the above example you have seen how to use the function 4171166124Srafanset_menu_format(). I didn't mention what the cols variable (third 4172166124Srafanparameter) does. Well, If your sub window is wide enough, you can 4173166124Srafanopt to display more than one item per row. This can be specified in 4174166124Srafanthe cols variable. To make things simpler, the following example 4175166124Srafandoesn't show descriptions for the items.</p> 4176166124Srafan<div class="EXAMPLE"><a name="MMEMUCO" id="MMEMUCO"></a> 4177166124Srafan<p><b>Example 21. Milt Columnar Menus Example</b></p> 4178166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 4179166124Srafan<tr> 4180166124Srafan<td> 4181166124Srafan<pre class="PROGRAMLISTING"> 4182166124Srafan<font color="#000000"><span class= 4183166124Srafan"INLINEMEDIAOBJECT">#include <curses.h> 4184166124Srafan#include <menu.h> 4185166124Srafan 4186166124Srafan#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) 4187166124Srafan#define CTRLD 4 4188166124Srafan 4189166124Srafanchar *choices[] = { 4190166124Srafan "Choice 1", "Choice 2", "Choice 3", "Choice 4", "Choice 5", 4191166124Srafan "Choice 6", "Choice 7", "Choice 8", "Choice 9", "Choice 10", 4192166124Srafan "Choice 11", "Choice 12", "Choice 13", "Choice 14", "Choice 15", 4193166124Srafan "Choice 16", "Choice 17", "Choice 18", "Choice 19", "Choice 20", 4194166124Srafan "Exit", 4195166124Srafan (char *)NULL, 4196166124Srafan }; 4197166124Srafan 4198166124Srafanint main() 4199166124Srafan{ ITEM **my_items; 4200166124Srafan int c; 4201166124Srafan MENU *my_menu; 4202166124Srafan WINDOW *my_menu_win; 4203166124Srafan int n_choices, i; 4204166124Srafan 4205166124Srafan /* Initialize curses */ 4206166124Srafan initscr(); 4207166124Srafan start_color(); 4208166124Srafan cbreak(); 4209166124Srafan noecho(); 4210166124Srafan keypad(stdscr, TRUE); 4211166124Srafan init_pair(1, COLOR_RED, COLOR_BLACK); 4212166124Srafan init_pair(2, COLOR_CYAN, COLOR_BLACK); 4213166124Srafan 4214166124Srafan /* Create items */ 4215166124Srafan n_choices = ARRAY_SIZE(choices); 4216166124Srafan my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *)); 4217166124Srafan for(i = 0; i < n_choices; ++i) 4218166124Srafan my_items[i] = new_item(choices[i], choices[i]); 4219166124Srafan 4220166124Srafan /* Crate menu */ 4221166124Srafan my_menu = new_menu((ITEM **)my_items); 4222166124Srafan 4223166124Srafan /* Set menu option not to show the description */ 4224166124Srafan menu_opts_off(my_menu, O_SHOWDESC); 4225166124Srafan 4226166124Srafan /* Create the window to be associated with the menu */ 4227166124Srafan my_menu_win = newwin(10, 70, 4, 4); 4228166124Srafan keypad(my_menu_win, TRUE); 4229166124Srafan 4230166124Srafan /* Set main window and sub window */ 4231166124Srafan set_menu_win(my_menu, my_menu_win); 4232166124Srafan set_menu_sub(my_menu, derwin(my_menu_win, 6, 68, 3, 1)); 4233166124Srafan set_menu_format(my_menu, 5, 3); 4234166124Srafan set_menu_mark(my_menu, " * "); 4235166124Srafan 4236166124Srafan /* Print a border around the main window and print a title */ 4237166124Srafan box(my_menu_win, 0, 0); 4238166124Srafan 4239166124Srafan attron(COLOR_PAIR(2)); 4240166124Srafan mvprintw(LINES - 3, 0, "Use PageUp and PageDown to scroll"); 4241166124Srafan mvprintw(LINES - 2, 0, "Use Arrow Keys to navigate (F1 to Exit)"); 4242166124Srafan attroff(COLOR_PAIR(2)); 4243166124Srafan refresh(); 4244166124Srafan 4245166124Srafan /* Post the menu */ 4246166124Srafan post_menu(my_menu); 4247166124Srafan wrefresh(my_menu_win); 4248166124Srafan 4249166124Srafan while((c = wgetch(my_menu_win)) != KEY_F(1)) 4250166124Srafan { switch(c) 4251166124Srafan { case KEY_DOWN: 4252166124Srafan menu_driver(my_menu, REQ_DOWN_ITEM); 4253166124Srafan break; 4254166124Srafan case KEY_UP: 4255166124Srafan menu_driver(my_menu, REQ_UP_ITEM); 4256166124Srafan break; 4257166124Srafan case KEY_LEFT: 4258166124Srafan menu_driver(my_menu, REQ_LEFT_ITEM); 4259166124Srafan break; 4260166124Srafan case KEY_RIGHT: 4261166124Srafan menu_driver(my_menu, REQ_RIGHT_ITEM); 4262166124Srafan break; 4263166124Srafan case KEY_NPAGE: 4264166124Srafan menu_driver(my_menu, REQ_SCR_DPAGE); 4265166124Srafan break; 4266166124Srafan case KEY_PPAGE: 4267166124Srafan menu_driver(my_menu, REQ_SCR_UPAGE); 4268166124Srafan break; 4269166124Srafan } 4270166124Srafan wrefresh(my_menu_win); 4271166124Srafan } 4272166124Srafan 4273166124Srafan /* Unpost and free all the memory taken up */ 4274166124Srafan unpost_menu(my_menu); 4275166124Srafan free_menu(my_menu); 4276166124Srafan for(i = 0; i < n_choices; ++i) 4277166124Srafan free_item(my_items[i]); 4278166124Srafan endwin(); 4279166124Srafan}</span></font> 4280166124Srafan</pre></td> 4281166124Srafan</tr> 4282166124Srafan</table> 4283166124Srafan</div> 4284166124Srafan<p>Watch the function call to set_menu_format(). It specifies the 4285166124Srafannumber of columns to be 3, thus displaying 3 items per row. We have 4286166124Srafanalso switched off the showing descriptions with the function 4287166124Srafanmenu_opts_off(). There are couple of functions set_menu_opts(), 4288166124Srafanmenu_opts_on() and menu_opts() which can be used to manipulate menu 4289166124Srafanoptions. The following menu options can be specified.</p> 4290166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 4291166124Srafan<tr> 4292166124Srafan<td> 4293166124Srafan<pre class="PROGRAMLISTING"> 4294166124Srafan<font color="#000000"> O_ONEVALUE 4295166124Srafan Only one item can be selected for this menu. 4296166124Srafan 4297166124Srafan O_SHOWDESC 4298166124Srafan Display the item descriptions when the menu is 4299166124Srafan posted. 4300166124Srafan 4301166124Srafan O_ROWMAJOR 4302166124Srafan Display the menu in row-major order. 4303166124Srafan 4304166124Srafan O_IGNORECASE 4305166124Srafan Ignore the case when pattern-matching. 4306166124Srafan 4307166124Srafan O_SHOWMATCH 4308166124Srafan Move the cursor to within the item name while pat­ 4309166124Srafan tern-matching. 4310166124Srafan 4311166124Srafan O_NONCYCLIC 4312166124Srafan Don't wrap around next-item and previous-item, 4313166124Srafan requests to the other end of the menu.</font> 4314166124Srafan</pre></td> 4315166124Srafan</tr> 4316166124Srafan</table> 4317166124Srafan<p>All options are on by default. You can switch specific 4318166124Srafanattributes on or off with menu_opts_on() and menu_opts_off() 4319166124Srafanfunctions. You can also use set_menu_opts() to directly specify the 4320166124Srafanoptions. The argument to this function should be a OR ed value of 4321166124Srafansome of those above constants. The function menu_opts() can be used 4322166124Srafanto find out a menu's present options.</p> 4323166124Srafan</div> 4324166124Srafan<div class="SECT2"> 4325166124Srafan<hr> 4326166124Srafan<h3 class="SECT2"><a name="MULTIVALUEMENUS" id= 4327166124Srafan"MULTIVALUEMENUS">17.7. Multi Valued Menus</a></h3> 4328166124Srafan<p>You might be wondering what if you switch off the option 4329166124SrafanO_ONEVALUE. Then the menu becomes multi-valued. That means you can 4330166124Srafanselect more than one item. This brings us to the request 4331166124SrafanREQ_TOGGLE_ITEM. Let's see it in action.</p> 4332166124Srafan<div class="EXAMPLE"><a name="MMETO" id="MMETO"></a> 4333166124Srafan<p><b>Example 22. Multi Valued Menus example</b></p> 4334166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 4335166124Srafan<tr> 4336166124Srafan<td> 4337166124Srafan<pre class="PROGRAMLISTING"> 4338166124Srafan<font color="#000000"><span class= 4339166124Srafan"INLINEMEDIAOBJECT">#include <curses.h> 4340166124Srafan#include <menu.h> 4341166124Srafan 4342166124Srafan#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) 4343166124Srafan#define CTRLD 4 4344166124Srafan 4345166124Srafanchar *choices[] = { 4346166124Srafan "Choice 1", 4347166124Srafan "Choice 2", 4348166124Srafan "Choice 3", 4349166124Srafan "Choice 4", 4350166124Srafan "Choice 5", 4351166124Srafan "Choice 6", 4352166124Srafan "Choice 7", 4353166124Srafan "Exit", 4354166124Srafan }; 4355166124Srafan 4356166124Srafanint main() 4357166124Srafan{ ITEM **my_items; 4358166124Srafan int c; 4359166124Srafan MENU *my_menu; 4360166124Srafan int n_choices, i; 4361166124Srafan ITEM *cur_item; 4362166124Srafan 4363166124Srafan /* Initialize curses */ 4364166124Srafan initscr(); 4365166124Srafan cbreak(); 4366166124Srafan noecho(); 4367166124Srafan keypad(stdscr, TRUE); 4368166124Srafan 4369166124Srafan /* Initialize items */ 4370166124Srafan n_choices = ARRAY_SIZE(choices); 4371166124Srafan my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); 4372166124Srafan for(i = 0; i < n_choices; ++i) 4373166124Srafan my_items[i] = new_item(choices[i], choices[i]); 4374166124Srafan my_items[n_choices] = (ITEM *)NULL; 4375166124Srafan 4376166124Srafan my_menu = new_menu((ITEM **)my_items); 4377166124Srafan 4378166124Srafan /* Make the menu multi valued */ 4379166124Srafan menu_opts_off(my_menu, O_ONEVALUE); 4380166124Srafan 4381166124Srafan mvprintw(LINES - 3, 0, "Use <SPACE> to select or unselect an item."); 4382166124Srafan mvprintw(LINES - 2, 0, "<ENTER> to see presently selected items(F1 to Exit)"); 4383166124Srafan post_menu(my_menu); 4384166124Srafan refresh(); 4385166124Srafan 4386166124Srafan while((c = getch()) != KEY_F(1)) 4387166124Srafan { switch(c) 4388166124Srafan { case KEY_DOWN: 4389166124Srafan menu_driver(my_menu, REQ_DOWN_ITEM); 4390166124Srafan break; 4391166124Srafan case KEY_UP: 4392166124Srafan menu_driver(my_menu, REQ_UP_ITEM); 4393166124Srafan break; 4394166124Srafan case ' ': 4395166124Srafan menu_driver(my_menu, REQ_TOGGLE_ITEM); 4396166124Srafan break; 4397166124Srafan case 10: /* Enter */ 4398166124Srafan { char temp[200]; 4399166124Srafan ITEM **items; 4400166124Srafan 4401166124Srafan items = menu_items(my_menu); 4402166124Srafan temp[0] = '\0'; 4403166124Srafan for(i = 0; i < item_count(my_menu); ++i) 4404166124Srafan if(item_value(items[i]) == TRUE) 4405166124Srafan { strcat(temp, item_name(items[i])); 4406166124Srafan strcat(temp, " "); 4407166124Srafan } 4408166124Srafan move(20, 0); 4409166124Srafan clrtoeol(); 4410166124Srafan mvprintw(20, 0, temp); 4411166124Srafan refresh(); 4412166124Srafan } 4413166124Srafan break; 4414166124Srafan } 4415166124Srafan } 4416166124Srafan 4417166124Srafan free_item(my_items[0]); 4418166124Srafan free_item(my_items[1]); 4419166124Srafan free_menu(my_menu); 4420166124Srafan endwin(); 4421166124Srafan} 4422166124Srafan </span></font> 4423166124Srafan</pre></td> 4424166124Srafan</tr> 4425166124Srafan</table> 4426166124Srafan</div> 4427166124Srafan<p>Whew, A lot of new functions. Let's take them one after another. 4428166124SrafanFirstly, the REQ_TOGGLE_ITEM. In a multi-valued menu, the user 4429166124Srafanshould be allowed to select or un select more than one item. The 4430166124Srafanrequest REQ_TOGGLE_ITEM toggles the present selection. In this case 4431166124Srafanwhen space is pressed REQ_TOGGLE_ITEM request is sent to 4432166124Srafanmenu_driver to achieve the result.</p> 4433166124Srafan<p>Now when the user presses <ENTER> we show the items he 4434166124Srafanpresently selected. First we find out the items associated with the 4435166124Srafanmenu using the function menu_items(). Then we loop through the 4436166124Srafanitems to find out if the item is selected or not. The function 4437166124Srafanitem_value() returns TRUE if an item is selected. The function 4438166124Srafanitem_count() returns the number of items in the menu. The item name 4439166124Srafancan be found with item_name(). You can also find the description 4440166124Srafanassociated with an item using item_description().</p> 4441166124Srafan</div> 4442166124Srafan<div class="SECT2"> 4443166124Srafan<hr> 4444166124Srafan<h3 class="SECT2"><a name="MENUOPT" id="MENUOPT">17.8. Menu 4445166124SrafanOptions</a></h3> 4446166124Srafan<p>Well, by this time you must be itching for some difference in 4447166124Srafanyour menu, with lots of functionality. I know. You want Colors !!!. 4448166124SrafanYou want to create nice menus similar to those text mode <a href= 4449166124Srafan"http://www.jersey.net/~debinjoe/games/" target="_top">dos 4450166124Srafangames</a>. The functions set_menu_fore() and set_menu_back() can be 4451166124Srafanused to change the attribute of the selected item and unselected 4452166124Srafanitem. The names are misleading. They don't change menu's foreground 4453166124Srafanor background which would have been useless.</p> 4454166124Srafan<p>The function set_menu_grey() can be used to set the display 4455166124Srafanattribute for the non-selectable items in the menu. This brings us 4456166124Srafanto the interesting option for an item the one and only 4457166124SrafanO_SELECTABLE. We can turn it off by the function item_opts_off() 4458166124Srafanand after that that item is not selectable. It's like a grayed item 4459166124Srafanin those fancy windows menus. Let's put these concepts in practice 4460166124Srafanwith this example</p> 4461166124Srafan<div class="EXAMPLE"><a name="MMEAT" id="MMEAT"></a> 4462166124Srafan<p><b>Example 23. Menu Options example</b></p> 4463166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 4464166124Srafan<tr> 4465166124Srafan<td> 4466166124Srafan<pre class="PROGRAMLISTING"> 4467166124Srafan<font color="#000000"><span class= 4468166124Srafan"INLINEMEDIAOBJECT">#include <menu.h> 4469166124Srafan 4470166124Srafan#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) 4471166124Srafan#define CTRLD 4 4472166124Srafan 4473166124Srafanchar *choices[] = { 4474166124Srafan "Choice 1", 4475166124Srafan "Choice 2", 4476166124Srafan "Choice 3", 4477166124Srafan "Choice 4", 4478166124Srafan "Choice 5", 4479166124Srafan "Choice 6", 4480166124Srafan "Choice 7", 4481166124Srafan "Exit", 4482166124Srafan }; 4483166124Srafan 4484166124Srafanint main() 4485166124Srafan{ ITEM **my_items; 4486166124Srafan int c; 4487166124Srafan MENU *my_menu; 4488166124Srafan int n_choices, i; 4489166124Srafan ITEM *cur_item; 4490166124Srafan 4491166124Srafan /* Initialize curses */ 4492166124Srafan initscr(); 4493166124Srafan start_color(); 4494166124Srafan cbreak(); 4495166124Srafan noecho(); 4496166124Srafan keypad(stdscr, TRUE); 4497166124Srafan init_pair(1, COLOR_RED, COLOR_BLACK); 4498166124Srafan init_pair(2, COLOR_GREEN, COLOR_BLACK); 4499166124Srafan init_pair(3, COLOR_MAGENTA, COLOR_BLACK); 4500166124Srafan 4501166124Srafan /* Initialize items */ 4502166124Srafan n_choices = ARRAY_SIZE(choices); 4503166124Srafan my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); 4504166124Srafan for(i = 0; i < n_choices; ++i) 4505166124Srafan my_items[i] = new_item(choices[i], choices[i]); 4506166124Srafan my_items[n_choices] = (ITEM *)NULL; 4507166124Srafan item_opts_off(my_items[3], O_SELECTABLE); 4508166124Srafan item_opts_off(my_items[6], O_SELECTABLE); 4509166124Srafan 4510166124Srafan /* Create menu */ 4511166124Srafan my_menu = new_menu((ITEM **)my_items); 4512166124Srafan 4513166124Srafan /* Set fore ground and back ground of the menu */ 4514166124Srafan set_menu_fore(my_menu, COLOR_PAIR(1) | A_REVERSE); 4515166124Srafan set_menu_back(my_menu, COLOR_PAIR(2)); 4516166124Srafan set_menu_grey(my_menu, COLOR_PAIR(3)); 4517166124Srafan 4518166124Srafan /* Post the menu */ 4519166124Srafan mvprintw(LINES - 3, 0, "Press <ENTER> to see the option selected"); 4520166124Srafan mvprintw(LINES - 2, 0, "Up and Down arrow keys to naviage (F1 to Exit)"); 4521166124Srafan post_menu(my_menu); 4522166124Srafan refresh(); 4523166124Srafan 4524166124Srafan while((c = getch()) != KEY_F(1)) 4525166124Srafan { switch(c) 4526166124Srafan { case KEY_DOWN: 4527166124Srafan menu_driver(my_menu, REQ_DOWN_ITEM); 4528166124Srafan break; 4529166124Srafan case KEY_UP: 4530166124Srafan menu_driver(my_menu, REQ_UP_ITEM); 4531166124Srafan break; 4532166124Srafan case 10: /* Enter */ 4533166124Srafan move(20, 0); 4534166124Srafan clrtoeol(); 4535166124Srafan mvprintw(20, 0, "Item selected is : %s", 4536166124Srafan item_name(current_item(my_menu))); 4537166124Srafan pos_menu_cursor(my_menu); 4538166124Srafan break; 4539166124Srafan } 4540166124Srafan } 4541166124Srafan unpost_menu(my_menu); 4542166124Srafan for(i = 0; i < n_choices; ++i) 4543166124Srafan free_item(my_items[i]); 4544166124Srafan free_menu(my_menu); 4545166124Srafan endwin(); 4546166124Srafan} 4547166124Srafan </span></font> 4548166124Srafan</pre></td> 4549166124Srafan</tr> 4550166124Srafan</table> 4551166124Srafan</div> 4552166124Srafan</div> 4553166124Srafan<div class="SECT2"> 4554166124Srafan<hr> 4555166124Srafan<h3 class="SECT2"><a name="MENUUSERPTR" id="MENUUSERPTR">17.9. The 4556166124Srafanuseful User Pointer</a></h3> 4557166124Srafan<p>We can associate a user pointer with each item in the menu. It 4558166124Srafanworks the same way as user pointer in panels. It's not touched by 4559166124Srafanmenu system. You can store any thing you like in that. I usually 4560166124Srafanuse it to store the function to be executed when the menu option is 4561166124Srafanchosen (It's selected and may be the user pressed 4562166124Srafan<ENTER>);</p> 4563166124Srafan<div class="EXAMPLE"><a name="MMEUS" id="MMEUS"></a> 4564166124Srafan<p><b>Example 24. Menu User Pointer Usage</b></p> 4565166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 4566166124Srafan<tr> 4567166124Srafan<td> 4568166124Srafan<pre class="PROGRAMLISTING"> 4569166124Srafan<font color="#000000"><span class= 4570166124Srafan"INLINEMEDIAOBJECT">#include <curses.h> 4571166124Srafan#include <menu.h> 4572166124Srafan 4573166124Srafan#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) 4574166124Srafan#define CTRLD 4 4575166124Srafan 4576166124Srafanchar *choices[] = { 4577166124Srafan "Choice 1", 4578166124Srafan "Choice 2", 4579166124Srafan "Choice 3", 4580166124Srafan "Choice 4", 4581166124Srafan "Choice 5", 4582166124Srafan "Choice 6", 4583166124Srafan "Choice 7", 4584166124Srafan "Exit", 4585166124Srafan }; 4586166124Srafanvoid func(char *name); 4587166124Srafan 4588166124Srafanint main() 4589166124Srafan{ ITEM **my_items; 4590166124Srafan int c; 4591166124Srafan MENU *my_menu; 4592166124Srafan int n_choices, i; 4593166124Srafan ITEM *cur_item; 4594166124Srafan 4595166124Srafan /* Initialize curses */ 4596166124Srafan initscr(); 4597166124Srafan start_color(); 4598166124Srafan cbreak(); 4599166124Srafan noecho(); 4600166124Srafan keypad(stdscr, TRUE); 4601166124Srafan init_pair(1, COLOR_RED, COLOR_BLACK); 4602166124Srafan init_pair(2, COLOR_GREEN, COLOR_BLACK); 4603166124Srafan init_pair(3, COLOR_MAGENTA, COLOR_BLACK); 4604166124Srafan 4605166124Srafan /* Initialize items */ 4606166124Srafan n_choices = ARRAY_SIZE(choices); 4607166124Srafan my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); 4608166124Srafan for(i = 0; i < n_choices; ++i) 4609166124Srafan { my_items[i] = new_item(choices[i], choices[i]); 4610166124Srafan /* Set the user pointer */ 4611166124Srafan set_item_userptr(my_items[i], func); 4612166124Srafan } 4613166124Srafan my_items[n_choices] = (ITEM *)NULL; 4614166124Srafan 4615166124Srafan /* Create menu */ 4616166124Srafan my_menu = new_menu((ITEM **)my_items); 4617166124Srafan 4618166124Srafan /* Post the menu */ 4619166124Srafan mvprintw(LINES - 3, 0, "Press <ENTER> to see the option selected"); 4620166124Srafan mvprintw(LINES - 2, 0, "Up and Down arrow keys to naviage (F1 to Exit)"); 4621166124Srafan post_menu(my_menu); 4622166124Srafan refresh(); 4623166124Srafan 4624166124Srafan while((c = getch()) != KEY_F(1)) 4625166124Srafan { switch(c) 4626166124Srafan { case KEY_DOWN: 4627166124Srafan menu_driver(my_menu, REQ_DOWN_ITEM); 4628166124Srafan break; 4629166124Srafan case KEY_UP: 4630166124Srafan menu_driver(my_menu, REQ_UP_ITEM); 4631166124Srafan break; 4632166124Srafan case 10: /* Enter */ 4633166124Srafan { ITEM *cur; 4634166124Srafan void (*p)(char *); 4635166124Srafan 4636166124Srafan cur = current_item(my_menu); 4637166124Srafan p = item_userptr(cur); 4638166124Srafan p((char *)item_name(cur)); 4639166124Srafan pos_menu_cursor(my_menu); 4640166124Srafan break; 4641166124Srafan } 4642166124Srafan break; 4643166124Srafan } 4644166124Srafan } 4645166124Srafan unpost_menu(my_menu); 4646166124Srafan for(i = 0; i < n_choices; ++i) 4647166124Srafan free_item(my_items[i]); 4648166124Srafan free_menu(my_menu); 4649166124Srafan endwin(); 4650166124Srafan} 4651166124Srafan 4652166124Srafanvoid func(char *name) 4653166124Srafan{ move(20, 0); 4654166124Srafan clrtoeol(); 4655166124Srafan mvprintw(20, 0, "Item selected is : %s", name); 4656166124Srafan} </span></font> 4657166124Srafan</pre></td> 4658166124Srafan</tr> 4659166124Srafan</table> 4660166124Srafan</div> 4661166124Srafan</div> 4662166124Srafan</div> 4663166124Srafan<div class="SECT1"> 4664166124Srafan<hr> 4665166124Srafan<h2 class="SECT1"><a name="FORMS" id="FORMS">18. Forms 4666166124SrafanLibrary</a></h2> 4667166124Srafan<p>Well. If you have seen those forms on web pages which take input 4668166124Srafanfrom users and do various kinds of things, you might be wondering 4669166124Srafanhow would any one create such forms in text mode display. It's 4670166124Srafanquite difficult to write those nifty forms in plain ncurses. Forms 4671166124Srafanlibrary tries to provide a basic frame work to build and maintain 4672166124Srafanforms with ease. It has lot of features(functions) which manage 4673166124Srafanvalidation, dynamic expansion of fields etc.. Let's see it in full 4674166124Srafanflow.</p> 4675166124Srafan<p>A form is a collection of fields; each field can be either a 4676166124Srafanlabel(static text) or a data-entry location. The forms also library 4677166124Srafanprovides functions to divide forms into multiple pages.</p> 4678166124Srafan<div class="SECT2"> 4679166124Srafan<hr> 4680166124Srafan<h3 class="SECT2"><a name="FORMBASICS" id="FORMBASICS">18.1. The 4681166124SrafanBasics</a></h3> 4682166124Srafan<p>Forms are created in much the same way as menus. First the 4683166124Srafanfields related to the form are created with new_field(). You can 4684166124Srafanset options for the fields, so that they can be displayed with some 4685166124Srafanfancy attributes, validated before the field looses focus etc.. 4686166124SrafanThen the fields are attached to form. After this, the form can be 4687166124Srafanposted to display and is ready to receive inputs. On the similar 4688166124Srafanlines to menu_driver(), the form is manipulated with form_driver(). 4689166124SrafanWe can send requests to form_driver to move focus to a certain 4690166124Srafanfield, move cursor to end of the field etc.. After the user enters 4691166124Srafanvalues in the fields and validation done, form can be unposted and 4692166124Srafanmemory allocated can be freed.</p> 4693166124Srafan<p>The general flow of control of a forms program looks like 4694166124Srafanthis.</p> 4695166124Srafan<ol type="1"> 4696166124Srafan<li> 4697166124Srafan<p>Initialize curses</p> 4698166124Srafan</li> 4699166124Srafan<li> 4700166124Srafan<p>Create fields using new_field(). You can specify the height and 4701166124Srafanwidth of the field, and its position on the form.</p> 4702166124Srafan</li> 4703166124Srafan<li> 4704166124Srafan<p>Create the forms with new_form() by specifying the fields to be 4705166124Srafanattached with.</p> 4706166124Srafan</li> 4707166124Srafan<li> 4708166124Srafan<p>Post the form with form_post() and refresh the screen.</p> 4709166124Srafan</li> 4710166124Srafan<li> 4711166124Srafan<p>Process the user requests with a loop and do necessary updates 4712166124Srafanto form with form_driver.</p> 4713166124Srafan</li> 4714166124Srafan<li> 4715166124Srafan<p>Unpost the menu with form_unpost()</p> 4716166124Srafan</li> 4717166124Srafan<li> 4718166124Srafan<p>Free the memory allocated to menu by free_form()</p> 4719166124Srafan</li> 4720166124Srafan<li> 4721166124Srafan<p>Free the memory allocated to the items with free_field()</p> 4722166124Srafan</li> 4723166124Srafan<li> 4724166124Srafan<p>End curses</p> 4725166124Srafan</li> 4726166124Srafan</ol> 4727166124Srafan<p>As you can see, working with forms library is much similar to 4728166124Srafanhandling menu library. The following examples will explore various 4729166124Srafanaspects of form processing. Let's start the journey with a simple 4730166124Srafanexample. first.</p> 4731166124Srafan</div> 4732166124Srafan<div class="SECT2"> 4733166124Srafan<hr> 4734166124Srafan<h3 class="SECT2"><a name="COMPILEFORMS" id="COMPILEFORMS">18.2. 4735166124SrafanCompiling With the Forms Library</a></h3> 4736166124Srafan<p>To use forms library functions, you have to include form.h and 4737166124Srafanto link the program with forms library the flag -lform should be 4738166124Srafanadded along with -lncurses in that order.</p> 4739166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 4740166124Srafan<tr> 4741166124Srafan<td> 4742166124Srafan<pre class="PROGRAMLISTING"> 4743166124Srafan<font color="#000000"> #include <form.h> 4744166124Srafan . 4745166124Srafan . 4746166124Srafan . 4747166124Srafan 4748166124Srafan compile and link: gcc <program file> -lform -lncurses</font> 4749166124Srafan</pre></td> 4750166124Srafan</tr> 4751166124Srafan</table> 4752166124Srafan<div class="EXAMPLE"><a name="FFOSI" id="FFOSI"></a> 4753166124Srafan<p><b>Example 25. Forms Basics</b></p> 4754166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 4755166124Srafan<tr> 4756166124Srafan<td> 4757166124Srafan<pre class="PROGRAMLISTING"> 4758166124Srafan<font color="#000000"><span class= 4759166124Srafan"INLINEMEDIAOBJECT">#include <form.h> 4760166124Srafan 4761166124Srafanint main() 4762166124Srafan{ FIELD *field[3]; 4763166124Srafan FORM *my_form; 4764166124Srafan int ch; 4765166124Srafan 4766166124Srafan /* Initialize curses */ 4767166124Srafan initscr(); 4768166124Srafan cbreak(); 4769166124Srafan noecho(); 4770166124Srafan keypad(stdscr, TRUE); 4771166124Srafan 4772166124Srafan /* Initialize the fields */ 4773166124Srafan field[0] = new_field(1, 10, 4, 18, 0, 0); 4774166124Srafan field[1] = new_field(1, 10, 6, 18, 0, 0); 4775166124Srafan field[2] = NULL; 4776166124Srafan 4777166124Srafan /* Set field options */ 4778166124Srafan set_field_back(field[0], A_UNDERLINE); /* Print a line for the option */ 4779166124Srafan field_opts_off(field[0], O_AUTOSKIP); /* Don't go to next field when this */ 4780166124Srafan /* Field is filled up */ 4781166124Srafan set_field_back(field[1], A_UNDERLINE); 4782166124Srafan field_opts_off(field[1], O_AUTOSKIP); 4783166124Srafan 4784166124Srafan /* Create the form and post it */ 4785166124Srafan my_form = new_form(field); 4786166124Srafan post_form(my_form); 4787166124Srafan refresh(); 4788166124Srafan 4789166124Srafan mvprintw(4, 10, "Value 1:"); 4790166124Srafan mvprintw(6, 10, "Value 2:"); 4791166124Srafan refresh(); 4792166124Srafan 4793166124Srafan /* Loop through to get user requests */ 4794166124Srafan while((ch = getch()) != KEY_F(1)) 4795166124Srafan { switch(ch) 4796166124Srafan { case KEY_DOWN: 4797166124Srafan /* Go to next field */ 4798166124Srafan form_driver(my_form, REQ_NEXT_FIELD); 4799166124Srafan /* Go to the end of the present buffer */ 4800166124Srafan /* Leaves nicely at the last character */ 4801166124Srafan form_driver(my_form, REQ_END_LINE); 4802166124Srafan break; 4803166124Srafan case KEY_UP: 4804166124Srafan /* Go to previous field */ 4805166124Srafan form_driver(my_form, REQ_PREV_FIELD); 4806166124Srafan form_driver(my_form, REQ_END_LINE); 4807166124Srafan break; 4808166124Srafan default: 4809166124Srafan /* If this is a normal character, it gets */ 4810166124Srafan /* Printed */ 4811166124Srafan form_driver(my_form, ch); 4812166124Srafan break; 4813166124Srafan } 4814166124Srafan } 4815166124Srafan 4816166124Srafan /* Un post form and free the memory */ 4817166124Srafan unpost_form(my_form); 4818166124Srafan free_form(my_form); 4819166124Srafan free_field(field[0]); 4820166124Srafan free_field(field[1]); 4821166124Srafan 4822166124Srafan endwin(); 4823166124Srafan return 0; 4824166124Srafan}</span></font> 4825166124Srafan</pre></td> 4826166124Srafan</tr> 4827166124Srafan</table> 4828166124Srafan</div> 4829166124Srafan<p>Above example is pretty straight forward. It creates two fields 4830166124Srafanwith <var class="LITERAL">new_field()</var>. new_field() takes 4831166124Srafanheight, width, starty, startx, number of offscreen rows and number 4832166124Srafanof additional working buffers. The fifth argument number of 4833166124Srafanoffscreen rows specifies how much of the field to be shown. If it 4834166124Srafanis zero, the entire field is always displayed otherwise the form 4835166124Srafanwill be scrollable when the user accesses not displayed parts of 4836166124Srafanthe field. The forms library allocates one buffer per field to 4837166124Srafanstore the data user enters. Using the last parameter to new_field() 4838166124Srafanwe can specify it to allocate some additional buffers. These can be 4839166124Srafanused for any purpose you like.</p> 4840166124Srafan<p>After creating the fields, back ground attribute of both of them 4841166124Srafanis set to an underscore with set_field_back(). The AUTOSKIP option 4842166124Srafanis turned off using field_opts_off(). If this option is turned on, 4843166124Srafanfocus will move to the next field in the form once the active field 4844166124Srafanis filled up completely.</p> 4845166124Srafan<p>After attaching the fields to the form, it is posted. Here on, 4846166124Srafanuser inputs are processed in the while loop, by making 4847166124Srafancorresponding requests to form_driver. The details of all the 4848166124Srafanrequests to the form_driver() are explained later.</p> 4849166124Srafan</div> 4850166124Srafan<div class="SECT2"> 4851166124Srafan<hr> 4852166124Srafan<h3 class="SECT2"><a name="PLAYFIELDS" id="PLAYFIELDS">18.3. 4853166124SrafanPlaying with Fields</a></h3> 4854166124Srafan<p>Each form field is associated with a lot of attributes. They can 4855166124Srafanbe manipulated to get the required effect and to have fun !!!. So 4856166124Srafanwhy wait?</p> 4857166124Srafan<div class="SECT3"> 4858166124Srafan<hr> 4859166124Srafan<h4 class="SECT3"><a name="FETCHINFO" id="FETCHINFO">18.3.1. 4860166124SrafanFetching Size and Location of Field</a></h4> 4861166124Srafan<p>The parameters we have given at the time of creation of a field 4862166124Srafancan be retrieved with field_info(). It returns height, width, 4863166124Srafanstarty, startx, number of offscreen rows, and number of additional 4864166124Srafanbuffers into the parameters given to it. It is a sort of inverse of 4865166124Srafannew_field().</p> 4866166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 4867166124Srafan<tr> 4868166124Srafan<td> 4869166124Srafan<pre class="PROGRAMLISTING"> 4870166124Srafan<font color= 4871166124Srafan"#000000">int field_info( FIELD *field, /* field from which to fetch */ 4872166124Srafan int *height, *int width, /* field size */ 4873166124Srafan int *top, int *left, /* upper left corner */ 4874166124Srafan int *offscreen, /* number of offscreen rows */ 4875166124Srafan int *nbuf); /* number of working buffers */</font> 4876166124Srafan</pre></td> 4877166124Srafan</tr> 4878166124Srafan</table> 4879166124Srafan</div> 4880166124Srafan<div class="SECT3"> 4881166124Srafan<hr> 4882166124Srafan<h4 class="SECT3"><a name="MOVEFIELD" id="MOVEFIELD">18.3.2. Moving 4883166124Srafanthe field</a></h4> 4884166124Srafan<p>The location of the field can be moved to a different position 4885166124Srafanwith move_field().</p> 4886166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 4887166124Srafan<tr> 4888166124Srafan<td> 4889166124Srafan<pre class="PROGRAMLISTING"> 4890166124Srafan<font color= 4891166124Srafan"#000000">int move_field( FIELD *field, /* field to alter */ 4892166124Srafan int top, int left); /* new upper-left corner */</font> 4893166124Srafan</pre></td> 4894166124Srafan</tr> 4895166124Srafan</table> 4896166124Srafan<p>As usual, the changed position can be queried with 4897166124Srafanfield_infor().</p> 4898166124Srafan</div> 4899166124Srafan<div class="SECT3"> 4900166124Srafan<hr> 4901166124Srafan<h4 class="SECT3"><a name="JUSTIFYFIELD" id="JUSTIFYFIELD">18.3.3. 4902166124SrafanField Justification</a></h4> 4903166124Srafan<p>The justification to be done for the field can be fixed using 4904166124Srafanthe function set_field_just().</p> 4905166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 4906166124Srafan<tr> 4907166124Srafan<td> 4908166124Srafan<pre class="PROGRAMLISTING"> 4909166124Srafan<font color= 4910166124Srafan"#000000"> int set_field_just(FIELD *field, /* field to alter */ 4911166124Srafan int justmode); /* mode to set */ 4912166124Srafan int field_just(FIELD *field); /* fetch justify mode of field */</font> 4913166124Srafan</pre></td> 4914166124Srafan</tr> 4915166124Srafan</table> 4916166124Srafan<p>The justification mode valued accepted and returned by these 4917166124Srafanfunctions are NO_JUSTIFICATION, JUSTIFY_RIGHT, JUSTIFY_LEFT, or 4918166124SrafanJUSTIFY_CENTER.</p> 4919166124Srafan</div> 4920166124Srafan<div class="SECT3"> 4921166124Srafan<hr> 4922166124Srafan<h4 class="SECT3"><a name="FIELDDISPATTRIB" id= 4923166124Srafan"FIELDDISPATTRIB">18.3.4. Field Display Attributes</a></h4> 4924166124Srafan<p>As you have seen, in the above example, display attribute for 4925166124Srafanthe fields can be set with set_field_fore() and setfield_back(). 4926166124SrafanThese functions set foreground and background attribute of the 4927166124Srafanfields. You can also specify a pad character which will be filled 4928166124Srafanin the unfilled portion of the field. The pad character is set with 4929166124Srafana call to set_field_pad(). Default pad value is a space. The 4930166124Srafanfunctions field_fore(), field_back, field_pad() can be used to 4931166124Srafanquery the present foreground, background attributes and pad 4932166124Srafancharacter for the field. The following list gives the usage of 4933166124Srafanfunctions.</p> 4934166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 4935166124Srafan<tr> 4936166124Srafan<td> 4937166124Srafan<pre class="PROGRAMLISTING"> 4938166124Srafan<font color= 4939166124Srafan"#000000"> int set_field_fore(FIELD *field, /* field to alter */ 4940166124Srafan chtype attr); /* attribute to set */ 4941166124Srafan 4942166124Srafanchtype field_fore(FIELD *field); /* field to query */ 4943166124Srafan /* returns foreground attribute */ 4944166124Srafan 4945166124Srafanint set_field_back(FIELD *field, /* field to alter */ 4946166124Srafan chtype attr); /* attribute to set */ 4947166124Srafan 4948166124Srafanchtype field_back(FIELD *field); /* field to query */ 4949166124Srafan /* returns background attribute */ 4950166124Srafan 4951166124Srafanint set_field_pad(FIELD *field, /* field to alter */ 4952166124Srafan int pad); /* pad character to set */ 4953166124Srafan 4954166124Srafanchtype field_pad(FIELD *field); /* field to query */ 4955166124Srafan /* returns present pad character */ </font> 4956166124Srafan</pre></td> 4957166124Srafan</tr> 4958166124Srafan</table> 4959166124Srafan<p>Though above functions seem quite simple, using colors with 4960166124Srafanset_field_fore() may be frustrating in the beginning. Let me first 4961166124Srafanexplain about foreground and background attributes of a field. The 4962166124Srafanforeground attribute is associated with the character. That means a 4963166124Srafancharacter in the field is printed with the attribute you have set 4964166124Srafanwith set_field_fore(). Background attribute is the attribute used 4965166124Srafanto fill background of field, whether any character is there or not. 4966166124SrafanSo what about colors? Since colors are always defined in pairs, 4967166124Srafanwhat is the right way to display colored fields? Here's an example 4968166124Srafanclarifying color attributes.</p> 4969166124Srafan<div class="EXAMPLE"><a name="FFOAT" id="FFOAT"></a> 4970166124Srafan<p><b>Example 26. Form Attributes example</b></p> 4971166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 4972166124Srafan<tr> 4973166124Srafan<td> 4974166124Srafan<pre class="PROGRAMLISTING"> 4975166124Srafan<font color="#000000"><span class= 4976166124Srafan"INLINEMEDIAOBJECT">#include <form.h> 4977166124Srafan 4978166124Srafanint main() 4979166124Srafan{ FIELD *field[3]; 4980166124Srafan FORM *my_form; 4981166124Srafan int ch; 4982166124Srafan 4983166124Srafan /* Initialize curses */ 4984166124Srafan initscr(); 4985166124Srafan start_color(); 4986166124Srafan cbreak(); 4987166124Srafan noecho(); 4988166124Srafan keypad(stdscr, TRUE); 4989166124Srafan 4990166124Srafan /* Initialize few color pairs */ 4991166124Srafan init_pair(1, COLOR_WHITE, COLOR_BLUE); 4992166124Srafan init_pair(2, COLOR_WHITE, COLOR_BLUE); 4993166124Srafan 4994166124Srafan /* Initialize the fields */ 4995166124Srafan field[0] = new_field(1, 10, 4, 18, 0, 0); 4996166124Srafan field[1] = new_field(1, 10, 6, 18, 0, 0); 4997166124Srafan field[2] = NULL; 4998166124Srafan 4999166124Srafan /* Set field options */ 5000166124Srafan set_field_fore(field[0], COLOR_PAIR(1));/* Put the field with blue background */ 5001166124Srafan set_field_back(field[0], COLOR_PAIR(2));/* and white foreground (characters */ 5002166124Srafan /* are printed in white */ 5003166124Srafan field_opts_off(field[0], O_AUTOSKIP); /* Don't go to next field when this */ 5004166124Srafan /* Field is filled up */ 5005166124Srafan set_field_back(field[1], A_UNDERLINE); 5006166124Srafan field_opts_off(field[1], O_AUTOSKIP); 5007166124Srafan 5008166124Srafan /* Create the form and post it */ 5009166124Srafan my_form = new_form(field); 5010166124Srafan post_form(my_form); 5011166124Srafan refresh(); 5012166124Srafan 5013166124Srafan set_current_field(my_form, field[0]); /* Set focus to the colored field */ 5014166124Srafan mvprintw(4, 10, "Value 1:"); 5015166124Srafan mvprintw(6, 10, "Value 2:"); 5016166124Srafan mvprintw(LINES - 2, 0, "Use UP, DOWN arrow keys to switch between fields"); 5017166124Srafan refresh(); 5018166124Srafan 5019166124Srafan /* Loop through to get user requests */ 5020166124Srafan while((ch = getch()) != KEY_F(1)) 5021166124Srafan { switch(ch) 5022166124Srafan { case KEY_DOWN: 5023166124Srafan /* Go to next field */ 5024166124Srafan form_driver(my_form, REQ_NEXT_FIELD); 5025166124Srafan /* Go to the end of the present buffer */ 5026166124Srafan /* Leaves nicely at the last character */ 5027166124Srafan form_driver(my_form, REQ_END_LINE); 5028166124Srafan break; 5029166124Srafan case KEY_UP: 5030166124Srafan /* Go to previous field */ 5031166124Srafan form_driver(my_form, REQ_PREV_FIELD); 5032166124Srafan form_driver(my_form, REQ_END_LINE); 5033166124Srafan break; 5034166124Srafan default: 5035166124Srafan /* If this is a normal character, it gets */ 5036166124Srafan /* Printed */ 5037166124Srafan form_driver(my_form, ch); 5038166124Srafan break; 5039166124Srafan } 5040166124Srafan } 5041166124Srafan 5042166124Srafan /* Un post form and free the memory */ 5043166124Srafan unpost_form(my_form); 5044166124Srafan free_form(my_form); 5045166124Srafan free_field(field[0]); 5046166124Srafan free_field(field[1]); 5047166124Srafan 5048166124Srafan endwin(); 5049166124Srafan return 0; 5050166124Srafan}</span></font> 5051166124Srafan</pre></td> 5052166124Srafan</tr> 5053166124Srafan</table> 5054166124Srafan</div> 5055166124Srafan<p>Play with the color pairs and try to understand the foreground 5056166124Srafanand background attributes. In my programs using color attributes, I 5057166124Srafanusually set only the background with set_field_back(). Curses 5058166124Srafansimply doesn't allow defining individual color attributes.</p> 5059166124Srafan</div> 5060166124Srafan<div class="SECT3"> 5061166124Srafan<hr> 5062166124Srafan<h4 class="SECT3"><a name="FIELDOPTIONBITS" id= 5063166124Srafan"FIELDOPTIONBITS">18.3.5. Field Option Bits</a></h4> 5064166124Srafan<p>There is also a large collection of field option bits you can 5065166124Srafanset to control various aspects of forms processing. You can 5066166124Srafanmanipulate them with these functions:</p> 5067166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 5068166124Srafan<tr> 5069166124Srafan<td> 5070166124Srafan<pre class="PROGRAMLISTING"> 5071166124Srafan<font color= 5072166124Srafan"#000000">int set_field_opts(FIELD *field, /* field to alter */ 5073166124Srafan int attr); /* attribute to set */ 5074166124Srafan 5075166124Srafanint field_opts_on(FIELD *field, /* field to alter */ 5076166124Srafan int attr); /* attributes to turn on */ 5077166124Srafan 5078166124Srafanint field_opts_off(FIELD *field, /* field to alter */ 5079166124Srafan int attr); /* attributes to turn off */ 5080166124Srafan 5081166124Srafanint field_opts(FIELD *field); /* field to query */ </font> 5082166124Srafan</pre></td> 5083166124Srafan</tr> 5084166124Srafan</table> 5085166124Srafan<p>The function set_field_opts() can be used to directly set 5086166124Srafanattributes of a field or you can choose to switch a few attributes 5087166124Srafanon and off with field_opts_on() and field_opts_off() selectively. 5088166124SrafanAnytime you can query the attributes of a field with field_opts(). 5089166124SrafanThe following is the list of available options. By default, all 5090166124Srafanoptions are on.</p> 5091166124Srafan<div class="VARIABLELIST"> 5092166124Srafan<dl> 5093166124Srafan<dt>O_VISIBLE</dt> 5094166124Srafan<dd> 5095166124Srafan<p>Controls whether the field is visible on the screen. Can be used 5096166124Srafanduring form processing to hide or pop up fields depending on the 5097166124Srafanvalue of parent fields.</p> 5098166124Srafan</dd> 5099166124Srafan<dt>O_ACTIVE</dt> 5100166124Srafan<dd> 5101166124Srafan<p>Controls whether the field is active during forms processing 5102166124Srafan(i.e. visited by form navigation keys). Can be used to make labels 5103166124Srafanor derived fields with buffer values alterable by the forms 5104166124Srafanapplication, not the user.</p> 5105166124Srafan</dd> 5106166124Srafan<dt>O_PUBLIC</dt> 5107166124Srafan<dd> 5108166124Srafan<p>Controls whether data is displayed during field entry. If this 5109166124Srafanoption is turned off on a field, the library will accept and edit 5110166124Srafandata in that field, but it will not be displayed and the visible 5111166124Srafanfield cursor will not move. You can turn off the O_PUBLIC bit to 5112166124Srafandefine password fields.</p> 5113166124Srafan</dd> 5114166124Srafan<dt>O_EDIT</dt> 5115166124Srafan<dd> 5116166124Srafan<p>Controls whether the field's data can be modified. When this 5117166124Srafanoption is off, all editing requests except <var class= 5118166124Srafan"LITERAL">REQ_PREV_CHOICE</var> and <var class= 5119166124Srafan"LITERAL">REQ_NEXT_CHOICE</var>will fail. Such read-only fields may 5120166124Srafanbe useful for help messages.</p> 5121166124Srafan</dd> 5122166124Srafan<dt>O_WRAP</dt> 5123166124Srafan<dd> 5124166124Srafan<p>Controls word-wrapping in multi-line fields. Normally, when any 5125166124Srafancharacter of a (blank-separated) word reaches the end of the 5126166124Srafancurrent line, the entire word is wrapped to the next line (assuming 5127166124Srafanthere is one). When this option is off, the word will be split 5128166124Srafanacross the line break.</p> 5129166124Srafan</dd> 5130166124Srafan<dt>O_BLANK</dt> 5131166124Srafan<dd> 5132166124Srafan<p>Controls field blanking. When this option is on, entering a 5133166124Srafancharacter at the first field position erases the entire field 5134166124Srafan(except for the just-entered character).</p> 5135166124Srafan</dd> 5136166124Srafan<dt>O_AUTOSKIP</dt> 5137166124Srafan<dd> 5138166124Srafan<p>Controls automatic skip to next field when this one fills. 5139166124SrafanNormally, when the forms user tries to type more data into a field 5140166124Srafanthan will fit, the editing location jumps to next field. When this 5141166124Srafanoption is off, the user's cursor will hang at the end of the field. 5142166124SrafanThis option is ignored in dynamic fields that have not reached 5143166124Srafantheir size limit.</p> 5144166124Srafan</dd> 5145166124Srafan<dt>O_NULLOK</dt> 5146166124Srafan<dd> 5147166124Srafan<p>Controls whether validation is applied to blank fields. 5148166124SrafanNormally, it is not; the user can leave a field blank without 5149166124Srafaninvoking the usual validation check on exit. If this option is off 5150166124Srafanon a field, exit from it will invoke a validation check.</p> 5151166124Srafan</dd> 5152166124Srafan<dt>O_PASSOK</dt> 5153166124Srafan<dd> 5154166124Srafan<p>Controls whether validation occurs on every exit, or only after 5155166124Srafanthe field is modified. Normally the latter is true. Setting 5156166124SrafanO_PASSOK may be useful if your field's validation function may 5157166124Srafanchange during forms processing.</p> 5158166124Srafan</dd> 5159166124Srafan<dt>O_STATIC</dt> 5160166124Srafan<dd> 5161166124Srafan<p>Controls whether the field is fixed to its initial dimensions. 5162166124SrafanIf you turn this off, the field becomes dynamic and will stretch to 5163166124Srafanfit entered data.</p> 5164166124Srafan</dd> 5165166124Srafan</dl> 5166166124Srafan</div> 5167166124Srafan<p>A field's options cannot be changed while the field is currently 5168166124Srafanselected. However, options may be changed on posted fields that are 5169166124Srafannot current.</p> 5170166124Srafan<p>The option values are bit-masks and can be composed with 5171166124Srafanlogical-or in the obvious way. You have seen the usage of switching 5172166124Srafanoff O_AUTOSKIP option. The following example clarifies usage of 5173166124Srafansome more options. Other options are explained where 5174166124Srafanappropriate.</p> 5175166124Srafan<div class="EXAMPLE"><a name="FFOOP" id="FFOOP"></a> 5176166124Srafan<p><b>Example 27. Field Options Usage example</b></p> 5177166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 5178166124Srafan<tr> 5179166124Srafan<td> 5180166124Srafan<pre class="PROGRAMLISTING"> 5181166124Srafan<font color="#000000"><span class= 5182166124Srafan"INLINEMEDIAOBJECT">#include <form.h> 5183166124Srafan 5184166124Srafan#define STARTX 15 5185166124Srafan#define STARTY 4 5186166124Srafan#define WIDTH 25 5187166124Srafan 5188166124Srafan#define N_FIELDS 3 5189166124Srafan 5190166124Srafanint main() 5191166124Srafan{ FIELD *field[N_FIELDS]; 5192166124Srafan FORM *my_form; 5193166124Srafan int ch, i; 5194166124Srafan 5195166124Srafan /* Initialize curses */ 5196166124Srafan initscr(); 5197166124Srafan cbreak(); 5198166124Srafan noecho(); 5199166124Srafan keypad(stdscr, TRUE); 5200166124Srafan 5201166124Srafan /* Initialize the fields */ 5202166124Srafan for(i = 0; i < N_FIELDS - 1; ++i) 5203166124Srafan field[i] = new_field(1, WIDTH, STARTY + i * 2, STARTX, 0, 0); 5204166124Srafan field[N_FIELDS - 1] = NULL; 5205166124Srafan 5206166124Srafan /* Set field options */ 5207166124Srafan set_field_back(field[1], A_UNDERLINE); /* Print a line for the option */ 5208166124Srafan 5209166124Srafan field_opts_off(field[0], O_ACTIVE); /* This field is a static label */ 5210166124Srafan field_opts_off(field[1], O_PUBLIC); /* This filed is like a password field*/ 5211166124Srafan field_opts_off(field[1], O_AUTOSKIP); /* To avoid entering the same field */ 5212166124Srafan /* after last character is entered */ 5213166124Srafan 5214166124Srafan /* Create the form and post it */ 5215166124Srafan my_form = new_form(field); 5216166124Srafan post_form(my_form); 5217166124Srafan refresh(); 5218166124Srafan 5219166124Srafan set_field_just(field[0], JUSTIFY_CENTER); /* Center Justification */ 5220166124Srafan set_field_buffer(field[0], 0, "This is a static Field"); 5221166124Srafan /* Initialize the field */ 5222166124Srafan mvprintw(STARTY, STARTX - 10, "Field 1:"); 5223166124Srafan mvprintw(STARTY + 2, STARTX - 10, "Field 2:"); 5224166124Srafan refresh(); 5225166124Srafan 5226166124Srafan /* Loop through to get user requests */ 5227166124Srafan while((ch = getch()) != KEY_F(1)) 5228166124Srafan { switch(ch) 5229166124Srafan { case KEY_DOWN: 5230166124Srafan /* Go to next field */ 5231166124Srafan form_driver(my_form, REQ_NEXT_FIELD); 5232166124Srafan /* Go to the end of the present buffer */ 5233166124Srafan /* Leaves nicely at the last character */ 5234166124Srafan form_driver(my_form, REQ_END_LINE); 5235166124Srafan break; 5236166124Srafan case KEY_UP: 5237166124Srafan /* Go to previous field */ 5238166124Srafan form_driver(my_form, REQ_PREV_FIELD); 5239166124Srafan form_driver(my_form, REQ_END_LINE); 5240166124Srafan break; 5241166124Srafan default: 5242166124Srafan /* If this is a normal character, it gets */ 5243166124Srafan /* Printed */ 5244166124Srafan form_driver(my_form, ch); 5245166124Srafan break; 5246166124Srafan } 5247166124Srafan } 5248166124Srafan 5249166124Srafan /* Un post form and free the memory */ 5250166124Srafan unpost_form(my_form); 5251166124Srafan free_form(my_form); 5252166124Srafan free_field(field[0]); 5253166124Srafan free_field(field[1]); 5254166124Srafan 5255166124Srafan endwin(); 5256166124Srafan return 0; 5257166124Srafan}</span></font> 5258166124Srafan</pre></td> 5259166124Srafan</tr> 5260166124Srafan</table> 5261166124Srafan</div> 5262166124Srafan<p>This example, though useless, shows the usage of options. If 5263166124Srafanused properly, they can present information very effectively in a 5264166124Srafanform. The second field being not O_PUBLIC, does not show the 5265166124Srafancharacters you are typing.</p> 5266166124Srafan</div> 5267166124Srafan<div class="SECT3"> 5268166124Srafan<hr> 5269166124Srafan<h4 class="SECT3"><a name="FIELDSTATUS" id="FIELDSTATUS">18.3.6. 5270166124SrafanField Status</a></h4> 5271166124Srafan<p>The field status specifies whether the field has got edited or 5272166124Srafannot. It is initially set to FALSE and when user enters something 5273166124Srafanand the data buffer gets modified it becomes TRUE. So a field's 5274166124Srafanstatus can be queried to find out whether it has been modified or 5275166124Srafannot. The following functions can assist in those operations.</p> 5276166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 5277166124Srafan<tr> 5278166124Srafan<td> 5279166124Srafan<pre class="PROGRAMLISTING"> 5280166124Srafan<font color= 5281166124Srafan"#000000">int set_field_status(FIELD *field, /* field to alter */ 5282166124Srafan int status); /* status to set */ 5283166124Srafan 5284166124Srafanint field_status(FIELD *field); /* fetch status of field */</font> 5285166124Srafan</pre></td> 5286166124Srafan</tr> 5287166124Srafan</table> 5288166124Srafan<p>It's better to check the field's status only after after leaving 5289166124Srafanthe field, as data buffer might not have been updated yet as the 5290166124Srafanvalidation is still due. To guarantee that right status is 5291166124Srafanreturned, call field_status() either (1) in the field's exit 5292166124Srafanvalidation check routine, (2) from the field's or form's 5293166124Srafaninitialization or termination hooks, or (3) just after a 5294166124SrafanREQ_VALIDATION request has been processed by the forms driver</p> 5295166124Srafan</div> 5296166124Srafan<div class="SECT3"> 5297166124Srafan<hr> 5298166124Srafan<h4 class="SECT3"><a name="FIELDUSERPTR" id="FIELDUSERPTR">18.3.7. 5299166124SrafanField User Pointer</a></h4> 5300166124Srafan<p>Every field structure contains one pointer that can be used by 5301166124Srafanthe user for various purposes. It is not touched by forms library 5302166124Srafanand can be used for any purpose by the user. The following 5303166124Srafanfunctions set and fetch user pointer.</p> 5304166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 5305166124Srafan<tr> 5306166124Srafan<td> 5307166124Srafan<pre class="PROGRAMLISTING"> 5308166124Srafan<font color="#000000">int set_field_userptr(FIELD *field, 5309166124Srafan char *userptr); /* the user pointer you wish to associate */ 5310166124Srafan /* with the field */ 5311166124Srafan 5312166124Srafanchar *field_userptr(FIELD *field); /* fetch user pointer of the field */</font> 5313166124Srafan</pre></td> 5314166124Srafan</tr> 5315166124Srafan</table> 5316166124Srafan</div> 5317166124Srafan<div class="SECT3"> 5318166124Srafan<hr> 5319166124Srafan<h4 class="SECT3"><a name="VARIABLESIZEFIELDS" id= 5320166124Srafan"VARIABLESIZEFIELDS">18.3.8. Variable-Sized Fields</a></h4> 5321166124Srafan<p>If you want a dynamically changing field with variable width, 5322166124Srafanthis is the feature you want to put to full use. This will allow 5323166124Srafanthe user to enter more data than the original size of the field and 5324166124Srafanlet the field grow. According to the field orientation it will 5325166124Srafanscroll horizontally or vertically to incorporate the new data.</p> 5326166124Srafan<p>To make a field dynamically growable, the option O_STATIC should 5327166124Srafanbe turned off. This can be done with a</p> 5328166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 5329166124Srafan<tr> 5330166124Srafan<td> 5331166124Srafan<pre class="PROGRAMLISTING"> 5332166124Srafan<font color= 5333166124Srafan"#000000"> field_opts_off(field_pointer, O_STATIC);</font> 5334166124Srafan</pre></td> 5335166124Srafan</tr> 5336166124Srafan</table> 5337166124Srafan<p>But it's usually not advisable to allow a field to grow 5338166124Srafaninfinitely. You can set a maximum limit to the growth of the field 5339166124Srafanwith</p> 5340166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 5341166124Srafan<tr> 5342166124Srafan<td> 5343166124Srafan<pre class="PROGRAMLISTING"> 5344166124Srafan<font color= 5345166124Srafan"#000000">int set_max_field(FIELD *field, /* Field on which to operate */ 5346166124Srafan int max_growth); /* maximum growth allowed for the field */</font> 5347166124Srafan</pre></td> 5348166124Srafan</tr> 5349166124Srafan</table> 5350166124Srafan<p>The field info for a dynamically growable field can be retrieved 5351166124Srafanby</p> 5352166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 5353166124Srafan<tr> 5354166124Srafan<td> 5355166124Srafan<pre class="PROGRAMLISTING"> 5356166124Srafan<font color= 5357166124Srafan"#000000">int dynamic_field_info( FIELD *field, /* Field on which to operate */ 5358166124Srafan int *prows, /* number of rows will be filled in this */ 5359166124Srafan int *pcols, /* number of columns will be filled in this*/ 5360166124Srafan int *pmax) /* maximum allowable growth will be filled */ 5361166124Srafan /* in this */</font> 5362166124Srafan</pre></td> 5363166124Srafan</tr> 5364166124Srafan</table> 5365166124SrafanThough field_info work as usual, it is advisable to use this 5366166124Srafanfunction to get the proper attributes of a dynamically growable 5367166124Srafanfield. 5368166124Srafan<p>Recall the library routine new_field; a new field created with 5369166124Srafanheight set to one will be defined to be a one line field. A new 5370166124Srafanfield created with height greater than one will be defined to be a 5371166124Srafanmulti line field.</p> 5372166124Srafan<p>A one line field with O_STATIC turned off (dynamically growable 5373166124Srafanfield) will contain a single fixed row, but the number of columns 5374166124Srafancan increase if the user enters more data than the initial field 5375166124Srafanwill hold. The number of columns displayed will remain fixed and 5376166124Srafanthe additional data will scroll horizontally.</p> 5377166124Srafan<p>A multi line field with O_STATIC turned off (dynamically 5378166124Srafangrowable field) will contain a fixed number of columns, but the 5379166124Srafannumber of rows can increase if the user enters more data than the 5380166124Srafaninitial field will hold. The number of rows displayed will remain 5381166124Srafanfixed and the additional data will scroll vertically.</p> 5382166124Srafan<p>The above two paragraphs pretty much describe a dynamically 5383166124Srafangrowable field's behavior. The way other parts of forms library 5384166124Srafanbehaves is described below:</p> 5385166124Srafan<ol type="1"> 5386166124Srafan<li> 5387166124Srafan<p>The field option O_AUTOSKIP will be ignored if the option 5388166124SrafanO_STATIC is off and there is no maximum growth specified for the 5389166124Srafanfield. Currently, O_AUTOSKIP generates an automatic REQ_NEXT_FIELD 5390166124Srafanform driver request when the user types in the last character 5391166124Srafanposition of a field. On a growable field with no maximum growth 5392166124Srafanspecified, there is no last character position. If a maximum growth 5393166124Srafanis specified, the O_AUTOSKIP option will work as normal if the 5394166124Srafanfield has grown to its maximum size.</p> 5395166124Srafan</li> 5396166124Srafan<li> 5397166124Srafan<p>The field justification will be ignored if the option O_STATIC 5398166124Srafanis off. Currently, set_field_just can be used to JUSTIFY_LEFT, 5399166124SrafanJUSTIFY_RIGHT, JUSTIFY_CENTER the contents of a one line field. A 5400166124Srafangrowable one line field will, by definition, grow and scroll 5401166124Srafanhorizontally and may contain more data than can be justified. The 5402166124Srafanreturn from field_just will be unchanged.</p> 5403166124Srafan</li> 5404166124Srafan<li> 5405166124Srafan<p>The overloaded form driver request REQ_NEW_LINE will operate the 5406166124Srafansame way regardless of the O_NL_OVERLOAD form option if the field 5407166124Srafanoption O_STATIC is off and there is no maximum growth specified for 5408166124Srafanthe field. Currently, if the form option O_NL_OVERLOAD is on, 5409166124SrafanREQ_NEW_LINE implicitly generates a REQ_NEXT_FIELD if called from 5410166124Srafanthe last line of a field. If a field can grow without bound, there 5411166124Srafanis no last line, so REQ_NEW_LINE will never implicitly generate a 5412166124SrafanREQ_NEXT_FIELD. If a maximum growth limit is specified and the 5413166124SrafanO_NL_OVERLOAD form option is on, REQ_NEW_LINE will only implicitly 5414166124Srafangenerate REQ_NEXT_FIELD if the field has grown to its maximum size 5415166124Srafanand the user is on the last line.</p> 5416166124Srafan</li> 5417166124Srafan<li> 5418166124Srafan<p>The library call dup_field will work as usual; it will duplicate 5419166124Srafanthe field, including the current buffer size and contents of the 5420166124Srafanfield being duplicated. Any specified maximum growth will also be 5421166124Srafanduplicated.</p> 5422166124Srafan</li> 5423166124Srafan<li> 5424166124Srafan<p>The library call link_field will work as usual; it will 5425166124Srafanduplicate all field attributes and share buffers with the field 5426166124Srafanbeing linked. If the O_STATIC field option is subsequently changed 5427166124Srafanby a field sharing buffers, how the system reacts to an attempt to 5428166124Srafanenter more data into the field than the buffer will currently hold 5429166124Srafanwill depend on the setting of the option in the current field.</p> 5430166124Srafan</li> 5431166124Srafan<li> 5432166124Srafan<p>The library call field_info will work as usual; the variable 5433166124Srafannrow will contain the value of the original call to new_field. The 5434166124Srafanuser should use dynamic_field_info, described above, to query the 5435166124Srafancurrent size of the buffer.</p> 5436166124Srafan</li> 5437166124Srafan</ol> 5438166124Srafan<p>Some of the above points make sense only after explaining form 5439166124Srafandriver. We will be looking into that in next few sections.</p> 5440166124Srafan</div> 5441166124Srafan</div> 5442166124Srafan<div class="SECT2"> 5443166124Srafan<hr> 5444166124Srafan<h3 class="SECT2"><a name="FORMWINDOWS" id="FORMWINDOWS">18.4. Form 5445166124SrafanWindows</a></h3> 5446166124Srafan<p>The form windows concept is pretty much similar to menu windows. 5447166124SrafanEvery form is associated with a main window and a sub window. The 5448166124Srafanform main window displays any title or border associated or 5449166124Srafanwhatever the user wishes. Then the sub window contains all the 5450166124Srafanfields and displays them according to their position. This gives 5451166124Srafanthe flexibility of manipulating fancy form displaying very 5452166124Srafaneasily.</p> 5453166124Srafan<p>Since this is pretty much similar to menu windows, I am 5454166124Srafanproviding an example with out much explanation. The functions are 5455166124Srafansimilar and they work the same way.</p> 5456166124Srafan<div class="EXAMPLE"><a name="FFOWI" id="FFOWI"></a> 5457166124Srafan<p><b>Example 28. Form Windows Example</b></p> 5458166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 5459166124Srafan<tr> 5460166124Srafan<td> 5461166124Srafan<pre class="PROGRAMLISTING"> 5462166124Srafan<font color="#000000"><span class= 5463166124Srafan"INLINEMEDIAOBJECT">#include <form.h> 5464166124Srafan 5465166124Srafanvoid print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); 5466166124Srafan 5467166124Srafanint main() 5468166124Srafan{ 5469166124Srafan FIELD *field[3]; 5470166124Srafan FORM *my_form; 5471166124Srafan WINDOW *my_form_win; 5472166124Srafan int ch, rows, cols; 5473166124Srafan 5474166124Srafan /* Initialize curses */ 5475166124Srafan initscr(); 5476166124Srafan start_color(); 5477166124Srafan cbreak(); 5478166124Srafan noecho(); 5479166124Srafan keypad(stdscr, TRUE); 5480166124Srafan 5481166124Srafan /* Initialize few color pairs */ 5482166124Srafan init_pair(1, COLOR_RED, COLOR_BLACK); 5483166124Srafan 5484166124Srafan /* Initialize the fields */ 5485166124Srafan field[0] = new_field(1, 10, 6, 1, 0, 0); 5486166124Srafan field[1] = new_field(1, 10, 8, 1, 0, 0); 5487166124Srafan field[2] = NULL; 5488166124Srafan 5489166124Srafan /* Set field options */ 5490166124Srafan set_field_back(field[0], A_UNDERLINE); 5491166124Srafan field_opts_off(field[0], O_AUTOSKIP); /* Don't go to next field when this */ 5492166124Srafan /* Field is filled up */ 5493166124Srafan set_field_back(field[1], A_UNDERLINE); 5494166124Srafan field_opts_off(field[1], O_AUTOSKIP); 5495166124Srafan 5496166124Srafan /* Create the form and post it */ 5497166124Srafan my_form = new_form(field); 5498166124Srafan 5499166124Srafan /* Calculate the area required for the form */ 5500166124Srafan scale_form(my_form, &rows, &cols); 5501166124Srafan 5502166124Srafan /* Create the window to be associated with the form */ 5503166124Srafan my_form_win = newwin(rows + 4, cols + 4, 4, 4); 5504166124Srafan keypad(my_form_win, TRUE); 5505166124Srafan 5506166124Srafan /* Set main window and sub window */ 5507166124Srafan set_form_win(my_form, my_form_win); 5508166124Srafan set_form_sub(my_form, derwin(my_form_win, rows, cols, 2, 2)); 5509166124Srafan 5510166124Srafan /* Print a border around the main window and print a title */ 5511166124Srafan box(my_form_win, 0, 0); 5512166124Srafan print_in_middle(my_form_win, 1, 0, cols + 4, "My Form", COLOR_PAIR(1)); 5513166124Srafan 5514166124Srafan post_form(my_form); 5515166124Srafan wrefresh(my_form_win); 5516166124Srafan 5517166124Srafan mvprintw(LINES - 2, 0, "Use UP, DOWN arrow keys to switch between fields"); 5518166124Srafan refresh(); 5519166124Srafan 5520166124Srafan /* Loop through to get user requests */ 5521166124Srafan while((ch = wgetch(my_form_win)) != KEY_F(1)) 5522166124Srafan { switch(ch) 5523166124Srafan { case KEY_DOWN: 5524166124Srafan /* Go to next field */ 5525166124Srafan form_driver(my_form, REQ_NEXT_FIELD); 5526166124Srafan /* Go to the end of the present buffer */ 5527166124Srafan /* Leaves nicely at the last character */ 5528166124Srafan form_driver(my_form, REQ_END_LINE); 5529166124Srafan break; 5530166124Srafan case KEY_UP: 5531166124Srafan /* Go to previous field */ 5532166124Srafan form_driver(my_form, REQ_PREV_FIELD); 5533166124Srafan form_driver(my_form, REQ_END_LINE); 5534166124Srafan break; 5535166124Srafan default: 5536166124Srafan /* If this is a normal character, it gets */ 5537166124Srafan /* Printed */ 5538166124Srafan form_driver(my_form, ch); 5539166124Srafan break; 5540166124Srafan } 5541166124Srafan } 5542166124Srafan 5543166124Srafan /* Un post form and free the memory */ 5544166124Srafan unpost_form(my_form); 5545166124Srafan free_form(my_form); 5546166124Srafan free_field(field[0]); 5547166124Srafan free_field(field[1]); 5548166124Srafan 5549166124Srafan endwin(); 5550166124Srafan return 0; 5551166124Srafan} 5552166124Srafan 5553166124Srafanvoid print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) 5554166124Srafan{ int length, x, y; 5555166124Srafan float temp; 5556166124Srafan 5557166124Srafan if(win == NULL) 5558166124Srafan win = stdscr; 5559166124Srafan getyx(win, y, x); 5560166124Srafan if(startx != 0) 5561166124Srafan x = startx; 5562166124Srafan if(starty != 0) 5563166124Srafan y = starty; 5564166124Srafan if(width == 0) 5565166124Srafan width = 80; 5566166124Srafan 5567166124Srafan length = strlen(string); 5568166124Srafan temp = (width - length)/ 2; 5569166124Srafan x = startx + (int)temp; 5570166124Srafan wattron(win, color); 5571166124Srafan mvwprintw(win, y, x, "%s", string); 5572166124Srafan wattroff(win, color); 5573166124Srafan refresh(); 5574166124Srafan}</span></font> 5575166124Srafan</pre></td> 5576166124Srafan</tr> 5577166124Srafan</table> 5578166124Srafan</div> 5579166124Srafan</div> 5580166124Srafan<div class="SECT2"> 5581166124Srafan<hr> 5582166124Srafan<h3 class="SECT2"><a name="FILEDVALIDATE" id="FILEDVALIDATE">18.5. 5583166124SrafanField Validation</a></h3> 5584166124Srafan<p>By default, a field will accept any data input by the user. It 5585166124Srafanis possible to attach validation to the field. Then any attempt by 5586166124Srafanthe user to leave the field, while it contains data that doesn't 5587166124Srafanmatch the validation type will fail. Some validation types also 5588166124Srafanhave a character-validity check for each time a character is 5589166124Srafanentered in the field.</p> 5590166124Srafan<p>Validation can be attached to a field with the following 5591166124Srafanfunction.</p> 5592166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 5593166124Srafan<tr> 5594166124Srafan<td> 5595166124Srafan<pre class="PROGRAMLISTING"> 5596166124Srafan<font color= 5597166124Srafan"#000000">int set_field_type(FIELD *field, /* field to alter */ 5598166124Srafan FIELDTYPE *ftype, /* type to associate */ 5599166124Srafan ...); /* additional arguments*/</font> 5600166124Srafan</pre></td> 5601166124Srafan</tr> 5602166124Srafan</table> 5603166124SrafanOnce set, the validation type for a field can be queried with 5604166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 5605166124Srafan<tr> 5606166124Srafan<td> 5607166124Srafan<pre class="PROGRAMLISTING"> 5608166124Srafan<font color= 5609166124Srafan"#000000">FIELDTYPE *field_type(FIELD *field); /* field to query */</font> 5610166124Srafan</pre></td> 5611166124Srafan</tr> 5612166124Srafan</table> 5613166124Srafan<p>The form driver validates the data in a field only when data is 5614166124Srafanentered by the end-user. Validation does not occur when</p> 5615166124Srafan<ul> 5616166124Srafan<li> 5617166124Srafan<p>the application program changes the field value by calling 5618166124Srafanset_field_buffer.</p> 5619166124Srafan</li> 5620166124Srafan<li> 5621166124Srafan<p>linked field values are changed indirectly -- by changing the 5622166124Srafanfield to which they are linked</p> 5623166124Srafan</li> 5624166124Srafan</ul> 5625166124Srafan<p>The following are the pre-defined validation types. You can also 5626166124Srafanspecify custom validation, though it's a bit tricky and 5627166124Srafancumbersome.</p> 5628166124Srafan<h1 class="BRIDGEHEAD"><a name="AEN1069" id= 5629166124Srafan"AEN1069"></a>TYPE_ALPHA</h1> 5630166124Srafan<p>This field type accepts alphabetic data; no blanks, no digits, 5631166124Srafanno special characters (this is checked at character-entry time). It 5632166124Srafanis set up with:</p> 5633166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 5634166124Srafan<tr> 5635166124Srafan<td> 5636166124Srafan<pre class="PROGRAMLISTING"> 5637166124Srafan<font color= 5638166124Srafan"#000000">int set_field_type(FIELD *field, /* field to alter */ 5639166124Srafan TYPE_ALPHA, /* type to associate */ 5640166124Srafan int width); /* maximum width of field */</font> 5641166124Srafan</pre></td> 5642166124Srafan</tr> 5643166124Srafan</table> 5644166124Srafan<p>The width argument sets a minimum width of data. The user has to 5645166124Srafanenter at-least width number of characters before he can leave the 5646166124Srafanfield. Typically you'll want to set this to the field width; if 5647166124Srafanit's greater than the field width, the validation check will always 5648166124Srafanfail. A minimum width of zero makes field completion optional.</p> 5649166124Srafan<h1 class="BRIDGEHEAD"><a name="AEN1073" id= 5650166124Srafan"AEN1073"></a>TYPE_ALNUM</h1> 5651166124Srafan<p>This field type accepts alphabetic data and digits; no blanks, 5652166124Srafanno special characters (this is checked at character-entry time). It 5653166124Srafanis set up with:</p> 5654166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 5655166124Srafan<tr> 5656166124Srafan<td> 5657166124Srafan<pre class="PROGRAMLISTING"> 5658166124Srafan<font color= 5659166124Srafan"#000000">int set_field_type(FIELD *field, /* field to alter */ 5660166124Srafan TYPE_ALNUM, /* type to associate */ 5661166124Srafan int width); /* maximum width of field */</font> 5662166124Srafan</pre></td> 5663166124Srafan</tr> 5664166124Srafan</table> 5665166124Srafan<p>The width argument sets a minimum width of data. As with 5666166124SrafanTYPE_ALPHA, typically you'll want to set this to the field width; 5667166124Srafanif it's greater than the field width, the validation check will 5668166124Srafanalways fail. A minimum width of zero makes field completion 5669166124Srafanoptional.</p> 5670166124Srafan<h1 class="BRIDGEHEAD"><a name="AEN1077" id= 5671166124Srafan"AEN1077"></a>TYPE_ENUM</h1> 5672166124Srafan<p>This type allows you to restrict a field's values to be among a 5673166124Srafanspecified set of string values (for example, the two-letter postal 5674166124Srafancodes for U.S. states). It is set up with:</p> 5675166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 5676166124Srafan<tr> 5677166124Srafan<td> 5678166124Srafan<pre class="PROGRAMLISTING"> 5679166124Srafan<font color= 5680166124Srafan"#000000">int set_field_type(FIELD *field, /* field to alter */ 5681166124Srafan TYPE_ENUM, /* type to associate */ 5682166124Srafan char **valuelist; /* list of possible values */ 5683166124Srafan int checkcase; /* case-sensitive? */ 5684166124Srafan int checkunique); /* must specify uniquely? */</font> 5685166124Srafan</pre></td> 5686166124Srafan</tr> 5687166124Srafan</table> 5688166124Srafan<p>The valuelist parameter must point at a NULL-terminated list of 5689166124Srafanvalid strings. The checkcase argument, if true, makes comparison 5690166124Srafanwith the string case-sensitive.</p> 5691166124Srafan<p>When the user exits a TYPE_ENUM field, the validation procedure 5692166124Srafantries to complete the data in the buffer to a valid entry. If a 5693166124Srafancomplete choice string has been entered, it is of course valid. But 5694166124Srafanit is also possible to enter a prefix of a valid string and have it 5695166124Srafancompleted for you.</p> 5696166124Srafan<p>By default, if you enter such a prefix and it matches more than 5697166124Srafanone value in the string list, the prefix will be completed to the 5698166124Srafanfirst matching value. But the checkunique argument, if true, 5699166124Srafanrequires prefix matches to be unique in order to be valid.</p> 5700166124Srafan<p>The REQ_NEXT_CHOICE and REQ_PREV_CHOICE input requests can be 5701166124Srafanparticularly useful with these fields.</p> 5702166124Srafan<h1 class="BRIDGEHEAD"><a name="AEN1084" id= 5703166124Srafan"AEN1084"></a>TYPE_INTEGER</h1> 5704166124Srafan<p>This field type accepts an integer. It is set up as follows:</p> 5705166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 5706166124Srafan<tr> 5707166124Srafan<td> 5708166124Srafan<pre class="PROGRAMLISTING"> 5709166124Srafan<font color= 5710166124Srafan"#000000">int set_field_type(FIELD *field, /* field to alter */ 5711166124Srafan TYPE_INTEGER, /* type to associate */ 5712166124Srafan int padding, /* # places to zero-pad to */ 5713166124Srafan int vmin, int vmax); /* valid range */</font> 5714166124Srafan</pre></td> 5715166124Srafan</tr> 5716166124Srafan</table> 5717166124Srafan<p>Valid characters consist of an optional leading minus and 5718166124Srafandigits. The range check is performed on exit. If the range maximum 5719166124Srafanis less than or equal to the minimum, the range is ignored.</p> 5720166124Srafan<p>If the value passes its range check, it is padded with as many 5721166124Srafanleading zero digits as necessary to meet the padding argument.</p> 5722166124Srafan<p>A TYPE_INTEGER value buffer can conveniently be interpreted with 5723166124Srafanthe C library function atoi(3).</p> 5724166124Srafan<h1 class="BRIDGEHEAD"><a name="AEN1090" id= 5725166124Srafan"AEN1090"></a>TYPE_NUMERIC</h1> 5726166124Srafan<p>This field type accepts a decimal number. It is set up as 5727166124Srafanfollows:</p> 5728166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 5729166124Srafan<tr> 5730166124Srafan<td> 5731166124Srafan<pre class="PROGRAMLISTING"> 5732166124Srafan<font color= 5733166124Srafan"#000000">int set_field_type(FIELD *field, /* field to alter */ 5734166124Srafan TYPE_NUMERIC, /* type to associate */ 5735166124Srafan int padding, /* # places of precision */ 5736166124Srafan int vmin, int vmax); /* valid range */</font> 5737166124Srafan</pre></td> 5738166124Srafan</tr> 5739166124Srafan</table> 5740166124Srafan<p>Valid characters consist of an optional leading minus and 5741166124Srafandigits. possibly including a decimal point. The range check is 5742166124Srafanperformed on exit. If the range maximum is less than or equal to 5743166124Srafanthe minimum, the range is ignored.</p> 5744166124Srafan<p>If the value passes its range check, it is padded with as many 5745166124Srafantrailing zero digits as necessary to meet the padding argument.</p> 5746166124Srafan<p>A TYPE_NUMERIC value buffer can conveniently be interpreted with 5747166124Srafanthe C library function atof(3).</p> 5748166124Srafan<h1 class="BRIDGEHEAD"><a name="AEN1096" id= 5749166124Srafan"AEN1096"></a>TYPE_REGEXP</h1> 5750166124Srafan<p>This field type accepts data matching a regular expression. It 5751166124Srafanis set up as follows:</p> 5752166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 5753166124Srafan<tr> 5754166124Srafan<td> 5755166124Srafan<pre class="PROGRAMLISTING"> 5756166124Srafan<font color= 5757166124Srafan"#000000">int set_field_type(FIELD *field, /* field to alter */ 5758166124Srafan TYPE_REGEXP, /* type to associate */ 5759166124Srafan char *regexp); /* expression to match */</font> 5760166124Srafan</pre></td> 5761166124Srafan</tr> 5762166124Srafan</table> 5763166124Srafan<p>The syntax for regular expressions is that of regcomp(3). The 5764166124Srafancheck for regular-expression match is performed on exit.</p> 5765166124Srafan</div> 5766166124Srafan<div class="SECT2"> 5767166124Srafan<hr> 5768166124Srafan<h3 class="SECT2"><a name="FORMDRIVER" id="FORMDRIVER">18.6. Form 5769166124SrafanDriver: The work horse of the forms system</a></h3> 5770166124Srafan<p>As in the menu system, form_driver() plays a very important role 5771166124Srafanin forms system. All types of requests to forms system should be 5772166124Srafanfunneled through form_driver().</p> 5773166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 5774166124Srafan<tr> 5775166124Srafan<td> 5776166124Srafan<pre class="PROGRAMLISTING"> 5777166124Srafan<font color= 5778166124Srafan"#000000">int form_driver(FORM *form, /* form on which to operate */ 5779166124Srafan int request) /* form request code */</font> 5780166124Srafan</pre></td> 5781166124Srafan</tr> 5782166124Srafan</table> 5783166124Srafan<p>As you have seen some of the examples above, you have to be in a 5784166124Srafanloop looking for user input and then decide whether it's a field 5785166124Srafandata or a form request. The form requests are then passed to 5786166124Srafanform_driver() to do the work.</p> 5787166124Srafan<p>The requests roughly can be divided into following categories. 5788166124SrafanDifferent requests and their usage is explained below:</p> 5789166124Srafan<div class="SECT3"> 5790166124Srafan<hr> 5791166124Srafan<h4 class="SECT3"><a name="PAGENAVREQ" id="PAGENAVREQ">18.6.1. Page 5792166124SrafanNavigation Requests</a></h4> 5793166124Srafan<p>These requests cause page-level moves through the form, 5794166124Srafantriggering display of a new form screen. A form can be made of 5795166124Srafanmultiple pages. If you have a big form with lot of fields and 5796166124Srafanlogical sections, then you can divide the form into pages. The 5797166124Srafanfunction set_new_page() to set a new page at the field 5798166124Srafanspecified.</p> 5799166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 5800166124Srafan<tr> 5801166124Srafan<td> 5802166124Srafan<pre class="PROGRAMLISTING"> 5803166124Srafan<font color= 5804166124Srafan"#000000">int set_new_page(FIELD *field,/* Field at which page break to be set or unset */ 5805166124Srafan bool new_page_flag); /* should be TRUE to put a break */</font> 5806166124Srafan</pre></td> 5807166124Srafan</tr> 5808166124Srafan</table> 5809166124Srafan<p>The following requests allow you to move to different pages</p> 5810166124Srafan<ul> 5811166124Srafan<li> 5812166124Srafan<p><em>REQ_NEXT_PAGE</em> Move to the next form page.</p> 5813166124Srafan</li> 5814166124Srafan<li> 5815166124Srafan<p><em>REQ_PREV_PAGE</em> Move to the previous form page.</p> 5816166124Srafan</li> 5817166124Srafan<li> 5818166124Srafan<p><em>REQ_FIRST_PAGE</em> Move to the first form page.</p> 5819166124Srafan</li> 5820166124Srafan<li> 5821166124Srafan<p><em>REQ_LAST_PAGE</em> Move to the last form page.</p> 5822166124Srafan</li> 5823166124Srafan</ul> 5824166124Srafan<p>These requests treat the list as cyclic; that is, REQ_NEXT_PAGE 5825166124Srafanfrom the last page goes to the first, and REQ_PREV_PAGE from the 5826166124Srafanfirst page goes to the last.</p> 5827166124Srafan</div> 5828166124Srafan<div class="SECT3"> 5829166124Srafan<hr> 5830166124Srafan<h4 class="SECT3"><a name="INTERFIELDNAVREQ" id= 5831166124Srafan"INTERFIELDNAVREQ">18.6.2. Inter-Field Navigation Requests</a></h4> 5832166124Srafan<p>These requests handle navigation between fields on the same 5833166124Srafanpage.</p> 5834166124Srafan<ul> 5835166124Srafan<li> 5836166124Srafan<p><em>REQ_NEXT_FIELD</em> Move to next field.</p> 5837166124Srafan</li> 5838166124Srafan<li> 5839166124Srafan<p><em>REQ_PREV_FIELD</em> Move to previous field.</p> 5840166124Srafan</li> 5841166124Srafan<li> 5842166124Srafan<p><em>REQ_FIRST_FIELD</em> Move to the first field.</p> 5843166124Srafan</li> 5844166124Srafan<li> 5845166124Srafan<p><em>REQ_LAST_FIELD</em> Move to the last field.</p> 5846166124Srafan</li> 5847166124Srafan<li> 5848166124Srafan<p><em>REQ_SNEXT_FIELD</em> Move to sorted next field.</p> 5849166124Srafan</li> 5850166124Srafan<li> 5851166124Srafan<p><em>REQ_SPREV_FIELD</em> Move to sorted previous field.</p> 5852166124Srafan</li> 5853166124Srafan<li> 5854166124Srafan<p><em>REQ_SFIRST_FIELD</em> Move to the sorted first field.</p> 5855166124Srafan</li> 5856166124Srafan<li> 5857166124Srafan<p><em>REQ_SLAST_FIELD</em> Move to the sorted last field.</p> 5858166124Srafan</li> 5859166124Srafan<li> 5860166124Srafan<p><em>REQ_LEFT_FIELD</em> Move left to field.</p> 5861166124Srafan</li> 5862166124Srafan<li> 5863166124Srafan<p><em>REQ_RIGHT_FIELD</em> Move right to field.</p> 5864166124Srafan</li> 5865166124Srafan<li> 5866166124Srafan<p><em>REQ_UP_FIELD</em> Move up to field.</p> 5867166124Srafan</li> 5868166124Srafan<li> 5869166124Srafan<p><em>REQ_DOWN_FIELD</em> Move down to field.</p> 5870166124Srafan</li> 5871166124Srafan</ul> 5872166124Srafan<p>These requests treat the list of fields on a page as cyclic; 5873166124Srafanthat is, REQ_NEXT_FIELD from the last field goes to the first, and 5874166124SrafanREQ_PREV_FIELD from the first field goes to the last. The order of 5875166124Srafanthe fields for these (and the REQ_FIRST_FIELD and REQ_LAST_FIELD 5876166124Srafanrequests) is simply the order of the field pointers in the form 5877166124Srafanarray (as set up by new_form() or set_form_fields()</p> 5878166124Srafan<p>It is also possible to traverse the fields as if they had been 5879166124Srafansorted in screen-position order, so the sequence goes left-to-right 5880166124Srafanand top-to-bottom. To do this, use the second group of four 5881166124Srafansorted-movement requests.</p> 5882166124Srafan<p>Finally, it is possible to move between fields using visual 5883166124Srafandirections up, down, right, and left. To accomplish this, use the 5884166124Srafanthird group of four requests. Note, however, that the position of a 5885166124Srafanform for purposes of these requests is its upper-left corner.</p> 5886166124Srafan<p>For example, suppose you have a multi-line field B, and two 5887166124Srafansingle-line fields A and C on the same line with B, with A to the 5888166124Srafanleft of B and C to the right of B. A REQ_MOVE_RIGHT from A will go 5889166124Srafanto B only if A, B, and C all share the same first line; otherwise 5890166124Srafanit will skip over B to C.</p> 5891166124Srafan</div> 5892166124Srafan<div class="SECT3"> 5893166124Srafan<hr> 5894166124Srafan<h4 class="SECT3"><a name="INTRAFIELDNAVREQ" id= 5895166124Srafan"INTRAFIELDNAVREQ">18.6.3. Intra-Field Navigation Requests</a></h4> 5896166124Srafan<p>These requests drive movement of the edit cursor within the 5897166124Srafancurrently selected field.</p> 5898166124Srafan<ul> 5899166124Srafan<li> 5900166124Srafan<p><em>REQ_NEXT_CHAR</em> Move to next character.</p> 5901166124Srafan</li> 5902166124Srafan<li> 5903166124Srafan<p><em>REQ_PREV_CHAR</em> Move to previous character.</p> 5904166124Srafan</li> 5905166124Srafan<li> 5906166124Srafan<p><em>REQ_NEXT_LINE</em> Move to next line.</p> 5907166124Srafan</li> 5908166124Srafan<li> 5909166124Srafan<p><em>REQ_PREV_LINE</em> Move to previous line.</p> 5910166124Srafan</li> 5911166124Srafan<li> 5912166124Srafan<p><em>REQ_NEXT_WORD</em> Move to next word.</p> 5913166124Srafan</li> 5914166124Srafan<li> 5915166124Srafan<p><em>REQ_PREV_WORD</em> Move to previous word.</p> 5916166124Srafan</li> 5917166124Srafan<li> 5918166124Srafan<p><em>REQ_BEG_FIELD</em> Move to beginning of field.</p> 5919166124Srafan</li> 5920166124Srafan<li> 5921166124Srafan<p><em>REQ_END_FIELD</em> Move to end of field.</p> 5922166124Srafan</li> 5923166124Srafan<li> 5924166124Srafan<p><em>REQ_BEG_LINE</em> Move to beginning of line.</p> 5925166124Srafan</li> 5926166124Srafan<li> 5927166124Srafan<p><em>REQ_END_LINE</em> Move to end of line.</p> 5928166124Srafan</li> 5929166124Srafan<li> 5930166124Srafan<p><em>REQ_LEFT_CHAR</em> Move left in field.</p> 5931166124Srafan</li> 5932166124Srafan<li> 5933166124Srafan<p><em>REQ_RIGHT_CHAR</em> Move right in field.</p> 5934166124Srafan</li> 5935166124Srafan<li> 5936166124Srafan<p><em>REQ_UP_CHAR</em> Move up in field.</p> 5937166124Srafan</li> 5938166124Srafan<li> 5939166124Srafan<p><em>REQ_DOWN_CHAR</em> Move down in field.</p> 5940166124Srafan</li> 5941166124Srafan</ul> 5942166124Srafan<p>Each word is separated from the previous and next characters by 5943166124Srafanwhitespace. The commands to move to beginning and end of line or 5944166124Srafanfield look for the first or last non-pad character in their 5945166124Srafanranges.</p> 5946166124Srafan</div> 5947166124Srafan<div class="SECT3"> 5948166124Srafan<hr> 5949166124Srafan<h4 class="SECT3"><a name="SCROLLREQ" id="SCROLLREQ">18.6.4. 5950166124SrafanScrolling Requests</a></h4> 5951166124Srafan<p>Fields that are dynamic and have grown and fields explicitly 5952166124Srafancreated with offscreen rows are scrollable. One-line fields scroll 5953166124Srafanhorizontally; multi-line fields scroll vertically. Most scrolling 5954166124Srafanis triggered by editing and intra-field movement (the library 5955166124Srafanscrolls the field to keep the cursor visible). It is possible to 5956166124Srafanexplicitly request scrolling with the following requests:</p> 5957166124Srafan<ul> 5958166124Srafan<li> 5959166124Srafan<p><em>REQ_SCR_FLINE</em> Scroll vertically forward a line.</p> 5960166124Srafan</li> 5961166124Srafan<li> 5962166124Srafan<p><em>REQ_SCR_BLINE</em> Scroll vertically backward a line.</p> 5963166124Srafan</li> 5964166124Srafan<li> 5965166124Srafan<p><em>REQ_SCR_FPAGE</em> Scroll vertically forward a page.</p> 5966166124Srafan</li> 5967166124Srafan<li> 5968166124Srafan<p><em>REQ_SCR_BPAGE</em> Scroll vertically backward a page.</p> 5969166124Srafan</li> 5970166124Srafan<li> 5971166124Srafan<p><em>REQ_SCR_FHPAGE</em> Scroll vertically forward half a 5972166124Srafanpage.</p> 5973166124Srafan</li> 5974166124Srafan<li> 5975166124Srafan<p><em>REQ_SCR_BHPAGE</em> Scroll vertically backward half a 5976166124Srafanpage.</p> 5977166124Srafan</li> 5978166124Srafan<li> 5979166124Srafan<p><em>REQ_SCR_FCHAR</em> Scroll horizontally forward a 5980166124Srafancharacter.</p> 5981166124Srafan</li> 5982166124Srafan<li> 5983166124Srafan<p><em>REQ_SCR_BCHAR</em> Scroll horizontally backward a 5984166124Srafancharacter.</p> 5985166124Srafan</li> 5986166124Srafan<li> 5987166124Srafan<p><em>REQ_SCR_HFLINE</em> Scroll horizontally one field width 5988166124Srafanforward.</p> 5989166124Srafan</li> 5990166124Srafan<li> 5991166124Srafan<p><em>REQ_SCR_HBLINE</em> Scroll horizontally one field width 5992166124Srafanbackward.</p> 5993166124Srafan</li> 5994166124Srafan<li> 5995166124Srafan<p><em>REQ_SCR_HFHALF</em> Scroll horizontally one half field width 5996166124Srafanforward.</p> 5997166124Srafan</li> 5998166124Srafan<li> 5999166124Srafan<p><em>REQ_SCR_HBHALF</em> Scroll horizontally one half field width 6000166124Srafanbackward.</p> 6001166124Srafan</li> 6002166124Srafan</ul> 6003166124Srafan<p>For scrolling purposes, a page of a field is the height of its 6004166124Srafanvisible part.</p> 6005166124Srafan</div> 6006166124Srafan<div class="SECT3"> 6007166124Srafan<hr> 6008166124Srafan<h4 class="SECT3"><a name="EDITREQ" id="EDITREQ">18.6.5. Editing 6009166124SrafanRequests</a></h4> 6010166124Srafan<p>When you pass the forms driver an ASCII character, it is treated 6011166124Srafanas a request to add the character to the field's data buffer. 6012166124SrafanWhether this is an insertion or a replacement depends on the 6013166124Srafanfield's edit mode (insertion is the default.</p> 6014166124Srafan<p>The following requests support editing the field and changing 6015166124Srafanthe edit mode:</p> 6016166124Srafan<ul> 6017166124Srafan<li> 6018166124Srafan<p><em>REQ_INS_MODE</em> Set insertion mode.</p> 6019166124Srafan</li> 6020166124Srafan<li> 6021166124Srafan<p><em>REQ_OVL_MODE</em> Set overlay mode.</p> 6022166124Srafan</li> 6023166124Srafan<li> 6024166124Srafan<p><em>REQ_NEW_LINE</em> New line request (see below for 6025166124Srafanexplanation).</p> 6026166124Srafan</li> 6027166124Srafan<li> 6028166124Srafan<p><em>REQ_INS_CHAR</em> Insert space at character location.</p> 6029166124Srafan</li> 6030166124Srafan<li> 6031166124Srafan<p><em>REQ_INS_LINE</em> Insert blank line at character 6032166124Srafanlocation.</p> 6033166124Srafan</li> 6034166124Srafan<li> 6035166124Srafan<p><em>REQ_DEL_CHAR</em> Delete character at cursor.</p> 6036166124Srafan</li> 6037166124Srafan<li> 6038166124Srafan<p><em>REQ_DEL_PREV</em> Delete previous word at cursor.</p> 6039166124Srafan</li> 6040166124Srafan<li> 6041166124Srafan<p><em>REQ_DEL_LINE</em> Delete line at cursor.</p> 6042166124Srafan</li> 6043166124Srafan<li> 6044166124Srafan<p><em>REQ_DEL_WORD</em> Delete word at cursor.</p> 6045166124Srafan</li> 6046166124Srafan<li> 6047166124Srafan<p><em>REQ_CLR_EOL</em> Clear to end of line.</p> 6048166124Srafan</li> 6049166124Srafan<li> 6050166124Srafan<p><em>REQ_CLR_EOF</em> Clear to end of field.</p> 6051166124Srafan</li> 6052166124Srafan<li> 6053166124Srafan<p><em>REQ_CLR_FIELD</em> Clear entire field.</p> 6054166124Srafan</li> 6055166124Srafan</ul> 6056166124Srafan<p>The behavior of the REQ_NEW_LINE and REQ_DEL_PREV requests is 6057166124Srafancomplicated and partly controlled by a pair of forms options. The 6058166124Srafanspecial cases are triggered when the cursor is at the beginning of 6059166124Srafana field, or on the last line of the field.</p> 6060166124Srafan<p>First, we consider REQ_NEW_LINE:</p> 6061166124Srafan<p>The normal behavior of REQ_NEW_LINE in insert mode is to break 6062166124Srafanthe current line at the position of the edit cursor, inserting the 6063166124Srafanportion of the current line after the cursor as a new line 6064166124Srafanfollowing the current and moving the cursor to the beginning of 6065166124Srafanthat new line (you may think of this as inserting a newline in the 6066166124Srafanfield buffer).</p> 6067166124Srafan<p>The normal behavior of REQ_NEW_LINE in overlay mode is to clear 6068166124Srafanthe current line from the position of the edit cursor to end of 6069166124Srafanline. The cursor is then moved to the beginning of the next 6070166124Srafanline.</p> 6071166124Srafan<p>However, REQ_NEW_LINE at the beginning of a field, or on the 6072166124Srafanlast line of a field, instead does a REQ_NEXT_FIELD. O_NL_OVERLOAD 6073166124Srafanoption is off, this special action is disabled.</p> 6074166124Srafan<p>Now, let us consider REQ_DEL_PREV:</p> 6075166124Srafan<p>The normal behavior of REQ_DEL_PREV is to delete the previous 6076166124Srafancharacter. If insert mode is on, and the cursor is at the start of 6077166124Srafana line, and the text on that line will fit on the previous one, it 6078166124Srafaninstead appends the contents of the current line to the previous 6079166124Srafanone and deletes the current line (you may think of this as deleting 6080166124Srafana newline from the field buffer).</p> 6081166124Srafan<p>However, REQ_DEL_PREV at the beginning of a field is instead 6082166124Srafantreated as a REQ_PREV_FIELD.</p> 6083166124Srafan<p>If the O_BS_OVERLOAD option is off, this special action is 6084166124Srafandisabled and the forms driver just returns E_REQUEST_DENIED.</p> 6085166124Srafan</div> 6086166124Srafan<div class="SECT3"> 6087166124Srafan<hr> 6088166124Srafan<h4 class="SECT3"><a name="ORDERREQ" id="ORDERREQ">18.6.6. Order 6089166124SrafanRequests</a></h4> 6090166124Srafan<p>If the type of your field is ordered, and has associated 6091166124Srafanfunctions for getting the next and previous values of the type from 6092166124Srafana given value, there are requests that can fetch that value into 6093166124Srafanthe field buffer:</p> 6094166124Srafan<ul> 6095166124Srafan<li> 6096166124Srafan<p><em>REQ_NEXT_CHOICE</em> Place the successor value of the 6097166124Srafancurrent value in the buffer.</p> 6098166124Srafan</li> 6099166124Srafan<li> 6100166124Srafan<p><em>REQ_PREV_CHOICE</em> Place the predecessor value of the 6101166124Srafancurrent value in the buffer.</p> 6102166124Srafan</li> 6103166124Srafan</ul> 6104166124Srafan<p>Of the built-in field types, only TYPE_ENUM has built-in 6105166124Srafansuccessor and predecessor functions. When you define a field type 6106166124Srafanof your own (see Custom Validation Types), you can associate our 6107166124Srafanown ordering functions.</p> 6108166124Srafan</div> 6109166124Srafan<div class="SECT3"> 6110166124Srafan<hr> 6111166124Srafan<h4 class="SECT3"><a name="APPLICCOMMANDS" id= 6112166124Srafan"APPLICCOMMANDS">18.6.7. Application Commands</a></h4> 6113166124Srafan<p>Form requests are represented as integers above the curses value 6114166124Srafangreater than KEY_MAX and less than or equal to the constant 6115166124SrafanMAX_COMMAND. A value within this range gets ignored by 6116166124Srafanform_driver(). So this can be used for any purpose by the 6117166124Srafanapplication. It can be treated as an application specific action 6118166124Srafanand take corresponding action.</p> 6119166124Srafan</div> 6120166124Srafan</div> 6121166124Srafan</div> 6122166124Srafan<div class="SECT1"> 6123166124Srafan<hr> 6124166124Srafan<h2 class="SECT1"><a name="TOOLS" id="TOOLS">19. Tools and Widget 6125166124SrafanLibraries</a></h2> 6126166124Srafan<p>Now that you have seen the capabilities of ncurses and its 6127166124Srafansister libraries, you are rolling your sleeves up and gearing for a 6128166124Srafanproject that heavily manipulates screen. But wait.. It can be 6129166124Srafanpretty difficult to write and maintain complex GUI widgets in plain 6130166124Srafanncurses or even with the additional libraries. There are some 6131166124Srafanready-to-use tools and widget libraries that can be used instead of 6132166124Srafanwriting your own widgets. You can use some of them, get ideas from 6133166124Srafanthe code, or even extend them.</p> 6134166124Srafan<div class="SECT2"> 6135166124Srafan<hr> 6136166124Srafan<h3 class="SECT2"><a name="CDK" id="CDK">19.1. CDK (Curses 6137166124SrafanDevelopment Kit)</a></h3> 6138166124Srafan<p>In the author's words</p> 6139166124Srafan<p><em>CDK stands for 'Curses Development Kit' and it currently 6140166124Srafancontains 21 ready to use widgets which facilitate the speedy 6141166124Srafandevelopment of full screen curses programs.</em></p> 6142166124Srafan<p>The kit provides some useful widgets, which can be used in your 6143166124Srafanprograms directly. It's pretty well written and the documentation 6144166124Srafanis very good. The examples in the examples directory can be a good 6145166124Srafanplace to start for beginners. The CDK can be downloaded from 6146166124Srafan<a href="http://invisible-island.net/cdk/" target= 6147166124Srafan"_top">http://invisible-island.net/cdk/</a> . Follow the 6148166124Srafaninstructions in README file to install it.</p> 6149166124Srafan<div class="SECT3"> 6150166124Srafan<hr> 6151166124Srafan<h4 class="SECT3"><a name="WIDGETLIST" id="WIDGETLIST">19.1.1. 6152166124SrafanWidget List</a></h4> 6153166124Srafan<p>The following is the list of widgets provided with cdk and their 6154166124Srafandescription.</p> 6155166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 6156166124Srafan<tr> 6157166124Srafan<td> 6158166124Srafan<pre class="PROGRAMLISTING"> 6159166124Srafan<font color="#000000">Widget Type Quick Description 6160166124Srafan=========================================================================== 6161166124SrafanAlphalist Allows a user to select from a list of words, with 6162166124Srafan the ability to narrow the search list by typing in a 6163166124Srafan few characters of the desired word. 6164166124SrafanButtonbox This creates a multiple button widget. 6165166124SrafanCalendar Creates a little simple calendar widget. 6166166124SrafanDialog Prompts the user with a message, and the user 6167166124Srafan can pick an answer from the buttons provided. 6168166124SrafanEntry Allows the user to enter various types of information. 6169166124SrafanFile Selector A file selector built from Cdk base widgets. This 6170166124Srafan example shows how to create more complicated widgets 6171166124Srafan using the Cdk widget library. 6172166124SrafanGraph Draws a graph. 6173166124SrafanHistogram Draws a histogram. 6174166124SrafanItem List Creates a pop up field which allows the user to select 6175166124Srafan one of several choices in a small field. Very useful 6176166124Srafan for things like days of the week or month names. 6177166124SrafanLabel Displays messages in a pop up box, or the label can be 6178166124Srafan considered part of the screen. 6179166124SrafanMarquee Displays a message in a scrolling marquee. 6180166124SrafanMatrix Creates a complex matrix with lots of options. 6181166124SrafanMenu Creates a pull-down menu interface. 6182166124SrafanMultiple Line Entry A multiple line entry field. Very useful 6183166124Srafan for long fields. (like a description 6184166124Srafan field) 6185166124SrafanRadio List Creates a radio button list. 6186166124SrafanScale Creates a numeric scale. Used for allowing a user to 6187166124Srafan pick a numeric value and restrict them to a range of 6188166124Srafan values. 6189166124SrafanScrolling List Creates a scrolling list/menu list. 6190166124SrafanScrolling Window Creates a scrolling log file viewer. Can add 6191166124Srafan information into the window while its running. 6192166124Srafan A good widget for displaying the progress of 6193166124Srafan something. (akin to a console window) 6194166124SrafanSelection List Creates a multiple option selection list. 6195166124SrafanSlider Akin to the scale widget, this widget provides a 6196166124Srafan visual slide bar to represent the numeric value. 6197166124SrafanTemplate Creates a entry field with character sensitive 6198166124Srafan positions. Used for pre-formatted fields like 6199166124Srafan dates and phone numbers. 6200166124SrafanViewer This is a file/information viewer. Very useful 6201166124Srafan when you need to display loads of information. 6202166124Srafan===========================================================================</font> 6203166124Srafan</pre></td> 6204166124Srafan</tr> 6205166124Srafan</table> 6206166124Srafan<p>A few of the widgets are modified by Thomas Dickey in recent 6207166124Srafanversions.</p> 6208166124Srafan</div> 6209166124Srafan<div class="SECT3"> 6210166124Srafan<hr> 6211166124Srafan<h4 class="SECT3"><a name="CDKATTRACT" id="CDKATTRACT">19.1.2. Some 6212166124SrafanAttractive Features</a></h4> 6213166124Srafan<p>Apart from making our life easier with readily usable widgets, 6214166124Srafancdk solves one frustrating problem with printing multi colored 6215166124Srafanstrings, justified strings elegantly. Special formatting tags can 6216166124Srafanbe embedded in the strings which are passed to CDK functions. For 6217166124SrafanExample</p> 6218166124Srafan<p>If the string</p> 6219166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 6220166124Srafan<tr> 6221166124Srafan<td> 6222166124Srafan<pre class="PROGRAMLISTING"> 6223166124Srafan<font color= 6224166124Srafan"#000000">"</B/1>This line should have a yellow foreground and a blue 6225166124Srafanbackground.<!1>"</font> 6226166124Srafan</pre></td> 6227166124Srafan</tr> 6228166124Srafan</table> 6229166124Srafan<p>given as a parameter to newCDKLabel(), it prints the line with 6230166124Srafanyellow foreground and blue background. There are other tags 6231166124Srafanavailable for justifying string, embedding special drawing 6232166124Srafancharacters etc.. Please refer to the man page cdk_display(3X) for 6233166124Srafandetails. The man page explains the usage with nice examples.</p> 6234166124Srafan</div> 6235166124Srafan<div class="SECT3"> 6236166124Srafan<hr> 6237166124Srafan<h4 class="SECT3"><a name="CDKCONCLUSION" id= 6238166124Srafan"CDKCONCLUSION">19.1.3. Conclusion</a></h4> 6239166124Srafan<p>All in all, CDK is a well-written package of widgets, which if 6240166124Srafanused properly can form a strong frame work for developing complex 6241166124SrafanGUI.</p> 6242166124Srafan</div> 6243166124Srafan</div> 6244166124Srafan<div class="SECT2"> 6245166124Srafan<hr> 6246166124Srafan<h3 class="SECT2"><a name="DIALOG" id="DIALOG">19.2. The 6247166124Srafandialog</a></h3> 6248166124Srafan<p>Long long ago, in September 1994, when few people knew linux, 6249166124SrafanJeff Tranter wrote an <a href= 6250166124Srafan"http://www2.linuxjournal.com/lj-issues/issue5/2807.html" target= 6251166124Srafan"_top">article</a> on dialog in Linux Journal. He starts the 6252166124Srafanarticle with these words..</p> 6253166124Srafan<p><em>Linux is based on the Unix operating system, but also 6254166124Srafanfeatures a number of unique and useful kernel features and 6255166124Srafanapplication programs that often go beyond what is available under 6256166124SrafanUnix. One little-known gem is "dialog", a utility for creating 6257166124Srafanprofessional-looking dialog boxes from within shell scripts. This 6258166124Srafanarticle presents a tutorial introduction to the dialog utility, and 6259166124Srafanshows examples of how and where it can be used</em></p> 6260166124Srafan<p>As he explains, dialog is a real gem in making 6261166124Srafanprofessional-looking dialog boxes with ease. It creates a variety 6262166124Srafanof dialog boxes, menus, check lists etc.. It is usually installed 6263166124Srafanby default. If not, you can download it from <a href= 6264166124Srafan"http://invisible-island.net/dialog/" target="_top">Thomas 6265166124SrafanDickey</a>'s site.</p> 6266166124Srafan<p>The above-mentioned article gives a very good overview of its 6267166124Srafanuses and capabilites. The man page has more details. It can be used 6268166124Srafanin variety of situations. One good example is building of linux 6269166124Srafankernel in text mode. Linux kernel uses a modified version of dialog 6270166124Srafantailored for its needs.</p> 6271166124Srafan<p>dialog was initially designed to be used with shell scripts. If 6272166124Srafanyou want to use its functionality in a c program, then you can use 6273166124Srafanlibdialog. The documentation regarding this is sparse. Definitive 6274166124Srafanreference is the dialog.h header file which comes with the library. 6275166124SrafanYou may need to hack here and there to get the required output. The 6276166124Srafansource is easily customizable. I have used it on a number of 6277166124Srafanoccasions by modifying the code.</p> 6278166124Srafan</div> 6279166124Srafan<div class="SECT2"> 6280166124Srafan<hr> 6281166124Srafan<h3 class="SECT2"><a name="PERLCURSES" id="PERLCURSES">19.3. Perl 6282166124SrafanCurses Modules CURSES::FORM and CURSES::WIDGETS</a></h3> 6283166124Srafan<p>The perl module Curses, Curses::Form and Curses::Widgets give 6284166124Srafanaccess to curses from perl. If you have curses and basic perl is 6285166124Srafaninstalled, you can get these modules from <a href= 6286166124Srafan"http://www.cpan.org/modules/01modules.index.html" target= 6287166124Srafan"_top">CPAN All Modules page</a>. Get the three zipped modules in 6288166124Srafanthe Curses category. Once installed you can use these modules from 6289166124Srafanperl scripts like any other module. For more information on perl 6290166124Srafanmodules see perlmod man page. The above modules come with good 6291166124Srafandocumentation and they have some demo scripts to test the 6292166124Srafanfunctionality. Though the widgets provided are very rudimentary, 6293166124Srafanthese modules provide good access to curses library from perl.</p> 6294166124Srafan<p>Some of my code examples are converted to perl by Anuradha 6295166124SrafanRatnaweera and they are available in the <var class= 6296166124Srafan"LITERAL">perl</var> directory.</p> 6297166124Srafan<p>For more information see man pages Curses(3) , Curses::Form(3) 6298166124Srafanand Curses::Widgets(3). These pages are installed only when the 6299166124Srafanabove modules are acquired and installed.</p> 6300166124Srafan</div> 6301166124Srafan</div> 6302166124Srafan<div class="SECT1"> 6303166124Srafan<hr> 6304166124Srafan<h2 class="SECT1"><a name="JUSTFORFUN" id="JUSTFORFUN">20. Just For 6305166124SrafanFun !!!</a></h2> 6306166124Srafan<p>This section contains few programs written by me just for fun. 6307166124SrafanThey don't signify a better programming practice or the best way of 6308166124Srafanusing ncurses. They are provided here so as to allow beginners to 6309166124Srafanget ideas and add more programs to this section. If you have 6310166124Srafanwritten a couple of nice, simple programs in curses and want them 6311166124Srafanto included here, contact <a href="mailto:ppadala@gmail.com" 6312166124Srafantarget="_top">me</a>.</p> 6313166124Srafan<div class="SECT2"> 6314166124Srafan<hr> 6315166124Srafan<h3 class="SECT2"><a name="GAMEOFLIFE" id="GAMEOFLIFE">20.1. The 6316166124SrafanGame of Life</a></h3> 6317166124Srafan<p>Game of life is a wonder of math. In <a href= 6318166124Srafan"http://www.math.com/students/wonders/life/life.html" target= 6319166124Srafan"_top">Paul Callahan</a>'s words</p> 6320166124Srafan<table border="0" bgcolor="#E0E0E0" width="100%"> 6321166124Srafan<tr> 6322166124Srafan<td> 6323166124Srafan<pre class="PROGRAMLISTING"> 6324166124Srafan<font color= 6325166124Srafan"#000000"><em>The Game of Life (or simply Life) is not a game in the conventional sense. There 6326166124Srafanare no players, and no winning or losing. Once the "pieces" are placed in the 6327166124Srafanstarting position, the rules determine everything that happens later. 6328166124SrafanNevertheless, Life is full of surprises! In most cases, it is impossible to look 6329166124Srafanat a starting position (or pattern) and see what will happen in the future. The 6330166124Srafanonly way to find out is to follow the rules of the game.</em></font> 6331166124Srafan</pre></td> 6332166124Srafan</tr> 6333166124Srafan</table> 6334166124Srafan<p>This program starts with a simple inverted U pattern and shows 6335166124Srafanhow wonderful life works. There is a lot of room for improvement in 6336166124Srafanthe program. You can let the user enter pattern of his choice or 6337166124Srafaneven take input from a file. You can also change rules and play 6338166124Srafanwith a lot of variations. Search on <a href="http://www.google.com" 6339166124Srafantarget="_top">google</a> for interesting information on game of 6340166124Srafanlife.</p> 6341166124Srafan<p><em>File Path: JustForFun/life.c</em></p> 6342166124Srafan</div> 6343166124Srafan<div class="SECT2"> 6344166124Srafan<hr> 6345166124Srafan<h3 class="SECT2"><a name="MAGIC" id="MAGIC">20.2. Magic 6346166124SrafanSquare</a></h3> 6347166124Srafan<p>Magic Square, another wonder of math, is very simple to 6348166124Srafanunderstand but very difficult to make. In a magic square sum of the 6349166124Srafannumbers in each row, each column is equal. Even diagnol sum can be 6350166124Srafanequal. There are many variations which have special properties.</p> 6351166124Srafan<p>This program creates a simple magic square of odd order.</p> 6352166124Srafan<p><em>File Path: JustForFun/magic.c</em></p> 6353166124Srafan</div> 6354166124Srafan<div class="SECT2"> 6355166124Srafan<hr> 6356166124Srafan<h3 class="SECT2"><a name="HANOI" id="HANOI">20.3. Towers of 6357166124SrafanHanoi</a></h3> 6358166124Srafan<p>The famous towers of hanoi solver. The aim of the game is to 6359166124Srafanmove the disks on the first peg to last peg, using middle peg as a 6360166124Srafantemporary stay. The catch is not to place a larger disk over a 6361166124Srafansmall disk at any time.</p> 6362166124Srafan<p><em>File Path: JustForFun/hanoi.c</em></p> 6363166124Srafan</div> 6364166124Srafan<div class="SECT2"> 6365166124Srafan<hr> 6366166124Srafan<h3 class="SECT2"><a name="QUEENS" id="QUEENS">20.4. Queens 6367166124SrafanPuzzle</a></h3> 6368166124Srafan<p>The objective of the famous N-Queen puzzle is to put N queens on 6369166124Srafana N X N chess board without attacking each other.</p> 6370166124Srafan<p>This program solves it with a simple backtracking technique.</p> 6371166124Srafan<p><em>File Path: JustForFun/queens.c</em></p> 6372166124Srafan</div> 6373166124Srafan<div class="SECT2"> 6374166124Srafan<hr> 6375166124Srafan<h3 class="SECT2"><a name="SHUFFLE" id="SHUFFLE">20.5. 6376166124SrafanShuffle</a></h3> 6377166124Srafan<p>A fun game, if you have time to kill.</p> 6378166124Srafan<p><em>File Path: JustForFun/shuffle.c</em></p> 6379166124Srafan</div> 6380166124Srafan<div class="SECT2"> 6381166124Srafan<hr> 6382166124Srafan<h3 class="SECT2"><a name="TT" id="TT">20.6. Typing Tutor</a></h3> 6383166124Srafan<p>A simple typing tutor, I created more out of need than for ease 6384166124Srafanof use. If you know how to put your fingers correctly on the 6385166124Srafankeyboard, but lack practice, this can be helpful.</p> 6386166124Srafan<p><em>File Path: JustForFun/tt.c</em></p> 6387166124Srafan</div> 6388166124Srafan</div> 6389166124Srafan<div class="SECT1"> 6390166124Srafan<hr> 6391166124Srafan<h2 class="SECT1"><a name="REF" id="REF">21. References</a></h2> 6392166124Srafan<ul> 6393166124Srafan<li> 6394166124Srafan<p>NCURSES man pages</p> 6395166124Srafan</li> 6396166124Srafan<li> 6397166124Srafan<p>NCURSES FAQ at <a href= 6398166124Srafan"http://invisible-island.net/ncurses/ncurses.faq.html" target= 6399166124Srafan"_top">http://invisible-island.net/ncurses/ncurses.faq.html</a></p> 6400166124Srafan</li> 6401166124Srafan<li> 6402166124Srafan<p>Writing programs with NCURSES by Eric Raymond and Zeyd M. 6403166124SrafanBen-Halim at <a href= 6404166124Srafan"http://invisible-island.net/ncurses/ncurses-intro.html" target= 6405166124Srafan"_top">http://invisible-island.net/ncurses/ncurses-intro.html</a> - 6406166124Srafansomewhat obsolete. I was inspired by this document and the 6407166124Srafanstructure of this HOWTO follows from the original document</p> 6408166124Srafan</li> 6409166124Srafan</ul> 6410166124Srafan</div> 6411166124Srafan</div> 6412166124Srafan</body> 6413166124Srafan</html> 6414