home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 414_02 / demos / testcurs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-17  |  12.0 KB  |  537 lines

  1. /*
  2.  *
  3.  * This is a test program for the PDCurses screen package for IBM PC type
  4.  * machines.
  5.  * This program was written by John Burnell (johnb@kea.am.dsir.govt.nz)
  6.  *
  7.  */
  8.  
  9. #ifdef PDCDEBUG
  10. char *rcsid_testcurs = "$Header: C:\CURSES\demos\RCS\testcurs.c 2.1 1993/06/18 20:24:11 MH Rel MH $";
  11. #endif
  12.  
  13. #include <stdio.h>
  14. #include <curses.h>
  15.  
  16. #ifdef __STDC__
  17. void inputTest (WINDOW *);
  18. void scrollTest (WINDOW *);
  19. void introTest (WINDOW *);
  20. int initTest (WINDOW **);
  21. void outputTest (WINDOW *);
  22. void padTest (WINDOW *);
  23. void resizeTest (WINDOW *);
  24. void display_menu(int,int);
  25. #else
  26. void inputTest ();
  27. void scrollTest ();
  28. void introTest ();
  29. int initTest ();
  30. void outputTest ();
  31. void padTest ();
  32. void resizeTest ();
  33. void display_menu();
  34. #endif
  35.  
  36. struct commands
  37. {
  38.  char *text;
  39. #ifdef __STDC__
  40.  void (*function)(WINDOW *);
  41. #else
  42.  void (*function)();
  43. #endif
  44. };
  45. typedef struct commands COMMAND;
  46. #define MAX_OPTIONS 6
  47. COMMAND command[MAX_OPTIONS] =
  48. {
  49.  {"Intro Test",introTest},
  50.  {"Pad Test",padTest},
  51.  {"Resize Test",resizeTest},
  52.  {"Scroll Test",scrollTest},
  53.  {"Input Test",inputTest},
  54.  {"Output Test",outputTest}
  55. };
  56.  
  57. int     width, height;
  58.  
  59. main()
  60. {
  61. WINDOW  *win;
  62. int key,old_option=(-1),new_option=0;
  63. bool quit=FALSE;
  64.  
  65. #ifdef PDCDEBUG
  66.     PDC_debug("testcurs started\n");
  67. #endif
  68.     if (!initTest (&win)) {return 1;}
  69.  
  70. #ifdef A_COLOR
  71.     if (has_colors())
  72.       {
  73.        init_pair(1,COLOR_WHITE,COLOR_BLUE);
  74.        wattrset(win, COLOR_PAIR(1));
  75.       }
  76.     else
  77.        wattrset(win, A_REVERSE);
  78. #else
  79.     wattrset(win, A_REVERSE);
  80. #endif
  81.  
  82.     erase();
  83.     display_menu(old_option,new_option);
  84.     while(1)
  85.       {
  86.        noecho();
  87.        keypad(stdscr,TRUE);
  88.        raw();
  89.        key = getch();
  90.        switch(key)
  91.          {
  92.           case 10:
  93.           case 13:
  94.           case KEY_ENTER:
  95.                          erase();
  96.                          refresh();
  97.                          (*command[new_option].function)(win);
  98.                          erase();
  99.                          display_menu(old_option,new_option);
  100.                          break;
  101.           case KEY_UP:
  102.                          new_option = (new_option == 0) ? new_option : new_option-1;
  103.                          display_menu(old_option,new_option);
  104.                          break;
  105.           case KEY_DOWN:
  106.                          new_option = (new_option == MAX_OPTIONS-1) ? new_option : new_option+1;
  107.                          display_menu(old_option,new_option);
  108.                          break;
  109.           case 'Q':
  110.           case 'q':
  111.                          quit = TRUE;
  112.                          break;
  113.           default:       break;
  114.          }
  115.        if (quit == TRUE)
  116.           break;
  117.       }
  118.  
  119.     delwin (win);
  120.  
  121.     endwin();
  122.     return 0;
  123. }
  124. #ifdef __STDC__
  125. void Continue (WINDOW *win)
  126. #else
  127. void Continue (win)
  128. WINDOW *win;
  129. #endif
  130. {
  131.     wmove(win, 10, 1);
  132.     wclrtoeol(win);
  133.     mvwaddstr(win, 10, 1, " Press any key to continue");
  134.     wrefresh(win);
  135.     raw();
  136.     wgetch(win);
  137. }
  138. #ifdef __STDC_
  139. int initTest (WINDOW **win)
  140. #else
  141. int initTest (win)
  142. WINDOW **win;
  143. #endif
  144. {
  145. #ifdef PDCDEBUG
  146.     PDC_debug("initTest called\n");
  147. #endif
  148.     initscr();
  149. #ifdef PDCDEBUG
  150.     PDC_debug("after initscr()\n");
  151. #endif
  152. #ifdef A_COLOR
  153.     if (has_colors())
  154.        start_color();
  155. #endif
  156.     width  = 60;
  157.     height = 13;                /* Create a drawing window */
  158.     *win = newwin(height, width, (LINES-height)/2, (COLS-width)/2);
  159.     if(*win == NULL)
  160.     {   endwin();
  161.         return 1;
  162.     }
  163. }
  164. #ifdef __STDC__
  165. void introTest (WINDOW *win)
  166. #else
  167. void introTest (win)
  168. WINDOW *win;
  169. #endif
  170. {
  171.     beep ();
  172.     werase(win);
  173.  
  174.     box(win, ACS_VLINE, ACS_HLINE);
  175.     wrefresh(win);
  176.     cbreak ();
  177.     mvwaddstr(win, 1, 1, "You should have rectangle in the middle of the screen");
  178.     mvwaddstr(win, 2, 1, "You should have heard a beep");
  179.     Continue(win);
  180.     return;
  181. }
  182. #ifdef __STDC__
  183. void scrollTest (WINDOW *win)
  184. #else
  185. void scrollTest (win)
  186. WINDOW *win;
  187. #endif
  188. {
  189.     int i;
  190.     int OldX, OldY;
  191.     char *Message = "The window will now scroll slowly";
  192.  
  193.     mvwprintw (win, height - 2, 1, Message);
  194.     wrefresh (win);
  195.     scrollok(win, TRUE);
  196.     for (i = 1; i <= height; i++) {
  197.       delay_output (250);
  198.       scroll(win);
  199.       wrefresh (win);
  200.     };
  201.  
  202.     getmaxyx (win, OldY, OldX);
  203.     mvwprintw (win, 6, 1, "The top of the window will scroll");
  204.     wmove (win, 1, 1);
  205.     wsetscrreg (win, 0, 4);
  206.     box(win, ACS_VLINE, ACS_HLINE);
  207.     wrefresh (win);
  208.     for (i = 1; i <= 5; i++) {
  209.       delay_output (500);
  210.       scroll(win);
  211.       wrefresh (win);
  212.     };
  213.     wsetscrreg (win, 0, --OldY);
  214.  
  215. }
  216. #ifdef __STDC__
  217. void inputTest (WINDOW *win)
  218. #else
  219. void inputTest (win)
  220. WINDOW *win;
  221. #endif
  222. {
  223.     int w, h, bx, by, sw, sh, i, c,num;
  224.     char buffer [80];
  225.     WINDOW *subWin;
  226.     wclear (win);
  227.  
  228.     w  = win->_maxx;
  229.     h  = win->_maxy;
  230.     bx = win->_begx;
  231.     by = win->_begy;
  232.     sw = w / 3;
  233.     sh = h / 3;
  234.     if((subWin = subwin(win, sh, sw, by + h - sh - 2, bx + w - sw - 2)) == NULL)
  235.         return;
  236.  
  237. #ifdef A_COLOR
  238.     if (has_colors())
  239.       {
  240.        init_pair(2,COLOR_CYAN,COLOR_BLUE);
  241.        wattrset(subWin, COLOR_PAIR(2) | A_BOLD);
  242.       }
  243.     else
  244.        wattrset(subWin, A_BOLD);
  245. #else
  246.     wattrset(subWin, A_BOLD);
  247. #endif
  248.     box(subWin, ACS_VLINE, ACS_HLINE);
  249.     wrefresh(win);
  250.  
  251.     nocbreak();
  252.     mvwaddstr(win, 2, 1, "Press some keys for 5 seconds");
  253.     mvwaddstr(win, 1, 1, "Pressing ^C should do nothing");
  254.     wrefresh(win);
  255.  
  256.     for (i = 0; i < 5; i++) {
  257.       werase (subWin);
  258.       box(subWin, ACS_VLINE, ACS_HLINE);
  259.       mvwprintw (subWin, 1, 1, "Time = %d", i);
  260.       wrefresh(subWin);
  261.       delay_output(1000);
  262.       flushinp();
  263.     }
  264.  
  265.     delwin (subWin);
  266.     werase(win);
  267.     flash();
  268.     wrefresh(win);
  269.     delay_output(500);
  270.  
  271.     mvwaddstr(win, 2, 1, "Press a key, followed by ENTER");
  272.     wmove(win, 9, 10);
  273.     wrefresh(win);
  274.     echo();
  275.     noraw();
  276.     wgetch(win);
  277.     flushinp();
  278.  
  279.     wmove(win, 9, 10);
  280.     wdelch(win);
  281.     mvwaddstr(win, 4, 1, "The character should now have been deleted");
  282.     Continue(win);
  283.  
  284.     wclear (win);
  285.     mvwaddstr(win, 2, 1, "Press a function key or an arrow key");
  286.     wrefresh(win);
  287.     keypad(win, TRUE);
  288.     raw();
  289.     c = wgetch(win);
  290.  
  291.     nodelay(win, TRUE);
  292.     wgetch(win);
  293.     nodelay(win, FALSE);
  294.  
  295.     refresh();
  296.     wclear (win);
  297.     mvwaddstr(win, 3, 2, "The window should have moved");
  298.     mvwaddstr(win, 4, 2, "This text should have appeared without you pressing a key");
  299.     mvwprintw(win, 2, 2, "Keycode = %d", c);
  300.     mvwaddstr(win, 6, 2, "Enter a number then a string seperated by space");
  301.     echo();
  302.     noraw();
  303.     mvwscanw(win, 7, 6, "%d %s", &num,buffer);
  304.     mvwprintw(win, 8, 6, "String: %s Number: %d", buffer,num);
  305.     Continue(win);
  306. }
  307. #ifdef __STDC__
  308. void outputTest (WINDOW *win)
  309. #else
  310. void outputTest (win)
  311. WINDOW *win;
  312. #endif
  313. {
  314.     WINDOW *win1;
  315.     char Buffer [80];
  316.     chtype ch;
  317.  
  318.     traceon();
  319.     nl ();
  320.     wclear (win);
  321.     mvwaddstr(win, 1, 1, "You should now have a screen in the upper left corner, and this text should have wrapped");
  322.     mvwin(win, 2, 1);
  323.     Continue(win);
  324.  
  325.     wclear(win);
  326.     mvwaddstr(win, 1, 1, "A new window will appear with this text in it");
  327.     mvwaddstr(win, 8, 1, "Press any key to continue");
  328.     wrefresh(win);
  329.     wgetch(win);
  330.  
  331.     win1 = newwin(10, 50, 15, 25);
  332.     if(win1 == NULL)
  333.     {   endwin();
  334.         return;
  335.     }
  336. #ifdef A_COLOR
  337.     if (has_colors())
  338.       {
  339.        init_pair(3,COLOR_BLUE,COLOR_WHITE);
  340.        wattrset(win1, COLOR_PAIR(3));
  341.       }
  342.     else
  343.        wattrset(win1, A_NORMAL);
  344. #else
  345.     wattrset(win1, A_NORMAL);
  346. #endif
  347.     wclear (win1);
  348.     mvwaddstr(win1, 5, 1, "This text should appear; using overlay option");
  349.     copywin(win, win1,0,0,0,0,10,50,TRUE);
  350.  
  351. #ifdef UNIX
  352.     box(win1,ACS_VLINE,ACS_HLINE);
  353. #else
  354.     box(win1,0xb3,0xc4);
  355. #endif
  356.     wmove(win1, 8, 26);
  357.     wrefresh(win1);
  358.     wgetch(win1);
  359.  
  360.     wclear(win1);
  361.     wattron(win1, A_BLINK);
  362.     mvwaddstr(win1, 4, 1, "This blinking text should appear in only the second window");
  363.     wattroff(win1, A_BLINK);
  364.     wrefresh(win1);
  365.     wgetch(win1);
  366.     delwin(win1);
  367.  
  368.     wclear(win);
  369.     wrefresh(win);
  370.     mvwaddstr(win, 6, 2, "This line shouldn't appear");
  371.     mvwaddstr(win, 4, 2, "Only half of the next line is visible");
  372.     mvwaddstr(win, 5, 2, "Only half of the next line is visible");
  373.     wmove(win, 6, 1);
  374.     wclrtobot (win);
  375.     wmove(win, 5, 20);
  376.     wclrtoeol (win);
  377.     mvwaddstr(win, 8, 2, "This line also shouldn't appear");
  378.     wmove(win, 8, 1);
  379.     wdeleteln(win);
  380.     Continue(win);
  381.     traceoff();
  382.  
  383.     wmove (win, 5, 9);
  384.     ch = winch (win);
  385.  
  386.     wclear(win);
  387.     wmove (win, 6, 2);
  388.     waddstr (win, "The next char should be l:  ");
  389.     winsch (win, ch);
  390.     Continue(win);
  391.  
  392.     wmove(win, 5, 1);
  393.     winsertln (win);
  394.     mvwaddstr(win, 5, 2, "The lines below should have moved down");
  395.     Continue(win);
  396.  
  397.     wclear(win);
  398.     wmove(win, 2, 2);
  399.     wprintw(win, "This is a formatted string in a window: %d %s\n", 42, "is it");
  400.     mvwaddstr(win, 10, 1, "Enter a string: ");
  401.     wrefresh(win);
  402.     noraw();
  403.     echo();
  404.     wscanw (win, "%s", Buffer);
  405.  
  406.     wclear(win);
  407.     mvwaddstr(win, 10, 1, "Enter a string");
  408.     wrefresh(win);
  409.     clear();
  410.     move(0,0);
  411.     printw("This is a formatted string in stdscr: %d %s\n", 42, "is it");
  412.     mvaddstr(10, 1, "Enter a string: ");
  413.     refresh();
  414.     noraw();
  415.     echo();
  416.     scanw ("%s", Buffer);
  417.  
  418.     wclear(win);
  419.     curs_set(2);
  420.     mvwaddstr(win, 1, 1, "The cursor should appear as a block");
  421.     Continue(win);
  422.  
  423.     wclear(win);
  424.     curs_set(0);
  425.     mvwaddstr(win, 1, 1, "The cursor should have disappeared");
  426.     Continue(win);
  427.  
  428.     wclear(win);
  429.     curs_set(1);
  430.     mvwaddstr(win, 1, 1, "The cursor should be an underline");
  431.     Continue(win);
  432. }
  433.  
  434. #ifdef __STDC__
  435. void resizeTest(WINDOW *dummy)
  436. #else
  437. void resizeTest(dummy)
  438. WINDOW *dummy;
  439. #endif
  440. {
  441.     WINDOW *win1;
  442.     char Buffer [80];
  443.     chtype ch;
  444.  
  445.     savetty ();
  446.  
  447.     clear();
  448.     refresh();
  449. #ifdef __PDCURSES__
  450.     resize(50);
  451. #endif
  452.  
  453.  
  454.     win1 = newwin(11, 50, 14, 25);
  455.     if(win1 == NULL)
  456.     {   endwin();
  457.         return;
  458.     }
  459. #ifdef A_COLOR
  460.     if (has_colors())
  461.       {
  462.        init_pair(3,COLOR_BLUE,COLOR_WHITE);
  463.        wattrset(win1, COLOR_PAIR(3));
  464.       }
  465. #endif
  466.     wclear (win1);
  467.  
  468.     mvwaddstr(win1, 1, 1, "The screen may now have 50 lines");
  469.     Continue(win1);
  470.  
  471.     resetty ();
  472.  
  473.     wclear (win1);
  474.     mvwaddstr(win1, 1, 1, "The screen should now be reset");
  475.     Continue(win1);
  476.  
  477.     delwin(win1);
  478.  
  479.     clear();
  480.     refresh();
  481.  
  482. }
  483. #ifdef __STDC__
  484. void padTest(WINDOW *dummy)
  485. #else
  486. void padTest(dummy)
  487. WINDOW *dummy;
  488. #endif
  489. {
  490. WINDOW *pad;
  491.  
  492.  pad = newpad(50,100);
  493.  mvwaddstr(pad, 5, 2, "This is a new pad");
  494.  mvwaddstr(pad, 8, 0, "The end of this line should be truncated here:abcd");
  495.  mvwaddstr(pad,11, 1, "This line should not appear.");
  496.  wmove(pad, 10, 1);
  497.  wclrtoeol(pad);
  498.  mvwaddstr(pad, 10, 1, " Press any key to continue");
  499.  prefresh(pad,0,0,0,0,10,45);
  500.  keypad(pad, TRUE);
  501.  raw();
  502.  wgetch(pad);
  503.  
  504.  mvwaddstr(pad, 35, 2, "This is displayed at line 35 in the pad");
  505.  mvwaddstr(pad, 40, 1, " Press any key to continue");
  506.  prefresh(pad,30,0,0,0,10,45);
  507.  keypad(pad, TRUE);
  508.  raw();
  509.  wgetch(pad);
  510.  
  511.  delwin(pad);
  512. }
  513.  
  514. #ifdef __STDC__
  515. void display_menu(int old_option,int new_option)
  516. #else
  517. void display_menu(old_option,new_option)
  518. int old_option,new_option;
  519. #endif
  520. {
  521.  register int i;
  522.  
  523.  attrset(A_NORMAL);
  524.  mvaddstr(3,20,"PDCurses Test Program");
  525.  
  526.  for (i=0;i<MAX_OPTIONS;i++)
  527.     mvaddstr(5+i,25,command[i].text);
  528.  if (old_option != (-1))
  529.     mvaddstr(5+old_option,25,command[old_option].text);
  530.  attrset(A_REVERSE);
  531.  mvaddstr(5+new_option,25,command[new_option].text);
  532.  attrset(A_NORMAL);
  533.  mvaddstr(13,3,"Use Up and Down Arrows to select - Enter to run - Q to quit");
  534.  refresh();
  535. }
  536.  
  537.