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.
/* 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: