X-Git-Url: https://ncurses.scripts.mit.edu/?a=blobdiff_plain;f=doc%2Fhtml%2FNCURSES-Programming-HOWTO.html;h=2296cdabfec9802a252874a57484adf57c5414b1;hb=88595a127ec2e56af0875eb04e0f2396d6d121c5;hp=15655a802291759607602815727e26b7f50f7e3d;hpb=bca50d0d8592defee6c584fdedd25f4b1a31345b;p=ncurses.git diff --git a/doc/html/NCURSES-Programming-HOWTO.html b/doc/html/NCURSES-Programming-HOWTO.html index 15655a80..2296cdab 100644 --- a/doc/html/NCURSES-Programming-HOWTO.html +++ b/doc/html/NCURSES-Programming-HOWTO.html @@ -1,16 +1,13 @@ - - + "HTML Tidy for HTML5 for Linux version 5.6.0"> NCURSES Programming HOWTO -
@@ -24,24 +21,35 @@

+ "mailto:ppadala@gmail.com">ppadala@gmail.com> +

v1.9, 2005-06-20

- +
+ History + - + + + + - + + + + + + @@ -53,9 +61,7 @@ - - @@ -68,9 +74,7 @@ - - @@ -82,9 +86,7 @@ - - @@ -96,9 +98,7 @@ - - @@ -109,9 +109,7 @@ - - @@ -122,9 +120,7 @@ - - @@ -135,9 +131,7 @@ - - @@ -148,9 +142,7 @@ - - @@ -162,9 +154,7 @@ - - @@ -176,9 +166,7 @@ - - @@ -191,8 +179,7 @@
- - +

This document is intended to be an "All in One" guide for programming with ncurses and its sister libraries. We @@ -200,15 +187,18 @@ complex form manipulation. No prior experience in ncurses is assumed. Send comments to this - address

+ address +

+
-
Table of Contents
+
Table of Contents +
1. Introduction
@@ -309,7 +299,7 @@
6.1. addch() class of functions
-
6.2. mvaddch(), waddch() and +
6.2. mvaddch(), waddch() and mvwaddch()
6.3. printw() class of @@ -323,7 +313,7 @@
6.3.2. wprintw() and mvwprintw
-
6.3.3. vwprintw()
+
6.3.3. vw_printw()
6.3.4. A Simple printw example
@@ -355,7 +345,7 @@
7.2.2. wscanw() and mvwscanw()
-
7.2.3. vwscanw()
+
7.2.3. vw_scanw()
@@ -667,7 +657,7 @@ cables. The terminals could be configured by sending a series of bytes. All the capabilities (such as moving the cursor to a new location, erasing part of the screen, scrolling the - screen, changing modes etc.) of terminals could be accessed + screen, changing modes, etc.) of terminals could be accessed through these series of bytes. These control seeuqnces are usually called escape sequences, because they start with an escape(0x1B) character. Even today, with proper emulation, we @@ -676,20 +666,16 @@

Suppose you wanted to print a line in color. Try typing this on your console.

-
-echo "^[[0;31;40mIn Color"
-
+
echo "^[[0;31;40mIn Color"

The first character is an escape character, which looks like two characters ^ and [. To be able to print it, you have to press CTRL+V and then the ESC key. All the others are normal printable characters. You should be able to see the string "In Color" in red. It stays that way and to revert back to the original mode type this.

-
-echo "^[[0;37;40m"
-
+
echo "^[[0;37;40m"

Now, what do these magic characters mean? Difficult to comprehend? They might even be different for different terminals. So the designers of UNIX invented a mechanism @@ -711,14 +697,14 @@ echo "^[[0;37;40m"

You might be wondering, what the import of all this technical gibberish is. In the above scenario, every application program is supposed to query the terminfo and - perform the necessary stuff (sending control characters + perform the necessary stuff (sending control characters, etc.). It soon became difficult to manage this complexity and this gave birth to 'CURSES'. Curses is a pun on the name "cursor optimization". The Curses library forms a wrapper over working with raw terminal codes, and provides highly flexible and efficient API (Application Programming Interface). It provides functions to move the cursor, - create windows, produce colors, play with mouse etc. The + create windows, produce colors, play with mouse, etc. The application programs need not worry about the underlying terminal capabilities.

@@ -732,9 +718,9 @@ echo "^[[0;37;40m"

A detailed history of NCURSES can be found in the NEWS file from the source distribution. The current package is - maintained by Thomas Dickey. You can contact the maintainers - at Thomas Dickey. You can contact the + maintainers at bug-ncurses@gnu.org.

@@ -747,7 +733,7 @@ echo "^[[0;37;40m"

NCURSES not only creates a wrapper over terminal capabilities, but also gives a robust framework to create nice looking UI (User Interface)s in text mode. It provides - functions to create windows etc. Its sister libraries + functions to create windows, etc. Its sister libraries panel, menu and form provide an extension to the basic curses library. These libraries usually come along with curses. One can create applications that contain multiple @@ -778,38 +764,39 @@ echo "^[[0;37;40m" on.

Compiling the - package

+ package +

NCURSES can be obtained from ftp://ftp.gnu.org/pub/gnu/ncurses/ncurses.tar.gz or any of the ftp sites mentioned in http://www.gnu.org/order/ftp.html.

+ "https://www.gnu.org/order/ftp.html" target= + "_top">https://www.gnu.org/order/ftp.html.

Read the README and INSTALL files for details on to how to install it. It usually involves the following operations.

-
-    tar zxvf ncurses<version>.tar.gz  # unzip and untar the archive
+
+        
    tar zxvf ncurses<version>.tar.gz  # unzip and untar the archive
     cd ncurses<version>               # cd to the directory
     ./configure                             # configure the build according to your 
                                             # environment
     make                                    # make it
     su root                                 # become root
-    make install                            # install it
-
- + make install # install it

Using the - RPM

+ RPM +

NCURSES RPM can be found and downloaded from http://rpmfind.net . - The RPM can be installed with the following command after + "https://rpmfind.net" target="_top">https://rpmfind.net + . The RPM can be installed with the following command after becoming root.

-
-    rpm -i <downloaded rpm>
-
+ +
    rpm -i <downloaded rpm>
@@ -834,11 +821,11 @@ echo "^[[0;37;40m"

All the programs in the document are available in zipped form here. Unzip and untar it. The directory structure looks like this.

-
-ncurses
+
+        
ncurses
    |
    |----> JustForFun     -- just for fun programs
    |----> basics         -- basic programs
@@ -852,13 +839,12 @@ ncurses
    |                            by Anuradha Ratnaweera)
    |----> Makefile       -- the top level Makefile
    |----> README         -- the top level README file. contains instructions
-   |----> COPYING        -- copyright notice
-
- + |----> COPYING -- copyright notice

The individual directories contain the following files.

-
-Description of files in each directory
+
+        
Description of files in each directory
 --------------------------------------
 JustForFun
     |
@@ -899,7 +885,7 @@ JustForFun
   menus 
     |
     |----> menu_attrib.c     -- Usage of menu attributes
-    |----> menu_item_data.c  -- Usage of item_name() etc.. functions
+    |----> menu_item_data.c  -- Usage of item_name(), etc. functions
     |----> menu_multi_column.c    -- Creates multi columnar menus
     |----> menu_scroll.c     -- Demonstrates scrolling capability of menus
     |----> menu_simple.c     -- A simple menu accessed by arrow keys
@@ -917,9 +903,7 @@ JustForFun
     |----> panel_simple.c    -- A simple panel example
 
   perl
-    |----> 01-10.pl          -- Perl equivalents of first ten example programs
-
- + |----> 01-10.pl -- Perl equivalents of first ten example programs

There is a top level Makefile included in the main directory. It builds all the files and puts the ready-to-use exes in demo/exe directory. You can also do @@ -932,9 +916,9 @@ JustForFun

If you prefer browsing individual programs, point your browser to http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/ncurses_programs/

+ "_top">https://tldp.org/HOWTO/NCURSES-Programming-HOWTO/ncurses_programs/

All the programs are released under the same license that is used by ncurses (MIT-style). This gives you the @@ -949,7 +933,7 @@ JustForFun

1.6. Other Formats of the document

-

This howto is also availabe in various other formats on +

This howto is also available in various other formats on the tldp.org site. Here are the links to other formats of this document.

@@ -963,25 +947,25 @@ JustForFun @@ -995,8 +979,9 @@ JustForFun

If above links are broken or if you want to experiment with sgml read on.

-
-
    Get both the source and the tar,gzipped programs, available at
+
+          

    Get both the source and the tar,gzipped programs, available at
         http://cvsview.tldp.org/index.cgi/LDP/howto/docbook/
         NCURSES-HOWTO/NCURSES-Programming-HOWTO.sgml
         http://cvsview.tldp.org/index.cgi/LDP/howto/docbook/
@@ -1017,13 +1002,11 @@ JustForFun
         NCURSES-ONE-BIG-FILE.html
     for ps, you would use
         htmldoc --size universal -t ps --firstpage p1 -f <output file name.ps>
-        NCURSES-ONE-BIG-FILE.html
-
- + NCURSES-ONE-BIG-FILE.html

See LDP Author guide for more details. If all else - failes, mail me at ppadala@gmail.com

@@ -1147,27 +1130,25 @@ JustForFun

2.1. Compiling With the NCURSES - Library

+ "COMPILECURSES">2.1. Compiling With the NCURSES Library

To use ncurses library functions, you have to include ncurses.h in your programs. To link the program with ncurses the flag -lncurses should be added.

-
-    #include <ncurses.h>
+
+        
    #include <ncurses.h>
     .
     .
     .
 
-    compile and link: gcc <program file> -lncurses
-
- + compile and link: gcc <program file> -lncurses
+

Example 1. The Hello World !!! Program +

-

Example 1. The Hello World !!! Program

-
-#include <ncurses.h>
+          
#include <curses.h>
 
 int main()
 {       
@@ -1178,8 +1159,7 @@ int main()
         endwin();                       /* End curses mode                */
 
         return 0;
-}
-
+}
@@ -1291,7 +1271,7 @@ int main() initscr() has to be called. There are functions which can be called after this initialization to customize our curses session. We may ask the curses system to set the terminal in - raw mode or initialize color or initialize the mouse etc.. + raw mode or initialize color or initialize the mouse, etc. Let's discuss some of the functions that are normally called immediately after initscr();

