You are on page 1of 105

NCURSES Programming HOWTO

NCURSES Programming HOWTO

1 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

NCURSES Programming HOWTO

Pradeep Padala
The Linux Documentation Projecthttp://www.tldp.org/
2003 3 23
Byron
E-mailbyronm@sina.com

Linux Open Source Linux


NCURSES-HOWTO
NCURSES CURSES New CURSES
C/C++ C/C++
NCURSES NCURSES

NCURSES HOWTO The Linux


Documentation Project NCURSES
NCURSES NCURESE Unix

NCURSES
http://www.cise.ufl.edu/~ppadala/ncurses/NCURSES_HOWTO/
p_padala@yahoo.com

http://poet.cosoft.org.cn/index.htm
byronm@sina.com

Byronbyronm@sina.com
NCURSES Programming HOWTO

1-151920
Simon Zhansimonzhan@21cn.com
DreamXSTxstsky@163.com 16-18 HTML
Simon Zhan DreamXST

Posuring
ciziLi Yewangiceiceice Candy

2 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

NCURSES Programming HOWTO


Pradeep Padala
p_padala@yahoo.com
V1.7.1 2002 6 25

1.7.1
2002 6 25
README

PPadala

1.7
2002 6 25
PPadala

1.6.1
2002 2 24
Changelog Makefile

PPadala

1.6
2002 2 16
ACS

PPadala

1.5
2002 1 05
TOC

PPadala

1.3.1
2001 7 26

PPadala

1.3
2001 7 24
PPadala
LDP GPL
printw printw_example
1.2
2001 6 5
PPadala
ravi Just For Fun

1.1
2001 5 22
PPadala
a word about window scanw
scanw_example
NCURSES
NCURSES Hello, World!
NCURSES
NCURSES NCURSES NCURSES
http://www.cise.ufl.edu/~ppadala/ncurses/NCURSES_HOWTO/
mailto:p_padala@yahoo.com
3 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

NCURSES Programming HOWTO


HOWTO Pradeep Padalap_padala@yahoo.com

Copyright (c) 2001 by Pradeep Padala. This document may be distributed under the terms
set forth in the LDP license at linuxdoc.org/COPYRIGHT.html.
This HOWTO is free documentation; you can redistribute it and/or modify it under the
terms of the LDP license. This document is distributed in the hope that it will be useful,
but without any warranty, without even the implied warranty of merchantability or fitness
for a particular purpose. See the LDP license for more details.

NCURSES Programming HOWTO


HOWTO Byron LDP
,
byronm@sina.com

4 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

NCURSES
1

escape sequence
0x1B ESC

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


