xref: /netbsd-src/lib/libcurses/PSD.doc/ex2.c (revision ce0bb6e8d2e560ecacbe865a848624f94498063b)
1 .\" Copyright (c) 1992, 1993
2 .\"	 The Regents of the University of California.  All rights reserved.
3 .\"
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
6 .\" are met:
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\"    notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\"    notice, this list of conditions and the following disclaimer in the
11 .\"    documentation and/or other materials provided with the distribution.
12 .\" 3. All advertising materials mentioning features or use of this software
13 .\"    must display the following acknowledgement:
14 .\"	This product includes software developed by the University of
15 .\"	California, Berkeley and its contributors.
16 .\" 4. Neither the name of the University nor the names of its contributors
17 .\"    may be used to endorse or promote products derived from this software
18 .\"    without specific prior written permission.
19 .\"
20 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 .\" SUCH DAMAGE.
31 .\"
32 .\"     @(#)ex2.c	8.1 (Berkeley) 6/8/93
33 .\"
34 #include <curses.h>
35 #include <stdio.h>
36 #include <signal.h>
37 
38 #define YSIZE LINES
39 #define XSIZE COLS
40 
41 static int quit();
42 
43 /*
44  * This program fills the screen up with characters and the allows the user to
45  * manipulate the text on the screen using some basic commands.
46  * Nothing fancy, just a demonstration of the elementary features of the
47  * curses(3) package.
48  */
49 main()
50 {
51 	int i, j, c, n, d = 0;
52 	char id[100];
53 	int hh = 0;
54 	int curx, cury, base, arg;
55 
56 	initscr();
57 	signal(SIGINT, quit);
58 	crmode();
59 	noecho();
60 	nonl();
61 	delwin(stdscr);
62 	stdscr = newwin(YSIZE, XSIZE, 0, 0);
63 	flushok(stdscr, TRUE);
64 	scrollok(stdscr, TRUE);
65 	erase();
66 	refresh();
67 
68 	move(0,0);
69 	refresh();
70 	for (i = 0; i < YSIZE + 2; i++) {
71 		sprintf(id, "%d: ", i);
72 		addstr(id);
73 		for (j = 0; j < XSIZE - strlen(id); j++)
74 			addch('0' + (i % 10));
75 	}
76 	c = getchar();
77 	base = 2;
78 	curx = cury = 0;
79 	move(0, 0);
80 	refresh();
81 
82 	/*
83 	 * The screen manipulator has the following commands:
84 	 * 'D' - clear to the end of the current line.
85 	 * 'B' - clear to the bottom of the screen.
86 	 * 'E' - erase the screen.
87 	 * 's' - enter standout mode.
88 	 * 'e' - exit standout mode.
89 	 * 'd' n - delete n lines below cursor line.
90 	 * 'i' n - insert n lines below cursor line.
91 	 * 'q' - quit.
92 	 * 'f' - move cursor one position to the right.
93 	 * 'b' - move cursor one position to the left.
94 	 * 'n' - move cursor one line down.
95 	 * 'p' - move cursor one line up.
96 	 * 'h' - home cusor.
97 	 * 'l' - force refresh.
98 	 * 'r' - simulate a carriage return.
99 	 *
100 	 * All other characters are ignored.
101 	 */
102 	for(;;) {
103 		switch(c = getchar()) {
104 		case 'D':
105 			clrtoeol();
106 			refresh();
107 			continue;
108 		case 'B':
109 			clrtobot();
110 			refresh();
111 			continue;
112 		case 'E':
113 			erase();
114 			refresh();
115 			continue;
116 		case 's':
117 			standout();
118 			continue;
119 		case 'e':
120 			standend();
121 			continue;
122 		case 'd':
123 			arg = getchar() - '0';
124 			for (i = 0; i < arg; i++)
125 				deleteln();
126 			refresh();
127 			continue;
128 		case 'i':
129 			arg = getchar() - '0';
130 			for (i = 0; i < arg; i++)
131 				insertln();
132 			refresh();
133 			continue;
134 		case 'q':
135 			quit();
136 		case 'f':
137 			if (curx < XSIZE - 1)
138 				curx++;
139 			else {
140 				cury++;
141 				curx = 0;
142 			}
143 			break;
144 		case 'b':
145 			if (curx == 0) {
146 				cury--;
147 				curx = XSIZE - 1;
148 			} else
149 				curx--;
150 			break;
151 		case 'n':
152 			cury++;
153 			break;
154 		case 'p':
155 			cury--;
156 			break;
157 		case 'h':
158 			curx = cury = 0;
159 			break;
160 		case 'l':
161 			wrefresh(curscr);
162 			continue;
163 		case 'r':   /* return */
164 		{
165 			int x, y;
166 			getyx(stdscr, y, x);
167 			move(y+1, 0);
168 			insertln();
169 			move(y, x);
170 			clrtoeol();
171 			refresh();
172 			continue;
173 		}
174 		default:
175 			continue;
176 		}
177 
178 		if (cury < 0) {
179 			base--;
180 			move(0, 0);
181 			insertln();
182 			sprintf(id, "%d: ", base);
183 			addstr(id);
184 			for (j = 0; j < XSIZE - strlen(id) - 2; j++)
185 				addch('0' + (base % 10));
186 			cury++;
187 		} else if (cury >= YSIZE) {
188 			move(0, 0);
189 			deleteln();
190 			move(YSIZE - 1, 0);
191 			sprintf(id, "%d: ", base + YSIZE);
192 			addstr(id);
193 			for (j = 0; j < XSIZE - strlen(id) - 2; j++)
194 				addch('0' + ((base + YSIZE) % 10));
195 			cury--;
196 			base++;
197 		}
198 		move(cury, curx);
199 		refresh();
200 	}
201 }
202 
203 int
204 quit()
205 {
206 	erase();
207 	refresh();
208 	endwin();
209 	exit(0);
210 }
211