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