From: Jean-Marc Lasgouttes Date: Mon, 22 Jun 2020 21:11:40 +0000 (+0200) Subject: Use new rowFlags() values to remove some inset hardcoding. X-Git-Tag: lyx-2.4.0dev-acb2ca7b~720 X-Git-Url: https://git.lyx.org/gitweb/?a=commitdiff_plain;h=ba738d0167bcea87df47f49b1bc90b04ae4dbd68;p=features.git 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. --- diff --git a/src/TextMetrics.cpp b/src/TextMetrics.cpp index d908ef30aa..9f55c5a920 100644 --- a/src/TextMetrics.cpp +++ b/src/TextMetrics.cpp @@ -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: - align = LYX_ALIGN_BLOCK; - break; - case Inset::AlignCenter: - align = LYX_ALIGN_CENTER; - break; - case Inset::Inline: - // unchanged (use align) - break; - case Inset::AlignRight: - align = LYX_ALIGN_RIGHT; - break; + if (inset->rowFlags() & Inset::Display) { + if (inset->rowFlags() & Inset::AlignLeft) + align = LYX_ALIGN_BLOCK; + else if (inset->rowFlags() & Inset::AlignRight) + align = LYX_ALIGN_RIGHT; + 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 diff --git a/src/insets/Inset.h b/src/insets/Inset.h index 1366ca59f2..e57a61bd99 100644 --- a/src/insets/Inset.h +++ b/src/insets/Inset.h @@ -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(int(d1) | int(d2)); +} + + +inline Inset::RowFlags operator&(Inset::RowFlags const d1, + Inset::RowFlags const d2) +{ + return static_cast(int(d1) & int(d2)); +} + + } // namespace lyx #endif diff --git a/src/insets/InsetBibtex.h b/src/insets/InsetBibtex.h index 56070d6710..073b376016 100644 --- a/src/insets/InsetBibtex.h +++ b/src/insets/InsetBibtex.h @@ -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; /// diff --git a/src/insets/InsetBox.h b/src/insets/InsetBox.h index 4e9894548c..a1f4466958 100644 --- a/src/insets/InsetBox.h +++ b/src/insets/InsetBox.h @@ -111,8 +111,6 @@ public: /// void metrics(MetricsInfo &, Dimension &) const; /// - DisplayType display() const { return Inline; } - /// ColorCode backgroundColor(PainterInfo const &) const; /// LyXAlignment contentAlignment() const; diff --git a/src/insets/InsetCaption.h b/src/insets/InsetCaption.h index be0d163041..2b0af0dd08 100644 --- a/src/insets/InsetCaption.h +++ b/src/insets/InsetCaption.h @@ -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; } /// diff --git a/src/insets/InsetFloatList.h b/src/insets/InsetFloatList.h index 7841709640..20c8e22d62 100644 --- a/src/insets/InsetFloatList.h +++ b/src/insets/InsetFloatList.h @@ -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; /// diff --git a/src/insets/InsetInclude.cpp b/src/insets/InsetInclude.cpp index ba52db91c2..87d2e5103d 100644 --- a/src/insets/InsetInclude.cpp +++ b/src/insets/InsetInclude.cpp @@ -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; } diff --git a/src/insets/InsetInclude.h b/src/insets/InsetInclude.h index 5588b0ee4e..08fe9434cc 100644 --- a/src/insets/InsetInclude.h +++ b/src/insets/InsetInclude.h @@ -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; } /// diff --git a/src/insets/InsetIndex.h b/src/insets/InsetIndex.h index 46fe845769..a0ed8fd82a 100644 --- a/src/insets/InsetIndex.h +++ b/src/insets/InsetIndex.h @@ -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 diff --git a/src/insets/InsetListings.cpp b/src/insets/InsetListings.cpp index 7f98186630..2f1462a154 100644 --- a/src/insets/InsetListings.cpp +++ b/src/insets/InsetListings.cpp @@ -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; } diff --git a/src/insets/InsetListings.h b/src/insets/InsetListings.h index 2b6de4e0f8..06c310739f 100644 --- a/src/insets/InsetListings.h +++ b/src/insets/InsetListings.h @@ -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; /// diff --git a/src/insets/InsetNewline.h b/src/insets/InsetNewline.h index 8ea59366d8..afa5fcdcc8 100644 --- a/src/insets/InsetNewline.h +++ b/src/insets/InsetNewline.h @@ -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 &); diff --git a/src/insets/InsetNewpage.h b/src/insets/InsetNewpage.h index 749d5972d8..7fc4cc49b1 100644 --- a/src/insets/InsetNewpage.h +++ b/src/insets/InsetNewpage.h @@ -74,7 +74,7 @@ private: /// void write(std::ostream & os) const; /// - DisplayType display() const { return AlignCenter; } + RowFlags rowFlags() const { return Display; } /// docstring insetLabel() const; /// diff --git a/src/insets/InsetNomencl.h b/src/insets/InsetNomencl.h index 35887e88cd..d023237865 100644 --- a/src/insets/InsetNomencl.h +++ b/src/insets/InsetNomencl.h @@ -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; /// diff --git a/src/insets/InsetNote.cpp b/src/insets/InsetNote.cpp index d9eb2f3699..31df791f59 100644 --- a/src/insets/InsetNote.cpp +++ b/src/insets/InsetNote.cpp @@ -116,12 +116,6 @@ docstring InsetNote::layoutName() const } -Inset::DisplayType InsetNote::display() const -{ - return Inline; -} - - void InsetNote::write(ostream & os) const { params_.write(os); diff --git a/src/insets/InsetNote.h b/src/insets/InsetNote.h index e40b769854..c38d5d4eaa 100644 --- a/src/insets/InsetNote.h +++ b/src/insets/InsetNote.h @@ -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 */ diff --git a/src/insets/InsetRef.h b/src/insets/InsetRef.h index 5839033c25..253718450b 100644 --- a/src/insets/InsetRef.h +++ b/src/insets/InsetRef.h @@ -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, diff --git a/src/insets/InsetScript.cpp b/src/insets/InsetScript.cpp index f87f6c4747..13b67e16e1 100644 --- a/src/insets/InsetScript.cpp +++ b/src/insets/InsetScript.cpp @@ -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); diff --git a/src/insets/InsetScript.h b/src/insets/InsetScript.h index cd61117a3f..7f9cd85278 100644 --- a/src/insets/InsetScript.h +++ b/src/insets/InsetScript.h @@ -66,8 +66,6 @@ public: InsetCode lyxCode() const { return SCRIPT_CODE; } /// docstring layoutName() const; - /// - DisplayType display() const; /// int topOffset(BufferView const *) const { return 0; } diff --git a/src/insets/InsetSeparator.h b/src/insets/InsetSeparator.h index 2433b8247b..a57f45c461 100644 --- a/src/insets/InsetSeparator.h +++ b/src/insets/InsetSeparator.h @@ -64,6 +64,8 @@ public: // remove warning return docstring(); } + /// + RowFlags rowFlags() const { return BreakAfter; } private: /// InsetCode lyxCode() const { return SEPARATOR_CODE; } diff --git a/src/insets/InsetTOC.h b/src/insets/InsetTOC.h index 00f6c7688b..8ab1cc84ac 100644 --- a/src/insets/InsetTOC.h +++ b/src/insets/InsetTOC.h @@ -37,7 +37,7 @@ public: /// docstring layoutName() const; /// - DisplayType display() const { return AlignCenter; } + RowFlags rowFlags() const { return Display; } /// virtual void validate(LaTeXFeatures &) const; /// diff --git a/src/insets/InsetTabular.cpp b/src/insets/InsetTabular.cpp index a0a86f54a5..a2d49ef54b 100644 --- a/src/insets/InsetTabular.cpp +++ b/src/insets/InsetTabular.cpp @@ -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; diff --git a/src/insets/InsetTabular.h b/src/insets/InsetTabular.h index 355a2520e9..2ee061445f 100644 --- a/src/insets/InsetTabular.h +++ b/src/insets/InsetTabular.h @@ -972,7 +972,7 @@ public: // bool isTable() const { return true; } /// - DisplayType display() const; + RowFlags rowFlags() const; /// void latex(otexstream &, OutputParams const &) const; /// diff --git a/src/insets/InsetVSpace.h b/src/insets/InsetVSpace.h index 16e50da096..08e8ea882e 100644 --- a/src/insets/InsetVSpace.h +++ b/src/insets/InsetVSpace.h @@ -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); /// diff --git a/src/mathed/InsetMathHull.cpp b/src/mathed/InsetMathHull.cpp index ab8cf9f19a..2dcd43af78 100644 --- a/src/mathed/InsetMathHull.cpp +++ b/src/mathed/InsetMathHull.cpp @@ -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(); } diff --git a/src/mathed/InsetMathHull.h b/src/mathed/InsetMathHull.h index 32e4f09fa0..96dbc10be6 100644 --- a/src/mathed/InsetMathHull.h +++ b/src/mathed/InsetMathHull.h @@ -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;