1 /****************************************************************************
2 * Copyright (c) 2008-2017,2019 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
29 * $Id: clip_printw.c,v 1.16 2019/08/17 21:49:40 tom Exp $
31 * demonstrate how to use printw without wrapping.
34 #include <test.priv.h>
35 #include <popup_msg.h>
39 #define SHOW(n) ((n) == ERR ? "ERR" : "OK")
40 #define COLOR_DEFAULT (-1)
59 clip_wprintw(WINDOW *win, NCURSES_CONST char *fmt, ...)
61 int y0, x0, y1, x1, width;
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.
72 width = getmaxx(win) - x0;
73 sub = derwin(win, 1, width, y0, x0);
80 rc = vw_printw(sub, fmt, ap);
86 wmove(win, y1 + y0, x1 + x0);
92 color_params(unsigned state, int *pair)
100 { 0, COLOR_DEFAULT, COLOR_DEFAULT, "default" },
101 { 1, COLOR_RED, COLOR_BLACK, "red/black" },
102 { 2, COLOR_WHITE, COLOR_BLUE, "white/blue" },
106 const char *result = 0;
109 static bool first = TRUE;
115 for (n = 0; n < SIZEOF(table); ++n) {
116 init_pair((short) table[n].pair,
118 (short) table[n].bg);
121 if (state < SIZEOF(table)) {
122 *pair = table[state].pair;
123 result = table[state].msg;
130 video_params(unsigned state, attr_t *attr)
137 { WA_NORMAL, "normal" },
139 { WA_REVERSE, "reverse" },
140 { WA_UNDERLINE, "underline" },
141 { WA_BLINK, "blink" },
145 const char *result = 0;
147 if (state < SIZEOF(table)) {
148 *attr = table[state].attr;
149 result = table[state].msg;
154 /* fill the window with a test-pattern */
156 fill_window(WINDOW *win)
159 int y0 = -1, x0 = -1;
163 while (waddstr(win, "0123456789 abcdefghijklmnopqrstuvwxyz ") != ERR) {
166 if (y1 == y0 && x1 == x0)
175 show_status(WINDOW *win, STATUS * sp)
181 wprintw(win, "Count %d", sp->count);
183 wprintw(win, " Video %s", sp->v_msg);
185 wprintw(win, " Color %s", sp->c_msg);
186 wprintw(win, " (%d)", sp->status);
192 do_subwindow(WINDOW *win, STATUS * sp, void func(WINDOW *))
194 WINDOW *win1 = newwin(sp->y_max - 2, sp->x_max - 2,
195 sp->y_beg + 1, sp->x_beg + 1);
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);
219 init_status(WINDOW *win, STATUS * sp)
221 memset(sp, 0, sizeof(*sp));
229 getbegyx(win, sp->y_beg, sp->x_beg);
230 getmaxyx(win, sp->y_max, sp->x_max);
234 show_help(WINDOW *win)
236 static const char *msgs[] =
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."
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"
251 popup_msg(win, msgs);
255 update_status(WINDOW *win, STATUS * sp)
258 case ' ': /* next test-iteration */
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));
267 if ((sp->v_msg = video_params(++(sp->v), &(sp->attr))) == 0) {
268 sp->v_msg = video_params(sp->v = 0, &(sp->attr));
272 show_status(win, sp);
277 wmove(win, sp->y_val, --(sp->x_val));
281 if (sp->y_val < sp->y_max)
282 wmove(win, ++(sp->y_val), sp->x_val);
287 wmove(win, --(sp->y_val), sp->x_val);
291 if (sp->x_val < sp->x_max)
292 wmove(win, sp->y_val, ++(sp->x_val));
295 touchline(win, sp->y_val, 1);
299 show_status(win, sp);
305 if (isdigit(sp->ch)) {
306 sp->count = (sp->count * 10) + (sp->ch - '0');
307 show_status(win, sp);
316 test_clipping(WINDOW *win)
323 init_status(win, &st);
327 case '.': /* change from current position */
328 (void) wattrset(win, AttrArg(COLOR_PAIR(st.pair), st.attr));
330 need = (unsigned) st.count + 1;
331 _nc_SPRINTF(fmt, _nc_SLIMIT(sizeof(fmt)) "%%c%%%ds%%c", st.count);
333 need = (unsigned) getmaxx(win) - 1;
334 _nc_STRCPY(fmt, "%c%s%c", sizeof(fmt));
336 if ((buffer = typeMalloc(char, need + 1)) != 0) {
337 for (j = 0; j < need; ++j) {
338 buffer[j] = (char) ('A' + (j % 26));
340 buffer[need - 1] = '\0';
341 st.status = clip_wprintw(win, fmt, '[', buffer, ']');
346 do_subwindow(win, &st, test_clipping);
351 update_status(win, &st);
354 } while ((st.ch = wgetch(win)) != ERR);
358 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
364 test_clipping(stdscr);
367 ExitProgram(EXIT_SUCCESS);
374 printf("This program requires the curses vw_printw function\n");
375 ExitProgram(EXIT_FAILURE);