home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c005 / 5.ddi / C / WNWRBUF.C < prev    next >
Encoding:
C/C++ Source or Header  |  1987-04-13  |  6.5 KB  |  205 lines

  1. /**
  2. *
  3. * Name        wnwrbuf -- Write buffer to the current window
  4. *
  5. * Synopsis    num_writ = wnwrbuf(row,col,num_spaces,buffer,fore,back,
  6. *                   option);
  7. *
  8. *        int  num_writ    Number of character cells actually
  9. *                written
  10. *        int  row,col    Row and column (relative to window's
  11. *                data area) at which to begin writing
  12. *        int  num_spaces Number of character cells to write.
  13. *                If CHARS_ONLY is specified (see "option"
  14. *                below), num_spaces == 0 indicates that
  15. *                the buffer is terminated by a NUL ('\0')
  16. *                character.
  17. *        char *buffer    Data to write
  18. *        int  fore    -1 if window's native foreground
  19. *                attribute is to be used;
  20. *                if option is 0 or 1, a value between 0 and
  21. *                15 specifies a new foreground attribute.
  22. *        int  back    -1 if window's native background
  23. *                attribute is to be used;
  24. *                if option is 0 or 1, a value between 0 and
  25. *                15 specifies a new background attribute.
  26. *        int  option    The option value tells WNWRBUF how the
  27. *                buffer is constructed and where to
  28. *                position the cursor after the buffer has
  29. *                been written.  Three different bits are
  30. *                relevant:
  31. *
  32. *              Value        Meaning
  33. *              -----------   ------------------------------------
  34. *              CUR_BEG        Leave cursor at (row,col).
  35. *              CUR_AFTER     Leave cursor after end of string.
  36. *
  37. *              CHARS_ONLY    Buffer contains characters only.
  38. *                    If num_spaces is zero, the string
  39. *                    is terminated by a NUL ('\0')
  40. *                    character.
  41. *              CHAR_ATTR     Buffer contains (char,attr) pairs.
  42. *
  43. *              MOVE_CUR        Leave cursor according to
  44. *                      CUR_BEG/CUR_AFTER bit.
  45. *              NO_MOVE_CUR   Preserve cursor location.
  46. *
  47. * Description    This function writes characters to the current window
  48. *        with or without their corresponding video attributes.
  49. *        It can leave the cursor at the beginning or end of the
  50. *        space written to.
  51. *
  52. *        This routine will not scroll the window, although it
  53. *        wraps text from one row to the next.
  54. *
  55. *        If the num_spaces argument is nonzero, it specifies the
  56. *        number of "character cells" to write, where "character
  57. *        cells" means the number of physical spaces on the
  58. *        screen.  (If CHAR_ATTR is specified, twice this number
  59. *        of bytes will be used from the buffer.)  If num_spaces
  60. *        exceeds the number of characters to the end of the row,
  61. *        the write will continue on the following row, and so on
  62. *        to the end of the window.  The write will stop at the
  63. *        end of the window, hence the number of cells actually
  64. *        written may be less than what the buffer contains.  The
  65. *        actual number is returned as the value of the function.
  66. *
  67. *        If the write includes the lower right corner of the
  68. *        window, then the cursor is left at the lower right
  69. *        corner if MOVE_CUR and CUR_AFTER are both specified.
  70. *
  71. *        Be aware that this function recognizes NULs ('\0') as
  72. *        the end of the buffer only if num_spaces is zero and
  73. *        CHARS_ONLY is specified.
  74. *
  75. *        An error occurs if no window is designated as current or
  76. *        if row or col exceeds the window's dimensions.
  77. *
  78. * Returns    num_writ    Number of character cells written
  79. *        b_pcurwin->cur_loc   Possibly altered.
  80. *        b_wnerr     Possible values:
  81. *                (No change)     Success.
  82. *                WN_BAD_WIN     b_pcurwin is invalid.
  83. *                WN_ILL_DIM     row or col out of range
  84. *                WN_NULL_PTR     Internal error.
  85. *
  86. * Version    3.0  (C)Copyright Blaise Computing Inc. 1986
  87. *
  88. * Version    3.02 March 23, 1987
  89. *        Forced WNUPDATE before performing screen I/O.
  90. *
  91. **/
  92.  
  93. #include <string.h>
  94.  
  95. #include <bgenvid.h>              /* This routine doesn't care    */
  96.                       /* whether direct or BIOS       */
  97.                       /* version of BGENVID.H is used.*/
  98. #include <bwindow.h>
  99.  
  100. int wnwrbuf(row,col,num_spaces,buffer,fore,back,option)
  101. int  row,col,num_spaces;
  102. char *buffer;
  103. int  fore,back,option;
  104. {
  105.     int  h,w,want_attr,i,offset,last,last_row;
  106.     int  attr;
  107.     CELL *pdata;
  108.  
  109.     if (wnvalwin(b_pcurwin) == NIL)   /* Validate window structure    */
  110.     {
  111.     wnerror(WN_BAD_WIN);
  112.     return 0;
  113.     }
  114.  
  115.     h = b_pcurwin->img.dim.h;
  116.     w = b_pcurwin->img.dim.w;
  117.     if (   utrange(row,0,h - 1)       /* Validate row, col          */
  118.     || utrange(col,0,w - 1))
  119.     {
  120.     wnerror(WN_ILL_DIM);
  121.     return 0;
  122.     }
  123.  
  124.     if (b_pcurwin->img.pdata == NIL)  /* Be sure there's valid data   */
  125.     {
  126.     wnerror(WN_NULL_PTR);
  127.     return 0;
  128.     }
  129.  
  130.     want_attr = ((option & CHAR_ATTR) != 0);
  131.  
  132.     if (fore == -1)
  133.     fore = utlonyb(b_pcurwin->attr);
  134.     if (back == -1)
  135.     back = uthinyb(b_pcurwin->attr);
  136.     attr = utnybbyt(back,fore);
  137.  
  138.     if ((!want_attr) && num_spaces == 0)
  139.     num_spaces = (int) strlen(buffer);
  140.  
  141.     /* Update the memory copy of the window's data area.              */
  142.  
  143.     offset = (row * w) + col;            /* Cell at which to start.*/
  144.     pdata  = b_pcurwin->img.pdata + offset; /* Pointer to first cell  */
  145.  
  146.     utuplim(num_spaces,((h * w) - offset))  /* Don't go beyond end of */
  147.                         /* window.              */
  148.  
  149.     last = offset + num_spaces;       /* "last" is offset of last     */
  150.                       /* char to write.           */
  151.     utuplim(last,(h * w) - 1);
  152.     last_row = last/w;              /* Last row written to window.  */
  153.  
  154.     for (i = 0; i < num_spaces; i++)
  155.     {                      /* Copy to data in memory.      */
  156.     pdata[i].ch   = *buffer++;
  157.     pdata[i].attr = (want_attr ? *buffer++ : (char) attr);
  158.     }
  159.  
  160.     /* Update the physical screen if possible.                  */
  161.  
  162.     if (   b_pcurwin->where_shown.dev != MONO
  163.     && b_pcurwin->where_shown.dev != COLOR)
  164.     {                      /* Not shown anywhere.          */
  165.     b_pcurwin->internals.dirty = 1;
  166.     }
  167.     else if (b_pcurwin->options.hidden)
  168.     {
  169.                       /* Do nothing if hidden.          */
  170.     }
  171.     else if (    b_pcurwin->options.delayed
  172.          || b_pcurwin->internals.frozen)
  173.     {                      /* Displayed, but can't be      */
  174.     b_pcurwin->internals.dirty = 1;          /* written to.   */
  175.     }
  176.     else
  177.     {                      /* Actually do the write.       */
  178.  
  179.     wnupdate(b_pcurwin);          /* Flush pending output.          */
  180.  
  181.                       /* Update all rows of the window*/
  182.                       /* which have been altered.     */
  183.     gvwrrect(b_pcurwin->where_shown.corner.row + row,
  184.          b_pcurwin->where_shown.corner.col,
  185.          b_pcurwin->where_shown.corner.row + last_row,
  186.          b_pcurwin->where_shown.corner.col + w - 1,
  187.          (char *) (b_pcurwin->img.pdata + (row * w)),
  188.          fore,back,CHAR_ATTR);
  189.     }
  190.  
  191.     if (!(option & NO_MOVE_CUR))
  192.     {
  193.     if (option & CUR_AFTER)
  194.     {                  /* Leave cursor after last char */
  195.         wncurmov(last_row,last % w);
  196.     }
  197.     else
  198.     {                  /* Leave cursor at beginning    */
  199.         wncurmov(row,col);          /* of string.              */
  200.     }
  201.     }
  202.  
  203.     return num_spaces;
  204. }
  205.