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