]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/ncurses/test/newdemo.c
This commit was generated by cvs2svn to compensate for changes in r159063,
[FreeBSD/FreeBSD.git] / contrib / ncurses / test / newdemo.c
1 /*
2  *  newdemo.c   -       A demo program using PDCurses. The program illustrate
3  *                      the use of colours for text output.
4  *
5  * $Id: newdemo.c,v 1.23 2002/03/23 22:17:24 tom Exp $
6  */
7
8 #include <time.h>
9
10 #include <test.priv.h>
11
12 #define delay_output(x) napms(x)
13
14 /*
15  *  The Australian map
16  */
17 const char *AusMap[16] =
18 {
19     "           A           A ",
20     "    N.T. AAAAA       AAAA ",
21     "     AAAAAAAAAAA  AAAAAAAA ",
22     "   AAAAAAAAAAAAAAAAAAAAAAAAA Qld.",
23     "AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ",
24     "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA ",
25     " AAAAAAAAAAAAAAAAAAAAAAAAAAAA ",
26     "   AAAAAAAAAAAAAAAAAAAAAAAAA N.S.W.",
27     "W.A. AAAAAAAAA      AAAAAA Vic.",
28     "       AAA   S.A.     AA",
29     "                       A  Tas.",
30     ""
31 };
32
33 /*
34  *  Funny messages
35  */
36 #define NMESSAGES   6
37
38 NCURSES_CONST char *messages[] =
39 {
40     "Hello from the Land Down Under",
41     "The Land of crocs. and a big Red Rock",
42     "Where the sunflower runs along the highways",
43     "the dusty red roads lead one to loneliness",
44     "Blue sky in the morning and",
45     "freezing nights and twinkling stars",
46     ""
47 };
48
49 /*
50  *  Trap interrupt
51  */
52 static RETSIGTYPE
53 trap(int sig GCC_UNUSED)
54 {
55     endwin();
56     ExitProgram(EXIT_FAILURE);
57 }
58
59 /*
60  *  Wait for user
61  */
62 static int
63 WaitForUser(WINDOW *win)
64 {
65     time_t t;
66     chtype key;
67
68     nodelay(win, TRUE);
69     t = time((time_t *) 0);
70     while (1) {
71         if ((int) (key = wgetch(win)) != ERR) {
72             if (key == 'q' || key == 'Q')
73                 return 1;
74             else
75                 return 0;
76         }
77         if (time((time_t *) 0) - t > 5)
78             return 0;
79     }
80 }
81
82 static void
83 set_colors(WINDOW *win, int pair, int foreground, int background)
84 {
85     if (has_colors()) {
86         if (pair > COLOR_PAIRS)
87             pair = COLOR_PAIRS;
88         init_pair(pair, foreground, background);
89         wattrset(win, COLOR_PAIR(pair));
90     }
91 }
92
93 static int
94 use_colors(WINDOW *win, int pair, int attrs)
95 {
96     if (has_colors()) {
97         if (pair > COLOR_PAIRS)
98             pair = COLOR_PAIRS;
99         attrs |= COLOR_PAIR(pair);
100     }
101     wattrset(win, attrs);
102     return attrs;
103 }
104
105 /*
106  * Test sub windows
107  */
108 static int
109 SubWinTest(WINDOW *win)
110 {
111     int w, h, sw, sh, bx, by;
112     WINDOW *swin1, *swin2, *swin3;
113
114     getmaxyx(win, h, w);
115     getbegyx(win, by, bx);
116     sw = w / 3;
117     sh = h / 3;
118     if ((swin1 = subwin(win, sh, sw, by + 3, bx + 5)) == NULL)
119         return 1;
120     if ((swin2 = subwin(win, sh, sw, by + 4, bx + 8)) == NULL)
121         return 1;
122     if ((swin3 = subwin(win, sh, sw, by + 5, bx + 11)) == NULL)
123         return 1;
124
125     set_colors(swin1, 8, COLOR_RED, COLOR_BLUE);
126     werase(swin1);
127     mvwaddstr(swin1, 0, 3, "Sub-window 1");
128     wrefresh(swin1);
129
130     set_colors(swin2, 9, COLOR_CYAN, COLOR_MAGENTA);
131     werase(swin2);
132     mvwaddstr(swin2, 0, 3, "Sub-window 2");
133     wrefresh(swin2);
134
135     set_colors(swin3, 10, COLOR_YELLOW, COLOR_GREEN);
136     werase(swin3);
137     mvwaddstr(swin3, 0, 3, "Sub-window 3");
138     wrefresh(swin3);
139
140     delwin(swin1);
141     delwin(swin2);
142     delwin(swin3);
143     WaitForUser(win);
144     return 0;
145 }
146
147 static int
148 bounce(int n, int *dir, int len)
149 {
150     if (*dir > 0)
151         ++n;
152     else
153         --n;
154     if (n <= 1 || n >= len - 2)
155         *dir = *dir ? 0 : 1;
156     return n;
157 }
158
159 /*
160  *  Bouncing balls
161  */
162 static int
163 BouncingBalls(WINDOW *win)
164 {
165     int w, h;
166     int x1, y1, xd1, yd1;
167     int x2, y2, xd2, yd2;
168     int x3, y3, xd3, yd3;
169
170     getmaxyx(win, h, w);
171
172     x1 = 2 + rand() % (w - 4);
173     y1 = 2 + rand() % (h - 4);
174     x2 = 2 + rand() % (w - 4);
175     y2 = 2 + rand() % (h - 4);
176     x3 = 2 + rand() % (w - 4);
177     y3 = 2 + rand() % (h - 4);
178
179     xd1 = 1;
180     yd1 = 1;
181     xd2 = 1;
182     yd2 = 0;
183     xd3 = 0;
184     yd3 = 1;
185
186     nodelay(win, TRUE);
187
188     while (wgetch(win) == ERR) {
189         x1 = bounce(x1, &xd1, w);
190         y1 = bounce(y1, &yd1, h);
191         x2 = bounce(x2, &xd2, w);
192         y2 = bounce(y2, &yd2, h);
193         x3 = bounce(x3, &xd3, w);
194         y3 = bounce(y3, &yd3, h);
195
196         set_colors(win, 11, COLOR_RED, COLOR_BLUE);
197         mvwaddch(win, y1, x1, 'O');
198
199         set_colors(win, 12, COLOR_BLUE, COLOR_RED);
200         mvwaddch(win, y2, x2, '*');
201
202         set_colors(win, 13, COLOR_YELLOW, COLOR_WHITE);
203         mvwaddch(win, y3, x3, '@');
204
205         wmove(win, 0, 0);
206         wrefresh(win);
207         delay_output(100);
208     }
209     return 0;
210 }
211
212 /*
213  *  Main driver
214  */
215 int
216 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
217 {
218     WINDOW *win;
219     int w, x, y, i, j, k;
220     char buffer[200];
221     const char *message;
222     int width, height;
223     chtype save[80];
224     chtype c;
225
226     initscr();
227     if (has_colors())
228         start_color();
229     cbreak();
230     curs_set(0);
231     signal(SIGINT, trap);
232     width = 48;
233     height = 14;                /* Create a drawing window */
234     win = newwin(height, width, (LINES - height) / 2, (COLS - width) / 2);
235     if (win == NULL) {
236         endwin();
237         ExitProgram(EXIT_FAILURE);
238     }
239
240     while (1) {
241         set_colors(win, 1, COLOR_WHITE, COLOR_BLUE);
242         werase(win);
243
244         set_colors(win, 2, COLOR_RED, COLOR_RED);
245         box(win, ACS_VLINE, ACS_HLINE);
246         wrefresh(win);
247         /* Do ramdom output of a character */
248         use_colors(win, 1, A_NORMAL);
249         c = 'a';
250         for (i = 0; i < 5000; ++i) {
251             x = rand() % (width - 2) + 1;
252             y = rand() % (height - 2) + 1;
253             mvwaddch(win, y, x, c);
254             wrefresh(win);
255             nodelay(win, TRUE);
256             if (wgetch(win) != ERR)
257                 break;
258             if (i == 2000) {
259                 c = 'b';
260                 set_colors(win, 3, COLOR_CYAN, COLOR_YELLOW);
261             }
262         }
263
264         SubWinTest(win);
265         /* Erase and draw green window */
266         set_colors(win, 4, COLOR_YELLOW, COLOR_GREEN);
267         wbkgd(win, use_colors(win, 4, A_BOLD));
268         werase(win);
269         wrefresh(win);
270         /* Draw RED bounding box */
271         use_colors(win, 2, A_NORMAL);
272         box(win, ' ', ' ');
273         wrefresh(win);
274         /* Display Australia map */
275         use_colors(win, 4, A_BOLD);
276         i = 0;
277         while (*AusMap[i]) {
278             mvwaddstr(win, i + 1, 8, AusMap[i]);
279             wrefresh(win);
280             delay_output(50);
281             ++i;
282         }
283
284         set_colors(win, 5, COLOR_BLUE, COLOR_WHITE);
285         use_colors(win, 5, A_BLINK);
286         mvwaddstr(win, height - 2, 6, " PDCurses 2.1 for DOS, OS/2 and Unix");
287         wrefresh(win);
288
289         /* Draw running messages */
290         set_colors(win, 6, COLOR_YELLOW, COLOR_WHITE);
291         message = messages[j = 0];
292         i = 1;
293         w = width - 2;
294         strcpy(buffer, message);
295         while (j < NMESSAGES) {
296             while ((int) strlen(buffer) < w) {
297                 strcat(buffer, " ... ");
298                 strcat(buffer, messages[++j % NMESSAGES]);
299             }
300
301             if (i < w)
302                 mvwaddnstr(win, height / 2, w - i, buffer, i);
303             else
304                 mvwaddnstr(win, height / 2, 1, buffer, w);
305
306             wrefresh(win);
307             nodelay(win, TRUE);
308             if (wgetch(win) != ERR) {
309                 flushinp();
310                 break;
311             }
312             if (i++ >= w) {
313                 for (k = 0; (buffer[k] = buffer[k + 1]) != '\0'; k++) ;
314             }
315             delay_output(100);
316         }
317
318         j = 0;
319         /*  Draw running As across in RED */
320         set_colors(win, 7, COLOR_RED, COLOR_GREEN);
321         for (i = 2; i < width - 4; ++i) {
322             k = mvwinch(win, 4, i);
323             if (k == ERR)
324                 break;
325             save[j++] = c = k;
326             c &= A_CHARTEXT;
327             mvwaddch(win, 4, i, c);
328         }
329         wrefresh(win);
330
331         /* Put a message up wait for a key */
332         i = height - 2;
333         use_colors(win, 5, A_NORMAL);
334         mvwaddstr(win, i, 5, " Type a key to continue or 'Q' to quit ");
335         wrefresh(win);
336
337         if (WaitForUser(win) == 1)
338             break;
339
340         j = 0;                  /* Restore the old line */
341         for (i = 2; i < width - 4; ++i)
342             mvwaddch(win, 4, i, save[j++]);
343         wrefresh(win);
344
345         BouncingBalls(win);
346         /* Put a message up wait for a key */
347         i = height - 2;
348         use_colors(win, 5, A_NORMAL);
349         mvwaddstr(win, i, 5, " Type a key to continue or 'Q' to quit ");
350         wrefresh(win);
351         if (WaitForUser(win) == 1)
352             break;
353     }
354     endwin();
355     ExitProgram(EXIT_SUCCESS);
356 }