]> git.lyx.org Git - features.git/commitdiff
Replace the hardcoded TEXT_TO_INSET_OFFSET by tunable values
authorJean-Marc Lasgouttes <lasgouttes@lyx.org>
Mon, 13 Jan 2020 21:09:06 +0000 (22:09 +0100)
committerJean-Marc Lasgouttes <lasgouttes@lyx.org>
Wed, 15 Jan 2020 10:13:54 +0000 (11:13 +0100)
The basic value is Inset::textOffset(BufferView*), which can in theory
change with the BufferView zoom and dpi. It is hardcoded to 4 for now.

Moreover, we introduce the virtual inset methods
(top|bottom|left|right)Offset, which can be tweaked for each inset.

No change intended (for now).

13 files changed:
src/RowPainter.cpp
src/TextMetrics.cpp
src/insets/Inset.h
src/insets/InsetCaption.cpp
src/insets/InsetCollapsible.cpp
src/insets/InsetIPA.cpp
src/insets/InsetPhantom.cpp
src/insets/InsetPreview.cpp
src/insets/InsetTabular.cpp
src/insets/InsetText.cpp
src/insets/RenderButton.cpp
src/insets/RenderGraphic.cpp
src/insets/RenderPreview.cpp

index 13c3be01963ddd6d719f2aced45670e052534817..16ff19519659766b4de3ba2c4c53a6f3ab6c5b9a 100644 (file)
@@ -479,10 +479,10 @@ void RowPainter::paintLast() const
                int x = 0;
                if (row_.isRTL()) {
                        int const normal_x = nestMargin() + changebarMargin();
-                       x = min(normal_x, row_.left_margin - size - Inset::TEXT_TO_INSET_OFFSET);
+                       x = min(normal_x, row_.left_margin - size - Inset::textOffset(pi_.base.bv));
                } else {
                        int const normal_x = tm_.width() - row_.right_margin
-                               - size - Inset::TEXT_TO_INSET_OFFSET;
+                               - size - Inset::textOffset(pi_.base.bv);
                        x = max(normal_x, row_.width());
                }
 
index 65a1aa7945eaebac5eebc0ffb0615a3dcc64f3fd..796e1bd25ec949f3c50a229f8ae33b93d5f75c8a 100644 (file)
@@ -1933,9 +1933,9 @@ void TextMetrics::drawParagraph(PainterInfo & pi, pit_type const pit, int const
                        // the begining/end of row. However, it will not work if
                        // the caret has a ridiculous width like 6. (see ticket
                        // #10797)
-                       pi.pain.fillRectangle(max(row_x, 0) - Inset::TEXT_TO_INSET_OFFSET,
+                       pi.pain.fillRectangle(max(row_x, 0) - Inset::textOffset(pi.base.bv),
                                              y - row.ascent(),
-                                             width() + 2 * Inset::TEXT_TO_INSET_OFFSET,
+                                             width() + 2 * Inset::textOffset(pi.base.bv),
                                              row.height(), pi.background_color);
                }
 
index cffaf77c6ea7ac5bd4a3b83d5ba70897afe44744..ab35584e126c06a830b3cee11fa294ec4008c474 100644 (file)
@@ -202,6 +202,17 @@ public:
        /// https://www.mail-archive.com/lyx-devel@lists.lyx.org/msg199001.html
        virtual Inset * editXY(Cursor & cur, int x, int y);
 
+       /// The default margin inside text insets
+       static int textOffset(BufferView const *) { return 4; }
+       ///
+       virtual int topOffset(BufferView const *bv) const { return textOffset(bv); }
+       ///
+       virtual int bottomOffset(BufferView const *bv) const { return textOffset(bv); }
+       ///
+       virtual int leftOffset(BufferView const *bv) const { return textOffset(bv); }
+       ///
+       virtual int rightOffset(BufferView const *bv) const { return textOffset(bv); }
+
        /// compute the size of the object returned in dim
        virtual void metrics(MetricsInfo & mi, Dimension & dim) const = 0;
        /// draw inset and update (xo, yo)-cache
@@ -610,8 +621,6 @@ public:
        virtual ColorCode backgroundColor(PainterInfo const &) const;
        ///
        virtual ColorCode labelColor() const;
-       //
-       enum { TEXT_TO_INSET_OFFSET = 4 };
 
        /// Determine the action of backspace and delete: do we select instead of
        /// deleting if not already selected?
index 919ecd8f6dd35280db374672bdb9a8aa90deb88c..43147ff8646ce03a9cf77ba541155fe174fc54ed 100644 (file)
@@ -114,7 +114,7 @@ void InsetCaption::metrics(MetricsInfo & mi, Dimension & dim) const
        mi.base.font = mi.base.bv->buffer().params().getFont().fontInfo();
        labelwidth_ = theFontMetrics(mi.base.font).width(full_label_);
        // add some space to separate the label from the inset text
-       labelwidth_ += 2 * TEXT_TO_INSET_OFFSET;
+       labelwidth_ += leftOffset(mi.base.bv) + rightOffset(mi.base.bv);
        dim.wid = labelwidth_;
        Dimension textdim;
        // Correct for button and label width
@@ -131,8 +131,8 @@ void InsetCaption::metrics(MetricsInfo & mi, Dimension & dim) const
 void InsetCaption::drawBackground(PainterInfo & pi, int x, int y) const
 {
        TextMetrics & tm = pi.base.bv->textMetrics(&text());
-       int const h = tm.height() + 2 * TEXT_TO_INSET_OFFSET;
-       int const yy = y - TEXT_TO_INSET_OFFSET - tm.ascent();
+       int const h = tm.height() + topOffset(pi.base.bv) + bottomOffset(pi.base.bv);
+       int const yy = y - topOffset(pi.base.bv) - tm.ascent();
        pi.pain.fillRectangle(x, yy, labelwidth_, h, pi.backgroundColor(this));
 }
 
@@ -150,7 +150,7 @@ void InsetCaption::draw(PainterInfo & pi, int x, int y) const
        FontInfo tmpfont = pi.base.font;
        pi.base.font = pi.base.bv->buffer().params().getFont().fontInfo();
        pi.base.font.setColor(pi.textColor(pi.base.font.color()).baseColor);
-       int const xx = x + TEXT_TO_INSET_OFFSET;
+       int const xx = x + leftOffset(pi.base.bv);
        pi.pain.text(xx, y, full_label_, pi.base.font);
        InsetText::draw(pi, x + labelwidth_, y);
        pi.base.font = tmpfont;
index cb9e23a33d7b663b027dc6642430240b7d82c337..f0b32c8a3f21f0f9a2356c8d64dbcb024c43a476 100644 (file)
@@ -164,7 +164,7 @@ Dimension InsetCollapsible::dimensionCollapsed(BufferView const & bv) const
        FontInfo labelfont(getLabelfont());
        labelfont.realize(sane_font);
        theFontMetrics(labelfont).buttonText(
-               buttonLabel(bv), TEXT_TO_INSET_OFFSET, dim.wid, dim.asc, dim.des);
+               buttonLabel(bv), Inset::textOffset(&bv), dim.wid, dim.asc, dim.des);
        return dim;
 }
 
@@ -221,7 +221,7 @@ void InsetCollapsible::metrics(MetricsInfo & mi, Dimension & dim) const
                                dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
                                dim.asc = textdim.asc;
                        } else {
-                               dim.des += textdim.height() + TEXT_TO_INSET_OFFSET;
+                               dim.des += textdim.height() + topOffset(mi.base.bv);
                                dim.wid = max(dim.wid, textdim.wid);
                        }
                }
@@ -264,7 +264,7 @@ void InsetCollapsible::draw(PainterInfo & pi, int x, int y) const
                labelfont.realize(pi.base.font);
                pi.pain.buttonText(x, y, buttonLabel(bv), labelfont,
                                   view_[&bv].mouse_hover_ ? Color_buttonhoverbg : Color_buttonbg,
-                                  Color_buttonframe, TEXT_TO_INSET_OFFSET);
+                                  Color_buttonframe, Inset::textOffset(pi.base.bv));
                // Draw the change tracking cue on the label, unless RowPainter already
                // takes care of it.
                if (canPaintChange(bv))
