home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / screen / curses / huge / newwin.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-27  |  6.3 KB  |  203 lines

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