home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / OWLSRC.PAK / UIHANDLE.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  6.7 KB  |  235 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1994, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   10.5  $
  6. //
  7. //----------------------------------------------------------------------------
  8. #include <owl/pch.h>
  9. #if !defined(OWL_UIHELPER_H)
  10. # include <owl/uihelper.h>
  11. #endif
  12. #if !defined(OWL_GDIOBJEC_H)
  13. # include <owl/gdiobjec.h>
  14. #endif
  15.  
  16. OWL_DIAGINFO;
  17.  
  18. //
  19. // Construct a UIHandle object given a frame, a style and a handle size
  20. //
  21. TUIHandle::TUIHandle(const TRect& frame, uint style, int thickness)
  22. :
  23.   Frame(frame),
  24.   Style(style),
  25.   HandleBox(thickness, thickness)
  26. {
  27. }
  28.  
  29. //
  30. //
  31. //
  32. void
  33. TUIHandle::Move(int dx, int dy)
  34. {
  35.   Frame.Offset(dx, dy);
  36. }
  37.  
  38. //
  39. //
  40. //
  41. void
  42. TUIHandle::MoveTo(int x, int y)
  43. {
  44.   Frame.Offset(x-Frame.left, y-Frame.top);
  45. }
  46.  
  47. //
  48. //
  49. //
  50. void
  51. TUIHandle::Size(int w, int h)
  52. {
  53.   Frame.right = Frame.left + w;
  54.   Frame.bottom = Frame.top + h;
  55. }
  56.  
  57. //
  58. // Calculate the outside frame rectangle
  59. //
  60. TRect
  61. TUIHandle::GetBoundingRect() const
  62. {
  63.   return (Style&HandlesOut) ? Frame.InflatedBy(HandleBox) : Frame;
  64. }
  65.  
  66. //
  67. // Compare a given point to various parts of the UIHandle and return where the
  68. // point hit.
  69. //
  70. TUIHandle::TWhere
  71. TUIHandle::HitTest(const TPoint& point) const
  72. {
  73.   // Check first to see if point is a total miss
  74.   //
  75.   TRect outsideFrame = GetBoundingRect();
  76.   if (!outsideFrame.Contains(point))
  77.     return Outside;
  78.  
  79.   // Next check to see if it is completely inside
  80.   //
  81.   TRect insideFrame = outsideFrame.InflatedBy(-HandleBox);
  82.   if (insideFrame.Contains(point))
  83.     return (Style & InsideSpecial) ? Inside : MidCenter;
  84.  
  85.   // If no grapple are used, hit must be in move area
  86.   //
  87.   if (!(Style & Grapples))
  88.     return MidCenter;
  89.  
  90.   // Determine which of the handles was hit. Calulate X and then Y parts
  91.   //
  92.   int where;
  93.   TPoint halfHandle(HandleBox.x/2, HandleBox.y/2);
  94.   if (point.x < insideFrame.left)
  95.     where = 0; //Left;
  96.   else if (point.x >= insideFrame.right)
  97.     where = 2; //Right;
  98.   else {
  99.     int center = insideFrame.left + insideFrame.Width()/2;
  100.     if (point.x >= center-halfHandle.x && point.x <= center+halfHandle.x)
  101.       where = 1; //Center;
  102.     else
  103.       return MidCenter;
  104.   }
  105.   if (point.y < insideFrame.top)
  106.     where += 0; //Top;
  107.   else if (point.y >= insideFrame.bottom)
  108.     where += 6; //Bottom;
  109.   else {
  110.     int middle = insideFrame.top + insideFrame.Height()/2;
  111.     if (point.y >= middle-halfHandle.y && point.y <= middle+halfHandle.y)
  112.       where += 3; //Middle;
  113.     else
  114.       return MidCenter;
  115.   }
  116.   return (TWhere)where;
  117. }
  118.  
  119. //
  120. // Helper function to convert a where code into a cursor shape Id
  121. //
  122. uint16
  123. TUIHandle::GetCursorId(TWhere where)
  124. {
  125.   static uint16 cursorId[] = {
  126.     32642, 32645, 32643,  // SIZENWSE, SIZENS, SIZENESW
  127.     32644, 32512, 32644,  // SIZEWE,   ARROW,  SIZEWE,
  128.     32643, 32645, 32642,  // SIZENESW, SIZENS, SIZENWSE
  129.   };
  130.   if (where == Outside)
  131.     return 0;
  132.   if (where == Inside)
  133.     return 32513;         // IBEAM
  134.   return cursorId[where];
  135. }
  136.  
  137. //
  138. // Paint this UIHandle object onto a given dc
  139. //
  140. void
  141. TUIHandle::Paint(TDC& dc) const
  142. {
  143.   TRect outerRect = GetBoundingRect();
  144.  
  145.   // Draw a frame rect outside of Frame if indicated by the style
  146.   //
  147.   if (Style & (Framed | DashFramed)) {
  148.     int oldRop2 = dc.GetROP2();
  149.     dc.SetROP2(R2_XORPEN);
  150.     TRect frame = Frame;
  151.     if (Style & Framed) {
  152.       TBrush frameBr(TColor::Black);
  153.       dc.FrameRect(frame, frameBr);
  154.     }
  155.     else {
  156.       TPen dashPen(TColor::White, 1, PS_DASH);
  157.       dc.SelectObject(dashPen);
  158.       dc.SelectStockObject(NULL_BRUSH);
  159.       dc.SetBkColor(TColor::Black);
  160.       dc.Rectangle(frame);
  161.       dc.RestorePen();
  162.     }
  163. //    dc.DrawFocusRect(Frame); // a useful dotted rect frame...
  164.     if (!(Style&HandlesOut))
  165.       outerRect.Inflate(-1, -1);
  166.     dc.SetROP2(oldRop2);
  167.   }
  168.  
  169.   // Calculate the grapple box rectangle and the midpoint handle topleft offsets
  170.   //
  171.   TPoint grappleBox = (Style & Grapples) ? HandleBox : TPoint(0, 0);
  172.   int center = outerRect.Width()/2 - grappleBox.x/2;
  173.   int middle = outerRect.Height()/2 - grappleBox.y/2;
  174.  
  175.   // Draw the 8 grapples if indicated by the style
  176.   //
  177.   if (Style & Grapples) {
  178.     const uint32 rop = DSTINVERT;
  179.     dc.PatBlt(outerRect.left, outerRect.top,
  180.               grappleBox.x, grappleBox.y, rop);
  181.     dc.PatBlt(outerRect.left+center, outerRect.top,
  182.               grappleBox.x, grappleBox.y, rop);
  183.     dc.PatBlt(outerRect.right-HandleBox.x, outerRect.top,
  184.               grappleBox.x, grappleBox.y, rop);
  185.  
  186.     dc.PatBlt(outerRect.left, outerRect.top+middle,
  187.               grappleBox.x, grappleBox.y, rop);
  188.     dc.PatBlt(outerRect.right-HandleBox.x, outerRect.top+middle,
  189.               grappleBox.x, grappleBox.y, rop);
  190.  
  191.     dc.PatBlt(outerRect.left, outerRect.bottom-HandleBox.y,
  192.               grappleBox.x, grappleBox.y, rop);
  193.     dc.PatBlt(outerRect.left+center, outerRect.bottom-HandleBox.y,
  194.               grappleBox.x, grappleBox.y, rop);
  195.     dc.PatBlt(outerRect.right-HandleBox.x, outerRect.bottom-HandleBox.y,
  196.               grappleBox.x, grappleBox.y, rop);
  197.   }
  198.  
  199.   // Draw the hatched border or whole rect if indicated by the style
  200.   //
  201.   if (Style & (HatchBorder | HatchRect)) {
  202.     const uint32 rop = PATINVERT;  // opposite color rop: 0x00A000C9L;
  203.     static THatch8x8Brush hatchBrush(THatch8x8Brush::Hatch13F1);
  204.     dc.SelectObject(hatchBrush);  //CQ set brush origin?
  205.     if (Style & HatchBorder) {
  206.       int center2 = outerRect.Width() - center - grappleBox.x;
  207.       int middle2 = outerRect.Height() - middle - grappleBox.y;
  208.  
  209.       dc.PatBlt(outerRect.left+grappleBox.x, outerRect.top,
  210.                 center-grappleBox.x, HandleBox.y, rop);
  211.       dc.PatBlt(outerRect.left+center+grappleBox.x, outerRect.top,
  212.                 center2-grappleBox.x, HandleBox.y, rop);
  213.  
  214.       dc.PatBlt(outerRect.left, outerRect.top+HandleBox.y,
  215.                 HandleBox.x, middle-HandleBox.y, rop);
  216.       dc.PatBlt(outerRect.left, outerRect.top+middle+grappleBox.y,
  217.                 HandleBox.x, middle2-HandleBox.y, rop);
  218.  
  219.       dc.PatBlt(outerRect.right-HandleBox.x, outerRect.top+HandleBox.y,
  220.                 HandleBox.x, middle-HandleBox.y, rop);
  221.       dc.PatBlt(outerRect.right-HandleBox.x, outerRect.top+middle+grappleBox.y,
  222.                 HandleBox.x, middle2-HandleBox.y, rop);
  223.  
  224.       dc.PatBlt(outerRect.left+grappleBox.x, outerRect.bottom-HandleBox.y,
  225.                 center-grappleBox.x, HandleBox.y, rop);
  226.       dc.PatBlt(outerRect.left+center+grappleBox.x, outerRect.bottom-HandleBox.y,
  227.                 center2-grappleBox.x, HandleBox.y, rop);
  228.     }
  229.     else {
  230.       dc.PatBlt(outerRect, rop);
  231.     }
  232.     dc.RestoreBrush();
  233.   }
  234. }
  235.