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