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