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