ed! - Ncurses Tutorial (Introduction), Part 1


Links
Home
Ncurses Tutorial - 1
Ncurses Tutorial - 2
Ncurses Tutorial - 3
Design Procedure
Feedback

Support writeKA
writeKA Language Tools

Data last updated on 24-Mar-2002.
Standardised html and moved to new domain on 25-Feb-2006.



The Interface - making use of the ncurses library
For making the interface of the editor, the ncurses library provided by Linux will come in extremely handy. For an extensive help on the ncurses library, go to console mode in Linux and type the following command:

$) man ncurses

This man page provides a complete list of functions provided by the library and other related man pages. To make it easier, I'll provide you with some info. You'll need to have an idea of the following stuff before you start.


Next      Ncurses-Site-List

initscr()
This function initializes the screen and should be called before any of the ncurses library functions are used.

endwin()
This function resets the screen and should always be called before exiting any program that uses the ncurses library. If you have forgotten to use this you'll end up with a garbled screen. In such a case enter the following command to reset the console.
$) stty sane

WINDOW
This is the structure that holds information on each window that you create. It is only the representation of the window in memory and any change that you make to it will be displayed on screen only if you display the window using an explicit command, refresh(). You use pointers of type WINDOW as handles to operate on the windows you make. wrefresh(vin) will display on screen the WINDOW called vin. vin will have to be declared as shown below and memory will have to assigned to it before you get started.
When initscr() is called two WINDOWs, the stdscr and curscr come into existance. stdscr (standard screen) is what will be displayed by the next refresh() call and curscr is the current state of the screen. refresh() is equivalent to wrefresh(stdscr). stdscr and curscr have size equivalent to that of the screen.
WINDOW *vin;
vin = newwin(0,0,0,0);
wrefresh(vin);

wrefresh(WINDOW *)
This function is used to display the contents of a window on the screen. The WINDOW pointer will have to passed as an argument.
refresh() - This functions brings the contents off the stdscr onto the display.

newwin(int,int,int,int)
This function is used to assign memory from the heap. The first argument is the number of rows the window will have, the second argument is the number of columns, the third argument is the y co-ordinate of the first row and the fourth argument is the x co-ordinate of the first column. If a zero is passed as first/second argument then the maximum number of rows/columns possible is taken i.e. for a normal console display it would be 25 rows and 80 columns. The x-y co-ordinates start from 0 and hence the maximum y co-ordinate is 24 and maximum x co-ordinate is 79.

delwin(WINDOW *)
This function frees the memory allocated using newwin(). It does not in any way affect the display on screen. The pointer can be used to point to a new WINDOW if the programmer wishes after the memory has been de-allocated. Since C/C++ does not employ a garbage collector the programmer should be careful to de-allocate any memory that has been allocated from the heap. Hence for every newwin() called there should be a corresponding delwin() called before exiting the program.

Next