home *** CD-ROM | disk | FTP | other *** search
/ Computer Club Elmshorn Atari PD / CCE_PD.iso / pc / 0600 / CCE_0636.ZIP / CCE_0636 / CURSES / CRSSRC12.ZOO / src / newwin.c < prev    next >
C/C++ Source or Header  |  1991-09-27  |  5KB  |  223 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 are permitted
  6.  * provided that the above copyright notice and this paragraph are
  7.  * duplicated in all such forms and that any documentation,
  8.  * advertising materials, and other materials related to such
  9.  * distribution and use acknowledge that the software was developed
  10.  * by the University of California, Berkeley.  The name of the
  11.  * University may not be used to endorse or promote products derived
  12.  * from this software without specific prior written permission.
  13.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  14.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  15.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  16.  */
  17.  
  18. #ifndef lint
  19. static char sccsid[] = "@(#)newwin.c    5.3 (Berkeley) 6/30/88";
  20. #endif /* not lint */
  21.  
  22. /*
  23.  * allocate space for and set up defaults for a new window
  24.  *
  25.  */
  26.  
  27. # include    "curses.ext"
  28.  
  29. #ifdef __STDC__
  30. #include <stdlib.h>
  31. #include <memory.h>
  32. #else
  33. char    *malloc();
  34. #endif
  35.  
  36. # define    SMALLOC    (short *) malloc
  37.  
  38. static WINDOW    *makenew __PROTO((int num_lines, int num_cols, int begy, int begx));
  39.  
  40. # undef        nl    /* don't need it here, and it interferes    */
  41.  
  42. WINDOW *
  43. newwin(num_lines, num_cols, begy, begx)
  44. int    num_lines, num_cols, begy, begx;
  45. {
  46.     reg WINDOW    *win;
  47.     reg char    *sp;
  48.     reg int        i, by, bx, nl, nc;
  49.     reg int        j;
  50.  
  51.     by = begy;
  52.     bx = begx;
  53.     nl = num_lines;
  54.     nc = num_cols;
  55.  
  56.     if (nl == 0)
  57.         nl = LINES - by;
  58.     if (nc == 0)
  59.         nc = COLS - bx;
  60.     if ((win = makenew(nl, nc, by, bx)) == NULL)
  61.         return ERR;
  62.     if ((win->_firstch = SMALLOC(nl * sizeof win->_firstch[0])) == NULL) {
  63.         free(win->_y);
  64.         free(win);
  65.         return NULL;
  66.     }
  67.     if ((win->_lastch = SMALLOC(nl * sizeof win->_lastch[0])) == NULL) {
  68.         free(win->_y);
  69.         free(win->_firstch);
  70.         free(win);
  71.         return NULL;
  72.     }
  73.     win->_nextp = win;
  74.     for (i = 0; i < nl; i++) {
  75.         win->_firstch[i] = _NOCHANGE;
  76.         win->_lastch[i] = _NOCHANGE;
  77.     }
  78.     for (i = 0; i < nl; i++)
  79.         if ((win->_y[i] = malloc(nc * sizeof win->_y[0])) == NULL) {
  80.             for (j = 0; j < i; j++)
  81.                 free(win->_y[j]);
  82.             free(win->_firstch);
  83.             free(win->_lastch);
  84.             free(win->_y);
  85.             free(win);
  86.             return ERR;
  87.         }
  88.         else
  89.             for (sp = win->_y[i]; sp < win->_y[i] + nc; )
  90.                 *sp++ = ' ';
  91.     win->_ch_off = 0;
  92. # ifdef DEBUG
  93.     fprintf(outf, "NEWWIN: win->_ch_off = %d\n", win->_ch_off);
  94. # endif
  95.     return win;
  96. }
  97.  
  98. WINDOW *
  99. subwin(orig, num_lines, num_cols, begy, begx)
  100. reg WINDOW    *orig;
  101. int        num_lines, num_cols, begy, begx;
  102. {
  103.     reg WINDOW    *win;
  104.     reg int        by, bx, nl, nc;
  105.  
  106.     by = begy;
  107.     bx = begx;
  108.     nl = num_lines;
  109.     nc = num_cols;
  110.  
  111.     /*
  112.      * make sure window fits inside the original one
  113.      */
  114. # ifdef    DEBUG
  115.     fprintf(outf, "SUBWIN(%0.2o, %d, %d, %d, %d)\n", orig, nl, nc, by, bx);
  116. # endif
  117.     if (by < orig->_begy || bx < orig->_begx
  118.         || by + nl > orig->_maxy + orig->_begy
  119.         || bx + nc > orig->_maxx + orig->_begx)
  120.         return ERR;
  121.     if (nl == 0)
  122.         nl = orig->_maxy + orig->_begy - by;
  123.     if (nc == 0)
  124.         nc = orig->_maxx + orig->_begx - bx;
  125.     if ((win = makenew(nl, nc, by, bx)) == NULL)
  126.         return ERR;
  127.     win->_nextp = orig->_nextp;
  128.     orig->_nextp = win;
  129.     win->_orig = orig;
  130.     _set_subwin_(orig, win);
  131.     return win;
  132. }
  133.  
  134. /*
  135.  * this code is shared with mvwin()
  136.  */
  137. void _set_subwin_(orig, win)
  138. register WINDOW    *orig, *win;
  139. {
  140.     register int    i, j, k;
  141.  
  142.     j = win->_begy - orig->_begy;
  143.     k = win->_begx - orig->_begx;
  144.     win->_ch_off = k;
  145. # ifdef DEBUG
  146.     fprintf(outf, "_SET_SUBWIN_: win->_ch_off = %d\n", win->_ch_off);
  147. # endif
  148.     win->_firstch = &orig->_firstch[j];
  149.     win->_lastch = &orig->_lastch[j];
  150.     for (i = 0; i < win->_maxy; i++, j++)
  151.         win->_y[i] = &orig->_y[j][k];
  152.  
  153. }
  154.  
  155. /*
  156.  *    This routine sets up a window buffer and returns a pointer to it.
  157.  */
  158. static WINDOW *
  159. makenew(num_lines, num_cols, begy, begx)
  160. int    num_lines, num_cols, begy, begx; {
  161.  
  162.     reg WINDOW    *win;
  163.     reg int        by, bx, nl, nc;
  164.  
  165.     by = begy;
  166.     bx = begx;
  167.     nl = num_lines;
  168.     nc = num_cols;
  169.  
  170. # ifdef    DEBUG
  171.     fprintf(outf, "MAKENEW(%d, %d, %d, %d)\n", nl, nc, by, bx);
  172. # endif
  173.     if ((win = (WINDOW *) malloc(sizeof *win)) == NULL)
  174.         return NULL;
  175. # ifdef DEBUG
  176.     fprintf(outf, "MAKENEW: nl = %d\n", nl);
  177. # endif
  178.     if ((win->_y = (char **) malloc(nl * sizeof win->_y[0])) == NULL) {
  179.         free(win);
  180.         return NULL;
  181.     }
  182. # ifdef DEBUG
  183.     fprintf(outf, "MAKENEW: nc = %d\n", nc);
  184. # endif
  185.     win->_cury = win->_curx = 0;
  186.     win->_clear = FALSE;
  187.     win->_maxy = nl;
  188.     win->_maxx = nc;
  189.     win->_begy = by;
  190.     win->_begx = bx;
  191.     win->_flags = 0;
  192.     win->_scroll = win->_leave = FALSE;
  193.     _swflags_(win);
  194. # ifdef DEBUG
  195.     fprintf(outf, "MAKENEW: win->_clear = %d\n", win->_clear);
  196.     fprintf(outf, "MAKENEW: win->_leave = %d\n", win->_leave);
  197.     fprintf(outf, "MAKENEW: win->_scroll = %d\n", win->_scroll);
  198.     fprintf(outf, "MAKENEW: win->_flags = %0.2o\n", win->_flags);
  199.     fprintf(outf, "MAKENEW: win->_maxy = %d\n", win->_maxy);
  200.     fprintf(outf, "MAKENEW: win->_maxx = %d\n", win->_maxx);
  201.     fprintf(outf, "MAKENEW: win->_begy = %d\n", win->_begy);
  202.     fprintf(outf, "MAKENEW: win->_begx = %d\n", win->_begx);
  203. # endif
  204.     return win;
  205. }
  206.  
  207. void _swflags_(win)
  208. register WINDOW    *win;
  209. {
  210.     win->_flags &= ~(_ENDLINE|_FULLLINE|_FULLWIN|_SCROLLWIN);
  211.     if (win->_begx + win->_maxx == COLS) {
  212.         win->_flags |= _ENDLINE;
  213.         if (win->_begx == 0) {
  214.             if (AL && DL)
  215.                 win->_flags |= _FULLLINE;
  216.             if (win->_maxy == LINES && win->_begy == 0)
  217.                 win->_flags |= _FULLWIN;
  218.         }
  219.         if (win->_begy + win->_maxy == LINES)
  220.             win->_flags |= _SCROLLWIN;
  221.     }
  222. }
  223.