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

  1. /*-------------------------------------------------------------------*/
  2. /*                                                                   */
  3. /*   Turbo Vision 1.0                                                */
  4. /*   Turbo Vision Demo                                               */
  5. /*   Copyright (c) 1991 by Borland International                     */
  6. /*                                                                   */
  7. /*   Gadgets.cpp:  Gadgets for the Turbo Vision Demo.  Includes a    */
  8. /*        heap view and a clock view which display the clock at the  */
  9. /*        right end of the menu bar and the current heap space at    */
  10. /*        the right end of the status line.                          */
  11. /*-------------------------------------------------------------------*/
  12.  
  13. #define Uses_TRect
  14. #define Uses_TView
  15. #define Uses_TDrawBuffer
  16. #include <tv.h>
  17.  
  18. #include <string.h>
  19. #include <stdlib.h>
  20. #include <ctype.h>
  21. #include <strstrea.h>
  22. #include <iomanip.h>
  23. #include <alloc.h>
  24. #include <time.h>
  25.  
  26. #include "gadgets.h"
  27.  
  28.  
  29. //
  30. // ------------- Heap Viewer functions
  31. //
  32.  
  33. THeapView::THeapView(TRect& r) : TView( r )
  34. {
  35.     oldMem = 0;
  36.     newMem = heapSize();
  37. }
  38.  
  39.  
  40. void THeapView::draw()
  41. {
  42.     TDrawBuffer buf;
  43.     char c = getColor(2);
  44.  
  45.     buf.moveChar(0, ' ', c, size.x);
  46.     buf.moveStr(0, heapStr, c);
  47.     writeLine(0, 0, size.x, 1, buf);
  48. }
  49.  
  50.  
  51. void THeapView::update()
  52. {
  53.     if( (newMem = heapSize()) != oldMem )
  54.         {
  55.         oldMem = newMem;
  56.         drawView();
  57.         }
  58. }
  59.  
  60.  
  61. long THeapView::heapSize()
  62. {
  63.     long total = farcoreleft();
  64.     struct farheapinfo heap;
  65.     ostrstream totalStr( heapStr, sizeof heapStr);
  66.  
  67.     switch( farheapcheck() )
  68.         {
  69.         case _HEAPEMPTY:
  70.             strcpy(heapStr, "     No heap");
  71.             total = -1;
  72.             break;
  73.  
  74.         case _HEAPCORRUPT:
  75.             strcpy(heapStr, "Heap corrupt");
  76.             total = -2;
  77.             break;
  78.  
  79.         case _HEAPOK:
  80.             heap.ptr = NULL;
  81.             while(farheapwalk(&heap) != _HEAPEND)
  82.                 if(!heap.in_use)
  83.                     total += heap.size;
  84.             totalStr << setw(12) << total << ends;
  85.             break;
  86.         }
  87.     return(total);
  88. }
  89.  
  90.  
  91. //
  92. // -------------- Clock Viewer functions
  93. //
  94.  
  95. TClockView::TClockView( TRect& r ) : TView( r )
  96. {
  97.     strcpy(lastTime, "        ");
  98.     strcpy(curTime, "        ");
  99. }
  100.  
  101.  
  102. void TClockView::draw()
  103. {
  104.     TDrawBuffer buf;
  105.     char c = getColor(2);
  106.  
  107.     buf.moveChar(0, ' ', c, size.x);
  108.     buf.moveStr(0, curTime, c);
  109.     writeLine(0, 0, size.x, 1, buf);
  110. }
  111.  
  112.  
  113. void TClockView::update()
  114. {
  115.     time_t t = time(0);
  116.     char *date = ctime(&t);
  117.  
  118.     date[19] = '\0';
  119.     strcpy(curTime, &date[11]);        /* Extract time. */
  120.  
  121.     if( strcmp(lastTime, curTime) )
  122.         {
  123.         drawView();
  124.         strcpy(lastTime, curTime);
  125.         }
  126. }
  127.  
  128.