home *** CD-ROM | disk | FTP | other *** search
/ Serving the Web / ServingTheWeb1995.disc1of1.iso / linux / slacksrce / d / libc / libc-4.6 / libc-4 / libc-linux / curses-old / newwin.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-28  |  6.3 KB  |  241 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.4 (Berkeley) 6/1/90";
  36. #endif /* not lint */
  37.  
  38. /*
  39.  * allocate space for and set up defaults for a new window
  40.  *
  41.  */
  42.  
  43. # include    "curses.ext"
  44.  
  45. #ifdef linux
  46. #include <stdlib.h>
  47. static WINDOW    *makenew(int, int, int, int);
  48. #else
  49. char    *malloc();
  50. static WINDOW    *makenew();
  51. #endif
  52.  
  53. # define    SMALLOC    (short *) malloc
  54.  
  55.  
  56. # undef        nl    /* don't need it here, and it interferes    */
  57.  
  58. WINDOW *
  59. newwin(num_lines, num_cols, begy, begx)
  60. int    num_lines, num_cols, begy, begx;
  61. {
  62.     register WINDOW    *win;
  63.     register char    *sp;
  64.     register int        i, by, bx, nl, nc;
  65.     register int        j;
  66.  
  67.     by = begy;
  68.     bx = begx;
  69.     nl = num_lines;
  70.     nc = num_cols;
  71.  
  72.     if (nl == 0)
  73.         nl = LINES - by;
  74.     if (nc == 0)
  75.         nc = COLS - bx;
  76.     if ((win = makenew(nl, nc, by, bx)) == NULL)
  77.         return ERR;
  78.     if ((win->_firstch = SMALLOC(nl * sizeof win->_firstch[0])) == NULL) {
  79.         free(win->_y);
  80.         free(win);
  81.         return NULL;
  82.     }
  83.     if ((win->_lastch = SMALLOC(nl * sizeof win->_lastch[0])) == NULL) {
  84.         free(win->_y);
  85.         free(win->_firstch);
  86.         free(win);
  87.         return NULL;
  88.     }
  89.     win->_nextp = win;
  90.     for (i = 0; i < nl; i++) {
  91.         win->_firstch[i] = _NOCHANGE;
  92.         win->_lastch[i] = _NOCHANGE;
  93.     }
  94.     for (i = 0; i < nl; i++)
  95.         if ((win->_y[i] = malloc(nc * sizeof win->_y[0])) == NULL) {
  96.             for (j = 0; j < i; j++)
  97.                 free(win->_y[j]);
  98.             free(win->_firstch);
  99.             free(win->_lastch);
  100.             free(win->_y);
  101.             free(win);
  102.             return ERR;
  103.         }
  104.         else
  105.             for (sp = win->_y[i]; sp < win->_y[i] + nc; )
  106.                 *sp++ = ' ';
  107.     win->_ch_off = 0;
  108. # ifdef DEBUG
  109.     fprintf(outf, "NEWWIN: win->_ch_off = %d\n", win->_ch_off);
  110. # endif
  111.     return win;
  112. }
  113.  
  114. WINDOW *
  115. subwin(orig, num_lines, num_cols, begy, begx)
  116. register WINDOW    *orig;
  117. int        num_lines, num_cols, begy, begx;
  118. {
  119.     register int        i;
  120.     register WINDOW    *win;
  121.     register int        by, bx, nl, nc;
  122.  
  123.     by = begy;
  124.     bx = begx;
  125.     nl = num_lines;
  126.     nc = num_cols;
  127.  
  128.     /*
  129.      * make sure window fits inside the original one
  130.      */
  131. # ifdef    DEBUG
  132.     fprintf(outf, "SUBWIN(%0.2o, %d, %d, %d, %d)\n", orig, nl, nc, by, bx);
  133. # endif
  134.     if (by < orig->_begy || bx < orig->_begx
  135.         || by + nl > orig->_maxy + orig->_begy
  136.         || bx + nc > orig->_maxx + orig->_begx)
  137.         return ERR;
  138.     if (nl == 0)
  139.         nl = orig->_maxy + orig->_begy - by;
  140.     if (nc == 0)
  141.         nc = orig->_maxx + orig->_begx - bx;
  142.     if ((win = makenew(nl, nc, by, bx)) == NULL)
  143.         return ERR;
  144.     win->_nextp = orig->_nextp;
  145.     orig->_nextp = win;
  146.     win->_orig = orig;
  147.     _set_subwin_(orig, win);
  148.     return win;
  149. }
  150.  
  151. /*
  152.  * this code is shared with mvwin()
  153.  */
  154. _set_subwin_(orig, win)
  155. register WINDOW    *orig, *win;
  156. {
  157.     register int    i, j, k;
  158.  
  159.     j = win->_begy - orig->_begy;
  160.     k = win->_begx - orig->_begx;
  161.     win->_ch_off = k;
  162. # ifdef DEBUG
  163.     fprintf(outf, "_SET_SUBWIN_: win->_ch_off = %d\n", win->_ch_off);
  164. # endif
  165.     win->_firstch = &orig->_firstch[j];
  166.     win->_lastch = &orig->_lastch[j];
  167.     for (i = 0; i < win->_maxy; i++, j++)
  168.         win->_y[i] = &orig->_y[j][k];
  169.  
  170. }
  171.  
  172. /*
  173.  *    This routine sets up a window buffer and returns a pointer to it.
  174.  */
  175. static WINDOW *
  176. makenew(num_lines, num_cols, begy, begx)
  177. int    num_lines, num_cols, begy, begx; {
  178.  
  179.     register int        i;
  180.     register WINDOW    *win;
  181.     register int        by, bx, nl, nc;
  182.  
  183.     by = begy;
  184.     bx = begx;
  185.     nl = num_lines;
  186.     nc = num_cols;
  187.  
  188. # ifdef    DEBUG
  189.     fprintf(outf, "MAKENEW(%d, %d, %d, %d)\n", nl, nc, by, bx);
  190. # endif
  191.     if ((win = (WINDOW *) malloc(sizeof *win)) == NULL)
  192.         return NULL;
  193. # ifdef DEBUG
  194.     fprintf(outf, "MAKENEW: nl = %d\n", nl);
  195. # endif
  196.     if ((win->_y = (char **) malloc(nl * sizeof win->_y[0])) == NULL) {
  197.         free(win);
  198.         return NULL;
  199.     }
  200. # ifdef DEBUG
  201.     fprintf(outf, "MAKENEW: nc = %d\n", nc);
  202. # endif
  203.     win->_cury = win->_curx = 0;
  204.     win->_clear = FALSE;
  205.     win->_maxy = nl;
  206.     win->_maxx = nc;
  207.     win->_begy = by;
  208.     win->_begx = bx;
  209.     win->_flags = 0;
  210.     win->_scroll = win->_leave = FALSE;
  211.     _swflags_(win);
  212. # ifdef DEBUG
  213.     fprintf(outf, "MAKENEW: win->_clear = %d\n", win->_clear);
  214.     fprintf(outf, "MAKENEW: win->_leave = %d\n", win->_leave);
  215.     fprintf(outf, "MAKENEW: win->_scroll = %d\n", win->_scroll);
  216.     fprintf(outf, "MAKENEW: win->_flags = %0.2o\n", win->_flags);
  217.     fprintf(outf, "MAKENEW: win->_maxy = %d\n", win->_maxy);
  218.     fprintf(outf, "MAKENEW: win->_maxx = %d\n", win->_maxx);
  219.     fprintf(outf, "MAKENEW: win->_begy = %d\n", win->_begy);
  220.     fprintf(outf, "MAKENEW: win->_begx = %d\n", win->_begx);
  221. # endif
  222.     return win;
  223. }
  224.  
  225. _swflags_(win)
  226. register WINDOW    *win;
  227. {
  228.     win->_flags &= ~(_ENDLINE|_FULLLINE|_FULLWIN|_SCROLLWIN);
  229.     if (win->_begx + win->_maxx == COLS) {
  230.         win->_flags |= _ENDLINE;
  231.         if (win->_begx == 0) {
  232.             if (AL && DL)
  233.                 win->_flags |= _FULLLINE;
  234.             if (win->_maxy == LINES && win->_begy == 0)
  235.                 win->_flags |= _FULLWIN;
  236.         }
  237.         if (win->_begy + win->_maxy == LINES)
  238.             win->_flags |= _SCROLLWIN;
  239.     }
  240. }
  241.