2 * This is a test program for PDCurses. Originally by
3 * John Burnell <johnb@kea.am.dsir.govt.nz>
5 * wrs(5/28/93) -- modified to be consistent (perform identically)
6 * with either PDCurses or under Unix System V, R4
8 * $Id: testcurs.c,v 1.85 2008/07/14 12:35:23 wmcbrine Exp $
11 #ifndef _XOPEN_SOURCE_EXTENDED
12 # define _XOPEN_SOURCE_EXTENDED 1
32 #if defined(PDCURSES) && !defined(XCURSES)
33 # define HAVE_RESIZE 1
35 # define HAVE_RESIZE 0
44 /* Set to non-zero if you want to test the PDCurses clipboard */
46 #define HAVE_CLIPBOARD 0
48 void inputTest(WINDOW *);
49 void scrollTest(WINDOW *);
50 void introTest(WINDOW *);
51 int initTest(WINDOW **, int, char **);
52 void outputTest(WINDOW *);
53 void padTest(WINDOW *);
54 void acsTest(WINDOW *);
57 void colorTest(WINDOW *);
61 void resizeTest(WINDOW *);
65 void clipboardTest(WINDOW *);
69 void wideTest(WINDOW *);
72 void display_menu(int, int);
77 void (*function)(WINDOW *);
80 typedef struct commands COMMAND;
82 #define MAX_OPTIONS (6 + HAVE_COLOR + HAVE_RESIZE + HAVE_CLIPBOARD + HAVE_WIDE)
84 COMMAND command[MAX_OPTIONS] =
86 {"Intro Test", introTest},
87 {"Pad Test", padTest},
89 {"Resize Test", resizeTest},
91 {"Scroll Test", scrollTest},
92 {"Input Test", inputTest},
93 {"Output Test", outputTest},
94 {"ACS Test", acsTest},
96 {"Color Test", colorTest},
99 {"Clipboard Test", clipboardTest},
102 {"Wide Input", wideTest}
108 int main(int argc, char *argv[])
111 int key, old_option = -1, new_option = 0;
114 setlocale(LC_ALL, "");
116 if (initTest(&win, argc, argv))
122 init_pair(1, COLOR_WHITE, COLOR_BLUE);
123 wbkgd(win, COLOR_PAIR(1));
127 wbkgd(win, A_REVERSE);
130 display_menu(old_option, new_option);
135 keypad(stdscr, TRUE);
148 (*command[new_option].function)(win);
150 display_menu(old_option, new_option);
155 old_option = new_option;
157 display_menu(old_option, new_option);
162 old_option = new_option;
163 new_option = MAX_OPTIONS - 1;
164 display_menu(old_option, new_option);
168 old_option = new_option;
169 new_option = (new_option == 0) ?
170 new_option : new_option - 1;
171 display_menu(old_option, new_option);
175 old_option = new_option;
176 new_option = (new_option == MAX_OPTIONS - 1) ?
177 new_option : new_option + 1;
178 display_menu(old_option, new_option);
187 display_menu(old_option, new_option);
205 void Continue(WINDOW *win)
207 mvwaddstr(win, 10, 1, " Press any key to continue");
217 mvaddstr(LINES - 2, 1, " Press any key to continue");
223 int initTest(WINDOW **win, int argc, char *argv[])
226 Xinitscr(argc, argv);
234 /* Create a drawing window */
239 *win = newwin(height, width, (LINES - height) / 2, (COLS - width) / 2);
250 void introTest(WINDOW *win)
253 wmove(win, height / 2 - 5, width / 2);
254 wvline(win, ACS_VLINE, 10);
255 wmove(win, height / 2, width / 2 - 10);
256 whline(win, ACS_HLINE, 20);
262 box(win, ACS_VLINE, ACS_HLINE);
267 "You should have a rectangle in the middle of the screen");
268 mvwaddstr(win, 2, 1, "You should have heard a beep");
272 mvwaddstr(win, 3, 1, "You should have seen a flash");
276 void scrollTest(WINDOW *win)
283 mvwaddstr(win, height - 2, 1, "The window will now scroll slowly");
284 box(win, ACS_VLINE, ACS_HLINE);
289 for (i = 1; i <= height; i++)
299 getmaxyx(win, OldY, OldX);
301 mvwaddstr(win, 6, 1, "The top of the window will scroll");
303 wsetscrreg(win, 0, 4);
304 box(win, ACS_VLINE, ACS_HLINE);
307 for (i = 1; i <= 5; i++)
314 mvwaddstr(win, 3, 1, "The bottom of the window will scroll");
316 wsetscrreg(win, 5, --OldY);
317 box(win, ACS_VLINE, ACS_HLINE);
320 for (i = 5; i <= OldY; i++)
327 wsetscrreg(win, 0, OldY);
330 void inputTest(WINDOW *win)
332 int w, h, bx, by, sw, sh, i, c, num = 0;
335 static const char spinner[4] = "/-\\|";
336 int spinner_count = 0;
341 getbegyx(win, by, bx);
346 if ((subWin = subwin(win, sh, sw, by + h - sh - 2, bx + w - sw - 2))
353 init_pair(2, COLOR_WHITE, COLOR_RED);
354 wbkgd(subWin, COLOR_PAIR(2) | A_BOLD);
358 wbkgd(subWin, A_BOLD);
360 box(subWin, ACS_VLINE, ACS_HLINE);
367 "Press keys (or mouse buttons) to show their names");
368 mvwaddstr(win, 2, 1, "Press spacebar to finish");
378 mouse_set(ALL_MOUSE_EVENTS);
379 PDC_save_key_modifiers(TRUE);
380 PDC_return_key_modifiers(TRUE);
382 curs_set(0); /* turn cursor off */
393 if (spinner_count == 4)
395 mvwaddch(win, 3, 3, spinner[spinner_count]);
405 mvwaddstr(win, 3, 5, "Key Pressed: ");
409 wprintw(win, "%s", keyname(c));
411 wprintw(win, "%c", c);
413 wprintw(win, "%s", unctrl(c));
420 if (BUTTON_CHANGED(1))
422 else if (BUTTON_CHANGED(2))
424 else if (BUTTON_CHANGED(3))
427 if (button && (BUTTON_STATUS(button) &
428 BUTTON_MODIFIER_MASK))
430 waddstr(win, " Modifier(s):");
432 if (BUTTON_STATUS(button) & BUTTON_SHIFT)
433 waddstr(win, " SHIFT");
435 if (BUTTON_STATUS(button) & BUTTON_CONTROL)
436 waddstr(win, " CONTROL");
438 if (BUTTON_STATUS(button) & BUTTON_ALT)
439 waddstr(win, " ALT");
444 wprintw(win, "Button %d: ", button);
447 waddstr(win, "moved: ");
448 else if (MOUSE_WHEEL_UP)
449 waddstr(win, "wheel up: ");
450 else if (MOUSE_WHEEL_DOWN)
451 waddstr(win, "wheel dn: ");
452 else if ((BUTTON_STATUS(button) &
453 BUTTON_ACTION_MASK) == BUTTON_PRESSED)
454 waddstr(win, "pressed: ");
455 else if ((BUTTON_STATUS(button) &
456 BUTTON_ACTION_MASK) == BUTTON_CLICKED)
457 waddstr(win, "clicked: ");
458 else if ((BUTTON_STATUS(button) &
459 BUTTON_ACTION_MASK) == BUTTON_DOUBLE_CLICKED)
460 waddstr(win, "double: ");
462 waddstr(win, "released: ");
464 wprintw(win, "Position: Y: %d X: %d", MOUSE_Y_POS, MOUSE_X_POS);
466 else if (PDC_get_key_modifiers())
468 waddstr(win, " Modifier(s):");
469 if (PDC_get_key_modifiers() & PDC_KEY_MODIFIER_SHIFT)
470 waddstr(win, " SHIFT");
472 if (PDC_get_key_modifiers() & PDC_KEY_MODIFIER_CONTROL)
473 waddstr(win, " CONTROL");
475 if (PDC_get_key_modifiers() & PDC_KEY_MODIFIER_ALT)
476 waddstr(win, " ALT");
478 if (PDC_get_key_modifiers() & PDC_KEY_MODIFIER_NUMLOCK)
479 waddstr(win, " NUMLOCK");
488 wtimeout(win, -1); /* turn off timeout() */
489 curs_set(1); /* turn cursor back on */
493 PDC_save_key_modifiers(FALSE);
494 PDC_return_key_modifiers(FALSE);
497 mvwaddstr(win, 2, 1, "Press some keys for 5 seconds");
498 mvwaddstr(win, 1, 1, "Pressing ^C should do nothing");
502 box(subWin, ACS_VLINE, ACS_HLINE);
504 for (i = 0; i < 5; i++)
506 mvwprintw(subWin, 1, 1, "Time = %d", i);
519 mvwaddstr(win, 2, 1, "Press a key, followed by ENTER");
526 wgetnstr(win, buffer, 3);
531 mvwaddstr(win, 4, 1, "The character should now have been deleted");
538 mvwaddstr(win, 3, 2, "The window should have moved");
540 "This text should have appeared without you pressing a key");
541 mvwaddstr(win, 6, 2, "Enter a number then a string seperated by space");
544 mvwscanw(win, 7, 6, "%d %s", &num, buffer);
545 mvwprintw(win, 8, 6, "String: %s Number: %d", buffer, num);
551 mvwaddstr(win, 3, 2, "Enter a 5 character string: ");
552 wgetnstr(win, buffer, 5);
553 mvwprintw(win, 4, 2, "String: %s", buffer);
557 void outputTest(WINDOW *win)
566 mvwaddstr(win, 1, 1, "You should now have a screen in the upper "
567 "left corner, and this text should have wrapped");
568 waddstr(win,"\nThis text should be down\n");
569 waddstr(win, "and broken into two here ^");
573 wattron(win, A_BOLD);
574 mvwaddstr(win, 1, 1, "A new window will appear with this text in it");
575 mvwaddstr(win, 8, 1, "Press any key to continue");
579 getbegyx(win, by, bx);
581 if (LINES < 24 || COLS < 75)
583 mvwaddstr(win, 5, 1, "Some tests have been skipped as they require a");
584 mvwaddstr(win, 6, 1, "display of at least 24 LINES by 75 COLUMNS");
589 win1 = newwin(10, 50, 14, 25);
600 init_pair(3, COLOR_BLUE, COLOR_WHITE);
601 wbkgd(win1, COLOR_PAIR(3));
605 wbkgd(win1, A_NORMAL);
608 mvwaddstr(win1, 5, 1, "This text should appear; using overlay option");
609 copywin(win, win1, 0, 0, 0, 0, 9, 49, TRUE);
610 box(win1, ACS_VLINE, ACS_HLINE);
617 wattron(win1, A_BLINK);
618 mvwaddstr(win1, 4, 1,
619 "This blinking text should appear in only the second window");
620 wattroff(win1, A_BLINK);
635 mvwaddstr(win, 6, 2, "This line shouldn't appear");
636 mvwaddstr(win, 4, 2, "Only half of the next line is visible");
637 mvwaddstr(win, 5, 2, "Only half of the next line is visible");
642 mvwaddstr(win, 8, 2, "This line also shouldn't appear");
652 waddstr(win, "The next char should be l: ");
656 mvwinsstr(win, 6, 2, "A1B2C3D4E5");
661 mvwaddstr(win, 5, 2, "The lines below should have moved down");
666 wprintw(win, "This is a formatted string in a window: %d %s\n",
668 mvwaddstr(win, 10, 1, "Enter a string: ");
671 wscanw(win, "%s", Buffer);
673 printw("This is a formatted string in stdscr: %d %s\n", 42, "is it");
674 mvaddstr(10, 1, "Enter a string: ");
679 mvwaddstr(win, 1, 1, "The cursor should be in high-visibility mode");
684 mvwaddstr(win, 1, 1, "The cursor should have disappeared");
689 mvwaddstr(win, 1, 1, "The cursor should be normal");
696 mvwaddstr(win, 1, 1, "Colors should change after you press a key");
699 init_pair(1, COLOR_RED, COLOR_WHITE);
704 mvwaddstr(win, 1, 1, "Information About Your Terminal");
705 mvwaddstr(win, 3, 1, termname());
706 mvwaddstr(win, 4, 1, longname());
708 if (termattrs() & A_BLINK)
709 mvwaddstr(win, 5, 1, "This terminal claims to support blinking.");
711 mvwaddstr(win, 5, 1, "This terminal does NOT support blinking.");
713 mvwaddnstr(win, 7, 5, "Have a nice day!ok", 16);
716 mvwinnstr(win, 7, 5, Buffer, 18);
717 mvaddstr(LINES - 2, 10, Buffer);
723 void resizeTest(WINDOW *dummy)
726 int nwidth = 135, nheight = 52;
727 int owidth = COLS, oheight = LINES;
731 resize_term(nheight, nwidth);
736 win1 = newwin(10, 50, 14, 25);
747 init_pair(3, COLOR_BLUE, COLOR_WHITE);
748 wattrset(win1, COLOR_PAIR(3));
753 mvwaddstr(win1, 0, 0, "The screen may now be resized");
754 mvwprintw(win1, 1, 4, "Given size: %d by %d", nwidth, nheight);
755 mvwprintw(win1, 2, 4, "Actual size: %d by %d", COLS, LINES);
761 mvwaddstr(win1, 0, 0, "The screen should now be reset");
762 mvwprintw(win1, 1, 6, "Old size: %d by %d", owidth, oheight);
763 mvwprintw(win1, 2, 6, "Size now: %d by %d", COLS, LINES);
771 #endif /* HAVE_RESIZE */
773 void padTest(WINDOW *dummy)
777 pad = newpad(50, 100);
778 wattron(pad, A_REVERSE);
779 mvwaddstr(pad, 5, 2, "This is a new pad");
782 "The end of this line should be truncated here:except now");
783 mvwaddstr(pad, 11, 1, "This line should not appear.It will now");
786 mvwaddstr(pad, 10, 1, " Press any key to continue");
787 prefresh(pad, 0, 0, 0, 0, 10, 45);
792 spad = subpad(pad, 12, 25, 7, 52);
793 mvwaddstr(spad, 2, 2, "This is a new subpad");
795 prefresh(pad, 0, 0, 0, 0, 15, 75);
800 mvwaddstr(pad, 35, 2, "This is displayed at line 35 in the pad");
801 mvwaddstr(pad, 40, 1, " Press any key to continue");
802 prefresh(pad, 30, 0, 0, 0, 10, 45);
811 void clipboardTest(WINDOW *win)
813 static const char *text =
814 "This string placed in clipboard by PDCurses test program, testcurs.";
819 "This test will display the contents of the system clipboard");
823 scrollok(stdscr, TRUE);
824 i = PDC_getclipboard(&ptr, &length);
828 case PDC_CLIP_ACCESS_ERROR:
829 mvaddstr(3, 1, "There was an error accessing the clipboard");
833 case PDC_CLIP_MEMORY_ERROR:
835 "Unable to allocate memory for clipboard contents");
839 mvaddstr(3, 1, "There was no text in the clipboard");
843 wsetscrreg(stdscr, 0, LINES - 1);
845 mvaddstr(1, 1, "Clipboard contents...");
846 mvprintw(2, 1, "%s\n", ptr);
853 "This test will place the following string in the system clipboard:");
854 mvaddstr(2, 1, text);
856 i = PDC_setclipboard(text, strlen(text));
860 case PDC_CLIP_ACCESS_ERROR:
861 mvaddstr(3, 1, "There was an error accessing the clipboard");
864 case PDC_CLIP_MEMORY_ERROR:
865 mvaddstr(3, 1, "Unable to allocate memory for clipboard contents");
869 mvaddstr(3, 1, "The string was placed in the clipboard successfully");
874 #endif /* HAVE_CLIPBOARD */
876 void acsTest(WINDOW *win)
883 static const char *acs_names[] =
885 "ACS_ULCORNER", "ACS_URCORNER", "ACS_LLCORNER", "ACS_LRCORNER",
886 "ACS_LTEE", "ACS_RTEE", "ACS_TTEE", "ACS_BTEE", "ACS_HLINE",
887 "ACS_VLINE", "ACS_PLUS",
889 "ACS_S1", "ACS_S9", "ACS_DIAMOND", "ACS_CKBOARD", "ACS_DEGREE",
890 "ACS_PLMINUS", "ACS_BULLET",
892 "ACS_LARROW", "ACS_RARROW", "ACS_UARROW", "ACS_DARROW",
893 "ACS_BOARD", "ACS_LANTERN", "ACS_BLOCK"
895 , "ACS_S3", "ACS_S7", "ACS_LEQUAL", "ACS_GEQUAL",
896 "ACS_PI", "ACS_NEQUAL", "ACS_STERLING"
900 chtype acs_values[ACSNUM];
903 cchar_t *wacs_values[] =
905 WACS_ULCORNER, WACS_URCORNER, WACS_LLCORNER, WACS_LRCORNER,
906 WACS_LTEE, WACS_RTEE, WACS_TTEE, WACS_BTEE, WACS_HLINE,
907 WACS_VLINE, WACS_PLUS,
909 WACS_S1, WACS_S9, WACS_DIAMOND, WACS_CKBOARD, WACS_DEGREE,
910 WACS_PLMINUS, WACS_BULLET,
912 WACS_LARROW, WACS_RARROW, WACS_UARROW, WACS_DARROW, WACS_BOARD,
913 WACS_LANTERN, WACS_BLOCK
915 , WACS_S3, WACS_S7, WACS_LEQUAL, WACS_GEQUAL, WACS_PI,
916 WACS_NEQUAL, WACS_STERLING
920 static const wchar_t russian[] = {0x0420, 0x0443, 0x0441, 0x0441,
921 0x043a, 0x0438, 0x0439, L' ', 0x044f, 0x0437, 0x044b, 0x043a, 0};
923 static const wchar_t greek[] = {0x0395, 0x03bb, 0x03bb, 0x03b7,
924 0x03bd, 0x03b9, 0x03ba, 0x03ac, 0};
926 static const wchar_t georgian[] = {0x10e5, 0x10d0, 0x10e0, 0x10d7,
927 0x10e3, 0x10da, 0x10d8, L' ', 0x10d4, 0x10dc, 0x10d0, 0};
930 int i, tmarg = (LINES - 22) / 2;
933 mvaddstr(tmarg, (COLS - 23) / 2, "Alternate Character Set");
938 #define A(b,c) acs_values[b] = ACS_##c
940 A(0,ULCORNER); A(1,URCORNER); A(2,LLCORNER); A(3,LRCORNER);
941 A(4,LTEE); A(5,RTEE); A(6,TTEE); A(7,BTEE);
942 A(8,HLINE); A(9,VLINE); A(10,PLUS); A(11,S1);
943 A(12,S9); A(13,DIAMOND); A(14,CKBOARD); A(15,DEGREE);
945 A(16,PLMINUS); A(17,BULLET); A(18,LARROW); A(19,RARROW);
946 A(20,UARROW); A(21,DARROW); A(22,BOARD); A(23,LANTERN);
949 A(25,S3); A(26,S7); A(27,LEQUAL); A(28,GEQUAL);
950 A(29,PI); A(30,NEQUAL); A(31,STERLING);
955 for (i = 0; i < ACSNUM; i++)
957 move((i % 8) * 2 + tmarg, (i / 8) * (COLS / 4) + (COLS / 8 - 7));
958 addch(acs_values[i]);
959 printw(" %s", acs_names[i]);
962 mvaddstr(tmarg + 18, 3, "Press any key to continue");
969 mvaddstr(tmarg - 3, (COLS - 28) / 2, "Wide Alternate Character Set");
972 for (i = 0; i < ACSNUM; i++)
974 move((i % 8) * 2 + tmarg, (i / 8) * (COLS / 4) + (COLS / 8 - 7));
975 add_wch(wacs_values[i]);
976 printw(" W%s", acs_names[i]);
979 /* Spanish, Russian, Greek, Georgian */
981 mvaddwstr(tmarg + 16, COLS / 8 - 5, L"Espa\xf1ol");
982 mvaddwstr(tmarg + 16, 3 * (COLS / 8) - 5, russian);
983 mvaddwstr(tmarg + 16, 5 * (COLS / 8) - 5, greek);
984 mvaddwstr(tmarg + 16, 7 * (COLS / 8) - 5, georgian);
986 mvaddstr(tmarg + 18, 3, "Press any key to continue");
992 void colorTest(WINDOW *win)
994 static const short colors[] =
996 COLOR_BLACK, COLOR_RED, COLOR_GREEN, COLOR_BLUE,
997 COLOR_CYAN, COLOR_MAGENTA, COLOR_YELLOW, COLOR_WHITE
1000 static const char *colornames[] =
1002 "COLOR_BLACK", "COLOR_RED", "COLOR_GREEN", "COLOR_BLUE",
1003 "COLOR_CYAN", "COLOR_MAGENTA", "COLOR_YELLOW", "COLOR_WHITE"
1006 chtype fill = ACS_BLOCK;
1008 int i, j, tmarg, col1, col2, col3;
1013 tmarg = (LINES - 19) / 2;
1014 col1 = (COLS - 60) / 2;
1019 mvaddstr(tmarg, (COLS - 22) / 2, "Color Attribute Macros");
1022 mvaddstr(tmarg + 3, col2 + 4, "A_NORMAL");
1023 mvaddstr(tmarg + 3, col3 + 5, "A_BOLD");
1025 for (i = 0; i < 8; i++)
1027 init_pair(i + 4, colors[i], COLOR_BLACK);
1029 mvaddstr(tmarg + i + 5, col1, colornames[i]);
1031 for (j = 0; j < 16; j++)
1033 mvaddch(tmarg + i + 5, col2 + j, fill | COLOR_PAIR(i + 4));
1034 mvaddch(tmarg + i + 5, col3 + j, fill | COLOR_PAIR(i + 4) | A_BOLD);
1038 mvprintw(tmarg + 15, col1, "COLORS = %d", COLORS);
1039 mvprintw(tmarg + 16, col1, "COLOR_PAIRS = %d", COLOR_PAIRS);
1041 mvaddstr(tmarg + 19, 3, "Press any key to continue");
1044 if (can_change_color())
1048 short red, green, blue;
1051 int MAXCOL = (COLORS >= 16) ? 16 : 8;
1056 for (i = 0; i < MAXCOL; i++)
1057 color_content(i, &(orgcolors[i].red),
1058 &(orgcolors[i].green),
1059 &(orgcolors[i].blue));
1062 mvaddstr(tmarg, (COLS - 22) / 2, " init_color() Example ");
1067 for (i = 0; i < 8; i++)
1069 init_color(colors[i], i * 125, 0, i * 125);
1072 init_color(colors[i] + 8, 0, i * 125, 0);
1075 mvaddstr(tmarg + 19, 3, "Press any key to continue");
1078 for (i = 0; i < MAXCOL; i++)
1079 init_color(i, orgcolors[i].red,
1087 void wideTest(WINDOW *win)
1093 mvaddstr(1, (COLS - 25) / 2, "Wide Character Input Test");
1096 mvaddstr(4, 1, "Enter a string: ");
1100 get_wstr((wint_t *)tmp);
1101 addstr("\n\n String:\n\n ");
1103 addstr("\n\n\n Hex:\n\n ");
1105 for (i = 0; i < wcslen(tmp); i++)
1107 printw("%04x ", tmp[i]);
1108 addnwstr(tmp + i, 1);
1118 void display_menu(int old_option, int new_option)
1120 int lmarg = (COLS - 14) / 2,
1121 tmarg = (LINES - (MAX_OPTIONS + 2)) / 2;
1123 if (old_option == -1)
1128 mvaddstr(tmarg - 3, lmarg - 5, "PDCurses Test Program");
1131 for (i = 0; i < MAX_OPTIONS; i++)
1132 mvaddstr(tmarg + i, lmarg, command[i].text);
1135 mvaddstr(tmarg + old_option, lmarg, command[old_option].text);
1138 mvaddstr(tmarg + new_option, lmarg, command[new_option].text);
1141 mvaddstr(tmarg + MAX_OPTIONS + 2, lmarg - 23,
1142 "Use Up and Down Arrows to select - Enter to run - Q to quit");