There are commands that you must use to initialize and restore the terminal screen when using Curses Screen Management functions and macros. Also, there are predefined variables and constants on which Curses depends. Example 6-1 shows how to set up a program using Curses.
#include <curses.h> WINDOW *win1, *win2, *win3; main() { initscr(); . . . endwin(); }
Key to Example 6-1:
Most Curses users wish to define and modify windows. Example 6-2 shows you how to define and write to a single window.
#include <curses.h> WINDOW *win1, *win2, *win3; main() { initscr(); win1 = newwin(24, 80, 0, 0); mvwaddstr(win1, 2, 2, "HELLO"); . . . endwin(); }
Key to Example 6-2:
When updating a window, specify the cursor position relative to the origin of the window, not the origin of the terminal screen. For example, if a window has a starting position of (10,10) and you want to add a character to the window at its starting position, specify the coordinates (0,0), not (10,10).
The string HELLO in Example 6-2 does not appear on the terminal screen until you refresh the screen. You accomplish this by using the wrefresh function. Example 6-3 shows how to display the contents of win1 on the terminal screen.
#include <curses.h> WINDOW *win1, *win2, *win3; main() { initscr(); win1 = newwin(22, 60, 0, 0); mvwaddstr(win1, 2, 2, "HELLO"); wrefresh(win1); . . . endwin(); }
The wrefresh function updates just the region of the specified window on the terminal screen. When the program is executed, the string HELLO appears on the terminal screen until the program executes the endwin function. The wrefresh function only refreshes the part of the window on the terminal screen that is not overlapped by another window. If win1 was overlapped by another window and you want all of win1 to be displayed on the terminal screen, call the touchwin function.