home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_GEN / FACETV.ZIP / TVSCREEN.CPP < prev    next >
C/C++ Source or Header  |  1994-01-04  |  5KB  |  183 lines

  1. /************************************************************************
  2. **
  3. ** @(#)tvscreen.cpp    08/13/93    Chris Ahlstrom
  4. **
  5. **    Uses TurboVision TTerminal class to implement a replacement for
  6. ** printf() routines.
  7. **
  8. **    The callers of printf() can instead format their strings first,
  9. ** then call screenmsg() to write text to a dumb terminal, where it
  10. ** collects so that the user can scroll through it and not miss any
  11. ** messages from subroutines.
  12. **
  13. **    I tried to do this myself in tvwindow.cpp, but later saw that
  14. ** TV already had there own (and better) version.
  15. **
  16. *************************************************************************/
  17.  
  18. #define TVSCREEN_cpp
  19.  
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <io.h>
  23. #include <alloc.h>
  24. #include <fcntl.h>
  25. #include <strstrea.h>
  26. #include <fstream.h>
  27.  
  28. #include "tvscreen.h"            /* CPP routines for TEST code    */
  29.  
  30.  
  31. /************************************************************************
  32. ** TWindowScreen constructor and desctructor
  33. **
  34. **    We could override TWindow::initFrame() for a fancier frame.
  35. **
  36. *************************************************************************/
  37.  
  38. TWindowScreen::TWindowScreen
  39. (
  40.     const TRect& bounds,    // initial size rectangle for window
  41.     const char *atitle,        // title for the top of the frame
  42.     short anumber,        // bogus number for the window (wmNoNumber)
  43.     ushort bufsize        // buffer size for window
  44. )
  45.   :
  46.     TWindowInit    (TWindowScreen::initFrame),
  47.     TWindow    (bounds, atitle, anumber)
  48. {
  49.     screenInterior = makeInterior(bounds, bufsize);
  50.     if (screenInterior)
  51.     insert(screenInterior);
  52. }
  53.  
  54.  
  55. TWindowScreen::~TWindowScreen()
  56. {
  57. //    if (screenInterior)           // might be deleted by the desktop
  58. //        delete screenInterior;
  59. }
  60.  
  61.  
  62. /************************************************************************
  63. ** TWindowScreen::makeInterior()
  64. *************************************************************************/
  65.  
  66.  
  67. TTerminal *
  68. TWindowScreen::makeInterior
  69. (
  70.     TRect bounds,
  71.     ushort bufsize
  72. )
  73. {
  74.     bounds = getExtent();        // size of the window
  75.     bounds.grow(-1, -1);        // interior size matches window
  76.     return new TTerminal
  77.     (
  78.     bounds,
  79.         0,                // no horizontal scrollbar
  80.         standardScrollBar(sbVertical | sbHandleKeyboard),
  81.         bufsize
  82.     );
  83. };
  84.  
  85.  
  86. /************************************************************************
  87. ** Friend functions of TWindowScreen class
  88. *************************************************************************/
  89.  
  90. #define LEFT_X     1
  91. #define LEFT_Y     5
  92. #define RIGHT_X    78
  93. #define RIGHT_Y    22
  94.  
  95. TWindowScreen *
  96. startWindowScreen
  97. (
  98.     TDeskTop *desktop,
  99.     unsigned buffsize
  100. )
  101. {
  102.     char title[64];                // must match sprintf() string
  103.  
  104.     if ((coreleft() - 1024) < buffsize)
  105.         buffsize = unsigned(coreleft() - 1024);
  106.  
  107.     TWindowScreen *crt = new TWindowScreen
  108.     (
  109.     TRect(LEFT_X, LEFT_Y, RIGHT_X, RIGHT_Y),
  110.     "Subroutine Messages",
  111.     wnNoNumber,
  112.     buffsize
  113.     );
  114.     desktop->insert(crt);
  115.  
  116.     /*
  117.     ** Assign the TTerminal interior text device driver to a text "file"
  118.     */
  119.  
  120.     otstream os(crt->screenInterior);        // open an output text stream
  121.  
  122.     // crt->screenOStream = &os;        // save handle to it
  123.  
  124.     sprintf(title, "TTERMINAL CONSOLE WINDOW, %u bytes\n\n\0", buffsize);
  125.     doScreenMessage(crt, title);        // sort of a sanity check
  126.     crt->screenInterior->scrollTo(0, 0);    // show top of the buffer
  127.  
  128.     return crt;                    // return the handle
  129. }
  130.  
  131.  
  132. /************************************************************************
  133. ** doScreenMessage()
  134. **
  135. **    This TWindowClass friend simply writes the message to the
  136. ** window's stream, then flushes the buffer so the message appears
  137. ** right away.
  138. **
  139. **    For some reason, cannot pass the pointer "screenOStream" without
  140. ** causing weird problems (see the unuseable code in the USEABLE_CODE
  141. ** #if below).
  142. **
  143. *************************************************************************/
  144.  
  145. void
  146. doScreenMessage
  147. (
  148.     TWindowScreen *handle,
  149.     char *message
  150. )
  151. {
  152.     otstream os(handle->screenInterior);
  153.     int length = strlen(message);
  154.  
  155.     if
  156.     (
  157.     (handle != (TWindowScreen *) 0)  && (message != NULL) &&
  158.     handle->screenInterior->canInsert(length)
  159.     )
  160.         os << message << flush;
  161. };
  162.  
  163.  
  164. #if defined(USEABLE_CODE)                // { USEABLE_CODE
  165.  
  166. void
  167. doScreenMessage
  168. (
  169.     TWindowScreen *handle,
  170.     char *message
  171. )
  172. {
  173.     if
  174.     (
  175.     (handle != (TWindowScreen *) 0)  &&
  176.     (message != NULL) &&
  177.     handle->screenInterior->canInsert(strlen(message))
  178.     )
  179.         *handle->screenOStream << message << flush;
  180. };
  181.  
  182. #endif                            // } USEABLE_CODE
  183.