Use new rowFlags() values to remove some inset hardcoding.

The enum DisplayType is replaced with the flags RowFlags that can be
combined. Here is the correspondence between the old DisplayType and
the new Inset::RowFlags:

DisplayType   RowFLags             Meaning
 Inline        Inline               plain inline inset
  --           BreakBefore          row ends before this inset
  --           BreakAfter           the row ends after this inset
 AlignCenter   Display	            the inset is centered on its own row
 AlignLeft     Display | AlignLeft  the inset is left-aligned on its row
 AlignRight    Display | AlignRight the inset is right-aligned on its row
  --           RowAfter             an extra row is needed after this inset

Display is just a shortcut for BreakBefore | BreakAfter.

The flags for the newline inset will be BreakAfter | RowAfter,
while the separator inset will just use BreakAfter.

This groundwork does not introduce any new feature at this point. It
aims to remve the numerous isNewLine and isSeparator all over the
code, and to eventually optional break after some insets like spaces
(see #11621).

Most display() methods are renamed to rowFlags(). Some are removed
because they returned Inline.

Now display() is only a helper function for hull insets.
This commit is contained in:
Jean-Marc Lasgouttes 2020-06-22 23:11:40 +02:00
parent 4dc33e52f8
commit ba738d0167
26 changed files with 92 additions and 88 deletions

View File

@ -622,19 +622,13 @@ LyXAlignment TextMetrics::getAlign(Paragraph const & par, Row const & row) const
// Display-style insets should always be on a centered row
if (Inset const * inset = par.getInset(row.pos())) {
switch (inset->display()) {
case Inset::AlignLeft:
if (inset->rowFlags() & Inset::Display) {
if (inset->rowFlags() & Inset::AlignLeft)
align = LYX_ALIGN_BLOCK;
break;
case Inset::AlignCenter:
align = LYX_ALIGN_CENTER;
break;
case Inset::Inline:
// unchanged (use align)
break;
case Inset::AlignRight:
else if (inset->rowFlags() & Inset::AlignRight)
align = LYX_ALIGN_RIGHT;
break;
else
align = LYX_ALIGN_CENTER;
}
}
@ -976,23 +970,22 @@ bool TextMetrics::breakRow(Row & row, int const right_margin) const
}
// Handle some situations that abruptly terminate the row
// - A newline inset
// - Before a display inset
// - After a display inset
Inset const * inset = 0;
if (par.isNewline(i) || par.isEnvSeparator(i)
|| (i + 1 < end && (inset = par.getInset(i + 1))
&& inset->display())
|| (!row.empty() && row.back().inset
&& row.back().inset->display())) {
// - Before an inset with BreakBefore
// - After an inset with BreakAfter
Inset const * prevInset = !row.empty() ? row.back().inset : 0;
Inset const * nextInset = (i + 1 < end) ? par.getInset(i + 1) : 0;
if ((nextInset && nextInset->rowFlags() & Inset::BreakBefore)
|| (prevInset && prevInset->rowFlags() & Inset::BreakAfter)) {
row.flushed(true);
// We will force a row creation after either
// - a newline;
// - a display inset followed by a end label.
need_new_row =
par.isNewline(i)
|| (inset && inset->display() && i + 1 == end
&& text_->getEndLabel(row.pit()) != END_LABEL_NO_LABEL);
// Force a row creation after this one if it is ended by
// an inset that either
// - has row flag RowAfter that enforces that;
// - or (1) did force the row breaking, (2) is at end of
// paragraph and (3) the said paragraph has an end label.
need_new_row = prevInset &&
(prevInset->rowFlags() & Inset::RowAfter
|| (prevInset->rowFlags() & Inset::BreakAfter && i + 1 == end
&& text_->getEndLabel(row.pit()) != END_LABEL_NO_LABEL));
++i;
break;
}
@ -1771,10 +1764,10 @@ int TextMetrics::leftMargin(pit_type const pit, pos_type const pos) const
&& !par.params().noindent()
// in some insets, paragraphs are never indented
&& !text_->inset().neverIndent()
// display style insets are always centered, omit indentation
// display style insets do not need indentation
&& !(!par.empty()
&& par.isInset(pos)
&& par.getInset(pos)->display())
&& par.getInset(pos)->rowFlags() & Inset::Display)
&& (!(tclass.isDefaultLayout(par.layout())
|| tclass.isPlainLayout(par.layout()))
|| buffer.params().paragraph_separation

View File

@ -485,15 +485,24 @@ public:
virtual OutputParams::CtObject CtObject(OutputParams const &) const { return OutputParams::CT_NORMAL; }
enum DisplayType {
enum RowFlags {
Inline = 0,
AlignLeft,
AlignCenter,
AlignRight
// break row before this inset
BreakBefore = 1 << 0,
// break row after this inset
BreakAfter = 1 << 1,
// force new (maybe empty) row after this inset
RowAfter = 1 << 2,
// specify an alignment (left, right) for a display inset
// (default is center)
AlignLeft = 1 << 3,
AlignRight = 1 << 4,
// A display inset breaks row at both ends
Display = BreakBefore | BreakAfter
};
/// should we have a non-filled line before this inset?
virtual DisplayType display() const { return Inline; }
/// How should this inset be displayed in its row?
virtual RowFlags rowFlags() const { return Inline; }
/// indentation before this inset (only needed for displayed hull insets with fleqn option)
virtual int indent(BufferView const &) const { return 0; }
///
@ -652,6 +661,21 @@ protected:
Buffer * buffer_;
};
inline Inset::RowFlags operator|(Inset::RowFlags const d1,
Inset::RowFlags const d2)
{
return static_cast<Inset::RowFlags>(int(d1) | int(d2));
}
inline Inset::RowFlags operator&(Inset::RowFlags const d1,
Inset::RowFlags const d2)
{
return static_cast<Inset::RowFlags>(int(d1) & int(d2));
}
} // namespace lyx
#endif

View File

@ -48,7 +48,7 @@ public:
///
InsetCode lyxCode() const { return BIBTEX_CODE; }
///
DisplayType display() const { return AlignCenter; }
RowFlags rowFlags() const { return Display; }
///
void latex(otexstream &, OutputParams const &) const;
///

View File

@ -111,8 +111,6 @@ public:
///
void metrics(MetricsInfo &, Dimension &) const;
///
DisplayType display() const { return Inline; }
///
ColorCode backgroundColor(PainterInfo const &) const;
///
LyXAlignment contentAlignment() const;

View File

@ -38,7 +38,7 @@ private:
///
void write(std::ostream & os) const;
///
DisplayType display() const { return AlignCenter; }
RowFlags rowFlags() const { return Display; }
///
bool neverIndent() const { return true; }
///

View File

@ -32,7 +32,7 @@ public:
///
InsetCode lyxCode() const { return FLOAT_LIST_CODE; }
///
DisplayType display() const { return AlignCenter; }
RowFlags rowFlags() const { return Display; }
///
void write(std::ostream &) const;
///

View File

@ -1224,9 +1224,9 @@ string InsetInclude::contextMenuName() const
}
Inset::DisplayType InsetInclude::display() const
Inset::RowFlags InsetInclude::rowFlags() const
{
return type(params()) == INPUT ? Inline : AlignCenter;
return type(params()) == INPUT ? Inline : Display;
}

