xref: /titanic_53/usr/src/lib/libxcurses/src/libc/xcurses/wrefresh.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1995, by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  * All rights reserved.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  * wrefresh.c
31*7c478bd9Sstevel@tonic-gate  *
32*7c478bd9Sstevel@tonic-gate  * XCurses Library
33*7c478bd9Sstevel@tonic-gate  *
34*7c478bd9Sstevel@tonic-gate  * Copyright 1990, 1995 by Mortice Kern Systems Inc.  All rights reserved.
35*7c478bd9Sstevel@tonic-gate  *
36*7c478bd9Sstevel@tonic-gate  */
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate #ifdef M_RCSID
39*7c478bd9Sstevel@tonic-gate #ifndef lint
40*7c478bd9Sstevel@tonic-gate static char rcsID[] = "$Header: /rd/src/libc/xcurses/rcs/wrefresh.c 1.3 1995/06/20 14:34:14 ant Exp $";
41*7c478bd9Sstevel@tonic-gate #endif
42*7c478bd9Sstevel@tonic-gate #endif
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate #include <private.h>
45*7c478bd9Sstevel@tonic-gate #include <string.h>
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate /*f
48*7c478bd9Sstevel@tonic-gate  * Update curscr with the given window then display to the terminal.
49*7c478bd9Sstevel@tonic-gate  * Unless leaveok() has been enabled, the physical cursor of the
50*7c478bd9Sstevel@tonic-gate  * terminal is left at the location of the cursor for that window.
51*7c478bd9Sstevel@tonic-gate  */
52*7c478bd9Sstevel@tonic-gate int
wrefresh(w)53*7c478bd9Sstevel@tonic-gate wrefresh(w)
54*7c478bd9Sstevel@tonic-gate WINDOW *w;
55*7c478bd9Sstevel@tonic-gate {
56*7c478bd9Sstevel@tonic-gate 	int value;
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate #ifdef M_CURSES_TRACE
59*7c478bd9Sstevel@tonic-gate 	__m_trace("wrefresh(%p)", w);
60*7c478bd9Sstevel@tonic-gate #endif
61*7c478bd9Sstevel@tonic-gate 	if (w == curscr)
62*7c478bd9Sstevel@tonic-gate 		value = clearok(__m_screen->_newscr, TRUE);
63*7c478bd9Sstevel@tonic-gate 	else
64*7c478bd9Sstevel@tonic-gate 		value = wnoutrefresh(w);
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate 	if (value == OK)
67*7c478bd9Sstevel@tonic-gate 		value = doupdate();
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate 	return __m_return_code("wrefresh", value);
70*7c478bd9Sstevel@tonic-gate }
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate /*f
73*7c478bd9Sstevel@tonic-gate  * Update newscr with the given window.  This allows newscr to be
74*7c478bd9Sstevel@tonic-gate  * updated with several windows before doing a doupdate() (and so
75*7c478bd9Sstevel@tonic-gate  * improve the efficiency of multiple updates in comparison to
76*7c478bd9Sstevel@tonic-gate  * looping through wrefresh() for all windows).
77*7c478bd9Sstevel@tonic-gate  */
78*7c478bd9Sstevel@tonic-gate int
wnoutrefresh(w)79*7c478bd9Sstevel@tonic-gate wnoutrefresh(w)
80*7c478bd9Sstevel@tonic-gate WINDOW *w;
81*7c478bd9Sstevel@tonic-gate {
82*7c478bd9Sstevel@tonic-gate 	int wy, wx, ny, nx, dx, value;
83*7c478bd9Sstevel@tonic-gate 	WINDOW *ns = __m_screen->_newscr;
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate #ifdef M_CURSES_TRACE
86*7c478bd9Sstevel@tonic-gate 	__m_trace("wnoutrefresh(%p)", w);
87*7c478bd9Sstevel@tonic-gate #endif
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate 	value = (w->_flags & W_IS_PAD) ? ERR : OK;
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate 	if (value == OK) {
92*7c478bd9Sstevel@tonic-gate 		/* This loop is similar to what copywin() does, except that
93*7c478bd9Sstevel@tonic-gate 		 * this loop only copies dirty lines, while copywin() copies
94*7c478bd9Sstevel@tonic-gate 		 * every line.
95*7c478bd9Sstevel@tonic-gate 		 */
96*7c478bd9Sstevel@tonic-gate 		for (wy = 0, ny = w->_begy; wy < w->_maxy; ++wy, ++ny) {
97*7c478bd9Sstevel@tonic-gate 			/* Has line been touched? */
98*7c478bd9Sstevel@tonic-gate 			if (w->_last[wy] <= w->_first[wy])
99*7c478bd9Sstevel@tonic-gate 				continue;
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate 			wx = w->_first[wy];
102*7c478bd9Sstevel@tonic-gate 			nx = w->_begx + wx;
103*7c478bd9Sstevel@tonic-gate 			dx = w->_last[wy] - wx;
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate 			/* Case 3 - Check target window for overlap of broad
106*7c478bd9Sstevel@tonic-gate 			 * characters around the outer edge of the source
107*7c478bd9Sstevel@tonic-gate 			 * window's location.
108*7c478bd9Sstevel@tonic-gate 			 */
109*7c478bd9Sstevel@tonic-gate 			(void) __m_cc_erase(ns, ny, nx, ny, nx);
110*7c478bd9Sstevel@tonic-gate 			(void) __m_cc_erase(ns, ny, nx+dx-1, ny, nx+dx-1);
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate                         (void) memcpy(
113*7c478bd9Sstevel@tonic-gate                                 &ns->_line[ny][nx], &w->_line[wy][wx],
114*7c478bd9Sstevel@tonic-gate                                 dx * sizeof **w->_line
115*7c478bd9Sstevel@tonic-gate                         );
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate 			if (!ns->_line[ny][nx]._f) {
118*7c478bd9Sstevel@tonic-gate 				/* Case 5 - Incomplete glyph copied from
119*7c478bd9Sstevel@tonic-gate 				 * source at screen margins.
120*7c478bd9Sstevel@tonic-gate 				 */
121*7c478bd9Sstevel@tonic-gate 				if (nx <= 0)
122*7c478bd9Sstevel@tonic-gate 					(void) __m_cc_erase(ns, ny, 0, ny, 0);
123*7c478bd9Sstevel@tonic-gate #ifdef M_CURSES_SENSIBLE_WINDOWS
124*7c478bd9Sstevel@tonic-gate 				/* Case 4 - Expand incomplete glyph from
125*7c478bd9Sstevel@tonic-gate 				 * source into target window.
126*7c478bd9Sstevel@tonic-gate 				 */
127*7c478bd9Sstevel@tonic-gate 				else if (0 < nx)
128*7c478bd9Sstevel@tonic-gate 					(void) __m_cc_expand(ns, ny, nx, -1);
129*7c478bd9Sstevel@tonic-gate #endif /* M_CURSES_SENSIBLE_WINDOWS */
130*7c478bd9Sstevel@tonic-gate 			}
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate 			if (!__m_cc_islast(ns, ny, nx+dx-1)) {
133*7c478bd9Sstevel@tonic-gate 				/* Case 5 - Incomplete glyph copied from
134*7c478bd9Sstevel@tonic-gate 				 * source at screen margins.
135*7c478bd9Sstevel@tonic-gate 				 */
136*7c478bd9Sstevel@tonic-gate 				if (ns->_maxx <= nx + dx)
137*7c478bd9Sstevel@tonic-gate 					(void) __m_cc_erase(
138*7c478bd9Sstevel@tonic-gate 						ns, ny, nx+dx-1, ny, nx+dx-1
139*7c478bd9Sstevel@tonic-gate 					);
140*7c478bd9Sstevel@tonic-gate #ifdef M_CURSES_SENSIBLE_WINDOWS
141*7c478bd9Sstevel@tonic-gate 				/* Case 4 - Expand incomplete glyph from
142*7c478bd9Sstevel@tonic-gate 				 * source into target window.
143*7c478bd9Sstevel@tonic-gate 				 */
144*7c478bd9Sstevel@tonic-gate 				else if (nx + dx < ns->_maxx)
145*7c478bd9Sstevel@tonic-gate 					(void) __m_cc_expand(
146*7c478bd9Sstevel@tonic-gate 						ns, ny, nx+dx-1, 1
147*7c478bd9Sstevel@tonic-gate 					);
148*7c478bd9Sstevel@tonic-gate #endif /* M_CURSES_SENSIBLE_WINDOWS */
149*7c478bd9Sstevel@tonic-gate 			}
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate 			/* Untouch line. */
152*7c478bd9Sstevel@tonic-gate 			w->_first[wy] = w->_maxx;
153*7c478bd9Sstevel@tonic-gate 			w->_last[wy] = -1;
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate 			/* Remember refresh region (inclusive). */
156*7c478bd9Sstevel@tonic-gate 			w->_refy = w->_begy;
157*7c478bd9Sstevel@tonic-gate 			w->_refx = w->_begx;
158*7c478bd9Sstevel@tonic-gate 			w->_sminy = w->_sminx = 0;
159*7c478bd9Sstevel@tonic-gate 			w->_smaxy = ns->_maxy-1;
160*7c478bd9Sstevel@tonic-gate 			w->_smaxx = ns->_maxx-1;
161*7c478bd9Sstevel@tonic-gate 		}
162*7c478bd9Sstevel@tonic-gate 
163*7c478bd9Sstevel@tonic-gate 		ns->_scroll = w->_scroll;
164*7c478bd9Sstevel@tonic-gate 		w->_scroll = 0;
165*7c478bd9Sstevel@tonic-gate 
166*7c478bd9Sstevel@tonic-gate 		/* Last refreshed window controls W_LEAVE_CURSOR flag. */
167*7c478bd9Sstevel@tonic-gate 		ns->_flags &= ~W_LEAVE_CURSOR;
168*7c478bd9Sstevel@tonic-gate 		ns->_cury = w->_cury + w->_begy;
169*7c478bd9Sstevel@tonic-gate 		ns->_curx = w->_curx + w->_begx;
170*7c478bd9Sstevel@tonic-gate 
171*7c478bd9Sstevel@tonic-gate 		ns->_flags |= w->_flags
172*7c478bd9Sstevel@tonic-gate 			& (W_CLEAR_WINDOW | W_REDRAW_WINDOW | W_LEAVE_CURSOR);
173*7c478bd9Sstevel@tonic-gate 		w->_flags &= ~(W_CLEAR_WINDOW | W_REDRAW_WINDOW);
174*7c478bd9Sstevel@tonic-gate 	}
175*7c478bd9Sstevel@tonic-gate 
176*7c478bd9Sstevel@tonic-gate 	return __m_return_code("wnoutrefresh", value);
177*7c478bd9Sstevel@tonic-gate }
178*7c478bd9Sstevel@tonic-gate 
179*7c478bd9Sstevel@tonic-gate /*
180*7c478bd9Sstevel@tonic-gate  * Check overlaping region on a line.
181*7c478bd9Sstevel@tonic-gate  *
182*7c478bd9Sstevel@tonic-gate  * When copying a source window region over another target window
183*7c478bd9Sstevel@tonic-gate  * region, we have a few cases which to concern ourselves with.
184*7c478bd9Sstevel@tonic-gate  *
185*7c478bd9Sstevel@tonic-gate  * Let {, [, ( and ), ], } denote the left and right halves of
186*7c478bd9Sstevel@tonic-gate  * broad glyphes.
187*7c478bd9Sstevel@tonic-gate  *
188*7c478bd9Sstevel@tonic-gate  * Let alpha-numerics and periods (.) be narrow glyphes.
189*7c478bd9Sstevel@tonic-gate  *
190*7c478bd9Sstevel@tonic-gate  * Let hash (#) be a narrow background character.
191*7c478bd9Sstevel@tonic-gate  *
192*7c478bd9Sstevel@tonic-gate  * Let vertical bar, hyphen, and plus represent the borders
193*7c478bd9Sstevel@tonic-gate  * of a window.
194*7c478bd9Sstevel@tonic-gate  *
195*7c478bd9Sstevel@tonic-gate  *  1.	Copy narrow characters over narrow characters.
196*7c478bd9Sstevel@tonic-gate  *		copywin(s, t, 0, 1, 0, 1, 1, 3, 0)
197*7c478bd9Sstevel@tonic-gate  *		   s      	   t      ==>      t
198*7c478bd9Sstevel@tonic-gate  *		+------+	+------+	+------+
199*7c478bd9Sstevel@tonic-gate  *		|abcdef|	|......|	|.bcd..|
200*7c478bd9Sstevel@tonic-gate  *		|ghijkl|	|......|	|.hij..|
201*7c478bd9Sstevel@tonic-gate  *		|mnopqr|	|......|	|......|
202*7c478bd9Sstevel@tonic-gate  *		+------+	+------+	+------+
203*7c478bd9Sstevel@tonic-gate  *	Nothing special.
204*7c478bd9Sstevel@tonic-gate  *
205*7c478bd9Sstevel@tonic-gate  *  2.	Copy whole broad characters over narrow characters.
206*7c478bd9Sstevel@tonic-gate  *		copywin(s, t, 0, 1, 0, 1, 1, 3, 0)
207*7c478bd9Sstevel@tonic-gate  *		   s               t       ==>     t
208*7c478bd9Sstevel@tonic-gate  *		+------+	+------+	+------+
209*7c478bd9Sstevel@tonic-gate  *		|a[]def|	|......|	|.[]d..|
210*7c478bd9Sstevel@tonic-gate  *		|gh{}kl|	|......|	|.h{}..|
211*7c478bd9Sstevel@tonic-gate  *		|mnopqr|	|......|	|......|
212*7c478bd9Sstevel@tonic-gate  *		+------+	+------+	+------+
213*7c478bd9Sstevel@tonic-gate  *	Nothing special.
214*7c478bd9Sstevel@tonic-gate  *
215*7c478bd9Sstevel@tonic-gate  *  3.	Copy narrow from source overlaps broad in target.
216*7c478bd9Sstevel@tonic-gate  *		copywin(s, t, 0, 1, 0, 1, 1, 3, 0)
217*7c478bd9Sstevel@tonic-gate  *		   s               t       ==>     t
218*7c478bd9Sstevel@tonic-gate  *		+------+	+------+	+------+
219*7c478bd9Sstevel@tonic-gate  *		|abcdef|	|[]....|	|#bcd..|
220*7c478bd9Sstevel@tonic-gate  *		|ghijkl|	|...{}.|	|.hij#.|
221*7c478bd9Sstevel@tonic-gate  *		|mnopqr|	|......|	|......|
222*7c478bd9Sstevel@tonic-gate  *		+------+	+------+	+------+
223*7c478bd9Sstevel@tonic-gate  *	The # background characters have wiped out the remaining
224*7c478bd9Sstevel@tonic-gate  *	halves of broad characters.  This may result also with
225*7c478bd9Sstevel@tonic-gate  *	a wnoutrefresh() of a window onto curscr.
226*7c478bd9Sstevel@tonic-gate  *
227*7c478bd9Sstevel@tonic-gate  * The following case appears to be disallowed in XPG4 V2
228*7c478bd9Sstevel@tonic-gate  * and I think they're wrong, so I've conditionalised the code
229*7c478bd9Sstevel@tonic-gate  * on M_CURSES_SENSIBLE_WINDOWS.
230*7c478bd9Sstevel@tonic-gate  *
231*7c478bd9Sstevel@tonic-gate  *  4.	Copy incomplete broad from source to target.
232*7c478bd9Sstevel@tonic-gate  *		copywin(s, t, 0, 1, 0, 1, 1, 3, 0)
233*7c478bd9Sstevel@tonic-gate  *		   s               t       ==>     t
234*7c478bd9Sstevel@tonic-gate  *		+------+	+------+	+------+
235*7c478bd9Sstevel@tonic-gate  *		|[]cdef|	|123456|	|[]cd56|
236*7c478bd9Sstevel@tonic-gate  *		|ghi{}l|	|789012|	|7hi{}2|
237*7c478bd9Sstevel@tonic-gate  *		|mnopqr|	|......|	|......|
238*7c478bd9Sstevel@tonic-gate  *		+------+	+------+	+------+
239*7c478bd9Sstevel@tonic-gate  *	The ] and { halves of broad characters have been copied and
240*7c478bd9Sstevel@tonic-gate  *	expanded into the target outside of the specified target region.
241*7c478bd9Sstevel@tonic-gate  *	This may result also with a wnoutrefresh() of a window onto
242*7c478bd9Sstevel@tonic-gate  *	curscr.
243*7c478bd9Sstevel@tonic-gate  *
244*7c478bd9Sstevel@tonic-gate  * Consider a pop-up dialog that contains narrow characters and
245*7c478bd9Sstevel@tonic-gate  * a base window that contains broad characters and we do the
246*7c478bd9Sstevel@tonic-gate  * following:
247*7c478bd9Sstevel@tonic-gate  *
248*7c478bd9Sstevel@tonic-gate  * 	save = dupwin(dialog);		// create backing store
249*7c478bd9Sstevel@tonic-gate  * 	overwrite(curscr, save);	// save region to be overlayed
250*7c478bd9Sstevel@tonic-gate  * 	wrefresh(dialog);		// display dialog
251*7c478bd9Sstevel@tonic-gate  * 	...				// do dialog stuff
252*7c478bd9Sstevel@tonic-gate  * 	wrefresh(save);			// restore screen image
253*7c478bd9Sstevel@tonic-gate  * 	delwin(save);			// release backing store
254*7c478bd9Sstevel@tonic-gate  *
255*7c478bd9Sstevel@tonic-gate  * Code similar to this has been used to implement generic popup()
256*7c478bd9Sstevel@tonic-gate  * and popdown() routines.  In the simple case where the base window
257*7c478bd9Sstevel@tonic-gate  * contains narrow characters only, it would be correctly restored.
258*7c478bd9Sstevel@tonic-gate  *
259*7c478bd9Sstevel@tonic-gate  * However with broad characters, the overwrite() could copy a
260*7c478bd9Sstevel@tonic-gate  * region with incomplete broad characters.  The wrefresh(dialog)
261*7c478bd9Sstevel@tonic-gate  * results in case 3.  In order to restore the window correctly with
262*7c478bd9Sstevel@tonic-gate  * wrefresh(save), we require case 4.
263*7c478bd9Sstevel@tonic-gate  *
264*7c478bd9Sstevel@tonic-gate  *  5.	Copy incomplete broad from source to target region next to margin.
265*7c478bd9Sstevel@tonic-gate  *
266*7c478bd9Sstevel@tonic-gate  *	a)
267*7c478bd9Sstevel@tonic-gate  *		copywin(s, t, 0, 1, 0, 0, 1, 2, 0)
268*7c478bd9Sstevel@tonic-gate  *		   s               t       ==>     t
269*7c478bd9Sstevel@tonic-gate  *		+------+	+------+	+------+
270*7c478bd9Sstevel@tonic-gate  *		|[]cdef|	|123456|	|#cd456|
271*7c478bd9Sstevel@tonic-gate  *		|ghijkl|	|789012|	|hij012|
272*7c478bd9Sstevel@tonic-gate  *		|mnopqr|	|......|	|......|
273*7c478bd9Sstevel@tonic-gate  *		+------+	+------+	+------+
274*7c478bd9Sstevel@tonic-gate  *	The # background character has replaced the ] character that
275*7c478bd9Sstevel@tonic-gate  *	would have been copied from the source, because it is not possible
276*7c478bd9Sstevel@tonic-gate  *	to expand the broad character to its complete form (case 4).
277*7c478bd9Sstevel@tonic-gate  *
278*7c478bd9Sstevel@tonic-gate  *	b)
279*7c478bd9Sstevel@tonic-gate  *		copywin(s, t, 0, 1, 0, 3, 1, 5, 0)
280*7c478bd9Sstevel@tonic-gate  *		   s               t       ==>     t
281*7c478bd9Sstevel@tonic-gate  *		+------+	+------+	+------+
282*7c478bd9Sstevel@tonic-gate  *		|abcdef|	|123456|	|123bcd|
283*7c478bd9Sstevel@tonic-gate  *		|ghi{}l|	|789012|	|789hi#|
284*7c478bd9Sstevel@tonic-gate  *		|mnopqr|	|......|	|......|
285*7c478bd9Sstevel@tonic-gate  *		+------+	+------+	+------+
286*7c478bd9Sstevel@tonic-gate  *	Same a 5a. but with the right margin.
287*7c478bd9Sstevel@tonic-gate  */
288