1 /****************************************************************************
2 * Copyright (c) 2002-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: ins_wide.c,v 1.15 2010/12/12 00:20:14 tom Exp $
31 * Demonstrate the wins_wstr() and wins_wch functions.
32 * Thomas Dickey - 2002/11/23
34 * Note: to provide inputs for *ins_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 *ins_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 * *ins_wstr() 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>
51 /* definitions to make it simpler to compare with inserts.c */
52 #define InsNStr ins_nwstr
53 #define InsStr ins_wstr
54 #define MvInsNStr (void) mvins_nwstr
55 #define MvInsStr (void) mvins_wstr
56 #define MvWInsNStr (void) mvwins_nwstr
57 #define MvWInsStr (void) mvwins_wstr
58 #define WInsNStr wins_nwstr
59 #define WInsStr wins_wstr
70 static bool m_opt
= FALSE
;
71 static bool w_opt
= FALSE
;
72 static int n_opt
= -1;
75 legend(WINDOW
*win
, int level
, Options state
, wchar_t *buffer
, int length
)
77 const char *showstate
;
85 showstate
= " (mvXXX)";
88 showstate
= " (winXXX)";
91 showstate
= " (mvwinXXX)";
97 "The Strings/Chars displays should match. Enter any characters, except:\n");
99 "down-arrow or ^N to repeat on next line, ^W for inner window, ESC to exit.\n");
101 wprintw(win
, "Level %d,%s inserted %d characters <", level
,
103 waddwstr(win
, buffer
);
108 ColOf(wchar_t *buffer
, int length
, int margin
)
113 for (n
= 0, result
= margin
+ 1; n
< length
; ++n
) {
117 /* actually newline should clear the remainder of the line
118 * and move to the next line - but that seems a little awkward
129 result
+= (MY_TABSIZE
- (result
% MY_TABSIZE
));
135 result
+= wcwidth(ch
);
145 ConvertCh(chtype source
, cchar_t
*target
)
147 wchar_t tmp_wchar
[2];
149 tmp_wchar
[0] = (wchar_t) source
;
151 if (setcchar(target
, tmp_wchar
, A_NORMAL
, 0, (void *) 0) == ERR
) {
159 MvWInsCh(WINDOW
*win
, int y
, int x
, chtype ch
)
164 if (ConvertCh(ch
, &tmp_cchar
)) {
165 code
= mvwins_wch(win
, y
, x
, &tmp_cchar
);
167 code
= mvwinsch(win
, y
, x
, ch
);
173 MvInsCh(int y
, int x
, chtype ch
)
178 if (ConvertCh(ch
, &tmp_cchar
)) {
179 code
= mvins_wch(y
, x
, &tmp_cchar
);
181 code
= mvinsch(y
, x
, ch
);
187 WInsCh(WINDOW
*win
, chtype ch
)
192 if (ConvertCh(ch
, &tmp_cchar
)) {
193 code
= wins_wch(win
, &tmp_cchar
);
195 code
= winsch(win
, ch
);
206 if (ConvertCh(ch
, &tmp_cchar
)) {
207 code
= ins_wch(&tmp_cchar
);
214 #define LEN(n) ((length - (n) > n_opt) ? n_opt : (length - (n)))
216 test_inserts(int level
)
218 static bool first
= TRUE
;
226 wchar_t buffer
[BUFSIZ
];
230 int margin
= (2 * MY_TABSIZE
) - 1;
231 Options option
= ((m_opt
? oMove
: oDefault
)
232 | ((w_opt
|| (level
> 0)) ? oWindow
: oDefault
));
236 setlocale(LC_ALL
, "");
238 putenv(strcpy(cmd
, "TABSIZE=8"));
241 (void) cbreak(); /* take input chars one at a time, no wait for \n */
242 (void) noecho(); /* don't echo input */
243 keypad(stdscr
, TRUE
);
248 look
= newwin(limit
, COLS
- (2 * (level
- 1)), 0, level
- 1);
249 work
= newwin(limit
- 2, COLS
- (2 * level
), 1, level
);
250 show
= newwin(4, COLS
, limit
+ 1, 0);
256 show
= derwin(stdscr
, 4, COLS
, limit
+ 1, 0);
260 for (col
= margin
+ 1; col
< COLS
; col
+= MY_TABSIZE
)
261 MvWVLine(work
, row
, col
, '.', limit
- 2);
263 MvWVLine(work
, row
, margin
, ACS_VLINE
, limit
- 2);
264 MvWVLine(work
, row
, margin
+ 1, ACS_VLINE
, limit
- 2);
267 MvWAddStr(work
, 1, 2, "String");
268 MvWAddStr(work
, limit
+ 1, 2, "Chars");
271 buffer
[length
= 0] = '\0';
272 legend(show
, level
, option
, buffer
, length
);
278 * Show the characters inserted in color, to distinguish from those that
283 init_pair(1, COLOR_WHITE
, COLOR_BLUE
);
284 wbkgdset(work
, COLOR_PAIR(1) | ' ');
287 while ((ch
= read_linedata(work
)) != ERR
&& !isQUIT(ch
)) {
288 wmove(work
, row
, margin
+ 1);
291 test_inserts(level
+ 1);
306 /* put the whole string in, all at once */
311 for (col
= 0; col
< length
; col
+= n_opt
) {
312 col2
= ColOf(buffer
, col
, margin
);
313 if (move(row
, col2
) != ERR
) {
314 InsNStr(buffer
+ col
, LEN(col
));
318 if (move(row
, col2
) != ERR
) {
325 for (col
= 0; col
< length
; col
+= n_opt
) {
326 col2
= ColOf(buffer
, col
, margin
);
327 MvInsNStr(row
, col2
, buffer
+ col
, LEN(col
));
330 MvInsStr(row
, col2
, buffer
);
335 for (col
= 0; col
< length
; col
+= n_opt
) {
336 col2
= ColOf(buffer
, col
, margin
);
337 if (wmove(work
, row
, col2
) != ERR
) {
338 WInsNStr(work
, buffer
+ col
, LEN(col
));
342 if (wmove(work
, row
, col2
) != ERR
) {
343 WInsStr(work
, buffer
);
349 for (col
= 0; col
< length
; col
+= n_opt
) {
350 col2
= ColOf(buffer
, col
, margin
);
351 MvWInsNStr(work
, row
, col2
, buffer
+ col
, LEN(col
));
354 MvWInsStr(work
, row
, col2
, buffer
);
359 /* do the corresponding single-character insertion */
361 for (col
= 0; col
< length
; ++col
) {
362 col2
= ColOf(buffer
, col
, margin
);
365 if (move(row2
, col2
) != ERR
) {
366 InsCh((chtype
) buffer
[col
]);
370 MvInsCh(row2
, col2
, (chtype
) buffer
[col
]);
373 if (wmove(work
, row2
, col2
) != ERR
) {
374 WInsCh(work
, (chtype
) buffer
[col
]);
378 MvWInsCh(work
, row2
, col2
, (chtype
) buffer
[col
]);
387 buffer
[length
++] = ch
;
388 buffer
[length
] = '\0';
390 /* put the string in, one character at a time */
391 col
= ColOf(buffer
, length
- 1, margin
);
394 if (move(row
, col
) != ERR
) {
395 InsStr(buffer
+ length
- 1);
399 MvInsStr(row
, col
, buffer
+ length
- 1);
402 if (wmove(work
, row
, col
) != ERR
) {
403 WInsStr(work
, buffer
+ length
- 1);
407 MvWInsStr(work
, row
, col
, buffer
+ length
- 1);
411 /* do the corresponding single-character insertion */
414 if (move(limit
+ row
, col
) != ERR
) {
419 MvInsCh(limit
+ row
, col
, (chtype
) ch
);
422 if (wmove(work
, limit
+ row
, col
) != ERR
) {
423 WInsCh(work
, (chtype
) ch
);
427 MvWInsCh(work
, limit
+ row
, col
, (chtype
) ch
);
433 legend(show
, level
, option
, buffer
, length
);
450 static const char *tbl
[] =
452 "Usage: inserts [options]"
455 ," -f FILE read data from given file"
456 ," -n NUM limit string-inserts to NUM bytes on ^N replay"
457 ," -m perform wmove/move separately from insert-functions"
458 ," -w use window-parameter even when stdscr would be implied"
461 for (n
= 0; n
< SIZEOF(tbl
); ++n
)
462 fprintf(stderr
, "%s\n", tbl
[n
]);
463 ExitProgram(EXIT_FAILURE
);
467 main(int argc GCC_UNUSED
, char *argv
[]GCC_UNUSED
)
471 setlocale(LC_ALL
, "");
473 while ((ch
= getopt(argc
, argv
, "f:mn:w")) != -1) {
476 init_linedata(optarg
);
482 n_opt
= atoi(optarg
);
499 ExitProgram(EXIT_SUCCESS
);
505 printf("This program requires the wide-ncurses library\n");
506 ExitProgram(EXIT_FAILURE
);