2 * $Id: ins_wide.c,v 1.6 2005/04/16 17:45:17 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>
21 /* definitions to make it simpler to compare with inserts.c */
22 #define InsNStr ins_nwstr
23 #define InsStr ins_wstr
24 #define MvInsNStr mvins_nwstr
25 #define MvInsStr mvins_wstr
26 #define MvWInsNStr mvwins_nwstr
27 #define MvWInsStr mvwins_wstr
28 #define WInsNStr wins_nwstr
29 #define WInsStr wins_wstr
40 static bool m_opt = FALSE;
41 static bool w_opt = FALSE;
42 static int n_opt = -1;
45 legend(WINDOW *win, int level, Options state, wchar_t *buffer, int length)
47 NCURSES_CONST char *showstate;
55 showstate = " (mvXXX)";
58 showstate = " (winXXX)";
61 showstate = " (mvwinXXX)";
67 "The Strings/Chars displays should match. Enter any characters, except:\n");
69 "down-arrow or ^N to repeat on next line, 'w' for inner window, 'q' to exit.\n");
71 wprintw(win, "Level %d,%s inserted %d characters <", level,
73 waddwstr(win, buffer);
78 ColOf(wchar_t *buffer, int length, int margin)
83 for (n = 0, result = margin + 1; n < length; ++n) {
87 /* actually newline should clear the remainder of the line
88 * and move to the next line - but that seems a little awkward
99 result += (TABSIZE - (result % TABSIZE));
105 result += wcwidth(ch);
115 ConvertCh(chtype source, cchar_t *target)
117 wchar_t tmp_wchar[2];
119 tmp_wchar[0] = source;
121 if (setcchar(target, tmp_wchar, A_NORMAL, 0, (void *) 0) == ERR) {
129 MvWInsCh(WINDOW *win, int y, int x, chtype ch)
134 if (ConvertCh(ch, &tmp_cchar)) {
135 code = mvwins_wch(win, y, x, &tmp_cchar);
137 code = mvwinsch(win, y, x, ch);
143 MvInsCh(int y, int x, chtype ch)
148 if (ConvertCh(ch, &tmp_cchar)) {
149 code = mvins_wch(y, x, &tmp_cchar);
151 code = mvinsch(y, x, ch);
157 WInsCh(WINDOW *win, chtype ch)
162 if (ConvertCh(ch, &tmp_cchar)) {
163 code = wins_wch(win, &tmp_cchar);
165 code = winsch(win, ch);
176 if (ConvertCh(ch, &tmp_cchar)) {
177 code = ins_wch(&tmp_cchar);
184 #define LEN(n) ((length - (n) > n_opt) ? n_opt : (length - (n)))
186 test_inserts(int level)
188 static bool first = TRUE;
197 wchar_t buffer[BUFSIZ];
201 int margin = (2 * TABSIZE) - 1;
202 Options option = ((m_opt ? oMove : oDefault)
203 | ((w_opt || (level > 0)) ? oWindow : oDefault));
207 setlocale(LC_ALL, "");
209 putenv(strcpy(cmd, "TABSIZE=8"));
212 (void) cbreak(); /* take input chars one at a time, no wait for \n */
213 (void) noecho(); /* don't echo input */
214 keypad(stdscr, TRUE);
219 look = newwin(limit, COLS - (2 * (level - 1)), 0, level - 1);
220 work = newwin(limit - 2, COLS - (2 * level), 1, level);
221 show = newwin(4, COLS, limit + 1, 0);
227 show = derwin(stdscr, 4, COLS, limit + 1, 0);
231 for (col = margin + 1; col < COLS; col += TABSIZE)
232 mvwvline(work, row, col, '.', limit - 2);
234 mvwvline(work, row, margin, ACS_VLINE, limit - 2);
235 mvwvline(work, row, margin + 1, ACS_VLINE, limit - 2);
238 mvwaddstr(work, 1, 2, "String");
239 mvwaddstr(work, limit + 1, 2, "Chars");
242 buffer[length = 0] = '\0';
243 legend(show, level, option, buffer, length);
249 * Show the characters inserted in color, to distinguish from those that
254 init_pair(1, COLOR_WHITE, COLOR_BLUE);
255 wbkgdset(work, COLOR_PAIR(1) | ' ');
258 while ((code = wget_wch(work, &ch)) != ERR) {
260 if (code == KEY_CODE_YES) {
272 } else if (code == ERR) {
279 wmove(work, row, margin + 1);
282 test_inserts(level + 1);
297 /* put the whole string in, all at once */
302 for (col = 0; col < length; col += n_opt) {
303 col2 = ColOf(buffer, col, margin);
304 if (move(row, col2) != ERR) {
305 InsNStr(buffer + col, LEN(col));
309 if (move(row, col2) != ERR) {
316 for (col = 0; col < length; col += n_opt) {
317 col2 = ColOf(buffer, col, margin);
318 MvInsNStr(row, col2, buffer + col, LEN(col));
321 MvInsStr(row, col2, buffer);
326 for (col = 0; col < length; col += n_opt) {
327 col2 = ColOf(buffer, col, margin);
328 if (wmove(work, row, col2) != ERR) {
329 WInsNStr(work, buffer + col, LEN(col));
333 if (wmove(work, row, col2) != ERR) {
334 WInsStr(work, buffer);
340 for (col = 0; col < length; col += n_opt) {
341 col2 = ColOf(buffer, col, margin);
342 MvWInsNStr(work, row, col2, buffer + col, LEN(col));
345 MvWInsStr(work, row, col2, buffer);
350 /* do the corresponding single-character insertion */
352 for (col = 0; col < length; ++col) {
353 col2 = ColOf(buffer, col, margin);
356 if (move(row2, col2) != ERR) {
357 InsCh((chtype) buffer[col]);
361 MvInsCh(row2, col2, (chtype) buffer[col]);
364 if (wmove(work, row2, col2) != ERR) {
365 WInsCh(work, (chtype) buffer[col]);
369 MvWInsCh(work, row2, col2, (chtype) buffer[col]);
381 buffer[length++] = ch;
382 buffer[length] = '\0';
384 /* put the string in, one character at a time */
385 col = ColOf(buffer, length - 1, margin);
388 if (move(row, col) != ERR) {
389 InsStr(buffer + length - 1);
393 MvInsStr(row, col, buffer + length - 1);
396 if (wmove(work, row, col) != ERR) {
397 WInsStr(work, buffer + length - 1);
401 MvWInsStr(work, row, col, buffer + length - 1);
405 /* do the corresponding single-character insertion */
408 if (move(limit + row, col) != ERR) {
413 MvInsCh(limit + row, col, ch);
416 if (wmove(work, limit + row, col) != ERR) {
421 MvWInsCh(work, limit + row, col, ch);
427 legend(show, level, option, buffer, length);
444 static const char *tbl[] =
446 "Usage: inserts [options]"
449 ," -n NUM limit string-inserts to NUM bytes on ^N replay"
450 ," -m perform wmove/move separately from insert-functions"
451 ," -w use window-parameter even when stdscr would be implied"
454 for (n = 0; n < SIZEOF(tbl); ++n)
455 fprintf(stderr, "%s\n", tbl[n]);
456 ExitProgram(EXIT_FAILURE);
460 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
464 setlocale(LC_ALL, "");
466 while ((ch = getopt(argc, argv, "mn:w")) != EOF) {
472 n_opt = atoi(optarg);
489 ExitProgram(EXIT_SUCCESS);
495 printf("This program requires the wide-ncurses library\n");
496 ExitProgram(EXIT_FAILURE);