home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / tvision / drwbak / hellomem.cpp
Encoding:
C/C++ Source or Header  |  1992-03-20  |  4.0 KB  |  168 lines

  1. //========================================================================
  2. //  The following example routines have been provided by the Technical
  3. //  Support staff at Borland International.  They are provided as a
  4. //  courtesy and not as part of a Borland product, and as such, are
  5. //  provided without the assurance of technical support or any specific
  6. //  guarantees.
  7. //========================================================================
  8. //  Turbo Vision - Example of using TMemo and overriding a draw member
  9. //                 function of TBackground.
  10. //
  11. //------------------------------------------------------------------------
  12. #define Uses_TKeys
  13. #define Uses_TApplication
  14. #define Uses_TEvent
  15. #define Uses_TRect
  16. #define Uses_TDialog
  17. #define Uses_TStaticText
  18. #define Uses_TButton
  19. #define Uses_TMenuBar
  20. #define Uses_TSubMenu
  21. #define Uses_TMenuItem
  22. #define Uses_TStatusLine
  23. #define Uses_TStatusItem
  24. #define Uses_TStatusDef
  25. #define Uses_TDeskTop
  26. #define Uses_TMemo
  27. #define Uses_TBackground
  28. #define Uses_TDeskTop
  29. #include <tv.h>
  30. #include <stdlib.h>
  31.  
  32. const aboutCmd = 100;
  33.  
  34. class TMyBackground : public TBackground
  35. {
  36. public:
  37.   TMyBackground(const TRect& b) : TBackground(b,' ') { };
  38.   void draw();
  39. };
  40.  
  41. void TMyBackground::draw()
  42. {
  43.   TRect rect = getClipRect();
  44.  
  45.   for(int i = rect.a.x;i < rect.b.x;i++)
  46.   {
  47.     for(int j = rect.a.y;j < rect.b.y;j++)
  48.     {
  49.       int r = random(4);
  50.       char c;
  51.  
  52.       switch(r)
  53.       {
  54.         case 0:
  55.           c = '▒';
  56.           break;
  57.         case 1:
  58.           c = '░';
  59.           break;
  60.         case 2:
  61.           c = '▓';
  62.           break;
  63.         case 3:
  64.           c = ' ';
  65.           break;
  66.       }
  67.       writeChar(i,j,c,1,1);
  68.     }
  69.   }
  70. }
  71.  
  72. class TMyDeskTop : public TDeskTop
  73. {
  74. public:
  75.   TMyDeskTop(TRect b) : TDeskTop(b), TDeskInit(TMyDeskTop::initBackground) {}
  76.   static TBackground* initBackground(TRect r)
  77.   {
  78.     return new TMyBackground(r);
  79.   }
  80. };
  81.  
  82. class THelloApp : public TApplication
  83. {
  84.  
  85. public:
  86.  
  87.     THelloApp();
  88.  
  89.     virtual void handleEvent( TEvent& event );
  90.     static TMenuBar *initMenuBar( TRect );
  91.     static TStatusLine *initStatusLine( TRect );
  92.     static TDeskTop *initDeskTop( TRect b )
  93.     {
  94.       return new TMyDeskTop(b);
  95.     }
  96.  
  97. private:
  98.  
  99.     void aboutBox();
  100. };
  101.  
  102. THelloApp::THelloApp() :
  103.     TProgInit( &THelloApp::initStatusLine,
  104.                &THelloApp::initMenuBar,
  105.                &THelloApp::initDeskTop
  106.              )
  107. {
  108. }
  109.  
  110. void THelloApp::aboutBox()
  111. {
  112.     TDialog *d = new TDialog(TRect( 15, 5, 65, 20 ), "About" );
  113.     d->insert( new TStaticText( TRect( 5,1,45,2), "\x003Sample Application" ));
  114.     d->insert( new TStaticText( TRect( 5,3,45,4), "\x003Using TMemo and "\
  115.                                                   "overriding" ));
  116.     d->insert( new TStaticText( TRect( 5,5,45,6), "\x003TBackground draw "\
  117.                                                   "function" ));
  118.     d->insert( new TMemo( TRect(5, 8, 45, 13), 0, 0, 0, 255));
  119.     deskTop->execView( d );
  120. }
  121.  
  122. void THelloApp::handleEvent( TEvent& event )
  123. {
  124.     TApplication::handleEvent( event );
  125.     if( event.what == evCommand )
  126.         {
  127.         switch( event.message.command )
  128.             {
  129.             case aboutCmd:
  130.                 aboutBox();
  131.                 clearEvent( event );
  132.                 break;
  133.             default:
  134.                 break;
  135.             }
  136.         }
  137. }
  138.  
  139. TMenuBar *THelloApp::initMenuBar( TRect r )
  140. {
  141.  
  142.     r.b.y = r.a.y+1;
  143.  
  144.     return new TMenuBar( r,
  145.       *new TSubMenu( "~H~ello", kbAltH ) +
  146.         *new TMenuItem( "~A~bout...", aboutCmd, kbAltG ) +
  147.          newLine() +
  148.         *new TMenuItem( "E~x~it", cmQuit, cmQuit, hcNoContext, "Alt-X" )
  149.         );
  150. }
  151.  
  152. TStatusLine *THelloApp::initStatusLine( TRect r )
  153. {
  154.     r.a.y = r.b.y-1;
  155.     return new TStatusLine( r,
  156.         *new TStatusDef( 0, 0xFFFF ) +
  157.             *new TStatusItem( "~Alt-X~ Exit", kbAltX, cmQuit ) +
  158.             *new TStatusItem( 0, kbF10, cmMenu )
  159.             );
  160. }
  161.  
  162. int main()
  163. {
  164.     THelloApp helloWorld;
  165.     helloWorld.run();
  166.     return 0;
  167. }
  168.