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

  1. /**
  2. *
  3. * Name          wnwrtty -- Write a character to current window TTY-style.
  4. *
  5. * Synopsis      wnwrtty(ch,fore,back);
  6. *
  7. *               char ch         Character to write
  8. *               int  fore       -1 if default foreground attribute is to
  9. *                               be used; a value between 0 and 15
  10. *                               specifies a new foreground attribute.
  11. *               int  back       -1 if default background attribute is to
  12. *                               be used; a value between 0 and 15
  13. *                               specifies a new background attribute.
  14. *
  15. * Description   This function writes a character to the window in the
  16. *               manner of a teletype.  It treats carriage return, line
  17. *               feed, backspace, and bell as commands rather than as
  18. *               printable characters.  All output and scrolling are
  19. *               confined to the window.
  20. *
  21. *               The window's cursor is left after the displayed
  22. *               character.  The visible cursor is not moved unless this
  23. *               window is currently selected (via WNCURSOR) to have this
  24. *               page's active cursor.
  25. *
  26. *               The window is scrolled if a line feed occurs on the last
  27. *               line of the window or if the last line is overflowed.
  28. *               The window's native attributes are used for the new
  29. *               blank row.
  30. *
  31. *               If the screen is in a text mode, specifying -1 for fore
  32. *               or back uses the window's default foreground or
  33. *               background attributes, respectively.  If the screen is
  34. *               in a graphics mode, then specifying -1 for fore will
  35. *               cause color 1 to be used.
  36. *
  37. * Special       Tab characters ('\t') are displayed literally.
  38. * characters
  39. *               NUL ('\0') is a printable character (printed as a blank
  40. *               space).
  41. *
  42. *               Line feed ('\12') causes the cursor to move down one
  43. *               row, unless it is already on the bottom row of the
  44. *               window, in which case the window is scrolled.
  45. *
  46. *               Carriage return ('\r') causes the cursor to move to the
  47. *               leftmost column of the window.
  48. *
  49. *               Backspace ('\b') causes the cursor to move one column to
  50. *               the left (non-destructively), unless it is already at
  51. *               the leftmost column of the window, in which case nothing
  52. *               happens.
  53. *
  54. *               The BEL character ('\7') causes the computer's bell to
  55. *               sound if the current page is active.
  56. *
  57. * Returns       (Function return type is void.)
  58. *               b_wnerr     Possible values:
  59. *                           (No change)     Success.
  60. *                           WN_BAD_WIN      No window designated
  61. *                                           as current.
  62. *                           WN_ILL_DIM      Internal error.
  63. *
  64. * Version       3.0  (C)Copyright Blaise Computing Inc. 1986
  65. *
  66. * Version       3.02 March 23, 1987
  67. *               Forced WNUPDATE before modifying screen.
  68. *
  69. **/
  70.  
  71. #include <bwindow.h>
  72.  
  73. #if MSC300
  74. #include <memory.h>
  75. #else
  76. #include <stdlib.h>
  77. #endif
  78.  
  79. #define  BEL     '\7'
  80. #define  BS      '\b'
  81. #define  CR      '\r'
  82. #define  LF      '\12'
  83.  
  84. void wnwrtty(ch,fore,back)
  85. char ch;
  86. int  fore,back;
  87. {
  88.     int  row,col;
  89.     int  ax,bx,cx,dx,flags;
  90.     int  old_curpos;
  91.     int  scr_fore,scr_back;
  92.     char scr_attr;
  93.     int  h,w,i;
  94.     CELL *ptr;
  95.  
  96.     if (wnvalwin(b_pcurwin) == NIL)
  97.     {
  98.         wnerror(WN_BAD_WIN);
  99.         return;
  100.     }
  101.  
  102.     wncurpos(&row,&col);
  103.  
  104.     switch (ch)
  105.     {
  106.         case BEL:
  107.             scttywrt(ch,0);
  108.             break;
  109.  
  110.         case BS:
  111.             if (col > 0)              /* Don't back up past first     */
  112.                 wncurmov(row,col - 1);            /* column           */
  113.             break;
  114.  
  115.         case CR:
  116.             wncurmov(row,0);          /* Beginning of current line    */
  117.             break;
  118.  
  119.         default:                      /* Includes the case of LF.     */
  120.  
  121.             scr_fore = utlonyb(b_pcurwin->attr);
  122.             if (fore == -1)
  123.                 fore = scr_fore;
  124.             scr_back = uthinyb(b_pcurwin->attr);
  125.             if (back == -1)
  126.                 back = scr_back;
  127.  
  128.             h = b_pcurwin->img.dim.h;
  129.             w = b_pcurwin->img.dim.w;
  130.  
  131.     /* Update the physical screen if possible.                        */
  132.  
  133.             if (   b_pcurwin->where_shown.dev != MONO
  134.                 && b_pcurwin->where_shown.dev != COLOR)
  135.             {                         /* Not shown anywhere.          */
  136.                 b_pcurwin->internals.dirty = 1;
  137.             }
  138.             else if (b_pcurwin->options.hidden)
  139.             {
  140.                                       /* Do nothing if hidden.        */
  141.             }
  142.             else if (   b_pcurwin->options.delayed
  143.                      || b_pcurwin->internals.frozen)
  144.             {                                 /* Displayed, but can't */
  145.                 b_pcurwin->internals.dirty = 1;     /* be written to. */
  146.             }
  147.             else
  148.             {                         /* Update the screen            */
  149.  
  150.                 wnupdate(b_pcurwin);  /* Flush pending output.        */
  151.  
  152.                                       /* Get former cursor position.  */
  153.                 ax = 0x0300;
  154.                 bx = utbyword(b_curpage,0);
  155.                 bios(16,&ax,&bx,&cx,&old_curpos,&flags);
  156.  
  157.                                       /* Move cursor for this window. */
  158.                 ax = 0x0200;
  159.                 bx = utbyword(b_curpage,0);
  160.                 dx = utbyword(b_pcurwin->where_shown.corner.row + row,
  161.                               b_pcurwin->where_shown.corner.col + col);
  162.                 bios(16,&ax,&bx,&cx,&dx,&flags);
  163.  
  164.                                       /* Write the character.         */
  165.                 scttywin(b_pcurwin->where_shown.corner.row,
  166.                          b_pcurwin->where_shown.corner.col,
  167.                          b_pcurwin->where_shown.corner.row + h - 1,
  168.                          b_pcurwin->where_shown.corner.col + w - 1,
  169.                          ch,fore,back,scr_fore,scr_back);
  170.  
  171.                                       /* Restore cursor position.     */
  172.                 ax = 0x0200;
  173.                 bx = utbyword(b_curpage,0);
  174.                 dx = old_curpos;
  175.                 bios(16,&ax,&bx,&cx,&dx,&flags);
  176.             }
  177.  
  178.     /* Update the memory copy of the window's data area.              */
  179.  
  180.             if (ch != LF)
  181.             {                         /* Ordinary character           */
  182.                 (b_pcurwin->img.pdata + (row * w) + col)->ch   = ch;
  183.                 (b_pcurwin->img.pdata + (row * w) + col)->attr
  184.                                            = (char) utnybbyt(back,fore);
  185.  
  186.                 if (++col < w)        /* Check for possible wrap      */
  187.                 {                     /* This fits on current line    */
  188.                     wncurmov(row,col);
  189.                     break;
  190.                 }
  191.                 col = 0;              /* Wrap to next line            */
  192.             }
  193.  
  194.             if (++row >= h)           /* Scroll the memory image up   */
  195.             {                         /* one line.                    */
  196.                 row = h - 1;
  197.                 memcpy((char *)  b_pcurwin->img.pdata,
  198.                        (char *) (b_pcurwin->img.pdata + w),
  199.                        (unsigned int) (w * sizeof(CELL) * row));
  200.  
  201.                                       /* Fill last row of memory      */
  202.                                       /* image with blanks.           */
  203.                 scr_attr = (char) b_pcurwin->attr;
  204.                 ptr      = b_pcurwin->img.pdata + (row * w);
  205.                 for (i = 0; i < w; i++)
  206.                 {
  207.                     ptr[i].ch   = ' ';
  208.                     ptr[i].attr = scr_attr;
  209.                 }
  210.             }
  211.  
  212.             wncurmov(row,col);        /* Move cursor to beginning of  */
  213.                                       /* new bottom line.             */
  214.             break;
  215.     }
  216. }
  217.