6.7 Program Example

The following program example shows the effects of many of the Curses macros and functions. You can find explanations of the individual lines of code, if not self-explanatory, in the comments to the right of the particular line. Detailed discussions of the functions follow the source code listing.

Example 6-6 shows the definition and manipulation of one user-defined window and stdscr.

Example 6-6 stdscr and Occluding Windows

/*  The following program defines one window: win1.         *
 *  win1 is located towards the center of the default       *
 *  window stdscr.  When writing to an occluding window     *
 *  (win1) that is later erased, the writing is             *
 *  erased as well.                                         */

#include <curses.h>           /* Include header file.       */

WINDOW   *win1;               /* Define windows.            */

main()
{
   char   str[80];            /* Variable declaration.      */

   initscr();                 /* Set up Curses.             */
   noecho();                  /* Turn off echo.             */
                              /* Create window.             */
   win1 = newwin(10, 20, 10, 10);

   box(stdscr, '|', '-');     /* Draw a box around stdscr.  */
   box(win1, '|', '-');       /* Draw a box around win1.    */

   refresh();                 /* Display stdscr on screen.  */
   wrefresh(win1);            /* Display win1 on screen.    */

    getstr(str);     /* Pause. Type a few words!   */

   mvaddstr(22, 1, str);
    getch();
                              /* Add string to win1.        */
   mvwaddstr(win1, 5, 5, "Hello");
   wrefresh(win1);            /* Add win1 to terminal scr.  */
   getch();                   /* Pause.  Press Return.      */

   delwin(win1);              /* Delete win1.               */
    touchwin(stdscr); /* Refresh all of stdscr.    */

   getch();                   /* Pause.  Press Return.      */
   endwin();                  /* Ends session.              */
}

Key to Example 6-6:

  1. The program waits for input. The echo was disabled using the noecho macro, so the words that you type do not appear on stdscr. However, the macro stores the words in the variable str for use elsewhere in the program.

  2. The getch macro causes the program to pause. When you are finished viewing the screen, press Return so the program can resume. The getch macro refreshes stdscr on the terminal screen without calling refresh. The screen appears like Figure 6-4.

Figure 6-4 An Example of the getch Macro

  1. (3)The touchwin function refreshes the screen so that all of stdscr is visible and the deleted occluding window no longer appears on the screen.


Previous Page | Next Page | Table of Contents | Index