@@ -325,8 +325,8 @@ void InsetCollapsible::draw(PainterInfo & pi, int x, int y) const
                // Colour the frame according to the change type. (Like for tables.)
                Color colour = pi.change.changed() ? pi.change.color()
                                                    : Color_foreground;
-               const int xx1 = x + TEXT_TO_INSET_OFFSET - 1;
-               const int xx2 = x + textdim.wid - TEXT_TO_INSET_OFFSET + 1;
+               const int xx1 = x + leftOffset(pi.base.bv) - 1;
+               const int xx2 = x + textdim.wid - rightOffset(pi.base.bv) + 1;
                pi.pain.line(xx1, y + desc - 4,
                             xx1, y + desc, colour);
                if (status_ == Open)
index c811cf6ff8fa3e34cea92cd3ba6b40067790092b..ac2b7d8422a11fe540784956c3edfaefc662935f 100644 (file)
@@ -184,8 +184,8 @@ void InsetIPA::metrics(MetricsInfo & mi, Dimension & dim) const
                dim.wid = max(dim.wid, 4);
                dim.asc = max(dim.asc, 4);
 
-               dim.asc += TEXT_TO_INSET_OFFSET;
-               dim.des += TEXT_TO_INSET_OFFSET;
+               dim.asc += topOffset(mi.base.bv);
+               dim.des += bottomOffset(mi.base.bv);
                // insert a one pixel gap
                dim.wid += 1;
                Dimension dim_dummy;
index dbce804356bf29480b0a8abe5068e5d41612ae80..f8c3c3895568aaa9febc6a8ea022a62f6f66d7c7 100644 (file)
@@ -194,10 +194,10 @@ void InsetPhantom::draw(PainterInfo & pi, int x, int y) const
                //       |   |        |   |
                //      x1  x2       x3  x4
 
-               x = x + TEXT_TO_INSET_OFFSET;
+               x += leftOffset(pi.base.bv);
                int const x1 = x;
                int const x2 = x + arrow_size;
-               int const x4 = x + dim.wid - 2 * TEXT_TO_INSET_OFFSET;
+               int const x4 = x + dim.wid - leftOffset(pi.base.bv) - rightOffset(pi.base.bv);
                int const x3 = x4 - arrow_size;
 
                int const y2 = y + (dim.des - dim.asc) / 2;
index 7b89e1ba6b9ecd851137686b84b316f88eda653b..1a70b1e17a2b1ece64adf280bcf5fbc451c5d034 100644 (file)
@@ -170,8 +170,8 @@ void InsetPreview::metrics(MetricsInfo & mi, Dimension & dim) const
                dim.wid = max(dim.wid, 4);
                dim.asc = max(dim.asc, 4);
 
-               dim.asc += TEXT_TO_INSET_OFFSET;
-               dim.des += TEXT_TO_INSET_OFFSET;
+               dim.asc += topOffset(mi.base.bv);
+               dim.des += bottomOffset(mi.base.bv);
                // insert a one pixel gap
                dim.wid += 1;
                Dimension dim_dummy;
index 1c4ed5dfc2d45b3de5a83a1dabd935a9cdc0fa20..ea76177ed27ddf40af444f4b1c6b438a3361bd12 100644 (file)
@@ -4180,10 +4180,11 @@ docstring InsetTableCell::xhtml(XHTMLStream & xs, OutputParams const & rp) const
 void InsetTableCell::metrics(MetricsInfo & mi, Dimension & dim) const
 {
        TextMetrics & tm = mi.base.bv->textMetrics(&text());
+       int const horiz_offset = leftOffset(mi.base.bv) + rightOffset(mi.base.bv);
 
        // Hand font through to contained lyxtext:
        tm.font_.fontInfo() = mi.base.font;
-       mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
+       mi.base.textwidth -= horiz_offset;
 
        // This can happen when a layout has a left and right margin,
        // and the view is made very narrow. We can't do better than
@@ -4197,10 +4198,10 @@ void InsetTableCell::metrics(MetricsInfo & mi, Dimension & dim) const
                tm.metrics(mi, dim, mi.base.textwidth, false);
        else
                tm.metrics(mi, dim, 0, false);
-       mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
-       dim.asc += TEXT_TO_INSET_OFFSET;
-       dim.des += TEXT_TO_INSET_OFFSET;
-       dim.wid += 2 * TEXT_TO_INSET_OFFSET;
+       mi.base.textwidth += horiz_offset;
+       dim.asc += topOffset(mi.base.bv);
+       dim.des += bottomOffset(mi.base.bv);
+       dim.wid += horiz_offset;
 }
 
 
