return "parbox";
case Tabular::BOX_MINIPAGE:
return "minipage";
+ case Tabular::BOX_VARWIDTH:
+ return "varwidth";
}
return string();
}
num = Tabular::BOX_PARBOX;
else if (str == "minipage")
num = Tabular::BOX_MINIPAGE;
+ else if (str == "varwidth")
+ num = Tabular::BOX_VARWIDTH;
else
return false;
return true;
// reset column and row of cells and update their width and alignment
for (row_type row = 0; row < nrows(); ++row)
for (col_type column = 0; column < ncols(); ++column) {
- if (isPartOfMultiColumn(row, column))
+ if (isPartOfMultiColumn(row, column)) {
+ cell_info[row][column].inset->toggleMultiCol(true);
continue;
+ }
+ cell_info[row][column].inset->toggleMultiCol(false);
// columnofcell needs to be called before setting width and aligment
// multirow cells inherit the width from the column width
if (!isPartOfMultiRow(row, column)) {
rowofcell[i] = row;
}
setFixedWidth(row, column);
- if (isPartOfMultiRow(row, column))
+ if (isPartOfMultiRow(row, column)) {
+ cell_info[row][column].inset->toggleMultiRow(true);
continue;
+ }
+ cell_info[row][column].inset->toggleMultiRow(false);
cell_info[row][column].inset->setContentAlignment(
getAlignment(cellIndex(row, column)));
++i;
}
-bool Tabular::updateColumnWidths()
+bool Tabular::updateColumnWidths(MetricsInfo & mi)
{
vector<int> max_dwidth(ncols(), 0);
+ // collect max. fixed width of column
+ map<col_type, int> max_pwidth;
+ // collect max. variable width of column
+ map<col_type, int> 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 if (!column_info[c].varwidth)
+ max_width[c] = max(max_width[c], cell_info[r][c].width);
+ }
+
+ // If we have a fixed tabular width, we take this into account
+ Length tab_width = tabular_width;
+ bool const tabularx = hasVarwidthColumn();
+ if (tabularx && tab_width.zero())
+ // If no tabular width is specified with X columns,
+ // we use 100% colwidth
+ tab_width = Length(100, Length::PCW);
+ int restwidth = -1;
+ if (!tab_width.zero()) {
+ restwidth = mi.base.inPixels(tab_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 (tabularx || 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
&& cell_info[r][c].decimal_width != 0)
new_width = max(new_width, cellInfo(i).width
+ max_dwidth[c] - cellInfo(i).decimal_width);
- else
+ else if (getPWidth(i).zero() && vcolwidth > 0) {
+ if (tabularx && !column_info[c].varwidth)
+ new_width = max(new_width, cellInfo(i).width);
+ else if (tabularx)
+ new_width = vcolwidth;
+ else
+ new_width = max(vcolwidth, max(new_width, cellInfo(i).width));
+ } else
new_width = max(new_width, cellInfo(i).width);
}
}
if (column_info[c].width != new_width) {
column_info[c].width = new_width;
- update = true;
+ // Do not trigger update when no space is left for variable
+ // columns, as this will loop
+ update = tab_width.zero() || restwidth > 0;
}
}
// update col widths to fit merged cells
if (cellInfo(i).width > old_width) {
column_info[c + span - 1].width += cellInfo(i).width - old_width;
- update = true;
+ // Do not trigger update when no space is left for variable
+ // columns, as this will loop
+ update = tab_width.zero() || restwidth > 0;
}
}
namespace {
/**
- * Allow line and paragraph breaks for fixed width cells or disallow them,
- * merge cell paragraphs and reset layout to standard for variable width
- * cells.
+ * Allow line and paragraph breaks for fixed width multicol/multirow cells
+ * or disallow them, merge cell paragraphs and reset layout to standard
+ * for variable width multicol cells.
*/
-void toggleFixedWidth(Cursor & cur, InsetTableCell * inset, bool fixedWidth)
+void toggleFixedWidth(Cursor & cur, InsetTableCell * inset,
+ bool const fixedWidth, bool const multicol,
+ bool const multirow)
{
inset->toggleFixedWidth(fixedWidth);
- if (fixedWidth)
+ if (!multirow && (fixedWidth || !multicol))
return;
// merge all paragraphs to one
while (inset->paragraphs().size() > 1)
mergeParagraph(bp, inset->paragraphs(), 0);
+ // This is relevant for multirows
+ if (fixedWidth)
+ return;
+
+ // remove newlines
+ ParagraphList::iterator pit = inset->paragraphs().begin();
+ for (; pit != inset->paragraphs().end(); ++pit) {
+ for (pos_type j = 0; j != pit->size(); ++j) {
+ if (pit->isNewline(j))
+ pit->eraseChar(j, bp.track_changes);
+ }
+ }
+
// reset layout
cur.push(*inset);
// undo information has already been recorded
idx_type const cidx = cellIndex(r, c);
// because of multicolumns
toggleFixedWidth(cur, cellInset(cidx).get(),
- !getPWidth(cidx).zero());
+ !getPWidth(cidx).zero(), isMultiColumn(cidx),
+ isMultiRow(cidx));
if (isMultiRow(cidx))
setAlignment(cidx, LYX_ALIGN_LEFT, false);
}
- // cur paragraph can become invalid after paragraphs were merged
- if (cur.pit() > cur.lastpit())
- cur.pit() = cur.lastpit();
- // cur position can become invalid after newlines were removed
- if (cur.pos() > cur.lastpos())
- cur.pos() = cur.lastpos();
+ // cur can become invalid after paragraphs were merged
+ cur.fixIfBroken();
}
return false;
cellInfo(cell).p_width = width;
- toggleFixedWidth(cur, cellInset(cell).get(), !width.zero());
- // cur paragraph can become invalid after paragraphs were merged
- if (cur.pit() > cur.lastpit())
- cur.pit() = cur.lastpit();
- // cur position can become invalid after newlines were removed
- if (cur.pos() > cur.lastpos())
- cur.pos() = cur.lastpos();
+ toggleFixedWidth(cur, cellInset(cell).get(), !width.zero(),
+ isMultiColumn(cell), isMultiRow(cell));
+ // cur can become invalid after paragraphs were merged
+ cur.fixIfBroken();
return true;
}
}
+bool Tabular::isVTypeColumn(col_type c) const
+{
+ for (row_type r = 0; r < nrows(); ++r) {
+ idx_type idx = cellIndex(r, c);
+ if (getRotateCell(idx) == 0 && useBox(idx) == BOX_VARWIDTH)
+ return true;
+ }
+ return false;
+}
+
Tabular::CellData const & Tabular::cellInfo(idx_type cell) const
{
}
-Tabular::idx_type Tabular::setMultiColumn(idx_type cell, idx_type number,
+Tabular::idx_type Tabular::setMultiColumn(Cursor & cur, idx_type cell, idx_type number,
bool const right_border)
{
idx_type const col = cellColumn(cell);
if (column_info[col].alignment != LYX_ALIGN_DECIMAL)
cs.alignment = column_info[col].alignment;
setRightLine(cell, right_border);
+ // non-fixed width multicolumns cannot have multiple paragraphs
+ if (getPWidth(cell).zero()) {
+ toggleFixedWidth(cur, cellInset(cell).get(),
+ !getPWidth(cell).zero(), isMultiColumn(cell),
+ isMultiRow(cell));
+ // cur can become invalid after paragraphs were merged
+ cur.fixIfBroken();
+ }
idx_type lastcell = cellIndex(row, col + number - 1);
for (idx_type i = 1; i < lastcell - cell + 1; ++i) {
return false;
}
-Tabular::idx_type Tabular::setMultiRow(idx_type cell, idx_type number,
+Tabular::idx_type Tabular::setMultiRow(Cursor & cur, idx_type cell, idx_type number,
bool const bottom_border,
LyXAlignment const halign)
{
else
cs.alignment = LYX_ALIGN_LEFT;
+ // Multirows cannot have multiple paragraphs
+ if (getPWidth(cell).zero()) {
+ toggleFixedWidth(cur, cellInset(cell).get(),
+ !getPWidth(cell).zero(),
+ isMultiColumn(cell), isMultiRow(cell));
+ // cur can become invalid after paragraphs were merged
+ cur.fixIfBroken();
+ }
+
// set the bottom line of the last selected cell
setBottomLine(cell, bottom_border);
}
-// FIXME: Remove this routine because we cannot insert \parboxes when the user
-// adds line breaks, see bug 4886.
Tabular::BoxType Tabular::getUsebox(idx_type cell) const
{
- if ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
- (isMultiColumn(cell) && !cellInfo(cell).p_width.zero()))
+ if (getRotateCell(cell) == 0
+ && ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
+ (isMultiColumn(cell) && !cellInfo(cell).p_width.zero())))
return BOX_NONE;
if (cellInfo(cell).usebox > 1)
return cellInfo(cell).usebox;
- return useParbox(cell);
+ return useBox(cell);
}
}
-Tabular::idx_type Tabular::setLTCaption(row_type row, bool what)
+Tabular::idx_type Tabular::setLTCaption(Cursor & cur, row_type row, bool what)
{
idx_type i = getFirstCellInRow(row);
if (what) {
- setMultiColumn(i, numberOfCellsInRow(row), false);
+ setMultiColumn(cur, i, numberOfCellsInRow(row), false);
setTopLine(i, false);
setBottomLine(i, false);
setLeftLine(i, false);
}
os << "]{" << from_ascii(getPWidth(cell).asLatexString())
<< "}\n";
+ } else if (getRotateCell(cell) != 0 && getUsebox(cell) == BOX_VARWIDTH) {
+ os << "\\begin{varwidth}[";
+ switch (valign) {
+ case LYX_VALIGN_TOP:
+ os << 't';
+ break;
+ case LYX_VALIGN_MIDDLE:
+ os << 'm';
+ break;
+ case LYX_VALIGN_BOTTOM:
+ os << 'b';
+ break;
}
+ os << "]{\\linewidth}\n";
+}
}
os << '}';
else if (getUsebox(cell) == BOX_MINIPAGE)
os << breakln << "\\end{minipage}";
+ else if (getRotateCell(cell) != 0 && getUsebox(cell) == BOX_VARWIDTH)
+ os << breakln << "\\end{varwidth}";
if (getRotateCell(cell) != 0)
os << breakln << "\\end{turn}";
if (ismultirow)
break;
}
os << 'X';
+ } else if (isVTypeColumn(c)) {
+ switch (column_info[c].alignment) {
+ case LYX_ALIGN_LEFT:
+ os << ">{\\raggedright}";
+ break;
+ case LYX_ALIGN_RIGHT:
+ os << ">{\\raggedleft}";
+ break;
+ case LYX_ALIGN_CENTER:
+ os << ">{\\centering}";
+ break;
+ case LYX_ALIGN_NONE:
+ case LYX_ALIGN_BLOCK:
+ case LYX_ALIGN_LAYOUT:
+ case LYX_ALIGN_SPECIAL:
+ case LYX_ALIGN_DECIMAL:
+ break;
+ }
+ os << "V{\\linewidth}";
} else {
switch (column_info[c].alignment) {
case LYX_ALIGN_LEFT:
for (idx_type cell = 0; cell < numberofcells; ++cell) {
if (isMultiRow(cell))
features.require("multirow");
+ if (getUsebox(cell) == BOX_VARWIDTH)
+ features.require("varwidth");
if (getVAlignment(cell) != LYX_VALIGN_TOP
- || !getPWidth(cell).zero())
+ || !getPWidth(cell).zero()
+ || isVTypeColumn(cellColumn(cell)))
features.require("array");
// Tell footnote that we need a savenote
// environment in non-long tables or
}
-Tabular::BoxType Tabular::useParbox(idx_type cell) const
+Tabular::BoxType Tabular::useBox(idx_type cell) const
{
ParagraphList const & parlist = cellInset(cell)->paragraphs();
+ if (parlist.size() > 1)
+ return BOX_VARWIDTH;
+
ParagraphList::const_iterator cit = parlist.begin();
ParagraphList::const_iterator end = parlist.end();
for (; cit != end; ++cit)
for (int i = 0; i < cit->size(); ++i)
- if (cit->isNewline(i))
- return BOX_PARBOX;
+ if (cit->isNewline(i) || cit->layout().isEnvironment())
+ return BOX_VARWIDTH;
return BOX_NONE;
}
InsetTableCell::InsetTableCell(Buffer * buf)
: InsetText(buf, InsetText::PlainLayout), isFixedWidth(false),
- contentAlign(LYX_ALIGN_CENTER)
+ isMultiColumn(false), isMultiRow(false), contentAlign(LYX_ALIGN_CENTER)
{}
bool InsetTableCell::forcePlainLayout(idx_type) const
{
- return !isFixedWidth;
+ return isMultiRow || (isMultiColumn && !isFixedWidth);
}
}
+void InsetTableCell::metrics(MetricsInfo & mi, Dimension & dim) const
+{
+ TextMetrics & tm = mi.base.bv->textMetrics(&text());
+
+ // Hand font through to contained lyxtext:
+ tm.font_.fontInfo() = mi.base.font;
+ mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
+
+ // This can happen when a layout has a left and right margin,
+ // and the view is made very narrow. We can't do better than
+ // to draw it partly out of view (bug 5890).
+ if (mi.base.textwidth < 1)
+ mi.base.textwidth = 1;
+
+ // We tell metrics here not to expand on multiple pars
+ // This is the difference to InsetText::Metrics
+ if (hasFixedWidth())
+ tm.metrics(mi, dim, mi.base.textwidth, false);
+ else
+ tm.metrics(mi, dim, 0, false);
+ mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
+ dim.asc += TEXT_TO_INSET_OFFSET;
+ dim.des += TEXT_TO_INSET_OFFSET;
+ dim.wid += 2 * TEXT_TO_INSET_OFFSET;
+}
+
+
/////////////////////////////////////////////////////////////////////
//
Length const p_width = tabular.getPWidth(cell);
if (!p_width.zero())
m.base.textwidth = mi.base.inPixels(p_width);
+ else if (tabular.column_info[c].varwidth)
+ m.base.textwidth = tabular.column_info[c].width;
tabular.cellInset(cell)->metrics(m, dim0);
- if (!p_width.zero())
+ if (!p_width.zero() || tabular.column_info[c].varwidth)
dim0.wid = m.base.textwidth;
tabular.cellInfo(cell).width = dim0.wid + 2 * WIDTH_OF_LINE
+ tabular.interColumnSpace(cell);
tabular.setRowDescent(r, maxdes + ADD_TO_HEIGHT + bottom_space);
}
- tabular.updateColumnWidths();
+ // We need to recalculate the metrics after column width calculation
+ // with xtabular (possibly multiple times, so the call is recursive).
+ if (tabular.updateColumnWidths(mi) && tabular.hasVarwidthColumn())
+ metrics(mi, dim);
dim.asc = tabular.rowAscent(0) - tabular.offsetVAlignment();
dim.des = tabular.height() - dim.asc;
dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
}
-bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col)
- const
+bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col) const
{
if (&cur.inset() == this && cur.selection()) {
if (cur.selIsMultiCell()) {
}
+namespace {
+
+void tabline(PainterInfo const & pi, int x1, int y1, int x2, int y2,
+ bool drawline, bool heavy = false)
+{
+ ColorCode const col = drawline ? Color_tabularline : Color_tabularonoffline;
+ pi.pain.line(x1, y1, x2, y2, pi.textColor(col),
+ drawline ? Painter::line_solid : Painter::line_onoffdash,
+ (heavy ? 2 : 1) * Painter::thin_line);
+}
+
+}
+
+
void InsetTabular::drawCellLines(PainterInfo & pi, int x, int y,
row_type row, idx_type cell) const
{
y -= tabular.rowAscent(row);
int const w = tabular.cellWidth(cell);
int const h = tabular.cellHeight(cell);
- Color const linecolor = pi.textColor(Color_tabularline);
- Color const gridcolor = pi.textColor(Color_tabularonoffline);
// Top
bool drawline = tabular.topLine(cell)
|| (row > 0 && tabular.bottomLine(tabular.cellAbove(cell)));
- pi.pain.line(x, y, x + w, y,
- drawline ? linecolor : gridcolor,
- drawline ? Painter::line_solid : Painter::line_onoffdash);
+ bool heavy = tabular.use_booktabs && row == 0 && tabular.rowTopLine(row);
+ tabline(pi, x, y, x + w, y, drawline, heavy);
// Bottom
drawline = tabular.bottomLine(cell);
- pi.pain.line(x, y + h, x + w, y + h,
- drawline ? linecolor : gridcolor,
- drawline ? Painter::line_solid : Painter::line_onoffdash);
+ heavy = tabular.use_booktabs && row == tabular.nrows() - 1
+ && tabular.rowBottomLine(row);
+ tabline(pi, x, y + h, x + w, y + h, drawline, heavy);
// Left
col_type const col = tabular.cellColumn(cell);
drawline = tabular.leftLine(cell)
|| (col > 0 && tabular.rightLine(tabular.cellIndex(row, col - 1)));
- pi.pain.line(x, y, x, y + h,
- drawline ? linecolor : gridcolor,
- drawline ? Painter::line_solid : Painter::line_onoffdash);
+ tabline(pi, x, y, x, y + h, drawline);
// Right
x -= tabular.interColumnSpace(cell);
drawline = tabular.rightLine(cell)
|| (next_cell_col < tabular.ncols()
&& tabular.leftLine(tabular.cellIndex(row, next_cell_col)));
- pi.pain.line(x + w, y, x + w, y + h,
- drawline ? linecolor : gridcolor,
- drawline ? Painter::line_solid : Painter::line_onoffdash);
+ tabline(pi, x + w, y, x + w, y + h, drawline);
}
return true;
}
// fall through
- case LFUN_NEWLINE_INSERT: {
- if (tabular.getPWidth(cur.idx()).zero()) {
+ case LFUN_NEWLINE_INSERT:
+ if ((tabular.isMultiColumn(cur.idx()) || tabular.isMultiRow(cur.idx()))
+ && tabular.getPWidth(cur.idx()).zero()) {
status.setEnabled(false);
return true;
- } else
- return cell(cur.idx())->getStatus(cur, cmd, status);
- }
+ }
+ return cell(cur.idx())->getStatus(cur, cmd, status);
case LFUN_NEWPAGE_INSERT:
status.setEnabled(false);
case Tabular::SET_PWIDTH: {
Length const len(value);
- tabular.setColumnPWidth(cur, cur.idx(), len);
- if (len.zero()
- && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
- tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
+ for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
+ tabular.setColumnPWidth(cur, tabular.cellIndex(row, c), len);
+ if (len.zero()
+ && tabular.getAlignment(tabular.cellIndex(row, c), true) == LYX_ALIGN_BLOCK)
+ tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
+ }
break;
}
case Tabular::TOGGLE_VARWIDTH_COLUMN: {
bool const varwidth = value == "on";
- tabular.toggleVarwidth(cur.idx(), varwidth);
+ for (col_type c = sel_col_start; c <= sel_col_end; ++c)
+ tabular.toggleVarwidth(tabular.cellIndex(row, c), varwidth);
break;
}
// just multicol for one single cell
// check whether we are completely in a multicol
if (!tabular.isMultiColumn(cur.idx()))
- tabular.setMultiColumn(cur.idx(), 1,
+ tabular.setMultiColumn(cur, cur.idx(), 1,
tabular.rightLine(cur.idx()));
break;
}
idx_type const s_start = cur.selBegin().idx();
row_type const col_start = tabular.cellColumn(s_start);
row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
- cur.idx() = tabular.setMultiColumn(s_start, col_end - col_start + 1,
+ cur.idx() = tabular.setMultiColumn(cur, s_start, col_end - col_start + 1,
tabular.rightLine(cur.selEnd().idx()));
cur.pit() = 0;
cur.pos() = 0;
// just multirow for one single cell
// check whether we are completely in a multirow
if (!tabular.isMultiRow(cur.idx()))
- tabular.setMultiRow(cur.idx(), 1,
+ tabular.setMultiRow(cur, cur.idx(), 1,
tabular.bottomLine(cur.idx()),
tabular.getAlignment(cur.idx()));
break;
idx_type const s_start = cur.selBegin().idx();
row_type const row_start = tabular.cellRow(s_start);
row_type const row_end = tabular.cellRow(cur.selEnd().idx());
- cur.idx() = tabular.setMultiRow(s_start, row_end - row_start + 1,
+ cur.idx() = tabular.setMultiRow(cur, s_start, row_end - row_start + 1,
tabular.bottomLine(cur.selEnd().idx()),
tabular.getAlignment(cur.selEnd().idx()));
cur.pit() = 0;
tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
break;
-
-
case Tabular::SET_ROTATE_CELL:
for (row_type r = sel_row_start; r <= sel_row_end; ++r)
for (col_type c = sel_col_start; c <= sel_col_end; ++c)
case Tabular::SET_LTCAPTION: {
if (tabular.ltCaption(row))
break;
- cur.idx() = tabular.setLTCaption(row, true);
+ cur.idx() = tabular.setLTCaption(cur, row, true);
cur.pit() = 0;
cur.pos() = 0;
cur.selection(false);
case Tabular::UNSET_LTCAPTION: {
if (!tabular.ltCaption(row))
break;
- cur.idx() = tabular.setLTCaption(row, false);
+ cur.idx() = tabular.setLTCaption(cur, row, false);
cur.pit() = 0;
cur.pos() = 0;
cur.selection(false);
cs = tabular.cellColumn(beg.idx());
ce = tabular.cellColumn(end.idx());
if (cs > ce)
- swap(cs, ce);
+ std::swap(cs, ce);
rs = tabular.cellRow(beg.idx());
re = tabular.cellRow(end.idx());
if (rs > re)
- swap(rs, re);
+ std::swap(rs, re);
}
bool InsetTabular::forcePlainLayout(idx_type cell) const
{
- return !tabular.getPWidth(cell).zero();
+ return tabular.isMultiColumn(cell) && !tabular.getPWidth(cell).zero();
}