]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/test_add_wchstr.c
ncurses 5.7 - patch 20091031
[ncurses.git] / test / test_add_wchstr.c
1 /****************************************************************************
2  * Copyright (c) 2009 Free Software Foundation, Inc.                        *
3  *                                                                          *
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:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
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.                               *
22  *                                                                          *
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       *
26  * authorization.                                                           *
27  ****************************************************************************/
28 /*
29  * $Id: test_add_wchstr.c,v 1.7 2009/09/13 00:00:28 tom Exp $
30  *
31  * Demonstrate the waddwchstr() and wadd_wch functions.
32  * Thomas Dickey - 2009/9/12
33  *
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  * *addwchstr() has the same limitation, but it uses a wchar_t string directly,
41  * and does not document how tabs are handled.
42  */
43
44 #include <test.priv.h>
45
46 #if USE_WIDEC_SUPPORT
47
48 #define WIDE_LINEDATA
49 #include <linedata.h>
50
51 /* definitions to make it simpler to compare with test_addstr.c */
52 #define AddNStr    add_wchnstr
53 #define AddStr     add_wchstr
54 #define MvAddNStr  mvadd_wchnstr
55 #define MvAddStr   mvadd_wchstr
56 #define MvWAddNStr mvwadd_wchnstr
57 #define MvWAddStr  mvwadd_wchstr
58 #define WAddNStr   wadd_wchnstr
59 #define WAddStr    wadd_wchstr
60
61 #define MY_TABSIZE 8
62
63 typedef enum {
64     oDefault = 0,
65     oMove = 1,
66     oWindow = 2,
67     oMoveWindow = 3
68 } Options;
69
70 static bool m_opt = FALSE;
71 static bool w_opt = FALSE;
72 static int n_opt = -1;
73
74 static cchar_t *temp_buffer;
75 static size_t temp_length;
76
77 #define TempBuffer(source_len, source_cast) \
78     if (source != 0) { \
79         size_t need = source_len + 1; \
80         wchar_t have[2]; \
81         int n = 0; \
82  \
83         if (need > temp_length) { \
84             temp_length = need * 2; \
85             temp_buffer = typeRealloc(cchar_t, temp_length, temp_buffer); \
86         } \
87         have[0] = 0; \
88         have[1] = 0; \
89         do { \
90             have[0] = source_cast; \
91             setcchar(&temp_buffer[n++], have, A_NORMAL, 0, NULL); \
92         } while (have[0] != 0); \
93     } else if (temp_buffer != 0) { \
94         free(temp_buffer); \
95         temp_buffer = 0; \
96         temp_length = 0; \
97     } \
98     return temp_buffer;
99
100 static cchar_t *
101 ChStr(const char *source)
102 {
103     TempBuffer(strlen(source), UChar(*source++));
104 }
105
106 static cchar_t *
107 ChWStr(const wchar_t *source)
108 {
109     TempBuffer(wcslen(source), *source++);
110 }
111
112 static void
113 legend(WINDOW *win, int level, Options state, wchar_t *buffer, int length)
114 {
115     NCURSES_CONST char *showstate;
116
117     switch (state) {
118     default:
119     case oDefault:
120         showstate = "";
121         break;
122     case oMove:
123         showstate = " (mvXXX)";
124         break;
125     case oWindow:
126         showstate = " (winXXX)";
127         break;
128     case oMoveWindow:
129         showstate = " (mvwinXXX)";
130         break;
131     }
132
133     wmove(win, 0, 0);
134     wprintw(win,
135             "The Strings/Chars displays should match.  Enter any characters, except:\n");
136     wprintw(win,
137             "down-arrow or ^N to repeat on next line, 'w' for inner window, 'q' to exit.\n");
138     wclrtoeol(win);
139     wprintw(win, "Level %d,%s added %d characters <", level,
140             showstate, length);
141     waddwstr(win, buffer);
142     waddstr(win, ">");
143 }
144
145 static int
146 ColOf(wchar_t *buffer, int length, int margin)
147 {
148     int n;
149     int result;
150
151     for (n = 0, result = margin + 1; n < length; ++n) {
152         int ch = buffer[n];
153         switch (ch) {
154         case '\n':
155             /* actually newline should clear the remainder of the line
156              * and move to the next line - but that seems a little awkward
157              * in this example.
158              */
159         case '\r':
160             result = 0;
161             break;
162         case '\b':
163             if (result > 0)
164                 --result;
165             break;
166         case '\t':
167             result += (MY_TABSIZE - (result % MY_TABSIZE));
168             break;
169         case '\177':
170             result += 2;
171             break;
172         default:
173             result += wcwidth(ch);
174             if (ch < 32)
175                 ++result;
176             break;
177         }
178     }
179     return result;
180 }
181
182 static int
183 ConvertCh(chtype source, cchar_t *target)
184 {
185     wchar_t tmp_wchar[2];
186
187     tmp_wchar[0] = source;
188     tmp_wchar[1] = 0;
189     if (setcchar(target, tmp_wchar, A_NORMAL, 0, (void *) 0) == ERR) {
190         beep();
191         return FALSE;
192     }
193     return TRUE;
194 }
195
196 static int
197 MvWAddCh(WINDOW *win, int y, int x, chtype ch)
198 {
199     int code;
200     cchar_t tmp_cchar;
201
202     if (ConvertCh(ch, &tmp_cchar)) {
203         code = mvwadd_wch(win, y, x, &tmp_cchar);
204     } else {
205         code = mvwaddch(win, y, x, ch);
206     }
207     return code;
208 }
209
210 static int
211 MvAddCh(int y, int x, chtype ch)
212 {
213     int code;
214     cchar_t tmp_cchar;
215
216     if (ConvertCh(ch, &tmp_cchar)) {
217         code = mvadd_wch(y, x, &tmp_cchar);
218     } else {
219         code = mvaddch(y, x, ch);
220     }
221     return code;
222 }
223
224 static int
225 WAddCh(WINDOW *win, chtype ch)
226 {
227     int code;
228     cchar_t tmp_cchar;
229
230     if (ConvertCh(ch, &tmp_cchar)) {
231         code = wadd_wch(win, &tmp_cchar);
232     } else {
233         code = waddch(win, ch);
234     }
235     return code;
236 }
237
238 static int
239 AddCh(chtype ch)
240 {
241     int code;
242     cchar_t tmp_cchar;
243
244     if (ConvertCh(ch, &tmp_cchar)) {
245         code = add_wch(&tmp_cchar);
246     } else {
247         code = addch(ch);
248     }
249     return code;
250 }
251
252 #define LEN(n) ((length - (n) > n_opt) ? n_opt : (length - (n)))
253 static void
254 test_add_wchstr(int level)
255 {
256     static bool first = TRUE;
257
258     int ch;
259     int limit;
260     int row = 1;
261     int col;
262     int row2, col2;
263     int length;
264     wchar_t buffer[BUFSIZ];
265     WINDOW *look = 0;
266     WINDOW *work = 0;
267     WINDOW *show = 0;
268     int margin = (2 * MY_TABSIZE) - 1;
269     Options option = ((m_opt ? oMove : oDefault)
270                       | ((w_opt || (level > 0)) ? oWindow : oDefault));
271
272     if (first) {
273         static char cmd[80];
274         setlocale(LC_ALL, "");
275
276         putenv(strcpy(cmd, "TABSIZE=8"));
277
278         initscr();
279         (void) cbreak();        /* take input chars one at a time, no wait for \n */
280         (void) noecho();        /* don't echo input */
281         keypad(stdscr, TRUE);
282     }
283
284     limit = LINES - 5;
285     if (level > 0) {
286         look = newwin(limit, COLS - (2 * (level - 1)), 0, level - 1);
287         work = newwin(limit - 2, COLS - (2 * level), 1, level);
288         show = newwin(4, COLS, limit + 1, 0);
289         box(look, 0, 0);
290         wnoutrefresh(look);
291         limit -= 2;
292     } else {
293         work = stdscr;
294         show = derwin(stdscr, 4, COLS, limit + 1, 0);
295     }
296     keypad(work, TRUE);
297
298     for (col = margin + 1; col < COLS; col += MY_TABSIZE)
299         mvwvline(work, row, col, '.', limit - 2);
300
301     mvwvline(work, row, margin, ACS_VLINE, limit - 2);
302     mvwvline(work, row, margin + 1, ACS_VLINE, limit - 2);
303     limit /= 2;
304
305     mvwadd_wchstr(work, 1, 2, ChStr("String"));
306     mvwadd_wchstr(work, limit + 1, 2, ChStr("Chars"));
307     wnoutrefresh(work);
308
309     buffer[length = 0] = '\0';
310     legend(show, level, option, buffer, length);
311     wnoutrefresh(show);
312
313     doupdate();
314
315     /*
316      * Show the characters added in color, to distinguish from those that
317      * are shifted.
318      */
319     if (has_colors()) {
320         start_color();
321         init_pair(1, COLOR_WHITE, COLOR_BLUE);
322         wbkgdset(work, COLOR_PAIR(1) | ' ');
323     }
324
325     while ((ch = read_linedata(work)) != ERR && !isQUIT(ch)) {
326         wmove(work, row, margin + 1);
327         switch (ch) {
328         case key_RECUR:
329             test_add_wchstr(level + 1);
330
331             touchwin(look);
332             touchwin(work);
333             touchwin(show);
334
335             wnoutrefresh(look);
336             wnoutrefresh(work);
337             wnoutrefresh(show);
338
339             doupdate();
340             break;
341         case key_NEWLINE:
342             if (row < limit) {
343                 ++row;
344                 /* put the whole string in, all at once */
345                 col2 = margin + 1;
346                 switch (option) {
347                 case oDefault:
348                     if (n_opt > 1) {
349                         for (col = 0; col < length; col += n_opt) {
350                             col2 = ColOf(buffer, col, margin);
351                             if (move(row, col2) != ERR) {
352                                 AddNStr(ChWStr(buffer + col), LEN(col));
353                             }
354                         }
355                     } else {
356                         if (move(row, col2) != ERR) {
357                             AddStr(ChWStr(buffer));
358                         }
359                     }
360                     break;
361                 case oMove:
362                     if (n_opt > 1) {
363                         for (col = 0; col < length; col += n_opt) {
364                             col2 = ColOf(buffer, col, margin);
365                             MvAddNStr(row, col2, ChWStr(buffer + col), LEN(col));
366                         }
367                     } else {
368                         MvAddStr(row, col2, ChWStr(buffer));
369                     }
370                     break;
371                 case oWindow:
372                     if (n_opt > 1) {
373                         for (col = 0; col < length; col += n_opt) {
374                             col2 = ColOf(buffer, col, margin);
375                             if (wmove(work, row, col2) != ERR) {
376                                 WAddNStr(work, ChWStr(buffer + col), LEN(col));
377                             }
378                         }
379                     } else {
380                         if (wmove(work, row, col2) != ERR) {
381                             WAddStr(work, ChWStr(buffer));
382                         }
383                     }
384                     break;
385                 case oMoveWindow:
386                     if (n_opt > 1) {
387                         for (col = 0; col < length; col += n_opt) {
388                             col2 = ColOf(buffer, col, margin);
389                             MvWAddNStr(work, row, col2, ChWStr(buffer +
390                                                                col), LEN(col));
391                         }
392                     } else {
393                         MvWAddStr(work, row, col2, ChWStr(buffer));
394                     }
395                     break;
396                 }
397
398                 /* do the corresponding single-character add */
399                 row2 = limit + row;
400                 for (col = 0; col < length; ++col) {
401                     col2 = ColOf(buffer, col, margin);
402                     switch (option) {
403                     case oDefault:
404                         if (move(row2, col2) != ERR) {
405                             AddCh((chtype) buffer[col]);
406                         }
407                         break;
408                     case oMove:
409                         MvAddCh(row2, col2, (chtype) buffer[col]);
410                         break;
411                     case oWindow:
412                         if (wmove(work, row2, col2) != ERR) {
413                             WAddCh(work, (chtype) buffer[col]);
414                         }
415                         break;
416                     case oMoveWindow:
417                         MvWAddCh(work, row2, col2, (chtype) buffer[col]);
418                         break;
419                     }
420                 }
421             } else {
422                 beep();
423             }
424             break;
425         default:
426             buffer[length++] = ch;
427             buffer[length] = '\0';
428
429             /* put the string in, one character at a time */
430             col = ColOf(buffer, length - 1, margin);
431             switch (option) {
432             case oDefault:
433                 if (move(row, col) != ERR) {
434                     AddStr(ChWStr(buffer + length - 1));
435                 }
436                 break;
437             case oMove:
438                 MvAddStr(row, col, ChWStr(buffer + length - 1));
439                 break;
440             case oWindow:
441                 if (wmove(work, row, col) != ERR) {
442                     WAddStr(work, ChWStr(buffer + length - 1));
443                 }
444                 break;
445             case oMoveWindow:
446                 MvWAddStr(work, row, col, ChWStr(buffer + length - 1));
447                 break;
448             }
449
450             /* do the corresponding single-character add */
451             switch (option) {
452             case oDefault:
453                 if (move(limit + row, col) != ERR) {
454                     AddCh(ch);
455                 }
456                 break;
457             case oMove:
458                 MvAddCh(limit + row, col, ch);
459                 break;
460             case oWindow:
461                 if (wmove(work, limit + row, col) != ERR) {
462                     WAddCh(work, ch);
463                 }
464                 break;
465             case oMoveWindow:
466                 MvWAddCh(work, limit + row, col, ch);
467                 break;
468             }
469
470             wnoutrefresh(work);
471
472             legend(show, level, option, buffer, length);
473             wnoutrefresh(show);
474
475             doupdate();
476             break;
477         }
478     }
479     if (level > 0) {
480         delwin(show);
481         delwin(work);
482         delwin(look);
483     }
484 }
485
486 static void
487 usage(void)
488 {
489     static const char *tbl[] =
490     {
491         "Usage: test_add_wchstr [options]"
492         ,""
493         ,"Options:"
494         ,"  -f FILE read data from given file"
495         ,"  -n NUM  limit string-adds to NUM bytes on ^N replay"
496         ,"  -m      perform wmove/move separately from add-functions"
497         ,"  -w      use window-parameter even when stdscr would be implied"
498     };
499     unsigned n;
500     for (n = 0; n < SIZEOF(tbl); ++n)
501         fprintf(stderr, "%s\n", tbl[n]);
502     ExitProgram(EXIT_FAILURE);
503 }
504
505 int
506 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
507 {
508     int ch;
509
510     setlocale(LC_ALL, "");
511
512     while ((ch = getopt(argc, argv, "f:mn:w")) != -1) {
513         switch (ch) {
514         case 'f':
515             init_linedata(optarg);
516             break;
517         case 'm':
518             m_opt = TRUE;
519             break;
520         case 'n':
521             n_opt = atoi(optarg);
522             if (n_opt == 0)
523                 n_opt = -1;
524             break;
525         case 'w':
526             w_opt = TRUE;
527             break;
528         default:
529             usage();
530             break;
531         }
532     }
533     if (optind < argc)
534         usage();
535
536     test_add_wchstr(0);
537     endwin();
538     ExitProgram(EXIT_SUCCESS);
539 }
540 #else
541 int
542 main(void)
543 {
544     printf("This program requires the wide-ncurses library\n");
545     ExitProgram(EXIT_FAILURE);
546 }
547 #endif