home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / lib / libcurses / newwin.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-23  |  6.1 KB  |  228 lines

  1. /*
  2.  * Copyright (c) 1981 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. static char sccsid[] = "@(#)newwin.c    5.6 (Berkeley) 8/23/92";
  36. #endif    /* not lint */
  37.  
  38. #include <curses.h>
  39. #include <stdlib.h>
  40.  
  41. #undef    nl        /* Don't need it here, and it interferes. */
  42.  
  43. static WINDOW *makenew __P((int, int, int, int));
  44.  
  45. /*
  46.  * newwin --
  47.  *    Allocate space for and set up defaults for a new window.
  48.  */
  49. WINDOW *
  50. newwin(num_lines, num_cols, begy, begx)
  51.     int num_lines, num_cols, begy, begx;
  52. {
  53.     register WINDOW *win;
  54.     register int by, bx, i, j, nl, nc;
  55.     register char *sp;
  56.  
  57.     by = begy;
  58.     bx = begx;
  59.     nl = num_lines;
  60.     nc = num_cols;
  61.  
  62.     if (nl == 0)
  63.         nl = LINES - by;
  64.     if (nc == 0)
  65.         nc = COLS - bx;
  66.     if ((win = makenew(nl, nc, by, bx)) == NULL)
  67.         return (NULL);
  68.     if ((win->_firstch = malloc(nl * sizeof(win->_firstch[0]))) == NULL) {
  69.         free(win->_y);
  70.         free(win);
  71.         return (NULL);
  72.     }
  73.     if ((win->_lastch = malloc(nl * sizeof(win->_lastch[0]))) == NULL) {
  74.         free(win->_y);
  75.         free(win->_firstch);
  76.         free(win);
  77.         return (NULL);
  78.     }
  79.     win->_nextp = win;
  80.     for (i = 0; i < nl; i++) {
  81.         win->_firstch[i] = _NOCHANGE;
  82.         win->_lastch[i] = _NOCHANGE;
  83.     }
  84.     for (i = 0; i < nl; i++)
  85.         if ((win->_y[i] = malloc(nc * sizeof(win->_y[0]))) == NULL) {
  86.             for (j = 0; j < i; j++)
  87.                 free(win->_y[j]);
  88.             free(win->_firstch);
  89.             free(win->_lastch);
  90.             free(win->_y);
  91.             free(win);
  92.             return (NULL);
  93.         } else
  94.             for (sp = win->_y[i]; sp < win->_y[i] + nc;)
  95.                 *sp++ = ' ';
  96.     win->_ch_off = 0;
  97. #ifdef DEBUG
  98.     __TRACE("newwin: win->_ch_off = %d\n", win->_ch_off);
  99. #endif
  100.     return (win);
  101. }
  102.  
  103. WINDOW *
  104. subwin(orig, num_lines, num_cols, begy, begx)
  105.     register WINDOW *orig;
  106.     int num_lines, num_cols, begy, begx;
  107. {
  108.     register WINDOW *win;
  109.     register int by, bx, nl, nc;
  110.  
  111.     by = begy;
  112.     bx = begx;
  113.     nl = num_lines;
  114.     nc = num_cols;
  115.  
  116.     /* Make sure window fits inside the original one. */
  117. #ifdef    DEBUG
  118.     __TRACE("subwin: (%0.2o, %d, %d, %d, %d)\n", orig, nl, nc, by, bx);
  119. #endif
  120.     if (by < orig->_begy || bx < orig->_begx
  121.         || by + nl > orig->_maxy + orig->_begy
  122.         || bx + nc > orig->_maxx + orig->_begx)
  123.         return (NULL);
  124.     if (nl == 0)
  125.         nl = orig->_maxy + orig->_begy - by;
  126.     if (nc == 0)
  127.         nc = orig->_maxx + orig->_begx - bx;
  128.     if ((win = makenew(nl, nc, by, bx)) == NULL)
  129.         return (NULL);
  130.     win->_nextp = orig->_nextp;
  131.     orig->_nextp = win;
  132.     win->_orig = orig;
  133.     __set_subwin(orig, win);
  134.     return (win);
  135. }
  136.  
  137. /*
  138.  * This code is shared with mvwin().
  139.  */
  140. void
  141. __set_subwin(orig, win)
  142.     register WINDOW *orig, *win;
  143. {
  144.     register int i, j, k;
  145.  
  146.     j = win->_begy - orig->_begy;
  147.     k = win->_begx - orig->_begx;
  148.     win->_ch_off = k;
  149. #ifdef DEBUG
  150.     __TRACE("__set_subwin: win->_ch_off = %d\n", win->_ch_off);
  151. #endif
  152.     win->_firstch = &orig->_firstch[j];
  153.     win->_lastch = &orig->_lastch[j];
  154.     for (i = 0; i < win->_maxy; i++, j++)
  155.         win->_y[i] = &orig->_y[j][k];
  156. }
  157.  
  158. /*
  159.  * makenew --
  160.  *    Set up a window buffer and returns a pointer to it.
  161.  */
  162. static WINDOW *
  163. makenew(num_lines, num_cols, begy, begx)
  164.     int num_lines, num_cols, begy, begx;
  165. {
  166.     register WINDOW *win;
  167.     register int by, bx, nl, nc;
  168.  
  169.     by = begy;
  170.     bx = begx;
  171.     nl = num_lines;
  172.     nc = num_cols;
  173.  
  174. #ifdef    DEBUG
  175.     __TRACE("makenew: (%d, %d, %d, %d)\n", nl, nc, by, bx);
  176. #endif
  177.     if ((win = malloc(sizeof(*win))) == NULL)
  178.         return (NULL);
  179. #ifdef DEBUG
  180.     __TRACE("makenew: nl = %d\n", nl);
  181. #endif
  182.     if ((win->_y = malloc(nl * sizeof(win->_y[0]))) == NULL) {
  183.         free(win);
  184.         return (NULL);
  185.     }
  186. #ifdef DEBUG
  187.     __TRACE("makenew: nc = %d\n", nc);
  188. #endif
  189.     win->_cury = win->_curx = 0;
  190.     win->_clear = 0;
  191.     win->_maxy = nl;
  192.     win->_maxx = nc;
  193.     win->_begy = by;
  194.     win->_begx = bx;
  195.     win->_flags = 0;
  196.     win->_scroll = win->_leave = 0;
  197.     __swflags(win);
  198. #ifdef DEBUG
  199.     __TRACE("makenew: win->_clear = %d\n", win->_clear);
  200.     __TRACE("makenew: win->_leave = %d\n", win->_leave);
  201.     __TRACE("makenew: win->_scroll = %d\n", win->_scroll);
  202.     __TRACE("makenew: win->_flags = %0.2o\n", win->_flags);
  203.     __TRACE("makenew: win->_maxy = %d\n", win->_maxy);
  204.     __TRACE("makenew: win->_maxx = %d\n", win->_maxx);
  205.     __TRACE("makenew: win->_begy = %d\n", win->_begy);
  206.     __TRACE("makenew: win->_begx = %d\n", win->_begx);
  207. #endif
  208.     return (win);
  209. }
  210.  
  211. void
  212. __swflags(win)
  213.     register WINDOW *win;
  214. {
  215.     win->_flags &= ~(_ENDLINE | _FULLLINE | _FULLWIN | _SCROLLWIN);
  216.     if (win->_begx + win->_maxx == COLS) {
  217.         win->_flags |= _ENDLINE;
  218.         if (win->_begx == 0) {
  219.             if (AL && DL)
  220.                 win->_flags |= _FULLLINE;
  221.             if (win->_maxy == LINES && win->_begy == 0)
  222.                 win->_flags |= _FULLWIN;
  223.         }
  224.         if (win->_begy + win->_maxy == LINES)
  225.             win->_flags |= _SCROLLWIN;
  226.     }
  227. }
  228.