]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/test_addstr.c
ncurses 5.9 - patch 20121124
[ncurses.git] / test / test_addstr.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_addstr.c,v 1.9 2012/11/24 19:49:02 tom Exp $
30  *
31  * Demonstrate the waddstr() and waddch functions.
32  * Thomas Dickey - 2009/9/12
33  */
34
35 #include <test.priv.h>
36
37 #include <linedata.h>
38
39 #define AddNStr    addnstr
40 #define AddStr     addstr
41 #define MvAddNStr  (void) mvaddnstr
42 #define MvWAddNStr (void) mvwaddnstr
43 #define WAddNStr   waddnstr
44 #define WAddStr    waddstr
45
46 #define AddCh      addch
47 #define WAddCh     waddch
48
49 #define MY_TABSIZE 8
50
51 typedef enum {
52     oDefault = 0,
53     oMove = 1,
54     oWindow = 2,
55     oMoveWindow = 3
56 } Options;
57
58 static bool m_opt = FALSE;
59 static bool w_opt = FALSE;
60 static int n_opt = -1;
61
62 static void
63 legend(WINDOW *win, int level, Options state, char *buffer, int length)
64 {
65     const char *showstate;
66
67     switch (state) {
68     default:
69     case oDefault:
70         showstate = "";
71         break;
72     case oMove:
73         showstate = " (mvXXX)";
74         break;
75     case oWindow:
76         showstate = " (winXXX)";
77         break;
78     case oMoveWindow:
79         showstate = " (mvwinXXX)";
80         break;
81     }
82
83     wmove(win, 0, 0);
84     wprintw(win,
85             "The Strings/Chars displays should match.  Enter any characters, except:\n");
86     wprintw(win,
87             "down-arrow or ^N to repeat on next line, ^W for inner window, ESC to exit.\n");
88     wclrtoeol(win);
89     wprintw(win, "Level %d,%s added %d characters <%s>", level,
90             showstate, length, buffer);
91 }
92
93 static int
94 ColOf(char *buffer, int length, int margin)
95 {
96     int n;
97     int result;
98
99     for (n = 0, result = margin + 1; n < length; ++n) {
100         int ch = UChar(buffer[n]);
101         switch (ch) {
102         case '\n':
103             /* actually newline should clear the remainder of the line
104              * and move to the next line - but that seems a little awkward
105              * in this example.
106              */
107         case '\r':
108             result = 0;
109             break;
110         case '\b':
111             if (result > 0)
112                 --result;
113             break;
114         case '\t':
115             result += (MY_TABSIZE - (result % MY_TABSIZE));
116             break;
117         case '\177':
118             result += 2;
119             break;
120         default:
121             ++result;
122             if (ch < 32)
123                 ++result;
124             break;
125         }
126     }
127     return result;
128 }
129
130 #define LEN(n) ((length - (n) > n_opt) ? n_opt : (length - (n)))
131 static void
132 test_adds(int level)
133 {
134     static bool first = TRUE;
135
136     int ch;
137     int limit;
138     int row = 1;
139     int col;
140     int row2, col2;
141     int length;
142     char buffer[BUFSIZ];
143     WINDOW *look = 0;
144     WINDOW *work = 0;
145     WINDOW *show = 0;
146     int margin = (2 * MY_TABSIZE) - 1;
147     Options option = (Options) ((unsigned) (m_opt
148                                             ? oMove
149                                             : oDefault)
150                                 | (unsigned) ((w_opt || (level > 0))
151                                               ? oWindow
152                                               : oDefault));
153
154     if (first) {
155         static char cmd[80];
156         setlocale(LC_ALL, "");
157
158         putenv(strcpy(cmd, "TABSIZE=8"));
159
160         initscr();
161         (void) cbreak();        /* take input chars one at a time, no wait for \n */
162         (void) noecho();        /* don't echo input */
163         keypad(stdscr, TRUE);
164
165         /*
166          * Show the characters added in color, to distinguish from those that
167          * are shifted.
168          */
169         if (has_colors()) {
170             start_color();
171             init_pair(1, COLOR_WHITE, COLOR_BLUE);
172         }
173     }
174
175     limit = LINES - 5;
176     if (level > 0) {
177         look = newwin(limit, COLS - (2 * (level - 1)), 0, level - 1);
178         work = newwin(limit - 2, COLS - (2 * level), 1, level);
179         show = newwin(4, COLS, limit + 1, 0);
180         box(look, 0, 0);
181         wnoutrefresh(look);
182         limit -= 2;
183     } else {
184         work = stdscr;
185         show = derwin(stdscr, 4, COLS, limit + 1, 0);
186     }
187     keypad(work, TRUE);
188
189     for (col = margin + 1; col < COLS; col += MY_TABSIZE)
190         MvWVLine(work, row, col, '.', limit - 2);
191
192     MvWVLine(work, row, margin, ACS_VLINE, limit - 2);
193     MvWVLine(work, row, margin + 1, ACS_VLINE, limit - 2);
194     limit /= 2;
195
196     MvWAddStr(work, 1, 2, "String");
197     MvWAddStr(work, limit + 1, 2, "Chars");
198     wnoutrefresh(work);
199
200     buffer[length = 0] = '\0';
201     legend(show, level, option, buffer, length);
202     wnoutrefresh(show);
203
204     doupdate();
205
206     if (has_colors()) {
207         wbkgdset(work, (chtype) (COLOR_PAIR(1) | ' '));
208     }
209
210     while ((ch = read_linedata(work)) != ERR && !isQUIT(ch)) {
211         wmove(work, row, margin + 1);
212         switch (ch) {
213         case key_RECUR:
214             test_adds(level + 1);
215
216             touchwin(look);
217             touchwin(work);
218             touchwin(show);
219
220             wnoutrefresh(look);
221             wnoutrefresh(work);
222             wnoutrefresh(show);
223
224             doupdate();
225             break;
226         case key_NEWLINE:
227             if (row < limit) {
228                 ++row;
229                 /* put the whole string in, all at once */
230                 col2 = margin + 1;
231                 switch (option) {
232                 case oDefault:
233                     if (n_opt > 1) {
234                         for (col = 0; col < length; col += n_opt) {
235                             col2 = ColOf(buffer, col, margin);
236                             if (move(row, col2) != ERR) {
237                                 AddNStr(buffer + col, LEN(col));
238                             }
239                         }
240                     } else {
241                         if (move(row, col2) != ERR) {
242                             AddStr(buffer);
243                         }
244                     }
245                     break;
246                 case oMove:
247                     if (n_opt > 1) {
248                         for (col = 0; col < length; col += n_opt) {
249                             col2 = ColOf(buffer, col, margin);
250                             MvAddNStr(row, col2, buffer + col, LEN(col));
251                         }
252                     } else {
253                         MvAddStr(row, col2, buffer);
254                     }
255                     break;
256                 case oWindow:
257                     if (n_opt > 1) {
258                         for (col = 0; col < length; col += n_opt) {
259                             col2 = ColOf(buffer, col, margin);
260                             if (wmove(work, row, col2) != ERR) {
261                                 WAddNStr(work, buffer + col, LEN(col));
262                             }
263                         }
264                     } else {
265                         if (wmove(work, row, col2) != ERR) {
266                             WAddStr(work, buffer);
267                         }
268                     }
269                     break;
270                 case oMoveWindow:
271                     if (n_opt > 1) {
272                         for (col = 0; col < length; col += n_opt) {
273                             col2 = ColOf(buffer, col, margin);
274                             MvWAddNStr(work, row, col2, buffer + col, LEN(col));
275                         }
276                     } else {
277                         MvWAddStr(work, row, col2, buffer);
278                     }
279                     break;
280                 }
281
282                 /* do the corresponding single-character add */
283                 row2 = limit + row;
284                 for (col = 0; col < length; ++col) {
285                     col2 = ColOf(buffer, col, margin);
286                     switch (option) {
287                     case oDefault:
288                         if (move(row2, col2) != ERR) {
289                             AddCh(UChar(buffer[col]));
290                         }
291                         break;
292                     case oMove:
293                         MvAddCh(row2, col2, UChar(buffer[col]));
294                         break;
295                     case oWindow:
296                         if (wmove(work, row2, col2) != ERR) {
297                             WAddCh(work, UChar(buffer[col]));
298                         }
299                         break;
300                     case oMoveWindow:
301                         MvWAddCh(work, row2, col2, UChar(buffer[col]));
302                         break;
303                     }
304                 }
305             } else {
306                 beep();
307             }
308             break;
309         default:
310             if (ch <= 0 || ch > 255) {
311                 beep();
312                 break;
313             }
314             buffer[length++] = (char) ch;
315             buffer[length] = '\0';
316
317             /* put the string in, one character at a time */
318             col = ColOf(buffer, length - 1, margin);
319             switch (option) {
320             case oDefault:
321                 if (move(row, col) != ERR) {
322                     AddStr(buffer + length - 1);
323                 }
324                 break;
325             case oMove:
326                 MvAddStr(row, col, buffer + length - 1);
327                 break;
328             case oWindow:
329                 if (wmove(work, row, col) != ERR) {
330                     WAddStr(work, buffer + length - 1);
331                 }
332                 break;
333             case oMoveWindow:
334                 MvWAddStr(work, row, col, buffer + length - 1);
335                 break;
336             }
337
338             /* do the corresponding single-character add */
339             switch (option) {
340             case oDefault:
341                 if (move(limit + row, col) != ERR) {
342                     AddCh(UChar(ch));
343                 }
344                 break;
345             case oMove:
346                 MvAddCh(limit + row, col, UChar(ch));
347                 break;
348             case oWindow:
349                 if (wmove(work, limit + row, col) != ERR) {
350                     WAddCh(work, UChar(ch));
351                 }
352                 break;
353             case oMoveWindow:
354                 MvWAddCh(work, limit + row, col, UChar(ch));
355                 break;
356             }
357
358             wnoutrefresh(work);
359
360             legend(show, level, option, buffer, length);
361             wnoutrefresh(show);
362
363             doupdate();
364             break;
365         }
366     }
367     delwin(show);
368     if (level > 0) {
369         delwin(work);
370         delwin(look);
371     }
372 }
373
374 static void
375 usage(void)
376 {
377     static const char *tbl[] =
378     {
379         "Usage: test_addstr [options]"
380         ,""
381         ,"Options:"
382         ,"  -f FILE read data from given file"
383         ,"  -n NUM  limit string-adds to NUM bytes on ^N replay"
384         ,"  -m      perform wmove/move separately from add-functions"
385         ,"  -w      use window-parameter even when stdscr would be implied"
386     };
387     unsigned n;
388     for (n = 0; n < SIZEOF(tbl); ++n)
389         fprintf(stderr, "%s\n", tbl[n]);
390     ExitProgram(EXIT_FAILURE);
391 }
392
393 int
394 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
395 {
396     int ch;
397
398     setlocale(LC_ALL, "");
399
400     while ((ch = getopt(argc, argv, "f:mn:w")) != -1) {
401         switch (ch) {
402         case 'f':
403             init_linedata(optarg);
404             break;
405         case 'm':
406             m_opt = TRUE;
407             break;
408         case 'n':
409             n_opt = atoi(optarg);
410             if (n_opt == 0)
411                 n_opt = -1;
412             break;
413         case 'w':
414             w_opt = TRUE;
415             break;
416         default:
417             usage();
418             break;
419         }
420     }
421     if (optind < argc)
422         usage();
423
424     test_adds(0);
425     endwin();
426     ExitProgram(EXIT_SUCCESS);
427 }