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

  1. /*-------------------------------------------------------------------*/
  2. /*                                                                   */
  3. /*   Turbo Vision Demo                                               */
  4. /*                                                                   */
  5. /*   Gadgets.cpp:  Gadgets for the Turbo Vision Demo.  Includes a    */
  6. /*        heap view and a clock view which display the clock at the  */
  7. /*        right end of the menu bar and the current heap space at    */
  8. /*        the right end of the status line.                          */
  9. /*                                                                   */
  10. /*-------------------------------------------------------------------*/
  11. /*
  12.  *      Turbo Vision - Version 2.0
  13.  *
  14.  *      Copyright (c) 1994 by Borland International
  15.  *      All Rights Reserved.
  16.  *
  17.  */
  18. /*
  19.  * Modified by Sergio Sigala <ssigala@globalnet.it>
  20.  */
  21.  
  22. #define Uses_TRect
  23. #define Uses_TView
  24. #define Uses_TDrawBuffer
  25. #include <tvision/tv.h>
  26.  
  27. #include <string.h>
  28. #include <stdlib.h>
  29. #include <ctype.h>
  30. #include <strstream.h>
  31. #include <iomanip.h>
  32. #include <time.h>
  33.  
  34. #include "gadgets.h"
  35.  
  36. //extern "C" unsigned long farcoreleft( void );
  37.  
  38. //
  39. // ------------- Heap Viewer functions
  40. //
  41.  
  42. THeapView::THeapView(TRect& r) : TView( r )
  43. {
  44.     oldMem = 0;
  45.     newMem = heapSize();
  46.  
  47.     /* SS: now resizing under X works well */
  48.     growMode = gfGrowLoX | gfGrowLoY | gfGrowHiX | gfGrowHiY;
  49. }
  50.  
  51.  
  52. void THeapView::draw()
  53. {
  54.     TDrawBuffer buf;
  55.     char c = getColor(2);
  56.  
  57.     buf.moveChar(0, ' ', c, (short)size.x);
  58.     buf.moveStr(0, heapStr, c);
  59.     writeLine(0, 0, (short)size.x, 1, buf);
  60. }
  61.  
  62.  
  63. void THeapView::update()
  64. {
  65.     if( (newMem = heapSize()) != oldMem )
  66.         {
  67.         oldMem = newMem;
  68.         drawView();
  69.         }
  70. }
  71.  
  72.  
  73. long THeapView::heapSize()
  74. {
  75.     /* SS: changed */
  76. #if 0
  77. //#if !defined( __DPMI32__ )
  78. //    long total = farcoreleft();
  79. //#else
  80.     long total = 0;
  81. //#endif
  82.  
  83. #if !defined( __DPMI16__ ) && !defined( __DPMI32__ )
  84.     struct farheapinfo heap;
  85. #endif
  86.  
  87.     ostrstream totalStr( heapStr, sizeof heapStr);
  88.  
  89. //#if defined( __DPMI32__ )
  90. //    switch( _HEAPEMPTY )
  91. //#else
  92.     switch( heapcheck() )
  93. //#endif
  94.         {
  95.         case _HEAPEMPTY:
  96.             strcpy(heapStr, "     No heap");
  97.             total = -1;
  98.             break;
  99.  
  100.         case _HEAPCORRUPT:
  101.             strcpy(heapStr, "Heap corrupt");
  102.             total = -2;
  103.             break;
  104.  
  105.         case _HEAPOK:
  106. #if !defined( __DPMI16__ ) && !defined( __DPMI32__ )
  107.             heap.ptr = NULL;
  108.             while(farheapwalk(&heap) != _HEAPEND)
  109.                 if(!heap.in_use)
  110.                     total += heap.size;
  111. #endif
  112.             totalStr << setw(12) << total << ends;
  113.             break;
  114.         }
  115.     return(total);
  116. #endif
  117.     strcpy(heapStr, "Hello world!");
  118.     return -1;
  119. }
  120.  
  121.  
  122. //
  123. // -------------- Clock Viewer functions
  124. //
  125.  
  126. TClockView::TClockView( TRect& r ) : TView( r )
  127. {
  128.     strcpy(lastTime, "        ");
  129.     strcpy(curTime, "        ");
  130.  
  131.     /* SS: now resizing under X works well */
  132.     growMode = gfGrowLoX | gfGrowHiX;
  133. }
  134.  
  135.  
  136. void TClockView::draw()
  137. {
  138.     TDrawBuffer buf;
  139.     char c = getColor(2);
  140.  
  141.     buf.moveChar(0, ' ', c, (short)size.x);
  142.     buf.moveStr(0, curTime, c);
  143.     writeLine(0, 0, (short)size.x, 1, buf);
  144. }
  145.  
  146.  
  147. void TClockView::update()
  148. {
  149.     time_t t = time(0);
  150.     char *date = ctime(&t);
  151.  
  152.     date[19] = '\0';
  153.     strcpy(curTime, &date[11]);        /* Extract time. */
  154.  
  155.     if( strcmp(lastTime, curTime) )
  156.         {
  157.         drawView();
  158.         strcpy(lastTime, curTime);
  159.         }
  160. }
  161.