home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / tvision / timed / demo.cpp next >
Encoding:
C/C++ Source or Header  |  1992-04-20  |  3.6 KB  |  155 lines

  1. // mmwabout.cpp             ABOUT BOX WITH TIMER
  2.  
  3. #define Uses_TKeys
  4. #define Uses_TMenuBar
  5. #define Uses_TSubMenu
  6. #define Uses_TMenuItem
  7. #define Uses_TEvent
  8. #define Uses_TDialog
  9. #define Uses_TLabel
  10. #define Uses_TEvent
  11. #define Uses_TButton
  12. #define Uses_TApplication
  13. #define Uses_TDeskTop
  14. #define Uses_TProgram
  15.  
  16. #include <tv.h>
  17. #include <time.h>
  18. const int cmAbout = 1000;
  19. class TMYAPP :  public TApplication
  20. {
  21.     public:
  22.         TMYAPP();
  23.         static TMenuBar* initMenuBar(TRect r);
  24.         virtual void handleEvent(TEvent& event);
  25.         void About();
  26. };
  27.  
  28. class TTimedBox : public TDialog
  29. {
  30. public:
  31.     TTimedBox(TRect &trect,char *header, char *message, int ttime);
  32.     virtual void handleEvent(TEvent &event);
  33. protected:
  34.     TStaticText *text;
  35.     TButton            *ok;
  36.     time_t first, second;
  37.     int delaytime, hcnt;
  38. };
  39.  
  40. //*-------------------------------------------------------------------------*/
  41. int main()
  42. {
  43.  
  44.     TMYAPP *tapp = new TMYAPP();
  45.  
  46.     tapp->run();
  47.  
  48.     return 0;
  49.  
  50. }
  51.  
  52. TTimedBox::TTimedBox(TRect &trect,char *header,char *message,int ttime) :
  53.         TDialog(trect, header),
  54.         TWindowInit(&TTimedBox::initFrame)
  55. {
  56.         //*** insert message
  57.         TRect temp;
  58.         temp.a.x = trect.a.x+2;
  59.         temp.a.y = trect.a.y+2;
  60.         temp.b.x = trect.b.x-2;
  61.         temp.b.y = trect.b.y-2;
  62.  
  63.         text = new TStaticText(temp, message);
  64.         insert(text);
  65.  
  66.         //** figure out correct centering for button
  67.         temp.a.y = trect.b.y - 3;
  68.         temp.a.x = trect.b.x / 2 - 6;
  69.         temp.b.y = temp.a.y +2;
  70.         temp.b.x = temp.a.x + 12;
  71.         TButton *tb = new TButton(temp, " OK", cmCancel, bfDefault);
  72.      insert(tb);
  73.      options |= ofCentered;
  74.  
  75.      delaytime = ttime;
  76.      first = time(NULL);
  77.      hcnt = 0;
  78. }
  79.  
  80. /*------------------------------------------------------------------------*/
  81. //*** here is where you can trap the events and cancel yourself
  82.  
  83. void TTimedBox::handleEvent(TEvent &event)
  84. {
  85.  
  86.      TDialog::handleEvent(event);
  87.  
  88.         second = time(NULL);   //*** get second time for compare
  89.  
  90.             if ((difftime(second,first) > delaytime) &&     (hcnt == 0) )
  91.             {
  92.                 // when time is up send cancel to remove ourself
  93.                 event.what = evCommand;
  94.                 event.message.command = cmCancel;
  95.                 event.message.infoPtr = 0;
  96.                 putEvent(event);
  97.                 //hcnt was necessary to prevent more than one putEvent from occurring
  98.                 hcnt++;
  99.         }
  100.  //*** put other handleEvent code here.
  101. }
  102.  
  103.  
  104. //*-------------------------------------------------------------------------*/
  105. TMYAPP::TMYAPP() :
  106.     TProgInit ( &TMYAPP::initStatusLine,
  107.             &TMYAPP::initMenuBar,
  108.             &TMYAPP::initDeskTop )
  109. {
  110. }
  111. //------------------------------------------------------------------------
  112. void TMYAPP::About()
  113. {
  114.         int timer = 5;   //** set delay to 5 seconds
  115. //*** create timed dialog box
  116.     TTimedBox *aboutBox = new TTimedBox(TRect(0, 0, 32, 12), "About",
  117.                                                                     "\003Widgit Wonders\n"
  118.                                                                     "\003(C) Copyright 1992\n"
  119.                                                                     "\003All Rights Reserved\n"
  120.                                                                     "\003Widgit Wonders Corp.\n"
  121.                                                                     "\0035 second box demo\n"
  122.                                                                     "\003 Version 0.1", timer);
  123.  
  124.      deskTop->execView(aboutBox);
  125.      destroy( aboutBox );
  126. }
  127.  
  128. void TMYAPP::handleEvent(TEvent& event)
  129. {
  130.     TApplication::handleEvent(event); // act like base!
  131.     if( event.what == evCommand )
  132.     {
  133.         switch( event.message.command )
  134.         {
  135.             case cmAbout:
  136.                 About();
  137.                 break;
  138.  
  139.             default:
  140.                 return;
  141.         }
  142.         clearEvent( event );       // clear event after handling
  143.     }
  144. }
  145. TMenuBar* TMYAPP::initMenuBar(TRect r)
  146. {
  147.     r.b.y = r.a.y + 1;
  148.  
  149.      TSubMenu& sub1 =
  150.         *new TSubMenu("~A~bout", 0) +
  151.             *new TMenuItem( "A~b~out", cmAbout, kbNoKey);
  152.  
  153.     return (new TMenuBar( r, sub1 ));
  154. }
  155.