home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / CURS13_2.ZIP / NEWWIN.C < prev    next >
Text File  |  1989-12-08  |  6KB  |  193 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(num_lines, num_columns, begy, begx)
  32.   int    num_lines, num_columns, begy, begx;
  33.   {
  34.   int         i;
  35.   WINDOW    *win;
  36.  
  37.   /* allocate the window structure itself */
  38.  
  39.   if ((win = (WINDOW *) malloc(sizeof(WINDOW))) == NULL)
  40.     return ((WINDOW *) ERR);
  41.  
  42.   /* allocate the line pointer array */
  43.  
  44.   if ((win->_line = (int **) calloc(num_lines, sizeof (int *))) == NULL)
  45.     {
  46.     free(win);
  47.     return((WINDOW *) ERR);
  48.     }
  49.  
  50.   /* allocate the minchng and maxchng arrays */
  51.  
  52.   if ((win->_minchng = (int *) calloc(num_lines, sizeof(int))) == NULL)
  53.     {
  54.     free(win);
  55.     free(win->_line);
  56.     return((WINDOW *) ERR);
  57.     }
  58.   if ((win->_maxchng = (int *) calloc(num_lines, sizeof(int))) == NULL)
  59.     {
  60.     free(win);
  61.     free(win->_line);
  62.     free(win->_minchng);
  63.     return((WINDOW *) ERR);
  64.     }
  65.  
  66.   /* initialize window variables */
  67.  
  68.   win->_curx      = 0;
  69.   win->_cury      = 0;
  70.   win->_maxy      = num_lines;
  71.   win->_maxx      = num_columns;
  72.   win->_begy      = begy;
  73.   win->_begx      = begx;
  74.   win->_flags     = 0;
  75.   win->_attrs     = ATR_NRM;
  76.   win->_tabsize   = 8;
  77.   win->_clear     = (bool) ((num_lines == LINES) && (num_columns == COLS));
  78.   win->_leave     = FALSE;
  79.   win->_scroll    = FALSE;
  80.   win->_nodelay   = FALSE;
  81.   win->_keypad    = FALSE;
  82.   win->_regtop    = 0;
  83.   win->_regbottom = num_lines - 1;
  84.  
  85.   for (i = 0; i < 8; i++)
  86.     win->_borderchars[i] = '\0';
  87.  
  88.   /* init to say window unchanged */
  89.  
  90.   for (i = 0; i < num_lines; i++)
  91.     {
  92.     win->_minchng[i] = 0;
  93.     win->_maxchng[i] = num_columns-1;
  94.     }
  95.  
  96.   /* set flags for window properties */
  97.  
  98.   if ((begy + num_lines) == LINES)
  99.     {
  100.     win->_flags |= _ENDLINE;
  101.     if ((begx == 0) && (num_columns == COLS) && (begy == 0))
  102.       win->_flags |= _FULLWIN;
  103.     } /* if */
  104.  
  105.   if (((begy + num_lines) == LINES)
  106.         &&
  107.       ((begx + num_columns) == COLS))
  108.     win->_flags |= _SCROLLWIN;
  109.   return(win);
  110.   } /* makenew */
  111.  
  112. /****************************************************************/
  113. /* Newwin() creates a new window with size num_lines * num_co-    */
  114. /* lumns, and origin begx,begy relative to the SCREEN. Special    */
  115. /* case: if num_lines and/or num_columns is 0, the remainder of    */
  116. /* the screen is used.                        */
  117. /****************************************************************/
  118.  
  119. WINDOW *newwin(num_lines, num_columns, begy, begx)
  120.   int    num_lines, num_columns, begy, begx;
  121.   {
  122.   WINDOW    *win;
  123.   int        *ptr;
  124.   int         i, j;
  125.  
  126.   if (num_lines == 0)
  127.     num_lines = LINES - begy;
  128.   if (num_columns == 0)
  129.     num_columns = COLS - begx;
  130.   if ((win = makenew(num_lines, num_columns, begy, begx)) == (WINDOW *) ERR)
  131.     return((WINDOW *) ERR);
  132.   for (i = 0; i < num_lines; i++)    /* make and clear the lines */
  133.     {
  134.     if((win->_line[i] = (int *) calloc(num_columns,sizeof(int))) == NULL)
  135.       {
  136.       for (j = 0; j < i; j++)        /* if error, free all the data */
  137.     free(win->_line[j]);
  138.       free(win->_minchng);
  139.       free(win->_maxchng);
  140.       free(win->_line);
  141.       free(win);
  142.       return((WINDOW *) ERR);
  143.       } /* if */
  144.     else
  145.       for (ptr = win->_line[i]; ptr < win->_line[i] + num_columns;)
  146.     *ptr++ = ' ' | ATR_NRM;
  147.     } /* for */
  148.   return(win);
  149.   } /* newwin */
  150.  
  151. /****************************************************************/
  152. /* Subwin() creates a sub-window in the 'orig' window, with    */
  153. /* size num_lines * num_columns, and with origin begx, begy    */
  154. /* relative to the SCREEN. Special case: if num_lines and/or    */
  155. /* num_columns is 0, the remainder of the original window is    */
  156. /* used. The subwindow uses the original window's line buffers    */
  157. /* to store its own lines.                    */
  158. /****************************************************************/
  159.  
  160. WINDOW *subwin(orig, num_lines, num_columns, begy, begx)
  161.   WINDOW    *orig;
  162.   int         num_lines, num_columns, begy, begx;
  163.   {
  164.   WINDOW    *win;
  165.   int         i, j, k;
  166.  
  167.   /* make sure window fits inside the original one */
  168.  
  169.   if (
  170.       begy < orig->_begy || 
  171.       begx < orig->_begx ||
  172.       (begy + num_lines) >= (orig->_begy + orig->_maxy) ||
  173.       (begx + num_columns) >= (orig->_begx + orig->_maxx)
  174.      )
  175.     return((WINDOW *) ERR);
  176.  
  177.   if (num_lines == 0)
  178.     num_lines = orig->_maxy - 1 - (begy - orig->_begy);
  179.   if (num_columns == 0)
  180.     num_columns = orig->_maxx - 1 - (begx - orig->_begx);
  181.   if ((win = makenew(num_lines, num_columns, begy, begx)) == (WINDOW *) ERR)
  182.     return((WINDOW *) ERR);
  183.  
  184.   /* set line pointers the same as in the original window */
  185.  
  186.   j = begy - orig->_begy;
  187.   k = begx - orig->_begx;
  188.   for (i = 0; i < num_lines; i++)
  189.     win->_line[i] = (orig->_line[j++]) + k;
  190.   win->_flags |= _SUBWIN;
  191.   return(win);
  192.   } /* subwin */
  193.