1 /****************************************************************************
2 * Copyright (c) 2008-2014,2016 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.12 2016/09/10 21:21:44 tom Exp $
31 * demonstrate how to use printw without wrapping.
34 #include <test.priv.h>
38 #define SHOW(n) ((n) == ERR ? "ERR" : "OK")
39 #define COLOR_DEFAULT (-1)
58 clip_wprintw(WINDOW *win, NCURSES_CONST char *fmt,...)
60 int y0, x0, y1, x1, width;
66 * Allocate a single-line derived window extending from the current
67 * cursor position to the end of the current line in the given window.
68 * Disable scrolling in the derived window.
71 width = getmaxx(win) - x0;
72 sub = derwin(win, 1, width, y0, x0);
79 rc = vw_printw(sub, fmt, ap);
85 wmove(win, y1 + y0, x1 + x0);
91 color_params(unsigned state, int *pair)
99 { 0, COLOR_DEFAULT, COLOR_DEFAULT, "default" },
100 { 1, COLOR_RED, COLOR_BLACK, "red/black" },
101 { 2, COLOR_WHITE, COLOR_BLUE, "white/blue" },
105 static bool first = TRUE;
106 const char *result = 0;
113 for (n = 0; n < SIZEOF(table); ++n) {
114 init_pair((short) table[n].pair,
116 (short) table[n].bg);
119 if (state < SIZEOF(table)) {
120 *pair = table[state].pair;
121 result = table[state].msg;
128 video_params(unsigned state, attr_t *attr)
135 { A_NORMAL, "normal" },
137 { A_REVERSE, "reverse" },
138 { A_UNDERLINE, "underline" },
139 { A_BLINK, "blink" },
143 const char *result = 0;
145 if (state < SIZEOF(table)) {
146 *attr = table[state].attr;
147 result = table[state].msg;
152 /* fill the window with a test-pattern */
154 fill_window(WINDOW *win)
157 int y0 = -1, x0 = -1;
161 while (waddstr(win, "0123456789 abcdefghijklmnopqrstuvwxyz ") != ERR) {
164 if (y1 == y0 && x1 == x0)
173 show_status(WINDOW *win, STATUS * sp)
179 wprintw(win, "Count %d", sp->count);
181 wprintw(win, " Video %s", sp->v_msg);
183 wprintw(win, " Color %s", sp->c_msg);
184 wprintw(win, " (%d)", sp->status);
190 do_subwindow(WINDOW *win, STATUS * sp, void func(WINDOW *))
192 WINDOW *win1 = newwin(sp->y_max - 2, sp->x_max - 2,
193 sp->y_beg + 1, sp->x_beg + 1);
195 if (win1 != 0 && sp->y_max > 4 && sp->x_max > 4) {
196 WINDOW *win2 = derwin(win1, sp->y_max - 4, sp->x_max - 4, 1, 1);
217 init_status(WINDOW *win, STATUS * sp)
219 memset(sp, 0, sizeof(*sp));
227 getbegyx(win, sp->y_beg, sp->x_beg);
228 getmaxyx(win, sp->y_max, sp->x_max);
232 show_help(WINDOW *win)
234 static const char *table[] =
237 ,"Use h/j/k/l or arrow keys to move the cursor."
238 ,"Set the count parameter for clip_wprintw by entering digits 0-9."
241 ,"space toggles through the set of video attributes and colors."
242 ,"t touches (forces repaint) of the current line."
243 ,". calls clip_wprintw at the current position with the given count."
244 ,"= resets count to zero."
245 ,"? shows this help-window"
252 getmaxyx(win, y_max, x_max);
253 for (row = 0; row < (int) SIZEOF(table) && row < y_max; ++row) {
254 MvWPrintw(win, row, 0, "%.*s", x_max, table[row]);
256 while (wgetch(win) != 'q')
261 update_status(WINDOW *win, STATUS * sp)
264 case ' ': /* next test-iteration */
266 if ((sp->c_msg = color_params(++(sp->c), &(sp->pair))) == 0) {
267 sp->c_msg = color_params(sp->c = 0, &(sp->pair));
268 if ((sp->v_msg = video_params(++(sp->v), &(sp->attr))) == 0) {
269 sp->v_msg = video_params(sp->v = 0, &(sp->attr));
273 if ((sp->v_msg = video_params(++(sp->v), &(sp->attr))) == 0) {
274 sp->v_msg = video_params(sp->v = 0, &(sp->attr));
278 show_status(win, sp);
283 wmove(win, sp->y_val, --(sp->x_val));
287 if (sp->y_val < sp->y_max)
288 wmove(win, ++(sp->y_val), sp->x_val);
293 wmove(win, --(sp->y_val), sp->x_val);
297 if (sp->x_val < sp->x_max)
298 wmove(win, sp->y_val, ++(sp->x_val));
301 touchline(win, sp->y_val, 1);
305 show_status(win, sp);
308 do_subwindow(win, sp, show_help);
311 if (isdigit(sp->ch)) {
312 sp->count = (sp->count * 10) + (sp->ch - '0');
313 show_status(win, sp);
322 test_clipping(WINDOW *win)
329 init_status(win, &st);
333 case '.': /* change from current position */
334 (void) wattrset(win, AttrArg(COLOR_PAIR(st.pair), st.attr));
336 need = (unsigned) st.count + 1;
337 _nc_SPRINTF(fmt, _nc_SLIMIT(sizeof(fmt)) "%%c%%%ds%%c", st.count);
339 need = (unsigned) getmaxx(win) - 1;
340 _nc_STRCPY(fmt, "%c%s%c", sizeof(fmt));
342 if ((buffer = typeMalloc(char, need + 1)) != 0) {
343 for (j = 0; j < need; ++j) {
344 buffer[j] = (char) ('A' + (j % 26));
346 buffer[need - 1] = '\0';
347 st.status = clip_wprintw(win, fmt, '[', buffer, ']');
352 do_subwindow(win, &st, test_clipping);
357 update_status(win, &st);
360 } while ((st.ch = wgetch(win)) != ERR);
364 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
370 test_clipping(stdscr);
373 ExitProgram(EXIT_SUCCESS);
380 printf("This program requires the curses vw_printw function\n");
381 ExitProgram(EXIT_FAILURE);