]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/testcurs.c
ncurses 5.3
[ncurses.git] / test / testcurs.c
1 /*
2  *
3  * This is a test program for the PDCurses screen package for IBM PC type
4  * machines.
5  *
6  * This program was written by John Burnell (johnb@kea.am.dsir.govt.nz)
7  *  wrs(5/28/93) -- modified to be consistent (perform identically) with either
8  *                  PDCurses or under Unix System V, R4
9  *
10  * $Id: testcurs.c,v 1.31 2002/09/14 22:45:15 tom Exp $
11  */
12
13 #include <test.priv.h>
14 #include <ctype.h>
15
16 #if defined(XCURSES)
17 char *XCursesProgramName = "testcurs";
18 #endif
19
20 static int initTest(WINDOW **);
21 static void display_menu(int, int);
22 static void inputTest(WINDOW *);
23 static void introTest(WINDOW *);
24 static void outputTest(WINDOW *);
25 static void padTest(WINDOW *);
26 static void scrollTest(WINDOW *);
27 #if defined(PDCURSES) && !defined(XCURSES)
28 static void resizeTest(WINDOW *);
29 #endif
30
31 struct commands {
32     NCURSES_CONST char *text;
33     void (*function) (WINDOW *);
34 };
35 typedef struct commands COMMAND;
36
37 const COMMAND command[] =
38 {
39     {"General Test", introTest},
40     {"Pad Test", padTest},
41 #if defined(PDCURSES) && !defined(XCURSES)
42     {"Resize Test", resizeTest},
43 #endif
44     {"Scroll Test", scrollTest},
45     {"Input Test", inputTest},
46     {"Output Test", outputTest}
47 };
48 #define MAX_OPTIONS SIZEOF(command)
49
50 #if !HAVE_STRDUP
51 #define strdup my_strdup
52 static char *
53 strdup(char *s)
54 {
55     char *p = (char *) malloc(strlen(s) + 1);
56     if (p)
57         strcpy(p, s);
58     return (p);
59 }
60 #endif /* not HAVE_STRDUP */
61
62 int width, height;
63
64 int
65 main(
66         int argc GCC_UNUSED,
67         char *argv[]GCC_UNUSED)
68 {
69     WINDOW *win;
70     int key;
71     int old_option = (-1);
72     int new_option = 0;
73     bool quit = FALSE;
74     unsigned n;
75
76     setlocale(LC_ALL, "");
77
78 #ifdef PDCDEBUG
79     PDC_debug("testcurs started\n");
80 #endif
81     if (!initTest(&win))
82         ExitProgram(EXIT_FAILURE);
83
84     erase();
85     display_menu(old_option, new_option);
86     for (;;) {
87 #ifdef A_COLOR
88         if (has_colors()) {
89             init_pair(1, COLOR_WHITE, COLOR_BLUE);
90             wbkgd(win, COLOR_PAIR(1));
91         } else
92             wbkgd(win, A_REVERSE);
93 #else
94         wbkgd(win, A_REVERSE);
95 #endif
96         werase(win);
97
98         noecho();
99         keypad(stdscr, TRUE);
100         raw();
101         key = getch();
102         if (key < KEY_MIN && key > 0 && isalpha(key)) {
103             if (islower(key))
104                 key = toupper(key);
105             for (n = 0; n < MAX_OPTIONS; ++n) {
106                 if (key == command[n].text[0]) {
107                     display_menu(old_option, new_option = n);
108                     key = ' ';
109                     break;
110                 }
111             }
112         }
113         switch (key) {
114         case 10:
115         case 13:
116         case KEY_ENTER:
117             erase();
118             refresh();
119             (*command[new_option].function) (win);
120             erase();
121             display_menu(old_option, new_option);
122             break;
123         case KEY_UP:
124             new_option = (new_option == 0) ? new_option : new_option - 1;
125             display_menu(old_option, new_option);
126             break;
127         case KEY_DOWN:
128             new_option = (new_option == MAX_OPTIONS - 1) ? new_option :
129                 new_option + 1;
130             display_menu(old_option, new_option);
131             break;
132         case 'Q':
133         case 'q':
134             quit = TRUE;
135             break;
136         default:
137             beep();
138             break;
139         case ' ':
140             break;
141         }
142         if (quit == TRUE)
143             break;
144     }
145
146     delwin(win);
147
148     endwin();
149 #ifdef XCURSES
150     XCursesExit();
151 #endif
152     ExitProgram(EXIT_SUCCESS);
153 }
154
155 static void
156 Continue(WINDOW *win)
157 {
158     int y1 = getmaxy(win);
159     int x1 = getmaxx(win);
160     int y0 = y1 < 10 ? y1 : 10;
161     int x0 = 1;
162     long save;
163
164     save = mvwinch(win, y0, x1 - 1);
165
166     mvwaddstr(win, y0, x0, " Press any key to continue");
167     wclrtoeol(win);
168     getyx(win, y0, x0);
169
170     mvwaddch(win, y0, x1 - 1, save);
171
172     wmove(win, y0, x0);
173     raw();
174     wgetch(win);
175 }
176
177 static int
178 initTest(WINDOW **win)
179 {
180 #ifdef PDCDEBUG
181     PDC_debug("initTest called\n");
182 #endif
183 #ifdef TRACE
184     trace(TRACE_MAXIMUM);
185 #endif
186     initscr();
187 #ifdef PDCDEBUG
188     PDC_debug("after initscr()\n");
189 #endif
190 #ifdef A_COLOR
191     if (has_colors())
192         start_color();
193 #endif
194     width = 60;
195     height = 13;                /* Create a drawing window */
196     *win = newwin(height, width, (LINES - height) / 2, (COLS - width) / 2);
197     if (*win == NULL) {
198         endwin();
199         return 0;
200     }
201     return 1;
202 }
203
204 static void
205 introTest(WINDOW *win)
206 {
207     wmove(win, height / 2 - 5, width / 2);
208     wvline(win, ACS_VLINE, 10);
209     wmove(win, height / 2, width / 2 - 10);
210     whline(win, ACS_HLINE, 20);
211     Continue(win);
212
213     beep();
214     werase(win);
215
216     box(win, ACS_VLINE, ACS_HLINE);
217     wrefresh(win);
218     cbreak();
219     mvwaddstr(win, 1, 1,
220               "You should have rectangle in the middle of the screen");
221     mvwaddstr(win, 2, 1, "You should have heard a beep");
222     Continue(win);
223     return;
224 }
225
226 static void
227 scrollTest(WINDOW *win)
228 {
229     int i;
230     int half;
231     int OldY;
232     NCURSES_CONST char *Message = "The window will now scroll slowly";
233
234     wclear(win);
235     OldY = getmaxy(win);
236     half = OldY / 2;
237     mvwprintw(win, OldY - 2, 1, Message);
238     wrefresh(win);
239     scrollok(win, TRUE);
240     for (i = 1; i <= OldY; i++) {
241         napms(600);
242         scroll(win);
243         wrefresh(win);
244     }
245
246     werase(win);
247     for (i = 1; i < OldY; i++) {
248         mvwprintw(win, i, 1, "Line %d", i);
249     }
250     mvwprintw(win, OldY - 2, 1, "The top of the window will scroll");
251     wmove(win, 1, 1);
252     wsetscrreg(win, 0, half - 1);
253     box(win, ACS_VLINE, ACS_HLINE);
254     wrefresh(win);
255     for (i = 1; i <= half; i++) {
256         napms(600);
257         scroll(win);
258         box(win, ACS_VLINE, ACS_HLINE);
259         wrefresh(win);
260     }
261
262     werase(win);
263     for (i = 1; i < OldY; i++) {
264         mvwprintw(win, i, 1, "Line %d", i);
265     }
266     mvwprintw(win, 1, 1, "The bottom of the window will scroll");
267     wmove(win, OldY - 2, 1);
268     wsetscrreg(win, half, --OldY);
269     box(win, ACS_VLINE, ACS_HLINE);
270     wrefresh(win);
271     for (i = half; i <= OldY; i++) {
272         napms(600);
273         wscrl(win, -1);
274         box(win, ACS_VLINE, ACS_HLINE);
275         wrefresh(win);
276     }
277     wsetscrreg(win, 0, OldY);
278 }
279
280 static void
281 inputTest(WINDOW *win)
282 {
283     int answered;
284     int repeat;
285     int w, h, bx, by, sw, sh, i, c, num;
286     char buffer[80];
287     WINDOW *subWin;
288     wclear(win);
289
290     getmaxyx(win, h, w);
291     getbegyx(win, by, bx);
292     sw = w / 3;
293     sh = h / 3;
294     if ((subWin = subwin(win, sh, sw, by + h - sh - 2, bx + w - sw - 2)) == NULL)
295         return;
296
297 #ifdef A_COLOR
298     if (has_colors()) {
299         init_pair(2, COLOR_WHITE, COLOR_RED);
300         wbkgd(subWin, COLOR_PAIR(2) | A_BOLD);
301     } else
302         wbkgd(subWin, A_BOLD);
303 #else
304     wbkgd(subWin, A_BOLD);
305 #endif
306     box(subWin, ACS_VLINE, ACS_HLINE);
307     wrefresh(win);
308
309     nocbreak();
310     mvwaddstr(win, 2, 1, "Press some keys for 5 seconds");
311     mvwaddstr(win, 1, 1, "Pressing ^C should do nothing");
312     wrefresh(win);
313
314     werase(subWin);
315     box(subWin, ACS_VLINE, ACS_HLINE);
316     for (i = 0; i < 5; i++) {
317         mvwprintw(subWin, 1, 1, "Time = %d", i);
318         wrefresh(subWin);
319         napms(1000);
320         flushinp();
321     }
322
323     delwin(subWin);
324     werase(win);
325     flash();
326     wrefresh(win);
327     napms(500);
328
329     mvwaddstr(win, 2, 1, "Press a key, followed by ENTER");
330     wmove(win, 9, 10);
331     wrefresh(win);
332     echo();
333     noraw();
334     wgetch(win);
335     flushinp();
336
337     wmove(win, 9, 10);
338     wdelch(win);
339     mvwaddstr(win, 4, 1, "The character should now have been deleted");
340     Continue(win);
341
342     wclear(win);
343     mvwaddstr(win, 1, 1, "Press keys (or mouse buttons) to show their names");
344     mvwaddstr(win, 2, 1, "Press spacebar to finish");
345     wrefresh(win);
346     keypad(win, TRUE);
347     raw();
348     noecho();
349     typeahead(-1);
350 #if defined(PDCURSES)
351     mouse_set(ALL_MOUSE_EVENTS);
352 #endif
353     for (;;) {
354         wmove(win, 3, 5);
355         c = wgetch(win);
356         wclrtobot(win);
357         if (c >= KEY_MIN)
358             wprintw(win, "Key Pressed: %s", keyname(c));
359         else if (isprint(c))
360             wprintw(win, "Key Pressed: %c", c);
361         else
362             wprintw(win, "Key Pressed: %s", unctrl(c));
363 #if defined(PDCURSES)
364         if (c == KEY_MOUSE) {
365             int button = 0;
366             request_mouse_pos();
367             if (BUTTON_CHANGED(1))
368                 button = 1;
369             else if (BUTTON_CHANGED(2))
370                 button = 2;
371             else if (BUTTON_CHANGED(3))
372                 button = 3;
373             else
374                 button = 0;
375             wmove(win, 4, 18);
376             wprintw(win, "Button %d: ", button);
377             if (MOUSE_MOVED)
378                 wprintw(win, "moved: ");
379             else if ((BUTTON_STATUS(button) & BUTTON_ACTION_MASK) == BUTTON_PRESSED)
380                 wprintw(win, "pressed: ");
381             else if ((BUTTON_STATUS(button) & BUTTON_ACTION_MASK) == BUTTON_DOUBLE_CLICKED)
382                 wprintw(win, "double: ");
383             else
384                 wprintw(win, "released: ");
385             wprintw(win, " Position: Y: %d X: %d", MOUSE_Y_POS, MOUSE_X_POS);
386         }
387 #endif
388         wrefresh(win);
389         if (c == ' ')
390             break;
391     }
392 #if 0
393     nodelay(win, TRUE);
394     wgetch(win);
395     nodelay(win, FALSE);
396 #endif
397 #if defined(PDCURSES)
398     mouse_set(0L);
399 #endif
400     refresh();
401
402     repeat = 0;
403     do {
404         static const char *fmt[] = {
405             "%d %10s",
406             "%d %[a-zA-Z]s",
407             "%d %[][a-zA-Z]s",
408             "%d %[^0-9]"
409         };
410         const char *format = fmt[repeat % SIZEOF(fmt)];
411
412         wclear(win);
413         mvwaddstr(win, 3, 2, "The window should have moved");
414         mvwaddstr(win, 4, 2,
415                   "This text should have appeared without you pressing a key");
416         mvwprintw(win, 6, 2,
417                   "Scanning with format \"%s\"", format);
418         mvwin(win, 2 + 2 * (repeat % 4), 1 + 2 * (repeat % 4));
419         erase();
420         refresh();
421         wrefresh(win);
422         echo();
423         noraw();
424         num = 0;
425         *buffer = 0;
426         answered = mvwscanw(win, 7, 6, strdup(format), &num, buffer);
427         mvwprintw(win, 8, 6,
428                   "String: %s Number: %d (%d values read)",
429                   buffer, num, answered);
430         Continue(win);
431         ++repeat;
432     } while (answered > 0);
433 }
434
435 static void
436 outputTest(WINDOW *win)
437 {
438     WINDOW *win1;
439     char Buffer[80];
440     chtype ch;
441     int by, bx;
442
443     nl();
444     wclear(win);
445     mvwaddstr(win, 1, 1,
446               "You should now have a screen in the upper left corner, and this text should have wrapped");
447     mvwin(win, 2, 1);
448     waddstr(win, "\nThis text should be down\n");
449     waddstr(win, "and broken into two here ^");
450     Continue(win);
451
452     wclear(win);
453     wattron(win, A_BOLD);
454     mvwaddstr(win, 1, 1, "A new window will appear with this text in it");
455     mvwaddstr(win, 8, 1, "Press any key to continue");
456     wrefresh(win);
457     wgetch(win);
458
459     getbegyx(win, by, bx);
460
461     if (LINES < 24 || COLS < 75) {
462         mvwaddstr(win, 5, 1,
463                   "Some tests have been skipped as they require a");
464         mvwaddstr(win, 6, 1, "display of at least 24 LINES by 75 COLUMNS");
465         Continue(win);
466     } else {
467         win1 = newwin(10, 50, 14, 25);
468         if (win1 == NULL) {
469             endwin();
470             return;
471         }
472 #ifdef A_COLOR
473         if (has_colors()) {
474             init_pair(3, COLOR_BLUE, COLOR_WHITE);
475             wbkgd(win1, COLOR_PAIR(3));
476         } else
477             wbkgd(win1, A_NORMAL);
478 #else
479         wbkgd(win1, A_NORMAL);
480 #endif
481         wclear(win1);
482         mvwaddstr(win1, 5, 1,
483                   "This text should appear; using overlay option");
484         copywin(win, win1, 0, 0, 0, 0, 9, 49, TRUE);
485
486 #if defined(PDCURSES) && !defined(XCURSES)
487         box(win1, 0xb3, 0xc4);
488 #else
489         box(win1, ACS_VLINE, ACS_HLINE);
490 #endif
491         wmove(win1, 8, 26);
492         wrefresh(win1);
493         wgetch(win1);
494
495         wclear(win1);
496         wattron(win1, A_BLINK);
497         mvwaddstr(win1, 4, 1,
498                   "This blinking text should appear in only the second window");
499         wattroff(win1, A_BLINK);
500         mvwin(win1, by, bx);
501         overlay(win, win1);
502         mvwin(win1, 14, 25);
503         wmove(win1, 8, 26);
504         wrefresh(win1);
505         wgetch(win1);
506         delwin(win1);
507     }
508
509     clear();
510     wclear(win);
511     wrefresh(win);
512     mvwaddstr(win, 6, 2, "This line shouldn't appear");
513     mvwaddstr(win, 4, 2, "Only half of the next line is visible");
514     mvwaddstr(win, 5, 2, "Only half of the next line is visible");
515     wmove(win, 6, 1);
516     wclrtobot(win);
517     wmove(win, 5, 20);
518     wclrtoeol(win);
519     mvwaddstr(win, 8, 2, "This line also shouldn't appear");
520     wmove(win, 8, 1);
521     wdeleteln(win);
522     Continue(win);
523
524     wmove(win, 5, 9);
525     ch = winch(win);
526
527     wclear(win);
528     wmove(win, 6, 2);
529     waddstr(win, "The next char should be l:  ");
530     winsch(win, ch);
531     Continue(win);
532
533     mvwinsstr(win, 6, 2, "A1B2C3D4E5");
534     Continue(win);
535
536     wmove(win, 5, 1);
537     winsertln(win);
538     mvwaddstr(win, 5, 2, "The lines below should have moved down");
539     Continue(win);
540
541     wclear(win);
542     wmove(win, 2, 2);
543     wprintw(win, "This is a formatted string in a window: %d %s\n", 42,
544             "is it");
545     mvwaddstr(win, 10, 1, "Enter a string: ");
546     wrefresh(win);
547     noraw();
548     echo();
549     *Buffer = 0;
550     wscanw(win, "%s", Buffer);
551
552     printw("This is a formatted string in stdscr: %d %s\n", 42, "is it");
553     mvaddstr(10, 1, "Enter a string: ");
554     *Buffer = 0;
555     scanw("%s", Buffer);
556
557     if (tigetstr("cvvis") != 0) {
558         wclear(win);
559         curs_set(2);
560         mvwaddstr(win, 1, 1, "The cursor should appear as a block (visible)");
561         Continue(win);
562     }
563
564     if (tigetstr("civis") != 0) {
565         wclear(win);
566         curs_set(0);
567         mvwaddstr(win, 1, 1,
568                   "The cursor should have disappeared (invisible)");
569         Continue(win);
570     }
571
572     if (tigetstr("cnorm") != 0) {
573         wclear(win);
574         curs_set(1);
575         mvwaddstr(win, 1, 1, "The cursor should be an underline (normal)");
576         Continue(win);
577     }
578 #ifdef A_COLOR
579     if (has_colors()) {
580         wclear(win);
581         mvwaddstr(win, 1, 1, "Colors should change after you press a key");
582         Continue(win);
583         init_pair(1, COLOR_RED, COLOR_WHITE);
584         wrefresh(win);
585     }
586 #endif
587
588     werase(win);
589     mvwaddstr(win, 1, 1, "Information About Your Terminal");
590     mvwaddstr(win, 3, 1, termname());
591     mvwaddstr(win, 4, 1, longname());
592     if (termattrs() & A_BLINK)
593         mvwaddstr(win, 5, 1, "This terminal supports blinking.");
594     else
595         mvwaddstr(win, 5, 1, "This terminal does NOT support blinking.");
596
597     mvwaddnstr(win, 7, 5, "Have a nice day!ok", 16);
598     wrefresh(win);
599
600     mvwinnstr(win, 7, 5, Buffer, 18);
601     mvaddstr(LINES - 2, 10, Buffer);
602     refresh();
603     Continue(win);
604 }
605
606 #if defined(PDCURSES) && !defined(XCURSES)
607 static void
608 resizeTest(WINDOW *dummy GCC_UNUSED)
609 {
610     WINDOW *win1;
611
612     savetty();
613
614     clear();
615     refresh();
616 #  if defined(OS2)
617     resize_term(50, 120);
618 #  else
619     resize_term(50, 80);
620 #  endif
621
622     win1 = newwin(10, 50, 14, 25);
623     if (win1 == NULL) {
624         endwin();
625         return;
626     }
627 #ifdef A_COLOR
628     if (has_colors()) {
629         init_pair(3, COLOR_BLUE, COLOR_WHITE);
630         wattrset(win1, COLOR_PAIR(3));
631     }
632 #endif
633     wclear(win1);
634
635     mvwaddstr(win1, 1, 1, "The screen may now have 50 lines");
636     Continue(win1);
637
638     wclear(win1);
639     resetty();
640
641     mvwaddstr(win1, 1, 1, "The screen should now be reset");
642     Continue(win1);
643
644     delwin(win1);
645
646     clear();
647     refresh();
648
649 }
650 #endif
651
652 static void
653 padTest(WINDOW *dummy GCC_UNUSED)
654 {
655     WINDOW *pad, *spad;
656
657     pad = newpad(50, 100);
658     wattron(pad, A_REVERSE);
659     mvwaddstr(pad, 5, 2, "This is a new pad");
660     wattrset(pad, A_NORMAL);
661     mvwaddstr(pad, 8, 0,
662               "The end of this line should be truncated here:except  now");
663     mvwaddstr(pad, 11, 1, "This line should not appear.It will now");
664     wmove(pad, 10, 1);
665     wclrtoeol(pad);
666     mvwaddstr(pad, 10, 1, " Press any key to continue");
667     prefresh(pad, 0, 0, 0, 0, 10, 45);
668     keypad(pad, TRUE);
669     raw();
670     wgetch(pad);
671
672     spad = subpad(pad, 12, 25, 6, 52);
673     mvwaddstr(spad, 2, 2, "This is a new subpad");
674     box(spad, 0, 0);
675     prefresh(pad, 0, 0, 0, 0, 15, 75);
676     keypad(pad, TRUE);
677     raw();
678     wgetch(pad);
679
680     mvwaddstr(pad, 35, 2, "This is displayed at line 35 in the pad");
681     mvwaddstr(pad, 40, 1, " Press any key to continue");
682     prefresh(pad, 30, 0, 0, 0, 10, 45);
683     keypad(pad, TRUE);
684     raw();
685     wgetch(pad);
686
687     delwin(pad);
688 }
689
690 static void
691 display_menu(int old_option, int new_option)
692 {
693     register size_t i;
694
695     attrset(A_NORMAL);
696     mvaddstr(3, 20, "PDCurses Test Program");
697
698     for (i = 0; i < MAX_OPTIONS; i++)
699         mvaddstr(5 + i, 25, command[i].text);
700     if (old_option != (-1))
701         mvaddstr(5 + old_option, 25, command[old_option].text);
702     attrset(A_REVERSE);
703     mvaddstr(5 + new_option, 25, command[new_option].text);
704     attrset(A_NORMAL);
705     mvaddstr(13, 3,
706              "Use Up and Down Arrows to select - Enter to run - Q to quit");
707     refresh();
708 }