Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 |
| 7 | NAME="GENERATOR" |
| 8 | CONTENT="Modular DocBook HTML Stylesheet Version 1.79"></HEAD |
| 9 | ><BODY |
| 10 | CLASS="ARTICLE" |
| 11 | BGCOLOR="#FFFFFF" |
| 12 | TEXT="#000000" |
| 13 | LINK="#0000FF" |
| 14 | VLINK="#840084" |
| 15 | ALINK="#0000FF" |
| 16 | ><DIV |
| 17 | CLASS="ARTICLE" |
| 18 | ><DIV |
| 19 | CLASS="TITLEPAGE" |
| 20 | ><H1 |
| 21 | CLASS="TITLE" |
| 22 | ><A |
| 23 | NAME="AEN2" |
| 24 | >NCURSES Programming HOWTO</A |
| 25 | ></H1 |
| 26 | ><H3 |
| 27 | CLASS="AUTHOR" |
| 28 | ><A |
| 29 | NAME="AEN4" |
| 30 | > Pradeep Padala </A |
| 31 | ></H3 |
| 32 | ><DIV |
| 33 | CLASS="AFFILIATION" |
| 34 | ><DIV |
| 35 | CLASS="ADDRESS" |
| 36 | ><P |
| 37 | CLASS="ADDRESS" |
| 38 | ><CODE |
| 39 | CLASS="EMAIL" |
| 40 | ><<A |
| 41 | HREF="mailto:ppadala@gmail.com" |
| 42 | >ppadala@gmail.com</A |
| 43 | >></CODE |
| 44 | ></P |
| 45 | ></DIV |
| 46 | ></DIV |
| 47 | ><P |
| 48 | CLASS="PUBDATE" |
| 49 | >v1.9, 2005-06-20<BR></P |
| 50 | ><DIV |
| 51 | CLASS="REVHISTORY" |
| 52 | ><TABLE |
| 53 | WIDTH="100%" |
| 54 | BORDER="0" |
| 55 | ><TR |
| 56 | ><TH |
| 57 | ALIGN="LEFT" |
| 58 | VALIGN="TOP" |
| 59 | COLSPAN="3" |
| 60 | ><B |
| 61 | >Revision History</B |
| 62 | ></TH |
| 63 | ></TR |
| 64 | ><TR |
| 65 | ><TD |
| 66 | ALIGN="LEFT" |
| 67 | >Revision 1.9</TD |
| 68 | ><TD |
| 69 | ALIGN="LEFT" |
| 70 | >2005-06-20</TD |
| 71 | ><TD |
| 72 | ALIGN="LEFT" |
| 73 | >Revised by: ppadala</TD |
| 74 | ></TR |
| 75 | ><TR |
| 76 | ><TD |
| 77 | ALIGN="LEFT" |
| 78 | COLSPAN="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 |
| 84 | ALIGN="LEFT" |
| 85 | >Revision 1.8</TD |
| 86 | ><TD |
| 87 | ALIGN="LEFT" |
| 88 | >2005-06-17</TD |
| 89 | ><TD |
| 90 | ALIGN="LEFT" |
| 91 | >Revised by: ppadala</TD |
| 92 | ></TR |
| 93 | ><TR |
| 94 | ><TD |
| 95 | ALIGN="LEFT" |
| 96 | COLSPAN="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 |
| 103 | ALIGN="LEFT" |
| 104 | >Revision 1.7.1</TD |
| 105 | ><TD |
| 106 | ALIGN="LEFT" |
| 107 | >2002-06-25</TD |
| 108 | ><TD |
| 109 | ALIGN="LEFT" |
| 110 | >Revised by: ppadala</TD |
| 111 | ></TR |
| 112 | ><TR |
| 113 | ><TD |
| 114 | ALIGN="LEFT" |
| 115 | COLSPAN="3" |
| 116 | >Added a README file for building and instructions |
| 117 | for building from source.</TD |
| 118 | ></TR |
| 119 | ><TR |
| 120 | ><TD |
| 121 | ALIGN="LEFT" |
| 122 | >Revision 1.7</TD |
| 123 | ><TD |
| 124 | ALIGN="LEFT" |
| 125 | >2002-06-25</TD |
| 126 | ><TD |
| 127 | ALIGN="LEFT" |
| 128 | >Revised by: ppadala</TD |
| 129 | ></TR |
| 130 | ><TR |
| 131 | ><TD |
| 132 | ALIGN="LEFT" |
| 133 | COLSPAN="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 |
| 139 | ALIGN="LEFT" |
| 140 | >Revision 1.6.1</TD |
| 141 | ><TD |
| 142 | ALIGN="LEFT" |
| 143 | >2002-02-24</TD |
| 144 | ><TD |
| 145 | ALIGN="LEFT" |
| 146 | >Revised by: ppadala</TD |
| 147 | ></TR |
| 148 | ><TR |
| 149 | ><TD |
| 150 | ALIGN="LEFT" |
| 151 | COLSPAN="3" |
| 152 | >Removed the old Changelog section, cleaned the makefiles</TD |
| 153 | ></TR |
| 154 | ><TR |
| 155 | ><TD |
| 156 | ALIGN="LEFT" |
| 157 | >Revision 1.6</TD |
| 158 | ><TD |
| 159 | ALIGN="LEFT" |
| 160 | >2002-02-16</TD |
| 161 | ><TD |
| 162 | ALIGN="LEFT" |
| 163 | >Revised by: ppadala</TD |
| 164 | ></TR |
| 165 | ><TR |
| 166 | ><TD |
| 167 | ALIGN="LEFT" |
| 168 | COLSPAN="3" |
| 169 | >Corrected a lot of spelling mistakes, added ACS variables |
| 170 | section</TD |
| 171 | ></TR |
| 172 | ><TR |
| 173 | ><TD |
| 174 | ALIGN="LEFT" |
| 175 | >Revision 1.5</TD |
| 176 | ><TD |
| 177 | ALIGN="LEFT" |
| 178 | >2002-01-05</TD |
| 179 | ><TD |
| 180 | ALIGN="LEFT" |
| 181 | >Revised by: ppadala</TD |
| 182 | ></TR |
| 183 | ><TR |
| 184 | ><TD |
| 185 | ALIGN="LEFT" |
| 186 | COLSPAN="3" |
| 187 | >Changed structure to present proper TOC</TD |
| 188 | ></TR |
| 189 | ><TR |
| 190 | ><TD |
| 191 | ALIGN="LEFT" |
| 192 | >Revision 1.3.1</TD |
| 193 | ><TD |
| 194 | ALIGN="LEFT" |
| 195 | >2001-07-26</TD |
| 196 | ><TD |
| 197 | ALIGN="LEFT" |
| 198 | >Revised by: ppadala</TD |
| 199 | ></TR |
| 200 | ><TR |
| 201 | ><TD |
| 202 | ALIGN="LEFT" |
| 203 | COLSPAN="3" |
| 204 | >Corrected maintainers paragraph, Corrected stable release number</TD |
| 205 | ></TR |
| 206 | ><TR |
| 207 | ><TD |
| 208 | ALIGN="LEFT" |
| 209 | >Revision 1.3</TD |
| 210 | ><TD |
| 211 | ALIGN="LEFT" |
| 212 | >2001-07-24</TD |
| 213 | ><TD |
| 214 | ALIGN="LEFT" |
| 215 | >Revised by: ppadala</TD |
| 216 | ></TR |
| 217 | ><TR |
| 218 | ><TD |
| 219 | ALIGN="LEFT" |
| 220 | COLSPAN="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 |
| 227 | ALIGN="LEFT" |
| 228 | >Revision 1.2</TD |
| 229 | ><TD |
| 230 | ALIGN="LEFT" |
| 231 | >2001-06-05</TD |
| 232 | ><TD |
| 233 | ALIGN="LEFT" |
| 234 | >Revised by: ppadala</TD |
| 235 | ></TR |
| 236 | ><TR |
| 237 | ><TD |
| 238 | ALIGN="LEFT" |
| 239 | COLSPAN="3" |
| 240 | >Incorporated ravi's changes. Mainly to introduction, menu, |
| 241 | form, justforfun sections</TD |
| 242 | ></TR |
| 243 | ><TR |
| 244 | ><TD |
| 245 | ALIGN="LEFT" |
| 246 | >Revision 1.1</TD |
| 247 | ><TD |
| 248 | ALIGN="LEFT" |
| 249 | >2001-05-22</TD |
| 250 | ><TD |
| 251 | ALIGN="LEFT" |
| 252 | >Revised by: ppadala</TD |
| 253 | ></TR |
| 254 | ><TR |
| 255 | ><TD |
| 256 | ALIGN="LEFT" |
| 257 | COLSPAN="3" |
| 258 | >Added "a word about window" section, Added scanw_example.</TD |
| 259 | ></TR |
| 260 | ></TABLE |
| 261 | ></DIV |
| 262 | ><DIV |
| 263 | ><DIV |
| 264 | CLASS="ABSTRACT" |
| 265 | ><P |
| 266 | ></P |
| 267 | ><A |
| 268 | NAME="AEN67" |
| 269 | ></A |
| 270 | ><P |
| 271 | > <SPAN |
| 272 | CLASS="emphasis" |
| 273 | ><I |
| 274 | CLASS="EMPHASIS" |
| 275 | >This document is intended to be an "All in One" guide for programming with |
| 276 | ncurses and its sister libraries. We graduate from a simple "Hello World" |
| 277 | program to more complex form manipulation. No prior experience in ncurses is |
| 278 | assumed. Send comments to <A |
| 279 | HREF="mailto:ppadala@gmail.com" |
| 280 | TARGET="_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 |
| 293 | CLASS="TOC" |
| 294 | ><DL |
| 295 | ><DT |
| 296 | ><B |
| 297 | >Table of Contents</B |
| 298 | ></DT |
| 299 | ><DT |
| 300 | >1. <A |
| 301 | HREF="#INTRO" |
| 302 | >Introduction</A |
| 303 | ></DT |
| 304 | ><DD |
| 305 | ><DL |
| 306 | ><DT |
| 307 | >1.1. <A |
| 308 | HREF="#WHATIS" |
| 309 | >What is NCURSES?</A |
| 310 | ></DT |
| 311 | ><DT |
| 312 | >1.2. <A |
| 313 | HREF="#WHATCANWEDO" |
| 314 | >What we can do with NCURSES</A |
| 315 | ></DT |
| 316 | ><DT |
| 317 | >1.3. <A |
| 318 | HREF="#WHERETOGETIT" |
| 319 | >Where to get it</A |
| 320 | ></DT |
| 321 | ><DT |
| 322 | >1.4. <A |
| 323 | HREF="#PURPOSE" |
| 324 | >Purpose/Scope of the document</A |
| 325 | ></DT |
| 326 | ><DT |
| 327 | >1.5. <A |
| 328 | HREF="#ABOUTPROGRAMS" |
| 329 | >About the Programs</A |
| 330 | ></DT |
| 331 | ><DT |
| 332 | >1.6. <A |
| 333 | HREF="#OTHERFORMATS" |
| 334 | >Other Formats of the document</A |
| 335 | ></DT |
| 336 | ><DD |
| 337 | ><DL |
| 338 | ><DT |
| 339 | >1.6.1. <A |
| 340 | HREF="#LISTFORMATS" |
| 341 | >Readily available formats from tldp.org</A |
| 342 | ></DT |
| 343 | ><DT |
| 344 | >1.6.2. <A |
| 345 | HREF="#BUILDSOURCE" |
| 346 | >Building from source</A |
| 347 | ></DT |
| 348 | ></DL |
| 349 | ></DD |
| 350 | ><DT |
| 351 | >1.7. <A |
| 352 | HREF="#CREDITS" |
| 353 | >Credits</A |
| 354 | ></DT |
| 355 | ><DT |
| 356 | >1.8. <A |
| 357 | HREF="#WISHLIST" |
| 358 | >Wish List</A |
| 359 | ></DT |
| 360 | ><DT |
| 361 | >1.9. <A |
| 362 | HREF="#COPYRIGHT" |
| 363 | >Copyright</A |
| 364 | ></DT |
| 365 | ></DL |
| 366 | ></DD |
| 367 | ><DT |
| 368 | >2. <A |
| 369 | HREF="#HELLOWORLD" |
| 370 | >Hello World !!!</A |
| 371 | ></DT |
| 372 | ><DD |
| 373 | ><DL |
| 374 | ><DT |
| 375 | >2.1. <A |
| 376 | HREF="#COMPILECURSES" |
| 377 | >Compiling With the NCURSES Library</A |
| 378 | ></DT |
| 379 | ><DT |
| 380 | >2.2. <A |
| 381 | HREF="#DISSECTION" |
| 382 | >Dissection</A |
| 383 | ></DT |
| 384 | ><DD |
| 385 | ><DL |
| 386 | ><DT |
| 387 | >2.2.1. <A |
| 388 | HREF="#ABOUT-INITSCR" |
| 389 | >About initscr()</A |
| 390 | ></DT |
| 391 | ><DT |
| 392 | >2.2.2. <A |
| 393 | HREF="#MYST-REFRESH" |
| 394 | >The mysterious refresh()</A |
| 395 | ></DT |
| 396 | ><DT |
| 397 | >2.2.3. <A |
| 398 | HREF="#ABOUT-ENDWIN" |
| 399 | >About endwin()</A |
| 400 | ></DT |
| 401 | ></DL |
| 402 | ></DD |
| 403 | ></DL |
| 404 | ></DD |
| 405 | ><DT |
| 406 | >3. <A |
| 407 | HREF="#GORY" |
| 408 | >The Gory Details</A |
| 409 | ></DT |
| 410 | ><DT |
| 411 | >4. <A |
| 412 | HREF="#INIT" |
| 413 | >Initialization</A |
| 414 | ></DT |
| 415 | ><DD |
| 416 | ><DL |
| 417 | ><DT |
| 418 | >4.1. <A |
| 419 | HREF="#ABOUTINIT" |
| 420 | >Initialization functions</A |
| 421 | ></DT |
| 422 | ><DT |
| 423 | >4.2. <A |
| 424 | HREF="#RAWCBREAK" |
| 425 | >raw() and cbreak()</A |
| 426 | ></DT |
| 427 | ><DT |
| 428 | >4.3. <A |
| 429 | HREF="#ECHONOECHO" |
| 430 | >echo() and noecho()</A |
| 431 | ></DT |
| 432 | ><DT |
| 433 | >4.4. <A |
| 434 | HREF="#KEYPAD" |
| 435 | >keypad()</A |
| 436 | ></DT |
| 437 | ><DT |
| 438 | >4.5. <A |
| 439 | HREF="#HALFDELAY" |
| 440 | >halfdelay()</A |
| 441 | ></DT |
| 442 | ><DT |
| 443 | >4.6. <A |
| 444 | HREF="#MISCINIT" |
| 445 | >Miscellaneous Initialization functions</A |
| 446 | ></DT |
| 447 | ><DT |
| 448 | >4.7. <A |
| 449 | HREF="#INITEX" |
| 450 | >An Example</A |
| 451 | ></DT |
| 452 | ></DL |
| 453 | ></DD |
| 454 | ><DT |
| 455 | >5. <A |
| 456 | HREF="#AWORDWINDOWS" |
| 457 | >A Word about Windows</A |
| 458 | ></DT |
| 459 | ><DT |
| 460 | >6. <A |
| 461 | HREF="#PRINTW" |
| 462 | >Output functions</A |
| 463 | ></DT |
| 464 | ><DD |
| 465 | ><DL |
| 466 | ><DT |
| 467 | >6.1. <A |
| 468 | HREF="#ADDCHCLASS" |
| 469 | >addch() class of functions</A |
| 470 | ></DT |
| 471 | ><DT |
| 472 | >6.2. <A |
| 473 | HREF="#AEN298" |
| 474 | >mvaddch(), waddch() and mvwaddch()</A |
| 475 | ></DT |
| 476 | ><DT |
| 477 | >6.3. <A |
| 478 | HREF="#PRINTWCLASS" |
| 479 | >printw() class of functions</A |
| 480 | ></DT |
| 481 | ><DD |
| 482 | ><DL |
| 483 | ><DT |
| 484 | >6.3.1. <A |
| 485 | HREF="#PRINTWMVPRINTW" |
| 486 | >printw() and mvprintw</A |
| 487 | ></DT |
| 488 | ><DT |
| 489 | >6.3.2. <A |
| 490 | HREF="#WPRINTWMVWPRINTW" |
| 491 | >wprintw() and mvwprintw</A |
| 492 | ></DT |
| 493 | ><DT |
| 494 | >6.3.3. <A |
| 495 | HREF="#VWPRINTW" |
| 496 | >vwprintw()</A |
| 497 | ></DT |
| 498 | ><DT |
| 499 | >6.3.4. <A |
| 500 | HREF="#SIMPLEPRINTWEX" |
| 501 | >A Simple printw example</A |
| 502 | ></DT |
| 503 | ></DL |
| 504 | ></DD |
| 505 | ><DT |
| 506 | >6.4. <A |
| 507 | HREF="#ADDSTRCLASS" |
| 508 | >addstr() class of functions</A |
| 509 | ></DT |
| 510 | ><DT |
| 511 | >6.5. <A |
| 512 | HREF="#ACAUTION" |
| 513 | >A word of caution</A |
| 514 | ></DT |
| 515 | ></DL |
| 516 | ></DD |
| 517 | ><DT |
| 518 | >7. <A |
| 519 | HREF="#SCANW" |
| 520 | >Input functions</A |
| 521 | ></DT |
| 522 | ><DD |
| 523 | ><DL |
| 524 | ><DT |
| 525 | >7.1. <A |
| 526 | HREF="#GETCHCLASS" |
| 527 | >getch() class of functions</A |
| 528 | ></DT |
| 529 | ><DT |
| 530 | >7.2. <A |
| 531 | HREF="#SCANWCLASS" |
| 532 | >scanw() class of functions</A |
| 533 | ></DT |
| 534 | ><DD |
| 535 | ><DL |
| 536 | ><DT |
| 537 | >7.2.1. <A |
| 538 | HREF="#SCANWMVSCANW" |
| 539 | >scanw() and mvscanw</A |
| 540 | ></DT |
| 541 | ><DT |
| 542 | >7.2.2. <A |
| 543 | HREF="#WSCANWMVWSCANW" |
| 544 | >wscanw() and mvwscanw()</A |
| 545 | ></DT |
| 546 | ><DT |
| 547 | >7.2.3. <A |
| 548 | HREF="#VWSCANW" |
| 549 | >vwscanw()</A |
| 550 | ></DT |
| 551 | ></DL |
| 552 | ></DD |
| 553 | ><DT |
| 554 | >7.3. <A |
| 555 | HREF="#GETSTRCLASS" |
| 556 | >getstr() class of functions</A |
| 557 | ></DT |
| 558 | ><DT |
| 559 | >7.4. <A |
| 560 | HREF="#GETSTREX" |
| 561 | >Some examples</A |
| 562 | ></DT |
| 563 | ></DL |
| 564 | ></DD |
| 565 | ><DT |
| 566 | >8. <A |
| 567 | HREF="#ATTRIB" |
| 568 | >Attributes</A |
| 569 | ></DT |
| 570 | ><DD |
| 571 | ><DL |
| 572 | ><DT |
| 573 | >8.1. <A |
| 574 | HREF="#ATTRIBDETAILS" |
| 575 | >The details</A |
| 576 | ></DT |
| 577 | ><DT |
| 578 | >8.2. <A |
| 579 | HREF="#ATTRONVSATTRSET" |
| 580 | >attron() vs attrset()</A |
| 581 | ></DT |
| 582 | ><DT |
| 583 | >8.3. <A |
| 584 | HREF="#ATTR_GET" |
| 585 | >attr_get()</A |
| 586 | ></DT |
| 587 | ><DT |
| 588 | >8.4. <A |
| 589 | HREF="#ATTR_FUNCS" |
| 590 | >attr_ functions</A |
| 591 | ></DT |
| 592 | ><DT |
| 593 | >8.5. <A |
| 594 | HREF="#WATTRFUNCS" |
| 595 | >wattr functions</A |
| 596 | ></DT |
| 597 | ><DT |
| 598 | >8.6. <A |
| 599 | HREF="#CHGAT" |
| 600 | >chgat() functions</A |
| 601 | ></DT |
| 602 | ></DL |
| 603 | ></DD |
| 604 | ><DT |
| 605 | >9. <A |
| 606 | HREF="#WINDOWS" |
| 607 | >Windows</A |
| 608 | ></DT |
| 609 | ><DD |
| 610 | ><DL |
| 611 | ><DT |
| 612 | >9.1. <A |
| 613 | HREF="#WINDOWBASICS" |
| 614 | >The basics</A |
| 615 | ></DT |
| 616 | ><DT |
| 617 | >9.2. <A |
| 618 | HREF="#LETBEWINDOW" |
| 619 | >Let there be a Window !!!</A |
| 620 | ></DT |
| 621 | ><DT |
| 622 | >9.3. <A |
| 623 | HREF="#BORDEREXEXPL" |
| 624 | >Explanation</A |
| 625 | ></DT |
| 626 | ><DT |
| 627 | >9.4. <A |
| 628 | HREF="#OTHERSTUFF" |
| 629 | >The other stuff in the example</A |
| 630 | ></DT |
| 631 | ><DT |
| 632 | >9.5. <A |
| 633 | HREF="#OTHERBORDERFUNCS" |
| 634 | >Other Border functions</A |
| 635 | ></DT |
| 636 | ></DL |
| 637 | ></DD |
| 638 | ><DT |
| 639 | >10. <A |
| 640 | HREF="#COLOR" |
| 641 | >Colors</A |
| 642 | ></DT |
| 643 | ><DD |
| 644 | ><DL |
| 645 | ><DT |
| 646 | >10.1. <A |
| 647 | HREF="#COLORBASICS" |
| 648 | >The basics</A |
| 649 | ></DT |
| 650 | ><DT |
| 651 | >10.2. <A |
| 652 | HREF="#CHANGECOLORDEFS" |
| 653 | >Changing Color Definitions</A |
| 654 | ></DT |
| 655 | ><DT |
| 656 | >10.3. <A |
| 657 | HREF="#COLORCONTENT" |
| 658 | >Color Content</A |
| 659 | ></DT |
| 660 | ></DL |
| 661 | ></DD |
| 662 | ><DT |
| 663 | >11. <A |
| 664 | HREF="#KEYS" |
| 665 | >Interfacing with the key board</A |
| 666 | ></DT |
| 667 | ><DD |
| 668 | ><DL |
| 669 | ><DT |
| 670 | >11.1. <A |
| 671 | HREF="#KEYSBASICS" |
| 672 | >The Basics</A |
| 673 | ></DT |
| 674 | ><DT |
| 675 | >11.2. <A |
| 676 | HREF="#SIMPLEKEYEX" |
| 677 | >A Simple Key Usage example</A |
| 678 | ></DT |
| 679 | ></DL |
| 680 | ></DD |
| 681 | ><DT |
| 682 | >12. <A |
| 683 | HREF="#MOUSE" |
| 684 | >Interfacing with the mouse</A |
| 685 | ></DT |
| 686 | ><DD |
| 687 | ><DL |
| 688 | ><DT |
| 689 | >12.1. <A |
| 690 | HREF="#MOUSEBASICS" |
| 691 | >The Basics</A |
| 692 | ></DT |
| 693 | ><DT |
| 694 | >12.2. <A |
| 695 | HREF="#GETTINGEVENTS" |
| 696 | >Getting the events</A |
| 697 | ></DT |
| 698 | ><DT |
| 699 | >12.3. <A |
| 700 | HREF="#MOUSETOGETHER" |
| 701 | >Putting it all Together</A |
| 702 | ></DT |
| 703 | ><DT |
| 704 | >12.4. <A |
| 705 | HREF="#MISCMOUSEFUNCS" |
| 706 | >Miscellaneous Functions</A |
| 707 | ></DT |
| 708 | ></DL |
| 709 | ></DD |
| 710 | ><DT |
| 711 | >13. <A |
| 712 | HREF="#SCREEN" |
| 713 | >Screen Manipulation</A |
| 714 | ></DT |
| 715 | ><DD |
| 716 | ><DL |
| 717 | ><DT |
| 718 | >13.1. <A |
| 719 | HREF="#GETYX" |
| 720 | >getyx() functions</A |
| 721 | ></DT |
| 722 | ><DT |
| 723 | >13.2. <A |
| 724 | HREF="#SCREENDUMP" |
| 725 | >Screen Dumping</A |
| 726 | ></DT |
| 727 | ><DT |
| 728 | >13.3. <A |
| 729 | HREF="#WINDOWDUMP" |
| 730 | >Window Dumping</A |
| 731 | ></DT |
| 732 | ></DL |
| 733 | ></DD |
| 734 | ><DT |
| 735 | >14. <A |
| 736 | HREF="#MISC" |
| 737 | >Miscellaneous features</A |
| 738 | ></DT |
| 739 | ><DD |
| 740 | ><DL |
| 741 | ><DT |
| 742 | >14.1. <A |
| 743 | HREF="#CURSSET" |
| 744 | >curs_set()</A |
| 745 | ></DT |
| 746 | ><DT |
| 747 | >14.2. <A |
| 748 | HREF="#TEMPLEAVE" |
| 749 | >Temporarily Leaving Curses mode</A |
| 750 | ></DT |
| 751 | ><DT |
| 752 | >14.3. <A |
| 753 | HREF="#ACSVARS" |
| 754 | >ACS_ variables</A |
| 755 | ></DT |
| 756 | ></DL |
| 757 | ></DD |
| 758 | ><DT |
| 759 | >15. <A |
| 760 | HREF="#OTHERLIB" |
| 761 | >Other libraries</A |
| 762 | ></DT |
| 763 | ><DT |
| 764 | >16. <A |
| 765 | HREF="#PANELS" |
| 766 | >Panel Library</A |
| 767 | ></DT |
| 768 | ><DD |
| 769 | ><DL |
| 770 | ><DT |
| 771 | >16.1. <A |
| 772 | HREF="#PANELBASICS" |
| 773 | >The Basics</A |
| 774 | ></DT |
| 775 | ><DT |
| 776 | >16.2. <A |
| 777 | HREF="#COMPILEPANELS" |
| 778 | >Compiling With the Panels Library</A |
| 779 | ></DT |
| 780 | ><DT |
| 781 | >16.3. <A |
| 782 | HREF="#PANELBROWSING" |
| 783 | >Panel Window Browsing</A |
| 784 | ></DT |
| 785 | ><DT |
| 786 | >16.4. <A |
| 787 | HREF="#USERPTRUSING" |
| 788 | >Using User Pointers</A |
| 789 | ></DT |
| 790 | ><DT |
| 791 | >16.5. <A |
| 792 | HREF="#PANELMOVERESIZE" |
| 793 | >Moving and Resizing Panels</A |
| 794 | ></DT |
| 795 | ><DT |
| 796 | >16.6. <A |
| 797 | HREF="#PANELSHOWHIDE" |
| 798 | >Hiding and Showing Panels</A |
| 799 | ></DT |
| 800 | ><DT |
| 801 | >16.7. <A |
| 802 | HREF="#PANELABOVE" |
| 803 | >panel_above() and panel_below() Functions</A |
| 804 | ></DT |
| 805 | ></DL |
| 806 | ></DD |
| 807 | ><DT |
| 808 | >17. <A |
| 809 | HREF="#MENUS" |
| 810 | >Menus Library</A |
| 811 | ></DT |
| 812 | ><DD |
| 813 | ><DL |
| 814 | ><DT |
| 815 | >17.1. <A |
| 816 | HREF="#MENUBASICS" |
| 817 | >The Basics</A |
| 818 | ></DT |
| 819 | ><DT |
| 820 | >17.2. <A |
| 821 | HREF="#COMPILEMENUS" |
| 822 | >Compiling With the Menu Library</A |
| 823 | ></DT |
| 824 | ><DT |
| 825 | >17.3. <A |
| 826 | HREF="#MENUDRIVER" |
| 827 | >Menu Driver: The work horse of the menu system</A |
| 828 | ></DT |
| 829 | ><DT |
| 830 | >17.4. <A |
| 831 | HREF="#MENUWINDOWS" |
| 832 | >Menu Windows</A |
| 833 | ></DT |
| 834 | ><DT |
| 835 | >17.5. <A |
| 836 | HREF="#SCROLLMENUS" |
| 837 | >Scrolling Menus</A |
| 838 | ></DT |
| 839 | ><DT |
| 840 | >17.6. <A |
| 841 | HREF="#MULTICOLUMN" |
| 842 | >Multi Columnar Menus</A |
| 843 | ></DT |
| 844 | ><DT |
| 845 | >17.7. <A |
| 846 | HREF="#MULTIVALUEMENUS" |
| 847 | >Multi Valued Menus</A |
| 848 | ></DT |
| 849 | ><DT |
| 850 | >17.8. <A |
| 851 | HREF="#MENUOPT" |
| 852 | >Menu Options</A |
| 853 | ></DT |
| 854 | ><DT |
| 855 | >17.9. <A |
| 856 | HREF="#MENUUSERPTR" |
| 857 | >The useful User Pointer</A |
| 858 | ></DT |
| 859 | ></DL |
| 860 | ></DD |
| 861 | ><DT |
| 862 | >18. <A |
| 863 | HREF="#FORMS" |
| 864 | >Forms Library</A |
| 865 | ></DT |
| 866 | ><DD |
| 867 | ><DL |
| 868 | ><DT |
| 869 | >18.1. <A |
| 870 | HREF="#FORMBASICS" |
| 871 | >The Basics</A |
| 872 | ></DT |
| 873 | ><DT |
| 874 | >18.2. <A |
| 875 | HREF="#COMPILEFORMS" |
| 876 | >Compiling With the Forms Library</A |
| 877 | ></DT |
| 878 | ><DT |
| 879 | >18.3. <A |
| 880 | HREF="#PLAYFIELDS" |
| 881 | >Playing with Fields</A |
| 882 | ></DT |
| 883 | ><DD |
| 884 | ><DL |
| 885 | ><DT |
| 886 | >18.3.1. <A |
| 887 | HREF="#FETCHINFO" |
| 888 | >Fetching Size and Location of Field</A |
| 889 | ></DT |
| 890 | ><DT |
| 891 | >18.3.2. <A |
| 892 | HREF="#MOVEFIELD" |
| 893 | >Moving the field</A |
| 894 | ></DT |
| 895 | ><DT |
| 896 | >18.3.3. <A |
| 897 | HREF="#JUSTIFYFIELD" |
| 898 | >Field Justification</A |
| 899 | ></DT |
| 900 | ><DT |
| 901 | >18.3.4. <A |
| 902 | HREF="#FIELDDISPATTRIB" |
| 903 | >Field Display Attributes</A |
| 904 | ></DT |
| 905 | ><DT |
| 906 | >18.3.5. <A |
| 907 | HREF="#FIELDOPTIONBITS" |
| 908 | >Field Option Bits</A |
| 909 | ></DT |
| 910 | ><DT |
| 911 | >18.3.6. <A |
| 912 | HREF="#FIELDSTATUS" |
| 913 | >Field Status</A |
| 914 | ></DT |
| 915 | ><DT |
| 916 | >18.3.7. <A |
| 917 | HREF="#FIELDUSERPTR" |
| 918 | >Field User Pointer</A |
| 919 | ></DT |
| 920 | ><DT |
| 921 | >18.3.8. <A |
| 922 | HREF="#VARIABLESIZEFIELDS" |
| 923 | >Variable-Sized Fields</A |
| 924 | ></DT |
| 925 | ></DL |
| 926 | ></DD |
| 927 | ><DT |
| 928 | >18.4. <A |
| 929 | HREF="#FORMWINDOWS" |
| 930 | >Form Windows</A |
| 931 | ></DT |
| 932 | ><DT |
| 933 | >18.5. <A |
| 934 | HREF="#FILEDVALIDATE" |
| 935 | >Field Validation</A |
| 936 | ></DT |
| 937 | ><DT |
| 938 | >18.6. <A |
| 939 | HREF="#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 |
| 946 | HREF="#PAGENAVREQ" |
| 947 | >Page Navigation Requests</A |
| 948 | ></DT |
| 949 | ><DT |
| 950 | >18.6.2. <A |
| 951 | HREF="#INTERFIELDNAVREQ" |
| 952 | >Inter-Field Navigation Requests</A |
| 953 | ></DT |
| 954 | ><DT |
| 955 | >18.6.3. <A |
| 956 | HREF="#INTRAFIELDNAVREQ" |
| 957 | >Intra-Field Navigation Requests</A |
| 958 | ></DT |
| 959 | ><DT |
| 960 | >18.6.4. <A |
| 961 | HREF="#SCROLLREQ" |
| 962 | >Scrolling Requests</A |
| 963 | ></DT |
| 964 | ><DT |
| 965 | >18.6.5. <A |
| 966 | HREF="#EDITREQ" |
| 967 | >Editing Requests</A |
| 968 | ></DT |
| 969 | ><DT |
| 970 | >18.6.6. <A |
| 971 | HREF="#ORDERREQ" |
| 972 | >Order Requests</A |
| 973 | ></DT |
| 974 | ><DT |
| 975 | >18.6.7. <A |
| 976 | HREF="#APPLICCOMMANDS" |
| 977 | >Application Commands</A |
| 978 | ></DT |
| 979 | ></DL |
| 980 | ></DD |
| 981 | ></DL |
| 982 | ></DD |
| 983 | ><DT |
| 984 | >19. <A |
| 985 | HREF="#TOOLS" |
| 986 | >Tools and Widget Libraries</A |
| 987 | ></DT |
| 988 | ><DD |
| 989 | ><DL |
| 990 | ><DT |
| 991 | >19.1. <A |
| 992 | HREF="#CDK" |
| 993 | >CDK (Curses Development Kit)</A |
| 994 | ></DT |
| 995 | ><DD |
| 996 | ><DL |
| 997 | ><DT |
| 998 | >19.1.1. <A |
| 999 | HREF="#WIDGETLIST" |
| 1000 | >Widget List</A |
| 1001 | ></DT |
| 1002 | ><DT |
| 1003 | >19.1.2. <A |
| 1004 | HREF="#CDKATTRACT" |
| 1005 | >Some Attractive Features</A |
| 1006 | ></DT |
| 1007 | ><DT |
| 1008 | >19.1.3. <A |
| 1009 | HREF="#CDKCONCLUSION" |
| 1010 | >Conclusion</A |
| 1011 | ></DT |
| 1012 | ></DL |
| 1013 | ></DD |
| 1014 | ><DT |
| 1015 | >19.2. <A |
| 1016 | HREF="#DIALOG" |
| 1017 | >The dialog</A |
| 1018 | ></DT |
| 1019 | ><DT |
| 1020 | >19.3. <A |
| 1021 | HREF="#PERLCURSES" |
| 1022 | >Perl Curses Modules CURSES::FORM and CURSES::WIDGETS</A |
| 1023 | ></DT |
| 1024 | ></DL |
| 1025 | ></DD |
| 1026 | ><DT |
| 1027 | >20. <A |
| 1028 | HREF="#JUSTFORFUN" |
| 1029 | >Just For Fun !!!</A |
| 1030 | ></DT |
| 1031 | ><DD |
| 1032 | ><DL |
| 1033 | ><DT |
| 1034 | >20.1. <A |
| 1035 | HREF="#GAMEOFLIFE" |
| 1036 | >The Game of Life</A |
| 1037 | ></DT |
| 1038 | ><DT |
| 1039 | >20.2. <A |
| 1040 | HREF="#MAGIC" |
| 1041 | >Magic Square</A |
| 1042 | ></DT |
| 1043 | ><DT |
| 1044 | >20.3. <A |
| 1045 | HREF="#HANOI" |
| 1046 | >Towers of Hanoi</A |
| 1047 | ></DT |
| 1048 | ><DT |
| 1049 | >20.4. <A |
| 1050 | HREF="#QUEENS" |
| 1051 | >Queens Puzzle</A |
| 1052 | ></DT |
| 1053 | ><DT |
| 1054 | >20.5. <A |
| 1055 | HREF="#SHUFFLE" |
| 1056 | >Shuffle</A |
| 1057 | ></DT |
| 1058 | ><DT |
| 1059 | >20.6. <A |
| 1060 | HREF="#TT" |
| 1061 | >Typing Tutor</A |
| 1062 | ></DT |
| 1063 | ></DL |
| 1064 | ></DD |
| 1065 | ><DT |
| 1066 | >21. <A |
| 1067 | HREF="#REF" |
| 1068 | >References</A |
| 1069 | ></DT |
| 1070 | ></DL |
| 1071 | ></DIV |
| 1072 | ><DIV |
| 1073 | CLASS="SECT1" |
| 1074 | ><H2 |
| 1075 | CLASS="SECT1" |
| 1076 | ><A |
| 1077 | NAME="INTRO" |
| 1078 | >1. Introduction</A |
| 1079 | ></H2 |
| 1080 | ><P |
| 1081 | >In the olden days of teletype terminals, terminals were away from computers and |
| 1082 | were connected to them through serial cables. The terminals could be configured |
| 1083 | by sending a series of bytes. All the capabilities (such as |
| 1084 | moving the cursor to a new location, erasing part of the screen, scrolling the |
| 1085 | screen, changing modes etc.) of terminals could be accessed through these |
| 1086 | series of bytes. These control seeuqnces are usually called escape sequences, |
| 1087 | because they start |
| 1088 | with an escape(0x1B) character. Even today, with proper emulation, we can send |
| 1089 | escape sequences to the emulator and achieve the same effect on a terminal |
| 1090 | window.</P |
| 1091 | ><P |
| 1092 | >Suppose you wanted to print a line in color. Try typing this on your console.</P |
| 1093 | ><PRE |
| 1094 | CLASS="PROGRAMLISTING" |
| 1095 | >echo "^[[0;31;40mIn Color"</PRE |
| 1096 | ><P |
| 1097 | >The first character is an escape character, which looks like two characters ^ |
| 1098 | and [. To be able to print it, you have to press CTRL+V and then the ESC key. |
| 1099 | All the others are normal printable characters. You should be able to see the |
| 1100 | string "In Color" in red. It stays that way and to revert back to the original |
| 1101 | mode type this.</P |
| 1102 | ><PRE |
| 1103 | CLASS="PROGRAMLISTING" |
| 1104 | >echo "^[[0;37;40m"</PRE |
| 1105 | ><P |
| 1106 | >Now, what do these magic characters mean? Difficult to comprehend? They might |
| 1107 | even be different for different terminals. So the designers of UNIX invented a |
| 1108 | mechanism named <TT |
| 1109 | CLASS="LITERAL" |
| 1110 | >termcap</TT |
| 1111 | >. It is a file that |
| 1112 | lists all the capabilities of a particular terminal, along with the escape |
| 1113 | sequences needed to achieve a particular effect. In the later years, this was |
| 1114 | replaced by <TT |
| 1115 | CLASS="LITERAL" |
| 1116 | >terminfo</TT |
| 1117 | >. Without delving too |
| 1118 | much into details, this mechanism allows application |
| 1119 | programs to query the terminfo database and obtain the control characters to be |
| 1120 | sent to a terminal or terminal emulator.</P |
| 1121 | ><DIV |
| 1122 | CLASS="SECT2" |
| 1123 | ><HR><H3 |
| 1124 | CLASS="SECT2" |
| 1125 | ><A |
| 1126 | NAME="WHATIS" |
| 1127 | >1.1. What is NCURSES?</A |
| 1128 | ></H3 |
| 1129 | ><P |
| 1130 | > |
| 1131 | You might be wondering, what the import of all this technical gibberish is. In |
| 1132 | the above scenario, every application program is supposed to query the terminfo |
| 1133 | and perform the necessary stuff (sending control characters etc.). It soon became |
| 1134 | difficult to manage this complexity and this gave birth to 'CURSES'. Curses is |
| 1135 | a pun on the name "cursor optimization". The Curses library forms a wrapper |
| 1136 | over working with raw terminal codes, and provides highly flexible and |
| 1137 | efficient API (Application Programming Interface). It provides functions to |
| 1138 | move the cursor, create windows, produce colors, play with mouse etc. The |
| 1139 | application 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 |
| 1143 | older version of curses. In short, it is a library of functions that manages |
| 1144 | an application's display on character-cell terminals. In the remainder of the |
| 1145 | document, 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 |
| 1148 | distribution. The current package is maintained by |
| 1149 | <A |
| 1150 | HREF="mailto:dickey@his.com" |
| 1151 | TARGET="_top" |
| 1152 | >Thomas Dickey</A |
| 1153 | >. |
| 1154 | You can contact the maintainers at <A |
| 1155 | HREF="mailto:bug-ncurses@gnu.org" |
| 1156 | TARGET="_top" |
| 1157 | >bug-ncurses@gnu.org</A |
| 1158 | >.</P |
| 1159 | ></DIV |
| 1160 | ><DIV |
| 1161 | CLASS="SECT2" |
| 1162 | ><HR><H3 |
| 1163 | CLASS="SECT2" |
| 1164 | ><A |
| 1165 | NAME="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 |
| 1170 | robust framework to create nice looking UI (User Interface)s in text mode. It |
| 1171 | provides functions to create windows etc. Its sister libraries panel, menu and |
| 1172 | form provide an extension to the basic curses library. These libraries usually |
| 1173 | come along with curses. One can create applications that contain multiple |
| 1174 | windows, menus, panels and forms. Windows can be managed independently, can |
| 1175 | provide 'scrollability' and even can be hidden.</P |
| 1176 | ><P |
| 1177 | > |
| 1178 | Menus provide the user with an easy command selection option. Forms allow the |
| 1179 | creation of easy-to-use data entry and display windows. Panels extend the |
| 1180 | capabilities 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 |
| 1183 | along, We will see all the capabilities of these libraries. </P |
| 1184 | ></DIV |
| 1185 | ><DIV |
| 1186 | CLASS="SECT2" |
| 1187 | ><HR><H3 |
| 1188 | CLASS="SECT2" |
| 1189 | ><A |
| 1190 | NAME="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 |
| 1195 | to get started. NCURSES is usually shipped with your installation. In case |
| 1196 | you don't have the library or want to compile it on your own, read on.</P |
| 1197 | ><P |
| 1198 | ><SPAN |
| 1199 | CLASS="emphasis" |
| 1200 | ><I |
| 1201 | CLASS="EMPHASIS" |
| 1202 | >Compiling the package</I |
| 1203 | ></SPAN |
| 1204 | > </P |
| 1205 | ><P |
| 1206 | >NCURSES can be obtained from <A |
| 1207 | HREF="ftp://ftp.gnu.org/pub/gnu/ncurses/ncurses.tar.gz" |
| 1208 | TARGET="_top" |
| 1209 | >ftp://ftp.gnu.org/pub/gnu/ncurses/ncurses.tar.gz</A |
| 1210 | > or any of the ftp |
| 1211 | sites mentioned in <A |
| 1212 | HREF="http://www.gnu.org/order/ftp.html" |
| 1213 | TARGET="_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 |
| 1218 | usually involves the following operations.</P |
| 1219 | ><PRE |
| 1220 | CLASS="PROGRAMLISTING" |
| 1221 | > tar zxvf ncurses<version>.tar.gz # unzip and untar the archive |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 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 |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 1227 | make install # install it</PRE |
| 1228 | ><P |
| 1229 | ><SPAN |
| 1230 | CLASS="emphasis" |
| 1231 | ><I |
| 1232 | CLASS="EMPHASIS" |
| 1233 | >Using the RPM </I |
| 1234 | ></SPAN |
| 1235 | ></P |
| 1236 | ><P |
| 1237 | >NCURSES RPM can be found and downloaded from <A |
| 1238 | HREF="http://rpmfind.net" |
| 1239 | TARGET="_top" |
| 1240 | >http://rpmfind.net </A |
| 1241 | >. The RPM can be installed with the following |
| 1242 | command after becoming root.</P |
| 1243 | ><PRE |
| 1244 | CLASS="PROGRAMLISTING" |
| 1245 | > rpm -i <downloaded rpm></PRE |
| 1246 | ></DIV |
| 1247 | ><DIV |
| 1248 | CLASS="SECT2" |
| 1249 | ><HR><H3 |
| 1250 | CLASS="SECT2" |
| 1251 | ><A |
| 1252 | NAME="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 |
| 1257 | ncurses and its sister libraries. We graduate from a simple "Hello World" |
| 1258 | program to more complex form manipulation. No prior experience in ncurses is |
| 1259 | assumed. The writing is informal, but a lot of detail is provided for |
| 1260 | each of the examples.</P |
| 1261 | ></DIV |
| 1262 | ><DIV |
| 1263 | CLASS="SECT2" |
| 1264 | ><HR><H3 |
| 1265 | CLASS="SECT2" |
| 1266 | ><A |
| 1267 | NAME="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 |
| 1273 | HREF="http://www.tldp.org/HOWTO/NCURSES-Programming-HOWTO/ncurses_programs.tar.gz" |
| 1274 | TARGET="_top" |
| 1275 | >here</A |
| 1276 | >. Unzip and untar it. The directory structure looks like this.</P |
| 1277 | ><PRE |
| 1278 | CLASS="PROGRAMLISTING" |
| 1279 | >ncurses |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 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 |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 1293 | |----> COPYING -- copyright notice</PRE |
| 1294 | ><P |
| 1295 | >The individual directories contain the following files.</P |
| 1296 | ><PRE |
| 1297 | CLASS="PROGRAMLISTING" |
| 1298 | >Description of files in each directory |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 1299 | -------------------------------------- |
| 1300 | JustForFun |
| 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 |
| 1307 | |----> tt.c -- A very trivial typing tutor |
| 1308 | |
| 1309 | basics |
| 1310 | | |
| 1311 | |----> acs_vars.c -- ACS_ variables example |
| 1312 | |----> hello_world.c -- Simple "Hello World" Program |
| 1313 | |----> init_func_example.c -- Initialization functions example |
| 1314 | |----> key_code.c -- Shows the scan code of the key pressed |
| 1315 | |----> mouse_menu.c -- A menu accessible by mouse |
| 1316 | |----> other_border.c -- Shows usage of other border functions apa |
| 1317 | | -- rt from box() |
| 1318 | |----> printw_example.c -- A very simple printw() example |
| 1319 | |----> scanw_example.c -- A very simple getstr() example |
| 1320 | |----> simple_attr.c -- A program that can print a c file with |
| 1321 | | -- comments in attribute |
| 1322 | |----> simple_color.c -- A simple example demonstrating colors |
| 1323 | |----> simple_key.c -- A menu accessible with keyboard UP, DOWN |
| 1324 | | -- arrows |
| 1325 | |----> temp_leave.c -- Demonstrates temporarily leaving curses mode |
| 1326 | |----> win_border.c -- Shows Creation of windows and borders |
| 1327 | |----> with_chgat.c -- chgat() usage example |
| 1328 | |
| 1329 | forms |
| 1330 | | |
| 1331 | |----> form_attrib.c -- Usage of field attributes |
| 1332 | |----> form_options.c -- Usage of field options |
| 1333 | |----> form_simple.c -- A simple form example |
| 1334 | |----> form_win.c -- Demo of windows associated with forms |
| 1335 | |
| 1336 | menus |
| 1337 | | |
| 1338 | |----> menu_attrib.c -- Usage of menu attributes |
| 1339 | |----> menu_item_data.c -- Usage of item_name() etc.. functions |
| 1340 | |----> menu_multi_column.c -- Creates multi columnar menus |
| 1341 | |----> menu_scroll.c -- Demonstrates scrolling capability of menus |
| 1342 | |----> menu_simple.c -- A simple menu accessed by arrow keys |
| 1343 | |----> menu_toggle.c -- Creates multi valued menus and explains |
| 1344 | | -- REQ_TOGGLE_ITEM |
| 1345 | |----> menu_userptr.c -- Usage of user pointer |
| 1346 | |----> menu_win.c -- Demo of windows associated with menus |
| 1347 | |
| 1348 | panels |
| 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 |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 |
| 1360 | files and puts the ready-to-use exes in demo/exe directory. You can also |
| 1361 | do selective make by going into the corresponding directory. Each directory |
| 1362 | contains 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 |
| 1365 | examples directory. </P |
| 1366 | ><P |
| 1367 | > If you prefer browsing individual programs, point your browser to |
| 1368 | <A |
| 1369 | HREF="http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/ncurses_programs/" |
| 1370 | TARGET="_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 |
| 1376 | claiming them as yours. Feel free to use them in your programs as appropriate.</P |
| 1377 | ></DIV |
| 1378 | ><DIV |
| 1379 | CLASS="SECT2" |
| 1380 | ><HR><H3 |
| 1381 | CLASS="SECT2" |
| 1382 | ><A |
| 1383 | NAME="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. |
| 1388 | Here are the links to other formats of this document.</P |
| 1389 | ><DIV |
| 1390 | CLASS="SECT3" |
| 1391 | ><HR><H4 |
| 1392 | CLASS="SECT3" |
| 1393 | ><A |
| 1394 | NAME="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 |
| 1403 | HREF="http://www.ibiblio.org/pub/Linux/docs/HOWTO/other-formats/pdf/NCURSES-Programming-HOWTO.pdf" |
| 1404 | TARGET="_top" |
| 1405 | >Acrobat PDF Format</A |
| 1406 | ></P |
| 1407 | ></LI |
| 1408 | ><LI |
| 1409 | ><P |
| 1410 | ><A |
| 1411 | HREF="http://www.ibiblio.org/pub/Linux/docs/HOWTO/other-formats/ps/NCURSES-Programming-HOWTO.ps.gz" |
| 1412 | TARGET="_top" |
| 1413 | >PostScript Format</A |
| 1414 | ></P |
| 1415 | ></LI |
| 1416 | ><LI |
| 1417 | ><P |
| 1418 | ><A |
| 1419 | HREF="http://www.ibiblio.org/pub/Linux/docs/HOWTO/other-formats/html/NCURSES-Programming-HOWTO-html.tar.gz" |
| 1420 | TARGET="_top" |
| 1421 | >In Multiple HTML pages</A |
| 1422 | ></P |
| 1423 | ></LI |
| 1424 | ><LI |
| 1425 | ><P |
| 1426 | ><A |
| 1427 | HREF="http://www.ibiblio.org/pub/Linux/docs/HOWTO/other-formats/html_single/NCURSES-Programming-HOWTO.html" |
| 1428 | TARGET="_top" |
| 1429 | >In One big HTML format</A |
| 1430 | ></P |
| 1431 | ></LI |
| 1432 | ></UL |
| 1433 | ></DIV |
| 1434 | ><DIV |
| 1435 | CLASS="SECT3" |
| 1436 | ><HR><H4 |
| 1437 | CLASS="SECT3" |
| 1438 | ><A |
| 1439 | NAME="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 |
| 1445 | CLASS="PROGRAMLISTING" |
| 1446 | > Get both the source and the tar,gzipped programs, available at |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 1447 | http://cvsview.tldp.org/index.cgi/LDP/howto/docbook/ |
| 1448 | NCURSES-HOWTO/NCURSES-Programming-HOWTO.sgml |
| 1449 | http://cvsview.tldp.org/index.cgi/LDP/howto/docbook/ |
| 1450 | NCURSES-HOWTO/ncurses_programs.tar.gz |
| 1451 | |
| 1452 | Unzip ncurses_programs.tar.gz with |
| 1453 | tar zxvf ncurses_programs.tar.gz |
| 1454 | |
| 1455 | Use jade to create various formats. For example if you just want to create |
| 1456 | the multiple html files, you would use |
| 1457 | jade -t sgml -i html -d <path to docbook html stylesheet> |
| 1458 | NCURSES-Programming-HOWTO.sgml |
| 1459 | to get pdf, first create a single html file of the HOWTO with |
| 1460 | jade -t sgml -i html -d <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> |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 1467 | NCURSES-ONE-BIG-FILE.html</PRE |
| 1468 | ></P |
| 1469 | ><P |
| 1470 | >See <A |
| 1471 | HREF="http://www.tldp.org/LDP/LDP-Author-Guide/" |
| 1472 | TARGET="_top" |
| 1473 | >LDP Author guide</A |
| 1474 | > for more details. If all else failes, mail me at |
| 1475 | <A |
| 1476 | HREF="ppadala@gmail.com" |
| 1477 | TARGET="_top" |
| 1478 | >ppadala@gmail.com</A |
| 1479 | ></P |
| 1480 | ></DIV |
| 1481 | ></DIV |
| 1482 | ><DIV |
| 1483 | CLASS="SECT2" |
| 1484 | ><HR><H3 |
| 1485 | CLASS="SECT2" |
| 1486 | ><A |
| 1487 | NAME="CREDITS" |
| 1488 | >1.7. Credits</A |
| 1489 | ></H3 |
| 1490 | ><P |
| 1491 | >I thank <A |
| 1492 | HREF="mailto:sharath_1@usa.net" |
| 1493 | TARGET="_top" |
| 1494 | >Sharath</A |
| 1495 | > and Emre Akbas for |
| 1496 | helping me with few sections. The introduction was initially written by sharath. |
| 1497 | I rewrote it with few excerpts taken from his initial work. Emre helped in |
| 1498 | writing printw and scanw sections.</P |
| 1499 | ><P |
| 1500 | >Perl equivalents of the example programs are contributed by <A |
| 1501 | HREF="mailto:Aratnaweera@virtusa.com" |
| 1502 | TARGET="_top" |
| 1503 | >Anuradha Ratnaweera</A |
| 1504 | >. </P |
| 1505 | ><P |
| 1506 | >Then comes <A |
| 1507 | HREF="mailto:parimi@ece.arizona.edu" |
| 1508 | TARGET="_top" |
| 1509 | >Ravi Parimi</A |
| 1510 | >, my |
| 1511 | dearest friend, who has been on this project before even one line was written. |
| 1512 | He constantly bombarded me with suggestions and patiently reviewed the whole |
| 1513 | text. He also checked each program on Linux and Solaris. </P |
| 1514 | ></DIV |
| 1515 | ><DIV |
| 1516 | CLASS="SECT2" |
| 1517 | ><HR><H3 |
| 1518 | CLASS="SECT2" |
| 1519 | ><A |
| 1520 | NAME="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 |
| 1525 | to work on completing the wish, mail <A |
| 1526 | HREF="mailto:ppadala@gmail.com" |
| 1527 | TARGET="_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 |
| 1540 | description of each program. Let the user compile and see the program in action. |
| 1541 | A 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 |
| 1550 | package.</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 |
| 1563 | CLASS="SECT2" |
| 1564 | ><HR><H3 |
| 1565 | CLASS="SECT2" |
| 1566 | ><A |
| 1567 | NAME="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 |
| 1574 | of this software and associated documentation files (the "Software"), to deal |
| 1575 | in the Software without restriction, including without limitation the rights |
| 1576 | to use, copy, modify, merge, publish, distribute, distribute with |
| 1577 | modifications, sublicense, and/or sell copies of the Software, and to permit |
| 1578 | persons to whom the Software is furnished to do so, subject to the following |
| 1579 | conditions:</P |
| 1580 | ><P |
| 1581 | >The above copyright notice and this permission notice shall be included in all |
| 1582 | copies or substantial portions of the Software.</P |
| 1583 | ><P |
| 1584 | >THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 1585 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 1586 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 1587 | ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 1588 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR |
| 1589 | IN 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 |
| 1592 | shall not be used in advertising or otherwise to promote the sale, use or |
| 1593 | other dealings in this Software without prior written authorization. </P |
| 1594 | ></DIV |
| 1595 | ></DIV |
| 1596 | ><DIV |
| 1597 | CLASS="SECT1" |
| 1598 | ><HR><H2 |
| 1599 | CLASS="SECT1" |
| 1600 | ><A |
| 1601 | NAME="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 |
| 1606 | its various features, let's write a simple program and say |
| 1607 | hello to the world. </P |
| 1608 | ><DIV |
| 1609 | CLASS="SECT2" |
| 1610 | ><HR><H3 |
| 1611 | CLASS="SECT2" |
| 1612 | ><A |
| 1613 | NAME="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 |
| 1618 | programs. To link the |
| 1619 | program with ncurses the flag -lncurses should be added.</P |
| 1620 | ><PRE |
| 1621 | CLASS="PROGRAMLISTING" |
| 1622 | > #include <ncurses.h> |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 1623 | . |
| 1624 | . |
| 1625 | . |
| 1626 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 1627 | compile and link: gcc <program file> -lncurses</PRE |
| 1628 | ><DIV |
| 1629 | CLASS="EXAMPLE" |
| 1630 | ><A |
| 1631 | NAME="BHW" |
| 1632 | ></A |
| 1633 | ><P |
| 1634 | ><B |
| 1635 | >Example 1. The Hello World !!! Program </B |
| 1636 | ></P |
| 1637 | ><PRE |
| 1638 | CLASS="PROGRAMLISTING" |
| 1639 | ><SPAN |
| 1640 | CLASS="INLINEMEDIAOBJECT" |
| 1641 | >#include <ncurses.h> |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 1642 | |
| 1643 | int main() |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 1650 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 1651 | return 0; |
| 1652 | }</SPAN |
| 1653 | ></PRE |
| 1654 | ></DIV |
| 1655 | ></DIV |
| 1656 | ><DIV |
| 1657 | CLASS="SECT2" |
| 1658 | ><HR><H3 |
| 1659 | CLASS="SECT2" |
| 1660 | ><A |
| 1661 | NAME="DISSECTION" |
| 1662 | >2.2. Dissection</A |
| 1663 | ></H3 |
| 1664 | ><P |
| 1665 | > |
| 1666 | The above program prints "Hello World !!!" to the screen and exits. This |
| 1667 | program shows how to initialize curses and do screen manipulation and |
| 1668 | end curses mode. Let's dissect it line by line. </P |
| 1669 | ><DIV |
| 1670 | CLASS="SECT3" |
| 1671 | ><HR><H4 |
| 1672 | CLASS="SECT3" |
| 1673 | ><A |
| 1674 | NAME="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 |
| 1679 | implementations, it clears the screen and presents a blank screen. To do any |
| 1680 | screen manipulation using curses package this has to be called first. This |
| 1681 | function initializes the curses system and allocates memory for our present |
| 1682 | window (called <TT |
| 1683 | CLASS="LITERAL" |
| 1684 | >stdscr</TT |
| 1685 | >) and some other data-structures. Under extreme |
| 1686 | cases this function might fail due to insufficient memory to allocate memory |
| 1687 | for curses library's data structures. </P |
| 1688 | ><P |
| 1689 | > |
| 1690 | After this is done, we can do a variety of initializations to customize |
| 1691 | our curses settings. These details will be explained <A |
| 1692 | HREF="#INIT" |
| 1693 | >later </A |
| 1694 | >.</P |
| 1695 | ></DIV |
| 1696 | ><DIV |
| 1697 | CLASS="SECT3" |
| 1698 | ><HR><H4 |
| 1699 | CLASS="SECT3" |
| 1700 | ><A |
| 1701 | NAME="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 |
| 1706 | function is analogous to normal printf in all respects except that it prints |
| 1707 | the data on a window called stdscr at the current (y,x) co-ordinates. Since our |
| 1708 | present co-ordinates are at 0,0 the string is printed at the left hand corner |
| 1709 | of the window.</P |
| 1710 | ><P |
| 1711 | >This brings us to that mysterious refresh(). Well, when we called printw |
| 1712 | the data is actually written to an imaginary window, which is not updated |
| 1713 | on the screen yet. The job of printw is to update a few flags |
| 1714 | and data structures and write the data to a buffer corresponding to stdscr. |
| 1715 | In order to show it on the screen, we need to call refresh() and tell the |
| 1716 | curses 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 |
| 1719 | on the imaginary screen or windows and do a refresh once all his screen update |
| 1720 | is done. refresh() checks the window and updates only the portion which has been |
| 1721 | changed. This improves performance and offers greater flexibility too. But, it is |
| 1722 | sometimes frustrating to beginners. A common mistake committed by beginners is |
| 1723 | to forget to call refresh() after they did some update through printw() class of |
| 1724 | functions. I still forget to add it sometimes :-) </P |
| 1725 | ></DIV |
| 1726 | ><DIV |
| 1727 | CLASS="SECT3" |
| 1728 | ><HR><H4 |
| 1729 | CLASS="SECT3" |
| 1730 | ><A |
| 1731 | NAME="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 |
| 1736 | behave strangely after the program quits. endwin() frees the memory taken by |
| 1737 | curses sub-system and its data structures and puts the terminal in normal |
| 1738 | mode. This function must be called after you are done with the curses mode. </P |
| 1739 | ></DIV |
| 1740 | ></DIV |
| 1741 | ></DIV |
| 1742 | ><DIV |
| 1743 | CLASS="SECT1" |
| 1744 | ><HR><H2 |
| 1745 | CLASS="SECT1" |
| 1746 | ><A |
| 1747 | NAME="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 |
| 1752 | details. There are many functions that help customize what you see on screen and |
| 1753 | many features which can be put to full use. </P |
| 1754 | ><P |
| 1755 | >Here we go...</P |
| 1756 | ></DIV |
| 1757 | ><DIV |
| 1758 | CLASS="SECT1" |
| 1759 | ><HR><H2 |
| 1760 | CLASS="SECT1" |
| 1761 | ><A |
| 1762 | NAME="INIT" |
| 1763 | >4. Initialization</A |
| 1764 | ></H2 |
| 1765 | ><P |
| 1766 | >We now know that to initialize curses system the function initscr() has to be |
| 1767 | called. There are functions which can be called after this initialization to |
| 1768 | customize our curses session. We may ask the curses system to set the terminal |
| 1769 | in raw mode or initialize color or initialize the mouse etc.. Let's discuss some |
| 1770 | of the functions that are normally called immediately after initscr();</P |
| 1771 | ><DIV |
| 1772 | CLASS="SECT2" |
| 1773 | ><HR><H3 |
| 1774 | CLASS="SECT2" |
| 1775 | ><A |
| 1776 | NAME="ABOUTINIT" |
| 1777 | >4.1. Initialization functions</A |
| 1778 | ></H3 |
| 1779 | ><P |
| 1780 | > </P |
| 1781 | ></DIV |
| 1782 | ><DIV |
| 1783 | CLASS="SECT2" |
| 1784 | ><HR><H3 |
| 1785 | CLASS="SECT2" |
| 1786 | ><A |
| 1787 | NAME="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 |
| 1792 | line or carriage return is encountered. But most programs require that the |
| 1793 | characters be available as soon as the user types them. The above two functions |
| 1794 | are used to disable line buffering. The difference between these two functions |
| 1795 | is 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 |
| 1797 | directly passed to the program without generating a signal. In the |
| 1798 | <TT |
| 1799 | CLASS="LITERAL" |
| 1800 | >cbreak()</TT |
| 1801 | > mode these control characters are |
| 1802 | interpreted as any other character by the terminal driver. I personally prefer |
| 1803 | to use raw() as I can exercise greater control over what the user does.</P |
| 1804 | ></DIV |
| 1805 | ><DIV |
| 1806 | CLASS="SECT2" |
| 1807 | ><HR><H3 |
| 1808 | CLASS="SECT2" |
| 1809 | ><A |
| 1810 | NAME="ECHONOECHO" |
| 1811 | >4.3. echo() and noecho()</A |
| 1812 | ></H3 |
| 1813 | ><P |
| 1814 | > |
| 1815 | These functions control the echoing of characters typed by the user to the |
| 1816 | terminal. <TT |
| 1817 | CLASS="LITERAL" |
| 1818 | >noecho()</TT |
| 1819 | > switches off echoing. The |
| 1820 | reason you might want to do this is to gain more control over echoing or to |
| 1821 | suppress unnecessary echoing while taking input from the user through the |
| 1822 | getch() etc. functions. Most of the interactive programs call |
| 1823 | <TT |
| 1824 | CLASS="LITERAL" |
| 1825 | >noecho()</TT |
| 1826 | > at initialization and do the echoing |
| 1827 | of characters in a controlled manner. It gives the programmer the flexibility |
| 1828 | of echoing characters at any place in the window without updating current (y,x) |
| 1829 | co-ordinates. </P |
| 1830 | ></DIV |
| 1831 | ><DIV |
| 1832 | CLASS="SECT2" |
| 1833 | ><HR><H3 |
| 1834 | CLASS="SECT2" |
| 1835 | ><A |
| 1836 | NAME="KEYPAD" |
| 1837 | >4.4. keypad()</A |
| 1838 | ></H3 |
| 1839 | ><P |
| 1840 | >This is my favorite initialization function. It enables the reading of function |
| 1841 | keys like F1, F2, arrow keys etc. Almost every interactive program enables this, |
| 1842 | as arrow keys are a major part of any User Interface. Do |
| 1843 | <TT |
| 1844 | CLASS="LITERAL" |
| 1845 | >keypad(stdscr, TRUE) </TT |
| 1846 | > to enable this feature |
| 1847 | for the regular screen (stdscr). You will learn more about key management in |
| 1848 | later sections of this document.</P |
| 1849 | ></DIV |
| 1850 | ><DIV |
| 1851 | CLASS="SECT2" |
| 1852 | ><HR><H3 |
| 1853 | CLASS="SECT2" |
| 1854 | ><A |
| 1855 | NAME="HALFDELAY" |
| 1856 | >4.5. halfdelay()</A |
| 1857 | ></H3 |
| 1858 | ><P |
| 1859 | >This function, though not used very often, is a useful one at times. |
| 1860 | halfdelay()is called to enable the half-delay mode, which is similar to the |
| 1861 | cbreak() mode in that characters typed are immediately available to program. |
| 1862 | However, it waits for 'X' tenths of a second for input and then returns ERR, if |
| 1863 | no input is available. 'X' is the timeout value passed to the function |
| 1864 | halfdelay(). This function is useful when you want to ask the user for input, |
| 1865 | and if he doesn't respond with in certain time, we can do some thing else. One |
| 1866 | possible example is a timeout at the password prompt. </P |
| 1867 | ></DIV |
| 1868 | ><DIV |
| 1869 | CLASS="SECT2" |
| 1870 | ><HR><H3 |
| 1871 | CLASS="SECT2" |
| 1872 | ><A |
| 1873 | NAME="MISCINIT" |
| 1874 | >4.6. Miscellaneous Initialization functions</A |
| 1875 | ></H3 |
| 1876 | ><P |
| 1877 | >There are few more functions which are called at initialization to |
| 1878 | customize curses behavior. They are not used as extensively as those mentioned |
| 1879 | above. Some of them are explained where appropriate.</P |
| 1880 | ></DIV |
| 1881 | ><DIV |
| 1882 | CLASS="SECT2" |
| 1883 | ><HR><H3 |
| 1884 | CLASS="SECT2" |
| 1885 | ><A |
| 1886 | NAME="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 |
| 1892 | CLASS="EXAMPLE" |
| 1893 | ><A |
| 1894 | NAME="BINFU" |
| 1895 | ></A |
| 1896 | ><P |
| 1897 | ><B |
| 1898 | >Example 2. Initialization Function Usage example </B |
| 1899 | ></P |
| 1900 | ><PRE |
| 1901 | CLASS="PROGRAMLISTING" |
| 1902 | ><SPAN |
| 1903 | CLASS="INLINEMEDIAOBJECT" |
| 1904 | >#include <ncurses.h> |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 1905 | |
| 1906 | int main() |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 1907 | { int ch; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 1908 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 1913 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 1932 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 |
| 1939 | yet. The function <TT |
| 1940 | CLASS="LITERAL" |
| 1941 | >getch()</TT |
| 1942 | > is used to get a |
| 1943 | character from user. It is equivalent to normal |
| 1944 | <TT |
| 1945 | CLASS="LITERAL" |
| 1946 | >getchar()</TT |
| 1947 | > except that we can disable the line |
| 1948 | buffering to avoid <enter> after input. Look for more about |
| 1949 | <TT |
| 1950 | CLASS="LITERAL" |
| 1951 | >getch()</TT |
| 1952 | >and reading keys in the <A |
| 1953 | HREF="#KEYS" |
| 1954 | > key management section </A |
| 1955 | >. The functions attron and attroff |
| 1956 | are used to switch some attributes on and off respectively. In the example I |
| 1957 | used them to print the character in bold. These functions are explained in detail |
| 1958 | later.</P |
| 1959 | ></DIV |
| 1960 | ></DIV |
| 1961 | ><DIV |
| 1962 | CLASS="SECT1" |
| 1963 | ><HR><H2 |
| 1964 | CLASS="SECT1" |
| 1965 | ><A |
| 1966 | NAME="AWORDWINDOWS" |
| 1967 | >5. A Word about Windows</A |
| 1968 | ></H2 |
| 1969 | ><P |
| 1970 | > |
| 1971 | Before we plunge into the myriad ncurses functions, let me clear few things |
| 1972 | about windows. Windows are explained in detail in following <A |
| 1973 | HREF="#WINDOWS" |
| 1974 | > sections </A |
| 1975 | ></P |
| 1976 | ><P |
| 1977 | >A Window is an imaginary screen defined by curses system. A window does not mean |
| 1978 | a bordered window which you usually see on Win9X platforms. When curses is |
| 1979 | initialized, it creates a default window named |
| 1980 | <TT |
| 1981 | CLASS="LITERAL" |
| 1982 | >stdscr</TT |
| 1983 | > which represents your 80x25 (or the size |
| 1984 | of window in which you are running) screen. If you are doing simple tasks like |
| 1985 | printing few strings, reading input etc., you can safely use this single window |
| 1986 | for all of your purposes. You can also create windows and call functions which |
| 1987 | explicitly work on the specified window.</P |
| 1988 | ><P |
| 1989 | >For example, if you call</P |
| 1990 | ><PRE |
| 1991 | CLASS="PROGRAMLISTING" |
| 1992 | > printw("Hi There !!!"); |
| 1993 | refresh();</PRE |
| 1994 | ><P |
| 1995 | >It prints the string on stdscr at the present cursor position. Similarly the |
| 1996 | call to refresh(), works on stdscr only. </P |
| 1997 | ><P |
| 1998 | >Say you have created <A |
| 1999 | HREF="#WINDOWS" |
| 2000 | >windows</A |
| 2001 | > then you have to |
| 2002 | call a function with a 'w' added to the usual function.</P |
| 2003 | ><PRE |
| 2004 | CLASS="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 |
| 2009 | same convention. For each function there usually are three more functions.</P |
| 2010 | ><PRE |
| 2011 | CLASS="PROGRAMLISTING" |
| 2012 | > printw(string); /* Print on stdscr at present cursor position */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 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 */ |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 2017 | /* co-ordinates and then print */</PRE |
| 2018 | ><P |
| 2019 | >Usually the w-less functions are macros which expand to corresponding w-function |
| 2020 | with stdscr as the window parameter.</P |
| 2021 | ></DIV |
| 2022 | ><DIV |
| 2023 | CLASS="SECT1" |
| 2024 | ><HR><H2 |
| 2025 | CLASS="SECT1" |
| 2026 | ><A |
| 2027 | NAME="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 |
| 2032 | curses functions. Now that curses is initialized, let's interact with |
| 2033 | world.</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 |
| 2039 | TYPE="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 |
| 2056 | which class is used. Let's see each one in detail.</P |
| 2057 | ><DIV |
| 2058 | CLASS="SECT2" |
| 2059 | ><HR><H3 |
| 2060 | CLASS="SECT2" |
| 2061 | ><A |
| 2062 | NAME="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 |
| 2067 | advance the position of the cursor. You can give the character to be printed but |
| 2068 | they usually are used to print a character with some attributes. Attributes are |
| 2069 | explained in detail in later <A |
| 2070 | HREF="#ATTRIB" |
| 2071 | > sections </A |
| 2072 | > of the |
| 2073 | document. If a character is associated with an attribute(bold, reverse video |
| 2074 | etc.), 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 |
| 2083 | macros could be found in the header file |
| 2084 | <TT |
| 2085 | CLASS="LITERAL" |
| 2086 | >ncurses.h</TT |
| 2087 | >. For example, you want to print a |
| 2088 | character ch(of type char) bold and underlined, you would call addch() as below. |
| 2089 | <PRE |
| 2090 | CLASS="PROGRAMLISTING" |
| 2091 | > addch(ch | A_BOLD | A_UNDERLINE);</PRE |
| 2092 | ></P |
| 2093 | ></LI |
| 2094 | ><LI |
| 2095 | ><P |
| 2096 | >By using functions like <TT |
| 2097 | CLASS="LITERAL" |
| 2098 | >attrset(),attron(),attroff()</TT |
| 2099 | >. These functions are explained in the <A |
| 2100 | HREF="#ATTRIB" |
| 2101 | >Attributes</A |
| 2102 | > section. Briefly, they manipulate the current attributes of |
| 2103 | the given window. Once set, the character printed in the window are associated |
| 2104 | with the attributes until it is turned off.</P |
| 2105 | ></LI |
| 2106 | ></UL |
| 2107 | ><P |
| 2108 | >Additionally, <TT |
| 2109 | CLASS="LITERAL" |
| 2110 | >curses</TT |
| 2111 | > provides some special |
| 2112 | characters for character-based graphics. You can draw tables, horizontal or |
| 2113 | vertical lines, etc. You can find all avaliable characters in the header file |
| 2114 | <TT |
| 2115 | CLASS="LITERAL" |
| 2116 | >ncurses.h</TT |
| 2117 | >. Try looking for macros beginning |
| 2118 | with <TT |
| 2119 | CLASS="LITERAL" |
| 2120 | >ACS_</TT |
| 2121 | > in this file. </P |
| 2122 | ></DIV |
| 2123 | ><DIV |
| 2124 | CLASS="SECT2" |
| 2125 | ><HR><H3 |
| 2126 | CLASS="SECT2" |
| 2127 | ><A |
| 2128 | NAME="AEN298" |
| 2129 | >6.2. mvaddch(), waddch() and mvwaddch()</A |
| 2130 | ></H3 |
| 2131 | ><P |
| 2132 | ><TT |
| 2133 | CLASS="LITERAL" |
| 2134 | >mvaddch()</TT |
| 2135 | > is used to move the cursor to a |
| 2136 | given point, and then print. Thus, the calls: |
| 2137 | <PRE |
| 2138 | CLASS="PROGRAMLISTING" |
| 2139 | > move(row,col); /* moves the cursor to row<SPAN |
| 2140 | CLASS="emphasis" |
| 2141 | ><I |
| 2142 | CLASS="EMPHASIS" |
| 2143 | >th</I |
| 2144 | ></SPAN |
| 2145 | > row and col<SPAN |
| 2146 | CLASS="emphasis" |
| 2147 | ><I |
| 2148 | CLASS="EMPHASIS" |
| 2149 | >th</I |
| 2150 | ></SPAN |
| 2151 | > column */ |
| 2152 | addch(ch);</PRE |
| 2153 | > |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 2154 | can be replaced by |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 2155 | <PRE |
| 2156 | CLASS="PROGRAMLISTING" |
| 2157 | > mvaddch(row,col,ch);</PRE |
| 2158 | ></P |
| 2159 | ><P |
| 2160 | ><TT |
| 2161 | CLASS="LITERAL" |
| 2162 | >waddch()</TT |
| 2163 | > is similar to |
| 2164 | <TT |
| 2165 | CLASS="LITERAL" |
| 2166 | >addch()</TT |
| 2167 | >, except that it adds a character into |
| 2168 | the given window. (Note that <TT |
| 2169 | CLASS="LITERAL" |
| 2170 | >addch()</TT |
| 2171 | > adds a |
| 2172 | character into the window <TT |
| 2173 | CLASS="LITERAL" |
| 2174 | >stdscr</TT |
| 2175 | >.)</P |
| 2176 | ><P |
| 2177 | >In a similar fashion <TT |
| 2178 | CLASS="LITERAL" |
| 2179 | >mvwaddch()</TT |
| 2180 | > function is |
| 2181 | used 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 |
| 2185 | CLASS="LITERAL" |
| 2186 | >addch()</TT |
| 2187 | >. But, if we want to print a string, it |
| 2188 | would be very annoying to print it character by character. Fortunately, |
| 2189 | <TT |
| 2190 | CLASS="LITERAL" |
| 2191 | >ncurses</TT |
| 2192 | > provides <TT |
| 2193 | CLASS="LITERAL" |
| 2194 | >printf</TT |
| 2195 | ><SPAN |
| 2196 | CLASS="emphasis" |
| 2197 | ><I |
| 2198 | CLASS="EMPHASIS" |
| 2199 | >-like</I |
| 2200 | ></SPAN |
| 2201 | > or |
| 2202 | <TT |
| 2203 | CLASS="LITERAL" |
| 2204 | >puts</TT |
| 2205 | ><SPAN |
| 2206 | CLASS="emphasis" |
| 2207 | ><I |
| 2208 | CLASS="EMPHASIS" |
| 2209 | >-like</I |
| 2210 | ></SPAN |
| 2211 | > functions.</P |
| 2212 | ></DIV |
| 2213 | ><DIV |
| 2214 | CLASS="SECT2" |
| 2215 | ><HR><H3 |
| 2216 | CLASS="SECT2" |
| 2217 | ><A |
| 2218 | NAME="PRINTWCLASS" |
| 2219 | >6.3. printw() class of functions</A |
| 2220 | ></H3 |
| 2221 | ><P |
| 2222 | >These functions are similar to <TT |
| 2223 | CLASS="LITERAL" |
| 2224 | >printf()</TT |
| 2225 | > with |
| 2226 | the added capability of printing at any position on the screen. </P |
| 2227 | ><DIV |
| 2228 | CLASS="SECT3" |
| 2229 | ><HR><H4 |
| 2230 | CLASS="SECT3" |
| 2231 | ><A |
| 2232 | NAME="PRINTWMVPRINTW" |
| 2233 | >6.3.1. printw() and mvprintw</A |
| 2234 | ></H4 |
| 2235 | ><P |
| 2236 | >These two functions work much like <TT |
| 2237 | CLASS="LITERAL" |
| 2238 | >printf()</TT |
| 2239 | >. |
| 2240 | <TT |
| 2241 | CLASS="LITERAL" |
| 2242 | >mvprintw()</TT |
| 2243 | > can be used to move the cursor to a |
| 2244 | position and then print. If you want to move the cursor first and then print |
| 2245 | using <TT |
| 2246 | CLASS="LITERAL" |
| 2247 | >printw()</TT |
| 2248 | > function, use |
| 2249 | <TT |
| 2250 | CLASS="LITERAL" |
| 2251 | >move() </TT |
| 2252 | > first and then use |
| 2253 | <TT |
| 2254 | CLASS="LITERAL" |
| 2255 | >printw()</TT |
| 2256 | > though I see no point why one should |
| 2257 | avoid using <TT |
| 2258 | CLASS="LITERAL" |
| 2259 | >mvprintw()</TT |
| 2260 | >, you have the |
| 2261 | flexibility to manipulate. </P |
| 2262 | ></DIV |
| 2263 | ><DIV |
| 2264 | CLASS="SECT3" |
| 2265 | ><HR><H4 |
| 2266 | CLASS="SECT3" |
| 2267 | ><A |
| 2268 | NAME="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 |
| 2273 | corresponding window given as argument. </P |
| 2274 | ></DIV |
| 2275 | ><DIV |
| 2276 | CLASS="SECT3" |
| 2277 | ><HR><H4 |
| 2278 | CLASS="SECT3" |
| 2279 | ><A |
| 2280 | NAME="VWPRINTW" |
| 2281 | >6.3.3. vwprintw()</A |
| 2282 | ></H4 |
| 2283 | ><P |
| 2284 | >This function is similar to <TT |
| 2285 | CLASS="LITERAL" |
| 2286 | >vprintf()</TT |
| 2287 | >. This can |
| 2288 | be used when variable number of arguments are to be printed.</P |
| 2289 | ></DIV |
| 2290 | ><DIV |
| 2291 | CLASS="SECT3" |
| 2292 | ><HR><H4 |
| 2293 | CLASS="SECT3" |
| 2294 | ><A |
| 2295 | NAME="SIMPLEPRINTWEX" |
| 2296 | >6.3.4. A Simple printw example</A |
| 2297 | ></H4 |
| 2298 | ><DIV |
| 2299 | CLASS="EXAMPLE" |
| 2300 | ><A |
| 2301 | NAME="BPREX" |
| 2302 | ></A |
| 2303 | ><P |
| 2304 | ><B |
| 2305 | >Example 3. A Simple printw example </B |
| 2306 | ></P |
| 2307 | ><PRE |
| 2308 | CLASS="PROGRAMLISTING" |
| 2309 | ><SPAN |
| 2310 | CLASS="INLINEMEDIAOBJECT" |
| 2311 | >#include <ncurses.h> /* ncurses.h includes stdio.h */ |
| 2312 | #include <string.h> |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 2313 | |
| 2314 | int main() |
| 2315 | { |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 2321 | mvprintw(row/2,(col-strlen(mesg))/2,"%s",mesg); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 2322 | /* print the message at the center of the screen */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 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; |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 2330 | }</SPAN |
| 2331 | ></PRE |
| 2332 | ></DIV |
| 2333 | ><P |
| 2334 | >Above program demonstrates how easy it is to use <TT |
| 2335 | CLASS="LITERAL" |
| 2336 | >printw</TT |
| 2337 | >. You just feed the coordinates and the message to be appeared |
| 2338 | on the screen, then it does what you want.</P |
| 2339 | ><P |
| 2340 | >The above program introduces us to a new function |
| 2341 | <TT |
| 2342 | CLASS="LITERAL" |
| 2343 | >getmaxyx()</TT |
| 2344 | >, a macro defined in |
| 2345 | <TT |
| 2346 | CLASS="LITERAL" |
| 2347 | >ncurses.h</TT |
| 2348 | >. It gives the number of columns and |
| 2349 | the number of rows in a given window. |
| 2350 | <TT |
| 2351 | CLASS="LITERAL" |
| 2352 | >getmaxyx()</TT |
| 2353 | > does this by updating the variables |
| 2354 | given to it. Since <TT |
| 2355 | CLASS="LITERAL" |
| 2356 | >getmaxyx()</TT |
| 2357 | > is not a function |
| 2358 | we don't pass pointers to it, we just give two integer variables. </P |
| 2359 | ></DIV |
| 2360 | ></DIV |
| 2361 | ><DIV |
| 2362 | CLASS="SECT2" |
| 2363 | ><HR><H3 |
| 2364 | CLASS="SECT2" |
| 2365 | ><A |
| 2366 | NAME="ADDSTRCLASS" |
| 2367 | >6.4. addstr() class of functions</A |
| 2368 | ></H3 |
| 2369 | ><P |
| 2370 | ><TT |
| 2371 | CLASS="LITERAL" |
| 2372 | >addstr()</TT |
| 2373 | > is used to put a character string into |
| 2374 | a given window. This function is similar to calling |
| 2375 | <TT |
| 2376 | CLASS="LITERAL" |
| 2377 | >addch()</TT |
| 2378 | > once for each character in a given |
| 2379 | string. This is true for all output functions. There are other functions from |
| 2380 | this family such as <TT |
| 2381 | CLASS="LITERAL" |
| 2382 | >mvaddstr(),mvwaddstr()</TT |
| 2383 | > and |
| 2384 | <TT |
| 2385 | CLASS="LITERAL" |
| 2386 | >waddstr()</TT |
| 2387 | >, which obey the naming convention of |
| 2388 | curses.(e.g. mvaddstr() is similar to the respective calls move() and then |
| 2389 | addstr().) Another function of this family is addnstr(), which takes an integer |
| 2390 | parameter(say n) additionally. This function puts at most n characters into the |
| 2391 | screen. If n is negative, then the entire string will be added. </P |
| 2392 | ></DIV |
| 2393 | ><DIV |
| 2394 | CLASS="SECT2" |
| 2395 | ><HR><H3 |
| 2396 | CLASS="SECT2" |
| 2397 | ><A |
| 2398 | NAME="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. |
| 2403 | A common mistake by beginners is to pass x,y in that order. If you are |
| 2404 | doing too many manipulations of (y,x) co-ordinates, think of dividing the |
| 2405 | screen into windows and manipulate each one separately. Windows are explained |
| 2406 | in the <A |
| 2407 | HREF="#WINDOWS" |
| 2408 | > windows </A |
| 2409 | > section.</P |
| 2410 | ></DIV |
| 2411 | ></DIV |
| 2412 | ><DIV |
| 2413 | CLASS="SECT1" |
| 2414 | ><HR><H2 |
| 2415 | CLASS="SECT1" |
| 2416 | ><A |
| 2417 | NAME="SCANW" |
| 2418 | >7. Input functions</A |
| 2419 | ></H2 |
| 2420 | ><P |
| 2421 | >Well, printing without taking input, is boring. Let's see functions which |
| 2422 | allow us to get input from user. These functions also can be divided into |
| 2423 | three categories.</P |
| 2424 | ><P |
| 2425 | ></P |
| 2426 | ><OL |
| 2427 | TYPE="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 |
| 2442 | CLASS="SECT2" |
| 2443 | ><HR><H3 |
| 2444 | CLASS="SECT2" |
| 2445 | ><A |
| 2446 | NAME="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 |
| 2451 | subtle facts to consider. For example if you don't use the function cbreak(), |
| 2452 | curses will not read your input characters contiguously but will begin read them |
| 2453 | only after a new line or an EOF is encountered. In order to avoid this, the |
| 2454 | cbreak() function must used so that characters are immediately available to your |
| 2455 | program. Another widely used function is noecho(). As the name suggests, when |
| 2456 | this function is set (used), the characters that are keyed in by the user will |
| 2457 | not show up on the screen. The two functions cbreak() and noecho() are typical |
| 2458 | examples of key management. Functions of this genre are explained in the |
| 2459 | <A |
| 2460 | HREF="#KEYS" |
| 2461 | >key management section </A |
| 2462 | >.</P |
| 2463 | ></DIV |
| 2464 | ><DIV |
| 2465 | CLASS="SECT2" |
| 2466 | ><HR><H3 |
| 2467 | CLASS="SECT2" |
| 2468 | ><A |
| 2469 | NAME="SCANWCLASS" |
| 2470 | >7.2. scanw() class of functions</A |
| 2471 | ></H3 |
| 2472 | ><P |
| 2473 | >These functions are similar to <TT |
| 2474 | CLASS="LITERAL" |
| 2475 | >scanf()</TT |
| 2476 | > with the |
| 2477 | added capability of getting the input from any location on the screen.</P |
| 2478 | ><DIV |
| 2479 | CLASS="SECT3" |
| 2480 | ><HR><H4 |
| 2481 | CLASS="SECT3" |
| 2482 | ><A |
| 2483 | NAME="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 |
| 2489 | CLASS="LITERAL" |
| 2490 | >sscanf()</TT |
| 2491 | >, where the line to be scanned is |
| 2492 | provided by <TT |
| 2493 | CLASS="LITERAL" |
| 2494 | >wgetstr()</TT |
| 2495 | > function. That is, these |
| 2496 | functions call to <TT |
| 2497 | CLASS="LITERAL" |
| 2498 | >wgetstr()</TT |
| 2499 | > function(explained |
| 2500 | below) and uses the resulting line for a scan. </P |
| 2501 | ></DIV |
| 2502 | ><DIV |
| 2503 | CLASS="SECT3" |
| 2504 | ><HR><H4 |
| 2505 | CLASS="SECT3" |
| 2506 | ><A |
| 2507 | NAME="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, |
| 2512 | which is supplied as one of the arguments to these functions. </P |
| 2513 | ></DIV |
| 2514 | ><DIV |
| 2515 | CLASS="SECT3" |
| 2516 | ><HR><H4 |
| 2517 | CLASS="SECT3" |
| 2518 | ><A |
| 2519 | NAME="VWSCANW" |
| 2520 | >7.2.3. vwscanw()</A |
| 2521 | ></H4 |
| 2522 | ><P |
| 2523 | >This function is similar to <TT |
| 2524 | CLASS="LITERAL" |
| 2525 | >vscanf()</TT |
| 2526 | >. This can |
| 2527 | be used when a variable number of arguments are to be scanned.</P |
| 2528 | ></DIV |
| 2529 | ></DIV |
| 2530 | ><DIV |
| 2531 | CLASS="SECT2" |
| 2532 | ><HR><H3 |
| 2533 | CLASS="SECT2" |
| 2534 | ><A |
| 2535 | NAME="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 |
| 2540 | function performs the same task as would be achieved by a series of calls to |
| 2541 | <TT |
| 2542 | CLASS="LITERAL" |
| 2543 | >getch()</TT |
| 2544 | > until a newline, carriage return, or |
| 2545 | end-of-file is received. The resulting string of characters are pointed to by |
| 2546 | <TT |
| 2547 | CLASS="LITERAL" |
| 2548 | >str</TT |
| 2549 | >, which is a character pointer provided by |
| 2550 | the user.</P |
| 2551 | ></DIV |
| 2552 | ><DIV |
| 2553 | CLASS="SECT2" |
| 2554 | ><HR><H3 |
| 2555 | CLASS="SECT2" |
| 2556 | ><A |
| 2557 | NAME="GETSTREX" |
| 2558 | >7.4. Some examples</A |
| 2559 | ></H3 |
| 2560 | ><DIV |
| 2561 | CLASS="EXAMPLE" |
| 2562 | ><A |
| 2563 | NAME="BSCEX" |
| 2564 | ></A |
| 2565 | ><P |
| 2566 | ><B |
| 2567 | >Example 4. A Simple scanw example </B |
| 2568 | ></P |
| 2569 | ><PRE |
| 2570 | CLASS="PROGRAMLISTING" |
| 2571 | ><SPAN |
| 2572 | CLASS="INLINEMEDIAOBJECT" |
| 2573 | >#include <ncurses.h> /* ncurses.h includes stdio.h */ |
| 2574 | #include <string.h> |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 2575 | |
| 2576 | int main() |
| 2577 | { |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 2578 | char mesg[]="Enter a string: "; /* message to be appeared on the screen */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 2579 | char str[80]; |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 2584 | mvprintw(row/2,(col-strlen(mesg))/2,"%s",mesg); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 2585 | /* print the message at the center of the screen */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 2586 | getstr(str); |
| 2587 | mvprintw(LINES - 2, 0, "You Entered: %s", str); |
| 2588 | getch(); |
| 2589 | endwin(); |
| 2590 | |
| 2591 | return 0; |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 2592 | }</SPAN |
| 2593 | ></PRE |
| 2594 | ></DIV |
| 2595 | ></DIV |
| 2596 | ></DIV |
| 2597 | ><DIV |
| 2598 | CLASS="SECT1" |
| 2599 | ><HR><H2 |
| 2600 | CLASS="SECT1" |
| 2601 | ><A |
| 2602 | NAME="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 |
| 2607 | some special effects. Attributes, when set prudently, can present information in |
| 2608 | an easy, understandable manner. The following program takes a C file as input |
| 2609 | and prints the file with comments in bold. Scan through the code. </P |
| 2610 | ><DIV |
| 2611 | CLASS="EXAMPLE" |
| 2612 | ><A |
| 2613 | NAME="BSIAT" |
| 2614 | ></A |
| 2615 | ><P |
| 2616 | ><B |
| 2617 | >Example 5. A Simple Attributes example </B |
| 2618 | ></P |
| 2619 | ><PRE |
| 2620 | CLASS="PROGRAMLISTING" |
| 2621 | ><SPAN |
| 2622 | CLASS="INLINEMEDIAOBJECT" |
| 2623 | >/* pager functionality by Joseph Spainhour" <spainhou@bellsouth.net> */ |
| 2624 | #include <ncurses.h> |
| 2625 | #include <stdlib.h> |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 2626 | |
| 2627 | int 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 | { |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 2636 | printf("Usage: %s <a c file name>\n", argv[0]); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 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 | } |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 2648 | { |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 2649 | getyx(stdscr, y, x); /* get the current curser position */ |
| 2650 | if(y == (row - 1)) /* are we are at the end of the screen */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 2651 | { |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 2652 | printw("<-Press Any Key->"); /* tell the user to press a key */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 2653 | getch(); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 2654 | clear(); /* clear the screen */ |
| 2655 | move(0, 0); /* start at the beginning of the screen */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 2656 | } |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 2657 | if(prev == '/' && ch == '*') /* If it is / and * then only |
| 2658 | * switch bold on */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 2659 | { |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 2664 | } |
| 2665 | else |
| 2666 | printw("%c", ch); |
| 2667 | refresh(); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 2668 | if(prev == '*' && ch == '/') |
| 2669 | attroff(A_BOLD); /* Switch it off once we got * |
| 2670 | * and then / */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 2671 | prev = ch; |
| 2672 | } |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 2673 | endwin(); /* End curses mode */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 2674 | fclose(fp); |
| 2675 | return 0; |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 2676 | }</SPAN |
| 2677 | ></PRE |
| 2678 | ></DIV |
| 2679 | ><P |
| 2680 | > |
| 2681 | Don't worry about all those initialization and other crap. Concentrate on |
| 2682 | the while loop. It reads each character in the file and searches for the |
| 2683 | pattern /*. Once it spots the pattern, it switches the BOLD attribute on with |
| 2684 | <TT |
| 2685 | CLASS="LITERAL" |
| 2686 | > attron()</TT |
| 2687 | > . When we get the pattern */ it is |
| 2688 | switched off by <TT |
| 2689 | CLASS="LITERAL" |
| 2690 | > attroff()</TT |
| 2691 | > .</P |
| 2692 | ><P |
| 2693 | > |
| 2694 | The above program also introduces us to two useful functions |
| 2695 | <TT |
| 2696 | CLASS="LITERAL" |
| 2697 | >getyx() </TT |
| 2698 | > and |
| 2699 | <TT |
| 2700 | CLASS="LITERAL" |
| 2701 | >move()</TT |
| 2702 | >. The first function gets the |
| 2703 | co-ordinates of the present cursor into the variables y, x. Since getyx() is a |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 2704 | macro we don't have to pass pointers to variables. The function |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 2705 | <TT |
| 2706 | CLASS="LITERAL" |
| 2707 | >move()</TT |
| 2708 | > moves the cursor to the co-ordinates |
| 2709 | given to it. </P |
| 2710 | ><P |
| 2711 | > |
| 2712 | The above program is really a simple one which doesn't do much. On these lines |
| 2713 | one could write a more useful program which reads a C file, parses it and prints |
| 2714 | it in different colors. One could even extend it to other languages as well.</P |
| 2715 | ><DIV |
| 2716 | CLASS="SECT2" |
| 2717 | ><HR><H3 |
| 2718 | CLASS="SECT2" |
| 2719 | ><A |
| 2720 | NAME="ATTRIBDETAILS" |
| 2721 | >8.1. The details</A |
| 2722 | ></H3 |
| 2723 | ><P |
| 2724 | >Let's get into more details of attributes. The functions <TT |
| 2725 | CLASS="LITERAL" |
| 2726 | >attron(), attroff(), attrset() </TT |
| 2727 | >, and their sister functions |
| 2728 | <TT |
| 2729 | CLASS="LITERAL" |
| 2730 | > attr_get()</TT |
| 2731 | > etc.. can be used to switch |
| 2732 | attributes 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 |
| 2735 | on or off, respectively. The following video attributes, which are defined in |
| 2736 | <curses.h> can be passed to these functions. </P |
| 2737 | ><PRE |
| 2738 | CLASS="PROGRAMLISTING" |
| 2739 | > |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 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 |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 2752 | </PRE |
| 2753 | ><P |
| 2754 | > |
| 2755 | The last one is the most colorful one :-) Colors are explained in the |
| 2756 | <A |
| 2757 | HREF="#color" |
| 2758 | TARGET="_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 |
| 2763 | wanted reverse video with blinking characters you can use</P |
| 2764 | ><PRE |
| 2765 | CLASS="PROGRAMLISTING" |
| 2766 | > attron(A_REVERSE | A_BLINK);</PRE |
| 2767 | ></DIV |
| 2768 | ><DIV |
| 2769 | CLASS="SECT2" |
| 2770 | ><HR><H3 |
| 2771 | CLASS="SECT2" |
| 2772 | ><A |
| 2773 | NAME="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 |
| 2778 | attributes of window whereas attron just switches on the attribute given to it. |
| 2779 | So attrset() fully overrides whatever attributes the window previously had and |
| 2780 | sets it to the new attribute(s). Similarly attroff() just switches off the |
| 2781 | attribute(s) given to it as an argument. This gives us the flexibility of |
| 2782 | managing attributes easily.But if you use them carelessly you may loose track of |
| 2783 | what attributes the window has and garble the display. This is especially true |
| 2784 | while managing menus with colors and highlighting. So decide on a consistent |
| 2785 | policy and stick to it. You can always use <TT |
| 2786 | CLASS="LITERAL" |
| 2787 | > standend()</TT |
| 2788 | > which is equivalent to <TT |
| 2789 | CLASS="LITERAL" |
| 2790 | > attrset(A_NORMAL)</TT |
| 2791 | > which turns off all attributes and brings you to normal mode.</P |
| 2792 | ></DIV |
| 2793 | ><DIV |
| 2794 | CLASS="SECT2" |
| 2795 | ><HR><H3 |
| 2796 | CLASS="SECT2" |
| 2797 | ><A |
| 2798 | NAME="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 |
| 2803 | window. Though we might not use this as often as the above functions, this is |
| 2804 | useful in scanning areas of screen. Say we wanted to do some complex update on |
| 2805 | screen and we are not sure what attribute each character is associated with. |
| 2806 | Then this function can be used with either attrset or attron to produce the |
| 2807 | desired effect. </P |
| 2808 | ></DIV |
| 2809 | ><DIV |
| 2810 | CLASS="SECT2" |
| 2811 | ><HR><H3 |
| 2812 | CLASS="SECT2" |
| 2813 | ><A |
| 2814 | NAME="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 |
| 2819 | to above functions except that they take parameters of type |
| 2820 | <TT |
| 2821 | CLASS="LITERAL" |
| 2822 | >attr_t</TT |
| 2823 | >.</P |
| 2824 | ></DIV |
| 2825 | ><DIV |
| 2826 | CLASS="SECT2" |
| 2827 | ><HR><H3 |
| 2828 | CLASS="SECT2" |
| 2829 | ><A |
| 2830 | NAME="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 |
| 2835 | operates on a particular window. The above functions operate on stdscr. </P |
| 2836 | ></DIV |
| 2837 | ><DIV |
| 2838 | CLASS="SECT2" |
| 2839 | ><HR><H3 |
| 2840 | CLASS="SECT2" |
| 2841 | ><A |
| 2842 | NAME="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 |
| 2847 | is a useful one. This function can be used to set attributes for a group of |
| 2848 | characters without moving. I mean it !!! without moving the cursor :-) It |
| 2849 | changes the attributes of a given number of characters starting at the current |
| 2850 | cursor location.</P |
| 2851 | ><P |
| 2852 | >We can give -1 as the character count to update till end of line. If you want to |
| 2853 | change attributes of characters from current position to end of line, just use |
| 2854 | this.</P |
| 2855 | ><PRE |
| 2856 | CLASS="PROGRAMLISTING" |
| 2857 | > chgat(-1, A_REVERSE, 0, NULL);</PRE |
| 2858 | ><P |
| 2859 | > |
| 2860 | This function is useful when changing attributes for characters that are |
| 2861 | already on the screen. Move to the character from which you want to change and |
| 2862 | change the attribute. </P |
| 2863 | ><P |
| 2864 | >Other functions wchgat(), mvchgat(), wchgat() behave similarly except that the w |
| 2865 | functions operate on the particular window. The mv functions first move the |
| 2866 | cursor then perform the work given to them. Actually chgat is a macro which is |
| 2867 | replaced by a wchgat() with stdscr as the window. Most of the "w-less" functions |
| 2868 | are macros.</P |
| 2869 | ><DIV |
| 2870 | CLASS="EXAMPLE" |
| 2871 | ><A |
| 2872 | NAME="BWICH" |
| 2873 | ></A |
| 2874 | ><P |
| 2875 | ><B |
| 2876 | >Example 6. Chgat() Usage example </B |
| 2877 | ></P |
| 2878 | ><PRE |
| 2879 | CLASS="PROGRAMLISTING" |
| 2880 | ><SPAN |
| 2881 | CLASS="INLINEMEDIAOBJECT" |
| 2882 | >#include <ncurses.h> |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 2883 | |
| 2884 | int main(int argc, char *argv[]) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 |
| 2910 | explained in detail later. Use 0 for no color.</P |
| 2911 | ></DIV |
| 2912 | ></DIV |
| 2913 | ><DIV |
| 2914 | CLASS="SECT1" |
| 2915 | ><HR><H2 |
| 2916 | CLASS="SECT1" |
| 2917 | ><A |
| 2918 | NAME="WINDOWS" |
| 2919 | >9. Windows</A |
| 2920 | ></H2 |
| 2921 | ><P |
| 2922 | >Windows form the most important concept in curses. You have seen the standard |
| 2923 | window stdscr above where all the functions implicitly operated on this window. |
| 2924 | Now to make design even a simplest GUI, you need to resort to windows. The main |
| 2925 | reason you may want to use windows is to manipulate parts of the screen |
| 2926 | separately, for better efficiency, by updating only the windows that need to be |
| 2927 | changed and for a better design. I would say the last reason is the most |
| 2928 | important in going for windows. You should always strive for a better and |
| 2929 | easy-to-manage design in your programs. If you are writing big, complex GUIs |
| 2930 | this is of pivotal importance before you start doing anything.</P |
| 2931 | ><DIV |
| 2932 | CLASS="SECT2" |
| 2933 | ><HR><H3 |
| 2934 | CLASS="SECT2" |
| 2935 | ><A |
| 2936 | NAME="WINDOWBASICS" |
| 2937 | >9.1. The basics</A |
| 2938 | ></H3 |
| 2939 | ><P |
| 2940 | >A Window can be created by calling the function |
| 2941 | <TT |
| 2942 | CLASS="LITERAL" |
| 2943 | >newwin()</TT |
| 2944 | >. It doesn't create any thing on the |
| 2945 | screen actually. It allocates memory for a structure to manipulate the window |
| 2946 | and updates the structure with data regarding the window like it's size, beginy, |
| 2947 | beginx etc.. Hence in curses, a window is just an abstraction of an imaginary |
| 2948 | window, which can be manipulated independent of other parts of screen. The |
| 2949 | function newwin() returns a pointer to structure WINDOW, which can be passed to |
| 2950 | window related functions like wprintw() etc.. Finally the window can be |
| 2951 | destroyed with delwin(). It will deallocate the memory associated with the |
| 2952 | window structure.</P |
| 2953 | ></DIV |
| 2954 | ><DIV |
| 2955 | CLASS="SECT2" |
| 2956 | ><HR><H3 |
| 2957 | CLASS="SECT2" |
| 2958 | ><A |
| 2959 | NAME="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 |
| 2964 | begins by displaying the window. The function |
| 2965 | <TT |
| 2966 | CLASS="LITERAL" |
| 2967 | >box()</TT |
| 2968 | > can be used to draw a border around the |
| 2969 | window. Let's explore these functions in more detail in this example.</P |
| 2970 | ><DIV |
| 2971 | CLASS="EXAMPLE" |
| 2972 | ><A |
| 2973 | NAME="BWIBO" |
| 2974 | ></A |
| 2975 | ><P |
| 2976 | ><B |
| 2977 | >Example 7. Window Border example </B |
| 2978 | ></P |
| 2979 | ><PRE |
| 2980 | CLASS="PROGRAMLISTING" |
| 2981 | ><SPAN |
| 2982 | CLASS="INLINEMEDIAOBJECT" |
| 2983 | >#include <ncurses.h> |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 2984 | |
| 2985 | |
| 2986 | WINDOW *create_newwin(int height, int width, int starty, int startx); |
| 2987 | void destroy_win(WINDOW *local_win); |
| 2988 | |
| 2989 | int main(int argc, char *argv[]) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 2990 | { WINDOW *my_win; |
| 2991 | int startx, starty, width, height; |
| 2992 | int ch; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 2993 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 2998 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 3006 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 3030 | } |
| 3031 | |
| 3032 | WINDOW *create_newwin(int height, int width, int starty, int startx) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 3033 | { WINDOW *local_win; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 3034 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 3040 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 3041 | return local_win; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 3042 | } |
| 3043 | |
| 3044 | void destroy_win(WINDOW *local_win) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 |
| 3069 | CLASS="SECT2" |
| 3070 | ><HR><H3 |
| 3071 | CLASS="SECT2" |
| 3072 | ><A |
| 3073 | NAME="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 |
| 3078 | things here :-). This program creates a rectangular window that can be moved |
| 3079 | with left, right, up, down arrow keys. It repeatedly creates and destroys |
| 3080 | windows as user press a key. Don't go beyond the screen limits. Checking for |
| 3081 | those limits is left as an exercise for the reader. Let's dissect it by line by line.</P |
| 3082 | ><P |
| 3083 | >The <TT |
| 3084 | CLASS="LITERAL" |
| 3085 | >create_newwin()</TT |
| 3086 | > function creates a window |
| 3087 | with <TT |
| 3088 | CLASS="LITERAL" |
| 3089 | >newwin() </TT |
| 3090 | > and displays a border around it |
| 3091 | with box. The function <TT |
| 3092 | CLASS="LITERAL" |
| 3093 | > destroy_win()</TT |
| 3094 | > first |
| 3095 | erases the window from screen by painting a border with ' ' character and then |
| 3096 | calling <TT |
| 3097 | CLASS="LITERAL" |
| 3098 | >delwin()</TT |
| 3099 | > to deallocate memory related |
| 3100 | to it. Depending on the key the user presses, starty or startx is changed and a |
| 3101 | new window is created.</P |
| 3102 | ><P |
| 3103 | >In the destroy_win, as you can see, I used wborder instead of box. The reason is |
| 3104 | written in the comments (You missed it. I know. Read the code :-)). wborder |
| 3105 | draws a border around the window with the characters given to it as the 4 corner |
| 3106 | points and the 4 lines. To put it clearly, if you have called wborder as below: |
| 3107 | <PRE |
| 3108 | CLASS="PROGRAMLISTING" |
| 3109 | > wborder(win, '|', '|', '-', '-', '+', '+', '+', '+');</PRE |
| 3110 | ></P |
| 3111 | ><P |
| 3112 | >it produces some thing like </P |
| 3113 | ><PRE |
| 3114 | CLASS="PROGRAMLISTING" |
| 3115 | > +------------+ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 3116 | | | |
| 3117 | | | |
| 3118 | | | |
| 3119 | | | |
| 3120 | | | |
| 3121 | | | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 3122 | +------------+</PRE |
| 3123 | ></DIV |
| 3124 | ><DIV |
| 3125 | CLASS="SECT2" |
| 3126 | ><HR><H3 |
| 3127 | CLASS="SECT2" |
| 3128 | ><A |
| 3129 | NAME="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, |
| 3134 | LINES which are initialized to the screen sizes after initscr(). They can be |
| 3135 | useful in finding screen dimensions and finding the center co-ordinate of the |
| 3136 | screen as above. The function <TT |
| 3137 | CLASS="LITERAL" |
| 3138 | >getch()</TT |
| 3139 | > as usual |
| 3140 | gets the key from keyboard and according to the key it does the corresponding |
| 3141 | work. This type of switch- case is very common in any GUI based programs.</P |
| 3142 | ></DIV |
| 3143 | ><DIV |
| 3144 | CLASS="SECT2" |
| 3145 | ><HR><H3 |
| 3146 | CLASS="SECT2" |
| 3147 | ><A |
| 3148 | NAME="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 |
| 3153 | is destroyed and another is created. So let's write a more efficient program |
| 3154 | which uses other border related functions.</P |
| 3155 | ><P |
| 3156 | >The following program uses <TT |
| 3157 | CLASS="LITERAL" |
| 3158 | >mvhline()</TT |
| 3159 | > and |
| 3160 | <TT |
| 3161 | CLASS="LITERAL" |
| 3162 | >mvvline()</TT |
| 3163 | > to achieve similar effect. These two |
| 3164 | functions are simple. They create a horizontal or vertical line of the specified |
| 3165 | length at the specified position.</P |
| 3166 | ><DIV |
| 3167 | CLASS="EXAMPLE" |
| 3168 | ><A |
| 3169 | NAME="BOTBO" |
| 3170 | ></A |
| 3171 | ><P |
| 3172 | ><B |
| 3173 | >Example 8. More border functions</B |
| 3174 | ></P |
| 3175 | ><PRE |
| 3176 | CLASS="PROGRAMLISTING" |
| 3177 | ><SPAN |
| 3178 | CLASS="INLINEMEDIAOBJECT" |
| 3179 | >#include <ncurses.h> |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 3180 | |
| 3181 | typedef struct _win_border_struct { |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 3182 | chtype ls, rs, ts, bs, |
| 3183 | tl, tr, bl, br; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 3184 | }WIN_BORDER; |
| 3185 | |
| 3186 | typedef struct _WIN_struct { |
| 3187 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 3188 | int startx, starty; |
| 3189 | int height, width; |
| 3190 | WIN_BORDER border; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 3191 | }WIN; |
| 3192 | |
| 3193 | void init_win_params(WIN *p_win); |
| 3194 | void print_win_params(WIN *p_win); |
| 3195 | void create_box(WIN *win, bool flag); |
| 3196 | |
| 3197 | int main(int argc, char *argv[]) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 3198 | { WIN win; |
| 3199 | int ch; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 3200 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 3208 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 3209 | /* Initialize the window parameters */ |
| 3210 | init_win_params(&win); |
| 3211 | print_win_params(&win); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 3212 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 3245 | } |
| 3246 | void init_win_params(WIN *p_win) |
| 3247 | { |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 3252 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 = '+'; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 3261 | |
| 3262 | } |
| 3263 | void print_win_params(WIN *p_win) |
| 3264 | { |
| 3265 | #ifdef _DEBUG |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 3266 | mvprintw(25, 0, "%d %d %d %d", p_win->startx, p_win->starty, |
| 3267 | p_win->width, p_win->height); |
| 3268 | refresh(); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 3269 | #endif |
| 3270 | } |
| 3271 | void create_box(WIN *p_win, bool flag) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 3272 | { int i, j; |
| 3273 | int x, y, w, h; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 3274 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 3275 | x = p_win->startx; |
| 3276 | y = p_win->starty; |
| 3277 | w = p_win->width; |
| 3278 | h = p_win->height; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 3279 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 3289 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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(); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 3297 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 3298 | }</SPAN |
| 3299 | ></PRE |
| 3300 | ></DIV |
| 3301 | ></DIV |
| 3302 | ></DIV |
| 3303 | ><DIV |
| 3304 | CLASS="SECT1" |
| 3305 | ><HR><H2 |
| 3306 | CLASS="SECT1" |
| 3307 | ><A |
| 3308 | NAME="COLOR" |
| 3309 | >10. Colors</A |
| 3310 | ></H2 |
| 3311 | ><DIV |
| 3312 | CLASS="SECT2" |
| 3313 | ><H3 |
| 3314 | CLASS="SECT2" |
| 3315 | ><A |
| 3316 | NAME="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. |
| 3321 | Let's get into the thick of the things with a small program.</P |
| 3322 | ><DIV |
| 3323 | CLASS="EXAMPLE" |
| 3324 | ><A |
| 3325 | NAME="BSICO" |
| 3326 | ></A |
| 3327 | ><P |
| 3328 | ><B |
| 3329 | >Example 9. A Simple Color example </B |
| 3330 | ></P |
| 3331 | ><PRE |
| 3332 | CLASS="PROGRAMLISTING" |
| 3333 | ><SPAN |
| 3334 | CLASS="INLINEMEDIAOBJECT" |
| 3335 | >#include <ncurses.h> |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 3336 | |
| 3337 | void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string); |
| 3338 | int main(int argc, char *argv[]) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 3347 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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(); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 3353 | } |
| 3354 | void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 3355 | { int length, x, y; |
| 3356 | float temp; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 3357 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 3367 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 3368 | length = strlen(string); |
| 3369 | temp = (width - length)/ 2; |
| 3370 | x = startx + (int)temp; |
| 3371 | mvwprintw(win, y, x, "%s", string); |
| 3372 | refresh(); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 3373 | } |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 |
| 3380 | CLASS="LITERAL" |
| 3381 | > start_color()</TT |
| 3382 | >. After that, you can use color |
| 3383 | capabilities of your terminals using various functions. To find out whether a |
| 3384 | terminal has color capabilities or not, you can use |
| 3385 | <TT |
| 3386 | CLASS="LITERAL" |
| 3387 | >has_colors()</TT |
| 3388 | > function, which returns FALSE if |
| 3389 | the terminal does not support color. </P |
| 3390 | ><P |
| 3391 | >Curses initializes all the colors supported by terminal when start_color() is |
| 3392 | called. These can be accessed by the define constants like |
| 3393 | <TT |
| 3394 | CLASS="LITERAL" |
| 3395 | >COLOR_BLACK </TT |
| 3396 | > etc. Now to actually start using |
| 3397 | colors, you have to define pairs. Colors are always used in pairs. That means |
| 3398 | you have to use the function <TT |
| 3399 | CLASS="LITERAL" |
| 3400 | >init_pair() </TT |
| 3401 | > to |
| 3402 | define the foreground and background for the pair number you give. After that |
| 3403 | that pair number can be used as a normal attribute with <TT |
| 3404 | CLASS="LITERAL" |
| 3405 | >COLOR_PAIR()</TT |
| 3406 | >function. This may seem to be cumbersome at first. |
| 3407 | But this elegant solution allows us to manage color pairs very easily. To |
| 3408 | appreciate it, you have to look into the the source code of "dialog", a utility |
| 3409 | for displaying dialog boxes from shell scripts. The developers have defined |
| 3410 | foreground and background combinations for all the colors they might need and |
| 3411 | initialized at the beginning. This makes it very easy to set attributes just by |
| 3412 | accessing a pair which we already have defined as a constant.</P |
| 3413 | ><P |
| 3414 | >The following colors are defined in <TT |
| 3415 | CLASS="LITERAL" |
| 3416 | >curses.h</TT |
| 3417 | >. |
| 3418 | You can use these as parameters for various color functions. |
| 3419 | <PRE |
| 3420 | CLASS="PROGRAMLISTING" |
| 3421 | > COLOR_BLACK 0 |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 3422 | COLOR_RED 1 |
| 3423 | COLOR_GREEN 2 |
| 3424 | COLOR_YELLOW 3 |
| 3425 | COLOR_BLUE 4 |
| 3426 | COLOR_MAGENTA 5 |
| 3427 | COLOR_CYAN 6 |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 3428 | COLOR_WHITE 7</PRE |
| 3429 | ></P |
| 3430 | ></DIV |
| 3431 | ><DIV |
| 3432 | CLASS="SECT2" |
| 3433 | ><HR><H3 |
| 3434 | CLASS="SECT2" |
| 3435 | ><A |
| 3436 | NAME="CHANGECOLORDEFS" |
| 3437 | >10.2. Changing Color Definitions</A |
| 3438 | ></H3 |
| 3439 | ><P |
| 3440 | >The function <TT |
| 3441 | CLASS="LITERAL" |
| 3442 | >init_color()</TT |
| 3443 | >can be used to change |
| 3444 | the rgb values for the colors defined by curses initially. Say you wanted to |
| 3445 | lighten the intensity of red color by a minuscule. Then you can use this |
| 3446 | function as</P |
| 3447 | ><PRE |
| 3448 | CLASS="PROGRAMLISTING" |
| 3449 | > init_color(COLOR_RED, 700, 0, 0); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 3450 | /* param 1 : color name |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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. |
| 3454 | The function <TT |
| 3455 | CLASS="LITERAL" |
| 3456 | >can_change_color()</TT |
| 3457 | > can be used to |
| 3458 | find out whether the terminal has the capability of changing color content or |
| 3459 | not. The rgb content is scaled from 0 to 1000. Initially RED color is defined |
| 3460 | with content 1000(r), 0(g), 0(b). </P |
| 3461 | ></DIV |
| 3462 | ><DIV |
| 3463 | CLASS="SECT2" |
| 3464 | ><HR><H3 |
| 3465 | CLASS="SECT2" |
| 3466 | ><A |
| 3467 | NAME="COLORCONTENT" |
| 3468 | >10.3. Color Content</A |
| 3469 | ></H3 |
| 3470 | ><P |
| 3471 | >The functions <TT |
| 3472 | CLASS="LITERAL" |
| 3473 | >color_content()</TT |
| 3474 | > and |
| 3475 | <TT |
| 3476 | CLASS="LITERAL" |
| 3477 | >pair_content()</TT |
| 3478 | > can be used to find the color |
| 3479 | content and foreground, background combination for the pair. </P |
| 3480 | ></DIV |
| 3481 | ></DIV |
| 3482 | ><DIV |
| 3483 | CLASS="SECT1" |
| 3484 | ><HR><H2 |
| 3485 | CLASS="SECT1" |
| 3486 | ><A |
| 3487 | NAME="KEYS" |
| 3488 | >11. Interfacing with the key board</A |
| 3489 | ></H2 |
| 3490 | ><DIV |
| 3491 | CLASS="SECT2" |
| 3492 | ><H3 |
| 3493 | CLASS="SECT2" |
| 3494 | ><A |
| 3495 | NAME="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 |
| 3500 | user, a curses program should be sensitive to key presses or the mouse actions |
| 3501 | done 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 |
| 3504 | input from the user. A simple way of getting key presses is to use |
| 3505 | <TT |
| 3506 | CLASS="LITERAL" |
| 3507 | >getch()</TT |
| 3508 | > function. The cbreak mode should be |
| 3509 | enabled to read keys when you are interested in reading individual key hits |
| 3510 | rather than complete lines of text (which usually end with a carriage return). |
| 3511 | keypad should be enabled to get the Functions keys, arrow keys etc. See the |
| 3512 | initialization section for details.</P |
| 3513 | ><P |
| 3514 | ><TT |
| 3515 | CLASS="LITERAL" |
| 3516 | >getch()</TT |
| 3517 | > returns an integer corresponding to the |
| 3518 | key pressed. If it is a normal character, the integer value will be equivalent |
| 3519 | to the character. Otherwise it returns a number which can be matched with the |
| 3520 | constants defined in <TT |
| 3521 | CLASS="LITERAL" |
| 3522 | >curses.h</TT |
| 3523 | >. For example if |
| 3524 | the user presses F1, the integer returned is 265. This can be checked using the |
| 3525 | macro KEY_F() defined in curses.h. This makes reading keys portable and easy to |
| 3526 | manage.</P |
| 3527 | ><P |
| 3528 | >For example, if you call getch() like this</P |
| 3529 | ><PRE |
| 3530 | CLASS="PROGRAMLISTING" |
| 3531 | > int ch; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 3532 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 3533 | ch = getch();</PRE |
| 3534 | ><P |
| 3535 | >getch() will wait for the user to press a key, (unless you specified a timeout) |
| 3536 | and when user presses a key, the corresponding integer is returned. Then you can |
| 3537 | check the value returned with the constants defined in curses.h to match against |
| 3538 | the keys you want.</P |
| 3539 | ><P |
| 3540 | >The following code piece will do that job.</P |
| 3541 | ><PRE |
| 3542 | CLASS="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 |
| 3547 | and down arrows.</P |
| 3548 | ></DIV |
| 3549 | ><DIV |
| 3550 | CLASS="SECT2" |
| 3551 | ><HR><H3 |
| 3552 | CLASS="SECT2" |
| 3553 | ><A |
| 3554 | NAME="SIMPLEKEYEX" |
| 3555 | >11.2. A Simple Key Usage example</A |
| 3556 | ></H3 |
| 3557 | ><DIV |
| 3558 | CLASS="EXAMPLE" |
| 3559 | ><A |
| 3560 | NAME="BSIKE" |
| 3561 | ></A |
| 3562 | ><P |
| 3563 | ><B |
| 3564 | >Example 10. A Simple Key Usage example </B |
| 3565 | ></P |
| 3566 | ><PRE |
| 3567 | CLASS="PROGRAMLISTING" |
| 3568 | ><SPAN |
| 3569 | CLASS="INLINEMEDIAOBJECT" |
| 3570 | >#include <stdio.h> |
| 3571 | #include <ncurses.h> |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 3572 | |
| 3573 | #define WIDTH 30 |
| 3574 | #define HEIGHT 10 |
| 3575 | |
| 3576 | int startx = 0; |
| 3577 | int starty = 0; |
| 3578 | |
| 3579 | char *choices[] = { |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 3580 | "Choice 1", |
| 3581 | "Choice 2", |
| 3582 | "Choice 3", |
| 3583 | "Choice 4", |
| 3584 | "Exit", |
| 3585 | }; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 3586 | int n_choices = sizeof(choices) / sizeof(char *); |
| 3587 | void print_menu(WINDOW *menu_win, int highlight); |
| 3588 | |
| 3589 | int main() |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 3590 | { WINDOW *menu_win; |
| 3591 | int highlight = 1; |
| 3592 | int choice = 0; |
| 3593 | int c; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 3594 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 3639 | } |
| 3640 | |
| 3641 | |
| 3642 | void print_menu(WINDOW *menu_win, int highlight) |
| 3643 | { |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 3644 | int x, y, i; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 3645 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 3660 | } |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 3661 | </SPAN |
| 3662 | ></PRE |
| 3663 | ></DIV |
| 3664 | ></DIV |
| 3665 | ></DIV |
| 3666 | ><DIV |
| 3667 | CLASS="SECT1" |
| 3668 | ><HR><H2 |
| 3669 | CLASS="SECT1" |
| 3670 | ><A |
| 3671 | NAME="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. |
| 3676 | Usually each UI allows the user to interact with both keyboard and mouse. </P |
| 3677 | ><DIV |
| 3678 | CLASS="SECT2" |
| 3679 | ><HR><H3 |
| 3680 | CLASS="SECT2" |
| 3681 | ><A |
| 3682 | NAME="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 |
| 3687 | with <TT |
| 3688 | CLASS="LITERAL" |
| 3689 | >mousemask()</TT |
| 3690 | >.</P |
| 3691 | ><PRE |
| 3692 | CLASS="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 |
| 3697 | listen. By default, all the events are turned off. The bit mask <TT |
| 3698 | CLASS="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 |
| 3704 | CLASS="PROGRAMLISTING" |
| 3705 | > Name Description |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 3706 | --------------------------------------------------------------------- |
| 3707 | BUTTON1_PRESSED mouse button 1 down |
| 3708 | BUTTON1_RELEASED mouse button 1 up |
| 3709 | BUTTON1_CLICKED mouse button 1 clicked |
| 3710 | BUTTON1_DOUBLE_CLICKED mouse button 1 double clicked |
| 3711 | BUTTON1_TRIPLE_CLICKED mouse button 1 triple clicked |
| 3712 | BUTTON2_PRESSED mouse button 2 down |
| 3713 | BUTTON2_RELEASED mouse button 2 up |
| 3714 | BUTTON2_CLICKED mouse button 2 clicked |
| 3715 | BUTTON2_DOUBLE_CLICKED mouse button 2 double clicked |
| 3716 | BUTTON2_TRIPLE_CLICKED mouse button 2 triple clicked |
| 3717 | BUTTON3_PRESSED mouse button 3 down |
| 3718 | BUTTON3_RELEASED mouse button 3 up |
| 3719 | BUTTON3_CLICKED mouse button 3 clicked |
| 3720 | BUTTON3_DOUBLE_CLICKED mouse button 3 double clicked |
| 3721 | BUTTON3_TRIPLE_CLICKED mouse button 3 triple clicked |
| 3722 | BUTTON4_PRESSED mouse button 4 down |
| 3723 | BUTTON4_RELEASED mouse button 4 up |
| 3724 | BUTTON4_CLICKED mouse button 4 clicked |
| 3725 | BUTTON4_DOUBLE_CLICKED mouse button 4 double clicked |
| 3726 | BUTTON4_TRIPLE_CLICKED mouse button 4 triple clicked |
| 3727 | BUTTON_SHIFT shift was down during button state change |
| 3728 | BUTTON_CTRL control was down during button state change |
| 3729 | BUTTON_ALT alt was down during button state change |
| 3730 | ALL_MOUSE_EVENTS report all button state changes |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 3731 | REPORT_MOUSE_POSITION report mouse movement</PRE |
| 3732 | ></DIV |
| 3733 | ><DIV |
| 3734 | CLASS="SECT2" |
| 3735 | ><HR><H3 |
| 3736 | CLASS="SECT2" |
| 3737 | ><A |
| 3738 | NAME="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 |
| 3743 | return KEY_MOUSE every time some mouse event happens. Then the mouse event can |
| 3744 | be retrieved with <TT |
| 3745 | CLASS="LITERAL" |
| 3746 | >getmouse()</TT |
| 3747 | >.</P |
| 3748 | ><P |
| 3749 | >The code approximately looks like this:</P |
| 3750 | ><PRE |
| 3751 | CLASS="PROGRAMLISTING" |
| 3752 | > MEVENT event; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 3753 | |
| 3754 | ch = getch(); |
| 3755 | if(ch == KEY_MOUSE) |
| 3756 | if(getmouse(&event) == OK) |
| 3757 | . /* Do some thing with the event */ |
| 3758 | . |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 3759 | .</PRE |
| 3760 | ><P |
| 3761 | > |
| 3762 | getmouse() returns the event into the pointer given to it. It's a structure |
| 3763 | which contains</P |
| 3764 | ><PRE |
| 3765 | CLASS="PROGRAMLISTING" |
| 3766 | > typedef struct |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 3767 | { |
| 3768 | short id; /* ID to distinguish multiple devices */ |
| 3769 | int x, y, z; /* event coordinates */ |
| 3770 | mmask_t bstate; /* button state bits */ |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 3771 | } </PRE |
| 3772 | ><P |
| 3773 | >The <TT |
| 3774 | CLASS="LITERAL" |
| 3775 | >bstate</TT |
| 3776 | > is the main variable we are |
| 3777 | interested 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 |
| 3781 | CLASS="PROGRAMLISTING" |
| 3782 | > if(event.bstate & BUTTON1_PRESSED) |
| 3783 | printw("Left Button Pressed");</PRE |
| 3784 | ></DIV |
| 3785 | ><DIV |
| 3786 | CLASS="SECT2" |
| 3787 | ><HR><H3 |
| 3788 | CLASS="SECT2" |
| 3789 | ><A |
| 3790 | NAME="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 |
| 3795 | mouse interaction. To make things simpler, key handling is removed.</P |
| 3796 | ><DIV |
| 3797 | CLASS="EXAMPLE" |
| 3798 | ><A |
| 3799 | NAME="BMOME" |
| 3800 | ></A |
| 3801 | ><P |
| 3802 | ><B |
| 3803 | >Example 11. Access the menu with mouse !!! </B |
| 3804 | ></P |
| 3805 | ><PRE |
| 3806 | CLASS="PROGRAMLISTING" |
| 3807 | ><SPAN |
| 3808 | CLASS="INLINEMEDIAOBJECT" |
| 3809 | >#include <ncurses.h> |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 3810 | |
| 3811 | #define WIDTH 30 |
| 3812 | #define HEIGHT 10 |
| 3813 | |
| 3814 | int startx = 0; |
| 3815 | int starty = 0; |
| 3816 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 3817 | char *choices[] = { "Choice 1", |
| 3818 | "Choice 2", |
| 3819 | "Choice 3", |
| 3820 | "Choice 4", |
| 3821 | "Exit", |
| 3822 | }; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 3823 | |
| 3824 | int n_choices = sizeof(choices) / sizeof(char *); |
| 3825 | |
| 3826 | void print_menu(WINDOW *menu_win, int highlight); |
| 3827 | void report_choice(int mouse_x, int mouse_y, int *p_choice); |
| 3828 | |
| 3829 | int main() |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 3830 | { int c, choice = 0; |
| 3831 | WINDOW *menu_win; |
| 3832 | MEVENT event; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 3833 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 3834 | /* Initialize curses */ |
| 3835 | initscr(); |
| 3836 | clear(); |
| 3837 | noecho(); |
| 3838 | cbreak(); //Line buffering disabled. pass on everything |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 3839 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 3848 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 | } |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 3873 | end: |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 3874 | endwin(); |
| 3875 | return 0; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 3876 | } |
| 3877 | |
| 3878 | |
| 3879 | void print_menu(WINDOW *menu_win, int highlight) |
| 3880 | { |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 3881 | int x, y, i; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 3882 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 3897 | } |
| 3898 | |
| 3899 | /* Report the choice according to mouse position */ |
| 3900 | void report_choice(int mouse_x, int mouse_y, int *p_choice) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 3901 | { int i,j, choice; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 3902 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 |
| 3919 | CLASS="SECT2" |
| 3920 | ><HR><H3 |
| 3921 | CLASS="SECT2" |
| 3922 | ><A |
| 3923 | NAME="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 |
| 3928 | co-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 |
| 3931 | second) that can elapse between press and release events in order for |
| 3932 | them to be recognized as a click. This function returns the previous |
| 3933 | interval value. The default is one fifth of a second.</P |
| 3934 | ></DIV |
| 3935 | ></DIV |
| 3936 | ><DIV |
| 3937 | CLASS="SECT1" |
| 3938 | ><HR><H2 |
| 3939 | CLASS="SECT1" |
| 3940 | ><A |
| 3941 | NAME="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 |
| 3946 | screen efficiently and to write some fancy programs. This is especially |
| 3947 | important in writing games. </P |
| 3948 | ><DIV |
| 3949 | CLASS="SECT2" |
| 3950 | ><HR><H3 |
| 3951 | CLASS="SECT2" |
| 3952 | ><A |
| 3953 | NAME="GETYX" |
| 3954 | >13.1. getyx() functions</A |
| 3955 | ></H3 |
| 3956 | ><P |
| 3957 | > The function <TT |
| 3958 | CLASS="LITERAL" |
| 3959 | >getyx()</TT |
| 3960 | > can be used to find out |
| 3961 | the present cursor co-ordinates. It will fill the values of x and y co-ordinates |
| 3962 | in the arguments given to it. Since getyx() is a macro you don't have to pass |
| 3963 | the address of the variables. It can be called as</P |
| 3964 | ><PRE |
| 3965 | CLASS="PROGRAMLISTING" |
| 3966 | > getyx(win, y, x); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 3967 | /* win: window pointer |
| 3968 | * y, x: y, x co-ordinates will be put into this variables |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 3969 | */</PRE |
| 3970 | ><P |
| 3971 | >The function getparyx() gets the beginning co-ordinates of the sub window |
| 3972 | relative to the main window. This is some times useful to update a sub window. |
| 3973 | When designing fancy stuff like writing multiple menus, it becomes difficult to |
| 3974 | store the menu positions, their first option co-ordinates etc. A simple solution |
| 3975 | to this problem, is to create menus in sub windows and later find the starting |
| 3976 | co-ordinates of the menus by using getparyx().</P |
| 3977 | ><P |
| 3978 | >The functions getbegyx() and getmaxyx() store current window's beginning and |
| 3979 | maximum co-ordinates. These functions are useful in the same way as above in |
| 3980 | managing the windows and sub windows effectively.</P |
| 3981 | ></DIV |
| 3982 | ><DIV |
| 3983 | CLASS="SECT2" |
| 3984 | ><HR><H3 |
| 3985 | CLASS="SECT2" |
| 3986 | ><A |
| 3987 | NAME="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 |
| 3992 | screen and restore it back to the same state. The function scr_dump() can be |
| 3993 | used to dump the screen contents to a file given as an argument. Later it can be |
| 3994 | restored by scr_restore function. These two simple functions can be used |
| 3995 | effectively to maintain a fast moving game with changing scenarios. </P |
| 3996 | ></DIV |
| 3997 | ><DIV |
| 3998 | CLASS="SECT2" |
| 3999 | ><HR><H3 |
| 4000 | CLASS="SECT2" |
| 4001 | ><A |
| 4002 | NAME="WINDOWDUMP" |
| 4003 | >13.3. Window Dumping</A |
| 4004 | ></H3 |
| 4005 | ><P |
| 4006 | >To store and restore windows, the functions |
| 4007 | <TT |
| 4008 | CLASS="LITERAL" |
| 4009 | >putwin()</TT |
| 4010 | > and <TT |
| 4011 | CLASS="LITERAL" |
| 4012 | >getwin()</TT |
| 4013 | > can be used. <TT |
| 4014 | CLASS="LITERAL" |
| 4015 | >putwin()</TT |
| 4016 | > puts |
| 4017 | the present window state into a file, which can be later restored by |
| 4018 | <TT |
| 4019 | CLASS="LITERAL" |
| 4020 | >getwin()</TT |
| 4021 | >.</P |
| 4022 | ><P |
| 4023 | > |
| 4024 | The function <TT |
| 4025 | CLASS="LITERAL" |
| 4026 | >copywin()</TT |
| 4027 | > can be used to copy a |
| 4028 | window completely onto another window. It takes the source and destination |
| 4029 | windows as parameters and according to the rectangle specified, it copies the |
| 4030 | rectangular region from source to destination window. It's last parameter |
| 4031 | specifies whether to overwrite or just overlay the contents on to the |
| 4032 | destination window. If this argument is true, then the copying is |
| 4033 | non-destructive.</P |
| 4034 | ></DIV |
| 4035 | ></DIV |
| 4036 | ><DIV |
| 4037 | CLASS="SECT1" |
| 4038 | ><HR><H2 |
| 4039 | CLASS="SECT1" |
| 4040 | ><A |
| 4041 | NAME="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 |
| 4046 | whistles. There are some miscellaneous functions which are useful in various |
| 4047 | cases. Let's go headlong into some of those.</P |
| 4048 | ><DIV |
| 4049 | CLASS="SECT2" |
| 4050 | ><HR><H3 |
| 4051 | CLASS="SECT2" |
| 4052 | ><A |
| 4053 | NAME="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 |
| 4058 | function should be </P |
| 4059 | ><PRE |
| 4060 | CLASS="PROGRAMLISTING" |
| 4061 | > 0 : invisible or |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4062 | 1 : normal or |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 4063 | 2 : very visible.</PRE |
| 4064 | ></DIV |
| 4065 | ><DIV |
| 4066 | CLASS="SECT2" |
| 4067 | ><HR><H3 |
| 4068 | CLASS="SECT2" |
| 4069 | ><A |
| 4070 | NAME="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) |
| 4075 | temporarily. In such a case you will first need to save the tty modes with a |
| 4076 | call to <TT |
| 4077 | CLASS="LITERAL" |
| 4078 | >def_prog_mode()</TT |
| 4079 | > and then call |
| 4080 | <TT |
| 4081 | CLASS="LITERAL" |
| 4082 | >endwin()</TT |
| 4083 | > to end the curses mode. This will |
| 4084 | leave you in the original tty mode. To get back to curses once you are done, |
| 4085 | call <TT |
| 4086 | CLASS="LITERAL" |
| 4087 | >reset_prog_mode() </TT |
| 4088 | >. This function returns |
| 4089 | the tty to the state stored by <TT |
| 4090 | CLASS="LITERAL" |
| 4091 | >def_prog_mode()</TT |
| 4092 | >. Then do refresh(), and you are back to the curses mode. Here |
| 4093 | is an example showing the sequence of things to be done.</P |
| 4094 | ><DIV |
| 4095 | CLASS="EXAMPLE" |
| 4096 | ><A |
| 4097 | NAME="BTELE" |
| 4098 | ></A |
| 4099 | ><P |
| 4100 | ><B |
| 4101 | >Example 12. Temporarily Leaving Curses Mode </B |
| 4102 | ></P |
| 4103 | ><PRE |
| 4104 | CLASS="PROGRAMLISTING" |
| 4105 | ><SPAN |
| 4106 | CLASS="INLINEMEDIAOBJECT" |
| 4107 | >#include <ncurses.h> |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4108 | |
| 4109 | int main() |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4124 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 4125 | return 0; |
| 4126 | }</SPAN |
| 4127 | ></PRE |
| 4128 | ></DIV |
| 4129 | ></DIV |
| 4130 | ><DIV |
| 4131 | CLASS="SECT2" |
| 4132 | ><HR><H3 |
| 4133 | CLASS="SECT2" |
| 4134 | ><A |
| 4135 | NAME="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 |
| 4140 | extended character set. They are printable only on some terminals. NCURSES |
| 4141 | functions like <TT |
| 4142 | CLASS="LITERAL" |
| 4143 | >box()</TT |
| 4144 | > use these characters. All |
| 4145 | these variables start with ACS meaning alternative character set. You might have |
| 4146 | noticed me using these characters in some of the programs above. Here's an example |
| 4147 | showing all the characters.</P |
| 4148 | ><DIV |
| 4149 | CLASS="EXAMPLE" |
| 4150 | ><A |
| 4151 | NAME="BACSVARS" |
| 4152 | ></A |
| 4153 | ><P |
| 4154 | ><B |
| 4155 | >Example 13. ACS Variables Example </B |
| 4156 | ></P |
| 4157 | ><PRE |
| 4158 | CLASS="PROGRAMLISTING" |
| 4159 | ><SPAN |
| 4160 | CLASS="INLINEMEDIAOBJECT" |
| 4161 | >#include <ncurses.h> |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4162 | |
| 4163 | int main() |
| 4164 | { |
| 4165 | initscr(); |
| 4166 | |
| 4167 | printw("Upper left corner "); addch(ACS_ULCORNER); printw("\n"); |
| 4168 | printw("Lower left corner "); addch(ACS_LLCORNER); printw("\n"); |
| 4169 | printw("Lower right corner "); addch(ACS_LRCORNER); printw("\n"); |
| 4170 | printw("Tee pointing right "); addch(ACS_LTEE); printw("\n"); |
| 4171 | printw("Tee pointing left "); addch(ACS_RTEE); printw("\n"); |
| 4172 | printw("Tee pointing up "); addch(ACS_BTEE); printw("\n"); |
| 4173 | printw("Tee pointing down "); addch(ACS_TTEE); printw("\n"); |
| 4174 | printw("Horizontal line "); addch(ACS_HLINE); printw("\n"); |
| 4175 | printw("Vertical line "); addch(ACS_VLINE); printw("\n"); |
| 4176 | printw("Large Plus or cross over "); addch(ACS_PLUS); printw("\n"); |
| 4177 | printw("Scan Line 1 "); addch(ACS_S1); printw("\n"); |
| 4178 | printw("Scan Line 3 "); addch(ACS_S3); printw("\n"); |
| 4179 | printw("Scan Line 7 "); addch(ACS_S7); printw("\n"); |
| 4180 | printw("Scan Line 9 "); addch(ACS_S9); printw("\n"); |
| 4181 | printw("Diamond "); addch(ACS_DIAMOND); printw("\n"); |
| 4182 | printw("Checker board (stipple) "); addch(ACS_CKBOARD); printw("\n"); |
| 4183 | printw("Degree Symbol "); addch(ACS_DEGREE); printw("\n"); |
| 4184 | printw("Plus/Minus Symbol "); addch(ACS_PLMINUS); printw("\n"); |
| 4185 | printw("Bullet "); addch(ACS_BULLET); printw("\n"); |
| 4186 | printw("Arrow Pointing Left "); addch(ACS_LARROW); printw("\n"); |
| 4187 | printw("Arrow Pointing Right "); addch(ACS_RARROW); printw("\n"); |
| 4188 | printw("Arrow Pointing Down "); addch(ACS_DARROW); printw("\n"); |
| 4189 | printw("Arrow Pointing Up "); addch(ACS_UARROW); printw("\n"); |
| 4190 | printw("Board of squares "); addch(ACS_BOARD); printw("\n"); |
| 4191 | printw("Lantern Symbol "); addch(ACS_LANTERN); printw("\n"); |
| 4192 | printw("Solid Square Block "); addch(ACS_BLOCK); printw("\n"); |
| 4193 | printw("Less/Equal sign "); addch(ACS_LEQUAL); printw("\n"); |
| 4194 | printw("Greater/Equal sign "); addch(ACS_GEQUAL); printw("\n"); |
| 4195 | printw("Pi "); addch(ACS_PI); printw("\n"); |
| 4196 | printw("Not equal "); addch(ACS_NEQUAL); printw("\n"); |
| 4197 | printw("UK pound sign "); addch(ACS_STERLING); printw("\n"); |
| 4198 | |
| 4199 | refresh(); |
| 4200 | getch(); |
| 4201 | endwin(); |
| 4202 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 4203 | return 0; |
| 4204 | }</SPAN |
| 4205 | ></PRE |
| 4206 | ></DIV |
| 4207 | ></DIV |
| 4208 | ></DIV |
| 4209 | ><DIV |
| 4210 | CLASS="SECT1" |
| 4211 | ><HR><H2 |
| 4212 | CLASS="SECT1" |
| 4213 | ><A |
| 4214 | NAME="OTHERLIB" |
| 4215 | >15. Other libraries</A |
| 4216 | ></H2 |
| 4217 | ><P |
| 4218 | >Apart from the curses library, there are few text mode libraries, which provide |
| 4219 | more functionality and a lot of features. The following sections explain three |
| 4220 | standard libraries which are usually distributed along with curses. </P |
| 4221 | ></DIV |
| 4222 | ><DIV |
| 4223 | CLASS="SECT1" |
| 4224 | ><HR><H2 |
| 4225 | CLASS="SECT1" |
| 4226 | ><A |
| 4227 | NAME="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 |
| 4232 | created a lot of overlapping windows to give a professional windows-type look. |
| 4233 | Unfortunately, it soon becomes difficult to manage these. The multiple |
| 4234 | refreshes, updates plunge you into a nightmare. The overlapping windows create |
| 4235 | blotches, 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 |
| 4238 | words of developers of ncurses </P |
| 4239 | ><P |
| 4240 | ><SPAN |
| 4241 | CLASS="emphasis" |
| 4242 | ><I |
| 4243 | CLASS="EMPHASIS" |
| 4244 | >When your interface design is such that windows may dive deeper into the |
| 4245 | visibility stack or pop to the top at runtime, the resulting book-keeping can be |
| 4246 | tedious 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 |
| 4251 | obviates the need of doing series of wnoutrefresh(), doupdate() and relieves the |
| 4252 | burden of doing it correctly(bottom up). The library maintains information about |
| 4253 | the order of windows, their overlapping and update the screen properly. So why |
| 4254 | wait? Let's take a close peek into panels.</P |
| 4255 | ><DIV |
| 4256 | CLASS="SECT2" |
| 4257 | ><HR><H3 |
| 4258 | CLASS="SECT2" |
| 4259 | ><A |
| 4260 | NAME="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 |
| 4265 | all other panel objects. The deck is treated as a stack with the top panel being |
| 4266 | completely visible and the other panels may or may not be obscured according to |
| 4267 | their positions. So the basic idea is to create a stack of overlapping panels |
| 4268 | and use panels library to display them correctly. There is a function similar to |
| 4269 | refresh() which, when called , displays panels in the correct order. Functions |
| 4270 | are provided to hide or show panels, move panels, change its size etc.. The |
| 4271 | overlapping problem is managed by the panels library during all the calls to |
| 4272 | these functions. </P |
| 4273 | ><P |
| 4274 | >The general flow of a panel program goes like this: |
| 4275 | |
| 4276 | <P |
| 4277 | ></P |
| 4278 | ><OL |
| 4279 | TYPE="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 |
| 4287 | desired 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 |
| 4292 | visibility 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 |
| 4297 | use of helper functions like panel_hidden() and panel_window(). Make use of user |
| 4298 | pointer to store custom data for a panel. Use the functions set_panel_userptr() |
| 4299 | and 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 |
| 4309 | program which creates 3 overlapping panels and shows them on the screen. </P |
| 4310 | ></DIV |
| 4311 | ><DIV |
| 4312 | CLASS="SECT2" |
| 4313 | ><HR><H3 |
| 4314 | CLASS="SECT2" |
| 4315 | ><A |
| 4316 | NAME="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 |
| 4321 | program with panels library the flag -lpanel should be added along with |
| 4322 | -lncurses in that order.</P |
| 4323 | ><PRE |
| 4324 | CLASS="PROGRAMLISTING" |
| 4325 | > #include <panel.h> |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4326 | . |
| 4327 | . |
| 4328 | . |
| 4329 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 4330 | compile and link: gcc <program file> -lpanel -lncurses</PRE |
| 4331 | ><DIV |
| 4332 | CLASS="EXAMPLE" |
| 4333 | ><A |
| 4334 | NAME="PPASI" |
| 4335 | ></A |
| 4336 | ><P |
| 4337 | ><B |
| 4338 | >Example 14. Panel basics</B |
| 4339 | ></P |
| 4340 | ><PRE |
| 4341 | CLASS="PROGRAMLISTING" |
| 4342 | ><SPAN |
| 4343 | CLASS="INLINEMEDIAOBJECT" |
| 4344 | >#include <panel.h> |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4345 | |
| 4346 | int main() |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 4347 | { WINDOW *my_wins[3]; |
| 4348 | PANEL *my_panels[3]; |
| 4349 | int lines = 10, cols = 40, y = 2, x = 4, i; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4350 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 4351 | initscr(); |
| 4352 | cbreak(); |
| 4353 | noecho(); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4354 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4359 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4366 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4371 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 4372 | /* Update the stacking order. 2nd panel will be on top */ |
| 4373 | update_panels(); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4374 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 4375 | /* Show it on the screen */ |
| 4376 | doupdate(); |
| 4377 | |
| 4378 | getch(); |
| 4379 | endwin(); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4380 | } |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 4381 | </SPAN |
| 4382 | ></PRE |
| 4383 | ></DIV |
| 4384 | ><P |
| 4385 | >As you can see, above program follows a simple flow as explained. The windows |
| 4386 | are created with newwin() and then they are attached to panels with new_panel(). |
| 4387 | As we attach one panel after another, the stack of panels gets updated. To put |
| 4388 | them on screen update_panels() and doupdate() are called.</P |
| 4389 | ></DIV |
| 4390 | ><DIV |
| 4391 | CLASS="SECT2" |
| 4392 | ><HR><H3 |
| 4393 | CLASS="SECT2" |
| 4394 | ><A |
| 4395 | NAME="PANELBROWSING" |
| 4396 | >16.3. Panel Window Browsing</A |
| 4397 | ></H3 |
| 4398 | ><P |
| 4399 | >A slightly complicated example is given below. This program creates 3 |
| 4400 | windows which can be cycled through using tab. Have a look at the code.</P |
| 4401 | ><DIV |
| 4402 | CLASS="EXAMPLE" |
| 4403 | ><A |
| 4404 | NAME="PPABR" |
| 4405 | ></A |
| 4406 | ><P |
| 4407 | ><B |
| 4408 | >Example 15. Panel Window Browsing Example </B |
| 4409 | ></P |
| 4410 | ><PRE |
| 4411 | CLASS="PROGRAMLISTING" |
| 4412 | ><SPAN |
| 4413 | CLASS="INLINEMEDIAOBJECT" |
| 4414 | >#include <panel.h> |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4415 | |
| 4416 | #define NLINES 10 |
| 4417 | #define NCOLS 40 |
| 4418 | |
| 4419 | void init_wins(WINDOW **wins, int n); |
| 4420 | void win_show(WINDOW *win, char *label, int label_color); |
| 4421 | void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); |
| 4422 | |
| 4423 | int main() |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 4424 | { WINDOW *my_wins[3]; |
| 4425 | PANEL *my_panels[3]; |
| 4426 | PANEL *top; |
| 4427 | int ch; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4428 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 4429 | /* Initialize curses */ |
| 4430 | initscr(); |
| 4431 | start_color(); |
| 4432 | cbreak(); |
| 4433 | noecho(); |
| 4434 | keypad(stdscr, TRUE); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4435 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4441 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4448 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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]); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4453 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 4454 | /* Update the stacking order. 2nd panel will be on top */ |
| 4455 | update_panels(); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4456 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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(); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4462 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4476 | } |
| 4477 | |
| 4478 | /* Put all the windows */ |
| 4479 | void init_wins(WINDOW **wins, int n) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 4480 | { int x, y, i; |
| 4481 | char label[80]; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4482 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 | } |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4492 | } |
| 4493 | |
| 4494 | /* Show the window with a border and a label */ |
| 4495 | void win_show(WINDOW *win, char *label, int label_color) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 4496 | { int startx, starty, height, width; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4497 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 4498 | getbegyx(win, starty, startx); |
| 4499 | getmaxyx(win, height, width); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4500 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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)); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4507 | } |
| 4508 | |
| 4509 | void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 4510 | { int length, x, y; |
| 4511 | float temp; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4512 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4522 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 |
| 4535 | CLASS="SECT2" |
| 4536 | ><HR><H3 |
| 4537 | CLASS="SECT2" |
| 4538 | ><A |
| 4539 | NAME="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 |
| 4544 | cycle. We can attach custom information to the panel by specifying a user |
| 4545 | pointer, which can point to any information you want to store. In this case I |
| 4546 | stored the pointer to the next panel in the cycle. User pointer for a panel can |
| 4547 | be set with the function <TT |
| 4548 | CLASS="LITERAL" |
| 4549 | > set_panel_userptr()</TT |
| 4550 | >. |
| 4551 | It can be accessed using the function <TT |
| 4552 | CLASS="LITERAL" |
| 4553 | >panel_userptr()</TT |
| 4554 | > which will return the user pointer for the panel given as |
| 4555 | argument. After finding the next panel in the cycle It's brought to the top by |
| 4556 | the function top_panel(). This function brings the panel given as argument to |
| 4557 | the top of the panel stack. </P |
| 4558 | ></DIV |
| 4559 | ><DIV |
| 4560 | CLASS="SECT2" |
| 4561 | ><HR><H3 |
| 4562 | CLASS="SECT2" |
| 4563 | ><A |
| 4564 | NAME="PANELMOVERESIZE" |
| 4565 | >16.5. Moving and Resizing Panels</A |
| 4566 | ></H3 |
| 4567 | ><P |
| 4568 | >The function <TT |
| 4569 | CLASS="LITERAL" |
| 4570 | >move_panel()</TT |
| 4571 | > can be used to move a |
| 4572 | panel to the desired location. It does not change the position of the panel in |
| 4573 | the stack. Make sure that you use move_panel() instead mvwin() on the window |
| 4574 | associated with the panel.</P |
| 4575 | ><P |
| 4576 | >Resizing a panel is slightly complex. There is no straight forward function |
| 4577 | just to resize the window associated with a panel. A solution to resize a panel |
| 4578 | is to create a new window with the desired sizes, change the window associated |
| 4579 | with the panel using replace_panel(). Don't forget to delete the old window. The |
| 4580 | window associated with a panel can be found by using the function |
| 4581 | panel_window().</P |
| 4582 | ><P |
| 4583 | >The following program shows these concepts, in supposedly simple program. You |
| 4584 | can cycle through the window with <TAB> as usual. To resize or move the |
| 4585 | active panel press 'r' for resize 'm' for moving. Then use arrow keys to resize |
| 4586 | or move it to the desired way and press enter to end your resizing or moving. |
| 4587 | This example makes use of user data to get the required data to do the |
| 4588 | operations. </P |
| 4589 | ><DIV |
| 4590 | CLASS="EXAMPLE" |
| 4591 | ><A |
| 4592 | NAME="PPARE" |
| 4593 | ></A |
| 4594 | ><P |
| 4595 | ><B |
| 4596 | >Example 16. Panel Moving and Resizing example </B |
| 4597 | ></P |
| 4598 | ><PRE |
| 4599 | CLASS="PROGRAMLISTING" |
| 4600 | ><SPAN |
| 4601 | CLASS="INLINEMEDIAOBJECT" |
| 4602 | >#include <panel.h> |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4603 | |
| 4604 | typedef struct _PANEL_DATA { |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 4605 | int x, y, w, h; |
| 4606 | char label[80]; |
| 4607 | int label_color; |
| 4608 | PANEL *next; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4609 | }PANEL_DATA; |
| 4610 | |
| 4611 | #define NLINES 10 |
| 4612 | #define NCOLS 40 |
| 4613 | |
| 4614 | void init_wins(WINDOW **wins, int n); |
| 4615 | void win_show(WINDOW *win, char *label, int label_color); |
| 4616 | void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); |
| 4617 | void set_user_ptrs(PANEL **panels, int n); |
| 4618 | |
| 4619 | int main() |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4628 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 4629 | /* Initialize curses */ |
| 4630 | initscr(); |
| 4631 | start_color(); |
| 4632 | cbreak(); |
| 4633 | noecho(); |
| 4634 | keypad(stdscr, TRUE); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4635 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4641 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4648 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 4649 | set_user_ptrs(my_panels, 3); |
| 4650 | /* Update the stacking order. 2nd panel will be on top */ |
| 4651 | update_panels(); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4652 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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(); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4659 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4753 | } |
| 4754 | |
| 4755 | /* Put all the windows */ |
| 4756 | void init_wins(WINDOW **wins, int n) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 4757 | { int x, y, i; |
| 4758 | char label[80]; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4759 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 | } |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4769 | } |
| 4770 | |
| 4771 | /* Set the PANEL_DATA structures for individual panels */ |
| 4772 | void set_user_ptrs(PANEL **panels, int n) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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)); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4779 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 | } |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4797 | } |
| 4798 | |
| 4799 | /* Show the window with a border and a label */ |
| 4800 | void win_show(WINDOW *win, char *label, int label_color) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 4801 | { int startx, starty, height, width; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4802 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 4803 | getbegyx(win, starty, startx); |
| 4804 | getmaxyx(win, height, width); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4805 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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)); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4812 | } |
| 4813 | |
| 4814 | void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 4815 | { int length, x, y; |
| 4816 | float temp; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4817 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4827 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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, |
| 4840 | it takes appropriate action. If 'r' is pressed resizing mode is started. After |
| 4841 | this the new sizes are updated as the user presses the arrow keys. When the user |
| 4842 | presses <ENTER> present selection ends and panel is resized by using the |
| 4843 | concept explained. While in resizing mode the program doesn't show how the |
| 4844 | window is getting resized. It's left as an exercise to the reader to print a |
| 4845 | dotted 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 |
| 4848 | resizing. As the arrow keys are pressed the new position is updated and |
| 4849 | pressing of <ENTER> causes the panel to be moved by calling the function |
| 4850 | move_panel().</P |
| 4851 | ><P |
| 4852 | >In this program the user data which is represented as PANEL_DATA, plays very |
| 4853 | important role in finding the associated information with a panel. As written in |
| 4854 | the comments, the PANEL_DATA stores the panel sizes, label, label color and a |
| 4855 | pointer to the next panel in the cycle.</P |
| 4856 | ></DIV |
| 4857 | ><DIV |
| 4858 | CLASS="SECT2" |
| 4859 | ><HR><H3 |
| 4860 | CLASS="SECT2" |
| 4861 | ><A |
| 4862 | NAME="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 |
| 4867 | removes it form the stack of panels, thus hiding it on the screen once you do |
| 4868 | update_panels() and doupdate(). It doesn't destroy the PANEL structure |
| 4869 | associated with the hidden panel. It can be shown again by using the |
| 4870 | show_panel() function.</P |
| 4871 | ><P |
| 4872 | >The following program shows the hiding of panels. Press 'a' or 'b' or 'c' to |
| 4873 | show or hide first, second and third windows respectively. It uses a user data |
| 4874 | with a small variable hide, which keeps track of whether the window is hidden or |
| 4875 | not. For some reason the function |
| 4876 | <TT |
| 4877 | CLASS="LITERAL" |
| 4878 | >panel_hidden()</TT |
| 4879 | > which tells whether a panel is |
| 4880 | hidden or not is not working. A bug report was also presented by Michael Andres |
| 4881 | <A |
| 4882 | HREF="http://www.geocrawler.com/archives/3/344/1999/9/0/2643549/" |
| 4883 | TARGET="_top" |
| 4884 | > here</A |
| 4885 | ></P |
| 4886 | ><DIV |
| 4887 | CLASS="EXAMPLE" |
| 4888 | ><A |
| 4889 | NAME="PPAHI" |
| 4890 | ></A |
| 4891 | ><P |
| 4892 | ><B |
| 4893 | >Example 17. Panel Hiding and Showing example </B |
| 4894 | ></P |
| 4895 | ><PRE |
| 4896 | CLASS="PROGRAMLISTING" |
| 4897 | ><SPAN |
| 4898 | CLASS="INLINEMEDIAOBJECT" |
| 4899 | >#include <panel.h> |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4900 | |
| 4901 | typedef struct _PANEL_DATA { |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 4902 | int hide; /* TRUE if panel is hidden */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4903 | }PANEL_DATA; |
| 4904 | |
| 4905 | #define NLINES 10 |
| 4906 | #define NCOLS 40 |
| 4907 | |
| 4908 | void init_wins(WINDOW **wins, int n); |
| 4909 | void win_show(WINDOW *win, char *label, int label_color); |
| 4910 | void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); |
| 4911 | |
| 4912 | int main() |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 4913 | { WINDOW *my_wins[3]; |
| 4914 | PANEL *my_panels[3]; |
| 4915 | PANEL_DATA panel_datas[3]; |
| 4916 | PANEL_DATA *temp; |
| 4917 | int ch; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4918 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 4919 | /* Initialize curses */ |
| 4920 | initscr(); |
| 4921 | start_color(); |
| 4922 | cbreak(); |
| 4923 | noecho(); |
| 4924 | keypad(stdscr, TRUE); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4925 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4931 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4938 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4943 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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]); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4947 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 4948 | /* Update the stacking order. 2nd panel will be on top */ |
| 4949 | update_panels(); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4950 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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"); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4955 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5000 | } |
| 5001 | |
| 5002 | /* Put all the windows */ |
| 5003 | void init_wins(WINDOW **wins, int n) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5004 | { int x, y, i; |
| 5005 | char label[80]; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5006 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 | } |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5016 | } |
| 5017 | |
| 5018 | /* Show the window with a border and a label */ |
| 5019 | void win_show(WINDOW *win, char *label, int label_color) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5020 | { int startx, starty, height, width; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5021 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5022 | getbegyx(win, starty, startx); |
| 5023 | getmaxyx(win, height, width); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5024 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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)); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5031 | } |
| 5032 | |
| 5033 | void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5034 | { int length, x, y; |
| 5035 | float temp; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5036 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5046 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 |
| 5059 | CLASS="SECT2" |
| 5060 | ><HR><H3 |
| 5061 | CLASS="SECT2" |
| 5062 | ><A |
| 5063 | NAME="PANELABOVE" |
| 5064 | >16.7. panel_above() and panel_below() Functions</A |
| 5065 | ></H3 |
| 5066 | ><P |
| 5067 | >The functions <TT |
| 5068 | CLASS="LITERAL" |
| 5069 | >panel_above()</TT |
| 5070 | > and |
| 5071 | <TT |
| 5072 | CLASS="LITERAL" |
| 5073 | >panel_below()</TT |
| 5074 | > can be used to find out the panel |
| 5075 | above and below a panel. If the argument to these functions is NULL, then they |
| 5076 | return a pointer to bottom panel and top panel respectively.</P |
| 5077 | ></DIV |
| 5078 | ></DIV |
| 5079 | ><DIV |
| 5080 | CLASS="SECT1" |
| 5081 | ><HR><H2 |
| 5082 | CLASS="SECT1" |
| 5083 | ><A |
| 5084 | NAME="MENUS" |
| 5085 | >17. Menus Library</A |
| 5086 | ></H2 |
| 5087 | ><P |
| 5088 | >The menus library provides a nice extension to basic curses, through which you |
| 5089 | can create menus. It provides a set of functions to create menus. But they have |
| 5090 | to be customized to give a nicer look, with colors etc. Let's get into the |
| 5091 | details.</P |
| 5092 | ><P |
| 5093 | >A menu is a screen display that assists the user to choose some subset of a |
| 5094 | given set of items. To put it simple, a menu is a collection of items from which |
| 5095 | one or more items can be chosen. Some readers might not be aware of multiple |
| 5096 | item selection capability. Menu library provides functionality to write menus |
| 5097 | from which the user can chose more than one item as the preferred choice. This |
| 5098 | is dealt with in a later section. Now it is time for some rudiments.</P |
| 5099 | ><DIV |
| 5100 | CLASS="SECT2" |
| 5101 | ><HR><H3 |
| 5102 | CLASS="SECT2" |
| 5103 | ><A |
| 5104 | NAME="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. |
| 5109 | After that, all the processing of user responses is done in an elegant function |
| 5110 | menu_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 |
| 5116 | TYPE="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 |
| 5124 | items.</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 |
| 5137 | menu_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 |
| 5159 | with up, down arrows. </P |
| 5160 | ></DIV |
| 5161 | ><DIV |
| 5162 | CLASS="SECT2" |
| 5163 | ><HR><H3 |
| 5164 | CLASS="SECT2" |
| 5165 | ><A |
| 5166 | NAME="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 |
| 5171 | program with menu library the flag -lmenu should be added along with -lncurses |
| 5172 | in that order.</P |
| 5173 | ><PRE |
| 5174 | CLASS="PROGRAMLISTING" |
| 5175 | > #include <menu.h> |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5176 | . |
| 5177 | . |
| 5178 | . |
| 5179 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5180 | compile and link: gcc <program file> -lmenu -lncurses</PRE |
| 5181 | ><DIV |
| 5182 | CLASS="EXAMPLE" |
| 5183 | ><A |
| 5184 | NAME="MMESI" |
| 5185 | ></A |
| 5186 | ><P |
| 5187 | ><B |
| 5188 | >Example 18. Menu Basics </B |
| 5189 | ></P |
| 5190 | ><PRE |
| 5191 | CLASS="PROGRAMLISTING" |
| 5192 | ><SPAN |
| 5193 | CLASS="INLINEMEDIAOBJECT" |
| 5194 | >#include <curses.h> |
| 5195 | #include <menu.h> |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5196 | |
| 5197 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5198 | #define CTRLD 4 |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5199 | |
| 5200 | char *choices[] = { |
| 5201 | "Choice 1", |
| 5202 | "Choice 2", |
| 5203 | "Choice 3", |
| 5204 | "Choice 4", |
| 5205 | "Exit", |
| 5206 | }; |
| 5207 | |
| 5208 | int main() |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 *)); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5223 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5227 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5228 | my_menu = new_menu((ITEM **)my_items); |
| 5229 | mvprintw(LINES - 2, 0, "F1 to Exit"); |
| 5230 | post_menu(my_menu); |
| 5231 | refresh(); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5232 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 | } |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5243 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5244 | free_item(my_items[0]); |
| 5245 | free_item(my_items[1]); |
| 5246 | free_menu(my_menu); |
| 5247 | endwin(); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5248 | } |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5249 | </SPAN |
| 5250 | ></PRE |
| 5251 | ></DIV |
| 5252 | ><P |
| 5253 | >This program demonstrates the basic concepts involved in creating a menu using |
| 5254 | menus library. First we create the items using new_item() and then attach them |
| 5255 | to the menu with new_menu() function. After posting the menu and refreshing the |
| 5256 | screen, the main processing loop starts. It reads user input and takes |
| 5257 | corresponding action. The function menu_driver() is the main work horse of the |
| 5258 | menu system. The second parameter to this function tells what's to be done with |
| 5259 | the menu. According to the parameter, menu_driver() does the corresponding task. |
| 5260 | The value can be either a menu navigational request, an ascii character, or a |
| 5261 | KEY_MOUSE special key associated with a mouse event.</P |
| 5262 | ><P |
| 5263 | >The menu_driver accepts following navigational requests. |
| 5264 | <PRE |
| 5265 | CLASS="PROGRAMLISTING" |
| 5266 | > REQ_LEFT_ITEM Move left to an item. |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 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. |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 |
| 5286 | after another. The options of interest in this example are REQ_UP_ITEM and |
| 5287 | REQ_DOWN_ITEM. These two options when passed to menu_driver, menu driver |
| 5288 | updates the current item to one item up or down respectively.</P |
| 5289 | ></DIV |
| 5290 | ><DIV |
| 5291 | CLASS="SECT2" |
| 5292 | ><HR><H3 |
| 5293 | CLASS="SECT2" |
| 5294 | ><A |
| 5295 | NAME="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 |
| 5300 | updating the menu. It is very important to understand various options it takes |
| 5301 | and what they do. As explained above, the second parameter to menu_driver() can |
| 5302 | be either a navigational request, a printable character or a KEY_MOUSE key. |
| 5303 | Let's dissect the different navigational requests.</P |
| 5304 | ><P |
| 5305 | ></P |
| 5306 | ><UL |
| 5307 | ><LI |
| 5308 | ><P |
| 5309 | ><SPAN |
| 5310 | CLASS="emphasis" |
| 5311 | ><I |
| 5312 | CLASS="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 |
| 5318 | be done by using the <TT |
| 5319 | CLASS="LITERAL" |
| 5320 | >menu_format()</TT |
| 5321 | >function. |
| 5322 | When a multi columnar menu is displayed these requests cause the menu driver to |
| 5323 | move the current selection to left or right.</P |
| 5324 | ></LI |
| 5325 | ><LI |
| 5326 | ><P |
| 5327 | ><SPAN |
| 5328 | CLASS="emphasis" |
| 5329 | ><I |
| 5330 | CLASS="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, |
| 5336 | makes the menu_driver to move the current selection to an item up or down.</P |
| 5337 | ></LI |
| 5338 | ><LI |
| 5339 | ><P |
| 5340 | > <SPAN |
| 5341 | CLASS="emphasis" |
| 5342 | ><I |
| 5343 | CLASS="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 |
| 5349 | related to scrolling. If all the items in the menu cannot be displayed in the |
| 5350 | menu sub window, then the menu is scrollable. These requests can be given to the |
| 5351 | menu_driver to do the scrolling either one line up, down or one page down or up |
| 5352 | respectively. </P |
| 5353 | ></LI |
| 5354 | ><LI |
| 5355 | ><P |
| 5356 | ><SPAN |
| 5357 | CLASS="emphasis" |
| 5358 | ><I |
| 5359 | CLASS="EMPHASIS" |
| 5360 | >REQ_FIRST_ITEM, REQ_LAST_ITEM, REQ_NEXT_ITEM and |
| 5361 | REQ_PREV_ITEM </I |
| 5362 | ></SPAN |
| 5363 | > </P |
| 5364 | ><P |
| 5365 | >These requests are self explanatory.</P |
| 5366 | ></LI |
| 5367 | ><LI |
| 5368 | ><P |
| 5369 | > <SPAN |
| 5370 | CLASS="emphasis" |
| 5371 | ><I |
| 5372 | CLASS="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 |
| 5378 | used only in a multi valued menu. So to use this request the option O_ONEVALUE |
| 5379 | must be off. This option can be made off or on with set_menu_opts().</P |
| 5380 | ></LI |
| 5381 | ><LI |
| 5382 | ><P |
| 5383 | > <SPAN |
| 5384 | CLASS="emphasis" |
| 5385 | ><I |
| 5386 | CLASS="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 |
| 5392 | match to the ascii characters entered by the user. Whenever ascii characters are |
| 5393 | given to menu_driver, it puts in to the pattern buffer. It also tries to find |
| 5394 | the nearest match to the pattern in the items list and moves current selection |
| 5395 | to that item. The request REQ_CLEAR_PATTERN clears the pattern buffer. The |
| 5396 | request REQ_BACK_PATTERN deletes the previous character in the pattern buffer. |
| 5397 | In case the pattern matches more than one item then the matched items can be |
| 5398 | cycled through REQ_NEXT_MATCH and REQ_PREV_MATCH which move the current |
| 5399 | selection to the next and previous matches respectively.</P |
| 5400 | ></LI |
| 5401 | ><LI |
| 5402 | ><P |
| 5403 | > <SPAN |
| 5404 | CLASS="emphasis" |
| 5405 | ><I |
| 5406 | CLASS="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 |
| 5412 | taken accordingly. The action to be taken is explained in the man page as, </P |
| 5413 | ><PRE |
| 5414 | CLASS="PROGRAMLISTING" |
| 5415 | ><SPAN |
| 5416 | CLASS="emphasis" |
| 5417 | ><I |
| 5418 | CLASS="EMPHASIS" |
| 5419 | > If the second argument is the KEY_MOUSE special key, the |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 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, |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 |
| 5438 | examples whenever appropriate.</P |
| 5439 | ></DIV |
| 5440 | ><DIV |
| 5441 | CLASS="SECT2" |
| 5442 | ><HR><H3 |
| 5443 | CLASS="SECT2" |
| 5444 | ><A |
| 5445 | NAME="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 |
| 5450 | displays any title or border associated with the menu. The menu sub window |
| 5451 | displays the menu items currently available for selection. But we didn't specify |
| 5452 | any window or sub window in the simple example. When a window is not specified, |
| 5453 | stdscr is taken as the main window, and then menu system calculates the sub |
| 5454 | window size required for the display of items. Then items are displayed in the |
| 5455 | calculated sub window. So let's play with these windows and display a menu with |
| 5456 | a border and a title.</P |
| 5457 | ><DIV |
| 5458 | CLASS="EXAMPLE" |
| 5459 | ><A |
| 5460 | NAME="MMEWI" |
| 5461 | ></A |
| 5462 | ><P |
| 5463 | ><B |
| 5464 | >Example 19. Menu Windows Usage example </B |
| 5465 | ></P |
| 5466 | ><PRE |
| 5467 | CLASS="PROGRAMLISTING" |
| 5468 | ><SPAN |
| 5469 | CLASS="INLINEMEDIAOBJECT" |
| 5470 | >#include <menu.h> |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5471 | |
| 5472 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5473 | #define CTRLD 4 |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5474 | |
| 5475 | char *choices[] = { |
| 5476 | "Choice 1", |
| 5477 | "Choice 2", |
| 5478 | "Choice 3", |
| 5479 | "Choice 4", |
| 5480 | "Exit", |
| 5481 | (char *)NULL, |
| 5482 | }; |
| 5483 | void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); |
| 5484 | |
| 5485 | int main() |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5486 | { ITEM **my_items; |
| 5487 | int c; |
| 5488 | MENU *my_menu; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5489 | WINDOW *my_menu_win; |
| 5490 | int n_choices, i; |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5491 | |
| 5492 | /* Initialize curses */ |
| 5493 | initscr(); |
| 5494 | start_color(); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5495 | cbreak(); |
| 5496 | noecho(); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5497 | keypad(stdscr, TRUE); |
| 5498 | init_pair(1, COLOR_RED, COLOR_BLACK); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5499 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5500 | /* Create items */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5501 | n_choices = ARRAY_SIZE(choices); |
| 5502 | my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *)); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5503 | for(i = 0; i < n_choices; ++i) |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5504 | my_items[i] = new_item(choices[i], choices[i]); |
| 5505 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5506 | /* Crate menu */ |
| 5507 | my_menu = new_menu((ITEM **)my_items); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5508 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5509 | /* Create the window to be associated with the menu */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5510 | my_menu_win = newwin(10, 40, 4, 4); |
| 5511 | keypad(my_menu_win, TRUE); |
| 5512 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5513 | /* Set main window and sub window */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5514 | set_menu_win(my_menu, my_menu_win); |
| 5515 | set_menu_sub(my_menu, derwin(my_menu_win, 6, 38, 3, 1)); |
| 5516 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5517 | /* Set menu mark to the string " * " */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5518 | set_menu_mark(my_menu, " * "); |
| 5519 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5520 | /* Print a border around the main window and print a title */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5521 | box(my_menu_win, 0, 0); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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(); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5528 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5529 | /* Post the menu */ |
| 5530 | post_menu(my_menu); |
| 5531 | wrefresh(my_menu_win); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5532 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 | } |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5542 | wrefresh(my_menu_win); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5543 | } |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5544 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5545 | /* Unpost and free all the memory taken up */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5546 | unpost_menu(my_menu); |
| 5547 | free_menu(my_menu); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5548 | for(i = 0; i < n_choices; ++i) |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5549 | free_item(my_items[i]); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5550 | endwin(); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5551 | } |
| 5552 | |
| 5553 | void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5554 | { int length, x, y; |
| 5555 | float temp; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5556 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5566 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 |
| 5579 | and the items. As you can see, in order to attach a window to a menu the |
| 5580 | function set_menu_win() has to be used. Then we attach the sub window also. This |
| 5581 | displays the items in the sub window. You can also set the mark string which |
| 5582 | gets displayed to the left of the selected item with set_menu_mark().</P |
| 5583 | ></DIV |
| 5584 | ><DIV |
| 5585 | CLASS="SECT2" |
| 5586 | ><HR><H3 |
| 5587 | CLASS="SECT2" |
| 5588 | ><A |
| 5589 | NAME="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, |
| 5594 | then the menu will be scrollable. When you are on the last item in the present |
| 5595 | list, if you send REQ_DOWN_ITEM, it gets translated into REQ_SCR_DLINE and the |
| 5596 | menu scrolls by one item. You can manually give REQ_SCR_ operations to do |
| 5597 | scrolling. Let's see how it can be done.</P |
| 5598 | ><DIV |
| 5599 | CLASS="EXAMPLE" |
| 5600 | ><A |
| 5601 | NAME="MMESC" |
| 5602 | ></A |
| 5603 | ><P |
| 5604 | ><B |
| 5605 | >Example 20. Scrolling Menus example </B |
| 5606 | ></P |
| 5607 | ><PRE |
| 5608 | CLASS="PROGRAMLISTING" |
| 5609 | ><SPAN |
| 5610 | CLASS="INLINEMEDIAOBJECT" |
| 5611 | >#include <curses.h> |
| 5612 | #include <menu.h> |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5613 | |
| 5614 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5615 | #define CTRLD 4 |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5616 | |
| 5617 | char *choices[] = { |
| 5618 | "Choice 1", |
| 5619 | "Choice 2", |
| 5620 | "Choice 3", |
| 5621 | "Choice 4", |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5622 | "Choice 5", |
| 5623 | "Choice 6", |
| 5624 | "Choice 7", |
| 5625 | "Choice 8", |
| 5626 | "Choice 9", |
| 5627 | "Choice 10", |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5628 | "Exit", |
| 5629 | (char *)NULL, |
| 5630 | }; |
| 5631 | void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); |
| 5632 | |
| 5633 | int main() |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5634 | { ITEM **my_items; |
| 5635 | int c; |
| 5636 | MENU *my_menu; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5637 | WINDOW *my_menu_win; |
| 5638 | int n_choices, i; |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5639 | |
| 5640 | /* Initialize curses */ |
| 5641 | initscr(); |
| 5642 | start_color(); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5643 | cbreak(); |
| 5644 | noecho(); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5645 | keypad(stdscr, TRUE); |
| 5646 | init_pair(1, COLOR_RED, COLOR_BLACK); |
| 5647 | init_pair(2, COLOR_CYAN, COLOR_BLACK); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5648 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5649 | /* Create items */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5650 | n_choices = ARRAY_SIZE(choices); |
| 5651 | my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *)); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5652 | for(i = 0; i < n_choices; ++i) |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5653 | my_items[i] = new_item(choices[i], choices[i]); |
| 5654 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5655 | /* Crate menu */ |
| 5656 | my_menu = new_menu((ITEM **)my_items); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5657 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5658 | /* Create the window to be associated with the menu */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5659 | my_menu_win = newwin(10, 40, 4, 4); |
| 5660 | keypad(my_menu_win, TRUE); |
| 5661 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5662 | /* Set main window and sub window */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5663 | set_menu_win(my_menu, my_menu_win); |
| 5664 | set_menu_sub(my_menu, derwin(my_menu_win, 6, 38, 3, 1)); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5665 | set_menu_format(my_menu, 5, 1); |
| 5666 | |
| 5667 | /* Set menu mark to the string " * " */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5668 | set_menu_mark(my_menu, " * "); |
| 5669 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5670 | /* Print a border around the main window and print a title */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5671 | box(my_menu_win, 0, 0); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5676 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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(); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5686 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 | } |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5702 | wrefresh(my_menu_win); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5703 | } |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5704 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5705 | /* Unpost and free all the memory taken up */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5706 | unpost_menu(my_menu); |
| 5707 | free_menu(my_menu); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5708 | for(i = 0; i < n_choices; ++i) |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5709 | free_item(my_items[i]); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5710 | endwin(); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5711 | } |
| 5712 | |
| 5713 | void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5714 | { int length, x, y; |
| 5715 | float temp; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5716 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5726 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 |
| 5739 | increased to ten, which is larger than our sub window size which can hold 6 |
| 5740 | items. This message has to be explicitly conveyed to the menu system with the |
| 5741 | function set_menu_format(). In here we specify the number of rows and columns we |
| 5742 | want to be displayed for a single page. We can specify any number of items to be |
| 5743 | shown, in the rows variables, if it is less than the height of the sub window. |
| 5744 | If the key pressed by the user is a PAGE UP or PAGE DOWN, the menu is scrolled a |
| 5745 | page due to the requests (REQ_SCR_DPAGE and REQ_SCR_UPAGE) given to |
| 5746 | menu_driver().</P |
| 5747 | ></DIV |
| 5748 | ><DIV |
| 5749 | CLASS="SECT2" |
| 5750 | ><HR><H3 |
| 5751 | CLASS="SECT2" |
| 5752 | ><A |
| 5753 | NAME="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 |
| 5758 | didn't mention what the cols variable (third parameter) does. Well, If your sub |
| 5759 | window is wide enough, you can opt to display more than one item per row. This |
| 5760 | can be specified in the cols variable. To make things simpler, the following |
| 5761 | example doesn't show descriptions for the items.</P |
| 5762 | ><DIV |
| 5763 | CLASS="EXAMPLE" |
| 5764 | ><A |
| 5765 | NAME="MMEMUCO" |
| 5766 | ></A |
| 5767 | ><P |
| 5768 | ><B |
| 5769 | >Example 21. Milt Columnar Menus Example </B |
| 5770 | ></P |
| 5771 | ><PRE |
| 5772 | CLASS="PROGRAMLISTING" |
| 5773 | ><SPAN |
| 5774 | CLASS="INLINEMEDIAOBJECT" |
| 5775 | >#include <curses.h> |
| 5776 | #include <menu.h> |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5777 | |
| 5778 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5779 | #define CTRLD 4 |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5780 | |
| 5781 | char *choices[] = { |
| 5782 | "Choice 1", "Choice 2", "Choice 3", "Choice 4", "Choice 5", |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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", |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5786 | "Exit", |
| 5787 | (char *)NULL, |
| 5788 | }; |
| 5789 | |
| 5790 | int main() |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5791 | { ITEM **my_items; |
| 5792 | int c; |
| 5793 | MENU *my_menu; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5794 | WINDOW *my_menu_win; |
| 5795 | int n_choices, i; |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5796 | |
| 5797 | /* Initialize curses */ |
| 5798 | initscr(); |
| 5799 | start_color(); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5800 | cbreak(); |
| 5801 | noecho(); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5802 | keypad(stdscr, TRUE); |
| 5803 | init_pair(1, COLOR_RED, COLOR_BLACK); |
| 5804 | init_pair(2, COLOR_CYAN, COLOR_BLACK); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5805 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5806 | /* Create items */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5807 | n_choices = ARRAY_SIZE(choices); |
| 5808 | my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *)); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5809 | for(i = 0; i < n_choices; ++i) |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5810 | my_items[i] = new_item(choices[i], choices[i]); |
| 5811 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5812 | /* Crate menu */ |
| 5813 | my_menu = new_menu((ITEM **)my_items); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5814 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5815 | /* Set menu option not to show the description */ |
| 5816 | menu_opts_off(my_menu, O_SHOWDESC); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5817 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5818 | /* Create the window to be associated with the menu */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5819 | my_menu_win = newwin(10, 70, 4, 4); |
| 5820 | keypad(my_menu_win, TRUE); |
| 5821 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5822 | /* Set main window and sub window */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5823 | set_menu_win(my_menu, my_menu_win); |
| 5824 | set_menu_sub(my_menu, derwin(my_menu_win, 6, 68, 3, 1)); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5825 | set_menu_format(my_menu, 5, 3); |
| 5826 | set_menu_mark(my_menu, " * "); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5827 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5828 | /* Print a border around the main window and print a title */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5829 | box(my_menu_win, 0, 0); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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(); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5836 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 | } |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5862 | wrefresh(my_menu_win); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5863 | } |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5864 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5865 | /* Unpost and free all the memory taken up */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5866 | unpost_menu(my_menu); |
| 5867 | free_menu(my_menu); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5868 | for(i = 0; i < n_choices; ++i) |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5869 | free_item(my_items[i]); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 |
| 5876 | to be 3, thus displaying 3 items per row. We have also switched off the showing |
| 5877 | descriptions with the function menu_opts_off(). There are couple of functions |
| 5878 | set_menu_opts(), menu_opts_on() and menu_opts() which can be used to manipulate |
| 5879 | menu options. The following menu options can be specified.</P |
| 5880 | ><PRE |
| 5881 | CLASS="PROGRAMLISTING" |
| 5882 | > O_ONEVALUE |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 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, |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 |
| 5904 | menu_opts_on() and menu_opts_off() functions. You can also use set_menu_opts() |
| 5905 | to directly specify the options. The argument to this function should be a OR ed |
| 5906 | value of some of those above constants. The function menu_opts() can be used to |
| 5907 | find out a menu's present options. </P |
| 5908 | ></DIV |
| 5909 | ><DIV |
| 5910 | CLASS="SECT2" |
| 5911 | ><HR><H3 |
| 5912 | CLASS="SECT2" |
| 5913 | ><A |
| 5914 | NAME="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 |
| 5919 | menu becomes multi-valued. That means you can select more than one item. This |
| 5920 | brings us to the request REQ_TOGGLE_ITEM. Let's see it in action.</P |
| 5921 | ><DIV |
| 5922 | CLASS="EXAMPLE" |
| 5923 | ><A |
| 5924 | NAME="MMETO" |
| 5925 | ></A |
| 5926 | ><P |
| 5927 | ><B |
| 5928 | >Example 22. Multi Valued Menus example </B |
| 5929 | ></P |
| 5930 | ><PRE |
| 5931 | CLASS="PROGRAMLISTING" |
| 5932 | ><SPAN |
| 5933 | CLASS="INLINEMEDIAOBJECT" |
| 5934 | >#include <curses.h> |
| 5935 | #include <menu.h> |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5936 | |
| 5937 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5938 | #define CTRLD 4 |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5939 | |
| 5940 | char *choices[] = { |
| 5941 | "Choice 1", |
| 5942 | "Choice 2", |
| 5943 | "Choice 3", |
| 5944 | "Choice 4", |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5945 | "Choice 5", |
| 5946 | "Choice 6", |
| 5947 | "Choice 7", |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5948 | "Exit", |
| 5949 | }; |
| 5950 | |
| 5951 | int main() |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5952 | { ITEM **my_items; |
| 5953 | int c; |
| 5954 | MENU *my_menu; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5955 | int n_choices, i; |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5956 | ITEM *cur_item; |
| 5957 | |
| 5958 | /* Initialize curses */ |
| 5959 | initscr(); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5960 | cbreak(); |
| 5961 | noecho(); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5962 | keypad(stdscr, TRUE); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5963 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5964 | /* Initialize items */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5965 | n_choices = ARRAY_SIZE(choices); |
| 5966 | my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5967 | for(i = 0; i < n_choices; ++i) |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5968 | my_items[i] = new_item(choices[i], choices[i]); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5969 | my_items[n_choices] = (ITEM *)NULL; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5970 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5971 | my_menu = new_menu((ITEM **)my_items); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5972 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 5973 | /* Make the menu multi valued */ |
| 5974 | menu_opts_off(my_menu, O_ONEVALUE); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5975 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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(); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5980 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 5995 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 | } |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 6011 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 6012 | free_item(my_items[0]); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 6013 | free_item(my_items[1]); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 6014 | free_menu(my_menu); |
| 6015 | endwin(); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 6016 | } |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 6017 | </SPAN |
| 6018 | ></PRE |
| 6019 | ></DIV |
| 6020 | ><P |
| 6021 | >Whew, A lot of new functions. Let's take them one after another. Firstly, the |
| 6022 | REQ_TOGGLE_ITEM. In a multi-valued menu, the user should be allowed to select |
| 6023 | or un select more than one item. The request REQ_TOGGLE_ITEM toggles the present |
| 6024 | selection. In this case when space is pressed REQ_TOGGLE_ITEM request is sent to |
| 6025 | menu_driver to achieve the result.</P |
| 6026 | ><P |
| 6027 | >Now when the user presses <ENTER> we show the items he presently selected. |
| 6028 | First we find out the items associated with the menu using the function |
| 6029 | menu_items(). Then we loop through the items to find out if the item is selected |
| 6030 | or not. The function item_value() returns TRUE if an item is selected. The |
| 6031 | function item_count() returns the number of items in the menu. The item name can |
| 6032 | be found with item_name(). You can also find the description associated with an |
| 6033 | item using item_description().</P |
| 6034 | ></DIV |
| 6035 | ><DIV |
| 6036 | CLASS="SECT2" |
| 6037 | ><HR><H3 |
| 6038 | CLASS="SECT2" |
| 6039 | ><A |
| 6040 | NAME="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 |
| 6045 | lots of functionality. I know. You want Colors !!!. You want to create nice |
| 6046 | menus similar to those text mode <A |
| 6047 | HREF="http://www.jersey.net/~debinjoe/games/" |
| 6048 | TARGET="_top" |
| 6049 | >dos games</A |
| 6050 | >. The functions |
| 6051 | set_menu_fore() and set_menu_back() can be used to change the attribute of the |
| 6052 | selected item and unselected item. The names are misleading. They don't change |
| 6053 | menu'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 |
| 6056 | non-selectable items in the menu. This brings us to the interesting option for |
| 6057 | an item the one and only O_SELECTABLE. We can turn it off by the function |
| 6058 | item_opts_off() and after that that item is not selectable. It's like a grayed |
| 6059 | item in those fancy windows menus. Let's put these concepts in practice with |
| 6060 | this example</P |
| 6061 | ><DIV |
| 6062 | CLASS="EXAMPLE" |
| 6063 | ><A |
| 6064 | NAME="MMEAT" |
| 6065 | ></A |
| 6066 | ><P |
| 6067 | ><B |
| 6068 | >Example 23. Menu Options example </B |
| 6069 | ></P |
| 6070 | ><PRE |
| 6071 | CLASS="PROGRAMLISTING" |
| 6072 | ><SPAN |
| 6073 | CLASS="INLINEMEDIAOBJECT" |
| 6074 | >#include <menu.h> |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 6075 | |
| 6076 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 6077 | #define CTRLD 4 |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 6078 | |
| 6079 | char *choices[] = { |
| 6080 | "Choice 1", |
| 6081 | "Choice 2", |
| 6082 | "Choice 3", |
| 6083 | "Choice 4", |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 6084 | "Choice 5", |
| 6085 | "Choice 6", |
| 6086 | "Choice 7", |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 6087 | "Exit", |
| 6088 | }; |
| 6089 | |
| 6090 | int main() |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 6091 | { ITEM **my_items; |
| 6092 | int c; |
| 6093 | MENU *my_menu; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 6094 | int n_choices, i; |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 6095 | ITEM *cur_item; |
| 6096 | |
| 6097 | /* Initialize curses */ |
| 6098 | initscr(); |
| 6099 | start_color(); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 6100 | cbreak(); |
| 6101 | noecho(); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 6106 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 6107 | /* Initialize items */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 6108 | n_choices = ARRAY_SIZE(choices); |
| 6109 | my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 6110 | for(i = 0; i < n_choices; ++i) |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 6111 | my_items[i] = new_item(choices[i], choices[i]); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 6115 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 6116 | /* Create menu */ |
| 6117 | my_menu = new_menu((ITEM **)my_items); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 6118 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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)); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 6123 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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(); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 6129 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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(); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 6152 | } |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 6153 | </SPAN |
| 6154 | ></PRE |
| 6155 | ></DIV |
| 6156 | ></DIV |
| 6157 | ><DIV |
| 6158 | CLASS="SECT2" |
| 6159 | ><HR><H3 |
| 6160 | CLASS="SECT2" |
| 6161 | ><A |
| 6162 | NAME="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 |
| 6167 | way as user pointer in panels. It's not touched by menu system. You can store |
| 6168 | any thing you like in that. I usually use it to store the function to be |
| 6169 | executed when the menu option is chosen (It's selected and may be the user |
| 6170 | pressed <ENTER>);</P |
| 6171 | ><DIV |
| 6172 | CLASS="EXAMPLE" |
| 6173 | ><A |
| 6174 | NAME="MMEUS" |
| 6175 | ></A |
| 6176 | ><P |
| 6177 | ><B |
| 6178 | >Example 24. Menu User Pointer Usage </B |
| 6179 | ></P |
| 6180 | ><PRE |
| 6181 | CLASS="PROGRAMLISTING" |
| 6182 | ><SPAN |
| 6183 | CLASS="INLINEMEDIAOBJECT" |
| 6184 | >#include <curses.h> |
| 6185 | #include <menu.h> |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 6186 | |
| 6187 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 6188 | #define CTRLD 4 |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 6189 | |
| 6190 | char *choices[] = { |
| 6191 | "Choice 1", |
| 6192 | "Choice 2", |
| 6193 | "Choice 3", |
| 6194 | "Choice 4", |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 6195 | "Choice 5", |
| 6196 | "Choice 6", |
| 6197 | "Choice 7", |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 6198 | "Exit", |
| 6199 | }; |
| 6200 | void func(char *name); |
| 6201 | |
| 6202 | int main() |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 6203 | { ITEM **my_items; |
| 6204 | int c; |
| 6205 | MENU *my_menu; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 6206 | int n_choices, i; |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 6207 | ITEM *cur_item; |
| 6208 | |
| 6209 | /* Initialize curses */ |
| 6210 | initscr(); |
| 6211 | start_color(); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 6212 | cbreak(); |
| 6213 | noecho(); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 6218 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 6219 | /* Initialize items */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 6220 | n_choices = ARRAY_SIZE(choices); |
| 6221 | my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 6228 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 6229 | /* Create menu */ |
| 6230 | my_menu = new_menu((ITEM **)my_items); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 6231 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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(); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 6237 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 *); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 6249 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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(); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 6264 | } |
| 6265 | |
| 6266 | void func(char *name) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 |
| 6276 | CLASS="SECT1" |
| 6277 | ><HR><H2 |
| 6278 | CLASS="SECT1" |
| 6279 | ><A |
| 6280 | NAME="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 |
| 6285 | do various kinds of things, you might be wondering how would any one create such |
| 6286 | forms in text mode display. It's quite difficult to write those nifty forms in |
| 6287 | plain ncurses. Forms library tries to provide a basic frame work to build and |
| 6288 | maintain forms with ease. It has lot of features(functions) which manage |
| 6289 | validation, 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) |
| 6292 | or a data-entry location. The forms also library provides functions to divide |
| 6293 | forms into multiple pages. </P |
| 6294 | ><DIV |
| 6295 | CLASS="SECT2" |
| 6296 | ><HR><H3 |
| 6297 | CLASS="SECT2" |
| 6298 | ><A |
| 6299 | NAME="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 |
| 6304 | form are created with new_field(). You can set options for the fields, so that |
| 6305 | they can be displayed with some fancy attributes, validated before the field |
| 6306 | looses focus etc.. Then the fields are attached to form. After this, the form |
| 6307 | can be posted to display and is ready to receive inputs. On the similar lines to |
| 6308 | menu_driver(), the form is manipulated with form_driver(). We can send requests |
| 6309 | to form_driver to move focus to a certain field, move cursor to end of the field |
| 6310 | etc.. After the user enters values in the fields and validation done, form can |
| 6311 | be 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 |
| 6318 | TYPE="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 |
| 6326 | width 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 |
| 6331 | attached 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 |
| 6340 | to 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 |
| 6362 | library. The following examples will explore various aspects of form |
| 6363 | processing. Let's start the journey with a simple example. first.</P |
| 6364 | ></DIV |
| 6365 | ><DIV |
| 6366 | CLASS="SECT2" |
| 6367 | ><HR><H3 |
| 6368 | CLASS="SECT2" |
| 6369 | ><A |
| 6370 | NAME="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 |
| 6375 | program with forms library the flag -lform should be added along with -lncurses |
| 6376 | in that order.</P |
| 6377 | ><PRE |
| 6378 | CLASS="PROGRAMLISTING" |
| 6379 | > #include <form.h> |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 6380 | . |
| 6381 | . |
| 6382 | . |
| 6383 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 6384 | compile and link: gcc <program file> -lform -lncurses</PRE |
| 6385 | ><DIV |
| 6386 | CLASS="EXAMPLE" |
| 6387 | ><A |
| 6388 | NAME="FFOSI" |
| 6389 | ></A |
| 6390 | ><P |
| 6391 | ><B |
| 6392 | >Example 25. Forms Basics </B |
| 6393 | ></P |
| 6394 | ><PRE |
| 6395 | CLASS="PROGRAMLISTING" |
| 6396 | ><SPAN |
| 6397 | CLASS="INLINEMEDIAOBJECT" |
| 6398 | >#include <form.h> |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 6399 | |
| 6400 | int main() |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 6410 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 6415 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 6422 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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(); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 6431 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 | } |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 6454 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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]); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 6460 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 |
| 6469 | CLASS="LITERAL" |
| 6470 | >new_field()</TT |
| 6471 | >. new_field() takes height, width, |
| 6472 | starty, startx, number of offscreen rows and number of additional working |
| 6473 | buffers. The fifth argument number of offscreen rows specifies how much of the |
| 6474 | field to be shown. If it is zero, the entire field is always displayed otherwise |
| 6475 | the form will be scrollable when the user accesses not displayed parts of the |
| 6476 | field. The forms library allocates one buffer per field to store the data user |
| 6477 | enters. Using the last parameter to new_field() we can specify it to allocate |
| 6478 | some 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 |
| 6481 | underscore with set_field_back(). The AUTOSKIP option is turned off using |
| 6482 | field_opts_off(). If this option is turned on, focus will move to the next |
| 6483 | field 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 |
| 6486 | processed in the while loop, by making corresponding requests to form_driver. |
| 6487 | The details of all the requests to the form_driver() are explained later.</P |
| 6488 | ></DIV |
| 6489 | ><DIV |
| 6490 | CLASS="SECT2" |
| 6491 | ><HR><H3 |
| 6492 | CLASS="SECT2" |
| 6493 | ><A |
| 6494 | NAME="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 |
| 6499 | to get the required effect and to have fun !!!. So why wait? </P |
| 6500 | ><DIV |
| 6501 | CLASS="SECT3" |
| 6502 | ><HR><H4 |
| 6503 | CLASS="SECT3" |
| 6504 | ><A |
| 6505 | NAME="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 |
| 6510 | with field_info(). It returns height, width, starty, startx, number of offscreen |
| 6511 | rows, and number of additional buffers into the parameters given to it. It is a |
| 6512 | sort of inverse of new_field().</P |
| 6513 | ><PRE |
| 6514 | CLASS="PROGRAMLISTING" |
| 6515 | >int field_info( FIELD *field, /* field from which to fetch */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 6516 | int *height, *int width, /* field size */ |
| 6517 | int *top, int *left, /* upper left corner */ |
| 6518 | int *offscreen, /* number of offscreen rows */ |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 6519 | int *nbuf); /* number of working buffers */</PRE |
| 6520 | ></DIV |
| 6521 | ><DIV |
| 6522 | CLASS="SECT3" |
| 6523 | ><HR><H4 |
| 6524 | CLASS="SECT3" |
| 6525 | ><A |
| 6526 | NAME="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 |
| 6531 | move_field().</P |
| 6532 | ><PRE |
| 6533 | CLASS="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 |
| 6540 | CLASS="SECT3" |
| 6541 | ><HR><H4 |
| 6542 | CLASS="SECT3" |
| 6543 | ><A |
| 6544 | NAME="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 |
| 6549 | set_field_just().</P |
| 6550 | ><PRE |
| 6551 | CLASS="PROGRAMLISTING" |
| 6552 | > int set_field_just(FIELD *field, /* field to alter */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 6553 | int justmode); /* mode to set */ |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 |
| 6557 | NO_JUSTIFICATION, JUSTIFY_RIGHT, JUSTIFY_LEFT, or JUSTIFY_CENTER.</P |
| 6558 | ></DIV |
| 6559 | ><DIV |
| 6560 | CLASS="SECT3" |
| 6561 | ><HR><H4 |
| 6562 | CLASS="SECT3" |
| 6563 | ><A |
| 6564 | NAME="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 |
| 6569 | set with set_field_fore() and setfield_back(). These functions set foreground |
| 6570 | and background attribute of the fields. You can also specify a pad character |
| 6571 | which will be filled in the unfilled portion of the field. The pad character is |
| 6572 | set with a call to set_field_pad(). Default pad value is a space. The functions |
| 6573 | field_fore(), field_back, field_pad() can be used to query the present |
| 6574 | foreground, background attributes and pad character for the field. The following |
| 6575 | list gives the usage of functions.</P |
| 6576 | ><PRE |
| 6577 | CLASS="PROGRAMLISTING" |
| 6578 | > int set_field_fore(FIELD *field, /* field to alter */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 6579 | chtype attr); /* attribute to set */ |
| 6580 | |
| 6581 | chtype field_fore(FIELD *field); /* field to query */ |
| 6582 | /* returns foreground attribute */ |
| 6583 | |
| 6584 | int set_field_back(FIELD *field, /* field to alter */ |
| 6585 | chtype attr); /* attribute to set */ |
| 6586 | |
| 6587 | chtype field_back(FIELD *field); /* field to query */ |
| 6588 | /* returns background attribute */ |
| 6589 | |
| 6590 | int set_field_pad(FIELD *field, /* field to alter */ |
| 6591 | int pad); /* pad character to set */ |
| 6592 | |
| 6593 | chtype field_pad(FIELD *field); /* field to query */ |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 6594 | /* returns present pad character */ </PRE |
| 6595 | ><P |
| 6596 | >Though above functions seem quite simple, using colors with set_field_fore() may |
| 6597 | be frustrating in the beginning. Let me first explain about foreground and |
| 6598 | background attributes of a field. The foreground attribute is associated with |
| 6599 | the character. That means a character in the field is printed with the attribute |
| 6600 | you have set with set_field_fore(). Background attribute is the attribute used |
| 6601 | to fill background of field, whether any character is there or not. So what |
| 6602 | about colors? Since colors are always defined in pairs, what is the right way to |
| 6603 | display colored fields? Here's an example clarifying color attributes.</P |
| 6604 | ><DIV |
| 6605 | CLASS="EXAMPLE" |
| 6606 | ><A |
| 6607 | NAME="FFOAT" |
| 6608 | ></A |
| 6609 | ><P |
| 6610 | ><B |
| 6611 | >Example 26. Form Attributes example </B |
| 6612 | ></P |
| 6613 | ><PRE |
| 6614 | CLASS="PROGRAMLISTING" |
| 6615 | ><SPAN |
| 6616 | CLASS="INLINEMEDIAOBJECT" |
| 6617 | >#include <form.h> |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 6618 | |
| 6619 | int main() |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 6630 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 6631 | /* Initialize few color pairs */ |
| 6632 | init_pair(1, COLOR_WHITE, COLOR_BLUE); |
| 6633 | init_pair(2, COLOR_WHITE, COLOR_BLUE); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 6634 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 6639 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 6648 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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(); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 6659 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 | } |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 6682 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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]); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 6688 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 |
| 6696 | attributes. In my programs using color attributes, I usually set only the |
| 6697 | background with set_field_back(). Curses simply doesn't allow defining |
| 6698 | individual color attributes. </P |
| 6699 | ></DIV |
| 6700 | ><DIV |
| 6701 | CLASS="SECT3" |
| 6702 | ><HR><H4 |
| 6703 | CLASS="SECT3" |
| 6704 | ><A |
| 6705 | NAME="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 |
| 6710 | various aspects of forms processing. You can manipulate them with these |
| 6711 | functions:</P |
| 6712 | ><PRE |
| 6713 | CLASS="PROGRAMLISTING" |
| 6714 | >int set_field_opts(FIELD *field, /* field to alter */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 6715 | int attr); /* attribute to set */ |
| 6716 | |
| 6717 | int field_opts_on(FIELD *field, /* field to alter */ |
| 6718 | int attr); /* attributes to turn on */ |
| 6719 | |
| 6720 | int field_opts_off(FIELD *field, /* field to alter */ |
| 6721 | int attr); /* attributes to turn off */ |
| 6722 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 6723 | int 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 |
| 6726 | or you can choose to switch a few attributes on and off with field_opts_on() and |
| 6727 | field_opts_off() selectively. Anytime you can query the attributes of a field |
| 6728 | with field_opts(). The following is the list of available options. By default, |
| 6729 | all options are on.</P |
| 6730 | ><P |
| 6731 | ></P |
| 6732 | ><DIV |
| 6733 | CLASS="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 |
| 6740 | during form processing to hide or pop up fields depending on the value |
| 6741 | of 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. |
| 6748 | visited by form navigation keys). Can be used to make labels or derived |
| 6749 | fields 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 |
| 6756 | turned off on a field, the library will accept and edit data in that field, |
| 6757 | but it will not be displayed and the visible field cursor will not move. |
| 6758 | You 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 |
| 6765 | off, all editing requests except <TT |
| 6766 | CLASS="LITERAL" |
| 6767 | >REQ_PREV_CHOICE</TT |
| 6768 | > and <TT |
| 6769 | CLASS="LITERAL" |
| 6770 | >REQ_NEXT_CHOICE</TT |
| 6771 | >will |
| 6772 | fail. 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 |
| 6779 | character of a (blank-separated) word reaches the end of the current line, the |
| 6780 | entire word is wrapped to the next line (assuming there is one). When this |
| 6781 | option 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 |
| 6788 | the first field position erases the entire field (except for the just-entered |
| 6789 | character).</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, |
| 6796 | when the forms user tries to type more data into a field than will fit, |
| 6797 | the editing location jumps to next field. When this option is off, the |
| 6798 | user's cursor will hang at the end of the field. This option is ignored |
| 6799 | in 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 |
| 6806 | blank fields. Normally, it is not; the user can leave a field blank |
| 6807 | without invoking the usual validation check on exit. If this option is |
| 6808 | off 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 |
| 6815 | the field is modified. Normally the latter is true. Setting O_PASSOK |
| 6816 | may be useful if your field's validation function may change during |
| 6817 | forms 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 |
| 6824 | turn this off, the field becomes dynamic and will |
| 6825 | stretch 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. |
| 6831 | However, 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 |
| 6834 | the obvious way. You have seen the usage of switching off O_AUTOSKIP option. |
| 6835 | The following example clarifies usage of some more options. Other options |
| 6836 | are explained where appropriate.</P |
| 6837 | ><DIV |
| 6838 | CLASS="EXAMPLE" |
| 6839 | ><A |
| 6840 | NAME="FFOOP" |
| 6841 | ></A |
| 6842 | ><P |
| 6843 | ><B |
| 6844 | >Example 27. Field Options Usage example </B |
| 6845 | ></P |
| 6846 | ><PRE |
| 6847 | CLASS="PROGRAMLISTING" |
| 6848 | ><SPAN |
| 6849 | CLASS="INLINEMEDIAOBJECT" |
| 6850 | >#include <form.h> |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 6851 | |
| 6852 | #define STARTX 15 |
| 6853 | #define STARTY 4 |
| 6854 | #define WIDTH 25 |
| 6855 | |
| 6856 | #define N_FIELDS 3 |
| 6857 | |
| 6858 | int main() |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 6868 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 6873 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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(); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 6893 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 | } |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 6916 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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]); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 6922 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 |
| 6930 | can present information very effectively in a form. The second field being not |
| 6931 | O_PUBLIC, does not show the characters you are typing.</P |
| 6932 | ></DIV |
| 6933 | ><DIV |
| 6934 | CLASS="SECT3" |
| 6935 | ><HR><H4 |
| 6936 | CLASS="SECT3" |
| 6937 | ><A |
| 6938 | NAME="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 |
| 6943 | initially set to FALSE and when user enters something and the data buffer gets |
| 6944 | modified it becomes TRUE. So a field's status can be queried to find out whether |
| 6945 | it has been modified or not. The following functions can assist in those |
| 6946 | operations.</P |
| 6947 | ><PRE |
| 6948 | CLASS="PROGRAMLISTING" |
| 6949 | >int set_field_status(FIELD *field, /* field to alter */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 6950 | int status); /* status to set */ |
| 6951 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 6952 | int 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 |
| 6955 | data buffer might not have been updated yet as the validation is still due. To |
| 6956 | guarantee that right status is returned, call field_status() either (1) in the |
| 6957 | field's exit validation check routine, (2) from the field's or form's |
| 6958 | initialization or termination hooks, or (3) just after a REQ_VALIDATION request |
| 6959 | has been processed by the forms driver</P |
| 6960 | ></DIV |
| 6961 | ><DIV |
| 6962 | CLASS="SECT3" |
| 6963 | ><HR><H4 |
| 6964 | CLASS="SECT3" |
| 6965 | ><A |
| 6966 | NAME="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 |
| 6971 | various purposes. It is not touched by forms library and can be used for any |
| 6972 | purpose by the user. The following functions set and fetch user pointer.</P |
| 6973 | ><PRE |
| 6974 | CLASS="PROGRAMLISTING" |
| 6975 | >int set_field_userptr(FIELD *field, |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 6976 | char *userptr); /* the user pointer you wish to associate */ |
| 6977 | /* with the field */ |
| 6978 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 6979 | char *field_userptr(FIELD *field); /* fetch user pointer of the field */</PRE |
| 6980 | ></DIV |
| 6981 | ><DIV |
| 6982 | CLASS="SECT3" |
| 6983 | ><HR><H4 |
| 6984 | CLASS="SECT3" |
| 6985 | ><A |
| 6986 | NAME="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 |
| 6991 | feature you want to put to full use. This will allow the user to enter more data |
| 6992 | than the original size of the field and let the field grow. According to the |
| 6993 | field orientation it will scroll horizontally or vertically to incorporate the |
| 6994 | new data.</P |
| 6995 | ><P |
| 6996 | >To make a field dynamically growable, the option O_STATIC should be turned off. |
| 6997 | This can be done with a |
| 6998 | <PRE |
| 6999 | CLASS="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 |
| 7004 | a maximum limit to the growth of the field with |
| 7005 | <PRE |
| 7006 | CLASS="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 |
| 7013 | CLASS="PROGRAMLISTING" |
| 7014 | >int dynamic_field_info( FIELD *field, /* Field on which to operate */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 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 */ |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 7018 | /* in this */</PRE |
| 7019 | > |
| 7020 | Though field_info work as usual, it is advisable to use this function to get the |
| 7021 | proper 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 |
| 7024 | will be defined to be a one line field. A new field created with height greater |
| 7025 | than 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 |
| 7028 | contain a single fixed row, but the number of columns can increase if the user |
| 7029 | enters more data than the initial field will hold. The number of columns |
| 7030 | displayed 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 |
| 7033 | contain a fixed number of columns, but the number of rows can increase if the |
| 7034 | user enters more data than the initial field will hold. The number of rows |
| 7035 | displayed 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 |
| 7038 | behavior. The way other parts of forms library behaves is described below:</P |
| 7039 | ><P |
| 7040 | ></P |
| 7041 | ><OL |
| 7042 | TYPE="1" |
| 7043 | ><LI |
| 7044 | ><P |
| 7045 | >The field option O_AUTOSKIP will be ignored if the option O_STATIC is off and |
| 7046 | there is no maximum growth specified for the field. Currently, O_AUTOSKIP |
| 7047 | generates an automatic REQ_NEXT_FIELD form driver request when the user types in |
| 7048 | the last character position of a field. On a growable field with no maximum |
| 7049 | growth specified, there is no last character position. If a maximum growth is |
| 7050 | specified, the O_AUTOSKIP option will work as normal if the field has grown to |
| 7051 | its maximum size. </P |
| 7052 | ></LI |
| 7053 | ><LI |
| 7054 | ><P |
| 7055 | >The field justification will be ignored if the option O_STATIC is off. |
| 7056 | Currently, set_field_just can be used to JUSTIFY_LEFT, JUSTIFY_RIGHT, |
| 7057 | JUSTIFY_CENTER the contents of a one line field. A growable one line field will, |
| 7058 | by definition, grow and scroll horizontally and may contain more data than can |
| 7059 | be 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 |
| 7064 | regardless of the O_NL_OVERLOAD form option if the field option O_STATIC is off |
| 7065 | and there is no maximum growth specified for the field. Currently, if the form |
| 7066 | option O_NL_OVERLOAD is on, REQ_NEW_LINE implicitly generates a REQ_NEXT_FIELD |
| 7067 | if called from the last line of a field. If a field can grow without bound, |
| 7068 | there is no last line, so REQ_NEW_LINE will never implicitly generate a |
| 7069 | REQ_NEXT_FIELD. If a maximum growth limit is specified and the O_NL_OVERLOAD |
| 7070 | form option is on, REQ_NEW_LINE will only implicitly generate REQ_NEXT_FIELD if |
| 7071 | the 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, |
| 7076 | including the current buffer size and contents of the field being duplicated. |
| 7077 | Any 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 |
| 7082 | attributes and share buffers with the field being linked. If the O_STATIC field |
| 7083 | option is subsequently changed by a field sharing buffers, how the system reacts |
| 7084 | to an attempt to enter more data into the field than the buffer will currently |
| 7085 | hold 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 |
| 7090 | the value of the original call to new_field. The user should use |
| 7091 | dynamic_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 |
| 7096 | be looking into that in next few sections.</P |
| 7097 | ></DIV |
| 7098 | ></DIV |
| 7099 | ><DIV |
| 7100 | CLASS="SECT2" |
| 7101 | ><HR><H3 |
| 7102 | CLASS="SECT2" |
| 7103 | ><A |
| 7104 | NAME="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 |
| 7109 | associated with a main window and a sub window. The form main window displays |
| 7110 | any title or border associated or whatever the user wishes. Then the sub window |
| 7111 | contains all the fields and displays them according to their position. This |
| 7112 | gives 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 |
| 7115 | with out much explanation. The functions are similar and they work the same way.</P |
| 7116 | ><DIV |
| 7117 | CLASS="EXAMPLE" |
| 7118 | ><A |
| 7119 | NAME="FFOWI" |
| 7120 | ></A |
| 7121 | ><P |
| 7122 | ><B |
| 7123 | >Example 28. Form Windows Example </B |
| 7124 | ></P |
| 7125 | ><PRE |
| 7126 | CLASS="PROGRAMLISTING" |
| 7127 | ><SPAN |
| 7128 | CLASS="INLINEMEDIAOBJECT" |
| 7129 | >#include <form.h> |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 7130 | |
| 7131 | void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); |
| 7132 | |
| 7133 | int main() |
| 7134 | { |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 7146 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 7147 | /* Initialize few color pairs */ |
| 7148 | init_pair(1, COLOR_RED, COLOR_BLACK); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 7149 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 7154 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 7167 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 7168 | /* Create the window to be associated with the form */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 7169 | my_form_win = newwin(rows + 4, cols + 4, 4, 4); |
| 7170 | keypad(my_form_win, TRUE); |
| 7171 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 7172 | /* Set main window and sub window */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 7173 | set_form_win(my_form, my_form_win); |
| 7174 | set_form_sub(my_form, derwin(my_form_win, rows, cols, 2, 2)); |
| 7175 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 7176 | /* Print a border around the main window and print a title */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 7177 | box(my_form_win, 0, 0); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 7182 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 7183 | mvprintw(LINES - 2, 0, "Use UP, DOWN arrow keys to switch between fields"); |
| 7184 | refresh(); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 7185 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 | } |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 7208 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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]); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 7214 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 7215 | endwin(); |
| 7216 | return 0; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 7217 | } |
| 7218 | |
| 7219 | void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 7220 | { int length, x, y; |
| 7221 | float temp; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 7222 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 7232 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 |
| 7245 | CLASS="SECT2" |
| 7246 | ><HR><H3 |
| 7247 | CLASS="SECT2" |
| 7248 | ><A |
| 7249 | NAME="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 |
| 7254 | attach validation to the field. Then any attempt by the user to leave the field, |
| 7255 | while it contains data that doesn't match the validation type will fail. Some |
| 7256 | validation types also have a character-validity check for each time a character |
| 7257 | is entered in the field.</P |
| 7258 | ><P |
| 7259 | >Validation can be attached to a field with the following function. |
| 7260 | <PRE |
| 7261 | CLASS="PROGRAMLISTING" |
| 7262 | >int set_field_type(FIELD *field, /* field to alter */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 7263 | FIELDTYPE *ftype, /* type to associate */ |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 7264 | ...); /* additional arguments*/</PRE |
| 7265 | > |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 7266 | Once set, the validation type for a field can be queried with |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 7267 | <PRE |
| 7268 | CLASS="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 |
| 7273 | end-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 |
| 7284 | they are linked</P |
| 7285 | ></LI |
| 7286 | ></UL |
| 7287 | ><P |
| 7288 | >The following are the pre-defined validation types. You can also specify custom |
| 7289 | validation, though it's a bit tricky and cumbersome.</P |
| 7290 | ><H1 |
| 7291 | CLASS="BRIDGEHEAD" |
| 7292 | ><A |
| 7293 | NAME="AEN1069" |
| 7294 | ></A |
| 7295 | >TYPE_ALPHA</H1 |
| 7296 | ><P |
| 7297 | >This field type accepts alphabetic data; no blanks, no digits, no special |
| 7298 | characters (this is checked at character-entry time). It is set up with: </P |
| 7299 | ><PRE |
| 7300 | CLASS="PROGRAMLISTING" |
| 7301 | >int set_field_type(FIELD *field, /* field to alter */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 7302 | TYPE_ALPHA, /* type to associate */ |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 |
| 7306 | width number of characters before he can leave the field. Typically |
| 7307 | you'll want to set this to the field width; if it's greater than the |
| 7308 | field width, the validation check will always fail. A minimum width |
| 7309 | of zero makes field completion optional. </P |
| 7310 | ><H1 |
| 7311 | CLASS="BRIDGEHEAD" |
| 7312 | ><A |
| 7313 | NAME="AEN1073" |
| 7314 | ></A |
| 7315 | >TYPE_ALNUM</H1 |
| 7316 | ><P |
| 7317 | >This field type accepts alphabetic data and digits; no blanks, no special |
| 7318 | characters (this is checked at character-entry time). It is set up with: </P |
| 7319 | ><PRE |
| 7320 | CLASS="PROGRAMLISTING" |
| 7321 | >int set_field_type(FIELD *field, /* field to alter */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 7322 | TYPE_ALNUM, /* type to associate */ |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 7323 | int width); /* maximum width of field */</PRE |
| 7324 | ><P |
| 7325 | >The width argument sets a minimum width of data. As with |
| 7326 | TYPE_ALPHA, typically you'll want to set this to the field width; if it's |
| 7327 | greater than the field width, the validation check will always fail. A |
| 7328 | minimum width of zero makes field completion optional. </P |
| 7329 | ><H1 |
| 7330 | CLASS="BRIDGEHEAD" |
| 7331 | ><A |
| 7332 | NAME="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 |
| 7337 | set of string values (for example, the two-letter postal codes for U.S. |
| 7338 | states). It is set up with: </P |
| 7339 | ><PRE |
| 7340 | CLASS="PROGRAMLISTING" |
| 7341 | >int set_field_type(FIELD *field, /* field to alter */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 7342 | TYPE_ENUM, /* type to associate */ |
| 7343 | char **valuelist; /* list of possible values */ |
| 7344 | int checkcase; /* case-sensitive? */ |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 7345 | int checkunique); /* must specify uniquely? */</PRE |
| 7346 | ><P |
| 7347 | >The valuelist parameter must point at a NULL-terminated list of |
| 7348 | valid strings. The checkcase argument, if true, makes comparison |
| 7349 | with the string case-sensitive. </P |
| 7350 | ><P |
| 7351 | >When the user exits a TYPE_ENUM field, the validation procedure tries to |
| 7352 | complete the data in the buffer to a valid entry. If a complete choice string |
| 7353 | has been entered, it is of course valid. But it is also possible to enter a |
| 7354 | prefix 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 |
| 7357 | in the string list, the prefix will be completed to the first matching |
| 7358 | value. But the checkunique argument, if true, requires prefix |
| 7359 | matches 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 |
| 7362 | useful with these fields. </P |
| 7363 | ><H1 |
| 7364 | CLASS="BRIDGEHEAD" |
| 7365 | ><A |
| 7366 | NAME="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 |
| 7372 | CLASS="PROGRAMLISTING" |
| 7373 | >int set_field_type(FIELD *field, /* field to alter */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 7374 | TYPE_INTEGER, /* type to associate */ |
| 7375 | int padding, /* # places to zero-pad to */ |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 7376 | int vmin, int vmax); /* valid range */</PRE |
| 7377 | ><P |
| 7378 | >Valid characters consist of an optional leading minus and digits. |
| 7379 | The range check is performed on exit. If the range maximum is less |
| 7380 | than 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 |
| 7383 | zero 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 |
| 7386 | function atoi(3).</P |
| 7387 | ><H1 |
| 7388 | CLASS="BRIDGEHEAD" |
| 7389 | ><A |
| 7390 | NAME="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 |
| 7396 | CLASS="PROGRAMLISTING" |
| 7397 | >int set_field_type(FIELD *field, /* field to alter */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 7398 | TYPE_NUMERIC, /* type to associate */ |
| 7399 | int padding, /* # places of precision */ |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 7400 | int vmin, int vmax); /* valid range */</PRE |
| 7401 | ><P |
| 7402 | >Valid characters consist of an optional leading minus and digits. possibly |
| 7403 | including a decimal point. The range check is performed on exit. If the |
| 7404 | range maximum is less than or equal to the minimum, the range is |
| 7405 | ignored. </P |
| 7406 | ><P |
| 7407 | >If the value passes its range check, it is padded with as many trailing |
| 7408 | zero 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 |
| 7411 | function atof(3).</P |
| 7412 | ><H1 |
| 7413 | CLASS="BRIDGEHEAD" |
| 7414 | ><A |
| 7415 | NAME="AEN1096" |
| 7416 | ></A |
| 7417 | >TYPE_REGEXP</H1 |
| 7418 | ><P |
| 7419 | >This field type accepts data matching a regular expression. It is set up |
| 7420 | as follows: </P |
| 7421 | ><PRE |
| 7422 | CLASS="PROGRAMLISTING" |
| 7423 | >int set_field_type(FIELD *field, /* field to alter */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 7424 | TYPE_REGEXP, /* type to associate */ |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 7425 | char *regexp); /* expression to match */</PRE |
| 7426 | ><P |
| 7427 | >The syntax for regular expressions is that of regcomp(3). |
| 7428 | The check for regular-expression match is performed on exit.</P |
| 7429 | ></DIV |
| 7430 | ><DIV |
| 7431 | CLASS="SECT2" |
| 7432 | ><HR><H3 |
| 7433 | CLASS="SECT2" |
| 7434 | ><A |
| 7435 | NAME="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 |
| 7440 | system. All types of requests to forms system should be funneled through |
| 7441 | form_driver().</P |
| 7442 | ><PRE |
| 7443 | CLASS="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 |
| 7448 | for user input and then decide whether it's a field data or a form request. The |
| 7449 | form 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 |
| 7452 | requests and their usage is explained below:</P |
| 7453 | ><DIV |
| 7454 | CLASS="SECT3" |
| 7455 | ><HR><H4 |
| 7456 | CLASS="SECT3" |
| 7457 | ><A |
| 7458 | NAME="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 |
| 7463 | new form screen. A form can be made of multiple pages. If you have a big form |
| 7464 | with lot of fields and logical sections, then you can divide the form into |
| 7465 | pages. The function set_new_page() to set a new page at the field specified.</P |
| 7466 | ><PRE |
| 7467 | CLASS="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 |
| 7478 | CLASS="emphasis" |
| 7479 | ><I |
| 7480 | CLASS="EMPHASIS" |
| 7481 | >REQ_NEXT_PAGE</I |
| 7482 | ></SPAN |
| 7483 | > Move to the next form page.</P |
| 7484 | ></LI |
| 7485 | ><LI |
| 7486 | ><P |
| 7487 | ><SPAN |
| 7488 | CLASS="emphasis" |
| 7489 | ><I |
| 7490 | CLASS="EMPHASIS" |
| 7491 | >REQ_PREV_PAGE</I |
| 7492 | ></SPAN |
| 7493 | > Move to the previous |
| 7494 | form page.</P |
| 7495 | ></LI |
| 7496 | ><LI |
| 7497 | ><P |
| 7498 | ><SPAN |
| 7499 | CLASS="emphasis" |
| 7500 | ><I |
| 7501 | CLASS="EMPHASIS" |
| 7502 | >REQ_FIRST_PAGE</I |
| 7503 | ></SPAN |
| 7504 | > Move to the first form page.</P |
| 7505 | ></LI |
| 7506 | ><LI |
| 7507 | ><P |
| 7508 | ><SPAN |
| 7509 | CLASS="emphasis" |
| 7510 | ><I |
| 7511 | CLASS="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 |
| 7519 | last page goes to the first, and REQ_PREV_PAGE from the first page goes to |
| 7520 | the last.</P |
| 7521 | ></DIV |
| 7522 | ><DIV |
| 7523 | CLASS="SECT3" |
| 7524 | ><HR><H4 |
| 7525 | CLASS="SECT3" |
| 7526 | ><A |
| 7527 | NAME="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 |
| 7538 | CLASS="emphasis" |
| 7539 | ><I |
| 7540 | CLASS="EMPHASIS" |
| 7541 | >REQ_NEXT_FIELD</I |
| 7542 | ></SPAN |
| 7543 | > |
| 7544 | Move to next field. </P |
| 7545 | ></LI |
| 7546 | ><LI |
| 7547 | ><P |
| 7548 | ><SPAN |
| 7549 | CLASS="emphasis" |
| 7550 | ><I |
| 7551 | CLASS="EMPHASIS" |
| 7552 | >REQ_PREV_FIELD</I |
| 7553 | ></SPAN |
| 7554 | > |
| 7555 | Move to previous field. </P |
| 7556 | ></LI |
| 7557 | ><LI |
| 7558 | ><P |
| 7559 | ><SPAN |
| 7560 | CLASS="emphasis" |
| 7561 | ><I |
| 7562 | CLASS="EMPHASIS" |
| 7563 | >REQ_FIRST_FIELD</I |
| 7564 | ></SPAN |
| 7565 | > |
| 7566 | Move to the first field. </P |
| 7567 | ></LI |
| 7568 | ><LI |
| 7569 | ><P |
| 7570 | ><SPAN |
| 7571 | CLASS="emphasis" |
| 7572 | ><I |
| 7573 | CLASS="EMPHASIS" |
| 7574 | >REQ_LAST_FIELD</I |
| 7575 | ></SPAN |
| 7576 | > |
| 7577 | Move to the last field. </P |
| 7578 | ></LI |
| 7579 | ><LI |
| 7580 | ><P |
| 7581 | ><SPAN |
| 7582 | CLASS="emphasis" |
| 7583 | ><I |
| 7584 | CLASS="EMPHASIS" |
| 7585 | >REQ_SNEXT_FIELD</I |
| 7586 | ></SPAN |
| 7587 | > |
| 7588 | Move to sorted next field. </P |
| 7589 | ></LI |
| 7590 | ><LI |
| 7591 | ><P |
| 7592 | ><SPAN |
| 7593 | CLASS="emphasis" |
| 7594 | ><I |
| 7595 | CLASS="EMPHASIS" |
| 7596 | >REQ_SPREV_FIELD</I |
| 7597 | ></SPAN |
| 7598 | > |
| 7599 | Move to sorted previous field. </P |
| 7600 | ></LI |
| 7601 | ><LI |
| 7602 | ><P |
| 7603 | ><SPAN |
| 7604 | CLASS="emphasis" |
| 7605 | ><I |
| 7606 | CLASS="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 |
| 7615 | CLASS="emphasis" |
| 7616 | ><I |
| 7617 | CLASS="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 |
| 7626 | CLASS="emphasis" |
| 7627 | ><I |
| 7628 | CLASS="EMPHASIS" |
| 7629 | >REQ_LEFT_FIELD</I |
| 7630 | ></SPAN |
| 7631 | > |
| 7632 | Move left to field. </P |
| 7633 | ></LI |
| 7634 | ><LI |
| 7635 | ><P |
| 7636 | ><SPAN |
| 7637 | CLASS="emphasis" |
| 7638 | ><I |
| 7639 | CLASS="EMPHASIS" |
| 7640 | >REQ_RIGHT_FIELD</I |
| 7641 | ></SPAN |
| 7642 | > |
| 7643 | Move right to field. </P |
| 7644 | ></LI |
| 7645 | ><LI |
| 7646 | ><P |
| 7647 | ><SPAN |
| 7648 | CLASS="emphasis" |
| 7649 | ><I |
| 7650 | CLASS="EMPHASIS" |
| 7651 | >REQ_UP_FIELD</I |
| 7652 | ></SPAN |
| 7653 | > |
| 7654 | Move up to field. </P |
| 7655 | ></LI |
| 7656 | ><LI |
| 7657 | ><P |
| 7658 | ><SPAN |
| 7659 | CLASS="emphasis" |
| 7660 | ><I |
| 7661 | CLASS="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, |
| 7670 | REQ_NEXT_FIELD from the last field goes to the first, and REQ_PREV_FIELD |
| 7671 | from 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 |
| 7673 | the field pointers in the form array (as set up by new_form() or |
| 7674 | set_form_fields()</P |
| 7675 | ><P |
| 7676 | >It is also possible to traverse the fields as if they had been sorted in |
| 7677 | screen-position order, so the sequence goes left-to-right and top-to-bottom. |
| 7678 | To 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, |
| 7681 | down, right, and left. To accomplish this, use the third group of four |
| 7682 | requests. Note, however, that the position of a form for purposes of these |
| 7683 | requests is its upper-left corner.</P |
| 7684 | ><P |
| 7685 | >For example, suppose you have a multi-line field B, and two single-line |
| 7686 | fields A and C on the same line with B, with A to the left of B and C to the |
| 7687 | right of B. A REQ_MOVE_RIGHT from A will go to B only if A, B, and C all |
| 7688 | share the same first line; otherwise it will skip over B to C.</P |
| 7689 | ></DIV |
| 7690 | ><DIV |
| 7691 | CLASS="SECT3" |
| 7692 | ><HR><H4 |
| 7693 | CLASS="SECT3" |
| 7694 | ><A |
| 7695 | NAME="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 |
| 7700 | selected field.</P |
| 7701 | ><P |
| 7702 | ></P |
| 7703 | ><UL |
| 7704 | ><LI |
| 7705 | ><P |
| 7706 | ><SPAN |
| 7707 | CLASS="emphasis" |
| 7708 | ><I |
| 7709 | CLASS="EMPHASIS" |
| 7710 | >REQ_NEXT_CHAR</I |
| 7711 | ></SPAN |
| 7712 | > |
| 7713 | Move to next character. </P |
| 7714 | ></LI |
| 7715 | ><LI |
| 7716 | ><P |
| 7717 | ><SPAN |
| 7718 | CLASS="emphasis" |
| 7719 | ><I |
| 7720 | CLASS="EMPHASIS" |
| 7721 | >REQ_PREV_CHAR</I |
| 7722 | ></SPAN |
| 7723 | > |
| 7724 | Move to previous character. </P |
| 7725 | ></LI |
| 7726 | ><LI |
| 7727 | ><P |
| 7728 | ><SPAN |
| 7729 | CLASS="emphasis" |
| 7730 | ><I |
| 7731 | CLASS="EMPHASIS" |
| 7732 | >REQ_NEXT_LINE</I |
| 7733 | ></SPAN |
| 7734 | > |
| 7735 | Move to next line. </P |
| 7736 | ></LI |
| 7737 | ><LI |
| 7738 | ><P |
| 7739 | ><SPAN |
| 7740 | CLASS="emphasis" |
| 7741 | ><I |
| 7742 | CLASS="EMPHASIS" |
| 7743 | >REQ_PREV_LINE</I |
| 7744 | ></SPAN |
| 7745 | > |
| 7746 | Move to previous line. </P |
| 7747 | ></LI |
| 7748 | ><LI |
| 7749 | ><P |
| 7750 | ><SPAN |
| 7751 | CLASS="emphasis" |
| 7752 | ><I |
| 7753 | CLASS="EMPHASIS" |
| 7754 | >REQ_NEXT_WORD</I |
| 7755 | ></SPAN |
| 7756 | > |
| 7757 | Move to next word. </P |
| 7758 | ></LI |
| 7759 | ><LI |
| 7760 | ><P |
| 7761 | ><SPAN |
| 7762 | CLASS="emphasis" |
| 7763 | ><I |
| 7764 | CLASS="EMPHASIS" |
| 7765 | >REQ_PREV_WORD</I |
| 7766 | ></SPAN |
| 7767 | > |
| 7768 | Move to previous word. </P |
| 7769 | ></LI |
| 7770 | ><LI |
| 7771 | ><P |
| 7772 | ><SPAN |
| 7773 | CLASS="emphasis" |
| 7774 | ><I |
| 7775 | CLASS="EMPHASIS" |
| 7776 | >REQ_BEG_FIELD</I |
| 7777 | ></SPAN |
| 7778 | > |
| 7779 | Move to beginning of field. </P |
| 7780 | ></LI |
| 7781 | ><LI |
| 7782 | ><P |
| 7783 | ><SPAN |
| 7784 | CLASS="emphasis" |
| 7785 | ><I |
| 7786 | CLASS="EMPHASIS" |
| 7787 | >REQ_END_FIELD</I |
| 7788 | ></SPAN |
| 7789 | > |
| 7790 | Move to end of field. </P |
| 7791 | ></LI |
| 7792 | ><LI |
| 7793 | ><P |
| 7794 | ><SPAN |
| 7795 | CLASS="emphasis" |
| 7796 | ><I |
| 7797 | CLASS="EMPHASIS" |
| 7798 | >REQ_BEG_LINE</I |
| 7799 | ></SPAN |
| 7800 | > |
| 7801 | Move to beginning of line. </P |
| 7802 | ></LI |
| 7803 | ><LI |
| 7804 | ><P |
| 7805 | ><SPAN |
| 7806 | CLASS="emphasis" |
| 7807 | ><I |
| 7808 | CLASS="EMPHASIS" |
| 7809 | >REQ_END_LINE</I |
| 7810 | ></SPAN |
| 7811 | > |
| 7812 | Move to end of line. </P |
| 7813 | ></LI |
| 7814 | ><LI |
| 7815 | ><P |
| 7816 | ><SPAN |
| 7817 | CLASS="emphasis" |
| 7818 | ><I |
| 7819 | CLASS="EMPHASIS" |
| 7820 | >REQ_LEFT_CHAR</I |
| 7821 | ></SPAN |
| 7822 | > |
| 7823 | Move left in field. </P |
| 7824 | ></LI |
| 7825 | ><LI |
| 7826 | ><P |
| 7827 | ><SPAN |
| 7828 | CLASS="emphasis" |
| 7829 | ><I |
| 7830 | CLASS="EMPHASIS" |
| 7831 | >REQ_RIGHT_CHAR</I |
| 7832 | ></SPAN |
| 7833 | > |
| 7834 | Move right in field. </P |
| 7835 | ></LI |
| 7836 | ><LI |
| 7837 | ><P |
| 7838 | ><SPAN |
| 7839 | CLASS="emphasis" |
| 7840 | ><I |
| 7841 | CLASS="EMPHASIS" |
| 7842 | >REQ_UP_CHAR</I |
| 7843 | ></SPAN |
| 7844 | > |
| 7845 | Move up in field. </P |
| 7846 | ></LI |
| 7847 | ><LI |
| 7848 | ><P |
| 7849 | ><SPAN |
| 7850 | CLASS="emphasis" |
| 7851 | ><I |
| 7852 | CLASS="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. |
| 7861 | The commands to move to beginning and end of line or field look for the |
| 7862 | first or last non-pad character in their ranges.</P |
| 7863 | ></DIV |
| 7864 | ><DIV |
| 7865 | CLASS="SECT3" |
| 7866 | ><HR><H4 |
| 7867 | CLASS="SECT3" |
| 7868 | ><A |
| 7869 | NAME="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 |
| 7874 | offscreen rows are scrollable. One-line fields scroll horizontally; |
| 7875 | multi-line fields scroll vertically. Most scrolling is triggered by editing |
| 7876 | and intra-field movement (the library scrolls the field to keep the cursor |
| 7877 | visible). It is possible to explicitly request scrolling with the following |
| 7878 | requests:</P |
| 7879 | ><P |
| 7880 | ></P |
| 7881 | ><UL |
| 7882 | ><LI |
| 7883 | ><P |
| 7884 | ><SPAN |
| 7885 | CLASS="emphasis" |
| 7886 | ><I |
| 7887 | CLASS="EMPHASIS" |
| 7888 | >REQ_SCR_FLINE</I |
| 7889 | ></SPAN |
| 7890 | > |
| 7891 | Scroll vertically forward a line. </P |
| 7892 | ></LI |
| 7893 | ><LI |
| 7894 | ><P |
| 7895 | ><SPAN |
| 7896 | CLASS="emphasis" |
| 7897 | ><I |
| 7898 | CLASS="EMPHASIS" |
| 7899 | >REQ_SCR_BLINE</I |
| 7900 | ></SPAN |
| 7901 | > |
| 7902 | Scroll vertically backward a line. </P |
| 7903 | ></LI |
| 7904 | ><LI |
| 7905 | ><P |
| 7906 | ><SPAN |
| 7907 | CLASS="emphasis" |
| 7908 | ><I |
| 7909 | CLASS="EMPHASIS" |
| 7910 | >REQ_SCR_FPAGE</I |
| 7911 | ></SPAN |
| 7912 | > |
| 7913 | Scroll vertically forward a page. </P |
| 7914 | ></LI |
| 7915 | ><LI |
| 7916 | ><P |
| 7917 | ><SPAN |
| 7918 | CLASS="emphasis" |
| 7919 | ><I |
| 7920 | CLASS="EMPHASIS" |
| 7921 | >REQ_SCR_BPAGE</I |
| 7922 | ></SPAN |
| 7923 | > |
| 7924 | Scroll vertically backward a page. </P |
| 7925 | ></LI |
| 7926 | ><LI |
| 7927 | ><P |
| 7928 | ><SPAN |
| 7929 | CLASS="emphasis" |
| 7930 | ><I |
| 7931 | CLASS="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 |
| 7940 | CLASS="emphasis" |
| 7941 | ><I |
| 7942 | CLASS="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 |
| 7951 | CLASS="emphasis" |
| 7952 | ><I |
| 7953 | CLASS="EMPHASIS" |
| 7954 | >REQ_SCR_FCHAR</I |
| 7955 | ></SPAN |
| 7956 | > |
| 7957 | Scroll horizontally forward a character. </P |
| 7958 | ></LI |
| 7959 | ><LI |
| 7960 | ><P |
| 7961 | ><SPAN |
| 7962 | CLASS="emphasis" |
| 7963 | ><I |
| 7964 | CLASS="EMPHASIS" |
| 7965 | >REQ_SCR_BCHAR</I |
| 7966 | ></SPAN |
| 7967 | > |
| 7968 | Scroll horizontally backward a character. </P |
| 7969 | ></LI |
| 7970 | ><LI |
| 7971 | ><P |
| 7972 | ><SPAN |
| 7973 | CLASS="emphasis" |
| 7974 | ><I |
| 7975 | CLASS="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 |
| 7984 | CLASS="emphasis" |
| 7985 | ><I |
| 7986 | CLASS="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 |
| 7995 | CLASS="emphasis" |
| 7996 | ><I |
| 7997 | CLASS="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 |
| 8006 | CLASS="emphasis" |
| 8007 | ><I |
| 8008 | CLASS="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 |
| 8019 | CLASS="SECT3" |
| 8020 | ><HR><H4 |
| 8021 | CLASS="SECT3" |
| 8022 | ><A |
| 8023 | NAME="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 |
| 8028 | request to add the character to the field's data buffer. Whether this is an |
| 8029 | insertion or a replacement depends on the field's edit mode (insertion is |
| 8030 | the 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 |
| 8039 | CLASS="emphasis" |
| 8040 | ><I |
| 8041 | CLASS="EMPHASIS" |
| 8042 | >REQ_INS_MODE</I |
| 8043 | ></SPAN |
| 8044 | > |
| 8045 | Set insertion mode. </P |
| 8046 | ></LI |
| 8047 | ><LI |
| 8048 | ><P |
| 8049 | ><SPAN |
| 8050 | CLASS="emphasis" |
| 8051 | ><I |
| 8052 | CLASS="EMPHASIS" |
| 8053 | >REQ_OVL_MODE</I |
| 8054 | ></SPAN |
| 8055 | > |
| 8056 | Set overlay mode. </P |
| 8057 | ></LI |
| 8058 | ><LI |
| 8059 | ><P |
| 8060 | ><SPAN |
| 8061 | CLASS="emphasis" |
| 8062 | ><I |
| 8063 | CLASS="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 |
| 8072 | CLASS="emphasis" |
| 8073 | ><I |
| 8074 | CLASS="EMPHASIS" |
| 8075 | >REQ_INS_CHAR</I |
| 8076 | ></SPAN |
| 8077 | > |
| 8078 | Insert space at character location. </P |
| 8079 | ></LI |
| 8080 | ><LI |
| 8081 | ><P |
| 8082 | ><SPAN |
| 8083 | CLASS="emphasis" |
| 8084 | ><I |
| 8085 | CLASS="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 |
| 8094 | CLASS="emphasis" |
| 8095 | ><I |
| 8096 | CLASS="EMPHASIS" |
| 8097 | >REQ_DEL_CHAR</I |
| 8098 | ></SPAN |
| 8099 | > |
| 8100 | Delete character at cursor. </P |
| 8101 | ></LI |
| 8102 | ><LI |
| 8103 | ><P |
| 8104 | ><SPAN |
| 8105 | CLASS="emphasis" |
| 8106 | ><I |
| 8107 | CLASS="EMPHASIS" |
| 8108 | >REQ_DEL_PREV</I |
| 8109 | ></SPAN |
| 8110 | > |
| 8111 | Delete previous word at cursor. </P |
| 8112 | ></LI |
| 8113 | ><LI |
| 8114 | ><P |
| 8115 | ><SPAN |
| 8116 | CLASS="emphasis" |
| 8117 | ><I |
| 8118 | CLASS="EMPHASIS" |
| 8119 | >REQ_DEL_LINE</I |
| 8120 | ></SPAN |
| 8121 | > |
| 8122 | Delete line at cursor. </P |
| 8123 | ></LI |
| 8124 | ><LI |
| 8125 | ><P |
| 8126 | ><SPAN |
| 8127 | CLASS="emphasis" |
| 8128 | ><I |
| 8129 | CLASS="EMPHASIS" |
| 8130 | >REQ_DEL_WORD</I |
| 8131 | ></SPAN |
| 8132 | > |
| 8133 | Delete word at cursor. </P |
| 8134 | ></LI |
| 8135 | ><LI |
| 8136 | ><P |
| 8137 | ><SPAN |
| 8138 | CLASS="emphasis" |
| 8139 | ><I |
| 8140 | CLASS="EMPHASIS" |
| 8141 | >REQ_CLR_EOL</I |
| 8142 | ></SPAN |
| 8143 | > |
| 8144 | Clear to end of line. </P |
| 8145 | ></LI |
| 8146 | ><LI |
| 8147 | ><P |
| 8148 | ><SPAN |
| 8149 | CLASS="emphasis" |
| 8150 | ><I |
| 8151 | CLASS="EMPHASIS" |
| 8152 | >REQ_CLR_EOF</I |
| 8153 | ></SPAN |
| 8154 | > |
| 8155 | Clear to end of field. </P |
| 8156 | ></LI |
| 8157 | ><LI |
| 8158 | ><P |
| 8159 | ><SPAN |
| 8160 | CLASS="emphasis" |
| 8161 | ><I |
| 8162 | CLASS="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 |
| 8171 | and partly controlled by a pair of forms options. The special cases are |
| 8172 | triggered when the cursor is at the beginning of a field, or on the last |
| 8173 | line 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 |
| 8178 | line at the position of the edit cursor, inserting the portion of the |
| 8179 | current line after the cursor as a new line following the current and moving |
| 8180 | the cursor to the beginning of that new line (you may think of this as |
| 8181 | inserting 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 |
| 8184 | line from the position of the edit cursor to end of line. The cursor is then |
| 8185 | moved 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 |
| 8188 | field, instead does a REQ_NEXT_FIELD. O_NL_OVERLOAD option is off, this |
| 8189 | special 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 |
| 8194 | insert mode is on, and the cursor is at the start of a line, and the text on |
| 8195 | that line will fit on the previous one, it instead appends the contents of |
| 8196 | the current line to the previous one and deletes the current line (you may |
| 8197 | think 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 |
| 8200 | REQ_PREV_FIELD.</P |
| 8201 | ><P |
| 8202 | >If the O_BS_OVERLOAD option is off, this special action is disabled and the |
| 8203 | forms driver just returns E_REQUEST_DENIED.</P |
| 8204 | ></DIV |
| 8205 | ><DIV |
| 8206 | CLASS="SECT3" |
| 8207 | ><HR><H4 |
| 8208 | CLASS="SECT3" |
| 8209 | ><A |
| 8210 | NAME="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 |
| 8215 | getting the next and previous values of the type from a given value, there |
| 8216 | are requests that can fetch that value into the field buffer:</P |
| 8217 | ><P |
| 8218 | ></P |
| 8219 | ><UL |
| 8220 | ><LI |
| 8221 | ><P |
| 8222 | ><SPAN |
| 8223 | CLASS="emphasis" |
| 8224 | ><I |
| 8225 | CLASS="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 |
| 8235 | CLASS="emphasis" |
| 8236 | ><I |
| 8237 | CLASS="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 |
| 8247 | predecessor functions. When you define a field type of your own (see Custom |
| 8248 | Validation Types), you can associate our own ordering functions.</P |
| 8249 | ></DIV |
| 8250 | ><DIV |
| 8251 | CLASS="SECT3" |
| 8252 | ><HR><H4 |
| 8253 | CLASS="SECT3" |
| 8254 | ><A |
| 8255 | NAME="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 |
| 8260 | KEY_MAX and less than or equal to the constant MAX_COMMAND. A value within this |
| 8261 | range gets ignored by form_driver(). So this can be used for any purpose by the |
| 8262 | application. It can be treated as an application specific action and take |
| 8263 | corresponding action.</P |
| 8264 | ></DIV |
| 8265 | ></DIV |
| 8266 | ></DIV |
| 8267 | ><DIV |
| 8268 | CLASS="SECT1" |
| 8269 | ><HR><H2 |
| 8270 | CLASS="SECT1" |
| 8271 | ><A |
| 8272 | NAME="TOOLS" |
| 8273 | >19. Tools and Widget Libraries</A |
| 8274 | ></H2 |
| 8275 | ><P |
| 8276 | > |
| 8277 | Now that you have seen the capabilities of ncurses and its sister libraries, you |
| 8278 | are rolling your sleeves up and gearing for a project that heavily manipulates |
| 8279 | screen. But wait.. It can be pretty difficult to write and maintain complex GUI |
| 8280 | widgets in plain ncurses or even with the additional libraries. There are some |
| 8281 | ready-to-use tools and widget libraries that can be used instead of writing your |
| 8282 | own widgets. You can use some of them, get ideas from the code, or even extend |
| 8283 | them.</P |
| 8284 | ><DIV |
| 8285 | CLASS="SECT2" |
| 8286 | ><HR><H3 |
| 8287 | CLASS="SECT2" |
| 8288 | ><A |
| 8289 | NAME="CDK" |
| 8290 | >19.1. CDK (Curses Development Kit)</A |
| 8291 | ></H3 |
| 8292 | ><P |
| 8293 | >In the author's words </P |
| 8294 | ><P |
| 8295 | ><SPAN |
| 8296 | CLASS="emphasis" |
| 8297 | ><I |
| 8298 | CLASS="EMPHASIS" |
| 8299 | > |
| 8300 | CDK stands for 'Curses Development Kit' and it currently contains 21 ready |
| 8301 | to use widgets which facilitate the speedy development of full screen |
| 8302 | curses programs. </I |
| 8303 | ></SPAN |
| 8304 | ></P |
| 8305 | ><P |
| 8306 | >The kit provides some useful widgets, which can be used in your programs |
| 8307 | directly. It's pretty well written and the documentation is very good. The |
| 8308 | examples in the examples directory can be a good place to start for beginners. |
| 8309 | The CDK can be downloaded from <A |
| 8310 | HREF="http://invisible-island.net/cdk/" |
| 8311 | TARGET="_top" |
| 8312 | >http://invisible-island.net/cdk/</A |
| 8313 | > |
| 8314 | . Follow the instructions in |
| 8315 | README file to install it.</P |
| 8316 | ><DIV |
| 8317 | CLASS="SECT3" |
| 8318 | ><HR><H4 |
| 8319 | CLASS="SECT3" |
| 8320 | ><A |
| 8321 | NAME="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 |
| 8327 | CLASS="PROGRAMLISTING" |
| 8328 | >Widget Type Quick Description |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 8329 | =========================================================================== |
| 8330 | Alphalist 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. |
| 8333 | Buttonbox This creates a multiple button widget. |
| 8334 | Calendar Creates a little simple calendar widget. |
| 8335 | Dialog Prompts the user with a message, and the user |
| 8336 | can pick an answer from the buttons provided. |
| 8337 | Entry Allows the user to enter various types of information. |
| 8338 | File Selector A file selector built from Cdk base widgets. This |
| 8339 | example shows how to create more complicated widgets |
| 8340 | using the Cdk widget library. |
| 8341 | Graph Draws a graph. |
| 8342 | Histogram Draws a histogram. |
| 8343 | Item List Creates a pop up field which allows the user to select |
| 8344 | one of several choices in a small field. Very useful |
| 8345 | for things like days of the week or month names. |
| 8346 | Label Displays messages in a pop up box, or the label can be |
| 8347 | considered part of the screen. |
| 8348 | Marquee Displays a message in a scrolling marquee. |
| 8349 | Matrix Creates a complex matrix with lots of options. |
| 8350 | Menu Creates a pull-down menu interface. |
| 8351 | Multiple Line Entry A multiple line entry field. Very useful |
| 8352 | for long fields. (like a description |
| 8353 | field) |
| 8354 | Radio List Creates a radio button list. |
| 8355 | Scale Creates a numeric scale. Used for allowing a user to |
| 8356 | pick a numeric value and restrict them to a range of |
| 8357 | values. |
| 8358 | Scrolling List Creates a scrolling list/menu list. |
| 8359 | Scrolling Window Creates a scrolling log file viewer. Can add |
| 8360 | information into the window while its running. |
| 8361 | A good widget for displaying the progress of |
| 8362 | something. (akin to a console window) |
| 8363 | Selection List Creates a multiple option selection list. |
| 8364 | Slider Akin to the scale widget, this widget provides a |
| 8365 | visual slide bar to represent the numeric value. |
| 8366 | Template Creates a entry field with character sensitive |
| 8367 | positions. Used for pre-formatted fields like |
| 8368 | dates and phone numbers. |
| 8369 | Viewer This is a file/information viewer. Very useful |
| 8370 | when you need to display loads of information. |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 8371 | ===========================================================================</PRE |
| 8372 | ><P |
| 8373 | >A few of the widgets are modified by Thomas Dickey in recent versions.</P |
| 8374 | ></DIV |
| 8375 | ><DIV |
| 8376 | CLASS="SECT3" |
| 8377 | ><HR><H4 |
| 8378 | CLASS="SECT3" |
| 8379 | ><A |
| 8380 | NAME="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 |
| 8385 | frustrating problem with printing multi colored strings, justified strings |
| 8386 | elegantly. Special formatting tags can be embedded in the strings which are |
| 8387 | passed to CDK functions. For Example</P |
| 8388 | ><P |
| 8389 | >If the string</P |
| 8390 | ><PRE |
| 8391 | CLASS="PROGRAMLISTING" |
| 8392 | >"</B/1>This line should have a yellow foreground and a blue |
| 8393 | background.<!1>"</PRE |
| 8394 | ><P |
| 8395 | >given as a parameter to newCDKLabel(), it prints the line with yellow foreground |
| 8396 | and blue background. There are other tags available for justifying string, |
| 8397 | embedding special drawing characters etc.. Please refer to the man page |
| 8398 | cdk_display(3X) for details. The man page explains the usage with nice examples.</P |
| 8399 | ></DIV |
| 8400 | ><DIV |
| 8401 | CLASS="SECT3" |
| 8402 | ><HR><H4 |
| 8403 | CLASS="SECT3" |
| 8404 | ><A |
| 8405 | NAME="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 |
| 8410 | form a strong frame work for developing complex GUI.</P |
| 8411 | ></DIV |
| 8412 | ></DIV |
| 8413 | ><DIV |
| 8414 | CLASS="SECT2" |
| 8415 | ><HR><H3 |
| 8416 | CLASS="SECT2" |
| 8417 | ><A |
| 8418 | NAME="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 |
| 8423 | an <A |
| 8424 | HREF="http://www2.linuxjournal.com/lj-issues/issue5/2807.html" |
| 8425 | TARGET="_top" |
| 8426 | >article</A |
| 8427 | > on dialog in Linux Journal. He starts the article with these words..</P |
| 8428 | ><P |
| 8429 | ><SPAN |
| 8430 | CLASS="emphasis" |
| 8431 | ><I |
| 8432 | CLASS="EMPHASIS" |
| 8433 | >Linux is based on the Unix operating system, but also features a number of |
| 8434 | unique and useful kernel features and application programs that often go beyond |
| 8435 | what is available under Unix. One little-known gem is "dialog", a utility for |
| 8436 | creating professional-looking dialog boxes from within shell scripts. This |
| 8437 | article presents a tutorial introduction to the dialog utility, and shows |
| 8438 | examples of how and where it can be used</I |
| 8439 | ></SPAN |
| 8440 | ></P |
| 8441 | ><P |
| 8442 | > |
| 8443 | As he explains, dialog is a real gem in making professional-looking dialog boxes |
| 8444 | with ease. It creates a variety of dialog boxes, menus, check lists etc.. It is |
| 8445 | usually installed by default. If not, you can download it from <A |
| 8446 | HREF="http://invisible-island.net/dialog/" |
| 8447 | TARGET="_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 |
| 8452 | capabilites. The man page has more details. It can be used in variety of |
| 8453 | situations. One good example is building of linux kernel in text mode. Linux |
| 8454 | kernel 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 |
| 8457 | its functionality in a c program, then you can use libdialog. The documentation |
| 8458 | regarding this is sparse. Definitive reference is the dialog.h header file which |
| 8459 | comes with the library. You may need to hack here and there to get the required |
| 8460 | output. The source is easily customizable. I have used it on a number of |
| 8461 | occasions by modifying the code.</P |
| 8462 | ></DIV |
| 8463 | ><DIV |
| 8464 | CLASS="SECT2" |
| 8465 | ><HR><H3 |
| 8466 | CLASS="SECT2" |
| 8467 | ><A |
| 8468 | NAME="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 |
| 8473 | from perl. If you have curses and basic perl is installed, you can get these |
| 8474 | modules from <A |
| 8475 | HREF="http://www.cpan.org/modules/01modules.index.html" |
| 8476 | TARGET="_top" |
| 8477 | > CPAN |
| 8478 | All Modules page</A |
| 8479 | >. Get the three zipped modules in the Curses category. |
| 8480 | Once installed you can use these modules from perl scripts like any other |
| 8481 | module. For more information on perl modules see perlmod man page. The above |
| 8482 | modules come with good documentation and they have some demo scripts to test the |
| 8483 | functionality. Though the widgets provided are very rudimentary, these modules |
| 8484 | provide 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 |
| 8487 | are available in the <TT |
| 8488 | CLASS="LITERAL" |
| 8489 | >perl</TT |
| 8490 | > directory.</P |
| 8491 | ><P |
| 8492 | > |
| 8493 | For more information see man pages Curses(3) , Curses::Form(3) and |
| 8494 | Curses::Widgets(3). These pages are installed only when the above modules are |
| 8495 | acquired and installed.</P |
| 8496 | ></DIV |
| 8497 | ></DIV |
| 8498 | ><DIV |
| 8499 | CLASS="SECT1" |
| 8500 | ><HR><H2 |
| 8501 | CLASS="SECT1" |
| 8502 | ><A |
| 8503 | NAME="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 |
| 8508 | signify a better programming practice or the best way of using ncurses. They are |
| 8509 | provided here so as to allow beginners to get ideas and add more programs to |
| 8510 | this section. If you have written a couple of nice, simple programs in curses |
| 8511 | and want them to included here, contact <A |
| 8512 | HREF="mailto:ppadala@gmail.com" |
| 8513 | TARGET="_top" |
| 8514 | >me</A |
| 8515 | >.</P |
| 8516 | ><DIV |
| 8517 | CLASS="SECT2" |
| 8518 | ><HR><H3 |
| 8519 | CLASS="SECT2" |
| 8520 | ><A |
| 8521 | NAME="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 |
| 8527 | HREF="http://www.math.com/students/wonders/life/life.html" |
| 8528 | TARGET="_top" |
| 8529 | >Paul Callahan</A |
| 8530 | >'s words</P |
| 8531 | ><PRE |
| 8532 | CLASS="PROGRAMLISTING" |
| 8533 | ><SPAN |
| 8534 | CLASS="emphasis" |
| 8535 | ><I |
| 8536 | CLASS="EMPHASIS" |
| 8537 | >The Game of Life (or simply Life) is not a game in the conventional sense. There |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 8538 | are no players, and no winning or losing. Once the "pieces" are placed in the |
| 8539 | starting position, the rules determine everything that happens later. |
| 8540 | Nevertheless, Life is full of surprises! In most cases, it is impossible to look |
| 8541 | at a starting position (or pattern) and see what will happen in the future. The |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 8542 | only 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 |
| 8547 | life works. There is a lot of room for improvement in the program. You can let |
| 8548 | the user enter pattern of his choice or even take input from a file. You can |
| 8549 | also change rules and play with a lot of variations. Search on <A |
| 8550 | HREF="http://www.google.com" |
| 8551 | TARGET="_top" |
| 8552 | >google</A |
| 8553 | > for interesting information on game |
| 8554 | of life.</P |
| 8555 | ><P |
| 8556 | ><SPAN |
| 8557 | CLASS="emphasis" |
| 8558 | ><I |
| 8559 | CLASS="EMPHASIS" |
| 8560 | >File Path: JustForFun/life.c</I |
| 8561 | ></SPAN |
| 8562 | ></P |
| 8563 | ></DIV |
| 8564 | ><DIV |
| 8565 | CLASS="SECT2" |
| 8566 | ><HR><H3 |
| 8567 | CLASS="SECT2" |
| 8568 | ><A |
| 8569 | NAME="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 |
| 8574 | difficult to make. In a magic square sum of the numbers in each row, each column |
| 8575 | is equal. Even diagnol sum can be equal. There are many variations which have |
| 8576 | special properties.</P |
| 8577 | ><P |
| 8578 | >This program creates a simple magic square of odd order.</P |
| 8579 | ><P |
| 8580 | ><SPAN |
| 8581 | CLASS="emphasis" |
| 8582 | ><I |
| 8583 | CLASS="EMPHASIS" |
| 8584 | >File Path: JustForFun/magic.c</I |
| 8585 | ></SPAN |
| 8586 | ></P |
| 8587 | ></DIV |
| 8588 | ><DIV |
| 8589 | CLASS="SECT2" |
| 8590 | ><HR><H3 |
| 8591 | CLASS="SECT2" |
| 8592 | ><A |
| 8593 | NAME="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 |
| 8598 | the first peg to last peg, using middle peg as a temporary stay. The catch is |
| 8599 | not to place a larger disk over a small disk at any time.</P |
| 8600 | ><P |
| 8601 | ><SPAN |
| 8602 | CLASS="emphasis" |
| 8603 | ><I |
| 8604 | CLASS="EMPHASIS" |
| 8605 | >File Path: JustForFun/hanoi.c</I |
| 8606 | ></SPAN |
| 8607 | ></P |
| 8608 | ></DIV |
| 8609 | ><DIV |
| 8610 | CLASS="SECT2" |
| 8611 | ><HR><H3 |
| 8612 | CLASS="SECT2" |
| 8613 | ><A |
| 8614 | NAME="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 |
| 8619 | board without attacking each other. </P |
| 8620 | ><P |
| 8621 | >This program solves it with a simple backtracking technique.</P |
| 8622 | ><P |
| 8623 | ><SPAN |
| 8624 | CLASS="emphasis" |
| 8625 | ><I |
| 8626 | CLASS="EMPHASIS" |
| 8627 | >File Path: JustForFun/queens.c</I |
| 8628 | ></SPAN |
| 8629 | ></P |
| 8630 | ></DIV |
| 8631 | ><DIV |
| 8632 | CLASS="SECT2" |
| 8633 | ><HR><H3 |
| 8634 | CLASS="SECT2" |
| 8635 | ><A |
| 8636 | NAME="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 |
| 8643 | CLASS="emphasis" |
| 8644 | ><I |
| 8645 | CLASS="EMPHASIS" |
| 8646 | >File Path: JustForFun/shuffle.c</I |
| 8647 | ></SPAN |
| 8648 | ></P |
| 8649 | ></DIV |
| 8650 | ><DIV |
| 8651 | CLASS="SECT2" |
| 8652 | ><HR><H3 |
| 8653 | CLASS="SECT2" |
| 8654 | ><A |
| 8655 | NAME="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 |
| 8660 | know how to put your fingers correctly on the keyboard, but lack practice, this |
| 8661 | can be helpful. </P |
| 8662 | ><P |
| 8663 | ><SPAN |
| 8664 | CLASS="emphasis" |
| 8665 | ><I |
| 8666 | CLASS="EMPHASIS" |
| 8667 | >File Path: JustForFun/tt.c</I |
| 8668 | ></SPAN |
| 8669 | ></P |
| 8670 | ></DIV |
| 8671 | ></DIV |
| 8672 | ><DIV |
| 8673 | CLASS="SECT1" |
| 8674 | ><HR><H2 |
| 8675 | CLASS="SECT1" |
| 8676 | ><A |
| 8677 | NAME="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 |
| 8690 | HREF="http://invisible-island.net/ncurses/ncurses.faq.html" |
| 8691 | TARGET="_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. |
| 8699 | Ben-Halim at |
| 8700 | <A |
| 8701 | HREF="http://invisible-island.net/ncurses/ncurses-intro.html" |
| 8702 | TARGET="_top" |
| 8703 | >http://invisible-island.net/ncurses/ncurses-intro.html</A |
| 8704 | > - somewhat |
| 8705 | obsolete. I was inspired by this document and the structure of this HOWTO |
| 8706 | follows from the original document</P |
| 8707 | ></LI |
| 8708 | ></UL |
| 8709 | ></DIV |
| 8710 | ></DIV |
| 8711 | ></BODY |
| 8712 | ></HTML |
| 8713 | > |