@@ -1335,7 +1315,7 @@ int main() "LITERAL">noecho() switches off echoing. The reason you might want to do this is to gain more control over echoing or to suppress unnecessary echoing while taking - input from the user through the getch() etc. functions. + input from the user through the getch(), etc. functions. Most of the interactive programs call noecho() at initialization and do the echoing of characters in a controlled manner. It gives the @@ -1351,7 +1331,7 @@ int main() keypad()

This is my favorite initialization function. It enables - the reading of function keys like F1, F2, arrow keys etc. + the reading of function keys like F1, F2, arrow keys, etc. Almost every interactive program enables this, as arrow keys are a major part of any User Interface. Do keypad(stdscr, TRUE) to enable this feature @@ -1401,11 +1381,12 @@ int main()

-

Example 2. Initialization Function Usage - example

-
-#include <ncurses.h>
+          example
+          

+ +
#include <curses.h>
 
 int main()
 {       int ch;
@@ -1422,7 +1403,7 @@ int main()
         if(ch == KEY_F(1))              /* Without keypad enabled this will */
                 printw("F1 Key pressed");/*  not get to us either       */
                                         /* Without noecho() some ugly escape
-                                         * charachters might have been printed
+                                         * characters might have been printed
                                          * on screen                    */
         else
         {       printw("The pressed key is ");
@@ -1435,8 +1416,7 @@ int main()
         endwin();                       /* End curses mode                */
 
         return 0;
-}
-
+}

This program is self-explanatory. But I used functions @@ -1470,17 +1450,15 @@ int main() creates a default window named stdscr which represents your 80x25 (or the size of window in which you are running) screen. If you are - doing simple tasks like printing few strings, reading input + doing simple tasks like printing few strings, reading input, etc., you can safely use this single window for all of your purposes. You can also create windows and call functions which explicitly work on the specified window.

For example, if you call

-
-    printw("Hi There !!!");
-    refresh();
-
+
    printw("Hi There !!!");
+    refresh();

It prints the string on stdscr at the present cursor position. Similarly the call to refresh(), works on stdscr only.

@@ -1488,23 +1466,20 @@ int main()

Say you have created windows then you have to call a function with a 'w' added to the usual function.

-
-    wprintw(win, "Hi There !!!");
-    wrefresh(win);
-
+
    wprintw(win, "Hi There !!!");
+    wrefresh(win);

As you will see in the rest of the document, naming of functions follow the same convention. For each function there usually are three more functions.

-
-    printw(string);        /* Print on stdscr at present cursor position */
+
+      
    printw(string);        /* Print on stdscr at present cursor position */
     mvprintw(y, x, string);/* Move to (y, x) then print string     */
     wprintw(win, string);  /* Print on window win at present cursor position */
                            /* in the window */
     mvwprintw(win, y, x, string);   /* Move to (y, x) relative to window */
-                                    /* co-ordinates and then print         */
-
- + /* co-ordinates and then print */

Usually the w-less functions are macros which expand to corresponding w-function with stdscr as the window parameter.

@@ -1539,7 +1514,7 @@ int main() -

These functions can be used interchangeably and it's a +

These functions can be used interchangeably and it is a matter of style as to which class is used. Let's see each one in detail.

@@ -1570,9 +1545,9 @@ int main() For example, you want to print a character ch(of type char) bold and underlined, you would call addch() as below.

