diff options
author | Sebastian Geerken <devnull@localhost> | 2015-01-07 12:42:48 +0100 |
---|---|---|
committer | Sebastian Geerken <devnull@localhost> | 2015-01-07 12:42:48 +0100 |
commit | 584790b382f4e92355e1c06408b7852ed5e0832f (patch) | |
tree | 572cdc7ccb455081ae45ddfeb63cb3a088b6f354 /dw/table.cc | |
parent | aae7393b268407443e79e9ed2fce177fd20593b2 (diff) |
Optimized Table::calcAvailWidthForDescendant.
Diffstat (limited to 'dw/table.cc')
-rw-r--r-- | dw/table.cc | 74 |
1 files changed, 43 insertions, 31 deletions
diff --git a/dw/table.cc b/dw/table.cc index 0493c393..4cf4c196 100644 --- a/dw/table.cc +++ b/dw/table.cc @@ -266,39 +266,31 @@ int Table::calcAvailWidthForDescendant (Widget *child) assert (actualChild != NULL); - // TODO This is inefficient. (Use parentRef?) - int width = -1; - for (int row = numRows - 1; width == -1 && row >= 0; row--) { - for (int col = 0; width == -1 && col < numCols; col++) { - int n = row * numCols + col; - if (childDefined (n) && - children->get(n)->cell.widget == actualChild) { - DBG_OBJ_MSGF ("resize", 1, "calculated from column %d", col); - width = (children->get(n)->cell.colspanEff - 1) - * getStyle()->hBorderSpacing; - for (int i = 0; i < children->get(n)->cell.colspanEff; i++) - width += colWidths->get (col + i); - width = misc::max (width, 0); - - if (child != actualChild) { - // For table cells (direct children: child == actualChild), - // CSS 'width' is already regarded in the column calculation. - // However, for children of the table cells, CSS 'width' must - // be regarded here. - - int corrWidth = width; - child->calcFinalWidth (child->getStyle(), -1, this, 0, true, - &corrWidth); - - // But better not exceed it ... (TODO: Only here?) - width = misc::min (width, corrWidth); - } - } - } + // ActualChild->parentRef contains the position in the children + // array (see addCell()), so the column can be easily determined. + int col = actualChild->parentRef % numCols; + int colspanEff = children->get(actualChild->parentRef)->cell.colspanEff; + DBG_OBJ_MSGF ("resize", 1, "calculated from column %d, colspanEff = %d", + col, colspanEff); + + int width = (colspanEff - 1) * getStyle()->hBorderSpacing; + for (int i = 0; i < colspanEff; i++) + width += colWidths->get (col + i); + width = misc::max (width, 0); + + if (child != actualChild) { + // For table cells (direct children: child == actualChild), CSS + // 'width' is already regarded in the column calculation. + // However, for children of the table cells, CSS 'width' must be + // regarded here. + + int corrWidth = width; + child->calcFinalWidth (child->getStyle(), -1, this, 0, true, &corrWidth); + + // But better not exceed it ... (TODO: Only here?) + width = misc::min (width, corrWidth); } - assert (width != -1); - DBG_OBJ_MSGF ("resize", 1, "=> %d", width); DBG_OBJ_LEAVE (); return width; @@ -483,6 +475,12 @@ void Table::addCell (Widget *widget, int colspan, int rowspan) child->cell.rowspan = rowspan; children->set (curRow * numCols + curCol, child); + // The position in the children array is assigned to parentRef, + // although incremental resizing is not implemented. Useful, e. g., + // in calcAvailWidthForDescendant(). See also reallocChildren(). + widget->parentRef = curRow * numCols + curCol; + DBG_OBJ_SET_NUM_O (widget, "parentRef", widget->parentRef); + curCol += colspanEff; widget->setParent (this); @@ -717,6 +715,20 @@ void Table::reallocChildren (int newNumCols, int newNumRows) rowStyle->set (row, NULL); // Rest is increased, when needed. + if (newNumCols > numCols) { + // Re-calculate parentRef. See addCell(). + for (int row = 1; row < newNumRows; row++) + for (int col = 0; col < newNumCols; col++) { + int n = row * newNumCols + col; + Child *child = children->get (n); + if (child != NULL && child->type == Child::CELL) { + child->cell.widget->parentRef = n; + DBG_OBJ_SET_NUM_O (child->cell.widget, "parentRef", + child->cell.widget->parentRef); + } + } + } + numCols = newNumCols; numRows = newNumRows; |