]> git.lyx.org Git - features.git/commitdiff
Use new rowFlags() values to remove some inset hardcoding.
authorJean-Marc Lasgouttes <lasgouttes@lyx.org>
Mon, 22 Jun 2020 21:11:40 +0000 (23:11 +0200)
committerJean-Marc Lasgouttes <lasgouttes@lyx.org>
Mon, 22 Jun 2020 21:11:40 +0000 (23:11 +0200)
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.

26 files changed:
src/TextMetrics.cpp
src/insets/Inset.h
src/insets/InsetBibtex.h
src/insets/InsetBox.h
src/insets/InsetCaption.h
src/insets/InsetFloatList.h
src/insets/InsetInclude.cpp
src/insets/InsetInclude.h
src/insets/InsetIndex.h
src/insets/InsetListings.cpp
src/insets/InsetListings.h
src/insets/InsetNewline.h
src/insets/InsetNewpage.h
src/insets/InsetNomencl.h
src/insets/InsetNote.cpp
src/insets/InsetNote.h
src/insets/InsetRef.h
src/insets/InsetScript.cpp
src/insets/InsetScript.h
src/insets/InsetSeparator.h
src/insets/InsetTOC.h
src/insets/InsetTabular.cpp
src/insets/InsetTabular.h
src/insets/InsetVSpace.h
src/mathed/InsetMathHull.cpp
src/mathed/InsetMathHull.h

index d908ef30aa2609e79905804535ec09d6c3e4d86b..9f55c5a920607b92bbb77f1e65c4331510e4deb4 100644 (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:
-                       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
index 1366ca59f2196a2de3eec9320652537c89f98ed7..e57a61bd9902a3bb2102b979adfbe0c2c9fa7b76 100644 (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
index 56070d6710f2875cf35907f91c5ef1f2212e37f6..073b3760161065fc1791e4bcdd500df79ab5fd2c 100644 (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;
        ///
index 4e9894548c17332e5aaf10d412713463b3380734..a1f44669580ab8d78bfc08ba7544a9b5747ee5cd 100644 (file)
@@ -111,8 +111,6 @@ public:
        ///
        void metrics(MetricsInfo &, Dimension &) const;
        ///
-       DisplayType display() const { return Inline; }
-       ///
        ColorCode backgroundColor(PainterInfo const &) const;
        ///
        LyXAlignment contentAlignment() const;
index be0d163041ef43504aab31f3135d90d7d6664476..2b0af0dd0829d96b3ca3a519e60b66603da9f84f 100644 (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; }
        ///
index 78417096407a1ad23ccc5f6b1bcdf44ea756c751..20c8e22d62587374635397fc547a6b21b75e36e4 100644 (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;
        ///
index ba52db91c2d30de45a353bb86a31b962b5c7e6f5..87d2e5103d0dd913d5df419807fe1b93fa7fcf10 100644 (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;
 }
 
 
index 5588b0ee4e4c19d6ac68e3a1d1e273109e0be6f5..08fe9434cc5f7830b51a00d3dd02c56ca95361e3 100644 (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; }
        ///
index 46fe84576939ca529c941a182c4c1a85938b1b21..a0ed8fd82a7733a86022c9ea9bda862b70e53c20 100644 (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
index 7f98186630c0c5adf1734d3a81c87549bfbf9676..2f1462a154a6419063ec5da984c76488be6c1897 100644 (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;
 }
 
 
index 2b6de4e0f825563573c708a609fbad19cbe0652a..06c310739f76e484ad2b6225f9be65a322c1cc9d 100644 (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;
        ///
index 8ea59366d8b64e344364f6f185767236da5dbcb8..afa5fcdcc88ee07fe0b1528816d2866ea42d34b7 100644 (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 &);
index 749d5972d8e83f5908baa8242a49513553532f70..7fc4cc49b1f2e23d82456bf29a1c5e98f91c5536 100644 (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;
        ///
index 35887e88cd51b402846803ec3b7e96f996edbd8a..d023237865c73485ece5f1c361969a34821c4579 100644 (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;
        ///
index d9eb2f36991b54b0f6ea4160688d72c6979ba1cf..31df791f59a0e237df21bbc194a60b598da16804 100644 (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);
index e40b769854f24ca44c45c73a605a416ac2a01a86..c38d5d4eaac14b7bb730b3f9dca3d88154ab24e1 100644 (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 */
index 5839033c2563aee5519234cda1537933c52310bb..253718450bba82f2e0d5798d18063afe09c84e8f 100644 (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,
index f87f6c474765f4a6b6888b442f8580cb69f4dbc8..13b67e16e149371e0f0dddca22e03f9e50c6d93c 100644 (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);
index cd61117a3f47c6fdf04a53d574ffd32c66ae11a4..7f9cd852786cbf6d836e6d02286ff4e2402f9619 100644 (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; }
index 2433b8247bac7e982c5fdaca7d97cae32e7fde79..a57f45c4613c493469c130f2921cca18332e3b94 100644 (file)
@@ -64,6 +64,8 @@ public:
                // remove warning
                return docstring();
        }
+       ///
+       RowFlags rowFlags() const { return BreakAfter; }
 private:
        ///
        InsetCode lyxCode() const { return SEPARATOR_CODE; }
index 00f6c7688bad4e52646faf1ebfc5e8532a97a292..8ab1cc84ac5d29123eb5dfdd4c8a63a3654b3cd6 100644 (file)
@@ -37,7 +37,7 @@ public:
        ///
        docstring layoutName() const;
        ///
-       DisplayType display() const { return AlignCenter; }
+       RowFlags rowFlags() const { return Display; }
        ///
        virtual void validate(LaTeXFeatures &) const;
        ///
index a0a86f54a5a8986c4aa04d45d90e5910c6a85246..a2d49ef54b7d01494995be6f808b6a4ffcf937d3 100644 (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;
index 355a2520e998921648e57e5863c25623f5176ee7..2ee061445fb991103189e8ad129b53cd78fd1f55 100644 (file)
@@ -972,7 +972,7 @@ public:
        //
        bool isTable() const { return true; }
        ///
-       DisplayType display() const;
+       RowFlags rowFlags() const;
        ///
        void latex(otexstream &, OutputParams const &) const;
        ///
index 16e50da09616201a8e8c5838190a82e26243ac3f..08e8ea882ec922051e7f95b1f2d357ff0caca58f 100644 (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);
        ///
index ab8cf9f19afcbc6cb04bd404798936481d8ac1ef..2dcd43af78b4a54d6f26fdd1de1e153d5ad2f7ce 100644 (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();
 }
 
 
index 32e4f09fa0930c726c018b95a6b5b2da46b770ec..96dbc10be604fecc48df3a08932809ea322ec3d4 100644 (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;