1 /****************************************************************************
2 * Copyright (c) 2008-2009,2010 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.7 2010/11/13 20:48:48 tom Exp $
31 * demonstrate how to use printw without wrapping.
34 #include <test.priv.h>
38 #define SHOW(n) ((n) == ERR ? "ERR" : "OK")
39 #define COLOR_DEFAULT (-1)
58 clip_wprintw(WINDOW
*win
, NCURSES_CONST
char *fmt
,...)
60 int y0
, x0
, y1
, x1
, width
;
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.
71 width
= getmaxx(win
) - x0
;
72 sub
= derwin(win
, 1, width
, y0
, x0
);
79 rc
= vw_printw(sub
, fmt
, ap
);
85 wmove(win
, y1
+ y0
, x1
+ x0
);
91 color_params(unsigned state
, int *pair
)
99 { 0, COLOR_DEFAULT
, COLOR_DEFAULT
, "default" },
100 { 1, COLOR_RED
, COLOR_BLACK
, "red/black" },
101 { 2, COLOR_WHITE
, COLOR_BLUE
, "white/blue" },
105 static bool first
= TRUE
;
106 const char *result
= 0;
113 for (n
= 0; n
< SIZEOF(table
); ++n
) {
114 init_pair((short) table
[n
].pair
,
116 (short) table
[n
].bg
);
119 if (state
< SIZEOF(table
)) {
120 *pair
= table
[state
].pair
;
121 result
= table
[state
].msg
;
128 video_params(unsigned state
, attr_t
*attr
)
135 { A_NORMAL
, "normal" },
137 { A_REVERSE
, "reverse" },
138 { A_UNDERLINE
, "underline" },
139 { A_BLINK
, "blink" },
143 const char *result
= 0;
145 if (state
< SIZEOF(table
)) {
146 *attr
= table
[state
].attr
;
147 result
= table
[state
].msg
;
152 /* fill the window with a test-pattern */
154 fill_window(WINDOW
*win
)
157 int y0
= -1, x0
= -1;
161 while (waddstr(win
, "0123456789 abcdefghijklmnopqrstuvwxyz ") != ERR
) {
164 if (y1
== y0
&& x1
== x0
)
173 show_status(WINDOW
*win
, STATUS
* sp
)
179 wprintw(win
, "Count %d", sp
->count
);
181 wprintw(win
, " Video %s", sp
->v_msg
);
183 wprintw(win
, " Color %s", sp
->c_msg
);
184 wprintw(win
, " (%d)", sp
->status
);
190 do_subwindow(WINDOW
*win
, STATUS
* sp
, void func(WINDOW
*))
192 WINDOW
*win1
= newwin(sp
->y_max
- 2, sp
->x_max
- 2,
193 sp
->y_beg
+ 1, sp
->x_beg
+ 1);
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);
215 init_status(WINDOW
*win
, STATUS
* sp
)
217 memset(sp
, 0, sizeof(*sp
));
225 getbegyx(win
, sp
->y_beg
, sp
->x_beg
);
226 getmaxyx(win
, sp
->y_max
, sp
->x_max
);
230 show_help(WINDOW
*win
)
232 static const char *table
[] =
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."
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"
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
]);
254 while (wgetch(win
) != 'q')
259 update_status(WINDOW
*win
, STATUS
* sp
)
262 case ' ': /* next test-iteration */
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
));
271 if ((sp
->v_msg
= video_params(++(sp
->v
), &(sp
->attr
))) == 0) {
272 sp
->v_msg
= video_params(sp
->v
= 0, &(sp
->attr
));
276 show_status(win
, sp
);
281 wmove(win
, sp
->y_val
, --(sp
->x_val
));
285 if (sp
->y_val
< sp
->y_max
)
286 wmove(win
, ++(sp
->y_val
), sp
->x_val
);
291 wmove(win
, --(sp
->y_val
), sp
->x_val
);
295 if (sp
->x_val
< sp
->x_max
)
296 wmove(win
, sp
->y_val
, ++(sp
->x_val
));
299 touchline(win
, sp
->y_val
, 1);
303 show_status(win
, sp
);
306 do_subwindow(win
, sp
, show_help
);
309 if (isdigit(sp
->ch
)) {
310 sp
->count
= (sp
->count
* 10) + (sp
->ch
- '0');
311 show_status(win
, sp
);
320 test_clipping(WINDOW
*win
)
327 init_status(win
, &st
);
331 case '.': /* change from current position */
332 (void) wattrset(win
, st
.attr
| (chtype
) COLOR_PAIR(st
.pair
));
334 need
= (unsigned) st
.count
+ 1;
335 sprintf(fmt
, "%%c%%%ds%%c", st
.count
);
337 need
= (unsigned) getmaxx(win
) - 1;
338 strcpy(fmt
, "%c%s%c");
340 if ((buffer
= typeMalloc(char, need
)) != 0) {
341 for (j
= 0; j
< need
; ++j
) {
342 buffer
[j
] = (char) ('A' + (j
% 26));
344 buffer
[need
- 1] = '\0';
345 st
.status
= clip_wprintw(win
, fmt
, '[', buffer
, ']');
349 do_subwindow(win
, &st
, test_clipping
);
354 update_status(win
, &st
);
357 } while ((st
.ch
= wgetch(win
)) != ERR
);
361 main(int argc GCC_UNUSED
, char *argv
[]GCC_UNUSED
)
367 test_clipping(stdscr
);
370 ExitProgram(EXIT_SUCCESS
);
377 printf("This program requires the curses vw_printw function\n");
378 ExitProgram(EXIT_FAILURE
);