1 /****************************************************************************
2 * Copyright (c) 2009,2010 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
29 * $Id: test_addwstr.c,v 1.6 2010/12/12 00:18:00 tom Exp $
31 * Demonstrate the waddwstr() and wadd_wch functions.
32 * Thomas Dickey - 2009/9/12
34 * Note: to provide inputs for *add_wch(), we use setcchar(). A quirk of the
35 * X/Open definition for that function is that the string contains no
36 * characters with negative width. Any control character (such as tab) falls
37 * into that category. So it follows that *add_wch() cannot render a tab
38 * character because there is no legal way to construct a cchar_t containing
39 * one. X/Open does not document this, and it would be logical to assume that
40 * *addwstr() has the same limitation, but it uses a wchar_t string directly,
41 * and does not document how tabs are handled.
44 #include <test.priv.h>
56 /* definitions to make it simpler to compare with inserts.c */
57 #define AddNStr addnwstr
58 #define AddStr addwstr
59 #define MvAddNStr (void) mvaddnwstr
60 #define MvAddStr (void) mvaddwstr
61 #define MvWAddNStr (void) mvwaddnwstr
62 #define MvWAddStr (void) mvwaddwstr
63 #define WAddNStr waddnwstr
64 #define WAddStr waddwstr
75 static bool m_opt = FALSE;
76 static bool w_opt = FALSE;
77 static int n_opt = -1;
80 legend(WINDOW *win, int level, Options state, wchar_t *buffer, int length)
82 const char *showstate;
90 showstate = " (mvXXX)";
93 showstate = " (winXXX)";
96 showstate = " (mvwinXXX)";
102 "The Strings/Chars displays should match. Enter any characters, except:\n");
104 "down-arrow or ^N to repeat on next line, ^W for inner window, ESC to exit.\n");
106 wprintw(win, "Level %d,%s inserted %d characters <", level,
108 waddwstr(win, buffer);
113 ColOf(wchar_t *buffer, int length, int margin)
118 for (n = 0, result = margin + 1; n < length; ++n) {
122 /* actually newline should clear the remainder of the line
123 * and move to the next line - but that seems a little awkward
134 result += (MY_TABSIZE - (result % MY_TABSIZE));
140 result += wcwidth(ch);
150 ConvertCh(chtype source, cchar_t *target)
152 wchar_t tmp_wchar[2];
154 tmp_wchar[0] = (wchar_t) source;
156 if (setcchar(target, tmp_wchar, A_NORMAL, 0, (void *) 0) == ERR) {
164 MvWAddCh(WINDOW *win, int y, int x, chtype ch)
169 if (ConvertCh(ch, &tmp_cchar)) {
170 code = mvwadd_wch(win, y, x, &tmp_cchar);
172 code = mvwaddch(win, y, x, ch);
178 MvAddCh(int y, int x, chtype ch)
183 if (ConvertCh(ch, &tmp_cchar)) {
184 code = mvadd_wch(y, x, &tmp_cchar);
186 code = mvaddch(y, x, ch);
192 WAddCh(WINDOW *win, chtype ch)
197 if (ConvertCh(ch, &tmp_cchar)) {
198 code = wadd_wch(win, &tmp_cchar);
200 code = waddch(win, ch);
211 if (ConvertCh(ch, &tmp_cchar)) {
212 code = add_wch(&tmp_cchar);
219 #define LEN(n) ((length - (n) > n_opt) ? n_opt : (length - (n)))
221 test_inserts(int level)
223 static bool first = TRUE;
231 wchar_t buffer[BUFSIZ];
235 int margin = (2 * MY_TABSIZE) - 1;
236 Options option = ((m_opt ? oMove : oDefault)
237 | ((w_opt || (level > 0)) ? oWindow : oDefault));
241 setlocale(LC_ALL, "");
243 putenv(strcpy(cmd, "TABSIZE=8"));
246 (void) cbreak(); /* take input chars one at a time, no wait for \n */
247 (void) noecho(); /* don't echo input */
248 keypad(stdscr, TRUE);
253 look = newwin(limit, COLS - (2 * (level - 1)), 0, level - 1);
254 work = newwin(limit - 2, COLS - (2 * level), 1, level);
255 show = newwin(4, COLS, limit + 1, 0);
261 show = derwin(stdscr, 4, COLS, limit + 1, 0);
265 for (col = margin + 1; col < COLS; col += MY_TABSIZE)
266 MvWVLine(work, row, col, '.', limit - 2);
268 MvWVLine(work, row, margin, ACS_VLINE, limit - 2);
269 MvWVLine(work, row, margin + 1, ACS_VLINE, limit - 2);
272 (void) mvwaddstr(work, 1, 2, "String");
273 (void) mvwaddstr(work, limit + 1, 2, "Chars");
276 buffer[length = 0] = '\0';
277 legend(show, level, option, buffer, length);
283 * Show the characters inserted in color, to distinguish from those that
288 init_pair(1, COLOR_WHITE, COLOR_BLUE);
289 wbkgdset(work, COLOR_PAIR(1) | ' ');
292 while ((ch = read_linedata(work)) != ERR && !isQUIT(ch)) {
293 wmove(work, row, margin + 1);
296 test_inserts(level + 1);
311 /* put the whole string in, all at once */
316 for (col = 0; col < length; col += n_opt) {
317 col2 = ColOf(buffer, col, margin);
318 if (move(row, col2) != ERR) {
319 AddNStr(buffer + col, LEN(col));
323 if (move(row, col2) != ERR) {
330 for (col = 0; col < length; col += n_opt) {
331 col2 = ColOf(buffer, col, margin);
332 MvAddNStr(row, col2, buffer + col, LEN(col));
335 MvAddStr(row, col2, buffer);
340 for (col = 0; col < length; col += n_opt) {
341 col2 = ColOf(buffer, col, margin);
342 if (wmove(work, row, col2) != ERR) {
343 WAddNStr(work, buffer + col, LEN(col));
347 if (wmove(work, row, col2) != ERR) {
348 WAddStr(work, buffer);
354 for (col = 0; col < length; col += n_opt) {
355 col2 = ColOf(buffer, col, margin);
356 MvWAddNStr(work, row, col2, buffer + col, LEN(col));
359 MvWAddStr(work, row, col2, buffer);
364 /* do the corresponding single-character insertion */
366 for (col = 0; col < length; ++col) {
367 col2 = ColOf(buffer, col, margin);
370 if (move(row2, col2) != ERR) {
371 AddCh((chtype) buffer[col]);
375 MvAddCh(row2, col2, (chtype) buffer[col]);
378 if (wmove(work, row2, col2) != ERR) {
379 WAddCh(work, (chtype) buffer[col]);
383 MvWAddCh(work, row2, col2, (chtype) buffer[col]);
395 buffer[length++] = ch;
396 buffer[length] = '\0';
398 /* put the string in, one character at a time */
399 col = ColOf(buffer, length - 1, margin);
402 if (move(row, col) != ERR) {
403 AddStr(buffer + length - 1);
407 MvAddStr(row, col, buffer + length - 1);
410 if (wmove(work, row, col) != ERR) {
411 WAddStr(work, buffer + length - 1);
415 MvWAddStr(work, row, col, buffer + length - 1);
419 /* do the corresponding single-character insertion */
422 if (move(limit + row, col) != ERR) {
427 MvAddCh(limit + row, col, (chtype) ch);
430 if (wmove(work, limit + row, col) != ERR) {
431 WAddCh(work, (chtype) ch);
435 MvWAddCh(work, limit + row, col, (chtype) ch);
441 legend(show, level, option, buffer, length);
458 static const char *tbl[] =
460 "Usage: inserts [options]"
463 ," -f FILE read data from given file"
464 ," -n NUM limit string-inserts to NUM bytes on ^N replay"
465 ," -m perform wmove/move separately from insert-functions"
466 ," -w use window-parameter even when stdscr would be implied"
469 for (n = 0; n < SIZEOF(tbl); ++n)
470 fprintf(stderr, "%s\n", tbl[n]);
471 ExitProgram(EXIT_FAILURE);
475 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
479 setlocale(LC_ALL, "");
481 while ((ch = getopt(argc, argv, "f:mn:w")) != -1) {
484 init_linedata(optarg);
490 n_opt = atoi(optarg);
507 ExitProgram(EXIT_SUCCESS);
513 printf("This program requires the wide-ncurses library\n");
514 ExitProgram(EXIT_FAILURE);