home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 332_01 / newwin.c < prev    next >
C/C++ Source or Header  |  1990-01-06  |  6KB  |  205 lines

  1. /****************************************************************/
  2. /* Newwin(), subwin() routines of the PCcurses package        */
  3. /*                                */
  4. /****************************************************************/
  5. /* This version of curses is based on ncurses, a curses version    */
  6. /* originally written by Pavel Curtis at Cornell University.    */
  7. /* I have made substantial changes to make it run on IBM PC's,    */
  8. /* and therefore consider myself free to make it public domain.    */
  9. /*                Bjorn Larsson (bl@infovox.se)    */
  10. /****************************************************************/
  11. /* 1.4:  References to win->borderchar[] removed due to        */
  12. /*     re-defined border() functions. Use of short        */
  13. /*     wherever possible. Bug in subwin() did not        */
  14. /*     allow subwin to be the same size as the origi-        */
  15. /*     nal window. Portability improvements:        900114    */
  16. /* 1.3:  MSC '-W3', Turbo'C' '-w -w-pro' checks.        */
  17. /*     Support for border(), wborder() functions:    881005    */
  18. /* 1.2:     Other max limits off by 1. Fixed thanks to        */
  19. /*     S. Creps:                    881002    */
  20. /* 1.1:     Fix in subwin: '+/-1' error when checking that        */
  21. /*     subwindow fits in parent window:        880305    */
  22. /* 1.0:     Release:                    870515    */
  23. /****************************************************************/
  24.  
  25. #include <stdio.h>
  26. #include <curses.h>
  27. #include <curspriv.h>
  28.  
  29. char _curses_newwin_rcsid[] = "@(#)newwin.c     v.1.4  - 900114";
  30.  
  31. extern    char    *malloc();
  32. extern    char    *calloc();
  33. extern    void     free();
  34.  
  35. /****************************************************************/
  36. /* Makenew() allocates all data for a new window except the    */
  37. /* actual lines themselves.                    */
  38. /****************************************************************/
  39.  
  40. static WINDOW *makenew(num_lines, num_columns, begy, begx)
  41.   int    num_lines;
  42.   int    num_columns;
  43.   int    begy;
  44.   int    begx;
  45.   {
  46.   short         i;
  47.   WINDOW    *win;
  48.  
  49.   /* allocate the window structure itself */
  50.  
  51.   if ((win = (WINDOW *) malloc(sizeof(WINDOW))) == NULL)
  52.     return ((WINDOW *) ERR);
  53.  
  54.   /* allocate the line pointer array */
  55.  
  56.   if ((win->_line = (short **) calloc(num_lines, sizeof (short *))) == NULL)
  57.     {
  58.     free(win);
  59.     return((WINDOW *) ERR);
  60.     }
  61.  
  62.   /* allocate the minchng and maxchng arrays */
  63.  
  64.   if ((win->_minchng = (short *) calloc(num_lines, sizeof(short))) == NULL)
  65.     {
  66.     free(win);
  67.     free(win->_line);
  68.     return((WINDOW *) ERR);
  69.     }
  70.   if ((win->_maxchng = (short *) calloc(num_lines, sizeof(short))) == NULL)
  71.     {
  72.     free(win);
  73.     free(win->_line);
  74.     free(win->_minchng);
  75.     return((WINDOW *) ERR);
  76.     }
  77.  
  78.   /* initialize window variables */
  79.  
  80.   win->_curx      = 0;
  81.   win->_cury      = 0;
  82.   win->_maxy      = num_lines;
  83.   win->_maxx      = num_columns;
  84.   win->_begy      = begy;
  85.   win->_begx      = begx;
  86.   win->_flags     = 0;
  87.   win->_attrs     = ATR_NRM;
  88.   win->_tabsize   = 8;
  89.   win->_clear     = (bool) ((num_lines == LINES) && (num_columns == COLS));
  90.   win->_leave     = FALSE;
  91.   win->_scroll    = FALSE;
  92.   win->_nodelay   = FALSE;
  93.   win->_keypad    = FALSE;
  94.   win->_regtop    = 0;
  95.   win->_regbottom = num_lines - 1;
  96.  
  97.   /* init to say window unchanged */
  98.  
  99.   for (i = 0; i < num_lines; i++)
  100.     {
  101.     win->_minchng[i] = 0;
  102.     win->_maxchng[i] = num_columns-1;
  103.     }
  104.  
  105.   /* set flags for window properties */
  106.  
  107.   if ((begy + num_lines) == LINES)
  108.     {
  109.     win->_flags |= _ENDLINE;
  110.     if ((begx == 0) && (num_columns == COLS) && (begy == 0))
  111.       win->_flags |= _FULLWIN;
  112.     } /* if */
  113.  
  114.   if (((begy + num_lines) == LINES)
  115.         &&
  116.       ((begx + num_columns) == COLS))
  117.     win->_flags |= _SCROLLWIN;
  118.   return(win);
  119.   } /* makenew */
  120.  
  121. /****************************************************************/
  122. /* Newwin() creates a new window with size num_lines * num_co-    */
  123. /* lumns, and origin begx,begy relative to the SCREEN. Special    */
  124. /* case: if num_lines and/or num_columns is 0, the remainder of    */
  125. /* the screen is used.                        */
  126. /****************************************************************/
  127.  
  128. WINDOW *newwin(num_lines, num_columns, begy, begx)
  129.   int    num_lines;
  130.   int    num_columns;
  131.   int    begy;
  132.   int    begx;
  133.   {
  134.   WINDOW    *win;
  135.   short        *ptr;
  136.   short         i, j;
  137.  
  138.   if (num_lines == 0)
  139.     num_lines = LINES - begy;
  140.   if (num_columns == 0)
  141.     num_columns = COLS - begx;
  142.   if ((win = makenew(num_lines, num_columns, begy, begx)) == (WINDOW *) ERR)
  143.     return((WINDOW *) ERR);
  144.   for (i = 0; i < num_lines; i++)    /* make and clear the lines */
  145.     {
  146.     if((win->_line[i] = (short *) calloc(num_columns,sizeof(short))) == NULL)
  147.       {
  148.       for (j = 0; j < i; j++)        /* if error, free all the data */
  149.     free(win->_line[j]);
  150.       free(win->_minchng);
  151.       free(win->_maxchng);
  152.       free(win->_line);
  153.       free(win);
  154.       return((WINDOW *) ERR);
  155.       } /* if */
  156.     else
  157.       for (ptr = win->_line[i]; ptr < win->_line[i] + num_columns;)
  158.     *ptr++ = ' ' | ATR_NRM;
  159.     } /* for */
  160.   return(win);
  161.   } /* newwin */
  162.  
  163. /****************************************************************/
  164. /* Subwin() creates a sub-window in the 'orig' window, with    */
  165. /* size num_lines * num_columns, and with origin begx, begy    */
  166. /* relative to the SCREEN. Special case: if num_lines and/or    */
  167. /* num_columns is 0, the remainder of the original window is    */
  168. /* used. The subwindow uses the original window's line buffers    */
  169. /* to store it's own lines.                    */
  170. /****************************************************************/
  171.  
  172. WINDOW *subwin(orig, num_lines, num_columns, begy, begx)
  173.   WINDOW    *orig;
  174.   int         num_lines, num_columns, begy, begx;
  175.   {
  176.   WINDOW    *win;
  177.   short         i, j, k;
  178.  
  179.   /* make sure window fits inside the original one */
  180.  
  181.   if (
  182.       begy < orig->_begy || 
  183.       begx < orig->_begx ||
  184.       (begy + num_lines) > (orig->_begy + orig->_maxy) ||
  185.       (begx + num_columns) > (orig->_begx + orig->_maxx)
  186.      )
  187.     return((WINDOW *) ERR);
  188.  
  189.   if (num_lines == 0)
  190.     num_lines = orig->_maxy - (begy - orig->_begy);
  191.   if (num_columns == 0)
  192.     num_columns = orig->_maxx - (begx - orig->_begx);
  193.   if ((win = makenew(num_lines, num_columns, begy, begx)) == (WINDOW *) ERR)
  194.     return((WINDOW *) ERR);
  195.  
  196.   /* set line pointers the same as in the original window */
  197.  
  198.   j = begy - orig->_begy;
  199.   k = begx - orig->_begx;
  200.   for (i = 0; i < num_lines; i++)
  201.     win->_line[i] = (orig->_line[j++]) + k;
  202.   win->_flags |= _SUBWIN;
  203.   return(win);
  204.   } /* subwin */
  205.