]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/ins_wide.c
ncurses 5.9 - patch 20120901
[ncurses.git] / test / ins_wide.c
1 /****************************************************************************
2  * Copyright (c) 2002-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: ins_wide.c,v 1.17 2012/06/09 20:29:33 tom Exp $
30  *
31  * Demonstrate the wins_wstr() and wins_wch functions.
32  * Thomas Dickey - 2002/11/23
33  *
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.
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 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
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     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, ESC 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] = (wchar_t) 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 MvWInsCh(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 = mvwins_wch(win, y, x, &tmp_cchar);
166     } else {
167         code = mvwinsch(win, y, x, ch);
168     }
169     return code;
170 }
171
172 static int
173 MvInsCh(int y, int x, chtype ch)
174 {
175     int code;
176     cchar_t tmp_cchar;
177
178     if (ConvertCh(ch, &tmp_cchar)) {
179         code = mvins_wch(y, x, &tmp_cchar);
180     } else {
181         code = mvinsch(y, x, ch);
182     }
183     return code;
184 }
185
186 static int
187 WInsCh(WINDOW *win, chtype ch)
188 {
189     int code;
190     cchar_t tmp_cchar;
191
192     if (ConvertCh(ch, &tmp_cchar)) {
193         code = wins_wch(win, &tmp_cchar);
194     } else {
195         code = winsch(win, ch);
196     }
197     return code;
198 }
199
200 static int
201 InsCh(chtype ch)
202 {
203     int code;
204     cchar_t tmp_cchar;
205
206     if (ConvertCh(ch, &tmp_cchar)) {
207         code = ins_wch(&tmp_cchar);
208     } else {
209         code = insch(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 = (Options) ((int) (m_opt ? oMove : oDefault)
232                                 | (int) ((w_opt || (level > 0))
233                                          ? oWindow : oDefault));
234
235     if (first) {
236         static char cmd[80];
237         setlocale(LC_ALL, "");
238
239         putenv(strcpy(cmd, "TABSIZE=8"));
240
241         initscr();
242         (void) cbreak();        /* take input chars one at a time, no wait for \n */
243         (void) noecho();        /* don't echo input */
244         keypad(stdscr, TRUE);
245     }
246
247     limit = LINES - 5;
248     if (level > 0) {
249         look = newwin(limit, COLS - (2 * (level - 1)), 0, level - 1);
250         work = newwin(limit - 2, COLS - (2 * level), 1, level);
251         show = newwin(4, COLS, limit + 1, 0);
252         box(look, 0, 0);
253         wnoutrefresh(look);
254         limit -= 2;
255     } else {
256         work = stdscr;
257         show = derwin(stdscr, 4, COLS, limit + 1, 0);
258     }
259     keypad(work, TRUE);
260
261     for (col = margin + 1; col < COLS; col += MY_TABSIZE)
262         MvWVLine(work, row, col, '.', limit - 2);
263
264     MvWVLine(work, row, margin, ACS_VLINE, limit - 2);
265     MvWVLine(work, row, margin + 1, ACS_VLINE, limit - 2);
266     limit /= 2;
267
268     MvWAddStr(work, 1, 2, "String");
269     MvWAddStr(work, limit + 1, 2, "Chars");
270     wnoutrefresh(work);
271
272     buffer[length = 0] = '\0';
273     legend(show, level, option, buffer, length);
274     wnoutrefresh(show);
275
276     doupdate();
277
278     /*
279      * Show the characters inserted in color, to distinguish from those that
280      * are shifted.
281      */
282     if (has_colors()) {
283         start_color();
284         init_pair(1, COLOR_WHITE, COLOR_BLUE);
285         wbkgdset(work, (chtype) (COLOR_PAIR(1) | ' '));
286     }
287
288     while ((ch = read_linedata(work)) != ERR && !isQUIT(ch)) {
289         wmove(work, row, margin + 1);
290         switch (ch) {
291         case key_RECUR:
292             test_inserts(level + 1);
293
294             touchwin(look);
295             touchwin(work);
296             touchwin(show);
297
298             wnoutrefresh(look);
299             wnoutrefresh(work);
300             wnoutrefresh(show);
301
302             doupdate();
303             break;
304         case key_NEWLINE:
305             if (row < limit) {
306                 ++row;
307                 /* put the whole string in, all at once */
308                 col2 = margin + 1;
309                 switch (option) {
310                 case oDefault:
311                     if (n_opt > 1) {
312                         for (col = 0; col < length; col += n_opt) {
313                             col2 = ColOf(buffer, col, margin);
314                             if (move(row, col2) != ERR) {
315                                 InsNStr(buffer + col, LEN(col));
316                             }
317                         }
318                     } else {
319                         if (move(row, col2) != ERR) {
320                             InsStr(buffer);
321                         }
322                     }
323                     break;
324                 case oMove:
325                     if (n_opt > 1) {
326                         for (col = 0; col < length; col += n_opt) {
327                             col2 = ColOf(buffer, col, margin);
328                             MvInsNStr(row, col2, buffer + col, LEN(col));
329                         }
330                     } else {
331                         MvInsStr(row, col2, buffer);
332                     }
333                     break;
334                 case oWindow:
335                     if (n_opt > 1) {
336                         for (col = 0; col < length; col += n_opt) {
337                             col2 = ColOf(buffer, col, margin);
338                             if (wmove(work, row, col2) != ERR) {
339                                 WInsNStr(work, buffer + col, LEN(col));
340                             }
341                         }
342                     } else {
343                         if (wmove(work, row, col2) != ERR) {
344                             WInsStr(work, buffer);
345                         }
346                     }
347                     break;
348                 case oMoveWindow:
349                     if (n_opt > 1) {
350                         for (col = 0; col < length; col += n_opt) {
351                             col2 = ColOf(buffer, col, margin);
352                             MvWInsNStr(work, row, col2, buffer + col, LEN(col));
353                         }
354                     } else {
355                         MvWInsStr(work, row, col2, buffer);
356                     }
357                     break;
358                 }
359
360                 /* do the corresponding single-character insertion */
361                 row2 = limit + row;
362                 for (col = 0; col < length; ++col) {
363                     col2 = ColOf(buffer, col, margin);
364                     switch (option) {
365                     case oDefault:
366                         if (move(row2, col2) != ERR) {
367                             InsCh((chtype) buffer[col]);
368                         }
369                         break;
370                     case oMove:
371                         MvInsCh(row2, col2, (chtype) buffer[col]);
372                         break;
373                     case oWindow:
374                         if (wmove(work, row2, col2) != ERR) {
375                             WInsCh(work, (chtype) buffer[col]);
376                         }
377                         break;
378                     case oMoveWindow:
379                         MvWInsCh(work, row2, col2, (chtype) buffer[col]);
380                         break;
381                     }
382                 }
383             } else {
384                 beep();
385             }
386             break;
387         default:
388             buffer[length++] = ch;
389             buffer[length] = '\0';
390
391             /* put the string in, one character at a time */
392             col = ColOf(buffer, length - 1, margin);
393             switch (option) {
394             case oDefault:
395                 if (move(row, col) != ERR) {
396                     InsStr(buffer + length - 1);
397                 }
398                 break;
399             case oMove:
400                 MvInsStr(row, col, buffer + length - 1);
401                 break;
402             case oWindow:
403                 if (wmove(work, row, col) != ERR) {
404                     WInsStr(work, buffer + length - 1);
405                 }
406                 break;
407             case oMoveWindow:
408                 MvWInsStr(work, row, col, buffer + length - 1);
409                 break;
410             }
411
412             /* do the corresponding single-character insertion */
413             switch (option) {
414             case oDefault:
415                 if (move(limit + row, col) != ERR) {
416                     InsCh((chtype) ch);
417                 }
418                 break;
419             case oMove:
420                 MvInsCh(limit + row, col, (chtype) ch);
421                 break;
422             case oWindow:
423                 if (wmove(work, limit + row, col) != ERR) {
424                     WInsCh(work, (chtype) ch);
425                 }
426                 break;
427             case oMoveWindow:
428                 MvWInsCh(work, limit + row, col, (chtype) ch);
429                 break;
430             }
431
432             wnoutrefresh(work);
433
434             legend(show, level, option, buffer, length);
435             wnoutrefresh(show);
436
437             doupdate();
438             break;
439         }
440     }
441     if (level > 0) {
442         delwin(show);
443         delwin(work);
444         delwin(look);
445     }
446 }
447
448 static void
449 usage(void)
450 {
451     static const char *tbl[] =
452     {
453         "Usage: inserts [options]"
454         ,""
455         ,"Options:"
456         ,"  -f FILE read data from given file"
457         ,"  -n NUM  limit string-inserts to NUM bytes on ^N replay"
458         ,"  -m      perform wmove/move separately from insert-functions"
459         ,"  -w      use window-parameter even when stdscr would be implied"
460     };
461     unsigned n;
462     for (n = 0; n < SIZEOF(tbl); ++n)
463         fprintf(stderr, "%s\n", tbl[n]);
464     ExitProgram(EXIT_FAILURE);
465 }
466
467 int
468 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
469 {
470     int ch;
471
472     setlocale(LC_ALL, "");
473
474     while ((ch = getopt(argc, argv, "f:mn:w")) != -1) {
475         switch (ch) {
476         case 'f':
477             init_linedata(optarg);
478             break;
479         case 'm':
480             m_opt = TRUE;
481             break;
482         case 'n':
483             n_opt = atoi(optarg);
484             if (n_opt == 0)
485                 n_opt = -1;
486             break;
487         case 'w':
488             w_opt = TRUE;
489             break;
490         default:
491             usage();
492             break;
493         }
494     }
495     if (optind < argc)
496         usage();
497
498     test_inserts(0);
499     endwin();
500     ExitProgram(EXIT_SUCCESS);
501 }
502 #else
503 int
504 main(void)
505 {
506     printf("This program requires the wide-ncurses library\n");
507     ExitProgram(EXIT_FAILURE);
508 }
509 #endif