View File

@ -76,7 +76,7 @@ public:
///
void draw(PainterInfo & pi, int x, int y) const;
///
DisplayType display() const;
RowFlags rowFlags() const;
///
InsetCode lyxCode() const { return INCLUDE_CODE; }
///

View File

@ -119,7 +119,7 @@ public:
///
bool hasSettings() const;
///
DisplayType display() const { return AlignCenter; }
RowFlags rowFlags() const { return Display; }
//@}
/// \name Static public methods obligated for InsetCommand derived classes

View File

@ -66,9 +66,9 @@ InsetListings::~InsetListings()
}
Inset::DisplayType InsetListings::display() const
Inset::RowFlags InsetListings::rowFlags() const
{
return params().isInline() || params().isFloat() ? Inline : AlignLeft;
return params().isInline() || params().isFloat() ? Inline : Display | AlignLeft;
}

View File

@ -46,7 +46,7 @@ private:
///
InsetCode lyxCode() const { return LISTINGS_CODE; }
/// lstinline is inlined, normal listing is displayed
DisplayType display() const;
RowFlags rowFlags() const;
///
docstring layoutName() const;
///

View File

@ -47,6 +47,8 @@ public:
explicit InsetNewline(InsetNewlineParams par) : Inset(0)
{ params_.kind = par.kind; }
///
RowFlags rowFlags() const { return BreakAfter | RowAfter; }
///
static void string2params(std::string const &, InsetNewlineParams &);
///
static std::string params2string(InsetNewlineParams const &);

View File

@ -74,7 +74,7 @@ private:
///
void write(std::ostream & os) const;
///
DisplayType display() const { return AlignCenter; }
RowFlags rowFlags() const { return Display; }
///
docstring insetLabel() const;
///

View File

@ -100,7 +100,7 @@ public:
///
bool hasSettings() const { return true; }
///
DisplayType display() const { return AlignCenter; }
RowFlags rowFlags() const { return Display; }
///
void latex(otexstream &, OutputParams const &) const;
///

View File

