Home | History | Annotate | Line # | Download | only in libcurses
touchwin.c revision 1.21
      1 /*	$NetBSD: touchwin.c,v 1.21 2006/01/01 11:48:31 jdc Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1981, 1993, 1994
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 #if 0
     35 static char sccsid[] = "@(#)touchwin.c	8.2 (Berkeley) 5/4/94";
     36 #else
     37 __RCSID("$NetBSD: touchwin.c,v 1.21 2006/01/01 11:48:31 jdc Exp $");
     38 #endif
     39 #endif				/* not lint */
     40 
     41 #include "curses.h"
     42 #include "curses_private.h"
     43 
     44 /*
     45  * is_linetouched --
     46  *    Indicate if line has been touched or not.
     47  */
     48 bool
     49 is_linetouched(WINDOW *win, int line)
     50 {
     51 	if (line > win->maxy)
     52 		return FALSE;
     53 
     54 	return ((win->lines[line]->flags & __ISDIRTY) != 0);
     55 }
     56 
     57 /*
     58  * touchline --
     59  *	Touch count lines starting at start.  This is the SUS v2 compliant
     60  *	version.
     61  */
     62 int
     63 touchline(WINDOW *win, int start, int count)
     64 {
     65 	return wtouchln(win, start, count, 1);
     66 }
     67 
     68 /*
     69  * wredrawln --
     70  *	Mark count lines starting at start as corrupted.  Implemented using
     71  *	wtouchln().
     72  */
     73 int wredrawln(WINDOW *win, int start, int count)
     74 {
     75 #ifdef DEBUG
     76 	__CTRACE("wredrawln: (%p, %d, %d)\n", win, start, count);
     77 #endif
     78 	return wtouchln(win, start, count, 1);
     79 }
     80 
     81 /*
     82  * is_wintouched --
     83  *      Check if the window has been touched.
     84  */
     85 bool
     86 is_wintouched(WINDOW *win)
     87 {
     88 	int y, maxy;
     89 
     90 	maxy = win->maxy;
     91 	for (y = 0; y < maxy; y++) {
     92 		if (is_linetouched(win, y) == TRUE)
     93 			return TRUE;
     94 	}
     95 
     96 	return FALSE;
     97 }
     98 
     99 /*
    100  * touchwin --
    101  *	Make it look like the whole window has been changed.
    102  */
    103 int
    104 touchwin(WINDOW *win)
    105 {
    106 #ifdef DEBUG
    107 	__CTRACE("touchwin: (%p)\n", win);
    108 #endif
    109 	return wtouchln(win, 0, win->maxy, 1);
    110 }
    111 
    112 /*
    113  * redrawwin --
    114  *	Mark entire window as corrupted.  Implemented using wtouchln().
    115  */
    116 int
    117 redrawwin(WINDOW *win)
    118 {
    119 #ifdef DEBUG
    120 	__CTRACE("redrawwin: (%p)\n", win);
    121 #endif
    122 	return wtouchln(win, 0, win->maxy, 1);
    123 }
    124 
    125 /*
    126  * untouchwin --
    127  *     Make it look like the window has not been changed.
    128  */
    129 int
    130 untouchwin(WINDOW *win)
    131 {
    132 	return wtouchln(win, 0, win->maxy, 0);
    133 }
    134 
    135 /*
    136  * wtouchln --
    137  *     If changed is 1 then touch n lines starting at line.  If changed
    138  *     is 0 then mark the lines as unchanged.
    139  */
    140 int
    141 wtouchln(WINDOW *win, int line, int n, int changed)
    142 {
    143 	int	y;
    144 	__LINE	*wlp;
    145 
    146 #ifdef DEBUG
    147 	__CTRACE("wtouchln: (%p) %d, %d, %d\n", win, line, n, changed);
    148 #endif
    149 	if (line + n > win->maxy)
    150 		line = win->maxy - n;
    151 	for (y = line; y < line + n; y++) {
    152 		if (changed == 1)
    153 			__touchline(win, y, 0, (int) win->maxx - 1);
    154 		else {
    155 			wlp = win->lines[y];
    156 			if (*wlp->firstchp >= win->ch_off &&
    157 			    *wlp->firstchp < win->maxx + win->ch_off)
    158 				*wlp->firstchp = win->maxx + win->ch_off;
    159 			if (*wlp->lastchp >= win->ch_off &&
    160 			    *wlp->lastchp < win->maxx + win->ch_off)
    161 				*wlp->lastchp = win->ch_off;
    162 			wlp->flags &= ~__ISDIRTY;
    163 		}
    164 	}
    165 
    166 	return OK;
    167 }
    168 
    169 
    170 int
    171 __touchwin(WINDOW *win)
    172 {
    173 	int     y, maxy;
    174 
    175 #ifdef DEBUG
    176 	__CTRACE("touchwin: (%p)\n", win);
    177 #endif
    178 	maxy = win->maxy;
    179 	for (y = 0; y < maxy; y++)
    180 		__touchline(win, y, 0, (int) win->maxx - 1);
    181 	return (OK);
    182 }
    183 
    184 int
    185 __touchline(WINDOW *win, int y, int sx, int ex)
    186 {
    187 #ifdef DEBUG
    188 	__CTRACE("touchline: (%p, %d, %d, %d)\n", win, y, sx, ex);
    189 	__CTRACE("touchline: first = %d, last = %d\n",
    190 	    *win->lines[y]->firstchp, *win->lines[y]->lastchp);
    191 #endif
    192 	sx += win->ch_off;
    193 	ex += win->ch_off;
    194 	if (!(win->lines[y]->flags & __ISDIRTY))
    195 		win->lines[y]->flags |= __ISDIRTY;
    196 	/* firstchp/lastchp are shared between parent window and sub-window. */
    197 	if (*win->lines[y]->firstchp > sx)
    198 		*win->lines[y]->firstchp = sx;
    199 	if (*win->lines[y]->lastchp < ex)
    200 		*win->lines[y]->lastchp = ex;
    201 #ifdef DEBUG
    202 	__CTRACE("touchline: first = %d, last = %d\n",
    203 	    *win->lines[y]->firstchp, *win->lines[y]->lastchp);
    204 #endif
    205 	return (OK);
    206 }
    207