2 * $Id: ins_wide.c,v 1.3 2003/08/09 22:07:23 tom Exp $
4 * Demonstrate the wins_wstr() and wins_wch functions.
5 * Thomas Dickey - 2002/11/23
7 * Note: to provide inputs for *ins_wch(), we use setcchar(). A quirk of the
8 * X/Open definition for that function is that the string contains no
9 * characters with negative width. Any control character (such as tab) falls
10 * into that category. So it follows that *ins_wch() cannot render a tab
11 * character because there is no legal way to construct a cchar_t containing
12 * one. X/Open does not document this, and it would be logical to assume that
13 * *ins_wstr() has the same limitation, but it uses a wchar_t string directly,
14 * and does not document how tabs are handled.
17 #include <test.priv.h>
22 static int margin = (2 * TABSIZE) - 1;
25 legend(WINDOW *win, wchar_t * buffer, int length)
29 "The Strings/Chars displays should match. Enter any characters.\n");
31 "Use down-arrow or ^N to repeat on the next line, 'q' to exit.\n");
33 wprintw(win, "Inserted %d characters <", length);
34 waddwstr(win, buffer);
39 ColOf(wchar_t * buffer, int length)
44 for (n = 0, result = margin + 1; n < length; ++n) {
48 /* actually newline should clear the remainder of the line
49 * and move to the next line - but that seems a little awkward
60 result += (TABSIZE - (result % TABSIZE));
76 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
86 wchar_t buffer[BUFSIZ];
92 (void) cbreak(); /* take input chars one at a time, no wait for \n */
93 (void) noecho(); /* don't echo input */
97 work = newwin(limit, COLS, 0, 0);
98 show = newwin(4, COLS, limit + 1, 0);
101 for (col = margin + 1; col < COLS; col += TABSIZE)
102 mvwvline(work, row, col, '.', limit - 2);
105 mvwvline(work, row, margin, ACS_VLINE, limit - 2);
106 mvwvline(work, row, margin + 1, ACS_VLINE, limit - 2);
109 mvwaddstr(work, 1, 2, "String");
110 mvwaddstr(work, limit + 1, 2, "Chars");
113 buffer[length = 0] = '\0';
114 legend(show, buffer, length);
120 * Show the characters inserted in color, to distinguish from those that
125 init_pair(1, COLOR_WHITE, COLOR_BLUE);
126 wbkgdset(work, COLOR_PAIR(1) | ' ');
129 while ((code = wget_wch(work, &ch)) != ERR) {
149 wmove(work, row, margin + 1);
150 if (ch == CTRL('N')) {
153 /* put the whole string in, all at once */
154 mvwins_wstr(work, row, margin + 1, buffer);
156 /* do the corresponding single-character insertion */
157 for (col = 0; col < length; ++col) {
158 memset(&tmp_cchar, 0, sizeof(tmp_cchar));
159 if (setcchar(&tmp_cchar,
163 (void *) 0) != ERR) {
164 mvwins_wch(work, limit + row, ColOf(buffer, col), &tmp_cchar);
166 beep(); /* even for tabs! */
169 ColOf(buffer, col), buffer[col]);
176 buffer[length++] = ch;
177 buffer[length] = '\0';
178 /* put the string in, one character at a time */
181 ColOf(buffer, length - 1), buffer + length - 1);
183 /* do the corresponding single-character insertion */
186 if (setcchar(&tmp_cchar,
190 (void *) 0) != ERR) {
193 ColOf(buffer, length - 1), &tmp_cchar);
195 beep(); /* even for tabs! */
198 ColOf(buffer, length - 1), ch);
202 legend(show, buffer, length);
209 ExitProgram(EXIT_SUCCESS);
215 printf("This program requires the wide-ncurses library\n");
216 ExitProgram(EXIT_FAILURE);