📄 autotablelayout.cpp
字号:
Length& width = m_layoutStruct[i].effWidth; switch (width.type()) { case Percent: havePercent = true; totalPercent += width.rawValue(); break; case Relative: haveRelative = true; totalRelative += width.value(); break; case Fixed: numFixed++; totalFixed += m_layoutStruct[i].effMaxWidth; // fall through break; case Auto: case Static: if (m_layoutStruct[i].emptyCellsOnly) numAutoEmptyCellsOnly++; else { numAuto++; totalAuto += m_layoutStruct[i].effMaxWidth; allocAuto += w; } break; default: break; } } // allocate width to percent cols if (available > 0 && havePercent) { for (int i = 0; i < nEffCols; i++) { Length &width = m_layoutStruct[i].effWidth; if (width.isPercent()) { int w = max(int(m_layoutStruct[i].effMinWidth), width.calcMinValue(tableWidth)); available += m_layoutStruct[i].calcWidth - w; m_layoutStruct[i].calcWidth = w; } } if (totalPercent > 100 * percentScaleFactor) { // remove overallocated space from the last columns int excess = tableWidth*(totalPercent - 100 * percentScaleFactor) / (100 * percentScaleFactor); for (int i = nEffCols-1; i >= 0; i--) { if (m_layoutStruct[i].effWidth.isPercent()) { int w = m_layoutStruct[i].calcWidth; int reduction = min(w, excess); // the lines below might look inconsistent, but that's the way it's handled in mozilla excess -= reduction; int newWidth = max(int (m_layoutStruct[i].effMinWidth), w - reduction); available += w - newWidth; m_layoutStruct[i].calcWidth = newWidth; } } } } // then allocate width to fixed cols if (available > 0) { for (int i = 0; i < nEffCols; ++i) { Length &width = m_layoutStruct[i].effWidth; if (width.isFixed() && width.value() > m_layoutStruct[i].calcWidth) { available += m_layoutStruct[i].calcWidth - width.value(); m_layoutStruct[i].calcWidth = width.value(); } } } // now satisfy relative if (available > 0) { for (int i = 0; i < nEffCols; i++) { Length &width = m_layoutStruct[i].effWidth; if (width.isRelative() && width.value() != 0) { // width=0* gets effMinWidth. int w = width.value() * tableWidth / totalRelative; available += m_layoutStruct[i].calcWidth - w; m_layoutStruct[i].calcWidth = w; } } } // now satisfy variable if (available > 0 && numAuto) { available += allocAuto; // this gets redistributed for (int i = 0; i < nEffCols; i++) { Length &width = m_layoutStruct[i].effWidth; if (width.isAuto() && totalAuto != 0 && !m_layoutStruct[i].emptyCellsOnly) { int w = max(m_layoutStruct[i].calcWidth, static_cast<int>(available * static_cast<float>(m_layoutStruct[i].effMaxWidth) / totalAuto)); available -= w; totalAuto -= m_layoutStruct[i].effMaxWidth; m_layoutStruct[i].calcWidth = w; } } } // spread over fixed columns if (available > 0 && numFixed) { // still have some width to spread, distribute to fixed columns for (int i = 0; i < nEffCols; i++) { Length &width = m_layoutStruct[i].effWidth; if (width.isFixed()) { int w = static_cast<int>(available * static_cast<float>(m_layoutStruct[i].effMaxWidth) / totalFixed); available -= w; totalFixed -= m_layoutStruct[i].effMaxWidth; m_layoutStruct[i].calcWidth += w; } } } // spread over percent colums if (available > 0 && m_hasPercent && totalPercent < 100 * percentScaleFactor) { // still have some width to spread, distribute weighted to percent columns for (int i = 0; i < nEffCols; i++) { Length &width = m_layoutStruct[i].effWidth; if (width.isPercent()) { int w = available * width.rawValue() / totalPercent; available -= w; totalPercent -= width.rawValue(); m_layoutStruct[i].calcWidth += w; if (!available || !totalPercent) break; } } } // spread over the rest if (available > 0 && nEffCols > numAutoEmptyCellsOnly) { int total = nEffCols - numAutoEmptyCellsOnly; // still have some width to spread int i = nEffCols; while (i--) { // variable columns with empty cells only don't get any width if (m_layoutStruct[i].effWidth.isAuto() && m_layoutStruct[i].emptyCellsOnly) continue; int w = available / total; available -= w; total--; m_layoutStruct[i].calcWidth += w; } } // if we have overallocated, reduce every cell according to the difference between desired width and minwidth // this seems to produce to the pixel exaxt results with IE. Wonder is some of this also holds for width distributing. if (available < 0) { // Need to reduce cells with the following prioritization: // (1) Auto // (2) Relative // (3) Fixed // (4) Percent // This is basically the reverse of how we grew the cells. if (available < 0) { int mw = 0; for (int i = nEffCols-1; i >= 0; i--) { Length &width = m_layoutStruct[i].effWidth; if (width.isAuto()) mw += m_layoutStruct[i].calcWidth - m_layoutStruct[i].effMinWidth; } for (int i = nEffCols-1; i >= 0 && mw > 0; i--) { Length &width = m_layoutStruct[i].effWidth; if (width.isAuto()) { int minMaxDiff = m_layoutStruct[i].calcWidth-m_layoutStruct[i].effMinWidth; int reduce = available * minMaxDiff / mw; m_layoutStruct[i].calcWidth += reduce; available -= reduce; mw -= minMaxDiff; if (available >= 0) break; } } } if (available < 0) { int mw = 0; for (int i = nEffCols-1; i >= 0; i--) { Length& width = m_layoutStruct[i].effWidth; if (width.isRelative()) mw += m_layoutStruct[i].calcWidth - m_layoutStruct[i].effMinWidth; } for (int i = nEffCols-1; i >= 0 && mw > 0; i--) { Length& width = m_layoutStruct[i].effWidth; if (width.isRelative()) { int minMaxDiff = m_layoutStruct[i].calcWidth-m_layoutStruct[i].effMinWidth; int reduce = available * minMaxDiff / mw; m_layoutStruct[i].calcWidth += reduce; available -= reduce; mw -= minMaxDiff; if (available >= 0) break; } } } if (available < 0) { int mw = 0; for (int i = nEffCols-1; i >= 0; i--) { Length& width = m_layoutStruct[i].effWidth; if (width.isFixed()) mw += m_layoutStruct[i].calcWidth - m_layoutStruct[i].effMinWidth; } for (int i = nEffCols-1; i >= 0 && mw > 0; i--) { Length& width = m_layoutStruct[i].effWidth; if (width.isFixed()) { int minMaxDiff = m_layoutStruct[i].calcWidth-m_layoutStruct[i].effMinWidth; int reduce = available * minMaxDiff / mw; m_layoutStruct[i].calcWidth += reduce; available -= reduce; mw -= minMaxDiff; if (available >= 0) break; } } } if (available < 0) { int mw = 0; for (int i = nEffCols-1; i >= 0; i--) { Length& width = m_layoutStruct[i].effWidth; if (width.isPercent()) mw += m_layoutStruct[i].calcWidth - m_layoutStruct[i].effMinWidth; } for (int i = nEffCols-1; i >= 0 && mw > 0; i--) { Length& width = m_layoutStruct[i].effWidth; if (width.isPercent()) { int minMaxDiff = m_layoutStruct[i].calcWidth-m_layoutStruct[i].effMinWidth; int reduce = available * minMaxDiff / mw; m_layoutStruct[i].calcWidth += reduce; available -= reduce; mw -= minMaxDiff; if (available >= 0) break; } } } } int pos = 0; for (int i = 0; i < nEffCols; i++) { m_table->columnPositions()[i] = pos; pos += m_layoutStruct[i].calcWidth + m_table->hBorderSpacing(); } m_table->columnPositions()[m_table->columnPositions().size() - 1] = pos;}void AutoTableLayout::calcPercentages() const{ unsigned totalPercent = 0; for (unsigned i = 0; i < m_layoutStruct.size(); i++) { if (m_layoutStruct[i].width.isPercent()) totalPercent += m_layoutStruct[i].width.rawValue(); } m_totalPercent = totalPercent / percentScaleFactor; m_percentagesDirty = false;}#undef DEBUG_LAYOUT}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -