home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 2.ddi / TVDEMOS.ZIP / ASCII.CPP next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  5.7 KB  |  288 lines

  1. /*----------------------------------------------------------*/
  2. /*                                                          */
  3. /*   Turbo Vision 1.0                                       */
  4. /*   Copyright (c) 1991 by Borland International            */
  5. /*                                                          */
  6. /*   Ascii.cpp: Member functions of following classes:      */
  7. /*                TTable                                    */
  8. /*                TReport                                   */
  9. /*                TAsciiChart                               */
  10. /*----------------------------------------------------------*/
  11.  
  12. #define Uses_TRect
  13. #define Uses_TEvent
  14. #define Uses_TKeys
  15. #define Uses_TDrawBuffer
  16. #define Uses_TStreamableClass
  17. #define Uses_TStreamable
  18. #define Uses_TView
  19. #define Uses_TWindow
  20. #include <tv.h>
  21. __link( RView )
  22. __link( RWindow )
  23.  
  24. #include <string.h>
  25. #include <stdlib.h>
  26. #include <ctype.h>
  27. #include <strstrea.h>
  28. #include <iomanip.h>
  29.  
  30. #include "ascii.h"
  31.  
  32.  
  33. //
  34. // TTable functions
  35. //
  36.  
  37. const char * const TTable::name = "TTable";
  38.  
  39.  
  40. void TTable::write( opstream& os )
  41. {
  42.     TView::write( os );
  43. }
  44.  
  45.  
  46. void *TTable::read( ipstream& is )
  47. {
  48.     TView::read( is );
  49.     return this;
  50. }
  51.  
  52.  
  53. TStreamable *TTable::build()
  54. {
  55.     return new TTable( streamableInit );
  56. }
  57.  
  58.  
  59. TStreamableClass RTable( TTable::name,
  60.              TTable::build,
  61.              __DELTA(TTable)
  62.                );
  63.  
  64.  
  65. TTable::TTable(TRect& r) :
  66.  TView( r )
  67. {
  68. }
  69.  
  70.  
  71. void TTable::draw()
  72. {
  73.     TDrawBuffer buf;
  74.     char        color = getColor(6);
  75.  
  76.     for(int y = 0; y <= size.y-1; y++)
  77.     {
  78.     buf.moveChar(0, ' ', color, size.x);
  79.     for(int x = 0; x <= size.x-1; x++)
  80.         buf.moveChar(x, 32*y+x, color, 1);
  81.     writeLine(0, y, size.x, 1, buf);
  82.     }
  83.     showCursor();
  84. }
  85.  
  86. //
  87. // cmCharFocused is a offset value (basically the ascii code of the
  88. // current selected character) thus should be added, not or'ed, to
  89. // cmAsciiTableCmdBase.
  90. //
  91.  
  92. void TTable::charFocused()
  93. {
  94.     message(owner, evBroadcast, cmAsciiTableCmdBase + cmCharFocused,
  95.       (void *) (cursor.x + 32 * cursor.y));
  96. }
  97.  
  98.  
  99. void TTable::handleEvent(TEvent& event)
  100. {
  101.     TView::handleEvent(event);
  102.  
  103.     if (event.what == evMouseDown)
  104.     {
  105.     do
  106.         {
  107.         if(mouseInView(event.mouse.where))
  108.         {
  109.         TPoint spot = makeLocal(event.mouse.where);
  110.         setCursor(spot.x, spot.y);
  111.         charFocused();
  112.         }
  113.         } while (mouseEvent(event, evMouseMove));
  114.     clearEvent(event);
  115.     }
  116.     else
  117.     {
  118.     if (event.what == evKeyboard)
  119.         {
  120.         switch (event.keyDown.keyCode)
  121.         {
  122.         case kbHome:
  123.             setCursor(0,0);
  124.             break;
  125.         case kbEnd:
  126.             setCursor(size.x-1, size.y-1);
  127.             break;
  128.         case kbUp:
  129.             if (cursor.y > 0)
  130.             setCursor(cursor.x, cursor.y-1);
  131.             break;
  132.         case kbDown:
  133.             if (cursor.y < size.y-1)
  134.             setCursor(cursor.x, cursor.y+1);
  135.             break;
  136.         case kbLeft:
  137.             if (cursor.x > 0)
  138.             setCursor(cursor.x-1, cursor.y);
  139.             break;
  140.         case kbRight:
  141.             if (cursor.x < size.x-1)
  142.             setCursor(cursor.x+1, cursor.y);
  143.                     break;
  144.         default:
  145.                     setCursor(event.keyDown.charScan.charCode % 32,
  146.                       event.keyDown.charScan.charCode / 32);
  147.                     break;
  148.                 }
  149.             charFocused();
  150.             clearEvent(event);
  151.         }
  152.         }
  153. }
  154.  
  155.  
  156. //
  157. // TReport functions
  158. //
  159.  
  160. const char * const TReport::name = "TReport";
  161.  
  162.  
  163. void TReport::write( opstream& os )
  164. {
  165.     TView::write( os );
  166.     os << asciiChar;
  167. }
  168.  
  169.  
  170. void *TReport::read( ipstream& is )
  171. {
  172.     TView::read( is );
  173.     is >> asciiChar;
  174.     return this;
  175. }
  176.  
  177.  
  178. TStreamable *TReport::build()
  179. {
  180.     return new TReport( streamableInit );
  181. }
  182.  
  183.  
  184. TStreamableClass RReport( TReport::name,
  185.               TReport::build,
  186.               __DELTA(TReport)
  187.             );
  188.  
  189.  
  190. TReport::TReport(TRect& r) :
  191.  TView(r)
  192. {
  193.     asciiChar = 0;
  194. }
  195.  
  196.  
  197. void TReport::draw()
  198. {
  199.     TDrawBuffer buf;
  200.     char        color = getColor(6);
  201.     char        str[80];
  202.     ostrstream  statusStr( str, sizeof str );
  203.  
  204.     statusStr
  205.       << "  Char: " << ((asciiChar == 0) ? (char) 0x20 : (char) asciiChar)
  206.       << " Decimal: " << setw(3) << (int) asciiChar
  207.       << " Hex " << hex << setiosflags(ios::uppercase)
  208.       << setw(2) << (int) asciiChar << "     " << ends;
  209.  
  210.     buf.moveStr(0, str, color);
  211.     writeLine(0, 0, 32, 1, buf);
  212. }
  213.  
  214.  
  215. void TReport::handleEvent(TEvent& event)
  216. {
  217.     TView::handleEvent(event);
  218.     if (event.what == evBroadcast)
  219.     {
  220.         if (event.message.command == cmAsciiTableCmdBase + cmCharFocused)
  221.             {
  222.         asciiChar = event.message.infoLong;
  223.         drawView();
  224.             }
  225.         }
  226. }
  227.  
  228.  
  229. //
  230. // TAsciiChart functions
  231. //
  232.  
  233. const char * const TAsciiChart::name = "TAsciiChart";
  234.  
  235.  
  236. void TAsciiChart::write( opstream& os )
  237. {
  238.     TWindow::write( os );
  239. }
  240.  
  241.  
  242. void *TAsciiChart::read( ipstream& is )
  243. {
  244.     TWindow::read( is );
  245.     return this;
  246. }
  247.  
  248.  
  249. TStreamable *TAsciiChart::build()
  250. {
  251.     return new TAsciiChart( streamableInit );
  252. }
  253.  
  254.  
  255. TStreamableClass RAsciiChart( TAsciiChart::name,
  256.                   TAsciiChart::build,
  257.                   __DELTA(TAsciiChart)
  258.                 );
  259.  
  260.  
  261. TAsciiChart::TAsciiChart() :
  262.     TWindow(TRect(0, 0, 34, 12), "ASCII Chart", wnNoNumber),
  263.     TWindowInit( &TAsciiChart::initFrame )
  264. {
  265.     TView *control;
  266.  
  267.     flags &= ~(wfGrow | wfZoom);
  268.     palette = wpGrayWindow;
  269.  
  270.     TRect r = getExtent();
  271.     r.grow(-1, -1);
  272.     r.a.y = r.b.y - 1;
  273.     control = new TReport( r );
  274.     control->options |= ofFramed;
  275.     control->eventMask |= evBroadcast;
  276.     insert(control);
  277.  
  278.     r = getExtent();
  279.     r.grow(-1, -1);
  280.     r.b.y = r.b.y - 2;
  281.     control = new TTable( r );
  282.     control->options |= ofFramed;
  283.     control->blockCursor();
  284.     insert(control);
  285.  
  286.     control->select();
  287. }
  288.