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.43 2016/09/10 21:25:53 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)
67 t = time((time_t *) 0);
69 if ((int) (key = (chtype) wgetch(win)) != ERR) {
70 if (key == 'q' || key == 'Q')
75 if (time((time_t *) 0) - t > 5)
81 set_colors(WINDOW *win, int pair, int foreground, int background)
84 if (pair > COLOR_PAIRS)
86 init_pair((short) pair, (short) foreground, (short) background);
87 (void) wattrset(win, AttrArg(COLOR_PAIR(pair), 0));
92 use_colors(WINDOW *win, int pair, chtype attrs)
95 if (pair > COLOR_PAIRS)
97 attrs |= (chtype) COLOR_PAIR(pair);
99 (void) wattrset(win, AttrArg(attrs, 0));
107 SubWinTest(WINDOW *win)
109 int w, h, sw, sh, bx, by;
110 WINDOW *swin1, *swin2, *swin3;
113 getbegyx(win, by, bx);
117 if ((swin1 = subwin(win, sh, sw, by + 3, bx + 5)) == NULL) {
120 if ((swin2 = subwin(win, sh, sw, by + 4, bx + 8)) == NULL) {
124 if ((swin3 = subwin(win, sh, sw, by + 5, bx + 11)) == NULL) {
130 set_colors(swin1, 8, COLOR_RED, COLOR_BLUE);
132 MvWAddStr(swin1, 0, 3, "Sub-window 1");
135 set_colors(swin2, 9, COLOR_CYAN, COLOR_MAGENTA);
137 MvWAddStr(swin2, 0, 3, "Sub-window 2");
140 set_colors(swin3, 10, COLOR_YELLOW, COLOR_GREEN);
142 MvWAddStr(swin3, 0, 3, "Sub-window 3");
153 bounce(int n, int *dir, int len)
159 if (n <= 1 || n >= len - 2)
168 BouncingBalls(WINDOW *win)
171 int x1, y1, xd1, yd1;
172 int x2, y2, xd2, yd2;
173 int x3, y3, xd3, yd3;
177 x1 = 2 + rand() % (w - 4);
178 y1 = 2 + rand() % (h - 4);
179 x2 = 2 + rand() % (w - 4);
180 y2 = 2 + rand() % (h - 4);
181 x3 = 2 + rand() % (w - 4);
182 y3 = 2 + rand() % (h - 4);
193 while (wgetch(win) == ERR) {
194 x1 = bounce(x1, &xd1, w);
195 y1 = bounce(y1, &yd1, h);
196 x2 = bounce(x2, &xd2, w);
197 y2 = bounce(y2, &yd2, h);
198 x3 = bounce(x3, &xd3, w);
199 y3 = bounce(y3, &yd3, h);
201 set_colors(win, 11, COLOR_RED, COLOR_BLUE);
202 MvWAddCh(win, y1, x1, 'O');
204 set_colors(win, 12, COLOR_BLUE, COLOR_RED);
205 MvWAddCh(win, y2, x2, '*');
207 set_colors(win, 13, COLOR_YELLOW, COLOR_WHITE);
208 MvWAddCh(win, y3, x3, '@');
221 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
224 int w, x, y, i, j, k;
225 char buffer[SIZEOF(messages) * 80];
231 setlocale(LC_ALL, "");
241 height = 14; /* Create a drawing window */
242 win = newwin(height, width, (LINES - height) / 2, (COLS - width) / 2);
245 ExitProgram(EXIT_FAILURE);
249 set_colors(win, 1, COLOR_WHITE, COLOR_BLUE);
252 set_colors(win, 2, COLOR_RED, COLOR_RED);
253 box(win, ACS_VLINE, ACS_HLINE);
255 /* Do ramdom output of a character */
256 use_colors(win, 1, A_NORMAL);
258 for (i = 0; i < 5000; ++i) {
259 x = rand() % (width - 2) + 1;
260 y = rand() % (height - 2) + 1;
261 MvWAddCh(win, y, x, c);
264 if (wgetch(win) != ERR)
268 set_colors(win, 3, COLOR_CYAN, COLOR_YELLOW);
273 /* Erase and draw green window */
274 set_colors(win, 4, COLOR_YELLOW, COLOR_GREEN);
275 wbkgd(win, use_colors(win, 4, A_BOLD));
278 /* Draw RED bounding box */
279 use_colors(win, 2, A_NORMAL);
282 /* Display Australia map */
283 use_colors(win, 4, A_BOLD);
286 MvWAddStr(win, i + 1, 8, AusMap[i]);
292 set_colors(win, 5, COLOR_BLUE, COLOR_WHITE);
293 use_colors(win, 5, A_BLINK);
294 MvWAddStr(win, height - 2, 6, " PDCurses 2.1 for DOS, OS/2 and Unix");
297 /* Draw running messages */
298 set_colors(win, 6, COLOR_YELLOW, COLOR_WHITE);
299 message = messages[j = 0];
302 _nc_STRCPY(buffer, message, sizeof(buffer));
303 while (j < NMESSAGES) {
304 while ((int) strlen(buffer) < w) {
305 _nc_STRCAT(buffer, " ... ", sizeof(buffer));
306 _nc_STRCAT(buffer, messages[++j % NMESSAGES], sizeof(buffer));
310 (void) mvwaddnstr(win, height / 2, w - i, buffer, i);
312 (void) mvwaddnstr(win, height / 2, 1, buffer, w);
316 if (wgetch(win) != ERR) {
321 for (k = 0; (buffer[k] = buffer[k + 1]) != '\0'; k++) ;
327 /* Draw running As across in RED */
328 set_colors(win, 7, COLOR_RED, COLOR_GREEN);
329 memset(save, ' ', sizeof(save));
330 for (i = 2; i < width - 4; ++i) {
331 k = (int) mvwinch(win, 4, i);
334 save[j++] = c = (chtype) k;
336 MvWAddCh(win, 4, i, c);
340 /* Put a message up wait for a key */
342 use_colors(win, 5, A_NORMAL);
343 MvWAddStr(win, i, 5, " Type a key to continue or 'Q' to quit ");
346 if (WaitForUser(win) == 1)
349 j = 0; /* Restore the old line */
350 for (i = 2; i < width - 4; ++i)
351 MvWAddCh(win, 4, i, save[j++]);
355 /* Put a message up wait for a key */
357 use_colors(win, 5, A_NORMAL);
358 MvWAddStr(win, i, 5, " Type a key to continue or 'Q' to quit ");
360 if (WaitForUser(win) == 1)
364 ExitProgram(EXIT_SUCCESS);