-
-    addch(ch | A_BOLD | A_UNDERLINE);
-
+ +
    addch(ch | A_BOLD | A_UNDERLINE);
  • @@ -1590,7 +1565,7 @@ int main()

    Additionally, curses provides some special characters for character-based graphics. You can draw tables, horizontal or vertical lines, etc. You can - find all avaliable characters in the header file ncurses.h. Try looking for macros beginning with ACS_ in this file.

    @@ -1598,22 +1573,18 @@ int main()

    -

    6.2. +

    6.2. mvaddch(), waddch() and mvwaddch()

    mvaddch() is used to move the cursor to a given point, and then print. Thus, the calls:

    -
    -    move(row,col);    /* moves the cursor to rowth row and colth column */
    -    addch(ch);
    -
    can be replaced by -
    -    mvaddch(row,col,ch);
    -
    +
        move(row,col);    /* moves the cursor to rowth row and colth column */
    +    addch(ch);
    can be replaced by + +
        mvaddch(row,col,ch);

    waddch() is similar to addch(), except that it adds a character into the given window. (Note that

    6.3.2. wprintw() and - mvwprintw

    + "WPRINTWMVWPRINTW">6.3.2. wprintw() and mvwprintw

    These two functions are similar to above two except that they print in the corresponding window given as @@ -1679,7 +1649,7 @@ int main()


    6.3.3. - vwprintw()

    + vw_printw()

    This function is similar to vprintf(). This can be used when variable @@ -1694,18 +1664,18 @@ int main()

    +

    Example 3. A Simple printw example +

    -

    Example 3. A Simple printw example

    -
    -#include <ncurses.h>                   /* ncurses.h includes stdio.h */  
    +            
    #include <curses.h>
     #include <string.h> 
      
     int main()
     {
      char mesg[]="Just a string";           /* message to be appeared on the screen */
      int row,col;                           /* to store the number of rows and *
    -                                         * the number of colums of the screen */
    +                                         * the number of columns of the screen */
      initscr();                             /* start the curses mode */
      getmaxyx(stdscr,row,col);              /* get the number of rows and columns */
      mvprintw(row/2,(col-strlen(mesg))/2,"%s",mesg);
    @@ -1717,8 +1687,7 @@ int main()
      endwin();
     
      return 0;
    -}
    -
    +}

    Above program demonstrates how easy it is to use @@ -1860,7 +1829,7 @@ int main()


    7.2.3. - vwscanw()

    + vw_scanw()

    This function is similar to vscanf(). This can be used when a variable @@ -1891,11 +1860,11 @@ int main()

    +

    Example 4. A Simple scanw example +

    -

    Example 4. A Simple scanw example

    -
    -#include <ncurses.h>                   /* ncurses.h includes stdio.h */  
    +          
    #include <curses.h>
     #include <string.h> 
      
     int main()
    @@ -1903,7 +1872,7 @@ int main()
      char mesg[]="Enter a string: ";                /* message to be appeared on the screen */
      char str[80];
      int row,col;                           /* to store the number of rows and *
    -                                         * the number of colums of the screen */
    +                                         * the number of columns of the screen */
      initscr();                             /* start the curses mode */
      getmaxyx(stdscr,row,col);              /* get the number of rows and columns */
      mvprintw(row/2,(col-strlen(mesg))/2,"%s",mesg);
    @@ -1914,8 +1883,7 @@ int main()
      endwin();
     
      return 0;
    -}
    -
    +}
    @@ -1935,12 +1903,12 @@ int main()
    +

    Example 5. A Simple Attributes example +

    -

    Example 5. A Simple Attributes example

    -
    -/* pager functionality by Joseph Spainhour" <spainhou@bellsouth.net> */
    -#include <ncurses.h>
    +        
    /* pager functionality by Joseph Spainhour" <spainhou@bellsouth.net> */
    +#include <curses.h>
     #include <stdlib.h>
     
     int main(int argc, char *argv[])
    @@ -1965,7 +1933,7 @@ int main(int argc, char *argv[])
       getmaxyx(stdscr, row, col);           /* find the boundaries of the screeen */
       while((ch = fgetc(fp)) != EOF)        /* read the file till we reach the end */
       {
    -    getyx(stdscr, y, x);                /* get the current curser position */
    +    getyx(stdscr, y, x);                /* get the current cursor position */
         if(y == (row - 1))                  /* are we are at the end of the screen */
         {
           printw("<-Press Any Key->");      /* tell the user to press a key */
    @@ -1977,7 +1945,7 @@ int main(int argc, char *argv[])
                                              * switch bold on */    
         {
           attron(A_BOLD);                   /* cut bold on */
    -      getyx(stdscr, y, x);              /* get the current curser position */
    +      getyx(stdscr, y, x);              /* get the current cursor position */
           move(y, x - 1);                   /* back up one space */
           printw("%c%c", '/', ch);          /* The actual printing is done here */
         }
    @@ -1992,8 +1960,7 @@ int main(int argc, char *argv[])
       endwin();                             /* End curses mode */
       fclose(fp);
       return 0;
    -}
    -
    +}

    Don't worry about all those initialization and other crap. @@ -2026,7 +1993,7 @@ int main(int argc, char *argv[])

    Let's get into more details of attributes. The functions attron(), attroff(), attrset() , and their sister functions attr_get() etc.. can be used to switch + "LITERAL">attr_get(), etc. can be used to switch attributes on/off , get attributes and produce a colorful display.

    @@ -2034,8 +2001,8 @@ int main(int argc, char *argv[]) attributes and switch them on or off, respectively. The following video attributes, which are defined in <curses.h> can be passed to these functions.

    -
    -    
    +
    +        
        
         A_NORMAL        Normal display (no highlight)
         A_STANDOUT      Best highlighting mode of the terminal.
         A_UNDERLINE     Underlining
    @@ -2048,9 +2015,7 @@ int main(int argc, char *argv[])
         A_ALTCHARSET    Alternate character set
         A_CHARTEXT      Bit-mask to extract a character
         COLOR_PAIR(n)   Color-pair number n 
    -    
    -
    - +

    The last one is the most colorful one :-) Colors are explained in the next sections.

    @@ -2058,9 +2023,9 @@ int main(int argc, char *argv[])

    We can OR(|) any number of above attributes to get a combined effect. If you wanted reverse video with blinking characters you can use

    -
    -    attron(A_REVERSE | A_BLINK);
    -
    + +
        attron(A_REVERSE | A_BLINK);
    @@ -2108,8 +2073,8 @@ int main(int argc, char *argv[])

    8.4. attr_ functions

    -

    There are series of functions like attr_set(), attr_on - etc.. These are similar to above functions except that they +

    There are series of functions like attr_set(), attr_on, + etc. These are similar to above functions except that they take parameters of type attr_t.

    @@ -2141,10 +2106,9 @@ int main(int argc, char *argv[])

    We can give -1 as the character count to update till end of line. If you want to change attributes of characters from current position to end of line, just use this.

    -
    -    chgat(-1, A_REVERSE, 0, NULL);
    -
    +
        chgat(-1, A_REVERSE, 0, NULL);

    This function is useful when changing attributes for characters that are already on the screen. Move to the character from which you want to change and change the @@ -2159,10 +2123,11 @@ int main(int argc, char *argv[])

    +

    Example 6. Chgat() Usage example +

    -

    Example 6. Chgat() Usage example

    -
    -#include <ncurses.h>
    +          
    #include <curses.h>
     
     int main(int argc, char *argv[])
     {       initscr();                      /* Start curses mode            */
    @@ -2176,7 +2141,7 @@ int main(int argc, char *argv[])
              * Third parameter number of characters to update. -1 means till 
              * end of line
              * Forth parameter is the normal attribute you wanted to give 
    -         * to the charcter
    +         * to the character
              * Fifth is the color index. It is the index given during init_pair()
              * use 0 if you didn't want color
              * Sixth one is always NULL 
    @@ -2185,8 +2150,7 @@ int main(int argc, char *argv[])
             getch();
             endwin();                       /* End curses mode                */
             return 0;
    -}
    -
    +}

    This example also introduces us to the color world of @@ -2224,13 +2188,13 @@ int main(int argc, char *argv[]) newwin(). It doesn't create any thing on the screen actually. It allocates memory for a structure to manipulate the window and updates the - structure with data regarding the window like it's size, - beginy, beginx etc.. Hence in curses, a window is just an + structure with data regarding the window such as its size, + beginy, beginx, etc. Hence in curses, a window is just an abstraction of an imaginary window, which can be manipulated independent of other parts of screen. The function newwin() returns a pointer to structure WINDOW, - which can be passed to window related functions like - wprintw() etc.. Finally the window can be destroyed with + which can be passed to window related functions such as + wprintw(), etc. Finally the window can be destroyed with delwin(). It will deallocate the memory associated with the window structure.

    @@ -2249,11 +2213,11 @@ int main(int argc, char *argv[])
    +

    Example 7. Window Border example +

    -

    Example 7. Window Border example

    -
    -#include <ncurses.h>
    -
    +          
    #include <curses.h>
     
     WINDOW *create_newwin(int height, int width, int starty, int startx);
     void destroy_win(WINDOW *local_win);
    @@ -2316,7 +2280,7 @@ WINDOW *create_newwin(int height, int width, int starty, int startx)
     void destroy_win(WINDOW *local_win)
     {       
             /* box(local_win, ' ', ' '); : This won't produce the desired
    -         * result of erasing the window. It will leave it's four corners 
    +         * result of erasing the window. It will leave its four corners 
              * and so an ugly remnant of window. 
              */
             wborder(local_win, ' ', ' ', ' ',' ',' ',' ',' ',' ');
    @@ -2333,8 +2297,7 @@ void destroy_win(WINDOW *local_win)
              */
             wrefresh(local_win);
             delwin(local_win);
    -}
    -
    +}
    @@ -2344,7 +2307,7 @@ void destroy_win(WINDOW *local_win)

    9.3. Explanation

    -

    Don't scream. I know it's a big example. But I have to +

    Don't scream. I know it is a big example. But I have to explain some important things here :-). This program creates a rectangular window that can be moved with left, right, up, down arrow keys. It repeatedly creates and @@ -2369,21 +2332,19 @@ void destroy_win(WINDOW *local_win) border around the window with the characters given to it as the 4 corner points and the 4 lines. To put it clearly, if you have called wborder as below:

    -
    -    wborder(win, '|', '|', '-', '-', '+', '+', '+', '+');
    -
    -

    it produces some thing like

    -
    -    +------------+
    +        
        wborder(win, '|', '|', '-', '-', '+', '+', '+', '+');
    +

    it produces something like

    + +
        +------------+
         |            |
         |            |
         |            |
         |            |
         |            |
         |            |
    -    +------------+
    -
    + +------------+
    @@ -2423,10 +2384,11 @@ void destroy_win(WINDOW *local_win)
    +

    Example 8. More border functions +

    -

    Example 8. More border functions

    -
    -#include <ncurses.h>
    +          
    #include <curses.h>
     
     typedef struct _win_border_struct {
             chtype  ls, rs, ts, bs, 
    @@ -2545,8 +2507,7 @@ void create_box(WIN *p_win, bool flag)
                                     
             refresh();
     
    -}
    -
    +}
    @@ -2554,8 +2515,7 @@ void create_box(WIN *p_win, bool flag)

    -

    10. - Colors

    +

    10. Colors

    +

    Example 9. A Simple Color example +

    -

    Example 9. A Simple Color example

    -
    -#include <ncurses.h>
    +          
    #include <stdlib.h>
    +#include <string.h>
    +#include <curses.h>
     
     void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string);
     int main(int argc, char *argv[])
    @@ -2609,8 +2572,7 @@ void print_in_middle(WINDOW *win, int starty, int startx, int width, char *strin
             mvwprintw(win, y, x, "%s", string);
             refresh();
     }
    -
    -
    +

    As you can see, to start using color, you should first @@ -2624,36 +2586,35 @@ void print_in_middle(WINDOW *win, int starty, int startx, int width, char *strin

    Curses initializes all the colors supported by terminal when start_color() is called. These can be accessed by the define constants like COLOR_BLACK - etc. Now to actually start using colors, you have to define - pairs. Colors are always used in pairs. That means you have - to use the function init_pair() to - define the foreground and background for the pair number - you give. After that that pair number can be used as a - normal attribute with COLOR_PAIR()function. This may seem to be - cumbersome at first. But this elegant solution allows us to - manage color pairs very easily. To appreciate it, you have - to look into the the source code of "dialog", a utility for - displaying dialog boxes from shell scripts. The developers - have defined foreground and background combinations for all - the colors they might need and initialized at the - beginning. This makes it very easy to set attributes just - by accessing a pair which we already have defined as a - constant.

    + , etc. Now to actually start using colors, you have to + define pairs. Colors are always used in pairs. That means + you have to use the function init_pair() to define the foreground and + background for the pair number you give. After that that + pair number can be used as a normal attribute with + COLOR_PAIR()function. This may + seem to be cumbersome at first. But this elegant solution + allows us to manage color pairs very easily. To appreciate + it, you have to look into the the source code of "dialog", + a utility for displaying dialog boxes from shell scripts. + The developers have defined foreground and background + combinations for all the colors they might need and + initialized at the beginning. This makes it very easy to + set attributes just by accessing a pair which we already + have defined as a constant.

    The following colors are defined in curses.h. You can use these as parameters for various color functions.

    -
    -        COLOR_BLACK   0
    +
    +        
            COLOR_BLACK   0
             COLOR_RED     1
             COLOR_GREEN   2
             COLOR_YELLOW  3
             COLOR_BLUE    4
             COLOR_MAGENTA 5
             COLOR_CYAN    6
    -        COLOR_WHITE   7
    -
    + COLOR_WHITE 7
    @@ -2667,12 +2628,11 @@ void print_in_middle(WINDOW *win, int starty, int startx, int width, char *strin curses initially. Say you wanted to lighten the intensity of red color by a minuscule. Then you can use this function as

    -
    -    init_color(COLOR_RED, 700, 0, 0);
    -    /* param 1     : color name
    -     * param 2, 3, 4 : rgb content min = 0, max = 1000 */
    -
    +
        init_color(COLOR_RED, 700, 0, 0);
    +    /* param 1     : color name
    +     * param 2, 3, 4 : rgb content min = 0, max = 1000 */

    If your terminal cannot change the color definitions, the function returns ERR. The function can_change_color() can be used to find out @@ -2710,14 +2670,14 @@ void print_in_middle(WINDOW *win, int starty, int startx, int width, char *strin sensitive to key presses or the mouse actions done by the user. Let's deal with the keys first.

    -

    As you have seen in almost all of the above examples, - it's very easy to get key input from the user. A simple way +

    As you have seen in almost all of the above examples, it + is very easy to get key input from the user. A simple way of getting key presses is to use getch() function. The cbreak mode should be enabled to read keys when you are interested in reading individual key hits rather than complete lines of text (which usually end with a carriage return). keypad should - be enabled to get the Functions keys, arrow keys etc. See + be enabled to get the Functions keys, arrow keys, etc. See the initialization section for details.

    getch() returns an integer @@ -2731,12 +2691,10 @@ void print_in_middle(WINDOW *win, int starty, int startx, int width, char *strin keys portable and easy to manage.

    For example, if you call getch() like this

    -
    -    int ch;
     
    -    ch = getch();
    -
    +
        int ch;
     
    +    ch = getch();

    getch() will wait for the user to press a key, (unless you specified a timeout) and when user presses a key, the corresponding integer is returned. Then you can check the @@ -2744,11 +2702,9 @@ void print_in_middle(WINDOW *win, int starty, int startx, int width, char *strin match against the keys you want.

    The following code piece will do that job.

    -
    -    if(ch == KEY_LEFT)
    -        printw("Left arrow is pressed\n");
    -
    +
        if(ch == KEY_LEFT)
    +        printw("Left arrow is pressed\n");

    Let's write a small program which creates a menu which can be navigated by up and down arrows.

    @@ -2761,11 +2717,11 @@ void print_in_middle(WINDOW *win, int starty, int startx, int width, char *strin
    +

    Example 10. A Simple Key Usage example +

    -

    Example 10. A Simple Key Usage example

    -
    -#include <stdio.h>
    -#include <ncurses.h>
    +          
    #include <curses.h>
     
     #define WIDTH 30
     #define HEIGHT 10 
    @@ -2820,7 +2776,7 @@ int main()
                                     choice = highlight;
                                     break;
                             default:
    -                                mvprintw(24, 0, "Charcter pressed is = %3d Hopefully it can be printed as '%c'", c, c);
    +                                mvprintw(24, 0, "Character pressed is = %3d Hopefully it can be printed as '%c'", c, c);
                                     refresh();
                                     break;
                     }
    @@ -2835,7 +2791,6 @@ int main()
             return 0;
     }
     
    -
     void print_menu(WINDOW *menu_win, int highlight)
     {
             int x, y, i;    
    @@ -2855,8 +2810,7 @@ void print_menu(WINDOW *menu_win, int highlight)
             }
             wrefresh(menu_win);
     }
    -
    -
    +
    @@ -2880,11 +2834,10 @@ void print_menu(WINDOW *menu_win, int highlight)

    Before you do any thing else, the events you want to receive have to be enabled with mousemask().

    -
    -    mousemask(  mmask_t newmask,    /* The events you want to listen to */
    -                mmask_t *oldmask)    /* The old events mask                */
    -
    +
        mousemask(  mmask_t newmask,    /* The events you want to listen to */
    +                mmask_t *oldmask)    /* The old events mask                */

    The first parameter to above function is a bit mask of events you would like to listen. By default, all the events are turned off. The bit mask

    The following are all the event masks:

    -
    -    Name            Description
    +
    +        
        Name            Description
            ---------------------------------------------------------------------
            BUTTON1_PRESSED          mouse button 1 down
            BUTTON1_RELEASED         mouse button 1 up
    @@ -2919,8 +2872,7 @@ void print_menu(WINDOW *menu_win, int highlight)
            BUTTON_CTRL              control was down during button state change
            BUTTON_ALT               alt was down during button state change
            ALL_MOUSE_EVENTS         report all button state changes
    -       REPORT_MOUSE_POSITION    report mouse movement
    -
    + REPORT_MOUSE_POSITION report mouse movement
    @@ -2935,38 +2887,34 @@ void print_menu(WINDOW *menu_win, int highlight) getmouse().

    The code approximately looks like this:

    -
    -    MEVENT event;
    +
    +        
        MEVENT event;
     
         ch = getch();
         if(ch == KEY_MOUSE)
             if(getmouse(&event) == OK)
                 .    /* Do some thing with the event */
                 .
    -            .
    -
    - + .

    getmouse() returns the event into the pointer given to - it. It's a structure which contains

    -
    -    typedef struct
    +        it. It is a structure which contains

    + +
        typedef struct
         {
             short id;         /* ID to distinguish multiple devices */
             int x, y, z;      /* event coordinates */
             mmask_t bstate;   /* button state bits */
    -    }    
    -
    - + }

    The bstate is the main variable we are interested in. It tells the button state of the mouse.

    Then with a code snippet like the following, we can find out what happened.

    -
    -    if(event.bstate & BUTTON1_PRESSED)
    -        printw("Left Button Pressed");
    -
    + +
        if(event.bstate & BUTTON1_PRESSED)
    +        printw("Left Button Pressed");
    @@ -2981,10 +2929,12 @@ void print_menu(WINDOW *menu_win, int highlight)
    +

    Example 11. Access the menu with mouse !!! +

    -

    Example 11. Access the menu with mouse !!!

    -
    -#include <ncurses.h>
    +          
    #include <string.h>
    +#include <curses.h>
     
     #define WIDTH 30
     #define HEIGHT 10 
    @@ -3053,7 +3003,6 @@ end:
             return 0;
     }
     
    -
     void print_menu(WINDOW *menu_win, int highlight)
     {
             int x, y, i;    
    @@ -3089,8 +3038,7 @@ void report_choice(int mouse_x, int mouse_y, int *p_choice)
                                     *p_choice = choice + 1; 
                             break;
                     }
    -}
    -
    +}
    @@ -3134,18 +3082,16 @@ void report_choice(int mouse_x, int mouse_y, int *p_choice) fill the values of x and y co-ordinates in the arguments given to it. Since getyx() is a macro you don't have to pass the address of the variables. It can be called as

    -
    -    getyx(win, y, x);
    +
    +        
        getyx(win, y, x);
         /* win: window pointer
          *   y, x: y, x co-ordinates will be put into this variables 
    -     */
    -
    - + */

    The function getparyx() gets the beginning co-ordinates of the sub window relative to the main window. This is some times useful to update a sub window. When designing fancy stuff like writing multiple menus, it becomes difficult to - store the menu positions, their first option co-ordinates + store the menu positions, their first option co-ordinates, etc. A simple solution to this problem, is to create menus in sub windows and later find the starting co-ordinates of the menus by using getparyx().

    @@ -3188,7 +3134,7 @@ void report_choice(int mouse_x, int mouse_y, int *p_choice) used to copy a window completely onto another window. It takes the source and destination windows as parameters and according to the rectangle specified, it copies the - rectangular region from source to destination window. It's + rectangular region from source to destination window. Its last parameter specifies whether to overwrite or just overlay the contents on to the destination window. If this argument is true, then the copying is non-destructive.

    @@ -3214,11 +3160,10 @@ void report_choice(int mouse_x, int mouse_y, int *p_choice)

    This function can be used to make the cursor invisible. The parameter to this function should be

    -
    -    0 : invisible      or
    +
    +        
        0 : invisible      or
         1 : normal    or
    -    2 : very visible.
    -
    + 2 : very visible.
    @@ -3242,10 +3187,12 @@ void report_choice(int mouse_x, int mouse_y, int *p_choice)
    +

    Example 12. Temporarily Leaving Curses Mode +

    -

    Example 12. Temporarily Leaving Curses Mode

    -
    -#include <ncurses.h>
    +          
    #include <stdlib.h>
    +#include <curses.h>
     
     int main()
     {       
    @@ -3264,8 +3211,7 @@ int main()
             endwin();                       /* End curses mode                */
     
             return 0;
    -}
    -
    +}
    @@ -3281,15 +3227,16 @@ int main() box() use these characters. All these variables start with ACS meaning alternative character set. You might have noticed me using these - characters in some of the programs above. Here's an example - showing all the characters.

    + characters in some of the programs above. Here is an + example showing all the characters.

    +

    Example 13. ACS Variables Example +

    -

    Example 13. ACS Variables Example

    -
    -#include <ncurses.h>
    +          
    #include <curses.h>
     
     int main()
     {
    @@ -3332,8 +3279,7 @@ int main()
             endwin();
     
             return 0;
    -}
    -
    +}
    @@ -3365,14 +3311,15 @@ int main() create blotches, whenever you forget to refresh the windows in the proper order.

    -

    Don't despair. There's an elegant solution provided in +

    Don't despair. There is an elegant solution provided in panels library. In the words of developers of ncurses

    When your interface design is such that windows may dive deeper into the visibility stack or pop to the top at runtime, the resulting book-keeping can be tedious and difficult to get - right. Hence the panels library.

    + right. Hence the panels library. +

    If you have lot of overlapping windows, then panels library is the way to go. It obviates the need of doing @@ -3397,7 +3344,7 @@ int main() to display them correctly. There is a function similar to refresh() which, when called , displays panels in the correct order. Functions are provided to hide or show - panels, move panels, change its size etc.. The overlapping + panels, move panels, change its size, etc. The overlapping problem is managed by the panels library during all the calls to these functions.

    @@ -3423,7 +3370,7 @@ int main()
  • Mainpulate the panels with show_panel(), - hide_panel(), move_panel() etc. Make use of helper + hide_panel(), move_panel(), etc. Make use of helper functions like panel_hidden() and panel_window(). Make use of user pointer to store custom data for a panel. Use the functions set_panel_userptr() and @@ -3446,28 +3393,26 @@ int main()


    16.2. Compiling With the Panels - Library

    + "COMPILEPANELS">16.2. Compiling With the Panels Library

    To use panels library functions, you have to include panel.h and to link the program with panels library the flag -lpanel should be added along with -lncurses in that order.

    -
    -    #include <panel.h>
    +
    +        
        #include <panel.h>
         .
         .
         .
     
    -    compile and link: gcc <program file> -lpanel -lncurses
    -
    - + compile and link: gcc <program file> -lpanel -lncurses
    +

    Example 14. Panel basics +

    -

    Example 14. Panel basics

    -
    -#include <panel.h>
    +          
    #include <panel.h>
     
     int main()
     {       WINDOW *my_wins[3];
    @@ -3504,8 +3449,7 @@ int main()
             getch();
             endwin();
     }
    -
    -
    +

    As you can see, above program follows a simple flow as @@ -3528,10 +3472,12 @@ int main()

    +

    Example 15. Panel Window Browsing Example +

    -

    Example 15. Panel Window Browsing Example

    -
    -#include <panel.h>
    +          
    #include <string.h>
    +#include <panel.h>
     
     #define NLINES 10
     #define NCOLS 40
    @@ -3647,8 +3593,7 @@ void print_in_middle(WINDOW *win, int starty, int startx, int width, char *strin
             mvwprintw(win, y, x, "%s", string);
             wattroff(win, color);
             refresh();
    -}
    -
    +}
    @@ -3668,7 +3613,7 @@ void print_in_middle(WINDOW *win, int starty, int startx, int width, char *strin can be accessed using the function panel_userptr() which will return the user pointer for the panel given as argument. After finding the - next panel in the cycle It's brought to the top by the + next panel in the cycle, it is brought to the top by the function top_panel(). This function brings the panel given as argument to the top of the panel stack.

    @@ -3705,11 +3650,13 @@ void print_in_middle(WINDOW *win, int starty, int startx, int width, char *strin
    +

    Example 16. Panel Moving and Resizing example +

    -

    Example 16. Panel Moving and Resizing - example

    -
    -#include <panel.h>
    +          
    #include <stdlib.h>
    +#include <string.h>
    +#include <panel.h>
     
     typedef struct _PANEL_DATA {
             int x, y, w, h;
    @@ -3942,8 +3889,7 @@ void print_in_middle(WINDOW *win, int starty, int startx, int width, char *strin
             mvwprintw(win, y, x, "%s", string);
             wattroff(win, color);
             refresh();
    -}
    -
    +}

    Concentrate on the main while loop. Once it finds out @@ -3953,7 +3899,7 @@ void print_in_middle(WINDOW *win, int starty, int startx, int width, char *strin the user presses <ENTER> present selection ends and panel is resized by using the concept explained. While in resizing mode the program doesn't show how the window is - getting resized. It's left as an exercise to the reader to + getting resized. It is left as an exercise to the reader to print a dotted border while it gets resized to a new position.

    @@ -3997,11 +3943,12 @@ void print_in_middle(WINDOW *win, int starty, int startx, int width, char *strin
    +

    Example 17. Panel Hiding and Showing example +

    -

    Example 17. Panel Hiding and Showing - example

    -
    -#include <panel.h>
    +          
    #include <string.h>
    +#include <panel.h>
     
     typedef struct _PANEL_DATA {
             int hide;       /* TRUE if panel is hidden */
    @@ -4041,7 +3988,7 @@ int main()
             my_panels[1] = new_panel(my_wins[1]);   /* Push 1, order: stdscr-0-1 */
             my_panels[2] = new_panel(my_wins[2]);   /* Push 2, order: stdscr-0-1-2 */
     
    -        /* Initialize panel datas saying that nothing is hidden */
    +        /* Initialize panel data saying that nothing is hidden */
             panel_datas[0].hide = FALSE;
             panel_datas[1].hide = FALSE;
             panel_datas[2].hide = FALSE;
    @@ -4156,8 +4103,7 @@ void print_in_middle(WINDOW *win, int starty, int startx, int width, char *strin
             mvwprintw(win, y, x, "%s", string);
             wattroff(win, color);
             refresh();
    -}
    -
    +}
    @@ -4185,7 +4131,7 @@ void print_in_middle(WINDOW *win, int starty, int startx, int width, char *strin

    The menus library provides a nice extension to basic curses, through which you can create menus. It provides a set of functions to create menus. But they have to be customized - to give a nicer look, with colors etc. Let's get into the + to give a nicer look, with colors, etc. Let's get into the details.

    A menu is a screen display that assists the user to choose @@ -4262,28 +4208,27 @@ void print_in_middle(WINDOW *win, int starty, int startx, int width, char *strin


    17.2. Compiling With the Menu - Library

    + "COMPILEMENUS">17.2. Compiling With the Menu Library

    To use menu library functions, you have to include menu.h and to link the program with menu library the flag -lmenu should be added along with -lncurses in that order.

    -
    -    #include <menu.h>
    +
    +        
        #include <menu.h>
         .
         .
         .
     
    -    compile and link: gcc <program file> -lmenu -lncurses
    -
    - + compile and link: gcc <program file> -lmenu -lncurses
    +

    Example 18. Menu Basics +

    -

    Example 18. Menu Basics

    -
    -#include <curses.h>
    +          
    #include <stdlib.h>
    +#include <curses.h>
     #include <menu.h>
     
     #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
    @@ -4338,8 +4283,7 @@ int main()
             free_menu(my_menu);
             endwin();
     }
    -        
    -
    +

    This program demonstrates the basic concepts involved in @@ -4357,8 +4301,9 @@ int main()

    The menu_driver accepts following navigational requests.

    -
    -
     REQ_LEFT_ITEM         Move left to an item.
    +
    +        
    
     REQ_LEFT_ITEM         Move left to an item.
          REQ_RIGHT_ITEM      Move right to an item.
          REQ_UP_ITEM         Move up to an item.
          REQ_DOWN_ITEM       Move down to an item.
    @@ -4374,9 +4319,7 @@ int main()
          REQ_CLEAR_PATTERN     Clear the menu pattern buffer.
          REQ_BACK_PATTERN      Delete the previous character from the pattern buffer.
          REQ_NEXT_MATCH     Move to the next item matching the pattern match.
    -     REQ_PREV_MATCH     Move to the previous item matching the pattern match.
    -
    - + REQ_PREV_MATCH Move to the previous item matching the pattern match.

    Don't get overwhelmed by the number of options. We will see them slowly one after another. The options of interest in this example are REQ_UP_ITEM and REQ_DOWN_ITEM. These @@ -4402,8 +4345,8 @@ int main()

    • REQ_LEFT_ITEM and - REQ_RIGHT_ITEM

      + "EMPHASIS">REQ_LEFT_ITEM and REQ_RIGHT_ITEM +

      A Menu can be displayed with multiple columns for more than one item. This can be done by using the @@ -4415,7 +4358,8 @@ int main()

    • REQ_UP_ITEM and REQ_DOWN_ITEM

      + "EMPHASIS">REQ_UP_ITEM and REQ_DOWN_ITEM +

      These two options you have seen in the above example. These options when given, makes the @@ -4425,7 +4369,8 @@ int main()

    • REQ_SCR_* - options

      + options +

      The four options REQ_SCR_ULINE, REQ_SCR_DLINE, REQ_SCR_DPAGE, REQ_SCR_UPAGE are related to scrolling. @@ -4439,14 +4384,16 @@ int main()

    • REQ_FIRST_ITEM, REQ_LAST_ITEM, REQ_NEXT_ITEM - and REQ_PREV_ITEM

      + and REQ_PREV_ITEM +

      These requests are self explanatory.

    • REQ_TOGGLE_ITEM

      + "EMPHASIS">REQ_TOGGLE_ITEM +

      This request when given, toggles the present selection. This option is to be used only in a multi @@ -4457,7 +4404,8 @@ int main()

    • Pattern - Requests

      + Requests +

      Every menu has an associated pattern buffer, which is used to find the nearest match to the ascii @@ -4476,14 +4424,16 @@ int main()

    • Mouse - Requests

      + Requests +

      In case of KEY_MOUSE requests, according to the mouse position an action is taken accordingly. The action to be taken is explained in the man page as,

      -
      -       If  the  second argument is the KEY_MOUSE special key, the
      +
      +            
             If  the  second argument is the KEY_MOUSE special key, the
              associated mouse event is translated into one of the above
              pre-defined  requests.   Currently only clicks in the user
              window (e.g. inside the menu display area or  the  decora­
      @@ -4495,8 +4445,7 @@ int main()
              generated, if you doubleclick a REQ_SCR_DPAGE is generated
              and  if  you  tripleclick a REQ_LAST_ITEM is generated. If
              you click at an item inside the display area of the  menu,
      -       the menu cursor is positioned to that item.
      -
      + the menu cursor is positioned to that item.
    @@ -4524,10 +4473,13 @@ int main()
    +

    Example 19. Menu Windows Usage example +

    -

    Example 19. Menu Windows Usage example

    -
    -#include <menu.h>
    +          
    #include <stdlib.h>
    +#include <string.h>
    +#include <menu.h>
     
     #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
     #define CTRLD   4
    @@ -4631,8 +4583,7 @@ void print_in_middle(WINDOW *win, int starty, int startx, int width, char *strin
             mvwprintw(win, y, x, "%s", string);
             wattroff(win, color);
             refresh();
    -}
    -
    +}

    This example creates a menu with a title, border, a @@ -4660,10 +4611,13 @@ void print_in_middle(WINDOW *win, int starty, int startx, int width, char *strin

    +

    Example 20. Scrolling Menus example +

    -

    Example 20. Scrolling Menus example

    -
    -#include <curses.h>
    +          
    #include <stdlib.h>
    +#include <string.h>
    +#include <curses.h>
     #include <menu.h>
     
     #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
    @@ -4734,7 +4688,7 @@ int main()
             wrefresh(my_menu_win);
             
             attron(COLOR_PAIR(2));
    -        mvprintw(LINES - 2, 0, "Use PageUp and PageDown to scoll down or up a page of items");
    +        mvprintw(LINES - 2, 0, "Use PageUp and PageDown to scroll down or up a page of items");
             mvprintw(LINES - 1, 0, "Arrow Keys to navigate (F1 to Exit)");
             attroff(COLOR_PAIR(2));
             refresh();
    @@ -4786,8 +4740,7 @@ void print_in_middle(WINDOW *win, int starty, int startx, int width, char *strin
             mvwprintw(win, y, x, "%s", string);
             wattroff(win, color);
             refresh();
    -}
    -
    +}

    This program is self-explanatory. In this example the @@ -4820,10 +4773,12 @@ void print_in_middle(WINDOW *win, int starty, int startx, int width, char *strin

    +

    Example 21. Milt Columnar Menus Example +

    -

    Example 21. Milt Columnar Menus Example

    -
    -#include <curses.h>
    +          
    #include <stdlib.h>
    +#include <curses.h>
     #include <menu.h>
     
     #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
    @@ -4919,8 +4874,7 @@ int main()
             for(i = 0; i < n_choices; ++i)
                     free_item(my_items[i]);
             endwin();
    -}
    -
    +}

    Watch the function call to set_menu_format(). It @@ -4930,8 +4884,8 @@ int main() couple of functions set_menu_opts(), menu_opts_on() and menu_opts() which can be used to manipulate menu options. The following menu options can be specified.

    -
    -       O_ONEVALUE
    +
    +        
           O_ONEVALUE
                 Only one item can be selected for this menu.
     
            O_SHOWDESC
    @@ -4950,9 +4904,7 @@ int main()
     
            O_NONCYCLIC
                 Don't   wrap   around  next-item  and  previous-item,
    -            requests to the other end of the menu.
    -
    - + requests to the other end of the menu.

    All options are on by default. You can switch specific attributes on or off with menu_opts_on() and menu_opts_off() functions. You can also use set_menu_opts() @@ -4975,10 +4927,13 @@ int main()

    +

    Example 22. Multi Valued Menus example +

    -

    Example 22. Multi Valued Menus example

    -
    -#include <curses.h>
    +          
    #include <stdlib.h>
    +#include <string.h>
    +#include <curses.h>
     #include <menu.h>
     
     #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
    @@ -5061,8 +5016,7 @@ int main()
             free_menu(my_menu);
             endwin();
     }
    -        
    -
    +

    Whew, A lot of new functions. Let's take them one after @@ -5106,16 +5060,18 @@ int main() This brings us to the interesting option for an item the one and only O_SELECTABLE. We can turn it off by the function item_opts_off() and after that that item is not - selectable. It's like a grayed item in those fancy windows + selectable. It is like a grayed item in those fancy windows menus. Let's put these concepts in practice with this example

    +

    Example 23. Menu Options example +

    -

    Example 23. Menu Options example

    -
    -#include <menu.h>
    +          
    #include <stdlib.h>
    +#include <menu.h>
     
     #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
     #define CTRLD   4
    @@ -5194,8 +5150,7 @@ int main()
             free_menu(my_menu);
             endwin();
     }
    -        
    -
    +
    @@ -5206,18 +5161,20 @@ int main() "MENUUSERPTR">17.9. The useful User Pointer

    We can associate a user pointer with each item in the - menu. It works the same way as user pointer in panels. It's - not touched by menu system. You can store any thing you + menu. It works the same way as user pointer in panels. It + is not touched by menu system. You can store any thing you like in that. I usually use it to store the function to be - executed when the menu option is chosen (It's selected and + executed when the menu option is chosen (It is selected and may be the user pressed <ENTER>);

    +

    Example 24. Menu User Pointer Usage +

    -

    Example 24. Menu User Pointer Usage

    -
    -#include <curses.h>
    +          
    #include <stdlib.h>
    +#include <curses.h>
     #include <menu.h>
     
     #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
    @@ -5303,8 +5260,7 @@ void func(char *name)
     {       move(20, 0);
             clrtoeol();
             mvprintw(20, 0, "Item selected is : %s", name);
    -}       
    -
    +}
    @@ -5318,11 +5274,11 @@ void func(char *name)

    Well. If you have seen those forms on web pages which take input from users and do various kinds of things, you might be wondering how would any one create such forms in text mode - display. It's quite difficult to write those nifty forms in + display. It is quite difficult to write those nifty forms in plain ncurses. Forms library tries to provide a basic frame work to build and maintain forms with ease. It has lot of features(functions) which manage validation, dynamic - expansion of fields etc.. Let's see it in full flow.

    + expansion of fields, etc. Let's see it in full flow.

    A form is a collection of fields; each field can be either a label(static text) or a data-entry location. The forms also @@ -5339,13 +5295,13 @@ void func(char *name) the fields related to the form are created with new_field(). You can set options for the fields, so that they can be displayed with some fancy attributes, validated - before the field looses focus etc.. Then the fields are + before the field looses focus, etc. Then the fields are attached to form. After this, the form can be posted to display and is ready to receive inputs. On the similar lines to menu_driver(), the form is manipulated with form_driver(). We can send requests to form_driver to move focus to a certain field, move cursor to end of the field - etc.. After the user enters values in the fields and + etc. After the user enters values in the fields and validation done, form can be unposted and memory allocated can be freed.

    @@ -5406,28 +5362,26 @@ void func(char *name)

    18.2. Compiling With the Forms - Library

    + "COMPILEFORMS">18.2. Compiling With the Forms Library

    To use forms library functions, you have to include form.h and to link the program with forms library the flag -lform should be added along with -lncurses in that order.

    -
    -    #include <form.h>
    +
    +        
        #include <form.h>
         .
         .
         .
     
    -    compile and link: gcc <program file> -lform -lncurses
    -
    - + compile and link: gcc <program file> -lform -lncurses
    +

    Example 25. Forms Basics +

    -

    Example 25. Forms Basics

    -
    -#include <form.h>
    +          
    #include <form.h>
     
     int main()
     {       FIELD *field[3];
    @@ -5492,8 +5446,7 @@ int main()
     
             endwin();
             return 0;
    -}
    -
    +}

    Above example is pretty straight forward. It creates two @@ -5545,13 +5498,13 @@ int main() height, width, starty, startx, number of offscreen rows, and number of additional buffers into the parameters given to it. It is a sort of inverse of new_field().

    -
    -int field_info(     FIELD *field,              /* field from which to fetch */
    +
    +          
    int field_info(     FIELD *field,              /* field from which to fetch */
                         int *height, *int width,   /* field size */ 
                         int *top, int *left,       /* upper left corner */
                         int *offscreen,            /* number of offscreen rows */
    -                    int *nbuf);                /* number of working buffers */
    -
    + int *nbuf); /* number of working buffers */
    @@ -5562,11 +5515,10 @@ int field_info( FIELD *field, /* field from which to fetch */

    The location of the field can be moved to a different position with move_field().

    -
    -int move_field(    FIELD *field,              /* field to alter */
    -                   int top, int left);        /* new upper-left corner */
    -
    +
    int move_field(    FIELD *field,              /* field to alter */
    +                   int top, int left);        /* new upper-left corner */

    As usual, the changed position can be queried with field_infor().

    @@ -5579,12 +5531,11 @@ int move_field( FIELD *field, /* field to alter */

    The justification to be done for the field can be fixed using the function set_field_just().

    -
    -    int set_field_just(FIELD *field,          /* field to alter */
    -               int justmode);         /* mode to set */
    -    int field_just(FIELD *field);          /* fetch justify mode of field */
    -
    +
        int set_field_just(FIELD *field,          /* field to alter */
    +               int justmode);         /* mode to set */
    +    int field_just(FIELD *field);          /* fetch justify mode of field */

    The justification mode valued accepted and returned by these functions are NO_JUSTIFICATION, JUSTIFY_RIGHT, JUSTIFY_LEFT, or JUSTIFY_CENTER.

    @@ -5594,8 +5545,7 @@ int move_field( FIELD *field, /* field to alter */

    18.3.4. Field Display - Attributes

    + "FIELDDISPATTRIB">18.3.4. Field Display Attributes

    As you have seen, in the above example, display attribute for the fields can be set with set_field_fore() @@ -5608,8 +5558,9 @@ int move_field( FIELD *field, /* field to alter */ be used to query the present foreground, background attributes and pad character for the field. The following list gives the usage of functions.

    -
    -
int set_field_fore(FIELD *field,        /* field to alter */
    +
    +          
    
int set_field_fore(FIELD *field,        /* field to alter */
                        chtype attr);        /* attribute to set */ 
     
     chtype field_fore(FIELD *field);        /* field to query */
    @@ -5625,9 +5576,7 @@ int set_field_pad(FIELD *field,         /* field to alter */
                       int pad);             /* pad character to set */ 
     
     chtype field_pad(FIELD *field);         /* field to query */  
    -                                        /* returns present pad character */
    -
    - + /* returns present pad character */

    Though above functions seem quite simple, using colors with set_field_fore() may be frustrating in the beginning. Let me first explain about foreground and @@ -5643,10 +5592,11 @@ chtype field_pad(FIELD *field); /* field to query */

    +

    Example 26. Form Attributes example +

    -

    Example 26. Form Attributes example

    -
    -#include <form.h>
    +            
    #include <form.h>
     
     int main()
     {       FIELD *field[3];
    @@ -5720,8 +5670,7 @@ int main()
     
             endwin();
             return 0;
    -}
    -
    +}

    Play with the color pairs and try to understand the @@ -5741,8 +5690,9 @@ int main() you can set to control various aspects of forms processing. You can manipulate them with these functions:

    -
    -int set_field_opts(FIELD *field,          /* field to alter */
    +
    +          
    int set_field_opts(FIELD *field,          /* field to alter */
                        int attr);             /* attribute to set */ 
     
     int field_opts_on(FIELD *field,           /* field to alter */
    @@ -5751,9 +5701,7 @@ int field_opts_on(FIELD *field,           /* field to alter */
     int field_opts_off(FIELD *field,          /* field to alter */
                       int attr);              /* attributes to turn off */ 
     
    -int field_opts(FIELD *field);             /* field to query */ 
    -
    - +int field_opts(FIELD *field); /* field to query */

    The function set_field_opts() can be used to directly set attributes of a field or you can choose to switch a few attributes on and off with field_opts_on() and @@ -5881,10 +5829,11 @@ int field_opts(FIELD *field); /* field to query */

    +

    Example 27. Field Options Usage example +

    -

    Example 27. Field Options Usage example

    -
    -#include <form.h>
    +            
    #include <form.h>
     
     #define STARTX 15
     #define STARTY 4
    @@ -5959,8 +5908,7 @@ int main()
     
             endwin();
             return 0;
    -}
    -
    +}

    This example, though useless, shows the usage of @@ -5982,14 +5930,13 @@ int main() becomes TRUE. So a field's status can be queried to find out whether it has been modified or not. The following functions can assist in those operations.

    -
    -int set_field_status(FIELD *field,      /* field to alter */
    -                   int status);         /* status to set */
     
    -int field_status(FIELD *field);         /* fetch status of field */
    -
    +
    int set_field_status(FIELD *field,      /* field to alter */
    +                   int status);         /* status to set */
     
    -          

    It's better to check the field's status only after +int field_status(FIELD *field); /* fetch status of field */

    +

    It is better to check the field's status only after after leaving the field, as data buffer might not have been updated yet as the validation is still due. To guarantee that right status is returned, call @@ -6011,21 +5958,20 @@ int field_status(FIELD *field); /* fetch status of field */ by forms library and can be used for any purpose by the user. The following functions set and fetch user pointer.

    -
    -int set_field_userptr(FIELD *field,   
    +
    +          
    int set_field_userptr(FIELD *field,   
                char *userptr);      /* the user pointer you wish to associate */
                                     /* with the field    */
     
    -char *field_userptr(FIELD *field);      /* fetch user pointer of the field */
    -
    +char *field_userptr(FIELD *field); /* fetch user pointer of the field */

    18.3.8. Variable-Sized - Fields

    + "VARIABLESIZEFIELDS">18.3.8. Variable-Sized Fields

    If you want a dynamically changing field with variable width, this is the feature you want to put to full use. @@ -6038,30 +5984,27 @@ char *field_userptr(FIELD *field); /* fetch user pointer of the field */

    To make a field dynamically growable, the option O_STATIC should be turned off. This can be done with a

    -
    -    field_opts_off(field_pointer, O_STATIC);
    -
    -

    But it's usually not advisable to allow a field to +

        field_opts_off(field_pointer, O_STATIC);
    +

    But it is usually not advisable to allow a field to grow infinitely. You can set a maximum limit to the growth of the field with

    -
    -int set_max_field(FIELD *field,    /* Field on which to operate */
    -                  int max_growth); /* maximum growth allowed for the field */
    -
    +
    int set_max_field(FIELD *field,    /* Field on which to operate */
    +                  int max_growth); /* maximum growth allowed for the field */

    The field info for a dynamically growable field can be retrieved by

    -
    -int dynamic_field_info( FIELD *field,     /* Field on which to operate */
    +
    +          
    int dynamic_field_info( FIELD *field,     /* Field on which to operate */
                 int   *prows,     /* number of rows will be filled in this */
                 int   *pcols,     /* number of columns will be filled in this*/
                 int   *pmax)      /* maximum allowable growth will be filled */
    -                              /* in this */
    -
    Though field_info work as usual, it is advisable to use this -function to get the proper attributes of a dynamically growable -field. - + /* in this */
    Though field_info +work as usual, it is advisable to use this function to get the +proper attributes of a dynamically growable field.

    Recall the library routine new_field; a new field created with height set to one will be defined to be a one line field. A new field created with height greater @@ -6181,10 +6124,12 @@ field.

    +

    Example 28. Form Windows Example +

    -

    Example 28. Form Windows Example

    -
    -#include <form.h>
    +          
    #include <string.h>
    +#include <form.h>
     
     void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color);
     
    @@ -6295,8 +6240,7 @@ void print_in_middle(WINDOW *win, int starty, int startx, int width, char *strin
             mvwprintw(win, y, x, "%s", string);
             wattroff(win, color);
             refresh();
    -}
    -
    +}
    @@ -6316,15 +6260,15 @@ void print_in_middle(WINDOW *win, int starty, int startx, int width, char *strin

    Validation can be attached to a field with the following function.

    -
    -int set_field_type(FIELD *field,          /* field to alter */
    +
    +        
    int set_field_type(FIELD *field,          /* field to alter */
                        FIELDTYPE *ftype,      /* type to associate */
    -                   ...);                  /* additional arguments*/
    -
    Once set, the validation type for a field can be queried with -
    -FIELDTYPE *field_type(FIELD *field);      /* field to query */
    -
    + ...); /* additional arguments*/
    Once +set, the validation type for a field can be queried with +
    FIELDTYPE *field_type(FIELD *field);      /* field to query */

    The form driver validates the data in a field only when data is entered by the end-user. Validation does not occur when

    @@ -6342,61 +6286,58 @@ FIELDTYPE *field_type(FIELD *field); /* field to query */

    The following are the pre-defined validation types. You - can also specify custom validation, though it's a bit + can also specify custom validation, though it is a bit tricky and cumbersome.

    -

    TYPE_ALPHA

    +

    TYPE_ALPHA

    This field type accepts alphabetic data; no blanks, no digits, no special characters (this is checked at character-entry time). It is set up with:

    -
    -int set_field_type(FIELD *field,          /* field to alter */
    -                   TYPE_ALPHA,            /* type to associate */
    -                   int width);            /* minimum width of field */
    -
    +
    int set_field_type(FIELD *field,          /* field to alter */
    +                   TYPE_ALPHA,            /* type to associate */
    +                   int width);            /* minimum width of field */

    The width argument sets a minimum width of data. The user has to enter at-least width number of characters before he can leave the field. Typically you'll want to set - this to the field width; if it's greater than the field + this to the field width; if it is greater than the field width, the validation check will always fail. A minimum width of zero makes field completion optional.

    -

    TYPE_ALNUM

    +

    TYPE_ALNUM

    This field type accepts alphabetic data and digits; no blanks, no special characters (this is checked at character-entry time). It is set up with:

    -
    -int set_field_type(FIELD *field,          /* field to alter */
    -                   TYPE_ALNUM,            /* type to associate */
    -                   int width);            /* minimum width of field */
    -
    +
    int set_field_type(FIELD *field,          /* field to alter */
    +                   TYPE_ALNUM,            /* type to associate */
    +                   int width);            /* minimum width of field */

    The width argument sets a minimum width of data. As with TYPE_ALPHA, typically you'll want to set this to the field - width; if it's greater than the field width, the validation - check will always fail. A minimum width of zero makes field - completion optional.

    + width; if it is greater than the field width, the + validation check will always fail. A minimum width of zero + makes field completion optional.

    -

    TYPE_ENUM

    +

    TYPE_ENUM

    This type allows you to restrict a field's values to be among a specified set of string values (for example, the two-letter postal codes for U.S. states). It is set up with:

    -
    -int set_field_type(FIELD *field,          /* field to alter */
    +
    +        
    int set_field_type(FIELD *field,          /* field to alter */
                        TYPE_ENUM,             /* type to associate */
                        char **valuelist;      /* list of possible values */
                        int checkcase;         /* case-sensitive? */
    -                   int checkunique);      /* must specify uniquely? */
    -
    - + int checkunique); /* must specify uniquely? */

    The valuelist parameter must point at a NULL-terminated list of valid strings. The checkcase argument, if true, makes comparison with the string case-sensitive.

    @@ -6416,18 +6357,17 @@ int set_field_type(FIELD *field, /* field to alter */

    The REQ_NEXT_CHOICE and REQ_PREV_CHOICE input requests can be particularly useful with these fields.

    -

    TYPE_INTEGER

    +

    TYPE_INTEGER

    This field type accepts an integer. It is set up as follows:

    -
    -int set_field_type(FIELD *field,          /* field to alter */
    +
    +        
    int set_field_type(FIELD *field,          /* field to alter */
                        TYPE_INTEGER,          /* type to associate */
                        int padding,           /* # places to zero-pad to */
    -                   int vmin, int vmax);   /* valid range */
    -
    - + int vmin, int vmax); /* valid range */

    Valid characters consist of an optional leading minus and digits. The range check is performed on exit. If the range maximum is less than or equal to the minimum, the @@ -6440,18 +6380,17 @@ int set_field_type(FIELD *field, /* field to alter */

    A TYPE_INTEGER value buffer can conveniently be interpreted with the C library function atoi(3).

    -

    TYPE_NUMERIC

    +

    TYPE_NUMERIC

    This field type accepts a decimal number. It is set up as follows:

    -
    -int set_field_type(FIELD *field,          /* field to alter */
    +
    +        
    int set_field_type(FIELD *field,          /* field to alter */
                        TYPE_NUMERIC,          /* type to associate */
                        int padding,           /* # places of precision */
    -                   int vmin, int vmax);   /* valid range */
    -
    - + int vmin, int vmax); /* valid range */

    Valid characters consist of an optional leading minus and digits. possibly including a decimal point. The range check is performed on exit. If the range maximum is less @@ -6464,17 +6403,16 @@ int set_field_type(FIELD *field, /* field to alter */

    A TYPE_NUMERIC value buffer can conveniently be interpreted with the C library function atof(3).

    -

    TYPE_REGEXP

    +

    TYPE_REGEXP

    This field type accepts data matching a regular expression. It is set up as follows:

    -
    -int set_field_type(FIELD *field,          /* field to alter */
    -                   TYPE_REGEXP,           /* type to associate */
    -                   char *regexp);         /* expression to match */
    -
    +
    int set_field_type(FIELD *field,          /* field to alter */
    +                   TYPE_REGEXP,           /* type to associate */
    +                   char *regexp);         /* expression to match */

    The syntax for regular expressions is that of regcomp(3). The check for regular-expression match is performed on exit.

    @@ -6490,14 +6428,13 @@ int set_field_type(FIELD *field, /* field to alter */

    As in the menu system, form_driver() plays a very important role in forms system. All types of requests to forms system should be funneled through form_driver().

    -
    -int form_driver(FORM *form,     /* form on which to operate     */
    -                int request)    /* form request code         */
    -
    +
    int form_driver(FORM *form,     /* form on which to operate     */
    +                int request)    /* form request code         */

    As you have seen some of the examples above, you have to be in a loop looking for user input and then decide whether - it's a field data or a form request. The form requests are + it is a field data or a form request. The form requests are then passed to form_driver() to do the work.

    The requests roughly can be divided into following @@ -6516,11 +6453,10 @@ int form_driver(FORM *form, /* form on which to operate */ lot of fields and logical sections, then you can divide the form into pages. The function set_new_page() to set a new page at the field specified.

    -
    -int set_new_page(FIELD *field,/* Field at which page break to be set or unset */
    -         bool new_page_flag); /* should be TRUE to put a break */
    -
    +
    int set_new_page(FIELD *field,/* Field at which page break to be set or unset */
    +         bool new_page_flag); /* should be TRUE to put a break */

    The following requests allow you to move to different pages

    @@ -7070,13 +7006,14 @@ int set_new_page(FIELD *field,/* Field at which page break to be set or unset */

    CDK stands for 'Curses Development Kit' and it currently contains 21 ready to use widgets which facilitate the speedy - development of full screen curses programs.

    + development of full screen curses programs. +

    The kit provides some useful widgets, which can be used - in your programs directly. It's pretty well written and the - documentation is very good. The examples in the examples - directory can be a good place to start for beginners. The - CDK can be downloaded from https://invisible-island.net/cdk/ . Follow the instructions in README file to install it.

    @@ -7089,8 +7026,9 @@ int set_new_page(FIELD *field,/* Field at which page break to be set or unset */

    The following is the list of widgets provided with cdk and their description.

    -
    -Widget Type           Quick Description
    +
    +          
    Widget Type           Quick Description
     ===========================================================================
     Alphalist             Allows a user to select from a list of words, with
                           the ability to narrow the search list by typing in a
    @@ -7133,9 +7071,7 @@ Template              Creates a entry field with character sensitive
                           dates and phone numbers.
     Viewer                This is a file/information viewer. Very useful
                           when you need to display loads of information.
    -===========================================================================
    -
    - +===========================================================================

    A few of the widgets are modified by Thomas Dickey in recent versions.

    @@ -7153,15 +7089,14 @@ Viewer This is a file/information viewer. Very useful which are passed to CDK functions. For Example

    If the string

    -
    -"</B/1>This line should have a yellow foreground and a blue
    -background.<!1>"
    -
    +
    "</B/1>This line should have a yellow foreground and a blue
    +background.<!1>"

    given as a parameter to newCDKLabel(), it prints the line with yellow foreground and blue background. There are other tags available for justifying string, embedding - special drawing characters etc.. Please refer to the man + special drawing characters, etc. Please refer to the man page cdk_display(3X) for details. The man page explains the usage with nice examples.

    @@ -7198,20 +7133,22 @@ background.<!1>" professional-looking dialog boxes from within shell scripts. This article presents a tutorial introduction to the dialog utility, and shows examples of how and where it - can be used

    + can be used +

    As he explains, dialog is a real gem in making professional-looking dialog boxes with ease. It creates a - variety of dialog boxes, menus, check lists etc.. It is + variety of dialog boxes, menus, check lists, etc. It is usually installed by default. If not, you can download it from Thomas Dickey's site.

    The above-mentioned article gives a very good overview - of its uses and capabilites. The man page has more details. - It can be used in variety of situations. One good example - is building of linux kernel in text mode. Linux kernel uses - a modified version of dialog tailored for its needs.

    + of its uses and capabilities. The man page has more + details. It can be used in variety of situations. One good + example is building of linux kernel in text mode. Linux + kernel uses a modified version of dialog tailored for its + needs.

    dialog was initially designed to be used with shell scripts. If you want to use its functionality in a c @@ -7278,27 +7215,26 @@ background.<!1>"

    Game of life is a wonder of math. In Paul Callahan's words

    -
    -The Game of Life (or simply Life) is not a game in the conventional sense. There
    +
    +        
    The Game of Life (or simply Life) is not a game in the conventional sense. There
     are no players, and no winning or losing. Once the "pieces" are placed in the
     starting position, the rules determine everything that happens later.
     Nevertheless, Life is full of surprises! In most cases, it is impossible to look
     at a starting position (or pattern) and see what will happen in the future. The
    -only way to find out is to follow the rules of the game.
    -
    - +only way to find out is to follow the rules of the game.

    This program starts with a simple inverted U pattern and shows how wonderful life works. There is a lot of room for improvement in the program. You can let the user enter pattern of his choice or even take input from a file. You can also change rules and play with a lot of variations. - Search on google for interesting information on game of life.

    File Path: - JustForFun/life.c

    + JustForFun/life.c +

    @@ -7310,14 +7246,15 @@ only way to find out is to follow the rules of the game.

    Magic Square, another wonder of math, is very simple to understand but very difficult to make. In a magic square sum of the numbers in each row, each column is equal. Even - diagnol sum can be equal. There are many variations which + diagonal sum can be equal. There are many variations which have special properties.

    This program creates a simple magic square of odd order.

    File Path: - JustForFun/magic.c

    + JustForFun/magic.c +

    @@ -7332,7 +7269,8 @@ only way to find out is to follow the rules of the game. larger disk over a small disk at any time.

    File Path: - JustForFun/hanoi.c

    + JustForFun/hanoi.c +

    @@ -7349,7 +7287,8 @@ only way to find out is to follow the rules of the game. technique.

    File Path: - JustForFun/queens.c

    + JustForFun/queens.c +

    @@ -7361,7 +7300,8 @@ only way to find out is to follow the rules of the game.

    A fun game, if you have time to kill.

    File Path: - JustForFun/shuffle.c

    + JustForFun/shuffle.c +

    @@ -7376,15 +7316,15 @@ only way to find out is to follow the rules of the game. helpful.

    File Path: - JustForFun/tt.c

    + JustForFun/tt.c +

  • Revision - History
    Revision 1.9Revision 2.02022-12-03Revised by: dickey
    2005-06-20
    Fixes for the sample + programs, Correct documentation errata.
    Revision 1.92005-06-20 Revised by: ppadala
    Revision 1.82005-06-17Revised by: ppadala
    Revision 1.7.12002-06-25Revised by: ppadala
    Revision 1.72002-06-25Revised by: ppadala
    Revision 1.6.12002-02-24Revised by: ppadala
    Revision 1.62002-02-16Revised by: ppadala
    Revision 1.52002-01-05Revised by: ppadala
    Revision 1.3.12001-07-26Revised by: ppadala
    Revision 1.32001-07-24Revised by: ppadala
    Revision 1.22001-06-05Revised by: ppadala
    Revision 1.12001-05-22Revised by: ppadala