Home | History | Annotate | Line # | Download | only in libcurses
insdelln.c revision 1.5
      1 /*	$NetBSD: insdelln.c,v 1.5 2000/04/18 22:45:24 jdc 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  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Based on deleteln.c and insertln.c -
     41  * Copyright (c) 1981, 1993, 1994
     42  *	The Regents of the University of California.  All rights reserved.
     43  */
     44 
     45 #include <string.h>
     46 
     47 #include "curses.h"
     48 #include "curses_private.h"
     49 
     50 #ifndef _CURSES_USE_MACROS
     51 
     52 /*
     53  * insdelln --
     54  *	Insert or delete lines on stdscr, leaving (cury, curx) unchanged.
     55  */
     56 int
     57 insdelln(int lines)
     58 {
     59 	return winsdelln(stdscr, lines);
     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 lines)
     70 {
     71 	int     y, i;
     72 	__LINE *temp;
     73 
     74 #ifdef DEBUG
     75 	__CTRACE("winsdelln: (%0.2o) cury=%d lines=%d\n", win, win->cury,
     76 	    lines);
     77 #endif
     78 
     79 	if (!lines)
     80 		return(OK);
     81 
     82 	if (lines > 0) {
     83 		/* Insert lines */
     84 		if (lines > win->maxy - win->cury)
     85 			lines = win->maxy - win->cury;
     86 		for (y = win->maxy - lines - 1 ; y >= win->cury; --y) {
     87 			win->lines[y]->flags &= ~__ISPASTEOL;
     88 			win->lines[y + lines]->flags &= ~__ISPASTEOL;
     89 			if (win->orig == NULL) {
     90 				temp = win->lines[y + lines];
     91 				win->lines[y + lines] = win->lines[y];
     92 				win->lines[y] = temp;
     93 			} else {
     94 				(void) memcpy(win->lines[y + lines]->line,
     95 				    win->lines[y]->line,
     96 				    (size_t) win->maxx * __LDATASIZE * lines);
     97 				temp = win->lines[y];
     98 			}
     99 		}
    100 		for (y = win->cury - 1 + lines; y >= win->cury; --y)
    101 			for (i = 0; i < win->maxx; i++) {
    102 				win->lines[y]->line[i].ch = ' ';
    103 				win->lines[y]->line[i].bch = win->bch;
    104 				win->lines[y]->line[i].attr = 0;
    105 				win->lines[y]->line[i].battr = win->battr;
    106 			}
    107 		for (y = win->maxy - 1; y >= win->cury; --y)
    108 			__touchline(win, y, 0, (int) win->maxx - 1, 0);
    109 	} else {
    110 		/* Delete lines */
    111 		lines = 0 - lines;
    112 		if (lines > win->maxy - win->cury)
    113 			lines = win->maxy - win->cury;
    114 		for (y = win->cury; y < win->maxy - lines; y++) {
    115 			win->lines[y]->flags &= ~__ISPASTEOL;
    116 			win->lines[y + lines]->flags &= ~__ISPASTEOL;
    117 			if (win->orig == NULL) {
    118 				temp = win->lines[y];
    119 				win->lines[y] = win->lines[y + lines];
    120 				win->lines[y + lines] = temp;
    121 			} else {
    122 				(void) memcpy(win->lines[y]->line,
    123 				    win->lines[y + lines]->line,
    124 				    (size_t) win->maxx * __LDATASIZE * lines);
    125 				temp = win->lines[y + lines];
    126 			}
    127 		}
    128 		for (y = win->maxy - lines; y < win->maxy; y++)
    129 			for (i = 0; i < win->maxx; i++) {
    130 				win->lines[y]->line[i].ch = ' ';
    131 				win->lines[y]->line[i].bch = win->bch;
    132 				win->lines[y]->line[i].attr = 0;
    133 				win->lines[y]->line[i].battr = win->battr;
    134 			}
    135 		for (y = win->cury; y < win->maxy; y++)
    136 			__touchline(win, y, 0, (int) win->maxx - 1, 0);
    137 	}
    138 	if (win->orig != NULL)
    139 		__id_subwins(win->orig);
    140 	return (OK);
    141 }
    142