@@ -4391,9 +4392,9 @@ void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
                        tabular.cell_info[r][c].decimal_width = decimal_width;
 
                        // with LYX_VALIGN_BOTTOM the descent is relative to the last par
-                       // = descent of text in last par + TEXT_TO_INSET_OFFSET:
+                       // = descent of text in last par + bottomOffset:
                        int const lastpardes = tm.last().second->descent()
-                               + TEXT_TO_INSET_OFFSET;
+                               + bottomOffset(mi.base.bv);
                        int offset = 0;
                        switch (tabular.getVAlignment(cell)) {
                                case Tabular::LYX_VALIGN_TOP:
index e7ec78a8f9023e0743cafbb4ec9663f01babc4e0..47b9fc3a4894e5e85bc3b9a81f0ee7ebb6b4282a 100644 (file)
@@ -141,9 +141,9 @@ Dimension const InsetText::dimensionHelper(BufferView const & bv) const
 {
        TextMetrics const & tm = bv.textMetrics(&text_);
        Dimension dim = tm.dim();
-       dim.wid += 2 * TEXT_TO_INSET_OFFSET;
-       dim.des += TEXT_TO_INSET_OFFSET;
-       dim.asc += TEXT_TO_INSET_OFFSET;
+       dim.wid += leftOffset(&bv) + rightOffset(&bv);
+       dim.des += bottomOffset(&bv);
+       dim.asc += topOffset(&bv);
        return dim;
 }
 
@@ -190,9 +190,11 @@ void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
 
        //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
 
+       int const horiz_offset = leftOffset(mi.base.bv) + rightOffset(mi.base.bv);
+
        // Hand font through to contained lyxtext:
        tm.font_.fontInfo() = mi.base.font;
-       mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
+       mi.base.textwidth -= horiz_offset;
 
        // This can happen when a layout has a left and right margin,
        // and the view is made very narrow. We can't do better than
@@ -204,10 +206,10 @@ void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
                tm.metrics(mi, dim, mi.base.textwidth);
        else
                tm.metrics(mi, dim);
-       mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
-       dim.asc += TEXT_TO_INSET_OFFSET;
-       dim.des += TEXT_TO_INSET_OFFSET;
-       dim.wid += 2 * TEXT_TO_INSET_OFFSET;
+       mi.base.textwidth += horiz_offset;
+       dim.asc += topOffset(mi.base.bv);
+       dim.des += bottomOffset(mi.base.bv);
+       dim.wid += horiz_offset;
 }
 
 
