home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c083 / 11.ddi / OWLSRC.PAK / GAUGE.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-02  |  6.0 KB  |  214 lines

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