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

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // (C) Copyright 1993, 1994 by Borland International, All Rights Reserved
  4. //
  5. //   Implementation of THSlider, horizontal slider UI widget
  6. //----------------------------------------------------------------------------
  7. #pragma hdrignore SECTION
  8. #include <owl/owlpch.h>
  9. #include <owl/slider.h>
  10. #include <owl/dc.h>
  11.  
  12. #if !defined(SECTION) || SECTION == 1
  13.  
  14. //
  15. // Constructor for a THSlider object
  16. //
  17. THSlider::THSlider(TWindow*        parent,
  18.                    int             id,
  19.                    int x, int y, int w, int h,
  20.                    TResId          thumbResId,
  21.                    TModule*        module)
  22. :
  23.   TSlider(parent, id, x, y, w, h, thumbResId, module)
  24. {
  25.   ThumbRect = TRect(0, 0, 9, 20);
  26.   CaretRect = TRect(3, 3, 3+3, 3+13);
  27.  
  28.   // get slot size from Attr.H set by TScrollBar ?
  29.   if (!h)
  30.     Attr.H = 32;
  31.  
  32.   SlotThick = 17;
  33. }
  34.  
  35. //
  36. // Calculate and return position given a thumb upper left point
  37. // and vice versa.
  38. //
  39. int
  40. THSlider::PointToPos(TPoint& point)
  41. {
  42.   return int(((long)point.x*(long)Range)/(Attr.W-ThumbRect.Width()) + Min);
  43. }
  44.  
  45. TPoint
  46. THSlider::PosToPoint(int pos)
  47. {
  48.   return TPoint(int(((long)(pos-Min)*(Attr.W-ThumbRect.Width()))/Range),
  49.                 ThumbRect.top);
  50. }
  51.  
  52. //
  53. // Notify parent of a scroll event by sending a WM_HSCROLL message
  54. //
  55. void
  56. THSlider::NotifyParent(int scrollCode, int pos)
  57. {
  58.   #if defined(BI_PLAT_WIN32)
  59.     Parent->HandleMessage(WM_HSCROLL, MAKEWPARAM(scrollCode, pos), LPARAM(HWindow));
  60.   #else
  61.     Parent->HandleMessage(WM_HSCROLL, scrollCode, MAKELPARAM(pos, HWindow));
  62.   #endif
  63. }
  64.  
  65. //
  66. // Determines if a point is within the thumb, or other hot areas of the
  67. // slider. Uses region if available, else uses thumb bounding rect.
  68. // Returns -1 if no hit.
  69. //
  70. int
  71. THSlider::HitTest(TPoint& point)
  72. {
  73.   if (ThumbRgn ? ThumbRgn->Contains(point) : ThumbRect.Contains(point))
  74.     return SB_THUMBTRACK;
  75.  
  76.   if (point.y > ThumbRect.bottom)
  77.     return SB_THUMBPOSITION;
  78.  
  79.   else if (point.x < ThumbRect.left)
  80.     return SB_PAGEUP;
  81.  
  82.   else if (point.x >= ThumbRect.right)
  83.     return SB_PAGEDOWN;
  84.  
  85.   return -1;
  86. }
  87.  
  88. //
  89. // Paint the ruler. The ruler doesn't overlap with the thumb or slot.
  90. // SysColors for text fg or bg are never dithered & can use TextRect.
  91. //
  92. void
  93. THSlider::PaintRuler(TDC& dc)
  94. {
  95.   //  Clear ruler area to bk color
  96.   //
  97.   dc.TextRect(0, ThumbRect.Height(), Attr.W, Attr.H, BkColor);
  98.  
  99.   //  Draw left tic & internal tics if any, then right tic
  100.   //
  101.   TBrush highlightbrush(::GetSysColor(COLOR_BTNHIGHLIGHT));
  102.   int    margin = ThumbRect.Width()/2;
  103.   int    rulerY = ThumbRect.Height()+1;
  104.   int    x;
  105.  
  106.   dc.SelectObject(highlightbrush);
  107.   dc.SetBkColor(::GetSysColor(COLOR_BTNTEXT));
  108.  
  109.   for (int i = Min; i < Max; i += TicGap) {
  110.     x = PosToPoint(i).x + margin;
  111.     dc.TextRect(x, rulerY, x+1, rulerY+5);
  112.     dc.PatBlt(x+1, rulerY, 1, 5);
  113.     if (!TicGap)
  114.       break;
  115.   }
  116.   x = Attr.W-margin-1;
  117.   dc.TextRect(x, rulerY, x+1, rulerY+5);
  118.   dc.PatBlt(x+1, rulerY, 1, 6);
  119.  
  120.   //  Draw ruler bottom
  121.   //
  122.   dc.TextRect(margin, rulerY+5, Attr.W-margin, rulerY+6);
  123.   dc.PatBlt(margin, rulerY+6, Attr.W-2*margin+1, 1);
  124.  
  125.   dc.RestoreBrush();
  126. }
  127.  
  128. //
  129. // Paint the slot that the thumb slides over.
  130. //
  131. void
  132. THSlider::PaintSlot(TDC& dc)
  133. {
  134.   int    hmargin = ThumbRect.Width()/2;                   // left & right margins
  135.   int    vmargin = (ThumbRect.Height()-SlotThick+1)/2+1;  // top & bottom
  136.  
  137.   //
  138.   // draw margins in background color
  139.   //
  140.   dc.SetBkColor(BkColor);
  141.   dc.TextRect(0, 0, Attr.W, vmargin);                               // above
  142.   dc.TextRect(0, vmargin, hmargin, vmargin+SlotThick);              // left
  143.   dc.TextRect(Attr.W-hmargin, vmargin, Attr.W, vmargin+SlotThick);  // right
  144.   dc.TextRect(0, SlotThick, Attr.W, SlotThick+vmargin);             // bottom
  145.  
  146.   //
  147.   // Draw slot frame, shadow, fill & highlight below
  148.   //
  149.   dc.TextRect(hmargin, vmargin, Attr.W-hmargin, SlotThick-1,
  150.               ::GetSysColor(COLOR_BTNTEXT));
  151.   dc.FillRect(hmargin+1, vmargin+1, Attr.W-hmargin-1, vmargin+2,
  152.               TBrush(::GetSysColor(COLOR_BTNSHADOW)));
  153.   dc.TextRect(hmargin+1, vmargin+2, Attr.W-hmargin-1, SlotThick-2,
  154.               ::GetSysColor(COLOR_BTNFACE));
  155.   dc.FillRect(hmargin, SlotThick-1, Attr.W-hmargin, SlotThick,
  156.               TBrush(::GetSysColor(COLOR_BTNHIGHLIGHT)));
  157.   dc.RestoreBrush();
  158. }
  159.  
  160. #endif
  161. #if !defined(SECTION) || SECTION == 2
  162.  
  163. IMPLEMENT_STREAMABLE1(THSlider, TSlider);
  164.  
  165. void*
  166. THSlider::Streamer::Read(ipstream& is, uint32 /* version */) const
  167. {
  168.   ReadBaseObject((TSlider*)GetObject(), is);
  169.   return GetObject();
  170. }
  171.  
  172. void
  173. THSlider::Streamer::Write(opstream& os) const
  174. {
  175.   WriteBaseObject((TSlider*)GetObject(), os);
  176. }
  177.  
  178. #endif
  179.