Merge branch 'release-4.0'
[kiteware-cmake.git] / Utilities / cmpdcurses / pdcurses / overlay.c
blob5bcc62745f82d2d50b2567acf77aedb354608fe8
1 /* PDCurses */
3 #include <curspriv.h>
5 /*man-start**************************************************************
7 overlay
8 -------
10 ### Synopsis
12 int overlay(const WINDOW *src_w, WINDOW *dst_w)
13 int overwrite(const WINDOW *src_w, WINDOW *dst_w)
14 int copywin(const WINDOW *src_w, WINDOW *dst_w, int src_tr,
15 int src_tc, int dst_tr, int dst_tc, int dst_br,
16 int dst_bc, int _overlay)
18 ### Description
20 overlay() and overwrite() copy all the text from src_w into dst_w.
21 The windows need not be the same size. Those characters in the source
22 window that intersect with the destination window are copied, so that
23 the characters appear in the same physical position on the screen.
24 The difference between the two functions is that overlay() is non-
25 destructive (blanks are not copied) while overwrite() is destructive
26 (blanks are copied).
28 copywin() is similar, but doesn't require that the two windows
29 overlap. The arguments src_tc and src_tr specify the top left corner
30 of the region to be copied. dst_tc, dst_tr, dst_br, and dst_bc
31 specify the region within the destination window to copy to. The
32 argument "overlay", if TRUE, indicates that the copy is done non-
33 destructively (as in overlay()); blanks in the source window are not
34 copied to the destination window. When overlay is FALSE, blanks are
35 copied.
37 ### Return Value
39 All functions return OK on success and ERR on error.
41 ### Portability
42 X/Open ncurses NetBSD
43 overlay Y Y Y
44 overwrite Y Y Y
45 copywin Y Y Y
47 **man-end****************************************************************/
49 /* Thanks to Andreas Otte <venn@@uni-paderborn.de> for the
50 corrected overlay()/overwrite() behavior. */
52 static int _copy_win(const WINDOW *src_w, WINDOW *dst_w, int src_tr,
53 int src_tc, int src_br, int src_bc, int dst_tr,
54 int dst_tc, bool _overlay)
56 int col, line, y1, fc, *minchng, *maxchng;
57 chtype *w1ptr, *w2ptr;
59 int lc = 0;
60 int xdiff = src_bc - src_tc;
61 int ydiff = src_br - src_tr;
63 if (!src_w || !dst_w)
64 return ERR;
66 minchng = dst_w->_firstch;
67 maxchng = dst_w->_lastch;
69 for (y1 = 0; y1 < dst_tr; y1++)
71 minchng++;
72 maxchng++;
75 for (line = 0; line < ydiff; line++)
77 w1ptr = src_w->_y[line + src_tr] + src_tc;
78 w2ptr = dst_w->_y[line + dst_tr] + dst_tc;
80 fc = _NO_CHANGE;
82 for (col = 0; col < xdiff; col++)
84 if ((*w1ptr) != (*w2ptr) &&
85 !((*w1ptr & A_CHARTEXT) == ' ' && _overlay))
87 *w2ptr = *w1ptr;
89 if (fc == _NO_CHANGE)
90 fc = col + dst_tc;
92 lc = col + dst_tc;
95 w1ptr++;
96 w2ptr++;
99 if (*minchng == _NO_CHANGE)
101 *minchng = fc;
102 *maxchng = lc;
104 else if (fc != _NO_CHANGE)
106 if (fc < *minchng)
107 *minchng = fc;
108 if (lc > *maxchng)
109 *maxchng = lc;
112 minchng++;
113 maxchng++;
116 return OK;
119 int _copy_overlap(const WINDOW *src_w, WINDOW *dst_w, bool overlay)
121 int first_line, first_col, last_line, last_col;
122 int src_start_x, src_start_y, dst_start_x, dst_start_y;
123 int xdiff, ydiff;
125 if (!src_w || !dst_w)
126 return ERR;
128 first_col = max(dst_w->_begx, src_w->_begx);
129 first_line = max(dst_w->_begy, src_w->_begy);
131 last_col = min(src_w->_begx + src_w->_maxx, dst_w->_begx + dst_w->_maxx);
132 last_line = min(src_w->_begy + src_w->_maxy, dst_w->_begy + dst_w->_maxy);
134 /* determine the overlapping region of the two windows in real
135 coordinates */
137 /* if no overlapping region, do nothing */
139 if ((last_col < first_col) || (last_line < first_line))
140 return OK;
142 /* size of overlapping region */
144 xdiff = last_col - first_col;
145 ydiff = last_line - first_line;
147 if (src_w->_begx <= dst_w->_begx)
149 src_start_x = dst_w->_begx - src_w->_begx;
150 dst_start_x = 0;
152 else
154 dst_start_x = src_w->_begx - dst_w->_begx;
155 src_start_x = 0;
158 if (src_w->_begy <= dst_w->_begy)
160 src_start_y = dst_w->_begy - src_w->_begy;
161 dst_start_y = 0;
163 else
165 dst_start_y = src_w->_begy - dst_w->_begy;
166 src_start_y = 0;
169 return _copy_win(src_w, dst_w, src_start_y, src_start_x,
170 src_start_y + ydiff, src_start_x + xdiff,
171 dst_start_y, dst_start_x, overlay);
174 int overlay(const WINDOW *src_w, WINDOW *dst_w)
176 PDC_LOG(("overlay() - called\n"));
178 return _copy_overlap(src_w, dst_w, TRUE);
181 int overwrite(const WINDOW *src_w, WINDOW *dst_w)
183 PDC_LOG(("overwrite() - called\n"));
185 return _copy_overlap(src_w, dst_w, FALSE);
188 int copywin(const WINDOW *src_w, WINDOW *dst_w, int src_tr, int src_tc,
189 int dst_tr, int dst_tc, int dst_br, int dst_bc, int _overlay)
191 int src_end_x, src_end_y;
192 int src_rows, src_cols, dst_rows, dst_cols;
193 int min_rows, min_cols;
195 PDC_LOG(("copywin() - called\n"));
197 if (!src_w || !dst_w || dst_w == curscr || dst_br >= dst_w->_maxy
198 || dst_bc >= dst_w->_maxx || dst_tr < 0 || dst_tc < 0)
199 return ERR;
201 src_rows = src_w->_maxy - src_tr;
202 src_cols = src_w->_maxx - src_tc;
203 dst_rows = dst_br - dst_tr + 1;
204 dst_cols = dst_bc - dst_tc + 1;
206 min_rows = min(src_rows, dst_rows);
207 min_cols = min(src_cols, dst_cols);
209 src_end_y = src_tr + min_rows;
210 src_end_x = src_tc + min_cols;
212 return _copy_win(src_w, dst_w, src_tr, src_tc, src_end_y, src_end_x,
213 dst_tr, dst_tc, _overlay);