1*c7ef0cfcSnicm /* $OpenBSD: lib_window.c,v 1.4 2023/10/17 09:52:09 nicm Exp $ */
292dd1ec0Smillert
392dd1ec0Smillert /****************************************************************************
4*c7ef0cfcSnicm * Copyright 2020,2021 Thomas E. Dickey *
5*c7ef0cfcSnicm * Copyright 1998-2010,2016 Free Software Foundation, Inc. *
692dd1ec0Smillert * *
792dd1ec0Smillert * Permission is hereby granted, free of charge, to any person obtaining a *
892dd1ec0Smillert * copy of this software and associated documentation files (the *
992dd1ec0Smillert * "Software"), to deal in the Software without restriction, including *
1092dd1ec0Smillert * without limitation the rights to use, copy, modify, merge, publish, *
1192dd1ec0Smillert * distribute, distribute with modifications, sublicense, and/or sell *
1292dd1ec0Smillert * copies of the Software, and to permit persons to whom the Software is *
1392dd1ec0Smillert * furnished to do so, subject to the following conditions: *
1492dd1ec0Smillert * *
1592dd1ec0Smillert * The above copyright notice and this permission notice shall be included *
1692dd1ec0Smillert * in all copies or substantial portions of the Software. *
1792dd1ec0Smillert * *
1892dd1ec0Smillert * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
1992dd1ec0Smillert * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
2092dd1ec0Smillert * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
2192dd1ec0Smillert * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
2292dd1ec0Smillert * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
2392dd1ec0Smillert * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
2492dd1ec0Smillert * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
2592dd1ec0Smillert * *
2692dd1ec0Smillert * Except as contained in this notice, the name(s) of the above copyright *
2792dd1ec0Smillert * holders shall not be used in advertising or otherwise to promote the *
2892dd1ec0Smillert * sale, use or other dealings in this Software without prior written *
2992dd1ec0Smillert * authorization. *
3092dd1ec0Smillert ****************************************************************************/
3192dd1ec0Smillert
3292dd1ec0Smillert /****************************************************************************
3392dd1ec0Smillert * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
3492dd1ec0Smillert * and: Eric S. Raymond <esr@snark.thyrsus.com> *
3592dd1ec0Smillert ****************************************************************************/
3692dd1ec0Smillert
3792dd1ec0Smillert /*
3892dd1ec0Smillert ** lib_window.c
3992dd1ec0Smillert **
4092dd1ec0Smillert **
4192dd1ec0Smillert */
4292dd1ec0Smillert
4392dd1ec0Smillert #include <curses.priv.h>
4492dd1ec0Smillert
45*c7ef0cfcSnicm MODULE_ID("$Id: lib_window.c,v 1.4 2023/10/17 09:52:09 nicm Exp $")
4692dd1ec0Smillert
NCURSES_EXPORT(void)4784af20ceSmillert NCURSES_EXPORT(void)
4884af20ceSmillert _nc_synchook(WINDOW *win)
4992dd1ec0Smillert /* hook to be called after each window change */
5092dd1ec0Smillert {
5184af20ceSmillert if (win->_immed)
5284af20ceSmillert wrefresh(win);
5384af20ceSmillert if (win->_sync)
5484af20ceSmillert wsyncup(win);
5592dd1ec0Smillert }
5692dd1ec0Smillert
5784af20ceSmillert NCURSES_EXPORT(int)
mvderwin(WINDOW * win,int y,int x)5884af20ceSmillert mvderwin(WINDOW *win, int y, int x)
5992dd1ec0Smillert /* move a derived window */
6092dd1ec0Smillert {
6192dd1ec0Smillert WINDOW *orig;
62*c7ef0cfcSnicm int rc = ERR;
63*c7ef0cfcSnicm
64*c7ef0cfcSnicm T((T_CALLED("mvderwin(%p,%d,%d)"), (void *) win, y, x));
65*c7ef0cfcSnicm
66*c7ef0cfcSnicm if (win != 0
67*c7ef0cfcSnicm && (orig = win->_parent) != 0
68*c7ef0cfcSnicm && (x >= 0 && y >= 0)
69*c7ef0cfcSnicm && (x + getmaxx(win) <= getmaxx(orig))
70*c7ef0cfcSnicm && (y + getmaxy(win) <= getmaxy(orig))) {
7192dd1ec0Smillert int i;
7292dd1ec0Smillert
7392dd1ec0Smillert wsyncup(win);
7492dd1ec0Smillert win->_parx = x;
7592dd1ec0Smillert win->_pary = y;
7692dd1ec0Smillert for (i = 0; i < getmaxy(win); i++)
7792dd1ec0Smillert win->_line[i].text = &(orig->_line[y++].text[x]);
78*c7ef0cfcSnicm rc = OK;
79*c7ef0cfcSnicm }
80*c7ef0cfcSnicm returnCode(rc);
8192dd1ec0Smillert }
8292dd1ec0Smillert
8384af20ceSmillert NCURSES_EXPORT(int)
syncok(WINDOW * win,bool bf)8484af20ceSmillert syncok(WINDOW *win, bool bf)
8592dd1ec0Smillert /* enable/disable automatic wsyncup() on each change to window */
8692dd1ec0Smillert {
87*c7ef0cfcSnicm T((T_CALLED("syncok(%p,%d)"), (void *) win, bf));
8892dd1ec0Smillert
8992dd1ec0Smillert if (win) {
9092dd1ec0Smillert win->_sync = bf;
9192dd1ec0Smillert returnCode(OK);
9292dd1ec0Smillert } else
9392dd1ec0Smillert returnCode(ERR);
9492dd1ec0Smillert }
9592dd1ec0Smillert
9684af20ceSmillert NCURSES_EXPORT(void)
wsyncup(WINDOW * win)9784af20ceSmillert wsyncup(WINDOW *win)
9892dd1ec0Smillert /* mark changed every cell in win's ancestors that is changed in win */
9992dd1ec0Smillert /* Rewritten by J. Pfeifer, 1-Apr-96 (don't even think that...) */
10092dd1ec0Smillert {
10192dd1ec0Smillert WINDOW *wp;
10292dd1ec0Smillert
103*c7ef0cfcSnicm T((T_CALLED("wsyncup(%p)"), (void *) win));
10481d8c4e1Snicm if (win && win->_parent) {
10584af20ceSmillert for (wp = win; wp->_parent; wp = wp->_parent) {
10692dd1ec0Smillert int y;
10792dd1ec0Smillert WINDOW *pp = wp->_parent;
10892dd1ec0Smillert
10992dd1ec0Smillert assert((wp->_pary <= pp->_maxy) &&
11092dd1ec0Smillert ((wp->_pary + wp->_maxy) <= pp->_maxy));
11192dd1ec0Smillert
11284af20ceSmillert for (y = 0; y <= wp->_maxy; y++) {
11392dd1ec0Smillert int left = wp->_line[y].firstchar;
11484af20ceSmillert if (left >= 0) { /* line is touched */
11592dd1ec0Smillert struct ldat *line = &(pp->_line[wp->_pary + y]);
11692dd1ec0Smillert /* left & right character in parent window coordinates */
11792dd1ec0Smillert int right = wp->_line[y].lastchar + wp->_parx;
11892dd1ec0Smillert left += wp->_parx;
11992dd1ec0Smillert
12092dd1ec0Smillert CHANGED_RANGE(line, left, right);
12192dd1ec0Smillert }
12292dd1ec0Smillert }
12392dd1ec0Smillert }
12492dd1ec0Smillert }
12581d8c4e1Snicm returnVoid;
12681d8c4e1Snicm }
12792dd1ec0Smillert
12884af20ceSmillert NCURSES_EXPORT(void)
wsyncdown(WINDOW * win)12984af20ceSmillert wsyncdown(WINDOW *win)
13092dd1ec0Smillert /* mark changed every cell in win that is changed in any of its ancestors */
13192dd1ec0Smillert /* Rewritten by J. Pfeifer, 1-Apr-96 (don't even think that...) */
13292dd1ec0Smillert {
133*c7ef0cfcSnicm T((T_CALLED("wsyncdown(%p)"), (void *) win));
13481d8c4e1Snicm
135*c7ef0cfcSnicm if (win != NULL && win->_parent != NULL) {
13692dd1ec0Smillert WINDOW *pp = win->_parent;
13792dd1ec0Smillert int y;
13892dd1ec0Smillert
13992dd1ec0Smillert /* This recursion guarantees, that the changes are propagated down-
14092dd1ec0Smillert wards from the root to our direct parent. */
14192dd1ec0Smillert wsyncdown(pp);
14292dd1ec0Smillert
14392dd1ec0Smillert /* and now we only have to propagate the changes from our direct
14492dd1ec0Smillert parent, if there are any. */
14592dd1ec0Smillert assert((win->_pary <= pp->_maxy) &&
14692dd1ec0Smillert ((win->_pary + win->_maxy) <= pp->_maxy));
14792dd1ec0Smillert
14884af20ceSmillert for (y = 0; y <= win->_maxy; y++) {
14984af20ceSmillert if (pp->_line[win->_pary + y].firstchar >= 0) { /* parent changed */
15092dd1ec0Smillert struct ldat *line = &(win->_line[y]);
15192dd1ec0Smillert /* left and right character in child coordinates */
15292dd1ec0Smillert int left = pp->_line[win->_pary + y].firstchar - win->_parx;
15392dd1ec0Smillert int right = pp->_line[win->_pary + y].lastchar - win->_parx;
15481d8c4e1Snicm /* The change may be outside the child's range */
15592dd1ec0Smillert if (left < 0)
15692dd1ec0Smillert left = 0;
15792dd1ec0Smillert if (right > win->_maxx)
15892dd1ec0Smillert right = win->_maxx;
15992dd1ec0Smillert CHANGED_RANGE(line, left, right);
16092dd1ec0Smillert }
16192dd1ec0Smillert }
16292dd1ec0Smillert }
16381d8c4e1Snicm returnVoid;
16492dd1ec0Smillert }
16592dd1ec0Smillert
16684af20ceSmillert NCURSES_EXPORT(void)
wcursyncup(WINDOW * win)16784af20ceSmillert wcursyncup(WINDOW *win)
16892dd1ec0Smillert /* sync the cursor in all derived windows to its value in the base window */
16992dd1ec0Smillert {
17092dd1ec0Smillert WINDOW *wp;
17181d8c4e1Snicm
172*c7ef0cfcSnicm T((T_CALLED("wcursyncup(%p)"), (void *) win));
17392dd1ec0Smillert for (wp = win; wp && wp->_parent; wp = wp->_parent) {
17492dd1ec0Smillert wmove(wp->_parent, wp->_pary + wp->_cury, wp->_parx + wp->_curx);
17592dd1ec0Smillert }
17681d8c4e1Snicm returnVoid;
17792dd1ec0Smillert }
17892dd1ec0Smillert
17984af20ceSmillert NCURSES_EXPORT(WINDOW *)
dupwin(WINDOW * win)18084af20ceSmillert dupwin(WINDOW *win)
18192dd1ec0Smillert /* make an exact duplicate of the given window */
18292dd1ec0Smillert {
18381d8c4e1Snicm WINDOW *nwin = 0;
18492dd1ec0Smillert
185*c7ef0cfcSnicm T((T_CALLED("dupwin(%p)"), (void *) win));
18692dd1ec0Smillert
18781d8c4e1Snicm if (win != 0) {
188*c7ef0cfcSnicm #if NCURSES_SP_FUNCS
189*c7ef0cfcSnicm SCREEN *sp = _nc_screen_of(win);
190*c7ef0cfcSnicm #endif
19181d8c4e1Snicm _nc_lock_global(curses);
192*c7ef0cfcSnicm if (IS_PAD(win)) {
193*c7ef0cfcSnicm nwin = NCURSES_SP_NAME(newpad) (NCURSES_SP_ARGx
194*c7ef0cfcSnicm win->_maxy + 1,
19581d8c4e1Snicm win->_maxx + 1);
19681d8c4e1Snicm } else {
197*c7ef0cfcSnicm nwin = NCURSES_SP_NAME(newwin) (NCURSES_SP_ARGx
198*c7ef0cfcSnicm win->_maxy + 1,
19981d8c4e1Snicm win->_maxx + 1,
20081d8c4e1Snicm win->_begy,
20181d8c4e1Snicm win->_begx);
20281d8c4e1Snicm }
20381d8c4e1Snicm
20481d8c4e1Snicm if (nwin != 0) {
205*c7ef0cfcSnicm int i;
206*c7ef0cfcSnicm size_t linesize;
20792dd1ec0Smillert
20892dd1ec0Smillert nwin->_curx = win->_curx;
20992dd1ec0Smillert nwin->_cury = win->_cury;
21092dd1ec0Smillert nwin->_maxy = win->_maxy;
21192dd1ec0Smillert nwin->_maxx = win->_maxx;
21292dd1ec0Smillert nwin->_begy = win->_begy;
21392dd1ec0Smillert nwin->_begx = win->_begx;
21492dd1ec0Smillert nwin->_yoffset = win->_yoffset;
21592dd1ec0Smillert
21692dd1ec0Smillert nwin->_flags = win->_flags & ~_SUBWIN;
21792dd1ec0Smillert /* Due to the use of newwin(), the clone is not a subwindow.
21892dd1ec0Smillert * The text is really copied into the clone.
21992dd1ec0Smillert */
22092dd1ec0Smillert
22181d8c4e1Snicm WINDOW_ATTRS(nwin) = WINDOW_ATTRS(win);
22281d8c4e1Snicm nwin->_nc_bkgd = win->_nc_bkgd;
22392dd1ec0Smillert
22481d8c4e1Snicm nwin->_notimeout = win->_notimeout;
22592dd1ec0Smillert nwin->_clear = win->_clear;
22692dd1ec0Smillert nwin->_leaveok = win->_leaveok;
22781d8c4e1Snicm nwin->_scroll = win->_scroll;
22881d8c4e1Snicm nwin->_idlok = win->_idlok;
22981d8c4e1Snicm nwin->_idcok = win->_idcok;
23092dd1ec0Smillert nwin->_immed = win->_immed;
23192dd1ec0Smillert nwin->_sync = win->_sync;
23281d8c4e1Snicm nwin->_use_keypad = win->_use_keypad;
23381d8c4e1Snicm nwin->_delay = win->_delay;
23492dd1ec0Smillert
23592dd1ec0Smillert nwin->_parx = 0;
23692dd1ec0Smillert nwin->_pary = 0;
23792dd1ec0Smillert nwin->_parent = (WINDOW *) 0;
23892dd1ec0Smillert /* See above: the clone isn't a subwindow! */
23992dd1ec0Smillert
24092dd1ec0Smillert nwin->_regtop = win->_regtop;
24192dd1ec0Smillert nwin->_regbottom = win->_regbottom;
24292dd1ec0Smillert
243*c7ef0cfcSnicm if (IS_PAD(win))
24481d8c4e1Snicm nwin->_pad = win->_pad;
24581d8c4e1Snicm
246*c7ef0cfcSnicm linesize = (unsigned) (win->_maxx + 1) * sizeof(NCURSES_CH_T);
24792dd1ec0Smillert for (i = 0; i <= nwin->_maxy; i++) {
24892dd1ec0Smillert memcpy(nwin->_line[i].text, win->_line[i].text, linesize);
24992dd1ec0Smillert nwin->_line[i].firstchar = win->_line[i].firstchar;
25092dd1ec0Smillert nwin->_line[i].lastchar = win->_line[i].lastchar;
25192dd1ec0Smillert }
25281d8c4e1Snicm }
25381d8c4e1Snicm _nc_unlock_global(curses);
25481d8c4e1Snicm }
25592dd1ec0Smillert returnWin(nwin);
25692dd1ec0Smillert }
257