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

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // (C) Copyright 1993, 1994 by Borland International, All Rights Reserved
  4. //
  5. //   Implementation of TVSlider, vertical 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 TVSlider object
  16. //
  17. TVSlider::TVSlider(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, 20, 9);
  26.   ThumbRect += TPoint((Attr.W-ThumbRect.Width())/2, 0);  // move past tics
  27.   CaretRect = TRect(3, 3, 3+14, 3+3);
  28.  
  29.   // get slot size from Attr.W set by TScrollBar ?
  30.   if (!w)
  31.     Attr.W = 32;
  32.   SlotThick = 14;
  33. }
  34.  
  35. //
  36. // Calculate and return position given a thumb upper left point
  37. // and vice versa.
  38. //
  39. int
  40. TVSlider::PointToPos(TPoint& point)
  41. {
  42.   return int(((long)point.y*(long)Range)/(Attr.H-ThumbRect.Height()) + Min);
  43. }
  44.  
  45. TPoint
  46. TVSlider::PosToPoint(int pos)
  47. {
  48.   return TPoint(ThumbRect.left,
  49.                 int(((long)(pos-Min)*(Attr.H-ThumbRect.Height()))/Range));
  50. }
  51.  
  52. //
  53. // Notify parent of a scroll event by sending a WM_VSCROLL message
  54. //
  55. void
  56. TVSlider::NotifyParent(int scrollCode, int pos)
  57. {
  58.   #if defined(BI_PLAT_WIN32)
  59.     Parent->HandleMessage(WM_VSCROLL, MAKEWPARAM(scrollCode, pos), LPARAM(HWindow));
  60.   #else
  61.     Parent->HandleMessage(WM_VSCROLL, 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. TVSlider::HitTest(TPoint& point)
  72. {
  73.   if (ThumbRgn ? ThumbRgn->Contains(point) : ThumbRect.Contains(point))
  74.     return SB_THUMBTRACK;
  75.  
  76.   if (point.x > ThumbRect.right || point.x < ThumbRect.left)
  77.     return SB_THUMBPOSITION;
  78.  
  79.   else if (point.y < ThumbRect.top)
  80.     return SB_PAGEUP;
  81.  
  82.   else if (point.y >= ThumbRect.bottom)
  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. TVSlider::PaintRuler(TDC& dc)
  94. {
  95.   int    ticW = (Attr.W-ThumbRect.Width()-2)/2;
  96.  
  97.   //
  98.   //  Clear ruler areas to bk color
  99.   //
  100.   dc.SetBkColor(BkColor);
  101.   dc.TextRect(0, 0, ticW+1, Attr.H);
  102.   dc.TextRect(Attr.W-ticW-1, 0, Attr.W, Attr.H);
  103.  
  104.   //
  105.   //  Draw bottom tic & internal tics if any, then top tic
  106.   //
  107.   TBrush highlightbrush(::GetSysColor(COLOR_BTNHIGHLIGHT));
  108.   int    margin = ThumbRect.Height()/2;
  109.   int    y;
  110.  
  111.   dc.SelectObject(highlightbrush);
  112.   dc.SetBkColor(::GetSysColor(COLOR_BTNTEXT));
  113.  
  114.   for (int i = Min; i < Max; i += TicGap) {
  115.     y = PosToPoint(i).y + margin;
  116.     dc.TextRect(0, y, ticW, y+1);
  117.     dc.PatBlt(1, y+1, ticW, 1);
  118.     dc.TextRect(Attr.W-ticW-1, y, Attr.W-1, y+1);
  119.     dc.PatBlt(Attr.W-ticW, y+1, ticW, 1);
  120.     if (!TicGap)
  121.       break;
  122.   }
  123.   y = Attr.H-margin-1;
  124.   dc.TextRect(0, y, ticW, y+1);
  125.   dc.PatBlt(1, y+1, ticW, 1);
  126.   dc.TextRect(Attr.W-ticW-1, y, Attr.W-1, y+1);
  127.   dc.PatBlt(Attr.W-ticW, y+1, ticW, 1);
  128.  
  129.   dc.RestoreBrush();
  130. }
  131.  
  132. //
  133. // Paint the slot that the thumb slides over.
  134. //
  135. void
  136. TVSlider::PaintSlot(TDC& dc)
  137. {
  138.   int    ticW = (Attr.W-ThumbRect.Width()-2)/2;
  139.   int    vmargin = ThumbRect.Height()/2;             // top & bottom margins
  140.   int    hmargin = (ThumbRect.Width()-SlotThick)/2;  // left & right margins
  141.  
  142.   //
  143.   // draw margins in background color
  144.   //
  145.   dc.SetBkColor(BkColor);
  146.   dc.TextRect(ticW+1, 0, ticW+1+ThumbRect.Width(), vmargin);              // above
  147.   dc.TextRect(ticW+1, vmargin, ticW+1+hmargin, Attr.H);                   // left
  148.   dc.TextRect(Attr.W-ticW-1-hmargin, vmargin, Attr.W-ticW-1, Attr.H);     // right
  149.   dc.TextRect(ticW+1, Attr.H-vmargin, ticW+1+ThumbRect.Width(), Attr.H);  // bottom
  150.  
  151.   //
  152.   // Draw slot frame, shadow, fill & highlight to the right
  153.   //
  154.   dc.FrameRect(ticW+1+hmargin, vmargin, ticW+1+hmargin+SlotThick-1,
  155.                Attr.H-vmargin, TBrush(::GetSysColor(COLOR_BTNTEXT)));
  156.   dc.FillRect(ticW+1+hmargin+1, vmargin+1, ticW+1+hmargin+2, Attr.H-vmargin-1,
  157.               TBrush(::GetSysColor(COLOR_BTNSHADOW)));
  158.   dc.TextRect(ticW+1+hmargin+2, vmargin+1,
  159.               ticW+1+hmargin+SlotThick-2, Attr.H-vmargin-1,
  160.               ::GetSysColor(COLOR_BTNFACE));
  161.   dc.FillRect(ticW+1+hmargin+SlotThick-1, vmargin,
  162.               ticW+1+hmargin+SlotThick, Attr.H-vmargin,
  163.               TBrush(::GetSysColor(COLOR_BTNHIGHLIGHT)));
  164. }
  165.  
  166. #endif
  167. #if !defined(SECTION) || SECTION == 2
  168.  
  169. IMPLEMENT_STREAMABLE1(TVSlider, TSlider);
  170.  
  171. void*
  172. TVSlider::Streamer::Read(ipstream& is, uint32 /* version */) const
  173. {
  174.   ReadBaseObject((TSlider*)GetObject(), is);
  175.   return GetObject();
  176. }
  177.  
  178. void
  179. TVSlider::Streamer::Write(opstream& os) const
  180. {
  181.   WriteBaseObject((TSlider*)GetObject(), os);
  182. }
  183.  
  184. #endif
  185.