Home | History | Annotate | Line # | Download | only in libcurses
insdelln.c revision 1.21
      1 /*	$NetBSD: insdelln.c,v 1.21 2022/04/12 07:03:04 blymn Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Julian Coleman.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 __RCSID("$NetBSD: insdelln.c,v 1.21 2022/04/12 07:03:04 blymn Exp $");
     35 #endif				/* not lint */
     36 
     37 /*
     38  * Based on deleteln.c and insertln.c -
     39  * Copyright (c) 1981, 1993, 1994
     40  *	The Regents of the University of California.  All rights reserved.
     41  */
     42 
     43 #include <string.h>
     44 #include <stdlib.h>
     45 
     46 #include "curses.h"
     47 #include "curses_private.h"
     48 
     49 #ifndef _CURSES_USE_MACROS
     50 
     51 /*
     52  * insdelln --
     53  *	Insert or delete lines on stdscr, leaving (cury, curx) unchanged.
     54  */
     55 int
     56 insdelln(int nlines)
     57 {
     58 
     59 	return winsdelln(stdscr, nlines);
     60 }
     61 
     62 #endif
     63 
     64 /*
     65  * winsdelln --
     66  *	Insert or delete lines on the window, leaving (cury, curx) unchanged.
     67  */
     68 int
     69 winsdelln(WINDOW *win, int nlines)
     70 {
     71 	int     y, i, last;
     72 	__LINE *temp;
     73 #ifdef HAVE_WCHAR
     74 	__LDATA *lp;
     75 #endif /* HAVE_WCHAR */
     76 	attr_t	attr;
     77 
     78 	__CTRACE(__CTRACE_LINE,
     79 	    "winsdelln: (%p) cury=%d lines=%d\n", win, win->cury, nlines);
     80 
     81 	if (!nlines)
     82 		return OK;
     83 
     84 	if (__using_color && win != curscr)
     85 		attr = win->battr & __COLOR;
     86 	else
     87 		attr = 0;
     88 
     89 	if (nlines > 0) {
     90 		/* Insert lines */
     91 		if (win->cury < win->scr_t || win->cury > win->scr_b) {
     92 			/*  Outside scrolling region */
     93 			if (nlines > win->maxy - win->cury)
     94 				nlines = win->maxy - win->cury;
     95 			last = win->maxy - 1;
     96 		} else {
     97 			/* Inside scrolling region */
     98 			if (nlines > win->scr_b + 1 - win->cury)
     99 				nlines = win->scr_b + 1 - win->cury;
    100 			last = win->scr_b;
    101 		}
    102 		for (y = last - nlines; y >= win->cury; --y) {
    103 			win->alines[y]->flags &= ~__ISPASTEOL;
    104 			win->alines[y + nlines]->flags &= ~__ISPASTEOL;
    105 			if (win->orig == NULL) {
    106 				temp = win->alines[y + nlines];
    107 				win->alines[y + nlines] = win->alines[y];
    108 				win->alines[y] = temp;
    109 			} else {
    110 				(void)memcpy(win->alines[y + nlines]->line,
    111 				    win->alines[y]->line,
    112 				    (size_t)win->maxx * __LDATASIZE);
    113 			}
    114 		}
    115 		for (y = win->cury - 1 + nlines; y >= win->cury; --y)
    116 			for (i = 0; i < win->maxx; i++) {
    117 				win->alines[y]->line[i].ch = win->bch;
    118 				win->alines[y]->line[i].attr = attr;
    119 				win->alines[y]->line[i].ch = win->bch;
    120 #ifdef HAVE_WCHAR
    121 				lp = &win->alines[y]->line[i];
    122 				if (_cursesi_copy_nsp(win->bnsp, lp) == ERR)
    123 					return ERR;
    124 				lp->wcols = 1;
    125 #endif /* HAVE_WCHAR */
    126 			}
    127 		for (y = last; y >= win->cury; --y)
    128 			__touchline(win, y, 0, (int)win->maxx - 1);
    129 	} else {
    130 		/* Delete nlines */
    131 		nlines = 0 - nlines;
    132 		if (win->cury < win->scr_t || win->cury > win->scr_b) {
    133 			/*  Outside scrolling region */
    134 			if (nlines > win->maxy - win->cury)
    135 				nlines = win->maxy - win->cury;
    136 			last = win->maxy;
    137 		} else {
    138 			/* Inside scrolling region */
    139 			if (nlines > win->scr_b + 1 - win->cury)
    140 				nlines = win->scr_b + 1 - win->cury;
    141 			last = win->scr_b + 1;
    142 		}
    143 		for (y = win->cury; y < last - nlines; y++) {
    144 			win->alines[y]->flags &= ~__ISPASTEOL;
    145 			win->alines[y + nlines]->flags &= ~__ISPASTEOL;
    146 			if (win->orig == NULL) {
    147 				temp = win->alines[y];
    148 				win->alines[y] = win->alines[y + nlines];
    149 				win->alines[y + nlines] = temp;
    150 			} else {
    151 				(void)memcpy(win->alines[y]->line,
    152 				    win->alines[y + nlines]->line,
    153 				    (size_t)win->maxx * __LDATASIZE);
    154 			}
    155 		}
    156 		for (y = last - nlines; y < last; y++)
    157 			for (i = 0; i < win->maxx; i++) {
    158 				win->alines[y]->line[i].ch = win->bch;
    159 				win->alines[y]->line[i].attr = attr;
    160 				win->alines[y]->line[i].ch = win->bch;
    161 #ifndef HAVE_WCHAR
    162 				lp = &win->alines[y]->line[i];
    163 				lp->wcols = 1;
    164 				if (_cursesi_copy_nsp(win->bnsp, lp) == ERR)
    165 					return ERR;
    166 #endif /* HAVE_WCHAR */
    167 			}
    168 		for (y = win->cury; y < last; y++)
    169 			__touchline(win, y, 0, (int)win->maxx - 1);
    170 	}
    171 	if (win->orig != NULL)
    172 		__id_subwins(win->orig);
    173 	__sync(win);
    174 	return OK;
    175 }
    176