blob: c34a1e09ff348d1153bb070f54a6153142e8695e [file] [log] [blame]
Steve Kondikae271bc2015-11-15 02:50:53 +01001/****************************************************************************
micky3879b9f5e72025-07-08 18:04:53 -04002 * Copyright 2019-2022,2023 Thomas E. Dickey *
3 * Copyright 2008-2016,2017 Free Software Foundation, Inc. *
Steve Kondikae271bc2015-11-15 02:50:53 +01004 * *
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: *
12 * *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
15 * *
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. *
23 * *
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 *
27 * authorization. *
28 ****************************************************************************/
29/*
micky3879b9f5e72025-07-08 18:04:53 -040030 * $Id: clip_printw.c,v 1.23 2023/09/30 19:57:44 tom Exp $
Steve Kondikae271bc2015-11-15 02:50:53 +010031 *
micky3879b9f5e72025-07-08 18:04:53 -040032 * demonstrate how to use printw with/without wrapping.
Steve Kondikae271bc2015-11-15 02:50:53 +010033 */
34
35#include <test.priv.h>
micky3879b9f5e72025-07-08 18:04:53 -040036#include <popup_msg.h>
Steve Kondikae271bc2015-11-15 02:50:53 +010037
38#ifdef HAVE_VW_PRINTW
39
40#define SHOW(n) ((n) == ERR ? "ERR" : "OK")
41#define COLOR_DEFAULT (-1)
42
micky3879b9f5e72025-07-08 18:04:53 -040043#define X_BASE 0
44#define Y_BASE 1
45#define MARGIN 1
46#define Y_TOP (Y_BASE + MARGIN)
47
Steve Kondikae271bc2015-11-15 02:50:53 +010048typedef struct {
49 unsigned c;
50 unsigned v;
micky3879b9f5e72025-07-08 18:04:53 -040051 bool single;
Steve Kondikae271bc2015-11-15 02:50:53 +010052 int status;
53 int pair;
54 attr_t attr;
55 int count;
56 int ch;
57 const char *c_msg;
58 const char *v_msg;
59 int y_val;
60 int x_val;
61 int y_beg, x_beg;
62 int y_max, x_max;
63} STATUS;
64
65static int
micky3879b9f5e72025-07-08 18:04:53 -040066clip_wprintw(WINDOW *win, bool single, NCURSES_CONST char *fmt, ...)
Steve Kondikae271bc2015-11-15 02:50:53 +010067{
68 int y0, x0, y1, x1, width;
69 WINDOW *sub;
70 va_list ap;
71 int rc;
72
Steve Kondikae271bc2015-11-15 02:50:53 +010073 getyx(win, y0, x0);
74 width = getmaxx(win) - x0;
micky3879b9f5e72025-07-08 18:04:53 -040075 if (single) {
76 /*
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.
80 */
81 sub = derwin(win, 1, width, y0, x0);
82 } else {
83 /*
84 * Allow printw to wrap through the entire window.
85 */
86 sub = derwin(win, getmaxy(win), getmaxx(win), 0, 0);
87 wmove(sub, y0, x0);
88 }
Steve Kondikae271bc2015-11-15 02:50:53 +010089 scrollok(sub, FALSE);
90
91 /*
92 * Print the text.
93 */
94 va_start(ap, fmt);
95 rc = vw_printw(sub, fmt, ap);
96 va_end(ap);
97
98 getyx(sub, y1, x1);
99 delwin(sub);
100
micky3879b9f5e72025-07-08 18:04:53 -0400101 if (single) {
102 wmove(win, y1 + y0, x1 + x0);
103 } else {
104 wmove(win, y1, x1);
105 }
Steve Kondikae271bc2015-11-15 02:50:53 +0100106
107 return rc;
108}
109
110static const char *
111color_params(unsigned state, int *pair)
112{
113 /* *INDENT-OFF* */
114 static struct {
115 int pair;
116 int fg, bg;
117 const char *msg;
118 } table[] = {
119 { 0, COLOR_DEFAULT, COLOR_DEFAULT, "default" },
120 { 1, COLOR_RED, COLOR_BLACK, "red/black" },
121 { 2, COLOR_WHITE, COLOR_BLUE, "white/blue" },
122 };
123 /* *INDENT-ON* */
124
Steve Kondikae271bc2015-11-15 02:50:53 +0100125 const char *result = 0;
126
127 if (has_colors()) {
micky3879b9f5e72025-07-08 18:04:53 -0400128 static bool first = TRUE;
129
Steve Kondikae271bc2015-11-15 02:50:53 +0100130 if (first) {
131 unsigned n;
132
133 start_color();
134 for (n = 0; n < SIZEOF(table); ++n) {
135 init_pair((short) table[n].pair,
136 (short) table[n].fg,
137 (short) table[n].bg);
138 }
139 }
140 if (state < SIZEOF(table)) {
141 *pair = table[state].pair;
142 result = table[state].msg;
143 }
144 }
145 return result;
146}
147
148static const char *
149video_params(unsigned state, attr_t *attr)
150{
151 /* *INDENT-OFF* */
152 static struct {
153 attr_t attr;
154 const char *msg;
155 } table[] = {
micky3879b9f5e72025-07-08 18:04:53 -0400156 { WA_NORMAL, "normal" },
157 { WA_BOLD, "bold" },
158 { WA_REVERSE, "reverse" },
159 { WA_UNDERLINE, "underline" },
160 { WA_BLINK, "blink" },
Steve Kondikae271bc2015-11-15 02:50:53 +0100161 };
162 /* *INDENT-ON* */
163
164 const char *result = 0;
165
166 if (state < SIZEOF(table)) {
167 *attr = table[state].attr;
168 result = table[state].msg;
169 }
170 return result;
171}
172
173/* fill the window with a test-pattern */
174static void
175fill_window(WINDOW *win)
176{
micky3879b9f5e72025-07-08 18:04:53 -0400177 int y_top = (win == stdscr) ? Y_BASE : MARGIN;
Steve Kondikae271bc2015-11-15 02:50:53 +0100178 int y, x;
179 int y0 = -1, x0 = -1;
180
181 getyx(win, y, x);
micky3879b9f5e72025-07-08 18:04:53 -0400182 wmove(win, y_top, 0);
Steve Kondikae271bc2015-11-15 02:50:53 +0100183 while (waddstr(win, "0123456789 abcdefghijklmnopqrstuvwxyz ") != ERR) {
184 int y1, x1;
185 getyx(win, y1, x1);
186 if (y1 == y0 && x1 == x0)
187 break;
188 x0 = x1;
189 y0 = y1;
190 }
191 wmove(win, y, x);
192}
193
194static void
195show_status(WINDOW *win, STATUS * sp)
196{
197 int y, x;
198
199 getyx(win, y, x);
micky3879b9f5e72025-07-08 18:04:53 -0400200 wattron(win, A_REVERSE);
Steve Kondikae271bc2015-11-15 02:50:53 +0100201 wmove(win, 0, 0);
micky3879b9f5e72025-07-08 18:04:53 -0400202 wprintw(win, "Clip %s", sp->single ? "line" : "window");
203 wprintw(win, " Count %d", sp->count);
Steve Kondikae271bc2015-11-15 02:50:53 +0100204 if (sp->v_msg != 0)
205 wprintw(win, " Video %s", sp->v_msg);
206 if (sp->c_msg != 0)
207 wprintw(win, " Color %s", sp->c_msg);
208 wprintw(win, " (%d)", sp->status);
209 wclrtoeol(win);
micky3879b9f5e72025-07-08 18:04:53 -0400210 wattroff(win, A_REVERSE);
Steve Kondikae271bc2015-11-15 02:50:53 +0100211 wmove(win, y, x);
212}
213
214static void
215do_subwindow(WINDOW *win, STATUS * sp, void func(WINDOW *))
216{
micky3879b9f5e72025-07-08 18:04:53 -0400217 WINDOW *win1 = newwin(sp->y_max - (2 * MARGIN),
218 sp->x_max - (2 * MARGIN),
219 sp->y_beg + MARGIN,
220 sp->x_beg + MARGIN);
Steve Kondikae271bc2015-11-15 02:50:53 +0100221
222 if (win1 != 0 && sp->y_max > 4 && sp->x_max > 4) {
micky3879b9f5e72025-07-08 18:04:53 -0400223 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,
227 MARGIN);
Steve Kondikae271bc2015-11-15 02:50:53 +0100228
229 if (win2 != 0) {
230 box(win1, 0, 0);
231 wrefresh(win1);
232 func(win2);
233
234 delwin(win2);
235 } else {
236 beep();
237 }
238 delwin(win1);
239 touchwin(win);
240 } else {
241 if (win1)
242 delwin(win1);
243 beep();
244 }
245}
246
micky3879b9f5e72025-07-08 18:04:53 -0400247/*
248 * The status line is within the same window as the test-data.
249 */
Steve Kondikae271bc2015-11-15 02:50:53 +0100250static void
251init_status(WINDOW *win, STATUS * sp)
252{
253 memset(sp, 0, sizeof(*sp));
micky3879b9f5e72025-07-08 18:04:53 -0400254 sp->single = TRUE;
Steve Kondikae271bc2015-11-15 02:50:53 +0100255 sp->c = 99;
256 sp->v = 99;
257 sp->ch = ' ';
258
259 keypad(win, TRUE);
260 fill_window(win);
261
262 getbegyx(win, sp->y_beg, sp->x_beg);
263 getmaxyx(win, sp->y_max, sp->x_max);
micky3879b9f5e72025-07-08 18:04:53 -0400264
265 wmove(win, sp->y_val = Y_BASE, sp->x_val = 0);
Steve Kondikae271bc2015-11-15 02:50:53 +0100266}
267
268static void
269show_help(WINDOW *win)
270{
micky3879b9f5e72025-07-08 18:04:53 -0400271 static const char *msgs[] =
Steve Kondikae271bc2015-11-15 02:50:53 +0100272 {
273 "Basic commands:"
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."
276 ,""
277 ,"Other commands:"
278 ,"space toggles through the set of video attributes and colors."
micky3879b9f5e72025-07-08 18:04:53 -0400279 ,"q exit subwindow, quit if last window."
280 ,"s toggles single-line updates versus entire windows."
Steve Kondikae271bc2015-11-15 02:50:53 +0100281 ,"t touches (forces repaint) of the current line."
micky3879b9f5e72025-07-08 18:04:53 -0400282 ,"w create subwindow."
283 ,". calls vw_printw at the current position with the given count."
Steve Kondikae271bc2015-11-15 02:50:53 +0100284 ,"= resets count to zero."
285 ,"? shows this help-window"
micky3879b9f5e72025-07-08 18:04:53 -0400286 ,0
Steve Kondikae271bc2015-11-15 02:50:53 +0100287 };
288
micky3879b9f5e72025-07-08 18:04:53 -0400289 popup_msg(win, msgs);
Steve Kondikae271bc2015-11-15 02:50:53 +0100290}
291
292static void
293update_status(WINDOW *win, STATUS * sp)
294{
micky3879b9f5e72025-07-08 18:04:53 -0400295 int margin = 0;
296
Steve Kondikae271bc2015-11-15 02:50:53 +0100297 switch (sp->ch) {
298 case ' ': /* next test-iteration */
299 if (has_colors()) {
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));
304 }
305 }
306 } else {
307 if ((sp->v_msg = video_params(++(sp->v), &(sp->attr))) == 0) {
308 sp->v_msg = video_params(sp->v = 0, &(sp->attr));
309 }
310 }
311 sp->count = 0;
312 show_status(win, sp);
313 break;
micky3879b9f5e72025-07-08 18:04:53 -0400314 case KEY_HOME:
315 case '^':
316 wmove(win, sp->y_val, sp->x_val = margin);
317 break;
318 case KEY_END:
319 case '$':
320 wmove(win, sp->y_val, sp->x_val = (sp->x_max - margin - 1));
321 break;
322 case KEY_PPAGE:
323 case CTRL('B'):
324 wmove(win, sp->y_val = Y_BASE, sp->x_val);
325 break;
326 case KEY_NPAGE:
327 case CTRL('F'):
328 wmove(win, sp->y_val = (sp->y_max - margin - 1), sp->x_val);
329 break;
Steve Kondikae271bc2015-11-15 02:50:53 +0100330 case KEY_LEFT:
331 case 'h':
micky3879b9f5e72025-07-08 18:04:53 -0400332 if (sp->x_val > margin)
Steve Kondikae271bc2015-11-15 02:50:53 +0100333 wmove(win, sp->y_val, --(sp->x_val));
334 break;
335 case KEY_DOWN:
336 case 'j':
micky3879b9f5e72025-07-08 18:04:53 -0400337 if (sp->y_val < (sp->y_max - margin))
Steve Kondikae271bc2015-11-15 02:50:53 +0100338 wmove(win, ++(sp->y_val), sp->x_val);
339 break;
340 case KEY_UP:
341 case 'k':
micky3879b9f5e72025-07-08 18:04:53 -0400342 if (sp->y_val > Y_BASE)
Steve Kondikae271bc2015-11-15 02:50:53 +0100343 wmove(win, --(sp->y_val), sp->x_val);
344 break;
345 case KEY_RIGHT:
346 case 'l':
micky3879b9f5e72025-07-08 18:04:53 -0400347 if (sp->x_val < (sp->x_max - margin))
Steve Kondikae271bc2015-11-15 02:50:53 +0100348 wmove(win, sp->y_val, ++(sp->x_val));
349 break;
350 case 't':
351 touchline(win, sp->y_val, 1);
352 break;
353 case '=':
354 sp->count = 0;
355 show_status(win, sp);
356 break;
micky3879b9f5e72025-07-08 18:04:53 -0400357 case 's':
358 sp->single = !sp->single;
359 show_status(win, sp);
360 break;
361 case HELP_KEY_1:
362 show_help(win);
Steve Kondikae271bc2015-11-15 02:50:53 +0100363 break;
364 default:
365 if (isdigit(sp->ch)) {
366 sp->count = (sp->count * 10) + (sp->ch - '0');
367 show_status(win, sp);
368 } else {
369 beep();
370 }
371 break;
372 }
373}
374
375static void
376test_clipping(WINDOW *win)
377{
378 STATUS st;
379 char fmt[80];
380 char *buffer;
381 unsigned j, need;
382
383 init_status(win, &st);
384
385 do {
386 switch (st.ch) {
387 case '.': /* change from current position */
388 (void) wattrset(win, AttrArg(COLOR_PAIR(st.pair), st.attr));
389 if (st.count > 0) {
390 need = (unsigned) st.count + 1;
micky3879b9f5e72025-07-08 18:04:53 -0400391 _nc_SPRINTF(fmt, _nc_SLIMIT(sizeof(fmt)) "%%c%%%ds%%c", st.count);
Steve Kondikae271bc2015-11-15 02:50:53 +0100392 } else {
micky3879b9f5e72025-07-08 18:04:53 -0400393 int want = getmaxx(win);
394 if (want < 10)
395 want = 10;
396 need = (unsigned) want - 1;
397 _nc_STRCPY(fmt, "%c%s%c", sizeof(fmt));
Steve Kondikae271bc2015-11-15 02:50:53 +0100398 }
399 if ((buffer = typeMalloc(char, need + 1)) != 0) {
400 for (j = 0; j < need; ++j) {
401 buffer[j] = (char) ('A' + (j % 26));
402 }
403 buffer[need - 1] = '\0';
micky3879b9f5e72025-07-08 18:04:53 -0400404 st.status = clip_wprintw(win, st.single, fmt, '[', buffer, ']');
Steve Kondikae271bc2015-11-15 02:50:53 +0100405 free(buffer);
406 }
407 break;
408 case 'w':
409 do_subwindow(win, &st, test_clipping);
410 break;
411 case 'q':
412 return;
413 default:
414 update_status(win, &st);
415 break;
416 }
417 } while ((st.ch = wgetch(win)) != ERR);
418}
419
micky3879b9f5e72025-07-08 18:04:53 -0400420static void
421usage(int ok)
Steve Kondikae271bc2015-11-15 02:50:53 +0100422{
micky3879b9f5e72025-07-08 18:04:53 -0400423 static const char *msg[] =
424 {
425 "Usage: clip_printw [options]"
426 ,""
427 ,USAGE_COMMON
428 };
429 size_t n;
430
431 for (n = 0; n < SIZEOF(msg); n++)
432 fprintf(stderr, "%s\n", msg[n]);
433
434 ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
435}
436/* *INDENT-OFF* */
437VERSION_COMMON()
438/* *INDENT-ON* */
439
440int
441main(int argc, char *argv[])
442{
443 int ch;
444
445 while ((ch = getopt(argc, argv, OPTS_COMMON)) != -1) {
446 switch (ch) {
447 case OPTS_VERSION:
448 show_version(argv);
449 ExitProgram(EXIT_SUCCESS);
450 default:
451 usage(ch == OPTS_USAGE);
452 /* NOTREACHED */
453 }
454 }
455 if (optind < argc)
456 usage(FALSE);
457
458 setlocale(LC_ALL, "");
Steve Kondikae271bc2015-11-15 02:50:53 +0100459 initscr();
460 cbreak();
461 noecho();
462
463 test_clipping(stdscr);
464 endwin();
465
466 ExitProgram(EXIT_SUCCESS);
467}
468
469#else
470int
471main(void)
472{
473 printf("This program requires the curses vw_printw function\n");
474 ExitProgram(EXIT_FAILURE);
475}
476#endif