Professional Documents
Culture Documents
1 105
Pradeep Padala
The Linux Documentation Projecthttp://www.tldp.org/
2003 3 23
Byron
E-mailbyronm@sina.com
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
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
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.
4 105
NCURSES
1
escape sequence
0x1B ESC
echo "^[[0;37;40m"
UNIX
termcap termcap
terminfo
termcap termcap
terminfo
2NCURSES
terminfo
terminfo
CURSES
CURSES cursor optimization
CURSES
API
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
RPM
http://rpmfind.net NCURSES RPM root
5.2
rpm vih RPM
http://www.linuxdoc.org/HOWTO/NCURSES-Programming-HOWTO/ncurses_programs.tar.gz
ncurses
|
|----> JustForFun
|----> basics
|----> demo
|
|
|
|----> exe
|----> forms
|----> menus
|----> panels
|----> Makefile
|----> README
|----> COPYING
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
|----> 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
http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/ncurses_programs/
GPLhttp://www.gnu.org/copyleft/gpl.txt
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
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
Hello World
NCURSES NCURSES
NCURSES
2.1 NCURSES
NCURSES ncurses.h C C++
#include <ncurses.h> NCURSES
NCURSES stdio.h
#include <ncurses.h>
*/
*/
*/
*/
return 0;
}
2.2
Hello World !!!
curses curses
2.2.1 initscr()
initscr() curses
NCURSES initscr()
curses
stdscr
initscr()
curses
10 105
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
CURSES CURSES
CURSES
12 105
initscr() CURSES
CURSES
terminal modecolor mode
mouse mode
line buffering
CTRL-ZCTRL-C raw()
cbreak()
raw()
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
4.5
CURSES
CURSES
4.6
2.
#include <ncurses.h>
int main()
{
int ch;
initscr();
raw();
keypad(stdscr, TRUE);
noecho();
/*
/*
/*
/*
curses
*/
*/
*/
getch() */
14 105
CURSES windows
CURSES Windows
CURSES stdscr
80 25
printw(Hi! There!);
refresh();
stdscr Hi! There! refresh()
stdscr
win
w
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
printw()
CURSES
CURSES curses
CURSES
addch()
printw() printf()
addstr()
6.1 addch()
addch()
CURSES
n ncurses.h
ch
BOLDUNDERLINE
attrset()attron()attroff()
attrset()
attroff()
CURSES
ncurses.h
ACS_
mvaddch(row,col,ch);
waddch() addch()waddch()
addch() stdscr
16 105
mvwaddch()
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
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
scanw()
getch()
scanw()
getstr()
7.1. getch()
cbreak()CURSES
cbreak() noecho()
cbeak() noecho()
7.2. scanw()
scanf()
7.2.3. vwscanw()vwscanw()
vprintf()
7.3. getstr()
getch()
19 105
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 */
20 105
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
/* 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
A_ALTCHARSET
A_CHARTEXT
COLOR_PAIR(n)
lst01.c A_NORMALA_BLINK
A_DIMA_INVISA_BOLDA_STANDOUT A_REVERSE A_STANDOUT
A_REVERSE
|
attron(A_REVERSE | A_BLINK);
standend()
attrset(A_NORMAL)
8.3 attr_get()
attr_get()
attrset() attron()
8.4 attr_
attr_attr_set()attr_on()
8.6 chgat()
23 105
-1
/* curses */
return 0;
}
24 105
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
/* */
/* */
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
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
wborader()
wborder(win, '|', '|', '-', '-', '+', '+', '+', '+');
+-----------------------+
|
|
|
|
|
|
|
|
|
|
|
|
+-----------------------+
27 105
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
/* */
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
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
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
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
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
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();
/* */
++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
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
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
123
11
#include <ncurses.h>
#define WIDTH 30
#define HEIGHT 10
int startx = 0;
int starty = 0;
char *choices[] = {
"Choice 1",
37 105
"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
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;
}
124
mouse_trafo() wmouse_trafo()
curs_mouse(3X) man
mouseinterval()
40 105
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
int
int
int
int
int
dminrow,
dmincol,
dmaxrow,
dmaxcol,
over)
/**/
/**/
/**/
/**/
/**/
42 105
CURSES
141 curs_set()curs_set()
012
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
CURSES */
Hello World!!! */
*/
tty */
CURSES */
*/
def_prog_mode() tty */
refresh() */
CURSES */
*/
CURSES */
return 0;
}
43 105
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);
44 105
curses CURSE
panel
menuform
45 105
curses
Panels
panels wnoutrefresh()
doupdate()
16.1
refresh
1 newwin
2
new_panel()
3 update_panels() doupdate()
16.2
panel.h
46 105
-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
}
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
*/
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
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
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
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
}
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
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
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
r
<ENTER>
m
<ENTER> panel()
PANEL_DATA
PANEL_DATA
16.6
hide_panel()
update_panels() doupdate()
show_panel()
a b c
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
59 105
/* */
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
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();
}
61 105
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
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
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_LEFT_ITEM REQ_RIGHT_ITEM
menu_format()
menu drive
64 105
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
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
/* */
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
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
"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
/* " * " */
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
}
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
"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
/* */
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
}
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
"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
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()
17.8
set_menu_grey()
76 105
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
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
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
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
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
/* */
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
/* */
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
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()
/* */
/* */
/* */
/* */
/* */
/* */
/* */
84 105
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
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
set_field_back()cureses
18.3.5
/* */
/* */
/* */
/* */
/* */
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
()
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
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
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
/* */
field_status()12
3Form Driver REQ_VALIDATION
18.3.7
/* */
/* */
18.3.8
90 105
O_STATIC
field_opts_off(field_pointer, O_STATIC);
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
REQ_NEW_LINE REQ_NEXT_FIELD
O_NL_OVERLOAD
REQ_NEW_LINE
REQ_NEXT_FIELD
4 dup_field()
5 link_field()
O_STATIC
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
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
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
18.5
set_field_buffer
TYPE_ALPHA
TYPE_ALNUM
width TYPE_ALPHA
0
TYPE_ENUM
TYPE_ENUM
checkunique
REQ_NEXT_CHOICE REQ_PREV_CHOICE
TYPE_INTEGER
0
C atoi3 TYPE_INTEGER
TYPE_NUMERIC
TYPE_NUMERIC,
int padding,
int vmin, int vmax);
/* */
/* */
/* */
0
C atoi3 TYPE_NUMERIC
TYPE_REGEXP
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
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
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
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
CURSES
NCURSES
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
101 105
Radio List
Scale
()
Scrolling List
Scrolling Window
Selection List
Slider
Scale
Template
Viewer
19.1.2.
CDK CURSES CDK CURSES
CDK
19.1.3.
CDK
19.2.
1994 9 Linux Jeff Tranter Linux Journal
102 105
Linux Unix
Unix dialog
shell
dialog
dialog
103 105
JustForFun
ncurses
curses
google
game of life
JustForFun/life.c
JustForFun/magic.c
JustForFun/queens.c
20.5. Shuffle
104 105
JustForFun/shuffle.c
JustForFun/tt.c
105 105