Back to index

tetex-bin  3.0
wresize.c
Go to the documentation of this file.
00001 /****************************************************************************
00002  * Copyright (c) 1998-2001,2002 Free Software Foundation, Inc.              *
00003  *                                                                          *
00004  * Permission is hereby granted, free of charge, to any person obtaining a  *
00005  * copy of this software and associated documentation files (the            *
00006  * "Software"), to deal in the Software without restriction, including      *
00007  * without limitation the rights to use, copy, modify, merge, publish,      *
00008  * distribute, distribute with modifications, sublicense, and/or sell       *
00009  * copies of the Software, and to permit persons to whom the Software is    *
00010  * furnished to do so, subject to the following conditions:                 *
00011  *                                                                          *
00012  * The above copyright notice and this permission notice shall be included  *
00013  * in all copies or substantial portions of the Software.                   *
00014  *                                                                          *
00015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
00016  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
00017  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
00018  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
00019  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
00020  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
00021  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
00022  *                                                                          *
00023  * Except as contained in this notice, the name(s) of the above copyright   *
00024  * holders shall not be used in advertising or otherwise to promote the     *
00025  * sale, use or other dealings in this Software without prior written       *
00026  * authorization.                                                           *
00027  ****************************************************************************/
00028 
00029 /****************************************************************************
00030  *  Author: Thomas E. Dickey 1996-2002                                      *
00031  ****************************************************************************/
00032 
00033 #include <curses.priv.h>
00034 
00035 MODULE_ID("$Id: wresize.c,v 1.23 2002/09/28 15:15:51 tom Exp $")
00036 
00037 static int
00038 cleanup_lines(struct ldat *data, int length)
00039 {
00040     while (--length >= 0)
00041        free(data->text);
00042     free(data);
00043     return ERR;
00044 }
00045 
00046 /*
00047  * If we have reallocated the ldat structs, we will have to repair pointers
00048  * used in subwindows.
00049  */
00050 static void
00051 repair_subwindows(WINDOW *cmp)
00052 {
00053     WINDOWLIST *wp;
00054     struct ldat *pline = cmp->_line;
00055     int row;
00056 
00057     for (wp = _nc_windows; wp != 0; wp = wp->next) {
00058        WINDOW *tst = &(wp->win);
00059 
00060        if (tst->_parent == cmp) {
00061 
00062            if (tst->_pary > cmp->_maxy)
00063               tst->_pary = cmp->_maxy;
00064            if (tst->_parx > cmp->_maxx)
00065               tst->_parx = cmp->_maxx;
00066 
00067            if (tst->_maxy + tst->_pary > cmp->_maxy)
00068               tst->_maxy = cmp->_maxy - tst->_pary;
00069            if (tst->_maxx + tst->_parx > cmp->_maxx)
00070               tst->_maxx = cmp->_maxx - tst->_parx;
00071 
00072            for (row = 0; row <= tst->_maxy; ++row) {
00073               tst->_line[row].text = &pline[tst->_pary + row].text[tst->_parx];
00074            }
00075            repair_subwindows(tst);
00076        }
00077     }
00078 }
00079 
00080 /*
00081  * Reallocate a curses WINDOW struct to either shrink or grow to the specified
00082  * new lines/columns.  If it grows, the new character cells are filled with
00083  * blanks.  The application is responsible for repainting the blank area.
00084  */
00085 NCURSES_EXPORT(int)
00086 wresize(WINDOW *win, int ToLines, int ToCols)
00087 {
00088     int col, row, size_x, size_y;
00089     struct ldat *pline;
00090     struct ldat *new_lines = 0;
00091 
00092 #ifdef TRACE
00093     T((T_CALLED("wresize(%p,%d,%d)"), win, ToLines, ToCols));
00094     if (win) {
00095        TR(TRACE_UPDATE, ("...beg (%d, %d), max(%d,%d), reg(%d,%d)",
00096                        win->_begy, win->_begx,
00097                        win->_maxy, win->_maxx,
00098                        win->_regtop, win->_regbottom));
00099        if (_nc_tracing & TRACE_UPDATE)
00100            _tracedump("...before", win);
00101     }
00102 #endif
00103 
00104     if (!win || --ToLines < 0 || --ToCols < 0)
00105        returnCode(ERR);
00106 
00107     size_x = win->_maxx;
00108     size_y = win->_maxy;
00109 
00110     if (ToLines == size_y
00111        && ToCols == size_x)
00112        returnCode(OK);
00113 
00114     if ((win->_flags & _SUBWIN)) {
00115        /*
00116         * Check if the new limits will fit into the parent window's size.  If
00117         * not, do not resize.  We could adjust the location of the subwindow,
00118         * but the application may not like that.
00119         */
00120        if (win->_pary + ToLines > win->_parent->_maxy
00121            || win->_parx + ToCols > win->_parent->_maxx) {
00122            returnCode(ERR);
00123        }
00124        pline = win->_parent->_line;
00125     } else {
00126        pline = 0;
00127     }
00128 
00129     /*
00130      * Allocate new memory as needed.  Do the allocations without modifying
00131      * the original window, in case an allocation fails.  Always allocate
00132      * (at least temporarily) the array pointing to the individual lines.
00133      */
00134     new_lines = typeCalloc(struct ldat, (unsigned) (ToLines + 1));
00135     if (new_lines == 0)
00136        returnCode(ERR);
00137 
00138     /*
00139      * For each line in the target, allocate or adjust pointers for the
00140      * corresponding text, depending on whether this is a window or a
00141      * subwindow.
00142      */
00143     for (row = 0; row <= ToLines; ++row) {
00144        int begin = (row > size_y) ? 0 : (size_x + 1);
00145        int end = ToCols;
00146        NCURSES_CH_T *s;
00147 
00148        if (!(win->_flags & _SUBWIN)) {
00149            if (row <= size_y) {
00150               if (ToCols != size_x) {
00151                   if ((s = typeMalloc(NCURSES_CH_T, ToCols + 1)) == 0)
00152                      returnCode(cleanup_lines(new_lines, row));
00153                   for (col = 0; col <= ToCols; ++col) {
00154                      s[col] = (col <= size_x
00155                               ? win->_line[row].text[col]
00156                               : win->_nc_bkgd);
00157                   }
00158               } else {
00159                   s = win->_line[row].text;
00160               }
00161            } else {
00162               if ((s = typeMalloc(NCURSES_CH_T, ToCols + 1)) == 0)
00163                   returnCode(cleanup_lines(new_lines, row));
00164               for (col = 0; col <= ToCols; ++col)
00165                   s[col] = win->_nc_bkgd;
00166            }
00167        } else {
00168            s = &pline[win->_pary + row].text[win->_parx];
00169        }
00170 
00171        if_USE_SCROLL_HINTS(new_lines[row].oldindex = row);
00172        if (row <= size_y) {
00173            new_lines[row].firstchar = win->_line[row].firstchar;
00174            new_lines[row].lastchar = win->_line[row].lastchar;
00175        }
00176        if ((ToCols != size_x) || (row > size_y)) {
00177            if (end >= begin) {     /* growing */
00178               if (new_lines[row].firstchar < begin)
00179                   new_lines[row].firstchar = begin;
00180            } else {         /* shrinking */
00181               new_lines[row].firstchar = 0;
00182            }
00183            new_lines[row].lastchar = ToCols;
00184        }
00185        new_lines[row].text = s;
00186     }
00187 
00188     /*
00189      * Dispose of unwanted memory.
00190      */
00191     if (!(win->_flags & _SUBWIN)) {
00192        if (ToCols == size_x) {
00193            for (row = ToLines + 1; row <= size_y; row++) {
00194               free(win->_line[row].text);
00195            }
00196        } else {
00197            for (row = 0; row <= size_y; row++) {
00198               free(win->_line[row].text);
00199            }
00200        }
00201     }
00202 
00203     free(win->_line);
00204     win->_line = new_lines;
00205 
00206     /*
00207      * Finally, adjust the parameters showing screen size and cursor
00208      * position:
00209      */
00210     win->_maxx = ToCols;
00211     win->_maxy = ToLines;
00212 
00213     if (win->_regtop > win->_maxy)
00214        win->_regtop = win->_maxy;
00215     if (win->_regbottom > win->_maxy
00216        || win->_regbottom == size_y)
00217        win->_regbottom = win->_maxy;
00218 
00219     if (win->_curx > win->_maxx)
00220        win->_curx = win->_maxx;
00221     if (win->_cury > win->_maxy)
00222        win->_cury = win->_maxy;
00223 
00224     /*
00225      * Check for subwindows of this one, and readjust pointers to our text,
00226      * if needed.
00227      */
00228     repair_subwindows(win);
00229 
00230 #ifdef TRACE
00231     TR(TRACE_UPDATE, ("...beg (%d, %d), max(%d,%d), reg(%d,%d)",
00232                     win->_begy, win->_begx,
00233                     win->_maxy, win->_maxx,
00234                     win->_regtop, win->_regbottom));
00235     if (_nc_tracing & TRACE_UPDATE)
00236        _tracedump("...after:", win);
00237 #endif
00238     returnCode(OK);
00239 }