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