1
/****************************************************************************
2
* Copyright (c) 2008 Free Software Foundation, Inc. *
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: *
12
* The above copyright notice and this permission notice shall be included *
13
* in all copies or substantial portions of the Software. *
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. *
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 *
27
****************************************************************************/
29
* $Id: clip_printw.c,v 1.1 2008/12/20 21:03:06 tom Exp $
31
* demonstrate how to use printw without wrapping.
34
#include <test.priv.h>
36
#define SHOW(n) ((n) == ERR ? "ERR" : "OK")
37
#define COLOR_DEFAULT (-1)
56
clip_wprintw(WINDOW *win, const char *fmt,...)
58
int y0, x0, y1, x1, width;
64
* Allocate a single-line derived window extending from the current
65
* cursor position to the end of the current line in the given window.
66
* Disable scrolling in the derived window.
69
width = getmaxx(win) - x0;
70
sub = derwin(win, 1, width, y0, x0);
77
rc = vw_printw(sub, fmt, ap);
83
wmove(win, y1 + y0, x1 + x0);
89
color_params(unsigned state, int *pair)
97
{ 0, COLOR_DEFAULT, COLOR_DEFAULT, "default" },
98
{ 1, COLOR_RED, COLOR_BLACK, "red/black" },
99
{ 2, COLOR_WHITE, COLOR_BLUE, "white/blue" },
103
static bool first = TRUE;
104
const char *result = 0;
111
for (n = 0; n < SIZEOF(table); ++n) {
112
init_pair(table[n].pair, table[n].fg, table[n].bg);
115
if (state < SIZEOF(table)) {
116
*pair = table[state].pair;
117
result = table[state].msg;
124
video_params(unsigned state, unsigned *attr)
131
{ A_NORMAL, "normal" },
133
{ A_REVERSE, "reverse" },
134
{ A_UNDERLINE, "underline" },
135
{ A_BLINK, "blink" },
139
const char *result = 0;
141
if (state < SIZEOF(table)) {
142
*attr = table[state].attr;
143
result = table[state].msg;
148
/* fill the window with a test-pattern */
150
fill_window(WINDOW *win)
156
while (waddstr(win, "0123456789 abcdefghijklmnopqrstuvwxyz ") != ERR) {
162
show_status(WINDOW *win, STATUS * sp)
168
wprintw(win, "Count %d", sp->count);
170
wprintw(win, " Video %s", sp->v_msg);
172
wprintw(win, " Color %s", sp->c_msg);
173
wprintw(win, " (%d)", sp->status);
179
do_subwindow(WINDOW *win, STATUS * sp, void func(WINDOW *))
181
WINDOW *win1 = newwin(sp->y_max - 2, sp->x_max - 2,
182
sp->y_beg + 1, sp->x_beg + 1);
184
if (win1 != 0 && sp->y_max > 4 && sp->x_max > 4) {
185
WINDOW *win2 = derwin(win1, sp->y_max - 4, sp->x_max - 4, 1, 1);
204
init_status(WINDOW *win, STATUS * sp)
206
memset(sp, 0, sizeof(*sp));
214
getbegyx(win, sp->y_beg, sp->x_beg);
215
getmaxyx(win, sp->y_max, sp->x_max);
219
show_help(WINDOW *win)
221
static const char *table[] =
224
,"Use h/j/k/l or arrow keys to move the cursor."
225
,"Set the count parameter for clip_wprintw by entering digits 0-9."
228
,"space toggles through the set of video attributes and colors."
229
,"t touches (forces repaint) of the current line."
230
,". calls clip_wprintw at the current position with the given count."
231
,"= resets count to zero."
232
,"? shows this help-window"
239
getmaxyx(win, y_max, x_max);
240
for (row = 0; row < (int) SIZEOF(table) && row < y_max; ++row) {
241
mvwprintw(win, row, 0, "%.*s", x_max, table[row]);
243
while (wgetch(win) != 'q')
248
update_status(WINDOW *win, STATUS * sp)
251
case ' ': /* next test-iteration */
253
if ((sp->c_msg = color_params(++(sp->c), &(sp->pair))) == 0) {
254
sp->c_msg = color_params(sp->c = 0, &(sp->pair));
255
if ((sp->v_msg = video_params(++(sp->v), &(sp->attr))) == 0) {
256
sp->v_msg = video_params(sp->v = 0, &(sp->attr));
260
if ((sp->v_msg = video_params(++(sp->v), &(sp->attr))) == 0) {
261
sp->v_msg = video_params(sp->v = 0, &(sp->attr));
265
show_status(win, sp);
270
wmove(win, sp->y_val, --(sp->x_val));
274
if (sp->y_val < sp->y_max)
275
wmove(win, ++(sp->y_val), sp->x_val);
280
wmove(win, --(sp->y_val), sp->x_val);
284
if (sp->x_val < sp->x_max)
285
wmove(win, sp->y_val, ++(sp->x_val));
288
touchline(win, sp->y_val, 1);
292
show_status(win, sp);
295
do_subwindow(win, sp, show_help);
298
if (isdigit(sp->ch)) {
299
sp->count = (sp->count * 10) + (sp->ch - '0');
300
show_status(win, sp);
309
test_clipping(WINDOW *win)
316
init_status(win, &st);
320
case '.': /* change from current position */
321
wattrset(win, st.attr | COLOR_PAIR(st.pair));
324
sprintf(fmt, "%%c%%%ds%%c", st.count);
326
need = getmaxx(win) - 1;
327
strcpy(fmt, "%c%s%c");
329
if ((buffer = typeMalloc(char, need)) != 0) {
330
for (j = 0; j < need; ++j) {
331
buffer[j] = 'A' + (j % 26);
333
buffer[need - 1] = '\0';
334
st.status = clip_wprintw(win, fmt, '[', buffer, ']');
338
do_subwindow(win, &st, test_clipping);
343
update_status(win, &st);
346
} while ((st.ch = wgetch(win)) != ERR);
350
main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
356
test_clipping(stdscr);
359
ExitProgram(EXIT_SUCCESS);