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.48 2022/12/10 23:36:05 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, '@');
221 static const char *msg[] =
223 "Usage: newdemo [options]"
229 for (n = 0; n < SIZEOF(msg); n++)
230 fprintf(stderr, "%s\n", msg[n]);
232 ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
242 main(int argc, char *argv[])
246 char buffer[SIZEOF(messages) * 80];
251 while ((ch = getopt(argc, argv, OPTS_COMMON)) != -1) {
255 ExitProgram(EXIT_SUCCESS);
257 usage(ch == OPTS_USAGE);
264 setlocale(LC_ALL, "");
266 InitAndCatch(initscr(), trap);
272 height = 14; /* Create a drawing window */
273 win = newwin(height, width, (LINES - height) / 2, (COLS - width) / 2);
276 ExitProgram(EXIT_FAILURE);
285 set_colors(win, 1, COLOR_WHITE, COLOR_BLUE);
288 set_colors(win, 2, COLOR_RED, COLOR_RED);
289 box(win, ACS_VLINE, ACS_HLINE);
291 /* Do ramdom output of a character */
292 use_colors(win, 1, A_NORMAL);
294 for (i = 0; i < 5000; ++i) {
295 x = rand() % (width - 2) + 1;
296 y = rand() % (height - 2) + 1;
297 MvWAddCh(win, y, x, c);
300 if (wgetch(win) != ERR)
304 set_colors(win, 3, COLOR_CYAN, COLOR_YELLOW);
309 /* Erase and draw green window */
310 set_colors(win, 4, COLOR_YELLOW, COLOR_GREEN);
311 wbkgd(win, use_colors(win, 4, A_BOLD));
314 /* Draw RED bounding box */
315 use_colors(win, 2, A_NORMAL);
318 /* Display Australia map */
319 use_colors(win, 4, A_BOLD);
322 MvWAddStr(win, i + 1, 8, AusMap[i]);
328 set_colors(win, 5, COLOR_BLUE, COLOR_WHITE);
329 use_colors(win, 5, A_BLINK);
330 MvWAddStr(win, height - 2, 6, " PDCurses 2.1 for DOS, OS/2 and Unix");
333 /* Draw running messages */
334 set_colors(win, 6, COLOR_YELLOW, COLOR_WHITE);
335 message = messages[j = 0];
338 _nc_STRCPY(buffer, message, sizeof(buffer));
339 while (j < NMESSAGES) {
340 while ((int) strlen(buffer) < w) {
341 _nc_STRCAT(buffer, " ... ", sizeof(buffer));
342 _nc_STRCAT(buffer, messages[++j % NMESSAGES], sizeof(buffer));
346 (void) mvwaddnstr(win, height / 2, w - i, buffer, i);
348 (void) mvwaddnstr(win, height / 2, 1, buffer, w);
352 if (wgetch(win) != ERR) {
357 for (k = 0; (buffer[k] = buffer[k + 1]) != '\0'; k++) ;
363 /* Draw running As across in RED */
364 set_colors(win, 7, COLOR_RED, COLOR_GREEN);
365 memset(save, ' ', sizeof(save));
366 for (i = 2; i < width - 4; ++i) {
367 k = (int) mvwinch(win, 4, i);
370 save[j++] = c = (chtype) k;
372 MvWAddCh(win, 4, i, c);
376 /* Put a message up wait for a key */
378 use_colors(win, 5, A_NORMAL);
379 MvWAddStr(win, i, 5, " Type a key to continue or 'Q' to quit ");
382 if (WaitForUser(win) == 1)
385 j = 0; /* Restore the old line */
386 for (i = 2; i < width - 4; ++i)
387 MvWAddCh(win, 4, i, save[j++]);
391 /* Put a message up wait for a key */
393 use_colors(win, 5, A_NORMAL);
394 MvWAddStr(win, i, 5, " Type a key to continue or 'Q' to quit ");
396 if (WaitForUser(win) == 1)
400 ExitProgram(EXIT_SUCCESS);