home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1997 May / VPR9705A.ISO / VPR_DATA / PROGRAM / CBTRIAL / SETUP / DATA.Z / GAUGES.CPP < prev    next >
C/C++ Source or Header  |  1997-02-14  |  10KB  |  349 lines

  1. //---------------------------------------------------------------------------
  2. // Borland C++Builder
  3. // Copyright (c) 1987, 1997 Borland International Inc.  All Rights Reserved.
  4. //---------------------------------------------------------------------------
  5. #if !defined (REGISTER_ALL_CONTROLS)
  6.   #include  "gauges.h"
  7. #else
  8.   #include "source\gauges.h"
  9. #endif
  10.  
  11. #include <memory>       //for auto_ptr STL class
  12.  
  13. //#pragma resource "*.res"   //IDE links .res automatically for components
  14.  
  15. namespace Gauges{
  16. void __fastcall Register()
  17. {
  18.   TComponentClass classes[1] = {__classid(TGauge)};
  19.   RegisterComponents("Samples", classes, 0);
  20. }
  21. } //namespace
  22.  
  23.  
  24. /* TBltBitmap */
  25. __fastcall TBltBitmap::TBltBitmap(): Graphics::TBitmap(){}
  26.  
  27. void __fastcall TBltBitmap::MakeLike(Graphics::TBitmap* ATemplate)
  28. {
  29.   Width = ATemplate->Width;
  30.   Height = ATemplate->Height;
  31.   Canvas->Brush->Color = TColor(clWindowFrame);
  32.   Canvas->Brush->Style = bsSolid;
  33.   Canvas->FillRect(Rect(0, 0, Width, Height));
  34. }
  35.  
  36. /* This function solves for x in the equation "x is y% of z". */
  37. Longint SolveForX(Longint Y, Longint Z)
  38. {
  39.   return floor( Z * (Y * 0.01) );
  40. }
  41.  
  42. /* This function solves for y in the equation "x is y% of z". */
  43. Longint SolveForY(Longint X, Longint Z)
  44. {
  45.   if (Z==0)
  46.     return 0;
  47.   else
  48.     return floor( (X * 100.0) / Z );
  49. }
  50.  
  51.  
  52. /* TGauge */
  53.  
  54. __fastcall TGauge::TGauge(TComponent *AOwner)
  55.                  : TGraphicControl(AOwner) 
  56.  
  57. {
  58.   ControlStyle << csFramed << csOpaque;
  59.   /* default values */
  60.   FMinValue = 0;
  61.   FMaxValue = 100;
  62.   FCurValue = 0;
  63.   FKind = gkHorizontalBar;
  64.   FShowText = True;
  65.   FBorderStyle = bsSingle;
  66.   FForeColor = clBlack;
  67.   FBackColor = clWhite;
  68.   Width = 100;
  69.   Height = 100;
  70. }
  71.  
  72. Longint __fastcall TGauge::GetPercentDone()
  73. {
  74.   return SolveForY(FCurValue - FMinValue, FMaxValue - FMinValue);
  75. }
  76.  
  77. void __fastcall TGauge::Paint()
  78. {
  79.   std::auto_ptr<Graphics::TBitmap> TheImage(new Graphics::TBitmap());
  80.   std::auto_ptr<TBltBitmap> OverlayImage(new TBltBitmap());
  81.   TRect PaintTRect;
  82.  
  83.   TheImage->Height = Height;
  84.   TheImage->Width = Width;
  85.   PaintBackground(TheImage.get());
  86.   PaintTRect = ClientRect;
  87.   if (FBorderStyle == bsSingle)
  88.     InflateRect(&RECT(PaintTRect), -1, -1);
  89.   OverlayImage->MakeLike(TheImage.get());
  90.   PaintBackground(OverlayImage.get());
  91.   switch(FKind){
  92.     case gkText:
  93.       PaintAsNothing(OverlayImage.get(), PaintTRect);
  94.       break;
  95.     case gkHorizontalBar:
  96.     case gkVerticalBar:
  97.       PaintAsBar(OverlayImage.get(), PaintTRect);
  98.       break;
  99.     case gkPie:
  100.       PaintAsPie(OverlayImage.get(), PaintTRect);
  101.       break;
  102.     case gkNeedle:
  103.       PaintAsNeedle(OverlayImage.get(), PaintTRect);
  104.       break;
  105.   }
  106.   TheImage->Canvas->CopyMode = cmSrcInvert;
  107.   TheImage->Canvas->Draw(0, 0, OverlayImage.get());
  108.   TheImage->Canvas->CopyMode = cmSrcCopy;
  109.   if (ShowText == True)
  110.     PaintAsText(TheImage.get(), PaintTRect);
  111.   Canvas->CopyMode = cmSrcCopy;
  112.   Canvas->Draw(0, 0, TheImage.get());
  113. }
  114.  
  115. void __fastcall TGauge::PaintBackground(Graphics::TBitmap* AnImage)
  116. {
  117.   TRect  ARect;
  118.   AnImage->Canvas->CopyMode = cmBlackness;
  119.   ARect = Rect(0, 0, Width, Height);
  120.   AnImage->Canvas->CopyRect(ARect, AnImage->Canvas, ARect);
  121.   AnImage->Canvas->CopyMode = cmSrcCopy;
  122. }
  123.  
  124. void __fastcall TGauge::PaintAsText(Graphics::TBitmap* AnImage, const TRect& PaintRect)
  125. {
  126.   String  S;
  127.   Integer X, Y;
  128.   std::auto_ptr<TBltBitmap> OverRect(new TBltBitmap);
  129.  
  130.   OverRect->MakeLike(AnImage);
  131.   PaintBackground(OverRect.get());
  132.   S = AnsiString((int)PercentDone);
  133.   S += "%";
  134.   OverRect->Canvas->Brush->Style = bsClear;
  135.   OverRect->Canvas->Font = Font;
  136.   OverRect->Canvas->Font->Color = clWhite;
  137.   X = (PaintRect.Right - PaintRect.Left + 1 - OverRect->Canvas->TextWidth(S)) / 2;
  138.   Y = (PaintRect.Bottom - PaintRect.Top + 1 - OverRect->Canvas->TextHeight(S)) / 2;
  139.   OverRect->Canvas->TextRect(PaintRect, X, Y, S);
  140.   AnImage->Canvas->CopyMode = cmSrcInvert;
  141.   AnImage->Canvas->Draw(0, 0, OverRect.get());
  142. }
  143.  
  144. void __fastcall TGauge::PaintAsNothing(Graphics::TBitmap* AnImage, const TRect &PaintRect)
  145. {
  146.     AnImage->Canvas->Brush->Color = BackColor;
  147.     AnImage->Canvas->FillRect(PaintRect);
  148. }
  149.  
  150. void __fastcall TGauge::PaintAsBar(Graphics::TBitmap* AnImage, const TRect& PaintRect)
  151. {
  152.   Longint  FillSize;
  153.   Integer  W, H;
  154.  
  155.   W = PaintRect.Right - PaintRect.Left + 1;
  156.   H = PaintRect.Bottom - PaintRect.Top + 1;
  157.  
  158.   AnImage->Canvas->Brush->Color = BackColor;
  159.   AnImage->Canvas->FillRect(PaintRect);
  160.   AnImage->Canvas->Pen->Color = ForeColor;
  161.   AnImage->Canvas->Pen->Width = 1;
  162.   AnImage->Canvas->Brush->Color = ForeColor;
  163.   switch(FKind){
  164.     case gkHorizontalBar:
  165.       FillSize = SolveForX(PercentDone, W);
  166.       if (FillSize > W)
  167.          FillSize = W;
  168.       if (FillSize > 0)
  169.          AnImage->Canvas->FillRect(Rect(PaintRect.Left,
  170.                                         PaintRect.Top,
  171.                                         FillSize,
  172.                                         H));
  173.      break;
  174.    case gkVerticalBar:
  175.      FillSize = SolveForX(PercentDone, H);
  176.      if (FillSize >= H)
  177.        FillSize = H - 1;
  178.        AnImage->Canvas->FillRect(Rect(PaintRect.Left,
  179.                                       H - FillSize,
  180.                                       W,
  181.                                       H));
  182.      break;
  183.   }
  184. }
  185.  
  186. void __fastcall TGauge::PaintAsPie(Graphics::TBitmap* AnImage, const TRect& PaintRect)
  187. {
  188.   Integer  MiddleX, MiddleY;
  189.   Double  Angle;
  190.   Integer  W, H;
  191.  
  192.   W = PaintRect.Right - PaintRect.Left;
  193.   H = PaintRect.Bottom - PaintRect.Top;
  194.  
  195.   if (FBorderStyle == bsSingle){
  196.       W++;
  197.       H++;
  198.   }
  199.   AnImage->Canvas->Brush->Color = Color;
  200.   AnImage->Canvas->FillRect(PaintRect);
  201.   AnImage->Canvas->Brush->Color = BackColor;
  202.   AnImage->Canvas->Pen->Color = ForeColor;
  203.   AnImage->Canvas->Pen->Width = 1;
  204.   AnImage->Canvas->Ellipse(PaintRect.Left,
  205.                            PaintRect.Top,
  206.                            W, H);
  207.   if (PercentDone > 0) {
  208.      AnImage->Canvas->Brush->Color = ForeColor;
  209.      MiddleX = W / 2;
  210.      MiddleY = H / 2;
  211.      Angle = (Pi * ((PercentDone / 50.0) + 0.5));
  212.      AnImage->Canvas->Pie(PaintRect.Left,
  213.                           PaintRect.Top,
  214.                           W, H,
  215.                           floor(MiddleX * (1 - cos(Angle))), //should be rounded, really
  216.                           floor(MiddleY * (1 - sin(Angle))),
  217.                           MiddleX, 0);
  218.   }
  219. }
  220.  
  221. void __fastcall TGauge::PaintAsNeedle(Graphics::TBitmap* AnImage, const TRect& PaintRect)
  222. {
  223.   Integer  MiddleX;
  224.   Double   Angle;
  225.   Integer  X, Y, W, H;
  226.  
  227.   X = PaintRect.Left;
  228.   Y = PaintRect.Top;
  229.   W = PaintRect.Right - PaintRect.Left;
  230.   H = PaintRect.Bottom - PaintRect.Top;
  231.   if (FBorderStyle == bsSingle){
  232.         W++;
  233.         H++;
  234.   }
  235.  
  236.   AnImage->Canvas->Brush->Color = Color;
  237.   AnImage->Canvas->FillRect(PaintRect);
  238.   AnImage->Canvas->Brush->Color = BackColor;
  239.   AnImage->Canvas->Pen->Color = ForeColor;
  240.   AnImage->Canvas->Pen->Width = 1;
  241.   AnImage->Canvas->Pie(X, Y,
  242.                        W, H * 2 - 1,
  243.                        X + W,
  244.                        PaintRect.Bottom - 1,
  245.                        X,
  246.                        PaintRect.Bottom - 1);
  247.   AnImage->Canvas->MoveTo(X, PaintRect.Bottom);
  248.   AnImage->Canvas->LineTo(X + W, PaintRect.Bottom);
  249.   if (PercentDone > 0){
  250.     AnImage->Canvas->Pen->Color = ForeColor;
  251.     MiddleX = Width / 2;
  252.     AnImage->Canvas->MoveTo(MiddleX, PaintRect.Bottom - 1);
  253.     Angle = (Pi * ((PercentDone / 100.0)));
  254.     AnImage->Canvas->LineTo(floor(MiddleX * (1 - cos(Angle))),
  255.                             floor((PaintRect.Bottom - 1) * (1 - sin(Angle))));
  256.   }
  257. }
  258.  
  259. void __fastcall TGauge::SetGaugeKind(TGaugeKind Value)
  260. {
  261.   if (Value != FKind){
  262.       FKind = Value;
  263.       Refresh();
  264.   }
  265. }
  266.  
  267. void __fastcall TGauge::SetShowText(Boolean Value)
  268. {
  269.   if (Value != FShowText){
  270.       FShowText = Value;
  271.       Refresh();
  272.   }
  273. }
  274.  
  275. void __fastcall TGauge::SetBorderStyle(TBorderStyle Value)
  276. {
  277.   if (Value != FBorderStyle) {
  278.       FBorderStyle = Value;
  279.       Refresh();
  280.   }
  281. }
  282.  
  283. void __fastcall TGauge::SetForeColor(TColor Value)
  284. {
  285.   if (Value != FForeColor){
  286.       FForeColor = Value;
  287.       Refresh();
  288.   }
  289. }
  290.  
  291. void __fastcall TGauge::SetBackColor(TColor Value)
  292. {
  293.   if (Value != FBackColor){
  294.       FBackColor = Value;
  295.       Refresh();
  296.   }
  297. }
  298.  
  299. void __fastcall TGauge::SetMinValue(Longint Value)
  300. {
  301.   if (Value!= FMinValue){
  302.     if (Value > FMaxValue)
  303.       throw new Exception(SOutOfRange);//,  !GC various constructors for Exception
  304.                           //-MaxInt,        !object not available at this time ! !
  305.                           //FMaxValue - 1);
  306.     FMinValue = Value;
  307.     if (FCurValue < Value)
  308.       FCurValue = Value;
  309.     Refresh();
  310.   }
  311. }
  312.  
  313. void __fastcall TGauge::SetMaxValue(Longint Value)
  314. {
  315.   if (Value){
  316.     if (Value)
  317.       throw new Exception(SOutOfRange);//,
  318.                          // FMinValue + 1,  !GC various constructors for Exception
  319.                          // MaxInt);        !object not available at this time ! !
  320.     FMaxValue = Value;
  321.     if (FCurValue > Value)
  322.       FCurValue = Value;
  323.     Refresh();
  324.   }
  325. }
  326.  
  327. void __fastcall TGauge::SetProgress(Longint Value)
  328. {
  329.   Longint  TempPercent;
  330.   TempPercent = GetPercentDone();  // remember where we were
  331.   if (Value < FMinValue)
  332.     Value = FMinValue;
  333.   else if (Value > FMaxValue)
  334.     Value = FMaxValue;
  335.   if (FCurValue != Value){
  336.     FCurValue = Value;
  337.     if (TempPercent != GetPercentDone()) //only refresh if percentage changed
  338.       Refresh();
  339.   }
  340. }
  341.  
  342. void __fastcall TGauge::AddProgress(Longint Value)
  343. {
  344.   Progress = FCurValue + Value;
  345.   Refresh();
  346. }
  347.  
  348. //End of conversion...
  349.