]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/inchs.c
ncurses 6.0 - patch 20170429
[ncurses.git] / test / inchs.c
1 /****************************************************************************
2  * Copyright (c) 2007-2012,2017 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.14 2017/04/29 22:03:26 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     int limit;
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         switch (j) {
136         case KEY_DOWN:
137         case 'j':
138             if (txt_y < getmaxy(txtwin) - 1)
139                 txt_y++;
140             else
141                 beep();
142             break;
143         case KEY_UP:
144         case 'k':
145             if (txt_y > base_y)
146                 txt_y--;
147             else
148                 beep();
149             break;
150         case KEY_LEFT:
151         case 'h':
152             if (txt_x > 0)
153                 txt_x--;
154             else
155                 beep();
156             break;
157         case KEY_RIGHT:
158         case 'l':
159             if (txt_x < getmaxx(txtwin) - 1)
160                 txt_x++;
161             else
162                 beep();
163             break;
164         case 'w':
165             test_inchs(level + 1, argv, chrwin, strwin);
166             if (txtbox != 0) {
167                 touchwin(txtbox);
168                 wnoutrefresh(txtbox);
169             } else {
170                 touchwin(txtwin);
171                 wnoutrefresh(txtwin);
172             }
173             break;
174         case HELP_KEY_1:
175             popup_msg(txtwin, help);
176             break;
177         default:
178             beep();
179             break;
180         }
181
182         MvWPrintw(chrwin, 0, 0, "char:");
183         wclrtoeol(chrwin);
184
185         if (txtwin != stdscr) {
186             wmove(txtwin, txt_y, txt_x);
187
188             if ((ch = (int) winch(txtwin)) != ERR) {
189                 if (waddch(chrwin, (chtype) ch) != ERR) {
190                     for (j = txt_x + 1; j < getmaxx(txtwin); ++j) {
191                         if ((ch = (int) mvwinch(txtwin, txt_y, j)) != ERR) {
192                             if (waddch(chrwin, (chtype) ch) == ERR) {
193                                 break;
194                             }
195                         } else {
196                             break;
197                         }
198                     }
199                 }
200             }
201         } else {
202             move(txt_y, txt_x);
203
204             if ((ch = (int) inch()) != ERR) {
205                 if (waddch(chrwin, (chtype) ch) != ERR) {
206                     for (j = txt_x + 1; j < getmaxx(txtwin); ++j) {
207                         if ((ch = (int) mvinch(txt_y, j)) != ERR) {
208                             if (waddch(chrwin, (chtype) ch) == ERR) {
209                                 break;
210                             }
211                         } else {
212                             break;
213                         }
214                     }
215                 }
216             }
217         }
218         wnoutrefresh(chrwin);
219
220         MvWPrintw(strwin, 0, 0, "text:");
221         wclrtobot(strwin);
222
223         limit = getmaxx(strwin) - 5;
224
225         if (txtwin != stdscr) {
226             wmove(txtwin, txt_y, txt_x);
227             if (winchstr(txtwin, text) != ERR) {
228                 MvWAddChStr(strwin, 0, 5, text);
229             }
230
231             wmove(txtwin, txt_y, txt_x);
232             if (winchnstr(txtwin, text, limit) != ERR) {
233                 MvWAddChStr(strwin, 1, 5, text);
234             }
235
236             if (mvwinchstr(txtwin, txt_y, txt_x, text) != ERR) {
237                 MvWAddChStr(strwin, 2, 5, text);
238             }
239
240             if (mvwinchnstr(txtwin, txt_y, txt_x, text, limit) != ERR) {
241                 MvWAddChStr(strwin, 3, 5, text);
242             }
243         } else {
244             move(txt_y, txt_x);
245             if (inchstr(text) != ERR) {
246                 MvWAddChStr(strwin, 0, 5, text);
247             }
248
249             move(txt_y, txt_x);
250             if (inchnstr(text, limit) != ERR) {
251                 MvWAddChStr(strwin, 1, 5, text);
252             }
253
254             if (mvinchstr(txt_y, txt_x, text) != ERR) {
255                 MvWAddChStr(strwin, 2, 5, text);
256             }
257
258             if (mvinchnstr(txt_y, txt_x, text, limit) != ERR) {
259                 MvWAddChStr(strwin, 3, 5, text);
260             }
261         }
262
263         wnoutrefresh(strwin);
264     }
265     if (level > 1) {
266         delwin(txtwin);
267         delwin(txtbox);
268     }
269     return TRUE;
270 }
271
272 int
273 main(int argc, char *argv[])
274 {
275     WINDOW *chrbox;
276     WINDOW *chrwin;
277     WINDOW *strwin;
278
279     setlocale(LC_ALL, "");
280
281     if (argc < 2) {
282         fprintf(stderr, "usage: %s file1 [file2 [...]]\n", argv[0]);
283         return EXIT_FAILURE;
284     }
285
286     initscr();
287
288     chrbox = derwin(stdscr, BASE_Y, COLS, 0, 0);
289     box(chrbox, 0, 0);
290     wnoutrefresh(chrbox);
291
292     chrwin = derwin(chrbox, 1, COLS - 2, 1, 1);
293     strwin = derwin(chrbox, 4, COLS - 2, 2, 1);
294
295     test_inchs(1, argv, chrwin, strwin);
296
297     endwin();
298     ExitProgram(EXIT_SUCCESS);
299 }