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