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