1 /****************************************************************************
2 * Copyright 2019-2022,2023 Thomas E. Dickey *
3 * Copyright 2008-2016,2017 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
30 * $Id: clip_printw.c,v 1.23 2023/09/30 19:57:44 tom Exp $
32 * demonstrate how to use printw with/without wrapping.
35 #include <test.priv.h>
36 #include <popup_msg.h>
40 #define SHOW(n) ((n) == ERR ? "ERR" : "OK")
41 #define COLOR_DEFAULT (-1)
46 #define Y_TOP (Y_BASE + MARGIN)
66 clip_wprintw(WINDOW *win, bool single, NCURSES_CONST char *fmt, ...)
68 int y0, x0, y1, x1, width;
74 width = getmaxx(win) - x0;
77 * Allocate a single-line derived window extending from the current
78 * cursor position to the end of the current line in the given window.
79 * Disable scrolling in the derived window.
81 sub = derwin(win, 1, width, y0, x0);
84 * Allow printw to wrap through the entire window.
86 sub = derwin(win, getmaxy(win), getmaxx(win), 0, 0);
95 rc = vw_printw(sub, fmt, ap);
102 wmove(win, y1 + y0, x1 + x0);
111 color_params(unsigned state, int *pair)
119 { 0, COLOR_DEFAULT, COLOR_DEFAULT, "default" },
120 { 1, COLOR_RED, COLOR_BLACK, "red/black" },
121 { 2, COLOR_WHITE, COLOR_BLUE, "white/blue" },
125 const char *result = 0;
128 static bool first = TRUE;
134 for (n = 0; n < SIZEOF(table); ++n) {
135 init_pair((short) table[n].pair,
137 (short) table[n].bg);
140 if (state < SIZEOF(table)) {
141 *pair = table[state].pair;
142 result = table[state].msg;
149 video_params(unsigned state, attr_t *attr)
156 { WA_NORMAL, "normal" },
158 { WA_REVERSE, "reverse" },
159 { WA_UNDERLINE, "underline" },
160 { WA_BLINK, "blink" },
164 const char *result = 0;
166 if (state < SIZEOF(table)) {
167 *attr = table[state].attr;
168 result = table[state].msg;
173 /* fill the window with a test-pattern */
175 fill_window(WINDOW *win)
177 int y_top = (win == stdscr) ? Y_BASE : MARGIN;
179 int y0 = -1, x0 = -1;
182 wmove(win, y_top, 0);
183 while (waddstr(win, "0123456789 abcdefghijklmnopqrstuvwxyz ") != ERR) {
186 if (y1 == y0 && x1 == x0)
195 show_status(WINDOW *win, STATUS * sp)
200 wattron(win, A_REVERSE);
202 wprintw(win, "Clip %s", sp->single ? "line" : "window");
203 wprintw(win, " Count %d", sp->count);
205 wprintw(win, " Video %s", sp->v_msg);
207 wprintw(win, " Color %s", sp->c_msg);
208 wprintw(win, " (%d)", sp->status);
210 wattroff(win, A_REVERSE);
215 do_subwindow(WINDOW *win, STATUS * sp, void func(WINDOW *))
217 WINDOW *win1 = newwin(sp->y_max - (2 * MARGIN),
218 sp->x_max - (2 * MARGIN),
222 if (win1 != 0 && sp->y_max > 4 && sp->x_max > 4) {
223 WINDOW *win2 = derwin(win1,
224 sp->y_max - (2 * MARGIN) - 2,
225 sp->x_max - (2 * MARGIN) - 2,
226 (win == stdscr) ? Y_BASE : Y_BASE,
248 * The status line is within the same window as the test-data.
251 init_status(WINDOW *win, STATUS * sp)
253 memset(sp, 0, sizeof(*sp));
262 getbegyx(win, sp->y_beg, sp->x_beg);
263 getmaxyx(win, sp->y_max, sp->x_max);
265 wmove(win, sp->y_val = Y_BASE, sp->x_val = 0);
269 show_help(WINDOW *win)
271 static const char *msgs[] =
274 ,"Use h/j/k/l or arrow keys to move the cursor."
275 ,"Set the count parameter for clip_wprintw by entering digits 0-9."
278 ,"space toggles through the set of video attributes and colors."
279 ,"q exit subwindow, quit if last window."
280 ,"s toggles single-line updates versus entire windows."
281 ,"t touches (forces repaint) of the current line."
282 ,"w create subwindow."
283 ,". calls vw_printw at the current position with the given count."
284 ,"= resets count to zero."
285 ,"? shows this help-window"
289 popup_msg(win, msgs);
293 update_status(WINDOW *win, STATUS * sp)
298 case ' ': /* next test-iteration */
300 if ((sp->c_msg = color_params(++(sp->c), &(sp->pair))) == 0) {
301 sp->c_msg = color_params(sp->c = 0, &(sp->pair));
302 if ((sp->v_msg = video_params(++(sp->v), &(sp->attr))) == 0) {
303 sp->v_msg = video_params(sp->v = 0, &(sp->attr));
307 if ((sp->v_msg = video_params(++(sp->v), &(sp->attr))) == 0) {
308 sp->v_msg = video_params(sp->v = 0, &(sp->attr));
312 show_status(win, sp);
316 wmove(win, sp->y_val, sp->x_val = margin);
320 wmove(win, sp->y_val, sp->x_val = (sp->x_max - margin - 1));
324 wmove(win, sp->y_val = Y_BASE, sp->x_val);
328 wmove(win, sp->y_val = (sp->y_max - margin - 1), sp->x_val);
332 if (sp->x_val > margin)
333 wmove(win, sp->y_val, --(sp->x_val));
337 if (sp->y_val < (sp->y_max - margin))
338 wmove(win, ++(sp->y_val), sp->x_val);
342 if (sp->y_val > Y_BASE)
343 wmove(win, --(sp->y_val), sp->x_val);
347 if (sp->x_val < (sp->x_max - margin))
348 wmove(win, sp->y_val, ++(sp->x_val));
351 touchline(win, sp->y_val, 1);
355 show_status(win, sp);
358 sp->single = !sp->single;
359 show_status(win, sp);
365 if (isdigit(sp->ch)) {
366 sp->count = (sp->count * 10) + (sp->ch - '0');
367 show_status(win, sp);
376 test_clipping(WINDOW *win)
383 init_status(win, &st);
387 case '.': /* change from current position */
388 (void) wattrset(win, AttrArg(COLOR_PAIR(st.pair), st.attr));
390 need = (unsigned) st.count + 1;
391 _nc_SPRINTF(fmt, _nc_SLIMIT(sizeof(fmt)) "%%c%%%ds%%c", st.count);
393 int want = getmaxx(win);
396 need = (unsigned) want - 1;
397 _nc_STRCPY(fmt, "%c%s%c", sizeof(fmt));
399 if ((buffer = typeMalloc(char, need + 1)) != 0) {
400 for (j = 0; j < need; ++j) {
401 buffer[j] = (char) ('A' + (j % 26));
403 buffer[need - 1] = '\0';
404 st.status = clip_wprintw(win, st.single, fmt, '[', buffer, ']');
409 do_subwindow(win, &st, test_clipping);
414 update_status(win, &st);
417 } while ((st.ch = wgetch(win)) != ERR);
423 static const char *msg[] =
425 "Usage: clip_printw [options]"
431 for (n = 0; n < SIZEOF(msg); n++)
432 fprintf(stderr, "%s\n", msg[n]);
434 ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
441 main(int argc, char *argv[])
445 while ((ch = getopt(argc, argv, OPTS_COMMON)) != -1) {
449 ExitProgram(EXIT_SUCCESS);
451 usage(ch == OPTS_USAGE);
458 setlocale(LC_ALL, "");
463 test_clipping(stdscr);
466 ExitProgram(EXIT_SUCCESS);
473 printf("This program requires the curses vw_printw function\n");
474 ExitProgram(EXIT_FAILURE);