home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1997 March / VPR9703A.ISO / VPR_DATA / DOGA / SOURCES / POLYEDIT.LZH / HSLIDER.CPP next >
C/C++ Source or Header  |  1996-05-22  |  5KB  |  176 lines

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