home *** CD-ROM | disk | FTP | other *** search
- /**
- *
- * Name wnwrbuf -- Write buffer to the current window
- *
- * Synopsis num_writ = wnwrbuf(row,col,num_spaces,buffer,fore,back,
- * option);
- *
- * int num_writ Number of character cells actually
- * written
- * int row,col Row and column (relative to window's
- * data area) at which to begin writing
- * int num_spaces Number of character cells to write.
- * If CHARS_ONLY is specified (see "option"
- * below), num_spaces == 0 indicates that
- * the buffer is terminated by a NUL ('\0')
- * character.
- * char *buffer Data to write
- * int fore -1 if window's native foreground
- * attribute is to be used;
- * if option is 0 or 1, a value between 0 and
- * 15 specifies a new foreground attribute.
- * int back -1 if window's native background
- * attribute is to be used;
- * if option is 0 or 1, a value between 0 and
- * 15 specifies a new background attribute.
- * int option The option value tells WNWRBUF how the
- * buffer is constructed and where to
- * position the cursor after the buffer has
- * been written. Three different bits are
- * relevant:
- *
- * Value Meaning
- * ----------- ------------------------------------
- * CUR_BEG Leave cursor at (row,col).
- * CUR_AFTER Leave cursor after end of string.
- *
- * CHARS_ONLY Buffer contains characters only.
- * If num_spaces is zero, the string
- * is terminated by a NUL ('\0')
- * character.
- * CHAR_ATTR Buffer contains (char,attr) pairs.
- *
- * MOVE_CUR Leave cursor according to
- * CUR_BEG/CUR_AFTER bit.
- * NO_MOVE_CUR Preserve cursor location.
- *
- * Description This function writes characters to the current window
- * with or without their corresponding video attributes.
- * It can leave the cursor at the beginning or end of the
- * space written to.
- *
- * This routine will not scroll the window, although it
- * wraps text from one row to the next.
- *
- * If the num_spaces argument is nonzero, it specifies the
- * number of "character cells" to write, where "character
- * cells" means the number of physical spaces on the
- * screen. (If CHAR_ATTR is specified, twice this number
- * of bytes will be used from the buffer.) If num_spaces
- * exceeds the number of characters to the end of the row,
- * the write will continue on the following row, and so on
- * to the end of the window. The write will stop at the
- * end of the window, hence the number of cells actually
- * written may be less than what the buffer contains. The
- * actual number is returned as the value of the function.
- *
- * If the write includes the lower right corner of the
- * window, then the cursor is left at the lower right
- * corner if MOVE_CUR and CUR_AFTER are both specified.
- *
- * Be aware that this function recognizes NULs ('\0') as
- * the end of the buffer only if num_spaces is zero and
- * CHARS_ONLY is specified.
- *
- * An error occurs if no window is designated as current or
- * if row or col exceeds the window's dimensions.
- *
- * Returns num_writ Number of character cells written
- * b_pcurwin->cur_loc Possibly altered.
- * b_wnerr Possible values:
- * (No change) Success.
- * WN_BAD_WIN b_pcurwin is invalid.
- * WN_ILL_DIM row or col out of range
- * WN_NULL_PTR Internal error.
- *
- * Version 3.0 (C)Copyright Blaise Computing Inc. 1986
- *
- * Version 3.02 March 23, 1987
- * Forced WNUPDATE before performing screen I/O.
- *
- **/
-
- #include <string.h>
-
- #include <bgenvid.h> /* This routine doesn't care */
- /* whether direct or BIOS */
- /* version of BGENVID.H is used.*/
- #include <bwindow.h>
-
- int wnwrbuf(row,col,num_spaces,buffer,fore,back,option)
- int row,col,num_spaces;
- char *buffer;
- int fore,back,option;
- {
- int h,w,want_attr,i,offset,last,last_row;
- int attr;
- CELL *pdata;
-
- if (wnvalwin(b_pcurwin) == NIL) /* Validate window structure */
- {
- wnerror(WN_BAD_WIN);
- return 0;
- }
-
- h = b_pcurwin->img.dim.h;
- w = b_pcurwin->img.dim.w;
- if ( utrange(row,0,h - 1) /* Validate row, col */
- || utrange(col,0,w - 1))
- {
- wnerror(WN_ILL_DIM);
- return 0;
- }
-
- if (b_pcurwin->img.pdata == NIL) /* Be sure there's valid data */
- {
- wnerror(WN_NULL_PTR);
- return 0;
- }
-
- want_attr = ((option & CHAR_ATTR) != 0);
-
- if (fore == -1)
- fore = utlonyb(b_pcurwin->attr);
- if (back == -1)
- back = uthinyb(b_pcurwin->attr);
- attr = utnybbyt(back,fore);
-
- if ((!want_attr) && num_spaces == 0)
- num_spaces = (int) strlen(buffer);
-
- /* Update the memory copy of the window's data area. */
-
- offset = (row * w) + col; /* Cell at which to start.*/
- pdata = b_pcurwin->img.pdata + offset; /* Pointer to first cell */
-
- utuplim(num_spaces,((h * w) - offset)) /* Don't go beyond end of */
- /* window. */
-
- last = offset + num_spaces; /* "last" is offset of last */
- /* char to write. */
- utuplim(last,(h * w) - 1);
- last_row = last/w; /* Last row written to window. */
-
- for (i = 0; i < num_spaces; i++)
- { /* Copy to data in memory. */
- pdata[i].ch = *buffer++;
- pdata[i].attr = (want_attr ? *buffer++ : (char) attr);
- }
-
- /* Update the physical screen if possible. */
-
- if ( b_pcurwin->where_shown.dev != MONO
- && b_pcurwin->where_shown.dev != COLOR)
- { /* Not shown anywhere. */
- b_pcurwin->internals.dirty = 1;
- }
- else if (b_pcurwin->options.hidden)
- {
- /* Do nothing if hidden. */
- }
- else if ( b_pcurwin->options.delayed
- || b_pcurwin->internals.frozen)
- { /* Displayed, but can't be */
- b_pcurwin->internals.dirty = 1; /* written to. */
- }
- else
- { /* Actually do the write. */
-
- wnupdate(b_pcurwin); /* Flush pending output. */
-
- /* Update all rows of the window*/
- /* which have been altered. */
- gvwrrect(b_pcurwin->where_shown.corner.row + row,
- b_pcurwin->where_shown.corner.col,
- b_pcurwin->where_shown.corner.row + last_row,
- b_pcurwin->where_shown.corner.col + w - 1,
- (char *) (b_pcurwin->img.pdata + (row * w)),
- fore,back,CHAR_ATTR);
- }
-
- if (!(option & NO_MOVE_CUR))
- {
- if (option & CUR_AFTER)
- { /* Leave cursor after last char */
- wncurmov(last_row,last % w);
- }
- else
- { /* Leave cursor at beginning */
- wncurmov(row,col); /* of string. */
- }
- }
-
- return num_spaces;
- }