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.45 2017/09/30 15:43:08 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, "");
233 InitAndCatch(initscr(), trap);
239 height = 14; /* Create a drawing window */
240 win = newwin(height, width, (LINES - height) / 2, (COLS - width) / 2);
243 ExitProgram(EXIT_FAILURE);
247 set_colors(win, 1, COLOR_WHITE, COLOR_BLUE);
250 set_colors(win, 2, COLOR_RED, COLOR_RED);
251 box(win, ACS_VLINE, ACS_HLINE);
253 /* Do ramdom output of a character */
254 use_colors(win, 1, A_NORMAL);
256 for (i = 0; i < 5000; ++i) {
257 x = rand() % (width - 2) + 1;
258 y = rand() % (height - 2) + 1;
259 MvWAddCh(win, y, x, c);
262 if (wgetch(win) != ERR)
266 set_colors(win, 3, COLOR_CYAN, COLOR_YELLOW);
271 /* Erase and draw green window */
272 set_colors(win, 4, COLOR_YELLOW, COLOR_GREEN);
273 wbkgd(win, use_colors(win, 4, A_BOLD));
276 /* Draw RED bounding box */
277 use_colors(win, 2, A_NORMAL);
280 /* Display Australia map */
281 use_colors(win, 4, A_BOLD);
284 MvWAddStr(win, i + 1, 8, AusMap[i]);
290 set_colors(win, 5, COLOR_BLUE, COLOR_WHITE);
291 use_colors(win, 5, A_BLINK);
292 MvWAddStr(win, height - 2, 6, " PDCurses 2.1 for DOS, OS/2 and Unix");
295 /* Draw running messages */
296 set_colors(win, 6, COLOR_YELLOW, COLOR_WHITE);
297 message = messages[j = 0];
300 _nc_STRCPY(buffer, message, sizeof(buffer));
301 while (j < NMESSAGES) {
302 while ((int) strlen(buffer) < w) {
303 _nc_STRCAT(buffer, " ... ", sizeof(buffer));
304 _nc_STRCAT(buffer, messages[++j % NMESSAGES], sizeof(buffer));
308 (void) mvwaddnstr(win, height / 2, w - i, buffer, i);
310 (void) mvwaddnstr(win, height / 2, 1, buffer, w);
314 if (wgetch(win) != ERR) {
319 for (k = 0; (buffer[k] = buffer[k + 1]) != '\0'; k++) ;
325 /* Draw running As across in RED */
326 set_colors(win, 7, COLOR_RED, COLOR_GREEN);
327 memset(save, ' ', sizeof(save));
328 for (i = 2; i < width - 4; ++i) {
329 k = (int) mvwinch(win, 4, i);
332 save[j++] = c = (chtype) k;
334 MvWAddCh(win, 4, i, c);
338 /* Put a message up wait for a key */
340 use_colors(win, 5, A_NORMAL);
341 MvWAddStr(win, i, 5, " Type a key to continue or 'Q' to quit ");
344 if (WaitForUser(win) == 1)
347 j = 0; /* Restore the old line */
348 for (i = 2; i < width - 4; ++i)
349 MvWAddCh(win, 4, i, save[j++]);
353 /* Put a message up wait for a key */
355 use_colors(win, 5, A_NORMAL);
356 MvWAddStr(win, i, 5, " Type a key to continue or 'Q' to quit ");
358 if (WaitForUser(win) == 1)
362 ExitProgram(EXIT_SUCCESS);