]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/clip_printw.c
ncurses 6.4 - patch 20240414
[ncurses.git] / test / clip_printw.c
1 /****************************************************************************
2  * Copyright 2019-2022,2023 Thomas E. Dickey                                *
3  * Copyright 2008-2016,2017 Free Software Foundation, Inc.                  *
4  *                                                                          *
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 /*
30  * $Id: clip_printw.c,v 1.23 2023/09/30 19:57:44 tom Exp $
31  *
32  * demonstrate how to use printw with/without wrapping.
33  */
34
35 #include <test.priv.h>
36 #include <popup_msg.h>
37
38 #ifdef HAVE_VW_PRINTW
39
40 #define SHOW(n) ((n) == ERR ? "ERR" : "OK")
41 #define COLOR_DEFAULT (-1)
42
43 #define X_BASE 0
44 #define Y_BASE 1
45 #define MARGIN 1
46 #define Y_TOP  (Y_BASE + MARGIN)
47
48 typedef struct {
49     unsigned c;
50     unsigned v;
51     bool single;
52     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
65 static int
66 clip_wprintw(WINDOW *win, bool single, NCURSES_CONST char *fmt, ...)
67 {
68     int y0, x0, y1, x1, width;
69     WINDOW *sub;
70     va_list ap;
71     int rc;
72
73     getyx(win, y0, x0);
74     width = getmaxx(win) - x0;
75     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     }
89     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
101     if (single) {
102         wmove(win, y1 + y0, x1 + x0);
103     } else {
104         wmove(win, y1, x1);
105     }
106
107     return rc;
108 }
109
110 static const char *
111 color_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
125     const char *result = 0;
126
127     if (has_colors()) {
128         static bool first = TRUE;
129
130         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
148 static const char *
149 video_params(unsigned state, attr_t *attr)
150 {
151     /* *INDENT-OFF* */
152     static struct {
153         attr_t attr;
154         const char *msg;
155     } table[] = {
156         { WA_NORMAL,    "normal" },
157         { WA_BOLD,      "bold" },
158         { WA_REVERSE,   "reverse" },
159         { WA_UNDERLINE, "underline" },
160         { WA_BLINK,     "blink" },
161     };
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 */
174 static void
175 fill_window(WINDOW *win)
176 {
177     int y_top = (win == stdscr) ? Y_BASE : MARGIN;
178     int y, x;
179     int y0 = -1, x0 = -1;
180
181     getyx(win, y, x);
182     wmove(win, y_top, 0);
183     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
194 static void
195 show_status(WINDOW *win, STATUS * sp)
196 {
197     int y, x;
198
199     getyx(win, y, x);
200     wattron(win, A_REVERSE);
201     wmove(win, 0, 0);
202     wprintw(win, "Clip %s", sp->single ? "line" : "window");
203     wprintw(win, " Count %d", sp->count);
204     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);
210     wattroff(win, A_REVERSE);
211     wmove(win, y, x);
212 }
213
214 static void
215 do_subwindow(WINDOW *win, STATUS * sp, void func(WINDOW *))
216 {
217     WINDOW *win1 = newwin(sp->y_max - (2 * MARGIN),
218                           sp->x_max - (2 * MARGIN),
219                           sp->y_beg + MARGIN,
220                           sp->x_beg + MARGIN);
221
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,
227                               MARGIN);
228
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
247 /*
248  * The status line is within the same window as the test-data.
249  */
250 static void
251 init_status(WINDOW *win, STATUS * sp)
252 {
253     memset(sp, 0, sizeof(*sp));
254     sp->single = TRUE;
255     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);
264
265     wmove(win, sp->y_val = Y_BASE, sp->x_val = 0);
266 }
267
268 static void
269 show_help(WINDOW *win)
270 {
271     static const char *msgs[] =
272     {
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."
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"
286         ,0
287     };
288
289     popup_msg(win, msgs);
290 }
291
292 static void
293 update_status(WINDOW *win, STATUS * sp)
294 {
295     int margin = 0;
296
297     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;
314     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;
330     case KEY_LEFT:
331     case 'h':
332         if (sp->x_val > margin)
333             wmove(win, sp->y_val, --(sp->x_val));
334         break;
335     case KEY_DOWN:
336     case 'j':
337         if (sp->y_val < (sp->y_max - margin))
338             wmove(win, ++(sp->y_val), sp->x_val);
339         break;
340     case KEY_UP:
341     case 'k':
342         if (sp->y_val > Y_BASE)
343             wmove(win, --(sp->y_val), sp->x_val);
344         break;
345     case KEY_RIGHT:
346     case 'l':
347         if (sp->x_val < (sp->x_max - margin))
348             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;
357     case 's':
358         sp->single = !sp->single;
359         show_status(win, sp);
360         break;
361     case HELP_KEY_1:
362         show_help(win);
363         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
375 static void
376 test_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;
391                 _nc_SPRINTF(fmt, _nc_SLIMIT(sizeof(fmt)) "%%c%%%ds%%c", st.count);
392             } else {
393                 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));
398             }
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';
404                 st.status = clip_wprintw(win, st.single, fmt, '[', buffer, ']');
405                 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
420 static void
421 usage(int ok)
422 {
423     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* */
437 VERSION_COMMON()
438 /* *INDENT-ON* */
439
440 int
441 main(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, "");
459     initscr();
460     cbreak();
461     noecho();
462
463     test_clipping(stdscr);
464     endwin();
465
466     ExitProgram(EXIT_SUCCESS);
467 }
468
469 #else
470 int
471 main(void)
472 {
473     printf("This program requires the curses vw_printw function\n");
474     ExitProgram(EXIT_FAILURE);
475 }
476 #endif