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