]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/inch_wide.c
ncurses 6.1 - patch 20190831
[ncurses.git] / test / inch_wide.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: inch_wide.c,v 1.10 2019/08/24 23:11:01 tom Exp $
30  */
31 /*
32        int in_wch(cchar_t *wcval);
33        int mvin_wch(int y, int x, cchar_t *wcval);
34        int mvwin_wch(WINDOW *win, int y, int x, cchar_t *wcval);
35        int win_wch(WINDOW *win, cchar_t *wcval);
36        int in_wchstr(cchar_t *wchstr);
37        int in_wchnstr(cchar_t *wchstr, int n);
38        int win_wchstr(WINDOW *win, cchar_t *wchstr);
39        int win_wchnstr(WINDOW *win, cchar_t *wchstr, int n);
40        int mvin_wchstr(int y, int x, cchar_t *wchstr);
41        int mvin_wchnstr(int y, int x, cchar_t *wchstr, int n);
42        int mvwin_wchstr(WINDOW *win, int y, int x, cchar_t *wchstr);
43        int mvwin_wchnstr(WINDOW *win, int y, int x, cchar_t *wchstr, int n);
44 */
45
46 #include <test.priv.h>
47 #include <popup_msg.h>
48
49 #if USE_WIDEC_SUPPORT
50
51 #define BASE_Y 7
52 #define MAX_COLS 1024
53
54 static bool
55 Quit(int ch)
56 {
57     return (ch == ERR || ch == 'q' || ch == QUIT || ch == ESCAPE);
58 }
59
60 static int
61 test_inchs(int level, char **argv, WINDOW *chrwin, WINDOW *strwin)
62 {
63     static const char *help[] =
64     {
65         "Test input from screen using inch(), etc., in a moveable viewport.",
66         "",
67         "Commands:",
68         " ESC/^Q                   - quit",
69         " h,j,k,l (and arrow-keys) - move viewport",
70         " w                        - recur to new window",
71         "                            for next input file",
72         0
73     };
74
75     WINDOW *txtbox = 0;
76     WINDOW *txtwin = 0;
77     FILE *fp;
78     int j;
79     int txt_x = 0, txt_y = 0;
80     int base_y;
81     cchar_t ch;
82     cchar_t text[MAX_COLS];
83
84     if (argv[level] == 0) {
85         beep();
86         return FALSE;
87     }
88
89     if (level > 1) {
90         txtbox = newwin(LINES - BASE_Y, COLS - level, BASE_Y, level);
91         box(txtbox, 0, 0);
92         wnoutrefresh(txtbox);
93
94         txtwin = derwin(txtbox,
95                         getmaxy(txtbox) - 2,
96                         getmaxx(txtbox) - 2,
97                         1, 1);
98         base_y = 0;
99     } else {
100         txtwin = stdscr;
101         base_y = BASE_Y;
102     }
103
104     keypad(txtwin, TRUE);       /* enable keyboard mapping */
105     (void) cbreak();            /* take input chars one at a time, no wait for \n */
106     (void) noecho();            /* don't echo input */
107
108     txt_y = base_y;
109     txt_x = 0;
110     wmove(txtwin, txt_y, txt_x);
111
112     if ((fp = fopen(argv[level], "r")) != 0) {
113         while ((j = fgetc(fp)) != EOF) {
114             if (waddch(txtwin, UChar(j)) != OK) {
115                 break;
116             }
117         }
118         fclose(fp);
119     } else {
120         wprintw(txtwin, "Cannot open:\n%s", argv[1]);
121     }
122
123     while (!Quit(j = mvwgetch(txtwin, txt_y, txt_x))) {
124         int limit;
125
126         switch (j) {
127         case KEY_DOWN:
128         case 'j':
129             if (txt_y < getmaxy(txtwin) - 1)
130                 txt_y++;
131             else
132                 beep();
133             break;
134         case KEY_UP:
135         case 'k':
136             if (txt_y > base_y)
137                 txt_y--;
138             else
139                 beep();
140             break;
141         case KEY_LEFT:
142         case 'h':
143             if (txt_x > 0)
144                 txt_x--;
145             else
146                 beep();
147             break;
148         case KEY_RIGHT:
149         case 'l':
150             if (txt_x < getmaxx(txtwin) - 1)
151                 txt_x++;
152             else
153                 beep();
154             break;
155         case 'w':
156             test_inchs(level + 1, argv, chrwin, strwin);
157             if (txtbox != 0) {
158                 touchwin(txtbox);
159                 wnoutrefresh(txtbox);
160             } else {
161                 touchwin(txtwin);
162                 wnoutrefresh(txtwin);
163             }
164             break;
165         case HELP_KEY_1:
166             popup_msg(txtwin, help);
167             break;
168         default:
169             beep();
170             break;
171         }
172
173         MvWPrintw(chrwin, 0, 0, "char:");
174         wclrtoeol(chrwin);
175
176         if (txtwin != stdscr) {
177             wmove(txtwin, txt_y, txt_x);
178             if (win_wch(txtwin, &ch) != ERR) {
179                 if (wadd_wch(chrwin, &ch) != ERR) {
180                     for (j = txt_x + 1; j < getmaxx(txtwin); ++j) {
181                         if (mvwin_wch(txtwin, txt_y, j, &ch) != ERR) {
182                             if (wadd_wch(chrwin, &ch) == ERR) {
183                                 break;
184                             }
185                         } else {
186                             break;
187                         }
188                     }
189                 }
190             }
191         } else {
192             move(txt_y, txt_x);
193             if (in_wch(&ch) != ERR) {
194                 if (wadd_wch(chrwin, &ch) != ERR) {
195                     for (j = txt_x + 1; j < getmaxx(txtwin); ++j) {
196                         if (mvin_wch(txt_y, j, &ch) != ERR) {
197                             if (wadd_wch(chrwin, &ch) == ERR) {
198                                 break;
199                             }
200                         } else {
201                             break;
202                         }
203                     }
204                 }
205             }
206         }
207         wnoutrefresh(chrwin);
208
209         MvWPrintw(strwin, 0, 0, "text:");
210         wclrtobot(strwin);
211
212         limit = getmaxx(strwin) - 5;
213
214         if (txtwin != stdscr) {
215             wmove(txtwin, txt_y, txt_x);
216             if (win_wchstr(txtwin, text) != ERR) {
217                 (void) mvwadd_wchstr(strwin, 0, 5, text);
218             }
219
220             wmove(txtwin, txt_y, txt_x);
221             if (win_wchnstr(txtwin, text, limit) != ERR) {
222                 (void) mvwadd_wchstr(strwin, 1, 5, text);
223             }
224
225             if (mvwin_wchstr(txtwin, txt_y, txt_x, text) != ERR) {
226                 (void) mvwadd_wchstr(strwin, 2, 5, text);
227             }
228
229             if (mvwin_wchnstr(txtwin, txt_y, txt_x, text, limit) != ERR) {
230                 (void) mvwadd_wchstr(strwin, 3, 5, text);
231             }
232         } else {
233             move(txt_y, txt_x);
234             if (in_wchstr(text) != ERR) {
235                 (void) mvwadd_wchstr(strwin, 0, 5, text);
236             }
237
238             move(txt_y, txt_x);
239             if (in_wchnstr(text, limit) != ERR) {
240                 (void) mvwadd_wchstr(strwin, 1, 5, text);
241             }
242
243             if (mvin_wchstr(txt_y, txt_x, text) != ERR) {
244                 (void) mvwadd_wchstr(strwin, 2, 5, text);
245             }
246
247             if (mvin_wchnstr(txt_y, txt_x, text, limit) != ERR) {
248                 (void) mvwadd_wchstr(strwin, 3, 5, text);
249             }
250         }
251
252         wnoutrefresh(strwin);
253     }
254     if (level > 1) {
255         delwin(txtwin);
256         delwin(txtbox);
257     }
258     return TRUE;
259 }
260
261 int
262 main(int argc, char *argv[])
263 {
264     WINDOW *chrbox;
265     WINDOW *chrwin;
266     WINDOW *strwin;
267
268     setlocale(LC_ALL, "");
269
270     if (argc < 2) {
271         fprintf(stderr, "usage: %s file1 [file2 [...]]\n", argv[0]);
272         return EXIT_FAILURE;
273     }
274
275     initscr();
276
277     chrbox = derwin(stdscr, BASE_Y, COLS, 0, 0);
278     box(chrbox, 0, 0);
279     wnoutrefresh(chrbox);
280
281     chrwin = derwin(chrbox, 1, COLS - 2, 1, 1);
282     strwin = derwin(chrbox, 4, COLS - 2, 2, 1);
283
284     test_inchs(1, argv, chrwin, strwin);
285
286     endwin();
287     ExitProgram(EXIT_SUCCESS);
288 }
289 #else
290 int
291 main(void)
292 {
293     printf("This program requires the wide-ncurses library\n");
294     ExitProgram(EXIT_FAILURE);
295 }
296 #endif