1 /****************************************************************************
2 * Copyright 2019,2020 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.19 2020/05/10 00:40:23 tom Exp $
32 * demonstrate how to use printw 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)
60 clip_wprintw(WINDOW *win, NCURSES_CONST char *fmt, ...)
62 int y0, x0, y1, x1, width;
68 * Allocate a single-line derived window extending from the current
69 * cursor position to the end of the current line in the given window.
70 * Disable scrolling in the derived window.
73 width = getmaxx(win) - x0;
74 sub = derwin(win, 1, width, y0, x0);
81 rc = vw_printw(sub, fmt, ap);
87 wmove(win, y1 + y0, x1 + x0);
93 color_params(unsigned state, int *pair)
101 { 0, COLOR_DEFAULT, COLOR_DEFAULT, "default" },
102 { 1, COLOR_RED, COLOR_BLACK, "red/black" },
103 { 2, COLOR_WHITE, COLOR_BLUE, "white/blue" },
107 const char *result = 0;
110 static bool first = TRUE;
116 for (n = 0; n < SIZEOF(table); ++n) {
117 init_pair((short) table[n].pair,
119 (short) table[n].bg);
122 if (state < SIZEOF(table)) {
123 *pair = table[state].pair;
124 result = table[state].msg;
131 video_params(unsigned state, attr_t *attr)
138 { WA_NORMAL, "normal" },
140 { WA_REVERSE, "reverse" },
141 { WA_UNDERLINE, "underline" },
142 { WA_BLINK, "blink" },
146 const char *result = 0;
148 if (state < SIZEOF(table)) {
149 *attr = table[state].attr;
150 result = table[state].msg;
155 /* fill the window with a test-pattern */
157 fill_window(WINDOW *win)
160 int y0 = -1, x0 = -1;
164 while (waddstr(win, "0123456789 abcdefghijklmnopqrstuvwxyz ") != ERR) {
167 if (y1 == y0 && x1 == x0)
176 show_status(WINDOW *win, STATUS * sp)
182 wprintw(win, "Count %d", sp->count);
184 wprintw(win, " Video %s", sp->v_msg);
186 wprintw(win, " Color %s", sp->c_msg);
187 wprintw(win, " (%d)", sp->status);
193 do_subwindow(WINDOW *win, STATUS * sp, void func(WINDOW *))
195 WINDOW *win1 = newwin(sp->y_max - 2, sp->x_max - 2,
196 sp->y_beg + 1, sp->x_beg + 1);
198 if (win1 != 0 && sp->y_max > 4 && sp->x_max > 4) {
199 WINDOW *win2 = derwin(win1, sp->y_max - 4, sp->x_max - 4, 1, 1);
220 init_status(WINDOW *win, STATUS * sp)
222 memset(sp, 0, sizeof(*sp));
230 getbegyx(win, sp->y_beg, sp->x_beg);
231 getmaxyx(win, sp->y_max, sp->x_max);
235 show_help(WINDOW *win)
237 static const char *msgs[] =
240 ,"Use h/j/k/l or arrow keys to move the cursor."
241 ,"Set the count parameter for clip_wprintw by entering digits 0-9."
244 ,"space toggles through the set of video attributes and colors."
245 ,"t touches (forces repaint) of the current line."
246 ,". calls vw_printw at the current position with the given count."
247 ,"= resets count to zero."
248 ,"? shows this help-window"
252 popup_msg(win, msgs);
256 update_status(WINDOW *win, STATUS * sp)
259 case ' ': /* next test-iteration */
261 if ((sp->c_msg = color_params(++(sp->c), &(sp->pair))) == 0) {
262 sp->c_msg = color_params(sp->c = 0, &(sp->pair));
263 if ((sp->v_msg = video_params(++(sp->v), &(sp->attr))) == 0) {
264 sp->v_msg = video_params(sp->v = 0, &(sp->attr));
268 if ((sp->v_msg = video_params(++(sp->v), &(sp->attr))) == 0) {
269 sp->v_msg = video_params(sp->v = 0, &(sp->attr));
273 show_status(win, sp);
278 wmove(win, sp->y_val, --(sp->x_val));
282 if (sp->y_val < sp->y_max)
283 wmove(win, ++(sp->y_val), sp->x_val);
288 wmove(win, --(sp->y_val), sp->x_val);
292 if (sp->x_val < sp->x_max)
293 wmove(win, sp->y_val, ++(sp->x_val));
296 touchline(win, sp->y_val, 1);
300 show_status(win, sp);
306 if (isdigit(sp->ch)) {
307 sp->count = (sp->count * 10) + (sp->ch - '0');
308 show_status(win, sp);
317 test_clipping(WINDOW *win)
324 init_status(win, &st);
328 case '.': /* change from current position */
329 (void) wattrset(win, AttrArg(COLOR_PAIR(st.pair), st.attr));
331 need = (unsigned) st.count + 1;
332 _nc_SPRINTF(fmt, _nc_SLIMIT(sizeof(fmt)) "%%c%%%ds%%c", st.count);
334 int want = getmaxx(win);
337 need = (unsigned) want - 1;
338 _nc_STRCPY(fmt, "%c%s%c", sizeof(fmt));
340 if ((buffer = typeMalloc(char, need + 1)) != 0) {
341 for (j = 0; j < need; ++j) {
342 buffer[j] = (char) ('A' + (j % 26));
344 buffer[need - 1] = '\0';
345 st.status = clip_wprintw(win, fmt, '[', buffer, ']');
350 do_subwindow(win, &st, test_clipping);
355 update_status(win, &st);
358 } while ((st.ch = wgetch(win)) != ERR);
362 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
368 test_clipping(stdscr);
371 ExitProgram(EXIT_SUCCESS);
378 printf("This program requires the curses vw_printw function\n");
379 ExitProgram(EXIT_FAILURE);