home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 May / Chip_2002-05_cd1.bin / chplus / cpp / 5 / Komponety.exe / cgauges.cpp < prev    next >
C/C++ Source or Header  |  1998-02-09  |  10KB  |  346 lines

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