]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - ncurses/base/wresize.c
ncurses 5.1
[ncurses.git] / ncurses / base / wresize.c
index 1b91476cbfd3dbced32d97e6943597da5f50752c..8121ff154dd58085c1d567821d9d12e104043700 100644 (file)
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 1998 Free Software Foundation, Inc.                        *
+ * Copyright (c) 1998,1999,2000 Free Software Foundation, Inc.              *
  *                                                                          *
  * Permission is hereby granted, free of charge, to any person obtaining a  *
  * copy of this software and associated documentation files (the            *
@@ -32,7 +32,7 @@
 
 #include <curses.priv.h>
 
-MODULE_ID("$Id: wresize.c,v 1.12 1999/02/27 18:57:31 tom Exp $")
+MODULE_ID("$Id: wresize.c,v 1.16 2000/03/05 00:14:35 tom Exp $")
 
 /*
  * Reallocate a curses WINDOW struct to either shrink or grow to the specified
@@ -47,120 +47,133 @@ MODULE_ID("$Id: wresize.c,v 1.12 1999/02/27 18:57:31 tom Exp $")
 int
 wresize(WINDOW *win, int ToLines, int ToCols)
 {
-       register int row;
-       int size_x, size_y;
-       struct ldat *pline;
-       chtype blank;
+    register int row;
+    int size_x, size_y;
+    struct ldat *pline;
+    chtype blank;
 
 #ifdef TRACE
-       T((T_CALLED("wresize(%p,%d,%d)"), win, ToLines, ToCols));
-       if (win) {
-         TR(TRACE_UPDATE, ("...beg (%d, %d), max(%d,%d), reg(%d,%d)",
-                           win->_begy, win->_begx,
-                           win->_maxy, win->_maxx,
-                           win->_regtop, win->_regbottom));
-         if (_nc_tracing & TRACE_UPDATE)
+    T((T_CALLED("wresize(%p,%d,%d)"), win, ToLines, ToCols));
+    if (win) {
+       TR(TRACE_UPDATE, ("...beg (%d, %d), max(%d,%d), reg(%d,%d)",
+               win->_begy, win->_begx,
+               win->_maxy, win->_maxx,
+               win->_regtop, win->_regbottom));
+       if (_nc_tracing & TRACE_UPDATE)
            _tracedump("...before", win);
-       }
+    }
 #endif
 
-       if (!win || --ToLines < 0 || --ToCols < 0)
-               returnCode(ERR);
+    if (!win || --ToLines < 0 || --ToCols < 0)
+       returnCode(ERR);
 
-       size_x = win->_maxx;
-       size_y = win->_maxy;
+    size_x = win->_maxx;
+    size_y = win->_maxy;
 
-       if (ToLines == size_y
-        && ToCols  == size_x)
-               returnCode(OK);
-
-       pline  = (win->_flags & _SUBWIN) ? win->_parent->_line : 0;
+    if (ToLines == size_y
+       && ToCols == size_x)
+       returnCode(OK);
 
+    if ((win->_flags & _SUBWIN)) {
        /*
-        * If the number of lines has changed, adjust the size of the overall
-        * vector:
+        * 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.
         */
-       if (ToLines != size_y) {
-               if (! (win->_flags & _SUBWIN)) {
-                       for (row = ToLines+1; row <= size_y; row++)
-                               free((char *)(win->_line[row].text));
-               }
-
-               win->_line = ld_ALLOC(win->_line, ToLines+1);
-               if (win->_line == 0)
-                       returnCode(ERR);
-
-               for (row = size_y+1; row <= ToLines; row++) {
-                       win->_line[row].text      = 0;
-                       win->_line[row].firstchar = 0;
-                       win->_line[row].lastchar  = ToCols;
-                       if ((win->_flags & _SUBWIN)) {
-                               win->_line[row].text =
-                               &pline[win->_begy + row].text[win->_begx];
-                       }
-               }
+       if (win->_pary + ToLines > win->_parent->_maxy
+           || win->_parx + ToCols > win->_parent->_maxx) {
+           returnCode(ERR);
        }
-
-       /*
-        * Adjust the width of the columns:
-        */
-       blank = _nc_background(win);
-       for (row = 0; row <= ToLines; row++) {
-               chtype  *s      = win->_line[row].text;
-               int     begin   = (s == 0) ? 0 : size_x + 1;
-               int     end     = ToCols;
-
-               if_USE_SCROLL_HINTS(win->_line[row].oldindex = row);
-
-               if (ToCols != size_x || s == 0) {
-                       if (! (win->_flags & _SUBWIN)) {
-                               win->_line[row].text = s = c_ALLOC(s, ToCols+1);
-                               if (win->_line[row].text == 0)
-                                       returnCode(ERR);
-                       } else if (s == 0) {
-                               win->_line[row].text = s =
-                               &pline[win->_begy + row].text[win->_begx];
-                       }
-
-                       if (end >= begin) {     /* growing */
-                               if (win->_line[row].firstchar < begin)
-                                       win->_line[row].firstchar = begin;
-                               win->_line[row].lastchar = ToCols;
-                               do {
-                                       s[end] = blank;
-                               } while (--end >= begin);
-                       } else {                /* shrinking */
-                               win->_line[row].firstchar = 0;
-                               win->_line[row].lastchar  = ToCols;
-                       }
-               }
+       pline = win->_parent->_line;
+    } else {
+       pline = 0;
+    }
+
+    /*
+     * If the number of lines has changed, adjust the size of the overall
+     * vector:
+     */
+    if (ToLines != size_y) {
+       if (!(win->_flags & _SUBWIN)) {
+           for (row = ToLines + 1; row <= size_y; row++)
+               free((char *) (win->_line[row].text));
        }
 
-       /*
-        * 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;
+       win->_line = ld_ALLOC(win->_line, ToLines + 1);
+       if (win->_line == 0)
+           returnCode(ERR);
+
+       for (row = size_y + 1; row <= ToLines; row++) {
+           win->_line[row].text = 0;
+           win->_line[row].firstchar = 0;
+           win->_line[row].lastchar = ToCols;
+           if ((win->_flags & _SUBWIN)) {
+               win->_line[row].text =
+                   &pline[win->_pary + row].text[win->_parx];
+           }
+       }
+    }
+
+    /*
+     * Adjust the width of the columns:
+     */
+    blank = _nc_background(win);
+    for (row = 0; row <= ToLines; row++) {
+       chtype *s = win->_line[row].text;
+       int begin = (s == 0) ? 0 : size_x + 1;
+       int end = ToCols;
+
+       if_USE_SCROLL_HINTS(win->_line[row].oldindex = row);
+
+       if (ToCols != size_x || s == 0) {
+           if (!(win->_flags & _SUBWIN)) {
+               win->_line[row].text = s = c_ALLOC(s, ToCols + 1);
+               if (win->_line[row].text == 0)
+                   returnCode(ERR);
+           } else if (s == 0) {
+               win->_line[row].text = s =
+                   &pline[win->_pary + row].text[win->_parx];
+           }
+
+           if (end >= begin) { /* growing */
+               if (win->_line[row].firstchar < begin)
+                   win->_line[row].firstchar = begin;
+               win->_line[row].lastchar = ToCols;
+               do {
+                   s[end] = blank;
+               } while (--end >= begin);
+           } else {            /* shrinking */
+               win->_line[row].firstchar = 0;
+               win->_line[row].lastchar = ToCols;
+           }
+       }
+    }
+
+    /*
+     * 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;
 
 #ifdef TRACE
-       TR(TRACE_UPDATE, ("...beg (%d, %d), max(%d,%d), reg(%d,%d)",
-               win->_begy, win->_begx,
-               win->_maxy, win->_maxx,
-               win->_regtop, win->_regbottom));
-       if (_nc_tracing & TRACE_UPDATE)
-               _tracedump("...after:", win);
+    TR(TRACE_UPDATE, ("...beg (%d, %d), max(%d,%d), reg(%d,%d)",
+           win->_begy, win->_begx,
+           win->_maxy, win->_maxx,
+           win->_regtop, win->_regbottom));
+    if (_nc_tracing & TRACE_UPDATE)
+       _tracedump("...after:", win);
 #endif
-       returnCode(OK);
+    returnCode(OK);
 }