]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/inchs.c
ncurses 6.2 - patch 20210323
[ncurses.git] / test / inchs.c
1 /****************************************************************************
2  * Copyright 2019,2020 Thomas E. Dickey                                     *
3  * Copyright 2007-2012,2017 Free Software Foundation, Inc.                  *
4  *                                                                          *
5  * Permission is hereby granted, free of charge, to any person obtaining a  *
6  * copy of this software and associated documentation files (the            *
7  * "Software"), to deal in the Software without restriction, including      *
8  * without limitation the rights to use, copy, modify, merge, publish,      *
9  * distribute, distribute with modifications, sublicense, and/or sell       *
10  * copies of the Software, and to permit persons to whom the Software is    *
11  * furnished to do so, subject to the following conditions:                 *
12  *                                                                          *
13  * The above copyright notice and this permission notice shall be included  *
14  * in all copies or substantial portions of the Software.                   *
15  *                                                                          *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
19  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
21  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
23  *                                                                          *
24  * Except as contained in this notice, the name(s) of the above copyright   *
25  * holders shall not be used in advertising or otherwise to promote the     *
26  * sale, use or other dealings in this Software without prior written       *
27  * authorization.                                                           *
28  ****************************************************************************/
29 /*
30  * $Id: inchs.c,v 1.18 2020/02/02 23:34:34 tom Exp $
31  *
32  * Author: Thomas E Dickey
33  */
34 /*
35        chtype inch(void);
36        chtype winch(WINDOW *win);
37        chtype mvinch(int y, int x);
38        chtype mvwinch(WINDOW *win, int y, int x);
39        int inchstr(chtype *chstr);
40        int inchnstr(chtype *chstr, int n);
41        int winchstr(WINDOW *win, chtype *chstr);
42        int winchnstr(WINDOW *win, chtype *chstr, int n);
43        int mvinchstr(int y, int x, chtype *chstr);
44        int mvinchnstr(int y, int x, chtype *chstr, int n);
45        int mvwinchstr(WINDOW *win, int y, int x, chtype *chstr);
46        int mvwinchnstr(WINDOW *win, int y, int x, chtype *chstr, int n);
47 */
48
49 #include <test.priv.h>
50 #include <popup_msg.h>
51
52 #define BASE_Y 7
53 #define MAX_COLS 1024
54
55 static void
56 failed(const char *s)
57 {
58     int save = errno;
59     endwin();
60     errno = save;
61     perror(s);
62     ExitProgram(EXIT_FAILURE);
63 }
64
65 static bool
66 Quit(int ch)
67 {
68     return (ch == ERR || ch == 'q' || ch == QUIT || ch == ESCAPE);
69 }
70
71 static int
72 test_inchs(int level, char **argv, WINDOW *chrwin, WINDOW *strwin)
73 {
74     static const char *help[] =
75     {
76         "Test input from screen using inch(), etc., in a moveable viewport.",
77         "",
78         "Commands:",
79         " ESC/^Q                   - quit",
80         " h,j,k,l (and arrow-keys) - move viewport",
81         " w                        - recur to new window",
82         "                            for next input file",
83         0
84     };
85     WINDOW *txtbox = 0;
86     WINDOW *txtwin = 0;
87     FILE *fp;
88     int ch, j;
89     int txt_x = 0, txt_y = 0;
90     int base_y;
91     chtype text[MAX_COLS];
92
93     if (argv[level] == 0) {
94         beep();
95         return FALSE;
96     }
97
98     if (level > 1) {
99         txtbox = newwin(LINES - BASE_Y, COLS - level, BASE_Y, level);
100         box(txtbox, 0, 0);
101         wnoutrefresh(txtbox);
102
103         txtwin = derwin(txtbox,
104                         getmaxy(txtbox) - 2,
105                         getmaxx(txtbox) - 2,
106                         1, 1);
107         base_y = 0;
108     } else {
109         txtwin = stdscr;
110         base_y = BASE_Y;
111     }
112     if (txtwin == 0)
113         failed("cannot create txtwin");
114
115     keypad(txtwin, TRUE);       /* enable keyboard mapping */
116     (void) cbreak();            /* take input chars one at a time, no wait for \n */
117     (void) noecho();            /* don't echo input */
118
119     txt_y = base_y;
120     txt_x = 0;
121     wmove(txtwin, txt_y, txt_x);
122
123     if ((fp = fopen(argv[level], "r")) != 0) {
124         while ((j = fgetc(fp)) != EOF) {
125             if (waddch(txtwin, UChar(j)) != OK) {
126                 break;
127             }
128         }
129         fclose(fp);
130     } else {
131         wprintw(txtwin, "Cannot open:\n%s", argv[1]);
132     }
133
134     while (!Quit(j = mvwgetch(txtwin, txt_y, txt_x))) {
135         int limit;
136
137         switch (j) {
138         case KEY_DOWN:
139         case 'j':
140             if (txt_y < getmaxy(txtwin) - 1)
141                 txt_y++;
142             else
143                 beep();
144             break;
145         case KEY_UP:
146         case 'k':
147             if (txt_y > base_y)
148                 txt_y--;
149             else
150                 beep();
151             break;
152         case KEY_LEFT:
153         case 'h':
154             if (txt_x > 0)
155                 txt_x--;
156             else
157                 beep();
158             break;
159         case KEY_RIGHT:
160         case 'l':
161             if (txt_x < getmaxx(txtwin) - 1)
162                 txt_x++;
163             else
164                 beep();
165             break;
166         case 'w':
167             test_inchs(level + 1, argv, chrwin, strwin);
168             if (txtbox != 0) {
169                 touchwin(txtbox);
170                 wnoutrefresh(txtbox);
171             } else {
172                 touchwin(txtwin);
173                 wnoutrefresh(txtwin);
174             }
175             break;
176         case HELP_KEY_1:
177             popup_msg(txtwin, help);
178             break;
179         default:
180             beep();
181             break;
182         }
183
184         MvWPrintw(chrwin, 0, 0, "char:");
185         wclrtoeol(chrwin);
186
187         if (txtwin != stdscr) {
188             wmove(txtwin, txt_y, txt_x);
189
190             if ((ch = (int) winch(txtwin)) != ERR) {
191                 if (waddch(chrwin, (chtype) ch) != ERR) {
192                     for (j = txt_x + 1; j < getmaxx(txtwin); ++j) {
193                         if ((ch = (int) mvwinch(txtwin, txt_y, j)) != ERR) {
194                             if (waddch(chrwin, (chtype) ch) == ERR) {
195                                 break;
196                             }
197                         } else {
198                             break;
199                         }
200                     }
201                 }
202             }
203         } else {
204             move(txt_y, txt_x);
205
206             if ((ch = (int) inch()) != ERR) {
207                 if (waddch(chrwin, (chtype) ch) != ERR) {
208                     for (j = txt_x + 1; j < getmaxx(txtwin); ++j) {
209                         if ((ch = (int) mvinch(txt_y, j)) != ERR) {
210                             if (waddch(chrwin, (chtype) ch) == ERR) {
211                                 break;
212                             }
213                         } else {
214                             break;
215                         }
216                     }
217                 }
218             }
219         }
220         wnoutrefresh(chrwin);
221
222         MvWPrintw(strwin, 0, 0, "text:");
223         wclrtobot(strwin);
224
225         limit = getmaxx(strwin) - 5;
226
227         if (txtwin != stdscr) {
228             wmove(txtwin, txt_y, txt_x);
229             if (winchstr(txtwin, text) != ERR) {
230                 MvWAddChStr(strwin, 0, 5, text);
231             }
232
233             wmove(txtwin, txt_y, txt_x);
234             if (winchnstr(txtwin, text, limit) != ERR) {
235                 MvWAddChStr(strwin, 1, 5, text);
236             }
237
238             if (mvwinchstr(txtwin, txt_y, txt_x, text) != ERR) {
239                 MvWAddChStr(strwin, 2, 5, text);
240             }
241
242             if (mvwinchnstr(txtwin, txt_y, txt_x, text, limit) != ERR) {
243                 MvWAddChStr(strwin, 3, 5, text);
244             }
245         } else {
246             move(txt_y, txt_x);
247             if (inchstr(text) != ERR) {
248                 MvWAddChStr(strwin, 0, 5, text);
249             }
250
251             move(txt_y, txt_x);
252             if (inchnstr(text, limit) != ERR) {
253                 MvWAddChStr(strwin, 1, 5, text);
254             }
255
256             if (mvinchstr(txt_y, txt_x, text) != ERR) {
257                 MvWAddChStr(strwin, 2, 5, text);
258             }
259
260             if (mvinchnstr(txt_y, txt_x, text, limit) != ERR) {
261                 MvWAddChStr(strwin, 3, 5, text);
262             }
263         }
264
265         wnoutrefresh(strwin);
266     }
267     if (level > 1) {
268         delwin(txtwin);
269         delwin(txtbox);
270     }
271     return TRUE;
272 }
273
274 int
275 main(int argc, char *argv[])
276 {
277     WINDOW *chrbox;
278     WINDOW *chrwin;
279     WINDOW *strwin;
280
281     setlocale(LC_ALL, "");
282
283     if (argc < 2) {
284         fprintf(stderr, "usage: %s file1 [file2 [...]]\n", argv[0]);
285         return EXIT_FAILURE;
286     }
287
288     initscr();
289
290     chrbox = derwin(stdscr, BASE_Y, COLS, 0, 0);
291     box(chrbox, 0, 0);
292     wnoutrefresh(chrbox);
293
294     chrwin = derwin(chrbox, 1, COLS - 2, 1, 1);
295     strwin = derwin(chrbox, 4, COLS - 2, 2, 1);
296
297     test_inchs(1, argv, chrwin, strwin);
298
299     endwin();
300     ExitProgram(EXIT_SUCCESS);
301 }