home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / cprog / cursesp.zip / NEWWIN.C < prev    next >
C/C++ Source or Header  |  1991-12-03  |  6KB  |  190 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 (...mcvax!enea!infovax!bl)        */
  10. /****************************************************************/
  11. /* 1.0: Release:                                        870515  */
  12. /* 1.1: Fix in subwin: '+/-1' error when checking that          */
  13. /*      subwindow fits in parent window:                880305  */
  14. /* 1.2: Other max limits off by 1. Fixed thanks to              */
  15. /*      S. Creps:                                       881002  */
  16. /* 1.3: MSC '-W3', Turbo'C' '-w -w-pro' checks. Support         */
  17. /*      for border(), wborder() functions:              881005  */
  18. /****************************************************************/
  19.  
  20. #include <stdio.h>
  21. #include <curses.h>
  22. #include <curspriv.h>
  23.  
  24. char _curses_newwin_rcsid[] = "@(#)newwin.c v1.3 - 881005";
  25.  
  26. /****************************************************************/
  27. /* Makenew() allocates all data for a new window except the     */
  28. /* actual lines themselves.                                     */
  29. /****************************************************************/
  30.  
  31. static WINDOW *makenew(int num_lines, int num_columns, int begy, int begx)
  32.   {
  33.   int            i;
  34.   WINDOW        *win;
  35.  
  36.   /* allocate the window structure itself */
  37.  
  38.   if ((win = (WINDOW *) malloc(sizeof(WINDOW))) == NULL)
  39.     return ((WINDOW *) NULL);
  40.  
  41.   /* allocate the line pointer array */
  42.  
  43.   if ((win->_line = (int **) calloc(num_lines, sizeof (int *))) == NULL)
  44.     {
  45.     free(win);
  46.     return((WINDOW *) NULL);
  47.     }
  48.  
  49.   /* allocate the minchng and maxchng arrays */
  50.  
  51.   if ((win->_minchng = (int *) calloc(num_lines, sizeof(int))) == NULL)
  52.     {
  53.     free(win);
  54.     free(win->_line);
  55.     return((WINDOW *) NULL);
  56.     }
  57.   if ((win->_maxchng = (int *) calloc(num_lines, sizeof(int))) == NULL)
  58.     {
  59.     free(win);
  60.     free(win->_line);
  61.     free(win->_minchng);
  62.     return((WINDOW *) NULL);
  63.     }
  64.  
  65.   /* initialize window variables */
  66.  
  67.   win->_curx      = 0;
  68.   win->_cury      = 0;
  69.   win->_maxy      = num_lines;
  70.   win->_maxx      = num_columns;
  71.   win->_begy      = begy;
  72.   win->_begx      = begx;
  73.   win->_flags     = 0;
  74.   win->_attrs     = ATR_NRM;
  75.   win->_tabsize   = 8;
  76.   win->_clear     = (bool) ((num_lines == _LINES) && (num_columns == _COLS));
  77.   win->_leave     = FALSE;
  78.   win->_scroll    = FALSE;
  79.   win->_nodelay   = FALSE;
  80.   win->_keypad    = FALSE;
  81.   win->_regtop    = 0;
  82.   win->_regbottom = num_lines - 1;
  83.  
  84.   for (i = 0; i < 8; i++)
  85.     win->_borderchars[i] = '\0';
  86.  
  87.   /* init to say window unchanged */
  88.  
  89.   for (i = 0; i < num_lines; i++)
  90.     {
  91.     win->_minchng[i] = 0;
  92.     win->_maxchng[i] = num_columns-1;
  93.     }
  94.  
  95.   /* set flags for window properties */
  96.  
  97.   if ((begy + num_lines) == _LINES)
  98.     {
  99.     win->_flags |= _ENDLINE;
  100.     if ((begx == 0) && (num_columns == _COLS) && (begy == 0))
  101.       win->_flags |= _FULLWIN;
  102.     } /* if */
  103.  
  104.   if (((begy + num_lines) == _LINES)
  105.         &&
  106.       ((begx + num_columns) == _COLS))
  107.     win->_flags |= _SCROLLWIN;
  108.   return(win);
  109.   } /* makenew */
  110.  
  111. /****************************************************************/
  112. /* Newwin() creates a new window with size num_lines * num_co-  */
  113. /* lumns, and origin begx,begy relative to the SCREEN. Special  */
  114. /* case: if num_lines and/or num_columns is 0, the remainder of */
  115. /* the screen is used.                                          */
  116. /****************************************************************/
  117.  
  118. WINDOW *newwin(num_lines, num_columns, begy, begx)
  119.   int   num_lines, num_columns, begy, begx;
  120.   {
  121.   WINDOW        *win;
  122.   int           *ptr;
  123.   int            i, j;
  124.  
  125.   if (num_lines == 0)
  126.     num_lines = _LINES - begy;
  127.   if (num_columns == 0)
  128.     num_columns = _COLS - begx;
  129.   if ((win = makenew(num_lines, num_columns, begy, begx)) == (WINDOW *) NULL)
  130.     return((WINDOW *)NULL);
  131.   for (i = 0; i < num_lines; i++)       /* make and clear the lines */
  132.     {
  133.     if((win->_line[i] = (int *) calloc(num_columns,sizeof(int))) == NULL)
  134.       {
  135.       for (j = 0; j < i; j++)           /* if error, free all the data */
  136.         free(win->_line[j]);
  137.       free(win->_minchng);
  138.       free(win->_maxchng);
  139.       free(win->_line);
  140.       free(win);
  141.       return((WINDOW *)NULL);
  142.       } /* if */
  143.     else
  144.       for (ptr = win->_line[i]; ptr < win->_line[i] + num_columns;)
  145.         *ptr++ = ' ' | ATR_NRM;
  146.     } /* for */
  147.   return(win);
  148.   } /* newwin */
  149.  
  150. /****************************************************************/
  151. /* Subwin() creates a sub-window in the 'orig' window, with     */
  152. /* size num_lines * num_columns, and with origin begx, begy     */
  153. /* relative to the SCREEN. Special case: if num_lines and/or    */
  154. /* num_columns is 0, the remainder of the original window is    */
  155. /* used. The subwindow uses the original window's line buffers  */
  156. /* to store its own lines.                                      */
  157. /****************************************************************/
  158.  
  159. WINDOW *subwin(WINDOW *orig, int num_lines, int num_columns, int begy, int begx)
  160.   {
  161.   WINDOW    *win;
  162.   short         i, j, k;
  163.  
  164.   /* make sure window fits inside the original one */
  165.  
  166.   if (
  167.       begy < orig->_begy || 
  168.       begx < orig->_begx ||
  169.       (begy + num_lines) > (orig->_begy + orig->_maxy) ||
  170.       (begx + num_columns) > (orig->_begx + orig->_maxx)
  171.      )
  172.     return((WINDOW *) ERR);
  173.  
  174.   if (num_lines == 0)
  175.     num_lines = orig->_maxy - (begy - orig->_begy);
  176.   if (num_columns == 0)
  177.     num_columns = orig->_maxx - (begx - orig->_begx);
  178.   if ((win = makenew(num_lines, num_columns, begy, begx)) == (WINDOW *) ERR)
  179.     return((WINDOW *) ERR);
  180.  
  181.   /* set line pointers the same as in the original window */
  182.  
  183.   j = begy - orig->_begy;
  184.   k = begx - orig->_begx;
  185.   for (i = 0; i < num_lines; i++)
  186.     win->_line[i] = (orig->_line[j++]) + k;
  187.   win->_flags |= _SUBWIN;
  188.   return(win);
  189.   } /* subwin */
  190.