]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/clip_printw.c
ncurses 5.9 - patch 20110430
[ncurses.git] / test / clip_printw.c
1 /****************************************************************************
2  * Copyright (c) 2008-2009,2010 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.7 2010/11/13 20:48:48 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     attr_t 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((short) table[n].pair,
115                           (short) table[n].fg,
116                           (short) table[n].bg);
117             }
118         }
119         if (state < SIZEOF(table)) {
120             *pair = table[state].pair;
121             result = table[state].msg;
122         }
123     }
124     return result;
125 }
126
127 static const char *
128 video_params(unsigned state, attr_t *attr)
129 {
130     /* *INDENT-OFF* */
131     static struct {
132         attr_t attr;
133         const char *msg;
134     } table[] = {
135         { A_NORMAL,     "normal" },
136         { A_BOLD,       "bold" },
137         { A_REVERSE,    "reverse" },
138         { A_UNDERLINE,  "underline" },
139         { A_BLINK,      "blink" },
140     };
141     /* *INDENT-ON* */
142
143     const char *result = 0;
144
145     if (state < SIZEOF(table)) {
146         *attr = table[state].attr;
147         result = table[state].msg;
148     }
149     return result;
150 }
151
152 /* fill the window with a test-pattern */
153 static void
154 fill_window(WINDOW *win)
155 {
156     int y, x;
157     int y0 = -1, x0 = -1;
158
159     getyx(win, y, x);
160     wmove(win, 0, 0);
161     while (waddstr(win, "0123456789 abcdefghijklmnopqrstuvwxyz ") != ERR) {
162         int y1, x1;
163         getyx(win, y1, x1);
164         if (y1 == y0 && x1 == x0)
165             break;
166         x0 = x1;
167         y0 = y1;
168     }
169     wmove(win, y, x);
170 }
171
172 static void
173 show_status(WINDOW *win, STATUS * sp)
174 {
175     int y, x;
176
177     getyx(win, y, x);
178     wmove(win, 0, 0);
179     wprintw(win, "Count %d", sp->count);
180     if (sp->v_msg != 0)
181         wprintw(win, " Video %s", sp->v_msg);
182     if (sp->c_msg != 0)
183         wprintw(win, " Color %s", sp->c_msg);
184     wprintw(win, " (%d)", sp->status);
185     wclrtoeol(win);
186     wmove(win, y, x);
187 }
188
189 static void
190 do_subwindow(WINDOW *win, STATUS * sp, void func(WINDOW *))
191 {
192     WINDOW *win1 = newwin(sp->y_max - 2, sp->x_max - 2,
193                           sp->y_beg + 1, sp->x_beg + 1);
194
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);
197
198         if (win2 != 0) {
199             box(win1, 0, 0);
200             wrefresh(win1);
201             func(win2);
202
203             delwin(win2);
204         } else {
205             beep();
206         }
207         delwin(win1);
208         touchwin(win);
209     } else {
210         beep();
211     }
212 }
213
214 static void
215 init_status(WINDOW *win, STATUS * sp)
216 {
217     memset(sp, 0, sizeof(*sp));
218     sp->c = 99;
219     sp->v = 99;
220     sp->ch = ' ';
221
222     keypad(win, TRUE);
223     fill_window(win);
224
225     getbegyx(win, sp->y_beg, sp->x_beg);
226     getmaxyx(win, sp->y_max, sp->x_max);
227 }
228
229 static void
230 show_help(WINDOW *win)
231 {
232     static const char *table[] =
233     {
234         "Basic commands:"
235         ,"Use h/j/k/l or arrow keys to move the cursor."
236         ,"Set the count parameter for clip_wprintw by entering digits 0-9."
237         ,""
238         ,"Other commands:"
239         ,"space toggles through the set of video attributes and colors."
240         ,"t     touches (forces repaint) of the current line."
241         ,".     calls clip_wprintw at the current position with the given count."
242         ,"=     resets count to zero."
243         ,"?     shows this help-window"
244         ,""
245     };
246
247     int y_max, x_max;
248     int row;
249
250     getmaxyx(win, y_max, x_max);
251     for (row = 0; row < (int) SIZEOF(table) && row < y_max; ++row) {
252         MvWPrintw(win, row, 0, "%.*s", x_max, table[row]);
253     }
254     while (wgetch(win) != 'q')
255         beep();
256 }
257
258 static void
259 update_status(WINDOW *win, STATUS * sp)
260 {
261     switch (sp->ch) {
262     case ' ':                   /* next test-iteration */
263         if (has_colors()) {
264             if ((sp->c_msg = color_params(++(sp->c), &(sp->pair))) == 0) {
265                 sp->c_msg = color_params(sp->c = 0, &(sp->pair));
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         } else {
271             if ((sp->v_msg = video_params(++(sp->v), &(sp->attr))) == 0) {
272                 sp->v_msg = video_params(sp->v = 0, &(sp->attr));
273             }
274         }
275         sp->count = 0;
276         show_status(win, sp);
277         break;
278     case KEY_LEFT:
279     case 'h':
280         if (sp->x_val > 0)
281             wmove(win, sp->y_val, --(sp->x_val));
282         break;
283     case KEY_DOWN:
284     case 'j':
285         if (sp->y_val < sp->y_max)
286             wmove(win, ++(sp->y_val), sp->x_val);
287         break;
288     case KEY_UP:
289     case 'k':
290         if (sp->y_val > 0)
291             wmove(win, --(sp->y_val), sp->x_val);
292         break;
293     case KEY_RIGHT:
294     case 'l':
295         if (sp->x_val < sp->x_max)
296             wmove(win, sp->y_val, ++(sp->x_val));
297         break;
298     case 't':
299         touchline(win, sp->y_val, 1);
300         break;
301     case '=':
302         sp->count = 0;
303         show_status(win, sp);
304         break;
305     case '?':
306         do_subwindow(win, sp, show_help);
307         break;
308     default:
309         if (isdigit(sp->ch)) {
310             sp->count = (sp->count * 10) + (sp->ch - '0');
311             show_status(win, sp);
312         } else {
313             beep();
314         }
315         break;
316     }
317 }
318
319 static void
320 test_clipping(WINDOW *win)
321 {
322     STATUS st;
323     char fmt[80];
324     char *buffer;
325     unsigned j, need;
326
327     init_status(win, &st);
328
329     do {
330         switch (st.ch) {
331         case '.':               /* change from current position */
332             (void) wattrset(win, st.attr | (chtype) COLOR_PAIR(st.pair));
333             if (st.count > 0) {
334                 need = (unsigned) st.count + 1;
335                 sprintf(fmt, "%%c%%%ds%%c", st.count);
336             } else {
337                 need = (unsigned) getmaxx(win) - 1;
338                 strcpy(fmt, "%c%s%c");
339             }
340             if ((buffer = typeMalloc(char, need)) != 0) {
341                 for (j = 0; j < need; ++j) {
342                     buffer[j] = (char) ('A' + (j % 26));
343                 }
344                 buffer[need - 1] = '\0';
345                 st.status = clip_wprintw(win, fmt, '[', buffer, ']');
346             }
347             break;
348         case 'w':
349             do_subwindow(win, &st, test_clipping);
350             break;
351         case 'q':
352             return;
353         default:
354             update_status(win, &st);
355             break;
356         }
357     } while ((st.ch = wgetch(win)) != ERR);
358 }
359
360 int
361 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
362 {
363     initscr();
364     cbreak();
365     noecho();
366
367     test_clipping(stdscr);
368     endwin();
369
370     ExitProgram(EXIT_SUCCESS);
371 }
372
373 #else
374 int
375 main(void)
376 {
377     printf("This program requires the curses vw_printw function\n");
378     ExitProgram(EXIT_FAILURE);
379 }
380 #endif