1 /****************************************************************************
2 * Copyright 2020-2022,2023 Thomas E. Dickey *
3 * Copyright 2009-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: test_addwstr.c,v 1.22 2023/05/27 20:13:10 tom Exp $
32 * Demonstrate the waddwstr() and wadd_wch functions.
33 * Thomas Dickey - 2009/9/12
35 * Note: to provide inputs for *add_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 *add_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 * *addwstr() 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>
60 * redefinitions to simplify comparison between test_*str programs
62 #define AddNStr addnwstr
63 #define AddStr addwstr
64 #define MvAddNStr (void) mvaddnwstr
65 #define MvAddStr (void) mvaddwstr
66 #define MvWAddNStr (void) mvwaddnwstr
67 #define MvWAddStr (void) mvwaddwstr
68 #define WAddNStr waddnwstr
69 #define WAddStr waddwstr
80 static bool m_opt = FALSE;
81 static bool w_opt = FALSE;
82 static int n_opt = -1;
85 legend(WINDOW *win, int level, Options state, wchar_t *buffer, int length)
87 const char *showstate;
95 showstate = " (mvXXX)";
98 showstate = " (winXXX)";
101 showstate = " (mvwinXXX)";
107 "The Strings/Chars displays should match. Enter any characters, except:\n");
109 "down-arrow or ^N to repeat on next line, ^W for inner window, ESC to exit.\n");
111 wprintw(win, "Level %d,%s added %d characters <", level, showstate, length);
112 waddwstr(win, buffer);
117 ColOf(const wchar_t *const buffer, int length, int margin)
122 for (n = 0, result = margin + 1; n < length; ++n) {
126 /* actually newline should clear the remainder of the line
127 * and move to the next line - but that seems a little awkward
138 result += (MY_TABSIZE - (result % MY_TABSIZE));
144 result += wcwidth((wchar_t) ch);
154 ConvertCh(chtype source, cchar_t *target)
156 wchar_t tmp_wchar[2];
158 tmp_wchar[0] = (wchar_t) source;
160 if (setcchar(target, tmp_wchar, A_NORMAL, 0, (void *) 0) == ERR) {
168 MvWAddCh(WINDOW *win, int y, int x, chtype ch)
173 if (ConvertCh(ch, &tmp_cchar)) {
174 code = mvwadd_wch(win, y, x, &tmp_cchar);
176 code = mvwaddch(win, y, x, ch);
182 MvAddCh(int y, int x, chtype ch)
187 if (ConvertCh(ch, &tmp_cchar)) {
188 code = mvadd_wch(y, x, &tmp_cchar);
190 code = mvaddch(y, x, ch);
196 WAddCh(WINDOW *win, chtype ch)
201 if (ConvertCh(ch, &tmp_cchar)) {
202 code = wadd_wch(win, &tmp_cchar);
204 code = waddch(win, ch);
215 if (ConvertCh(ch, &tmp_cchar)) {
216 code = add_wch(&tmp_cchar);
223 #define LEN(n) ((length - (n) > n_opt) ? n_opt : (length - (n)))
225 recursive_test(int level)
227 static bool first = TRUE;
235 wchar_t buffer[BUFSIZ];
239 int margin = (2 * MY_TABSIZE) - 1;
240 Options option = (Options) ((unsigned) (m_opt
243 | (unsigned) ((w_opt || (level > 0))
249 setlocale(LC_ALL, "");
251 _nc_STRCPY(cmd, "TABSIZE=8", sizeof(cmd));
255 (void) cbreak(); /* take input chars one at a time, no wait for \n */
256 (void) noecho(); /* don't echo input */
257 keypad(stdscr, TRUE);
260 * Show the characters added in color, to distinguish from those that
265 init_pair(1, COLOR_WHITE, COLOR_BLUE);
271 look = newwin(limit, COLS - (2 * (level - 1)), 0, level - 1);
272 work = newwin(limit - 2, COLS - (2 * level), 1, level);
273 show = newwin(4, COLS, limit + 1, 0);
279 show = derwin(stdscr, 4, COLS, limit + 1, 0);
283 for (col = margin + 1; col < COLS; col += MY_TABSIZE)
284 MvWVLine(work, row, col, '.', limit - 2);
286 MvWVLine(work, row, margin, ACS_VLINE, limit - 2);
287 MvWVLine(work, row, margin + 1, ACS_VLINE, limit - 2);
290 (void) mvwaddstr(work, 1, 2, "String");
291 (void) mvwaddstr(work, limit + 1, 2, "Chars");
294 buffer[length = 0] = '\0';
295 legend(show, level, option, buffer, length);
301 wbkgdset(work, (chtype) (COLOR_PAIR(1) | ' '));
304 while ((ch = read_linedata(work)) != ERR && !isQUIT(ch)) {
305 wmove(work, row, margin + 1);
308 recursive_test(level + 1);
325 /* put the whole string in, all at once */
330 for (col = 0; col < length; col += n_opt) {
331 col2 = ColOf(buffer, col, margin);
332 if (move(row, col2) != ERR) {
333 AddNStr(buffer + col, LEN(col));
337 if (move(row, col2) != ERR) {
344 for (col = 0; col < length; col += n_opt) {
345 col2 = ColOf(buffer, col, margin);
346 MvAddNStr(row, col2, buffer + col, LEN(col));
349 MvAddStr(row, col2, buffer);
354 for (col = 0; col < length; col += n_opt) {
355 col2 = ColOf(buffer, col, margin);
356 if (wmove(work, row, col2) != ERR) {
357 WAddNStr(work, buffer + col, LEN(col));
361 if (wmove(work, row, col2) != ERR) {
362 WAddStr(work, buffer);
368 for (col = 0; col < length; col += n_opt) {
369 col2 = ColOf(buffer, col, margin);
370 MvWAddNStr(work, row, col2, buffer + col, LEN(col));
373 MvWAddStr(work, row, col2, buffer);
378 /* do the corresponding single-character add */
380 for (col = 0; col < length; ++col) {
381 col2 = ColOf(buffer, col, margin);
384 if (move(row2, col2) != ERR) {
385 AddCh((chtype) buffer[col]);
389 MvAddCh(row2, col2, (chtype) buffer[col]);
392 if (wmove(work, row2, col2) != ERR) {
393 WAddCh(work, (chtype) buffer[col]);
397 MvWAddCh(work, row2, col2, (chtype) buffer[col]);
409 buffer[length++] = (wchar_t) ch;
410 buffer[length] = '\0';
412 /* put the string in, one character at a time */
413 col = ColOf(buffer, length - 1, margin);
416 if (move(row, col) != ERR) {
417 AddStr(buffer + length - 1);
421 MvAddStr(row, col, buffer + length - 1);
424 if (wmove(work, row, col) != ERR) {
425 WAddStr(work, buffer + length - 1);
429 MvWAddStr(work, row, col, buffer + length - 1);
433 /* do the corresponding single-character add */
436 if (move(limit + row, col) != ERR) {
441 MvAddCh(limit + row, col, (chtype) ch);
444 if (wmove(work, limit + row, col) != ERR) {
445 WAddCh(work, (chtype) ch);
449 MvWAddCh(work, limit + row, col, (chtype) ch);
455 legend(show, level, option, buffer, length);
472 static const char *tbl[] =
474 "Usage: test_addwstr [options]"
478 ," -f FILE read data from given file"
479 ," -n NUM limit string-adds to NUM bytes on ^N replay"
480 ," -m perform wmove/move separately from add-functions"
481 ," -w use window-parameter even when stdscr would be implied"
484 for (n = 0; n < SIZEOF(tbl); ++n)
485 fprintf(stderr, "%s\n", tbl[n]);
486 ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
493 main(int argc, char *argv[])
497 setlocale(LC_ALL, "");
499 while ((ch = getopt(argc, argv, OPTS_COMMON "f:mn:w")) != -1) {
502 init_linedata(optarg);
508 n_opt = atoi(optarg);
517 ExitProgram(EXIT_SUCCESS);
519 usage(ch == OPTS_USAGE);
528 ExitProgram(EXIT_SUCCESS);
534 printf("This program requires the wide-ncurses library\n");
535 ExitProgram(EXIT_FAILURE);