]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/test_inwstr.c
ncurses 6.1 - patch 20180623
[ncurses.git] / test / test_inwstr.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: test_inwstr.c,v 1.5 2017/04/15 14:14:48 tom Exp $
30  *
31  * Author: Thomas E Dickey
32  *
33  * Demonstrate the inwstr functions from the curses library.
34
35        int inwstr(wchar_t *str);
36        int innwstr(wchar_t *str, int n);
37        int winwstr(WINDOW *win, wchar_t *str);
38        int winnwstr(WINDOW *win, wchar_t *str, int n);
39        int mvinwstr(int y, int x, wchar_t *str);
40        int mvinnwstr(int y, int x, wchar_t *str, int n);
41        int mvwinwstr(WINDOW *win, int y, int x, wchar_t *str);
42        int mvwinnwstr(WINDOW *win, int y, int x, wchar_t *str, int n);
43  */
44
45 #include <test.priv.h>
46
47 #if USE_WIDEC_SUPPORT
48
49 #define BASE_Y 6
50 #define MAX_COLS 1024
51
52 static bool
53 Quit(int ch)
54 {
55     return (ch == ERR || ch == 'q' || ch == QUIT || ch == ESCAPE);
56 }
57
58 static void
59 show_1st(WINDOW *win, int line, wchar_t *buffer)
60 {
61     (void) mvwaddwstr(win, line, 5, buffer);
62 }
63
64 static void
65 showmore(WINDOW *win, int line, wchar_t *buffer)
66 {
67     wmove(win, line, 0);
68     wclrtoeol(win);
69     show_1st(win, line, buffer);
70 }
71
72 static int
73 recursive_test(int level, char **argv, WINDOW *chrwin, WINDOW *strwin)
74 {
75     WINDOW *txtbox = 0;
76     WINDOW *txtwin = 0;
77     FILE *fp;
78     int ch;
79     int txt_x = 0, txt_y = 0;
80     int base_y;
81     int limit = getmaxx(strwin) - 5;
82     wchar_t buffer[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 ((ch = fgetc(fp)) != EOF) {
114             if (waddch(txtwin, UChar(ch)) != OK) {
115                 break;
116             }
117         }
118         fclose(fp);
119     } else {
120         wprintw(txtwin, "Cannot open:\n%s", argv[1]);
121     }
122
123     while (!Quit(ch = mvwgetch(txtwin, txt_y, txt_x))) {
124         switch (ch) {
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             recursive_test(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 '-':
164             if (limit > 0) {
165                 --limit;
166             } else {
167                 beep();
168             }
169             break;
170         case '+':
171             ++limit;
172             break;
173         default:
174             beep();
175             break;
176         }
177
178         MvWPrintw(chrwin, 0, 0, "line:");
179         wclrtoeol(chrwin);
180
181         if (txtwin != stdscr) {
182             wmove(txtwin, txt_y, txt_x);
183
184             if (winwstr(txtwin, buffer) != ERR) {
185                 show_1st(chrwin, 0, buffer);
186             }
187             if (mvwinwstr(txtwin, txt_y, txt_x, buffer) != ERR) {
188                 showmore(chrwin, 1, buffer);
189             }
190         } else {
191             move(txt_y, txt_x);
192
193             if (inwstr(buffer) != ERR) {
194                 show_1st(chrwin, 0, buffer);
195             }
196             if (mvinwstr(txt_y, txt_x, buffer) != ERR) {
197                 showmore(chrwin, 1, buffer);
198             }
199         }
200         wnoutrefresh(chrwin);
201
202         MvWPrintw(strwin, 0, 0, "%4d:", limit);
203         wclrtobot(strwin);
204
205         if (txtwin != stdscr) {
206             wmove(txtwin, txt_y, txt_x);
207             if (winnwstr(txtwin, buffer, limit) != ERR) {
208                 show_1st(strwin, 0, buffer);
209             }
210
211             if (mvwinnwstr(txtwin, txt_y, txt_x, buffer, limit) != ERR) {
212                 showmore(strwin, 1, buffer);
213             }
214         } else {
215             move(txt_y, txt_x);
216             if (innwstr(buffer, limit) != ERR) {
217                 show_1st(strwin, 0, buffer);
218             }
219
220             if (mvinnwstr(txt_y, txt_x, buffer, limit) != ERR) {
221                 showmore(strwin, 1, buffer);
222             }
223         }
224
225         wnoutrefresh(strwin);
226     }
227     if (level > 1) {
228         delwin(txtwin);
229         delwin(txtbox);
230     }
231     return TRUE;
232 }
233
234 int
235 main(int argc, char *argv[])
236 {
237     WINDOW *chrbox;
238     WINDOW *chrwin;
239     WINDOW *strwin;
240
241     setlocale(LC_ALL, "");
242
243     if (argc < 2) {
244         fprintf(stderr, "usage: %s file\n", argv[0]);
245         return EXIT_FAILURE;
246     }
247
248     initscr();
249
250     chrbox = derwin(stdscr, BASE_Y, COLS, 0, 0);
251     box(chrbox, 0, 0);
252     wnoutrefresh(chrbox);
253
254     chrwin = derwin(chrbox, 2, COLS - 2, 1, 1);
255     strwin = derwin(chrbox, 2, COLS - 2, 3, 1);
256
257     recursive_test(1, argv, chrwin, strwin);
258
259     endwin();
260     ExitProgram(EXIT_SUCCESS);
261 }
262 #else
263 int
264 main(void)
265 {
266     printf("This program requires the wide-ncurses library\n");
267     ExitProgram(EXIT_FAILURE);
268 }
269 #endif