ncursesw-morphos/ncurses/base/wresize.c

251 lines
7.9 KiB
C
Raw Normal View History

1998-03-01 12:21:12 +08:00
/****************************************************************************
* Copyright (c) 1998-2008,2009 Free Software Foundation, Inc. *
1998-03-01 12:21:12 +08:00
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the *
* "Software"), to deal in the Software without restriction, including *
* without limitation the rights to use, copy, modify, merge, publish, *
* distribute, distribute with modifications, sublicense, and/or sell *
* copies of the Software, and to permit persons to whom the Software is *
* furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included *
* in all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
* IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
* THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
* *
* Except as contained in this notice, the name(s) of the above copyright *
* holders shall not be used in advertising or otherwise to promote the *
* sale, use or other dealings in this Software without prior written *
* authorization. *
****************************************************************************/
/****************************************************************************
* Author: Thomas E. Dickey 1996-on *
* and: Juergen Pfeifer *
1998-03-01 12:21:12 +08:00
****************************************************************************/
1997-05-15 12:00:00 +08:00
#include <curses.priv.h>
MODULE_ID("$Id: wresize.c,v 1.31 2009/05/23 19:50:16 tom Exp $")
2002-10-13 11:35:53 +08:00
static int
cleanup_lines(struct ldat *data, int length)
{
while (--length >= 0)
2007-04-08 09:10:28 +08:00
free(data[length].text);
2002-10-13 11:35:53 +08:00
free(data);
return ERR;
}
/*
* If we have reallocated the ldat structs, we will have to repair pointers
* used in subwindows.
*/
static void
repair_subwindows(WINDOW *cmp)
{
WINDOWLIST *wp;
struct ldat *pline = cmp->_line;
int row;
#ifdef USE_SP_WINDOWLIST
SCREEN *sp = _nc_screen_of(cmp);
#endif
2002-10-13 11:35:53 +08:00
_nc_lock_global(curses);
for (each_window(SP_PARM, wp)) {
2002-10-13 11:35:53 +08:00
WINDOW *tst = &(wp->win);
if (tst->_parent == cmp) {
if (tst->_pary > cmp->_maxy)
tst->_pary = cmp->_maxy;
if (tst->_parx > cmp->_maxx)
tst->_parx = cmp->_maxx;
if (tst->_maxy + tst->_pary > cmp->_maxy)
tst->_maxy = cmp->_maxy - tst->_pary;
if (tst->_maxx + tst->_parx > cmp->_maxx)
tst->_maxx = cmp->_maxx - tst->_parx;
for (row = 0; row <= tst->_maxy; ++row) {
tst->_line[row].text = &pline[tst->_pary + row].text[tst->_parx];
}
repair_subwindows(tst);
}
}
_nc_unlock_global(curses);
2002-10-13 11:35:53 +08:00
}
1997-05-15 12:00:00 +08:00
/*
* Reallocate a curses WINDOW struct to either shrink or grow to the specified
* new lines/columns. If it grows, the new character cells are filled with
* blanks. The application is responsible for repainting the blank area.
*/
2002-10-13 11:35:53 +08:00
NCURSES_EXPORT(int)
1997-05-15 12:00:00 +08:00
wresize(WINDOW *win, int ToLines, int ToCols)
{
2002-10-13 11:35:53 +08:00
int col, row, size_x, size_y;
2000-07-09 10:46:08 +08:00
struct ldat *pline;
2002-10-13 11:35:53 +08:00
struct ldat *new_lines = 0;
1997-05-15 12:00:00 +08:00
#ifdef TRACE
2000-07-09 10:46:08 +08:00
T((T_CALLED("wresize(%p,%d,%d)"), win, ToLines, ToCols));
if (win) {
2006-12-18 12:32:42 +08:00
TR(TRACE_UPDATE, ("...beg (%ld, %ld), max(%ld,%ld), reg(%ld,%ld)",
(long) win->_begy, (long) win->_begx,
(long) win->_maxy, (long) win->_maxx,
(long) win->_regtop, (long) win->_regbottom));
if (USE_TRACEF(TRACE_UPDATE)) {
1998-03-01 12:21:12 +08:00
_tracedump("...before", win);
_nc_unlock_global(tracef);
}
2000-07-09 10:46:08 +08:00
}
1997-05-15 12:00:00 +08:00
#endif
2000-07-09 10:46:08 +08:00
if (!win || --ToLines < 0 || --ToCols < 0)
returnCode(ERR);
1997-05-15 12:00:00 +08:00
2000-07-09 10:46:08 +08:00
size_x = win->_maxx;
size_y = win->_maxy;
1997-05-15 12:00:00 +08:00
2000-07-09 10:46:08 +08:00
if (ToLines == size_y
&& ToCols == size_x)
returnCode(OK);
1998-03-01 12:21:12 +08:00
2000-07-09 10:46:08 +08:00
if ((win->_flags & _SUBWIN)) {
1997-05-15 12:00:00 +08:00
/*
2000-07-09 10:46:08 +08:00
* Check if the new limits will fit into the parent window's size. If
* not, do not resize. We could adjust the location of the subwindow,
* but the application may not like that.
1997-05-15 12:00:00 +08:00
*/
2000-07-09 10:46:08 +08:00
if (win->_pary + ToLines > win->_parent->_maxy
|| win->_parx + ToCols > win->_parent->_maxx) {
returnCode(ERR);
1997-05-15 12:00:00 +08:00
}
2000-07-09 10:46:08 +08:00
pline = win->_parent->_line;
} else {
pline = 0;
}
/*
2002-10-13 11:35:53 +08:00
* Allocate new memory as needed. Do the allocations without modifying
* the original window, in case an allocation fails. Always allocate
* (at least temporarily) the array pointing to the individual lines.
*/
new_lines = typeCalloc(struct ldat, (unsigned) (ToLines + 1));
if (new_lines == 0)
returnCode(ERR);
/*
* For each line in the target, allocate or adjust pointers for the
* corresponding text, depending on whether this is a window or a
* subwindow.
2000-07-09 10:46:08 +08:00
*/
2002-10-13 11:35:53 +08:00
for (row = 0; row <= ToLines; ++row) {
int begin = (row > size_y) ? 0 : (size_x + 1);
int end = ToCols;
NCURSES_CH_T *s;
2000-07-09 10:46:08 +08:00
if (!(win->_flags & _SUBWIN)) {
2002-10-13 11:35:53 +08:00
if (row <= size_y) {
if (ToCols != size_x) {
if ((s = typeMalloc(NCURSES_CH_T, ToCols + 1)) == 0)
returnCode(cleanup_lines(new_lines, row));
for (col = 0; col <= ToCols; ++col) {
s[col] = (col <= size_x
? win->_line[row].text[col]
: win->_nc_bkgd);
}
} else {
s = win->_line[row].text;
}
} else {
if ((s = typeMalloc(NCURSES_CH_T, ToCols + 1)) == 0)
returnCode(cleanup_lines(new_lines, row));
for (col = 0; col <= ToCols; ++col)
s[col] = win->_nc_bkgd;
}
} else {
s = &pline[win->_pary + row].text[win->_parx];
1997-05-15 12:00:00 +08:00
}
2002-10-13 11:35:53 +08:00
if_USE_SCROLL_HINTS(new_lines[row].oldindex = row);
if (row <= size_y) {
new_lines[row].firstchar = win->_line[row].firstchar;
new_lines[row].lastchar = win->_line[row].lastchar;
}
if ((ToCols != size_x) || (row > size_y)) {
if (end >= begin) { /* growing */
if (new_lines[row].firstchar < begin)
new_lines[row].firstchar = begin;
} else { /* shrinking */
new_lines[row].firstchar = 0;
2000-07-09 10:46:08 +08:00
}
2002-10-13 11:35:53 +08:00
new_lines[row].lastchar = ToCols;
2000-07-09 10:46:08 +08:00
}
2002-10-13 11:35:53 +08:00
new_lines[row].text = s;
2000-07-09 10:46:08 +08:00
}
/*
2002-10-13 11:35:53 +08:00
* Dispose of unwanted memory.
2000-07-09 10:46:08 +08:00
*/
2002-10-13 11:35:53 +08:00
if (!(win->_flags & _SUBWIN)) {
if (ToCols == size_x) {
for (row = ToLines + 1; row <= size_y; row++) {
free(win->_line[row].text);
2000-07-09 10:46:08 +08:00
}
2002-10-13 11:35:53 +08:00
} else {
for (row = 0; row <= size_y; row++) {
free(win->_line[row].text);
2000-07-09 10:46:08 +08:00
}
}
}
2002-10-13 11:35:53 +08:00
free(win->_line);
win->_line = new_lines;
2000-07-09 10:46:08 +08:00
/*
* Finally, adjust the parameters showing screen size and cursor
* position:
*/
win->_maxx = ToCols;
win->_maxy = ToLines;
if (win->_regtop > win->_maxy)
win->_regtop = win->_maxy;
if (win->_regbottom > win->_maxy
|| win->_regbottom == size_y)
win->_regbottom = win->_maxy;
if (win->_curx > win->_maxx)
win->_curx = win->_maxx;
if (win->_cury > win->_maxy)
win->_cury = win->_maxy;
1997-05-15 12:00:00 +08:00
2002-10-13 11:35:53 +08:00
/*
* Check for subwindows of this one, and readjust pointers to our text,
* if needed.
*/
repair_subwindows(win);
1997-05-15 12:00:00 +08:00
#ifdef TRACE
2006-12-18 12:32:42 +08:00
TR(TRACE_UPDATE, ("...beg (%ld, %ld), max(%ld,%ld), reg(%ld,%ld)",
(long) win->_begy, (long) win->_begx,
(long) win->_maxy, (long) win->_maxx,
(long) win->_regtop, (long) win->_regbottom));
if (USE_TRACEF(TRACE_UPDATE)) {
2000-07-09 10:46:08 +08:00
_tracedump("...after:", win);
_nc_unlock_global(tracef);
}
1997-05-15 12:00:00 +08:00
#endif
2000-07-09 10:46:08 +08:00
returnCode(OK);
1997-05-15 12:00:00 +08:00
}