]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/clip_printw.c
ncurses 6.1 - patch 20200118
[ncurses.git] / test / clip_printw.c
1 /****************************************************************************
2  * Copyright (c) 2008-2017,2019 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: clip_printw.c,v 1.16 2019/08/17 21:49:40 tom Exp $
30  *
31  * demonstrate how to use printw without wrapping.
32  */
33
34 #include <test.priv.h>
35 #include <popup_msg.h>
36
37 #ifdef HAVE_VW_PRINTW
38
39 #define SHOW(n) ((n) == ERR ? "ERR" : "OK")
40 #define COLOR_DEFAULT (-1)
41
42 typedef struct {
43     unsigned c;
44     unsigned v;
45     int status;
46     int pair;
47     attr_t attr;
48     int count;
49     int ch;
50     const char *c_msg;
51     const char *v_msg;
52     int y_val;
53     int x_val;
54     int y_beg, x_beg;
55     int y_max, x_max;
56 } STATUS;
57
58 static int
59 clip_wprintw(WINDOW *win, NCURSES_CONST char *fmt, ...)
60 {
61     int y0, x0, y1, x1, width;
62     WINDOW *sub;
63     va_list ap;
64     int rc;
65
66     /*
67      * Allocate a single-line derived window extending from the current
68      * cursor position to the end of the current line in the given window.
69      * Disable scrolling in the derived window.
70      */
71     getyx(win, y0, x0);
72     width = getmaxx(win) - x0;
73     sub = derwin(win, 1, width, y0, x0);
74     scrollok(sub, FALSE);
75
76     /*
77      * Print the text.
78      */
79     va_start(ap, fmt);
80     rc = vw_printw(sub, fmt, ap);
81     va_end(ap);
82
83     getyx(sub, y1, x1);
84     delwin(sub);
85
86     wmove(win, y1 + y0, x1 + x0);
87
88     return rc;
89 }
90
91 static const char *
92 color_params(unsigned state, int *pair)
93 {
94     /* *INDENT-OFF* */
95     static struct {
96         int pair;
97         int fg, bg;
98         const char *msg;
99     } table[] = {
100         { 0, COLOR_DEFAULT, COLOR_DEFAULT, "default" },
101         { 1, COLOR_RED,     COLOR_BLACK,   "red/black" },
102         { 2, COLOR_WHITE,   COLOR_BLUE,    "white/blue" },
103     };
104     /* *INDENT-ON* */
105
106     const char *result = 0;
107
108     if (has_colors()) {
109         static bool first = TRUE;
110
111         if (first) {
112             unsigned n;
113
114             start_color();
115             for (n = 0; n < SIZEOF(table); ++n) {
116                 init_pair((short) table[n].pair,
117                           (short) table[n].fg,
118                           (short) table[n].bg);
119             }
120         }
121         if (state < SIZEOF(table)) {
122             *pair = table[state].pair;
123             result = table[state].msg;
124         }
125     }
126     return result;
127 }
128
129 static const char *
130 video_params(unsigned state, attr_t *attr)
131 {
132     /* *INDENT-OFF* */
133     static struct {
134         attr_t attr;
135         const char *msg;
136     } table[] = {
137         { WA_NORMAL,    "normal" },
138         { WA_BOLD,      "bold" },
139         { WA_REVERSE,   "reverse" },
140         { WA_UNDERLINE, "underline" },
141         { WA_BLINK,     "blink" },
142     };
143     /* *INDENT-ON* */
144
145     const char *result = 0;
146
147     if (state < SIZEOF(table)) {
148         *attr = table[state].attr;
149         result = table[state].msg;
150     }
151     return result;
152 }
153
154 /* fill the window with a test-pattern */
155 static void
156 fill_window(WINDOW *win)
157 {
158     int y, x;
159     int y0 = -1, x0 = -1;
160
161     getyx(win, y, x);
162     wmove(win, 0, 0);
163     while (waddstr(win, "0123456789 abcdefghijklmnopqrstuvwxyz ") != ERR) {
164         int y1, x1;
165         getyx(win, y1, x1);
166         if (y1 == y0 && x1 == x0)
167             break;
168         x0 = x1;
169         y0 = y1;
170     }
171     wmove(win, y, x);
172 }
173
174 static void
175 show_status(WINDOW *win, STATUS * sp)
176 {
177     int y, x;
178
179     getyx(win, y, x);
180     wmove(win, 0, 0);
181     wprintw(win, "Count %d", sp->count);
182     if (sp->v_msg != 0)
183         wprintw(win, " Video %s", sp->v_msg);
184     if (sp->c_msg != 0)
185         wprintw(win, " Color %s", sp->c_msg);
186     wprintw(win, " (%d)", sp->status);
187     wclrtoeol(win);
188     wmove(win, y, x);
189 }
190
191 static void
192 do_subwindow(WINDOW *win, STATUS * sp, void func(WINDOW *))
193 {
194     WINDOW *win1 = newwin(sp->y_max - 2, sp->x_max - 2,
195                           sp->y_beg + 1, sp->x_beg + 1);
196
197     if (win1 != 0 && sp->y_max > 4 && sp->x_max > 4) {
198         WINDOW *win2 = derwin(win1, sp->y_max - 4, sp->x_max - 4, 1, 1);
199
200         if (win2 != 0) {
201             box(win1, 0, 0);
202             wrefresh(win1);
203             func(win2);
204
205             delwin(win2);
206         } else {
207             beep();
208         }
209         delwin(win1);
210         touchwin(win);
211     } else {
212         if (win1)
213             delwin(win1);
214         beep();
215     }
216 }
217
218 static void
219 init_status(WINDOW *win, STATUS * sp)
220 {
221     memset(sp, 0, sizeof(*sp));
222     sp->c = 99;
223     sp->v = 99;
224     sp->ch = ' ';
225
226     keypad(win, TRUE);
227     fill_window(win);
228
229     getbegyx(win, sp->y_beg, sp->x_beg);
230     getmaxyx(win, sp->y_max, sp->x_max);
231 }
232
233 static void
234 show_help(WINDOW *win)
235 {
236     static const char *msgs[] =
237     {
238         "Basic commands:"
239         ,"Use h/j/k/l or arrow keys to move the cursor."
240         ,"Set the count parameter for clip_wprintw by entering digits 0-9."
241         ,""
242         ,"Other commands:"
243         ,"space toggles through the set of video attributes and colors."
244         ,"t     touches (forces repaint) of the current line."
245         ,".     calls vw_printw at the current position with the given count."
246         ,"=     resets count to zero."
247         ,"?     shows this help-window"
248         ,0
249     };
250
251     popup_msg(win, msgs);
252 }
253
254 static void
255 update_status(WINDOW *win, STATUS * sp)
256 {
257     switch (sp->ch) {
258     case ' ':                   /* next test-iteration */
259         if (has_colors()) {
260             if ((sp->c_msg = color_params(++(sp->c), &(sp->pair))) == 0) {
261                 sp->c_msg = color_params(sp->c = 0, &(sp->pair));
262                 if ((sp->v_msg = video_params(++(sp->v), &(sp->attr))) == 0) {
263                     sp->v_msg = video_params(sp->v = 0, &(sp->attr));
264                 }
265             }
266         } else {
267             if ((sp->v_msg = video_params(++(sp->v), &(sp->attr))) == 0) {
268                 sp->v_msg = video_params(sp->v = 0, &(sp->attr));
269             }
270         }
271         sp->count = 0;
272         show_status(win, sp);
273         break;
274     case KEY_LEFT:
275     case 'h':
276         if (sp->x_val > 0)
277             wmove(win, sp->y_val, --(sp->x_val));
278         break;
279     case KEY_DOWN:
280     case 'j':
281         if (sp->y_val < sp->y_max)
282             wmove(win, ++(sp->y_val), sp->x_val);
283         break;
284     case KEY_UP:
285     case 'k':
286         if (sp->y_val > 0)
287             wmove(win, --(sp->y_val), sp->x_val);
288         break;
289     case KEY_RIGHT:
290     case 'l':
291         if (sp->x_val < sp->x_max)
292             wmove(win, sp->y_val, ++(sp->x_val));
293         break;
294     case 't':
295         touchline(win, sp->y_val, 1);
296         break;
297     case '=':
298         sp->count = 0;
299         show_status(win, sp);
300         break;
301     case HELP_KEY_1:
302         show_help(win);
303         break;
304     default:
305         if (isdigit(sp->ch)) {
306             sp->count = (sp->count * 10) + (sp->ch - '0');
307             show_status(win, sp);
308         } else {
309             beep();
310         }
311         break;
312     }
313 }
314
315 static void
316 test_clipping(WINDOW *win)
317 {
318     STATUS st;
319     char fmt[80];
320     char *buffer;
321     unsigned j, need;
322
323     init_status(win, &st);
324
325     do {
326         switch (st.ch) {
327         case '.':               /* change from current position */
328             (void) wattrset(win, AttrArg(COLOR_PAIR(st.pair), st.attr));
329             if (st.count > 0) {
330                 need = (unsigned) st.count + 1;
331                 _nc_SPRINTF(fmt, _nc_SLIMIT(sizeof(fmt)) "%%c%%%ds%%c", st.count);
332             } else {
333                 need = (unsigned) getmaxx(win) - 1;
334                 _nc_STRCPY(fmt, "%c%s%c", sizeof(fmt));
335             }
336             if ((buffer = typeMalloc(char, need + 1)) != 0) {
337                 for (j = 0; j < need; ++j) {
338                     buffer[j] = (char) ('A' + (j % 26));
339                 }
340                 buffer[need - 1] = '\0';
341                 st.status = clip_wprintw(win, fmt, '[', buffer, ']');
342                 free(buffer);
343             }
344             break;
345         case 'w':
346             do_subwindow(win, &st, test_clipping);
347             break;
348         case 'q':
349             return;
350         default:
351             update_status(win, &st);
352             break;
353         }
354     } while ((st.ch = wgetch(win)) != ERR);
355 }
356
357 int
358 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
359 {
360     initscr();
361     cbreak();
362     noecho();
363
364     test_clipping(stdscr);
365     endwin();
366
367     ExitProgram(EXIT_SUCCESS);
368 }
369
370 #else
371 int
372 main(void)
373 {
374     printf("This program requires the curses vw_printw function\n");
375     ExitProgram(EXIT_FAILURE);
376 }
377 #endif