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

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