From e5c79a43ee78192f45a9ab34873f8bb426b5f37e Mon Sep 17 00:00:00 2001 From: Juergen Spitzmueller Date: Mon, 2 Jul 2018 17:05:46 +0200 Subject: [PATCH] Attempt to reflect tabular width in workarea This is not perfect, since variable width columns will break at a certain treshold, but it is better than what we have now (no tabular width indication at all) --- src/insets/InsetTabular.cpp | 45 +++++++++++++++++++++++++++++++++++-- src/insets/InsetTabular.h | 2 +- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/src/insets/InsetTabular.cpp b/src/insets/InsetTabular.cpp index a25f4d6356..7cb486f606 100644 --- a/src/insets/InsetTabular.cpp +++ b/src/insets/InsetTabular.cpp @@ -1029,15 +1029,54 @@ int Tabular::cellHeight(idx_type cell) const } -bool Tabular::updateColumnWidths() +bool Tabular::updateColumnWidths(MetricsInfo & mi) { vector max_dwidth(ncols(), 0); + // collect max. fixed width of column + map max_pwidth; + // collect max. variable width of column + map max_width; + for(col_type c = 0; c < ncols(); ++c) for(row_type r = 0; r < nrows(); ++r) { idx_type const i = cellIndex(r, c); if (getAlignment(i) == LYX_ALIGN_DECIMAL) max_dwidth[c] = max(max_dwidth[c], cell_info[r][c].decimal_width); + if (!getPWidth(i).zero()) + max_pwidth[c] = max(max_pwidth[c], cell_info[r][c].width); + else + max_width[c] = max(max_width[c], cell_info[r][c].width); + } + + // If we have a fixed tabular width, we take this into account + int restwidth = -1; + if (!tabular_width.zero()) { + restwidth = mi.base.inPixels(tabular_width); + // Substract the fixed widths from the table width + for (auto const w : max_pwidth) + restwidth -= w.second; + } + + // If we have a fixed width, distribute the available table width + // (minus the fixed widths) to the variable-width columns + int vcolwidth = -1; + int restcols = ncols() - max_pwidth.size(); + if (restwidth > 0) + vcolwidth = restwidth / restcols; + + // Now consider that some variable width columns exceed the vcolwidth + if (vcolwidth > 0) { + bool changed = false; + for (auto const w : max_width) { + if (w.second > vcolwidth) { + --restcols; + restwidth -= w.second; + changed = true; + } } + if (changed && restwidth > 0) + vcolwidth = restwidth / restcols; + } bool update = false; // for each col get max of single col cells @@ -1050,6 +1089,8 @@ bool Tabular::updateColumnWidths() && cell_info[r][c].decimal_width != 0) new_width = max(new_width, cellInfo(i).width + max_dwidth[c] - cellInfo(i).decimal_width); + else if (getPWidth(i).zero() && vcolwidth > 0) + new_width = max(vcolwidth, max(new_width, cellInfo(i).width)); else new_width = max(new_width, cellInfo(i).width); } @@ -4006,7 +4047,7 @@ void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const tabular.setRowDescent(r, maxdes + ADD_TO_HEIGHT + bottom_space); } - tabular.updateColumnWidths(); + tabular.updateColumnWidths(mi); dim.asc = tabular.rowAscent(0) - tabular.offsetVAlignment(); dim.des = tabular.height() - dim.asc; dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH; diff --git a/src/insets/InsetTabular.h b/src/insets/InsetTabular.h index b91518112e..87114ec367 100644 --- a/src/insets/InsetTabular.h +++ b/src/insets/InsetTabular.h @@ -807,7 +807,7 @@ public: /// bool setFixedWidth(row_type r, col_type c); /// return true of update is needed - bool updateColumnWidths(); + bool updateColumnWidths(MetricsInfo & mi); /// idx_type columnSpan(idx_type cell) const; /// -- 2.39.2