]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/inserts.c
990da1d4fe99c8e95609fd6ff5d1db1b99efadf2
[ncurses.git] / test / inserts.c
1 /*
2  * $Id: inserts.c,v 1.5 2003/08/09 22:07:06 tom Exp $
3  *
4  * Demonstrate the winsstr() and winsch functions.
5  * Thomas Dickey - 2002/10/19
6  */
7
8 #include <test.priv.h>
9
10 #define TABSIZE 8
11
12 static int margin = (2 * TABSIZE) - 1;
13
14 static void
15 legend(WINDOW *win, char *buffer, int length)
16 {
17     wmove(win, 0, 0);
18     wprintw(win,
19             "The Strings/Chars displays should match.  Enter any characters.\n");
20     wprintw(win,
21             "Use down-arrow or ^N to repeat on the next line, 'q' to exit.\n");
22     wclrtoeol(win);
23     wprintw(win, "Inserted %d characters <%s>", length, buffer);
24 }
25
26 static int
27 ColOf(char *buffer, int length)
28 {
29     int n;
30     int result;
31
32     for (n = 0, result = margin + 1; n < length; ++n) {
33         int ch = UChar(buffer[n]);
34         switch (ch) {
35         case '\n':
36             /* actually newline should clear the remainder of the line
37              * and move to the next line - but that seems a little awkward
38              * in this example.
39              */
40         case '\r':
41             result = 0;
42             break;
43         case '\b':
44             if (result > 0)
45                 --result;
46             break;
47         case '\t':
48             result += (TABSIZE - (result % TABSIZE));
49             break;
50         case '\177':
51             result += 2;
52             break;
53         default:
54             ++result;
55             if (ch < 32)
56                 ++result;
57             break;
58         }
59     }
60     return result;
61 }
62
63 int
64 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
65 {
66     int ch;
67     int limit;
68     int row = 1;
69     int col;
70     int length;
71     char buffer[BUFSIZ];
72     WINDOW *work;
73     WINDOW *show;
74
75     putenv("TABSIZE=8");
76     initscr();
77     (void) cbreak();            /* take input chars one at a time, no wait for \n */
78     (void) noecho();            /* don't echo input */
79     keypad(stdscr, TRUE);
80
81     limit = LINES - 5;
82     work = newwin(limit, COLS, 0, 0);
83     show = newwin(4, COLS, limit + 1, 0);
84     keypad(work, TRUE);
85
86     for (col = margin + 1; col < COLS; col += TABSIZE)
87         mvwvline(work, row, col, '.', limit - 2);
88
89     box(work, 0, 0);
90     mvwvline(work, row, margin, ACS_VLINE, limit - 2);
91     mvwvline(work, row, margin + 1, ACS_VLINE, limit - 2);
92     limit /= 2;
93
94     mvwaddstr(work, 1, 2, "String");
95     mvwaddstr(work, limit + 1, 2, "Chars");
96     wnoutrefresh(work);
97
98     buffer[length = 0] = '\0';
99     legend(show, buffer, length);
100     wnoutrefresh(show);
101
102     doupdate();
103
104     /*
105      * Show the characters inserted in color, to distinguish from those that
106      * are shifted.
107      */
108     if (has_colors()) {
109         start_color();
110         init_pair(1, COLOR_WHITE, COLOR_BLUE);
111         wbkgdset(work, COLOR_PAIR(1) | ' ');
112     }
113
114     while ((ch = wgetch(work)) != 'q') {
115         wmove(work, row, margin + 1);
116         switch (ch) {
117         case CTRL('N'):
118         case KEY_DOWN:
119             if (row < limit) {
120                 ++row;
121                 /* put the whole string in, all at once */
122                 mvwinsstr(work, row, margin + 1, buffer);
123
124                 /* do the corresponding single-character insertion */
125                 for (col = 0; col < length; ++col) {
126                     mvwinsch(work, limit + row, ColOf(buffer, col), buffer[col]);
127                 }
128             } else {
129                 beep();
130             }
131             break;
132         case KEY_BACKSPACE:
133             ch = '\b';
134             /* FALLTHRU */
135         default:
136             if (ch <= 0 || ch > 255) {
137                 beep();
138                 break;
139             }
140             buffer[length++] = ch;
141             buffer[length] = '\0';
142             /* put the string in, one character at a time */
143             mvwinsstr(work,
144                       row,
145                       ColOf(buffer, length - 1), buffer + length - 1);
146
147             /* do the corresponding single-character insertion */
148             mvwinsch(work,
149                      limit + row,
150                      ColOf(buffer, length - 1), ch);
151             wnoutrefresh(work);
152
153             legend(show, buffer, length);
154             wnoutrefresh(show);
155
156             doupdate();
157             break;
158         }
159     }
160     endwin();
161     ExitProgram(EXIT_SUCCESS);
162 }