2 * newdemo.c - A demo program using PDCurses. The program illustrate
3 * the use of colours for text output.
5 * $Id: newdemo.c,v 1.47 2019/12/14 23:25:29 tom Exp $
15 static CONST_MENUS char *AusMap[16] =
19 " AAAAAAAAAAA AAAAAAAA ",
20 " AAAAAAAAAAAAAAAAAAAAAAAAA Qld.",
21 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ",
22 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA ",
23 " AAAAAAAAAAAAAAAAAAAAAAAAAAAA ",
24 " AAAAAAAAAAAAAAAAAAAAAAAAA N.S.W.",
25 "W.A. AAAAAAAAA AAAAAA Vic.",
36 static const char *messages[] =
38 "Hello from the Land Down Under",
39 "The Land of crocs. and a big Red Rock",
40 "Where the sunflower runs along the highways",
41 "the dusty red roads lead one to loneliness",
42 "Blue sky in the morning and",
43 "freezing nights and twinkling stars",
51 trap(int sig GCC_UNUSED)
54 ExitProgram(EXIT_FAILURE);
61 WaitForUser(WINDOW *win)
66 t = time((time_t *) 0);
70 if ((int) (key = (chtype) wgetch(win)) != ERR) {
71 if (key == 'q' || key == 'Q')
76 if (time((time_t *) 0) - t > 5)
82 set_colors(WINDOW *win, int pair, int foreground, int background)
85 if (pair > COLOR_PAIRS)
87 init_pair((short) pair, (short) foreground, (short) background);
88 (void) wattrset(win, AttrArg(COLOR_PAIR(pair), 0));
93 use_colors(WINDOW *win, int pair, chtype attrs)
96 if (pair > COLOR_PAIRS)
98 attrs |= (chtype) COLOR_PAIR(pair);
100 (void) wattrset(win, AttrArg(attrs, 0));
108 SubWinTest(WINDOW *win)
110 int w, h, sw, sh, bx, by;
111 WINDOW *swin1, *swin2, *swin3;
114 getbegyx(win, by, bx);
118 if ((swin1 = subwin(win, sh, sw, by + 3, bx + 5)) == NULL) {
121 if ((swin2 = subwin(win, sh, sw, by + 4, bx + 8)) == NULL) {
125 if ((swin3 = subwin(win, sh, sw, by + 5, bx + 11)) == NULL) {
131 set_colors(swin1, 8, COLOR_RED, COLOR_BLUE);
133 MvWAddStr(swin1, 0, 3, "Sub-window 1");
136 set_colors(swin2, 9, COLOR_CYAN, COLOR_MAGENTA);
138 MvWAddStr(swin2, 0, 3, "Sub-window 2");
141 set_colors(swin3, 10, COLOR_YELLOW, COLOR_GREEN);
143 MvWAddStr(swin3, 0, 3, "Sub-window 3");
154 bounce(int n, int *dir, int len)
160 if (n <= 1 || n >= len - 2)
169 BouncingBalls(WINDOW *win)
172 int x1, y1, xd1, yd1;
173 int x2, y2, xd2, yd2;
174 int x3, y3, xd3, yd3;
178 x1 = 2 + rand() % (w - 4);
179 y1 = 2 + rand() % (h - 4);
180 x2 = 2 + rand() % (w - 4);
181 y2 = 2 + rand() % (h - 4);
182 x3 = 2 + rand() % (w - 4);
183 y3 = 2 + rand() % (h - 4);
194 while (wgetch(win) == ERR) {
195 x1 = bounce(x1, &xd1, w);
196 y1 = bounce(y1, &yd1, h);
197 x2 = bounce(x2, &xd2, w);
198 y2 = bounce(y2, &yd2, h);
199 x3 = bounce(x3, &xd3, w);
200 y3 = bounce(y3, &yd3, h);
202 set_colors(win, 11, COLOR_RED, COLOR_BLUE);
203 MvWAddCh(win, y1, x1, 'O');
205 set_colors(win, 12, COLOR_BLUE, COLOR_RED);
206 MvWAddCh(win, y2, x2, '*');
208 set_colors(win, 13, COLOR_YELLOW, COLOR_WHITE);
209 MvWAddCh(win, y3, x3, '@');
222 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
226 char buffer[SIZEOF(messages) * 80];
230 setlocale(LC_ALL, "");
232 InitAndCatch(initscr(), trap);
238 height = 14; /* Create a drawing window */
239 win = newwin(height, width, (LINES - height) / 2, (COLS - width) / 2);
242 ExitProgram(EXIT_FAILURE);
251 set_colors(win, 1, COLOR_WHITE, COLOR_BLUE);
254 set_colors(win, 2, COLOR_RED, COLOR_RED);
255 box(win, ACS_VLINE, ACS_HLINE);
257 /* Do ramdom output of a character */
258 use_colors(win, 1, A_NORMAL);
260 for (i = 0; i < 5000; ++i) {
261 x = rand() % (width - 2) + 1;
262 y = rand() % (height - 2) + 1;
263 MvWAddCh(win, y, x, c);
266 if (wgetch(win) != ERR)
270 set_colors(win, 3, COLOR_CYAN, COLOR_YELLOW);
275 /* Erase and draw green window */
276 set_colors(win, 4, COLOR_YELLOW, COLOR_GREEN);
277 wbkgd(win, use_colors(win, 4, A_BOLD));
280 /* Draw RED bounding box */
281 use_colors(win, 2, A_NORMAL);
284 /* Display Australia map */
285 use_colors(win, 4, A_BOLD);
288 MvWAddStr(win, i + 1, 8, AusMap[i]);
294 set_colors(win, 5, COLOR_BLUE, COLOR_WHITE);
295 use_colors(win, 5, A_BLINK);
296 MvWAddStr(win, height - 2, 6, " PDCurses 2.1 for DOS, OS/2 and Unix");
299 /* Draw running messages */
300 set_colors(win, 6, COLOR_YELLOW, COLOR_WHITE);
301 message = messages[j = 0];
304 _nc_STRCPY(buffer, message, sizeof(buffer));
305 while (j < NMESSAGES) {
306 while ((int) strlen(buffer) < w) {
307 _nc_STRCAT(buffer, " ... ", sizeof(buffer));
308 _nc_STRCAT(buffer, messages[++j % NMESSAGES], sizeof(buffer));
312 (void) mvwaddnstr(win, height / 2, w - i, buffer, i);
314 (void) mvwaddnstr(win, height / 2, 1, buffer, w);
318 if (wgetch(win) != ERR) {
323 for (k = 0; (buffer[k] = buffer[k + 1]) != '\0'; k++) ;
329 /* Draw running As across in RED */
330 set_colors(win, 7, COLOR_RED, COLOR_GREEN);
331 memset(save, ' ', sizeof(save));
332 for (i = 2; i < width - 4; ++i) {
333 k = (int) mvwinch(win, 4, i);
336 save[j++] = c = (chtype) k;
338 MvWAddCh(win, 4, i, c);
342 /* Put a message up wait for a key */
344 use_colors(win, 5, A_NORMAL);
345 MvWAddStr(win, i, 5, " Type a key to continue or 'Q' to quit ");
348 if (WaitForUser(win) == 1)
351 j = 0; /* Restore the old line */
352 for (i = 2; i < width - 4; ++i)
353 MvWAddCh(win, 4, i, save[j++]);
357 /* Put a message up wait for a key */
359 use_colors(win, 5, A_NORMAL);
360 MvWAddStr(win, i, 5, " Type a key to continue or 'Q' to quit ");
362 if (WaitForUser(win) == 1)
366 ExitProgram(EXIT_SUCCESS);