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

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