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

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // (C) Copyright 1992, 1994 by Borland International, All Rights Reserved
  4. //
  5. //   Implementation of class TToolBox, a 2-d arrangement of TButtonGadgets.
  6. //----------------------------------------------------------------------------
  7. #include <owl/owlpch.h>
  8. #include <owl/toolbox.h>
  9. #include <owl/buttonga.h>
  10.  
  11. IMPLEMENT_CASTABLE(TToolBox);
  12.  
  13. TToolBox::TToolBox(TWindow*        parent,
  14.                    int             numColumns,
  15.                    int             numRows,
  16.                    TTileDirection  direction,
  17.                    TModule*        module)
  18. :
  19.   TGadgetWindow(parent, direction, new TGadgetWindowFont, module)
  20. {
  21.   NumRows = numRows;
  22.   NumColumns = numColumns;
  23.  
  24.   //
  25.   // make the gadget borders overlap the tool box's borders
  26.   //
  27.   Margins.Units = TMargins::BorderUnits;
  28.   Margins.Left = Margins.Right = Margins.Top = Margins.Bottom = -1;
  29.   
  30.   ShrinkWrapWidth = true;
  31. }
  32.  
  33. void
  34. TToolBox::Insert(TGadget& g, TPlacement placement, TGadget* sibling)
  35. {
  36.   TGadgetWindow::Insert(g, placement, sibling);
  37.   ((TButtonGadget&)g).SetNotchCorners(false);
  38. }
  39.  
  40. //
  41. // Swap the rows & columns count, & let our base class do the rest
  42. //
  43. void
  44. TToolBox::SetDirection(TTileDirection direction)
  45. {
  46.   if (Direction != direction) {
  47.     int t = NumRows; 
  48.     NumRows = NumColumns;
  49.     NumColumns = t;
  50.   }
  51.  
  52.   TGadgetWindow::SetDirection(direction);
  53. }
  54.  
  55. //
  56. // compute the numer of rows & columns, filling in rows OR columns if left
  57. // unspecified using AS_MANY_AS_NEEDED (but not both).
  58. //
  59. void
  60. TToolBox::ComputeNumRowsColumns(int& numRows, int& numColumns)
  61. {
  62.   CHECK(NumRows != AS_MANY_AS_NEEDED || NumColumns != AS_MANY_AS_NEEDED);
  63.   numRows = NumRows == AS_MANY_AS_NEEDED ?
  64.               (NumGadgets + NumColumns - 1) / NumColumns :
  65.               NumRows;
  66.   numColumns = NumColumns == AS_MANY_AS_NEEDED ?
  67.                  (NumGadgets + NumRows - 1) / NumRows :
  68.                  NumColumns;
  69. }
  70.  
  71. //
  72. // compute the cell size which is determined by the widest and the highest
  73. // gadget
  74. //
  75. void
  76. TToolBox::ComputeCellSize(TSize& cellSize)
  77. {
  78.   cellSize.cx = cellSize.cy = 0;
  79.  
  80.   for (TGadget* g = Gadgets; g; g = g->NextGadget()) {
  81.     TSize  desiredSize;
  82.  
  83.     g->GetDesiredSize(desiredSize);
  84.  
  85.     if (desiredSize.cx > cellSize.cx)
  86.       cellSize.cx = desiredSize.cx;
  87.  
  88.     if (desiredSize.cy > cellSize.cy)
  89.       cellSize.cy = desiredSize.cy;
  90.   }
  91. }
  92.  
  93. void
  94. TToolBox::GetDesiredSize(TSize& size)
  95. {
  96.   TSize  cellSize;
  97.   int    left, right, top, bottom;
  98.   int    numRows, numColumns;
  99.   int    cxBorder = GetSystemMetrics(SM_CXBORDER);
  100.   int    cyBorder = GetSystemMetrics(SM_CYBORDER);
  101.  
  102.   GetMargins(Margins, left, right, top, bottom);
  103.   size.cx = left + right;
  104.   size.cy = top + bottom;
  105.  
  106.   if (Attr.Style & WS_BORDER) {
  107.     size.cx += 2 * cxBorder;
  108.     size.cy += 2 * cyBorder;
  109.   }
  110.  
  111.   ComputeCellSize(cellSize);
  112.   ComputeNumRowsColumns(numRows, numColumns);
  113.  
  114.   size.cx += numColumns * cellSize.cx;
  115.   size.cy += numRows * cellSize.cy;
  116.  
  117.   //
  118.   // compensate for the gadgets overlapping
  119.   //
  120.   size.cx -= (numColumns - 1) * cxBorder;
  121.   size.cy -= (numRows - 1) * cyBorder;
  122. }
  123.  
  124. TRect
  125. TToolBox::TileGadgets()
  126. {
  127.   TSize     cellSize;
  128.   int       numRows, numColumns;
  129.   TRect     innerRect;
  130.   TRect     invalidRect;
  131.   TGadget*  g = Gadgets;
  132.   int       x, y;
  133.   int       cxBorder = GetSystemMetrics(SM_CXBORDER);
  134.   int       cyBorder = GetSystemMetrics(SM_CYBORDER);
  135.  
  136.   ComputeCellSize(cellSize);
  137.   ComputeNumRowsColumns(numRows, numColumns);
  138.   GetInnerRect(innerRect);
  139.   invalidRect.SetEmpty();
  140.  
  141.   if (Direction == Horizontal) {    // Row Major--
  142.     y = innerRect.top;
  143.  
  144.     for (int r = 0; r < numRows; r++) {
  145.       x = innerRect.left;
  146.  
  147.       for (int c = 0; c < numColumns && g; c++) {
  148.         TRect bounds(TPoint(x, y), cellSize);
  149.         TRect originalBounds(g->GetBounds());
  150.  
  151.         if (bounds != g->GetBounds()) {
  152.           g->SetBounds(bounds);
  153.  
  154.           if (invalidRect.IsNull())
  155.             invalidRect = bounds;
  156.           else
  157.             invalidRect |= bounds;
  158.  
  159.           if (originalBounds.TopLeft() != TPoint(0, 0))
  160.             invalidRect |= originalBounds;
  161.         }
  162.  
  163.         x += cellSize.cx - cxBorder;
  164.         g = g->NextGadget();
  165.       }
  166.  
  167.       y += cellSize.cy - cyBorder;
  168.     }
  169.   }
  170.   else {
  171.     x = innerRect.left;
  172.  
  173.     for (int c = 0; c < numColumns; c++) {
  174.       y = innerRect.top;
  175.  
  176.       for (int r = 0; r < numRows && g; r++) {
  177.         TRect bounds(TPoint(x, y), cellSize);
  178.         TRect originalBounds(g->GetBounds());
  179.  
  180.         if (bounds != originalBounds) {
  181.           g->SetBounds(bounds);
  182.  
  183.           if (invalidRect.IsNull())
  184.             invalidRect = bounds;
  185.           else
  186.             invalidRect |= bounds;
  187.  
  188.           if (originalBounds.TopLeft() != TPoint(0, 0))
  189.             invalidRect |= originalBounds;
  190.         }
  191.  
  192.         y += cellSize.cy - cyBorder;
  193.         g = g->NextGadget();
  194.       }
  195.  
  196.       x += cellSize.cx - cxBorder;
  197.     }
  198.   }
  199.   return invalidRect;
  200. }
  201.  
  202. void
  203. TToolBox::LayoutSession()
  204. {
  205.   TGadgetWindow::LayoutSession();
  206.   TSize sz;
  207.   GetDesiredSize(sz);
  208.   MoveWindow(0, 0, sz.cx, sz.cy, true);
  209. }
  210.  
  211.