@ -116,12 +116,6 @@ docstring InsetNote::layoutName() const
}
Inset::DisplayType InsetNote::display() const
{
return Inline;
}
void InsetNote::write(ostream & os) const
{
params_.write(os);

View File

@ -61,8 +61,6 @@ private:
InsetCode lyxCode() const { return NOTE_CODE; }
///
docstring layoutName() const;
///
DisplayType display() const;
/** returns false if, when outputing LaTeX, font changes should
be closed before generating this inset. This is needed for
insets that may contain several paragraphs */

View File

@ -56,8 +56,6 @@ public:
///
InsetCode lyxCode() const { return REF_CODE; }
///
DisplayType display() const { return Inline; }
///
void latex(otexstream &, OutputParams const &) const;
///
int plaintext(odocstringstream & ods, OutputParams const & op,

View File

@ -151,12 +151,6 @@ docstring InsetScript::layoutName() const
}
Inset::DisplayType InsetScript::display() const
{
return Inline;
}
void InsetScript::metrics(MetricsInfo & mi, Dimension & dim) const
{
int const shift = params_.shift(mi.base.font);

View File

@ -66,8 +66,6 @@ public:
InsetCode lyxCode() const { return SCRIPT_CODE; }
///
docstring layoutName() const;
///
DisplayType display() const;
///
int topOffset(BufferView const *) const { return 0; }

View File

@ -64,6 +64,8 @@ public:
// remove warning
return docstring();
}
///
RowFlags rowFlags() const { return BreakAfter; }
private:
///
InsetCode lyxCode() const { return SEPARATOR_CODE; }

View File

@ -37,7 +37,7 @@ public:
///
docstring layoutName() const;
///
DisplayType display() const { return AlignCenter; }
RowFlags rowFlags() const { return Display; }
///
virtual void validate(LaTeXFeatures &) const;
///

View File

@ -5966,18 +5966,18 @@ bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
}
Inset::DisplayType InsetTabular::display() const
Inset::RowFlags InsetTabular::rowFlags() const
{
if (tabular.is_long_tabular) {
switch (tabular.longtabular_alignment) {
case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
return AlignLeft;
return Display | AlignLeft;
case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
return AlignCenter;
return Display;
case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
return AlignRight;
return Display | AlignRight;
default:
return AlignCenter;
return Display;
}
} else
return Inline;

View File

@ -972,7 +972,7 @@ public:
//
bool isTable() const { return true; }
///
DisplayType display() const;
RowFlags rowFlags() const;
///
void latex(otexstream &, OutputParams const &) const;
///

View File

@ -62,7 +62,7 @@ private:
///
void write(std::ostream & os) const;
///
DisplayType display() const { return AlignCenter; }
RowFlags rowFlags() const { return Display; }
///
void doDispatch(Cursor & cur, FuncRequest & cmd);
///

View File

@ -1015,7 +1015,7 @@ bool InsetMathHull::outerDisplay() const
}
Inset::DisplayType InsetMathHull::display() const
Inset::RowFlags InsetMathHull::rowFlags() const
{
switch (type_) {
case hullUnknown:
@ -1033,12 +1033,12 @@ Inset::DisplayType InsetMathHull::display() const
case hullMultline:
case hullGather:
if (buffer().params().is_math_indent)
return AlignLeft;
return Display | AlignLeft;
else
return AlignCenter;
return Display;
}
// avoid warning
return AlignCenter;
return Display;
}
@ -1046,7 +1046,7 @@ int InsetMathHull::indent(BufferView const & bv) const
{
// FIXME: set this in the textclass. This value is what the article class uses.
static Length default_indent(2.5, Length::EM);
if (display() != Inline && buffer().params().is_math_indent) {
if (display() && buffer().params().is_math_indent) {
Length const & len = buffer().params().getMathIndent();
if (len.empty())
return bv.inPixels(default_indent);
@ -2104,15 +2104,15 @@ bool InsetMathHull::getStatus(Cursor & cur, FuncRequest const & cmd,
return true;
}
case LFUN_MATH_DISPLAY: {
status.setEnabled(display() != Inline || allowDisplayMath(cur));
status.setOnOff(display() != Inline);
status.setEnabled(display() || allowDisplayMath(cur));
status.setOnOff(display());
return true;
}
case LFUN_MATH_NUMBER_TOGGLE:
// FIXME: what is the right test, this or the one of
// LABEL_INSERT?
status.setEnabled(display() != Inline);
status.setEnabled(display());
status.setOnOff(numberedType());
return true;
@ -2121,7 +2121,7 @@ bool InsetMathHull::getStatus(Cursor & cur, FuncRequest const & cmd,
// LABEL_INSERT?
bool const enable = (type_ == hullMultline)
? (nrows() - 1 == cur.row())
: display() != Inline;
: display();
row_type const r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
status.setEnabled(enable);
status.setOnOff(enable && numbered(r));
@ -2752,7 +2752,7 @@ void InsetMathHull::recordLocation(DocIterator const & di)
bool InsetMathHull::canPaintChange(BufferView const &) const
{
// We let RowPainter do it seamlessly for inline insets
return display() != Inline;
return display();
}

View File

@ -288,7 +288,10 @@ public:
///
Inset * editXY(Cursor & cur, int x, int y);
///
DisplayType display() const;
RowFlags rowFlags() const;
/// helper function
bool display() const { return rowFlags() & Display; }
///
int indent(BufferView const &) const;