home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tv20cpp.zip / demos / ascii.cpp next >
C/C++ Source or Header  |  1998-01-19  |  6KB  |  301 lines

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