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

View File

@ -485,15 +485,24 @@ public:
virtual OutputParams::CtObject CtObject(OutputParams const &) const { return OutputParams::CT_NORMAL; } virtual OutputParams::CtObject CtObject(OutputParams const &) const { return OutputParams::CT_NORMAL; }
enum DisplayType { enum RowFlags {
Inline = 0, Inline = 0,
AlignLeft, // break row before this inset
AlignCenter, BreakBefore = 1 << 0,
AlignRight // 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? /// How should this inset be displayed in its row?
virtual DisplayType display() const { return Inline; } virtual RowFlags rowFlags() const { return Inline; }
/// indentation before this inset (only needed for displayed hull insets with fleqn option) /// indentation before this inset (only needed for displayed hull insets with fleqn option)
virtual int indent(BufferView const &) const { return 0; } virtual int indent(BufferView const &) const { return 0; }
/// ///
@ -652,6 +661,21 @@ protected:
Buffer * buffer_; 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 } // namespace lyx
#endif #endif

View File

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

View File

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

View File

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

View File

@ -32,7 +32,7 @@ public:
/// ///
InsetCode lyxCode() const { return FLOAT_LIST_CODE; } InsetCode lyxCode() const { return FLOAT_LIST_CODE; }
/// ///
DisplayType display() const { return AlignCenter; } RowFlags rowFlags() const { return Display; }
/// ///
void write(std::ostream &) const; 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; void draw(PainterInfo & pi, int x, int y) const;
/// ///
DisplayType display() const; RowFlags rowFlags() const;
/// ///
InsetCode lyxCode() const { return INCLUDE_CODE; } InsetCode lyxCode() const { return INCLUDE_CODE; }
/// ///

View File

@ -119,7 +119,7 @@ public:
/// ///
bool hasSettings() const; bool hasSettings() const;
/// ///
DisplayType display() const { return AlignCenter; } RowFlags rowFlags() const { return Display; }
//@} //@}
/// \name Static public methods obligated for InsetCommand derived classes /// \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; } InsetCode lyxCode() const { return LISTINGS_CODE; }
/// lstinline is inlined, normal listing is displayed /// lstinline is inlined, normal listing is displayed
DisplayType display() const; RowFlags rowFlags() const;
/// ///
docstring layoutName() const; docstring layoutName() const;
/// ///

View File

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

View File

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

View File

@ -100,7 +100,7 @@ public:
/// ///
bool hasSettings() const { return true; } bool hasSettings() const { return true; }
/// ///
DisplayType display() const { return AlignCenter; } RowFlags rowFlags() const { return Display; }
/// ///
void latex(otexstream &, OutputParams const &) const; 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 void InsetNote::write(ostream & os) const
{ {
params_.write(os); params_.write(os);

View File

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

View File

@ -56,8 +56,6 @@ public:
/// ///
InsetCode lyxCode() const { return REF_CODE; } InsetCode lyxCode() const { return REF_CODE; }
/// ///
DisplayType display() const { return Inline; }
///
void latex(otexstream &, OutputParams const &) const; void latex(otexstream &, OutputParams const &) const;
/// ///
int plaintext(odocstringstream & ods, OutputParams const & op, 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 void InsetScript::metrics(MetricsInfo & mi, Dimension & dim) const
{ {
int const shift = params_.shift(mi.base.font); int const shift = params_.shift(mi.base.font);

View File

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

View File

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

View File

@ -37,7 +37,7 @@ public:
/// ///
docstring layoutName() const; docstring layoutName() const;
/// ///
DisplayType display() const { return AlignCenter; } RowFlags rowFlags() const { return Display; }
/// ///
virtual void validate(LaTeXFeatures &) const; 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) { if (tabular.is_long_tabular) {
switch (tabular.longtabular_alignment) { switch (tabular.longtabular_alignment) {
case Tabular::LYX_LONGTABULAR_ALIGN_LEFT: case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
return AlignLeft; return Display | AlignLeft;
case Tabular::LYX_LONGTABULAR_ALIGN_CENTER: case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
return AlignCenter; return Display;
case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT: case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
return AlignRight; return Display | AlignRight;
default: default:
return AlignCenter; return Display;
} }
} else } else
return Inline; return Inline;

View File

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

View File

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