1 /****************************************************************************
2 * Copyright 2020 Thomas E. Dickey *
3 * Copyright 2002-2016,2017 Free Software Foundation, Inc. *
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: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
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. *
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 *
28 ****************************************************************************/
30 * $Id: ins_wide.c,v 1.25 2020/02/02 23:34:34 tom Exp $
32 * Demonstrate the wins_wstr() and wins_wch functions.
33 * Thomas Dickey - 2002/11/23
35 * Note: to provide inputs for *ins_wch(), we use setcchar(). A quirk of the
36 * X/Open definition for that function is that the string contains no
37 * characters with negative width. Any control character (such as tab) falls
38 * into that category. So it follows that *ins_wch() cannot render a tab
39 * character because there is no legal way to construct a cchar_t containing
40 * one. X/Open does not document this, and it would be logical to assume that
41 * *ins_wstr() has the same limitation, but it uses a wchar_t string directly,
42 * and does not document how tabs are handled.
45 #include <test.priv.h>
52 /* definitions to make it simpler to compare with inserts.c */
53 #define InsNStr ins_nwstr
54 #define InsStr ins_wstr
55 #define MvInsNStr (void) mvins_nwstr
56 #define MvInsStr (void) mvins_wstr
57 #define MvWInsNStr (void) mvwins_nwstr
58 #define MvWInsStr (void) mvwins_wstr
59 #define WInsNStr wins_nwstr
60 #define WInsStr wins_wstr
71 static bool m_opt = FALSE;
72 static bool w_opt = FALSE;
73 static int n_opt = -1;
76 legend(WINDOW *win, int level, Options state, wchar_t *buffer, int length)
78 const char *showstate;
86 showstate = " (mvXXX)";
89 showstate = " (winXXX)";
92 showstate = " (mvwinXXX)";
98 "The Strings/Chars displays should match. Enter any characters, except:\n");
100 "down-arrow or ^N to repeat on next line, ^W for inner window, ESC to exit.\n");
102 wprintw(win, "Level %d,%s inserted %d characters <", level,
104 waddwstr(win, buffer);
109 ColOf(wchar_t *buffer, int length, int margin)
114 for (n = 0, result = margin + 1; n < length; ++n) {
118 /* actually newline should clear the remainder of the line
119 * and move to the next line - but that seems a little awkward
130 result += (MY_TABSIZE - (result % MY_TABSIZE));
136 result += wcwidth((wchar_t) ch);
146 ConvertCh(chtype source, cchar_t *target)
148 wchar_t tmp_wchar[2];
150 tmp_wchar[0] = (wchar_t) source;
152 if (setcchar(target, tmp_wchar, A_NORMAL, 0, (void *) 0) == ERR) {
160 MvWInsCh(WINDOW *win, int y, int x, chtype ch)
165 if (ConvertCh(ch, &tmp_cchar)) {
166 code = mvwins_wch(win, y, x, &tmp_cchar);
168 code = mvwinsch(win, y, x, ch);
174 MvInsCh(int y, int x, chtype ch)
179 if (ConvertCh(ch, &tmp_cchar)) {
180 code = mvins_wch(y, x, &tmp_cchar);
182 code = mvinsch(y, x, ch);
188 WInsCh(WINDOW *win, chtype ch)
193 if (ConvertCh(ch, &tmp_cchar)) {
194 code = wins_wch(win, &tmp_cchar);
196 code = winsch(win, ch);
207 if (ConvertCh(ch, &tmp_cchar)) {
208 code = ins_wch(&tmp_cchar);
215 #define LEN(n) ((length - (n) > n_opt) ? n_opt : (length - (n)))
217 test_inserts(int level)
219 static bool first = TRUE;
227 wchar_t buffer[BUFSIZ];
231 int margin = (2 * MY_TABSIZE) - 1;
232 Options option = (Options) ((int) (m_opt ? oMove : oDefault)
233 | (int) ((w_opt || (level > 0))
234 ? oWindow : oDefault));
238 setlocale(LC_ALL, "");
240 _nc_STRCPY(cmd, "TABSIZE=8", sizeof(cmd));
244 (void) cbreak(); /* take input chars one at a time, no wait for \n */
245 (void) noecho(); /* don't echo input */
246 keypad(stdscr, TRUE);
249 * Show the characters inserted in color, to distinguish from those
254 init_pair(1, COLOR_WHITE, COLOR_BLUE);
260 look = newwin(limit, COLS - (2 * (level - 1)), 0, level - 1);
261 work = newwin(limit - 2, COLS - (2 * level), 1, level);
262 show = newwin(4, COLS, limit + 1, 0);
268 show = derwin(stdscr, 4, COLS, limit + 1, 0);
272 for (col = margin + 1; col < COLS; col += MY_TABSIZE)
273 MvWVLine(work, row, col, '.', limit - 2);
275 MvWVLine(work, row, margin, ACS_VLINE, limit - 2);
276 MvWVLine(work, row, margin + 1, ACS_VLINE, limit - 2);
279 MvWAddStr(work, 1, 2, "String");
280 MvWAddStr(work, limit + 1, 2, "Chars");
283 buffer[length = 0] = '\0';
284 legend(show, level, option, buffer, length);
290 wbkgdset(work, (chtype) (COLOR_PAIR(1) | ' '));
293 while ((ch = read_linedata(work)) != ERR && !isQUIT(ch)) {
294 wmove(work, row, margin + 1);
297 test_inserts(level + 1);
314 /* put the whole string in, all at once */
319 for (col = 0; col < length; col += n_opt) {
320 col2 = ColOf(buffer, col, margin);
321 if (move(row, col2) != ERR) {
322 InsNStr(buffer + col, LEN(col));
326 if (move(row, col2) != ERR) {
333 for (col = 0; col < length; col += n_opt) {
334 col2 = ColOf(buffer, col, margin);
335 MvInsNStr(row, col2, buffer + col, LEN(col));
338 MvInsStr(row, col2, buffer);
343 for (col = 0; col < length; col += n_opt) {
344 col2 = ColOf(buffer, col, margin);
345 if (wmove(work, row, col2) != ERR) {
346 WInsNStr(work, buffer + col, LEN(col));
350 if (wmove(work, row, col2) != ERR) {
351 WInsStr(work, buffer);
357 for (col = 0; col < length; col += n_opt) {
358 col2 = ColOf(buffer, col, margin);
359 MvWInsNStr(work, row, col2, buffer + col, LEN(col));
362 MvWInsStr(work, row, col2, buffer);
367 /* do the corresponding single-character insertion */
369 for (col = 0; col < length; ++col) {
370 col2 = ColOf(buffer, col, margin);
373 if (move(row2, col2) != ERR) {
374 InsCh((chtype) buffer[col]);
378 MvInsCh(row2, col2, (chtype) buffer[col]);
381 if (wmove(work, row2, col2) != ERR) {
382 WInsCh(work, (chtype) buffer[col]);
386 MvWInsCh(work, row2, col2, (chtype) buffer[col]);
395 if (length >= BUFSIZ - 2)
397 buffer[length++] = (wchar_t) ch;
398 buffer[length] = '\0';
400 /* put the string in, one character at a time */
401 col = ColOf(buffer, length - 1, margin);
404 if (move(row, col) != ERR) {
405 InsStr(buffer + length - 1);
409 MvInsStr(row, col, buffer + length - 1);
412 if (wmove(work, row, col) != ERR) {
413 WInsStr(work, buffer + length - 1);
417 MvWInsStr(work, row, col, buffer + length - 1);
421 /* do the corresponding single-character insertion */
424 if (move(limit + row, col) != ERR) {
429 MvInsCh(limit + row, col, (chtype) ch);
432 if (wmove(work, limit + row, col) != ERR) {
433 WInsCh(work, (chtype) ch);
437 MvWInsCh(work, limit + row, col, (chtype) ch);
443 legend(show, level, option, buffer, length);
460 static const char *tbl[] =
462 "Usage: ins_wide [options]"
465 ," -f FILE read data from given file"
466 ," -n NUM limit string-inserts to NUM bytes on ^N replay"
467 ," -m perform wmove/move separately from insert-functions"
468 ," -w use window-parameter even when stdscr would be implied"
471 for (n = 0; n < SIZEOF(tbl); ++n)
472 fprintf(stderr, "%s\n", tbl[n]);
473 ExitProgram(EXIT_FAILURE);
477 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
481 setlocale(LC_ALL, "");
483 while ((ch = getopt(argc, argv, "f:mn:w")) != -1) {
486 init_linedata(optarg);
492 n_opt = atoi(optarg);
509 ExitProgram(EXIT_SUCCESS);
515 printf("This program requires the wide-ncurses library\n");
516 ExitProgram(EXIT_FAILURE);