NCURSES-Programming-HOWTO.html (166125) | NCURSES-Programming-HOWTO.html (262685) |
---|---|
1 2<html> 3<head> 4<meta name="generator" content= 5"HTML Tidy for Linux/x86 (vers 1st December 2004), see www.w3.org"> 6<title>NCURSES Programming HOWTO</title> 7<meta name="GENERATOR" content= 8"Modular DocBook HTML Stylesheet Version 1.7"> 9</head> 10<body class="ARTICLE" bgcolor="#FFFFFF" text="#000000" link= 11"#0000FF" vlink="#840084" alink="#0000FF"> 12<div class="ARTICLE"> 13<div class="TITLEPAGE"> 14<h1 class="TITLE"><a name="AEN2" id="AEN2">NCURSES Programming 15HOWTO</a></h1> 16<h3 class="AUTHOR"><a name="AEN4" id="AEN4">Pradeep Padala</a></h3> 17<div class="AFFILIATION"> 18<div class="ADDRESS"> 19<p class="ADDRESS"><code class="EMAIL"><<a href= 20"mailto:ppadala@gmail.com">ppadala@gmail.com</a>></code></p> 21</div> 22</div> 23<p class="PUBDATE">v1.9, 2005-06-20<br></p> 24<div class="REVHISTORY"> 25<table width="100%" border="0"> 26<tr> 27<th align="left" valign="top" colspan="3"><b>Revision 28History</b></th> 29</tr> 30<tr> 31<td align="left">Revision 1.9</td> 32<td align="left">2005-06-20</td> 33<td align="left">Revised by: ppadala</td> 34</tr> 35<tr> 36<td align="left" colspan="3">The license has been changed to the 37MIT-style license used by NCURSES. Note that the programs are also 38re-licensed under this.</td> 39</tr> 40<tr> 41<td align="left">Revision 1.8</td> 42<td align="left">2005-06-17</td> 43<td align="left">Revised by: ppadala</td> 44</tr> 45<tr> 46<td align="left" colspan="3">Lots of updates. Added references and 47perl examples. Changes to examples. Many grammatical and stylistic 48changes to the content. Changes to NCURSES history.</td> 49</tr> 50<tr> 51<td align="left">Revision 1.7.1</td> 52<td align="left">2002-06-25</td> 53<td align="left">Revised by: ppadala</td> 54</tr> 55<tr> 56<td align="left" colspan="3">Added a README file for building and 57instructions for building from source.</td> 58</tr> 59<tr> 60<td align="left">Revision 1.7</td> 61<td align="left">2002-06-25</td> 62<td align="left">Revised by: ppadala</td> 63</tr> 64<tr> 65<td align="left" colspan="3">Added "Other formats" section and made 66a lot of fancy changes to the programs. Inlining of programs is 67gone.</td> 68</tr> 69<tr> 70<td align="left">Revision 1.6.1</td> 71<td align="left">2002-02-24</td> 72<td align="left">Revised by: ppadala</td> 73</tr> 74<tr> 75<td align="left" colspan="3">Removed the old Changelog section, 76cleaned the makefiles</td> 77</tr> 78<tr> 79<td align="left">Revision 1.6</td> 80<td align="left">2002-02-16</td> 81<td align="left">Revised by: ppadala</td> 82</tr> 83<tr> 84<td align="left" colspan="3">Corrected a lot of spelling mistakes, 85added ACS variables section</td> 86</tr> 87<tr> 88<td align="left">Revision 1.5</td> 89<td align="left">2002-01-05</td> 90<td align="left">Revised by: ppadala</td> 91</tr> 92<tr> 93<td align="left" colspan="3">Changed structure to present proper 94TOC</td> 95</tr> 96<tr> 97<td align="left">Revision 1.3.1</td> 98<td align="left">2001-07-26</td> 99<td align="left">Revised by: ppadala</td> 100</tr> 101<tr> 102<td align="left" colspan="3">Corrected maintainers paragraph, 103Corrected stable release number</td> 104</tr> 105<tr> 106<td align="left">Revision 1.3</td> 107<td align="left">2001-07-24</td> 108<td align="left">Revised by: ppadala</td> 109</tr> 110<tr> 111<td align="left" colspan="3">Added copyright notices to main 112document (LDP license) and programs (GPL), Corrected 113printw_example.</td> 114</tr> 115<tr> 116<td align="left">Revision 1.2</td> 117<td align="left">2001-06-05</td> 118<td align="left">Revised by: ppadala</td> 119</tr> 120<tr> 121<td align="left" colspan="3">Incorporated ravi's changes. Mainly to 122introduction, menu, form, justforfun sections</td> 123</tr> 124<tr> 125<td align="left">Revision 1.1</td> 126<td align="left">2001-05-22</td> 127<td align="left">Revised by: ppadala</td> 128</tr> 129<tr> 130<td align="left" colspan="3">Added "a word about window" section, 131Added scanw_example.</td> 132</tr> 133</table> 134</div> 135<div> 136<div class="ABSTRACT"><a name="AEN67" id="AEN67"></a> 137<p><em>This document is intended to be an "All in One" guide for 138programming with ncurses and its sister libraries. We graduate from 139a simple "Hello World" program to more complex form manipulation. 140No prior experience in ncurses is assumed. Send comments to 141<a href="mailto:ppadala@gmail.com" target="_top">this 142address</a></em></p> 143</div> 144</div> 145<hr></div> 146<div class="TOC"> 147<dl> 148<dt><b>Table of Contents</b></dt> 149<dt>1. <a href="#INTRO">Introduction</a></dt> 150<dd> 151<dl> 152<dt>1.1. <a href="#WHATIS">What is NCURSES?</a></dt> 153<dt>1.2. <a href="#WHATCANWEDO">What we can do with 154NCURSES</a></dt> 155<dt>1.3. <a href="#WHERETOGETIT">Where to get it</a></dt> 156<dt>1.4. <a href="#PURPOSE">Purpose/Scope of the document</a></dt> 157<dt>1.5. <a href="#ABOUTPROGRAMS">About the Programs</a></dt> 158<dt>1.6. <a href="#OTHERFORMATS">Other Formats of the 159document</a></dt> 160<dt>1.7. <a href="#CREDITS">Credits</a></dt> 161<dt>1.8. <a href="#WISHLIST">Wish List</a></dt> 162<dt>1.9. <a href="#COPYRIGHT">Copyright</a></dt> 163</dl> 164</dd> 165<dt>2. <a href="#HELLOWORLD">Hello World !!!</a></dt> 166<dd> 167<dl> 168<dt>2.1. <a href="#COMPILECURSES">Compiling With the NCURSES 169Library</a></dt> 170<dt>2.2. <a href="#DISSECTION">Dissection</a></dt> 171</dl> 172</dd> 173<dt>3. <a href="#GORY">The Gory Details</a></dt> 174<dt>4. <a href="#INIT">Initialization</a></dt> 175<dd> 176<dl> 177<dt>4.1. <a href="#ABOUTINIT">Initialization functions</a></dt> 178<dt>4.2. <a href="#RAWCBREAK">raw() and cbreak()</a></dt> 179<dt>4.3. <a href="#ECHONOECHO">echo() and noecho()</a></dt> 180<dt>4.4. <a href="#KEYPAD">keypad()</a></dt> 181<dt>4.5. <a href="#HALFDELAY">halfdelay()</a></dt> 182<dt>4.6. <a href="#MISCINIT">Miscellaneous Initialization 183functions</a></dt> 184<dt>4.7. <a href="#INITEX">An Example</a></dt> 185</dl> 186</dd> 187<dt>5. <a href="#AWORDWINDOWS">A Word about Windows</a></dt> 188<dt>6. <a href="#PRINTW">Output functions</a></dt> 189<dd> 190<dl> 191<dt>6.1. <a href="#ADDCHCLASS">addch() class of functions</a></dt> 192<dt>6.2. <a href="#AEN298">mvaddch(), waddch() and 193mvwaddch()</a></dt> 194<dt>6.3. <a href="#PRINTWCLASS">printw() class of 195functions</a></dt> 196<dt>6.4. <a href="#ADDSTRCLASS">addstr() class of 197functions</a></dt> 198<dt>6.5. <a href="#ACAUTION">A word of caution</a></dt> 199</dl> 200</dd> 201<dt>7. <a href="#SCANW">Input functions</a></dt> 202<dd> 203<dl> 204<dt>7.1. <a href="#GETCHCLASS">getch() class of functions</a></dt> 205<dt>7.2. <a href="#SCANWCLASS">scanw() class of functions</a></dt> 206<dt>7.3. <a href="#GETSTRCLASS">getstr() class of 207functions</a></dt> 208<dt>7.4. <a href="#GETSTREX">Some examples</a></dt> 209</dl> 210</dd> 211<dt>8. <a href="#ATTRIB">Attributes</a></dt> 212<dd> 213<dl> 214<dt>8.1. <a href="#ATTRIBDETAILS">The details</a></dt> 215<dt>8.2. <a href="#ATTRONVSATTRSET">attron() vs attrset()</a></dt> 216<dt>8.3. <a href="#ATTR_GET">attr_get()</a></dt> 217<dt>8.4. <a href="#ATTR_FUNCS">attr_ functions</a></dt> 218<dt>8.5. <a href="#WATTRFUNCS">wattr functions</a></dt> 219<dt>8.6. <a href="#CHGAT">chgat() functions</a></dt> 220</dl> 221</dd> 222<dt>9. <a href="#WINDOWS">Windows</a></dt> 223<dd> 224<dl> 225<dt>9.1. <a href="#WINDOWBASICS">The basics</a></dt> 226<dt>9.2. <a href="#LETBEWINDOW">Let there be a Window !!!</a></dt> 227<dt>9.3. <a href="#BORDEREXEXPL">Explanation</a></dt> 228<dt>9.4. <a href="#OTHERSTUFF">The other stuff in the 229example</a></dt> 230<dt>9.5. <a href="#OTHERBORDERFUNCS">Other Border 231functions</a></dt> 232</dl> 233</dd> 234<dt>10. <a href="#COLOR">Colors</a></dt> 235<dd> 236<dl> 237<dt>10.1. <a href="#COLORBASICS">The basics</a></dt> 238<dt>10.2. <a href="#CHANGECOLORDEFS">Changing Color 239Definitions</a></dt> 240<dt>10.3. <a href="#COLORCONTENT">Color Content</a></dt> 241</dl> 242</dd> 243<dt>11. <a href="#KEYS">Interfacing with the key board</a></dt> 244<dd> 245<dl> 246<dt>11.1. <a href="#KEYSBASICS">The Basics</a></dt> 247<dt>11.2. <a href="#SIMPLEKEYEX">A Simple Key Usage 248example</a></dt> 249</dl> 250</dd> 251<dt>12. <a href="#MOUSE">Interfacing with the mouse</a></dt> 252<dd> 253<dl> 254<dt>12.1. <a href="#MOUSEBASICS">The Basics</a></dt> 255<dt>12.2. <a href="#GETTINGEVENTS">Getting the events</a></dt> 256<dt>12.3. <a href="#MOUSETOGETHER">Putting it all Together</a></dt> 257<dt>12.4. <a href="#MISCMOUSEFUNCS">Miscellaneous 258Functions</a></dt> 259</dl> 260</dd> 261<dt>13. <a href="#SCREEN">Screen Manipulation</a></dt> 262<dd> 263<dl> 264<dt>13.1. <a href="#GETYX">getyx() functions</a></dt> 265<dt>13.2. <a href="#SCREENDUMP">Screen Dumping</a></dt> 266<dt>13.3. <a href="#WINDOWDUMP">Window Dumping</a></dt> 267</dl> 268</dd> 269<dt>14. <a href="#MISC">Miscellaneous features</a></dt> 270<dd> 271<dl> 272<dt>14.1. <a href="#CURSSET">curs_set()</a></dt> 273<dt>14.2. <a href="#TEMPLEAVE">Temporarily Leaving Curses 274mode</a></dt> 275<dt>14.3. <a href="#ACSVARS">ACS_ variables</a></dt> 276</dl> 277</dd> 278<dt>15. <a href="#OTHERLIB">Other libraries</a></dt> 279<dt>16. <a href="#PANELS">Panel Library</a></dt> 280<dd> 281<dl> 282<dt>16.1. <a href="#PANELBASICS">The Basics</a></dt> 283<dt>16.2. <a href="#COMPILEPANELS">Compiling With the Panels 284Library</a></dt> 285<dt>16.3. <a href="#PANELBROWSING">Panel Window Browsing</a></dt> 286<dt>16.4. <a href="#USERPTRUSING">Using User Pointers</a></dt> 287<dt>16.5. <a href="#PANELMOVERESIZE">Moving and Resizing 288Panels</a></dt> 289<dt>16.6. <a href="#PANELSHOWHIDE">Hiding and Showing 290Panels</a></dt> 291<dt>16.7. <a href="#PANELABOVE">panel_above() and panel_below() 292Functions</a></dt> 293</dl> 294</dd> 295<dt>17. <a href="#MENUS">Menus Library</a></dt> 296<dd> 297<dl> 298<dt>17.1. <a href="#MENUBASICS">The Basics</a></dt> 299<dt>17.2. <a href="#COMPILEMENUS">Compiling With the Menu 300Library</a></dt> 301<dt>17.3. <a href="#MENUDRIVER">Menu Driver: The work horse of the 302menu system</a></dt> 303<dt>17.4. <a href="#MENUWINDOWS">Menu Windows</a></dt> 304<dt>17.5. <a href="#SCROLLMENUS">Scrolling Menus</a></dt> 305<dt>17.6. <a href="#MULTICOLUMN">Multi Columnar Menus</a></dt> 306<dt>17.7. <a href="#MULTIVALUEMENUS">Multi Valued Menus</a></dt> 307<dt>17.8. <a href="#MENUOPT">Menu Options</a></dt> 308<dt>17.9. <a href="#MENUUSERPTR">The useful User Pointer</a></dt> 309</dl> 310</dd> 311<dt>18. <a href="#FORMS">Forms Library</a></dt> 312<dd> 313<dl> 314<dt>18.1. <a href="#FORMBASICS">The Basics</a></dt> 315<dt>18.2. <a href="#COMPILEFORMS">Compiling With the Forms 316Library</a></dt> 317<dt>18.3. <a href="#PLAYFIELDS">Playing with Fields</a></dt> 318<dt>18.4. <a href="#FORMWINDOWS">Form Windows</a></dt> 319<dt>18.5. <a href="#FILEDVALIDATE">Field Validation</a></dt> 320<dt>18.6. <a href="#FORMDRIVER">Form Driver: The work horse of the 321forms system</a></dt> 322</dl> 323</dd> 324<dt>19. <a href="#TOOLS">Tools and Widget Libraries</a></dt> 325<dd> 326<dl> 327<dt>19.1. <a href="#CDK">CDK (Curses Development Kit)</a></dt> 328<dt>19.2. <a href="#DIALOG">The dialog</a></dt> 329<dt>19.3. <a href="#PERLCURSES">Perl Curses Modules CURSES::FORM 330and CURSES::WIDGETS</a></dt> 331</dl> 332</dd> 333<dt>20. <a href="#JUSTFORFUN">Just For Fun !!!</a></dt> 334<dd> 335<dl> 336<dt>20.1. <a href="#GAMEOFLIFE">The Game of Life</a></dt> 337<dt>20.2. <a href="#MAGIC">Magic Square</a></dt> 338<dt>20.3. <a href="#HANOI">Towers of Hanoi</a></dt> 339<dt>20.4. <a href="#QUEENS">Queens Puzzle</a></dt> 340<dt>20.5. <a href="#SHUFFLE">Shuffle</a></dt> 341<dt>20.6. <a href="#TT">Typing Tutor</a></dt> 342</dl> 343</dd> 344<dt>21. <a href="#REF">References</a></dt> 345</dl> 346</div> 347<div class="SECT1"> 348<h2 class="SECT1"><a name="INTRO" id="INTRO">1. 349Introduction</a></h2> 350<p>In the olden days of teletype terminals, terminals were away 351from computers and were connected to them through serial cables. 352The terminals could be configured by sending a series of bytes. All 353the capabilities (such as moving the cursor to a new location, 354erasing part of the screen, scrolling the screen, changing modes 355etc.) of terminals could be accessed through these series of bytes. 356These control seeuqnces are usually called escape sequences, 357because they start with an escape(0x1B) character. Even today, with 358proper emulation, we can send escape sequences to the emulator and 359achieve the same effect on a terminal window.</p> 360<p>Suppose you wanted to print a line in color. Try typing this on 361your console.</p> 362<table border="0" bgcolor="#E0E0E0" width="100%"> 363<tr> 364<td> 365<pre class="PROGRAMLISTING"> 366<font color="#000000">echo "^[[0;31;40mIn Color"</font> 367</pre></td> 368</tr> 369</table> 370<p>The first character is an escape character, which looks like two 371characters ^ and [. To be able to print it, you have to press 372CTRL+V and then the ESC key. All the others are normal printable 373characters. You should be able to see the string "In Color" in red. 374It stays that way and to revert back to the original mode type 375this.</p> 376<table border="0" bgcolor="#E0E0E0" width="100%"> 377<tr> 378<td> 379<pre class="PROGRAMLISTING"> 380<font color="#000000">echo "^[[0;37;40m"</font> 381</pre></td> 382</tr> 383</table> 384<p>Now, what do these magic characters mean? Difficult to 385comprehend? They might even be different for different terminals. 386So the designers of UNIX invented a mechanism named <var class= 387"LITERAL">termcap</var>. It is a file that lists all the 388capabilities of a particular terminal, along with the escape 389sequences needed to achieve a particular effect. In the later 390years, this was replaced by <var class="LITERAL">terminfo</var>. 391Without delving too much into details, this mechanism allows 392application programs to query the terminfo database and obtain the 393control characters to be sent to a terminal or terminal 394emulator.</p> 395<div class="SECT2"> 396<hr> 397<h3 class="SECT2"><a name="WHATIS" id="WHATIS">1.1. What is 398NCURSES?</a></h3> 399<p>You might be wondering, what the import of all this technical 400gibberish is. In the above scenario, every application program is 401supposed to query the terminfo and perform the necessary stuff 402(sending control characters etc.). It soon became difficult to 403manage this complexity and this gave birth to 'CURSES'. Curses is a 404pun on the name "cursor optimization". The Curses library forms a 405wrapper over working with raw terminal codes, and provides highly 406flexible and efficient API (Application Programming Interface). It 407provides functions to move the cursor, create windows, produce 408colors, play with mouse etc. The application programs need not 409worry about the underlying terminal capabilities.</p> 410<p>So what is NCURSES? NCURSES is a clone of the original System V 411Release 4.0 (SVr4) curses. It is a freely distributable library, 412fully compatible with older version of curses. In short, it is a 413library of functions that manages an application's display on 414character-cell terminals. In the remainder of the document, the 415terms curses and ncurses are used interchangeably.</p> 416<p>A detailed history of NCURSES can be found in the NEWS file from 417the source distribution. The current package is maintained by 418<a href="mailto:dickey@his.com" target="_top">Thomas Dickey</a>. 419You can contact the maintainers at <a href= 420"mailto:bug-ncurses@gnu.org" target= 421"_top">bug-ncurses@gnu.org</a>.</p> 422</div> 423<div class="SECT2"> 424<hr> 425<h3 class="SECT2"><a name="WHATCANWEDO" id="WHATCANWEDO">1.2. What 426we can do with NCURSES</a></h3> 427<p>NCURSES not only creates a wrapper over terminal capabilities, 428but also gives a robust framework to create nice looking UI (User 429Interface)s in text mode. It provides functions to create windows 430etc. Its sister libraries panel, menu and form provide an extension 431to the basic curses library. These libraries usually come along 432with curses. One can create applications that contain multiple 433windows, menus, panels and forms. Windows can be managed 434independently, can provide 'scrollability' and even can be 435hidden.</p> 436<p>Menus provide the user with an easy command selection option. 437Forms allow the creation of easy-to-use data entry and display 438windows. Panels extend the capabilities of ncurses to deal with 439overlapping and stacked windows.</p> 440<p>These are just some of the basic things we can do with ncurses. 441As we move along, We will see all the capabilities of these 442libraries.</p> 443</div> 444<div class="SECT2"> 445<hr> 446<h3 class="SECT2"><a name="WHERETOGETIT" id="WHERETOGETIT">1.3. 447Where to get it</a></h3> 448<p>All right, now that you know what you can do with ncurses, you 449must be rearing to get started. NCURSES is usually shipped with 450your installation. In case you don't have the library or want to 451compile it on your own, read on.</p> 452<p><em>Compiling the package</em></p> 453<p>NCURSES can be obtained from <a href= 454"ftp://ftp.gnu.org/pub/gnu/ncurses/ncurses.tar.gz" target= 455"_top">ftp://ftp.gnu.org/pub/gnu/ncurses/ncurses.tar.gz</a> or any 456of the ftp sites mentioned in <a href= 457"http://www.gnu.org/order/ftp.html" target= 458"_top">http://www.gnu.org/order/ftp.html</a>.</p> 459<p>Read the README and INSTALL files for details on to how to 460install it. It usually involves the following operations.</p> 461<table border="0" bgcolor="#E0E0E0" width="100%"> 462<tr> 463<td> 464<pre class="PROGRAMLISTING"> 465<font color= 466"#000000"> tar zxvf ncurses<version>.tar.gz # unzip and untar the archive | 1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"> 2<HTML 3><HEAD 4><TITLE 5> NCURSES Programming HOWTO </TITLE 6><META 7NAME="GENERATOR" 8CONTENT="Modular DocBook HTML Stylesheet Version 1.79"></HEAD 9><BODY 10CLASS="ARTICLE" 11BGCOLOR="#FFFFFF" 12TEXT="#000000" 13LINK="#0000FF" 14VLINK="#840084" 15ALINK="#0000FF" 16><DIV 17CLASS="ARTICLE" 18><DIV 19CLASS="TITLEPAGE" 20><H1 21CLASS="TITLE" 22><A 23NAME="AEN2" 24>NCURSES Programming HOWTO</A 25></H1 26><H3 27CLASS="AUTHOR" 28><A 29NAME="AEN4" 30> Pradeep Padala </A 31></H3 32><DIV 33CLASS="AFFILIATION" 34><DIV 35CLASS="ADDRESS" 36><P 37CLASS="ADDRESS" 38><CODE 39CLASS="EMAIL" 40><<A 41HREF="mailto:ppadala@gmail.com" 42>ppadala@gmail.com</A 43>></CODE 44></P 45></DIV 46></DIV 47><P 48CLASS="PUBDATE" 49>v1.9, 2005-06-20<BR></P 50><DIV 51CLASS="REVHISTORY" 52><TABLE 53WIDTH="100%" 54BORDER="0" 55><TR 56><TH 57ALIGN="LEFT" 58VALIGN="TOP" 59COLSPAN="3" 60><B 61>Revision History</B 62></TH 63></TR 64><TR 65><TD 66ALIGN="LEFT" 67>Revision 1.9</TD 68><TD 69ALIGN="LEFT" 70>2005-06-20</TD 71><TD 72ALIGN="LEFT" 73>Revised by: ppadala</TD 74></TR 75><TR 76><TD 77ALIGN="LEFT" 78COLSPAN="3" 79>The license has been changed to the MIT-style license used 80 by NCURSES. Note that the programs are also re-licensed under this.</TD 81></TR 82><TR 83><TD 84ALIGN="LEFT" 85>Revision 1.8</TD 86><TD 87ALIGN="LEFT" 88>2005-06-17</TD 89><TD 90ALIGN="LEFT" 91>Revised by: ppadala</TD 92></TR 93><TR 94><TD 95ALIGN="LEFT" 96COLSPAN="3" 97>Lots of updates. Added references and perl examples. 98 Changes to examples. Many grammatical and stylistic changes to the 99 content. Changes to NCURSES history.</TD 100></TR 101><TR 102><TD 103ALIGN="LEFT" 104>Revision 1.7.1</TD 105><TD 106ALIGN="LEFT" 107>2002-06-25</TD 108><TD 109ALIGN="LEFT" 110>Revised by: ppadala</TD 111></TR 112><TR 113><TD 114ALIGN="LEFT" 115COLSPAN="3" 116>Added a README file for building and instructions 117 for building from source.</TD 118></TR 119><TR 120><TD 121ALIGN="LEFT" 122>Revision 1.7</TD 123><TD 124ALIGN="LEFT" 125>2002-06-25</TD 126><TD 127ALIGN="LEFT" 128>Revised by: ppadala</TD 129></TR 130><TR 131><TD 132ALIGN="LEFT" 133COLSPAN="3" 134>Added "Other formats" section and made a lot of fancy 135 changes to the programs. Inlining of programs is gone.</TD 136></TR 137><TR 138><TD 139ALIGN="LEFT" 140>Revision 1.6.1</TD 141><TD 142ALIGN="LEFT" 143>2002-02-24</TD 144><TD 145ALIGN="LEFT" 146>Revised by: ppadala</TD 147></TR 148><TR 149><TD 150ALIGN="LEFT" 151COLSPAN="3" 152>Removed the old Changelog section, cleaned the makefiles</TD 153></TR 154><TR 155><TD 156ALIGN="LEFT" 157>Revision 1.6</TD 158><TD 159ALIGN="LEFT" 160>2002-02-16</TD 161><TD 162ALIGN="LEFT" 163>Revised by: ppadala</TD 164></TR 165><TR 166><TD 167ALIGN="LEFT" 168COLSPAN="3" 169>Corrected a lot of spelling mistakes, added ACS variables 170 section</TD 171></TR 172><TR 173><TD 174ALIGN="LEFT" 175>Revision 1.5</TD 176><TD 177ALIGN="LEFT" 178>2002-01-05</TD 179><TD 180ALIGN="LEFT" 181>Revised by: ppadala</TD 182></TR 183><TR 184><TD 185ALIGN="LEFT" 186COLSPAN="3" 187>Changed structure to present proper TOC</TD 188></TR 189><TR 190><TD 191ALIGN="LEFT" 192>Revision 1.3.1</TD 193><TD 194ALIGN="LEFT" 195>2001-07-26</TD 196><TD 197ALIGN="LEFT" 198>Revised by: ppadala</TD 199></TR 200><TR 201><TD 202ALIGN="LEFT" 203COLSPAN="3" 204>Corrected maintainers paragraph, Corrected stable release number</TD 205></TR 206><TR 207><TD 208ALIGN="LEFT" 209>Revision 1.3</TD 210><TD 211ALIGN="LEFT" 212>2001-07-24</TD 213><TD 214ALIGN="LEFT" 215>Revised by: ppadala</TD 216></TR 217><TR 218><TD 219ALIGN="LEFT" 220COLSPAN="3" 221>Added copyright notices to main document (LDP license) 222 and programs (GPL), Corrected 223 printw_example.</TD 224></TR 225><TR 226><TD 227ALIGN="LEFT" 228>Revision 1.2</TD 229><TD 230ALIGN="LEFT" 231>2001-06-05</TD 232><TD 233ALIGN="LEFT" 234>Revised by: ppadala</TD 235></TR 236><TR 237><TD 238ALIGN="LEFT" 239COLSPAN="3" 240>Incorporated ravi's changes. Mainly to introduction, menu, 241 form, justforfun sections</TD 242></TR 243><TR 244><TD 245ALIGN="LEFT" 246>Revision 1.1</TD 247><TD 248ALIGN="LEFT" 249>2001-05-22</TD 250><TD 251ALIGN="LEFT" 252>Revised by: ppadala</TD 253></TR 254><TR 255><TD 256ALIGN="LEFT" 257COLSPAN="3" 258>Added "a word about window" section, Added scanw_example.</TD 259></TR 260></TABLE 261></DIV 262><DIV 263><DIV 264CLASS="ABSTRACT" 265><P 266></P 267><A 268NAME="AEN67" 269></A 270><P 271> <SPAN 272CLASS="emphasis" 273><I 274CLASS="EMPHASIS" 275>This document is intended to be an "All in One" guide for programming with 276ncurses and its sister libraries. We graduate from a simple "Hello World" 277program to more complex form manipulation. No prior experience in ncurses is 278assumed. Send comments to <A 279HREF="mailto:ppadala@gmail.com" 280TARGET="_top" 281>this address</A 282> 283 </I 284></SPAN 285> 286 </P 287><P 288></P 289></DIV 290></DIV 291><HR></DIV 292><DIV 293CLASS="TOC" 294><DL 295><DT 296><B 297>Table of Contents</B 298></DT 299><DT 300>1. <A 301HREF="#INTRO" 302>Introduction</A 303></DT 304><DD 305><DL 306><DT 307>1.1. <A 308HREF="#WHATIS" 309>What is NCURSES?</A 310></DT 311><DT 312>1.2. <A 313HREF="#WHATCANWEDO" 314>What we can do with NCURSES</A 315></DT 316><DT 317>1.3. <A 318HREF="#WHERETOGETIT" 319>Where to get it</A 320></DT 321><DT 322>1.4. <A 323HREF="#PURPOSE" 324>Purpose/Scope of the document</A 325></DT 326><DT 327>1.5. <A 328HREF="#ABOUTPROGRAMS" 329>About the Programs</A 330></DT 331><DT 332>1.6. <A 333HREF="#OTHERFORMATS" 334>Other Formats of the document</A 335></DT 336><DD 337><DL 338><DT 339>1.6.1. <A 340HREF="#LISTFORMATS" 341>Readily available formats from tldp.org</A 342></DT 343><DT 344>1.6.2. <A 345HREF="#BUILDSOURCE" 346>Building from source</A 347></DT 348></DL 349></DD 350><DT 351>1.7. <A 352HREF="#CREDITS" 353>Credits</A 354></DT 355><DT 356>1.8. <A 357HREF="#WISHLIST" 358>Wish List</A 359></DT 360><DT 361>1.9. <A 362HREF="#COPYRIGHT" 363>Copyright</A 364></DT 365></DL 366></DD 367><DT 368>2. <A 369HREF="#HELLOWORLD" 370>Hello World !!!</A 371></DT 372><DD 373><DL 374><DT 375>2.1. <A 376HREF="#COMPILECURSES" 377>Compiling With the NCURSES Library</A 378></DT 379><DT 380>2.2. <A 381HREF="#DISSECTION" 382>Dissection</A 383></DT 384><DD 385><DL 386><DT 387>2.2.1. <A 388HREF="#ABOUT-INITSCR" 389>About initscr()</A 390></DT 391><DT 392>2.2.2. <A 393HREF="#MYST-REFRESH" 394>The mysterious refresh()</A 395></DT 396><DT 397>2.2.3. <A 398HREF="#ABOUT-ENDWIN" 399>About endwin()</A 400></DT 401></DL 402></DD 403></DL 404></DD 405><DT 406>3. <A 407HREF="#GORY" 408>The Gory Details</A 409></DT 410><DT 411>4. <A 412HREF="#INIT" 413>Initialization</A 414></DT 415><DD 416><DL 417><DT 418>4.1. <A 419HREF="#ABOUTINIT" 420>Initialization functions</A 421></DT 422><DT 423>4.2. <A 424HREF="#RAWCBREAK" 425>raw() and cbreak()</A 426></DT 427><DT 428>4.3. <A 429HREF="#ECHONOECHO" 430>echo() and noecho()</A 431></DT 432><DT 433>4.4. <A 434HREF="#KEYPAD" 435>keypad()</A 436></DT 437><DT 438>4.5. <A 439HREF="#HALFDELAY" 440>halfdelay()</A 441></DT 442><DT 443>4.6. <A 444HREF="#MISCINIT" 445>Miscellaneous Initialization functions</A 446></DT 447><DT 448>4.7. <A 449HREF="#INITEX" 450>An Example</A 451></DT 452></DL 453></DD 454><DT 455>5. <A 456HREF="#AWORDWINDOWS" 457>A Word about Windows</A 458></DT 459><DT 460>6. <A 461HREF="#PRINTW" 462>Output functions</A 463></DT 464><DD 465><DL 466><DT 467>6.1. <A 468HREF="#ADDCHCLASS" 469>addch() class of functions</A 470></DT 471><DT 472>6.2. <A 473HREF="#AEN298" 474>mvaddch(), waddch() and mvwaddch()</A 475></DT 476><DT 477>6.3. <A 478HREF="#PRINTWCLASS" 479>printw() class of functions</A 480></DT 481><DD 482><DL 483><DT 484>6.3.1. <A 485HREF="#PRINTWMVPRINTW" 486>printw() and mvprintw</A 487></DT 488><DT 489>6.3.2. <A 490HREF="#WPRINTWMVWPRINTW" 491>wprintw() and mvwprintw</A 492></DT 493><DT 494>6.3.3. <A 495HREF="#VWPRINTW" 496>vwprintw()</A 497></DT 498><DT 499>6.3.4. <A 500HREF="#SIMPLEPRINTWEX" 501>A Simple printw example</A 502></DT 503></DL 504></DD 505><DT 506>6.4. <A 507HREF="#ADDSTRCLASS" 508>addstr() class of functions</A 509></DT 510><DT 511>6.5. <A 512HREF="#ACAUTION" 513>A word of caution</A 514></DT 515></DL 516></DD 517><DT 518>7. <A 519HREF="#SCANW" 520>Input functions</A 521></DT 522><DD 523><DL 524><DT 525>7.1. <A 526HREF="#GETCHCLASS" 527>getch() class of functions</A 528></DT 529><DT 530>7.2. <A 531HREF="#SCANWCLASS" 532>scanw() class of functions</A 533></DT 534><DD 535><DL 536><DT 537>7.2.1. <A 538HREF="#SCANWMVSCANW" 539>scanw() and mvscanw</A 540></DT 541><DT 542>7.2.2. <A 543HREF="#WSCANWMVWSCANW" 544>wscanw() and mvwscanw()</A 545></DT 546><DT 547>7.2.3. <A 548HREF="#VWSCANW" 549>vwscanw()</A 550></DT 551></DL 552></DD 553><DT 554>7.3. <A 555HREF="#GETSTRCLASS" 556>getstr() class of functions</A 557></DT 558><DT 559>7.4. <A 560HREF="#GETSTREX" 561>Some examples</A 562></DT 563></DL 564></DD 565><DT 566>8. <A 567HREF="#ATTRIB" 568>Attributes</A 569></DT 570><DD 571><DL 572><DT 573>8.1. <A 574HREF="#ATTRIBDETAILS" 575>The details</A 576></DT 577><DT 578>8.2. <A 579HREF="#ATTRONVSATTRSET" 580>attron() vs attrset()</A 581></DT 582><DT 583>8.3. <A 584HREF="#ATTR_GET" 585>attr_get()</A 586></DT 587><DT 588>8.4. <A 589HREF="#ATTR_FUNCS" 590>attr_ functions</A 591></DT 592><DT 593>8.5. <A 594HREF="#WATTRFUNCS" 595>wattr functions</A 596></DT 597><DT 598>8.6. <A 599HREF="#CHGAT" 600>chgat() functions</A 601></DT 602></DL 603></DD 604><DT 605>9. <A 606HREF="#WINDOWS" 607>Windows</A 608></DT 609><DD 610><DL 611><DT 612>9.1. <A 613HREF="#WINDOWBASICS" 614>The basics</A 615></DT 616><DT 617>9.2. <A 618HREF="#LETBEWINDOW" 619>Let there be a Window !!!</A 620></DT 621><DT 622>9.3. <A 623HREF="#BORDEREXEXPL" 624>Explanation</A 625></DT 626><DT 627>9.4. <A 628HREF="#OTHERSTUFF" 629>The other stuff in the example</A 630></DT 631><DT 632>9.5. <A 633HREF="#OTHERBORDERFUNCS" 634>Other Border functions</A 635></DT 636></DL 637></DD 638><DT 639>10. <A 640HREF="#COLOR" 641>Colors</A 642></DT 643><DD 644><DL 645><DT 646>10.1. <A 647HREF="#COLORBASICS" 648>The basics</A 649></DT 650><DT 651>10.2. <A 652HREF="#CHANGECOLORDEFS" 653>Changing Color Definitions</A 654></DT 655><DT 656>10.3. <A 657HREF="#COLORCONTENT" 658>Color Content</A 659></DT 660></DL 661></DD 662><DT 663>11. <A 664HREF="#KEYS" 665>Interfacing with the key board</A 666></DT 667><DD 668><DL 669><DT 670>11.1. <A 671HREF="#KEYSBASICS" 672>The Basics</A 673></DT 674><DT 675>11.2. <A 676HREF="#SIMPLEKEYEX" 677>A Simple Key Usage example</A 678></DT 679></DL 680></DD 681><DT 682>12. <A 683HREF="#MOUSE" 684>Interfacing with the mouse</A 685></DT 686><DD 687><DL 688><DT 689>12.1. <A 690HREF="#MOUSEBASICS" 691>The Basics</A 692></DT 693><DT 694>12.2. <A 695HREF="#GETTINGEVENTS" 696>Getting the events</A 697></DT 698><DT 699>12.3. <A 700HREF="#MOUSETOGETHER" 701>Putting it all Together</A 702></DT 703><DT 704>12.4. <A 705HREF="#MISCMOUSEFUNCS" 706>Miscellaneous Functions</A 707></DT 708></DL 709></DD 710><DT 711>13. <A 712HREF="#SCREEN" 713>Screen Manipulation</A 714></DT 715><DD 716><DL 717><DT 718>13.1. <A 719HREF="#GETYX" 720>getyx() functions</A 721></DT 722><DT 723>13.2. <A 724HREF="#SCREENDUMP" 725>Screen Dumping</A 726></DT 727><DT 728>13.3. <A 729HREF="#WINDOWDUMP" 730>Window Dumping</A 731></DT 732></DL 733></DD 734><DT 735>14. <A 736HREF="#MISC" 737>Miscellaneous features</A 738></DT 739><DD 740><DL 741><DT 742>14.1. <A 743HREF="#CURSSET" 744>curs_set()</A 745></DT 746><DT 747>14.2. <A 748HREF="#TEMPLEAVE" 749>Temporarily Leaving Curses mode</A 750></DT 751><DT 752>14.3. <A 753HREF="#ACSVARS" 754>ACS_ variables</A 755></DT 756></DL 757></DD 758><DT 759>15. <A 760HREF="#OTHERLIB" 761>Other libraries</A 762></DT 763><DT 764>16. <A 765HREF="#PANELS" 766>Panel Library</A 767></DT 768><DD 769><DL 770><DT 771>16.1. <A 772HREF="#PANELBASICS" 773>The Basics</A 774></DT 775><DT 776>16.2. <A 777HREF="#COMPILEPANELS" 778>Compiling With the Panels Library</A 779></DT 780><DT 781>16.3. <A 782HREF="#PANELBROWSING" 783>Panel Window Browsing</A 784></DT 785><DT 786>16.4. <A 787HREF="#USERPTRUSING" 788>Using User Pointers</A 789></DT 790><DT 791>16.5. <A 792HREF="#PANELMOVERESIZE" 793>Moving and Resizing Panels</A 794></DT 795><DT 796>16.6. <A 797HREF="#PANELSHOWHIDE" 798>Hiding and Showing Panels</A 799></DT 800><DT 801>16.7. <A 802HREF="#PANELABOVE" 803>panel_above() and panel_below() Functions</A 804></DT 805></DL 806></DD 807><DT 808>17. <A 809HREF="#MENUS" 810>Menus Library</A 811></DT 812><DD 813><DL 814><DT 815>17.1. <A 816HREF="#MENUBASICS" 817>The Basics</A 818></DT 819><DT 820>17.2. <A 821HREF="#COMPILEMENUS" 822>Compiling With the Menu Library</A 823></DT 824><DT 825>17.3. <A 826HREF="#MENUDRIVER" 827>Menu Driver: The work horse of the menu system</A 828></DT 829><DT 830>17.4. <A 831HREF="#MENUWINDOWS" 832>Menu Windows</A 833></DT 834><DT 835>17.5. <A 836HREF="#SCROLLMENUS" 837>Scrolling Menus</A 838></DT 839><DT 840>17.6. <A 841HREF="#MULTICOLUMN" 842>Multi Columnar Menus</A 843></DT 844><DT 845>17.7. <A 846HREF="#MULTIVALUEMENUS" 847>Multi Valued Menus</A 848></DT 849><DT 850>17.8. <A 851HREF="#MENUOPT" 852>Menu Options</A 853></DT 854><DT 855>17.9. <A 856HREF="#MENUUSERPTR" 857>The useful User Pointer</A 858></DT 859></DL 860></DD 861><DT 862>18. <A 863HREF="#FORMS" 864>Forms Library</A 865></DT 866><DD 867><DL 868><DT 869>18.1. <A 870HREF="#FORMBASICS" 871>The Basics</A 872></DT 873><DT 874>18.2. <A 875HREF="#COMPILEFORMS" 876>Compiling With the Forms Library</A 877></DT 878><DT 879>18.3. <A 880HREF="#PLAYFIELDS" 881>Playing with Fields</A 882></DT 883><DD 884><DL 885><DT 886>18.3.1. <A 887HREF="#FETCHINFO" 888>Fetching Size and Location of Field</A 889></DT 890><DT 891>18.3.2. <A 892HREF="#MOVEFIELD" 893>Moving the field</A 894></DT 895><DT 896>18.3.3. <A 897HREF="#JUSTIFYFIELD" 898>Field Justification</A 899></DT 900><DT 901>18.3.4. <A 902HREF="#FIELDDISPATTRIB" 903>Field Display Attributes</A 904></DT 905><DT 906>18.3.5. <A 907HREF="#FIELDOPTIONBITS" 908>Field Option Bits</A 909></DT 910><DT 911>18.3.6. <A 912HREF="#FIELDSTATUS" 913>Field Status</A 914></DT 915><DT 916>18.3.7. <A 917HREF="#FIELDUSERPTR" 918>Field User Pointer</A 919></DT 920><DT 921>18.3.8. <A 922HREF="#VARIABLESIZEFIELDS" 923>Variable-Sized Fields</A 924></DT 925></DL 926></DD 927><DT 928>18.4. <A 929HREF="#FORMWINDOWS" 930>Form Windows</A 931></DT 932><DT 933>18.5. <A 934HREF="#FILEDVALIDATE" 935>Field Validation</A 936></DT 937><DT 938>18.6. <A 939HREF="#FORMDRIVER" 940>Form Driver: The work horse of the forms system</A 941></DT 942><DD 943><DL 944><DT 945>18.6.1. <A 946HREF="#PAGENAVREQ" 947>Page Navigation Requests</A 948></DT 949><DT 950>18.6.2. <A 951HREF="#INTERFIELDNAVREQ" 952>Inter-Field Navigation Requests</A 953></DT 954><DT 955>18.6.3. <A 956HREF="#INTRAFIELDNAVREQ" 957>Intra-Field Navigation Requests</A 958></DT 959><DT 960>18.6.4. <A 961HREF="#SCROLLREQ" 962>Scrolling Requests</A 963></DT 964><DT 965>18.6.5. <A 966HREF="#EDITREQ" 967>Editing Requests</A 968></DT 969><DT 970>18.6.6. <A 971HREF="#ORDERREQ" 972>Order Requests</A 973></DT 974><DT 975>18.6.7. <A 976HREF="#APPLICCOMMANDS" 977>Application Commands</A 978></DT 979></DL 980></DD 981></DL 982></DD 983><DT 984>19. <A 985HREF="#TOOLS" 986>Tools and Widget Libraries</A 987></DT 988><DD 989><DL 990><DT 991>19.1. <A 992HREF="#CDK" 993>CDK (Curses Development Kit)</A 994></DT 995><DD 996><DL 997><DT 998>19.1.1. <A 999HREF="#WIDGETLIST" 1000>Widget List</A 1001></DT 1002><DT 1003>19.1.2. <A 1004HREF="#CDKATTRACT" 1005>Some Attractive Features</A 1006></DT 1007><DT 1008>19.1.3. <A 1009HREF="#CDKCONCLUSION" 1010>Conclusion</A 1011></DT 1012></DL 1013></DD 1014><DT 1015>19.2. <A 1016HREF="#DIALOG" 1017>The dialog</A 1018></DT 1019><DT 1020>19.3. <A 1021HREF="#PERLCURSES" 1022>Perl Curses Modules CURSES::FORM and CURSES::WIDGETS</A 1023></DT 1024></DL 1025></DD 1026><DT 1027>20. <A 1028HREF="#JUSTFORFUN" 1029>Just For Fun !!!</A 1030></DT 1031><DD 1032><DL 1033><DT 1034>20.1. <A 1035HREF="#GAMEOFLIFE" 1036>The Game of Life</A 1037></DT 1038><DT 1039>20.2. <A 1040HREF="#MAGIC" 1041>Magic Square</A 1042></DT 1043><DT 1044>20.3. <A 1045HREF="#HANOI" 1046>Towers of Hanoi</A 1047></DT 1048><DT 1049>20.4. <A 1050HREF="#QUEENS" 1051>Queens Puzzle</A 1052></DT 1053><DT 1054>20.5. <A 1055HREF="#SHUFFLE" 1056>Shuffle</A 1057></DT 1058><DT 1059>20.6. <A 1060HREF="#TT" 1061>Typing Tutor</A 1062></DT 1063></DL 1064></DD 1065><DT 1066>21. <A 1067HREF="#REF" 1068>References</A 1069></DT 1070></DL 1071></DIV 1072><DIV 1073CLASS="SECT1" 1074><H2 1075CLASS="SECT1" 1076><A 1077NAME="INTRO" 1078>1. Introduction</A 1079></H2 1080><P 1081>In the olden days of teletype terminals, terminals were away from computers and 1082were connected to them through serial cables. The terminals could be configured 1083by sending a series of bytes. All the capabilities (such as 1084moving the cursor to a new location, erasing part of the screen, scrolling the 1085screen, changing modes etc.) of terminals could be accessed through these 1086series of bytes. These control seeuqnces are usually called escape sequences, 1087because they start 1088with an escape(0x1B) character. Even today, with proper emulation, we can send 1089escape sequences to the emulator and achieve the same effect on a terminal 1090window.</P 1091><P 1092>Suppose you wanted to print a line in color. Try typing this on your console.</P 1093><PRE 1094CLASS="PROGRAMLISTING" 1095>echo "^[[0;31;40mIn Color"</PRE 1096><P 1097>The first character is an escape character, which looks like two characters ^ 1098and [. To be able to print it, you have to press CTRL+V and then the ESC key. 1099All the others are normal printable characters. You should be able to see the 1100string "In Color" in red. It stays that way and to revert back to the original 1101mode type this.</P 1102><PRE 1103CLASS="PROGRAMLISTING" 1104>echo "^[[0;37;40m"</PRE 1105><P 1106>Now, what do these magic characters mean? Difficult to comprehend? They might 1107even be different for different terminals. So the designers of UNIX invented a 1108mechanism named <TT 1109CLASS="LITERAL" 1110>termcap</TT 1111>. It is a file that 1112lists all the capabilities of a particular terminal, along with the escape 1113sequences needed to achieve a particular effect. In the later years, this was 1114replaced by <TT 1115CLASS="LITERAL" 1116>terminfo</TT 1117>. Without delving too 1118much into details, this mechanism allows application 1119programs to query the terminfo database and obtain the control characters to be 1120sent to a terminal or terminal emulator.</P 1121><DIV 1122CLASS="SECT2" 1123><HR><H3 1124CLASS="SECT2" 1125><A 1126NAME="WHATIS" 1127>1.1. What is NCURSES?</A 1128></H3 1129><P 1130> 1131You might be wondering, what the import of all this technical gibberish is. In 1132the above scenario, every application program is supposed to query the terminfo 1133and perform the necessary stuff (sending control characters etc.). It soon became 1134difficult to manage this complexity and this gave birth to 'CURSES'. Curses is 1135a pun on the name "cursor optimization". The Curses library forms a wrapper 1136over working with raw terminal codes, and provides highly flexible and 1137efficient API (Application Programming Interface). It provides functions to 1138move the cursor, create windows, produce colors, play with mouse etc. The 1139application programs need not worry about the underlying terminal capabilities.</P 1140><P 1141>So what is NCURSES? NCURSES is a clone of the original System V Release 4.0 1142(SVr4) curses. It is a freely distributable library, fully compatible with 1143older version of curses. In short, it is a library of functions that manages 1144an application's display on character-cell terminals. In the remainder of the 1145document, the terms curses and ncurses are used interchangeably. </P 1146><P 1147>A detailed history of NCURSES can be found in the NEWS file from the source 1148distribution. The current package is maintained by 1149<A 1150HREF="mailto:dickey@his.com" 1151TARGET="_top" 1152>Thomas Dickey</A 1153>. 1154You can contact the maintainers at <A 1155HREF="mailto:bug-ncurses@gnu.org" 1156TARGET="_top" 1157>bug-ncurses@gnu.org</A 1158>.</P 1159></DIV 1160><DIV 1161CLASS="SECT2" 1162><HR><H3 1163CLASS="SECT2" 1164><A 1165NAME="WHATCANWEDO" 1166>1.2. What we can do with NCURSES</A 1167></H3 1168><P 1169>NCURSES not only creates a wrapper over terminal capabilities, but also gives a 1170robust framework to create nice looking UI (User Interface)s in text mode. It 1171provides functions to create windows etc. Its sister libraries panel, menu and 1172form provide an extension to the basic curses library. These libraries usually 1173come along with curses. One can create applications that contain multiple 1174windows, menus, panels and forms. Windows can be managed independently, can 1175provide 'scrollability' and even can be hidden.</P 1176><P 1177> 1178Menus provide the user with an easy command selection option. Forms allow the 1179creation of easy-to-use data entry and display windows. Panels extend the 1180capabilities of ncurses to deal with overlapping and stacked windows.</P 1181><P 1182>These are just some of the basic things we can do with ncurses. As we move 1183along, We will see all the capabilities of these libraries. </P 1184></DIV 1185><DIV 1186CLASS="SECT2" 1187><HR><H3 1188CLASS="SECT2" 1189><A 1190NAME="WHERETOGETIT" 1191>1.3. Where to get it</A 1192></H3 1193><P 1194>All right, now that you know what you can do with ncurses, you must be rearing 1195to get started. NCURSES is usually shipped with your installation. In case 1196you don't have the library or want to compile it on your own, read on.</P 1197><P 1198><SPAN 1199CLASS="emphasis" 1200><I 1201CLASS="EMPHASIS" 1202>Compiling the package</I 1203></SPAN 1204> </P 1205><P 1206>NCURSES can be obtained from <A 1207HREF="ftp://ftp.gnu.org/pub/gnu/ncurses/ncurses.tar.gz" 1208TARGET="_top" 1209>ftp://ftp.gnu.org/pub/gnu/ncurses/ncurses.tar.gz</A 1210> or any of the ftp 1211sites mentioned in <A 1212HREF="http://www.gnu.org/order/ftp.html" 1213TARGET="_top" 1214>http://www.gnu.org/order/ftp.html</A 1215>. </P 1216><P 1217>Read the README and INSTALL files for details on to how to install it. It 1218usually involves the following operations.</P 1219><PRE 1220CLASS="PROGRAMLISTING" 1221> tar zxvf ncurses<version>.tar.gz # unzip and untar the archive |
467 cd ncurses<version> # cd to the directory 468 ./configure # configure the build according to your 469 # environment 470 make # make it 471 su root # become root | 1222 cd ncurses<version> # cd to the directory 1223 ./configure # configure the build according to your 1224 # environment 1225 make # make it 1226 su root # become root |
472 make install # install it</font> 473</pre></td> 474</tr> 475</table> 476<p><em>Using the RPM</em></p> 477<p>NCURSES RPM can be found and downloaded from <a href= 478"http://rpmfind.net" target="_top">http://rpmfind.net</a> . The RPM 479can be installed with the following command after becoming 480root.</p> 481<table border="0" bgcolor="#E0E0E0" width="100%"> 482<tr> 483<td> 484<pre class="PROGRAMLISTING"> 485<font color="#000000"> rpm -i <downloaded rpm></font> 486</pre></td> 487</tr> 488</table> 489</div> 490<div class="SECT2"> 491<hr> 492<h3 class="SECT2"><a name="PURPOSE" id="PURPOSE">1.4. Purpose/Scope 493of the document</a></h3> 494<p>This document is intended to be a "All in One" guide for 495programming with ncurses and its sister libraries. We graduate from 496a simple "Hello World" program to more complex form manipulation. 497No prior experience in ncurses is assumed. The writing is informal, 498but a lot of detail is provided for each of the examples.</p> 499</div> 500<div class="SECT2"> 501<hr> 502<h3 class="SECT2"><a name="ABOUTPROGRAMS" id="ABOUTPROGRAMS">1.5. 503About the Programs</a></h3> 504<p>All the programs in the document are available in zipped form 505<a href= 506"http://www.tldp.org/HOWTO/NCURSES-Programming-HOWTO/ncurses_programs.tar.gz" 507target="_top">here</a>. Unzip and untar it. The directory structure 508looks like this.</p> 509<table border="0" bgcolor="#E0E0E0" width="100%"> 510<tr> 511<td> 512<pre class="PROGRAMLISTING"> 513<font color="#000000">ncurses | 1227 make install # install it</PRE 1228><P 1229><SPAN 1230CLASS="emphasis" 1231><I 1232CLASS="EMPHASIS" 1233>Using the RPM </I 1234></SPAN 1235></P 1236><P 1237>NCURSES RPM can be found and downloaded from <A 1238HREF="http://rpmfind.net" 1239TARGET="_top" 1240>http://rpmfind.net </A 1241>. The RPM can be installed with the following 1242command after becoming root.</P 1243><PRE 1244CLASS="PROGRAMLISTING" 1245> rpm -i <downloaded rpm></PRE 1246></DIV 1247><DIV 1248CLASS="SECT2" 1249><HR><H3 1250CLASS="SECT2" 1251><A 1252NAME="PURPOSE" 1253>1.4. Purpose/Scope of the document</A 1254></H3 1255><P 1256>This document is intended to be a "All in One" guide for programming with 1257ncurses and its sister libraries. We graduate from a simple "Hello World" 1258program to more complex form manipulation. No prior experience in ncurses is 1259assumed. The writing is informal, but a lot of detail is provided for 1260each of the examples.</P 1261></DIV 1262><DIV 1263CLASS="SECT2" 1264><HR><H3 1265CLASS="SECT2" 1266><A 1267NAME="ABOUTPROGRAMS" 1268>1.5. About the Programs</A 1269></H3 1270><P 1271>All the programs in the document are available in zipped form 1272<A 1273HREF="http://www.tldp.org/HOWTO/NCURSES-Programming-HOWTO/ncurses_programs.tar.gz" 1274TARGET="_top" 1275>here</A 1276>. Unzip and untar it. The directory structure looks like this.</P 1277><PRE 1278CLASS="PROGRAMLISTING" 1279>ncurses |
514 | 515 |----> JustForFun -- just for fun programs 516 |----> basics -- basic programs 517 |----> demo -- output files go into this directory after make 518 | | 519 | |----> exe -- exe files of all example programs 520 |----> forms -- programs related to form library 521 |----> menus -- programs related to menus library 522 |----> panels -- programs related to panels library 523 |----> perl -- perl equivalents of the examples (contributed 524 | by Anuradha Ratnaweera) 525 |----> Makefile -- the top level Makefile 526 |----> README -- the top level README file. contains instructions | 1280 | 1281 |----> JustForFun -- just for fun programs 1282 |----> basics -- basic programs 1283 |----> demo -- output files go into this directory after make 1284 | | 1285 | |----> exe -- exe files of all example programs 1286 |----> forms -- programs related to form library 1287 |----> menus -- programs related to menus library 1288 |----> panels -- programs related to panels library 1289 |----> perl -- perl equivalents of the examples (contributed 1290 | by Anuradha Ratnaweera) 1291 |----> Makefile -- the top level Makefile 1292 |----> README -- the top level README file. contains instructions |
527 |----> COPYING -- copyright notice</font> 528</pre></td> 529</tr> 530</table> 531<p>The individual directories contain the following files.</p> 532<table border="0" bgcolor="#E0E0E0" width="100%"> 533<tr> 534<td> 535<pre class="PROGRAMLISTING"> 536<font color="#000000">Description of files in each directory | 1293 |----> COPYING -- copyright notice</PRE 1294><P 1295>The individual directories contain the following files.</P 1296><PRE 1297CLASS="PROGRAMLISTING" 1298>Description of files in each directory |
537-------------------------------------- 538JustForFun 539 | 540 |----> hanoi.c -- The Towers of Hanoi Solver 541 |----> life.c -- The Game of Life demo 542 |----> magic.c -- An Odd Order Magic Square builder 543 |----> queens.c -- The famous N-Queens Solver 544 |----> shuffle.c -- A fun game, if you have time to kill --- 42 unchanged lines hidden (view full) --- 587 | 588 |----> panel_browse.c -- Panel browsing through tab. Usage of user 589 | -- pointer 590 |----> panel_hide.c -- Hiding and Un hiding of panels 591 |----> panel_resize.c -- Moving and resizing of panels 592 |----> panel_simple.c -- A simple panel example 593 594 perl | 1299-------------------------------------- 1300JustForFun 1301 | 1302 |----> hanoi.c -- The Towers of Hanoi Solver 1303 |----> life.c -- The Game of Life demo 1304 |----> magic.c -- An Odd Order Magic Square builder 1305 |----> queens.c -- The famous N-Queens Solver 1306 |----> shuffle.c -- A fun game, if you have time to kill --- 42 unchanged lines hidden (view full) --- 1349 | 1350 |----> panel_browse.c -- Panel browsing through tab. Usage of user 1351 | -- pointer 1352 |----> panel_hide.c -- Hiding and Un hiding of panels 1353 |----> panel_resize.c -- Moving and resizing of panels 1354 |----> panel_simple.c -- A simple panel example 1355 1356 perl |
595 |----> 01-10.pl -- Perl equivalents of first ten example programs</font> 596</pre></td> 597</tr> 598</table> 599<p>There is a top level Makefile included in the main directory. It 600builds all the files and puts the ready-to-use exes in demo/exe 601directory. You can also do selective make by going into the 602corresponding directory. Each directory contains a README file 603explaining the purpose of each c file in the directory.</p> 604<p>For every example, I have included path name for the file 605relative to the examples directory.</p> 606<p>If you prefer browsing individual programs, point your browser 607to <a href= 608"http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/ncurses_programs/" 609target= 610"_top">http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/ncurses_programs/</a></p> 611<p>All the programs are released under the same license that is 612used by ncurses (MIT-style). This gives you the ability to do 613pretty much anything other than claiming them as yours. Feel free 614to use them in your programs as appropriate.</p> 615</div> 616<div class="SECT2"> 617<hr> 618<h3 class="SECT2"><a name="OTHERFORMATS" id="OTHERFORMATS">1.6. 619Other Formats of the document</a></h3> 620<p>This howto is also availabe in various other formats on the 621tldp.org site. Here are the links to other formats of this 622document.</p> 623<div class="SECT3"> 624<hr> 625<h4 class="SECT3"><a name="LISTFORMATS" id="LISTFORMATS">1.6.1. 626Readily available formats from tldp.org</a></h4> 627<ul> 628<li> 629<p><a href= 630"http://www.ibiblio.org/pub/Linux/docs/HOWTO/other-formats/pdf/NCURSES-Programming-HOWTO.pdf" 631target="_top">Acrobat PDF Format</a></p> 632</li> 633<li> 634<p><a href= 635"http://www.ibiblio.org/pub/Linux/docs/HOWTO/other-formats/ps/NCURSES-Programming-HOWTO.ps.gz" 636target="_top">PostScript Format</a></p> 637</li> 638<li> 639<p><a href= 640"http://www.ibiblio.org/pub/Linux/docs/HOWTO/other-formats/html/NCURSES-Programming-HOWTO-html.tar.gz" 641target="_top">In Multiple HTML pages</a></p> 642</li> 643<li> 644<p><a href= 645"http://www.ibiblio.org/pub/Linux/docs/HOWTO/other-formats/html_single/NCURSES-Programming-HOWTO.html" 646target="_top">In One big HTML format</a></p> 647</li> 648</ul> 649</div> 650<div class="SECT3"> 651<hr> 652<h4 class="SECT3"><a name="BUILDSOURCE" id="BUILDSOURCE">1.6.2. 653Building from source</a></h4> 654<p>If above links are broken or if you want to experiment with sgml 655read on.</p> 656<table border="0" bgcolor="#E0E0E0" width="100%"> 657<tr> 658<td> 659<pre class="PROGRAMLISTING"> 660<font color= 661"#000000"> Get both the source and the tar,gzipped programs, available at | 1357 |----> 01-10.pl -- Perl equivalents of first ten example programs</PRE 1358><P 1359>There is a top level Makefile included in the main directory. It builds all the 1360files and puts the ready-to-use exes in demo/exe directory. You can also 1361do selective make by going into the corresponding directory. Each directory 1362contains a README file explaining the purpose of each c file in the directory.</P 1363><P 1364>For every example, I have included path name for the file relative to the 1365examples directory. </P 1366><P 1367> If you prefer browsing individual programs, point your browser to 1368<A 1369HREF="http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/ncurses_programs/" 1370TARGET="_top" 1371>http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/ncurses_programs/</A 1372></P 1373><P 1374>All the programs are released under the same license that is used by ncurses 1375(MIT-style). This gives you the ability to do pretty much anything other than 1376claiming them as yours. Feel free to use them in your programs as appropriate.</P 1377></DIV 1378><DIV 1379CLASS="SECT2" 1380><HR><H3 1381CLASS="SECT2" 1382><A 1383NAME="OTHERFORMATS" 1384>1.6. Other Formats of the document</A 1385></H3 1386><P 1387>This howto is also availabe in various other formats on the tldp.org site. 1388Here are the links to other formats of this document.</P 1389><DIV 1390CLASS="SECT3" 1391><HR><H4 1392CLASS="SECT3" 1393><A 1394NAME="LISTFORMATS" 1395>1.6.1. Readily available formats from tldp.org</A 1396></H4 1397><P 1398></P 1399><UL 1400><LI 1401><P 1402><A 1403HREF="http://www.ibiblio.org/pub/Linux/docs/HOWTO/other-formats/pdf/NCURSES-Programming-HOWTO.pdf" 1404TARGET="_top" 1405>Acrobat PDF Format</A 1406></P 1407></LI 1408><LI 1409><P 1410><A 1411HREF="http://www.ibiblio.org/pub/Linux/docs/HOWTO/other-formats/ps/NCURSES-Programming-HOWTO.ps.gz" 1412TARGET="_top" 1413>PostScript Format</A 1414></P 1415></LI 1416><LI 1417><P 1418><A 1419HREF="http://www.ibiblio.org/pub/Linux/docs/HOWTO/other-formats/html/NCURSES-Programming-HOWTO-html.tar.gz" 1420TARGET="_top" 1421>In Multiple HTML pages</A 1422></P 1423></LI 1424><LI 1425><P 1426><A 1427HREF="http://www.ibiblio.org/pub/Linux/docs/HOWTO/other-formats/html_single/NCURSES-Programming-HOWTO.html" 1428TARGET="_top" 1429>In One big HTML format</A 1430></P 1431></LI 1432></UL 1433></DIV 1434><DIV 1435CLASS="SECT3" 1436><HR><H4 1437CLASS="SECT3" 1438><A 1439NAME="BUILDSOURCE" 1440>1.6.2. Building from source</A 1441></H4 1442><P 1443>If above links are broken or if you want to experiment with sgml read on. 1444<PRE 1445CLASS="PROGRAMLISTING" 1446> Get both the source and the tar,gzipped programs, available at |
662 http://cvsview.tldp.org/index.cgi/LDP/howto/docbook/ 663 NCURSES-HOWTO/NCURSES-Programming-HOWTO.sgml 664 http://cvsview.tldp.org/index.cgi/LDP/howto/docbook/ 665 NCURSES-HOWTO/ncurses_programs.tar.gz 666 667 Unzip ncurses_programs.tar.gz with 668 tar zxvf ncurses_programs.tar.gz 669 --- 4 unchanged lines hidden (view full) --- 674 to get pdf, first create a single html file of the HOWTO with 675 jade -t sgml -i html -d <path to docbook html stylesheet> -V nochunks 676 NCURSES-Programming-HOWTO.sgml > NCURSES-ONE-BIG-FILE.html 677 then use htmldoc to get pdf file with 678 htmldoc --size universal -t pdf --firstpage p1 -f <output file name.pdf> 679 NCURSES-ONE-BIG-FILE.html 680 for ps, you would use 681 htmldoc --size universal -t ps --firstpage p1 -f <output file name.ps> | 1447 http://cvsview.tldp.org/index.cgi/LDP/howto/docbook/ 1448 NCURSES-HOWTO/NCURSES-Programming-HOWTO.sgml 1449 http://cvsview.tldp.org/index.cgi/LDP/howto/docbook/ 1450 NCURSES-HOWTO/ncurses_programs.tar.gz 1451 1452 Unzip ncurses_programs.tar.gz with 1453 tar zxvf ncurses_programs.tar.gz 1454 --- 4 unchanged lines hidden (view full) --- 1459 to get pdf, first create a single html file of the HOWTO with 1460 jade -t sgml -i html -d <path to docbook html stylesheet> -V nochunks 1461 NCURSES-Programming-HOWTO.sgml > NCURSES-ONE-BIG-FILE.html 1462 then use htmldoc to get pdf file with 1463 htmldoc --size universal -t pdf --firstpage p1 -f <output file name.pdf> 1464 NCURSES-ONE-BIG-FILE.html 1465 for ps, you would use 1466 htmldoc --size universal -t ps --firstpage p1 -f <output file name.ps> |
682 NCURSES-ONE-BIG-FILE.html</font> 683</pre></td> 684</tr> 685</table> 686<p>See <a href="http://www.tldp.org/LDP/LDP-Author-Guide/" target= 687"_top">LDP Author guide</a> for more details. If all else failes, 688mail me at <a href="ppadala@gmail.com" target= 689"_top">ppadala@gmail.com</a></p> 690</div> 691</div> 692<div class="SECT2"> 693<hr> 694<h3 class="SECT2"><a name="CREDITS" id="CREDITS">1.7. 695Credits</a></h3> 696<p>I thank <a href="mailto:sharath_1@usa.net" target= 697"_top">Sharath</a> and Emre Akbas for helping me with few sections. 698The introduction was initially written by sharath. I rewrote it 699with few excerpts taken from his initial work. Emre helped in 700writing printw and scanw sections.</p> 701<p>Perl equivalents of the example programs are contributed by 702<a href="mailto:Aratnaweera@virtusa.com" target="_top">Anuradha 703Ratnaweera</a>.</p> 704<p>Then comes <a href="mailto:parimi@ece.arizona.edu" target= 705"_top">Ravi Parimi</a>, my dearest friend, who has been on this 706project before even one line was written. He constantly bombarded 707me with suggestions and patiently reviewed the whole text. He also 708checked each program on Linux and Solaris.</p> 709</div> 710<div class="SECT2"> 711<hr> 712<h3 class="SECT2"><a name="WISHLIST" id="WISHLIST">1.8. Wish 713List</a></h3> 714<p>This is the wish list, in the order of priority. If you have a 715wish or you want to work on completing the wish, mail <a href= 716"mailto:ppadala@gmail.com" target="_top">me</a>.</p> 717<ul> 718<li> 719<p>Add examples to last parts of forms section.</p> 720</li> 721<li> 722<p>Prepare a Demo showing all the programs and allow the user to 723browse through description of each program. Let the user compile 724and see the program in action. A dialog based interface is 725preferred.</p> 726</li> 727<li> 728<p>Add debug info. _tracef, _tracemouse stuff.</p> 729</li> 730<li> 731<p>Accessing termcap, terminfo using functions provided by ncurses 732package.</p> 733</li> 734<li> 735<p>Working on two terminals simultaneously.</p> 736</li> 737<li> 738<p>Add more stuff to miscellaneous section.</p> 739</li> 740</ul> 741</div> 742<div class="SECT2"> 743<hr> 744<h3 class="SECT2"><a name="COPYRIGHT" id="COPYRIGHT">1.9. 745Copyright</a></h3> 746<p>Copyright © 2001 by Pradeep Padala.</p> 747<p>Permission is hereby granted, free of charge, to any person 748obtaining a copy of this software and associated documentation 749files (the "Software"), to deal in the Software without 750restriction, including without limitation the rights to use, copy, 751modify, merge, publish, distribute, distribute with modifications, 752sublicense, and/or sell copies of the Software, and to permit 753persons to whom the Software is furnished to do so, subject to the 754following conditions:</p> 755<p>The above copyright notice and this permission notice shall be 756included in all copies or substantial portions of the Software.</p> 757<p>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 758EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 759MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 760NONINFRINGEMENT. IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE 761LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 762ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 763CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 764SOFTWARE.</p> 765<p>Except as contained in this notice, the name(s) of the above 766copyright holders shall not be used in advertising or otherwise to 767promote the sale, use or other dealings in this Software without 768prior written authorization.</p> 769</div> 770</div> 771<div class="SECT1"> 772<hr> 773<h2 class="SECT1"><a name="HELLOWORLD" id="HELLOWORLD">2. Hello 774World !!!</a></h2> 775<p>Welcome to the world of curses. Before we plunge into the 776library and look into its various features, let's write a simple 777program and say hello to the world.</p> 778<div class="SECT2"> 779<hr> 780<h3 class="SECT2"><a name="COMPILECURSES" id="COMPILECURSES">2.1. 781Compiling With the NCURSES Library</a></h3> 782<p>To use ncurses library functions, you have to include ncurses.h 783in your programs. To link the program with ncurses the flag 784-lncurses should be added.</p> 785<table border="0" bgcolor="#E0E0E0" width="100%"> 786<tr> 787<td> 788<pre class="PROGRAMLISTING"> 789<font color="#000000"> #include <ncurses.h> | 1467 NCURSES-ONE-BIG-FILE.html</PRE 1468></P 1469><P 1470>See <A 1471HREF="http://www.tldp.org/LDP/LDP-Author-Guide/" 1472TARGET="_top" 1473>LDP Author guide</A 1474> for more details. If all else failes, mail me at 1475<A 1476HREF="ppadala@gmail.com" 1477TARGET="_top" 1478>ppadala@gmail.com</A 1479></P 1480></DIV 1481></DIV 1482><DIV 1483CLASS="SECT2" 1484><HR><H3 1485CLASS="SECT2" 1486><A 1487NAME="CREDITS" 1488>1.7. Credits</A 1489></H3 1490><P 1491>I thank <A 1492HREF="mailto:sharath_1@usa.net" 1493TARGET="_top" 1494>Sharath</A 1495> and Emre Akbas for 1496helping me with few sections. The introduction was initially written by sharath. 1497I rewrote it with few excerpts taken from his initial work. Emre helped in 1498writing printw and scanw sections.</P 1499><P 1500>Perl equivalents of the example programs are contributed by <A 1501HREF="mailto:Aratnaweera@virtusa.com" 1502TARGET="_top" 1503>Anuradha Ratnaweera</A 1504>. </P 1505><P 1506>Then comes <A 1507HREF="mailto:parimi@ece.arizona.edu" 1508TARGET="_top" 1509>Ravi Parimi</A 1510>, my 1511dearest friend, who has been on this project before even one line was written. 1512He constantly bombarded me with suggestions and patiently reviewed the whole 1513text. He also checked each program on Linux and Solaris. </P 1514></DIV 1515><DIV 1516CLASS="SECT2" 1517><HR><H3 1518CLASS="SECT2" 1519><A 1520NAME="WISHLIST" 1521>1.8. Wish List</A 1522></H3 1523><P 1524>This is the wish list, in the order of priority. If you have a wish or you want 1525to work on completing the wish, mail <A 1526HREF="mailto:ppadala@gmail.com" 1527TARGET="_top" 1528>me</A 1529>. </P 1530><P 1531></P 1532><UL 1533><LI 1534><P 1535>Add examples to last parts of forms section.</P 1536></LI 1537><LI 1538><P 1539>Prepare a Demo showing all the programs and allow the user to browse through 1540description of each program. Let the user compile and see the program in action. 1541A dialog based interface is preferred.</P 1542></LI 1543><LI 1544><P 1545>Add debug info. _tracef, _tracemouse stuff.</P 1546></LI 1547><LI 1548><P 1549>Accessing termcap, terminfo using functions provided by ncurses 1550package.</P 1551></LI 1552><LI 1553><P 1554>Working on two terminals simultaneously.</P 1555></LI 1556><LI 1557><P 1558>Add more stuff to miscellaneous section.</P 1559></LI 1560></UL 1561></DIV 1562><DIV 1563CLASS="SECT2" 1564><HR><H3 1565CLASS="SECT2" 1566><A 1567NAME="COPYRIGHT" 1568>1.9. Copyright</A 1569></H3 1570><P 1571>Copyright © 2001 by Pradeep Padala. </P 1572><P 1573>Permission is hereby granted, free of charge, to any person obtaining a copy 1574of this software and associated documentation files (the "Software"), to deal 1575in the Software without restriction, including without limitation the rights 1576to use, copy, modify, merge, publish, distribute, distribute with 1577modifications, sublicense, and/or sell copies of the Software, and to permit 1578persons to whom the Software is furnished to do so, subject to the following 1579conditions:</P 1580><P 1581>The above copyright notice and this permission notice shall be included in all 1582copies or substantial portions of the Software.</P 1583><P 1584>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1585IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1586FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 1587ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 1588WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 1589IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.</P 1590><P 1591>Except as contained in this notice, the name(s) of the above copyright holders 1592shall not be used in advertising or otherwise to promote the sale, use or 1593other dealings in this Software without prior written authorization. </P 1594></DIV 1595></DIV 1596><DIV 1597CLASS="SECT1" 1598><HR><H2 1599CLASS="SECT1" 1600><A 1601NAME="HELLOWORLD" 1602>2. Hello World !!!</A 1603></H2 1604><P 1605>Welcome to the world of curses. Before we plunge into the library and look into 1606its various features, let's write a simple program and say 1607hello to the world. </P 1608><DIV 1609CLASS="SECT2" 1610><HR><H3 1611CLASS="SECT2" 1612><A 1613NAME="COMPILECURSES" 1614>2.1. Compiling With the NCURSES Library</A 1615></H3 1616><P 1617>To use ncurses library functions, you have to include ncurses.h in your 1618programs. To link the 1619program with ncurses the flag -lncurses should be added.</P 1620><PRE 1621CLASS="PROGRAMLISTING" 1622> #include <ncurses.h> |
790 . 791 . 792 . 793 | 1623 . 1624 . 1625 . 1626 |
794 compile and link: gcc <program file> -lncurses</font> 795</pre></td> 796</tr> 797</table> 798<div class="EXAMPLE"><a name="BHW" id="BHW"></a> 799<p><b>Example 1. The Hello World !!! Program</b></p> 800<table border="0" bgcolor="#E0E0E0" width="100%"> 801<tr> 802<td> 803<pre class="PROGRAMLISTING"> 804<font color="#000000"><span class= 805"INLINEMEDIAOBJECT">#include <ncurses.h> | 1627 compile and link: gcc <program file> -lncurses</PRE 1628><DIV 1629CLASS="EXAMPLE" 1630><A 1631NAME="BHW" 1632></A 1633><P 1634><B 1635>Example 1. The Hello World !!! Program </B 1636></P 1637><PRE 1638CLASS="PROGRAMLISTING" 1639><SPAN 1640CLASS="INLINEMEDIAOBJECT" 1641>#include <ncurses.h> |
806 807int main() | 1642 1643int main() |
808{ 809 initscr(); /* Start curses mode */ 810 printw("Hello World !!!"); /* Print Hello World */ 811 refresh(); /* Print it on to the real screen */ 812 getch(); /* Wait for user input */ 813 endwin(); /* End curses mode */ | 1644{ 1645 initscr(); /* Start curses mode */ 1646 printw("Hello World !!!"); /* Print Hello World */ 1647 refresh(); /* Print it on to the real screen */ 1648 getch(); /* Wait for user input */ 1649 endwin(); /* End curses mode */ |
814 | 1650 |
815 return 0; 816}</span></font> 817</pre></td> 818</tr> 819</table> 820</div> 821</div> 822<div class="SECT2"> 823<hr> 824<h3 class="SECT2"><a name="DISSECTION" id="DISSECTION">2.2. 825Dissection</a></h3> 826<p>The above program prints "Hello World !!!" to the screen and 827exits. This program shows how to initialize curses and do screen 828manipulation and end curses mode. Let's dissect it line by 829line.</p> 830<div class="SECT3"> 831<hr> 832<h4 class="SECT3"><a name="ABOUT-INITSCR" id="ABOUT-INITSCR">2.2.1. 833About initscr()</a></h4> 834<p>The function initscr() initializes the terminal in curses mode. 835In some implementations, it clears the screen and presents a blank 836screen. To do any screen manipulation using curses package this has 837to be called first. This function initializes the curses system and 838allocates memory for our present window (called <var class= 839"LITERAL">stdscr</var>) and some other data-structures. Under 840extreme cases this function might fail due to insufficient memory 841to allocate memory for curses library's data structures.</p> 842<p>After this is done, we can do a variety of initializations to 843customize our curses settings. These details will be explained 844<a href="#INIT">later</a> .</p> 845</div> 846<div class="SECT3"> 847<hr> 848<h4 class="SECT3"><a name="MYST-REFRESH" id="MYST-REFRESH">2.2.2. 849The mysterious refresh()</a></h4> 850<p>The next line printw prints the string "Hello World !!!" on to 851the screen. This function is analogous to normal printf in all 852respects except that it prints the data on a window called stdscr 853at the current (y,x) co-ordinates. Since our present co-ordinates 854are at 0,0 the string is printed at the left hand corner of the 855window.</p> 856<p>This brings us to that mysterious refresh(). Well, when we 857called printw the data is actually written to an imaginary window, 858which is not updated on the screen yet. The job of printw is to 859update a few flags and data structures and write the data to a 860buffer corresponding to stdscr. In order to show it on the screen, 861we need to call refresh() and tell the curses system to dump the 862contents on the screen.</p> 863<p>The philosophy behind all this is to allow the programmer to do 864multiple updates on the imaginary screen or windows and do a 865refresh once all his screen update is done. refresh() checks the 866window and updates only the portion which has been changed. This 867improves performance and offers greater flexibility too. But, it is 868sometimes frustrating to beginners. A common mistake committed by 869beginners is to forget to call refresh() after they did some update 870through printw() class of functions. I still forget to add it 871sometimes :-)</p> 872</div> 873<div class="SECT3"> 874<hr> 875<h4 class="SECT3"><a name="ABOUT-ENDWIN" id="ABOUT-ENDWIN">2.2.3. 876About endwin()</a></h4> 877<p>And finally don't forget to end the curses mode. Otherwise your 878terminal might behave strangely after the program quits. endwin() 879frees the memory taken by curses sub-system and its data structures 880and puts the terminal in normal mode. This function must be called 881after you are done with the curses mode.</p> 882</div> 883</div> 884</div> 885<div class="SECT1"> 886<hr> 887<h2 class="SECT1"><a name="GORY" id="GORY">3. The Gory 888Details</a></h2> 889<p>Now that we have seen how to write a simple curses program let's 890get into the details. There are many functions that help customize 891what you see on screen and many features which can be put to full 892use.</p> 893<p>Here we go...</p> 894</div> 895<div class="SECT1"> 896<hr> 897<h2 class="SECT1"><a name="INIT" id="INIT">4. 898Initialization</a></h2> 899<p>We now know that to initialize curses system the function 900initscr() has to be called. There are functions which can be called 901after this initialization to customize our curses session. We may 902ask the curses system to set the terminal in raw mode or initialize 903color or initialize the mouse etc.. Let's discuss some of the 904functions that are normally called immediately after initscr();</p> 905<div class="SECT2"> 906<hr> 907<h3 class="SECT2"><a name="ABOUTINIT" id="ABOUTINIT">4.1. 908Initialization functions</a></h3> 909</div> 910<div class="SECT2"> 911<hr> 912<h3 class="SECT2"><a name="RAWCBREAK" id="RAWCBREAK">4.2. raw() and 913cbreak()</a></h3> 914<p>Normally the terminal driver buffers the characters a user types 915until a new line or carriage return is encountered. But most 916programs require that the characters be available as soon as the 917user types them. The above two functions are used to disable line 918buffering. The difference between these two functions is in the way 919control characters like suspend (CTRL-Z), interrupt and quit 920(CTRL-C) are passed to the program. In the raw() mode these 921characters are directly passed to the program without generating a 922signal. In the <var class="LITERAL">cbreak()</var> mode these 923control characters are interpreted as any other character by the 924terminal driver. I personally prefer to use raw() as I can exercise 925greater control over what the user does.</p> 926</div> 927<div class="SECT2"> 928<hr> 929<h3 class="SECT2"><a name="ECHONOECHO" id="ECHONOECHO">4.3. echo() 930and noecho()</a></h3> 931<p>These functions control the echoing of characters typed by the 932user to the terminal. <var class="LITERAL">noecho()</var> switches 933off echoing. The reason you might want to do this is to gain more 934control over echoing or to suppress unnecessary echoing while 935taking input from the user through the getch() etc. functions. Most 936of the interactive programs call <var class= 937"LITERAL">noecho()</var> at initialization and do the echoing of 938characters in a controlled manner. It gives the programmer the 939flexibility of echoing characters at any place in the window 940without updating current (y,x) co-ordinates.</p> 941</div> 942<div class="SECT2"> 943<hr> 944<h3 class="SECT2"><a name="KEYPAD" id="KEYPAD">4.4. 945keypad()</a></h3> 946<p>This is my favorite initialization function. It enables the 947reading of function keys like F1, F2, arrow keys etc. Almost every 948interactive program enables this, as arrow keys are a major part of 949any User Interface. Do <var class="LITERAL">keypad(stdscr, 950TRUE)</var> to enable this feature for the regular screen (stdscr). 951You will learn more about key management in later sections of this 952document.</p> 953</div> 954<div class="SECT2"> 955<hr> 956<h3 class="SECT2"><a name="HALFDELAY" id="HALFDELAY">4.5. 957halfdelay()</a></h3> 958<p>This function, though not used very often, is a useful one at 959times. halfdelay()is called to enable the half-delay mode, which is 960similar to the cbreak() mode in that characters typed are 961immediately available to program. However, it waits for 'X' tenths 962of a second for input and then returns ERR, if no input is 963available. 'X' is the timeout value passed to the function 964halfdelay(). This function is useful when you want to ask the user 965for input, and if he doesn't respond with in certain time, we can 966do some thing else. One possible example is a timeout at the 967password prompt.</p> 968</div> 969<div class="SECT2"> 970<hr> 971<h3 class="SECT2"><a name="MISCINIT" id="MISCINIT">4.6. 972Miscellaneous Initialization functions</a></h3> 973<p>There are few more functions which are called at initialization 974to customize curses behavior. They are not used as extensively as 975those mentioned above. Some of them are explained where 976appropriate.</p> 977</div> 978<div class="SECT2"> 979<hr> 980<h3 class="SECT2"><a name="INITEX" id="INITEX">4.7. An 981Example</a></h3> 982<p>Let's write a program which will clarify the usage of these 983functions.</p> 984<div class="EXAMPLE"><a name="BINFU" id="BINFU"></a> 985<p><b>Example 2. Initialization Function Usage example</b></p> 986<table border="0" bgcolor="#E0E0E0" width="100%"> 987<tr> 988<td> 989<pre class="PROGRAMLISTING"> 990<font color="#000000"><span class= 991"INLINEMEDIAOBJECT">#include <ncurses.h> | 1651 return 0; 1652}</SPAN 1653></PRE 1654></DIV 1655></DIV 1656><DIV 1657CLASS="SECT2" 1658><HR><H3 1659CLASS="SECT2" 1660><A 1661NAME="DISSECTION" 1662>2.2. Dissection</A 1663></H3 1664><P 1665> 1666The above program prints "Hello World !!!" to the screen and exits. This 1667program shows how to initialize curses and do screen manipulation and 1668end curses mode. Let's dissect it line by line. </P 1669><DIV 1670CLASS="SECT3" 1671><HR><H4 1672CLASS="SECT3" 1673><A 1674NAME="ABOUT-INITSCR" 1675>2.2.1. About initscr()</A 1676></H4 1677><P 1678>The function initscr() initializes the terminal in curses mode. In some 1679implementations, it clears the screen and presents a blank screen. To do any 1680screen manipulation using curses package this has to be called first. This 1681function initializes the curses system and allocates memory for our present 1682window (called <TT 1683CLASS="LITERAL" 1684>stdscr</TT 1685>) and some other data-structures. Under extreme 1686cases this function might fail due to insufficient memory to allocate memory 1687for curses library's data structures. </P 1688><P 1689> 1690After this is done, we can do a variety of initializations to customize 1691our curses settings. These details will be explained <A 1692HREF="#INIT" 1693>later </A 1694>.</P 1695></DIV 1696><DIV 1697CLASS="SECT3" 1698><HR><H4 1699CLASS="SECT3" 1700><A 1701NAME="MYST-REFRESH" 1702>2.2.2. The mysterious refresh()</A 1703></H4 1704><P 1705>The next line printw prints the string "Hello World !!!" on to the screen. This 1706function is analogous to normal printf in all respects except that it prints 1707the data on a window called stdscr at the current (y,x) co-ordinates. Since our 1708present co-ordinates are at 0,0 the string is printed at the left hand corner 1709of the window.</P 1710><P 1711>This brings us to that mysterious refresh(). Well, when we called printw 1712the data is actually written to an imaginary window, which is not updated 1713on the screen yet. The job of printw is to update a few flags 1714and data structures and write the data to a buffer corresponding to stdscr. 1715In order to show it on the screen, we need to call refresh() and tell the 1716curses system to dump the contents on the screen.</P 1717><P 1718>The philosophy behind all this is to allow the programmer to do multiple updates 1719on the imaginary screen or windows and do a refresh once all his screen update 1720is done. refresh() checks the window and updates only the portion which has been 1721changed. This improves performance and offers greater flexibility too. But, it is 1722sometimes frustrating to beginners. A common mistake committed by beginners is 1723to forget to call refresh() after they did some update through printw() class of 1724functions. I still forget to add it sometimes :-) </P 1725></DIV 1726><DIV 1727CLASS="SECT3" 1728><HR><H4 1729CLASS="SECT3" 1730><A 1731NAME="ABOUT-ENDWIN" 1732>2.2.3. About endwin()</A 1733></H4 1734><P 1735>And finally don't forget to end the curses mode. Otherwise your terminal might 1736behave strangely after the program quits. endwin() frees the memory taken by 1737curses sub-system and its data structures and puts the terminal in normal 1738mode. This function must be called after you are done with the curses mode. </P 1739></DIV 1740></DIV 1741></DIV 1742><DIV 1743CLASS="SECT1" 1744><HR><H2 1745CLASS="SECT1" 1746><A 1747NAME="GORY" 1748>3. The Gory Details</A 1749></H2 1750><P 1751>Now that we have seen how to write a simple curses program let's get into the 1752details. There are many functions that help customize what you see on screen and 1753many features which can be put to full use. </P 1754><P 1755>Here we go...</P 1756></DIV 1757><DIV 1758CLASS="SECT1" 1759><HR><H2 1760CLASS="SECT1" 1761><A 1762NAME="INIT" 1763>4. Initialization</A 1764></H2 1765><P 1766>We now know that to initialize curses system the function initscr() has to be 1767called. There are functions which can be called after this initialization to 1768customize our curses session. We may ask the curses system to set the terminal 1769in raw mode or initialize color or initialize the mouse etc.. Let's discuss some 1770of the functions that are normally called immediately after initscr();</P 1771><DIV 1772CLASS="SECT2" 1773><HR><H3 1774CLASS="SECT2" 1775><A 1776NAME="ABOUTINIT" 1777>4.1. Initialization functions</A 1778></H3 1779><P 1780> </P 1781></DIV 1782><DIV 1783CLASS="SECT2" 1784><HR><H3 1785CLASS="SECT2" 1786><A 1787NAME="RAWCBREAK" 1788>4.2. raw() and cbreak()</A 1789></H3 1790><P 1791>Normally the terminal driver buffers the characters a user types until a new 1792line or carriage return is encountered. But most programs require that the 1793characters be available as soon as the user types them. The above two functions 1794are used to disable line buffering. The difference between these two functions 1795is in the way control characters like suspend (CTRL-Z), interrupt and quit 1796(CTRL-C) are passed to the program. In the raw() mode these characters are 1797directly passed to the program without generating a signal. In the 1798<TT 1799CLASS="LITERAL" 1800>cbreak()</TT 1801> mode these control characters are 1802interpreted as any other character by the terminal driver. I personally prefer 1803to use raw() as I can exercise greater control over what the user does.</P 1804></DIV 1805><DIV 1806CLASS="SECT2" 1807><HR><H3 1808CLASS="SECT2" 1809><A 1810NAME="ECHONOECHO" 1811>4.3. echo() and noecho()</A 1812></H3 1813><P 1814> 1815These functions control the echoing of characters typed by the user to the 1816terminal. <TT 1817CLASS="LITERAL" 1818>noecho()</TT 1819> switches off echoing. The 1820reason you might want to do this is to gain more control over echoing or to 1821suppress unnecessary echoing while taking input from the user through the 1822getch() etc. functions. Most of the interactive programs call 1823<TT 1824CLASS="LITERAL" 1825>noecho()</TT 1826> at initialization and do the echoing 1827of characters in a controlled manner. It gives the programmer the flexibility 1828of echoing characters at any place in the window without updating current (y,x) 1829co-ordinates. </P 1830></DIV 1831><DIV 1832CLASS="SECT2" 1833><HR><H3 1834CLASS="SECT2" 1835><A 1836NAME="KEYPAD" 1837>4.4. keypad()</A 1838></H3 1839><P 1840>This is my favorite initialization function. It enables the reading of function 1841keys like F1, F2, arrow keys etc. Almost every interactive program enables this, 1842as arrow keys are a major part of any User Interface. Do 1843<TT 1844CLASS="LITERAL" 1845>keypad(stdscr, TRUE) </TT 1846> to enable this feature 1847for the regular screen (stdscr). You will learn more about key management in 1848later sections of this document.</P 1849></DIV 1850><DIV 1851CLASS="SECT2" 1852><HR><H3 1853CLASS="SECT2" 1854><A 1855NAME="HALFDELAY" 1856>4.5. halfdelay()</A 1857></H3 1858><P 1859>This function, though not used very often, is a useful one at times. 1860halfdelay()is called to enable the half-delay mode, which is similar to the 1861cbreak() mode in that characters typed are immediately available to program. 1862However, it waits for 'X' tenths of a second for input and then returns ERR, if 1863no input is available. 'X' is the timeout value passed to the function 1864halfdelay(). This function is useful when you want to ask the user for input, 1865and if he doesn't respond with in certain time, we can do some thing else. One 1866possible example is a timeout at the password prompt. </P 1867></DIV 1868><DIV 1869CLASS="SECT2" 1870><HR><H3 1871CLASS="SECT2" 1872><A 1873NAME="MISCINIT" 1874>4.6. Miscellaneous Initialization functions</A 1875></H3 1876><P 1877>There are few more functions which are called at initialization to 1878customize curses behavior. They are not used as extensively as those mentioned 1879above. Some of them are explained where appropriate.</P 1880></DIV 1881><DIV 1882CLASS="SECT2" 1883><HR><H3 1884CLASS="SECT2" 1885><A 1886NAME="INITEX" 1887>4.7. An Example</A 1888></H3 1889><P 1890>Let's write a program which will clarify the usage of these functions.</P 1891><DIV 1892CLASS="EXAMPLE" 1893><A 1894NAME="BINFU" 1895></A 1896><P 1897><B 1898>Example 2. Initialization Function Usage example </B 1899></P 1900><PRE 1901CLASS="PROGRAMLISTING" 1902><SPAN 1903CLASS="INLINEMEDIAOBJECT" 1904>#include <ncurses.h> |
992 993int main() | 1905 1906int main() |
994{ int ch; | 1907{ int ch; |
995 | 1908 |
996 initscr(); /* Start curses mode */ 997 raw(); /* Line buffering disabled */ 998 keypad(stdscr, TRUE); /* We get F1, F2 etc.. */ 999 noecho(); /* Don't echo() while we do getch */ | 1909 initscr(); /* Start curses mode */ 1910 raw(); /* Line buffering disabled */ 1911 keypad(stdscr, TRUE); /* We get F1, F2 etc.. */ 1912 noecho(); /* Don't echo() while we do getch */ |
1000 | 1913 |
1001 printw("Type any character to see it in bold\n"); 1002 ch = getch(); /* If raw() hadn't been called 1003 * we have to press enter before it 1004 * gets to the program */ 1005 if(ch == KEY_F(1)) /* Without keypad enabled this will */ 1006 printw("F1 Key pressed");/* not get to us either */ 1007 /* Without noecho() some ugly escape 1008 * charachters might have been printed 1009 * on screen */ 1010 else 1011 { printw("The pressed key is "); 1012 attron(A_BOLD); 1013 printw("%c", ch); 1014 attroff(A_BOLD); 1015 } 1016 refresh(); /* Print it on to the real screen */ 1017 getch(); /* Wait for user input */ 1018 endwin(); /* End curses mode */ | 1914 printw("Type any character to see it in bold\n"); 1915 ch = getch(); /* If raw() hadn't been called 1916 * we have to press enter before it 1917 * gets to the program */ 1918 if(ch == KEY_F(1)) /* Without keypad enabled this will */ 1919 printw("F1 Key pressed");/* not get to us either */ 1920 /* Without noecho() some ugly escape 1921 * charachters might have been printed 1922 * on screen */ 1923 else 1924 { printw("The pressed key is "); 1925 attron(A_BOLD); 1926 printw("%c", ch); 1927 attroff(A_BOLD); 1928 } 1929 refresh(); /* Print it on to the real screen */ 1930 getch(); /* Wait for user input */ 1931 endwin(); /* End curses mode */ |
1019 | 1932 |
1020 return 0; 1021}</span></font> 1022</pre></td> 1023</tr> 1024</table> 1025</div> 1026<p>This program is self-explanatory. But I used functions which 1027aren't explained yet. The function <var class= 1028"LITERAL">getch()</var> is used to get a character from user. It is 1029equivalent to normal <var class="LITERAL">getchar()</var> except 1030that we can disable the line buffering to avoid <enter> after 1031input. Look for more about <var class="LITERAL">getch()</var>and 1032reading keys in the <a href="#KEYS">key management section</a> . 1033The functions attron and attroff are used to switch some attributes 1034on and off respectively. In the example I used them to print the 1035character in bold. These functions are explained in detail 1036later.</p> 1037</div> 1038</div> 1039<div class="SECT1"> 1040<hr> 1041<h2 class="SECT1"><a name="AWORDWINDOWS" id="AWORDWINDOWS">5. A 1042Word about Windows</a></h2> 1043<p>Before we plunge into the myriad ncurses functions, let me clear 1044few things about windows. Windows are explained in detail in 1045following <a href="#WINDOWS">sections</a></p> 1046<p>A Window is an imaginary screen defined by curses system. A 1047window does not mean a bordered window which you usually see on 1048Win9X platforms. When curses is initialized, it creates a default 1049window named <var class="LITERAL">stdscr</var> which represents 1050your 80x25 (or the size of window in which you are running) screen. 1051If you are doing simple tasks like printing few strings, reading 1052input etc., you can safely use this single window for all of your 1053purposes. You can also create windows and call functions which 1054explicitly work on the specified window.</p> 1055<p>For example, if you call</p> 1056<table border="0" bgcolor="#E0E0E0" width="100%"> 1057<tr> 1058<td> 1059<pre class="PROGRAMLISTING"> 1060<font color="#000000"> printw("Hi There !!!"); 1061 refresh();</font> 1062</pre></td> 1063</tr> 1064</table> 1065<p>It prints the string on stdscr at the present cursor position. 1066Similarly the call to refresh(), works on stdscr only.</p> 1067<p>Say you have created <a href="#WINDOWS">windows</a> then you 1068have to call a function with a 'w' added to the usual function.</p> 1069<table border="0" bgcolor="#E0E0E0" width="100%"> 1070<tr> 1071<td> 1072<pre class="PROGRAMLISTING"> 1073<font color="#000000"> wprintw(win, "Hi There !!!"); 1074 wrefresh(win);</font> 1075</pre></td> 1076</tr> 1077</table> 1078<p>As you will see in the rest of the document, naming of functions 1079follow the same convention. For each function there usually are 1080three more functions.</p> 1081<table border="0" bgcolor="#E0E0E0" width="100%"> 1082<tr> 1083<td> 1084<pre class="PROGRAMLISTING"> 1085<font color= 1086"#000000"> printw(string); /* Print on stdscr at present cursor position */ | 1933 return 0; 1934}</SPAN 1935></PRE 1936></DIV 1937><P 1938>This program is self-explanatory. But I used functions which aren't explained 1939yet. The function <TT 1940CLASS="LITERAL" 1941>getch()</TT 1942> is used to get a 1943character from user. It is equivalent to normal 1944<TT 1945CLASS="LITERAL" 1946>getchar()</TT 1947> except that we can disable the line 1948buffering to avoid <enter> after input. Look for more about 1949<TT 1950CLASS="LITERAL" 1951>getch()</TT 1952>and reading keys in the <A 1953HREF="#KEYS" 1954> key management section </A 1955>. The functions attron and attroff 1956are used to switch some attributes on and off respectively. In the example I 1957used them to print the character in bold. These functions are explained in detail 1958later.</P 1959></DIV 1960></DIV 1961><DIV 1962CLASS="SECT1" 1963><HR><H2 1964CLASS="SECT1" 1965><A 1966NAME="AWORDWINDOWS" 1967>5. A Word about Windows</A 1968></H2 1969><P 1970> 1971Before we plunge into the myriad ncurses functions, let me clear few things 1972about windows. Windows are explained in detail in following <A 1973HREF="#WINDOWS" 1974> sections </A 1975></P 1976><P 1977>A Window is an imaginary screen defined by curses system. A window does not mean 1978a bordered window which you usually see on Win9X platforms. When curses is 1979initialized, it creates a default window named 1980<TT 1981CLASS="LITERAL" 1982>stdscr</TT 1983> which represents your 80x25 (or the size 1984of window in which you are running) screen. If you are doing simple tasks like 1985printing few strings, reading input etc., you can safely use this single window 1986for all of your purposes. You can also create windows and call functions which 1987explicitly work on the specified window.</P 1988><P 1989>For example, if you call</P 1990><PRE 1991CLASS="PROGRAMLISTING" 1992> printw("Hi There !!!"); 1993 refresh();</PRE 1994><P 1995>It prints the string on stdscr at the present cursor position. Similarly the 1996call to refresh(), works on stdscr only. </P 1997><P 1998>Say you have created <A 1999HREF="#WINDOWS" 2000>windows</A 2001> then you have to 2002call a function with a 'w' added to the usual function.</P 2003><PRE 2004CLASS="PROGRAMLISTING" 2005> wprintw(win, "Hi There !!!"); 2006 wrefresh(win);</PRE 2007><P 2008>As you will see in the rest of the document, naming of functions follow the 2009same convention. For each function there usually are three more functions.</P 2010><PRE 2011CLASS="PROGRAMLISTING" 2012> printw(string); /* Print on stdscr at present cursor position */ |
1087 mvprintw(y, x, string);/* Move to (y, x) then print string */ 1088 wprintw(win, string); /* Print on window win at present cursor position */ 1089 /* in the window */ 1090 mvwprintw(win, y, x, string); /* Move to (y, x) relative to window */ | 2013 mvprintw(y, x, string);/* Move to (y, x) then print string */ 2014 wprintw(win, string); /* Print on window win at present cursor position */ 2015 /* in the window */ 2016 mvwprintw(win, y, x, string); /* Move to (y, x) relative to window */ |
1091 /* co-ordinates and then print */</font> 1092</pre></td> 1093</tr> 1094</table> 1095<p>Usually the w-less functions are macros which expand to 1096corresponding w-function with stdscr as the window parameter.</p> 1097</div> 1098<div class="SECT1"> 1099<hr> 1100<h2 class="SECT1"><a name="PRINTW" id="PRINTW">6. Output 1101functions</a></h2> 1102<p>I guess you can't wait any more to see some action. Back to our 1103odyssey of curses functions. Now that curses is initialized, let's 1104interact with world.</p> 1105<p>There are three classes of functions which you can use to do 1106output on screen.</p> 1107<ol type="1"> 1108<li> 1109<p>addch() class: Print single character with attributes</p> 1110</li> 1111<li> 1112<p>printw() class: Print formatted output similar to printf()</p> 1113</li> 1114<li> 1115<p>addstr() class: Print strings</p> 1116</li> 1117</ol> 1118<p>These functions can be used interchangeably and it's a matter of 1119style as to which class is used. Let's see each one in detail.</p> 1120<div class="SECT2"> 1121<hr> 1122<h3 class="SECT2"><a name="ADDCHCLASS" id="ADDCHCLASS">6.1. addch() 1123class of functions</a></h3> 1124<p>These functions put a single character into the current cursor 1125location and advance the position of the cursor. You can give the 1126character to be printed but they usually are used to print a 1127character with some attributes. Attributes are explained in detail 1128in later <a href="#ATTRIB">sections</a> of the document. If a 1129character is associated with an attribute(bold, reverse video 1130etc.), when curses prints the character, it is printed in that 1131attribute.</p> 1132<p>In order to combine a character with some attributes, you have 1133two options:</p> 1134<ul> 1135<li> 1136<p>By OR'ing a single character with the desired attribute macros. 1137These attribute macros could be found in the header file 1138<var class="LITERAL">ncurses.h</var>. For example, you want to 1139print a character ch(of type char) bold and underlined, you would 1140call addch() as below.</p> 1141<table border="0" bgcolor="#E0E0E0" width="90%"> 1142<tr> 1143<td> 1144<pre class="PROGRAMLISTING"> 1145<font color="#000000"> addch(ch | A_BOLD | A_UNDERLINE);</font> 1146</pre></td> 1147</tr> 1148</table> 1149</li> 1150<li> 1151<p>By using functions like <var class= 1152"LITERAL">attrset(),attron(),attroff()</var>. These functions are 1153explained in the <a href="#ATTRIB">Attributes</a> section. Briefly, 1154they manipulate the current attributes of the given window. Once 1155set, the character printed in the window are associated with the 1156attributes until it is turned off.</p> 1157</li> 1158</ul> 1159<p>Additionally, <var class="LITERAL">curses</var> provides some 1160special characters for character-based graphics. You can draw 1161tables, horizontal or vertical lines, etc. You can find all 1162avaliable characters in the header file <var class= 1163"LITERAL">ncurses.h</var>. Try looking for macros beginning with 1164<var class="LITERAL">ACS_</var> in this file.</p> 1165</div> 1166<div class="SECT2"> 1167<hr> 1168<h3 class="SECT2"><a name="AEN298" id="AEN298">6.2. mvaddch(), 1169waddch() and mvwaddch()</a></h3> 1170<p><var class="LITERAL">mvaddch()</var> is used to move the cursor 1171to a given point, and then print. Thus, the calls:</p> 1172<table border="0" bgcolor="#E0E0E0" width="100%"> 1173<tr> 1174<td> 1175<pre class="PROGRAMLISTING"> 1176<font color= 1177"#000000"> move(row,col); /* moves the cursor to row<em>th</em> row and col<em>th</em> column */ 1178 addch(ch);</font> 1179</pre></td> 1180</tr> 1181</table> | 2017 /* co-ordinates and then print */</PRE 2018><P 2019>Usually the w-less functions are macros which expand to corresponding w-function 2020with stdscr as the window parameter.</P 2021></DIV 2022><DIV 2023CLASS="SECT1" 2024><HR><H2 2025CLASS="SECT1" 2026><A 2027NAME="PRINTW" 2028>6. Output functions</A 2029></H2 2030><P 2031>I guess you can't wait any more to see some action. Back to our odyssey of 2032curses functions. Now that curses is initialized, let's interact with 2033world.</P 2034><P 2035>There are three classes of functions which you can use to do output on screen. 2036<P 2037></P 2038><OL 2039TYPE="1" 2040><LI 2041><P 2042>addch() class: Print single character with attributes </P 2043></LI 2044><LI 2045><P 2046>printw() class: Print formatted output similar to printf()</P 2047></LI 2048><LI 2049><P 2050>addstr() class: Print strings</P 2051></LI 2052></OL 2053></P 2054><P 2055>These functions can be used interchangeably and it's a matter of style as to 2056which class is used. Let's see each one in detail.</P 2057><DIV 2058CLASS="SECT2" 2059><HR><H3 2060CLASS="SECT2" 2061><A 2062NAME="ADDCHCLASS" 2063>6.1. addch() class of functions</A 2064></H3 2065><P 2066>These functions put a single character into the current cursor location and 2067advance the position of the cursor. You can give the character to be printed but 2068they usually are used to print a character with some attributes. Attributes are 2069explained in detail in later <A 2070HREF="#ATTRIB" 2071> sections </A 2072> of the 2073document. If a character is associated with an attribute(bold, reverse video 2074etc.), when curses prints the character, it is printed in that attribute.</P 2075><P 2076>In order to combine a character with some attributes, you have two options:</P 2077><P 2078></P 2079><UL 2080><LI 2081><P 2082>By OR'ing a single character with the desired attribute macros. These attribute 2083macros could be found in the header file 2084<TT 2085CLASS="LITERAL" 2086>ncurses.h</TT 2087>. For example, you want to print a 2088character ch(of type char) bold and underlined, you would call addch() as below. 2089<PRE 2090CLASS="PROGRAMLISTING" 2091> addch(ch | A_BOLD | A_UNDERLINE);</PRE 2092></P 2093></LI 2094><LI 2095><P 2096>By using functions like <TT 2097CLASS="LITERAL" 2098>attrset(),attron(),attroff()</TT 2099>. These functions are explained in the <A 2100HREF="#ATTRIB" 2101>Attributes</A 2102> section. Briefly, they manipulate the current attributes of 2103the given window. Once set, the character printed in the window are associated 2104with the attributes until it is turned off.</P 2105></LI 2106></UL 2107><P 2108>Additionally, <TT 2109CLASS="LITERAL" 2110>curses</TT 2111> provides some special 2112characters for character-based graphics. You can draw tables, horizontal or 2113vertical lines, etc. You can find all avaliable characters in the header file 2114<TT 2115CLASS="LITERAL" 2116>ncurses.h</TT 2117>. Try looking for macros beginning 2118with <TT 2119CLASS="LITERAL" 2120>ACS_</TT 2121> in this file. </P 2122></DIV 2123><DIV 2124CLASS="SECT2" 2125><HR><H3 2126CLASS="SECT2" 2127><A 2128NAME="AEN298" 2129>6.2. mvaddch(), waddch() and mvwaddch()</A 2130></H3 2131><P 2132><TT 2133CLASS="LITERAL" 2134>mvaddch()</TT 2135> is used to move the cursor to a 2136given point, and then print. Thus, the calls: 2137<PRE 2138CLASS="PROGRAMLISTING" 2139> move(row,col); /* moves the cursor to row<SPAN 2140CLASS="emphasis" 2141><I 2142CLASS="EMPHASIS" 2143>th</I 2144></SPAN 2145> row and col<SPAN 2146CLASS="emphasis" 2147><I 2148CLASS="EMPHASIS" 2149>th</I 2150></SPAN 2151> column */ 2152 addch(ch);</PRE 2153> |
1182can be replaced by | 2154can be replaced by |
1183<table border="0" bgcolor="#E0E0E0" width="100%"> 1184<tr> 1185<td> 1186<pre class="PROGRAMLISTING"> 1187<font color="#000000"> mvaddch(row,col,ch);</font> 1188</pre></td> 1189</tr> 1190</table> 1191<p><var class="LITERAL">waddch()</var> is similar to <var class= 1192"LITERAL">addch()</var>, except that it adds a character into the 1193given window. (Note that <var class="LITERAL">addch()</var> adds a 1194character into the window <var class="LITERAL">stdscr</var>.)</p> 1195<p>In a similar fashion <var class="LITERAL">mvwaddch()</var> 1196function is used to add a character into the given window at the 1197given coordinates.</p> 1198<p>Now, we are familiar with the basic output function <var class= 1199"LITERAL">addch()</var>. But, if we want to print a string, it 1200would be very annoying to print it character by character. 1201Fortunately, <var class="LITERAL">ncurses</var> provides 1202<var class="LITERAL">printf</var><em>-like</em> or <var class= 1203"LITERAL">puts</var><em>-like</em> functions.</p> 1204</div> 1205<div class="SECT2"> 1206<hr> 1207<h3 class="SECT2"><a name="PRINTWCLASS" id="PRINTWCLASS">6.3. 1208printw() class of functions</a></h3> 1209<p>These functions are similar to <var class= 1210"LITERAL">printf()</var> with the added capability of printing at 1211any position on the screen.</p> 1212<div class="SECT3"> 1213<hr> 1214<h4 class="SECT3"><a name="PRINTWMVPRINTW" id= 1215"PRINTWMVPRINTW">6.3.1. printw() and mvprintw</a></h4> 1216<p>These two functions work much like <var class= 1217"LITERAL">printf()</var>. <var class="LITERAL">mvprintw()</var> can 1218be used to move the cursor to a position and then print. If you 1219want to move the cursor first and then print using <var class= 1220"LITERAL">printw()</var> function, use <var class= 1221"LITERAL">move()</var> first and then use <var class= 1222"LITERAL">printw()</var> though I see no point why one should avoid 1223using <var class="LITERAL">mvprintw()</var>, you have the 1224flexibility to manipulate.</p> 1225</div> 1226<div class="SECT3"> 1227<hr> 1228<h4 class="SECT3"><a name="WPRINTWMVWPRINTW" id= 1229"WPRINTWMVWPRINTW">6.3.2. wprintw() and mvwprintw</a></h4> 1230<p>These two functions are similar to above two except that they 1231print in the corresponding window given as argument.</p> 1232</div> 1233<div class="SECT3"> 1234<hr> 1235<h4 class="SECT3"><a name="VWPRINTW" id="VWPRINTW">6.3.3. 1236vwprintw()</a></h4> 1237<p>This function is similar to <var class= 1238"LITERAL">vprintf()</var>. This can be used when variable number of 1239arguments are to be printed.</p> 1240</div> 1241<div class="SECT3"> 1242<hr> 1243<h4 class="SECT3"><a name="SIMPLEPRINTWEX" id= 1244"SIMPLEPRINTWEX">6.3.4. A Simple printw example</a></h4> 1245<div class="EXAMPLE"><a name="BPREX" id="BPREX"></a> 1246<p><b>Example 3. A Simple printw example</b></p> 1247<table border="0" bgcolor="#E0E0E0" width="100%"> 1248<tr> 1249<td> 1250<pre class="PROGRAMLISTING"> 1251<font color="#000000"><span class= 1252"INLINEMEDIAOBJECT">#include <ncurses.h> /* ncurses.h includes stdio.h */ 1253#include <string.h> | 2155<PRE 2156CLASS="PROGRAMLISTING" 2157> mvaddch(row,col,ch);</PRE 2158></P 2159><P 2160><TT 2161CLASS="LITERAL" 2162>waddch()</TT 2163> is similar to 2164<TT 2165CLASS="LITERAL" 2166>addch()</TT 2167>, except that it adds a character into 2168the given window. (Note that <TT 2169CLASS="LITERAL" 2170>addch()</TT 2171> adds a 2172character into the window <TT 2173CLASS="LITERAL" 2174>stdscr</TT 2175>.)</P 2176><P 2177>In a similar fashion <TT 2178CLASS="LITERAL" 2179>mvwaddch()</TT 2180> function is 2181used to add a character into the given window at the given coordinates.</P 2182><P 2183>Now, we are familiar with the basic output function 2184<TT 2185CLASS="LITERAL" 2186>addch()</TT 2187>. But, if we want to print a string, it 2188would be very annoying to print it character by character. Fortunately, 2189<TT 2190CLASS="LITERAL" 2191>ncurses</TT 2192> provides <TT 2193CLASS="LITERAL" 2194>printf</TT 2195><SPAN 2196CLASS="emphasis" 2197><I 2198CLASS="EMPHASIS" 2199>-like</I 2200></SPAN 2201> or 2202<TT 2203CLASS="LITERAL" 2204>puts</TT 2205><SPAN 2206CLASS="emphasis" 2207><I 2208CLASS="EMPHASIS" 2209>-like</I 2210></SPAN 2211> functions.</P 2212></DIV 2213><DIV 2214CLASS="SECT2" 2215><HR><H3 2216CLASS="SECT2" 2217><A 2218NAME="PRINTWCLASS" 2219>6.3. printw() class of functions</A 2220></H3 2221><P 2222>These functions are similar to <TT 2223CLASS="LITERAL" 2224>printf()</TT 2225> with 2226the added capability of printing at any position on the screen. </P 2227><DIV 2228CLASS="SECT3" 2229><HR><H4 2230CLASS="SECT3" 2231><A 2232NAME="PRINTWMVPRINTW" 2233>6.3.1. printw() and mvprintw</A 2234></H4 2235><P 2236>These two functions work much like <TT 2237CLASS="LITERAL" 2238>printf()</TT 2239>. 2240<TT 2241CLASS="LITERAL" 2242>mvprintw()</TT 2243> can be used to move the cursor to a 2244position and then print. If you want to move the cursor first and then print 2245using <TT 2246CLASS="LITERAL" 2247>printw()</TT 2248> function, use 2249<TT 2250CLASS="LITERAL" 2251>move() </TT 2252> first and then use 2253<TT 2254CLASS="LITERAL" 2255>printw()</TT 2256> though I see no point why one should 2257avoid using <TT 2258CLASS="LITERAL" 2259>mvprintw()</TT 2260>, you have the 2261flexibility to manipulate. </P 2262></DIV 2263><DIV 2264CLASS="SECT3" 2265><HR><H4 2266CLASS="SECT3" 2267><A 2268NAME="WPRINTWMVWPRINTW" 2269>6.3.2. wprintw() and mvwprintw</A 2270></H4 2271><P 2272>These two functions are similar to above two except that they print in the 2273corresponding window given as argument. </P 2274></DIV 2275><DIV 2276CLASS="SECT3" 2277><HR><H4 2278CLASS="SECT3" 2279><A 2280NAME="VWPRINTW" 2281>6.3.3. vwprintw()</A 2282></H4 2283><P 2284>This function is similar to <TT 2285CLASS="LITERAL" 2286>vprintf()</TT 2287>. This can 2288be used when variable number of arguments are to be printed.</P 2289></DIV 2290><DIV 2291CLASS="SECT3" 2292><HR><H4 2293CLASS="SECT3" 2294><A 2295NAME="SIMPLEPRINTWEX" 2296>6.3.4. A Simple printw example</A 2297></H4 2298><DIV 2299CLASS="EXAMPLE" 2300><A 2301NAME="BPREX" 2302></A 2303><P 2304><B 2305>Example 3. A Simple printw example </B 2306></P 2307><PRE 2308CLASS="PROGRAMLISTING" 2309><SPAN 2310CLASS="INLINEMEDIAOBJECT" 2311>#include <ncurses.h> /* ncurses.h includes stdio.h */ 2312#include <string.h> |
1254 1255int main() 1256{ | 2313 2314int main() 2315{ |
1257 char mesg[]="Just a string"; /* message to be appeared on the screen */ 1258 int row,col; /* to store the number of rows and * 1259 * the number of colums of the screen */ 1260 initscr(); /* start the curses mode */ 1261 getmaxyx(stdscr,row,col); /* get the number of rows and columns */ | 2316 char mesg[]="Just a string"; /* message to be appeared on the screen */ 2317 int row,col; /* to store the number of rows and * 2318 * the number of colums of the screen */ 2319 initscr(); /* start the curses mode */ 2320 getmaxyx(stdscr,row,col); /* get the number of rows and columns */ |
1262 mvprintw(row/2,(col-strlen(mesg))/2,"%s",mesg); | 2321 mvprintw(row/2,(col-strlen(mesg))/2,"%s",mesg); |
1263 /* print the message at the center of the screen */ | 2322 /* print the message at the center of the screen */ |
1264 mvprintw(row-2,0,"This screen has %d rows and %d columns\n",row,col); 1265 printw("Try resizing your window(if possible) and then run this program again"); 1266 refresh(); 1267 getch(); 1268 endwin(); 1269 1270 return 0; | 2323 mvprintw(row-2,0,"This screen has %d rows and %d columns\n",row,col); 2324 printw("Try resizing your window(if possible) and then run this program again"); 2325 refresh(); 2326 getch(); 2327 endwin(); 2328 2329 return 0; |
1271}</span></font> 1272</pre></td> 1273</tr> 1274</table> 1275</div> 1276<p>Above program demonstrates how easy it is to use <var class= 1277"LITERAL">printw</var>. You just feed the coordinates and the 1278message to be appeared on the screen, then it does what you 1279want.</p> 1280<p>The above program introduces us to a new function <var class= 1281"LITERAL">getmaxyx()</var>, a macro defined in <var class= 1282"LITERAL">ncurses.h</var>. It gives the number of columns and the 1283number of rows in a given window. <var class= 1284"LITERAL">getmaxyx()</var> does this by updating the variables 1285given to it. Since <var class="LITERAL">getmaxyx()</var> is not a 1286function we don't pass pointers to it, we just give two integer 1287variables.</p> 1288</div> 1289</div> 1290<div class="SECT2"> 1291<hr> 1292<h3 class="SECT2"><a name="ADDSTRCLASS" id="ADDSTRCLASS">6.4. 1293addstr() class of functions</a></h3> 1294<p><var class="LITERAL">addstr()</var> is used to put a character 1295string into a given window. This function is similar to calling 1296<var class="LITERAL">addch()</var> once for each character in a 1297given string. This is true for all output functions. There are 1298other functions from this family such as <var class= 1299"LITERAL">mvaddstr(),mvwaddstr()</var> and <var class= 1300"LITERAL">waddstr()</var>, which obey the naming convention of 1301curses.(e.g. mvaddstr() is similar to the respective calls move() 1302and then addstr().) Another function of this family is addnstr(), 1303which takes an integer parameter(say n) additionally. This function 1304puts at most n characters into the screen. If n is negative, then 1305the entire string will be added.</p> 1306</div> 1307<div class="SECT2"> 1308<hr> 1309<h3 class="SECT2"><a name="ACAUTION" id="ACAUTION">6.5. A word of 1310caution</a></h3> 1311<p>All these functions take y co-ordinate first and then x in their 1312arguments. A common mistake by beginners is to pass x,y in that 1313order. If you are doing too many manipulations of (y,x) 1314co-ordinates, think of dividing the screen into windows and 1315manipulate each one separately. Windows are explained in the 1316<a href="#WINDOWS">windows</a> section.</p> 1317</div> 1318</div> 1319<div class="SECT1"> 1320<hr> 1321<h2 class="SECT1"><a name="SCANW" id="SCANW">7. Input 1322functions</a></h2> 1323<p>Well, printing without taking input, is boring. Let's see 1324functions which allow us to get input from user. These functions 1325also can be divided into three categories.</p> 1326<ol type="1"> 1327<li> 1328<p>getch() class: Get a character</p> 1329</li> 1330<li> 1331<p>scanw() class: Get formatted input</p> 1332</li> 1333<li> 1334<p>getstr() class: Get strings</p> 1335</li> 1336</ol> 1337<div class="SECT2"> 1338<hr> 1339<h3 class="SECT2"><a name="GETCHCLASS" id="GETCHCLASS">7.1. getch() 1340class of functions</a></h3> 1341<p>These functions read a single character from the terminal. But 1342there are several subtle facts to consider. For example if you 1343don't use the function cbreak(), curses will not read your input 1344characters contiguously but will begin read them only after a new 1345line or an EOF is encountered. In order to avoid this, the cbreak() 1346function must used so that characters are immediately available to 1347your program. Another widely used function is noecho(). As the name 1348suggests, when this function is set (used), the characters that are 1349keyed in by the user will not show up on the screen. The two 1350functions cbreak() and noecho() are typical examples of key 1351management. Functions of this genre are explained in the <a href= 1352"#KEYS">key management section</a> .</p> 1353</div> 1354<div class="SECT2"> 1355<hr> 1356<h3 class="SECT2"><a name="SCANWCLASS" id="SCANWCLASS">7.2. scanw() 1357class of functions</a></h3> 1358<p>These functions are similar to <var class= 1359"LITERAL">scanf()</var> with the added capability of getting the 1360input from any location on the screen.</p> 1361<div class="SECT3"> 1362<hr> 1363<h4 class="SECT3"><a name="SCANWMVSCANW" id="SCANWMVSCANW">7.2.1. 1364scanw() and mvscanw</a></h4> 1365<p>The usage of these functions is similar to that of <var class= 1366"LITERAL">sscanf()</var>, where the line to be scanned is provided 1367by <var class="LITERAL">wgetstr()</var> function. That is, these 1368functions call to <var class="LITERAL">wgetstr()</var> 1369function(explained below) and uses the resulting line for a 1370scan.</p> 1371</div> 1372<div class="SECT3"> 1373<hr> 1374<h4 class="SECT3"><a name="WSCANWMVWSCANW" id= 1375"WSCANWMVWSCANW">7.2.2. wscanw() and mvwscanw()</a></h4> 1376<p>These are similar to above two functions except that they read 1377from a window, which is supplied as one of the arguments to these 1378functions.</p> 1379</div> 1380<div class="SECT3"> 1381<hr> 1382<h4 class="SECT3"><a name="VWSCANW" id="VWSCANW">7.2.3. 1383vwscanw()</a></h4> 1384<p>This function is similar to <var class="LITERAL">vscanf()</var>. 1385This can be used when a variable number of arguments are to be 1386scanned.</p> 1387</div> 1388</div> 1389<div class="SECT2"> 1390<hr> 1391<h3 class="SECT2"><a name="GETSTRCLASS" id="GETSTRCLASS">7.3. 1392getstr() class of functions</a></h3> 1393<p>These functions are used to get strings from the terminal. In 1394essence, this function performs the same task as would be achieved 1395by a series of calls to <var class="LITERAL">getch()</var> until a 1396newline, carriage return, or end-of-file is received. The resulting 1397string of characters are pointed to by <var class= 1398"LITERAL">str</var>, which is a character pointer provided by the 1399user.</p> 1400</div> 1401<div class="SECT2"> 1402<hr> 1403<h3 class="SECT2"><a name="GETSTREX" id="GETSTREX">7.4. Some 1404examples</a></h3> 1405<div class="EXAMPLE"><a name="BSCEX" id="BSCEX"></a> 1406<p><b>Example 4. A Simple scanw example</b></p> 1407<table border="0" bgcolor="#E0E0E0" width="100%"> 1408<tr> 1409<td> 1410<pre class="PROGRAMLISTING"> 1411<font color="#000000"><span class= 1412"INLINEMEDIAOBJECT">#include <ncurses.h> /* ncurses.h includes stdio.h */ 1413#include <string.h> | 2330}</SPAN 2331></PRE 2332></DIV 2333><P 2334>Above program demonstrates how easy it is to use <TT 2335CLASS="LITERAL" 2336>printw</TT 2337>. You just feed the coordinates and the message to be appeared 2338on the screen, then it does what you want.</P 2339><P 2340>The above program introduces us to a new function 2341<TT 2342CLASS="LITERAL" 2343>getmaxyx()</TT 2344>, a macro defined in 2345<TT 2346CLASS="LITERAL" 2347>ncurses.h</TT 2348>. It gives the number of columns and 2349the number of rows in a given window. 2350<TT 2351CLASS="LITERAL" 2352>getmaxyx()</TT 2353> does this by updating the variables 2354given to it. Since <TT 2355CLASS="LITERAL" 2356>getmaxyx()</TT 2357> is not a function 2358we don't pass pointers to it, we just give two integer variables. </P 2359></DIV 2360></DIV 2361><DIV 2362CLASS="SECT2" 2363><HR><H3 2364CLASS="SECT2" 2365><A 2366NAME="ADDSTRCLASS" 2367>6.4. addstr() class of functions</A 2368></H3 2369><P 2370><TT 2371CLASS="LITERAL" 2372>addstr()</TT 2373> is used to put a character string into 2374a given window. This function is similar to calling 2375<TT 2376CLASS="LITERAL" 2377>addch()</TT 2378> once for each character in a given 2379string. This is true for all output functions. There are other functions from 2380this family such as <TT 2381CLASS="LITERAL" 2382>mvaddstr(),mvwaddstr()</TT 2383> and 2384<TT 2385CLASS="LITERAL" 2386>waddstr()</TT 2387>, which obey the naming convention of 2388curses.(e.g. mvaddstr() is similar to the respective calls move() and then 2389addstr().) Another function of this family is addnstr(), which takes an integer 2390parameter(say n) additionally. This function puts at most n characters into the 2391screen. If n is negative, then the entire string will be added. </P 2392></DIV 2393><DIV 2394CLASS="SECT2" 2395><HR><H3 2396CLASS="SECT2" 2397><A 2398NAME="ACAUTION" 2399>6.5. A word of caution</A 2400></H3 2401><P 2402>All these functions take y co-ordinate first and then x in their arguments. 2403A common mistake by beginners is to pass x,y in that order. If you are 2404doing too many manipulations of (y,x) co-ordinates, think of dividing the 2405screen into windows and manipulate each one separately. Windows are explained 2406in the <A 2407HREF="#WINDOWS" 2408> windows </A 2409> section.</P 2410></DIV 2411></DIV 2412><DIV 2413CLASS="SECT1" 2414><HR><H2 2415CLASS="SECT1" 2416><A 2417NAME="SCANW" 2418>7. Input functions</A 2419></H2 2420><P 2421>Well, printing without taking input, is boring. Let's see functions which 2422allow us to get input from user. These functions also can be divided into 2423three categories.</P 2424><P 2425></P 2426><OL 2427TYPE="1" 2428><LI 2429><P 2430>getch() class: Get a character</P 2431></LI 2432><LI 2433><P 2434>scanw() class: Get formatted input</P 2435></LI 2436><LI 2437><P 2438>getstr() class: Get strings</P 2439></LI 2440></OL 2441><DIV 2442CLASS="SECT2" 2443><HR><H3 2444CLASS="SECT2" 2445><A 2446NAME="GETCHCLASS" 2447>7.1. getch() class of functions</A 2448></H3 2449><P 2450>These functions read a single character from the terminal. But there are several 2451subtle facts to consider. For example if you don't use the function cbreak(), 2452curses will not read your input characters contiguously but will begin read them 2453only after a new line or an EOF is encountered. In order to avoid this, the 2454cbreak() function must used so that characters are immediately available to your 2455program. Another widely used function is noecho(). As the name suggests, when 2456this function is set (used), the characters that are keyed in by the user will 2457not show up on the screen. The two functions cbreak() and noecho() are typical 2458examples of key management. Functions of this genre are explained in the 2459<A 2460HREF="#KEYS" 2461>key management section </A 2462>.</P 2463></DIV 2464><DIV 2465CLASS="SECT2" 2466><HR><H3 2467CLASS="SECT2" 2468><A 2469NAME="SCANWCLASS" 2470>7.2. scanw() class of functions</A 2471></H3 2472><P 2473>These functions are similar to <TT 2474CLASS="LITERAL" 2475>scanf()</TT 2476> with the 2477added capability of getting the input from any location on the screen.</P 2478><DIV 2479CLASS="SECT3" 2480><HR><H4 2481CLASS="SECT3" 2482><A 2483NAME="SCANWMVSCANW" 2484>7.2.1. scanw() and mvscanw</A 2485></H4 2486><P 2487>The usage of these functions is similar to that of 2488<TT 2489CLASS="LITERAL" 2490>sscanf()</TT 2491>, where the line to be scanned is 2492provided by <TT 2493CLASS="LITERAL" 2494>wgetstr()</TT 2495> function. That is, these 2496functions call to <TT 2497CLASS="LITERAL" 2498>wgetstr()</TT 2499> function(explained 2500below) and uses the resulting line for a scan. </P 2501></DIV 2502><DIV 2503CLASS="SECT3" 2504><HR><H4 2505CLASS="SECT3" 2506><A 2507NAME="WSCANWMVWSCANW" 2508>7.2.2. wscanw() and mvwscanw()</A 2509></H4 2510><P 2511>These are similar to above two functions except that they read from a window, 2512which is supplied as one of the arguments to these functions. </P 2513></DIV 2514><DIV 2515CLASS="SECT3" 2516><HR><H4 2517CLASS="SECT3" 2518><A 2519NAME="VWSCANW" 2520>7.2.3. vwscanw()</A 2521></H4 2522><P 2523>This function is similar to <TT 2524CLASS="LITERAL" 2525>vscanf()</TT 2526>. This can 2527be used when a variable number of arguments are to be scanned.</P 2528></DIV 2529></DIV 2530><DIV 2531CLASS="SECT2" 2532><HR><H3 2533CLASS="SECT2" 2534><A 2535NAME="GETSTRCLASS" 2536>7.3. getstr() class of functions</A 2537></H3 2538><P 2539>These functions are used to get strings from the terminal. In essence, this 2540function performs the same task as would be achieved by a series of calls to 2541<TT 2542CLASS="LITERAL" 2543>getch()</TT 2544> until a newline, carriage return, or 2545end-of-file is received. The resulting string of characters are pointed to by 2546<TT 2547CLASS="LITERAL" 2548>str</TT 2549>, which is a character pointer provided by 2550the user.</P 2551></DIV 2552><DIV 2553CLASS="SECT2" 2554><HR><H3 2555CLASS="SECT2" 2556><A 2557NAME="GETSTREX" 2558>7.4. Some examples</A 2559></H3 2560><DIV 2561CLASS="EXAMPLE" 2562><A 2563NAME="BSCEX" 2564></A 2565><P 2566><B 2567>Example 4. A Simple scanw example </B 2568></P 2569><PRE 2570CLASS="PROGRAMLISTING" 2571><SPAN 2572CLASS="INLINEMEDIAOBJECT" 2573>#include <ncurses.h> /* ncurses.h includes stdio.h */ 2574#include <string.h> |
1414 1415int main() 1416{ | 2575 2576int main() 2577{ |
1417 char mesg[]="Enter a string: "; /* message to be appeared on the screen */ | 2578 char mesg[]="Enter a string: "; /* message to be appeared on the screen */ |
1418 char str[80]; | 2579 char str[80]; |
1419 int row,col; /* to store the number of rows and * 1420 * the number of colums of the screen */ 1421 initscr(); /* start the curses mode */ 1422 getmaxyx(stdscr,row,col); /* get the number of rows and columns */ | 2580 int row,col; /* to store the number of rows and * 2581 * the number of colums of the screen */ 2582 initscr(); /* start the curses mode */ 2583 getmaxyx(stdscr,row,col); /* get the number of rows and columns */ |
1423 mvprintw(row/2,(col-strlen(mesg))/2,"%s",mesg); | 2584 mvprintw(row/2,(col-strlen(mesg))/2,"%s",mesg); |
1424 /* print the message at the center of the screen */ | 2585 /* print the message at the center of the screen */ |
1425 getstr(str); 1426 mvprintw(LINES - 2, 0, "You Entered: %s", str); 1427 getch(); 1428 endwin(); 1429 1430 return 0; | 2586 getstr(str); 2587 mvprintw(LINES - 2, 0, "You Entered: %s", str); 2588 getch(); 2589 endwin(); 2590 2591 return 0; |
1431}</span></font> 1432</pre></td> 1433</tr> 1434</table> 1435</div> 1436</div> 1437</div> 1438<div class="SECT1"> 1439<hr> 1440<h2 class="SECT1"><a name="ATTRIB" id="ATTRIB">8. 1441Attributes</a></h2> 1442<p>We have seen an example of how attributes can be used to print 1443characters with some special effects. Attributes, when set 1444prudently, can present information in an easy, understandable 1445manner. The following program takes a C file as input and prints 1446the file with comments in bold. Scan through the code.</p> 1447<div class="EXAMPLE"><a name="BSIAT" id="BSIAT"></a> 1448<p><b>Example 5. A Simple Attributes example</b></p> 1449<table border="0" bgcolor="#E0E0E0" width="100%"> 1450<tr> 1451<td> 1452<pre class="PROGRAMLISTING"> 1453<font color="#000000"><span class= 1454"INLINEMEDIAOBJECT">/* pager functionality by Joseph Spainhour" <spainhou@bellsouth.net> */ 1455#include <ncurses.h> 1456#include <stdlib.h> | 2592}</SPAN 2593></PRE 2594></DIV 2595></DIV 2596></DIV 2597><DIV 2598CLASS="SECT1" 2599><HR><H2 2600CLASS="SECT1" 2601><A 2602NAME="ATTRIB" 2603>8. Attributes</A 2604></H2 2605><P 2606>We have seen an example of how attributes can be used to print characters with 2607some special effects. Attributes, when set prudently, can present information in 2608an easy, understandable manner. The following program takes a C file as input 2609and prints the file with comments in bold. Scan through the code. </P 2610><DIV 2611CLASS="EXAMPLE" 2612><A 2613NAME="BSIAT" 2614></A 2615><P 2616><B 2617>Example 5. A Simple Attributes example </B 2618></P 2619><PRE 2620CLASS="PROGRAMLISTING" 2621><SPAN 2622CLASS="INLINEMEDIAOBJECT" 2623>/* pager functionality by Joseph Spainhour" <spainhou@bellsouth.net> */ 2624#include <ncurses.h> 2625#include <stdlib.h> |
1457 1458int main(int argc, char *argv[]) 1459{ 1460 int ch, prev, row, col; 1461 prev = EOF; 1462 FILE *fp; 1463 int y, x; 1464 1465 if(argc != 2) 1466 { | 2626 2627int main(int argc, char *argv[]) 2628{ 2629 int ch, prev, row, col; 2630 prev = EOF; 2631 FILE *fp; 2632 int y, x; 2633 2634 if(argc != 2) 2635 { |
1467 printf("Usage: %s <a c file name>\n", argv[0]); | 2636 printf("Usage: %s <a c file name>\n", argv[0]); |
1468 exit(1); 1469 } 1470 fp = fopen(argv[1], "r"); 1471 if(fp == NULL) 1472 { 1473 perror("Cannot open input file"); 1474 exit(1); 1475 } | 2637 exit(1); 2638 } 2639 fp = fopen(argv[1], "r"); 2640 if(fp == NULL) 2641 { 2642 perror("Cannot open input file"); 2643 exit(1); 2644 } |
1476 initscr(); /* Start curses mode */ 1477 getmaxyx(stdscr, row, col); /* find the boundaries of the screeen */ 1478 while((ch = fgetc(fp)) != EOF) /* read the file till we reach the end */ | 2645 initscr(); /* Start curses mode */ 2646 getmaxyx(stdscr, row, col); /* find the boundaries of the screeen */ 2647 while((ch = fgetc(fp)) != EOF) /* read the file till we reach the end */ |
1479 { | 2648 { |
1480 getyx(stdscr, y, x); /* get the current curser position */ 1481 if(y == (row - 1)) /* are we are at the end of the screen */ | 2649 getyx(stdscr, y, x); /* get the current curser position */ 2650 if(y == (row - 1)) /* are we are at the end of the screen */ |
1482 { | 2651 { |
1483 printw("<-Press Any Key->"); /* tell the user to press a key */ | 2652 printw("<-Press Any Key->"); /* tell the user to press a key */ |
1484 getch(); | 2653 getch(); |
1485 clear(); /* clear the screen */ 1486 move(0, 0); /* start at the beginning of the screen */ | 2654 clear(); /* clear the screen */ 2655 move(0, 0); /* start at the beginning of the screen */ |
1487 } | 2656 } |
1488 if(prev == '/' && ch == '*') /* If it is / and * then only 1489 * switch bold on */ | 2657 if(prev == '/' && ch == '*') /* If it is / and * then only 2658 * switch bold on */ |
1490 { | 2659 { |
1491 attron(A_BOLD); /* cut bold on */ 1492 getyx(stdscr, y, x); /* get the current curser position */ 1493 move(y, x - 1); /* back up one space */ 1494 printw("%c%c", '/', ch); /* The actual printing is done here */ | 2660 attron(A_BOLD); /* cut bold on */ 2661 getyx(stdscr, y, x); /* get the current curser position */ 2662 move(y, x - 1); /* back up one space */ 2663 printw("%c%c", '/', ch); /* The actual printing is done here */ |
1495 } 1496 else 1497 printw("%c", ch); 1498 refresh(); | 2664 } 2665 else 2666 printw("%c", ch); 2667 refresh(); |
1499 if(prev == '*' && ch == '/') 1500 attroff(A_BOLD); /* Switch it off once we got * 1501 * and then / */ | 2668 if(prev == '*' && ch == '/') 2669 attroff(A_BOLD); /* Switch it off once we got * 2670 * and then / */ |
1502 prev = ch; 1503 } | 2671 prev = ch; 2672 } |
1504 endwin(); /* End curses mode */ | 2673 endwin(); /* End curses mode */ |
1505 fclose(fp); 1506 return 0; | 2674 fclose(fp); 2675 return 0; |
1507}</span></font> 1508</pre></td> 1509</tr> 1510</table> 1511</div> 1512<p>Don't worry about all those initialization and other crap. 1513Concentrate on the while loop. It reads each character in the file 1514and searches for the pattern /*. Once it spots the pattern, it 1515switches the BOLD attribute on with <var class= 1516"LITERAL">attron()</var> . When we get the pattern */ it is 1517switched off by <var class="LITERAL">attroff()</var> .</p> 1518<p>The above program also introduces us to two useful functions 1519<var class="LITERAL">getyx()</var> and <var class= 1520"LITERAL">move()</var>. The first function gets the co-ordinates of 1521the present cursor into the variables y, x. Since getyx() is a | 2676}</SPAN 2677></PRE 2678></DIV 2679><P 2680> 2681Don't worry about all those initialization and other crap. Concentrate on 2682the while loop. It reads each character in the file and searches for the 2683pattern /*. Once it spots the pattern, it switches the BOLD attribute on with 2684<TT 2685CLASS="LITERAL" 2686> attron()</TT 2687> . When we get the pattern */ it is 2688switched off by <TT 2689CLASS="LITERAL" 2690> attroff()</TT 2691> .</P 2692><P 2693> 2694The above program also introduces us to two useful functions 2695<TT 2696CLASS="LITERAL" 2697>getyx() </TT 2698> and 2699<TT 2700CLASS="LITERAL" 2701>move()</TT 2702>. The first function gets the 2703co-ordinates of the present cursor into the variables y, x. Since getyx() is a |
1522macro we don't have to pass pointers to variables. The function | 2704macro we don't have to pass pointers to variables. The function |
1523<var class="LITERAL">move()</var> moves the cursor to the 1524co-ordinates given to it.</p> 1525<p>The above program is really a simple one which doesn't do much. 1526On these lines one could write a more useful program which reads a 1527C file, parses it and prints it in different colors. One could even 1528extend it to other languages as well.</p> 1529<div class="SECT2"> 1530<hr> 1531<h3 class="SECT2"><a name="ATTRIBDETAILS" id="ATTRIBDETAILS">8.1. 1532The details</a></h3> 1533<p>Let's get into more details of attributes. The functions 1534<var class="LITERAL">attron(), attroff(), attrset()</var> , and 1535their sister functions <var class="LITERAL">attr_get()</var> etc.. 1536can be used to switch attributes on/off , get attributes and 1537produce a colorful display.</p> 1538<p>The functions attron and attroff take a bit-mask of attributes 1539and switch them on or off, respectively. The following video 1540attributes, which are defined in <curses.h> can be passed to 1541these functions.</p> 1542<table border="0" bgcolor="#E0E0E0" width="100%"> 1543<tr> 1544<td> 1545<pre class="PROGRAMLISTING"> 1546<font color="#000000"> | 2705<TT 2706CLASS="LITERAL" 2707>move()</TT 2708> moves the cursor to the co-ordinates 2709given to it. </P 2710><P 2711> 2712The above program is really a simple one which doesn't do much. On these lines 2713one could write a more useful program which reads a C file, parses it and prints 2714it in different colors. One could even extend it to other languages as well.</P 2715><DIV 2716CLASS="SECT2" 2717><HR><H3 2718CLASS="SECT2" 2719><A 2720NAME="ATTRIBDETAILS" 2721>8.1. The details</A 2722></H3 2723><P 2724>Let's get into more details of attributes. The functions <TT 2725CLASS="LITERAL" 2726>attron(), attroff(), attrset() </TT 2727>, and their sister functions 2728<TT 2729CLASS="LITERAL" 2730> attr_get()</TT 2731> etc.. can be used to switch 2732attributes on/off , get attributes and produce a colorful display.</P 2733><P 2734>The functions attron and attroff take a bit-mask of attributes and switch them 2735on or off, respectively. The following video attributes, which are defined in 2736<curses.h> can be passed to these functions. </P 2737><PRE 2738CLASS="PROGRAMLISTING" 2739> |
1547 A_NORMAL Normal display (no highlight) 1548 A_STANDOUT Best highlighting mode of the terminal. 1549 A_UNDERLINE Underlining 1550 A_REVERSE Reverse video 1551 A_BLINK Blinking 1552 A_DIM Half bright 1553 A_BOLD Extra bright or bold 1554 A_PROTECT Protected mode 1555 A_INVIS Invisible or blank mode 1556 A_ALTCHARSET Alternate character set 1557 A_CHARTEXT Bit-mask to extract a character 1558 COLOR_PAIR(n) Color-pair number n | 2740 A_NORMAL Normal display (no highlight) 2741 A_STANDOUT Best highlighting mode of the terminal. 2742 A_UNDERLINE Underlining 2743 A_REVERSE Reverse video 2744 A_BLINK Blinking 2745 A_DIM Half bright 2746 A_BOLD Extra bright or bold 2747 A_PROTECT Protected mode 2748 A_INVIS Invisible or blank mode 2749 A_ALTCHARSET Alternate character set 2750 A_CHARTEXT Bit-mask to extract a character 2751 COLOR_PAIR(n) Color-pair number n |
1559 </font> 1560</pre></td> 1561</tr> 1562</table> 1563<p>The last one is the most colorful one :-) Colors are explained 1564in the <a href="#color" target="_top">next sections</a>.</p> 1565<p>We can OR(|) any number of above attributes to get a combined 1566effect. If you wanted reverse video with blinking characters you 1567can use</p> 1568<table border="0" bgcolor="#E0E0E0" width="100%"> 1569<tr> 1570<td> 1571<pre class="PROGRAMLISTING"> 1572<font color="#000000"> attron(A_REVERSE | A_BLINK);</font> 1573</pre></td> 1574</tr> 1575</table> 1576</div> 1577<div class="SECT2"> 1578<hr> 1579<h3 class="SECT2"><a name="ATTRONVSATTRSET" id= 1580"ATTRONVSATTRSET">8.2. attron() vs attrset()</a></h3> 1581<p>Then what is the difference between attron() and attrset()? 1582attrset sets the attributes of window whereas attron just switches 1583on the attribute given to it. So attrset() fully overrides whatever 1584attributes the window previously had and sets it to the new 1585attribute(s). Similarly attroff() just switches off the 1586attribute(s) given to it as an argument. This gives us the 1587flexibility of managing attributes easily.But if you use them 1588carelessly you may loose track of what attributes the window has 1589and garble the display. This is especially true while managing 1590menus with colors and highlighting. So decide on a consistent 1591policy and stick to it. You can always use <var class= 1592"LITERAL">standend()</var> which is equivalent to <var class= 1593"LITERAL">attrset(A_NORMAL)</var> which turns off all attributes 1594and brings you to normal mode.</p> 1595</div> 1596<div class="SECT2"> 1597<hr> 1598<h3 class="SECT2"><a name="ATTR_GET" id="ATTR_GET">8.3. 1599attr_get()</a></h3> 1600<p>The function attr_get() gets the current attributes and color 1601pair of the window. Though we might not use this as often as the 1602above functions, this is useful in scanning areas of screen. Say we 1603wanted to do some complex update on screen and we are not sure what 1604attribute each character is associated with. Then this function can 1605be used with either attrset or attron to produce the desired 1606effect.</p> 1607</div> 1608<div class="SECT2"> 1609<hr> 1610<h3 class="SECT2"><a name="ATTR_FUNCS" id="ATTR_FUNCS">8.4. attr_ 1611functions</a></h3> 1612<p>There are series of functions like attr_set(), attr_on etc.. 1613These are similar to above functions except that they take 1614parameters of type <var class="LITERAL">attr_t</var>.</p> 1615</div> 1616<div class="SECT2"> 1617<hr> 1618<h3 class="SECT2"><a name="WATTRFUNCS" id="WATTRFUNCS">8.5. wattr 1619functions</a></h3> 1620<p>For each of the above functions we have a corresponding function 1621with 'w' which operates on a particular window. The above functions 1622operate on stdscr.</p> 1623</div> 1624<div class="SECT2"> 1625<hr> 1626<h3 class="SECT2"><a name="CHGAT" id="CHGAT">8.6. chgat() 1627functions</a></h3> 1628<p>The function chgat() is listed in the end of the man page 1629curs_attr. It actually is a useful one. This function can be used 1630to set attributes for a group of characters without moving. I mean 1631it !!! without moving the cursor :-) It changes the attributes of a 1632given number of characters starting at the current cursor 1633location.</p> 1634<p>We can give -1 as the character count to update till end of 1635line. If you want to change attributes of characters from current 1636position to end of line, just use this.</p> 1637<table border="0" bgcolor="#E0E0E0" width="100%"> 1638<tr> 1639<td> 1640<pre class="PROGRAMLISTING"> 1641<font color="#000000"> chgat(-1, A_REVERSE, 0, NULL);</font> 1642</pre></td> 1643</tr> 1644</table> 1645<p>This function is useful when changing attributes for characters 1646that are already on the screen. Move to the character from which 1647you want to change and change the attribute.</p> 1648<p>Other functions wchgat(), mvchgat(), wchgat() behave similarly 1649except that the w functions operate on the particular window. The 1650mv functions first move the cursor then perform the work given to 1651them. Actually chgat is a macro which is replaced by a wchgat() 1652with stdscr as the window. Most of the "w-less" functions are 1653macros.</p> 1654<div class="EXAMPLE"><a name="BWICH" id="BWICH"></a> 1655<p><b>Example 6. Chgat() Usage example</b></p> 1656<table border="0" bgcolor="#E0E0E0" width="100%"> 1657<tr> 1658<td> 1659<pre class="PROGRAMLISTING"> 1660<font color="#000000"><span class= 1661"INLINEMEDIAOBJECT">#include <ncurses.h> | 2752 </PRE 2753><P 2754> 2755The last one is the most colorful one :-) Colors are explained in the 2756<A 2757HREF="#color" 2758TARGET="_top" 2759>next sections</A 2760>.</P 2761><P 2762>We can OR(|) any number of above attributes to get a combined effect. If you 2763wanted reverse video with blinking characters you can use</P 2764><PRE 2765CLASS="PROGRAMLISTING" 2766> attron(A_REVERSE | A_BLINK);</PRE 2767></DIV 2768><DIV 2769CLASS="SECT2" 2770><HR><H3 2771CLASS="SECT2" 2772><A 2773NAME="ATTRONVSATTRSET" 2774>8.2. attron() vs attrset()</A 2775></H3 2776><P 2777>Then what is the difference between attron() and attrset()? attrset sets the 2778attributes of window whereas attron just switches on the attribute given to it. 2779So attrset() fully overrides whatever attributes the window previously had and 2780sets it to the new attribute(s). Similarly attroff() just switches off the 2781attribute(s) given to it as an argument. This gives us the flexibility of 2782managing attributes easily.But if you use them carelessly you may loose track of 2783what attributes the window has and garble the display. This is especially true 2784while managing menus with colors and highlighting. So decide on a consistent 2785policy and stick to it. You can always use <TT 2786CLASS="LITERAL" 2787> standend()</TT 2788> which is equivalent to <TT 2789CLASS="LITERAL" 2790> attrset(A_NORMAL)</TT 2791> which turns off all attributes and brings you to normal mode.</P 2792></DIV 2793><DIV 2794CLASS="SECT2" 2795><HR><H3 2796CLASS="SECT2" 2797><A 2798NAME="ATTR_GET" 2799>8.3. attr_get()</A 2800></H3 2801><P 2802> The function attr_get() gets the current attributes and color pair of the 2803window. Though we might not use this as often as the above functions, this is 2804useful in scanning areas of screen. Say we wanted to do some complex update on 2805screen and we are not sure what attribute each character is associated with. 2806Then this function can be used with either attrset or attron to produce the 2807desired effect. </P 2808></DIV 2809><DIV 2810CLASS="SECT2" 2811><HR><H3 2812CLASS="SECT2" 2813><A 2814NAME="ATTR_FUNCS" 2815>8.4. attr_ functions</A 2816></H3 2817><P 2818>There are series of functions like attr_set(), attr_on etc.. These are similar 2819to above functions except that they take parameters of type 2820<TT 2821CLASS="LITERAL" 2822>attr_t</TT 2823>.</P 2824></DIV 2825><DIV 2826CLASS="SECT2" 2827><HR><H3 2828CLASS="SECT2" 2829><A 2830NAME="WATTRFUNCS" 2831>8.5. wattr functions</A 2832></H3 2833><P 2834>For each of the above functions we have a corresponding function with 'w' which 2835operates on a particular window. The above functions operate on stdscr. </P 2836></DIV 2837><DIV 2838CLASS="SECT2" 2839><HR><H3 2840CLASS="SECT2" 2841><A 2842NAME="CHGAT" 2843>8.6. chgat() functions</A 2844></H3 2845><P 2846>The function chgat() is listed in the end of the man page curs_attr. It actually 2847is a useful one. This function can be used to set attributes for a group of 2848characters without moving. I mean it !!! without moving the cursor :-) It 2849changes the attributes of a given number of characters starting at the current 2850cursor location.</P 2851><P 2852>We can give -1 as the character count to update till end of line. If you want to 2853change attributes of characters from current position to end of line, just use 2854this.</P 2855><PRE 2856CLASS="PROGRAMLISTING" 2857> chgat(-1, A_REVERSE, 0, NULL);</PRE 2858><P 2859> 2860This function is useful when changing attributes for characters that are 2861already on the screen. Move to the character from which you want to change and 2862change the attribute. </P 2863><P 2864>Other functions wchgat(), mvchgat(), wchgat() behave similarly except that the w 2865functions operate on the particular window. The mv functions first move the 2866cursor then perform the work given to them. Actually chgat is a macro which is 2867replaced by a wchgat() with stdscr as the window. Most of the "w-less" functions 2868are macros.</P 2869><DIV 2870CLASS="EXAMPLE" 2871><A 2872NAME="BWICH" 2873></A 2874><P 2875><B 2876>Example 6. Chgat() Usage example </B 2877></P 2878><PRE 2879CLASS="PROGRAMLISTING" 2880><SPAN 2881CLASS="INLINEMEDIAOBJECT" 2882>#include <ncurses.h> |
1662 1663int main(int argc, char *argv[]) | 2883 2884int main(int argc, char *argv[]) |
1664{ initscr(); /* Start curses mode */ 1665 start_color(); /* Start color functionality */ 1666 1667 init_pair(1, COLOR_CYAN, COLOR_BLACK); 1668 printw("A Big string which i didn't care to type fully "); 1669 mvchgat(0, 0, -1, A_BLINK, 1, NULL); 1670 /* 1671 * First two parameters specify the position at which to start 1672 * Third parameter number of characters to update. -1 means till 1673 * end of line 1674 * Forth parameter is the normal attribute you wanted to give 1675 * to the charcter 1676 * Fifth is the color index. It is the index given during init_pair() 1677 * use 0 if you didn't want color 1678 * Sixth one is always NULL 1679 */ 1680 refresh(); 1681 getch(); 1682 endwin(); /* End curses mode */ 1683 return 0; 1684}</span></font> 1685</pre></td> 1686</tr> 1687</table> 1688</div> 1689<p>This example also introduces us to the color world of curses. 1690Colors will be explained in detail later. Use 0 for no color.</p> 1691</div> 1692</div> 1693<div class="SECT1"> 1694<hr> 1695<h2 class="SECT1"><a name="WINDOWS" id="WINDOWS">9. 1696Windows</a></h2> 1697<p>Windows form the most important concept in curses. You have seen 1698the standard window stdscr above where all the functions implicitly 1699operated on this window. Now to make design even a simplest GUI, 1700you need to resort to windows. The main reason you may want to use 1701windows is to manipulate parts of the screen separately, for better 1702efficiency, by updating only the windows that need to be changed 1703and for a better design. I would say the last reason is the most 1704important in going for windows. You should always strive for a 1705better and easy-to-manage design in your programs. If you are 1706writing big, complex GUIs this is of pivotal importance before you 1707start doing anything.</p> 1708<div class="SECT2"> 1709<hr> 1710<h3 class="SECT2"><a name="WINDOWBASICS" id="WINDOWBASICS">9.1. The 1711basics</a></h3> 1712<p>A Window can be created by calling the function <var class= 1713"LITERAL">newwin()</var>. It doesn't create any thing on the screen 1714actually. It allocates memory for a structure to manipulate the 1715window and updates the structure with data regarding the window 1716like it's size, beginy, beginx etc.. Hence in curses, a window is 1717just an abstraction of an imaginary window, which can be 1718manipulated independent of other parts of screen. The function 1719newwin() returns a pointer to structure WINDOW, which can be passed 1720to window related functions like wprintw() etc.. Finally the window 1721can be destroyed with delwin(). It will deallocate the memory 1722associated with the window structure.</p> 1723</div> 1724<div class="SECT2"> 1725<hr> 1726<h3 class="SECT2"><a name="LETBEWINDOW" id="LETBEWINDOW">9.2. Let 1727there be a Window !!!</a></h3> 1728<p>What fun is it, if a window is created and we can't see it. So 1729the fun part begins by displaying the window. The function 1730<var class="LITERAL">box()</var> can be used to draw a border 1731around the window. Let's explore these functions in more detail in 1732this example.</p> 1733<div class="EXAMPLE"><a name="BWIBO" id="BWIBO"></a> 1734<p><b>Example 7. Window Border example</b></p> 1735<table border="0" bgcolor="#E0E0E0" width="100%"> 1736<tr> 1737<td> 1738<pre class="PROGRAMLISTING"> 1739<font color="#000000"><span class= 1740"INLINEMEDIAOBJECT">#include <ncurses.h> | 2885{ initscr(); /* Start curses mode */ 2886 start_color(); /* Start color functionality */ 2887 2888 init_pair(1, COLOR_CYAN, COLOR_BLACK); 2889 printw("A Big string which i didn't care to type fully "); 2890 mvchgat(0, 0, -1, A_BLINK, 1, NULL); 2891 /* 2892 * First two parameters specify the position at which to start 2893 * Third parameter number of characters to update. -1 means till 2894 * end of line 2895 * Forth parameter is the normal attribute you wanted to give 2896 * to the charcter 2897 * Fifth is the color index. It is the index given during init_pair() 2898 * use 0 if you didn't want color 2899 * Sixth one is always NULL 2900 */ 2901 refresh(); 2902 getch(); 2903 endwin(); /* End curses mode */ 2904 return 0; 2905}</SPAN 2906></PRE 2907></DIV 2908><P 2909>This example also introduces us to the color world of curses. Colors will be 2910explained in detail later. Use 0 for no color.</P 2911></DIV 2912></DIV 2913><DIV 2914CLASS="SECT1" 2915><HR><H2 2916CLASS="SECT1" 2917><A 2918NAME="WINDOWS" 2919>9. Windows</A 2920></H2 2921><P 2922>Windows form the most important concept in curses. You have seen the standard 2923window stdscr above where all the functions implicitly operated on this window. 2924Now to make design even a simplest GUI, you need to resort to windows. The main 2925reason you may want to use windows is to manipulate parts of the screen 2926separately, for better efficiency, by updating only the windows that need to be 2927changed and for a better design. I would say the last reason is the most 2928important in going for windows. You should always strive for a better and 2929easy-to-manage design in your programs. If you are writing big, complex GUIs 2930this is of pivotal importance before you start doing anything.</P 2931><DIV 2932CLASS="SECT2" 2933><HR><H3 2934CLASS="SECT2" 2935><A 2936NAME="WINDOWBASICS" 2937>9.1. The basics</A 2938></H3 2939><P 2940>A Window can be created by calling the function 2941<TT 2942CLASS="LITERAL" 2943>newwin()</TT 2944>. It doesn't create any thing on the 2945screen actually. It allocates memory for a structure to manipulate the window 2946and updates the structure with data regarding the window like it's size, beginy, 2947beginx etc.. Hence in curses, a window is just an abstraction of an imaginary 2948window, which can be manipulated independent of other parts of screen. The 2949function newwin() returns a pointer to structure WINDOW, which can be passed to 2950window related functions like wprintw() etc.. Finally the window can be 2951destroyed with delwin(). It will deallocate the memory associated with the 2952window structure.</P 2953></DIV 2954><DIV 2955CLASS="SECT2" 2956><HR><H3 2957CLASS="SECT2" 2958><A 2959NAME="LETBEWINDOW" 2960>9.2. Let there be a Window !!!</A 2961></H3 2962><P 2963>What fun is it, if a window is created and we can't see it. So the fun part 2964begins by displaying the window. The function 2965<TT 2966CLASS="LITERAL" 2967>box()</TT 2968> can be used to draw a border around the 2969window. Let's explore these functions in more detail in this example.</P 2970><DIV 2971CLASS="EXAMPLE" 2972><A 2973NAME="BWIBO" 2974></A 2975><P 2976><B 2977>Example 7. Window Border example </B 2978></P 2979><PRE 2980CLASS="PROGRAMLISTING" 2981><SPAN 2982CLASS="INLINEMEDIAOBJECT" 2983>#include <ncurses.h> |
1741 1742 1743WINDOW *create_newwin(int height, int width, int starty, int startx); 1744void destroy_win(WINDOW *local_win); 1745 1746int main(int argc, char *argv[]) | 2984 2985 2986WINDOW *create_newwin(int height, int width, int starty, int startx); 2987void destroy_win(WINDOW *local_win); 2988 2989int main(int argc, char *argv[]) |
1747{ WINDOW *my_win; 1748 int startx, starty, width, height; 1749 int ch; | 2990{ WINDOW *my_win; 2991 int startx, starty, width, height; 2992 int ch; |
1750 | 2993 |
1751 initscr(); /* Start curses mode */ 1752 cbreak(); /* Line buffering disabled, Pass on 1753 * everty thing to me */ 1754 keypad(stdscr, TRUE); /* I need that nifty F1 */ | 2994 initscr(); /* Start curses mode */ 2995 cbreak(); /* Line buffering disabled, Pass on 2996 * everty thing to me */ 2997 keypad(stdscr, TRUE); /* I need that nifty F1 */ |
1755 | 2998 |
1756 height = 3; 1757 width = 10; 1758 starty = (LINES - height) / 2; /* Calculating for a center placement */ 1759 startx = (COLS - width) / 2; /* of the window */ 1760 printw("Press F1 to exit"); 1761 refresh(); 1762 my_win = create_newwin(height, width, starty, startx); | 2999 height = 3; 3000 width = 10; 3001 starty = (LINES - height) / 2; /* Calculating for a center placement */ 3002 startx = (COLS - width) / 2; /* of the window */ 3003 printw("Press F1 to exit"); 3004 refresh(); 3005 my_win = create_newwin(height, width, starty, startx); |
1763 | 3006 |
1764 while((ch = getch()) != KEY_F(1)) 1765 { switch(ch) 1766 { case KEY_LEFT: 1767 destroy_win(my_win); 1768 my_win = create_newwin(height, width, starty,--startx); 1769 break; 1770 case KEY_RIGHT: 1771 destroy_win(my_win); 1772 my_win = create_newwin(height, width, starty,++startx); 1773 break; 1774 case KEY_UP: 1775 destroy_win(my_win); 1776 my_win = create_newwin(height, width, --starty,startx); 1777 break; 1778 case KEY_DOWN: 1779 destroy_win(my_win); 1780 my_win = create_newwin(height, width, ++starty,startx); 1781 break; 1782 } 1783 } 1784 1785 endwin(); /* End curses mode */ 1786 return 0; | 3007 while((ch = getch()) != KEY_F(1)) 3008 { switch(ch) 3009 { case KEY_LEFT: 3010 destroy_win(my_win); 3011 my_win = create_newwin(height, width, starty,--startx); 3012 break; 3013 case KEY_RIGHT: 3014 destroy_win(my_win); 3015 my_win = create_newwin(height, width, starty,++startx); 3016 break; 3017 case KEY_UP: 3018 destroy_win(my_win); 3019 my_win = create_newwin(height, width, --starty,startx); 3020 break; 3021 case KEY_DOWN: 3022 destroy_win(my_win); 3023 my_win = create_newwin(height, width, ++starty,startx); 3024 break; 3025 } 3026 } 3027 3028 endwin(); /* End curses mode */ 3029 return 0; |
1787} 1788 1789WINDOW *create_newwin(int height, int width, int starty, int startx) | 3030} 3031 3032WINDOW *create_newwin(int height, int width, int starty, int startx) |
1790{ WINDOW *local_win; | 3033{ WINDOW *local_win; |
1791 | 3034 |
1792 local_win = newwin(height, width, starty, startx); 1793 box(local_win, 0 , 0); /* 0, 0 gives default characters 1794 * for the vertical and horizontal 1795 * lines */ 1796 wrefresh(local_win); /* Show that box */ | 3035 local_win = newwin(height, width, starty, startx); 3036 box(local_win, 0 , 0); /* 0, 0 gives default characters 3037 * for the vertical and horizontal 3038 * lines */ 3039 wrefresh(local_win); /* Show that box */ |
1797 | 3040 |
1798 return local_win; | 3041 return local_win; |
1799} 1800 1801void destroy_win(WINDOW *local_win) | 3042} 3043 3044void destroy_win(WINDOW *local_win) |
1802{ 1803 /* box(local_win, ' ', ' '); : This won't produce the desired 1804 * result of erasing the window. It will leave it's four corners 1805 * and so an ugly remnant of window. 1806 */ 1807 wborder(local_win, ' ', ' ', ' ',' ',' ',' ',' ',' '); 1808 /* The parameters taken are 1809 * 1. win: the window on which to operate 1810 * 2. ls: character to be used for the left side of the window 1811 * 3. rs: character to be used for the right side of the window 1812 * 4. ts: character to be used for the top side of the window 1813 * 5. bs: character to be used for the bottom side of the window 1814 * 6. tl: character to be used for the top left corner of the window 1815 * 7. tr: character to be used for the top right corner of the window 1816 * 8. bl: character to be used for the bottom left corner of the window 1817 * 9. br: character to be used for the bottom right corner of the window 1818 */ 1819 wrefresh(local_win); 1820 delwin(local_win); 1821}</span></font> 1822</pre></td> 1823</tr> 1824</table> 1825</div> 1826</div> 1827<div class="SECT2"> 1828<hr> 1829<h3 class="SECT2"><a name="BORDEREXEXPL" id="BORDEREXEXPL">9.3. 1830Explanation</a></h3> 1831<p>Don't scream. I know it's a big example. But I have to explain 1832some important things here :-). This program creates a rectangular 1833window that can be moved with left, right, up, down arrow keys. It 1834repeatedly creates and destroys windows as user press a key. Don't 1835go beyond the screen limits. Checking for those limits is left as 1836an exercise for the reader. Let's dissect it by line by line.</p> 1837<p>The <var class="LITERAL">create_newwin()</var> function creates 1838a window with <var class="LITERAL">newwin()</var> and displays a 1839border around it with box. The function <var class= 1840"LITERAL">destroy_win()</var> first erases the window from screen 1841by painting a border with ' ' character and then calling 1842<var class="LITERAL">delwin()</var> to deallocate memory related to 1843it. Depending on the key the user presses, starty or startx is 1844changed and a new window is created.</p> 1845<p>In the destroy_win, as you can see, I used wborder instead of 1846box. The reason is written in the comments (You missed it. I know. 1847Read the code :-)). wborder draws a border around the window with 1848the characters given to it as the 4 corner points and the 4 lines. 1849To put it clearly, if you have called wborder as below:</p> 1850<table border="0" bgcolor="#E0E0E0" width="100%"> 1851<tr> 1852<td> 1853<pre class="PROGRAMLISTING"> 1854<font color= 1855"#000000"> wborder(win, '|', '|', '-', '-', '+', '+', '+', '+');</font> 1856</pre></td> 1857</tr> 1858</table> 1859<p>it produces some thing like</p> 1860<table border="0" bgcolor="#E0E0E0" width="100%"> 1861<tr> 1862<td> 1863<pre class="PROGRAMLISTING"> 1864<font color="#000000"> +------------+ | 3045{ 3046 /* box(local_win, ' ', ' '); : This won't produce the desired 3047 * result of erasing the window. It will leave it's four corners 3048 * and so an ugly remnant of window. 3049 */ 3050 wborder(local_win, ' ', ' ', ' ',' ',' ',' ',' ',' '); 3051 /* The parameters taken are 3052 * 1. win: the window on which to operate 3053 * 2. ls: character to be used for the left side of the window 3054 * 3. rs: character to be used for the right side of the window 3055 * 4. ts: character to be used for the top side of the window 3056 * 5. bs: character to be used for the bottom side of the window 3057 * 6. tl: character to be used for the top left corner of the window 3058 * 7. tr: character to be used for the top right corner of the window 3059 * 8. bl: character to be used for the bottom left corner of the window 3060 * 9. br: character to be used for the bottom right corner of the window 3061 */ 3062 wrefresh(local_win); 3063 delwin(local_win); 3064}</SPAN 3065></PRE 3066></DIV 3067></DIV 3068><DIV 3069CLASS="SECT2" 3070><HR><H3 3071CLASS="SECT2" 3072><A 3073NAME="BORDEREXEXPL" 3074>9.3. Explanation</A 3075></H3 3076><P 3077>Don't scream. I know it's a big example. But I have to explain some important 3078things here :-). This program creates a rectangular window that can be moved 3079with left, right, up, down arrow keys. It repeatedly creates and destroys 3080windows as user press a key. Don't go beyond the screen limits. Checking for 3081those limits is left as an exercise for the reader. Let's dissect it by line by line.</P 3082><P 3083>The <TT 3084CLASS="LITERAL" 3085>create_newwin()</TT 3086> function creates a window 3087with <TT 3088CLASS="LITERAL" 3089>newwin() </TT 3090> and displays a border around it 3091with box. The function <TT 3092CLASS="LITERAL" 3093> destroy_win()</TT 3094> first 3095erases the window from screen by painting a border with ' ' character and then 3096calling <TT 3097CLASS="LITERAL" 3098>delwin()</TT 3099> to deallocate memory related 3100to it. Depending on the key the user presses, starty or startx is changed and a 3101new window is created.</P 3102><P 3103>In the destroy_win, as you can see, I used wborder instead of box. The reason is 3104written in the comments (You missed it. I know. Read the code :-)). wborder 3105draws a border around the window with the characters given to it as the 4 corner 3106points and the 4 lines. To put it clearly, if you have called wborder as below: 3107<PRE 3108CLASS="PROGRAMLISTING" 3109> wborder(win, '|', '|', '-', '-', '+', '+', '+', '+');</PRE 3110></P 3111><P 3112>it produces some thing like </P 3113><PRE 3114CLASS="PROGRAMLISTING" 3115> +------------+ |
1865 | | 1866 | | 1867 | | 1868 | | 1869 | | 1870 | | | 3116 | | 3117 | | 3118 | | 3119 | | 3120 | | 3121 | | |
1871 +------------+</font> 1872</pre></td> 1873</tr> 1874</table> 1875</div> 1876<div class="SECT2"> 1877<hr> 1878<h3 class="SECT2"><a name="OTHERSTUFF" id="OTHERSTUFF">9.4. The 1879other stuff in the example</a></h3> 1880<p>You can also see in the above examples, that I have used the 1881variables COLS, LINES which are initialized to the screen sizes 1882after initscr(). They can be useful in finding screen dimensions 1883and finding the center co-ordinate of the screen as above. The 1884function <var class="LITERAL">getch()</var> as usual gets the key 1885from keyboard and according to the key it does the corresponding 1886work. This type of switch- case is very common in any GUI based 1887programs.</p> 1888</div> 1889<div class="SECT2"> 1890<hr> 1891<h3 class="SECT2"><a name="OTHERBORDERFUNCS" id= 1892"OTHERBORDERFUNCS">9.5. Other Border functions</a></h3> 1893<p>Above program is grossly inefficient in that with each press of 1894a key, a window is destroyed and another is created. So let's write 1895a more efficient program which uses other border related 1896functions.</p> 1897<p>The following program uses <var class="LITERAL">mvhline()</var> 1898and <var class="LITERAL">mvvline()</var> to achieve similar effect. 1899These two functions are simple. They create a horizontal or 1900vertical line of the specified length at the specified 1901position.</p> 1902<div class="EXAMPLE"><a name="BOTBO" id="BOTBO"></a> 1903<p><b>Example 8. More border functions</b></p> 1904<table border="0" bgcolor="#E0E0E0" width="100%"> 1905<tr> 1906<td> 1907<pre class="PROGRAMLISTING"> 1908<font color="#000000"><span class= 1909"INLINEMEDIAOBJECT">#include <ncurses.h> | 3122 +------------+</PRE 3123></DIV 3124><DIV 3125CLASS="SECT2" 3126><HR><H3 3127CLASS="SECT2" 3128><A 3129NAME="OTHERSTUFF" 3130>9.4. The other stuff in the example</A 3131></H3 3132><P 3133>You can also see in the above examples, that I have used the variables COLS, 3134LINES which are initialized to the screen sizes after initscr(). They can be 3135useful in finding screen dimensions and finding the center co-ordinate of the 3136screen as above. The function <TT 3137CLASS="LITERAL" 3138>getch()</TT 3139> as usual 3140gets the key from keyboard and according to the key it does the corresponding 3141work. This type of switch- case is very common in any GUI based programs.</P 3142></DIV 3143><DIV 3144CLASS="SECT2" 3145><HR><H3 3146CLASS="SECT2" 3147><A 3148NAME="OTHERBORDERFUNCS" 3149>9.5. Other Border functions</A 3150></H3 3151><P 3152>Above program is grossly inefficient in that with each press of a key, a window 3153is destroyed and another is created. So let's write a more efficient program 3154which uses other border related functions.</P 3155><P 3156>The following program uses <TT 3157CLASS="LITERAL" 3158>mvhline()</TT 3159> and 3160<TT 3161CLASS="LITERAL" 3162>mvvline()</TT 3163> to achieve similar effect. These two 3164functions are simple. They create a horizontal or vertical line of the specified 3165length at the specified position.</P 3166><DIV 3167CLASS="EXAMPLE" 3168><A 3169NAME="BOTBO" 3170></A 3171><P 3172><B 3173>Example 8. More border functions</B 3174></P 3175><PRE 3176CLASS="PROGRAMLISTING" 3177><SPAN 3178CLASS="INLINEMEDIAOBJECT" 3179>#include <ncurses.h> |
1910 1911typedef struct _win_border_struct { | 3180 3181typedef struct _win_border_struct { |
1912 chtype ls, rs, ts, bs, 1913 tl, tr, bl, br; | 3182 chtype ls, rs, ts, bs, 3183 tl, tr, bl, br; |
1914}WIN_BORDER; 1915 1916typedef struct _WIN_struct { 1917 | 3184}WIN_BORDER; 3185 3186typedef struct _WIN_struct { 3187 |
1918 int startx, starty; 1919 int height, width; 1920 WIN_BORDER border; | 3188 int startx, starty; 3189 int height, width; 3190 WIN_BORDER border; |
1921}WIN; 1922 1923void init_win_params(WIN *p_win); 1924void print_win_params(WIN *p_win); 1925void create_box(WIN *win, bool flag); 1926 1927int main(int argc, char *argv[]) | 3191}WIN; 3192 3193void init_win_params(WIN *p_win); 3194void print_win_params(WIN *p_win); 3195void create_box(WIN *win, bool flag); 3196 3197int main(int argc, char *argv[]) |
1928{ WIN win; 1929 int ch; | 3198{ WIN win; 3199 int ch; |
1930 | 3200 |
1931 initscr(); /* Start curses mode */ 1932 start_color(); /* Start the color functionality */ 1933 cbreak(); /* Line buffering disabled, Pass on 1934 * everty thing to me */ 1935 keypad(stdscr, TRUE); /* I need that nifty F1 */ 1936 noecho(); 1937 init_pair(1, COLOR_CYAN, COLOR_BLACK); | 3201 initscr(); /* Start curses mode */ 3202 start_color(); /* Start the color functionality */ 3203 cbreak(); /* Line buffering disabled, Pass on 3204 * everty thing to me */ 3205 keypad(stdscr, TRUE); /* I need that nifty F1 */ 3206 noecho(); 3207 init_pair(1, COLOR_CYAN, COLOR_BLACK); |
1938 | 3208 |
1939 /* Initialize the window parameters */ 1940 init_win_params(&win); 1941 print_win_params(&win); | 3209 /* Initialize the window parameters */ 3210 init_win_params(&win); 3211 print_win_params(&win); |
1942 | 3212 |
1943 attron(COLOR_PAIR(1)); 1944 printw("Press F1 to exit"); 1945 refresh(); 1946 attroff(COLOR_PAIR(1)); 1947 1948 create_box(&win, TRUE); 1949 while((ch = getch()) != KEY_F(1)) 1950 { switch(ch) 1951 { case KEY_LEFT: 1952 create_box(&win, FALSE); 1953 --win.startx; 1954 create_box(&win, TRUE); 1955 break; 1956 case KEY_RIGHT: 1957 create_box(&win, FALSE); 1958 ++win.startx; 1959 create_box(&win, TRUE); 1960 break; 1961 case KEY_UP: 1962 create_box(&win, FALSE); 1963 --win.starty; 1964 create_box(&win, TRUE); 1965 break; 1966 case KEY_DOWN: 1967 create_box(&win, FALSE); 1968 ++win.starty; 1969 create_box(&win, TRUE); 1970 break; 1971 } 1972 } 1973 endwin(); /* End curses mode */ 1974 return 0; | 3213 attron(COLOR_PAIR(1)); 3214 printw("Press F1 to exit"); 3215 refresh(); 3216 attroff(COLOR_PAIR(1)); 3217 3218 create_box(&win, TRUE); 3219 while((ch = getch()) != KEY_F(1)) 3220 { switch(ch) 3221 { case KEY_LEFT: 3222 create_box(&win, FALSE); 3223 --win.startx; 3224 create_box(&win, TRUE); 3225 break; 3226 case KEY_RIGHT: 3227 create_box(&win, FALSE); 3228 ++win.startx; 3229 create_box(&win, TRUE); 3230 break; 3231 case KEY_UP: 3232 create_box(&win, FALSE); 3233 --win.starty; 3234 create_box(&win, TRUE); 3235 break; 3236 case KEY_DOWN: 3237 create_box(&win, FALSE); 3238 ++win.starty; 3239 create_box(&win, TRUE); 3240 break; 3241 } 3242 } 3243 endwin(); /* End curses mode */ 3244 return 0; |
1975} 1976void init_win_params(WIN *p_win) 1977{ | 3245} 3246void init_win_params(WIN *p_win) 3247{ |
1978 p_win->height = 3; 1979 p_win->width = 10; 1980 p_win->starty = (LINES - p_win->height)/2; 1981 p_win->startx = (COLS - p_win->width)/2; | 3248 p_win->height = 3; 3249 p_win->width = 10; 3250 p_win->starty = (LINES - p_win->height)/2; 3251 p_win->startx = (COLS - p_win->width)/2; |
1982 | 3252 |
1983 p_win->border.ls = '|'; 1984 p_win->border.rs = '|'; 1985 p_win->border.ts = '-'; 1986 p_win->border.bs = '-'; 1987 p_win->border.tl = '+'; 1988 p_win->border.tr = '+'; 1989 p_win->border.bl = '+'; 1990 p_win->border.br = '+'; | 3253 p_win->border.ls = '|'; 3254 p_win->border.rs = '|'; 3255 p_win->border.ts = '-'; 3256 p_win->border.bs = '-'; 3257 p_win->border.tl = '+'; 3258 p_win->border.tr = '+'; 3259 p_win->border.bl = '+'; 3260 p_win->border.br = '+'; |
1991 1992} 1993void print_win_params(WIN *p_win) 1994{ 1995#ifdef _DEBUG | 3261 3262} 3263void print_win_params(WIN *p_win) 3264{ 3265#ifdef _DEBUG |
1996 mvprintw(25, 0, "%d %d %d %d", p_win->startx, p_win->starty, 1997 p_win->width, p_win->height); 1998 refresh(); | 3266 mvprintw(25, 0, "%d %d %d %d", p_win->startx, p_win->starty, 3267 p_win->width, p_win->height); 3268 refresh(); |
1999#endif 2000} 2001void create_box(WIN *p_win, bool flag) | 3269#endif 3270} 3271void create_box(WIN *p_win, bool flag) |
2002{ int i, j; 2003 int x, y, w, h; | 3272{ int i, j; 3273 int x, y, w, h; |
2004 | 3274 |
2005 x = p_win->startx; 2006 y = p_win->starty; 2007 w = p_win->width; 2008 h = p_win->height; | 3275 x = p_win->startx; 3276 y = p_win->starty; 3277 w = p_win->width; 3278 h = p_win->height; |
2009 | 3279 |
2010 if(flag == TRUE) 2011 { mvaddch(y, x, p_win->border.tl); 2012 mvaddch(y, x + w, p_win->border.tr); 2013 mvaddch(y + h, x, p_win->border.bl); 2014 mvaddch(y + h, x + w, p_win->border.br); 2015 mvhline(y, x + 1, p_win->border.ts, w - 1); 2016 mvhline(y + h, x + 1, p_win->border.bs, w - 1); 2017 mvvline(y + 1, x, p_win->border.ls, h - 1); 2018 mvvline(y + 1, x + w, p_win->border.rs, h - 1); | 3280 if(flag == TRUE) 3281 { mvaddch(y, x, p_win->border.tl); 3282 mvaddch(y, x + w, p_win->border.tr); 3283 mvaddch(y + h, x, p_win->border.bl); 3284 mvaddch(y + h, x + w, p_win->border.br); 3285 mvhline(y, x + 1, p_win->border.ts, w - 1); 3286 mvhline(y + h, x + 1, p_win->border.bs, w - 1); 3287 mvvline(y + 1, x, p_win->border.ls, h - 1); 3288 mvvline(y + 1, x + w, p_win->border.rs, h - 1); |
2019 | 3289 |
2020 } 2021 else 2022 for(j = y; j <= y + h; ++j) 2023 for(i = x; i <= x + w; ++i) 2024 mvaddch(j, i, ' '); 2025 2026 refresh(); | 3290 } 3291 else 3292 for(j = y; j <= y + h; ++j) 3293 for(i = x; i <= x + w; ++i) 3294 mvaddch(j, i, ' '); 3295 3296 refresh(); |
2027 | 3297 |
2028}</span></font> 2029</pre></td> 2030</tr> 2031</table> 2032</div> 2033</div> 2034</div> 2035<div class="SECT1"> 2036<hr> 2037<h2 class="SECT1"><a name="COLOR" id="COLOR">10. Colors</a></h2> 2038<div class="SECT2"> 2039<h3 class="SECT2"><a name="COLORBASICS" id="COLORBASICS">10.1. The 2040basics</a></h3> 2041<p>Life seems dull with no colors. Curses has a nice mechanism to 2042handle colors. Let's get into the thick of the things with a small 2043program.</p> 2044<div class="EXAMPLE"><a name="BSICO" id="BSICO"></a> 2045<p><b>Example 9. A Simple Color example</b></p> 2046<table border="0" bgcolor="#E0E0E0" width="100%"> 2047<tr> 2048<td> 2049<pre class="PROGRAMLISTING"> 2050<font color="#000000"><span class= 2051"INLINEMEDIAOBJECT">#include <ncurses.h> | 3298}</SPAN 3299></PRE 3300></DIV 3301></DIV 3302></DIV 3303><DIV 3304CLASS="SECT1" 3305><HR><H2 3306CLASS="SECT1" 3307><A 3308NAME="COLOR" 3309>10. Colors</A 3310></H2 3311><DIV 3312CLASS="SECT2" 3313><H3 3314CLASS="SECT2" 3315><A 3316NAME="COLORBASICS" 3317>10.1. The basics</A 3318></H3 3319><P 3320>Life seems dull with no colors. Curses has a nice mechanism to handle colors. 3321Let's get into the thick of the things with a small program.</P 3322><DIV 3323CLASS="EXAMPLE" 3324><A 3325NAME="BSICO" 3326></A 3327><P 3328><B 3329>Example 9. A Simple Color example </B 3330></P 3331><PRE 3332CLASS="PROGRAMLISTING" 3333><SPAN 3334CLASS="INLINEMEDIAOBJECT" 3335>#include <ncurses.h> |
2052 2053void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string); 2054int main(int argc, char *argv[]) | 3336 3337void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string); 3338int main(int argc, char *argv[]) |
2055{ initscr(); /* Start curses mode */ 2056 if(has_colors() == FALSE) 2057 { endwin(); 2058 printf("Your terminal does not support color\n"); 2059 exit(1); 2060 } 2061 start_color(); /* Start color */ 2062 init_pair(1, COLOR_RED, COLOR_BLACK); | 3339{ initscr(); /* Start curses mode */ 3340 if(has_colors() == FALSE) 3341 { endwin(); 3342 printf("Your terminal does not support color\n"); 3343 exit(1); 3344 } 3345 start_color(); /* Start color */ 3346 init_pair(1, COLOR_RED, COLOR_BLACK); |
2063 | 3347 |
2064 attron(COLOR_PAIR(1)); 2065 print_in_middle(stdscr, LINES / 2, 0, 0, "Viola !!! In color ..."); 2066 attroff(COLOR_PAIR(1)); 2067 getch(); 2068 endwin(); | 3348 attron(COLOR_PAIR(1)); 3349 print_in_middle(stdscr, LINES / 2, 0, 0, "Viola !!! In color ..."); 3350 attroff(COLOR_PAIR(1)); 3351 getch(); 3352 endwin(); |
2069} 2070void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string) | 3353} 3354void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string) |
2071{ int length, x, y; 2072 float temp; | 3355{ int length, x, y; 3356 float temp; |
2073 | 3357 |
2074 if(win == NULL) 2075 win = stdscr; 2076 getyx(win, y, x); 2077 if(startx != 0) 2078 x = startx; 2079 if(starty != 0) 2080 y = starty; 2081 if(width == 0) 2082 width = 80; | 3358 if(win == NULL) 3359 win = stdscr; 3360 getyx(win, y, x); 3361 if(startx != 0) 3362 x = startx; 3363 if(starty != 0) 3364 y = starty; 3365 if(width == 0) 3366 width = 80; |
2083 | 3367 |
2084 length = strlen(string); 2085 temp = (width - length)/ 2; 2086 x = startx + (int)temp; 2087 mvwprintw(win, y, x, "%s", string); 2088 refresh(); | 3368 length = strlen(string); 3369 temp = (width - length)/ 2; 3370 x = startx + (int)temp; 3371 mvwprintw(win, y, x, "%s", string); 3372 refresh(); |
2089} | 3373} |
2090</span></font> 2091</pre></td> 2092</tr> 2093</table> 2094</div> 2095<p>As you can see, to start using color, you should first call the 2096function <var class="LITERAL">start_color()</var>. After that, you 2097can use color capabilities of your terminals using various 2098functions. To find out whether a terminal has color capabilities or 2099not, you can use <var class="LITERAL">has_colors()</var> function, 2100which returns FALSE if the terminal does not support color.</p> 2101<p>Curses initializes all the colors supported by terminal when 2102start_color() is called. These can be accessed by the define 2103constants like <var class="LITERAL">COLOR_BLACK</var> etc. Now to 2104actually start using colors, you have to define pairs. Colors are 2105always used in pairs. That means you have to use the function 2106<var class="LITERAL">init_pair()</var> to define the foreground and 2107background for the pair number you give. After that that pair 2108number can be used as a normal attribute with <var class= 2109"LITERAL">COLOR_PAIR()</var>function. This may seem to be 2110cumbersome at first. But this elegant solution allows us to manage 2111color pairs very easily. To appreciate it, you have to look into 2112the the source code of "dialog", a utility for displaying dialog 2113boxes from shell scripts. The developers have defined foreground 2114and background combinations for all the colors they might need and 2115initialized at the beginning. This makes it very easy to set 2116attributes just by accessing a pair which we already have defined 2117as a constant.</p> 2118<p>The following colors are defined in <var class= 2119"LITERAL">curses.h</var>. You can use these as parameters for 2120various color functions.</p> 2121<table border="0" bgcolor="#E0E0E0" width="100%"> 2122<tr> 2123<td> 2124<pre class="PROGRAMLISTING"> 2125<font color="#000000"> COLOR_BLACK 0 | 3374</SPAN 3375></PRE 3376></DIV 3377><P 3378>As you can see, to start using color, you should first call the function 3379<TT 3380CLASS="LITERAL" 3381> start_color()</TT 3382>. After that, you can use color 3383capabilities of your terminals using various functions. To find out whether a 3384terminal has color capabilities or not, you can use 3385<TT 3386CLASS="LITERAL" 3387>has_colors()</TT 3388> function, which returns FALSE if 3389the terminal does not support color. </P 3390><P 3391>Curses initializes all the colors supported by terminal when start_color() is 3392called. These can be accessed by the define constants like 3393<TT 3394CLASS="LITERAL" 3395>COLOR_BLACK </TT 3396> etc. Now to actually start using 3397colors, you have to define pairs. Colors are always used in pairs. That means 3398you have to use the function <TT 3399CLASS="LITERAL" 3400>init_pair() </TT 3401> to 3402define the foreground and background for the pair number you give. After that 3403that pair number can be used as a normal attribute with <TT 3404CLASS="LITERAL" 3405>COLOR_PAIR()</TT 3406>function. This may seem to be cumbersome at first. 3407But this elegant solution allows us to manage color pairs very easily. To 3408appreciate it, you have to look into the the source code of "dialog", a utility 3409for displaying dialog boxes from shell scripts. The developers have defined 3410foreground and background combinations for all the colors they might need and 3411initialized at the beginning. This makes it very easy to set attributes just by 3412accessing a pair which we already have defined as a constant.</P 3413><P 3414>The following colors are defined in <TT 3415CLASS="LITERAL" 3416>curses.h</TT 3417>. 3418You can use these as parameters for various color functions. 3419<PRE 3420CLASS="PROGRAMLISTING" 3421> COLOR_BLACK 0 |
2126 COLOR_RED 1 2127 COLOR_GREEN 2 2128 COLOR_YELLOW 3 2129 COLOR_BLUE 4 2130 COLOR_MAGENTA 5 2131 COLOR_CYAN 6 | 3422 COLOR_RED 1 3423 COLOR_GREEN 2 3424 COLOR_YELLOW 3 3425 COLOR_BLUE 4 3426 COLOR_MAGENTA 5 3427 COLOR_CYAN 6 |
2132 COLOR_WHITE 7</font> 2133</pre></td> 2134</tr> 2135</table> 2136</div> 2137<div class="SECT2"> 2138<hr> 2139<h3 class="SECT2"><a name="CHANGECOLORDEFS" id= 2140"CHANGECOLORDEFS">10.2. Changing Color Definitions</a></h3> 2141<p>The function <var class="LITERAL">init_color()</var>can be used 2142to change the rgb values for the colors defined by curses 2143initially. Say you wanted to lighten the intensity of red color by 2144a minuscule. Then you can use this function as</p> 2145<table border="0" bgcolor="#E0E0E0" width="100%"> 2146<tr> 2147<td> 2148<pre class="PROGRAMLISTING"> 2149<font color="#000000"> init_color(COLOR_RED, 700, 0, 0); | 3428 COLOR_WHITE 7</PRE 3429></P 3430></DIV 3431><DIV 3432CLASS="SECT2" 3433><HR><H3 3434CLASS="SECT2" 3435><A 3436NAME="CHANGECOLORDEFS" 3437>10.2. Changing Color Definitions</A 3438></H3 3439><P 3440>The function <TT 3441CLASS="LITERAL" 3442>init_color()</TT 3443>can be used to change 3444the rgb values for the colors defined by curses initially. Say you wanted to 3445lighten the intensity of red color by a minuscule. Then you can use this 3446function as</P 3447><PRE 3448CLASS="PROGRAMLISTING" 3449> init_color(COLOR_RED, 700, 0, 0); |
2150 /* param 1 : color name | 3450 /* param 1 : color name |
2151 * param 2, 3, 4 : rgb content min = 0, max = 1000 */</font> 2152</pre></td> 2153</tr> 2154</table> 2155<p>If your terminal cannot change the color definitions, the 2156function returns ERR. The function <var class= 2157"LITERAL">can_change_color()</var> can be used to find out whether 2158the terminal has the capability of changing color content or not. 2159The rgb content is scaled from 0 to 1000. Initially RED color is 2160defined with content 1000(r), 0(g), 0(b).</p> 2161</div> 2162<div class="SECT2"> 2163<hr> 2164<h3 class="SECT2"><a name="COLORCONTENT" id="COLORCONTENT">10.3. 2165Color Content</a></h3> 2166<p>The functions <var class="LITERAL">color_content()</var> and 2167<var class="LITERAL">pair_content()</var> can be used to find the 2168color content and foreground, background combination for the 2169pair.</p> 2170</div> 2171</div> 2172<div class="SECT1"> 2173<hr> 2174<h2 class="SECT1"><a name="KEYS" id="KEYS">11. Interfacing with the 2175key board</a></h2> 2176<div class="SECT2"> 2177<h3 class="SECT2"><a name="KEYSBASICS" id="KEYSBASICS">11.1. The 2178Basics</a></h3> 2179<p>No GUI is complete without a strong user interface and to 2180interact with the user, a curses program should be sensitive to key 2181presses or the mouse actions done by the user. Let's deal with the 2182keys first.</p> 2183<p>As you have seen in almost all of the above examples, it's very 2184easy to get key input from the user. A simple way of getting key 2185presses is to use <var class="LITERAL">getch()</var> function. The 2186cbreak mode should be enabled to read keys when you are interested 2187in reading individual key hits rather than complete lines of text 2188(which usually end with a carriage return). keypad should be 2189enabled to get the Functions keys, arrow keys etc. See the 2190initialization section for details.</p> 2191<p><var class="LITERAL">getch()</var> returns an integer 2192corresponding to the key pressed. If it is a normal character, the 2193integer value will be equivalent to the character. Otherwise it 2194returns a number which can be matched with the constants defined in 2195<var class="LITERAL">curses.h</var>. For example if the user 2196presses F1, the integer returned is 265. This can be checked using 2197the macro KEY_F() defined in curses.h. This makes reading keys 2198portable and easy to manage.</p> 2199<p>For example, if you call getch() like this</p> 2200<table border="0" bgcolor="#E0E0E0" width="100%"> 2201<tr> 2202<td> 2203<pre class="PROGRAMLISTING"> 2204<font color="#000000"> int ch; | 3451 * param 2, 3, 4 : rgb content min = 0, max = 1000 */</PRE 3452><P 3453>If your terminal cannot change the color definitions, the function returns ERR. 3454The function <TT 3455CLASS="LITERAL" 3456>can_change_color()</TT 3457> can be used to 3458find out whether the terminal has the capability of changing color content or 3459not. The rgb content is scaled from 0 to 1000. Initially RED color is defined 3460with content 1000(r), 0(g), 0(b). </P 3461></DIV 3462><DIV 3463CLASS="SECT2" 3464><HR><H3 3465CLASS="SECT2" 3466><A 3467NAME="COLORCONTENT" 3468>10.3. Color Content</A 3469></H3 3470><P 3471>The functions <TT 3472CLASS="LITERAL" 3473>color_content()</TT 3474> and 3475<TT 3476CLASS="LITERAL" 3477>pair_content()</TT 3478> can be used to find the color 3479content and foreground, background combination for the pair. </P 3480></DIV 3481></DIV 3482><DIV 3483CLASS="SECT1" 3484><HR><H2 3485CLASS="SECT1" 3486><A 3487NAME="KEYS" 3488>11. Interfacing with the key board</A 3489></H2 3490><DIV 3491CLASS="SECT2" 3492><H3 3493CLASS="SECT2" 3494><A 3495NAME="KEYSBASICS" 3496>11.1. The Basics</A 3497></H3 3498><P 3499>No GUI is complete without a strong user interface and to interact with the 3500user, a curses program should be sensitive to key presses or the mouse actions 3501done by the user. Let's deal with the keys first.</P 3502><P 3503>As you have seen in almost all of the above examples, it's very easy to get key 3504input from the user. A simple way of getting key presses is to use 3505<TT 3506CLASS="LITERAL" 3507>getch()</TT 3508> function. The cbreak mode should be 3509enabled to read keys when you are interested in reading individual key hits 3510rather than complete lines of text (which usually end with a carriage return). 3511keypad should be enabled to get the Functions keys, arrow keys etc. See the 3512initialization section for details.</P 3513><P 3514><TT 3515CLASS="LITERAL" 3516>getch()</TT 3517> returns an integer corresponding to the 3518key pressed. If it is a normal character, the integer value will be equivalent 3519to the character. Otherwise it returns a number which can be matched with the 3520constants defined in <TT 3521CLASS="LITERAL" 3522>curses.h</TT 3523>. For example if 3524the user presses F1, the integer returned is 265. This can be checked using the 3525macro KEY_F() defined in curses.h. This makes reading keys portable and easy to 3526manage.</P 3527><P 3528>For example, if you call getch() like this</P 3529><PRE 3530CLASS="PROGRAMLISTING" 3531> int ch; |
2205 | 3532 |
2206 ch = getch();</font> 2207</pre></td> 2208</tr> 2209</table> 2210<p>getch() will wait for the user to press a key, (unless you 2211specified a timeout) and when user presses a key, the corresponding 2212integer is returned. Then you can check the value returned with the 2213constants defined in curses.h to match against the keys you 2214want.</p> 2215<p>The following code piece will do that job.</p> 2216<table border="0" bgcolor="#E0E0E0" width="100%"> 2217<tr> 2218<td> 2219<pre class="PROGRAMLISTING"> 2220<font color="#000000"> if(ch == KEY_LEFT) 2221 printw("Left arrow is pressed\n");</font> 2222</pre></td> 2223</tr> 2224</table> 2225<p>Let's write a small program which creates a menu which can be 2226navigated by up and down arrows.</p> 2227</div> 2228<div class="SECT2"> 2229<hr> 2230<h3 class="SECT2"><a name="SIMPLEKEYEX" id="SIMPLEKEYEX">11.2. A 2231Simple Key Usage example</a></h3> 2232<div class="EXAMPLE"><a name="BSIKE" id="BSIKE"></a> 2233<p><b>Example 10. A Simple Key Usage example</b></p> 2234<table border="0" bgcolor="#E0E0E0" width="100%"> 2235<tr> 2236<td> 2237<pre class="PROGRAMLISTING"> 2238<font color="#000000"><span class= 2239"INLINEMEDIAOBJECT">#include <stdio.h> 2240#include <ncurses.h> | 3533 ch = getch();</PRE 3534><P 3535>getch() will wait for the user to press a key, (unless you specified a timeout) 3536and when user presses a key, the corresponding integer is returned. Then you can 3537check the value returned with the constants defined in curses.h to match against 3538the keys you want.</P 3539><P 3540>The following code piece will do that job.</P 3541><PRE 3542CLASS="PROGRAMLISTING" 3543> if(ch == KEY_LEFT) 3544 printw("Left arrow is pressed\n");</PRE 3545><P 3546>Let's write a small program which creates a menu which can be navigated by up 3547and down arrows.</P 3548></DIV 3549><DIV 3550CLASS="SECT2" 3551><HR><H3 3552CLASS="SECT2" 3553><A 3554NAME="SIMPLEKEYEX" 3555>11.2. A Simple Key Usage example</A 3556></H3 3557><DIV 3558CLASS="EXAMPLE" 3559><A 3560NAME="BSIKE" 3561></A 3562><P 3563><B 3564>Example 10. A Simple Key Usage example </B 3565></P 3566><PRE 3567CLASS="PROGRAMLISTING" 3568><SPAN 3569CLASS="INLINEMEDIAOBJECT" 3570>#include <stdio.h> 3571#include <ncurses.h> |
2241 2242#define WIDTH 30 2243#define HEIGHT 10 2244 2245int startx = 0; 2246int starty = 0; 2247 2248char *choices[] = { | 3572 3573#define WIDTH 30 3574#define HEIGHT 10 3575 3576int startx = 0; 3577int starty = 0; 3578 3579char *choices[] = { |
2249 "Choice 1", 2250 "Choice 2", 2251 "Choice 3", 2252 "Choice 4", 2253 "Exit", 2254 }; | 3580 "Choice 1", 3581 "Choice 2", 3582 "Choice 3", 3583 "Choice 4", 3584 "Exit", 3585 }; |
2255int n_choices = sizeof(choices) / sizeof(char *); 2256void print_menu(WINDOW *menu_win, int highlight); 2257 2258int main() | 3586int n_choices = sizeof(choices) / sizeof(char *); 3587void print_menu(WINDOW *menu_win, int highlight); 3588 3589int main() |
2259{ WINDOW *menu_win; 2260 int highlight = 1; 2261 int choice = 0; 2262 int c; | 3590{ WINDOW *menu_win; 3591 int highlight = 1; 3592 int choice = 0; 3593 int c; |
2263 | 3594 |
2264 initscr(); 2265 clear(); 2266 noecho(); 2267 cbreak(); /* Line buffering disabled. pass on everything */ 2268 startx = (80 - WIDTH) / 2; 2269 starty = (24 - HEIGHT) / 2; 2270 2271 menu_win = newwin(HEIGHT, WIDTH, starty, startx); 2272 keypad(menu_win, TRUE); 2273 mvprintw(0, 0, "Use arrow keys to go up and down, Press enter to select a choice"); 2274 refresh(); 2275 print_menu(menu_win, highlight); 2276 while(1) 2277 { c = wgetch(menu_win); 2278 switch(c) 2279 { case KEY_UP: 2280 if(highlight == 1) 2281 highlight = n_choices; 2282 else 2283 --highlight; 2284 break; 2285 case KEY_DOWN: 2286 if(highlight == n_choices) 2287 highlight = 1; 2288 else 2289 ++highlight; 2290 break; 2291 case 10: 2292 choice = highlight; 2293 break; 2294 default: 2295 mvprintw(24, 0, "Charcter pressed is = %3d Hopefully it can be printed as '%c'", c, c); 2296 refresh(); 2297 break; 2298 } 2299 print_menu(menu_win, highlight); 2300 if(choice != 0) /* User did a choice come out of the infinite loop */ 2301 break; 2302 } 2303 mvprintw(23, 0, "You chose choice %d with choice string %s\n", choice, choices[choice - 1]); 2304 clrtoeol(); 2305 refresh(); 2306 endwin(); 2307 return 0; | 3595 initscr(); 3596 clear(); 3597 noecho(); 3598 cbreak(); /* Line buffering disabled. pass on everything */ 3599 startx = (80 - WIDTH) / 2; 3600 starty = (24 - HEIGHT) / 2; 3601 3602 menu_win = newwin(HEIGHT, WIDTH, starty, startx); 3603 keypad(menu_win, TRUE); 3604 mvprintw(0, 0, "Use arrow keys to go up and down, Press enter to select a choice"); 3605 refresh(); 3606 print_menu(menu_win, highlight); 3607 while(1) 3608 { c = wgetch(menu_win); 3609 switch(c) 3610 { case KEY_UP: 3611 if(highlight == 1) 3612 highlight = n_choices; 3613 else 3614 --highlight; 3615 break; 3616 case KEY_DOWN: 3617 if(highlight == n_choices) 3618 highlight = 1; 3619 else 3620 ++highlight; 3621 break; 3622 case 10: 3623 choice = highlight; 3624 break; 3625 default: 3626 mvprintw(24, 0, "Charcter pressed is = %3d Hopefully it can be printed as '%c'", c, c); 3627 refresh(); 3628 break; 3629 } 3630 print_menu(menu_win, highlight); 3631 if(choice != 0) /* User did a choice come out of the infinite loop */ 3632 break; 3633 } 3634 mvprintw(23, 0, "You chose choice %d with choice string %s\n", choice, choices[choice - 1]); 3635 clrtoeol(); 3636 refresh(); 3637 endwin(); 3638 return 0; |
2308} 2309 2310 2311void print_menu(WINDOW *menu_win, int highlight) 2312{ | 3639} 3640 3641 3642void print_menu(WINDOW *menu_win, int highlight) 3643{ |
2313 int x, y, i; | 3644 int x, y, i; |
2314 | 3645 |
2315 x = 2; 2316 y = 2; 2317 box(menu_win, 0, 0); 2318 for(i = 0; i < n_choices; ++i) 2319 { if(highlight == i + 1) /* High light the present choice */ 2320 { wattron(menu_win, A_REVERSE); 2321 mvwprintw(menu_win, y, x, "%s", choices[i]); 2322 wattroff(menu_win, A_REVERSE); 2323 } 2324 else 2325 mvwprintw(menu_win, y, x, "%s", choices[i]); 2326 ++y; 2327 } 2328 wrefresh(menu_win); | 3646 x = 2; 3647 y = 2; 3648 box(menu_win, 0, 0); 3649 for(i = 0; i < n_choices; ++i) 3650 { if(highlight == i + 1) /* High light the present choice */ 3651 { wattron(menu_win, A_REVERSE); 3652 mvwprintw(menu_win, y, x, "%s", choices[i]); 3653 wattroff(menu_win, A_REVERSE); 3654 } 3655 else 3656 mvwprintw(menu_win, y, x, "%s", choices[i]); 3657 ++y; 3658 } 3659 wrefresh(menu_win); |
2329} | 3660} |
2330</span></font> 2331</pre></td> 2332</tr> 2333</table> 2334</div> 2335</div> 2336</div> 2337<div class="SECT1"> 2338<hr> 2339<h2 class="SECT1"><a name="MOUSE" id="MOUSE">12. Interfacing with 2340the mouse</a></h2> 2341<p>Now that you have seen how to get keys, lets do the same thing 2342from mouse. Usually each UI allows the user to interact with both 2343keyboard and mouse.</p> 2344<div class="SECT2"> 2345<hr> 2346<h3 class="SECT2"><a name="MOUSEBASICS" id="MOUSEBASICS">12.1. The 2347Basics</a></h3> 2348<p>Before you do any thing else, the events you want to receive 2349have to be enabled with <var class="LITERAL">mousemask()</var>.</p> 2350<table border="0" bgcolor="#E0E0E0" width="100%"> 2351<tr> 2352<td> 2353<pre class="PROGRAMLISTING"> 2354<font color= 2355"#000000"> mousemask( mmask_t newmask, /* The events you want to listen to */ 2356 mmask_t *oldmask) /* The old events mask */</font> 2357</pre></td> 2358</tr> 2359</table> 2360<p>The first parameter to above function is a bit mask of events 2361you would like to listen. By default, all the events are turned 2362off. The bit mask <var class="LITERAL">ALL_MOUSE_EVENTS</var> can 2363be used to get all the events.</p> 2364<p>The following are all the event masks:</p> 2365<table border="0" bgcolor="#E0E0E0" width="100%"> 2366<tr> 2367<td> 2368<pre class="PROGRAMLISTING"> 2369<font color="#000000"> Name Description | 3661</SPAN 3662></PRE 3663></DIV 3664></DIV 3665></DIV 3666><DIV 3667CLASS="SECT1" 3668><HR><H2 3669CLASS="SECT1" 3670><A 3671NAME="MOUSE" 3672>12. Interfacing with the mouse</A 3673></H2 3674><P 3675>Now that you have seen how to get keys, lets do the same thing from mouse. 3676Usually each UI allows the user to interact with both keyboard and mouse. </P 3677><DIV 3678CLASS="SECT2" 3679><HR><H3 3680CLASS="SECT2" 3681><A 3682NAME="MOUSEBASICS" 3683>12.1. The Basics</A 3684></H3 3685><P 3686>Before you do any thing else, the events you want to receive have to be enabled 3687with <TT 3688CLASS="LITERAL" 3689>mousemask()</TT 3690>.</P 3691><PRE 3692CLASS="PROGRAMLISTING" 3693> mousemask( mmask_t newmask, /* The events you want to listen to */ 3694 mmask_t *oldmask) /* The old events mask */</PRE 3695><P 3696>The first parameter to above function is a bit mask of events you would like to 3697listen. By default, all the events are turned off. The bit mask <TT 3698CLASS="LITERAL" 3699> ALL_MOUSE_EVENTS</TT 3700> can be used to get all the events.</P 3701><P 3702>The following are all the event masks:</P 3703><PRE 3704CLASS="PROGRAMLISTING" 3705> Name Description |
2370 --------------------------------------------------------------------- 2371 BUTTON1_PRESSED mouse button 1 down 2372 BUTTON1_RELEASED mouse button 1 up 2373 BUTTON1_CLICKED mouse button 1 clicked 2374 BUTTON1_DOUBLE_CLICKED mouse button 1 double clicked 2375 BUTTON1_TRIPLE_CLICKED mouse button 1 triple clicked 2376 BUTTON2_PRESSED mouse button 2 down 2377 BUTTON2_RELEASED mouse button 2 up --- 9 unchanged lines hidden (view full) --- 2387 BUTTON4_RELEASED mouse button 4 up 2388 BUTTON4_CLICKED mouse button 4 clicked 2389 BUTTON4_DOUBLE_CLICKED mouse button 4 double clicked 2390 BUTTON4_TRIPLE_CLICKED mouse button 4 triple clicked 2391 BUTTON_SHIFT shift was down during button state change 2392 BUTTON_CTRL control was down during button state change 2393 BUTTON_ALT alt was down during button state change 2394 ALL_MOUSE_EVENTS report all button state changes | 3706 --------------------------------------------------------------------- 3707 BUTTON1_PRESSED mouse button 1 down 3708 BUTTON1_RELEASED mouse button 1 up 3709 BUTTON1_CLICKED mouse button 1 clicked 3710 BUTTON1_DOUBLE_CLICKED mouse button 1 double clicked 3711 BUTTON1_TRIPLE_CLICKED mouse button 1 triple clicked 3712 BUTTON2_PRESSED mouse button 2 down 3713 BUTTON2_RELEASED mouse button 2 up --- 9 unchanged lines hidden (view full) --- 3723 BUTTON4_RELEASED mouse button 4 up 3724 BUTTON4_CLICKED mouse button 4 clicked 3725 BUTTON4_DOUBLE_CLICKED mouse button 4 double clicked 3726 BUTTON4_TRIPLE_CLICKED mouse button 4 triple clicked 3727 BUTTON_SHIFT shift was down during button state change 3728 BUTTON_CTRL control was down during button state change 3729 BUTTON_ALT alt was down during button state change 3730 ALL_MOUSE_EVENTS report all button state changes |
2395 REPORT_MOUSE_POSITION report mouse movement</font> 2396</pre></td> 2397</tr> 2398</table> 2399</div> 2400<div class="SECT2"> 2401<hr> 2402<h3 class="SECT2"><a name="GETTINGEVENTS" id="GETTINGEVENTS">12.2. 2403Getting the events</a></h3> 2404<p>Once a class of mouse events have been enabled, getch() class of 2405functions return KEY_MOUSE every time some mouse event happens. 2406Then the mouse event can be retrieved with <var class= 2407"LITERAL">getmouse()</var>.</p> 2408<p>The code approximately looks like this:</p> 2409<table border="0" bgcolor="#E0E0E0" width="100%"> 2410<tr> 2411<td> 2412<pre class="PROGRAMLISTING"> 2413<font color="#000000"> MEVENT event; | 3731 REPORT_MOUSE_POSITION report mouse movement</PRE 3732></DIV 3733><DIV 3734CLASS="SECT2" 3735><HR><H3 3736CLASS="SECT2" 3737><A 3738NAME="GETTINGEVENTS" 3739>12.2. Getting the events</A 3740></H3 3741><P 3742>Once a class of mouse events have been enabled, getch() class of functions 3743return KEY_MOUSE every time some mouse event happens. Then the mouse event can 3744be retrieved with <TT 3745CLASS="LITERAL" 3746>getmouse()</TT 3747>.</P 3748><P 3749>The code approximately looks like this:</P 3750><PRE 3751CLASS="PROGRAMLISTING" 3752> MEVENT event; |
2414 2415 ch = getch(); 2416 if(ch == KEY_MOUSE) 2417 if(getmouse(&event) == OK) 2418 . /* Do some thing with the event */ 2419 . | 3753 3754 ch = getch(); 3755 if(ch == KEY_MOUSE) 3756 if(getmouse(&event) == OK) 3757 . /* Do some thing with the event */ 3758 . |
2420 .</font> 2421</pre></td> 2422</tr> 2423</table> 2424<p>getmouse() returns the event into the pointer given to it. It's 2425a structure which contains</p> 2426<table border="0" bgcolor="#E0E0E0" width="100%"> 2427<tr> 2428<td> 2429<pre class="PROGRAMLISTING"> 2430<font color="#000000"> typedef struct | 3759 .</PRE 3760><P 3761> 3762getmouse() returns the event into the pointer given to it. It's a structure 3763which contains</P 3764><PRE 3765CLASS="PROGRAMLISTING" 3766> typedef struct |
2431 { 2432 short id; /* ID to distinguish multiple devices */ 2433 int x, y, z; /* event coordinates */ 2434 mmask_t bstate; /* button state bits */ | 3767 { 3768 short id; /* ID to distinguish multiple devices */ 3769 int x, y, z; /* event coordinates */ 3770 mmask_t bstate; /* button state bits */ |
2435 } </font> 2436</pre></td> 2437</tr> 2438</table> 2439<p>The <var class="LITERAL">bstate</var> is the main variable we 2440are interested in. It tells the button state of the mouse.</p> 2441<p>Then with a code snippet like the following, we can find out 2442what happened.</p> 2443<table border="0" bgcolor="#E0E0E0" width="100%"> 2444<tr> 2445<td> 2446<pre class="PROGRAMLISTING"> 2447<font color="#000000"> if(event.bstate & BUTTON1_PRESSED) 2448 printw("Left Button Pressed");</font> 2449</pre></td> 2450</tr> 2451</table> 2452</div> 2453<div class="SECT2"> 2454<hr> 2455<h3 class="SECT2"><a name="MOUSETOGETHER" id="MOUSETOGETHER">12.3. 2456Putting it all Together</a></h3> 2457<p>That's pretty much interfacing with mouse. Let's create the same 2458menu and enable mouse interaction. To make things simpler, key 2459handling is removed.</p> 2460<div class="EXAMPLE"><a name="BMOME" id="BMOME"></a> 2461<p><b>Example 11. Access the menu with mouse !!!</b></p> 2462<table border="0" bgcolor="#E0E0E0" width="100%"> 2463<tr> 2464<td> 2465<pre class="PROGRAMLISTING"> 2466<font color="#000000"><span class= 2467"INLINEMEDIAOBJECT">#include <ncurses.h> | 3771 } </PRE 3772><P 3773>The <TT 3774CLASS="LITERAL" 3775>bstate</TT 3776> is the main variable we are 3777interested in. It tells the button state of the mouse.</P 3778><P 3779>Then with a code snippet like the following, we can find out what happened.</P 3780><PRE 3781CLASS="PROGRAMLISTING" 3782> if(event.bstate & BUTTON1_PRESSED) 3783 printw("Left Button Pressed");</PRE 3784></DIV 3785><DIV 3786CLASS="SECT2" 3787><HR><H3 3788CLASS="SECT2" 3789><A 3790NAME="MOUSETOGETHER" 3791>12.3. Putting it all Together</A 3792></H3 3793><P 3794>That's pretty much interfacing with mouse. Let's create the same menu and enable 3795mouse interaction. To make things simpler, key handling is removed.</P 3796><DIV 3797CLASS="EXAMPLE" 3798><A 3799NAME="BMOME" 3800></A 3801><P 3802><B 3803>Example 11. Access the menu with mouse !!! </B 3804></P 3805><PRE 3806CLASS="PROGRAMLISTING" 3807><SPAN 3808CLASS="INLINEMEDIAOBJECT" 3809>#include <ncurses.h> |
2468 2469#define WIDTH 30 2470#define HEIGHT 10 2471 2472int startx = 0; 2473int starty = 0; 2474 | 3810 3811#define WIDTH 30 3812#define HEIGHT 10 3813 3814int startx = 0; 3815int starty = 0; 3816 |
2475char *choices[] = { "Choice 1", 2476 "Choice 2", 2477 "Choice 3", 2478 "Choice 4", 2479 "Exit", 2480 }; | 3817char *choices[] = { "Choice 1", 3818 "Choice 2", 3819 "Choice 3", 3820 "Choice 4", 3821 "Exit", 3822 }; |
2481 2482int n_choices = sizeof(choices) / sizeof(char *); 2483 2484void print_menu(WINDOW *menu_win, int highlight); 2485void report_choice(int mouse_x, int mouse_y, int *p_choice); 2486 2487int main() | 3823 3824int n_choices = sizeof(choices) / sizeof(char *); 3825 3826void print_menu(WINDOW *menu_win, int highlight); 3827void report_choice(int mouse_x, int mouse_y, int *p_choice); 3828 3829int main() |
2488{ int c, choice = 0; 2489 WINDOW *menu_win; 2490 MEVENT event; | 3830{ int c, choice = 0; 3831 WINDOW *menu_win; 3832 MEVENT event; |
2491 | 3833 |
2492 /* Initialize curses */ 2493 initscr(); 2494 clear(); 2495 noecho(); 2496 cbreak(); //Line buffering disabled. pass on everything | 3834 /* Initialize curses */ 3835 initscr(); 3836 clear(); 3837 noecho(); 3838 cbreak(); //Line buffering disabled. pass on everything |
2497 | 3839 |
2498 /* Try to put the window in the middle of screen */ 2499 startx = (80 - WIDTH) / 2; 2500 starty = (24 - HEIGHT) / 2; 2501 2502 attron(A_REVERSE); 2503 mvprintw(23, 1, "Click on Exit to quit (Works best in a virtual console)"); 2504 refresh(); 2505 attroff(A_REVERSE); | 3840 /* Try to put the window in the middle of screen */ 3841 startx = (80 - WIDTH) / 2; 3842 starty = (24 - HEIGHT) / 2; 3843 3844 attron(A_REVERSE); 3845 mvprintw(23, 1, "Click on Exit to quit (Works best in a virtual console)"); 3846 refresh(); 3847 attroff(A_REVERSE); |
2506 | 3848 |
2507 /* Print the menu for the first time */ 2508 menu_win = newwin(HEIGHT, WIDTH, starty, startx); 2509 print_menu(menu_win, 1); 2510 /* Get all the mouse events */ 2511 mousemask(ALL_MOUSE_EVENTS, NULL); 2512 2513 while(1) 2514 { c = wgetch(menu_win); 2515 switch(c) 2516 { case KEY_MOUSE: 2517 if(getmouse(&event) == OK) 2518 { /* When the user clicks left mouse button */ 2519 if(event.bstate & BUTTON1_PRESSED) 2520 { report_choice(event.x + 1, event.y + 1, &choice); 2521 if(choice == -1) //Exit chosen 2522 goto end; 2523 mvprintw(22, 1, "Choice made is : %d String Chosen is \"%10s\"", choice, choices[choice - 1]); 2524 refresh(); 2525 } 2526 } 2527 print_menu(menu_win, choice); 2528 break; 2529 } 2530 } | 3849 /* Print the menu for the first time */ 3850 menu_win = newwin(HEIGHT, WIDTH, starty, startx); 3851 print_menu(menu_win, 1); 3852 /* Get all the mouse events */ 3853 mousemask(ALL_MOUSE_EVENTS, NULL); 3854 3855 while(1) 3856 { c = wgetch(menu_win); 3857 switch(c) 3858 { case KEY_MOUSE: 3859 if(getmouse(&event) == OK) 3860 { /* When the user clicks left mouse button */ 3861 if(event.bstate & BUTTON1_PRESSED) 3862 { report_choice(event.x + 1, event.y + 1, &choice); 3863 if(choice == -1) //Exit chosen 3864 goto end; 3865 mvprintw(22, 1, "Choice made is : %d String Chosen is \"%10s\"", choice, choices[choice - 1]); 3866 refresh(); 3867 } 3868 } 3869 print_menu(menu_win, choice); 3870 break; 3871 } 3872 } |
2531end: | 3873end: |
2532 endwin(); 2533 return 0; | 3874 endwin(); 3875 return 0; |
2534} 2535 2536 2537void print_menu(WINDOW *menu_win, int highlight) 2538{ | 3876} 3877 3878 3879void print_menu(WINDOW *menu_win, int highlight) 3880{ |
2539 int x, y, i; | 3881 int x, y, i; |
2540 | 3882 |
2541 x = 2; 2542 y = 2; 2543 box(menu_win, 0, 0); 2544 for(i = 0; i < n_choices; ++i) 2545 { if(highlight == i + 1) 2546 { wattron(menu_win, A_REVERSE); 2547 mvwprintw(menu_win, y, x, "%s", choices[i]); 2548 wattroff(menu_win, A_REVERSE); 2549 } 2550 else 2551 mvwprintw(menu_win, y, x, "%s", choices[i]); 2552 ++y; 2553 } 2554 wrefresh(menu_win); | 3883 x = 2; 3884 y = 2; 3885 box(menu_win, 0, 0); 3886 for(i = 0; i < n_choices; ++i) 3887 { if(highlight == i + 1) 3888 { wattron(menu_win, A_REVERSE); 3889 mvwprintw(menu_win, y, x, "%s", choices[i]); 3890 wattroff(menu_win, A_REVERSE); 3891 } 3892 else 3893 mvwprintw(menu_win, y, x, "%s", choices[i]); 3894 ++y; 3895 } 3896 wrefresh(menu_win); |
2555} 2556 2557/* Report the choice according to mouse position */ 2558void report_choice(int mouse_x, int mouse_y, int *p_choice) | 3897} 3898 3899/* Report the choice according to mouse position */ 3900void report_choice(int mouse_x, int mouse_y, int *p_choice) |
2559{ int i,j, choice; | 3901{ int i,j, choice; |
2560 | 3902 |
2561 i = startx + 2; 2562 j = starty + 3; 2563 2564 for(choice = 0; choice < n_choices; ++choice) 2565 if(mouse_y == j + choice && mouse_x >= i && mouse_x <= i + strlen(choices[choice])) 2566 { if(choice == n_choices - 1) 2567 *p_choice = -1; 2568 else 2569 *p_choice = choice + 1; 2570 break; 2571 } 2572}</span></font> 2573</pre></td> 2574</tr> 2575</table> 2576</div> 2577</div> 2578<div class="SECT2"> 2579<hr> 2580<h3 class="SECT2"><a name="MISCMOUSEFUNCS" id= 2581"MISCMOUSEFUNCS">12.4. Miscellaneous Functions</a></h3> 2582<p>The functions mouse_trafo() and wmouse_trafo() can be used to 2583convert to mouse co-ordinates to screen relative co-ordinates. See 2584curs_mouse(3X) man page for details.</p> 2585<p>The mouseinterval function sets the maximum time (in thousands 2586of a second) that can elapse between press and release events in 2587order for them to be recognized as a click. This function returns 2588the previous interval value. The default is one fifth of a 2589second.</p> 2590</div> 2591</div> 2592<div class="SECT1"> 2593<hr> 2594<h2 class="SECT1"><a name="SCREEN" id="SCREEN">13. Screen 2595Manipulation</a></h2> 2596<p>In this section, we will look into some functions, which allow 2597us to manage the screen efficiently and to write some fancy 2598programs. This is especially important in writing games.</p> 2599<div class="SECT2"> 2600<hr> 2601<h3 class="SECT2"><a name="GETYX" id="GETYX">13.1. getyx() 2602functions</a></h3> 2603<p>The function <var class="LITERAL">getyx()</var> can be used to 2604find out the present cursor co-ordinates. It will fill the values 2605of x and y co-ordinates in the arguments given to it. Since getyx() 2606is a macro you don't have to pass the address of the variables. It 2607can be called as</p> 2608<table border="0" bgcolor="#E0E0E0" width="100%"> 2609<tr> 2610<td> 2611<pre class="PROGRAMLISTING"> 2612<font color="#000000"> getyx(win, y, x); | 3903 i = startx + 2; 3904 j = starty + 3; 3905 3906 for(choice = 0; choice < n_choices; ++choice) 3907 if(mouse_y == j + choice && mouse_x >= i && mouse_x <= i + strlen(choices[choice])) 3908 { if(choice == n_choices - 1) 3909 *p_choice = -1; 3910 else 3911 *p_choice = choice + 1; 3912 break; 3913 } 3914}</SPAN 3915></PRE 3916></DIV 3917></DIV 3918><DIV 3919CLASS="SECT2" 3920><HR><H3 3921CLASS="SECT2" 3922><A 3923NAME="MISCMOUSEFUNCS" 3924>12.4. Miscellaneous Functions</A 3925></H3 3926><P 3927>The functions mouse_trafo() and wmouse_trafo() can be used to convert to mouse 3928co-ordinates to screen relative co-ordinates. See curs_mouse(3X) man page for details.</P 3929><P 3930>The mouseinterval function sets the maximum time (in thousands of a 3931second) that can elapse between press and release events in order for 3932them to be recognized as a click. This function returns the previous 3933interval value. The default is one fifth of a second.</P 3934></DIV 3935></DIV 3936><DIV 3937CLASS="SECT1" 3938><HR><H2 3939CLASS="SECT1" 3940><A 3941NAME="SCREEN" 3942>13. Screen Manipulation</A 3943></H2 3944><P 3945>In this section, we will look into some functions, which allow us to manage the 3946screen efficiently and to write some fancy programs. This is especially 3947important in writing games. </P 3948><DIV 3949CLASS="SECT2" 3950><HR><H3 3951CLASS="SECT2" 3952><A 3953NAME="GETYX" 3954>13.1. getyx() functions</A 3955></H3 3956><P 3957> The function <TT 3958CLASS="LITERAL" 3959>getyx()</TT 3960> can be used to find out 3961the present cursor co-ordinates. It will fill the values of x and y co-ordinates 3962in the arguments given to it. Since getyx() is a macro you don't have to pass 3963the address of the variables. It can be called as</P 3964><PRE 3965CLASS="PROGRAMLISTING" 3966> getyx(win, y, x); |
2613 /* win: window pointer 2614 * y, x: y, x co-ordinates will be put into this variables | 3967 /* win: window pointer 3968 * y, x: y, x co-ordinates will be put into this variables |
2615 */</font> 2616</pre></td> 2617</tr> 2618</table> 2619<p>The function getparyx() gets the beginning co-ordinates of the 2620sub window relative to the main window. This is some times useful 2621to update a sub window. When designing fancy stuff like writing 2622multiple menus, it becomes difficult to store the menu positions, 2623their first option co-ordinates etc. A simple solution to this 2624problem, is to create menus in sub windows and later find the 2625starting co-ordinates of the menus by using getparyx().</p> 2626<p>The functions getbegyx() and getmaxyx() store current window's 2627beginning and maximum co-ordinates. These functions are useful in 2628the same way as above in managing the windows and sub windows 2629effectively.</p> 2630</div> 2631<div class="SECT2"> 2632<hr> 2633<h3 class="SECT2"><a name="SCREENDUMP" id="SCREENDUMP">13.2. Screen 2634Dumping</a></h3> 2635<p>While writing games, some times it becomes necessary to store 2636the state of the screen and restore it back to the same state. The 2637function scr_dump() can be used to dump the screen contents to a 2638file given as an argument. Later it can be restored by scr_restore 2639function. These two simple functions can be used effectively to 2640maintain a fast moving game with changing scenarios.</p> 2641</div> 2642<div class="SECT2"> 2643<hr> 2644<h3 class="SECT2"><a name="WINDOWDUMP" id="WINDOWDUMP">13.3. Window 2645Dumping</a></h3> 2646<p>To store and restore windows, the functions <var class= 2647"LITERAL">putwin()</var> and <var class="LITERAL">getwin()</var> 2648can be used. <var class="LITERAL">putwin()</var> puts the present 2649window state into a file, which can be later restored by 2650<var class="LITERAL">getwin()</var>.</p> 2651<p>The function <var class="LITERAL">copywin()</var> can be used to 2652copy a window completely onto another window. It takes the source 2653and destination windows as parameters and according to the 2654rectangle specified, it copies the rectangular region from source 2655to destination window. It's last parameter specifies whether to 2656overwrite or just overlay the contents on to the destination 2657window. If this argument is true, then the copying is 2658non-destructive.</p> 2659</div> 2660</div> 2661<div class="SECT1"> 2662<hr> 2663<h2 class="SECT1"><a name="MISC" id="MISC">14. Miscellaneous 2664features</a></h2> 2665<p>Now you know enough features to write a good curses program, 2666with all bells and whistles. There are some miscellaneous functions 2667which are useful in various cases. Let's go headlong into some of 2668those.</p> 2669<div class="SECT2"> 2670<hr> 2671<h3 class="SECT2"><a name="CURSSET" id="CURSSET">14.1. 2672curs_set()</a></h3> 2673<p>This function can be used to make the cursor invisible. The 2674parameter to this function should be</p> 2675<table border="0" bgcolor="#E0E0E0" width="100%"> 2676<tr> 2677<td> 2678<pre class="PROGRAMLISTING"> 2679<font color="#000000"> 0 : invisible or | 3969 */</PRE 3970><P 3971>The function getparyx() gets the beginning co-ordinates of the sub window 3972relative to the main window. This is some times useful to update a sub window. 3973When designing fancy stuff like writing multiple menus, it becomes difficult to 3974store the menu positions, their first option co-ordinates etc. A simple solution 3975to this problem, is to create menus in sub windows and later find the starting 3976co-ordinates of the menus by using getparyx().</P 3977><P 3978>The functions getbegyx() and getmaxyx() store current window's beginning and 3979maximum co-ordinates. These functions are useful in the same way as above in 3980managing the windows and sub windows effectively.</P 3981></DIV 3982><DIV 3983CLASS="SECT2" 3984><HR><H3 3985CLASS="SECT2" 3986><A 3987NAME="SCREENDUMP" 3988>13.2. Screen Dumping</A 3989></H3 3990><P 3991>While writing games, some times it becomes necessary to store the state of the 3992screen and restore it back to the same state. The function scr_dump() can be 3993used to dump the screen contents to a file given as an argument. Later it can be 3994restored by scr_restore function. These two simple functions can be used 3995effectively to maintain a fast moving game with changing scenarios. </P 3996></DIV 3997><DIV 3998CLASS="SECT2" 3999><HR><H3 4000CLASS="SECT2" 4001><A 4002NAME="WINDOWDUMP" 4003>13.3. Window Dumping</A 4004></H3 4005><P 4006>To store and restore windows, the functions 4007<TT 4008CLASS="LITERAL" 4009>putwin()</TT 4010> and <TT 4011CLASS="LITERAL" 4012>getwin()</TT 4013> can be used. <TT 4014CLASS="LITERAL" 4015>putwin()</TT 4016> puts 4017the present window state into a file, which can be later restored by 4018<TT 4019CLASS="LITERAL" 4020>getwin()</TT 4021>.</P 4022><P 4023> 4024The function <TT 4025CLASS="LITERAL" 4026>copywin()</TT 4027> can be used to copy a 4028window completely onto another window. It takes the source and destination 4029windows as parameters and according to the rectangle specified, it copies the 4030rectangular region from source to destination window. It's last parameter 4031specifies whether to overwrite or just overlay the contents on to the 4032destination window. If this argument is true, then the copying is 4033non-destructive.</P 4034></DIV 4035></DIV 4036><DIV 4037CLASS="SECT1" 4038><HR><H2 4039CLASS="SECT1" 4040><A 4041NAME="MISC" 4042>14. Miscellaneous features</A 4043></H2 4044><P 4045>Now you know enough features to write a good curses program, with all bells and 4046whistles. There are some miscellaneous functions which are useful in various 4047cases. Let's go headlong into some of those.</P 4048><DIV 4049CLASS="SECT2" 4050><HR><H3 4051CLASS="SECT2" 4052><A 4053NAME="CURSSET" 4054>14.1. curs_set()</A 4055></H3 4056><P 4057>This function can be used to make the cursor invisible. The parameter to this 4058function should be </P 4059><PRE 4060CLASS="PROGRAMLISTING" 4061> 0 : invisible or |
2680 1 : normal or | 4062 1 : normal or |
2681 2 : very visible.</font> 2682</pre></td> 2683</tr> 2684</table> 2685</div> 2686<div class="SECT2"> 2687<hr> 2688<h3 class="SECT2"><a name="TEMPLEAVE" id="TEMPLEAVE">14.2. 2689Temporarily Leaving Curses mode</a></h3> 2690<p>Some times you may want to get back to cooked mode (normal line 2691buffering mode) temporarily. In such a case you will first need to 2692save the tty modes with a call to <var class= 2693"LITERAL">def_prog_mode()</var> and then call <var class= 2694"LITERAL">endwin()</var> to end the curses mode. This will leave 2695you in the original tty mode. To get back to curses once you are 2696done, call <var class="LITERAL">reset_prog_mode()</var> . This 2697function returns the tty to the state stored by <var class= 2698"LITERAL">def_prog_mode()</var>. Then do refresh(), and you are 2699back to the curses mode. Here is an example showing the sequence of 2700things to be done.</p> 2701<div class="EXAMPLE"><a name="BTELE" id="BTELE"></a> 2702<p><b>Example 12. Temporarily Leaving Curses Mode</b></p> 2703<table border="0" bgcolor="#E0E0E0" width="100%"> 2704<tr> 2705<td> 2706<pre class="PROGRAMLISTING"> 2707<font color="#000000"><span class= 2708"INLINEMEDIAOBJECT">#include <ncurses.h> | 4063 2 : very visible.</PRE 4064></DIV 4065><DIV 4066CLASS="SECT2" 4067><HR><H3 4068CLASS="SECT2" 4069><A 4070NAME="TEMPLEAVE" 4071>14.2. Temporarily Leaving Curses mode</A 4072></H3 4073><P 4074>Some times you may want to get back to cooked mode (normal line buffering mode) 4075temporarily. In such a case you will first need to save the tty modes with a 4076call to <TT 4077CLASS="LITERAL" 4078>def_prog_mode()</TT 4079> and then call 4080<TT 4081CLASS="LITERAL" 4082>endwin()</TT 4083> to end the curses mode. This will 4084leave you in the original tty mode. To get back to curses once you are done, 4085call <TT 4086CLASS="LITERAL" 4087>reset_prog_mode() </TT 4088>. This function returns 4089the tty to the state stored by <TT 4090CLASS="LITERAL" 4091>def_prog_mode()</TT 4092>. Then do refresh(), and you are back to the curses mode. Here 4093is an example showing the sequence of things to be done.</P 4094><DIV 4095CLASS="EXAMPLE" 4096><A 4097NAME="BTELE" 4098></A 4099><P 4100><B 4101>Example 12. Temporarily Leaving Curses Mode </B 4102></P 4103><PRE 4104CLASS="PROGRAMLISTING" 4105><SPAN 4106CLASS="INLINEMEDIAOBJECT" 4107>#include <ncurses.h> |
2709 2710int main() | 4108 4109int main() |
2711{ 2712 initscr(); /* Start curses mode */ 2713 printw("Hello World !!!\n"); /* Print Hello World */ 2714 refresh(); /* Print it on to the real screen */ 2715 def_prog_mode(); /* Save the tty modes */ 2716 endwin(); /* End curses mode temporarily */ 2717 system("/bin/sh"); /* Do whatever you like in cooked mode */ 2718 reset_prog_mode(); /* Return to the previous tty mode*/ 2719 /* stored by def_prog_mode() */ 2720 refresh(); /* Do refresh() to restore the */ 2721 /* Screen contents */ 2722 printw("Another String\n"); /* Back to curses use the full */ 2723 refresh(); /* capabilities of curses */ 2724 endwin(); /* End curses mode */ | 4110{ 4111 initscr(); /* Start curses mode */ 4112 printw("Hello World !!!\n"); /* Print Hello World */ 4113 refresh(); /* Print it on to the real screen */ 4114 def_prog_mode(); /* Save the tty modes */ 4115 endwin(); /* End curses mode temporarily */ 4116 system("/bin/sh"); /* Do whatever you like in cooked mode */ 4117 reset_prog_mode(); /* Return to the previous tty mode*/ 4118 /* stored by def_prog_mode() */ 4119 refresh(); /* Do refresh() to restore the */ 4120 /* Screen contents */ 4121 printw("Another String\n"); /* Back to curses use the full */ 4122 refresh(); /* capabilities of curses */ 4123 endwin(); /* End curses mode */ |
2725 | 4124 |
2726 return 0; 2727}</span></font> 2728</pre></td> 2729</tr> 2730</table> 2731</div> 2732</div> 2733<div class="SECT2"> 2734<hr> 2735<h3 class="SECT2"><a name="ACSVARS" id="ACSVARS">14.3. ACS_ 2736variables</a></h3> 2737<p>If you have ever programmed in DOS, you know about those nifty 2738characters in extended character set. They are printable only on 2739some terminals. NCURSES functions like <var class= 2740"LITERAL">box()</var> use these characters. All these variables 2741start with ACS meaning alternative character set. You might have 2742noticed me using these characters in some of the programs above. 2743Here's an example showing all the characters.</p> 2744<div class="EXAMPLE"><a name="BACSVARS" id="BACSVARS"></a> 2745<p><b>Example 13. ACS Variables Example</b></p> 2746<table border="0" bgcolor="#E0E0E0" width="100%"> 2747<tr> 2748<td> 2749<pre class="PROGRAMLISTING"> 2750<font color="#000000"><span class= 2751"INLINEMEDIAOBJECT">#include <ncurses.h> | 4125 return 0; 4126}</SPAN 4127></PRE 4128></DIV 4129></DIV 4130><DIV 4131CLASS="SECT2" 4132><HR><H3 4133CLASS="SECT2" 4134><A 4135NAME="ACSVARS" 4136>14.3. ACS_ variables</A 4137></H3 4138><P 4139>If you have ever programmed in DOS, you know about those nifty characters in 4140extended character set. They are printable only on some terminals. NCURSES 4141functions like <TT 4142CLASS="LITERAL" 4143>box()</TT 4144> use these characters. All 4145these variables start with ACS meaning alternative character set. You might have 4146noticed me using these characters in some of the programs above. Here's an example 4147showing all the characters.</P 4148><DIV 4149CLASS="EXAMPLE" 4150><A 4151NAME="BACSVARS" 4152></A 4153><P 4154><B 4155>Example 13. ACS Variables Example </B 4156></P 4157><PRE 4158CLASS="PROGRAMLISTING" 4159><SPAN 4160CLASS="INLINEMEDIAOBJECT" 4161>#include <ncurses.h> |
2752 2753int main() 2754{ 2755 initscr(); 2756 2757 printw("Upper left corner "); addch(ACS_ULCORNER); printw("\n"); 2758 printw("Lower left corner "); addch(ACS_LLCORNER); printw("\n"); 2759 printw("Lower right corner "); addch(ACS_LRCORNER); printw("\n"); --- 25 unchanged lines hidden (view full) --- 2785 printw("Pi "); addch(ACS_PI); printw("\n"); 2786 printw("Not equal "); addch(ACS_NEQUAL); printw("\n"); 2787 printw("UK pound sign "); addch(ACS_STERLING); printw("\n"); 2788 2789 refresh(); 2790 getch(); 2791 endwin(); 2792 | 4162 4163int main() 4164{ 4165 initscr(); 4166 4167 printw("Upper left corner "); addch(ACS_ULCORNER); printw("\n"); 4168 printw("Lower left corner "); addch(ACS_LLCORNER); printw("\n"); 4169 printw("Lower right corner "); addch(ACS_LRCORNER); printw("\n"); --- 25 unchanged lines hidden (view full) --- 4195 printw("Pi "); addch(ACS_PI); printw("\n"); 4196 printw("Not equal "); addch(ACS_NEQUAL); printw("\n"); 4197 printw("UK pound sign "); addch(ACS_STERLING); printw("\n"); 4198 4199 refresh(); 4200 getch(); 4201 endwin(); 4202 |
2793 return 0; 2794}</span></font> 2795</pre></td> 2796</tr> 2797</table> 2798</div> 2799</div> 2800</div> 2801<div class="SECT1"> 2802<hr> 2803<h2 class="SECT1"><a name="OTHERLIB" id="OTHERLIB">15. Other 2804libraries</a></h2> 2805<p>Apart from the curses library, there are few text mode 2806libraries, which provide more functionality and a lot of features. 2807The following sections explain three standard libraries which are 2808usually distributed along with curses.</p> 2809</div> 2810<div class="SECT1"> 2811<hr> 2812<h2 class="SECT1"><a name="PANELS" id="PANELS">16. Panel 2813Library</a></h2> 2814<p>Now that you are proficient in curses, you wanted to do some 2815thing big. You created a lot of overlapping windows to give a 2816professional windows-type look. Unfortunately, it soon becomes 2817difficult to manage these. The multiple refreshes, updates plunge 2818you into a nightmare. The overlapping windows create blotches, 2819whenever you forget to refresh the windows in the proper order.</p> 2820<p>Don't despair. There's an elegant solution provided in panels 2821library. In the words of developers of ncurses</p> 2822<p><em>When your interface design is such that windows may dive 2823deeper into the visibility stack or pop to the top at runtime, the 2824resulting book-keeping can be tedious and difficult to get right. 2825Hence the panels library.</em></p> 2826<p>If you have lot of overlapping windows, then panels library is 2827the way to go. It obviates the need of doing series of 2828wnoutrefresh(), doupdate() and relieves the burden of doing it 2829correctly(bottom up). The library maintains information about the 2830order of windows, their overlapping and update the screen properly. 2831So why wait? Let's take a close peek into panels.</p> 2832<div class="SECT2"> 2833<hr> 2834<h3 class="SECT2"><a name="PANELBASICS" id="PANELBASICS">16.1. The 2835Basics</a></h3> 2836<p>Panel object is a window that is implicitly treated as part of a 2837deck including all other panel objects. The deck is treated as a 2838stack with the top panel being completely visible and the other 2839panels may or may not be obscured according to their positions. So 2840the basic idea is to create a stack of overlapping panels and use 2841panels library to display them correctly. There is a function 2842similar to refresh() which, when called , displays panels in the 2843correct order. Functions are provided to hide or show panels, move 2844panels, change its size etc.. The overlapping problem is managed by 2845the panels library during all the calls to these functions.</p> 2846<p>The general flow of a panel program goes like this:</p> 2847<ol type="1"> 2848<li> 2849<p>Create the windows (with newwin()) to be attached to the 2850panels.</p> 2851</li> 2852<li> 2853<p>Create panels with the chosen visibility order. Stack them up 2854according to the desired visibility. The function new_panel() is 2855used to created panels.</p> 2856</li> 2857<li> 2858<p>Call update_panels() to write the panels to the virtual screen 2859in correct visibility order. Do a doupdate() to show it on the 2860screen.</p> 2861</li> 2862<li> 2863<p>Mainpulate the panels with show_panel(), hide_panel(), 2864move_panel() etc. Make use of helper functions like panel_hidden() 2865and panel_window(). Make use of user pointer to store custom data 2866for a panel. Use the functions set_panel_userptr() and 2867panel_userptr() to set and get the user pointer for a panel.</p> 2868</li> 2869<li> 2870<p>When you are done with the panel use del_panel() to delete the 2871panel.</p> 2872</li> 2873</ol> 2874<p>Let's make the concepts clear, with some programs. The following 2875is a simple program which creates 3 overlapping panels and shows 2876them on the screen.</p> 2877</div> 2878<div class="SECT2"> 2879<hr> 2880<h3 class="SECT2"><a name="COMPILEPANELS" id="COMPILEPANELS">16.2. 2881Compiling With the Panels Library</a></h3> 2882<p>To use panels library functions, you have to include panel.h and 2883to link the program with panels library the flag -lpanel should be 2884added along with -lncurses in that order.</p> 2885<table border="0" bgcolor="#E0E0E0" width="100%"> 2886<tr> 2887<td> 2888<pre class="PROGRAMLISTING"> 2889<font color="#000000"> #include <panel.h> | 4203 return 0; 4204}</SPAN 4205></PRE 4206></DIV 4207></DIV 4208></DIV 4209><DIV 4210CLASS="SECT1" 4211><HR><H2 4212CLASS="SECT1" 4213><A 4214NAME="OTHERLIB" 4215>15. Other libraries</A 4216></H2 4217><P 4218>Apart from the curses library, there are few text mode libraries, which provide 4219more functionality and a lot of features. The following sections explain three 4220standard libraries which are usually distributed along with curses. </P 4221></DIV 4222><DIV 4223CLASS="SECT1" 4224><HR><H2 4225CLASS="SECT1" 4226><A 4227NAME="PANELS" 4228>16. Panel Library</A 4229></H2 4230><P 4231>Now that you are proficient in curses, you wanted to do some thing big. You 4232created a lot of overlapping windows to give a professional windows-type look. 4233Unfortunately, it soon becomes difficult to manage these. The multiple 4234refreshes, updates plunge you into a nightmare. The overlapping windows create 4235blotches, whenever you forget to refresh the windows in the proper order. </P 4236><P 4237>Don't despair. There's an elegant solution provided in panels library. In the 4238words of developers of ncurses </P 4239><P 4240><SPAN 4241CLASS="emphasis" 4242><I 4243CLASS="EMPHASIS" 4244>When your interface design is such that windows may dive deeper into the 4245visibility stack or pop to the top at runtime, the resulting book-keeping can be 4246tedious and difficult to get right. Hence the panels library.</I 4247></SPAN 4248></P 4249><P 4250>If you have lot of overlapping windows, then panels library is the way to go. It 4251obviates the need of doing series of wnoutrefresh(), doupdate() and relieves the 4252burden of doing it correctly(bottom up). The library maintains information about 4253the order of windows, their overlapping and update the screen properly. So why 4254wait? Let's take a close peek into panels.</P 4255><DIV 4256CLASS="SECT2" 4257><HR><H3 4258CLASS="SECT2" 4259><A 4260NAME="PANELBASICS" 4261>16.1. The Basics</A 4262></H3 4263><P 4264>Panel object is a window that is implicitly treated as part of a deck including 4265all other panel objects. The deck is treated as a stack with the top panel being 4266completely visible and the other panels may or may not be obscured according to 4267their positions. So the basic idea is to create a stack of overlapping panels 4268and use panels library to display them correctly. There is a function similar to 4269refresh() which, when called , displays panels in the correct order. Functions 4270are provided to hide or show panels, move panels, change its size etc.. The 4271overlapping problem is managed by the panels library during all the calls to 4272these functions. </P 4273><P 4274>The general flow of a panel program goes like this: 4275 4276<P 4277></P 4278><OL 4279TYPE="1" 4280><LI 4281><P 4282>Create the windows (with newwin()) to be attached to the panels.</P 4283></LI 4284><LI 4285><P 4286>Create panels with the chosen visibility order. Stack them up according to the 4287desired visibility. The function new_panel() is used to created panels.</P 4288></LI 4289><LI 4290><P 4291>Call update_panels() to write the panels to the virtual screen in correct 4292visibility order. Do a doupdate() to show it on the screen. </P 4293></LI 4294><LI 4295><P 4296>Mainpulate the panels with show_panel(), hide_panel(), move_panel() etc. Make 4297use of helper functions like panel_hidden() and panel_window(). Make use of user 4298pointer to store custom data for a panel. Use the functions set_panel_userptr() 4299and panel_userptr() to set and get the user pointer for a panel.</P 4300></LI 4301><LI 4302><P 4303>When you are done with the panel use del_panel() to delete the panel.</P 4304></LI 4305></OL 4306></P 4307><P 4308>Let's make the concepts clear, with some programs. The following is a simple 4309program which creates 3 overlapping panels and shows them on the screen. </P 4310></DIV 4311><DIV 4312CLASS="SECT2" 4313><HR><H3 4314CLASS="SECT2" 4315><A 4316NAME="COMPILEPANELS" 4317>16.2. Compiling With the Panels Library</A 4318></H3 4319><P 4320>To use panels library functions, you have to include panel.h and to link the 4321program with panels library the flag -lpanel should be added along with 4322-lncurses in that order.</P 4323><PRE 4324CLASS="PROGRAMLISTING" 4325> #include <panel.h> |
2890 . 2891 . 2892 . 2893 | 4326 . 4327 . 4328 . 4329 |
2894 compile and link: gcc <program file> -lpanel -lncurses</font> 2895</pre></td> 2896</tr> 2897</table> 2898<div class="EXAMPLE"><a name="PPASI" id="PPASI"></a> 2899<p><b>Example 14. Panel basics</b></p> 2900<table border="0" bgcolor="#E0E0E0" width="100%"> 2901<tr> 2902<td> 2903<pre class="PROGRAMLISTING"> 2904<font color="#000000"><span class= 2905"INLINEMEDIAOBJECT">#include <panel.h> | 4330 compile and link: gcc <program file> -lpanel -lncurses</PRE 4331><DIV 4332CLASS="EXAMPLE" 4333><A 4334NAME="PPASI" 4335></A 4336><P 4337><B 4338>Example 14. Panel basics</B 4339></P 4340><PRE 4341CLASS="PROGRAMLISTING" 4342><SPAN 4343CLASS="INLINEMEDIAOBJECT" 4344>#include <panel.h> |
2906 2907int main() | 4345 4346int main() |
2908{ WINDOW *my_wins[3]; 2909 PANEL *my_panels[3]; 2910 int lines = 10, cols = 40, y = 2, x = 4, i; | 4347{ WINDOW *my_wins[3]; 4348 PANEL *my_panels[3]; 4349 int lines = 10, cols = 40, y = 2, x = 4, i; |
2911 | 4350 |
2912 initscr(); 2913 cbreak(); 2914 noecho(); | 4351 initscr(); 4352 cbreak(); 4353 noecho(); |
2915 | 4354 |
2916 /* Create windows for the panels */ 2917 my_wins[0] = newwin(lines, cols, y, x); 2918 my_wins[1] = newwin(lines, cols, y + 1, x + 5); 2919 my_wins[2] = newwin(lines, cols, y + 2, x + 10); | 4355 /* Create windows for the panels */ 4356 my_wins[0] = newwin(lines, cols, y, x); 4357 my_wins[1] = newwin(lines, cols, y + 1, x + 5); 4358 my_wins[2] = newwin(lines, cols, y + 2, x + 10); |
2920 | 4359 |
2921 /* 2922 * Create borders around the windows so that you can see the effect 2923 * of panels 2924 */ 2925 for(i = 0; i < 3; ++i) 2926 box(my_wins[i], 0, 0); | 4360 /* 4361 * Create borders around the windows so that you can see the effect 4362 * of panels 4363 */ 4364 for(i = 0; i < 3; ++i) 4365 box(my_wins[i], 0, 0); |
2927 | 4366 |
2928 /* Attach a panel to each window */ /* Order is bottom up */ 2929 my_panels[0] = new_panel(my_wins[0]); /* Push 0, order: stdscr-0 */ 2930 my_panels[1] = new_panel(my_wins[1]); /* Push 1, order: stdscr-0-1 */ 2931 my_panels[2] = new_panel(my_wins[2]); /* Push 2, order: stdscr-0-1-2 */ | 4367 /* Attach a panel to each window */ /* Order is bottom up */ 4368 my_panels[0] = new_panel(my_wins[0]); /* Push 0, order: stdscr-0 */ 4369 my_panels[1] = new_panel(my_wins[1]); /* Push 1, order: stdscr-0-1 */ 4370 my_panels[2] = new_panel(my_wins[2]); /* Push 2, order: stdscr-0-1-2 */ |
2932 | 4371 |
2933 /* Update the stacking order. 2nd panel will be on top */ 2934 update_panels(); | 4372 /* Update the stacking order. 2nd panel will be on top */ 4373 update_panels(); |
2935 | 4374 |
2936 /* Show it on the screen */ 2937 doupdate(); 2938 2939 getch(); 2940 endwin(); | 4375 /* Show it on the screen */ 4376 doupdate(); 4377 4378 getch(); 4379 endwin(); |
2941} | 4380} |
2942</span></font> 2943</pre></td> 2944</tr> 2945</table> 2946</div> 2947<p>As you can see, above program follows a simple flow as 2948explained. The windows are created with newwin() and then they are 2949attached to panels with new_panel(). As we attach one panel after 2950another, the stack of panels gets updated. To put them on screen 2951update_panels() and doupdate() are called.</p> 2952</div> 2953<div class="SECT2"> 2954<hr> 2955<h3 class="SECT2"><a name="PANELBROWSING" id="PANELBROWSING">16.3. 2956Panel Window Browsing</a></h3> 2957<p>A slightly complicated example is given below. This program 2958creates 3 windows which can be cycled through using tab. Have a 2959look at the code.</p> 2960<div class="EXAMPLE"><a name="PPABR" id="PPABR"></a> 2961<p><b>Example 15. Panel Window Browsing Example</b></p> 2962<table border="0" bgcolor="#E0E0E0" width="100%"> 2963<tr> 2964<td> 2965<pre class="PROGRAMLISTING"> 2966<font color="#000000"><span class= 2967"INLINEMEDIAOBJECT">#include <panel.h> | 4381</SPAN 4382></PRE 4383></DIV 4384><P 4385>As you can see, above program follows a simple flow as explained. The windows 4386are created with newwin() and then they are attached to panels with new_panel(). 4387As we attach one panel after another, the stack of panels gets updated. To put 4388them on screen update_panels() and doupdate() are called.</P 4389></DIV 4390><DIV 4391CLASS="SECT2" 4392><HR><H3 4393CLASS="SECT2" 4394><A 4395NAME="PANELBROWSING" 4396>16.3. Panel Window Browsing</A 4397></H3 4398><P 4399>A slightly complicated example is given below. This program creates 3 4400windows which can be cycled through using tab. Have a look at the code.</P 4401><DIV 4402CLASS="EXAMPLE" 4403><A 4404NAME="PPABR" 4405></A 4406><P 4407><B 4408>Example 15. Panel Window Browsing Example </B 4409></P 4410><PRE 4411CLASS="PROGRAMLISTING" 4412><SPAN 4413CLASS="INLINEMEDIAOBJECT" 4414>#include <panel.h> |
2968 2969#define NLINES 10 2970#define NCOLS 40 2971 2972void init_wins(WINDOW **wins, int n); 2973void win_show(WINDOW *win, char *label, int label_color); 2974void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); 2975 2976int main() | 4415 4416#define NLINES 10 4417#define NCOLS 40 4418 4419void init_wins(WINDOW **wins, int n); 4420void win_show(WINDOW *win, char *label, int label_color); 4421void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); 4422 4423int main() |
2977{ WINDOW *my_wins[3]; 2978 PANEL *my_panels[3]; 2979 PANEL *top; 2980 int ch; | 4424{ WINDOW *my_wins[3]; 4425 PANEL *my_panels[3]; 4426 PANEL *top; 4427 int ch; |
2981 | 4428 |
2982 /* Initialize curses */ 2983 initscr(); 2984 start_color(); 2985 cbreak(); 2986 noecho(); 2987 keypad(stdscr, TRUE); | 4429 /* Initialize curses */ 4430 initscr(); 4431 start_color(); 4432 cbreak(); 4433 noecho(); 4434 keypad(stdscr, TRUE); |
2988 | 4435 |
2989 /* Initialize all the colors */ 2990 init_pair(1, COLOR_RED, COLOR_BLACK); 2991 init_pair(2, COLOR_GREEN, COLOR_BLACK); 2992 init_pair(3, COLOR_BLUE, COLOR_BLACK); 2993 init_pair(4, COLOR_CYAN, COLOR_BLACK); | 4436 /* Initialize all the colors */ 4437 init_pair(1, COLOR_RED, COLOR_BLACK); 4438 init_pair(2, COLOR_GREEN, COLOR_BLACK); 4439 init_pair(3, COLOR_BLUE, COLOR_BLACK); 4440 init_pair(4, COLOR_CYAN, COLOR_BLACK); |
2994 | 4441 |
2995 init_wins(my_wins, 3); 2996 2997 /* Attach a panel to each window */ /* Order is bottom up */ 2998 my_panels[0] = new_panel(my_wins[0]); /* Push 0, order: stdscr-0 */ 2999 my_panels[1] = new_panel(my_wins[1]); /* Push 1, order: stdscr-0-1 */ 3000 my_panels[2] = new_panel(my_wins[2]); /* Push 2, order: stdscr-0-1-2 */ | 4442 init_wins(my_wins, 3); 4443 4444 /* Attach a panel to each window */ /* Order is bottom up */ 4445 my_panels[0] = new_panel(my_wins[0]); /* Push 0, order: stdscr-0 */ 4446 my_panels[1] = new_panel(my_wins[1]); /* Push 1, order: stdscr-0-1 */ 4447 my_panels[2] = new_panel(my_wins[2]); /* Push 2, order: stdscr-0-1-2 */ |
3001 | 4448 |
3002 /* Set up the user pointers to the next panel */ 3003 set_panel_userptr(my_panels[0], my_panels[1]); 3004 set_panel_userptr(my_panels[1], my_panels[2]); 3005 set_panel_userptr(my_panels[2], my_panels[0]); | 4449 /* Set up the user pointers to the next panel */ 4450 set_panel_userptr(my_panels[0], my_panels[1]); 4451 set_panel_userptr(my_panels[1], my_panels[2]); 4452 set_panel_userptr(my_panels[2], my_panels[0]); |
3006 | 4453 |
3007 /* Update the stacking order. 2nd panel will be on top */ 3008 update_panels(); | 4454 /* Update the stacking order. 2nd panel will be on top */ 4455 update_panels(); |
3009 | 4456 |
3010 /* Show it on the screen */ 3011 attron(COLOR_PAIR(4)); 3012 mvprintw(LINES - 2, 0, "Use tab to browse through the windows (F1 to Exit)"); 3013 attroff(COLOR_PAIR(4)); 3014 doupdate(); | 4457 /* Show it on the screen */ 4458 attron(COLOR_PAIR(4)); 4459 mvprintw(LINES - 2, 0, "Use tab to browse through the windows (F1 to Exit)"); 4460 attroff(COLOR_PAIR(4)); 4461 doupdate(); |
3015 | 4462 |
3016 top = my_panels[2]; 3017 while((ch = getch()) != KEY_F(1)) 3018 { switch(ch) 3019 { case 9: 3020 top = (PANEL *)panel_userptr(top); 3021 top_panel(top); 3022 break; 3023 } 3024 update_panels(); 3025 doupdate(); 3026 } 3027 endwin(); 3028 return 0; | 4463 top = my_panels[2]; 4464 while((ch = getch()) != KEY_F(1)) 4465 { switch(ch) 4466 { case 9: 4467 top = (PANEL *)panel_userptr(top); 4468 top_panel(top); 4469 break; 4470 } 4471 update_panels(); 4472 doupdate(); 4473 } 4474 endwin(); 4475 return 0; |
3029} 3030 3031/* Put all the windows */ 3032void init_wins(WINDOW **wins, int n) | 4476} 4477 4478/* Put all the windows */ 4479void init_wins(WINDOW **wins, int n) |
3033{ int x, y, i; 3034 char label[80]; | 4480{ int x, y, i; 4481 char label[80]; |
3035 | 4482 |
3036 y = 2; 3037 x = 10; 3038 for(i = 0; i < n; ++i) 3039 { wins[i] = newwin(NLINES, NCOLS, y, x); 3040 sprintf(label, "Window Number %d", i + 1); 3041 win_show(wins[i], label, i + 1); 3042 y += 3; 3043 x += 7; 3044 } | 4483 y = 2; 4484 x = 10; 4485 for(i = 0; i < n; ++i) 4486 { wins[i] = newwin(NLINES, NCOLS, y, x); 4487 sprintf(label, "Window Number %d", i + 1); 4488 win_show(wins[i], label, i + 1); 4489 y += 3; 4490 x += 7; 4491 } |
3045} 3046 3047/* Show the window with a border and a label */ 3048void win_show(WINDOW *win, char *label, int label_color) | 4492} 4493 4494/* Show the window with a border and a label */ 4495void win_show(WINDOW *win, char *label, int label_color) |
3049{ int startx, starty, height, width; | 4496{ int startx, starty, height, width; |
3050 | 4497 |
3051 getbegyx(win, starty, startx); 3052 getmaxyx(win, height, width); | 4498 getbegyx(win, starty, startx); 4499 getmaxyx(win, height, width); |
3053 | 4500 |
3054 box(win, 0, 0); 3055 mvwaddch(win, 2, 0, ACS_LTEE); 3056 mvwhline(win, 2, 1, ACS_HLINE, width - 2); 3057 mvwaddch(win, 2, width - 1, ACS_RTEE); 3058 3059 print_in_middle(win, 1, 0, width, label, COLOR_PAIR(label_color)); | 4501 box(win, 0, 0); 4502 mvwaddch(win, 2, 0, ACS_LTEE); 4503 mvwhline(win, 2, 1, ACS_HLINE, width - 2); 4504 mvwaddch(win, 2, width - 1, ACS_RTEE); 4505 4506 print_in_middle(win, 1, 0, width, label, COLOR_PAIR(label_color)); |
3060} 3061 3062void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) | 4507} 4508 4509void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) |
3063{ int length, x, y; 3064 float temp; | 4510{ int length, x, y; 4511 float temp; |
3065 | 4512 |
3066 if(win == NULL) 3067 win = stdscr; 3068 getyx(win, y, x); 3069 if(startx != 0) 3070 x = startx; 3071 if(starty != 0) 3072 y = starty; 3073 if(width == 0) 3074 width = 80; | 4513 if(win == NULL) 4514 win = stdscr; 4515 getyx(win, y, x); 4516 if(startx != 0) 4517 x = startx; 4518 if(starty != 0) 4519 y = starty; 4520 if(width == 0) 4521 width = 80; |
3075 | 4522 |
3076 length = strlen(string); 3077 temp = (width - length)/ 2; 3078 x = startx + (int)temp; 3079 wattron(win, color); 3080 mvwprintw(win, y, x, "%s", string); 3081 wattroff(win, color); 3082 refresh(); 3083}</span></font> 3084</pre></td> 3085</tr> 3086</table> 3087</div> 3088</div> 3089<div class="SECT2"> 3090<hr> 3091<h3 class="SECT2"><a name="USERPTRUSING" id="USERPTRUSING">16.4. 3092Using User Pointers</a></h3> 3093<p>In the above example I used user pointers to find out the next 3094window in the cycle. We can attach custom information to the panel 3095by specifying a user pointer, which can point to any information 3096you want to store. In this case I stored the pointer to the next 3097panel in the cycle. User pointer for a panel can be set with the 3098function <var class="LITERAL">set_panel_userptr()</var>. It can be 3099accessed using the function <var class= 3100"LITERAL">panel_userptr()</var> which will return the user pointer 3101for the panel given as argument. After finding the next panel in 3102the cycle It's brought to the top by the function top_panel(). This 3103function brings the panel given as argument to the top of the panel 3104stack.</p> 3105</div> 3106<div class="SECT2"> 3107<hr> 3108<h3 class="SECT2"><a name="PANELMOVERESIZE" id= 3109"PANELMOVERESIZE">16.5. Moving and Resizing Panels</a></h3> 3110<p>The function <var class="LITERAL">move_panel()</var> can be used 3111to move a panel to the desired location. It does not change the 3112position of the panel in the stack. Make sure that you use 3113move_panel() instead mvwin() on the window associated with the 3114panel.</p> 3115<p>Resizing a panel is slightly complex. There is no straight 3116forward function just to resize the window associated with a panel. 3117A solution to resize a panel is to create a new window with the 3118desired sizes, change the window associated with the panel using 3119replace_panel(). Don't forget to delete the old window. The window 3120associated with a panel can be found by using the function 3121panel_window().</p> 3122<p>The following program shows these concepts, in supposedly simple 3123program. You can cycle through the window with <TAB> as 3124usual. To resize or move the active panel press 'r' for resize 'm' 3125for moving. Then use arrow keys to resize or move it to the desired 3126way and press enter to end your resizing or moving. This example 3127makes use of user data to get the required data to do the 3128operations.</p> 3129<div class="EXAMPLE"><a name="PPARE" id="PPARE"></a> 3130<p><b>Example 16. Panel Moving and Resizing example</b></p> 3131<table border="0" bgcolor="#E0E0E0" width="100%"> 3132<tr> 3133<td> 3134<pre class="PROGRAMLISTING"> 3135<font color="#000000"><span class= 3136"INLINEMEDIAOBJECT">#include <panel.h> | 4523 length = strlen(string); 4524 temp = (width - length)/ 2; 4525 x = startx + (int)temp; 4526 wattron(win, color); 4527 mvwprintw(win, y, x, "%s", string); 4528 wattroff(win, color); 4529 refresh(); 4530}</SPAN 4531></PRE 4532></DIV 4533></DIV 4534><DIV 4535CLASS="SECT2" 4536><HR><H3 4537CLASS="SECT2" 4538><A 4539NAME="USERPTRUSING" 4540>16.4. Using User Pointers</A 4541></H3 4542><P 4543>In the above example I used user pointers to find out the next window in the 4544cycle. We can attach custom information to the panel by specifying a user 4545pointer, which can point to any information you want to store. In this case I 4546stored the pointer to the next panel in the cycle. User pointer for a panel can 4547be set with the function <TT 4548CLASS="LITERAL" 4549> set_panel_userptr()</TT 4550>. 4551It can be accessed using the function <TT 4552CLASS="LITERAL" 4553>panel_userptr()</TT 4554> which will return the user pointer for the panel given as 4555argument. After finding the next panel in the cycle It's brought to the top by 4556the function top_panel(). This function brings the panel given as argument to 4557the top of the panel stack. </P 4558></DIV 4559><DIV 4560CLASS="SECT2" 4561><HR><H3 4562CLASS="SECT2" 4563><A 4564NAME="PANELMOVERESIZE" 4565>16.5. Moving and Resizing Panels</A 4566></H3 4567><P 4568>The function <TT 4569CLASS="LITERAL" 4570>move_panel()</TT 4571> can be used to move a 4572panel to the desired location. It does not change the position of the panel in 4573the stack. Make sure that you use move_panel() instead mvwin() on the window 4574associated with the panel.</P 4575><P 4576>Resizing a panel is slightly complex. There is no straight forward function 4577just to resize the window associated with a panel. A solution to resize a panel 4578is to create a new window with the desired sizes, change the window associated 4579with the panel using replace_panel(). Don't forget to delete the old window. The 4580window associated with a panel can be found by using the function 4581panel_window().</P 4582><P 4583>The following program shows these concepts, in supposedly simple program. You 4584can cycle through the window with <TAB> as usual. To resize or move the 4585active panel press 'r' for resize 'm' for moving. Then use arrow keys to resize 4586or move it to the desired way and press enter to end your resizing or moving. 4587This example makes use of user data to get the required data to do the 4588operations. </P 4589><DIV 4590CLASS="EXAMPLE" 4591><A 4592NAME="PPARE" 4593></A 4594><P 4595><B 4596>Example 16. Panel Moving and Resizing example </B 4597></P 4598><PRE 4599CLASS="PROGRAMLISTING" 4600><SPAN 4601CLASS="INLINEMEDIAOBJECT" 4602>#include <panel.h> |
3137 3138typedef struct _PANEL_DATA { | 4603 4604typedef struct _PANEL_DATA { |
3139 int x, y, w, h; 3140 char label[80]; 3141 int label_color; 3142 PANEL *next; | 4605 int x, y, w, h; 4606 char label[80]; 4607 int label_color; 4608 PANEL *next; |
3143}PANEL_DATA; 3144 3145#define NLINES 10 3146#define NCOLS 40 3147 3148void init_wins(WINDOW **wins, int n); 3149void win_show(WINDOW *win, char *label, int label_color); 3150void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); 3151void set_user_ptrs(PANEL **panels, int n); 3152 3153int main() | 4609}PANEL_DATA; 4610 4611#define NLINES 10 4612#define NCOLS 40 4613 4614void init_wins(WINDOW **wins, int n); 4615void win_show(WINDOW *win, char *label, int label_color); 4616void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); 4617void set_user_ptrs(PANEL **panels, int n); 4618 4619int main() |
3154{ WINDOW *my_wins[3]; 3155 PANEL *my_panels[3]; 3156 PANEL_DATA *top; 3157 PANEL *stack_top; 3158 WINDOW *temp_win, *old_win; 3159 int ch; 3160 int newx, newy, neww, newh; 3161 int size = FALSE, move = FALSE; | 4620{ WINDOW *my_wins[3]; 4621 PANEL *my_panels[3]; 4622 PANEL_DATA *top; 4623 PANEL *stack_top; 4624 WINDOW *temp_win, *old_win; 4625 int ch; 4626 int newx, newy, neww, newh; 4627 int size = FALSE, move = FALSE; |
3162 | 4628 |
3163 /* Initialize curses */ 3164 initscr(); 3165 start_color(); 3166 cbreak(); 3167 noecho(); 3168 keypad(stdscr, TRUE); | 4629 /* Initialize curses */ 4630 initscr(); 4631 start_color(); 4632 cbreak(); 4633 noecho(); 4634 keypad(stdscr, TRUE); |
3169 | 4635 |
3170 /* Initialize all the colors */ 3171 init_pair(1, COLOR_RED, COLOR_BLACK); 3172 init_pair(2, COLOR_GREEN, COLOR_BLACK); 3173 init_pair(3, COLOR_BLUE, COLOR_BLACK); 3174 init_pair(4, COLOR_CYAN, COLOR_BLACK); | 4636 /* Initialize all the colors */ 4637 init_pair(1, COLOR_RED, COLOR_BLACK); 4638 init_pair(2, COLOR_GREEN, COLOR_BLACK); 4639 init_pair(3, COLOR_BLUE, COLOR_BLACK); 4640 init_pair(4, COLOR_CYAN, COLOR_BLACK); |
3175 | 4641 |
3176 init_wins(my_wins, 3); 3177 3178 /* Attach a panel to each window */ /* Order is bottom up */ 3179 my_panels[0] = new_panel(my_wins[0]); /* Push 0, order: stdscr-0 */ 3180 my_panels[1] = new_panel(my_wins[1]); /* Push 1, order: stdscr-0-1 */ 3181 my_panels[2] = new_panel(my_wins[2]); /* Push 2, order: stdscr-0-1-2 */ | 4642 init_wins(my_wins, 3); 4643 4644 /* Attach a panel to each window */ /* Order is bottom up */ 4645 my_panels[0] = new_panel(my_wins[0]); /* Push 0, order: stdscr-0 */ 4646 my_panels[1] = new_panel(my_wins[1]); /* Push 1, order: stdscr-0-1 */ 4647 my_panels[2] = new_panel(my_wins[2]); /* Push 2, order: stdscr-0-1-2 */ |
3182 | 4648 |
3183 set_user_ptrs(my_panels, 3); 3184 /* Update the stacking order. 2nd panel will be on top */ 3185 update_panels(); | 4649 set_user_ptrs(my_panels, 3); 4650 /* Update the stacking order. 2nd panel will be on top */ 4651 update_panels(); |
3186 | 4652 |
3187 /* Show it on the screen */ 3188 attron(COLOR_PAIR(4)); 3189 mvprintw(LINES - 3, 0, "Use 'm' for moving, 'r' for resizing"); 3190 mvprintw(LINES - 2, 0, "Use tab to browse through the windows (F1 to Exit)"); 3191 attroff(COLOR_PAIR(4)); 3192 doupdate(); | 4653 /* Show it on the screen */ 4654 attron(COLOR_PAIR(4)); 4655 mvprintw(LINES - 3, 0, "Use 'm' for moving, 'r' for resizing"); 4656 mvprintw(LINES - 2, 0, "Use tab to browse through the windows (F1 to Exit)"); 4657 attroff(COLOR_PAIR(4)); 4658 doupdate(); |
3193 | 4659 |
3194 stack_top = my_panels[2]; 3195 top = (PANEL_DATA *)panel_userptr(stack_top); 3196 newx = top->x; 3197 newy = top->y; 3198 neww = top->w; 3199 newh = top->h; 3200 while((ch = getch()) != KEY_F(1)) 3201 { switch(ch) 3202 { case 9: /* Tab */ 3203 top = (PANEL_DATA *)panel_userptr(stack_top); 3204 top_panel(top->next); 3205 stack_top = top->next; 3206 top = (PANEL_DATA *)panel_userptr(stack_top); 3207 newx = top->x; 3208 newy = top->y; 3209 neww = top->w; 3210 newh = top->h; 3211 break; 3212 case 'r': /* Re-Size*/ 3213 size = TRUE; 3214 attron(COLOR_PAIR(4)); 3215 mvprintw(LINES - 4, 0, "Entered Resizing :Use Arrow Keys to resize and press <ENTER> to end resizing"); 3216 refresh(); 3217 attroff(COLOR_PAIR(4)); 3218 break; 3219 case 'm': /* Move */ 3220 attron(COLOR_PAIR(4)); 3221 mvprintw(LINES - 4, 0, "Entered Moving: Use Arrow Keys to Move and press <ENTER> to end moving"); 3222 refresh(); 3223 attroff(COLOR_PAIR(4)); 3224 move = TRUE; 3225 break; 3226 case KEY_LEFT: 3227 if(size == TRUE) 3228 { --newx; 3229 ++neww; 3230 } 3231 if(move == TRUE) 3232 --newx; 3233 break; 3234 case KEY_RIGHT: 3235 if(size == TRUE) 3236 { ++newx; 3237 --neww; 3238 } 3239 if(move == TRUE) 3240 ++newx; 3241 break; 3242 case KEY_UP: 3243 if(size == TRUE) 3244 { --newy; 3245 ++newh; 3246 } 3247 if(move == TRUE) 3248 --newy; 3249 break; 3250 case KEY_DOWN: 3251 if(size == TRUE) 3252 { ++newy; 3253 --newh; 3254 } 3255 if(move == TRUE) 3256 ++newy; 3257 break; 3258 case 10: /* Enter */ 3259 move(LINES - 4, 0); 3260 clrtoeol(); 3261 refresh(); 3262 if(size == TRUE) 3263 { old_win = panel_window(stack_top); 3264 temp_win = newwin(newh, neww, newy, newx); 3265 replace_panel(stack_top, temp_win); 3266 win_show(temp_win, top->label, top->label_color); 3267 delwin(old_win); 3268 size = FALSE; 3269 } 3270 if(move == TRUE) 3271 { move_panel(stack_top, newy, newx); 3272 move = FALSE; 3273 } 3274 break; 3275 3276 } 3277 attron(COLOR_PAIR(4)); 3278 mvprintw(LINES - 3, 0, "Use 'm' for moving, 'r' for resizing"); 3279 mvprintw(LINES - 2, 0, "Use tab to browse through the windows (F1 to Exit)"); 3280 attroff(COLOR_PAIR(4)); 3281 refresh(); 3282 update_panels(); 3283 doupdate(); 3284 } 3285 endwin(); 3286 return 0; | 4660 stack_top = my_panels[2]; 4661 top = (PANEL_DATA *)panel_userptr(stack_top); 4662 newx = top->x; 4663 newy = top->y; 4664 neww = top->w; 4665 newh = top->h; 4666 while((ch = getch()) != KEY_F(1)) 4667 { switch(ch) 4668 { case 9: /* Tab */ 4669 top = (PANEL_DATA *)panel_userptr(stack_top); 4670 top_panel(top->next); 4671 stack_top = top->next; 4672 top = (PANEL_DATA *)panel_userptr(stack_top); 4673 newx = top->x; 4674 newy = top->y; 4675 neww = top->w; 4676 newh = top->h; 4677 break; 4678 case 'r': /* Re-Size*/ 4679 size = TRUE; 4680 attron(COLOR_PAIR(4)); 4681 mvprintw(LINES - 4, 0, "Entered Resizing :Use Arrow Keys to resize and press <ENTER> to end resizing"); 4682 refresh(); 4683 attroff(COLOR_PAIR(4)); 4684 break; 4685 case 'm': /* Move */ 4686 attron(COLOR_PAIR(4)); 4687 mvprintw(LINES - 4, 0, "Entered Moving: Use Arrow Keys to Move and press <ENTER> to end moving"); 4688 refresh(); 4689 attroff(COLOR_PAIR(4)); 4690 move = TRUE; 4691 break; 4692 case KEY_LEFT: 4693 if(size == TRUE) 4694 { --newx; 4695 ++neww; 4696 } 4697 if(move == TRUE) 4698 --newx; 4699 break; 4700 case KEY_RIGHT: 4701 if(size == TRUE) 4702 { ++newx; 4703 --neww; 4704 } 4705 if(move == TRUE) 4706 ++newx; 4707 break; 4708 case KEY_UP: 4709 if(size == TRUE) 4710 { --newy; 4711 ++newh; 4712 } 4713 if(move == TRUE) 4714 --newy; 4715 break; 4716 case KEY_DOWN: 4717 if(size == TRUE) 4718 { ++newy; 4719 --newh; 4720 } 4721 if(move == TRUE) 4722 ++newy; 4723 break; 4724 case 10: /* Enter */ 4725 move(LINES - 4, 0); 4726 clrtoeol(); 4727 refresh(); 4728 if(size == TRUE) 4729 { old_win = panel_window(stack_top); 4730 temp_win = newwin(newh, neww, newy, newx); 4731 replace_panel(stack_top, temp_win); 4732 win_show(temp_win, top->label, top->label_color); 4733 delwin(old_win); 4734 size = FALSE; 4735 } 4736 if(move == TRUE) 4737 { move_panel(stack_top, newy, newx); 4738 move = FALSE; 4739 } 4740 break; 4741 4742 } 4743 attron(COLOR_PAIR(4)); 4744 mvprintw(LINES - 3, 0, "Use 'm' for moving, 'r' for resizing"); 4745 mvprintw(LINES - 2, 0, "Use tab to browse through the windows (F1 to Exit)"); 4746 attroff(COLOR_PAIR(4)); 4747 refresh(); 4748 update_panels(); 4749 doupdate(); 4750 } 4751 endwin(); 4752 return 0; |
3287} 3288 3289/* Put all the windows */ 3290void init_wins(WINDOW **wins, int n) | 4753} 4754 4755/* Put all the windows */ 4756void init_wins(WINDOW **wins, int n) |
3291{ int x, y, i; 3292 char label[80]; | 4757{ int x, y, i; 4758 char label[80]; |
3293 | 4759 |
3294 y = 2; 3295 x = 10; 3296 for(i = 0; i < n; ++i) 3297 { wins[i] = newwin(NLINES, NCOLS, y, x); 3298 sprintf(label, "Window Number %d", i + 1); 3299 win_show(wins[i], label, i + 1); 3300 y += 3; 3301 x += 7; 3302 } | 4760 y = 2; 4761 x = 10; 4762 for(i = 0; i < n; ++i) 4763 { wins[i] = newwin(NLINES, NCOLS, y, x); 4764 sprintf(label, "Window Number %d", i + 1); 4765 win_show(wins[i], label, i + 1); 4766 y += 3; 4767 x += 7; 4768 } |
3303} 3304 3305/* Set the PANEL_DATA structures for individual panels */ 3306void set_user_ptrs(PANEL **panels, int n) | 4769} 4770 4771/* Set the PANEL_DATA structures for individual panels */ 4772void set_user_ptrs(PANEL **panels, int n) |
3307{ PANEL_DATA *ptrs; 3308 WINDOW *win; 3309 int x, y, w, h, i; 3310 char temp[80]; 3311 3312 ptrs = (PANEL_DATA *)calloc(n, sizeof(PANEL_DATA)); | 4773{ PANEL_DATA *ptrs; 4774 WINDOW *win; 4775 int x, y, w, h, i; 4776 char temp[80]; 4777 4778 ptrs = (PANEL_DATA *)calloc(n, sizeof(PANEL_DATA)); |
3313 | 4779 |
3314 for(i = 0;i < n; ++i) 3315 { win = panel_window(panels[i]); 3316 getbegyx(win, y, x); 3317 getmaxyx(win, h, w); 3318 ptrs[i].x = x; 3319 ptrs[i].y = y; 3320 ptrs[i].w = w; 3321 ptrs[i].h = h; 3322 sprintf(temp, "Window Number %d", i + 1); 3323 strcpy(ptrs[i].label, temp); 3324 ptrs[i].label_color = i + 1; 3325 if(i + 1 == n) 3326 ptrs[i].next = panels[0]; 3327 else 3328 ptrs[i].next = panels[i + 1]; 3329 set_panel_userptr(panels[i], &ptrs[i]); 3330 } | 4780 for(i = 0;i < n; ++i) 4781 { win = panel_window(panels[i]); 4782 getbegyx(win, y, x); 4783 getmaxyx(win, h, w); 4784 ptrs[i].x = x; 4785 ptrs[i].y = y; 4786 ptrs[i].w = w; 4787 ptrs[i].h = h; 4788 sprintf(temp, "Window Number %d", i + 1); 4789 strcpy(ptrs[i].label, temp); 4790 ptrs[i].label_color = i + 1; 4791 if(i + 1 == n) 4792 ptrs[i].next = panels[0]; 4793 else 4794 ptrs[i].next = panels[i + 1]; 4795 set_panel_userptr(panels[i], &ptrs[i]); 4796 } |
3331} 3332 3333/* Show the window with a border and a label */ 3334void win_show(WINDOW *win, char *label, int label_color) | 4797} 4798 4799/* Show the window with a border and a label */ 4800void win_show(WINDOW *win, char *label, int label_color) |
3335{ int startx, starty, height, width; | 4801{ int startx, starty, height, width; |
3336 | 4802 |
3337 getbegyx(win, starty, startx); 3338 getmaxyx(win, height, width); | 4803 getbegyx(win, starty, startx); 4804 getmaxyx(win, height, width); |
3339 | 4805 |
3340 box(win, 0, 0); 3341 mvwaddch(win, 2, 0, ACS_LTEE); 3342 mvwhline(win, 2, 1, ACS_HLINE, width - 2); 3343 mvwaddch(win, 2, width - 1, ACS_RTEE); 3344 3345 print_in_middle(win, 1, 0, width, label, COLOR_PAIR(label_color)); | 4806 box(win, 0, 0); 4807 mvwaddch(win, 2, 0, ACS_LTEE); 4808 mvwhline(win, 2, 1, ACS_HLINE, width - 2); 4809 mvwaddch(win, 2, width - 1, ACS_RTEE); 4810 4811 print_in_middle(win, 1, 0, width, label, COLOR_PAIR(label_color)); |
3346} 3347 3348void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) | 4812} 4813 4814void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) |
3349{ int length, x, y; 3350 float temp; | 4815{ int length, x, y; 4816 float temp; |
3351 | 4817 |
3352 if(win == NULL) 3353 win = stdscr; 3354 getyx(win, y, x); 3355 if(startx != 0) 3356 x = startx; 3357 if(starty != 0) 3358 y = starty; 3359 if(width == 0) 3360 width = 80; | 4818 if(win == NULL) 4819 win = stdscr; 4820 getyx(win, y, x); 4821 if(startx != 0) 4822 x = startx; 4823 if(starty != 0) 4824 y = starty; 4825 if(width == 0) 4826 width = 80; |
3361 | 4827 |
3362 length = strlen(string); 3363 temp = (width - length)/ 2; 3364 x = startx + (int)temp; 3365 wattron(win, color); 3366 mvwprintw(win, y, x, "%s", string); 3367 wattroff(win, color); 3368 refresh(); 3369}</span></font> 3370</pre></td> 3371</tr> 3372</table> 3373</div> 3374<p>Concentrate on the main while loop. Once it finds out the type 3375of key pressed, it takes appropriate action. If 'r' is pressed 3376resizing mode is started. After this the new sizes are updated as 3377the user presses the arrow keys. When the user presses 3378<ENTER> present selection ends and panel is resized by using 3379the concept explained. While in resizing mode the program doesn't 3380show how the window is getting resized. It's left as an exercise to 3381the reader to print a dotted border while it gets resized to a new 3382position.</p> 3383<p>When the user presses 'm' the move mode starts. This is a bit 3384simpler than resizing. As the arrow keys are pressed the new 3385position is updated and pressing of <ENTER> causes the panel 3386to be moved by calling the function move_panel().</p> 3387<p>In this program the user data which is represented as 3388PANEL_DATA, plays very important role in finding the associated 3389information with a panel. As written in the comments, the 3390PANEL_DATA stores the panel sizes, label, label color and a pointer 3391to the next panel in the cycle.</p> 3392</div> 3393<div class="SECT2"> 3394<hr> 3395<h3 class="SECT2"><a name="PANELSHOWHIDE" id="PANELSHOWHIDE">16.6. 3396Hiding and Showing Panels</a></h3> 3397<p>A Panel can be hidden by using the function hide_panel(). This 3398function merely removes it form the stack of panels, thus hiding it 3399on the screen once you do update_panels() and doupdate(). It 3400doesn't destroy the PANEL structure associated with the hidden 3401panel. It can be shown again by using the show_panel() 3402function.</p> 3403<p>The following program shows the hiding of panels. Press 'a' or 3404'b' or 'c' to show or hide first, second and third windows 3405respectively. It uses a user data with a small variable hide, which 3406keeps track of whether the window is hidden or not. For some reason 3407the function <var class="LITERAL">panel_hidden()</var> which tells 3408whether a panel is hidden or not is not working. A bug report was 3409also presented by Michael Andres <a href= 3410"http://www.geocrawler.com/archives/3/344/1999/9/0/2643549/" 3411target="_top">here</a></p> 3412<div class="EXAMPLE"><a name="PPAHI" id="PPAHI"></a> 3413<p><b>Example 17. Panel Hiding and Showing example</b></p> 3414<table border="0" bgcolor="#E0E0E0" width="100%"> 3415<tr> 3416<td> 3417<pre class="PROGRAMLISTING"> 3418<font color="#000000"><span class= 3419"INLINEMEDIAOBJECT">#include <panel.h> | 4828 length = strlen(string); 4829 temp = (width - length)/ 2; 4830 x = startx + (int)temp; 4831 wattron(win, color); 4832 mvwprintw(win, y, x, "%s", string); 4833 wattroff(win, color); 4834 refresh(); 4835}</SPAN 4836></PRE 4837></DIV 4838><P 4839>Concentrate on the main while loop. Once it finds out the type of key pressed, 4840it takes appropriate action. If 'r' is pressed resizing mode is started. After 4841this the new sizes are updated as the user presses the arrow keys. When the user 4842presses <ENTER> present selection ends and panel is resized by using the 4843concept explained. While in resizing mode the program doesn't show how the 4844window is getting resized. It's left as an exercise to the reader to print a 4845dotted border while it gets resized to a new position. </P 4846><P 4847>When the user presses 'm' the move mode starts. This is a bit simpler than 4848resizing. As the arrow keys are pressed the new position is updated and 4849pressing of <ENTER> causes the panel to be moved by calling the function 4850move_panel().</P 4851><P 4852>In this program the user data which is represented as PANEL_DATA, plays very 4853important role in finding the associated information with a panel. As written in 4854the comments, the PANEL_DATA stores the panel sizes, label, label color and a 4855pointer to the next panel in the cycle.</P 4856></DIV 4857><DIV 4858CLASS="SECT2" 4859><HR><H3 4860CLASS="SECT2" 4861><A 4862NAME="PANELSHOWHIDE" 4863>16.6. Hiding and Showing Panels</A 4864></H3 4865><P 4866>A Panel can be hidden by using the function hide_panel(). This function merely 4867removes it form the stack of panels, thus hiding it on the screen once you do 4868update_panels() and doupdate(). It doesn't destroy the PANEL structure 4869associated with the hidden panel. It can be shown again by using the 4870show_panel() function.</P 4871><P 4872>The following program shows the hiding of panels. Press 'a' or 'b' or 'c' to 4873show or hide first, second and third windows respectively. It uses a user data 4874with a small variable hide, which keeps track of whether the window is hidden or 4875not. For some reason the function 4876<TT 4877CLASS="LITERAL" 4878>panel_hidden()</TT 4879> which tells whether a panel is 4880hidden or not is not working. A bug report was also presented by Michael Andres 4881<A 4882HREF="http://www.geocrawler.com/archives/3/344/1999/9/0/2643549/" 4883TARGET="_top" 4884> here</A 4885></P 4886><DIV 4887CLASS="EXAMPLE" 4888><A 4889NAME="PPAHI" 4890></A 4891><P 4892><B 4893>Example 17. Panel Hiding and Showing example </B 4894></P 4895><PRE 4896CLASS="PROGRAMLISTING" 4897><SPAN 4898CLASS="INLINEMEDIAOBJECT" 4899>#include <panel.h> |
3420 3421typedef struct _PANEL_DATA { | 4900 4901typedef struct _PANEL_DATA { |
3422 int hide; /* TRUE if panel is hidden */ | 4902 int hide; /* TRUE if panel is hidden */ |
3423}PANEL_DATA; 3424 3425#define NLINES 10 3426#define NCOLS 40 3427 3428void init_wins(WINDOW **wins, int n); 3429void win_show(WINDOW *win, char *label, int label_color); 3430void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); 3431 3432int main() | 4903}PANEL_DATA; 4904 4905#define NLINES 10 4906#define NCOLS 40 4907 4908void init_wins(WINDOW **wins, int n); 4909void win_show(WINDOW *win, char *label, int label_color); 4910void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); 4911 4912int main() |
3433{ WINDOW *my_wins[3]; 3434 PANEL *my_panels[3]; 3435 PANEL_DATA panel_datas[3]; 3436 PANEL_DATA *temp; 3437 int ch; | 4913{ WINDOW *my_wins[3]; 4914 PANEL *my_panels[3]; 4915 PANEL_DATA panel_datas[3]; 4916 PANEL_DATA *temp; 4917 int ch; |
3438 | 4918 |
3439 /* Initialize curses */ 3440 initscr(); 3441 start_color(); 3442 cbreak(); 3443 noecho(); 3444 keypad(stdscr, TRUE); | 4919 /* Initialize curses */ 4920 initscr(); 4921 start_color(); 4922 cbreak(); 4923 noecho(); 4924 keypad(stdscr, TRUE); |
3445 | 4925 |
3446 /* Initialize all the colors */ 3447 init_pair(1, COLOR_RED, COLOR_BLACK); 3448 init_pair(2, COLOR_GREEN, COLOR_BLACK); 3449 init_pair(3, COLOR_BLUE, COLOR_BLACK); 3450 init_pair(4, COLOR_CYAN, COLOR_BLACK); | 4926 /* Initialize all the colors */ 4927 init_pair(1, COLOR_RED, COLOR_BLACK); 4928 init_pair(2, COLOR_GREEN, COLOR_BLACK); 4929 init_pair(3, COLOR_BLUE, COLOR_BLACK); 4930 init_pair(4, COLOR_CYAN, COLOR_BLACK); |
3451 | 4931 |
3452 init_wins(my_wins, 3); 3453 3454 /* Attach a panel to each window */ /* Order is bottom up */ 3455 my_panels[0] = new_panel(my_wins[0]); /* Push 0, order: stdscr-0 */ 3456 my_panels[1] = new_panel(my_wins[1]); /* Push 1, order: stdscr-0-1 */ 3457 my_panels[2] = new_panel(my_wins[2]); /* Push 2, order: stdscr-0-1-2 */ | 4932 init_wins(my_wins, 3); 4933 4934 /* Attach a panel to each window */ /* Order is bottom up */ 4935 my_panels[0] = new_panel(my_wins[0]); /* Push 0, order: stdscr-0 */ 4936 my_panels[1] = new_panel(my_wins[1]); /* Push 1, order: stdscr-0-1 */ 4937 my_panels[2] = new_panel(my_wins[2]); /* Push 2, order: stdscr-0-1-2 */ |
3458 | 4938 |
3459 /* Initialize panel datas saying that nothing is hidden */ 3460 panel_datas[0].hide = FALSE; 3461 panel_datas[1].hide = FALSE; 3462 panel_datas[2].hide = FALSE; | 4939 /* Initialize panel datas saying that nothing is hidden */ 4940 panel_datas[0].hide = FALSE; 4941 panel_datas[1].hide = FALSE; 4942 panel_datas[2].hide = FALSE; |
3463 | 4943 |
3464 set_panel_userptr(my_panels[0], &panel_datas[0]); 3465 set_panel_userptr(my_panels[1], &panel_datas[1]); 3466 set_panel_userptr(my_panels[2], &panel_datas[2]); | 4944 set_panel_userptr(my_panels[0], &panel_datas[0]); 4945 set_panel_userptr(my_panels[1], &panel_datas[1]); 4946 set_panel_userptr(my_panels[2], &panel_datas[2]); |
3467 | 4947 |
3468 /* Update the stacking order. 2nd panel will be on top */ 3469 update_panels(); | 4948 /* Update the stacking order. 2nd panel will be on top */ 4949 update_panels(); |
3470 | 4950 |
3471 /* Show it on the screen */ 3472 attron(COLOR_PAIR(4)); 3473 mvprintw(LINES - 3, 0, "Show or Hide a window with 'a'(first window) 'b'(Second Window) 'c'(Third Window)"); 3474 mvprintw(LINES - 2, 0, "F1 to Exit"); | 4951 /* Show it on the screen */ 4952 attron(COLOR_PAIR(4)); 4953 mvprintw(LINES - 3, 0, "Show or Hide a window with 'a'(first window) 'b'(Second Window) 'c'(Third Window)"); 4954 mvprintw(LINES - 2, 0, "F1 to Exit"); |
3475 | 4955 |
3476 attroff(COLOR_PAIR(4)); 3477 doupdate(); 3478 3479 while((ch = getch()) != KEY_F(1)) 3480 { switch(ch) 3481 { case 'a': 3482 temp = (PANEL_DATA *)panel_userptr(my_panels[0]); 3483 if(temp->hide == FALSE) 3484 { hide_panel(my_panels[0]); 3485 temp->hide = TRUE; 3486 } 3487 else 3488 { show_panel(my_panels[0]); 3489 temp->hide = FALSE; 3490 } 3491 break; 3492 case 'b': 3493 temp = (PANEL_DATA *)panel_userptr(my_panels[1]); 3494 if(temp->hide == FALSE) 3495 { hide_panel(my_panels[1]); 3496 temp->hide = TRUE; 3497 } 3498 else 3499 { show_panel(my_panels[1]); 3500 temp->hide = FALSE; 3501 } 3502 break; 3503 case 'c': 3504 temp = (PANEL_DATA *)panel_userptr(my_panels[2]); 3505 if(temp->hide == FALSE) 3506 { hide_panel(my_panels[2]); 3507 temp->hide = TRUE; 3508 } 3509 else 3510 { show_panel(my_panels[2]); 3511 temp->hide = FALSE; 3512 } 3513 break; 3514 } 3515 update_panels(); 3516 doupdate(); 3517 } 3518 endwin(); 3519 return 0; | 4956 attroff(COLOR_PAIR(4)); 4957 doupdate(); 4958 4959 while((ch = getch()) != KEY_F(1)) 4960 { switch(ch) 4961 { case 'a': 4962 temp = (PANEL_DATA *)panel_userptr(my_panels[0]); 4963 if(temp->hide == FALSE) 4964 { hide_panel(my_panels[0]); 4965 temp->hide = TRUE; 4966 } 4967 else 4968 { show_panel(my_panels[0]); 4969 temp->hide = FALSE; 4970 } 4971 break; 4972 case 'b': 4973 temp = (PANEL_DATA *)panel_userptr(my_panels[1]); 4974 if(temp->hide == FALSE) 4975 { hide_panel(my_panels[1]); 4976 temp->hide = TRUE; 4977 } 4978 else 4979 { show_panel(my_panels[1]); 4980 temp->hide = FALSE; 4981 } 4982 break; 4983 case 'c': 4984 temp = (PANEL_DATA *)panel_userptr(my_panels[2]); 4985 if(temp->hide == FALSE) 4986 { hide_panel(my_panels[2]); 4987 temp->hide = TRUE; 4988 } 4989 else 4990 { show_panel(my_panels[2]); 4991 temp->hide = FALSE; 4992 } 4993 break; 4994 } 4995 update_panels(); 4996 doupdate(); 4997 } 4998 endwin(); 4999 return 0; |
3520} 3521 3522/* Put all the windows */ 3523void init_wins(WINDOW **wins, int n) | 5000} 5001 5002/* Put all the windows */ 5003void init_wins(WINDOW **wins, int n) |
3524{ int x, y, i; 3525 char label[80]; | 5004{ int x, y, i; 5005 char label[80]; |
3526 | 5006 |
3527 y = 2; 3528 x = 10; 3529 for(i = 0; i < n; ++i) 3530 { wins[i] = newwin(NLINES, NCOLS, y, x); 3531 sprintf(label, "Window Number %d", i + 1); 3532 win_show(wins[i], label, i + 1); 3533 y += 3; 3534 x += 7; 3535 } | 5007 y = 2; 5008 x = 10; 5009 for(i = 0; i < n; ++i) 5010 { wins[i] = newwin(NLINES, NCOLS, y, x); 5011 sprintf(label, "Window Number %d", i + 1); 5012 win_show(wins[i], label, i + 1); 5013 y += 3; 5014 x += 7; 5015 } |
3536} 3537 3538/* Show the window with a border and a label */ 3539void win_show(WINDOW *win, char *label, int label_color) | 5016} 5017 5018/* Show the window with a border and a label */ 5019void win_show(WINDOW *win, char *label, int label_color) |
3540{ int startx, starty, height, width; | 5020{ int startx, starty, height, width; |
3541 | 5021 |
3542 getbegyx(win, starty, startx); 3543 getmaxyx(win, height, width); | 5022 getbegyx(win, starty, startx); 5023 getmaxyx(win, height, width); |
3544 | 5024 |
3545 box(win, 0, 0); 3546 mvwaddch(win, 2, 0, ACS_LTEE); 3547 mvwhline(win, 2, 1, ACS_HLINE, width - 2); 3548 mvwaddch(win, 2, width - 1, ACS_RTEE); 3549 3550 print_in_middle(win, 1, 0, width, label, COLOR_PAIR(label_color)); | 5025 box(win, 0, 0); 5026 mvwaddch(win, 2, 0, ACS_LTEE); 5027 mvwhline(win, 2, 1, ACS_HLINE, width - 2); 5028 mvwaddch(win, 2, width - 1, ACS_RTEE); 5029 5030 print_in_middle(win, 1, 0, width, label, COLOR_PAIR(label_color)); |
3551} 3552 3553void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) | 5031} 5032 5033void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) |
3554{ int length, x, y; 3555 float temp; | 5034{ int length, x, y; 5035 float temp; |
3556 | 5036 |
3557 if(win == NULL) 3558 win = stdscr; 3559 getyx(win, y, x); 3560 if(startx != 0) 3561 x = startx; 3562 if(starty != 0) 3563 y = starty; 3564 if(width == 0) 3565 width = 80; | 5037 if(win == NULL) 5038 win = stdscr; 5039 getyx(win, y, x); 5040 if(startx != 0) 5041 x = startx; 5042 if(starty != 0) 5043 y = starty; 5044 if(width == 0) 5045 width = 80; |
3566 | 5046 |
3567 length = strlen(string); 3568 temp = (width - length)/ 2; 3569 x = startx + (int)temp; 3570 wattron(win, color); 3571 mvwprintw(win, y, x, "%s", string); 3572 wattroff(win, color); 3573 refresh(); 3574}</span></font> 3575</pre></td> 3576</tr> 3577</table> 3578</div> 3579</div> 3580<div class="SECT2"> 3581<hr> 3582<h3 class="SECT2"><a name="PANELABOVE" id="PANELABOVE">16.7. 3583panel_above() and panel_below() Functions</a></h3> 3584<p>The functions <var class="LITERAL">panel_above()</var> and 3585<var class="LITERAL">panel_below()</var> can be used to find out 3586the panel above and below a panel. If the argument to these 3587functions is NULL, then they return a pointer to bottom panel and 3588top panel respectively.</p> 3589</div> 3590</div> 3591<div class="SECT1"> 3592<hr> 3593<h2 class="SECT1"><a name="MENUS" id="MENUS">17. Menus 3594Library</a></h2> 3595<p>The menus library provides a nice extension to basic curses, 3596through which you can create menus. It provides a set of functions 3597to create menus. But they have to be customized to give a nicer 3598look, with colors etc. Let's get into the details.</p> 3599<p>A menu is a screen display that assists the user to choose some 3600subset of a given set of items. To put it simple, a menu is a 3601collection of items from which one or more items can be chosen. 3602Some readers might not be aware of multiple item selection 3603capability. Menu library provides functionality to write menus from 3604which the user can chose more than one item as the preferred 3605choice. This is dealt with in a later section. Now it is time for 3606some rudiments.</p> 3607<div class="SECT2"> 3608<hr> 3609<h3 class="SECT2"><a name="MENUBASICS" id="MENUBASICS">17.1. The 3610Basics</a></h3> 3611<p>To create menus, you first create items, and then post the menu 3612to the display. After that, all the processing of user responses is 3613done in an elegant function menu_driver() which is the work horse 3614of any menu program.</p> 3615<p>The general flow of control of a menu program looks like 3616this.</p> 3617<ol type="1"> 3618<li> 3619<p>Initialize curses</p> 3620</li> 3621<li> 3622<p>Create items using new_item(). You can specify a name and 3623description for the items.</p> 3624</li> 3625<li> 3626<p>Create the menu with new_menu() by specifying the items to be 3627attached with.</p> 3628</li> 3629<li> 3630<p>Post the menu with menu_post() and refresh the screen.</p> 3631</li> 3632<li> 3633<p>Process the user requests with a loop and do necessary updates 3634to menu with menu_driver.</p> 3635</li> 3636<li> 3637<p>Unpost the menu with menu_unpost()</p> 3638</li> 3639<li> 3640<p>Free the memory allocated to menu by free_menu()</p> 3641</li> 3642<li> 3643<p>Free the memory allocated to the items with free_item()</p> 3644</li> 3645<li> 3646<p>End curses</p> 3647</li> 3648</ol> 3649<p>Let's see a program which prints a simple menu and updates the 3650current selection with up, down arrows.</p> 3651</div> 3652<div class="SECT2"> 3653<hr> 3654<h3 class="SECT2"><a name="COMPILEMENUS" id="COMPILEMENUS">17.2. 3655Compiling With the Menu Library</a></h3> 3656<p>To use menu library functions, you have to include menu.h and to 3657link the program with menu library the flag -lmenu should be added 3658along with -lncurses in that order.</p> 3659<table border="0" bgcolor="#E0E0E0" width="100%"> 3660<tr> 3661<td> 3662<pre class="PROGRAMLISTING"> 3663<font color="#000000"> #include <menu.h> | 5047 length = strlen(string); 5048 temp = (width - length)/ 2; 5049 x = startx + (int)temp; 5050 wattron(win, color); 5051 mvwprintw(win, y, x, "%s", string); 5052 wattroff(win, color); 5053 refresh(); 5054}</SPAN 5055></PRE 5056></DIV 5057></DIV 5058><DIV 5059CLASS="SECT2" 5060><HR><H3 5061CLASS="SECT2" 5062><A 5063NAME="PANELABOVE" 5064>16.7. panel_above() and panel_below() Functions</A 5065></H3 5066><P 5067>The functions <TT 5068CLASS="LITERAL" 5069>panel_above()</TT 5070> and 5071<TT 5072CLASS="LITERAL" 5073>panel_below()</TT 5074> can be used to find out the panel 5075above and below a panel. If the argument to these functions is NULL, then they 5076return a pointer to bottom panel and top panel respectively.</P 5077></DIV 5078></DIV 5079><DIV 5080CLASS="SECT1" 5081><HR><H2 5082CLASS="SECT1" 5083><A 5084NAME="MENUS" 5085>17. Menus Library</A 5086></H2 5087><P 5088>The menus library provides a nice extension to basic curses, through which you 5089can create menus. It provides a set of functions to create menus. But they have 5090to be customized to give a nicer look, with colors etc. Let's get into the 5091details.</P 5092><P 5093>A menu is a screen display that assists the user to choose some subset of a 5094given set of items. To put it simple, a menu is a collection of items from which 5095one or more items can be chosen. Some readers might not be aware of multiple 5096item selection capability. Menu library provides functionality to write menus 5097from which the user can chose more than one item as the preferred choice. This 5098is dealt with in a later section. Now it is time for some rudiments.</P 5099><DIV 5100CLASS="SECT2" 5101><HR><H3 5102CLASS="SECT2" 5103><A 5104NAME="MENUBASICS" 5105>17.1. The Basics</A 5106></H3 5107><P 5108>To create menus, you first create items, and then post the menu to the display. 5109After that, all the processing of user responses is done in an elegant function 5110menu_driver() which is the work horse of any menu program. </P 5111><P 5112>The general flow of control of a menu program looks like this. 5113<P 5114></P 5115><OL 5116TYPE="1" 5117><LI 5118><P 5119>Initialize curses</P 5120></LI 5121><LI 5122><P 5123>Create items using new_item(). You can specify a name and description for the 5124items.</P 5125></LI 5126><LI 5127><P 5128>Create the menu with new_menu() by specifying the items to be attached with.</P 5129></LI 5130><LI 5131><P 5132>Post the menu with menu_post() and refresh the screen.</P 5133></LI 5134><LI 5135><P 5136>Process the user requests with a loop and do necessary updates to menu with 5137menu_driver.</P 5138></LI 5139><LI 5140><P 5141>Unpost the menu with menu_unpost()</P 5142></LI 5143><LI 5144><P 5145>Free the memory allocated to menu by free_menu()</P 5146></LI 5147><LI 5148><P 5149>Free the memory allocated to the items with free_item() </P 5150></LI 5151><LI 5152><P 5153>End curses </P 5154></LI 5155></OL 5156></P 5157><P 5158>Let's see a program which prints a simple menu and updates the current selection 5159with up, down arrows. </P 5160></DIV 5161><DIV 5162CLASS="SECT2" 5163><HR><H3 5164CLASS="SECT2" 5165><A 5166NAME="COMPILEMENUS" 5167>17.2. Compiling With the Menu Library</A 5168></H3 5169><P 5170>To use menu library functions, you have to include menu.h and to link the 5171program with menu library the flag -lmenu should be added along with -lncurses 5172in that order.</P 5173><PRE 5174CLASS="PROGRAMLISTING" 5175> #include <menu.h> |
3664 . 3665 . 3666 . 3667 | 5176 . 5177 . 5178 . 5179 |
3668 compile and link: gcc <program file> -lmenu -lncurses</font> 3669</pre></td> 3670</tr> 3671</table> 3672<div class="EXAMPLE"><a name="MMESI" id="MMESI"></a> 3673<p><b>Example 18. Menu Basics</b></p> 3674<table border="0" bgcolor="#E0E0E0" width="100%"> 3675<tr> 3676<td> 3677<pre class="PROGRAMLISTING"> 3678<font color="#000000"><span class= 3679"INLINEMEDIAOBJECT">#include <curses.h> 3680#include <menu.h> | 5180 compile and link: gcc <program file> -lmenu -lncurses</PRE 5181><DIV 5182CLASS="EXAMPLE" 5183><A 5184NAME="MMESI" 5185></A 5186><P 5187><B 5188>Example 18. Menu Basics </B 5189></P 5190><PRE 5191CLASS="PROGRAMLISTING" 5192><SPAN 5193CLASS="INLINEMEDIAOBJECT" 5194>#include <curses.h> 5195#include <menu.h> |
3681 3682#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) | 5196 5197#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) |
3683#define CTRLD 4 | 5198#define CTRLD 4 |
3684 3685char *choices[] = { 3686 "Choice 1", 3687 "Choice 2", 3688 "Choice 3", 3689 "Choice 4", 3690 "Exit", 3691 }; 3692 3693int main() | 5199 5200char *choices[] = { 5201 "Choice 1", 5202 "Choice 2", 5203 "Choice 3", 5204 "Choice 4", 5205 "Exit", 5206 }; 5207 5208int main() |
3694{ ITEM **my_items; 3695 int c; 3696 MENU *my_menu; 3697 int n_choices, i; 3698 ITEM *cur_item; 3699 3700 3701 initscr(); 3702 cbreak(); 3703 noecho(); 3704 keypad(stdscr, TRUE); 3705 3706 n_choices = ARRAY_SIZE(choices); 3707 my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); | 5209{ ITEM **my_items; 5210 int c; 5211 MENU *my_menu; 5212 int n_choices, i; 5213 ITEM *cur_item; 5214 5215 5216 initscr(); 5217 cbreak(); 5218 noecho(); 5219 keypad(stdscr, TRUE); 5220 5221 n_choices = ARRAY_SIZE(choices); 5222 my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); |
3708 | 5223 |
3709 for(i = 0; i < n_choices; ++i) 3710 my_items[i] = new_item(choices[i], choices[i]); 3711 my_items[n_choices] = (ITEM *)NULL; | 5224 for(i = 0; i < n_choices; ++i) 5225 my_items[i] = new_item(choices[i], choices[i]); 5226 my_items[n_choices] = (ITEM *)NULL; |
3712 | 5227 |
3713 my_menu = new_menu((ITEM **)my_items); 3714 mvprintw(LINES - 2, 0, "F1 to Exit"); 3715 post_menu(my_menu); 3716 refresh(); | 5228 my_menu = new_menu((ITEM **)my_items); 5229 mvprintw(LINES - 2, 0, "F1 to Exit"); 5230 post_menu(my_menu); 5231 refresh(); |
3717 | 5232 |
3718 while((c = getch()) != KEY_F(1)) 3719 { switch(c) 3720 { case KEY_DOWN: 3721 menu_driver(my_menu, REQ_DOWN_ITEM); 3722 break; 3723 case KEY_UP: 3724 menu_driver(my_menu, REQ_UP_ITEM); 3725 break; 3726 } 3727 } | 5233 while((c = getch()) != KEY_F(1)) 5234 { switch(c) 5235 { case KEY_DOWN: 5236 menu_driver(my_menu, REQ_DOWN_ITEM); 5237 break; 5238 case KEY_UP: 5239 menu_driver(my_menu, REQ_UP_ITEM); 5240 break; 5241 } 5242 } |
3728 | 5243 |
3729 free_item(my_items[0]); 3730 free_item(my_items[1]); 3731 free_menu(my_menu); 3732 endwin(); | 5244 free_item(my_items[0]); 5245 free_item(my_items[1]); 5246 free_menu(my_menu); 5247 endwin(); |
3733} | 5248} |
3734 </span></font> 3735</pre></td> 3736</tr> 3737</table> 3738</div> 3739<p>This program demonstrates the basic concepts involved in 3740creating a menu using menus library. First we create the items 3741using new_item() and then attach them to the menu with new_menu() 3742function. After posting the menu and refreshing the screen, the 3743main processing loop starts. It reads user input and takes 3744corresponding action. The function menu_driver() is the main work 3745horse of the menu system. The second parameter to this function 3746tells what's to be done with the menu. According to the parameter, 3747menu_driver() does the corresponding task. The value can be either 3748a menu navigational request, an ascii character, or a KEY_MOUSE 3749special key associated with a mouse event.</p> 3750<p>The menu_driver accepts following navigational requests.</p> 3751<table border="0" bgcolor="#E0E0E0" width="100%"> 3752<tr> 3753<td> 3754<pre class="PROGRAMLISTING"> 3755<font color= 3756"#000000"> REQ_LEFT_ITEM Move left to an item. | 5249 </SPAN 5250></PRE 5251></DIV 5252><P 5253>This program demonstrates the basic concepts involved in creating a menu using 5254menus library. First we create the items using new_item() and then attach them 5255to the menu with new_menu() function. After posting the menu and refreshing the 5256screen, the main processing loop starts. It reads user input and takes 5257corresponding action. The function menu_driver() is the main work horse of the 5258menu system. The second parameter to this function tells what's to be done with 5259the menu. According to the parameter, menu_driver() does the corresponding task. 5260The value can be either a menu navigational request, an ascii character, or a 5261KEY_MOUSE special key associated with a mouse event.</P 5262><P 5263>The menu_driver accepts following navigational requests. 5264<PRE 5265CLASS="PROGRAMLISTING" 5266> REQ_LEFT_ITEM Move left to an item. |
3757 REQ_RIGHT_ITEM Move right to an item. 3758 REQ_UP_ITEM Move up to an item. 3759 REQ_DOWN_ITEM Move down to an item. 3760 REQ_SCR_ULINE Scroll up a line. 3761 REQ_SCR_DLINE Scroll down a line. 3762 REQ_SCR_DPAGE Scroll down a page. 3763 REQ_SCR_UPAGE Scroll up a page. 3764 REQ_FIRST_ITEM Move to the first item. 3765 REQ_LAST_ITEM Move to the last item. 3766 REQ_NEXT_ITEM Move to the next item. 3767 REQ_PREV_ITEM Move to the previous item. 3768 REQ_TOGGLE_ITEM Select/deselect an item. 3769 REQ_CLEAR_PATTERN Clear the menu pattern buffer. 3770 REQ_BACK_PATTERN Delete the previous character from the pattern buffer. 3771 REQ_NEXT_MATCH Move to the next item matching the pattern match. | 5267 REQ_RIGHT_ITEM Move right to an item. 5268 REQ_UP_ITEM Move up to an item. 5269 REQ_DOWN_ITEM Move down to an item. 5270 REQ_SCR_ULINE Scroll up a line. 5271 REQ_SCR_DLINE Scroll down a line. 5272 REQ_SCR_DPAGE Scroll down a page. 5273 REQ_SCR_UPAGE Scroll up a page. 5274 REQ_FIRST_ITEM Move to the first item. 5275 REQ_LAST_ITEM Move to the last item. 5276 REQ_NEXT_ITEM Move to the next item. 5277 REQ_PREV_ITEM Move to the previous item. 5278 REQ_TOGGLE_ITEM Select/deselect an item. 5279 REQ_CLEAR_PATTERN Clear the menu pattern buffer. 5280 REQ_BACK_PATTERN Delete the previous character from the pattern buffer. 5281 REQ_NEXT_MATCH Move to the next item matching the pattern match. |
3772 REQ_PREV_MATCH Move to the previous item matching the pattern match. </font> 3773</pre></td> 3774</tr> 3775</table> 3776<p>Don't get overwhelmed by the number of options. We will see them 3777slowly one after another. The options of interest in this example 3778are REQ_UP_ITEM and REQ_DOWN_ITEM. These two options when passed to 3779menu_driver, menu driver updates the current item to one item up or 3780down respectively.</p> 3781</div> 3782<div class="SECT2"> 3783<hr> 3784<h3 class="SECT2"><a name="MENUDRIVER" id="MENUDRIVER">17.3. Menu 3785Driver: The work horse of the menu system</a></h3> 3786<p>As you have seen in the above example, menu_driver plays an 3787important role in updating the menu. It is very important to 3788understand various options it takes and what they do. As explained 3789above, the second parameter to menu_driver() can be either a 3790navigational request, a printable character or a KEY_MOUSE key. 3791Let's dissect the different navigational requests.</p> 3792<ul> 3793<li> 3794<p><em>REQ_LEFT_ITEM and REQ_RIGHT_ITEM</em></p> 3795<p>A Menu can be displayed with multiple columns for more than one 3796item. This can be done by using the <var class= 3797"LITERAL">menu_format()</var>function. When a multi columnar menu 3798is displayed these requests cause the menu driver to move the 3799current selection to left or right.</p> 3800</li> 3801<li> 3802<p><em>REQ_UP_ITEM and REQ_DOWN_ITEM</em></p> 3803<p>These two options you have seen in the above example. These 3804options when given, makes the menu_driver to move the current 3805selection to an item up or down.</p> 3806</li> 3807<li> 3808<p><em>REQ_SCR_* options</em></p> 3809<p>The four options REQ_SCR_ULINE, REQ_SCR_DLINE, REQ_SCR_DPAGE, 3810REQ_SCR_UPAGE are related to scrolling. If all the items in the 3811menu cannot be displayed in the menu sub window, then the menu is 3812scrollable. These requests can be given to the menu_driver to do 3813the scrolling either one line up, down or one page down or up 3814respectively.</p> 3815</li> 3816<li> 3817<p><em>REQ_FIRST_ITEM, REQ_LAST_ITEM, REQ_NEXT_ITEM and 3818REQ_PREV_ITEM</em></p> 3819<p>These requests are self explanatory.</p> 3820</li> 3821<li> 3822<p><em>REQ_TOGGLE_ITEM</em></p> 3823<p>This request when given, toggles the present selection. This 3824option is to be used only in a multi valued menu. So to use this 3825request the option O_ONEVALUE must be off. This option can be made 3826off or on with set_menu_opts().</p> 3827</li> 3828<li> 3829<p><em>Pattern Requests</em></p> 3830<p>Every menu has an associated pattern buffer, which is used to 3831find the nearest match to the ascii characters entered by the user. 3832Whenever ascii characters are given to menu_driver, it puts in to 3833the pattern buffer. It also tries to find the nearest match to the 3834pattern in the items list and moves current selection to that item. 3835The request REQ_CLEAR_PATTERN clears the pattern buffer. The 3836request REQ_BACK_PATTERN deletes the previous character in the 3837pattern buffer. In case the pattern matches more than one item then 3838the matched items can be cycled through REQ_NEXT_MATCH and 3839REQ_PREV_MATCH which move the current selection to the next and 3840previous matches respectively.</p> 3841</li> 3842<li> 3843<p><em>Mouse Requests</em></p> 3844<p>In case of KEY_MOUSE requests, according to the mouse position 3845an action is taken accordingly. The action to be taken is explained 3846in the man page as,</p> 3847<table border="0" bgcolor="#E0E0E0" width="90%"> 3848<tr> 3849<td> 3850<pre class="PROGRAMLISTING"> 3851<font color= 3852"#000000"><em> If the second argument is the KEY_MOUSE special key, the | 5282 REQ_PREV_MATCH Move to the previous item matching the pattern match. </PRE 5283></P 5284><P 5285>Don't get overwhelmed by the number of options. We will see them slowly one 5286after another. The options of interest in this example are REQ_UP_ITEM and 5287REQ_DOWN_ITEM. These two options when passed to menu_driver, menu driver 5288updates the current item to one item up or down respectively.</P 5289></DIV 5290><DIV 5291CLASS="SECT2" 5292><HR><H3 5293CLASS="SECT2" 5294><A 5295NAME="MENUDRIVER" 5296>17.3. Menu Driver: The work horse of the menu system</A 5297></H3 5298><P 5299>As you have seen in the above example, menu_driver plays an important role in 5300updating the menu. It is very important to understand various options it takes 5301and what they do. As explained above, the second parameter to menu_driver() can 5302be either a navigational request, a printable character or a KEY_MOUSE key. 5303Let's dissect the different navigational requests.</P 5304><P 5305></P 5306><UL 5307><LI 5308><P 5309><SPAN 5310CLASS="emphasis" 5311><I 5312CLASS="EMPHASIS" 5313>REQ_LEFT_ITEM and REQ_RIGHT_ITEM</I 5314></SPAN 5315></P 5316><P 5317>A Menu can be displayed with multiple columns for more than one item. This can 5318be done by using the <TT 5319CLASS="LITERAL" 5320>menu_format()</TT 5321>function. 5322When a multi columnar menu is displayed these requests cause the menu driver to 5323move the current selection to left or right.</P 5324></LI 5325><LI 5326><P 5327><SPAN 5328CLASS="emphasis" 5329><I 5330CLASS="EMPHASIS" 5331>REQ_UP_ITEM and REQ_DOWN_ITEM </I 5332></SPAN 5333> </P 5334><P 5335>These two options you have seen in the above example. These options when given, 5336makes the menu_driver to move the current selection to an item up or down.</P 5337></LI 5338><LI 5339><P 5340> <SPAN 5341CLASS="emphasis" 5342><I 5343CLASS="EMPHASIS" 5344>REQ_SCR_* options</I 5345></SPAN 5346> </P 5347><P 5348>The four options REQ_SCR_ULINE, REQ_SCR_DLINE, REQ_SCR_DPAGE, REQ_SCR_UPAGE are 5349related to scrolling. If all the items in the menu cannot be displayed in the 5350menu sub window, then the menu is scrollable. These requests can be given to the 5351menu_driver to do the scrolling either one line up, down or one page down or up 5352respectively. </P 5353></LI 5354><LI 5355><P 5356><SPAN 5357CLASS="emphasis" 5358><I 5359CLASS="EMPHASIS" 5360>REQ_FIRST_ITEM, REQ_LAST_ITEM, REQ_NEXT_ITEM and 5361REQ_PREV_ITEM </I 5362></SPAN 5363> </P 5364><P 5365>These requests are self explanatory.</P 5366></LI 5367><LI 5368><P 5369> <SPAN 5370CLASS="emphasis" 5371><I 5372CLASS="EMPHASIS" 5373>REQ_TOGGLE_ITEM</I 5374></SPAN 5375> </P 5376><P 5377>This request when given, toggles the present selection. This option is to be 5378used only in a multi valued menu. So to use this request the option O_ONEVALUE 5379must be off. This option can be made off or on with set_menu_opts().</P 5380></LI 5381><LI 5382><P 5383> <SPAN 5384CLASS="emphasis" 5385><I 5386CLASS="EMPHASIS" 5387>Pattern Requests </I 5388></SPAN 5389></P 5390><P 5391>Every menu has an associated pattern buffer, which is used to find the nearest 5392match to the ascii characters entered by the user. Whenever ascii characters are 5393given to menu_driver, it puts in to the pattern buffer. It also tries to find 5394the nearest match to the pattern in the items list and moves current selection 5395to that item. The request REQ_CLEAR_PATTERN clears the pattern buffer. The 5396request REQ_BACK_PATTERN deletes the previous character in the pattern buffer. 5397In case the pattern matches more than one item then the matched items can be 5398cycled through REQ_NEXT_MATCH and REQ_PREV_MATCH which move the current 5399selection to the next and previous matches respectively.</P 5400></LI 5401><LI 5402><P 5403> <SPAN 5404CLASS="emphasis" 5405><I 5406CLASS="EMPHASIS" 5407>Mouse Requests</I 5408></SPAN 5409></P 5410><P 5411>In case of KEY_MOUSE requests, according to the mouse position an action is 5412taken accordingly. The action to be taken is explained in the man page as, </P 5413><PRE 5414CLASS="PROGRAMLISTING" 5415><SPAN 5416CLASS="emphasis" 5417><I 5418CLASS="EMPHASIS" 5419> If the second argument is the KEY_MOUSE special key, the |
3853 associated mouse event is translated into one of the above 3854 pre-defined requests. Currently only clicks in the user 3855 window (e.g. inside the menu display area or the decora­ 3856 tion window) are handled. If you click above the display 3857 region of the menu, a REQ_SCR_ULINE is generated, if you 3858 doubleclick a REQ_SCR_UPAGE is generated and if you 3859 tripleclick a REQ_FIRST_ITEM is generated. If you click 3860 below the display region of the menu, a REQ_SCR_DLINE is 3861 generated, if you doubleclick a REQ_SCR_DPAGE is generated 3862 and if you tripleclick a REQ_LAST_ITEM is generated. If 3863 you click at an item inside the display area of the menu, | 5420 associated mouse event is translated into one of the above 5421 pre-defined requests. Currently only clicks in the user 5422 window (e.g. inside the menu display area or the decora­ 5423 tion window) are handled. If you click above the display 5424 region of the menu, a REQ_SCR_ULINE is generated, if you 5425 doubleclick a REQ_SCR_UPAGE is generated and if you 5426 tripleclick a REQ_FIRST_ITEM is generated. If you click 5427 below the display region of the menu, a REQ_SCR_DLINE is 5428 generated, if you doubleclick a REQ_SCR_DPAGE is generated 5429 and if you tripleclick a REQ_LAST_ITEM is generated. If 5430 you click at an item inside the display area of the menu, |
3864 the menu cursor is positioned to that item.</em></font> 3865</pre></td> 3866</tr> 3867</table> 3868</li> 3869</ul> 3870<p>Each of the above requests will be explained in the following 3871lines with several examples whenever appropriate.</p> 3872</div> 3873<div class="SECT2"> 3874<hr> 3875<h3 class="SECT2"><a name="MENUWINDOWS" id="MENUWINDOWS">17.4. Menu 3876Windows</a></h3> 3877<p>Every menu created is associated with a window and a sub window. 3878The menu window displays any title or border associated with the 3879menu. The menu sub window displays the menu items currently 3880available for selection. But we didn't specify any window or sub 3881window in the simple example. When a window is not specified, 3882stdscr is taken as the main window, and then menu system calculates 3883the sub window size required for the display of items. Then items 3884are displayed in the calculated sub window. So let's play with 3885these windows and display a menu with a border and a title.</p> 3886<div class="EXAMPLE"><a name="MMEWI" id="MMEWI"></a> 3887<p><b>Example 19. Menu Windows Usage example</b></p> 3888<table border="0" bgcolor="#E0E0E0" width="100%"> 3889<tr> 3890<td> 3891<pre class="PROGRAMLISTING"> 3892<font color="#000000"><span class= 3893"INLINEMEDIAOBJECT">#include <menu.h> | 5431 the menu cursor is positioned to that item.</I 5432></SPAN 5433></PRE 5434></LI 5435></UL 5436><P 5437>Each of the above requests will be explained in the following lines with several 5438examples whenever appropriate.</P 5439></DIV 5440><DIV 5441CLASS="SECT2" 5442><HR><H3 5443CLASS="SECT2" 5444><A 5445NAME="MENUWINDOWS" 5446>17.4. Menu Windows</A 5447></H3 5448><P 5449>Every menu created is associated with a window and a sub window. The menu window 5450displays any title or border associated with the menu. The menu sub window 5451displays the menu items currently available for selection. But we didn't specify 5452any window or sub window in the simple example. When a window is not specified, 5453stdscr is taken as the main window, and then menu system calculates the sub 5454window size required for the display of items. Then items are displayed in the 5455calculated sub window. So let's play with these windows and display a menu with 5456a border and a title.</P 5457><DIV 5458CLASS="EXAMPLE" 5459><A 5460NAME="MMEWI" 5461></A 5462><P 5463><B 5464>Example 19. Menu Windows Usage example </B 5465></P 5466><PRE 5467CLASS="PROGRAMLISTING" 5468><SPAN 5469CLASS="INLINEMEDIAOBJECT" 5470>#include <menu.h> |
3894 3895#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) | 5471 5472#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) |
3896#define CTRLD 4 | 5473#define CTRLD 4 |
3897 3898char *choices[] = { 3899 "Choice 1", 3900 "Choice 2", 3901 "Choice 3", 3902 "Choice 4", 3903 "Exit", 3904 (char *)NULL, 3905 }; 3906void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); 3907 3908int main() | 5474 5475char *choices[] = { 5476 "Choice 1", 5477 "Choice 2", 5478 "Choice 3", 5479 "Choice 4", 5480 "Exit", 5481 (char *)NULL, 5482 }; 5483void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); 5484 5485int main() |
3909{ ITEM **my_items; 3910 int c; 3911 MENU *my_menu; | 5486{ ITEM **my_items; 5487 int c; 5488 MENU *my_menu; |
3912 WINDOW *my_menu_win; 3913 int n_choices, i; | 5489 WINDOW *my_menu_win; 5490 int n_choices, i; |
3914 3915 /* Initialize curses */ 3916 initscr(); 3917 start_color(); | 5491 5492 /* Initialize curses */ 5493 initscr(); 5494 start_color(); |
3918 cbreak(); 3919 noecho(); | 5495 cbreak(); 5496 noecho(); |
3920 keypad(stdscr, TRUE); 3921 init_pair(1, COLOR_RED, COLOR_BLACK); | 5497 keypad(stdscr, TRUE); 5498 init_pair(1, COLOR_RED, COLOR_BLACK); |
3922 | 5499 |
3923 /* Create items */ | 5500 /* Create items */ |
3924 n_choices = ARRAY_SIZE(choices); 3925 my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *)); | 5501 n_choices = ARRAY_SIZE(choices); 5502 my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *)); |
3926 for(i = 0; i < n_choices; ++i) | 5503 for(i = 0; i < n_choices; ++i) |
3927 my_items[i] = new_item(choices[i], choices[i]); 3928 | 5504 my_items[i] = new_item(choices[i], choices[i]); 5505 |
3929 /* Crate menu */ 3930 my_menu = new_menu((ITEM **)my_items); | 5506 /* Crate menu */ 5507 my_menu = new_menu((ITEM **)my_items); |
3931 | 5508 |
3932 /* Create the window to be associated with the menu */ | 5509 /* Create the window to be associated with the menu */ |
3933 my_menu_win = newwin(10, 40, 4, 4); 3934 keypad(my_menu_win, TRUE); 3935 | 5510 my_menu_win = newwin(10, 40, 4, 4); 5511 keypad(my_menu_win, TRUE); 5512 |
3936 /* Set main window and sub window */ | 5513 /* Set main window and sub window */ |
3937 set_menu_win(my_menu, my_menu_win); 3938 set_menu_sub(my_menu, derwin(my_menu_win, 6, 38, 3, 1)); 3939 | 5514 set_menu_win(my_menu, my_menu_win); 5515 set_menu_sub(my_menu, derwin(my_menu_win, 6, 38, 3, 1)); 5516 |
3940 /* Set menu mark to the string " * " */ | 5517 /* Set menu mark to the string " * " */ |
3941 set_menu_mark(my_menu, " * "); 3942 | 5518 set_menu_mark(my_menu, " * "); 5519 |
3943 /* Print a border around the main window and print a title */ | 5520 /* Print a border around the main window and print a title */ |
3944 box(my_menu_win, 0, 0); | 5521 box(my_menu_win, 0, 0); |
3945 print_in_middle(my_menu_win, 1, 0, 40, "My Menu", COLOR_PAIR(1)); 3946 mvwaddch(my_menu_win, 2, 0, ACS_LTEE); 3947 mvwhline(my_menu_win, 2, 1, ACS_HLINE, 38); 3948 mvwaddch(my_menu_win, 2, 39, ACS_RTEE); 3949 mvprintw(LINES - 2, 0, "F1 to exit"); 3950 refresh(); | 5522 print_in_middle(my_menu_win, 1, 0, 40, "My Menu", COLOR_PAIR(1)); 5523 mvwaddch(my_menu_win, 2, 0, ACS_LTEE); 5524 mvwhline(my_menu_win, 2, 1, ACS_HLINE, 38); 5525 mvwaddch(my_menu_win, 2, 39, ACS_RTEE); 5526 mvprintw(LINES - 2, 0, "F1 to exit"); 5527 refresh(); |
3951 | 5528 |
3952 /* Post the menu */ 3953 post_menu(my_menu); 3954 wrefresh(my_menu_win); | 5529 /* Post the menu */ 5530 post_menu(my_menu); 5531 wrefresh(my_menu_win); |
3955 | 5532 |
3956 while((c = wgetch(my_menu_win)) != KEY_F(1)) 3957 { switch(c) 3958 { case KEY_DOWN: 3959 menu_driver(my_menu, REQ_DOWN_ITEM); 3960 break; 3961 case KEY_UP: 3962 menu_driver(my_menu, REQ_UP_ITEM); 3963 break; 3964 } | 5533 while((c = wgetch(my_menu_win)) != KEY_F(1)) 5534 { switch(c) 5535 { case KEY_DOWN: 5536 menu_driver(my_menu, REQ_DOWN_ITEM); 5537 break; 5538 case KEY_UP: 5539 menu_driver(my_menu, REQ_UP_ITEM); 5540 break; 5541 } |
3965 wrefresh(my_menu_win); | 5542 wrefresh(my_menu_win); |
3966 } | 5543 } |
3967 | 5544 |
3968 /* Unpost and free all the memory taken up */ | 5545 /* Unpost and free all the memory taken up */ |
3969 unpost_menu(my_menu); 3970 free_menu(my_menu); | 5546 unpost_menu(my_menu); 5547 free_menu(my_menu); |
3971 for(i = 0; i < n_choices; ++i) | 5548 for(i = 0; i < n_choices; ++i) |
3972 free_item(my_items[i]); | 5549 free_item(my_items[i]); |
3973 endwin(); | 5550 endwin(); |
3974} 3975 3976void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) | 5551} 5552 5553void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) |
3977{ int length, x, y; 3978 float temp; | 5554{ int length, x, y; 5555 float temp; |
3979 | 5556 |
3980 if(win == NULL) 3981 win = stdscr; 3982 getyx(win, y, x); 3983 if(startx != 0) 3984 x = startx; 3985 if(starty != 0) 3986 y = starty; 3987 if(width == 0) 3988 width = 80; | 5557 if(win == NULL) 5558 win = stdscr; 5559 getyx(win, y, x); 5560 if(startx != 0) 5561 x = startx; 5562 if(starty != 0) 5563 y = starty; 5564 if(width == 0) 5565 width = 80; |
3989 | 5566 |
3990 length = strlen(string); 3991 temp = (width - length)/ 2; 3992 x = startx + (int)temp; 3993 wattron(win, color); 3994 mvwprintw(win, y, x, "%s", string); 3995 wattroff(win, color); 3996 refresh(); 3997}</span></font> 3998</pre></td> 3999</tr> 4000</table> 4001</div> 4002<p>This example creates a menu with a title, border, a fancy line 4003separating title and the items. As you can see, in order to attach 4004a window to a menu the function set_menu_win() has to be used. Then 4005we attach the sub window also. This displays the items in the sub 4006window. You can also set the mark string which gets displayed to 4007the left of the selected item with set_menu_mark().</p> 4008</div> 4009<div class="SECT2"> 4010<hr> 4011<h3 class="SECT2"><a name="SCROLLMENUS" id="SCROLLMENUS">17.5. 4012Scrolling Menus</a></h3> 4013<p>If the sub window given for a window is not big enough to show 4014all the items, then the menu will be scrollable. When you are on 4015the last item in the present list, if you send REQ_DOWN_ITEM, it 4016gets translated into REQ_SCR_DLINE and the menu scrolls by one 4017item. You can manually give REQ_SCR_ operations to do scrolling. 4018Let's see how it can be done.</p> 4019<div class="EXAMPLE"><a name="MMESC" id="MMESC"></a> 4020<p><b>Example 20. Scrolling Menus example</b></p> 4021<table border="0" bgcolor="#E0E0E0" width="100%"> 4022<tr> 4023<td> 4024<pre class="PROGRAMLISTING"> 4025<font color="#000000"><span class= 4026"INLINEMEDIAOBJECT">#include <curses.h> 4027#include <menu.h> | 5567 length = strlen(string); 5568 temp = (width - length)/ 2; 5569 x = startx + (int)temp; 5570 wattron(win, color); 5571 mvwprintw(win, y, x, "%s", string); 5572 wattroff(win, color); 5573 refresh(); 5574}</SPAN 5575></PRE 5576></DIV 5577><P 5578>This example creates a menu with a title, border, a fancy line separating title 5579and the items. As you can see, in order to attach a window to a menu the 5580function set_menu_win() has to be used. Then we attach the sub window also. This 5581displays the items in the sub window. You can also set the mark string which 5582gets displayed to the left of the selected item with set_menu_mark().</P 5583></DIV 5584><DIV 5585CLASS="SECT2" 5586><HR><H3 5587CLASS="SECT2" 5588><A 5589NAME="SCROLLMENUS" 5590>17.5. Scrolling Menus</A 5591></H3 5592><P 5593>If the sub window given for a window is not big enough to show all the items, 5594then the menu will be scrollable. When you are on the last item in the present 5595list, if you send REQ_DOWN_ITEM, it gets translated into REQ_SCR_DLINE and the 5596menu scrolls by one item. You can manually give REQ_SCR_ operations to do 5597scrolling. Let's see how it can be done.</P 5598><DIV 5599CLASS="EXAMPLE" 5600><A 5601NAME="MMESC" 5602></A 5603><P 5604><B 5605>Example 20. Scrolling Menus example </B 5606></P 5607><PRE 5608CLASS="PROGRAMLISTING" 5609><SPAN 5610CLASS="INLINEMEDIAOBJECT" 5611>#include <curses.h> 5612#include <menu.h> |
4028 4029#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) | 5613 5614#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) |
4030#define CTRLD 4 | 5615#define CTRLD 4 |
4031 4032char *choices[] = { 4033 "Choice 1", 4034 "Choice 2", 4035 "Choice 3", 4036 "Choice 4", | 5616 5617char *choices[] = { 5618 "Choice 1", 5619 "Choice 2", 5620 "Choice 3", 5621 "Choice 4", |
4037 "Choice 5", 4038 "Choice 6", 4039 "Choice 7", 4040 "Choice 8", 4041 "Choice 9", 4042 "Choice 10", | 5622 "Choice 5", 5623 "Choice 6", 5624 "Choice 7", 5625 "Choice 8", 5626 "Choice 9", 5627 "Choice 10", |
4043 "Exit", 4044 (char *)NULL, 4045 }; 4046void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); 4047 4048int main() | 5628 "Exit", 5629 (char *)NULL, 5630 }; 5631void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); 5632 5633int main() |
4049{ ITEM **my_items; 4050 int c; 4051 MENU *my_menu; | 5634{ ITEM **my_items; 5635 int c; 5636 MENU *my_menu; |
4052 WINDOW *my_menu_win; 4053 int n_choices, i; | 5637 WINDOW *my_menu_win; 5638 int n_choices, i; |
4054 4055 /* Initialize curses */ 4056 initscr(); 4057 start_color(); | 5639 5640 /* Initialize curses */ 5641 initscr(); 5642 start_color(); |
4058 cbreak(); 4059 noecho(); | 5643 cbreak(); 5644 noecho(); |
4060 keypad(stdscr, TRUE); 4061 init_pair(1, COLOR_RED, COLOR_BLACK); 4062 init_pair(2, COLOR_CYAN, COLOR_BLACK); | 5645 keypad(stdscr, TRUE); 5646 init_pair(1, COLOR_RED, COLOR_BLACK); 5647 init_pair(2, COLOR_CYAN, COLOR_BLACK); |
4063 | 5648 |
4064 /* Create items */ | 5649 /* Create items */ |
4065 n_choices = ARRAY_SIZE(choices); 4066 my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *)); | 5650 n_choices = ARRAY_SIZE(choices); 5651 my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *)); |
4067 for(i = 0; i < n_choices; ++i) | 5652 for(i = 0; i < n_choices; ++i) |
4068 my_items[i] = new_item(choices[i], choices[i]); 4069 | 5653 my_items[i] = new_item(choices[i], choices[i]); 5654 |
4070 /* Crate menu */ 4071 my_menu = new_menu((ITEM **)my_items); | 5655 /* Crate menu */ 5656 my_menu = new_menu((ITEM **)my_items); |
4072 | 5657 |
4073 /* Create the window to be associated with the menu */ | 5658 /* Create the window to be associated with the menu */ |
4074 my_menu_win = newwin(10, 40, 4, 4); 4075 keypad(my_menu_win, TRUE); 4076 | 5659 my_menu_win = newwin(10, 40, 4, 4); 5660 keypad(my_menu_win, TRUE); 5661 |
4077 /* Set main window and sub window */ | 5662 /* Set main window and sub window */ |
4078 set_menu_win(my_menu, my_menu_win); 4079 set_menu_sub(my_menu, derwin(my_menu_win, 6, 38, 3, 1)); | 5663 set_menu_win(my_menu, my_menu_win); 5664 set_menu_sub(my_menu, derwin(my_menu_win, 6, 38, 3, 1)); |
4080 set_menu_format(my_menu, 5, 1); 4081 4082 /* Set menu mark to the string " * " */ | 5665 set_menu_format(my_menu, 5, 1); 5666 5667 /* Set menu mark to the string " * " */ |
4083 set_menu_mark(my_menu, " * "); 4084 | 5668 set_menu_mark(my_menu, " * "); 5669 |
4085 /* Print a border around the main window and print a title */ | 5670 /* Print a border around the main window and print a title */ |
4086 box(my_menu_win, 0, 0); | 5671 box(my_menu_win, 0, 0); |
4087 print_in_middle(my_menu_win, 1, 0, 40, "My Menu", COLOR_PAIR(1)); 4088 mvwaddch(my_menu_win, 2, 0, ACS_LTEE); 4089 mvwhline(my_menu_win, 2, 1, ACS_HLINE, 38); 4090 mvwaddch(my_menu_win, 2, 39, ACS_RTEE); | 5672 print_in_middle(my_menu_win, 1, 0, 40, "My Menu", COLOR_PAIR(1)); 5673 mvwaddch(my_menu_win, 2, 0, ACS_LTEE); 5674 mvwhline(my_menu_win, 2, 1, ACS_HLINE, 38); 5675 mvwaddch(my_menu_win, 2, 39, ACS_RTEE); |
4091 | 5676 |
4092 /* Post the menu */ 4093 post_menu(my_menu); 4094 wrefresh(my_menu_win); 4095 4096 attron(COLOR_PAIR(2)); 4097 mvprintw(LINES - 2, 0, "Use PageUp and PageDown to scoll down or up a page of items"); 4098 mvprintw(LINES - 1, 0, "Arrow Keys to navigate (F1 to Exit)"); 4099 attroff(COLOR_PAIR(2)); 4100 refresh(); | 5677 /* Post the menu */ 5678 post_menu(my_menu); 5679 wrefresh(my_menu_win); 5680 5681 attron(COLOR_PAIR(2)); 5682 mvprintw(LINES - 2, 0, "Use PageUp and PageDown to scoll down or up a page of items"); 5683 mvprintw(LINES - 1, 0, "Arrow Keys to navigate (F1 to Exit)"); 5684 attroff(COLOR_PAIR(2)); 5685 refresh(); |
4101 | 5686 |
4102 while((c = wgetch(my_menu_win)) != KEY_F(1)) 4103 { switch(c) 4104 { case KEY_DOWN: 4105 menu_driver(my_menu, REQ_DOWN_ITEM); 4106 break; 4107 case KEY_UP: 4108 menu_driver(my_menu, REQ_UP_ITEM); 4109 break; 4110 case KEY_NPAGE: 4111 menu_driver(my_menu, REQ_SCR_DPAGE); 4112 break; 4113 case KEY_PPAGE: 4114 menu_driver(my_menu, REQ_SCR_UPAGE); 4115 break; 4116 } | 5687 while((c = wgetch(my_menu_win)) != KEY_F(1)) 5688 { switch(c) 5689 { case KEY_DOWN: 5690 menu_driver(my_menu, REQ_DOWN_ITEM); 5691 break; 5692 case KEY_UP: 5693 menu_driver(my_menu, REQ_UP_ITEM); 5694 break; 5695 case KEY_NPAGE: 5696 menu_driver(my_menu, REQ_SCR_DPAGE); 5697 break; 5698 case KEY_PPAGE: 5699 menu_driver(my_menu, REQ_SCR_UPAGE); 5700 break; 5701 } |
4117 wrefresh(my_menu_win); | 5702 wrefresh(my_menu_win); |
4118 } | 5703 } |
4119 | 5704 |
4120 /* Unpost and free all the memory taken up */ | 5705 /* Unpost and free all the memory taken up */ |
4121 unpost_menu(my_menu); 4122 free_menu(my_menu); | 5706 unpost_menu(my_menu); 5707 free_menu(my_menu); |
4123 for(i = 0; i < n_choices; ++i) | 5708 for(i = 0; i < n_choices; ++i) |
4124 free_item(my_items[i]); | 5709 free_item(my_items[i]); |
4125 endwin(); | 5710 endwin(); |
4126} 4127 4128void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) | 5711} 5712 5713void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) |
4129{ int length, x, y; 4130 float temp; | 5714{ int length, x, y; 5715 float temp; |
4131 | 5716 |
4132 if(win == NULL) 4133 win = stdscr; 4134 getyx(win, y, x); 4135 if(startx != 0) 4136 x = startx; 4137 if(starty != 0) 4138 y = starty; 4139 if(width == 0) 4140 width = 80; | 5717 if(win == NULL) 5718 win = stdscr; 5719 getyx(win, y, x); 5720 if(startx != 0) 5721 x = startx; 5722 if(starty != 0) 5723 y = starty; 5724 if(width == 0) 5725 width = 80; |
4141 | 5726 |
4142 length = strlen(string); 4143 temp = (width - length)/ 2; 4144 x = startx + (int)temp; 4145 wattron(win, color); 4146 mvwprintw(win, y, x, "%s", string); 4147 wattroff(win, color); 4148 refresh(); 4149}</span></font> 4150</pre></td> 4151</tr> 4152</table> 4153</div> 4154<p>This program is self-explanatory. In this example the number of 4155choices has been increased to ten, which is larger than our sub 4156window size which can hold 6 items. This message has to be 4157explicitly conveyed to the menu system with the function 4158set_menu_format(). In here we specify the number of rows and 4159columns we want to be displayed for a single page. We can specify 4160any number of items to be shown, in the rows variables, if it is 4161less than the height of the sub window. If the key pressed by the 4162user is a PAGE UP or PAGE DOWN, the menu is scrolled a page due to 4163the requests (REQ_SCR_DPAGE and REQ_SCR_UPAGE) given to 4164menu_driver().</p> 4165</div> 4166<div class="SECT2"> 4167<hr> 4168<h3 class="SECT2"><a name="MULTICOLUMN" id="MULTICOLUMN">17.6. 4169Multi Columnar Menus</a></h3> 4170<p>In the above example you have seen how to use the function 4171set_menu_format(). I didn't mention what the cols variable (third 4172parameter) does. Well, If your sub window is wide enough, you can 4173opt to display more than one item per row. This can be specified in 4174the cols variable. To make things simpler, the following example 4175doesn't show descriptions for the items.</p> 4176<div class="EXAMPLE"><a name="MMEMUCO" id="MMEMUCO"></a> 4177<p><b>Example 21. Milt Columnar Menus Example</b></p> 4178<table border="0" bgcolor="#E0E0E0" width="100%"> 4179<tr> 4180<td> 4181<pre class="PROGRAMLISTING"> 4182<font color="#000000"><span class= 4183"INLINEMEDIAOBJECT">#include <curses.h> 4184#include <menu.h> | 5727 length = strlen(string); 5728 temp = (width - length)/ 2; 5729 x = startx + (int)temp; 5730 wattron(win, color); 5731 mvwprintw(win, y, x, "%s", string); 5732 wattroff(win, color); 5733 refresh(); 5734}</SPAN 5735></PRE 5736></DIV 5737><P 5738>This program is self-explanatory. In this example the number of choices has been 5739increased to ten, which is larger than our sub window size which can hold 6 5740items. This message has to be explicitly conveyed to the menu system with the 5741function set_menu_format(). In here we specify the number of rows and columns we 5742want to be displayed for a single page. We can specify any number of items to be 5743shown, in the rows variables, if it is less than the height of the sub window. 5744If the key pressed by the user is a PAGE UP or PAGE DOWN, the menu is scrolled a 5745page due to the requests (REQ_SCR_DPAGE and REQ_SCR_UPAGE) given to 5746menu_driver().</P 5747></DIV 5748><DIV 5749CLASS="SECT2" 5750><HR><H3 5751CLASS="SECT2" 5752><A 5753NAME="MULTICOLUMN" 5754>17.6. Multi Columnar Menus</A 5755></H3 5756><P 5757>In the above example you have seen how to use the function set_menu_format(). I 5758didn't mention what the cols variable (third parameter) does. Well, If your sub 5759window is wide enough, you can opt to display more than one item per row. This 5760can be specified in the cols variable. To make things simpler, the following 5761example doesn't show descriptions for the items.</P 5762><DIV 5763CLASS="EXAMPLE" 5764><A 5765NAME="MMEMUCO" 5766></A 5767><P 5768><B 5769>Example 21. Milt Columnar Menus Example </B 5770></P 5771><PRE 5772CLASS="PROGRAMLISTING" 5773><SPAN 5774CLASS="INLINEMEDIAOBJECT" 5775>#include <curses.h> 5776#include <menu.h> |
4185 4186#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) | 5777 5778#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) |
4187#define CTRLD 4 | 5779#define CTRLD 4 |
4188 4189char *choices[] = { 4190 "Choice 1", "Choice 2", "Choice 3", "Choice 4", "Choice 5", | 5780 5781char *choices[] = { 5782 "Choice 1", "Choice 2", "Choice 3", "Choice 4", "Choice 5", |
4191 "Choice 6", "Choice 7", "Choice 8", "Choice 9", "Choice 10", 4192 "Choice 11", "Choice 12", "Choice 13", "Choice 14", "Choice 15", 4193 "Choice 16", "Choice 17", "Choice 18", "Choice 19", "Choice 20", | 5783 "Choice 6", "Choice 7", "Choice 8", "Choice 9", "Choice 10", 5784 "Choice 11", "Choice 12", "Choice 13", "Choice 14", "Choice 15", 5785 "Choice 16", "Choice 17", "Choice 18", "Choice 19", "Choice 20", |
4194 "Exit", 4195 (char *)NULL, 4196 }; 4197 4198int main() | 5786 "Exit", 5787 (char *)NULL, 5788 }; 5789 5790int main() |
4199{ ITEM **my_items; 4200 int c; 4201 MENU *my_menu; | 5791{ ITEM **my_items; 5792 int c; 5793 MENU *my_menu; |
4202 WINDOW *my_menu_win; 4203 int n_choices, i; | 5794 WINDOW *my_menu_win; 5795 int n_choices, i; |
4204 4205 /* Initialize curses */ 4206 initscr(); 4207 start_color(); | 5796 5797 /* Initialize curses */ 5798 initscr(); 5799 start_color(); |
4208 cbreak(); 4209 noecho(); | 5800 cbreak(); 5801 noecho(); |
4210 keypad(stdscr, TRUE); 4211 init_pair(1, COLOR_RED, COLOR_BLACK); 4212 init_pair(2, COLOR_CYAN, COLOR_BLACK); | 5802 keypad(stdscr, TRUE); 5803 init_pair(1, COLOR_RED, COLOR_BLACK); 5804 init_pair(2, COLOR_CYAN, COLOR_BLACK); |
4213 | 5805 |
4214 /* Create items */ | 5806 /* Create items */ |
4215 n_choices = ARRAY_SIZE(choices); 4216 my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *)); | 5807 n_choices = ARRAY_SIZE(choices); 5808 my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *)); |
4217 for(i = 0; i < n_choices; ++i) | 5809 for(i = 0; i < n_choices; ++i) |
4218 my_items[i] = new_item(choices[i], choices[i]); 4219 | 5810 my_items[i] = new_item(choices[i], choices[i]); 5811 |
4220 /* Crate menu */ 4221 my_menu = new_menu((ITEM **)my_items); | 5812 /* Crate menu */ 5813 my_menu = new_menu((ITEM **)my_items); |
4222 | 5814 |
4223 /* Set menu option not to show the description */ 4224 menu_opts_off(my_menu, O_SHOWDESC); | 5815 /* Set menu option not to show the description */ 5816 menu_opts_off(my_menu, O_SHOWDESC); |
4225 | 5817 |
4226 /* Create the window to be associated with the menu */ | 5818 /* Create the window to be associated with the menu */ |
4227 my_menu_win = newwin(10, 70, 4, 4); 4228 keypad(my_menu_win, TRUE); 4229 | 5819 my_menu_win = newwin(10, 70, 4, 4); 5820 keypad(my_menu_win, TRUE); 5821 |
4230 /* Set main window and sub window */ | 5822 /* Set main window and sub window */ |
4231 set_menu_win(my_menu, my_menu_win); 4232 set_menu_sub(my_menu, derwin(my_menu_win, 6, 68, 3, 1)); | 5823 set_menu_win(my_menu, my_menu_win); 5824 set_menu_sub(my_menu, derwin(my_menu_win, 6, 68, 3, 1)); |
4233 set_menu_format(my_menu, 5, 3); 4234 set_menu_mark(my_menu, " * "); | 5825 set_menu_format(my_menu, 5, 3); 5826 set_menu_mark(my_menu, " * "); |
4235 | 5827 |
4236 /* Print a border around the main window and print a title */ | 5828 /* Print a border around the main window and print a title */ |
4237 box(my_menu_win, 0, 0); | 5829 box(my_menu_win, 0, 0); |
4238 4239 attron(COLOR_PAIR(2)); 4240 mvprintw(LINES - 3, 0, "Use PageUp and PageDown to scroll"); 4241 mvprintw(LINES - 2, 0, "Use Arrow Keys to navigate (F1 to Exit)"); 4242 attroff(COLOR_PAIR(2)); 4243 refresh(); | 5830 5831 attron(COLOR_PAIR(2)); 5832 mvprintw(LINES - 3, 0, "Use PageUp and PageDown to scroll"); 5833 mvprintw(LINES - 2, 0, "Use Arrow Keys to navigate (F1 to Exit)"); 5834 attroff(COLOR_PAIR(2)); 5835 refresh(); |
4244 | 5836 |
4245 /* Post the menu */ 4246 post_menu(my_menu); 4247 wrefresh(my_menu_win); 4248 4249 while((c = wgetch(my_menu_win)) != KEY_F(1)) 4250 { switch(c) 4251 { case KEY_DOWN: 4252 menu_driver(my_menu, REQ_DOWN_ITEM); 4253 break; 4254 case KEY_UP: 4255 menu_driver(my_menu, REQ_UP_ITEM); 4256 break; 4257 case KEY_LEFT: 4258 menu_driver(my_menu, REQ_LEFT_ITEM); 4259 break; 4260 case KEY_RIGHT: 4261 menu_driver(my_menu, REQ_RIGHT_ITEM); 4262 break; 4263 case KEY_NPAGE: 4264 menu_driver(my_menu, REQ_SCR_DPAGE); 4265 break; 4266 case KEY_PPAGE: 4267 menu_driver(my_menu, REQ_SCR_UPAGE); 4268 break; 4269 } | 5837 /* Post the menu */ 5838 post_menu(my_menu); 5839 wrefresh(my_menu_win); 5840 5841 while((c = wgetch(my_menu_win)) != KEY_F(1)) 5842 { switch(c) 5843 { case KEY_DOWN: 5844 menu_driver(my_menu, REQ_DOWN_ITEM); 5845 break; 5846 case KEY_UP: 5847 menu_driver(my_menu, REQ_UP_ITEM); 5848 break; 5849 case KEY_LEFT: 5850 menu_driver(my_menu, REQ_LEFT_ITEM); 5851 break; 5852 case KEY_RIGHT: 5853 menu_driver(my_menu, REQ_RIGHT_ITEM); 5854 break; 5855 case KEY_NPAGE: 5856 menu_driver(my_menu, REQ_SCR_DPAGE); 5857 break; 5858 case KEY_PPAGE: 5859 menu_driver(my_menu, REQ_SCR_UPAGE); 5860 break; 5861 } |
4270 wrefresh(my_menu_win); | 5862 wrefresh(my_menu_win); |
4271 } | 5863 } |
4272 | 5864 |
4273 /* Unpost and free all the memory taken up */ | 5865 /* Unpost and free all the memory taken up */ |
4274 unpost_menu(my_menu); 4275 free_menu(my_menu); | 5866 unpost_menu(my_menu); 5867 free_menu(my_menu); |
4276 for(i = 0; i < n_choices; ++i) | 5868 for(i = 0; i < n_choices; ++i) |
4277 free_item(my_items[i]); | 5869 free_item(my_items[i]); |
4278 endwin(); 4279}</span></font> 4280</pre></td> 4281</tr> 4282</table> 4283</div> 4284<p>Watch the function call to set_menu_format(). It specifies the 4285number of columns to be 3, thus displaying 3 items per row. We have 4286also switched off the showing descriptions with the function 4287menu_opts_off(). There are couple of functions set_menu_opts(), 4288menu_opts_on() and menu_opts() which can be used to manipulate menu 4289options. The following menu options can be specified.</p> 4290<table border="0" bgcolor="#E0E0E0" width="100%"> 4291<tr> 4292<td> 4293<pre class="PROGRAMLISTING"> 4294<font color="#000000"> O_ONEVALUE | 5870 endwin(); 5871}</SPAN 5872></PRE 5873></DIV 5874><P 5875>Watch the function call to set_menu_format(). It specifies the number of columns 5876to be 3, thus displaying 3 items per row. We have also switched off the showing 5877descriptions with the function menu_opts_off(). There are couple of functions 5878set_menu_opts(), menu_opts_on() and menu_opts() which can be used to manipulate 5879menu options. The following menu options can be specified.</P 5880><PRE 5881CLASS="PROGRAMLISTING" 5882> O_ONEVALUE |
4295 Only one item can be selected for this menu. 4296 4297 O_SHOWDESC 4298 Display the item descriptions when the menu is 4299 posted. 4300 4301 O_ROWMAJOR 4302 Display the menu in row-major order. 4303 4304 O_IGNORECASE 4305 Ignore the case when pattern-matching. 4306 4307 O_SHOWMATCH 4308 Move the cursor to within the item name while pat­ 4309 tern-matching. 4310 4311 O_NONCYCLIC 4312 Don't wrap around next-item and previous-item, | 5883 Only one item can be selected for this menu. 5884 5885 O_SHOWDESC 5886 Display the item descriptions when the menu is 5887 posted. 5888 5889 O_ROWMAJOR 5890 Display the menu in row-major order. 5891 5892 O_IGNORECASE 5893 Ignore the case when pattern-matching. 5894 5895 O_SHOWMATCH 5896 Move the cursor to within the item name while pat­ 5897 tern-matching. 5898 5899 O_NONCYCLIC 5900 Don't wrap around next-item and previous-item, |
4313 requests to the other end of the menu.</font> 4314</pre></td> 4315</tr> 4316</table> 4317<p>All options are on by default. You can switch specific 4318attributes on or off with menu_opts_on() and menu_opts_off() 4319functions. You can also use set_menu_opts() to directly specify the 4320options. The argument to this function should be a OR ed value of 4321some of those above constants. The function menu_opts() can be used 4322to find out a menu's present options.</p> 4323</div> 4324<div class="SECT2"> 4325<hr> 4326<h3 class="SECT2"><a name="MULTIVALUEMENUS" id= 4327"MULTIVALUEMENUS">17.7. Multi Valued Menus</a></h3> 4328<p>You might be wondering what if you switch off the option 4329O_ONEVALUE. Then the menu becomes multi-valued. That means you can 4330select more than one item. This brings us to the request 4331REQ_TOGGLE_ITEM. Let's see it in action.</p> 4332<div class="EXAMPLE"><a name="MMETO" id="MMETO"></a> 4333<p><b>Example 22. Multi Valued Menus example</b></p> 4334<table border="0" bgcolor="#E0E0E0" width="100%"> 4335<tr> 4336<td> 4337<pre class="PROGRAMLISTING"> 4338<font color="#000000"><span class= 4339"INLINEMEDIAOBJECT">#include <curses.h> 4340#include <menu.h> | 5901 requests to the other end of the menu.</PRE 5902><P 5903>All options are on by default. You can switch specific attributes on or off with 5904menu_opts_on() and menu_opts_off() functions. You can also use set_menu_opts() 5905to directly specify the options. The argument to this function should be a OR ed 5906value of some of those above constants. The function menu_opts() can be used to 5907find out a menu's present options. </P 5908></DIV 5909><DIV 5910CLASS="SECT2" 5911><HR><H3 5912CLASS="SECT2" 5913><A 5914NAME="MULTIVALUEMENUS" 5915>17.7. Multi Valued Menus</A 5916></H3 5917><P 5918>You might be wondering what if you switch off the option O_ONEVALUE. Then the 5919menu becomes multi-valued. That means you can select more than one item. This 5920brings us to the request REQ_TOGGLE_ITEM. Let's see it in action.</P 5921><DIV 5922CLASS="EXAMPLE" 5923><A 5924NAME="MMETO" 5925></A 5926><P 5927><B 5928>Example 22. Multi Valued Menus example </B 5929></P 5930><PRE 5931CLASS="PROGRAMLISTING" 5932><SPAN 5933CLASS="INLINEMEDIAOBJECT" 5934>#include <curses.h> 5935#include <menu.h> |
4341 4342#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) | 5936 5937#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) |
4343#define CTRLD 4 | 5938#define CTRLD 4 |
4344 4345char *choices[] = { 4346 "Choice 1", 4347 "Choice 2", 4348 "Choice 3", 4349 "Choice 4", | 5939 5940char *choices[] = { 5941 "Choice 1", 5942 "Choice 2", 5943 "Choice 3", 5944 "Choice 4", |
4350 "Choice 5", 4351 "Choice 6", 4352 "Choice 7", | 5945 "Choice 5", 5946 "Choice 6", 5947 "Choice 7", |
4353 "Exit", 4354 }; 4355 4356int main() | 5948 "Exit", 5949 }; 5950 5951int main() |
4357{ ITEM **my_items; 4358 int c; 4359 MENU *my_menu; | 5952{ ITEM **my_items; 5953 int c; 5954 MENU *my_menu; |
4360 int n_choices, i; | 5955 int n_choices, i; |
4361 ITEM *cur_item; 4362 4363 /* Initialize curses */ 4364 initscr(); | 5956 ITEM *cur_item; 5957 5958 /* Initialize curses */ 5959 initscr(); |
4365 cbreak(); 4366 noecho(); | 5960 cbreak(); 5961 noecho(); |
4367 keypad(stdscr, TRUE); | 5962 keypad(stdscr, TRUE); |
4368 | 5963 |
4369 /* Initialize items */ | 5964 /* Initialize items */ |
4370 n_choices = ARRAY_SIZE(choices); 4371 my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); | 5965 n_choices = ARRAY_SIZE(choices); 5966 my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); |
4372 for(i = 0; i < n_choices; ++i) | 5967 for(i = 0; i < n_choices; ++i) |
4373 my_items[i] = new_item(choices[i], choices[i]); | 5968 my_items[i] = new_item(choices[i], choices[i]); |
4374 my_items[n_choices] = (ITEM *)NULL; | 5969 my_items[n_choices] = (ITEM *)NULL; |
4375 | 5970 |
4376 my_menu = new_menu((ITEM **)my_items); | 5971 my_menu = new_menu((ITEM **)my_items); |
4377 | 5972 |
4378 /* Make the menu multi valued */ 4379 menu_opts_off(my_menu, O_ONEVALUE); | 5973 /* Make the menu multi valued */ 5974 menu_opts_off(my_menu, O_ONEVALUE); |
4380 | 5975 |
4381 mvprintw(LINES - 3, 0, "Use <SPACE> to select or unselect an item."); 4382 mvprintw(LINES - 2, 0, "<ENTER> to see presently selected items(F1 to Exit)"); 4383 post_menu(my_menu); 4384 refresh(); | 5976 mvprintw(LINES - 3, 0, "Use <SPACE> to select or unselect an item."); 5977 mvprintw(LINES - 2, 0, "<ENTER> to see presently selected items(F1 to Exit)"); 5978 post_menu(my_menu); 5979 refresh(); |
4385 | 5980 |
4386 while((c = getch()) != KEY_F(1)) 4387 { switch(c) 4388 { case KEY_DOWN: 4389 menu_driver(my_menu, REQ_DOWN_ITEM); 4390 break; 4391 case KEY_UP: 4392 menu_driver(my_menu, REQ_UP_ITEM); 4393 break; 4394 case ' ': 4395 menu_driver(my_menu, REQ_TOGGLE_ITEM); 4396 break; 4397 case 10: /* Enter */ 4398 { char temp[200]; 4399 ITEM **items; | 5981 while((c = getch()) != KEY_F(1)) 5982 { switch(c) 5983 { case KEY_DOWN: 5984 menu_driver(my_menu, REQ_DOWN_ITEM); 5985 break; 5986 case KEY_UP: 5987 menu_driver(my_menu, REQ_UP_ITEM); 5988 break; 5989 case ' ': 5990 menu_driver(my_menu, REQ_TOGGLE_ITEM); 5991 break; 5992 case 10: /* Enter */ 5993 { char temp[200]; 5994 ITEM **items; |
4400 | 5995 |
4401 items = menu_items(my_menu); 4402 temp[0] = '\0'; 4403 for(i = 0; i < item_count(my_menu); ++i) 4404 if(item_value(items[i]) == TRUE) 4405 { strcat(temp, item_name(items[i])); 4406 strcat(temp, " "); 4407 } 4408 move(20, 0); 4409 clrtoeol(); 4410 mvprintw(20, 0, temp); 4411 refresh(); 4412 } 4413 break; 4414 } 4415 } | 5996 items = menu_items(my_menu); 5997 temp[0] = '\0'; 5998 for(i = 0; i < item_count(my_menu); ++i) 5999 if(item_value(items[i]) == TRUE) 6000 { strcat(temp, item_name(items[i])); 6001 strcat(temp, " "); 6002 } 6003 move(20, 0); 6004 clrtoeol(); 6005 mvprintw(20, 0, temp); 6006 refresh(); 6007 } 6008 break; 6009 } 6010 } |
4416 | 6011 |
4417 free_item(my_items[0]); | 6012 free_item(my_items[0]); |
4418 free_item(my_items[1]); | 6013 free_item(my_items[1]); |
4419 free_menu(my_menu); 4420 endwin(); | 6014 free_menu(my_menu); 6015 endwin(); |
4421} | 6016} |
4422 </span></font> 4423</pre></td> 4424</tr> 4425</table> 4426</div> 4427<p>Whew, A lot of new functions. Let's take them one after another. 4428Firstly, the REQ_TOGGLE_ITEM. In a multi-valued menu, the user 4429should be allowed to select or un select more than one item. The 4430request REQ_TOGGLE_ITEM toggles the present selection. In this case 4431when space is pressed REQ_TOGGLE_ITEM request is sent to 4432menu_driver to achieve the result.</p> 4433<p>Now when the user presses <ENTER> we show the items he 4434presently selected. First we find out the items associated with the 4435menu using the function menu_items(). Then we loop through the 4436items to find out if the item is selected or not. The function 4437item_value() returns TRUE if an item is selected. The function 4438item_count() returns the number of items in the menu. The item name 4439can be found with item_name(). You can also find the description 4440associated with an item using item_description().</p> 4441</div> 4442<div class="SECT2"> 4443<hr> 4444<h3 class="SECT2"><a name="MENUOPT" id="MENUOPT">17.8. Menu 4445Options</a></h3> 4446<p>Well, by this time you must be itching for some difference in 4447your menu, with lots of functionality. I know. You want Colors !!!. 4448You want to create nice menus similar to those text mode <a href= 4449"http://www.jersey.net/~debinjoe/games/" target="_top">dos 4450games</a>. The functions set_menu_fore() and set_menu_back() can be 4451used to change the attribute of the selected item and unselected 4452item. The names are misleading. They don't change menu's foreground 4453or background which would have been useless.</p> 4454<p>The function set_menu_grey() can be used to set the display 4455attribute for the non-selectable items in the menu. This brings us 4456to the interesting option for an item the one and only 4457O_SELECTABLE. We can turn it off by the function item_opts_off() 4458and after that that item is not selectable. It's like a grayed item 4459in those fancy windows menus. Let's put these concepts in practice 4460with this example</p> 4461<div class="EXAMPLE"><a name="MMEAT" id="MMEAT"></a> 4462<p><b>Example 23. Menu Options example</b></p> 4463<table border="0" bgcolor="#E0E0E0" width="100%"> 4464<tr> 4465<td> 4466<pre class="PROGRAMLISTING"> 4467<font color="#000000"><span class= 4468"INLINEMEDIAOBJECT">#include <menu.h> | 6017 </SPAN 6018></PRE 6019></DIV 6020><P 6021>Whew, A lot of new functions. Let's take them one after another. Firstly, the 6022REQ_TOGGLE_ITEM. In a multi-valued menu, the user should be allowed to select 6023or un select more than one item. The request REQ_TOGGLE_ITEM toggles the present 6024selection. In this case when space is pressed REQ_TOGGLE_ITEM request is sent to 6025menu_driver to achieve the result.</P 6026><P 6027>Now when the user presses <ENTER> we show the items he presently selected. 6028First we find out the items associated with the menu using the function 6029menu_items(). Then we loop through the items to find out if the item is selected 6030or not. The function item_value() returns TRUE if an item is selected. The 6031function item_count() returns the number of items in the menu. The item name can 6032be found with item_name(). You can also find the description associated with an 6033item using item_description().</P 6034></DIV 6035><DIV 6036CLASS="SECT2" 6037><HR><H3 6038CLASS="SECT2" 6039><A 6040NAME="MENUOPT" 6041>17.8. Menu Options</A 6042></H3 6043><P 6044>Well, by this time you must be itching for some difference in your menu, with 6045lots of functionality. I know. You want Colors !!!. You want to create nice 6046menus similar to those text mode <A 6047HREF="http://www.jersey.net/~debinjoe/games/" 6048TARGET="_top" 6049>dos games</A 6050>. The functions 6051set_menu_fore() and set_menu_back() can be used to change the attribute of the 6052selected item and unselected item. The names are misleading. They don't change 6053menu's foreground or background which would have been useless. </P 6054><P 6055>The function set_menu_grey() can be used to set the display attribute for the 6056non-selectable items in the menu. This brings us to the interesting option for 6057an item the one and only O_SELECTABLE. We can turn it off by the function 6058item_opts_off() and after that that item is not selectable. It's like a grayed 6059item in those fancy windows menus. Let's put these concepts in practice with 6060this example</P 6061><DIV 6062CLASS="EXAMPLE" 6063><A 6064NAME="MMEAT" 6065></A 6066><P 6067><B 6068>Example 23. Menu Options example </B 6069></P 6070><PRE 6071CLASS="PROGRAMLISTING" 6072><SPAN 6073CLASS="INLINEMEDIAOBJECT" 6074>#include <menu.h> |
4469 4470#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) | 6075 6076#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) |
4471#define CTRLD 4 | 6077#define CTRLD 4 |
4472 4473char *choices[] = { 4474 "Choice 1", 4475 "Choice 2", 4476 "Choice 3", 4477 "Choice 4", | 6078 6079char *choices[] = { 6080 "Choice 1", 6081 "Choice 2", 6082 "Choice 3", 6083 "Choice 4", |
4478 "Choice 5", 4479 "Choice 6", 4480 "Choice 7", | 6084 "Choice 5", 6085 "Choice 6", 6086 "Choice 7", |
4481 "Exit", 4482 }; 4483 4484int main() | 6087 "Exit", 6088 }; 6089 6090int main() |
4485{ ITEM **my_items; 4486 int c; 4487 MENU *my_menu; | 6091{ ITEM **my_items; 6092 int c; 6093 MENU *my_menu; |
4488 int n_choices, i; | 6094 int n_choices, i; |
4489 ITEM *cur_item; 4490 4491 /* Initialize curses */ 4492 initscr(); 4493 start_color(); | 6095 ITEM *cur_item; 6096 6097 /* Initialize curses */ 6098 initscr(); 6099 start_color(); |
4494 cbreak(); 4495 noecho(); | 6100 cbreak(); 6101 noecho(); |
4496 keypad(stdscr, TRUE); 4497 init_pair(1, COLOR_RED, COLOR_BLACK); 4498 init_pair(2, COLOR_GREEN, COLOR_BLACK); 4499 init_pair(3, COLOR_MAGENTA, COLOR_BLACK); | 6102 keypad(stdscr, TRUE); 6103 init_pair(1, COLOR_RED, COLOR_BLACK); 6104 init_pair(2, COLOR_GREEN, COLOR_BLACK); 6105 init_pair(3, COLOR_MAGENTA, COLOR_BLACK); |
4500 | 6106 |
4501 /* Initialize items */ | 6107 /* Initialize items */ |
4502 n_choices = ARRAY_SIZE(choices); 4503 my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); | 6108 n_choices = ARRAY_SIZE(choices); 6109 my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); |
4504 for(i = 0; i < n_choices; ++i) | 6110 for(i = 0; i < n_choices; ++i) |
4505 my_items[i] = new_item(choices[i], choices[i]); | 6111 my_items[i] = new_item(choices[i], choices[i]); |
4506 my_items[n_choices] = (ITEM *)NULL; 4507 item_opts_off(my_items[3], O_SELECTABLE); 4508 item_opts_off(my_items[6], O_SELECTABLE); | 6112 my_items[n_choices] = (ITEM *)NULL; 6113 item_opts_off(my_items[3], O_SELECTABLE); 6114 item_opts_off(my_items[6], O_SELECTABLE); |
4509 | 6115 |
4510 /* Create menu */ 4511 my_menu = new_menu((ITEM **)my_items); | 6116 /* Create menu */ 6117 my_menu = new_menu((ITEM **)my_items); |
4512 | 6118 |
4513 /* Set fore ground and back ground of the menu */ 4514 set_menu_fore(my_menu, COLOR_PAIR(1) | A_REVERSE); 4515 set_menu_back(my_menu, COLOR_PAIR(2)); 4516 set_menu_grey(my_menu, COLOR_PAIR(3)); | 6119 /* Set fore ground and back ground of the menu */ 6120 set_menu_fore(my_menu, COLOR_PAIR(1) | A_REVERSE); 6121 set_menu_back(my_menu, COLOR_PAIR(2)); 6122 set_menu_grey(my_menu, COLOR_PAIR(3)); |
4517 | 6123 |
4518 /* Post the menu */ 4519 mvprintw(LINES - 3, 0, "Press <ENTER> to see the option selected"); 4520 mvprintw(LINES - 2, 0, "Up and Down arrow keys to naviage (F1 to Exit)"); 4521 post_menu(my_menu); 4522 refresh(); | 6124 /* Post the menu */ 6125 mvprintw(LINES - 3, 0, "Press <ENTER> to see the option selected"); 6126 mvprintw(LINES - 2, 0, "Up and Down arrow keys to naviage (F1 to Exit)"); 6127 post_menu(my_menu); 6128 refresh(); |
4523 | 6129 |
4524 while((c = getch()) != KEY_F(1)) 4525 { switch(c) 4526 { case KEY_DOWN: 4527 menu_driver(my_menu, REQ_DOWN_ITEM); 4528 break; 4529 case KEY_UP: 4530 menu_driver(my_menu, REQ_UP_ITEM); 4531 break; 4532 case 10: /* Enter */ 4533 move(20, 0); 4534 clrtoeol(); 4535 mvprintw(20, 0, "Item selected is : %s", 4536 item_name(current_item(my_menu))); 4537 pos_menu_cursor(my_menu); 4538 break; 4539 } 4540 } 4541 unpost_menu(my_menu); 4542 for(i = 0; i < n_choices; ++i) 4543 free_item(my_items[i]); 4544 free_menu(my_menu); 4545 endwin(); | 6130 while((c = getch()) != KEY_F(1)) 6131 { switch(c) 6132 { case KEY_DOWN: 6133 menu_driver(my_menu, REQ_DOWN_ITEM); 6134 break; 6135 case KEY_UP: 6136 menu_driver(my_menu, REQ_UP_ITEM); 6137 break; 6138 case 10: /* Enter */ 6139 move(20, 0); 6140 clrtoeol(); 6141 mvprintw(20, 0, "Item selected is : %s", 6142 item_name(current_item(my_menu))); 6143 pos_menu_cursor(my_menu); 6144 break; 6145 } 6146 } 6147 unpost_menu(my_menu); 6148 for(i = 0; i < n_choices; ++i) 6149 free_item(my_items[i]); 6150 free_menu(my_menu); 6151 endwin(); |
4546} | 6152} |
4547 </span></font> 4548</pre></td> 4549</tr> 4550</table> 4551</div> 4552</div> 4553<div class="SECT2"> 4554<hr> 4555<h3 class="SECT2"><a name="MENUUSERPTR" id="MENUUSERPTR">17.9. The 4556useful User Pointer</a></h3> 4557<p>We can associate a user pointer with each item in the menu. It 4558works the same way as user pointer in panels. It's not touched by 4559menu system. You can store any thing you like in that. I usually 4560use it to store the function to be executed when the menu option is 4561chosen (It's selected and may be the user pressed 4562<ENTER>);</p> 4563<div class="EXAMPLE"><a name="MMEUS" id="MMEUS"></a> 4564<p><b>Example 24. Menu User Pointer Usage</b></p> 4565<table border="0" bgcolor="#E0E0E0" width="100%"> 4566<tr> 4567<td> 4568<pre class="PROGRAMLISTING"> 4569<font color="#000000"><span class= 4570"INLINEMEDIAOBJECT">#include <curses.h> 4571#include <menu.h> | 6153 </SPAN 6154></PRE 6155></DIV 6156></DIV 6157><DIV 6158CLASS="SECT2" 6159><HR><H3 6160CLASS="SECT2" 6161><A 6162NAME="MENUUSERPTR" 6163>17.9. The useful User Pointer</A 6164></H3 6165><P 6166>We can associate a user pointer with each item in the menu. It works the same 6167way as user pointer in panels. It's not touched by menu system. You can store 6168any thing you like in that. I usually use it to store the function to be 6169executed when the menu option is chosen (It's selected and may be the user 6170pressed <ENTER>);</P 6171><DIV 6172CLASS="EXAMPLE" 6173><A 6174NAME="MMEUS" 6175></A 6176><P 6177><B 6178>Example 24. Menu User Pointer Usage </B 6179></P 6180><PRE 6181CLASS="PROGRAMLISTING" 6182><SPAN 6183CLASS="INLINEMEDIAOBJECT" 6184>#include <curses.h> 6185#include <menu.h> |
4572 4573#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) | 6186 6187#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) |
4574#define CTRLD 4 | 6188#define CTRLD 4 |
4575 4576char *choices[] = { 4577 "Choice 1", 4578 "Choice 2", 4579 "Choice 3", 4580 "Choice 4", | 6189 6190char *choices[] = { 6191 "Choice 1", 6192 "Choice 2", 6193 "Choice 3", 6194 "Choice 4", |
4581 "Choice 5", 4582 "Choice 6", 4583 "Choice 7", | 6195 "Choice 5", 6196 "Choice 6", 6197 "Choice 7", |
4584 "Exit", 4585 }; 4586void func(char *name); 4587 4588int main() | 6198 "Exit", 6199 }; 6200void func(char *name); 6201 6202int main() |
4589{ ITEM **my_items; 4590 int c; 4591 MENU *my_menu; | 6203{ ITEM **my_items; 6204 int c; 6205 MENU *my_menu; |
4592 int n_choices, i; | 6206 int n_choices, i; |
4593 ITEM *cur_item; 4594 4595 /* Initialize curses */ 4596 initscr(); 4597 start_color(); | 6207 ITEM *cur_item; 6208 6209 /* Initialize curses */ 6210 initscr(); 6211 start_color(); |
4598 cbreak(); 4599 noecho(); | 6212 cbreak(); 6213 noecho(); |
4600 keypad(stdscr, TRUE); 4601 init_pair(1, COLOR_RED, COLOR_BLACK); 4602 init_pair(2, COLOR_GREEN, COLOR_BLACK); 4603 init_pair(3, COLOR_MAGENTA, COLOR_BLACK); | 6214 keypad(stdscr, TRUE); 6215 init_pair(1, COLOR_RED, COLOR_BLACK); 6216 init_pair(2, COLOR_GREEN, COLOR_BLACK); 6217 init_pair(3, COLOR_MAGENTA, COLOR_BLACK); |
4604 | 6218 |
4605 /* Initialize items */ | 6219 /* Initialize items */ |
4606 n_choices = ARRAY_SIZE(choices); 4607 my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); | 6220 n_choices = ARRAY_SIZE(choices); 6221 my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); |
4608 for(i = 0; i < n_choices; ++i) 4609 { my_items[i] = new_item(choices[i], choices[i]); 4610 /* Set the user pointer */ 4611 set_item_userptr(my_items[i], func); 4612 } 4613 my_items[n_choices] = (ITEM *)NULL; | 6222 for(i = 0; i < n_choices; ++i) 6223 { my_items[i] = new_item(choices[i], choices[i]); 6224 /* Set the user pointer */ 6225 set_item_userptr(my_items[i], func); 6226 } 6227 my_items[n_choices] = (ITEM *)NULL; |
4614 | 6228 |
4615 /* Create menu */ 4616 my_menu = new_menu((ITEM **)my_items); | 6229 /* Create menu */ 6230 my_menu = new_menu((ITEM **)my_items); |
4617 | 6231 |
4618 /* Post the menu */ 4619 mvprintw(LINES - 3, 0, "Press <ENTER> to see the option selected"); 4620 mvprintw(LINES - 2, 0, "Up and Down arrow keys to naviage (F1 to Exit)"); 4621 post_menu(my_menu); 4622 refresh(); | 6232 /* Post the menu */ 6233 mvprintw(LINES - 3, 0, "Press <ENTER> to see the option selected"); 6234 mvprintw(LINES - 2, 0, "Up and Down arrow keys to naviage (F1 to Exit)"); 6235 post_menu(my_menu); 6236 refresh(); |
4623 | 6237 |
4624 while((c = getch()) != KEY_F(1)) 4625 { switch(c) 4626 { case KEY_DOWN: 4627 menu_driver(my_menu, REQ_DOWN_ITEM); 4628 break; 4629 case KEY_UP: 4630 menu_driver(my_menu, REQ_UP_ITEM); 4631 break; 4632 case 10: /* Enter */ 4633 { ITEM *cur; 4634 void (*p)(char *); | 6238 while((c = getch()) != KEY_F(1)) 6239 { switch(c) 6240 { case KEY_DOWN: 6241 menu_driver(my_menu, REQ_DOWN_ITEM); 6242 break; 6243 case KEY_UP: 6244 menu_driver(my_menu, REQ_UP_ITEM); 6245 break; 6246 case 10: /* Enter */ 6247 { ITEM *cur; 6248 void (*p)(char *); |
4635 | 6249 |
4636 cur = current_item(my_menu); 4637 p = item_userptr(cur); 4638 p((char *)item_name(cur)); 4639 pos_menu_cursor(my_menu); 4640 break; 4641 } 4642 break; 4643 } 4644 } 4645 unpost_menu(my_menu); 4646 for(i = 0; i < n_choices; ++i) 4647 free_item(my_items[i]); 4648 free_menu(my_menu); 4649 endwin(); | 6250 cur = current_item(my_menu); 6251 p = item_userptr(cur); 6252 p((char *)item_name(cur)); 6253 pos_menu_cursor(my_menu); 6254 break; 6255 } 6256 break; 6257 } 6258 } 6259 unpost_menu(my_menu); 6260 for(i = 0; i < n_choices; ++i) 6261 free_item(my_items[i]); 6262 free_menu(my_menu); 6263 endwin(); |
4650} 4651 4652void func(char *name) | 6264} 6265 6266void func(char *name) |
4653{ move(20, 0); 4654 clrtoeol(); 4655 mvprintw(20, 0, "Item selected is : %s", name); 4656} </span></font> 4657</pre></td> 4658</tr> 4659</table> 4660</div> 4661</div> 4662</div> 4663<div class="SECT1"> 4664<hr> 4665<h2 class="SECT1"><a name="FORMS" id="FORMS">18. Forms 4666Library</a></h2> 4667<p>Well. If you have seen those forms on web pages which take input 4668from users and do various kinds of things, you might be wondering 4669how would any one create such forms in text mode display. It's 4670quite difficult to write those nifty forms in plain ncurses. Forms 4671library tries to provide a basic frame work to build and maintain 4672forms with ease. It has lot of features(functions) which manage 4673validation, dynamic expansion of fields etc.. Let's see it in full 4674flow.</p> 4675<p>A form is a collection of fields; each field can be either a 4676label(static text) or a data-entry location. The forms also library 4677provides functions to divide forms into multiple pages.</p> 4678<div class="SECT2"> 4679<hr> 4680<h3 class="SECT2"><a name="FORMBASICS" id="FORMBASICS">18.1. The 4681Basics</a></h3> 4682<p>Forms are created in much the same way as menus. First the 4683fields related to the form are created with new_field(). You can 4684set options for the fields, so that they can be displayed with some 4685fancy attributes, validated before the field looses focus etc.. 4686Then the fields are attached to form. After this, the form can be 4687posted to display and is ready to receive inputs. On the similar 4688lines to menu_driver(), the form is manipulated with form_driver(). 4689We can send requests to form_driver to move focus to a certain 4690field, move cursor to end of the field etc.. After the user enters 4691values in the fields and validation done, form can be unposted and 4692memory allocated can be freed.</p> 4693<p>The general flow of control of a forms program looks like 4694this.</p> 4695<ol type="1"> 4696<li> 4697<p>Initialize curses</p> 4698</li> 4699<li> 4700<p>Create fields using new_field(). You can specify the height and 4701width of the field, and its position on the form.</p> 4702</li> 4703<li> 4704<p>Create the forms with new_form() by specifying the fields to be 4705attached with.</p> 4706</li> 4707<li> 4708<p>Post the form with form_post() and refresh the screen.</p> 4709</li> 4710<li> 4711<p>Process the user requests with a loop and do necessary updates 4712to form with form_driver.</p> 4713</li> 4714<li> 4715<p>Unpost the menu with form_unpost()</p> 4716</li> 4717<li> 4718<p>Free the memory allocated to menu by free_form()</p> 4719</li> 4720<li> 4721<p>Free the memory allocated to the items with free_field()</p> 4722</li> 4723<li> 4724<p>End curses</p> 4725</li> 4726</ol> 4727<p>As you can see, working with forms library is much similar to 4728handling menu library. The following examples will explore various 4729aspects of form processing. Let's start the journey with a simple 4730example. first.</p> 4731</div> 4732<div class="SECT2"> 4733<hr> 4734<h3 class="SECT2"><a name="COMPILEFORMS" id="COMPILEFORMS">18.2. 4735Compiling With the Forms Library</a></h3> 4736<p>To use forms library functions, you have to include form.h and 4737to link the program with forms library the flag -lform should be 4738added along with -lncurses in that order.</p> 4739<table border="0" bgcolor="#E0E0E0" width="100%"> 4740<tr> 4741<td> 4742<pre class="PROGRAMLISTING"> 4743<font color="#000000"> #include <form.h> | 6267{ move(20, 0); 6268 clrtoeol(); 6269 mvprintw(20, 0, "Item selected is : %s", name); 6270} </SPAN 6271></PRE 6272></DIV 6273></DIV 6274></DIV 6275><DIV 6276CLASS="SECT1" 6277><HR><H2 6278CLASS="SECT1" 6279><A 6280NAME="FORMS" 6281>18. Forms Library</A 6282></H2 6283><P 6284>Well. If you have seen those forms on web pages which take input from users and 6285do various kinds of things, you might be wondering how would any one create such 6286forms in text mode display. It's quite difficult to write those nifty forms in 6287plain ncurses. Forms library tries to provide a basic frame work to build and 6288maintain forms with ease. It has lot of features(functions) which manage 6289validation, dynamic expansion of fields etc.. Let's see it in full flow.</P 6290><P 6291>A form is a collection of fields; each field can be either a label(static text) 6292or a data-entry location. The forms also library provides functions to divide 6293forms into multiple pages. </P 6294><DIV 6295CLASS="SECT2" 6296><HR><H3 6297CLASS="SECT2" 6298><A 6299NAME="FORMBASICS" 6300>18.1. The Basics</A 6301></H3 6302><P 6303>Forms are created in much the same way as menus. First the fields related to the 6304form are created with new_field(). You can set options for the fields, so that 6305they can be displayed with some fancy attributes, validated before the field 6306looses focus etc.. Then the fields are attached to form. After this, the form 6307can be posted to display and is ready to receive inputs. On the similar lines to 6308menu_driver(), the form is manipulated with form_driver(). We can send requests 6309to form_driver to move focus to a certain field, move cursor to end of the field 6310etc.. After the user enters values in the fields and validation done, form can 6311be unposted and memory allocated can be freed.</P 6312><P 6313>The general flow of control of a forms program looks like this. 6314 6315<P 6316></P 6317><OL 6318TYPE="1" 6319><LI 6320><P 6321>Initialize curses</P 6322></LI 6323><LI 6324><P 6325>Create fields using new_field(). You can specify the height and 6326width of the field, and its position on the form.</P 6327></LI 6328><LI 6329><P 6330>Create the forms with new_form() by specifying the fields to be 6331attached with.</P 6332></LI 6333><LI 6334><P 6335>Post the form with form_post() and refresh the screen.</P 6336></LI 6337><LI 6338><P 6339>Process the user requests with a loop and do necessary updates 6340to form with form_driver.</P 6341></LI 6342><LI 6343><P 6344>Unpost the menu with form_unpost()</P 6345></LI 6346><LI 6347><P 6348>Free the memory allocated to menu by free_form()</P 6349></LI 6350><LI 6351><P 6352>Free the memory allocated to the items with free_field()</P 6353></LI 6354><LI 6355><P 6356>End curses</P 6357></LI 6358></OL 6359></P 6360><P 6361>As you can see, working with forms library is much similar to handling menu 6362library. The following examples will explore various aspects of form 6363processing. Let's start the journey with a simple example. first.</P 6364></DIV 6365><DIV 6366CLASS="SECT2" 6367><HR><H3 6368CLASS="SECT2" 6369><A 6370NAME="COMPILEFORMS" 6371>18.2. Compiling With the Forms Library</A 6372></H3 6373><P 6374>To use forms library functions, you have to include form.h and to link the 6375program with forms library the flag -lform should be added along with -lncurses 6376in that order.</P 6377><PRE 6378CLASS="PROGRAMLISTING" 6379> #include <form.h> |
4744 . 4745 . 4746 . 4747 | 6380 . 6381 . 6382 . 6383 |
4748 compile and link: gcc <program file> -lform -lncurses</font> 4749</pre></td> 4750</tr> 4751</table> 4752<div class="EXAMPLE"><a name="FFOSI" id="FFOSI"></a> 4753<p><b>Example 25. Forms Basics</b></p> 4754<table border="0" bgcolor="#E0E0E0" width="100%"> 4755<tr> 4756<td> 4757<pre class="PROGRAMLISTING"> 4758<font color="#000000"><span class= 4759"INLINEMEDIAOBJECT">#include <form.h> | 6384 compile and link: gcc <program file> -lform -lncurses</PRE 6385><DIV 6386CLASS="EXAMPLE" 6387><A 6388NAME="FFOSI" 6389></A 6390><P 6391><B 6392>Example 25. Forms Basics </B 6393></P 6394><PRE 6395CLASS="PROGRAMLISTING" 6396><SPAN 6397CLASS="INLINEMEDIAOBJECT" 6398>#include <form.h> |
4760 4761int main() | 6399 6400int main() |
4762{ FIELD *field[3]; 4763 FORM *my_form; 4764 int ch; 4765 4766 /* Initialize curses */ 4767 initscr(); 4768 cbreak(); 4769 noecho(); 4770 keypad(stdscr, TRUE); | 6401{ FIELD *field[3]; 6402 FORM *my_form; 6403 int ch; 6404 6405 /* Initialize curses */ 6406 initscr(); 6407 cbreak(); 6408 noecho(); 6409 keypad(stdscr, TRUE); |
4771 | 6410 |
4772 /* Initialize the fields */ 4773 field[0] = new_field(1, 10, 4, 18, 0, 0); 4774 field[1] = new_field(1, 10, 6, 18, 0, 0); 4775 field[2] = NULL; | 6411 /* Initialize the fields */ 6412 field[0] = new_field(1, 10, 4, 18, 0, 0); 6413 field[1] = new_field(1, 10, 6, 18, 0, 0); 6414 field[2] = NULL; |
4776 | 6415 |
4777 /* Set field options */ 4778 set_field_back(field[0], A_UNDERLINE); /* Print a line for the option */ 4779 field_opts_off(field[0], O_AUTOSKIP); /* Don't go to next field when this */ 4780 /* Field is filled up */ 4781 set_field_back(field[1], A_UNDERLINE); 4782 field_opts_off(field[1], O_AUTOSKIP); | 6416 /* Set field options */ 6417 set_field_back(field[0], A_UNDERLINE); /* Print a line for the option */ 6418 field_opts_off(field[0], O_AUTOSKIP); /* Don't go to next field when this */ 6419 /* Field is filled up */ 6420 set_field_back(field[1], A_UNDERLINE); 6421 field_opts_off(field[1], O_AUTOSKIP); |
4783 | 6422 |
4784 /* Create the form and post it */ 4785 my_form = new_form(field); 4786 post_form(my_form); 4787 refresh(); 4788 4789 mvprintw(4, 10, "Value 1:"); 4790 mvprintw(6, 10, "Value 2:"); 4791 refresh(); | 6423 /* Create the form and post it */ 6424 my_form = new_form(field); 6425 post_form(my_form); 6426 refresh(); 6427 6428 mvprintw(4, 10, "Value 1:"); 6429 mvprintw(6, 10, "Value 2:"); 6430 refresh(); |
4792 | 6431 |
4793 /* Loop through to get user requests */ 4794 while((ch = getch()) != KEY_F(1)) 4795 { switch(ch) 4796 { case KEY_DOWN: 4797 /* Go to next field */ 4798 form_driver(my_form, REQ_NEXT_FIELD); 4799 /* Go to the end of the present buffer */ 4800 /* Leaves nicely at the last character */ 4801 form_driver(my_form, REQ_END_LINE); 4802 break; 4803 case KEY_UP: 4804 /* Go to previous field */ 4805 form_driver(my_form, REQ_PREV_FIELD); 4806 form_driver(my_form, REQ_END_LINE); 4807 break; 4808 default: 4809 /* If this is a normal character, it gets */ 4810 /* Printed */ 4811 form_driver(my_form, ch); 4812 break; 4813 } 4814 } | 6432 /* Loop through to get user requests */ 6433 while((ch = getch()) != KEY_F(1)) 6434 { switch(ch) 6435 { case KEY_DOWN: 6436 /* Go to next field */ 6437 form_driver(my_form, REQ_NEXT_FIELD); 6438 /* Go to the end of the present buffer */ 6439 /* Leaves nicely at the last character */ 6440 form_driver(my_form, REQ_END_LINE); 6441 break; 6442 case KEY_UP: 6443 /* Go to previous field */ 6444 form_driver(my_form, REQ_PREV_FIELD); 6445 form_driver(my_form, REQ_END_LINE); 6446 break; 6447 default: 6448 /* If this is a normal character, it gets */ 6449 /* Printed */ 6450 form_driver(my_form, ch); 6451 break; 6452 } 6453 } |
4815 | 6454 |
4816 /* Un post form and free the memory */ 4817 unpost_form(my_form); 4818 free_form(my_form); 4819 free_field(field[0]); 4820 free_field(field[1]); | 6455 /* Un post form and free the memory */ 6456 unpost_form(my_form); 6457 free_form(my_form); 6458 free_field(field[0]); 6459 free_field(field[1]); |
4821 | 6460 |
4822 endwin(); 4823 return 0; 4824}</span></font> 4825</pre></td> 4826</tr> 4827</table> 4828</div> 4829<p>Above example is pretty straight forward. It creates two fields 4830with <var class="LITERAL">new_field()</var>. new_field() takes 4831height, width, starty, startx, number of offscreen rows and number 4832of additional working buffers. The fifth argument number of 4833offscreen rows specifies how much of the field to be shown. If it 4834is zero, the entire field is always displayed otherwise the form 4835will be scrollable when the user accesses not displayed parts of 4836the field. The forms library allocates one buffer per field to 4837store the data user enters. Using the last parameter to new_field() 4838we can specify it to allocate some additional buffers. These can be 4839used for any purpose you like.</p> 4840<p>After creating the fields, back ground attribute of both of them 4841is set to an underscore with set_field_back(). The AUTOSKIP option 4842is turned off using field_opts_off(). If this option is turned on, 4843focus will move to the next field in the form once the active field 4844is filled up completely.</p> 4845<p>After attaching the fields to the form, it is posted. Here on, 4846user inputs are processed in the while loop, by making 4847corresponding requests to form_driver. The details of all the 4848requests to the form_driver() are explained later.</p> 4849</div> 4850<div class="SECT2"> 4851<hr> 4852<h3 class="SECT2"><a name="PLAYFIELDS" id="PLAYFIELDS">18.3. 4853Playing with Fields</a></h3> 4854<p>Each form field is associated with a lot of attributes. They can 4855be manipulated to get the required effect and to have fun !!!. So 4856why wait?</p> 4857<div class="SECT3"> 4858<hr> 4859<h4 class="SECT3"><a name="FETCHINFO" id="FETCHINFO">18.3.1. 4860Fetching Size and Location of Field</a></h4> 4861<p>The parameters we have given at the time of creation of a field 4862can be retrieved with field_info(). It returns height, width, 4863starty, startx, number of offscreen rows, and number of additional 4864buffers into the parameters given to it. It is a sort of inverse of 4865new_field().</p> 4866<table border="0" bgcolor="#E0E0E0" width="100%"> 4867<tr> 4868<td> 4869<pre class="PROGRAMLISTING"> 4870<font color= 4871"#000000">int field_info( FIELD *field, /* field from which to fetch */ | 6461 endwin(); 6462 return 0; 6463}</SPAN 6464></PRE 6465></DIV 6466><P 6467>Above example is pretty straight forward. It creates two fields with 6468<TT 6469CLASS="LITERAL" 6470>new_field()</TT 6471>. new_field() takes height, width, 6472starty, startx, number of offscreen rows and number of additional working 6473buffers. The fifth argument number of offscreen rows specifies how much of the 6474field to be shown. If it is zero, the entire field is always displayed otherwise 6475the form will be scrollable when the user accesses not displayed parts of the 6476field. The forms library allocates one buffer per field to store the data user 6477enters. Using the last parameter to new_field() we can specify it to allocate 6478some additional buffers. These can be used for any purpose you like.</P 6479><P 6480>After creating the fields, back ground attribute of both of them is set to an 6481underscore with set_field_back(). The AUTOSKIP option is turned off using 6482field_opts_off(). If this option is turned on, focus will move to the next 6483field in the form once the active field is filled up completely.</P 6484><P 6485>After attaching the fields to the form, it is posted. Here on, user inputs are 6486processed in the while loop, by making corresponding requests to form_driver. 6487The details of all the requests to the form_driver() are explained later.</P 6488></DIV 6489><DIV 6490CLASS="SECT2" 6491><HR><H3 6492CLASS="SECT2" 6493><A 6494NAME="PLAYFIELDS" 6495>18.3. Playing with Fields</A 6496></H3 6497><P 6498>Each form field is associated with a lot of attributes. They can be manipulated 6499to get the required effect and to have fun !!!. So why wait? </P 6500><DIV 6501CLASS="SECT3" 6502><HR><H4 6503CLASS="SECT3" 6504><A 6505NAME="FETCHINFO" 6506>18.3.1. Fetching Size and Location of Field</A 6507></H4 6508><P 6509>The parameters we have given at the time of creation of a field can be retrieved 6510with field_info(). It returns height, width, starty, startx, number of offscreen 6511rows, and number of additional buffers into the parameters given to it. It is a 6512sort of inverse of new_field().</P 6513><PRE 6514CLASS="PROGRAMLISTING" 6515>int field_info( FIELD *field, /* field from which to fetch */ |
4872 int *height, *int width, /* field size */ 4873 int *top, int *left, /* upper left corner */ 4874 int *offscreen, /* number of offscreen rows */ | 6516 int *height, *int width, /* field size */ 6517 int *top, int *left, /* upper left corner */ 6518 int *offscreen, /* number of offscreen rows */ |
4875 int *nbuf); /* number of working buffers */</font> 4876</pre></td> 4877</tr> 4878</table> 4879</div> 4880<div class="SECT3"> 4881<hr> 4882<h4 class="SECT3"><a name="MOVEFIELD" id="MOVEFIELD">18.3.2. Moving 4883the field</a></h4> 4884<p>The location of the field can be moved to a different position 4885with move_field().</p> 4886<table border="0" bgcolor="#E0E0E0" width="100%"> 4887<tr> 4888<td> 4889<pre class="PROGRAMLISTING"> 4890<font color= 4891"#000000">int move_field( FIELD *field, /* field to alter */ 4892 int top, int left); /* new upper-left corner */</font> 4893</pre></td> 4894</tr> 4895</table> 4896<p>As usual, the changed position can be queried with 4897field_infor().</p> 4898</div> 4899<div class="SECT3"> 4900<hr> 4901<h4 class="SECT3"><a name="JUSTIFYFIELD" id="JUSTIFYFIELD">18.3.3. 4902Field Justification</a></h4> 4903<p>The justification to be done for the field can be fixed using 4904the function set_field_just().</p> 4905<table border="0" bgcolor="#E0E0E0" width="100%"> 4906<tr> 4907<td> 4908<pre class="PROGRAMLISTING"> 4909<font color= 4910"#000000"> int set_field_just(FIELD *field, /* field to alter */ | 6519 int *nbuf); /* number of working buffers */</PRE 6520></DIV 6521><DIV 6522CLASS="SECT3" 6523><HR><H4 6524CLASS="SECT3" 6525><A 6526NAME="MOVEFIELD" 6527>18.3.2. Moving the field</A 6528></H4 6529><P 6530>The location of the field can be moved to a different position with 6531move_field().</P 6532><PRE 6533CLASS="PROGRAMLISTING" 6534>int move_field( FIELD *field, /* field to alter */ 6535 int top, int left); /* new upper-left corner */</PRE 6536><P 6537>As usual, the changed position can be queried with field_infor().</P 6538></DIV 6539><DIV 6540CLASS="SECT3" 6541><HR><H4 6542CLASS="SECT3" 6543><A 6544NAME="JUSTIFYFIELD" 6545>18.3.3. Field Justification</A 6546></H4 6547><P 6548>The justification to be done for the field can be fixed using the function 6549set_field_just().</P 6550><PRE 6551CLASS="PROGRAMLISTING" 6552> int set_field_just(FIELD *field, /* field to alter */ |
4911 int justmode); /* mode to set */ | 6553 int justmode); /* mode to set */ |
4912 int field_just(FIELD *field); /* fetch justify mode of field */</font> 4913</pre></td> 4914</tr> 4915</table> 4916<p>The justification mode valued accepted and returned by these 4917functions are NO_JUSTIFICATION, JUSTIFY_RIGHT, JUSTIFY_LEFT, or 4918JUSTIFY_CENTER.</p> 4919</div> 4920<div class="SECT3"> 4921<hr> 4922<h4 class="SECT3"><a name="FIELDDISPATTRIB" id= 4923"FIELDDISPATTRIB">18.3.4. Field Display Attributes</a></h4> 4924<p>As you have seen, in the above example, display attribute for 4925the fields can be set with set_field_fore() and setfield_back(). 4926These functions set foreground and background attribute of the 4927fields. You can also specify a pad character which will be filled 4928in the unfilled portion of the field. The pad character is set with 4929a call to set_field_pad(). Default pad value is a space. The 4930functions field_fore(), field_back, field_pad() can be used to 4931query the present foreground, background attributes and pad 4932character for the field. The following list gives the usage of 4933functions.</p> 4934<table border="0" bgcolor="#E0E0E0" width="100%"> 4935<tr> 4936<td> 4937<pre class="PROGRAMLISTING"> 4938<font color= 4939"#000000"> int set_field_fore(FIELD *field, /* field to alter */ | 6554 int field_just(FIELD *field); /* fetch justify mode of field */</PRE 6555><P 6556>The justification mode valued accepted and returned by these functions are 6557NO_JUSTIFICATION, JUSTIFY_RIGHT, JUSTIFY_LEFT, or JUSTIFY_CENTER.</P 6558></DIV 6559><DIV 6560CLASS="SECT3" 6561><HR><H4 6562CLASS="SECT3" 6563><A 6564NAME="FIELDDISPATTRIB" 6565>18.3.4. Field Display Attributes</A 6566></H4 6567><P 6568>As you have seen, in the above example, display attribute for the fields can be 6569set with set_field_fore() and setfield_back(). These functions set foreground 6570and background attribute of the fields. You can also specify a pad character 6571which will be filled in the unfilled portion of the field. The pad character is 6572set with a call to set_field_pad(). Default pad value is a space. The functions 6573field_fore(), field_back, field_pad() can be used to query the present 6574foreground, background attributes and pad character for the field. The following 6575list gives the usage of functions.</P 6576><PRE 6577CLASS="PROGRAMLISTING" 6578> int set_field_fore(FIELD *field, /* field to alter */ |
4940 chtype attr); /* attribute to set */ 4941 4942chtype field_fore(FIELD *field); /* field to query */ 4943 /* returns foreground attribute */ 4944 4945int set_field_back(FIELD *field, /* field to alter */ 4946 chtype attr); /* attribute to set */ 4947 4948chtype field_back(FIELD *field); /* field to query */ 4949 /* returns background attribute */ 4950 4951int set_field_pad(FIELD *field, /* field to alter */ 4952 int pad); /* pad character to set */ 4953 4954chtype field_pad(FIELD *field); /* field to query */ | 6579 chtype attr); /* attribute to set */ 6580 6581chtype field_fore(FIELD *field); /* field to query */ 6582 /* returns foreground attribute */ 6583 6584int set_field_back(FIELD *field, /* field to alter */ 6585 chtype attr); /* attribute to set */ 6586 6587chtype field_back(FIELD *field); /* field to query */ 6588 /* returns background attribute */ 6589 6590int set_field_pad(FIELD *field, /* field to alter */ 6591 int pad); /* pad character to set */ 6592 6593chtype field_pad(FIELD *field); /* field to query */ |
4955 /* returns present pad character */ </font> 4956</pre></td> 4957</tr> 4958</table> 4959<p>Though above functions seem quite simple, using colors with 4960set_field_fore() may be frustrating in the beginning. Let me first 4961explain about foreground and background attributes of a field. The 4962foreground attribute is associated with the character. That means a 4963character in the field is printed with the attribute you have set 4964with set_field_fore(). Background attribute is the attribute used 4965to fill background of field, whether any character is there or not. 4966So what about colors? Since colors are always defined in pairs, 4967what is the right way to display colored fields? Here's an example 4968clarifying color attributes.</p> 4969<div class="EXAMPLE"><a name="FFOAT" id="FFOAT"></a> 4970<p><b>Example 26. Form Attributes example</b></p> 4971<table border="0" bgcolor="#E0E0E0" width="100%"> 4972<tr> 4973<td> 4974<pre class="PROGRAMLISTING"> 4975<font color="#000000"><span class= 4976"INLINEMEDIAOBJECT">#include <form.h> | 6594 /* returns present pad character */ </PRE 6595><P 6596>Though above functions seem quite simple, using colors with set_field_fore() may 6597be frustrating in the beginning. Let me first explain about foreground and 6598background attributes of a field. The foreground attribute is associated with 6599the character. That means a character in the field is printed with the attribute 6600you have set with set_field_fore(). Background attribute is the attribute used 6601to fill background of field, whether any character is there or not. So what 6602about colors? Since colors are always defined in pairs, what is the right way to 6603display colored fields? Here's an example clarifying color attributes.</P 6604><DIV 6605CLASS="EXAMPLE" 6606><A 6607NAME="FFOAT" 6608></A 6609><P 6610><B 6611>Example 26. Form Attributes example </B 6612></P 6613><PRE 6614CLASS="PROGRAMLISTING" 6615><SPAN 6616CLASS="INLINEMEDIAOBJECT" 6617>#include <form.h> |
4977 4978int main() | 6618 6619int main() |
4979{ FIELD *field[3]; 4980 FORM *my_form; 4981 int ch; 4982 4983 /* Initialize curses */ 4984 initscr(); 4985 start_color(); 4986 cbreak(); 4987 noecho(); 4988 keypad(stdscr, TRUE); | 6620{ FIELD *field[3]; 6621 FORM *my_form; 6622 int ch; 6623 6624 /* Initialize curses */ 6625 initscr(); 6626 start_color(); 6627 cbreak(); 6628 noecho(); 6629 keypad(stdscr, TRUE); |
4989 | 6630 |
4990 /* Initialize few color pairs */ 4991 init_pair(1, COLOR_WHITE, COLOR_BLUE); 4992 init_pair(2, COLOR_WHITE, COLOR_BLUE); | 6631 /* Initialize few color pairs */ 6632 init_pair(1, COLOR_WHITE, COLOR_BLUE); 6633 init_pair(2, COLOR_WHITE, COLOR_BLUE); |
4993 | 6634 |
4994 /* Initialize the fields */ 4995 field[0] = new_field(1, 10, 4, 18, 0, 0); 4996 field[1] = new_field(1, 10, 6, 18, 0, 0); 4997 field[2] = NULL; | 6635 /* Initialize the fields */ 6636 field[0] = new_field(1, 10, 4, 18, 0, 0); 6637 field[1] = new_field(1, 10, 6, 18, 0, 0); 6638 field[2] = NULL; |
4998 | 6639 |
4999 /* Set field options */ 5000 set_field_fore(field[0], COLOR_PAIR(1));/* Put the field with blue background */ 5001 set_field_back(field[0], COLOR_PAIR(2));/* and white foreground (characters */ 5002 /* are printed in white */ 5003 field_opts_off(field[0], O_AUTOSKIP); /* Don't go to next field when this */ 5004 /* Field is filled up */ 5005 set_field_back(field[1], A_UNDERLINE); 5006 field_opts_off(field[1], O_AUTOSKIP); | 6640 /* Set field options */ 6641 set_field_fore(field[0], COLOR_PAIR(1));/* Put the field with blue background */ 6642 set_field_back(field[0], COLOR_PAIR(2));/* and white foreground (characters */ 6643 /* are printed in white */ 6644 field_opts_off(field[0], O_AUTOSKIP); /* Don't go to next field when this */ 6645 /* Field is filled up */ 6646 set_field_back(field[1], A_UNDERLINE); 6647 field_opts_off(field[1], O_AUTOSKIP); |
5007 | 6648 |
5008 /* Create the form and post it */ 5009 my_form = new_form(field); 5010 post_form(my_form); 5011 refresh(); 5012 5013 set_current_field(my_form, field[0]); /* Set focus to the colored field */ 5014 mvprintw(4, 10, "Value 1:"); 5015 mvprintw(6, 10, "Value 2:"); 5016 mvprintw(LINES - 2, 0, "Use UP, DOWN arrow keys to switch between fields"); 5017 refresh(); | 6649 /* Create the form and post it */ 6650 my_form = new_form(field); 6651 post_form(my_form); 6652 refresh(); 6653 6654 set_current_field(my_form, field[0]); /* Set focus to the colored field */ 6655 mvprintw(4, 10, "Value 1:"); 6656 mvprintw(6, 10, "Value 2:"); 6657 mvprintw(LINES - 2, 0, "Use UP, DOWN arrow keys to switch between fields"); 6658 refresh(); |
5018 | 6659 |
5019 /* Loop through to get user requests */ 5020 while((ch = getch()) != KEY_F(1)) 5021 { switch(ch) 5022 { case KEY_DOWN: 5023 /* Go to next field */ 5024 form_driver(my_form, REQ_NEXT_FIELD); 5025 /* Go to the end of the present buffer */ 5026 /* Leaves nicely at the last character */ 5027 form_driver(my_form, REQ_END_LINE); 5028 break; 5029 case KEY_UP: 5030 /* Go to previous field */ 5031 form_driver(my_form, REQ_PREV_FIELD); 5032 form_driver(my_form, REQ_END_LINE); 5033 break; 5034 default: 5035 /* If this is a normal character, it gets */ 5036 /* Printed */ 5037 form_driver(my_form, ch); 5038 break; 5039 } 5040 } | 6660 /* Loop through to get user requests */ 6661 while((ch = getch()) != KEY_F(1)) 6662 { switch(ch) 6663 { case KEY_DOWN: 6664 /* Go to next field */ 6665 form_driver(my_form, REQ_NEXT_FIELD); 6666 /* Go to the end of the present buffer */ 6667 /* Leaves nicely at the last character */ 6668 form_driver(my_form, REQ_END_LINE); 6669 break; 6670 case KEY_UP: 6671 /* Go to previous field */ 6672 form_driver(my_form, REQ_PREV_FIELD); 6673 form_driver(my_form, REQ_END_LINE); 6674 break; 6675 default: 6676 /* If this is a normal character, it gets */ 6677 /* Printed */ 6678 form_driver(my_form, ch); 6679 break; 6680 } 6681 } |
5041 | 6682 |
5042 /* Un post form and free the memory */ 5043 unpost_form(my_form); 5044 free_form(my_form); 5045 free_field(field[0]); 5046 free_field(field[1]); | 6683 /* Un post form and free the memory */ 6684 unpost_form(my_form); 6685 free_form(my_form); 6686 free_field(field[0]); 6687 free_field(field[1]); |
5047 | 6688 |
5048 endwin(); 5049 return 0; 5050}</span></font> 5051</pre></td> 5052</tr> 5053</table> 5054</div> 5055<p>Play with the color pairs and try to understand the foreground 5056and background attributes. In my programs using color attributes, I 5057usually set only the background with set_field_back(). Curses 5058simply doesn't allow defining individual color attributes.</p> 5059</div> 5060<div class="SECT3"> 5061<hr> 5062<h4 class="SECT3"><a name="FIELDOPTIONBITS" id= 5063"FIELDOPTIONBITS">18.3.5. Field Option Bits</a></h4> 5064<p>There is also a large collection of field option bits you can 5065set to control various aspects of forms processing. You can 5066manipulate them with these functions:</p> 5067<table border="0" bgcolor="#E0E0E0" width="100%"> 5068<tr> 5069<td> 5070<pre class="PROGRAMLISTING"> 5071<font color= 5072"#000000">int set_field_opts(FIELD *field, /* field to alter */ | 6689 endwin(); 6690 return 0; 6691}</SPAN 6692></PRE 6693></DIV 6694><P 6695>Play with the color pairs and try to understand the foreground and background 6696attributes. In my programs using color attributes, I usually set only the 6697background with set_field_back(). Curses simply doesn't allow defining 6698individual color attributes. </P 6699></DIV 6700><DIV 6701CLASS="SECT3" 6702><HR><H4 6703CLASS="SECT3" 6704><A 6705NAME="FIELDOPTIONBITS" 6706>18.3.5. Field Option Bits</A 6707></H4 6708><P 6709>There is also a large collection of field option bits you can set to control 6710various aspects of forms processing. You can manipulate them with these 6711functions:</P 6712><PRE 6713CLASS="PROGRAMLISTING" 6714>int set_field_opts(FIELD *field, /* field to alter */ |
5073 int attr); /* attribute to set */ 5074 5075int field_opts_on(FIELD *field, /* field to alter */ 5076 int attr); /* attributes to turn on */ 5077 5078int field_opts_off(FIELD *field, /* field to alter */ 5079 int attr); /* attributes to turn off */ 5080 | 6715 int attr); /* attribute to set */ 6716 6717int field_opts_on(FIELD *field, /* field to alter */ 6718 int attr); /* attributes to turn on */ 6719 6720int field_opts_off(FIELD *field, /* field to alter */ 6721 int attr); /* attributes to turn off */ 6722 |
5081int field_opts(FIELD *field); /* field to query */ </font> 5082</pre></td> 5083</tr> 5084</table> 5085<p>The function set_field_opts() can be used to directly set 5086attributes of a field or you can choose to switch a few attributes 5087on and off with field_opts_on() and field_opts_off() selectively. 5088Anytime you can query the attributes of a field with field_opts(). 5089The following is the list of available options. By default, all 5090options are on.</p> 5091<div class="VARIABLELIST"> 5092<dl> 5093<dt>O_VISIBLE</dt> 5094<dd> 5095<p>Controls whether the field is visible on the screen. Can be used 5096during form processing to hide or pop up fields depending on the 5097value of parent fields.</p> 5098</dd> 5099<dt>O_ACTIVE</dt> 5100<dd> 5101<p>Controls whether the field is active during forms processing 5102(i.e. visited by form navigation keys). Can be used to make labels 5103or derived fields with buffer values alterable by the forms 5104application, not the user.</p> 5105</dd> 5106<dt>O_PUBLIC</dt> 5107<dd> 5108<p>Controls whether data is displayed during field entry. If this 5109option is turned off on a field, the library will accept and edit 5110data in that field, but it will not be displayed and the visible 5111field cursor will not move. You can turn off the O_PUBLIC bit to 5112define password fields.</p> 5113</dd> 5114<dt>O_EDIT</dt> 5115<dd> 5116<p>Controls whether the field's data can be modified. When this 5117option is off, all editing requests except <var class= 5118"LITERAL">REQ_PREV_CHOICE</var> and <var class= 5119"LITERAL">REQ_NEXT_CHOICE</var>will fail. Such read-only fields may 5120be useful for help messages.</p> 5121</dd> 5122<dt>O_WRAP</dt> 5123<dd> 5124<p>Controls word-wrapping in multi-line fields. Normally, when any 5125character of a (blank-separated) word reaches the end of the 5126current line, the entire word is wrapped to the next line (assuming 5127there is one). When this option is off, the word will be split 5128across the line break.</p> 5129</dd> 5130<dt>O_BLANK</dt> 5131<dd> 5132<p>Controls field blanking. When this option is on, entering a 5133character at the first field position erases the entire field 5134(except for the just-entered character).</p> 5135</dd> 5136<dt>O_AUTOSKIP</dt> 5137<dd> 5138<p>Controls automatic skip to next field when this one fills. 5139Normally, when the forms user tries to type more data into a field 5140than will fit, the editing location jumps to next field. When this 5141option is off, the user's cursor will hang at the end of the field. 5142This option is ignored in dynamic fields that have not reached 5143their size limit.</p> 5144</dd> 5145<dt>O_NULLOK</dt> 5146<dd> 5147<p>Controls whether validation is applied to blank fields. 5148Normally, it is not; the user can leave a field blank without 5149invoking the usual validation check on exit. If this option is off 5150on a field, exit from it will invoke a validation check.</p> 5151</dd> 5152<dt>O_PASSOK</dt> 5153<dd> 5154<p>Controls whether validation occurs on every exit, or only after 5155the field is modified. Normally the latter is true. Setting 5156O_PASSOK may be useful if your field's validation function may 5157change during forms processing.</p> 5158</dd> 5159<dt>O_STATIC</dt> 5160<dd> 5161<p>Controls whether the field is fixed to its initial dimensions. 5162If you turn this off, the field becomes dynamic and will stretch to 5163fit entered data.</p> 5164</dd> 5165</dl> 5166</div> 5167<p>A field's options cannot be changed while the field is currently 5168selected. However, options may be changed on posted fields that are 5169not current.</p> 5170<p>The option values are bit-masks and can be composed with 5171logical-or in the obvious way. You have seen the usage of switching 5172off O_AUTOSKIP option. The following example clarifies usage of 5173some more options. Other options are explained where 5174appropriate.</p> 5175<div class="EXAMPLE"><a name="FFOOP" id="FFOOP"></a> 5176<p><b>Example 27. Field Options Usage example</b></p> 5177<table border="0" bgcolor="#E0E0E0" width="100%"> 5178<tr> 5179<td> 5180<pre class="PROGRAMLISTING"> 5181<font color="#000000"><span class= 5182"INLINEMEDIAOBJECT">#include <form.h> | 6723int field_opts(FIELD *field); /* field to query */ </PRE 6724><P 6725>The function set_field_opts() can be used to directly set attributes of a field 6726or you can choose to switch a few attributes on and off with field_opts_on() and 6727field_opts_off() selectively. Anytime you can query the attributes of a field 6728with field_opts(). The following is the list of available options. By default, 6729all options are on.</P 6730><P 6731></P 6732><DIV 6733CLASS="VARIABLELIST" 6734><DL 6735><DT 6736>O_VISIBLE</DT 6737><DD 6738><P 6739>Controls whether the field is visible on the screen. Can be used 6740during form processing to hide or pop up fields depending on the value 6741of parent fields.</P 6742></DD 6743><DT 6744>O_ACTIVE</DT 6745><DD 6746><P 6747>Controls whether the field is active during forms processing (i.e. 6748visited by form navigation keys). Can be used to make labels or derived 6749fields with buffer values alterable by the forms application, not the user.</P 6750></DD 6751><DT 6752>O_PUBLIC</DT 6753><DD 6754><P 6755>Controls whether data is displayed during field entry. If this option is 6756turned off on a field, the library will accept and edit data in that field, 6757but it will not be displayed and the visible field cursor will not move. 6758You can turn off the O_PUBLIC bit to define password fields.</P 6759></DD 6760><DT 6761>O_EDIT</DT 6762><DD 6763><P 6764>Controls whether the field's data can be modified. When this option is 6765off, all editing requests except <TT 6766CLASS="LITERAL" 6767>REQ_PREV_CHOICE</TT 6768> and <TT 6769CLASS="LITERAL" 6770>REQ_NEXT_CHOICE</TT 6771>will 6772fail. Such read-only fields may be useful for help messages.</P 6773></DD 6774><DT 6775>O_WRAP</DT 6776><DD 6777><P 6778>Controls word-wrapping in multi-line fields. Normally, when any 6779character of a (blank-separated) word reaches the end of the current line, the 6780entire word is wrapped to the next line (assuming there is one). When this 6781option is off, the word will be split across the line break.</P 6782></DD 6783><DT 6784>O_BLANK</DT 6785><DD 6786><P 6787>Controls field blanking. When this option is on, entering a character at 6788the first field position erases the entire field (except for the just-entered 6789character).</P 6790></DD 6791><DT 6792>O_AUTOSKIP</DT 6793><DD 6794><P 6795>Controls automatic skip to next field when this one fills. Normally, 6796when the forms user tries to type more data into a field than will fit, 6797the editing location jumps to next field. When this option is off, the 6798user's cursor will hang at the end of the field. This option is ignored 6799in dynamic fields that have not reached their size limit.</P 6800></DD 6801><DT 6802>O_NULLOK</DT 6803><DD 6804><P 6805>Controls whether validation is applied to 6806blank fields. Normally, it is not; the user can leave a field blank 6807without invoking the usual validation check on exit. If this option is 6808off on a field, exit from it will invoke a validation check.</P 6809></DD 6810><DT 6811>O_PASSOK</DT 6812><DD 6813><P 6814>Controls whether validation occurs on every exit, or only after 6815the field is modified. Normally the latter is true. Setting O_PASSOK 6816may be useful if your field's validation function may change during 6817forms processing.</P 6818></DD 6819><DT 6820>O_STATIC</DT 6821><DD 6822><P 6823>Controls whether the field is fixed to its initial dimensions. If you 6824turn this off, the field becomes dynamic and will 6825stretch to fit entered data.</P 6826></DD 6827></DL 6828></DIV 6829><P 6830>A field's options cannot be changed while the field is currently selected. 6831However, options may be changed on posted fields that are not current. </P 6832><P 6833>The option values are bit-masks and can be composed with logical-or in 6834the obvious way. You have seen the usage of switching off O_AUTOSKIP option. 6835The following example clarifies usage of some more options. Other options 6836are explained where appropriate.</P 6837><DIV 6838CLASS="EXAMPLE" 6839><A 6840NAME="FFOOP" 6841></A 6842><P 6843><B 6844>Example 27. Field Options Usage example </B 6845></P 6846><PRE 6847CLASS="PROGRAMLISTING" 6848><SPAN 6849CLASS="INLINEMEDIAOBJECT" 6850>#include <form.h> |
5183 5184#define STARTX 15 5185#define STARTY 4 5186#define WIDTH 25 5187 5188#define N_FIELDS 3 5189 5190int main() | 6851 6852#define STARTX 15 6853#define STARTY 4 6854#define WIDTH 25 6855 6856#define N_FIELDS 3 6857 6858int main() |
5191{ FIELD *field[N_FIELDS]; 5192 FORM *my_form; 5193 int ch, i; 5194 5195 /* Initialize curses */ 5196 initscr(); 5197 cbreak(); 5198 noecho(); 5199 keypad(stdscr, TRUE); | 6859{ FIELD *field[N_FIELDS]; 6860 FORM *my_form; 6861 int ch, i; 6862 6863 /* Initialize curses */ 6864 initscr(); 6865 cbreak(); 6866 noecho(); 6867 keypad(stdscr, TRUE); |
5200 | 6868 |
5201 /* Initialize the fields */ 5202 for(i = 0; i < N_FIELDS - 1; ++i) 5203 field[i] = new_field(1, WIDTH, STARTY + i * 2, STARTX, 0, 0); 5204 field[N_FIELDS - 1] = NULL; | 6869 /* Initialize the fields */ 6870 for(i = 0; i < N_FIELDS - 1; ++i) 6871 field[i] = new_field(1, WIDTH, STARTY + i * 2, STARTX, 0, 0); 6872 field[N_FIELDS - 1] = NULL; |
5205 | 6873 |
5206 /* Set field options */ 5207 set_field_back(field[1], A_UNDERLINE); /* Print a line for the option */ 5208 5209 field_opts_off(field[0], O_ACTIVE); /* This field is a static label */ 5210 field_opts_off(field[1], O_PUBLIC); /* This filed is like a password field*/ 5211 field_opts_off(field[1], O_AUTOSKIP); /* To avoid entering the same field */ 5212 /* after last character is entered */ 5213 5214 /* Create the form and post it */ 5215 my_form = new_form(field); 5216 post_form(my_form); 5217 refresh(); 5218 5219 set_field_just(field[0], JUSTIFY_CENTER); /* Center Justification */ 5220 set_field_buffer(field[0], 0, "This is a static Field"); 5221 /* Initialize the field */ 5222 mvprintw(STARTY, STARTX - 10, "Field 1:"); 5223 mvprintw(STARTY + 2, STARTX - 10, "Field 2:"); 5224 refresh(); | 6874 /* Set field options */ 6875 set_field_back(field[1], A_UNDERLINE); /* Print a line for the option */ 6876 6877 field_opts_off(field[0], O_ACTIVE); /* This field is a static label */ 6878 field_opts_off(field[1], O_PUBLIC); /* This filed is like a password field*/ 6879 field_opts_off(field[1], O_AUTOSKIP); /* To avoid entering the same field */ 6880 /* after last character is entered */ 6881 6882 /* Create the form and post it */ 6883 my_form = new_form(field); 6884 post_form(my_form); 6885 refresh(); 6886 6887 set_field_just(field[0], JUSTIFY_CENTER); /* Center Justification */ 6888 set_field_buffer(field[0], 0, "This is a static Field"); 6889 /* Initialize the field */ 6890 mvprintw(STARTY, STARTX - 10, "Field 1:"); 6891 mvprintw(STARTY + 2, STARTX - 10, "Field 2:"); 6892 refresh(); |
5225 | 6893 |
5226 /* Loop through to get user requests */ 5227 while((ch = getch()) != KEY_F(1)) 5228 { switch(ch) 5229 { case KEY_DOWN: 5230 /* Go to next field */ 5231 form_driver(my_form, REQ_NEXT_FIELD); 5232 /* Go to the end of the present buffer */ 5233 /* Leaves nicely at the last character */ 5234 form_driver(my_form, REQ_END_LINE); 5235 break; 5236 case KEY_UP: 5237 /* Go to previous field */ 5238 form_driver(my_form, REQ_PREV_FIELD); 5239 form_driver(my_form, REQ_END_LINE); 5240 break; 5241 default: 5242 /* If this is a normal character, it gets */ 5243 /* Printed */ 5244 form_driver(my_form, ch); 5245 break; 5246 } 5247 } | 6894 /* Loop through to get user requests */ 6895 while((ch = getch()) != KEY_F(1)) 6896 { switch(ch) 6897 { case KEY_DOWN: 6898 /* Go to next field */ 6899 form_driver(my_form, REQ_NEXT_FIELD); 6900 /* Go to the end of the present buffer */ 6901 /* Leaves nicely at the last character */ 6902 form_driver(my_form, REQ_END_LINE); 6903 break; 6904 case KEY_UP: 6905 /* Go to previous field */ 6906 form_driver(my_form, REQ_PREV_FIELD); 6907 form_driver(my_form, REQ_END_LINE); 6908 break; 6909 default: 6910 /* If this is a normal character, it gets */ 6911 /* Printed */ 6912 form_driver(my_form, ch); 6913 break; 6914 } 6915 } |
5248 | 6916 |
5249 /* Un post form and free the memory */ 5250 unpost_form(my_form); 5251 free_form(my_form); 5252 free_field(field[0]); 5253 free_field(field[1]); | 6917 /* Un post form and free the memory */ 6918 unpost_form(my_form); 6919 free_form(my_form); 6920 free_field(field[0]); 6921 free_field(field[1]); |
5254 | 6922 |
5255 endwin(); 5256 return 0; 5257}</span></font> 5258</pre></td> 5259</tr> 5260</table> 5261</div> 5262<p>This example, though useless, shows the usage of options. If 5263used properly, they can present information very effectively in a 5264form. The second field being not O_PUBLIC, does not show the 5265characters you are typing.</p> 5266</div> 5267<div class="SECT3"> 5268<hr> 5269<h4 class="SECT3"><a name="FIELDSTATUS" id="FIELDSTATUS">18.3.6. 5270Field Status</a></h4> 5271<p>The field status specifies whether the field has got edited or 5272not. It is initially set to FALSE and when user enters something 5273and the data buffer gets modified it becomes TRUE. So a field's 5274status can be queried to find out whether it has been modified or 5275not. The following functions can assist in those operations.</p> 5276<table border="0" bgcolor="#E0E0E0" width="100%"> 5277<tr> 5278<td> 5279<pre class="PROGRAMLISTING"> 5280<font color= 5281"#000000">int set_field_status(FIELD *field, /* field to alter */ | 6923 endwin(); 6924 return 0; 6925}</SPAN 6926></PRE 6927></DIV 6928><P 6929>This example, though useless, shows the usage of options. If used properly, they 6930can present information very effectively in a form. The second field being not 6931O_PUBLIC, does not show the characters you are typing.</P 6932></DIV 6933><DIV 6934CLASS="SECT3" 6935><HR><H4 6936CLASS="SECT3" 6937><A 6938NAME="FIELDSTATUS" 6939>18.3.6. Field Status</A 6940></H4 6941><P 6942>The field status specifies whether the field has got edited or not. It is 6943initially set to FALSE and when user enters something and the data buffer gets 6944modified it becomes TRUE. So a field's status can be queried to find out whether 6945it has been modified or not. The following functions can assist in those 6946operations.</P 6947><PRE 6948CLASS="PROGRAMLISTING" 6949>int set_field_status(FIELD *field, /* field to alter */ |
5282 int status); /* status to set */ 5283 | 6950 int status); /* status to set */ 6951 |
5284int field_status(FIELD *field); /* fetch status of field */</font> 5285</pre></td> 5286</tr> 5287</table> 5288<p>It's better to check the field's status only after after leaving 5289the field, as data buffer might not have been updated yet as the 5290validation is still due. To guarantee that right status is 5291returned, call field_status() either (1) in the field's exit 5292validation check routine, (2) from the field's or form's 5293initialization or termination hooks, or (3) just after a 5294REQ_VALIDATION request has been processed by the forms driver</p> 5295</div> 5296<div class="SECT3"> 5297<hr> 5298<h4 class="SECT3"><a name="FIELDUSERPTR" id="FIELDUSERPTR">18.3.7. 5299Field User Pointer</a></h4> 5300<p>Every field structure contains one pointer that can be used by 5301the user for various purposes. It is not touched by forms library 5302and can be used for any purpose by the user. The following 5303functions set and fetch user pointer.</p> 5304<table border="0" bgcolor="#E0E0E0" width="100%"> 5305<tr> 5306<td> 5307<pre class="PROGRAMLISTING"> 5308<font color="#000000">int set_field_userptr(FIELD *field, | 6952int field_status(FIELD *field); /* fetch status of field */</PRE 6953><P 6954>It's better to check the field's status only after after leaving the field, as 6955data buffer might not have been updated yet as the validation is still due. To 6956guarantee that right status is returned, call field_status() either (1) in the 6957field's exit validation check routine, (2) from the field's or form's 6958initialization or termination hooks, or (3) just after a REQ_VALIDATION request 6959has been processed by the forms driver</P 6960></DIV 6961><DIV 6962CLASS="SECT3" 6963><HR><H4 6964CLASS="SECT3" 6965><A 6966NAME="FIELDUSERPTR" 6967>18.3.7. Field User Pointer</A 6968></H4 6969><P 6970>Every field structure contains one pointer that can be used by the user for 6971various purposes. It is not touched by forms library and can be used for any 6972purpose by the user. The following functions set and fetch user pointer.</P 6973><PRE 6974CLASS="PROGRAMLISTING" 6975>int set_field_userptr(FIELD *field, |
5309 char *userptr); /* the user pointer you wish to associate */ 5310 /* with the field */ 5311 | 6976 char *userptr); /* the user pointer you wish to associate */ 6977 /* with the field */ 6978 |
5312char *field_userptr(FIELD *field); /* fetch user pointer of the field */</font> 5313</pre></td> 5314</tr> 5315</table> 5316</div> 5317<div class="SECT3"> 5318<hr> 5319<h4 class="SECT3"><a name="VARIABLESIZEFIELDS" id= 5320"VARIABLESIZEFIELDS">18.3.8. Variable-Sized Fields</a></h4> 5321<p>If you want a dynamically changing field with variable width, 5322this is the feature you want to put to full use. This will allow 5323the user to enter more data than the original size of the field and 5324let the field grow. According to the field orientation it will 5325scroll horizontally or vertically to incorporate the new data.</p> 5326<p>To make a field dynamically growable, the option O_STATIC should 5327be turned off. This can be done with a</p> 5328<table border="0" bgcolor="#E0E0E0" width="100%"> 5329<tr> 5330<td> 5331<pre class="PROGRAMLISTING"> 5332<font color= 5333"#000000"> field_opts_off(field_pointer, O_STATIC);</font> 5334</pre></td> 5335</tr> 5336</table> 5337<p>But it's usually not advisable to allow a field to grow 5338infinitely. You can set a maximum limit to the growth of the field 5339with</p> 5340<table border="0" bgcolor="#E0E0E0" width="100%"> 5341<tr> 5342<td> 5343<pre class="PROGRAMLISTING"> 5344<font color= 5345"#000000">int set_max_field(FIELD *field, /* Field on which to operate */ 5346 int max_growth); /* maximum growth allowed for the field */</font> 5347</pre></td> 5348</tr> 5349</table> 5350<p>The field info for a dynamically growable field can be retrieved 5351by</p> 5352<table border="0" bgcolor="#E0E0E0" width="100%"> 5353<tr> 5354<td> 5355<pre class="PROGRAMLISTING"> 5356<font color= 5357"#000000">int dynamic_field_info( FIELD *field, /* Field on which to operate */ | 6979char *field_userptr(FIELD *field); /* fetch user pointer of the field */</PRE 6980></DIV 6981><DIV 6982CLASS="SECT3" 6983><HR><H4 6984CLASS="SECT3" 6985><A 6986NAME="VARIABLESIZEFIELDS" 6987>18.3.8. Variable-Sized Fields</A 6988></H4 6989><P 6990>If you want a dynamically changing field with variable width, this is the 6991feature you want to put to full use. This will allow the user to enter more data 6992than the original size of the field and let the field grow. According to the 6993field orientation it will scroll horizontally or vertically to incorporate the 6994new data.</P 6995><P 6996>To make a field dynamically growable, the option O_STATIC should be turned off. 6997This can be done with a 6998<PRE 6999CLASS="PROGRAMLISTING" 7000> field_opts_off(field_pointer, O_STATIC);</PRE 7001></P 7002><P 7003>But it's usually not advisable to allow a field to grow infinitely. You can set 7004a maximum limit to the growth of the field with 7005<PRE 7006CLASS="PROGRAMLISTING" 7007>int set_max_field(FIELD *field, /* Field on which to operate */ 7008 int max_growth); /* maximum growth allowed for the field */</PRE 7009></P 7010><P 7011>The field info for a dynamically growable field can be retrieved by 7012<PRE 7013CLASS="PROGRAMLISTING" 7014>int dynamic_field_info( FIELD *field, /* Field on which to operate */ |
5358 int *prows, /* number of rows will be filled in this */ 5359 int *pcols, /* number of columns will be filled in this*/ 5360 int *pmax) /* maximum allowable growth will be filled */ | 7015 int *prows, /* number of rows will be filled in this */ 7016 int *pcols, /* number of columns will be filled in this*/ 7017 int *pmax) /* maximum allowable growth will be filled */ |
5361 /* in this */</font> 5362</pre></td> 5363</tr> 5364</table> 5365Though field_info work as usual, it is advisable to use this 5366function to get the proper attributes of a dynamically growable 5367field. 5368<p>Recall the library routine new_field; a new field created with 5369height set to one will be defined to be a one line field. A new 5370field created with height greater than one will be defined to be a 5371multi line field.</p> 5372<p>A one line field with O_STATIC turned off (dynamically growable 5373field) will contain a single fixed row, but the number of columns 5374can increase if the user enters more data than the initial field 5375will hold. The number of columns displayed will remain fixed and 5376the additional data will scroll horizontally.</p> 5377<p>A multi line field with O_STATIC turned off (dynamically 5378growable field) will contain a fixed number of columns, but the 5379number of rows can increase if the user enters more data than the 5380initial field will hold. The number of rows displayed will remain 5381fixed and the additional data will scroll vertically.</p> 5382<p>The above two paragraphs pretty much describe a dynamically 5383growable field's behavior. The way other parts of forms library 5384behaves is described below:</p> 5385<ol type="1"> 5386<li> 5387<p>The field option O_AUTOSKIP will be ignored if the option 5388O_STATIC is off and there is no maximum growth specified for the 5389field. Currently, O_AUTOSKIP generates an automatic REQ_NEXT_FIELD 5390form driver request when the user types in the last character 5391position of a field. On a growable field with no maximum growth 5392specified, there is no last character position. If a maximum growth 5393is specified, the O_AUTOSKIP option will work as normal if the 5394field has grown to its maximum size.</p> 5395</li> 5396<li> 5397<p>The field justification will be ignored if the option O_STATIC 5398is off. Currently, set_field_just can be used to JUSTIFY_LEFT, 5399JUSTIFY_RIGHT, JUSTIFY_CENTER the contents of a one line field. A 5400growable one line field will, by definition, grow and scroll 5401horizontally and may contain more data than can be justified. The 5402return from field_just will be unchanged.</p> 5403</li> 5404<li> 5405<p>The overloaded form driver request REQ_NEW_LINE will operate the 5406same way regardless of the O_NL_OVERLOAD form option if the field 5407option O_STATIC is off and there is no maximum growth specified for 5408the field. Currently, if the form option O_NL_OVERLOAD is on, 5409REQ_NEW_LINE implicitly generates a REQ_NEXT_FIELD if called from 5410the last line of a field. If a field can grow without bound, there 5411is no last line, so REQ_NEW_LINE will never implicitly generate a 5412REQ_NEXT_FIELD. If a maximum growth limit is specified and the 5413O_NL_OVERLOAD form option is on, REQ_NEW_LINE will only implicitly 5414generate REQ_NEXT_FIELD if the field has grown to its maximum size 5415and the user is on the last line.</p> 5416</li> 5417<li> 5418<p>The library call dup_field will work as usual; it will duplicate 5419the field, including the current buffer size and contents of the 5420field being duplicated. Any specified maximum growth will also be 5421duplicated.</p> 5422</li> 5423<li> 5424<p>The library call link_field will work as usual; it will 5425duplicate all field attributes and share buffers with the field 5426being linked. If the O_STATIC field option is subsequently changed 5427by a field sharing buffers, how the system reacts to an attempt to 5428enter more data into the field than the buffer will currently hold 5429will depend on the setting of the option in the current field.</p> 5430</li> 5431<li> 5432<p>The library call field_info will work as usual; the variable 5433nrow will contain the value of the original call to new_field. The 5434user should use dynamic_field_info, described above, to query the 5435current size of the buffer.</p> 5436</li> 5437</ol> 5438<p>Some of the above points make sense only after explaining form 5439driver. We will be looking into that in next few sections.</p> 5440</div> 5441</div> 5442<div class="SECT2"> 5443<hr> 5444<h3 class="SECT2"><a name="FORMWINDOWS" id="FORMWINDOWS">18.4. Form 5445Windows</a></h3> 5446<p>The form windows concept is pretty much similar to menu windows. 5447Every form is associated with a main window and a sub window. The 5448form main window displays any title or border associated or 5449whatever the user wishes. Then the sub window contains all the 5450fields and displays them according to their position. This gives 5451the flexibility of manipulating fancy form displaying very 5452easily.</p> 5453<p>Since this is pretty much similar to menu windows, I am 5454providing an example with out much explanation. The functions are 5455similar and they work the same way.</p> 5456<div class="EXAMPLE"><a name="FFOWI" id="FFOWI"></a> 5457<p><b>Example 28. Form Windows Example</b></p> 5458<table border="0" bgcolor="#E0E0E0" width="100%"> 5459<tr> 5460<td> 5461<pre class="PROGRAMLISTING"> 5462<font color="#000000"><span class= 5463"INLINEMEDIAOBJECT">#include <form.h> | 7018 /* in this */</PRE 7019> 7020Though field_info work as usual, it is advisable to use this function to get the 7021proper attributes of a dynamically growable field.</P 7022><P 7023>Recall the library routine new_field; a new field created with height set to one 7024will be defined to be a one line field. A new field created with height greater 7025than one will be defined to be a multi line field. </P 7026><P 7027>A one line field with O_STATIC turned off (dynamically growable field) will 7028contain a single fixed row, but the number of columns can increase if the user 7029enters more data than the initial field will hold. The number of columns 7030displayed will remain fixed and the additional data will scroll horizontally. </P 7031><P 7032>A multi line field with O_STATIC turned off (dynamically growable field) will 7033contain a fixed number of columns, but the number of rows can increase if the 7034user enters more data than the initial field will hold. The number of rows 7035displayed will remain fixed and the additional data will scroll vertically.</P 7036><P 7037>The above two paragraphs pretty much describe a dynamically growable field's 7038behavior. The way other parts of forms library behaves is described below:</P 7039><P 7040></P 7041><OL 7042TYPE="1" 7043><LI 7044><P 7045>The field option O_AUTOSKIP will be ignored if the option O_STATIC is off and 7046there is no maximum growth specified for the field. Currently, O_AUTOSKIP 7047generates an automatic REQ_NEXT_FIELD form driver request when the user types in 7048the last character position of a field. On a growable field with no maximum 7049growth specified, there is no last character position. If a maximum growth is 7050specified, the O_AUTOSKIP option will work as normal if the field has grown to 7051its maximum size. </P 7052></LI 7053><LI 7054><P 7055>The field justification will be ignored if the option O_STATIC is off. 7056Currently, set_field_just can be used to JUSTIFY_LEFT, JUSTIFY_RIGHT, 7057JUSTIFY_CENTER the contents of a one line field. A growable one line field will, 7058by definition, grow and scroll horizontally and may contain more data than can 7059be justified. The return from field_just will be unchanged. </P 7060></LI 7061><LI 7062><P 7063>The overloaded form driver request REQ_NEW_LINE will operate the same way 7064regardless of the O_NL_OVERLOAD form option if the field option O_STATIC is off 7065and there is no maximum growth specified for the field. Currently, if the form 7066option O_NL_OVERLOAD is on, REQ_NEW_LINE implicitly generates a REQ_NEXT_FIELD 7067if called from the last line of a field. If a field can grow without bound, 7068there is no last line, so REQ_NEW_LINE will never implicitly generate a 7069REQ_NEXT_FIELD. If a maximum growth limit is specified and the O_NL_OVERLOAD 7070form option is on, REQ_NEW_LINE will only implicitly generate REQ_NEXT_FIELD if 7071the field has grown to its maximum size and the user is on the last line. </P 7072></LI 7073><LI 7074><P 7075>The library call dup_field will work as usual; it will duplicate the field, 7076including the current buffer size and contents of the field being duplicated. 7077Any specified maximum growth will also be duplicated. </P 7078></LI 7079><LI 7080><P 7081>The library call link_field will work as usual; it will duplicate all field 7082attributes and share buffers with the field being linked. If the O_STATIC field 7083option is subsequently changed by a field sharing buffers, how the system reacts 7084to an attempt to enter more data into the field than the buffer will currently 7085hold will depend on the setting of the option in the current field. </P 7086></LI 7087><LI 7088><P 7089>The library call field_info will work as usual; the variable nrow will contain 7090the value of the original call to new_field. The user should use 7091dynamic_field_info, described above, to query the current size of the buffer.</P 7092></LI 7093></OL 7094><P 7095>Some of the above points make sense only after explaining form driver. We will 7096be looking into that in next few sections.</P 7097></DIV 7098></DIV 7099><DIV 7100CLASS="SECT2" 7101><HR><H3 7102CLASS="SECT2" 7103><A 7104NAME="FORMWINDOWS" 7105>18.4. Form Windows</A 7106></H3 7107><P 7108>The form windows concept is pretty much similar to menu windows. Every form is 7109associated with a main window and a sub window. The form main window displays 7110any title or border associated or whatever the user wishes. Then the sub window 7111contains all the fields and displays them according to their position. This 7112gives the flexibility of manipulating fancy form displaying very easily. </P 7113><P 7114>Since this is pretty much similar to menu windows, I am providing an example 7115with out much explanation. The functions are similar and they work the same way.</P 7116><DIV 7117CLASS="EXAMPLE" 7118><A 7119NAME="FFOWI" 7120></A 7121><P 7122><B 7123>Example 28. Form Windows Example </B 7124></P 7125><PRE 7126CLASS="PROGRAMLISTING" 7127><SPAN 7128CLASS="INLINEMEDIAOBJECT" 7129>#include <form.h> |
5464 5465void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); 5466 5467int main() 5468{ | 7130 7131void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); 7132 7133int main() 7134{ |
5469 FIELD *field[3]; 5470 FORM *my_form; 5471 WINDOW *my_form_win; 5472 int ch, rows, cols; 5473 5474 /* Initialize curses */ 5475 initscr(); 5476 start_color(); 5477 cbreak(); 5478 noecho(); 5479 keypad(stdscr, TRUE); | 7135 FIELD *field[3]; 7136 FORM *my_form; 7137 WINDOW *my_form_win; 7138 int ch, rows, cols; 7139 7140 /* Initialize curses */ 7141 initscr(); 7142 start_color(); 7143 cbreak(); 7144 noecho(); 7145 keypad(stdscr, TRUE); |
5480 | 7146 |
5481 /* Initialize few color pairs */ 5482 init_pair(1, COLOR_RED, COLOR_BLACK); | 7147 /* Initialize few color pairs */ 7148 init_pair(1, COLOR_RED, COLOR_BLACK); |
5483 | 7149 |
5484 /* Initialize the fields */ 5485 field[0] = new_field(1, 10, 6, 1, 0, 0); 5486 field[1] = new_field(1, 10, 8, 1, 0, 0); 5487 field[2] = NULL; | 7150 /* Initialize the fields */ 7151 field[0] = new_field(1, 10, 6, 1, 0, 0); 7152 field[1] = new_field(1, 10, 8, 1, 0, 0); 7153 field[2] = NULL; |
5488 | 7154 |
5489 /* Set field options */ 5490 set_field_back(field[0], A_UNDERLINE); 5491 field_opts_off(field[0], O_AUTOSKIP); /* Don't go to next field when this */ 5492 /* Field is filled up */ 5493 set_field_back(field[1], A_UNDERLINE); 5494 field_opts_off(field[1], O_AUTOSKIP); 5495 5496 /* Create the form and post it */ 5497 my_form = new_form(field); 5498 5499 /* Calculate the area required for the form */ 5500 scale_form(my_form, &rows, &cols); | 7155 /* Set field options */ 7156 set_field_back(field[0], A_UNDERLINE); 7157 field_opts_off(field[0], O_AUTOSKIP); /* Don't go to next field when this */ 7158 /* Field is filled up */ 7159 set_field_back(field[1], A_UNDERLINE); 7160 field_opts_off(field[1], O_AUTOSKIP); 7161 7162 /* Create the form and post it */ 7163 my_form = new_form(field); 7164 7165 /* Calculate the area required for the form */ 7166 scale_form(my_form, &rows, &cols); |
5501 | 7167 |
5502 /* Create the window to be associated with the form */ | 7168 /* Create the window to be associated with the form */ |
5503 my_form_win = newwin(rows + 4, cols + 4, 4, 4); 5504 keypad(my_form_win, TRUE); 5505 | 7169 my_form_win = newwin(rows + 4, cols + 4, 4, 4); 7170 keypad(my_form_win, TRUE); 7171 |
5506 /* Set main window and sub window */ | 7172 /* Set main window and sub window */ |
5507 set_form_win(my_form, my_form_win); 5508 set_form_sub(my_form, derwin(my_form_win, rows, cols, 2, 2)); 5509 | 7173 set_form_win(my_form, my_form_win); 7174 set_form_sub(my_form, derwin(my_form_win, rows, cols, 2, 2)); 7175 |
5510 /* Print a border around the main window and print a title */ | 7176 /* Print a border around the main window and print a title */ |
5511 box(my_form_win, 0, 0); | 7177 box(my_form_win, 0, 0); |
5512 print_in_middle(my_form_win, 1, 0, cols + 4, "My Form", COLOR_PAIR(1)); 5513 5514 post_form(my_form); 5515 wrefresh(my_form_win); | 7178 print_in_middle(my_form_win, 1, 0, cols + 4, "My Form", COLOR_PAIR(1)); 7179 7180 post_form(my_form); 7181 wrefresh(my_form_win); |
5516 | 7182 |
5517 mvprintw(LINES - 2, 0, "Use UP, DOWN arrow keys to switch between fields"); 5518 refresh(); | 7183 mvprintw(LINES - 2, 0, "Use UP, DOWN arrow keys to switch between fields"); 7184 refresh(); |
5519 | 7185 |
5520 /* Loop through to get user requests */ 5521 while((ch = wgetch(my_form_win)) != KEY_F(1)) 5522 { switch(ch) 5523 { case KEY_DOWN: 5524 /* Go to next field */ 5525 form_driver(my_form, REQ_NEXT_FIELD); 5526 /* Go to the end of the present buffer */ 5527 /* Leaves nicely at the last character */ 5528 form_driver(my_form, REQ_END_LINE); 5529 break; 5530 case KEY_UP: 5531 /* Go to previous field */ 5532 form_driver(my_form, REQ_PREV_FIELD); 5533 form_driver(my_form, REQ_END_LINE); 5534 break; 5535 default: 5536 /* If this is a normal character, it gets */ 5537 /* Printed */ 5538 form_driver(my_form, ch); 5539 break; 5540 } 5541 } | 7186 /* Loop through to get user requests */ 7187 while((ch = wgetch(my_form_win)) != KEY_F(1)) 7188 { switch(ch) 7189 { case KEY_DOWN: 7190 /* Go to next field */ 7191 form_driver(my_form, REQ_NEXT_FIELD); 7192 /* Go to the end of the present buffer */ 7193 /* Leaves nicely at the last character */ 7194 form_driver(my_form, REQ_END_LINE); 7195 break; 7196 case KEY_UP: 7197 /* Go to previous field */ 7198 form_driver(my_form, REQ_PREV_FIELD); 7199 form_driver(my_form, REQ_END_LINE); 7200 break; 7201 default: 7202 /* If this is a normal character, it gets */ 7203 /* Printed */ 7204 form_driver(my_form, ch); 7205 break; 7206 } 7207 } |
5542 | 7208 |
5543 /* Un post form and free the memory */ 5544 unpost_form(my_form); 5545 free_form(my_form); 5546 free_field(field[0]); 5547 free_field(field[1]); | 7209 /* Un post form and free the memory */ 7210 unpost_form(my_form); 7211 free_form(my_form); 7212 free_field(field[0]); 7213 free_field(field[1]); |
5548 | 7214 |
5549 endwin(); 5550 return 0; | 7215 endwin(); 7216 return 0; |
5551} 5552 5553void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) | 7217} 7218 7219void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) |
5554{ int length, x, y; 5555 float temp; | 7220{ int length, x, y; 7221 float temp; |
5556 | 7222 |
5557 if(win == NULL) 5558 win = stdscr; 5559 getyx(win, y, x); 5560 if(startx != 0) 5561 x = startx; 5562 if(starty != 0) 5563 y = starty; 5564 if(width == 0) 5565 width = 80; | 7223 if(win == NULL) 7224 win = stdscr; 7225 getyx(win, y, x); 7226 if(startx != 0) 7227 x = startx; 7228 if(starty != 0) 7229 y = starty; 7230 if(width == 0) 7231 width = 80; |
5566 | 7232 |
5567 length = strlen(string); 5568 temp = (width - length)/ 2; 5569 x = startx + (int)temp; 5570 wattron(win, color); 5571 mvwprintw(win, y, x, "%s", string); 5572 wattroff(win, color); 5573 refresh(); 5574}</span></font> 5575</pre></td> 5576</tr> 5577</table> 5578</div> 5579</div> 5580<div class="SECT2"> 5581<hr> 5582<h3 class="SECT2"><a name="FILEDVALIDATE" id="FILEDVALIDATE">18.5. 5583Field Validation</a></h3> 5584<p>By default, a field will accept any data input by the user. It 5585is possible to attach validation to the field. Then any attempt by 5586the user to leave the field, while it contains data that doesn't 5587match the validation type will fail. Some validation types also 5588have a character-validity check for each time a character is 5589entered in the field.</p> 5590<p>Validation can be attached to a field with the following 5591function.</p> 5592<table border="0" bgcolor="#E0E0E0" width="100%"> 5593<tr> 5594<td> 5595<pre class="PROGRAMLISTING"> 5596<font color= 5597"#000000">int set_field_type(FIELD *field, /* field to alter */ | 7233 length = strlen(string); 7234 temp = (width - length)/ 2; 7235 x = startx + (int)temp; 7236 wattron(win, color); 7237 mvwprintw(win, y, x, "%s", string); 7238 wattroff(win, color); 7239 refresh(); 7240}</SPAN 7241></PRE 7242></DIV 7243></DIV 7244><DIV 7245CLASS="SECT2" 7246><HR><H3 7247CLASS="SECT2" 7248><A 7249NAME="FILEDVALIDATE" 7250>18.5. Field Validation</A 7251></H3 7252><P 7253>By default, a field will accept any data input by the user. It is possible to 7254attach validation to the field. Then any attempt by the user to leave the field, 7255while it contains data that doesn't match the validation type will fail. Some 7256validation types also have a character-validity check for each time a character 7257is entered in the field.</P 7258><P 7259>Validation can be attached to a field with the following function. 7260<PRE 7261CLASS="PROGRAMLISTING" 7262>int set_field_type(FIELD *field, /* field to alter */ |
5598 FIELDTYPE *ftype, /* type to associate */ | 7263 FIELDTYPE *ftype, /* type to associate */ |
5599 ...); /* additional arguments*/</font> 5600</pre></td> 5601</tr> 5602</table> | 7264 ...); /* additional arguments*/</PRE 7265> |
5603Once set, the validation type for a field can be queried with | 7266Once set, the validation type for a field can be queried with |
5604<table border="0" bgcolor="#E0E0E0" width="100%"> 5605<tr> 5606<td> 5607<pre class="PROGRAMLISTING"> 5608<font color= 5609"#000000">FIELDTYPE *field_type(FIELD *field); /* field to query */</font> 5610</pre></td> 5611</tr> 5612</table> 5613<p>The form driver validates the data in a field only when data is 5614entered by the end-user. Validation does not occur when</p> 5615<ul> 5616<li> 5617<p>the application program changes the field value by calling 5618set_field_buffer.</p> 5619</li> 5620<li> 5621<p>linked field values are changed indirectly -- by changing the 5622field to which they are linked</p> 5623</li> 5624</ul> 5625<p>The following are the pre-defined validation types. You can also 5626specify custom validation, though it's a bit tricky and 5627cumbersome.</p> 5628<h1 class="BRIDGEHEAD"><a name="AEN1069" id= 5629"AEN1069"></a>TYPE_ALPHA</h1> 5630<p>This field type accepts alphabetic data; no blanks, no digits, 5631no special characters (this is checked at character-entry time). It 5632is set up with:</p> 5633<table border="0" bgcolor="#E0E0E0" width="100%"> 5634<tr> 5635<td> 5636<pre class="PROGRAMLISTING"> 5637<font color= 5638"#000000">int set_field_type(FIELD *field, /* field to alter */ | 7267<PRE 7268CLASS="PROGRAMLISTING" 7269>FIELDTYPE *field_type(FIELD *field); /* field to query */</PRE 7270></P 7271><P 7272>The form driver validates the data in a field only when data is entered by the 7273end-user. Validation does not occur when </P 7274><P 7275></P 7276><UL 7277><LI 7278><P 7279>the application program changes the field value by calling set_field_buffer. </P 7280></LI 7281><LI 7282><P 7283>linked field values are changed indirectly -- by changing the field to which 7284they are linked</P 7285></LI 7286></UL 7287><P 7288>The following are the pre-defined validation types. You can also specify custom 7289validation, though it's a bit tricky and cumbersome.</P 7290><H1 7291CLASS="BRIDGEHEAD" 7292><A 7293NAME="AEN1069" 7294></A 7295>TYPE_ALPHA</H1 7296><P 7297>This field type accepts alphabetic data; no blanks, no digits, no special 7298characters (this is checked at character-entry time). It is set up with: </P 7299><PRE 7300CLASS="PROGRAMLISTING" 7301>int set_field_type(FIELD *field, /* field to alter */ |
5639 TYPE_ALPHA, /* type to associate */ | 7302 TYPE_ALPHA, /* type to associate */ |
5640 int width); /* maximum width of field */</font> 5641</pre></td> 5642</tr> 5643</table> 5644<p>The width argument sets a minimum width of data. The user has to 5645enter at-least width number of characters before he can leave the 5646field. Typically you'll want to set this to the field width; if 5647it's greater than the field width, the validation check will always 5648fail. A minimum width of zero makes field completion optional.</p> 5649<h1 class="BRIDGEHEAD"><a name="AEN1073" id= 5650"AEN1073"></a>TYPE_ALNUM</h1> 5651<p>This field type accepts alphabetic data and digits; no blanks, 5652no special characters (this is checked at character-entry time). It 5653is set up with:</p> 5654<table border="0" bgcolor="#E0E0E0" width="100%"> 5655<tr> 5656<td> 5657<pre class="PROGRAMLISTING"> 5658<font color= 5659"#000000">int set_field_type(FIELD *field, /* field to alter */ | 7303 int width); /* maximum width of field */</PRE 7304><P 7305>The width argument sets a minimum width of data. The user has to enter at-least 7306width number of characters before he can leave the field. Typically 7307you'll want to set this to the field width; if it's greater than the 7308field width, the validation check will always fail. A minimum width 7309of zero makes field completion optional. </P 7310><H1 7311CLASS="BRIDGEHEAD" 7312><A 7313NAME="AEN1073" 7314></A 7315>TYPE_ALNUM</H1 7316><P 7317>This field type accepts alphabetic data and digits; no blanks, no special 7318characters (this is checked at character-entry time). It is set up with: </P 7319><PRE 7320CLASS="PROGRAMLISTING" 7321>int set_field_type(FIELD *field, /* field to alter */ |
5660 TYPE_ALNUM, /* type to associate */ | 7322 TYPE_ALNUM, /* type to associate */ |
5661 int width); /* maximum width of field */</font> 5662</pre></td> 5663</tr> 5664</table> 5665<p>The width argument sets a minimum width of data. As with 5666TYPE_ALPHA, typically you'll want to set this to the field width; 5667if it's greater than the field width, the validation check will 5668always fail. A minimum width of zero makes field completion 5669optional.</p> 5670<h1 class="BRIDGEHEAD"><a name="AEN1077" id= 5671"AEN1077"></a>TYPE_ENUM</h1> 5672<p>This type allows you to restrict a field's values to be among a 5673specified set of string values (for example, the two-letter postal 5674codes for U.S. states). It is set up with:</p> 5675<table border="0" bgcolor="#E0E0E0" width="100%"> 5676<tr> 5677<td> 5678<pre class="PROGRAMLISTING"> 5679<font color= 5680"#000000">int set_field_type(FIELD *field, /* field to alter */ | 7323 int width); /* maximum width of field */</PRE 7324><P 7325>The width argument sets a minimum width of data. As with 7326TYPE_ALPHA, typically you'll want to set this to the field width; if it's 7327greater than the field width, the validation check will always fail. A 7328minimum width of zero makes field completion optional. </P 7329><H1 7330CLASS="BRIDGEHEAD" 7331><A 7332NAME="AEN1077" 7333></A 7334>TYPE_ENUM</H1 7335><P 7336>This type allows you to restrict a field's values to be among a specified 7337set of string values (for example, the two-letter postal codes for U.S. 7338states). It is set up with: </P 7339><PRE 7340CLASS="PROGRAMLISTING" 7341>int set_field_type(FIELD *field, /* field to alter */ |
5681 TYPE_ENUM, /* type to associate */ 5682 char **valuelist; /* list of possible values */ 5683 int checkcase; /* case-sensitive? */ | 7342 TYPE_ENUM, /* type to associate */ 7343 char **valuelist; /* list of possible values */ 7344 int checkcase; /* case-sensitive? */ |
5684 int checkunique); /* must specify uniquely? */</font> 5685</pre></td> 5686</tr> 5687</table> 5688<p>The valuelist parameter must point at a NULL-terminated list of 5689valid strings. The checkcase argument, if true, makes comparison 5690with the string case-sensitive.</p> 5691<p>When the user exits a TYPE_ENUM field, the validation procedure 5692tries to complete the data in the buffer to a valid entry. If a 5693complete choice string has been entered, it is of course valid. But 5694it is also possible to enter a prefix of a valid string and have it 5695completed for you.</p> 5696<p>By default, if you enter such a prefix and it matches more than 5697one value in the string list, the prefix will be completed to the 5698first matching value. But the checkunique argument, if true, 5699requires prefix matches to be unique in order to be valid.</p> 5700<p>The REQ_NEXT_CHOICE and REQ_PREV_CHOICE input requests can be 5701particularly useful with these fields.</p> 5702<h1 class="BRIDGEHEAD"><a name="AEN1084" id= 5703"AEN1084"></a>TYPE_INTEGER</h1> 5704<p>This field type accepts an integer. It is set up as follows:</p> 5705<table border="0" bgcolor="#E0E0E0" width="100%"> 5706<tr> 5707<td> 5708<pre class="PROGRAMLISTING"> 5709<font color= 5710"#000000">int set_field_type(FIELD *field, /* field to alter */ | 7345 int checkunique); /* must specify uniquely? */</PRE 7346><P 7347>The valuelist parameter must point at a NULL-terminated list of 7348valid strings. The checkcase argument, if true, makes comparison 7349with the string case-sensitive. </P 7350><P 7351>When the user exits a TYPE_ENUM field, the validation procedure tries to 7352complete the data in the buffer to a valid entry. If a complete choice string 7353has been entered, it is of course valid. But it is also possible to enter a 7354prefix of a valid string and have it completed for you. </P 7355><P 7356>By default, if you enter such a prefix and it matches more than one value 7357in the string list, the prefix will be completed to the first matching 7358value. But the checkunique argument, if true, requires prefix 7359matches to be unique in order to be valid. </P 7360><P 7361>The REQ_NEXT_CHOICE and REQ_PREV_CHOICE input requests can be particularly 7362useful with these fields. </P 7363><H1 7364CLASS="BRIDGEHEAD" 7365><A 7366NAME="AEN1084" 7367></A 7368>TYPE_INTEGER</H1 7369><P 7370>This field type accepts an integer. It is set up as follows: </P 7371><PRE 7372CLASS="PROGRAMLISTING" 7373>int set_field_type(FIELD *field, /* field to alter */ |
5711 TYPE_INTEGER, /* type to associate */ 5712 int padding, /* # places to zero-pad to */ | 7374 TYPE_INTEGER, /* type to associate */ 7375 int padding, /* # places to zero-pad to */ |
5713 int vmin, int vmax); /* valid range */</font> 5714</pre></td> 5715</tr> 5716</table> 5717<p>Valid characters consist of an optional leading minus and 5718digits. The range check is performed on exit. If the range maximum 5719is less than or equal to the minimum, the range is ignored.</p> 5720<p>If the value passes its range check, it is padded with as many 5721leading zero digits as necessary to meet the padding argument.</p> 5722<p>A TYPE_INTEGER value buffer can conveniently be interpreted with 5723the C library function atoi(3).</p> 5724<h1 class="BRIDGEHEAD"><a name="AEN1090" id= 5725"AEN1090"></a>TYPE_NUMERIC</h1> 5726<p>This field type accepts a decimal number. It is set up as 5727follows:</p> 5728<table border="0" bgcolor="#E0E0E0" width="100%"> 5729<tr> 5730<td> 5731<pre class="PROGRAMLISTING"> 5732<font color= 5733"#000000">int set_field_type(FIELD *field, /* field to alter */ | 7376 int vmin, int vmax); /* valid range */</PRE 7377><P 7378>Valid characters consist of an optional leading minus and digits. 7379The range check is performed on exit. If the range maximum is less 7380than or equal to the minimum, the range is ignored. </P 7381><P 7382>If the value passes its range check, it is padded with as many leading 7383zero digits as necessary to meet the padding argument. </P 7384><P 7385>A TYPE_INTEGER value buffer can conveniently be interpreted with the C library 7386function atoi(3).</P 7387><H1 7388CLASS="BRIDGEHEAD" 7389><A 7390NAME="AEN1090" 7391></A 7392>TYPE_NUMERIC</H1 7393><P 7394>This field type accepts a decimal number. It is set up as follows: </P 7395><PRE 7396CLASS="PROGRAMLISTING" 7397>int set_field_type(FIELD *field, /* field to alter */ |
5734 TYPE_NUMERIC, /* type to associate */ 5735 int padding, /* # places of precision */ | 7398 TYPE_NUMERIC, /* type to associate */ 7399 int padding, /* # places of precision */ |
5736 int vmin, int vmax); /* valid range */</font> 5737</pre></td> 5738</tr> 5739</table> 5740<p>Valid characters consist of an optional leading minus and 5741digits. possibly including a decimal point. The range check is 5742performed on exit. If the range maximum is less than or equal to 5743the minimum, the range is ignored.</p> 5744<p>If the value passes its range check, it is padded with as many 5745trailing zero digits as necessary to meet the padding argument.</p> 5746<p>A TYPE_NUMERIC value buffer can conveniently be interpreted with 5747the C library function atof(3).</p> 5748<h1 class="BRIDGEHEAD"><a name="AEN1096" id= 5749"AEN1096"></a>TYPE_REGEXP</h1> 5750<p>This field type accepts data matching a regular expression. It 5751is set up as follows:</p> 5752<table border="0" bgcolor="#E0E0E0" width="100%"> 5753<tr> 5754<td> 5755<pre class="PROGRAMLISTING"> 5756<font color= 5757"#000000">int set_field_type(FIELD *field, /* field to alter */ | 7400 int vmin, int vmax); /* valid range */</PRE 7401><P 7402>Valid characters consist of an optional leading minus and digits. possibly 7403including a decimal point. The range check is performed on exit. If the 7404range maximum is less than or equal to the minimum, the range is 7405ignored. </P 7406><P 7407>If the value passes its range check, it is padded with as many trailing 7408zero digits as necessary to meet the padding argument. </P 7409><P 7410>A TYPE_NUMERIC value buffer can conveniently be interpreted with the C library 7411function atof(3).</P 7412><H1 7413CLASS="BRIDGEHEAD" 7414><A 7415NAME="AEN1096" 7416></A 7417>TYPE_REGEXP</H1 7418><P 7419>This field type accepts data matching a regular expression. It is set up 7420as follows: </P 7421><PRE 7422CLASS="PROGRAMLISTING" 7423>int set_field_type(FIELD *field, /* field to alter */ |
5758 TYPE_REGEXP, /* type to associate */ | 7424 TYPE_REGEXP, /* type to associate */ |
5759 char *regexp); /* expression to match */</font> 5760</pre></td> 5761</tr> 5762</table> 5763<p>The syntax for regular expressions is that of regcomp(3). The 5764check for regular-expression match is performed on exit.</p> 5765</div> 5766<div class="SECT2"> 5767<hr> 5768<h3 class="SECT2"><a name="FORMDRIVER" id="FORMDRIVER">18.6. Form 5769Driver: The work horse of the forms system</a></h3> 5770<p>As in the menu system, form_driver() plays a very important role 5771in forms system. All types of requests to forms system should be 5772funneled through form_driver().</p> 5773<table border="0" bgcolor="#E0E0E0" width="100%"> 5774<tr> 5775<td> 5776<pre class="PROGRAMLISTING"> 5777<font color= 5778"#000000">int form_driver(FORM *form, /* form on which to operate */ 5779 int request) /* form request code */</font> 5780</pre></td> 5781</tr> 5782</table> 5783<p>As you have seen some of the examples above, you have to be in a 5784loop looking for user input and then decide whether it's a field 5785data or a form request. The form requests are then passed to 5786form_driver() to do the work.</p> 5787<p>The requests roughly can be divided into following categories. 5788Different requests and their usage is explained below:</p> 5789<div class="SECT3"> 5790<hr> 5791<h4 class="SECT3"><a name="PAGENAVREQ" id="PAGENAVREQ">18.6.1. Page 5792Navigation Requests</a></h4> 5793<p>These requests cause page-level moves through the form, 5794triggering display of a new form screen. A form can be made of 5795multiple pages. If you have a big form with lot of fields and 5796logical sections, then you can divide the form into pages. The 5797function set_new_page() to set a new page at the field 5798specified.</p> 5799<table border="0" bgcolor="#E0E0E0" width="100%"> 5800<tr> 5801<td> 5802<pre class="PROGRAMLISTING"> 5803<font color= 5804"#000000">int set_new_page(FIELD *field,/* Field at which page break to be set or unset */ 5805 bool new_page_flag); /* should be TRUE to put a break */</font> 5806</pre></td> 5807</tr> 5808</table> 5809<p>The following requests allow you to move to different pages</p> 5810<ul> 5811<li> 5812<p><em>REQ_NEXT_PAGE</em> Move to the next form page.</p> 5813</li> 5814<li> 5815<p><em>REQ_PREV_PAGE</em> Move to the previous form page.</p> 5816</li> 5817<li> 5818<p><em>REQ_FIRST_PAGE</em> Move to the first form page.</p> 5819</li> 5820<li> 5821<p><em>REQ_LAST_PAGE</em> Move to the last form page.</p> 5822</li> 5823</ul> 5824<p>These requests treat the list as cyclic; that is, REQ_NEXT_PAGE 5825from the last page goes to the first, and REQ_PREV_PAGE from the 5826first page goes to the last.</p> 5827</div> 5828<div class="SECT3"> 5829<hr> 5830<h4 class="SECT3"><a name="INTERFIELDNAVREQ" id= 5831"INTERFIELDNAVREQ">18.6.2. Inter-Field Navigation Requests</a></h4> 5832<p>These requests handle navigation between fields on the same 5833page.</p> 5834<ul> 5835<li> 5836<p><em>REQ_NEXT_FIELD</em> Move to next field.</p> 5837</li> 5838<li> 5839<p><em>REQ_PREV_FIELD</em> Move to previous field.</p> 5840</li> 5841<li> 5842<p><em>REQ_FIRST_FIELD</em> Move to the first field.</p> 5843</li> 5844<li> 5845<p><em>REQ_LAST_FIELD</em> Move to the last field.</p> 5846</li> 5847<li> 5848<p><em>REQ_SNEXT_FIELD</em> Move to sorted next field.</p> 5849</li> 5850<li> 5851<p><em>REQ_SPREV_FIELD</em> Move to sorted previous field.</p> 5852</li> 5853<li> 5854<p><em>REQ_SFIRST_FIELD</em> Move to the sorted first field.</p> 5855</li> 5856<li> 5857<p><em>REQ_SLAST_FIELD</em> Move to the sorted last field.</p> 5858</li> 5859<li> 5860<p><em>REQ_LEFT_FIELD</em> Move left to field.</p> 5861</li> 5862<li> 5863<p><em>REQ_RIGHT_FIELD</em> Move right to field.</p> 5864</li> 5865<li> 5866<p><em>REQ_UP_FIELD</em> Move up to field.</p> 5867</li> 5868<li> 5869<p><em>REQ_DOWN_FIELD</em> Move down to field.</p> 5870</li> 5871</ul> 5872<p>These requests treat the list of fields on a page as cyclic; 5873that is, REQ_NEXT_FIELD from the last field goes to the first, and 5874REQ_PREV_FIELD from the first field goes to the last. The order of 5875the fields for these (and the REQ_FIRST_FIELD and REQ_LAST_FIELD 5876requests) is simply the order of the field pointers in the form 5877array (as set up by new_form() or set_form_fields()</p> 5878<p>It is also possible to traverse the fields as if they had been 5879sorted in screen-position order, so the sequence goes left-to-right 5880and top-to-bottom. To do this, use the second group of four 5881sorted-movement requests.</p> 5882<p>Finally, it is possible to move between fields using visual 5883directions up, down, right, and left. To accomplish this, use the 5884third group of four requests. Note, however, that the position of a 5885form for purposes of these requests is its upper-left corner.</p> 5886<p>For example, suppose you have a multi-line field B, and two 5887single-line fields A and C on the same line with B, with A to the 5888left of B and C to the right of B. A REQ_MOVE_RIGHT from A will go 5889to B only if A, B, and C all share the same first line; otherwise 5890it will skip over B to C.</p> 5891</div> 5892<div class="SECT3"> 5893<hr> 5894<h4 class="SECT3"><a name="INTRAFIELDNAVREQ" id= 5895"INTRAFIELDNAVREQ">18.6.3. Intra-Field Navigation Requests</a></h4> 5896<p>These requests drive movement of the edit cursor within the 5897currently selected field.</p> 5898<ul> 5899<li> 5900<p><em>REQ_NEXT_CHAR</em> Move to next character.</p> 5901</li> 5902<li> 5903<p><em>REQ_PREV_CHAR</em> Move to previous character.</p> 5904</li> 5905<li> 5906<p><em>REQ_NEXT_LINE</em> Move to next line.</p> 5907</li> 5908<li> 5909<p><em>REQ_PREV_LINE</em> Move to previous line.</p> 5910</li> 5911<li> 5912<p><em>REQ_NEXT_WORD</em> Move to next word.</p> 5913</li> 5914<li> 5915<p><em>REQ_PREV_WORD</em> Move to previous word.</p> 5916</li> 5917<li> 5918<p><em>REQ_BEG_FIELD</em> Move to beginning of field.</p> 5919</li> 5920<li> 5921<p><em>REQ_END_FIELD</em> Move to end of field.</p> 5922</li> 5923<li> 5924<p><em>REQ_BEG_LINE</em> Move to beginning of line.</p> 5925</li> 5926<li> 5927<p><em>REQ_END_LINE</em> Move to end of line.</p> 5928</li> 5929<li> 5930<p><em>REQ_LEFT_CHAR</em> Move left in field.</p> 5931</li> 5932<li> 5933<p><em>REQ_RIGHT_CHAR</em> Move right in field.</p> 5934</li> 5935<li> 5936<p><em>REQ_UP_CHAR</em> Move up in field.</p> 5937</li> 5938<li> 5939<p><em>REQ_DOWN_CHAR</em> Move down in field.</p> 5940</li> 5941</ul> 5942<p>Each word is separated from the previous and next characters by 5943whitespace. The commands to move to beginning and end of line or 5944field look for the first or last non-pad character in their 5945ranges.</p> 5946</div> 5947<div class="SECT3"> 5948<hr> 5949<h4 class="SECT3"><a name="SCROLLREQ" id="SCROLLREQ">18.6.4. 5950Scrolling Requests</a></h4> 5951<p>Fields that are dynamic and have grown and fields explicitly 5952created with offscreen rows are scrollable. One-line fields scroll 5953horizontally; multi-line fields scroll vertically. Most scrolling 5954is triggered by editing and intra-field movement (the library 5955scrolls the field to keep the cursor visible). It is possible to 5956explicitly request scrolling with the following requests:</p> 5957<ul> 5958<li> 5959<p><em>REQ_SCR_FLINE</em> Scroll vertically forward a line.</p> 5960</li> 5961<li> 5962<p><em>REQ_SCR_BLINE</em> Scroll vertically backward a line.</p> 5963</li> 5964<li> 5965<p><em>REQ_SCR_FPAGE</em> Scroll vertically forward a page.</p> 5966</li> 5967<li> 5968<p><em>REQ_SCR_BPAGE</em> Scroll vertically backward a page.</p> 5969</li> 5970<li> 5971<p><em>REQ_SCR_FHPAGE</em> Scroll vertically forward half a 5972page.</p> 5973</li> 5974<li> 5975<p><em>REQ_SCR_BHPAGE</em> Scroll vertically backward half a 5976page.</p> 5977</li> 5978<li> 5979<p><em>REQ_SCR_FCHAR</em> Scroll horizontally forward a 5980character.</p> 5981</li> 5982<li> 5983<p><em>REQ_SCR_BCHAR</em> Scroll horizontally backward a 5984character.</p> 5985</li> 5986<li> 5987<p><em>REQ_SCR_HFLINE</em> Scroll horizontally one field width 5988forward.</p> 5989</li> 5990<li> 5991<p><em>REQ_SCR_HBLINE</em> Scroll horizontally one field width 5992backward.</p> 5993</li> 5994<li> 5995<p><em>REQ_SCR_HFHALF</em> Scroll horizontally one half field width 5996forward.</p> 5997</li> 5998<li> 5999<p><em>REQ_SCR_HBHALF</em> Scroll horizontally one half field width 6000backward.</p> 6001</li> 6002</ul> 6003<p>For scrolling purposes, a page of a field is the height of its 6004visible part.</p> 6005</div> 6006<div class="SECT3"> 6007<hr> 6008<h4 class="SECT3"><a name="EDITREQ" id="EDITREQ">18.6.5. Editing 6009Requests</a></h4> 6010<p>When you pass the forms driver an ASCII character, it is treated 6011as a request to add the character to the field's data buffer. 6012Whether this is an insertion or a replacement depends on the 6013field's edit mode (insertion is the default.</p> 6014<p>The following requests support editing the field and changing 6015the edit mode:</p> 6016<ul> 6017<li> 6018<p><em>REQ_INS_MODE</em> Set insertion mode.</p> 6019</li> 6020<li> 6021<p><em>REQ_OVL_MODE</em> Set overlay mode.</p> 6022</li> 6023<li> 6024<p><em>REQ_NEW_LINE</em> New line request (see below for 6025explanation).</p> 6026</li> 6027<li> 6028<p><em>REQ_INS_CHAR</em> Insert space at character location.</p> 6029</li> 6030<li> 6031<p><em>REQ_INS_LINE</em> Insert blank line at character 6032location.</p> 6033</li> 6034<li> 6035<p><em>REQ_DEL_CHAR</em> Delete character at cursor.</p> 6036</li> 6037<li> 6038<p><em>REQ_DEL_PREV</em> Delete previous word at cursor.</p> 6039</li> 6040<li> 6041<p><em>REQ_DEL_LINE</em> Delete line at cursor.</p> 6042</li> 6043<li> 6044<p><em>REQ_DEL_WORD</em> Delete word at cursor.</p> 6045</li> 6046<li> 6047<p><em>REQ_CLR_EOL</em> Clear to end of line.</p> 6048</li> 6049<li> 6050<p><em>REQ_CLR_EOF</em> Clear to end of field.</p> 6051</li> 6052<li> 6053<p><em>REQ_CLR_FIELD</em> Clear entire field.</p> 6054</li> 6055</ul> 6056<p>The behavior of the REQ_NEW_LINE and REQ_DEL_PREV requests is 6057complicated and partly controlled by a pair of forms options. The 6058special cases are triggered when the cursor is at the beginning of 6059a field, or on the last line of the field.</p> 6060<p>First, we consider REQ_NEW_LINE:</p> 6061<p>The normal behavior of REQ_NEW_LINE in insert mode is to break 6062the current line at the position of the edit cursor, inserting the 6063portion of the current line after the cursor as a new line 6064following the current and moving the cursor to the beginning of 6065that new line (you may think of this as inserting a newline in the 6066field buffer).</p> 6067<p>The normal behavior of REQ_NEW_LINE in overlay mode is to clear 6068the current line from the position of the edit cursor to end of 6069line. The cursor is then moved to the beginning of the next 6070line.</p> 6071<p>However, REQ_NEW_LINE at the beginning of a field, or on the 6072last line of a field, instead does a REQ_NEXT_FIELD. O_NL_OVERLOAD 6073option is off, this special action is disabled.</p> 6074<p>Now, let us consider REQ_DEL_PREV:</p> 6075<p>The normal behavior of REQ_DEL_PREV is to delete the previous 6076character. If insert mode is on, and the cursor is at the start of 6077a line, and the text on that line will fit on the previous one, it 6078instead appends the contents of the current line to the previous 6079one and deletes the current line (you may think of this as deleting 6080a newline from the field buffer).</p> 6081<p>However, REQ_DEL_PREV at the beginning of a field is instead 6082treated as a REQ_PREV_FIELD.</p> 6083<p>If the O_BS_OVERLOAD option is off, this special action is 6084disabled and the forms driver just returns E_REQUEST_DENIED.</p> 6085</div> 6086<div class="SECT3"> 6087<hr> 6088<h4 class="SECT3"><a name="ORDERREQ" id="ORDERREQ">18.6.6. Order 6089Requests</a></h4> 6090<p>If the type of your field is ordered, and has associated 6091functions for getting the next and previous values of the type from 6092a given value, there are requests that can fetch that value into 6093the field buffer:</p> 6094<ul> 6095<li> 6096<p><em>REQ_NEXT_CHOICE</em> Place the successor value of the 6097current value in the buffer.</p> 6098</li> 6099<li> 6100<p><em>REQ_PREV_CHOICE</em> Place the predecessor value of the 6101current value in the buffer.</p> 6102</li> 6103</ul> 6104<p>Of the built-in field types, only TYPE_ENUM has built-in 6105successor and predecessor functions. When you define a field type 6106of your own (see Custom Validation Types), you can associate our 6107own ordering functions.</p> 6108</div> 6109<div class="SECT3"> 6110<hr> 6111<h4 class="SECT3"><a name="APPLICCOMMANDS" id= 6112"APPLICCOMMANDS">18.6.7. Application Commands</a></h4> 6113<p>Form requests are represented as integers above the curses value 6114greater than KEY_MAX and less than or equal to the constant 6115MAX_COMMAND. A value within this range gets ignored by 6116form_driver(). So this can be used for any purpose by the 6117application. It can be treated as an application specific action 6118and take corresponding action.</p> 6119</div> 6120</div> 6121</div> 6122<div class="SECT1"> 6123<hr> 6124<h2 class="SECT1"><a name="TOOLS" id="TOOLS">19. Tools and Widget 6125Libraries</a></h2> 6126<p>Now that you have seen the capabilities of ncurses and its 6127sister libraries, you are rolling your sleeves up and gearing for a 6128project that heavily manipulates screen. But wait.. It can be 6129pretty difficult to write and maintain complex GUI widgets in plain 6130ncurses or even with the additional libraries. There are some 6131ready-to-use tools and widget libraries that can be used instead of 6132writing your own widgets. You can use some of them, get ideas from 6133the code, or even extend them.</p> 6134<div class="SECT2"> 6135<hr> 6136<h3 class="SECT2"><a name="CDK" id="CDK">19.1. CDK (Curses 6137Development Kit)</a></h3> 6138<p>In the author's words</p> 6139<p><em>CDK stands for 'Curses Development Kit' and it currently 6140contains 21 ready to use widgets which facilitate the speedy 6141development of full screen curses programs.</em></p> 6142<p>The kit provides some useful widgets, which can be used in your 6143programs directly. It's pretty well written and the documentation 6144is very good. The examples in the examples directory can be a good 6145place to start for beginners. The CDK can be downloaded from 6146<a href="http://invisible-island.net/cdk/" target= 6147"_top">http://invisible-island.net/cdk/</a> . Follow the 6148instructions in README file to install it.</p> 6149<div class="SECT3"> 6150<hr> 6151<h4 class="SECT3"><a name="WIDGETLIST" id="WIDGETLIST">19.1.1. 6152Widget List</a></h4> 6153<p>The following is the list of widgets provided with cdk and their 6154description.</p> 6155<table border="0" bgcolor="#E0E0E0" width="100%"> 6156<tr> 6157<td> 6158<pre class="PROGRAMLISTING"> 6159<font color="#000000">Widget Type Quick Description | 7425 char *regexp); /* expression to match */</PRE 7426><P 7427>The syntax for regular expressions is that of regcomp(3). 7428The check for regular-expression match is performed on exit.</P 7429></DIV 7430><DIV 7431CLASS="SECT2" 7432><HR><H3 7433CLASS="SECT2" 7434><A 7435NAME="FORMDRIVER" 7436>18.6. Form Driver: The work horse of the forms system</A 7437></H3 7438><P 7439>As in the menu system, form_driver() plays a very important role in forms 7440system. All types of requests to forms system should be funneled through 7441form_driver().</P 7442><PRE 7443CLASS="PROGRAMLISTING" 7444>int form_driver(FORM *form, /* form on which to operate */ 7445 int request) /* form request code */</PRE 7446><P 7447>As you have seen some of the examples above, you have to be in a loop looking 7448for user input and then decide whether it's a field data or a form request. The 7449form requests are then passed to form_driver() to do the work.</P 7450><P 7451>The requests roughly can be divided into following categories. Different 7452requests and their usage is explained below:</P 7453><DIV 7454CLASS="SECT3" 7455><HR><H4 7456CLASS="SECT3" 7457><A 7458NAME="PAGENAVREQ" 7459>18.6.1. Page Navigation Requests</A 7460></H4 7461><P 7462>These requests cause page-level moves through the form, triggering display of a 7463new form screen. A form can be made of multiple pages. If you have a big form 7464with lot of fields and logical sections, then you can divide the form into 7465pages. The function set_new_page() to set a new page at the field specified.</P 7466><PRE 7467CLASS="PROGRAMLISTING" 7468>int set_new_page(FIELD *field,/* Field at which page break to be set or unset */ 7469 bool new_page_flag); /* should be TRUE to put a break */</PRE 7470><P 7471>The following requests allow you to move to different pages</P 7472><P 7473></P 7474><UL 7475><LI 7476><P 7477><SPAN 7478CLASS="emphasis" 7479><I 7480CLASS="EMPHASIS" 7481>REQ_NEXT_PAGE</I 7482></SPAN 7483> Move to the next form page.</P 7484></LI 7485><LI 7486><P 7487><SPAN 7488CLASS="emphasis" 7489><I 7490CLASS="EMPHASIS" 7491>REQ_PREV_PAGE</I 7492></SPAN 7493> Move to the previous 7494form page.</P 7495></LI 7496><LI 7497><P 7498><SPAN 7499CLASS="emphasis" 7500><I 7501CLASS="EMPHASIS" 7502>REQ_FIRST_PAGE</I 7503></SPAN 7504> Move to the first form page.</P 7505></LI 7506><LI 7507><P 7508><SPAN 7509CLASS="emphasis" 7510><I 7511CLASS="EMPHASIS" 7512>REQ_LAST_PAGE</I 7513></SPAN 7514> Move to the last form page. </P 7515></LI 7516></UL 7517><P 7518>These requests treat the list as cyclic; that is, REQ_NEXT_PAGE from the 7519last page goes to the first, and REQ_PREV_PAGE from the first page goes to 7520the last.</P 7521></DIV 7522><DIV 7523CLASS="SECT3" 7524><HR><H4 7525CLASS="SECT3" 7526><A 7527NAME="INTERFIELDNAVREQ" 7528>18.6.2. Inter-Field Navigation Requests</A 7529></H4 7530><P 7531>These requests handle navigation between fields on the same page.</P 7532><P 7533></P 7534><UL 7535><LI 7536><P 7537><SPAN 7538CLASS="emphasis" 7539><I 7540CLASS="EMPHASIS" 7541>REQ_NEXT_FIELD</I 7542></SPAN 7543> 7544 Move to next field. </P 7545></LI 7546><LI 7547><P 7548><SPAN 7549CLASS="emphasis" 7550><I 7551CLASS="EMPHASIS" 7552>REQ_PREV_FIELD</I 7553></SPAN 7554> 7555 Move to previous field. </P 7556></LI 7557><LI 7558><P 7559><SPAN 7560CLASS="emphasis" 7561><I 7562CLASS="EMPHASIS" 7563>REQ_FIRST_FIELD</I 7564></SPAN 7565> 7566 Move to the first field. </P 7567></LI 7568><LI 7569><P 7570><SPAN 7571CLASS="emphasis" 7572><I 7573CLASS="EMPHASIS" 7574>REQ_LAST_FIELD</I 7575></SPAN 7576> 7577 Move to the last field. </P 7578></LI 7579><LI 7580><P 7581><SPAN 7582CLASS="emphasis" 7583><I 7584CLASS="EMPHASIS" 7585>REQ_SNEXT_FIELD</I 7586></SPAN 7587> 7588 Move to sorted next field. </P 7589></LI 7590><LI 7591><P 7592><SPAN 7593CLASS="emphasis" 7594><I 7595CLASS="EMPHASIS" 7596>REQ_SPREV_FIELD</I 7597></SPAN 7598> 7599 Move to sorted previous field. </P 7600></LI 7601><LI 7602><P 7603><SPAN 7604CLASS="emphasis" 7605><I 7606CLASS="EMPHASIS" 7607>REQ_SFIRST_FIELD</I 7608></SPAN 7609> 7610 Move to the sorted first field. </P 7611></LI 7612><LI 7613><P 7614><SPAN 7615CLASS="emphasis" 7616><I 7617CLASS="EMPHASIS" 7618>REQ_SLAST_FIELD</I 7619></SPAN 7620> 7621 Move to the sorted last field. </P 7622></LI 7623><LI 7624><P 7625><SPAN 7626CLASS="emphasis" 7627><I 7628CLASS="EMPHASIS" 7629>REQ_LEFT_FIELD</I 7630></SPAN 7631> 7632 Move left to field. </P 7633></LI 7634><LI 7635><P 7636><SPAN 7637CLASS="emphasis" 7638><I 7639CLASS="EMPHASIS" 7640>REQ_RIGHT_FIELD</I 7641></SPAN 7642> 7643 Move right to field. </P 7644></LI 7645><LI 7646><P 7647><SPAN 7648CLASS="emphasis" 7649><I 7650CLASS="EMPHASIS" 7651>REQ_UP_FIELD</I 7652></SPAN 7653> 7654 Move up to field. </P 7655></LI 7656><LI 7657><P 7658><SPAN 7659CLASS="emphasis" 7660><I 7661CLASS="EMPHASIS" 7662>REQ_DOWN_FIELD</I 7663></SPAN 7664> 7665 Move down to field. </P 7666></LI 7667></UL 7668><P 7669>These requests treat the list of fields on a page as cyclic; that is, 7670REQ_NEXT_FIELD from the last field goes to the first, and REQ_PREV_FIELD 7671from the first field goes to the last. The order of the fields for these 7672(and the REQ_FIRST_FIELD and REQ_LAST_FIELD requests) is simply the order of 7673the field pointers in the form array (as set up by new_form() or 7674set_form_fields()</P 7675><P 7676>It is also possible to traverse the fields as if they had been sorted in 7677screen-position order, so the sequence goes left-to-right and top-to-bottom. 7678To do this, use the second group of four sorted-movement requests.</P 7679><P 7680>Finally, it is possible to move between fields using visual directions up, 7681down, right, and left. To accomplish this, use the third group of four 7682requests. Note, however, that the position of a form for purposes of these 7683requests is its upper-left corner.</P 7684><P 7685>For example, suppose you have a multi-line field B, and two single-line 7686fields A and C on the same line with B, with A to the left of B and C to the 7687right of B. A REQ_MOVE_RIGHT from A will go to B only if A, B, and C all 7688share the same first line; otherwise it will skip over B to C.</P 7689></DIV 7690><DIV 7691CLASS="SECT3" 7692><HR><H4 7693CLASS="SECT3" 7694><A 7695NAME="INTRAFIELDNAVREQ" 7696>18.6.3. Intra-Field Navigation Requests</A 7697></H4 7698><P 7699>These requests drive movement of the edit cursor within the currently 7700selected field.</P 7701><P 7702></P 7703><UL 7704><LI 7705><P 7706><SPAN 7707CLASS="emphasis" 7708><I 7709CLASS="EMPHASIS" 7710>REQ_NEXT_CHAR</I 7711></SPAN 7712> 7713 Move to next character. </P 7714></LI 7715><LI 7716><P 7717><SPAN 7718CLASS="emphasis" 7719><I 7720CLASS="EMPHASIS" 7721>REQ_PREV_CHAR</I 7722></SPAN 7723> 7724 Move to previous character. </P 7725></LI 7726><LI 7727><P 7728><SPAN 7729CLASS="emphasis" 7730><I 7731CLASS="EMPHASIS" 7732>REQ_NEXT_LINE</I 7733></SPAN 7734> 7735 Move to next line. </P 7736></LI 7737><LI 7738><P 7739><SPAN 7740CLASS="emphasis" 7741><I 7742CLASS="EMPHASIS" 7743>REQ_PREV_LINE</I 7744></SPAN 7745> 7746 Move to previous line. </P 7747></LI 7748><LI 7749><P 7750><SPAN 7751CLASS="emphasis" 7752><I 7753CLASS="EMPHASIS" 7754>REQ_NEXT_WORD</I 7755></SPAN 7756> 7757 Move to next word. </P 7758></LI 7759><LI 7760><P 7761><SPAN 7762CLASS="emphasis" 7763><I 7764CLASS="EMPHASIS" 7765>REQ_PREV_WORD</I 7766></SPAN 7767> 7768 Move to previous word. </P 7769></LI 7770><LI 7771><P 7772><SPAN 7773CLASS="emphasis" 7774><I 7775CLASS="EMPHASIS" 7776>REQ_BEG_FIELD</I 7777></SPAN 7778> 7779 Move to beginning of field. </P 7780></LI 7781><LI 7782><P 7783><SPAN 7784CLASS="emphasis" 7785><I 7786CLASS="EMPHASIS" 7787>REQ_END_FIELD</I 7788></SPAN 7789> 7790 Move to end of field. </P 7791></LI 7792><LI 7793><P 7794><SPAN 7795CLASS="emphasis" 7796><I 7797CLASS="EMPHASIS" 7798>REQ_BEG_LINE</I 7799></SPAN 7800> 7801 Move to beginning of line. </P 7802></LI 7803><LI 7804><P 7805><SPAN 7806CLASS="emphasis" 7807><I 7808CLASS="EMPHASIS" 7809>REQ_END_LINE</I 7810></SPAN 7811> 7812 Move to end of line. </P 7813></LI 7814><LI 7815><P 7816><SPAN 7817CLASS="emphasis" 7818><I 7819CLASS="EMPHASIS" 7820>REQ_LEFT_CHAR</I 7821></SPAN 7822> 7823 Move left in field. </P 7824></LI 7825><LI 7826><P 7827><SPAN 7828CLASS="emphasis" 7829><I 7830CLASS="EMPHASIS" 7831>REQ_RIGHT_CHAR</I 7832></SPAN 7833> 7834 Move right in field. </P 7835></LI 7836><LI 7837><P 7838><SPAN 7839CLASS="emphasis" 7840><I 7841CLASS="EMPHASIS" 7842>REQ_UP_CHAR</I 7843></SPAN 7844> 7845 Move up in field. </P 7846></LI 7847><LI 7848><P 7849><SPAN 7850CLASS="emphasis" 7851><I 7852CLASS="EMPHASIS" 7853>REQ_DOWN_CHAR</I 7854></SPAN 7855> 7856 Move down in field. </P 7857></LI 7858></UL 7859><P 7860>Each word is separated from the previous and next characters by whitespace. 7861The commands to move to beginning and end of line or field look for the 7862first or last non-pad character in their ranges.</P 7863></DIV 7864><DIV 7865CLASS="SECT3" 7866><HR><H4 7867CLASS="SECT3" 7868><A 7869NAME="SCROLLREQ" 7870>18.6.4. Scrolling Requests</A 7871></H4 7872><P 7873>Fields that are dynamic and have grown and fields explicitly created with 7874offscreen rows are scrollable. One-line fields scroll horizontally; 7875multi-line fields scroll vertically. Most scrolling is triggered by editing 7876and intra-field movement (the library scrolls the field to keep the cursor 7877visible). It is possible to explicitly request scrolling with the following 7878requests:</P 7879><P 7880></P 7881><UL 7882><LI 7883><P 7884><SPAN 7885CLASS="emphasis" 7886><I 7887CLASS="EMPHASIS" 7888>REQ_SCR_FLINE</I 7889></SPAN 7890> 7891 Scroll vertically forward a line. </P 7892></LI 7893><LI 7894><P 7895><SPAN 7896CLASS="emphasis" 7897><I 7898CLASS="EMPHASIS" 7899>REQ_SCR_BLINE</I 7900></SPAN 7901> 7902 Scroll vertically backward a line. </P 7903></LI 7904><LI 7905><P 7906><SPAN 7907CLASS="emphasis" 7908><I 7909CLASS="EMPHASIS" 7910>REQ_SCR_FPAGE</I 7911></SPAN 7912> 7913 Scroll vertically forward a page. </P 7914></LI 7915><LI 7916><P 7917><SPAN 7918CLASS="emphasis" 7919><I 7920CLASS="EMPHASIS" 7921>REQ_SCR_BPAGE</I 7922></SPAN 7923> 7924 Scroll vertically backward a page. </P 7925></LI 7926><LI 7927><P 7928><SPAN 7929CLASS="emphasis" 7930><I 7931CLASS="EMPHASIS" 7932>REQ_SCR_FHPAGE</I 7933></SPAN 7934> 7935 Scroll vertically forward half a page. </P 7936></LI 7937><LI 7938><P 7939><SPAN 7940CLASS="emphasis" 7941><I 7942CLASS="EMPHASIS" 7943>REQ_SCR_BHPAGE</I 7944></SPAN 7945> 7946 Scroll vertically backward half a page. </P 7947></LI 7948><LI 7949><P 7950><SPAN 7951CLASS="emphasis" 7952><I 7953CLASS="EMPHASIS" 7954>REQ_SCR_FCHAR</I 7955></SPAN 7956> 7957 Scroll horizontally forward a character. </P 7958></LI 7959><LI 7960><P 7961><SPAN 7962CLASS="emphasis" 7963><I 7964CLASS="EMPHASIS" 7965>REQ_SCR_BCHAR</I 7966></SPAN 7967> 7968 Scroll horizontally backward a character. </P 7969></LI 7970><LI 7971><P 7972><SPAN 7973CLASS="emphasis" 7974><I 7975CLASS="EMPHASIS" 7976>REQ_SCR_HFLINE</I 7977></SPAN 7978> 7979 Scroll horizontally one field width forward. </P 7980></LI 7981><LI 7982><P 7983><SPAN 7984CLASS="emphasis" 7985><I 7986CLASS="EMPHASIS" 7987>REQ_SCR_HBLINE</I 7988></SPAN 7989> 7990 Scroll horizontally one field width backward. </P 7991></LI 7992><LI 7993><P 7994><SPAN 7995CLASS="emphasis" 7996><I 7997CLASS="EMPHASIS" 7998>REQ_SCR_HFHALF</I 7999></SPAN 8000> 8001 Scroll horizontally one half field width forward. </P 8002></LI 8003><LI 8004><P 8005><SPAN 8006CLASS="emphasis" 8007><I 8008CLASS="EMPHASIS" 8009>REQ_SCR_HBHALF</I 8010></SPAN 8011> 8012 Scroll horizontally one half field width backward. </P 8013></LI 8014></UL 8015><P 8016>For scrolling purposes, a page of a field is the height of its visible part.</P 8017></DIV 8018><DIV 8019CLASS="SECT3" 8020><HR><H4 8021CLASS="SECT3" 8022><A 8023NAME="EDITREQ" 8024>18.6.5. Editing Requests</A 8025></H4 8026><P 8027>When you pass the forms driver an ASCII character, it is treated as a 8028request to add the character to the field's data buffer. Whether this is an 8029insertion or a replacement depends on the field's edit mode (insertion is 8030the default.</P 8031><P 8032>The following requests support editing the field and changing the edit mode:</P 8033><P 8034></P 8035><UL 8036><LI 8037><P 8038><SPAN 8039CLASS="emphasis" 8040><I 8041CLASS="EMPHASIS" 8042>REQ_INS_MODE</I 8043></SPAN 8044> 8045 Set insertion mode. </P 8046></LI 8047><LI 8048><P 8049><SPAN 8050CLASS="emphasis" 8051><I 8052CLASS="EMPHASIS" 8053>REQ_OVL_MODE</I 8054></SPAN 8055> 8056 Set overlay mode. </P 8057></LI 8058><LI 8059><P 8060><SPAN 8061CLASS="emphasis" 8062><I 8063CLASS="EMPHASIS" 8064>REQ_NEW_LINE</I 8065></SPAN 8066> 8067 New line request (see below for explanation). </P 8068></LI 8069><LI 8070><P 8071><SPAN 8072CLASS="emphasis" 8073><I 8074CLASS="EMPHASIS" 8075>REQ_INS_CHAR</I 8076></SPAN 8077> 8078 Insert space at character location. </P 8079></LI 8080><LI 8081><P 8082><SPAN 8083CLASS="emphasis" 8084><I 8085CLASS="EMPHASIS" 8086>REQ_INS_LINE</I 8087></SPAN 8088> 8089 Insert blank line at character location. </P 8090></LI 8091><LI 8092><P 8093><SPAN 8094CLASS="emphasis" 8095><I 8096CLASS="EMPHASIS" 8097>REQ_DEL_CHAR</I 8098></SPAN 8099> 8100 Delete character at cursor. </P 8101></LI 8102><LI 8103><P 8104><SPAN 8105CLASS="emphasis" 8106><I 8107CLASS="EMPHASIS" 8108>REQ_DEL_PREV</I 8109></SPAN 8110> 8111 Delete previous word at cursor. </P 8112></LI 8113><LI 8114><P 8115><SPAN 8116CLASS="emphasis" 8117><I 8118CLASS="EMPHASIS" 8119>REQ_DEL_LINE</I 8120></SPAN 8121> 8122 Delete line at cursor. </P 8123></LI 8124><LI 8125><P 8126><SPAN 8127CLASS="emphasis" 8128><I 8129CLASS="EMPHASIS" 8130>REQ_DEL_WORD</I 8131></SPAN 8132> 8133 Delete word at cursor. </P 8134></LI 8135><LI 8136><P 8137><SPAN 8138CLASS="emphasis" 8139><I 8140CLASS="EMPHASIS" 8141>REQ_CLR_EOL</I 8142></SPAN 8143> 8144 Clear to end of line. </P 8145></LI 8146><LI 8147><P 8148><SPAN 8149CLASS="emphasis" 8150><I 8151CLASS="EMPHASIS" 8152>REQ_CLR_EOF</I 8153></SPAN 8154> 8155 Clear to end of field. </P 8156></LI 8157><LI 8158><P 8159><SPAN 8160CLASS="emphasis" 8161><I 8162CLASS="EMPHASIS" 8163>REQ_CLR_FIELD</I 8164></SPAN 8165> 8166 Clear entire field. </P 8167></LI 8168></UL 8169><P 8170>The behavior of the REQ_NEW_LINE and REQ_DEL_PREV requests is complicated 8171and partly controlled by a pair of forms options. The special cases are 8172triggered when the cursor is at the beginning of a field, or on the last 8173line of the field.</P 8174><P 8175>First, we consider REQ_NEW_LINE:</P 8176><P 8177>The normal behavior of REQ_NEW_LINE in insert mode is to break the current 8178line at the position of the edit cursor, inserting the portion of the 8179current line after the cursor as a new line following the current and moving 8180the cursor to the beginning of that new line (you may think of this as 8181inserting a newline in the field buffer).</P 8182><P 8183>The normal behavior of REQ_NEW_LINE in overlay mode is to clear the current 8184line from the position of the edit cursor to end of line. The cursor is then 8185moved to the beginning of the next line.</P 8186><P 8187>However, REQ_NEW_LINE at the beginning of a field, or on the last line of a 8188field, instead does a REQ_NEXT_FIELD. O_NL_OVERLOAD option is off, this 8189special action is disabled.</P 8190><P 8191>Now, let us consider REQ_DEL_PREV:</P 8192><P 8193>The normal behavior of REQ_DEL_PREV is to delete the previous character. If 8194insert mode is on, and the cursor is at the start of a line, and the text on 8195that line will fit on the previous one, it instead appends the contents of 8196the current line to the previous one and deletes the current line (you may 8197think of this as deleting a newline from the field buffer).</P 8198><P 8199>However, REQ_DEL_PREV at the beginning of a field is instead treated as a 8200REQ_PREV_FIELD.</P 8201><P 8202>If the O_BS_OVERLOAD option is off, this special action is disabled and the 8203forms driver just returns E_REQUEST_DENIED.</P 8204></DIV 8205><DIV 8206CLASS="SECT3" 8207><HR><H4 8208CLASS="SECT3" 8209><A 8210NAME="ORDERREQ" 8211>18.6.6. Order Requests</A 8212></H4 8213><P 8214>If the type of your field is ordered, and has associated functions for 8215getting the next and previous values of the type from a given value, there 8216are requests that can fetch that value into the field buffer:</P 8217><P 8218></P 8219><UL 8220><LI 8221><P 8222><SPAN 8223CLASS="emphasis" 8224><I 8225CLASS="EMPHASIS" 8226>REQ_NEXT_CHOICE</I 8227></SPAN 8228> 8229 Place the successor value of the current value in the buffer. 8230 </P 8231></LI 8232><LI 8233><P 8234><SPAN 8235CLASS="emphasis" 8236><I 8237CLASS="EMPHASIS" 8238>REQ_PREV_CHOICE</I 8239></SPAN 8240> 8241 Place the predecessor value of the current value in the buffer. 8242 </P 8243></LI 8244></UL 8245><P 8246>Of the built-in field types, only TYPE_ENUM has built-in successor and 8247predecessor functions. When you define a field type of your own (see Custom 8248Validation Types), you can associate our own ordering functions.</P 8249></DIV 8250><DIV 8251CLASS="SECT3" 8252><HR><H4 8253CLASS="SECT3" 8254><A 8255NAME="APPLICCOMMANDS" 8256>18.6.7. Application Commands</A 8257></H4 8258><P 8259>Form requests are represented as integers above the curses value greater than 8260KEY_MAX and less than or equal to the constant MAX_COMMAND. A value within this 8261range gets ignored by form_driver(). So this can be used for any purpose by the 8262application. It can be treated as an application specific action and take 8263corresponding action.</P 8264></DIV 8265></DIV 8266></DIV 8267><DIV 8268CLASS="SECT1" 8269><HR><H2 8270CLASS="SECT1" 8271><A 8272NAME="TOOLS" 8273>19. Tools and Widget Libraries</A 8274></H2 8275><P 8276> 8277Now that you have seen the capabilities of ncurses and its sister libraries, you 8278are rolling your sleeves up and gearing for a project that heavily manipulates 8279screen. But wait.. It can be pretty difficult to write and maintain complex GUI 8280widgets in plain ncurses or even with the additional libraries. There are some 8281ready-to-use tools and widget libraries that can be used instead of writing your 8282own widgets. You can use some of them, get ideas from the code, or even extend 8283them.</P 8284><DIV 8285CLASS="SECT2" 8286><HR><H3 8287CLASS="SECT2" 8288><A 8289NAME="CDK" 8290>19.1. CDK (Curses Development Kit)</A 8291></H3 8292><P 8293>In the author's words </P 8294><P 8295><SPAN 8296CLASS="emphasis" 8297><I 8298CLASS="EMPHASIS" 8299> 8300CDK stands for 'Curses Development Kit' and it currently contains 21 ready 8301to use widgets which facilitate the speedy development of full screen 8302curses programs. </I 8303></SPAN 8304></P 8305><P 8306>The kit provides some useful widgets, which can be used in your programs 8307directly. It's pretty well written and the documentation is very good. The 8308examples in the examples directory can be a good place to start for beginners. 8309The CDK can be downloaded from <A 8310HREF="http://invisible-island.net/cdk/" 8311TARGET="_top" 8312>http://invisible-island.net/cdk/</A 8313> 8314. Follow the instructions in 8315README file to install it.</P 8316><DIV 8317CLASS="SECT3" 8318><HR><H4 8319CLASS="SECT3" 8320><A 8321NAME="WIDGETLIST" 8322>19.1.1. Widget List</A 8323></H4 8324><P 8325>The following is the list of widgets provided with cdk and their description.</P 8326><PRE 8327CLASS="PROGRAMLISTING" 8328>Widget Type Quick Description |
6160=========================================================================== 6161Alphalist Allows a user to select from a list of words, with 6162 the ability to narrow the search list by typing in a 6163 few characters of the desired word. 6164Buttonbox This creates a multiple button widget. 6165Calendar Creates a little simple calendar widget. 6166Dialog Prompts the user with a message, and the user 6167 can pick an answer from the buttons provided. --- 26 unchanged lines hidden (view full) --- 6194Selection List Creates a multiple option selection list. 6195Slider Akin to the scale widget, this widget provides a 6196 visual slide bar to represent the numeric value. 6197Template Creates a entry field with character sensitive 6198 positions. Used for pre-formatted fields like 6199 dates and phone numbers. 6200Viewer This is a file/information viewer. Very useful 6201 when you need to display loads of information. | 8329=========================================================================== 8330Alphalist Allows a user to select from a list of words, with 8331 the ability to narrow the search list by typing in a 8332 few characters of the desired word. 8333Buttonbox This creates a multiple button widget. 8334Calendar Creates a little simple calendar widget. 8335Dialog Prompts the user with a message, and the user 8336 can pick an answer from the buttons provided. --- 26 unchanged lines hidden (view full) --- 8363Selection List Creates a multiple option selection list. 8364Slider Akin to the scale widget, this widget provides a 8365 visual slide bar to represent the numeric value. 8366Template Creates a entry field with character sensitive 8367 positions. Used for pre-formatted fields like 8368 dates and phone numbers. 8369Viewer This is a file/information viewer. Very useful 8370 when you need to display loads of information. |
6202===========================================================================</font> 6203</pre></td> 6204</tr> 6205</table> 6206<p>A few of the widgets are modified by Thomas Dickey in recent 6207versions.</p> 6208</div> 6209<div class="SECT3"> 6210<hr> 6211<h4 class="SECT3"><a name="CDKATTRACT" id="CDKATTRACT">19.1.2. Some 6212Attractive Features</a></h4> 6213<p>Apart from making our life easier with readily usable widgets, 6214cdk solves one frustrating problem with printing multi colored 6215strings, justified strings elegantly. Special formatting tags can 6216be embedded in the strings which are passed to CDK functions. For 6217Example</p> 6218<p>If the string</p> 6219<table border="0" bgcolor="#E0E0E0" width="100%"> 6220<tr> 6221<td> 6222<pre class="PROGRAMLISTING"> 6223<font color= 6224"#000000">"</B/1>This line should have a yellow foreground and a blue 6225background.<!1>"</font> 6226</pre></td> 6227</tr> 6228</table> 6229<p>given as a parameter to newCDKLabel(), it prints the line with 6230yellow foreground and blue background. There are other tags 6231available for justifying string, embedding special drawing 6232characters etc.. Please refer to the man page cdk_display(3X) for 6233details. The man page explains the usage with nice examples.</p> 6234</div> 6235<div class="SECT3"> 6236<hr> 6237<h4 class="SECT3"><a name="CDKCONCLUSION" id= 6238"CDKCONCLUSION">19.1.3. Conclusion</a></h4> 6239<p>All in all, CDK is a well-written package of widgets, which if 6240used properly can form a strong frame work for developing complex 6241GUI.</p> 6242</div> 6243</div> 6244<div class="SECT2"> 6245<hr> 6246<h3 class="SECT2"><a name="DIALOG" id="DIALOG">19.2. The 6247dialog</a></h3> 6248<p>Long long ago, in September 1994, when few people knew linux, 6249Jeff Tranter wrote an <a href= 6250"http://www2.linuxjournal.com/lj-issues/issue5/2807.html" target= 6251"_top">article</a> on dialog in Linux Journal. He starts the 6252article with these words..</p> 6253<p><em>Linux is based on the Unix operating system, but also 6254features a number of unique and useful kernel features and 6255application programs that often go beyond what is available under 6256Unix. One little-known gem is "dialog", a utility for creating 6257professional-looking dialog boxes from within shell scripts. This 6258article presents a tutorial introduction to the dialog utility, and 6259shows examples of how and where it can be used</em></p> 6260<p>As he explains, dialog is a real gem in making 6261professional-looking dialog boxes with ease. It creates a variety 6262of dialog boxes, menus, check lists etc.. It is usually installed 6263by default. If not, you can download it from <a href= 6264"http://invisible-island.net/dialog/" target="_top">Thomas 6265Dickey</a>'s site.</p> 6266<p>The above-mentioned article gives a very good overview of its 6267uses and capabilites. The man page has more details. It can be used 6268in variety of situations. One good example is building of linux 6269kernel in text mode. Linux kernel uses a modified version of dialog 6270tailored for its needs.</p> 6271<p>dialog was initially designed to be used with shell scripts. If 6272you want to use its functionality in a c program, then you can use 6273libdialog. The documentation regarding this is sparse. Definitive 6274reference is the dialog.h header file which comes with the library. 6275You may need to hack here and there to get the required output. The 6276source is easily customizable. I have used it on a number of 6277occasions by modifying the code.</p> 6278</div> 6279<div class="SECT2"> 6280<hr> 6281<h3 class="SECT2"><a name="PERLCURSES" id="PERLCURSES">19.3. Perl 6282Curses Modules CURSES::FORM and CURSES::WIDGETS</a></h3> 6283<p>The perl module Curses, Curses::Form and Curses::Widgets give 6284access to curses from perl. If you have curses and basic perl is 6285installed, you can get these modules from <a href= 6286"http://www.cpan.org/modules/01modules.index.html" target= 6287"_top">CPAN All Modules page</a>. Get the three zipped modules in 6288the Curses category. Once installed you can use these modules from 6289perl scripts like any other module. For more information on perl 6290modules see perlmod man page. The above modules come with good 6291documentation and they have some demo scripts to test the 6292functionality. Though the widgets provided are very rudimentary, 6293these modules provide good access to curses library from perl.</p> 6294<p>Some of my code examples are converted to perl by Anuradha 6295Ratnaweera and they are available in the <var class= 6296"LITERAL">perl</var> directory.</p> 6297<p>For more information see man pages Curses(3) , Curses::Form(3) 6298and Curses::Widgets(3). These pages are installed only when the 6299above modules are acquired and installed.</p> 6300</div> 6301</div> 6302<div class="SECT1"> 6303<hr> 6304<h2 class="SECT1"><a name="JUSTFORFUN" id="JUSTFORFUN">20. Just For 6305Fun !!!</a></h2> 6306<p>This section contains few programs written by me just for fun. 6307They don't signify a better programming practice or the best way of 6308using ncurses. They are provided here so as to allow beginners to 6309get ideas and add more programs to this section. If you have 6310written a couple of nice, simple programs in curses and want them 6311to included here, contact <a href="mailto:ppadala@gmail.com" 6312target="_top">me</a>.</p> 6313<div class="SECT2"> 6314<hr> 6315<h3 class="SECT2"><a name="GAMEOFLIFE" id="GAMEOFLIFE">20.1. The 6316Game of Life</a></h3> 6317<p>Game of life is a wonder of math. In <a href= 6318"http://www.math.com/students/wonders/life/life.html" target= 6319"_top">Paul Callahan</a>'s words</p> 6320<table border="0" bgcolor="#E0E0E0" width="100%"> 6321<tr> 6322<td> 6323<pre class="PROGRAMLISTING"> 6324<font color= 6325"#000000"><em>The Game of Life (or simply Life) is not a game in the conventional sense. There | 8371===========================================================================</PRE 8372><P 8373>A few of the widgets are modified by Thomas Dickey in recent versions.</P 8374></DIV 8375><DIV 8376CLASS="SECT3" 8377><HR><H4 8378CLASS="SECT3" 8379><A 8380NAME="CDKATTRACT" 8381>19.1.2. Some Attractive Features</A 8382></H4 8383><P 8384>Apart from making our life easier with readily usable widgets, cdk solves one 8385frustrating problem with printing multi colored strings, justified strings 8386elegantly. Special formatting tags can be embedded in the strings which are 8387passed to CDK functions. For Example</P 8388><P 8389>If the string</P 8390><PRE 8391CLASS="PROGRAMLISTING" 8392>"</B/1>This line should have a yellow foreground and a blue 8393background.<!1>"</PRE 8394><P 8395>given as a parameter to newCDKLabel(), it prints the line with yellow foreground 8396and blue background. There are other tags available for justifying string, 8397embedding special drawing characters etc.. Please refer to the man page 8398cdk_display(3X) for details. The man page explains the usage with nice examples.</P 8399></DIV 8400><DIV 8401CLASS="SECT3" 8402><HR><H4 8403CLASS="SECT3" 8404><A 8405NAME="CDKCONCLUSION" 8406>19.1.3. Conclusion</A 8407></H4 8408><P 8409>All in all, CDK is a well-written package of widgets, which if used properly can 8410form a strong frame work for developing complex GUI.</P 8411></DIV 8412></DIV 8413><DIV 8414CLASS="SECT2" 8415><HR><H3 8416CLASS="SECT2" 8417><A 8418NAME="DIALOG" 8419>19.2. The dialog</A 8420></H3 8421><P 8422>Long long ago, in September 1994, when few people knew linux, Jeff Tranter wrote 8423an <A 8424HREF="http://www2.linuxjournal.com/lj-issues/issue5/2807.html" 8425TARGET="_top" 8426>article</A 8427> on dialog in Linux Journal. He starts the article with these words..</P 8428><P 8429><SPAN 8430CLASS="emphasis" 8431><I 8432CLASS="EMPHASIS" 8433>Linux is based on the Unix operating system, but also features a number of 8434unique and useful kernel features and application programs that often go beyond 8435what is available under Unix. One little-known gem is "dialog", a utility for 8436creating professional-looking dialog boxes from within shell scripts. This 8437article presents a tutorial introduction to the dialog utility, and shows 8438examples of how and where it can be used</I 8439></SPAN 8440></P 8441><P 8442> 8443As he explains, dialog is a real gem in making professional-looking dialog boxes 8444with ease. It creates a variety of dialog boxes, menus, check lists etc.. It is 8445usually installed by default. If not, you can download it from <A 8446HREF="http://invisible-island.net/dialog/" 8447TARGET="_top" 8448>Thomas Dickey</A 8449>'s site. </P 8450><P 8451>The above-mentioned article gives a very good overview of its uses and 8452capabilites. The man page has more details. It can be used in variety of 8453situations. One good example is building of linux kernel in text mode. Linux 8454kernel uses a modified version of dialog tailored for its needs. </P 8455><P 8456>dialog was initially designed to be used with shell scripts. If you want to use 8457its functionality in a c program, then you can use libdialog. The documentation 8458regarding this is sparse. Definitive reference is the dialog.h header file which 8459comes with the library. You may need to hack here and there to get the required 8460output. The source is easily customizable. I have used it on a number of 8461occasions by modifying the code.</P 8462></DIV 8463><DIV 8464CLASS="SECT2" 8465><HR><H3 8466CLASS="SECT2" 8467><A 8468NAME="PERLCURSES" 8469>19.3. Perl Curses Modules CURSES::FORM and CURSES::WIDGETS</A 8470></H3 8471><P 8472>The perl module Curses, Curses::Form and Curses::Widgets give access to curses 8473from perl. If you have curses and basic perl is installed, you can get these 8474modules from <A 8475HREF="http://www.cpan.org/modules/01modules.index.html" 8476TARGET="_top" 8477> CPAN 8478All Modules page</A 8479>. Get the three zipped modules in the Curses category. 8480Once installed you can use these modules from perl scripts like any other 8481module. For more information on perl modules see perlmod man page. The above 8482modules come with good documentation and they have some demo scripts to test the 8483functionality. Though the widgets provided are very rudimentary, these modules 8484provide good access to curses library from perl.</P 8485><P 8486>Some of my code examples are converted to perl by Anuradha Ratnaweera and they 8487are available in the <TT 8488CLASS="LITERAL" 8489>perl</TT 8490> directory.</P 8491><P 8492> 8493For more information see man pages Curses(3) , Curses::Form(3) and 8494Curses::Widgets(3). These pages are installed only when the above modules are 8495acquired and installed.</P 8496></DIV 8497></DIV 8498><DIV 8499CLASS="SECT1" 8500><HR><H2 8501CLASS="SECT1" 8502><A 8503NAME="JUSTFORFUN" 8504>20. Just For Fun !!!</A 8505></H2 8506><P 8507>This section contains few programs written by me just for fun. They don't 8508signify a better programming practice or the best way of using ncurses. They are 8509provided here so as to allow beginners to get ideas and add more programs to 8510this section. If you have written a couple of nice, simple programs in curses 8511and want them to included here, contact <A 8512HREF="mailto:ppadala@gmail.com" 8513TARGET="_top" 8514>me</A 8515>.</P 8516><DIV 8517CLASS="SECT2" 8518><HR><H3 8519CLASS="SECT2" 8520><A 8521NAME="GAMEOFLIFE" 8522>20.1. The Game of Life</A 8523></H3 8524><P 8525>Game of life is a wonder of math. In 8526<A 8527HREF="http://www.math.com/students/wonders/life/life.html" 8528TARGET="_top" 8529>Paul Callahan</A 8530>'s words</P 8531><PRE 8532CLASS="PROGRAMLISTING" 8533><SPAN 8534CLASS="emphasis" 8535><I 8536CLASS="EMPHASIS" 8537>The Game of Life (or simply Life) is not a game in the conventional sense. There |
6326are no players, and no winning or losing. Once the "pieces" are placed in the 6327starting position, the rules determine everything that happens later. 6328Nevertheless, Life is full of surprises! In most cases, it is impossible to look 6329at a starting position (or pattern) and see what will happen in the future. The | 8538are no players, and no winning or losing. Once the "pieces" are placed in the 8539starting position, the rules determine everything that happens later. 8540Nevertheless, Life is full of surprises! In most cases, it is impossible to look 8541at a starting position (or pattern) and see what will happen in the future. The |
6330only way to find out is to follow the rules of the game.</em></font> 6331</pre></td> 6332</tr> 6333</table> 6334<p>This program starts with a simple inverted U pattern and shows 6335how wonderful life works. There is a lot of room for improvement in 6336the program. You can let the user enter pattern of his choice or 6337even take input from a file. You can also change rules and play 6338with a lot of variations. Search on <a href="http://www.google.com" 6339target="_top">google</a> for interesting information on game of 6340life.</p> 6341<p><em>File Path: JustForFun/life.c</em></p> 6342</div> 6343<div class="SECT2"> 6344<hr> 6345<h3 class="SECT2"><a name="MAGIC" id="MAGIC">20.2. Magic 6346Square</a></h3> 6347<p>Magic Square, another wonder of math, is very simple to 6348understand but very difficult to make. In a magic square sum of the 6349numbers in each row, each column is equal. Even diagnol sum can be 6350equal. There are many variations which have special properties.</p> 6351<p>This program creates a simple magic square of odd order.</p> 6352<p><em>File Path: JustForFun/magic.c</em></p> 6353</div> 6354<div class="SECT2"> 6355<hr> 6356<h3 class="SECT2"><a name="HANOI" id="HANOI">20.3. Towers of 6357Hanoi</a></h3> 6358<p>The famous towers of hanoi solver. The aim of the game is to 6359move the disks on the first peg to last peg, using middle peg as a 6360temporary stay. The catch is not to place a larger disk over a 6361small disk at any time.</p> 6362<p><em>File Path: JustForFun/hanoi.c</em></p> 6363</div> 6364<div class="SECT2"> 6365<hr> 6366<h3 class="SECT2"><a name="QUEENS" id="QUEENS">20.4. Queens 6367Puzzle</a></h3> 6368<p>The objective of the famous N-Queen puzzle is to put N queens on 6369a N X N chess board without attacking each other.</p> 6370<p>This program solves it with a simple backtracking technique.</p> 6371<p><em>File Path: JustForFun/queens.c</em></p> 6372</div> 6373<div class="SECT2"> 6374<hr> 6375<h3 class="SECT2"><a name="SHUFFLE" id="SHUFFLE">20.5. 6376Shuffle</a></h3> 6377<p>A fun game, if you have time to kill.</p> 6378<p><em>File Path: JustForFun/shuffle.c</em></p> 6379</div> 6380<div class="SECT2"> 6381<hr> 6382<h3 class="SECT2"><a name="TT" id="TT">20.6. Typing Tutor</a></h3> 6383<p>A simple typing tutor, I created more out of need than for ease 6384of use. If you know how to put your fingers correctly on the 6385keyboard, but lack practice, this can be helpful.</p> 6386<p><em>File Path: JustForFun/tt.c</em></p> 6387</div> 6388</div> 6389<div class="SECT1"> 6390<hr> 6391<h2 class="SECT1"><a name="REF" id="REF">21. References</a></h2> 6392<ul> 6393<li> 6394<p>NCURSES man pages</p> 6395</li> 6396<li> 6397<p>NCURSES FAQ at <a href= 6398"http://invisible-island.net/ncurses/ncurses.faq.html" target= 6399"_top">http://invisible-island.net/ncurses/ncurses.faq.html</a></p> 6400</li> 6401<li> 6402<p>Writing programs with NCURSES by Eric Raymond and Zeyd M. 6403Ben-Halim at <a href= 6404"http://invisible-island.net/ncurses/ncurses-intro.html" target= 6405"_top">http://invisible-island.net/ncurses/ncurses-intro.html</a> - 6406somewhat obsolete. I was inspired by this document and the 6407structure of this HOWTO follows from the original document</p> 6408</li> 6409</ul> 6410</div> 6411</div> 6412</body> 6413</html> | 8542only way to find out is to follow the rules of the game.</I 8543></SPAN 8544></PRE 8545><P 8546>This program starts with a simple inverted U pattern and shows how wonderful 8547life works. There is a lot of room for improvement in the program. You can let 8548the user enter pattern of his choice or even take input from a file. You can 8549also change rules and play with a lot of variations. Search on <A 8550HREF="http://www.google.com" 8551TARGET="_top" 8552>google</A 8553> for interesting information on game 8554of life.</P 8555><P 8556><SPAN 8557CLASS="emphasis" 8558><I 8559CLASS="EMPHASIS" 8560>File Path: JustForFun/life.c</I 8561></SPAN 8562></P 8563></DIV 8564><DIV 8565CLASS="SECT2" 8566><HR><H3 8567CLASS="SECT2" 8568><A 8569NAME="MAGIC" 8570>20.2. Magic Square</A 8571></H3 8572><P 8573>Magic Square, another wonder of math, is very simple to understand but very 8574difficult to make. In a magic square sum of the numbers in each row, each column 8575is equal. Even diagnol sum can be equal. There are many variations which have 8576special properties.</P 8577><P 8578>This program creates a simple magic square of odd order.</P 8579><P 8580><SPAN 8581CLASS="emphasis" 8582><I 8583CLASS="EMPHASIS" 8584>File Path: JustForFun/magic.c</I 8585></SPAN 8586></P 8587></DIV 8588><DIV 8589CLASS="SECT2" 8590><HR><H3 8591CLASS="SECT2" 8592><A 8593NAME="HANOI" 8594>20.3. Towers of Hanoi</A 8595></H3 8596><P 8597>The famous towers of hanoi solver. The aim of the game is to move the disks on 8598the first peg to last peg, using middle peg as a temporary stay. The catch is 8599not to place a larger disk over a small disk at any time.</P 8600><P 8601><SPAN 8602CLASS="emphasis" 8603><I 8604CLASS="EMPHASIS" 8605>File Path: JustForFun/hanoi.c</I 8606></SPAN 8607></P 8608></DIV 8609><DIV 8610CLASS="SECT2" 8611><HR><H3 8612CLASS="SECT2" 8613><A 8614NAME="QUEENS" 8615>20.4. Queens Puzzle</A 8616></H3 8617><P 8618>The objective of the famous N-Queen puzzle is to put N queens on a N X N chess 8619board without attacking each other. </P 8620><P 8621>This program solves it with a simple backtracking technique.</P 8622><P 8623><SPAN 8624CLASS="emphasis" 8625><I 8626CLASS="EMPHASIS" 8627>File Path: JustForFun/queens.c</I 8628></SPAN 8629></P 8630></DIV 8631><DIV 8632CLASS="SECT2" 8633><HR><H3 8634CLASS="SECT2" 8635><A 8636NAME="SHUFFLE" 8637>20.5. Shuffle</A 8638></H3 8639><P 8640>A fun game, if you have time to kill. </P 8641><P 8642><SPAN 8643CLASS="emphasis" 8644><I 8645CLASS="EMPHASIS" 8646>File Path: JustForFun/shuffle.c</I 8647></SPAN 8648></P 8649></DIV 8650><DIV 8651CLASS="SECT2" 8652><HR><H3 8653CLASS="SECT2" 8654><A 8655NAME="TT" 8656>20.6. Typing Tutor</A 8657></H3 8658><P 8659>A simple typing tutor, I created more out of need than for ease of use. If you 8660know how to put your fingers correctly on the keyboard, but lack practice, this 8661can be helpful. </P 8662><P 8663><SPAN 8664CLASS="emphasis" 8665><I 8666CLASS="EMPHASIS" 8667>File Path: JustForFun/tt.c</I 8668></SPAN 8669></P 8670></DIV 8671></DIV 8672><DIV 8673CLASS="SECT1" 8674><HR><H2 8675CLASS="SECT1" 8676><A 8677NAME="REF" 8678>21. References</A 8679></H2 8680><P 8681></P 8682><UL 8683><LI 8684><P 8685>NCURSES man pages </P 8686></LI 8687><LI 8688><P 8689>NCURSES FAQ at <A 8690HREF="http://invisible-island.net/ncurses/ncurses.faq.html" 8691TARGET="_top" 8692>http://invisible-island.net/ncurses/ncurses.faq.html</A 8693> 8694 </P 8695></LI 8696><LI 8697><P 8698>Writing programs with NCURSES by Eric Raymond and Zeyd M. 8699Ben-Halim at 8700<A 8701HREF="http://invisible-island.net/ncurses/ncurses-intro.html" 8702TARGET="_top" 8703>http://invisible-island.net/ncurses/ncurses-intro.html</A 8704> - somewhat 8705obsolete. I was inspired by this document and the structure of this HOWTO 8706follows from the original document</P 8707></LI 8708></UL 8709></DIV 8710></DIV 8711></BODY 8712></HTML 8713> |