home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / bc45 / owlsrc.pak / GAUGE.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-24  |  6.0 KB  |  217 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // (C) Copyright 1993, 1994 by Borland International, All Rights Reserved
  4. //
  5. //   Implementation of TGauge, gauge user interface widget
  6. //----------------------------------------------------------------------------
  7. #include <owl/owlpch.h>
  8. #include <owl/gauge.h>
  9. #include <owl/dc.h>
  10.  
  11. DEFINE_RESPONSE_TABLE1(TGauge, TControl)
  12.   EV_WM_ERASEBKGND,
  13. END_RESPONSE_TABLE;
  14.  
  15. //
  16. // Constructor for a TGauge object
  17. //
  18. TGauge::TGauge(TWindow*        parent,
  19.                const char far* title,
  20.                int             id,
  21.                int x, int y, int w, int h,
  22.                bool            isHorizontal,
  23.                int             margin,
  24.                TModule*        module)
  25. :
  26.   TControl(parent, id, title, x, y, w, h, module)
  27. {
  28.   SetRange(0, 100);
  29.   Value = 0;
  30.   Margin = margin * GetSystemMetrics(SM_CXBORDER);
  31.   IsHorizontal = isHorizontal;
  32.   LedSpacing = 0;
  33.   LedThick = 0;
  34.   BarColor = TColor(0, 0, 255);  // default to solid blue
  35.   Attr.Style &= ~WS_TABSTOP;     // no input for us
  36. }
  37.  
  38. //
  39. // Check & set the gauge range
  40. //
  41. void
  42. TGauge::SetRange(int min, int max) 
  43. {
  44.   Min = min;
  45.   Max = max;
  46.   if (Max <= Min)
  47.     Max = Min+1;
  48. }
  49.  
  50. //
  51. // Set the value of the gauge
  52. //
  53. void
  54. TGauge::SetValue(int value)
  55. {
  56.   //
  57.   // constrain value to be in the range "Min .. Max"
  58.   //
  59.   if (value > Max)
  60.     value = Max;
  61.  
  62.   else if (value < Min)
  63.     value = Min;
  64.   
  65.   //
  66.   // Paint to new position, converting value to pixels
  67.   //
  68.   if (value != Value) {
  69.     if (HWindow) {
  70.       Invalidate(false);
  71.     }
  72.     Value = value;
  73.   }
  74. }
  75.  
  76. //
  77. // Set led parameters
  78. //
  79. void
  80. TGauge::SetLed(int spacing, int thickPercent)
  81. {
  82.   LedSpacing = spacing;
  83.   LedThick = thickPercent;
  84. }
  85.  
  86. //
  87. // Paint the border-- bevel & margin
  88. //
  89. void
  90. TGauge::PaintBorder(TDC& dc)
  91. {
  92.   int    xBorder = ::GetSystemMetrics(SM_CXBORDER);
  93.   int    yBorder = ::GetSystemMetrics(SM_CYBORDER);
  94.  
  95.   TBrush shadowBrush(::GetSysColor(COLOR_BTNSHADOW));
  96.   dc.SelectObject(shadowBrush);
  97.   dc.PatBlt(0, 0, Attr.W, yBorder);
  98.   dc.PatBlt(0, yBorder, xBorder, Attr.H-yBorder);
  99.  
  100.   TBrush hiliteBrush(::GetSysColor(COLOR_BTNHIGHLIGHT));
  101.   dc.SelectObject(hiliteBrush);
  102.   dc.PatBlt(xBorder, Attr.H-yBorder, Attr.W-xBorder, Attr.H-yBorder);
  103.   dc.PatBlt(Attr.W-xBorder, yBorder, xBorder, Attr.H-yBorder-yBorder);
  104.  
  105.   TBrush  faceBrush(::GetSysColor(COLOR_BTNFACE));
  106.   TRect   innerRect(xBorder, yBorder, Attr.W-xBorder, Attr.H-yBorder);
  107.  
  108.   //
  109.   // Walk in from the bevel painting frames as we go
  110.   //
  111.   for (int i = 0; i < Margin; i++) {
  112.     dc.FrameRect(innerRect, faceBrush);
  113.     innerRect.Inflate(-1, -1);
  114.   }
  115. }
  116.  
  117. //
  118. // Paint the whole gauge: border & graphic
  119. //
  120. void
  121. TGauge::Paint(TDC& dc, bool /*erase*/, TRect&)
  122. {
  123.   PaintBorder(dc);
  124.  
  125.   //
  126.   // Prepare to paint the bar or LED sequence in the well
  127.   //
  128.   int    xBorder = ::GetSystemMetrics(SM_CXBORDER);
  129.   int    yBorder = ::GetSystemMetrics(SM_CYBORDER);
  130.  
  131.   TBrush  barBrush(BarColor);
  132.   TBrush  faceBrush(::GetSysColor(COLOR_BTNFACE));
  133.   TRect   innerRect(xBorder+Margin, yBorder+Margin, 
  134.                    Attr.W-xBorder-Margin, Attr.H-yBorder-Margin);
  135.   //
  136.   // Draw either LEDs or a solid bar as indicated by LedSpacing
  137.   //
  138.   if (LedSpacing) {
  139.     if (IsHorizontal) {
  140.       int ledStep = (innerRect.Width()*LedSpacing)/(Max-Min);
  141.       int ledWidth = (ledStep*LedThick)/100;
  142.       int gapWidth = ledStep - ledWidth;
  143.       int x = innerRect.left;
  144.       int right = innerRect.left +
  145.                   int((long(Value-Min)*innerRect.Width())/(Max-Min));
  146.       for (; x < right; x += ledStep) {
  147.         dc.FillRect(x, innerRect.top, x+ledWidth, innerRect.bottom, barBrush);
  148.         dc.FillRect(x+ledWidth, innerRect.top, x+ledWidth+gapWidth, innerRect.bottom, faceBrush);
  149.       }
  150.       dc.FillRect(x, innerRect.top, innerRect.right, innerRect.bottom, faceBrush);
  151.     }
  152.     else {
  153.       int ledStep = int((long(innerRect.Height())*LedSpacing)/(Max-Min));
  154.       int ledHeight = int((long(ledStep)*LedThick)/100);
  155.       int gapHeight = ledStep - ledHeight;
  156.       int y = innerRect.bottom;
  157.       int top = innerRect.top + innerRect.Height() -
  158.                 int((long(Value-Min)*innerRect.Height())/(Max-Min));
  159.       for (; y > top; y -= ledStep) {
  160.         dc.FillRect(innerRect.left, y-ledHeight, innerRect.right, y, barBrush);
  161.         dc.FillRect(innerRect.left, y-ledHeight-gapHeight, innerRect.right, y-ledHeight, faceBrush);
  162.       }
  163.       dc.FillRect(innerRect.left, innerRect.top, innerRect.right, y, faceBrush);
  164.     }
  165.   }
  166.   else {
  167.     TRect barRect(innerRect);
  168.     TRect emptyRect(innerRect);
  169.     if (IsHorizontal) {
  170.       int w = int((long(Value-Min)*innerRect.Width())/(Max-Min));
  171.       barRect.right = emptyRect.left = innerRect.left+w;
  172.     }
  173.     else {
  174.       int h = innerRect.Height() -
  175.               int((long(Value-Min)*innerRect.Height())/(Max-Min));
  176.       barRect.top = emptyRect.bottom = innerRect.top+h;
  177.     }
  178.     dc.FillRect(emptyRect, faceBrush);
  179.     dc.FillRect(barRect, barBrush);
  180.     
  181.     if (Title && *Title) {
  182.       char buff[32];
  183.       wsprintf(buff, Title, Value);
  184.  
  185.       int   len = strlen(buff);
  186.       TSize extent = dc.GetTextExtent(buff, len);
  187.       int   x = innerRect.left;
  188.       int   y = innerRect.top;
  189.  
  190.       if (extent.cx < innerRect.Width())
  191.         x += (innerRect.Width() - extent.cx) / 2;  // center text horizontally
  192.  
  193.       if (extent.cy < innerRect.Height())
  194.         y += (innerRect.Height() - extent.cy) / 2; // center text vertically
  195.       
  196.       //
  197.       // use ExtTextOut() to paint the text in contrasting colors to the bar
  198.       // and background
  199.       //
  200.       dc.SetBkMode(TRANSPARENT);
  201.       dc.SetTextColor(GetSysColor(COLOR_BTNFACE));
  202.       dc.ExtTextOut(x, y, ETO_CLIPPED, &barRect, buff, strlen(buff));
  203.       dc.SetTextColor(BarColor);
  204.       dc.ExtTextOut(x, y, ETO_CLIPPED, &emptyRect, buff, strlen(buff));
  205.     }
  206.   }
  207. }
  208.  
  209. //
  210. // We'll always erase as we paint to avoid flicker
  211. //
  212. bool
  213. TGauge::EvEraseBkgnd(HDC)
  214. {
  215.   return true;
  216. }
  217.