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