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