]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/test_addstr.c
ncurses 5.7 - patch 20100724
[ncurses.git] / test / test_addstr.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_addstr.c,v 1.4 2010/05/01 19:13:46 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     NCURSES_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, 'q' 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     limit = LINES - 5;
167     if (level > 0) {
168         look = newwin(limit, COLS - (2 * (level - 1)), 0, level - 1);
169         work = newwin(limit - 2, COLS - (2 * level), 1, level);
170         show = newwin(4, COLS, limit + 1, 0);
171         box(look, 0, 0);
172         wnoutrefresh(look);
173         limit -= 2;
174     } else {
175         work = stdscr;
176         show = derwin(stdscr, 4, COLS, limit + 1, 0);
177     }
178     keypad(work, TRUE);
179
180     for (col = margin + 1; col < COLS; col += MY_TABSIZE)
181         MvWVLine(work, row, col, '.', limit - 2);
182
183     MvWVLine(work, row, margin, ACS_VLINE, limit - 2);
184     MvWVLine(work, row, margin + 1, ACS_VLINE, limit - 2);
185     limit /= 2;
186
187     MvWAddStr(work, 1, 2, "String");
188     MvWAddStr(work, limit + 1, 2, "Chars");
189     wnoutrefresh(work);
190
191     buffer[length = 0] = '\0';
192     legend(show, level, option, buffer, length);
193     wnoutrefresh(show);
194
195     doupdate();
196
197     /*
198      * Show the characters added in color, to distinguish from those that
199      * are shifted.
200      */
201     if (has_colors()) {
202         start_color();
203         init_pair(1, COLOR_WHITE, COLOR_BLUE);
204         wbkgdset(work, COLOR_PAIR(1) | ' ');
205     }
206
207     while ((ch = read_linedata(work)) != ERR && !isQUIT(ch)) {
208         wmove(work, row, margin + 1);
209         switch (ch) {
210         case key_RECUR:
211             test_adds(level + 1);
212
213             touchwin(look);
214             touchwin(work);
215             touchwin(show);
216
217             wnoutrefresh(look);
218             wnoutrefresh(work);
219             wnoutrefresh(show);
220
221             doupdate();
222             break;
223         case key_NEWLINE:
224             if (row < limit) {
225                 ++row;
226                 /* put the whole string in, all at once */
227                 col2 = margin + 1;
228                 switch (option) {
229                 case oDefault:
230                     if (n_opt > 1) {
231                         for (col = 0; col < length; col += n_opt) {
232                             col2 = ColOf(buffer, col, margin);
233                             if (move(row, col2) != ERR) {
234                                 AddNStr(buffer + col, LEN(col));
235                             }
236                         }
237                     } else {
238                         if (move(row, col2) != ERR) {
239                             AddStr(buffer);
240                         }
241                     }
242                     break;
243                 case oMove:
244                     if (n_opt > 1) {
245                         for (col = 0; col < length; col += n_opt) {
246                             col2 = ColOf(buffer, col, margin);
247                             MvAddNStr(row, col2, buffer + col, LEN(col));
248                         }
249                     } else {
250                         MvAddStr(row, col2, buffer);
251                     }
252                     break;
253                 case oWindow:
254                     if (n_opt > 1) {
255                         for (col = 0; col < length; col += n_opt) {
256                             col2 = ColOf(buffer, col, margin);
257                             if (wmove(work, row, col2) != ERR) {
258                                 WAddNStr(work, buffer + col, LEN(col));
259                             }
260                         }
261                     } else {
262                         if (wmove(work, row, col2) != ERR) {
263                             WAddStr(work, buffer);
264                         }
265                     }
266                     break;
267                 case oMoveWindow:
268                     if (n_opt > 1) {
269                         for (col = 0; col < length; col += n_opt) {
270                             col2 = ColOf(buffer, col, margin);
271                             MvWAddNStr(work, row, col2, buffer + col, LEN(col));
272                         }
273                     } else {
274                         MvWAddStr(work, row, col2, buffer);
275                     }
276                     break;
277                 }
278
279                 /* do the corresponding single-character add */
280                 row2 = limit + row;
281                 for (col = 0; col < length; ++col) {
282                     col2 = ColOf(buffer, col, margin);
283                     switch (option) {
284                     case oDefault:
285                         if (move(row2, col2) != ERR) {
286                             AddCh(UChar(buffer[col]));
287                         }
288                         break;
289                     case oMove:
290                         MvAddCh(row2, col2, UChar(buffer[col]));
291                         break;
292                     case oWindow:
293                         if (wmove(work, row2, col2) != ERR) {
294                             WAddCh(work, UChar(buffer[col]));
295                         }
296                         break;
297                     case oMoveWindow:
298                         MvWAddCh(work, row2, col2, UChar(buffer[col]));
299                         break;
300                     }
301                 }
302             } else {
303                 beep();
304             }
305             break;
306         default:
307             if (ch <= 0 || ch > 255) {
308                 beep();
309                 break;
310             }
311             buffer[length++] = (char) ch;
312             buffer[length] = '\0';
313
314             /* put the string in, one character at a time */
315             col = ColOf(buffer, length - 1, margin);
316             switch (option) {
317             case oDefault:
318                 if (move(row, col) != ERR) {
319                     AddStr(buffer + length - 1);
320                 }
321                 break;
322             case oMove:
323                 MvAddStr(row, col, buffer + length - 1);
324                 break;
325             case oWindow:
326                 if (wmove(work, row, col) != ERR) {
327                     WAddStr(work, buffer + length - 1);
328                 }
329                 break;
330             case oMoveWindow:
331                 MvWAddStr(work, row, col, buffer + length - 1);
332                 break;
333             }
334
335             /* do the corresponding single-character add */
336             switch (option) {
337             case oDefault:
338                 if (move(limit + row, col) != ERR) {
339                     AddCh(UChar(ch));
340                 }
341                 break;
342             case oMove:
343                 MvAddCh(limit + row, col, UChar(ch));
344                 break;
345             case oWindow:
346                 if (wmove(work, limit + row, col) != ERR) {
347                     WAddCh(work, UChar(ch));
348                 }
349                 break;
350             case oMoveWindow:
351                 MvWAddCh(work, limit + row, col, UChar(ch));
352                 break;
353             }
354
355             wnoutrefresh(work);
356
357             legend(show, level, option, buffer, length);
358             wnoutrefresh(show);
359
360             doupdate();
361             break;
362         }
363     }
364     if (level > 0) {
365         delwin(show);
366         delwin(work);
367         delwin(look);
368     }
369 }
370
371 static void
372 usage(void)
373 {
374     static const char *tbl[] =
375     {
376         "Usage: test_addstr [options]"
377         ,""
378         ,"Options:"
379         ,"  -f FILE read data from given file"
380         ,"  -n NUM  limit string-adds to NUM bytes on ^N replay"
381         ,"  -m      perform wmove/move separately from add-functions"
382         ,"  -w      use window-parameter even when stdscr would be implied"
383     };
384     unsigned n;
385     for (n = 0; n < SIZEOF(tbl); ++n)
386         fprintf(stderr, "%s\n", tbl[n]);
387     ExitProgram(EXIT_FAILURE);
388 }
389
390 int
391 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
392 {
393     int ch;
394
395     setlocale(LC_ALL, "");
396
397     while ((ch = getopt(argc, argv, "f:mn:w")) != -1) {
398         switch (ch) {
399         case 'f':
400             init_linedata(optarg);
401             break;
402         case 'm':
403             m_opt = TRUE;
404             break;
405         case 'n':
406             n_opt = atoi(optarg);
407             if (n_opt == 0)
408                 n_opt = -1;
409             break;
410         case 'w':
411             w_opt = TRUE;
412             break;
413         default:
414             usage();
415             break;
416         }
417     }
418     if (optind < argc)
419         usage();
420
421     test_adds(0);
422     endwin();
423     ExitProgram(EXIT_SUCCESS);
424 }