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