

General Programming Concepts: Writing and Debugging Programs

Manipulating Video Attributes

Your program can manipulate a number of video attributes.

The following sections provide information on video attributes and the subroutines that affect them.

Video Attributes, Bit Masks, and the Default Colors

Curses enables you to control the following attributes:

A_STANDOUT Terminal's best highlighting mode.
A_UNDERLINE Underline.
A_REVERSE Reverse video.
A_BLINK Blinking.
A_DIM Half-bright.
A_BOLD Extra bright or bold.
A_ALTCHARSET Alternate character set.
A_NORMAL Normal attributes.
COLOR_PAIR (Number) Displays the color pair represented by Number. You must have already initialized the color pair using the init_pair subroutine.

These attributes are defined in the curses.h file. You can pass attributes to the wattron, wattroff, and wattrset subroutines or you can OR them with the characters passed to the waddch subroutine. The C logical OR operator is a | (pipe symbol). The following bit masks are also provided:

A_NORMAL Turns all video attributes off.
A_CHARTEXT Extracts a character.
A_ATTRIBUTES Extracts attributes.
A_COLOR Extracts color-pair field information.

Two macros are provided for working with color pairs: COLOR_PAIR( Number) and PAIR_NUMBER( Attribute). The COLOR_PAIR( Number) macro and the A_COLOR mask are used by the PAIR_NUMBER( Attribute) macro to extract the color-pair number found in the attributes specified by the Attribute parameter.

If your program uses color, the curses.h file defines a number of macros that identify default colors. These colors are the following:

Color Integer Value

Curses assumes that the default background color for all terminals is 0 (COLOR_BLACK ).

Setting Video Attributes

The current window attributes are applied to all characters written into the window with the addch subroutines. These attributes remain as a property of the characters. The characters retain these attributes during terminal operations.

attroff or wattroff Turns off attributes.
attron orwattron Turns on attributes.
attrset or wattrset Sets the current attributes of a window.
standout, wstandout, standend, orwstandend
  Puts a window into and out of the terminal's best highlight mode.
vidputs or vidattr Outputs a string that puts the terminal in a video-attribute mode.

The attrset subroutine sets the current attributes of the default screen. The wattrset subroutine sets the current attributes of the user-defined window.

Use the attron and attroff subroutines to turn on and off the specified attributes in the stdscr without affecting any others. The wattron and wattroff subroutines perform the same actions in user-defined windows.

The standout subroutine is the same as a call to the attron subroutine with the A_STANDOUT attribute. It puts the stdscr into the terminal's best highlight mode. The wstandout subroutine is the same as a call to the wattron( Window, A_STANDOUT) subroutine. It puts the user-defined window into the terminal's best highlight mode. The standend subroutine is the same as a call to the attrset(0) subroutine. It turns off all attributes for stdscr. The wstandend subroutine is the same as a call to the wattrset( Window, 0) subroutine. It turns off all attributes for the specified window.

The vidputs subroutine outputs a string that puts the terminal in the specified attribute mode. Characters are output through the putc subroutine. The vidattr subroutine is the same as the vidputs subroutine except that characters are output through the putchar subroutine.

Working with Color Pairs

The COLOR_PAIR (Number) macro is defined in the curses.h file so you can manipulate color attributes as you would any other attributes. You must initialize a color pair with the init_pair subroutine before you use it. The init_pair subroutine has three parameters Pair, Foreground, and Background. The Pair parameter must be between 1 and COLOR_PAIRS -1. The Foreground and Background parameters must be between 0 and COLORS -1. For example, to initialize color pair 1 to a foreground of black with a background of cyan, you would use the following:

init_pair(1, COLOR_BLACK, COLOR_CYAN);

You could then set the attributes for the window as:

wattrset(win, COLOR_PAIR(1));

If you then write the string Let's add Color to the terminal, the string appears as black characters on a cyan background.

Extracting Attributes

You can use the results from the call to the winch subroutine to extract attribute information, including the color-pair number. The following example uses the value returned by a call to the winch subroutine with the C logical AND operator (&) and the A_ATTRIBUTES bit mask to extract the attributes assigned to the current position in the window. The results from this operation are used with the PAIR_NUMBER macro to extract the color-pair number, and the number 1 is printed on the screen.

win = newwin(10, 10, 0, 0);
init_pair(1, COLOR_RED, COLOR_YELLOW);
wattrset(win, COLOR_PAIR(1));
waddstr(win, "apple");
number = PAIR_NUMBER((mvwinch(win, 0, 0) & A_ATTRIBUTES));
wprintw(win, "%d\n", number);

Lights and Whistles

The curses library provides alarm subroutines to signal the user.

beep Sounds an audible alarm on the terminal
flash Displays a visible alarm on the terminal

Setting Curses Options

All curses options are initially turned off, so it is not necessary to turn them off before calling the endwin subroutine. The following subroutines allow you to set various options with curses:

curs_set Sets the cursor visibility to invisible, normal, or very visible.
idlok Specifies whether curses can use the hardware insert and delete line features of terminals so equipped.
intrflush Specifies whether an interrupt key (interrupt, quit, or suspend) flushes all output in the tty driver. This option's default is inherited from the tty driver.
keypad Specifies whether curses retrieves the information from the terminal's keypad. If enabled, the user can press a function key (such as an arrow key) and the wgetch subroutine returns a single value representing that function key. If disabled, curses will not treat the function keys specially and your program must interpret the escape sequences. For a list of these function keys, see the wgetch subroutine.
typeahead Instructs curses to check for type ahead in an alternative file descriptor.

See the wgetch subroutines and Setting Terminal Input and Output Modes for descriptions of additional curses options.

Related Information

Chapter 2, The Curses Library

List of Additional Curses Subroutines
