]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/testcurs.c
ff776570e2e8ff38ea36acd2084ceb4feef0e7e8
[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.19 1999/02/14 00:42:28 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     NCURSES_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     num = 0;
363     *buffer = 0;
364     mvwscanw(win, 7, 6, "%d %s", &num,buffer);
365     mvwprintw(win, 8, 6, "String: %s Number: %d", buffer,num);
366     Continue(win);
367 }
368
369 static void
370 outputTest (WINDOW *win)
371 {
372     WINDOW *win1;
373     char Buffer [80];
374     chtype ch;
375     int by, bx;
376
377     nl ();
378     wclear (win);
379     mvwaddstr(win, 1, 1, "You should now have a screen in the upper left corner, and this text should have wrapped");
380     mvwin(win, 2, 1);
381     waddstr(win,"\nThis text should be down\n");
382     waddstr(win,  "and broken into two here ^");
383     Continue(win);
384
385     wclear(win);
386     wattron(win, A_BOLD);
387     mvwaddstr(win, 1, 1, "A new window will appear with this text in it");
388     mvwaddstr(win, 8, 1, "Press any key to continue");
389     wrefresh(win);
390     wgetch(win);
391
392     getbegyx(win, by, bx);
393
394     if (LINES < 24 || COLS < 75) {
395        mvwaddstr(win, 5, 1, "Some tests have been skipped as they require a");
396        mvwaddstr(win, 6, 1, "display of at least 24 LINES by 75 COLUMNS");
397        Continue(win);
398     } else {
399       win1 = newwin(10, 50, 14, 25);
400       if(win1 == NULL)
401       {
402           endwin();
403           return;
404       }
405 #ifdef A_COLOR
406       if (has_colors())
407         {
408          init_pair(3,COLOR_BLUE,COLOR_WHITE);
409          wbkgd(win1, COLOR_PAIR(3));
410         }
411       else
412         wbkgd(win1, A_NORMAL);
413 #else
414       wbkgd(win1, A_NORMAL);
415 #endif
416       wclear (win1);
417       mvwaddstr(win1, 5, 1, "This text should appear; using overlay option");
418       copywin(win, win1,0,0,0,0,9,49,TRUE);
419
420 #if defined(PDCURSES) && !defined(XCURSES)
421       box(win1,0xb3,0xc4);
422 #else
423       box(win1,ACS_VLINE,ACS_HLINE);
424 #endif
425       wmove(win1, 8, 26);
426       wrefresh(win1);
427       wgetch(win1);
428
429       wclear(win1);
430       wattron(win1, A_BLINK);
431       mvwaddstr(win1, 4, 1, "This blinking text should appear in only the second window");
432       wattroff(win1, A_BLINK);
433       mvwin(win1, by, bx);
434       overlay(win, win1);
435       mvwin(win1,14,25);
436       wmove(win1, 8, 26);
437       wrefresh(win1);
438       wgetch(win1);
439       delwin(win1);
440     }
441
442     clear();
443     wclear(win);
444     wrefresh(win);
445     mvwaddstr(win, 6, 2, "This line shouldn't appear");
446     mvwaddstr(win, 4, 2, "Only half of the next line is visible");
447     mvwaddstr(win, 5, 2, "Only half of the next line is visible");
448     wmove(win, 6, 1);
449     wclrtobot (win);
450     wmove(win, 5, 20);
451     wclrtoeol (win);
452     mvwaddstr(win, 8, 2, "This line also shouldn't appear");
453     wmove(win, 8, 1);
454     wdeleteln(win);
455     Continue(win);
456
457     wmove (win, 5, 9);
458     ch = winch (win);
459
460     wclear(win);
461     wmove (win, 6, 2);
462     waddstr (win, "The next char should be l:  ");
463     winsch (win, ch);
464     Continue(win);
465
466     mvwinsstr( win, 6, 2, "A1B2C3D4E5" );
467     Continue(win);
468
469     wmove(win, 5, 1);
470     winsertln (win);
471     mvwaddstr(win, 5, 2, "The lines below should have moved down");
472     Continue(win);
473
474     wclear(win);
475     wmove(win, 2, 2);
476     wprintw(win, "This is a formatted string in a window: %d %s\n", 42, "is it");
477     mvwaddstr(win, 10, 1, "Enter a string: ");
478     wrefresh(win);
479     noraw();
480     echo();
481     *Buffer = 0;
482     wscanw (win, "%s", Buffer);
483
484     printw("This is a formatted string in stdscr: %d %s\n", 42, "is it");
485     mvaddstr(10, 1, "Enter a string: ");
486     *Buffer = 0;
487     scanw ("%s", Buffer);
488
489     if (tigetstr("cvvis") != 0) {
490         wclear(win);
491         curs_set(2);
492         mvwaddstr(win, 1, 1, "The cursor should appear as a block (visible)");
493         Continue(win);
494     }
495
496     if (tigetstr("civis") != 0) {
497         wclear(win);
498         curs_set(0);
499         mvwaddstr(win, 1, 1, "The cursor should have disappeared (invisible)");
500         Continue(win);
501     }
502
503     if (tigetstr("cnorm") != 0) {
504         wclear(win);
505         curs_set(1);
506         mvwaddstr(win, 1, 1, "The cursor should be an underline (normal)");
507         Continue(win);
508     }
509
510 #ifdef A_COLOR
511     if ( has_colors() ) {
512        wclear(win);
513        mvwaddstr(win, 1, 1, "Colors should change after you press a key");
514        Continue(win);
515        init_pair(1, COLOR_RED, COLOR_WHITE);
516        wrefresh(win);
517     }
518 #endif
519
520     werase(win);
521     mvwaddstr(win, 1, 1, "Information About Your Terminal");
522     mvwaddstr(win, 3, 1, termname());
523     mvwaddstr(win, 4, 1, longname());
524     if ( termattrs() & A_BLINK )
525        mvwaddstr(win,5, 1, "This terminal supports blinking.");
526     else
527        mvwaddstr(win,5, 1, "This terminal does NOT support blinking.");
528
529     mvwaddnstr( win, 7,5, "Have a nice day!ok", 16 );
530     wrefresh(win);
531
532     mvwinnstr( win, 7,5, Buffer, 18 );
533     mvaddstr( LINES-2, 10, Buffer );
534     refresh();
535     Continue(win);
536 }
537
538 #if defined(PDCURSES) && !defined(XCURSES)
539 static void
540 resizeTest(WINDOW *dummy GCC_UNUSED)
541 {
542     WINDOW *win1;
543
544     savetty ();
545
546     clear();
547     refresh();
548 #  if defined(OS2)
549     resize_term(50,120);
550 #  else
551     resize_term(50,80);
552 #  endif
553
554     win1 = newwin(10, 50, 14, 25);
555     if(win1 == NULL)
556     {   endwin();
557         return;
558     }
559 #ifdef A_COLOR
560     if (has_colors())
561       {
562        init_pair(3,COLOR_BLUE,COLOR_WHITE);
563        wattrset(win1, COLOR_PAIR(3));
564       }
565 #endif
566     wclear (win1);
567
568     mvwaddstr(win1, 1, 1, "The screen may now have 50 lines");
569     Continue(win1);
570
571     wclear (win1);
572     resetty ();
573
574     mvwaddstr(win1, 1, 1, "The screen should now be reset");
575     Continue(win1);
576
577     delwin(win1);
578
579     clear();
580     refresh();
581
582 }
583 #endif
584
585 static void
586 padTest(WINDOW *dummy GCC_UNUSED)
587 {
588 WINDOW *pad, *spad;
589
590  pad = newpad(50,100);
591  wattron(pad, A_REVERSE);
592  mvwaddstr(pad, 5, 2, "This is a new pad");
593  wattrset(pad,A_NORMAL);
594  mvwaddstr(pad, 8, 0, "The end of this line should be truncated here:except  now");
595  mvwaddstr(pad,11, 1, "This line should not appear.It will now");
596  wmove(pad, 10, 1);
597  wclrtoeol(pad);
598  mvwaddstr(pad, 10, 1, " Press any key to continue");
599  prefresh(pad,0,0,0,0,10,45);
600  keypad(pad, TRUE);
601  raw();
602  wgetch(pad);
603
604  spad = subpad(pad,12,25,6,52);
605  mvwaddstr(spad, 2, 2, "This is a new subpad");
606  box(spad,0,0);
607  prefresh(pad,0,0,0,0,15,75);
608  keypad(pad, TRUE);
609  raw();
610  wgetch(pad);
611
612  mvwaddstr(pad, 35, 2, "This is displayed at line 35 in the pad");
613  mvwaddstr(pad, 40, 1, " Press any key to continue");
614  prefresh(pad,30,0,0,0,10,45);
615  keypad(pad, TRUE);
616  raw();
617  wgetch(pad);
618
619  delwin(pad);
620 }
621
622 static void
623 display_menu(int old_option,int new_option)
624 {
625  register size_t i;
626
627  attrset(A_NORMAL);
628  mvaddstr(3,20,"PDCurses Test Program");
629
630  for (i=0;i<MAX_OPTIONS;i++)
631     mvaddstr(5+i,25,command[i].text);
632  if (old_option != (-1))
633     mvaddstr(5+old_option,25,command[old_option].text);
634  attrset(A_REVERSE);
635  mvaddstr(5+new_option,25,command[new_option].text);
636  attrset(A_NORMAL);
637  mvaddstr(13,3,"Use Up and Down Arrows to select - Enter to run - Q to quit");
638  refresh();
639 }
640