home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / tvision / statbo / calc.cpp next >
Encoding:
C/C++ Source or Header  |  1992-01-27  |  5.8 KB  |  252 lines

  1. /*
  2.         Please excuse my "ignorance" in all of this, but this thing
  3.         is based on the CALC.CPP code in the TVision demos directory.
  4.  
  5.         I don't know what everything does (yet) so if some things are
  6.         inefficient, or not required, don't blame me - I'm still learning
  7.  
  8.         If you have questions that I might be able to answer,
  9.         leave a message to Ernest W Johnson in Borland progB under
  10.         DOS Frameworks - my CIS ID is: 70751,2567
  11. */
  12.  
  13.  
  14. //
  15. //  Not sure if I need all of these, but what the heck <grin>
  16. //
  17. #define Uses_TBackground
  18. #define Uses_TListBox
  19. #define Uses_TMenu
  20. #define Uses_TMenuBar
  21. #define Uses_TMenuItem
  22. #define Uses_TScrollBar
  23. #define Uses_TStaticText
  24. #define Uses_TStatusDef
  25. #define Uses_TStatusItem
  26. #define Uses_TStatusLine
  27. #define Uses_TStringCollection
  28. #define Uses_MsgBox
  29. #define Uses_TEventQueue
  30. #define Uses_TApplication
  31. #define Uses_TRect
  32. #define Uses_TDeskTop
  33. #define Uses_TView
  34. #define Uses_TWindow
  35. #define Uses_TDialog
  36. #define Uses_TButton
  37. #define Uses_StaticText
  38. #define Uses_TSItem
  39. #define Uses_TLabel
  40. #define Uses_TInputLine
  41. #define Uses_TEvent
  42. #define Uses_TKeys
  43. #define Uses_TDrawBuffer
  44. #define Uses_TStreamableClass
  45. #define Uses_TStreamable
  46.  
  47. #include <tv.h>
  48. __link( RView )
  49. __link( RDialog )
  50. __link( RButton )
  51.  
  52. #include <dos.h>
  53. #include <stdio.h>
  54. #include <string.h>
  55. #include <stdlib.h>
  56. #include <ctype.h>
  57. #include <strstrea.h>
  58. #include <iomanip.h>
  59. #include <conio.h>
  60.  
  61. #include "calc.h"
  62.  
  63. const cmAboutCmd   = 100;  // User selected menu item 'About'
  64. const cmStatusCmd  = 101;  // User selected menu item 'List'
  65.  
  66.  
  67. TCalcDisplay::TCalcDisplay(TRect& r) : TView ( r )
  68. {
  69.     options |= ofSelectable;
  70.     eventMask = (evKeyboard | evBroadcast);
  71.     dnfile= new char[DISPLAYLEN];
  72.     strcpy(dnfile,"0");
  73. }
  74.  
  75. TCalcDisplay::~TCalcDisplay()
  76. {
  77.    delete dnfile;
  78. }
  79.  
  80. void TCalcDisplay::handleEvent(TEvent& event)
  81. {
  82.     TView::handleEvent(event);
  83.  
  84.     switch(event.what)
  85.         {
  86.         case evKeyboard:
  87.             calcKey(event.keyDown.charScan.charCode);
  88.             clearEvent(event);
  89.             break;
  90.         case evBroadcast:
  91.             if(event.message.command == cmOK)
  92.                 {
  93.                 calcKey( ((TButton *) event.message.infoPtr)->title[0]);
  94.                 clearEvent(event);
  95.                 }
  96.             break;
  97.         }
  98. }
  99.  
  100. void TCalcDisplay::draw()
  101. {
  102.     char color = getColor(1);
  103.     int i;
  104.     TDrawBuffer nbuf;
  105.     char Buf[DISPLAYLEN+1];
  106.  
  107.     nbuf.moveChar(0,' ',color,size.x);
  108.  
  109.     sprintf(Buf," %s",dnfile);
  110.     nbuf.moveStr(0,Buf,color);
  111.  
  112.     writeLine(0, 0, size.x, 1, nbuf);
  113. }
  114.  
  115. void TCalcDisplay::calcKey(unsigned char key)
  116. {
  117.     ShowFiles("C:\\");
  118.     strcpy(dnfile,"Press any key.");
  119.     drawView();
  120.     getch();
  121.     message(owner,evCommand,cmOK,this); // close dialog box
  122. }
  123.  
  124. TCalculator::TCalculator() :
  125.     TDialog( TRect(5, 3, 69, 18), "Add Files" ),
  126.     TWindowInit( &TCalculator::initFrame )
  127. {
  128.     TView *tv;
  129.     TRect r;
  130.  
  131.     options |= ofFirstClick;
  132.     options |= ofCentered;
  133.  
  134.     r = TRect( 5, 9, 16, 11 );
  135.     tv = new TButton( r, "~G~o 4 It", cmOK, bfNormal | bfBroadcast );
  136.     tv->options &= ~ofSelectable;
  137.     insert( tv );
  138.     insert(
  139.         new TButton( TRect( 26, 9, 37, 11 ), "~A~bort", cmCancel, bfNormal ) );
  140.  
  141.     insert(
  142.         new TStaticText(TRect(2,3,15,6),"Directory:"));
  143.  
  144.     insert(new TCalcDisplay(TRect(14,3,61,4)));
  145. }
  146.  
  147. //
  148. //  The ShowFiles() function does its thing with the data "members" (?)
  149. //  and calls drawView() whenever the display needs updating
  150. //
  151. void TCalcDisplay::ShowFiles(char *directory)
  152. {
  153.     for(int x=0;x<=10;x++)
  154.     {
  155.         sprintf(dnfile,"Count %d",x);
  156.         drawView();
  157.         delay(500);
  158.     }
  159.     delay(1000);
  160.     strcpy(dnfile,"All done!");
  161.     drawView();
  162. }
  163.  
  164. class TMyApplication : public TApplication
  165. {
  166. public:
  167.     TMyApplication();
  168.     static TMenuBar *initMenuBar(TRect);
  169.     void handleEvent(TEvent &);
  170. private:
  171.     void aboutDlg();
  172.     void statusDlg();
  173. };
  174.  
  175. TMyApplication::TMyApplication() :
  176.     TProgInit(&TApplication::initStatusLine,&TMyApplication::initMenuBar,
  177.               &TApplication::initDeskTop)
  178. {
  179. }
  180.  
  181. TMenuBar *TMyApplication::initMenuBar(TRect bounds)
  182. {
  183.     bounds.b.y = bounds.a.y + 1;
  184.     return(new TMenuBar(bounds,
  185.         new TMenu(
  186.             *new TMenuItem("~A~bout",cmAboutCmd,kbAltA,hcNoContext,0,
  187.              new TMenuItem("~S~tatus Box",cmStatusCmd,kbAltL,hcNoContext,0)))));
  188. }
  189.  
  190. void TMyApplication::handleEvent(TEvent &event)
  191. {
  192.     TApplication::handleEvent(event);
  193.  
  194.     if (event.what == evCommand)
  195.     {
  196.         switch (event.message.command)
  197.         {
  198.             case cmAboutCmd:
  199.             {
  200.                 aboutDlg();
  201.                 clearEvent(event);
  202.                 break;
  203.             }
  204.             case cmStatusCmd:
  205.             {
  206.                 statusDlg();
  207.                 clearEvent(event);
  208.                 break;
  209.             }
  210.         }
  211.     }
  212. }
  213.  
  214. void TMyApplication::aboutDlg()
  215. {
  216.     TDialog *pd = new TDialog(TRect(0,0,35,12),"About");
  217.     if (pd)
  218.     {
  219.         pd->options |= ofCentered;
  220.         pd->insert(new TStaticText(TRect(1,2,34,7),
  221.                    "\003Turbo Vision Example\n\003\n"
  222.                    "\003Creating a StatusBox\n\003\n"));
  223.         pd->insert(new TButton(TRect(3,9,32,11),"~O~k",cmOK,bfDefault));
  224.  
  225.         if (validView(pd) != 0)
  226.         {
  227.             deskTop->execView(pd);
  228.  
  229.             destroy(pd);
  230.         }
  231.     }
  232. }
  233.  
  234. void TMyApplication::statusDlg()
  235. {
  236.     TCalculator *calc = (TCalculator *) validView(new TCalculator);
  237.     if(calc != 0) {
  238.         message(this,evBroadcast,cmOK,this);
  239.         deskTop->execView(calc);
  240.     }
  241. }
  242.  
  243. int main(void)
  244. {
  245.     TMyApplication myApplication;
  246.  
  247.     myApplication.run();
  248.  
  249.     return 0;
  250. }
  251.  
  252.