Previous | Contents | Index |
This section explains some of the Curses terminology and shows you how Curses looks on the terminal screen.
Consider a Curses application as being a series of overlapping windows.
Window overlapping is called occlusion. To distinguish the
boundaries of these occluding windows, you can outline the rectangular
windows with specified characters, or you can turn on the reverse video
option (make the window a light background with dark writing).
6.3.1 Predefined Windows (stdscr and curscr)
Initially, two windows the size of the terminal screen are predefined by Curses. These windows are called stdscr and curscr . The stdscr window is defined for your use. Many Curses macros default to this window. For example, if you draw a box around stdscr , move the cursor to the left-corner area of the screen, write a string to stdscr , and then display stdscr on the terminal screen, your display will look like that in Figure 6-1.
Figure 6-1 An Example of the stdscr Window
The second predefined window,
curscr
, is designed for internal Curses work; it is an image of what is
currently displayed on the terminal screen. The only HP C for OpenVMS
Curses function that will accept this window as an argument is
clearok
. Do not write to or read from
curscr
. Use
stdscr
and user-defined windows for all your Curses applications.
6.3.2 User-Defined Windows
You can occlude stdscr with your own windows. The size and location of each window is given in terms of the number of lines, the number of columns, and the starting position.
The lines and columns of the terminal screen form a coordinate system, or grid, on which the windows are formed. You specify the starting position of a window with the (y,x) coordinates on the terminal screen where the upper left corner of the window is located. The coordinates (0,0) on the terminal screen, for example, are the upper left corner of the screen.
The entire area of the window must be within the terminal screen borders; windows can be as small as a single character or as large as the entire terminal screen. You can create as many windows as memory allows.
When writing to or deleting from windows, changes do not appear on the terminal screen until the window is refreshed. When refreshing a window, you place the updated window onto the terminal screen, which leaves the rest of the screen unaltered.
All user-defined windows, by default, occlude stdscr . You can create two or more windows that occlude each other as well as stdscr . When writing data to one occluding window, the data is not written to the underlying window.
You can create overlapping windows (called subwindows). A declared window must contain the entire area of its subwindow. When writing data to a subwindow or to the portion of the window overlapped by the subwindow, both windows contain the new data. For instance, if you write data to a subwindow and then delete that subwindow, the data is still present on the underlying window.
If you create a window that occludes stdscr and a subwindow of stdscr , your terminal screen will look like Figure 6-2.
Figure 6-2 Displaying Windows and Subwindows
If you delete both the user-defined window and the subwindow, and then update the terminal screen with the new image, your terminal screen will look like Figure 6-3.
Figure 6-3 Updating the Terminal Screen
The string written on the window is deleted, but the string written on
the subwindow remains on
stdscr
.
6.4 Getting Started with Curses
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.
Example 6-1 A Curses Program |
---|
(1)#include <curses.h> (2)WINDOW *win1, *win2, *win3; main() { (3) 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.
Example 6-2 Manipulating Windows |
---|
#include <curses.h> WINDOW *win1, *win2, *win3; main() { initscr(); (1) win1 = newwin(24, 80, 0, 0); (2) mvwaddstr(win1, 2, 2, "HELLO"); . . . endwin(); } |
Key to Example 6-2:
Most Curses macros update stdscr by default. Curses functions that update other windows have the same name as the macros but with the added prefix "w". For example, the addstr macro adds a given string to stdscr at the current cursor position. The waddstr function adds a given string to a specified window at the current cursor position. |
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.
Example 6-3 Refreshing 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.
6.5 Predefined Variables and Constants
The <curses.h> header file defines variables and constants useful for implementing Curses (see Table 6-2).
Name | Type | Description |
---|---|---|
curscr | WINDOW * | Window of current screen |
stdscr | WINDOW * | Default window |
LINES | int | Number of lines on the terminal screen |
COLS | int | Number of columns on the terminal screen |
ERR | --- | Flag (0) for failed routines |
OK | --- | Flag (1) for successful routines |
TRUE | --- | Boolean true flag (1) |
FALSE | --- | Boolean false flag (0) |
_BLINK | --- | Parameter for setattr and clrattr |
_BOLD | --- | Parameter for setattr and clrattr |
_REVERSE | --- | Parameter for setattr and clrattr |
_UNDERLINE | --- | Parameter for setattr and clrattr |
For example, you can use the predefined macro ERR to test the success or failure of a Curses function. Example 6-4 shows how to perform such a test.
Example 6-4 Curses Predefined Variables |
---|
#include <curses.h> WINDOW *win1, *win2, *win3; main() { initscr(); win1 = newwin(10, 10, 1, 5); . . . if (mvwin(win1, 1, 10) == ERR) addstr("The MVWIN function failed."); . . . endwin(); } |
In Example 6-4, if the
mvwin
function fails, the program adds a string to
stdscr
that explains the outcome. The Curses
mvwin
function moves the starting position of a window.
6.6 Cursor Movement
In the UNIX system environment, you can use Curses functions to move the cursor across the terminal screen. With other implementations, you can either allow Curses to move the cursor using the move function, or you can specify the origin and the destination of the cursor to the mvcur function, which moves the cursor in a more efficient manner.
In HP C for OpenVMS Systems, the two functions are functionally equivalent and move the cursor with the same efficiency.
Example 6-5 shows how to use the move and mvcur functions.
Example 6-5 The Cursor Movement Functions |
---|
#include <curses.h> main() { initscr(); . . . (1) clear(); (2) move(10, 10); (3) move(LINES/2, COLS/2); (4) mvcur(0, COLS-1, LINES-1, 0); . . . endwin(); } |
Key to Example 6-5:
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 |
---|
/* CHAP_6_STDSCR_OCCLUDE.C */ /* This 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. */ (1) getstr(str); /* Pause. Type a few words! */ mvaddstr(22, 1, str); (2) 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. */ (3) touchwin(stdscr); /* Refresh all of stdscr. */ getch(); /* Pause. Press Return. */ endwin(); /* Ends session. */ } |
Key to Example 6-6:
Figure 6-4 An Example of the getch Macro
Previous | Next | Contents | Index |