^ [ ^[
^[ Ctrl+V
ESC In Color

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

UNIX
termcap termcap

terminfo
termcap termcap
terminfo

2NCURSES
terminfo
terminfo
CURSES
CURSES cursor optimization
CURSES
API

NCURSES System V Release 4.0 (SVr4) CURSES


5 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

CURSES
CURSES NCURSES
NCURSES Pavel Curtis Zeyd Ben-Halim <zmbenhal@netcom.com> Eric S.
Raymond <esr@snark.thyrsus.com> 1.8.1
Jrgen Pfeifer< juergen.pfeifer@gmx.net >
Ada95 Thomas
Dickey<dickey@herndon4.his.com> Jrgen Pfeifer NCURSES
Florian La Roche <florian@gnu.org> NCURSES GNU
NCURSES NCURSES
bug-ncurses@gnu.org

3NCURSES
NCURSES
Framework
MenuPanel Form CURSES CURSES
multiple windowsmenus
panelsformsscrollability

MenusForms
Panels NCURSES

NCURSES
NCURSES

4Where to get it
NCURSES
Linux NCURSES

ftp://ftp.gnu.org/pub/gnu/ncurses/ncurses.tar.gz NCURSES
GNU FTP http://www.gnu.org/order/ftp.html
NCURSES 5.2 20001021. 5.4
NCURSES
README INSTALL NCURSES
NCURSES
tar zxvf ncurses<version>.tar.gz
#
cd ncurses<version>
#
./configure
#
make
# NCURSES
su root
# root
make install
# NCURSES
6 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

RPM
http://rpmfind.net NCURSES RPM root
5.2
rpm vih RPM

1.4 Purpose/Scope of the document


NCURSES sister libraries
NCURSES Hello, World!
NCURSES
NCURSES

1.5 About the Programs


tar.gz

http://www.linuxdoc.org/HOWTO/NCURSES-Programming-HOWTO/ncurses_programs.tar.gz

ncurses
|
|----> JustForFun
|----> basics
|----> demo
|
|
|
|----> exe
|----> forms
|----> menus
|----> panels
|----> Makefile
|----> README
|----> COPYING

-- just for fun


--
-- make
--------

form
menus
panels
Makefile
README

JustForFun
|
|----> hanoi.c
|----> life.c
|----> magic.c
|----> queens.c
|----> shuffle.c
|----> tt.c
basics
|
|----> acs_vars.c

-------

-- ACS
7 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

|----> hello_world.c
|----> init_func_example.c
|----> key_code.c
|----> mouse_menu.c
|----> other_border.c
|----> printw_example.c
|----> scanw_example.c
|----> simple_attr.c
|----> simple_color.c
|----> simple_key.c
|----> temp_leave.c
|----> win_border.c
|----> with_chgat.c

-- Hello, World!
--
--
--
-- box()
-- printw()
-- getstr()
-- C
--
--
-- CURSES
--
-- chgat()

forms
|
|----> form_attrib.c
|----> form_options.c
|----> form_simple.c
|----> form_win.c

-----

field
field

menus
|
|----> menu_attrib.c
|----> menu_item_data.c
|----> menu_multi_column.c
|----> menu_scroll.c
|----> menu_simple.c
|----> menu_toggle.c
|----> menu_userptr.c
|----> menu_win.c

---------

item_name()

REQ_TOGGLE_ITEM

panels
|
|----> panel_browse.c
|----> panel_hide.c
|----> panel_resize.c
|----> panel_simple.c

-----

tab

Makefile
demo/exec
README C
NCURSES
8 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/ncurses_programs/
GPLhttp://www.gnu.org/copyleft/gpl.txt

1.6 (Other Formats of the document)


HOWTO tldp.org

1.6.1 tldp.org
Acrobat PDF
PostScript
HTML
HTML

1.7 Credits
Sharathsharath_1@usa.net Emre Akbas
Sharath Emre
printw() scanw()
Ravi Parimiparimi@ece.arizona.edu

Linux Solaris

1.8 Wish List


Padala

l form Padala
l

N.N.Ashok
l Debug _trace_tarcemouse
l NCURSES termcap tremios
l
l Miscellaneous features

1.9 Copyright
Copyright (c) 2001 by Pradeep Padala. This document may be distributed under the terms
set forth in the LDP license at linuxdoc.org/COPYRIGHT.html.
This HOWTO is free documentation; you can redistribute it and/or modify it under the
terms of the LDP license. This document is distributed in the hope that it will be useful,
but without any warranty, without even the implied warranty of merchantability or fitness
for a particular purpose. See the LDP license for more details.
9 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

Hello World
NCURSES NCURSES
NCURSES

2.1 NCURSES
NCURSES ncurses.h C C++
#include <ncurses.h> NCURSES
NCURSES stdio.h
#include <ncurses.h>

: gcc <> -lncurses

1Hello World !!!


#include <ncurses.h>
int main()
{
initscr();
/* NCURSES
*/
printw("Hello World !!!"); /* Hello, World!!!
refresh();
/*
getch();
/*
endwin();
/* NCURSES

*/
*/
*/
*/

return 0;
}

2.2
Hello World !!!
curses curses

2.2.1 initscr()
initscr() curses
NCURSES initscr()
curses
stdscr
initscr()
curses

10 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

2.2.2 refresh()
printw HelloWorld
printf()printw
stdscr0,00,0
refresh() printw Hello
Worldstdscr
printw()
stdscr
refresh curses
refresh
refresh

refresh()

2.2.3 endwin()
curses
endwin() curses
curses
endwin() curses

initscr() endwin()

11 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

CURSES CURSES
CURSES

12 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

initscr() CURSES
CURSES
terminal modecolor mode
mouse mode

4.1 raw() cbreak()

line buffering
CTRL-ZCTRL-C raw()
cbreak()

raw()

4.2 echo() noecho()

getch()
noecho() CTRL-C
echo()noecho()

4.3 keypad()
F1F2
keypad(stdscr,TURE)
stdscr

4.4 halfdelay()
halfdelay()
half-delay mode cbreak()
0.1
ERR halfdelay() 0.1

13 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

4.5
CURSES
CURSES

4.6

2.
#include <ncurses.h>
int main()
{
int ch;
initscr();
raw();
keypad(stdscr, TRUE);
noecho();

/*
/*
/*
/*

curses
*/

*/

*/
getch() */

printw("Type any character to see it in bold\n");


ch = getch();
/* raw() enter */
if(ch == KEY_F(1)) /* keypad */
printw("F1 Key pressed");
/* noecho() */
else
{ printw("The pressed key is ");
attron(A_BOLD);
printw("%c", ch);
attroff(A_BOLD);
}
refresh();
/* */
getch();
/*
*/
endwin();
/* curses
*/
return 0;
}
getch()
getchar() getch
enter
attron() attroff()

14 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

CURSES windows

CURSES Windows
CURSES stdscr
80 25

printw(Hi! There!);
refresh();
stdscr Hi! There! refresh()
stdscr
win
w

wprintw(win, "Hi There !!!");


wrefresh(win);

printw(string);
mvprintw(y, x, string);
wprintw(win, string);

/* stdscr string */
/* string (y,x) */
/* win string */

mvwprintw(win, y, x, string);
/* win (y,x) string */
w stdscr

15 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

printw()
CURSES
CURSES curses
CURSES
addch()
printw() printf()
addstr()

6.1 addch()
addch()

CURSES

n ncurses.h
ch
BOLDUNDERLINE

addch(ch | A_BOLD | A_UNDERLINE);


n

attrset()attron()attroff()

attrset()
attroff()

CURSES
ncurses.h
ACS_

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


mvaddch()
move(row,col);
/* row col */
addch(ch);

mvaddch(row,col,ch);
waddch() addch()waddch()
addch() stdscr

16 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

mvwaddch()

CURSES printf() puts()

6.3. printw()
printf()
6.3.1. printw() mvprintw()
printw() printf()mvprintw()

6.3.2wprintw() mvwprintw

stdscr
6.3.3. vwprintw()
vprintf()
6.3.4. printw()A Simple printw example
3 printw()

#include <ncurses.h>

/* ncurses.h stdio.h */

#include <string.h>
int main()
{
char mesg[]="Just a string";
int row,col;

/* */
/*

*/
initscr();
getmaxyx(stdscr,row,col);

/* curses */
/* stdscr */

mvprintw(row/2,(col-strlen(mesg))/2,"%s",mesg);
/* mesg*/
mvprintw(row-2,0,"This screen has %d rows and %d columns\n",row,col);
printw("Try resizing your window(if possible) and then run this program again");
refresh();
getch();
endwin();
return 0;
}

17 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

printw()

getmaxyx() ncurses.h

getmaxyx()

6.4. addstr()
addstr() addch()
mvaddstr()
mvwaddstr() waddstr() CURSES
mvaddstr() move() addstr()
addnstr()
addnstr()

6.5.
y x
x y

18 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

scanw()

getch()
scanw()
getstr()

7.1. getch()

cbreak()CURSES

cbreak() noecho()
cbeak() noecho()

7.2. scanw()
scanf()

7.2.1. scanw() mvscanw()


scanw() sscanf() scanw()
wgetstr() wgetstr() scanw()
wgetstr()

7.2.2. wscanw() mvwscanw()

7.2.3. vwscanw()vwscanw()
vprintf()

7.3. getstr()
getch()

19 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

7.4.
4 scanw()
#include <ncurses.h>

/* ncurses.h stdio.h */

#include <string.h>
int main()
{
char mesg[]="Enter a string: "; /* */
char str[80];
int row,col;
*/
initscr();
getmaxyx(stdscr,row,col);

/*
/* curses */
/* stdscr */

mvprintw(row/2,(col-strlen(mesg))/2,"%s",mesg);
/* mesg */
getstr(str);

/* str */

mvprintw(LINES - 2, 0, "You Entered: %s", str);


getch();
endwin();
return 0;
}

20 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

Attributes

C BOLD
5
#include <ncurses.h>
int main(int argc, char *argv[])
{
int ch, prev;
FILE *fp;
int goto_prev = FALSE, y, x;
if(argc != 2)
{ printf("Usage: %s <a c file name>\n", argv[0]);
exit(1);
}
fp = fopen(argv[1], "r");
/* */
if(fp == NULL)
{ perror("Cannot open input file");
exit(1);
}
initscr();
/* CURSES */
prev = EOF;
while((ch = fgetc(fp)) != EOF)
{
if(prev == '/' && ch == '*') /* /*
*/
{
attron(A_BOLD);

/* /*

*/
goto_prev = TRUE;
}
if(goto_prev == TRUE)

/* /* */

{
getyx(stdscr, y, x);
move(y, x - 1);
printw("%c%c", '/', ch);
ch = 'a';

/* */
/*

goto_prev = FALSE;

/* */

*/
}

21 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

else printw("%c", ch);


refresh();
/* */
if(prev == '*' && ch == '/')
attroff(A_BOLD); /* */*/
prev = ch;
}
getch();
endwin();

/* Curses */

return 0;
}
while
/*
attron()*/
attroff()
getyx() move()getyx()
ncurses.h getyx()
move()

y x

8.1
attron()attroff() attrset()
sister functions attr_get()

attron() attroff()onoff
CURSES.h
A_NORMAL
()
A_STANDOUT

A_UNDERLINE

A_REVERSE

A_BLINK

A_DIM

A_BOLD

A_PROTECT

A_INVIS

22 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

A_ALTCHARSET

A_CHARTEXT

COLOR_PAIR(n)

lst01.c A_NORMALA_BLINK
A_DIMA_INVISA_BOLDA_STANDOUT A_REVERSE A_STANDOUT
A_REVERSE

E-mail

|
attron(A_REVERSE | A_BLINK);

8.2 attron() attrset()


attron() attrset()attrset()
attron() attrset()

standend()
attrset(A_NORMAL)

8.3 attr_get()
attr_get()

attrset() attron()

8.4 attr_
attr_attr_set()attr_on()

8.5 wattr_ wattr functions


stdscr

8.6 chgat()
23 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

chgar() curs_attr man_page

-1

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

wchgat(), mvchgat() chgat()


wchgat()mvchgat()
chgat() stdscr w
stdscr
mvchgat()
#include <ncurses.h>
int main(int argc, char *argv[])
{
initscr();
/* curses */
start_color();
/* */
init_pair(1, COLOR_CYAN, COLOR_BLACK);
printw("A Big string which i didn't care to type fully ");
mvchgat(0, 0, -1, A_BLINK, 1, NULL);
/*
*
*-1
*
* init_pair()
*
* NULL
*/
refresh();
getch();
endwin();

/* curses */

return 0;
}

24 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

Window CURSES
stdscr
GUI

9.1
newwin()

CURSES
newwin()
wprintw() delwin()

9.2

box()

7
#include <ncurses.h>
WINDOW *create_newwin(int height, int width, int starty, int startx);
void destroy_win(WINDOW *local_win);
int main(int argc, char *argv[])
{ WINDOW *my_win;
int startx, starty, width, height;
int ch;
initscr();
cbreak();
keypad(stdscr, TRUE);

/* curses
*/
/* */
/* F1
*/

height = 3;
width = 10;

25 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

starty = (LINES - height) / 2;


startx = (COLS - width) / 2;

/* */
/* */

printw("Press F1 to exit");
refresh();
my_win = create_newwin(height, width, starty, startx);
while((ch = getch()) != KEY_F(1))
{ switch(ch)
{ case KEY_LEFT:
destroy_win(my_win);
my_win = create_newwin(height,
break;
case KEY_RIGHT:
destroy_win(my_win);
my_win = create_newwin(height,
break;
case KEY_UP:
destroy_win(my_win);
my_win = create_newwin(height,
break;
case KEY_DOWN:
destroy_win(my_win);
my_win = create_newwin(height,
break;
}
}
endwin();

/* curses

width, starty,--startx);

width, starty,++startx);

width, --starty,startx);

width, ++starty,startx);

*/

return 0;
}
WINDOW *create_newwin(int height, int width, int starty, int startx)
{
WINDOW *local_win;
local_win = newwin(height, width, starty, startx);
box(local_win, 0 , 0);
/* 0, 0 */
wrefresh(local_win);
/* box
*/
return local_win;
}
void destroy_win(WINDOW *local_win)
{/
* box(local_win, ' ', ' ');
*/
26 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

wborder(local_win, ' ', ' ', ' ',' ',' ',' ',' ',' ');
/* 9.3
* 1. win:
* 2. ls:
* 3. rs:
* 4. ts:
* 5. bs:
* 6. tl:
* 7. tr:
* 8. bl:
* 9. br:
*/
wrefresh(local_win);
delwin(local_win);
}

9.3

creat_newwin() newwin() box()


destory_win()
delwin(),startx starty

destory_win() wborder box


wborder()

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

+-----------------------+
|
|
|
|
|
|
|
|
|
|
|
|
+-----------------------+

27 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

9.4
COLS LINES initscr()

getch()

9.5

mvhline() mvvline()

8
#include <ncurses.h>
typedef struct _win_border_struct {
chtype ls, rs, ts, bs,
tl, tr, bl, br;
}WIN_BORDER;
typedef struct _WIN_struct {
int startx, starty;
int height, width;
WIN_BORDER border;
}WIN;
void init_win_params(WIN *p_win);
void print_win_params(WIN *p_win);
void create_box(WIN *win, int bool);
int main(int argc, char *argv[])
{ WIN win;
int ch;
initscr();
/* curses
start_color();
/*
cbreak();
/*
keypad(stdscr, TRUE);
/* F1

*/
*/
*/
*/

noecho();
init_pair(1, COLOR_CYAN, COLOR_BLACK);
28 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

/* */
init_win_params(&win);
print_win_params(&win);
attron(COLOR_PAIR(1));
printw("Press F1 to exit");
refresh();
attroff(COLOR_PAIR(1));
create_box(&win, TRUE);
while((ch = getch()) != KEY_F(1))
{ switch(ch)
{ case KEY_LEFT:
create_box(&win, FALSE);
--win.startx;
create_box(&win, TRUE);
break;
case KEY_RIGHT:
create_box(&win, FALSE);
++win.startx;
create_box(&win, TRUE);
break;
case KEY_UP:
create_box(&win, FALSE);
--win.starty;
create_box(&win, TRUE);
break;
case KEY_DOWN:
create_box(&win, FALSE);
++win.starty;
create_box(&win, TRUE);
break;
}
}
endwin();
/* curses

*/

return 0;
}
void init_win_params(WIN *p_win)
{
p_win->height = 3;
p_win->width = 10;
p_win->starty = (LINES - p_win->height)/2;
p_win->startx = (COLS - p_win->width)/2;

29 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

p_win->border.ls = '|';
p_win->border.rs = '|';
p_win->border.ts = '-';
p_win->border.bs = '-';
p_win->border.tl = '+';
p_win->border.tr = '+';
p_win->border.bl = '+';
p_win->border.br = '+';
}
void print_win_params(WIN *p_win)
{
#ifdef _DEBUG
mvprintw(25, 0, "%d %d %d %d", p_win->startx, p_win->starty,
p_win->width, p_win->height);
refresh();
#endif
}
void create_box(WIN *p_win, int bool)
{ int i, j;
int x, y, w, h;
x = p_win->startx;
y = p_win->starty;
w = p_win->width;
h = p_win->height;
if(bool == TRUE)
{ mvaddch(y, x, p_win->border.tl);
mvaddch(y, x + w, p_win->border.tr);
mvaddch(y + h, x, p_win->border.bl);
mvaddch(y + h, x + w, p_win->border.br);
mvhline(y, x + 1, p_win->border.ts, w - 1);
mvhline(y + h, x + 1, p_win->border.bs, w - 1);
mvvline(y + 1, x, p_win->border.ls, h - 1);
mvvline(y + 1, x + w, p_win->border.rs, h - 1);
}
else
for(j = y; j <= y + h; ++j)
for(i = x; i <= x + w; ++i)
mvaddch(j, i, ' ');
refresh();
}
30 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

10.1
CURSES

9
#include <ncurses.h>
void print_in_middle(WINDOW *win, int starty, int startx, int width, char
*string);
int main(int argc, char *argv[])
{ initscr();
/* curses */
if(has_colors() == FALSE)
{ endwin();
printf("You terminal does not support color\n");
exit(1);
}
start_color();
/* color */
init_pair(1, COLOR_RED, COLOR_BLACK);
attron(COLOR_PAIR(1));
print_in_middle(stdscr, LINES / 2, 0, 0, "Viola !!! In color ...");
attroff(COLOR_PAIR(1));
getch();
endwin();
}
void print_in_middle(WINDOW *win, int starty, int startx, int width, char
*string)
{ int length, x, y;
float temp;
if(win == NULL)
win = stdscr;
getyx(win, y, x);
if(startx != 0)
x = startx;
if(starty != 0)
y = starty;
if(width == 0)
width = 80;
length = strlen(string);
temp = (width - length)/ 2;

31 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

x = startx + (int)temp;
mvwprintw(win, y, x, "%s", string);
refresh();
}
start_color()

has_colors()
FLASE
start_color()CURSES
COLOR_BLACK

init_pairs()
COLOR_PAIR()
dailog
dailog shell

curses.h

COLOR_BLACK
0

COLOR_RED
1

COLOR_GREEN
2

COLOR_YELLOW
3

COLOR_BLUE
4

COLOR_MAGENTA
5

COLOR_CYAN
6
,
COLOR_WHITE
7

10.2
init_color() RGB
init_color()
init_color(COLOR_RED, 700, 0, 0);
/* 1
:
* 2, 3, 4 : R(red),G(green),B(blue)0
1000*/
ERRcan_change_color()
RGB 0 1000
(RED) R1000G0B0

10.3 Color Content


color_content() pair_content() pair
32 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

11.1
GUI
CURSES

getch()
cbreak
getch()
curses.h
F1 265 curses.h KEY_F()

getch()
int ch;
ch = getch();
getch()

curses.h

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

11.2.
10
#include <stdio.h>
#include <ncurses.h>
#define WIDTH 30
#define HEIGHT 10
33 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

int startx = 0;
int starty = 0;
char *choices[] = {
"Choice 1",
"Choice 2",
"Choice 3",
"Choice 4",
"Exit",
};
int n_choices = sizeof(choices) / sizeof(char *);
void print_menu(WINDOW *menu_win, int highlight);
int main()
{ WINDOW *menu_win;
int highlight = 1;
int choice = 0;
int c;
initscr();
clear();
noecho();
cbreak();

/* */

startx = (80 - WIDTH) / 2;


starty = (24 - HEIGHT) / 2;
menu_win = newwin(HEIGHT, WIDTH, starty, startx);
keypad(menu_win, TRUE);
mvprintw(0, 0,"Use arrow keys to go up and down, Press enter to select a
choice");
refresh();
print_menu(menu_win, highlight);
while(1)
{ c = wgetch(menu_win);
switch(c)
{ case KEY_UP:
if(highlight == 1)
highlight = n_choices;
else
--highlight;
break;
case KEY_DOWN:
if(highlight == n_choices)
highlight = 1;
else
34 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

++highlight;
break;
case 10:
choice = highlight;
break;
default:
mvprintw(24, 0, "Charcter pressed is = %3d Hopefully it can be printed
as '%c'", c, c);
refresh();
break;
}
print_menu(menu_win, highlight);
if(choice != 0) /**/
break;
}
mvprintw(23, 0, "You chose choice %d with choice string %s\n", choice,
choices[choice - 1]);
clrtoeol();
refresh();
endwin();
return 0;
}
void print_menu(WINDOW *menu_win, int highlight)
{
int x, y, i;
x = 2;
y = 2;
box(menu_win, 0, 0);
for(i = 0; i < n_choices; ++i)
{ if(highlight == i + 1)
/* */
{

wattron(menu_win, A_REVERSE);
mvwprintw(menu_win, y, x, "%s", choices[i]);
wattroff(menu_win, A_REVERSE);

}
else
mvwprintw(menu_win, y, x, "%s", choices[i]);
++y;
}
wrefresh(menu_win);
}

35 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

121 The Basics


, mousemask( ).
mousemask( mmask_t newmask, /* */
mmask_t *oldmask ) /* */
,
ALL_MOUSE_EVENTS

NCURSES
1

BUTTON1_PRESSED
1
BUTTON1_RELEASED
1
BUTTON1_CLICKED
1
BUTTON1_DOUBLE_CLICKED
1
BUTTON1_TRIPLE_CLICKED
1
BUTTON2_PRESSED
2
BUTTON2_RELEASED
2
BUTTON2_CLICKED
2
BUTTON2_DOUBLE_CLICKED
2
BUTTON2_TRIPLE_CLICKED
2
BUTTON3_PRESSED
3
BUTTON3_RELEASED
3
BUTTON3_CLICKED
3
BUTTON3_DOUBLE_CLICKED
3
BUTTON3_TRIPLE_CLICKED
3
BUTTON4_PRESSED
4
BUTTON4_RELEASED
4
BUTTON4_CLICKED
4
BUTTON4_DOUBLE_CLICKED
4
BUTTON4_TRIPLE_CLICKED
4
BUTTON_SHIFT
Shift
BUTTON_CTRL
Ctrl
BUTTON_ALT
Alt
ALL_MOUSE_EVENTS

REPORT_MOUSE_POSITION

36 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

122 Getting the events


getch()
KEY_MOUSE getmouse()

MEVENT event;
ch = getch();
if(ch == KEY_MOUSE)
if(getmouse(&event) == OK)
{
.
/* */
}

getmouse()
typedef struct
{
short id;
int x, y, z;
mmask_t bstate;
}

/* ID */
/* */
/* */

Bstate

if(event.bstate & BUTTON1_PRESSED)


printw("Left Button Pressed");

123

11
#include <ncurses.h>
#define WIDTH 30
#define HEIGHT 10
int startx = 0;
int starty = 0;
char *choices[] = {
"Choice 1",
37 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

"Choice 2",
"Choice 3",
"Choice 4",
"Exit",
};
int n_choices = sizeof(choices) / sizeof(char *);
void print_menu(WINDOW *menu_win, int highlight);
void report_choice(int mouse_x, int mouse_y, int *p_choice);
int main()
{ int c, choice = 0;
WINDOW *menu_win;
MEVENT event;
/* curses */
initscr();
clear();
noecho();
cbreak();

/* */

/* */
startx = (80 - WIDTH) / 2;
starty = (24 - HEIGHT) / 2;
attron(A_REVERSE);
mvprintw(23, 1, "Click on Exit to quit (Works best in a virtual console)");
refresh();
attroff(A_REVERSE);
/* */
menu_win = newwin(HEIGHT, WIDTH, starty, startx);
print_menu(menu_win, 1);
/* */
mousemask(ALL_MOUSE_EVENTS, NULL);
while(1)
{ c = wgetch(menu_win);
switch(c)
{ case KEY_MOUSE:
if(getmouse(&event) == OK)
{ /* */
if(event.bstate & BUTTON1_PRESSED)
38 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

report_choice(event.x + 1, event.y + 1, &choice);


if(choice == -1) /* */

goto end;
mvprintw(22, 1, "Choice made is : %d String Chosen is
\"%10s\"", choice, choices[choice - 1]);
refresh();
}
}
print_menu(menu_win, choice);
break;
}
}
end:
endwin();
return 0;
}

void print_menu(WINDOW *menu_win, int highlight)


{
int x, y, i;
x = 2;
y = 2;
box(menu_win, 0, 0);
for(i = 0; i < n_choices; ++i)
{ if(highlight == i + 1)
{ wattron(menu_win, A_REVERSE);
mvwprintw(menu_win, y, x, "%s", choices[i]);
wattroff(menu_win, A_REVERSE);
}
else
mvwprintw(menu_win, y, x, "%s", choices[i]);
++y;
}
wrefresh(menu_win);
}
/* */
void report_choice(int mouse_x, int mouse_y, int *p_choice)
{ int i,j, choice;
i = startx + 2;
j = starty + 3;
39 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

for(choice = 0; choice < n_choices; ++choice)


if(mouse_y == j + choice && mouse_x >= i && mouse_x <= i +
strlen(choices[choice]))
{ if(choice == n_choices - 1)
*p_choice = -1;
else
*p_choice = choice + 1;
break;
}
}

124
mouse_trafo() wmouse_trafo()
curs_mouse(3X) man
mouseinterval()

40 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

131 getyx()
getyx()
getyx()

getyx(win, y, x);
/*
win:
*
y, x: y, x
*/
getparyx()

getparyx()
getbegyx() getmaxyx()

132
scr_dump()
scr_dump(const char *file)
scr_restore()scr_restore(const
char *file)

133
getwin()getwin(FILE * filep)
putwin()
putwin(WINDOW *win, FILE * filep)
copywin()

int copywin(
const WINDOW *src,
WINDOW *dst,
int sminrow,
int smincol,

/**/
/**/
/**/
/**/

41 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

int
int
int
int
int

dminrow,
dmincol,
dmaxrow,
dmaxcol,
over)

/**/
/**/
/**/
/**/
/**/

42 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

CURSES

141 curs_set()curs_set()
012

142 Curses Temporarily Leaving Curses mode


CURSES
def_prog_mode() tty
end_win() CURSES tty
reset_prog_mode() def_prog_mode()
refresh()
CURSES
12
#include <ncurses.h>
int main()
{
initscr();
printw("Hello World !!!\n");
refresh();
def_prog_mode();
endwin();
system("/bin/sh");
reset_prog_mode();
refresh();
printw("Another String\n");
refresh();
endwin();

/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*

CURSES */
Hello World!!! */
*/
tty */
CURSES */
*/
def_prog_mode() tty */
refresh() */
CURSES */
*/
CURSES */

return 0;
}

143 ACS_ACS_ variables


DOS
NCURSES box()
ACS_ ACS Alternative
Character Set

43 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

13ACS
#include <ncurses.h>
int main()
{
initscr();
printw("Upper left corner
printw("\n");
printw("Lower left corner
printw("Lower right corner
printw("\n");
printw("Tee pointing right
printw("Tee pointing left
printw("Tee pointing up
printw("Tee pointing down
printw("Horizontal line
printw("Vertical line
printw("Large Plus or cross over
printw("Scan Line 1
printw("Scan Line 3
printw("Scan Line 7
printw("Scan Line 9
printw("Diamond
printw("Checker board (stipple)
printw("Degree Symbol
printw("Plus/Minus Symbol
printw("Bullet
printw("Arrow Pointing Left
printw("Arrow Pointing Right
printw("Arrow Pointing Down
printw("Arrow Pointing Up
printw("Board of squares
printw("Lantern Symbol
printw("Solid Square Block
printw("Less/Equal sign
printw("Greater/Equal sign
printw("Pi
printw("Not equal
printw("UK pound sign
refresh();
getch();
endwin();
return 0;
}

");

addch(ACS_ULCORNER);

"); addch(ACS_LLCORNER); printw("\n");


");
addch(ACS_LRCORNER);
"); addch(ACS_LTEE); printw("\n");
"); addch(ACS_RTEE); printw("\n");
"); addch(ACS_BTEE); printw("\n");
"); addch(ACS_TTEE); printw("\n");
"); addch(ACS_HLINE); printw("\n");
"); addch(ACS_VLINE); printw("\n");
"); addch(ACS_PLUS); printw("\n");
"); addch(ACS_S1); printw("\n");
"); addch(ACS_S3); printw("\n");
"); addch(ACS_S7); printw("\n");
"); addch(ACS_S9); printw("\n");
"); addch(ACS_DIAMOND); printw("\n");
"); addch(ACS_CKBOARD); printw("\n");
"); addch(ACS_DEGREE); printw("\n");
"); addch(ACS_PLMINUS); printw("\n");
"); addch(ACS_BULLET); printw("\n");
"); addch(ACS_LARROW); printw("\n");
"); addch(ACS_RARROW); printw("\n");
"); addch(ACS_DARROW); printw("\n");
"); addch(ACS_UARROW); printw("\n");
"); addch(ACS_BOARD); printw("\n");
"); addch(ACS_LANTERN); printw("\n");
"); addch(ACS_BLOCK); printw("\n");
"); addch(ACS_LEQUAL); printw("\n");
"); addch(ACS_GEQUAL); printw("\n");
"); addch(ACS_PI); printw("\n");
"); addch(ACS_NEQUAL); printw("\n");
"); addch(ACS_STERLING); printw("\n");

44 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

curses CURSE
panel
menuform

45 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

curses

Panel Library ncureses

Panels
panels wnoutrefresh()
doupdate()

16.1

refresh

1 newwin
2
new_panel()
3 update_panels() doupdate()

4 show_panel(), hide_panel(), move_panel()


panel_hidden() panel_window()
set_panel_userptr()
panel_userptr()
5 del_panel()
3

16.2

panel.h

46 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

-lpanel lncurses
#include <panel.h>
.
.
.
: gcc <program file> -lpanel lncurses
14
#include <panel.h>
int main()
{
WINDOW *my_wins[3];
PANEL *my_panels[3];
int lines = 10, cols = 40, y = 2, x = 4, i;
initscr();
cbreak();
noecho();
/* */
my_wins[0] = newwin(lines, cols, y, x);
my_wins[1] = newwin(lines, cols, y + 1, x + 5);
my_wins[2] = newwin(lines, cols, y + 2, x + 10);
/* */
for(i = 0; i < 3; +++i)
box(my_wins[i], 0, 0);
/* */
my_panels[0] = new_panel(my_wins[0]);/* 0 , :
stdscr-0 */
my_panels[1] = new_panel(my_wins[1]);/* 1 , :
stdscr-0-1 */
my_panels[2] = new_panel(my_wins[2]);/* 2 , :
stdscr-0-1-2 */
/* 2 */
update_panels();
/* */
doupdate();
getch();
endwin();
47 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

}
newwin()
new_panel()
panels update_panels() doupdate()

16.3
3 <TAB>

15
#include <panel.h>
#define NLINES 10
#define NCOLS 40
void init_wins(WINDOW **wins, int n);
void win_show(WINDOW *win, char *label, int label_color);
void print_in_middle(WINDOW *win, int starty, int startx, int width, char
*string, chtype color);
int main()
{ WINDOW *my_wins[3];
PANEL *my_panels[3];
PANEL *top;
int ch;
/* curses */
initscr();
start_color();
cbreak();
noecho();
keypad(stdscr, TRUE);
/* */
init_pair(1,
init_pair(2,
init_pair(3,
init_pair(4,

COLOR_RED, COLOR_BLACK);
COLOR_GREEN, COLOR_BLACK);
COLOR_BLUE, COLOR_BLACK);
COLOR_CYAN, COLOR_BLACK);

init_wins(my_wins, 3);
/* */
my_panels[0] = new_panel(my_wins[0]);/* 0 : stdscr-0
48 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

*/
my_panels[1] = new_panel(my_wins[1]); /* 1 :
stdscr-0-1 */
my_panels[2] = new_panel(my_wins[2]); /* 2 :
stdscr-0-1-2 */
/* */
set_panel_userptr(my_panels[0], my_panels[1]);
set_panel_userptr(my_panels[1], my_panels[2]);
set_panel_userptr(my_panels[2], my_panels[0]);
/* 2 */
update_panels();
/* */
attron(COLOR_PAIR(4));
mvprintw(LINES - 2, 0, "Use tab to browse through the windows (F1 to Exit)");
attroff(COLOR_PAIR(4));
doupdate();
top = my_panels[2];
while((ch = getch()) != KEY_F(1))
{ switch(ch)
{ case 9:
top = (PANEL *)panel_userptr(top);
top_panel(top);
break;
}
update_panels();
doupdate();
}
endwin();
return 0;
}
/* */
void init_wins(WINDOW **wins, int n)
{ int x, y, i;
char label[80];
y = 2;
x = 10;
for(i = 0; i < n; ++i)
{ wins[i] = newwin(NLINES, NCOLS, y, x);
49 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

sprintf(label, "Window Number %d", i + 1);


win_show(wins[i], label, i + 1);
y += 3;
x += 7;
}
}
/* */
void win_show(WINDOW *win, char *label, int label_color)
{ int startx, starty, height, width;
getbegyx(win, starty, startx);
getmaxyx(win, height, width);
box(win, 0, 0);
mvwaddch(win, 2, 0, ACS_LTEE);
mvwhline(win, 2, 1, ACS_HLINE, width - 2);
mvwaddch(win, 2, width - 1, ACS_RTEE);
print_in_middle(win, 1, 0, width, label, COLOR_PAIR(label_color));
}
void print_in_middle(WINDOW *win, int starty, int startx, int width, char
*string, chtype color)
{ int length, x, y;
float temp;
if(win == NULL)
win = stdscr;
getyx(win, y, x);
if(startx != 0)
x = startx;
if(starty != 0)
y = starty;
if(width == 0)
width = 80;
length = strlen(string);
temp = (width - length)/ 2;
x = startx + (int)temp;
wattron(win, color);
mvwprintw(win, y, x, "%s", string);
wattroff(win, color);
refresh();
50 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

16.4

set_panel_userptr() panel_userptr()
top_panel()

16.5
move_panel()
move_panel() mvwin()

replace_panel()

panel_window()
<TAB>
r m

16
#include <panel.h>
typedef struct _PANEL_DATA {
int x, y, w, h;
char label[80];
int label_color;
PANEL *next;
}PANEL_DATA;
#define NLINES 10
#define NCOLS 40
void init_wins(WINDOW **wins, int n);
void win_show(WINDOW *win, char *label, int label_color);
void print_in_middle(WINDOW *win, int starty, int startx, int width, char
*string, chtype color);
void set_user_ptrs(PANEL **panels, int n);
int main()
{ WINDOW *my_wins[3];
51 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

PANEL *my_panels[3];
PANEL_DATA *top;
PANEL *stack_top;
WINDOW *temp_win, *old_win;
int ch;
int newx, newy, neww, newh;
int size = FALSE, move = FALSE;
/* curses */
initscr();
start_color();
cbreak();
noecho();
keypad(stdscr, TRUE);
/* */
init_pair(1,
init_pair(2,
init_pair(3,
init_pair(4,

COLOR_RED, COLOR_BLACK);
COLOR_GREEN, COLOR_BLACK);
COLOR_BLUE, COLOR_BLACK);
COLOR_CYAN, COLOR_BLACK);

init_wins(my_wins, 3);
/* 2 */
my_panels[0] = new_panel(my_wins[0]); /* 0 :
stdscr-0 */
my_panels[1] = new_panel(my_wins[1]); /* 1 :
stdscr-0-1 */
my_panels[2] = new_panel(my_wins[2]); /* 2 , :
stdscr-0-1-2 */
set_user_ptrs(my_panels, 3);
/* 2 */
update_panels();
/* */
attron(COLOR_PAIR(4));
mvprintw(LINES - 3, 0, "Use 'm' for moving, 'r' for resizing");
mvprintw(LINES - 2, 0, "Use tab to browse through the windows (F1 to Exit)");
attroff(COLOR_PAIR(4));
doupdate();
stack_top = my_panels[2];
top = (PANEL_DATA *)panel_userptr(stack_top);
52 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

newx = top->x;
newy = top->y;
neww = top->w;
newh = top->h;
while((ch = getch()) != KEY_F(1))
{ switch(ch)
{ case 9:
/* Tab */
top = (PANEL_DATA *)panel_userptr(stack_top);
top_panel(top->next);
stack_top = top->next;
top = (PANEL_DATA *)panel_userptr(stack_top);
newx = top->x;
newy = top->y;
neww = top->w;
newh = top->h;
break;
case 'r': /* */
size = TRUE;
attron(COLOR_PAIR(4));
mvprintw(LINES - 4, 0, "Entered Resizing :Use Arrow Keys to
resize and press <ENTER> to end resizing");
refresh();
attroff(COLOR_PAIR(4));
break;
case 'm':
/* */
attron(COLOR_PAIR(4));
mvprintw(LINES - 4, 0, "Entered Moving: Use Arrow Keys to
Move and press <ENTER> to end moving");
refresh();
attroff(COLOR_PAIR(4));
move = TRUE;
break;
case KEY_LEFT:
if(size == TRUE)
{ --newx;
++neww;
}
if(move == TRUE)
--newx;
break;
case KEY_RIGHT:
if(size == TRUE)
{ ++newx;
--neww;
53 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

}
if(move == TRUE)
++newx;
break;
case KEY_UP:
if(size == TRUE)
{ --newy;
++newh;
}
if(move == TRUE)
--newy;
break;
case KEY_DOWN:
if(size == TRUE)
{ ++newy;
--newh;
}
if(move == TRUE)
++newy;
break;
case 10:
/* Enter */
move(LINES - 4, 0);
clrtoeol();
refresh();
if(size == TRUE)
{ old_win = panel_window(stack_top);
temp_win = newwin(newh, neww, newy, newx);
replace_panel(stack_top, temp_win);
win_show(temp_win, top->label, top->label_color);
delwin(old_win);
size = FALSE;
}
if(move == TRUE)
{ move_panel(stack_top, newy, newx);
move = FALSE;
}
break;
}
attron(COLOR_PAIR(4));
mvprintw(LINES - 3, 0, "Use 'm' for moving, 'r' for resizing");
mvprintw(LINES - 2, 0, "Use tab to browse through the windows (F1 to
Exit)");
attroff(COLOR_PAIR(4));
54 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

refresh();
update_panels();
doupdate();
}
endwin();
return 0;
}
/* */
void init_wins(WINDOW **wins, int n)
{ int x, y, i;
char label[80];
y = 2;
x = 10;
for(i = 0; i < n; ++i)
{ wins[i] = newwin(NLINES, NCOLS, y, x);
sprintf(label, "Window Number %d", i + 1);
win_show(wins[i], label, i + 1);
y += 3;
x += 7;
}
}
/* PANEL_DATA */
void set_user_ptrs(PANEL **panels, int n)
{ PANEL_DATA *ptrs;
WINDOW *win;
int x, y, w, h, i;
char temp[80];
ptrs = (PANEL_DATA *)calloc(n, sizeof(PANEL_DATA));
for(i = 0;i < n; ++i)
{ win = panel_window(panels[i]);
getbegyx(win, y, x);
getmaxyx(win, h, w);
ptrs[i].x = x;
ptrs[i].y = y;
ptrs[i].w = w;
ptrs[i].h = h;
sprintf(temp, "Window Number %d", i + 1);
strcpy(ptrs[i].label, temp);
ptrs[i].label_color = i + 1;
55 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

if(i + 1 == n)
ptrs[i].next = panels[0];
else
ptrs[i].next = panels[i + 1];
set_panel_userptr(panels[i], &ptrs[i]);
}
}
/* */
void win_show(WINDOW *win, char *label, int label_color)
{ int startx, starty, height, width;
getbegyx(win, starty, startx);
getmaxyx(win, height, width);
box(win, 0, 0);
mvwaddch(win, 2, 0, ACS_LTEE);
mvwhline(win, 2, 1, ACS_HLINE, width - 2);
mvwaddch(win, 2, width - 1, ACS_RTEE);
print_in_middle(win, 1, 0, width, label, COLOR_PAIR(label_color));
}
void print_in_middle(WINDOW *win, int starty, int startx, int width, char
*string, chtype color)
{ int length, x, y;
float temp;
if(win == NULL)
win = stdscr;
getyx(win, y, x);
if(startx != 0)
x = startx;
if(starty != 0)
y = starty;
if(width == 0)
width = 80;
length = strlen(string);
temp = (width - length)/ 2;
x = startx + (int)temp;
wattron(win, color);
mvwprintw(win, y, x, "%s", string);
wattroff(win, color);
refresh();
56 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

r
<ENTER>

m
<ENTER> panel()

PANEL_DATA
PANEL_DATA

16.6
hide_panel()
update_panels() doupdate()
show_panel()
a b c

panel_hidden Michael Andres


17
#include <panel.h>
typedef struct _PANEL_DATA {
int hide;
/* */
}PANEL_DATA;
#define NLINES 10
#define NCOLS 40
void init_wins(WINDOW **wins, int n);
void win_show(WINDOW *win, char *label, int label_color);
void print_in_middle(WINDOW *win, int starty, int startx, int width, char
*string, chtype color);
int main()
{ WINDOW *my_wins[3];
PANEL *my_panels[3];
PANEL_DATA panel_datas[3];
PANEL_DATA *temp;
57 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

int ch;
/* curses */
initscr();
start_color();
cbreak();
noecho();
keypad(stdscr, TRUE);
/* */
init_pair(1,
init_pair(2,
init_pair(3,
init_pair(4,

COLOR_RED, COLOR_BLACK);
COLOR_GREEN, COLOR_BLACK);
COLOR_BLUE, COLOR_BLACK);
COLOR_CYAN, COLOR_BLACK);

init_wins(my_wins, 3);
/* 2 */
my_panels[0] = new_panel(my_wins[0]); /* Push 0, order: stdscr-0 */
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
*/
/* */
panel_datas[0].hide = FALSE;
panel_datas[1].hide = FALSE;
panel_datas[2].hide = FALSE;
set_panel_userptr(my_panels[0], &panel_datas[0]);
set_panel_userptr(my_panels[1], &panel_datas[1]);
set_panel_userptr(my_panels[2], &panel_datas[2]);
/* */
update_panels();
/* */
attron(COLOR_PAIR(4));
mvprintw(LINES - 3, 0, "Show or Hide a window with 'a'(first window)
'b'(Second Window) 'c'(Third Window)");
mvprintw(LINES - 2, 0, "F1 to Exit");
attroff(COLOR_PAIR(4));
doupdate();
58 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

while((ch = getch()) != KEY_F(1))


{ switch(ch)
{ case 'a':
temp = (PANEL_DATA *)panel_userptr(my_panels[0]);
if(temp->hide == FALSE)
{ hide_panel(my_panels[0]);
temp->hide = TRUE;
}
else
{ show_panel(my_panels[0]);
temp->hide = FALSE;
}
break;
case 'b':
temp = (PANEL_DATA *)panel_userptr(my_panels[1]);
if(temp->hide == FALSE)
{ hide_panel(my_panels[1]);
temp->hide = TRUE;
}
else
{ show_panel(my_panels[1]);
temp->hide = FALSE;
}
break;
case 'c':
temp = (PANEL_DATA *)panel_userptr(my_panels[2]);
if(temp->hide == FALSE)
{ hide_panel(my_panels[2]);
temp->hide = TRUE;
}
else
{ show_panel(my_panels[2]);
temp->hide = FALSE;
}
break;
}
update_panels();
doupdate();
}
endwin();
return 0;
}

59 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

/* */
void init_wins(WINDOW **wins, int n)
{ int x, y, i;
char label[80];
y = 2;
x = 10;
for(i = 0; i < n; ++i)
{ wins[i] = newwin(NLINES, NCOLS, y, x);
sprintf(label, "Window Number %d", i + 1);
win_show(wins[i], label, i + 1);
y += 3;
x += 7;
}
}
/* */
void win_show(WINDOW *win, char *label, int label_color)
{ int startx, starty, height, width;
getbegyx(win, starty, startx);
getmaxyx(win, height, width);
box(win, 0, 0);
mvwaddch(win, 2, 0, ACS_LTEE);
mvwhline(win, 2, 1, ACS_HLINE, width - 2);
mvwaddch(win, 2, width - 1, ACS_RTEE);
print_in_middle(win, 1, 0, width, label, COLOR_PAIR(label_color));
}
void print_in_middle(WINDOW *win, int starty, int startx, int width, char
*string, chtype color)
{ int length, x, y;
float temp;
if(win == NULL)
win = stdscr;
getyx(win, y, x);
if(startx != 0)
x = startx;
if(starty != 0)
y = starty;
if(width == 0)
60 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

width = 80;
length = strlen(string);
temp = (width - length)/ 2;
x = startx + (int)temp;
wattron(win, color);
mvwprintw(win, y, x, "%s", string);
wattroff(win, color);
refresh();
}

16.7 panel_above() panel_below()


panel_above() panel_below()
NULL

61 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

Menu cureses

171

menu_driver()

1 curses
2 new_item()
3 new_menu()
4 menu_post()
5 menu_driver()
6 menu_unpost()
7 free_menu()
8 free_item()
9 curses

172
menu.h
-lmenu -lncurses
#include <menu.h>
.
.
.
: gcc <> -lmenu lncurses
18
#include <curses.h>
#include <menu.h>
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
#define CTRLD
4

62 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

char *choices[] = {
"Choice
"Choice
"Choice
"Choice
"Exit",

1",
2",
3",
4",

};
int main()
{ ITEM **my_items;
int c;
MENU *my_menu;
int n_choices, i;
ITEM *cur_item;
initscr();
cbreak();
noecho();
keypad(stdscr, TRUE);
n_choices = ARRAY_SIZE(choices);
my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *));
for(i = 0; i < n_choices; ++i)
my_items[i] = new_item(choices[i], choices[i]);
my_items[n_choices] = (ITEM *)NULL;
my_menu = new_menu((ITEM **)my_items);
mvprintw(LINES - 2, 0, "F1 to Exit");
post_menu(my_menu);
refresh();
while((c = getch()) != KEY_F(1))
{ switch(c)
{ case KEY_DOWN:
menu_driver(my_menu, REQ_DOWN_ITEM);
break;
case KEY_UP:
menu_driver(my_menu, REQ_UP_ITEM);
break;
}
}
free_item(my_items[0]);
63 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

free_item(my_items[1]);
free_menu(my_menu);
endwin();
}
new_item()
new_menu()
menu_driver()
menu_driver()
ASCII
KEY_MOUSE
menu_driver()
REQ_LEFT_ITEM

REQ_RIGHT_ITEM

REQ_UP_ITEM

REQ_DOWN_ITEM

REQ_SCR_ULINE

REQ_SCR_DLINE

REQ_SCR_DPAGE

REQ_SCR_UPAGE

REQ_FIRST_ITEM

REQ_LAST_ITEM

REQ_NEXT_ITEM

REQ_PREV_ITEM

REQ_TOGGLE_ITEM
/
REQ_CLEAR_PATTERN
REQ_BACK_PATTERN

REQ_NEXT_MATCH

REQ_PREV_MATCH

REQ_UP_ITEM REQ_DOWN_ITEM menu_driver()


menu_driver()

17.3 Menu Driver


menu_driver
menu_driver()
KEY_MOUSE

REQ_LEFT_ITEM REQ_RIGHT_ITEM
menu_format()
menu drive

64 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

REQ_UP_ITEM REQ_DOWN_ITEM
menu_driver
menu_driver
REQ_SCR_*
REQ_SCR_ULINE, REQ_SCR_DLINE, REQ_SCR_DPAGE REQ_SCR_UPAGE

menu_driver //
REQ_FIRST_ITEM, REQ_LAST_ITEM, REQ_NEXT_ITEM REQ_PREV_ITEM

REQ_TOGGLE_ITEM

O_ONEVALUE set_menu_opts()

ASCII
menu_driver ASCII

REQ_CLEAR_PATTERN
REQ_BACK_PATTERN
REQ_NEXT_PATTERN REQ_PREV_PATTERN
/

man

KEY_MOUSE

REQ_SCR_ULINE
REQ_SCR_UPAGE REQ_FIRST_ITEM
REQ_SCR_DLINE REQ_SCR_DPAGE
REQ_LAST_ITEM

17.4

stdscr

65 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

19
#include <menu.h>
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
#define CTRLD
4
char *choices[] = {
"Choice 1",
"Choice 2",
"Choice 3",
"Choice 4",
"Exit",
(char *)NULL,
};
void print_in_middle(WINDOW *win, int starty, int startx, int width, char
*string, chtype color);
int main()
{ ITEM **my_items;
int c;
MENU *my_menu;
WINDOW *my_menu_win;
int n_choices, i;
/* curses */
initscr();
start_color();
cbreak();
noecho();
keypad(stdscr, TRUE);
init_pair(1, COLOR_RED, COLOR_BLACK);
/* */
n_choices = ARRAY_SIZE(choices);
my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *));
for(i = 0; i < n_choices; ++i)
my_items[i] = new_item(choices[i], choices[i]);
/* */
my_menu = new_menu((ITEM **)my_items);
/* */
my_menu_win = newwin(10, 40, 4, 4);
keypad(my_menu_win, TRUE);
66 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

/* */
set_menu_win(my_menu, my_menu_win);
set_menu_sub(my_menu, derwin(my_menu_win, 6, 38, 3, 1));
/* " * " */
set_menu_mark(my_menu, " * ");
/* */
box(my_menu_win, 0, 0);
print_in_middle(my_menu_win, 1, 0, 40, "My Menu", COLOR_PAIR(1));
mvwaddch(my_menu_win, 2, 0, ACS_LTEE);
mvwhline(my_menu_win, 2, 1, ACS_HLINE, 38);
mvwaddch(my_menu_win, 2, 39, ACS_RTEE);
mvprintw(LINES - 2, 0, "F1 to exit");
refresh();
/* */
post_menu(my_menu);
wrefresh(my_menu_win);
while((c = wgetch(my_menu_win)) != KEY_F(1))
{
switch(c)
{ case KEY_DOWN:
menu_driver(my_menu, REQ_DOWN_ITEM);
break;
case KEY_UP:
menu_driver(my_menu, REQ_UP_ITEM);
break;
}
wrefresh(my_menu_win);
}
/* */
unpost_menu(my_menu);
free_menu(my_menu);
for(i = 0; i < n_choices; ++i)
free_item(my_items[i]);
endwin();
}
void print_in_middle(WINDOW *win, int starty, int startx, int width, char
*string, chtype color)
{ int length, x, y;
67 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

float temp;
if(win == NULL)
win = stdscr;
getyx(win, y, x);
if(startx != 0)
x = startx;
if(starty != 0)
y = starty;
if(width == 0)
width = 80;
length = strlen(string);
temp = (width - length)/ 2;
x = startx + (int)temp;
wattron(win, color);
mvwprintw(win, y, x, "%s", string);
wattroff(win, color);
refresh();
}

set_menu_win()
set_menu_mark()

17.5

REQ_DOWN_ITEM
REQ_SCR_DLINE REQ_SCR_

20
#include <curses.h>
#include <menu.h>
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
#define CTRLD
4
char *choices[] = {
"Choice 1",
"Choice 2",
"Choice 3",
"Choice 4",
"Choice 5",
"Choice 6",
68 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

"Choice 7",
"Choice 8",
"Choice 9",
"Choice 10",
"Exit",
(char *)NULL,
};
void print_in_middle(WINDOW *win, int starty, int startx, int width, char
*string, chtype color);
int main()
{ ITEM **my_items;
int c;
MENU *my_menu;
WINDOW *my_menu_win;
int n_choices, i;
/* curses */
initscr();
start_color();
cbreak();
noecho();
keypad(stdscr, TRUE);
init_pair(1, COLOR_RED, COLOR_BLACK);
init_pair(2, COLOR_CYAN, COLOR_BLACK);
/* */
n_choices = ARRAY_SIZE(choices);
my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *));
for(i = 0; i < n_choices; ++i)
my_items[i] = new_item(choices[i], choices[i]);
/* */
my_menu = new_menu((ITEM **)my_items);
/* */
my_menu_win = newwin(10, 40, 4, 4);
keypad(my_menu_win, TRUE);
/* */
set_menu_win(my_menu, my_menu_win);
set_menu_sub(my_menu, derwin(my_menu_win, 6, 38, 3, 1));
set_menu_format(my_menu, 5, 1);

69 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

/* " * " */
set_menu_mark(my_menu, " * ");
/* */
box(my_menu_win, 0, 0);
print_in_middle(my_menu_win, 1, 0, 40, "My Menu", COLOR_PAIR(1));
mvwaddch(my_menu_win, 2, 0, ACS_LTEE);
mvwhline(my_menu_win, 2, 1, ACS_HLINE, 38);
mvwaddch(my_menu_win, 2, 39, ACS_RTEE);
/* */
post_menu(my_menu);
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 - 1, 0, "Arrow Keys to navigate (F1 to Exit)");
attroff(COLOR_PAIR(2));
refresh();
while((c = wgetch(my_menu_win)) != KEY_F(1))
{
switch(c)
{ case KEY_DOWN:
menu_driver(my_menu, REQ_DOWN_ITEM);
break;
case KEY_UP:
menu_driver(my_menu, REQ_UP_ITEM);
break;
case KEY_NPAGE:
menu_driver(my_menu, REQ_SCR_DPAGE);
break;
case KEY_PPAGE:
menu_driver(my_menu, REQ_SCR_UPAGE);
break;
}
wrefresh(my_menu_win);
}
/* */
unpost_menu(my_menu);
free_menu(my_menu);
for(i = 0; i < n_choices; ++i)
free_item(my_items[i]);
endwin();
70 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

}
void print_in_middle(WINDOW *win, int starty, int startx, int width, char
*string, chtype color)
{ int length, x, y;
float temp;
if(win == NULL)
win = stdscr;
getyx(win, y, x);
if(startx != 0)
x = startx;
if(starty != 0)
y = starty;
if(width == 0)
width = 80;
length = strlen(string);
temp = (width - length)/ 2;
x = startx + (int)temp;
wattron(win, color);
mvwprintw(win, y, x, "%s", string);
wattroff(win, color);
refresh();
}
10
6 set_menu_format()

PAGE UP PAGE
DOWN REQ_SCR_UPAGE REQ_SCR_PATTERN menu_driver()

17.6
set_menu_format()

21
#include <curses.h>
#include <menu.h>
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
#define CTRLD
4
char *choices[] = {
"Choice 1", "Choice 2", "Choice 3", "Choice 4", "Choice 5",
71 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

"Choice 6", "Choice 7", "Choice 8", "Choice 9", "Choice 10",
"Choice 11", "Choice 12", "Choice 13", "Choice 14", "Choice 15",
"Choice 16", "Choice 17", "Choice 18", "Choice 19", "Choice 20",
"Exit",
(char *)NULL,
};
int main()
{ ITEM **my_items;
int c;
MENU *my_menu;
WINDOW *my_menu_win;
int n_choices, i;
/* curses */
initscr();
start_color();
cbreak();
noecho();
keypad(stdscr, TRUE);
init_pair(1, COLOR_RED, COLOR_BLACK);
init_pair(2, COLOR_CYAN, COLOR_BLACK);
/* */
n_choices = ARRAY_SIZE(choices);
my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *));
for(i = 0; i < n_choices; ++i)
my_items[i] = new_item(choices[i], choices[i]);
/* */
my_menu = new_menu((ITEM **)my_items);
/* */
menu_opts_off(my_menu, O_SHOWDESC);
/* */
my_menu_win = newwin(10, 70, 4, 4);
keypad(my_menu_win, TRUE);
/* */
set_menu_win(my_menu, my_menu_win);
set_menu_sub(my_menu, derwin(my_menu_win, 6, 68, 3, 1));
set_menu_format(my_menu, 5, 3);
set_menu_mark(my_menu, " * ");
72 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

/* */
box(my_menu_win, 0, 0);
attron(COLOR_PAIR(2));
mvprintw(LINES - 3, 0, "Use PageUp and PageDown to scroll");
mvprintw(LINES - 2, 0, "Use Arrow Keys to navigate (F1 to Exit)");
attroff(COLOR_PAIR(2));
refresh();
/* */
post_menu(my_menu);
wrefresh(my_menu_win);
while((c = wgetch(my_menu_win)) != KEY_F(1))
{
switch(c)
{ case KEY_DOWN:
menu_driver(my_menu, REQ_DOWN_ITEM);
break;
case KEY_UP:
menu_driver(my_menu, REQ_UP_ITEM);
break;
case KEY_LEFT:
menu_driver(my_menu, REQ_LEFT_ITEM);
break;
case KEY_RIGHT:
menu_driver(my_menu, REQ_RIGHT_ITEM);
break;
case KEY_NPAGE:
menu_driver(my_menu, REQ_SCR_DPAGE);
break;
case KEY_PPAGE:
menu_driver(my_menu, REQ_SCR_UPAGE);
break;
}
wrefresh(my_menu_win);
}
/* */
unpost_menu(my_menu);
free_menu(my_menu);
for(i = 0; i < n_choices; ++i)
free_item(my_items[i]);
endwin();
73 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

}
set_menu_format() 3 3
menu_opts_off()
set_menu_opts()menu_opts_on() menu_opts()

O_ONEVALUE

O_SHOWDESC

O_ROWMAJOR

O_IGNORECASE

O_SHOWMATCH

O_NONCYCLIC

menu_opts_on() menu_opts_off()
set_menu_opts()

OR menu_opts()

17.7
O_ONEVALUE
REQ_TOGGLE_ITEM

22
#include <curses.h>
#include <menu.h>
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
#define CTRLD
4
char *choices[] = {
"Choice 1",
"Choice 2",
"Choice 3",
"Choice 4",
74 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

"Choice 5",
"Choice 6",
"Choice 7",
"Exit",
};
int main()
{ ITEM **my_items;
int c;
MENU *my_menu;
int n_choices, i;
ITEM *cur_item;
/* curses */
initscr();
cbreak();
noecho();
keypad(stdscr, TRUE);
/* */
n_choices = ARRAY_SIZE(choices);
my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *));
for(i = 0; i < n_choices; ++i)
my_items[i] = new_item(choices[i], choices[i]);
my_items[n_choices] = (ITEM *)NULL;
my_menu = new_menu((ITEM **)my_items);
/* */
menu_opts_off(my_menu, O_ONEVALUE);
mvprintw(LINES - 3, 0, "Use <SPACE> to select or unselect an item.");
mvprintw(LINES - 2, 0, "<ENTER> to see presently selected items(F1 to Exit)");
post_menu(my_menu);
refresh();
while((c = getch()) != KEY_F(1))
{
switch(c)
{ case KEY_DOWN:
menu_driver(my_menu, REQ_DOWN_ITEM);
break;
case KEY_UP:
menu_driver(my_menu, REQ_UP_ITEM);
break;
75 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

case ' ':


menu_driver(my_menu, REQ_TOGGLE_ITEM);
break;
case 10:
/* Enter */
{

char temp[200];
ITEM **items;
items = menu_items(my_menu);
temp[0] = '\0';
for(i = 0; i < item_count(my_menu); ++i)
if(item_value(items[i]) == TRUE)
{ strcat(temp, item_name(items[i]));
strcat(temp, " ");
}
move(20, 0);
clrtoeol();
mvprintw(20, 0, temp);
refresh();

}
break;
}
}
free_item(my_items[0]);
free_item(my_items[1]);
free_menu(my_menu);
endwin();
}
Woo REQ_TOGGLE_ITEM
REQ_TOGGLE_ITEM
REQ_TOGGLE_ITEM menu_driver

<ENTER> menu_items()

item_value() TRUE item_count()


item_name() item_description()

17.8

DOS set_menu_fore() set_menu_back()

set_menu_grey()
76 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

O_SELECTABLE item_opts_off()

23
#include <menu.h>
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
#define CTRLD
4
char *choices[] = {
"Choice
"Choice
"Choice
"Choice
"Choice
"Choice
"Choice
"Exit",

1",
2",
3",
4",
5",
6",
7",

};
int main()
{ ITEM **my_items;
int c;
MENU *my_menu;
int n_choices, i;
ITEM *cur_item;
/* curses */
initscr();
start_color();
cbreak();
noecho();
keypad(stdscr, TRUE);
init_pair(1, COLOR_RED, COLOR_BLACK);
init_pair(2, COLOR_GREEN, COLOR_BLACK);
init_pair(3, COLOR_MAGENTA, COLOR_BLACK);
/* */
n_choices = ARRAY_SIZE(choices);
my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *));
for(i = 0; i < n_choices; ++i)
my_items[i] = new_item(choices[i], choices[i]);
my_items[n_choices] = (ITEM *)NULL;
item_opts_off(my_items[3], O_SELECTABLE);
77 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

item_opts_off(my_items[6], O_SELECTABLE);
/* */
my_menu = new_menu((ITEM **)my_items);
/* */
set_menu_fore(my_menu, COLOR_PAIR(1) | A_REVERSE);
set_menu_back(my_menu, COLOR_PAIR(2));
set_menu_grey(my_menu, COLOR_PAIR(3));
/* */
mvprintw(LINES - 3, 0, "Press <ENTER> to see the option selected");
mvprintw(LINES - 2, 0, "Up and Down arrow keys to naviage (F1 to Exit)");
post_menu(my_menu);
refresh();
while((c = getch()) != KEY_F(1))
{
switch(c)
{ case KEY_DOWN:
menu_driver(my_menu, REQ_DOWN_ITEM);
break;
case KEY_UP:
menu_driver(my_menu, REQ_UP_ITEM);
break;
case 10: /* Enter */
move(20, 0);
clrtoeol();
mvprintw(20, 0, "Item selected is : %s",
item_name(current_item(my_menu)));
pos_menu_cursor(my_menu);
break;
}
}
unpost_menu(my_menu);
for(i = 0; i < n_choices; ++i)
free_item(my_items[i]);
free_menu(my_menu);
endwin();
}

17.9

<ENTER>
78 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

24
#include <curses.h>
#include <menu.h>
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
#define CTRLD
4
char *choices[] = {
"Choice 1",
"Choice 2",
"Choice 3",
"Choice 4",
"Choice 5",
"Choice 6",
"Choice 7",
"Exit",
};
void func(char *name);
int main()
{ ITEM **my_items;
int c;
MENU *my_menu;
int n_choices, i;
ITEM *cur_item;
/* curses */
initscr();
start_color();
cbreak();
noecho();
keypad(stdscr, TRUE);
init_pair(1, COLOR_RED, COLOR_BLACK);
init_pair(2, COLOR_GREEN, COLOR_BLACK);
init_pair(3, COLOR_MAGENTA, COLOR_BLACK);
/* */
n_choices = ARRAY_SIZE(choices);
my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *));
for(i = 0; i < n_choices; ++i)
{
my_items[i] = new_item(choices[i], choices[i]);
/* */
set_item_userptr(my_items[i], func);
}
79 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

my_items[n_choices] = (ITEM *)NULL;


/* */
my_menu = new_menu((ITEM **)my_items);
/* */
mvprintw(LINES - 3, 0, "Press <ENTER> to see the option selected");
mvprintw(LINES - 2, 0, "Up and Down arrow keys to naviage (F1 to Exit)");
post_menu(my_menu);
refresh();
while((c = getch()) != KEY_F(1))
{
switch(c)
{ case KEY_DOWN:
menu_driver(my_menu, REQ_DOWN_ITEM);
break;
case KEY_UP:
menu_driver(my_menu, REQ_UP_ITEM);
break;
case 10: /* Enter */
{

ITEM *cur;
void (*p)(char *);
cur = current_item(my_menu);
p = item_userptr(cur);
p((char *)item_name(cur));
pos_menu_cursor(my_menu);
break;

}
break;
}
}
unpost_menu(my_menu);
for(i = 0; i < n_choices; ++i)
free_item(my_items[i]);
free_menu(my_menu);
endwin();
}
void func(char *name)
{ move(20, 0);
clrtoeol();
mvprintw(20, 0, "Item selected is : %s", name);
}
80 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

ncurses Form Library

18.1.

new_field()
,

menu_driver() form_driver() form_driver()

curses
new_field()
new_form()
form_post()
form_driver()
form_unpost()
free_form()

curses

18.2
form.h
lform lncurses
#include <form.h>
.
.
.

81 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

: gcc <> -lform lncurses


25
#include <form.h>
int main()
{ FIELD *field[3];
FORM *my_form;
int ch;
/* curses */
initscr();
cbreak();
noecho();
keypad(stdscr, TRUE);
/* */
field[0] = new_field(1, 10, 4, 18, 0, 0);
field[1] = new_field(1, 10, 6, 18, 0, 0);
field[2] = NULL;
/* */
set_field_back(field[0], A_UNDERLINE);

/* */

field_opts_off(field[0], O_AUTOSKIP);
/* */
set_field_back(field[1], A_UNDERLINE);
field_opts_off(field[1], O_AUTOSKIP);
/* */
my_form = new_form(field);
post_form(my_form);
refresh();
mvprintw(4, 10, "Value 1:");
mvprintw(6, 10, "Value 2:");
refresh();
/* */
while((ch = getch()) != KEY_F(1))
{ switch(ch)
{ case KEY_DOWN:
/* */
form_driver(my_form, REQ_NEXT_FIELD);
/* */
82 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

/* */
form_driver(my_form, REQ_END_LINE);
break;
case KEY_UP:
/* */
form_driver(my_form, REQ_PREV_FIELD);
form_driver(my_form, REQ_END_LINE);
break;
default:
/* , */
form_driver(my_form, ch);
break;
}
}
/* */
unpost_form(my_form);
free_form(my_form);
free_field(field[0]);
free_field(field[1]);
endwin();
return 0;
}
new_field()new_field()

new_field()
set_field_back() AUTOSKIP
field_opts_off()

form_driver()
while form_driver()

18.3

18.3.1
83 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

field_info()
new_field()
int field_info( FIELD *field,
/* */
int *height, *int width,
/* */
int *top, int *left,
/* y x */
int *offscreen,
/* */
int *nbuf);
/* */
18.3.2
move_field()
int move_field( FIELD *field,
/* */
int top, int left);
/* */
field_info()
18.3.3
set_field_just()
int set_field_just(FIELD *field,
/* */
int justmode);
/* */
int field_just(FIELD *field);

/* */

NO_JUSTIFICATIONJUSTIFY_RIGHTJUSTIFY_LEFT
JUSTIFY_CENTER
18.3.4
set_field_fore()
set_field_back()
set_field_pad()
field_fore()field_back()field_pad()

int set_field_fore(FIELD *field,


/* */
chtype attr);
/* */
chtype field_fore(FIELD *field);

/* */
/* */

int set_field_back(FIELD *field,


chtype attr);

/* */
/* */

chtype field_back(FIELD *field);

/* */
/* */

int set_field_pad(FIELD *field,

/* */
84 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

int pad);
chtype field_pad(FIELD *field);

/* */
/* */
/* */

set_field_fore()

set_field_for()

26
#include <form.h>
int main()
{ FIELD *field[3];
FORM *my_form;
int ch;
/* curses */
initscr();
start_color();
cbreak();
noecho();
keypad(stdscr, TRUE);
/* */
init_pair(1, COLOR_WHITE, COLOR_BLUE);
init_pair(2, COLOR_WHITE, COLOR_BLUE);
/* */
field[0] = new_field(1, 10, 4, 18, 0, 0);
field[1] = new_field(1, 10, 6, 18, 0, 0);
field[2] = NULL;
/* */
set_field_fore(field[0], COLOR_PAIR(1)); /* */
set_field_back(field[0], COLOR_PAIR(2)); /*
*/
field_opts_off(field[0], O_AUTOSKIP);
/* */
set_field_back(field[1], A_UNDERLINE);
field_opts_off(field[1], O_AUTOSKIP);
/* */
my_form = new_form(field);
85 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

post_form(my_form);
refresh();
set_current_field(my_form, field[0]); /* */
mvprintw(4, 10, "Value 1:");
mvprintw(6, 10, "Value 2:");
mvprintw(LINES - 2, 0, "Use UP, DOWN arrow keys to switch between fields");
refresh();
/* */
while((ch = getch()) != KEY_F(1))
{ switch(ch)
{ case KEY_DOWN:
/* */
form_driver(my_form, REQ_NEXT_FIELD);
/* */
/* */
form_driver(my_form, REQ_END_LINE);
break;
case KEY_UP:
/* */
form_driver(my_form, REQ_PREV_FIELD);
form_driver(my_form, REQ_END_LINE);
break;
default:
/* */
form_driver(my_form, ch);
break;
}
}
/* */
unpost_form(my_form);
free_form(my_form);
free_field(field[0]);
free_field(field[1]);
endwin();
return 0;
}

86 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

set_field_back()cureses
18.3.5

int set_field_opts(FIELD *field,


/* */
int attr);
/* */
int field_opts_on(FIELD *field,
int attr);

/* */
/* */

int field_opts_off(FIELD *field,


int attr);

/* */
/* */

int field_opts(FIELD *field);

/* */

set_field_opts()
field_opts_on() field_opts_off() field_opts()

O_VISIBLE

O_ACTIVE
Tab

O_PUBLIC

O_PUBLIC
O_EDIT
REQ_PREV_CHOICE
REQ_NEXT_CHOICE

O_WRAP

O_BLANK

87 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

()
O_AUTOSKIP

O_NULLOK

O_PASSOK

O_PASSOK
O_STATIC

O_AUTOSKIP

27
#include <form.h>
#define STARTX 15
#define STARTY 4
#define WIDTH 25
#define N_FIELDS 3
int main()
{ FIELD *field[N_FIELDS];
FORM *my_form;
int ch, i;
/* curses */
initscr();
cbreak();
88 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

noecho();
keypad(stdscr, TRUE);
/* */
for(i = 0; i < N_FIELDS - 1; ++i)
field[i] = new_field(1, WIDTH, STARTY + i * 2, STARTX, 0, 0);
field[N_FIELDS - 1] = NULL;
/* */
set_field_back(field[1], A_UNDERLINE);
field_opts_off(field[0], O_ACTIVE);
field_opts_off(field[1], O_PUBLIC);

/* */
/* */
/* */

field_opts_off(field[1], O_AUTOSKIP);
/* */
/* */
my_form = new_form(field);
post_form(my_form);
refresh();
set_field_just(field[0], JUSTIFY_CENTER); /* */
set_field_buffer(field[0], 0, "This is a static Field "); /* */
mvprintw(STARTY, STARTX - 10, "Field 1:");
mvprintw(STARTY + 2, STARTX - 10, "Field 2:");
refresh();
/* */
while((ch = getch()) != KEY_F(1))
{ switch(ch)
{ case KEY_DOWN:
/* */
form_driver(my_form, REQ_NEXT_FIELD);
/* */
/* */
form_driver(my_form, REQ_END_LINE);
break;
case KEY_UP:
/* */
form_driver(my_form, REQ_PREV_FIELD);
form_driver(my_form, REQ_END_LINE);
break;
default:
/* */
form_driver(my_form, ch);
89 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

break;
}
}
/* */
unpost_form(my_form);
free_form(my_form);
free_field(field[0]);
free_field(field[1]);
endwin();
return 0;
}

O_PUBLIC

18.3.6
FALSE
TRUE

int set_field_status(FIELD *field,


/* */
int status);
/* */
int field_status(FIELD *field);

/* */

field_status()12
3Form Driver REQ_VALIDATION
18.3.7

int set_field_userptr(FIELD *field,


char *userptr);
char *field_userptr(FIELD *field);

/* */
/* */

18.3.8

90 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

O_STATIC

field_opts_off(field_pointer, O_STATIC);

int set_max_field(FIELD *field,


/* */
int max_growth);
/* */

int dynamic_field_info( FIELD *field, /* */


int *prows,
/* */
int *pcols,
/* */
int *pmax)
/* */

new_field()

O_STATIC

O_STATIC

1 O_STATIC
O_AUTOSKIP
O_AUTOSKIP Form Driver REQ_NEXT_FIELD

O_AUTOSKIP
2 O_STATIC set_field_just
JUSTIFY_LEFTJUSTIFY_RIGHTJUSTIFY_CENTER

set_field_just
field_jus
3 O_STATIC Form Driver
REQ_NEW_LINE O_NL_OVERLOAD
O_NL_OVERLOAD
REQ_NEW_LINE REQ_NEXT_FIELD
91 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

REQ_NEW_LINE REQ_NEXT_FIELD
O_NL_OVERLOAD
REQ_NEW_LINE
REQ_NEXT_FIELD
4 dup_field()

5 link_field()
O_STATIC

6 field_info() nrow new_field


dynamic_field_info
Form Driver
Form Driver

18.4

28
#include <form.h>
void print_in_middle(WINDOW *win, int starty, int startx, int width, char
*string, chtype color);
int main()
{
FIELD *field[3];
FORM *my_form;
WINDOW *my_form_win;
int ch, rows, cols;
/* curses */
initscr();
start_color();
cbreak();
92 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

noecho();
keypad(stdscr, TRUE);
/* */
init_pair(1, COLOR_RED, COLOR_BLACK);
/* */
field[0] = new_field(1, 10, 6, 1, 0, 0);
field[1] = new_field(1, 10, 8, 1, 0, 0);
field[2] = NULL;
/* */
set_field_back(field[0], A_UNDERLINE);
field_opts_off(field[0], O_AUTOSKIP);
/* */
set_field_back(field[1], A_UNDERLINE);
field_opts_off(field[1], O_AUTOSKIP);
/* */
my_form = new_form(field);
/* */
scale_form(my_form, &rows, &cols);
/* */
my_form_win = newwin(rows + 4, cols + 4, 4, 4);
keypad(my_form_win, TRUE);
/* */
set_form_win(my_form, my_form_win);
set_form_sub(my_form, derwin(my_form_win, rows, cols, 2, 2));
/* */
box(my_form_win, 0, 0);
print_in_middle(my_form_win, 1, 0, cols + 4, "My Form", COLOR_PAIR(1));
post_form(my_form);
wrefresh(my_form_win);
mvprintw(LINES - 2, 0, "Use UP, DOWN arrow keys to switch between fields");
refresh();
/* */
while((ch = wgetch(my_form_win)) != KEY_F(1))
{ switch(ch)
93 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

case KEY_DOWN:
/* */
form_driver(my_form, REQ_NEXT_FIELD);
/* */
/* */
form_driver(my_form, REQ_END_LINE);
break;
case KEY_UP:
/* */
form_driver(my_form, REQ_PREV_FIELD);
form_driver(my_form, REQ_END_LINE);
break;
default:
/* */
form_driver(my_form, ch);
break;

}
}
/* */
unpost_form(my_form);
free_form(my_form);
free_field(field[0]);
free_field(field[1]);
endwin();
return 0;
}
void print_in_middle(WINDOW *win, int starty, int startx, int width, char
*string, chtype color)
{ int length, x, y;
float temp;
if(win == NULL)
win = stdscr;
getyx(win, y, x);
if(startx != 0)
x = startx;
if(starty != 0)
y = starty;
if(width == 0)
width = 80;
length = strlen(string);
temp = (width - length)/ 2;
x = startx + (int)temp;
wattron(win, color);
94 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

mvwprintw(win, y, x, "%s", string);


wattroff(win, color);
refresh();
}

18.5

int set_field_type(FIELD *field,


/* */
FIELDTYPE *ftype,
/* */
...);
/* */

FIELDTYPE *field_type(FIELD *field);


/* */
Form Driver

set_field_buffer

TYPE_ALPHA

int set_field_type(FIELD *field,


/* */
TYPE_ ALPHA
/* */
int width);
/* */
width

TYPE_ALNUM

int set_field_type(FIELD *field,


/* */
TYPE_ AKLUNM
/* */
int width);
/* */
95 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

width TYPE_ALPHA

0
TYPE_ENUM

int set_field_type(FIELD *field,


/* */
TYPE_ENUM,
/* */
char **valuelist;
/* */
int checkcase;
/* */
int checkunique);
/* */
valuelist NULL checkcase

TYPE_ENUM

checkunique

REQ_NEXT_CHOICE REQ_PREV_CHOICE
TYPE_INTEGER

int set_field_type(FIELD *field,


/* */
TYPE_INTEGER,
/* */
int padding,
/* 0 */
int vmin, int vmax);
/* */

0
C atoi3 TYPE_INTEGER
TYPE_NUMERIC

int set_field_type(FIELD *field,


/* */
96 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

TYPE_NUMERIC,
int padding,
int vmin, int vmax);

/* */
/* */
/* */

0
C atoi3 TYPE_NUMERIC
TYPE_REGEXP

int set_field_type(FIELD *field,


/* */
TYPE_REGEXP,
/* */
char *regexp);
/* */
regcomp(3)

18.6Form Driver
form_driver()
form_driver()
int form_driver(FORM *form,
/* */
int request)
/* */

form_driver()

18.6.1

set_new_page()
int set_new_page(FIELD *field,
/* */
bool new_page_flag);
/* TRUE */

REQ_NEXT_PAGE

REQ_PREV_PAGE

REQ_FIRST_PAGE

REQ_LAST_PAGE

97 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

REQ_NEXT_PAGE
REQ_PREV_PAGE

18.6.2

REQ_NEXT_FIELD

REQ_PREV_FIELD

REQ_FIRST_FIELD

REQ_LAST_FIELD

REQ_SNEXT_FIELD
REQ_SPREV_FIELD
REQ_SFIRST_FIELD
REQ_SLAST_FIELD

REQ_LEFT_FIELD
REQ_RIGHT_FIELD
REQ_UP_FIELD
REQ_DOWN_FIELD

REQ_NEXT_FIELD
REQ_PREV_FIELD
( REQ_FIRST_FIELD REQ_LAST_FIELD)
new_form() set_form_fields()

REQ_S

B B A C A B
C B REQ_MOVE_RIGHT A,B,C
A B B C
18.6.3
.
REQ_NEXT_CHAR

REQ_PREV_CHAR

REQ_NEXT_LINE

REQ_PREV_LINE

REQ_BEG_FIELD

REQ_END_FIELD

98 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

REQ_BEG_LINE
REQ_END_LINE
REQ_LEFT_CHAR
REQ_RIGHT_CHAR
REQ_UP_CHAR
REQ_DOWN_CHAR

18.6.4

REQ_SCR_FLINE

REQ_SCR_BLINE

REQ_SCR_FPAGE

REQ_SCR_BPAGE

REQ_SCR_FHPAGE

REQ_SCR_BHPAGE

REQ_SCR_FCHAR

REQ_SCR_BCHAR

REQ_SCR_HFLINE

REQ_SCR_HBLINE

REQ_SCR_HFHALF

REQ_SCR_HBHALF

18.6.5
ASCII Form Driver

REQ_INS_MODE

REQ_OVL_MODE

REQ_NEW_LINE

REQ_INS_CHAR

REQ_INS_LINE

REQ_DEL_CHAR

REQ_DEL_PREV

REQ_DEL_LINE

REQ_DEL_WORD

REQ_CLR_EOL

99 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

REQ_CLR_EOF
REQ_CLEAR_FIELD

REQ_NEW_LINE REQ_DEL_PREV

REQ_NEW_LINE
REQ_NEW_LINE

REQ_NEW_LINE

REQ_NEW_LINE
REQ_NEXT_FIELD O_NL_OVERLOAD

REQ_DEL_PREV
REQ_DEL_PREV

REQ_DEL_PREV REQ_PREV_FIELD
O_BS_OVERLOAD Form Driver
E_REQUEST_DENIED
18.6.6

REQ_NEXT_CHOICE
REQ_PREV_CHOICE

TYPE_ENUM

18.6.7
KEY_MAX MAX_COMMAND
form_driver()

100 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

CURSES
NCURSES

19.1. CDK (Curses Development Kit)

CDK CURSES Curses Development Kit 21


CURESES

CURSES
CDK http://www.vexus.ca/products/CDK/cdk.tar.gz
Poet http://poet.cosoft.org.cn/downloads/develop/cdk.tar.gz
tar
README

19.1.1.
CDK

Alphalist

Buttonbox

Calendar

Dialog

Entry

File Selector

CDK
CDK

Graph

Histogram

Item List

Label

Marquee

Matrix

Menu

Multiple Line Entry

101 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

Radio List

Scale

()

Scrolling List

Scrolling Window

Selection List

Slider

Scale

Template

Viewer

19.1.2.
CDK CURSES CDK CURSES
CDK

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


newCDKLabel()
man
cdk_display(3X)

19.1.3.
CDK

19.2.
1994 9 Linux Jeff Tranter Linux Journal

102 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

Linux Unix
Unix dialog
shell
dialog
dialog

ibiblio linux archive.


dialog man dialog
Linux dialog
Linux dialog
dialog shell C
lobdialog libdialog dialog.h

19.3. Perl CURSES


Perl CURSES Curses::Form Curses::Widgets Perl Curses
Curses Perl CPAN Curses
Perl
perlmod man
widgets Perl
curses
man Curses(3)Curses::Form(3) Curses::Widgets(3)

103 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

JustForFun
ncurses

curses

20.1. (The Game of Life)


Paul Callahan
()

google
game of life
JustForFun/life.c

20.2. (Magic Square)

JustForFun/magic.c

20.3. (Towers of Hanoi)

20.4. Queens Puzzle


8

JustForFun/queens.c

20.5. Shuffle

104 105

PDF "pdfFactory Pro" www.fineprint.com.cn

NCURSES Programming HOWTO

JustForFun/shuffle.c

20.6 Typing Tutor

JustForFun/tt.c

105 105

PDF "pdfFactory Pro" www.fineprint.com.cn

You might also like