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