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

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