From 0ef2c192c02dfb4578c189e68a0951280ebeea47 Mon Sep 17 00:00:00 2001 From: Jean-Marc Lasgouttes Date: Mon, 13 Jan 2020 22:09:06 +0100 Subject: [PATCH] Replace the hardcoded TEXT_TO_INSET_OFFSET by tunable values 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). --- src/RowPainter.cpp | 4 ++-- src/TextMetrics.cpp | 4 ++-- src/insets/Inset.h | 13 +++++++++++-- src/insets/InsetCaption.cpp | 8 ++++---- src/insets/InsetCollapsible.cpp | 10 +++++----- src/insets/InsetIPA.cpp | 4 ++-- src/insets/InsetPhantom.cpp | 4 ++-- src/insets/InsetPreview.cpp | 4 ++-- src/insets/InsetTabular.cpp | 15 ++++++++------- src/insets/InsetText.cpp | 31 +++++++++++++++++-------------- src/insets/RenderButton.cpp | 6 +++--- src/insets/RenderGraphic.cpp | 6 +++--- src/insets/RenderPreview.cpp | 2 +- 13 files changed, 62 insertions(+), 49 deletions(-) diff --git a/src/RowPainter.cpp b/src/RowPainter.cpp index 13c3be0196..16ff195196 100644 --- a/src/RowPainter.cpp +++ b/src/RowPainter.cpp @@ -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()); } diff --git a/src/TextMetrics.cpp b/src/TextMetrics.cpp index 65a1aa7945..796e1bd25e 100644 --- a/src/TextMetrics.cpp +++ b/src/TextMetrics.cpp @@ -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); } diff --git a/src/insets/Inset.h b/src/insets/Inset.h index cffaf77c6e..ab35584e12 100644 --- a/src/insets/Inset.h +++ b/src/insets/Inset.h @@ -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? diff --git a/src/insets/InsetCaption.cpp b/src/insets/InsetCaption.cpp index 919ecd8f6d..43147ff864 100644 --- a/src/insets/InsetCaption.cpp +++ b/src/insets/InsetCaption.cpp @@ -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; diff --git a/src/insets/InsetCollapsible.cpp b/src/insets/InsetCollapsible.cpp index cb9e23a33d..f0b32c8a3f 100644 --- a/src/insets/InsetCollapsible.cpp +++ b/src/insets/InsetCollapsible.cpp @@ -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) diff --git a/src/insets/InsetIPA.cpp b/src/insets/InsetIPA.cpp index c811cf6ff8..ac2b7d8422 100644 --- a/src/insets/InsetIPA.cpp +++ b/src/insets/InsetIPA.cpp @@ -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; diff --git a/src/insets/InsetPhantom.cpp b/src/insets/InsetPhantom.cpp index dbce804356..f8c3c38955 100644 --- a/src/insets/InsetPhantom.cpp +++ b/src/insets/InsetPhantom.cpp @@ -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; diff --git a/src/insets/InsetPreview.cpp b/src/insets/InsetPreview.cpp index 7b89e1ba6b..1a70b1e17a 100644 --- a/src/insets/InsetPreview.cpp +++ b/src/insets/InsetPreview.cpp @@ -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; diff --git a/src/insets/InsetTabular.cpp b/src/insets/InsetTabular.cpp index 1c4ed5dfc2..ea76177ed2 100644 --- a/src/insets/InsetTabular.cpp +++ b/src/insets/InsetTabular.cpp @@ -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: diff --git a/src/insets/InsetText.cpp b/src/insets/InsetText.cpp index e7ec78a8f9..47b9fc3a48 100644 --- a/src/insets/InsetText.cpp +++ b/src/insets/InsetText.cpp @@ -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); } diff --git a/src/insets/RenderButton.cpp b/src/insets/RenderButton.cpp index 5320507371..44fd17a1b8 100644 --- a/src/insets/RenderButton.cpp +++ b/src/insets/RenderButton.cpp @@ -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)); } } diff --git a/src/insets/RenderGraphic.cpp b/src/insets/RenderGraphic.cpp index 743c60f7c2..fe2d9b9eb4 100644 --- a/src/insets/RenderGraphic.cpp +++ b/src/insets/RenderGraphic.cpp @@ -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_)) diff --git a/src/insets/RenderPreview.cpp b/src/insets/RenderPreview.cpp index d626b3c50c..0b17d2f83c 100644 --- a/src/insets/RenderPreview.cpp +++ b/src/insets/RenderPreview.cpp @@ -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,