]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/clip_printw.c
ncurses 6.0 - patch 20170916
[ncurses.git] / test / clip_printw.c
1 /****************************************************************************
2  * Copyright (c) 2008-2016,2017 Free Software Foundation, Inc.              *
3  *                                                                          *
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:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
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.                               *
22  *                                                                          *
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       *
26  * authorization.                                                           *
27  ****************************************************************************/
28 /*
29  * $Id: clip_printw.c,v 1.14 2017/04/15 17:28:10 tom Exp $
30  *
31  * demonstrate how to use printw without wrapping.
32  */
33
34 #include <test.priv.h>
35 #include <popup_msg.h>
36
37 #ifdef HAVE_VW_PRINTW
38
39 #define SHOW(n) ((n) == ERR ? "ERR" : "OK")
40 #define COLOR_DEFAULT (-1)
41
42 typedef struct {
43     unsigned c;
44     unsigned v;
45     int status;
46     int pair;
47     attr_t attr;
48     int count;
49     int ch;
50     const char *c_msg;
51     const char *v_msg;
52     int y_val;
53     int x_val;
54     int y_beg, x_beg;
55     int y_max, x_max;
56 } STATUS;
57
58 static int
59 clip_wprintw(WINDOW *win, NCURSES_CONST char *fmt,...)
60 {
61     int y0, x0, y1, x1, width;
62     WINDOW *sub;
63     va_list ap;
64     int rc;
65
66     /*
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.
70      */
71     getyx(win, y0, x0);
72     width = getmaxx(win) - x0;
73     sub = derwin(win, 1, width, y0, x0);
74     scrollok(sub, FALSE);
75
76     /*
77      * Print the text.
78      */
79     va_start(ap, fmt);
80     rc = vw_printw(sub, fmt, ap);
81     va_end(ap);
82
83     getyx(sub, y1, x1);
84     delwin(sub);
85
86     wmove(win, y1 + y0, x1 + x0);
87
88     return rc;
89 }
90
91 static const char *
92 color_params(unsigned state, int *pair)
93 {
94     /* *INDENT-OFF* */
95     static struct {
96         int pair;
97         int fg, bg;
98         const char *msg;
99     } table[] = {
100         { 0, COLOR_DEFAULT, COLOR_DEFAULT, "default" },
101         { 1, COLOR_RED,     COLOR_BLACK,   "red/black" },
102         { 2, COLOR_WHITE,   COLOR_BLUE,    "white/blue" },
103     };
104     /* *INDENT-ON* */
105
106     static bool first = TRUE;
107     const char *result = 0;
108
109     if (has_colors()) {
110         if (first) {
111             unsigned n;
112
113             start_color();
114             for (n = 0; n < SIZEOF(table); ++n) {
115                 init_pair((short) table[n].pair,
116                           (short) table[n].fg,
117                           (short) table[n].bg);
118             }
119         }
120         if (state < SIZEOF(table)) {
121             *pair = table[state].pair;
122             result = table[state].msg;
123         }
124     }
125     return result;
126 }
127
128 static const char *
129 video_params(unsigned state, attr_t *attr)
130 {
131     /* *INDENT-OFF* */
132     static struct {
133         attr_t attr;
134         const char *msg;
135     } table[] = {
136         { A_NORMAL,     "normal" },
137         { A_BOLD,       "bold" },
138         { A_REVERSE,    "reverse" },
139         { A_UNDERLINE,  "underline" },
140         { A_BLINK,      "blink" },
141     };
142     /* *INDENT-ON* */
143
144     const char *result = 0;
145
146     if (state < SIZEOF(table)) {
147         *attr = table[state].attr;
148         result = table[state].msg;
149     }
150     return result;
151 }
152
153 /* fill the window with a test-pattern */
154 static void
155 fill_window(WINDOW *win)
156 {
157     int y, x;
158     int y0 = -1, x0 = -1;
159
160     getyx(win, y, x);
161     wmove(win, 0, 0);
162     while (waddstr(win, "0123456789 abcdefghijklmnopqrstuvwxyz ") != ERR) {
163         int y1, x1;
164         getyx(win, y1, x1);
165         if (y1 == y0 && x1 == x0)
166             break;
167         x0 = x1;
168         y0 = y1;
169     }
170     wmove(win, y, x);
171 }
172
173 static void
174 show_status(WINDOW *win, STATUS * sp)
175 {
176     int y, x;
177
178     getyx(win, y, x);
179     wmove(win, 0, 0);
180     wprintw(win, "Count %d", sp->count);
181     if (sp->v_msg != 0)
182         wprintw(win, " Video %s", sp->v_msg);
183     if (sp->c_msg != 0)
184         wprintw(win, " Color %s", sp->c_msg);
185     wprintw(win, " (%d)", sp->status);
186     wclrtoeol(win);
187     wmove(win, y, x);
188 }
189
190 static void
191 do_subwindow(WINDOW *win, STATUS * sp, void func(WINDOW *))
192 {
193     WINDOW *win1 = newwin(sp->y_max - 2, sp->x_max - 2,
194                           sp->y_beg + 1, sp->x_beg + 1);
195
196     if (win1 != 0 && sp->y_max > 4 && sp->x_max > 4) {
197         WINDOW *win2 = derwin(win1, sp->y_max - 4, sp->x_max - 4, 1, 1);
198
199         if (win2 != 0) {
200             box(win1, 0, 0);
201             wrefresh(win1);
202             func(win2);
203
204             delwin(win2);
205         } else {
206             beep();
207         }
208         delwin(win1);
209         touchwin(win);
210     } else {
211         if (win1)
212             delwin(win1);
213         beep();
214     }
215 }
216
217 static void
218 init_status(WINDOW *win, STATUS * sp)
219 {
220     memset(sp, 0, sizeof(*sp));
221     sp->c = 99;
222     sp->v = 99;
223     sp->ch = ' ';
224
225     keypad(win, TRUE);
226     fill_window(win);
227
228     getbegyx(win, sp->y_beg, sp->x_beg);
229     getmaxyx(win, sp->y_max, sp->x_max);
230 }
231
232 static void
233 show_help(WINDOW *win)
234 {
235     static const char *msgs[] =
236     {
237         "Basic commands:"
238         ,"Use h/j/k/l or arrow keys to move the cursor."
239         ,"Set the count parameter for clip_wprintw by entering digits 0-9."
240         ,""
241         ,"Other commands:"
242         ,"space toggles through the set of video attributes and colors."
243         ,"t     touches (forces repaint) of the current line."
244         ,".     calls vw_printw at the current position with the given count."
245         ,"=     resets count to zero."
246         ,"?     shows this help-window"
247         ,0
248     };
249
250     popup_msg(win, msgs);
251 }
252
253 static void
254 update_status(WINDOW *win, STATUS * sp)
255 {
256     switch (sp->ch) {
257     case ' ':                   /* next test-iteration */
258         if (has_colors()) {
259             if ((sp->c_msg = color_params(++(sp->c), &(sp->pair))) == 0) {
260                 sp->c_msg = color_params(sp->c = 0, &(sp->pair));
261                 if ((sp->v_msg = video_params(++(sp->v), &(sp->attr))) == 0) {
262                     sp->v_msg = video_params(sp->v = 0, &(sp->attr));
263                 }
264             }
265         } else {
266             if ((sp->v_msg = video_params(++(sp->v), &(sp->attr))) == 0) {
267                 sp->v_msg = video_params(sp->v = 0, &(sp->attr));
268             }
269         }
270         sp->count = 0;
271         show_status(win, sp);
272         break;
273     case KEY_LEFT:
274     case 'h':
275         if (sp->x_val > 0)
276             wmove(win, sp->y_val, --(sp->x_val));
277         break;
278     case KEY_DOWN:
279     case 'j':
280         if (sp->y_val < sp->y_max)
281             wmove(win, ++(sp->y_val), sp->x_val);
282         break;
283     case KEY_UP:
284     case 'k':
285         if (sp->y_val > 0)
286             wmove(win, --(sp->y_val), sp->x_val);
287         break;
288     case KEY_RIGHT:
289     case 'l':
290         if (sp->x_val < sp->x_max)
291             wmove(win, sp->y_val, ++(sp->x_val));
292         break;
293     case 't':
294         touchline(win, sp->y_val, 1);
295         break;
296     case '=':
297         sp->count = 0;
298         show_status(win, sp);
299         break;
300     case HELP_KEY_1:
301         show_help(win);
302         break;
303     default:
304         if (isdigit(sp->ch)) {
305             sp->count = (sp->count * 10) + (sp->ch - '0');
306             show_status(win, sp);
307         } else {
308             beep();
309         }
310         break;
311     }
312 }
313
314 static void
315 test_clipping(WINDOW *win)
316 {
317     STATUS st;
318     char fmt[80];
319     char *buffer;
320     unsigned j, need;
321
322     init_status(win, &st);
323
324     do {
325         switch (st.ch) {
326         case '.':               /* change from current position */
327             (void) wattrset(win, AttrArg(COLOR_PAIR(st.pair), st.attr));
328             if (st.count > 0) {
329                 need = (unsigned) st.count + 1;
330                 _nc_SPRINTF(fmt, _nc_SLIMIT(sizeof(fmt)) "%%c%%%ds%%c", st.count);
331             } else {
332                 need = (unsigned) getmaxx(win) - 1;
333                 _nc_STRCPY(fmt, "%c%s%c", sizeof(fmt));
334             }
335             if ((buffer = typeMalloc(char, need + 1)) != 0) {
336                 for (j = 0; j < need; ++j) {
337                     buffer[j] = (char) ('A' + (j % 26));
338                 }
339                 buffer[need - 1] = '\0';
340                 st.status = clip_wprintw(win, fmt, '[', buffer, ']');
341                 free(buffer);
342             }
343             break;
344         case 'w':
345             do_subwindow(win, &st, test_clipping);
346             break;
347         case 'q':
348             return;
349         default:
350             update_status(win, &st);
351             break;
352         }
353     } while ((st.ch = wgetch(win)) != ERR);
354 }
355
356 int
357 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
358 {
359     initscr();
360     cbreak();
361     noecho();
362
363     test_clipping(stdscr);
364     endwin();
365
366     ExitProgram(EXIT_SUCCESS);
367 }
368
369 #else
370 int
371 main(void)
372 {
373     printf("This program requires the curses vw_printw function\n");
374     ExitProgram(EXIT_FAILURE);
375 }
376 #endif