@@ -215,10 +217,11 @@ void InsetText::draw(PainterInfo & pi, int x, int y) const
 {
        TextMetrics & tm = pi.base.bv->textMetrics(&text_);
 
-       int const w = tm.width() + TEXT_TO_INSET_OFFSET;
-       int const yframe = y - TEXT_TO_INSET_OFFSET - tm.ascent();
-       int const h = tm.height() + 2 * TEXT_TO_INSET_OFFSET;
-       int const xframe = x + TEXT_TO_INSET_OFFSET / 2;
+       int const horiz_offset = leftOffset(pi.base.bv) + rightOffset(pi.base.bv);
+       int const w = tm.width() + (horiz_offset - horiz_offset / 2);
+       int const yframe = y - topOffset(pi.base.bv) - tm.ascent();
+       int const h = tm.height() + topOffset(pi.base.bv) + bottomOffset(pi.base.bv);
+       int const xframe = x + leftOffset(pi.base.bv) / 2;
        bool change_drawn = false;
        if (pi.full_repaint)
                        pi.pain.fillRectangle(xframe, yframe, w, h,
@@ -229,7 +232,7 @@ void InsetText::draw(PainterInfo & pi, int x, int y) const
                                            pi.backgroundColor(this, false));
                // The change tracking cue must not be inherited
                Changer dummy2 = make_change(pi.change, Change());
-               tm.draw(pi, x + TEXT_TO_INSET_OFFSET, y);
+               tm.draw(pi, x + leftOffset(pi.base.bv), y);
        }
 
        if (drawFrame_) {
@@ -699,7 +702,7 @@ void InsetText::getArgs(otexstream & os, OutputParams const & runparams_in,
 void InsetText::cursorPos(BufferView const & bv,
                CursorSlice const & sl, bool boundary, int & x, int & y) const
 {
-       x = bv.textMetrics(&text_).cursorX(sl, boundary) + TEXT_TO_INSET_OFFSET;
+       x = bv.textMetrics(&text_).cursorX(sl, boundary) + leftOffset(&bv);
        y = bv.textMetrics(&text_).cursorY(sl, boundary);
 }
 
index 5320507371d844cc3d4c70f78a17571a53a65f44..44fd17a1b87c390246e9ccdadfd1f4536f94433b 100644 (file)
@@ -47,7 +47,7 @@ void RenderButton::metrics(MetricsInfo & mi, Dimension & dim) const
        font.decSize();
        frontend::FontMetrics const & fm = theFontMetrics(font);
 
-       fm.buttonText(text_, Inset::TEXT_TO_INSET_OFFSET, dim.wid, dim.asc, dim.des);
+       fm.buttonText(text_, Inset::textOffset(mi.base.bv), dim.wid, dim.asc, dim.des);
 
        dim_ = dim;
 }
@@ -63,11 +63,11 @@ void RenderButton::draw(PainterInfo & pi, int x, int y) const
        if (editable_) {
                pi.pain.buttonText(x, y, text_, font,
                                   renderState() ? Color_buttonhoverbg : Color_buttonbg,
-                                  Color_buttonframe, Inset::TEXT_TO_INSET_OFFSET);
+                                  Color_buttonframe, Inset::textOffset(pi.base.bv));
        } else {
                pi.pain.buttonText(x, y, text_, font,
                                   Color_commandbg, Color_commandframe,
-                                  Inset::TEXT_TO_INSET_OFFSET);
+                                  Inset::textOffset(pi.base.bv));
        }
 }
 
index 743c60f7c25ae3019387dde7a3ed855bc8fa8050..fe2d9b9eb4d97fb02e3b75fd6a138223d567ceb1 100644 (file)
@@ -147,7 +147,7 @@ void RenderGraphic::metrics(MetricsInfo & mi, Dimension & dim) const
 
        bool const image_ready = displayGraphic(params_) && readyToDisplay(loader_);
        if (image_ready) {
-               dim.wid = loader_.image()->width() + 2 * Inset::TEXT_TO_INSET_OFFSET;
+               dim.wid = loader_.image()->width() + 2 * Inset::textOffset(mi.base.bv);
                dim.asc = loader_.image()->height();
                dim_ = dim;
                return;
@@ -190,9 +190,9 @@ void RenderGraphic::draw(PainterInfo & pi, int x, int y) const
 {
        // This will draw the graphics. If the graphics has not been
        // loaded yet, we draw just a rectangle.
-       int const x1 = x + Inset::TEXT_TO_INSET_OFFSET;
+       int const x1 = x + Inset::textOffset(pi.base.bv);
        int const y1 = y - dim_.asc;
-       int const w = dim_.wid - 2 * Inset::TEXT_TO_INSET_OFFSET;
+       int const w = dim_.wid - 2 * Inset::textOffset(pi.base.bv);
        int const h = dim_.height();
 
        if (displayGraphic(params_) && readyToDisplay(loader_))
index d626b3c50c6fdc78885ef1b2d46f05c58df3ba0a..0b17d2f83c5ff40d9d938ba66642d01df4c4d87a 100644 (file)
@@ -166,7 +166,7 @@ void RenderPreview::draw(PainterInfo & pi, int x, int y) const
                pi.pain.image(x, y - dim_.asc, dim_.wid, dim_.height(),
                              *image);
        } else {
-               int const offset = Inset::TEXT_TO_INSET_OFFSET;
+               int const offset = Inset::textOffset(pi.base.bv);
 
                pi.pain.rectangle(x + offset,
                                  y - dim_.asc,