xref: /original-bsd/lib/libcurses/PSD.doc/ex2.c (revision c3e32dec)
1 .\" Copyright (c) 1992, 1993
2 .\"	 The Regents of the University of California.  All rights reserved.
3 .\"
4 .\" %sccs.include.redist.roff%
5 .\"
6 .\"     @(#)ex2.c	8.1 (Berkeley) 06/08/93
7 .\"
8 #include <curses.h>
9 #include <stdio.h>
10 #include <signal.h>
11 
12 #define YSIZE LINES
13 #define XSIZE COLS
14 
15 static int quit();
16 
17 /*
18  * This program fills the screen up with characters and the allows the user to
19  * manipulate the text on the screen using some basic commands.
20  * Nothing fancy, just a demonstration of the elementary features of the
21  * curses(3) package.
22  */
23 main()
24 {
25 	int i, j, c, n, d = 0;
26 	char id[100];
27 	int hh = 0;
28 	int curx, cury, base, arg;
29 
30 	initscr();
31 	signal(SIGINT, quit);
32 	crmode();
33 	noecho();
34 	nonl();
35 	delwin(stdscr);
36 	stdscr = newwin(YSIZE, XSIZE, 0, 0);
37 	flushok(stdscr, TRUE);
38 	scrollok(stdscr, TRUE);
39 	erase();
40 	refresh();
41 
42 	move(0,0);
43 	refresh();
44 	for (i = 0; i < YSIZE + 2; i++) {
45 		sprintf(id, "%d: ", i);
46 		addstr(id);
47 		for (j = 0; j < XSIZE - strlen(id); j++)
48 			addch('0' + (i % 10));
49 	}
50 	c = getchar();
51 	base = 2;
52 	curx = cury = 0;
53 	move(0, 0);
54 	refresh();
55 
56 	/*
57 	 * The screen manipulator has the following commands:
58 	 * 'D' - clear to the end of the current line.
59 	 * 'B' - clear to the bottom of the screen.
60 	 * 'E' - erase the screen.
61 	 * 's' - enter standout mode.
62 	 * 'e' - exit standout mode.
63 	 * 'd' n - delete n lines below cursor line.
64 	 * 'i' n - insert n lines below cursor line.
65 	 * 'q' - quit.
66 	 * 'f' - move cursor one position to the right.
67 	 * 'b' - move cursor one position to the left.
68 	 * 'n' - move cursor one line down.
69 	 * 'p' - move cursor one line up.
70 	 * 'h' - home cusor.
71 	 * 'l' - force refresh.
72 	 * 'r' - simulate a carriage return.
73 	 *
74 	 * All other characters are ignored.
75 	 */
76 	for(;;) {
77 		switch(c = getchar()) {
78 		case 'D':
79 			clrtoeol();
80 			refresh();
81 			continue;
82 		case 'B':
83 			clrtobot();
84 			refresh();
85 			continue;
86 		case 'E':
87 			erase();
88 			refresh();
89 			continue;
90 		case 's':
91 			standout();
92 			continue;
93 		case 'e':
94 			standend();
95 			continue;
96 		case 'd':
97 			arg = getchar() - '0';
98 			for (i = 0; i < arg; i++)
99 				deleteln();
100 			refresh();
101 			continue;
102 		case 'i':
103 			arg = getchar() - '0';
104 			for (i = 0; i < arg; i++)
105 				insertln();
106 			refresh();
107 			continue;
108 		case 'q':
109 			quit();
110 		case 'f':
111 			if (curx < XSIZE - 1)
112 				curx++;
113 			else {
114 				cury++;
115 				curx = 0;
116 			}
117 			break;
118 		case 'b':
119 			if (curx == 0) {
120 				cury--;
121 				curx = XSIZE - 1;
122 			} else
123 				curx--;
124 			break;
125 		case 'n':
126 			cury++;
127 			break;
128 		case 'p':
129 			cury--;
130 			break;
131 		case 'h':
132 			curx = cury = 0;
133 			break;
134 		case 'l':
135 			wrefresh(curscr);
136 			continue;
137 		case 'r':   /* return */
138 		{
139 			int x, y;
140 			getyx(stdscr, y, x);
141 			move(y+1, 0);
142 			insertln();
143 			move(y, x);
144 			clrtoeol();
145 			refresh();
146 			continue;
147 		}
148 		default:
149 			continue;
150 		}
151 
152 		if (cury < 0) {
153 			base--;
154 			move(0, 0);
155 			insertln();
156 			sprintf(id, "%d: ", base);
157 			addstr(id);
158 			for (j = 0; j < XSIZE - strlen(id) - 2; j++)
159 				addch('0' + (base % 10));
160 			cury++;
161 		} else if (cury >= YSIZE) {
162 			move(0, 0);
163 			deleteln();
164 			move(YSIZE - 1, 0);
165 			sprintf(id, "%d: ", base + YSIZE);
166 			addstr(id);
167 			for (j = 0; j < XSIZE - strlen(id) - 2; j++)
168 				addch('0' + ((base + YSIZE) % 10));
169 			cury--;
170 			base++;
171 		}
172 		move(cury, curx);
173 		refresh();
174 	}
175 }
176 
177 int
178 quit()
179 {
180 	erase();
181 	refresh();
182 	endwin();
183 	exit(0);
184 }
185