Restrict caption insertion in tables to where it makes sense (#13037)

This requires to move insetAllowed() to the cell level, as only cells
in the caption row of a longtable actually can take a caption.
This commit is contained in:
Juergen Spitzmueller 2024-01-28 12:13:27 +01:00
parent 7eb5003a81
commit ad6aa8ba4b
2 changed files with 34 additions and 23 deletions

View File

@ -1064,9 +1064,10 @@ void Tabular::updateIndexes()
rowofcell.resize(numberofcells);
columnofcell.resize(numberofcells);
idx_type i = 0;
// reset column and row of cells and update their width, alignment and ct status
// reset column and row of cells and update their width, alignment, caption, and ct status
for (row_type row = 0; row < nrows(); ++row) {
for (col_type column = 0; column < ncols(); ++column) {
cell_info[row][column].inset->toggleCaptionRow(is_long_tabular && ltCaption(row));
if (isPartOfMultiColumn(row, column)) {
cell_info[row][column].inset->toggleMultiCol(true);
continue;
@ -2021,6 +2022,7 @@ void Tabular::read(Lexer & lex)
<< line << ')' << endl;
return;
}
cell_info[i][j].inset->toggleCaptionRow(is_long_tabular && row_info[i].caption);
getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
getTokenValue(line, "multirow", cell_info[i][j].multirow);
getTokenValue(line, "mroffset", cell_info[i][j].mroffset);
@ -4358,7 +4360,8 @@ bool Tabular::hasNewlines(idx_type cell) const
InsetTableCell::InsetTableCell(Buffer * buf)
: InsetText(buf, InsetText::PlainLayout), isFixedWidth(false), isVarwidth(false),
isMultiColumn(false), isMultiRow(false), contentAlign(LYX_ALIGN_CENTER)
isMultiColumn(false), isMultiRow(false), isCaptionRow(false),
contentAlign(LYX_ALIGN_CENTER)
{}
bool InsetTableCell::allowParagraphCustomization(idx_type) const
@ -4463,6 +4466,24 @@ void InsetTableCell::metrics(MetricsInfo & mi, Dimension & dim) const
}
bool InsetTableCell::insetAllowed(InsetCode code) const
{
switch (code) {
case FLOAT_CODE:
case MARGIN_CODE:
case MATHMACRO_CODE:
case WRAP_CODE:
return false;
case CAPTION_CODE:
return isCaptionRow;
default:
return true;
}
}
/////////////////////////////////////////////////////////////////////
//
// InsetTabular
@ -4497,24 +4518,6 @@ void InsetTabular::setBuffer(Buffer & buf)
}
bool InsetTabular::insetAllowed(InsetCode code) const
{
switch (code) {
case FLOAT_CODE:
case MARGIN_CODE:
case MATHMACRO_CODE:
case WRAP_CODE:
return false;
case CAPTION_CODE:
return tabular.is_long_tabular;
default:
return true;
}
}
bool InsetTabular::allowMultiPar() const
{
for (col_type c = 0; c < tabular.ncols(); ++c) {
@ -7276,6 +7279,8 @@ void InsetTabular::tabularFeatures(Cursor & cur,
cur.pit() = 0;
cur.pos() = 0;
cur.selection(false);
// update the captionRow status of all cells
tabular.updateIndexes();
// If a row is set as caption, then also insert
// a caption. Otherwise the LaTeX output is broken.
// Select cell if it is non-empty
@ -7295,6 +7300,8 @@ void InsetTabular::tabularFeatures(Cursor & cur,
FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
if (lyx::getStatus(fr).enabled())
lyx::dispatch(fr);
// update the captionRow status of all cells
tabular.updateIndexes();
break;
}

View File

@ -77,6 +77,8 @@ public:
///
void toggleMultiRow(bool m) { isMultiRow = m; }
///
void toggleCaptionRow(bool m) { isCaptionRow = m; }
///
void setContentAlignment(LyXAlignment al) { contentAlign = al; }
/// writes the contents of the cell as a string, optionally
/// descending into insets
@ -133,7 +135,9 @@ private:
bool isMultiColumn;
///
bool isMultiRow;
// FIXME: Here the thoughts from the comment above also apply.
// FIXME: For the next two items the thoughts from the comment above also apply.
///
bool isCaptionRow;
///
LyXAlignment contentAlign;
/// should paragraph indentation be omitted in any case?
@ -148,6 +152,8 @@ private:
bool forceLocalFontSwitch() const override;
/// Is the width forced to some value?
bool hasFixedWidth() const override { return isFixedWidth; }
///
bool insetAllowed(InsetCode code) const override;
};
@ -1031,8 +1037,6 @@ public:
///
bool hasSettings() const override { return true; }
///
bool insetAllowed(InsetCode code) const override;
///
bool allowSpellCheck() const override { return true; }
///
bool canTrackChanges() const override { return true; }