210 likes | 373 Views
The Curses Library. CNS 3060. Originally developed as part of BSD Unix, the curses library provides a halfway house between line oriented terminal programs and GUI applications. Most Unix systems, including OS/X now use an updated version called ncurses.
E N D
The Curses Library CNS 3060
Originally developed as part of BSD Unix, the curses library provides a halfway house between line oriented terminal programs and GUI applications. Most Unix systems, including OS/X now use an updated version called ncurses
When compiling curses programs you must #include <curses.h> compile with -lcurses option
the stdscr data structure is where data is output by default. The refresh( ) function updates the screen with the data in stdscr. The default input mode is canonical. 0,0 LINES, COLUMNS
When refresh( ) is called, the contents of stdscr is compared with a like data structure, curscr, which contains the current contents of the screen. Curses uses the differences between the two data structures to update the screen. 0,0 LINES, COLUMNS
#include <curses.h> int main( ) { initscr( ); move(5, 15); printw(“%s”, Hello World”); refresh( ); sleep(3); endwin( ); return 0; }
gcc -lcurses -o ex1 ex1.c this option is required to link in the curses library.
#include <curses.h> int main( ) { initscr( ); move(5, 15); printw(“%s”, Hello World”); refresh( ); sleep(3); endwin( ); return 0; } This statement allocates storage for the curses data structures and initializes them.
#include <curses.h> int main( ) { initscr( ); move(5, 15); printw(“%s”, Hello World”); refresh( ); sleep(3); endwin( ); return 0; } This statement moves the cursor to row 5, and column 15.
Hello World #include <curses.h> int main( ) { initscr( ); move(5, 15); printw(“%s”, Hello World”); refresh( ); sleep(3); endwin( ); return 0; } This statement is like printf, but it writes to stdscr.
Hello World Hello World #include <curses.h> int main( ) { initscr( ); move(5, 15); printw(“%s”, Hello World”); refresh( ); sleep(3); endwin( ); return 0; } This statement causes the screen to be updated with the contents of stdscr.
Hello World Hello World #include <curses.h> int main( ) { initscr( ); move(5, 15); printw(“%s”, Hello World”); refresh( ); sleep(3); endwin( ); return 0; } Sleep for a little while so that you can see the output.
Hello World Hello World #include <curses.h> int main( ) { initscr( ); move(5, 15); printw(“%s”, Hello World”); refresh( ); sleep(3); endwin( ); return 0; } Terminates curses.
Output to the Screen int addch(const chtype char_to_add); curses has its own character type chtype, which may have more bits than a normal character. addch adds a character at the current position. It returns 0 on success and -1 on failure.
Output to the Screen int addstr(const char *string_to_add); adds the given string at the current location.
Clearing the Screen int clear( ); stores blanks at every screen position.
Character Attributes The curses library contains a handful of standard character attributes, defines by the constants A_BLINK A_BOLD A_DIM A_REVERSE A_STANDOUT A_UNDERLINE
Character Attributes int attron (chtype attribute); int attroff(chtype attribute); int standout( ); int standend( ); mcbeth.c
The Keyboard int echo( ); int noecho( ); int cbreak( ); int nocbreak( ); int raw( ) int noraw( );
Keyboard Input int getch( ); int getstr(char *string); int getnstr(char *string, int num_chars); * these will cause refresh( ) to be called.
For an in-depth tutorial on using curses, go to http://www.linux.com/howtos/NCURSES-Programming-HOWTO/ Includes using windows, panels, menus, etc...