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