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