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).
This commit is contained in:
Jean-Marc Lasgouttes 2020-01-13 22:09:06 +01:00
parent 8bd47bda8c
commit 0ef2c192c0
13 changed files with 62 additions and 49 deletions

View File

@ -479,10 +479,10 @@ void RowPainter::paintLast() const
int x = 0; int x = 0;
if (row_.isRTL()) { if (row_.isRTL()) {
int const normal_x = nestMargin() + changebarMargin(); 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 { } else {
int const normal_x = tm_.width() - row_.right_margin 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()); x = max(normal_x, row_.width());
} }

View 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 begining/end of row. However, it will not work if
// the caret has a ridiculous width like 6. (see ticket // the caret has a ridiculous width like 6. (see ticket
// #10797) // #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(), y - row.ascent(),
width() + 2 * Inset::TEXT_TO_INSET_OFFSET, width() + 2 * Inset::textOffset(pi.base.bv),
row.height(), pi.background_color); row.height(), pi.background_color);
} }

View File

@ -202,6 +202,17 @@ public:
/// https://www.mail-archive.com/lyx-devel@lists.lyx.org/msg199001.html /// https://www.mail-archive.com/lyx-devel@lists.lyx.org/msg199001.html
virtual Inset * editXY(Cursor & cur, int x, int y); 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 /// compute the size of the object returned in dim
virtual void metrics(MetricsInfo & mi, Dimension & dim) const = 0; virtual void metrics(MetricsInfo & mi, Dimension & dim) const = 0;
/// draw inset and update (xo, yo)-cache /// draw inset and update (xo, yo)-cache
@ -610,8 +621,6 @@ public:
virtual ColorCode backgroundColor(PainterInfo const &) const; virtual ColorCode backgroundColor(PainterInfo const &) const;
/// ///
virtual ColorCode labelColor() const; virtual ColorCode labelColor() const;
//
enum { TEXT_TO_INSET_OFFSET = 4 };
/// Determine the action of backspace and delete: do we select instead of /// Determine the action of backspace and delete: do we select instead of
/// deleting if not already selected? /// deleting if not already selected?

View File

@ -114,7 +114,7 @@ void InsetCaption::metrics(MetricsInfo & mi, Dimension & dim) const
mi.base.font = mi.base.bv->buffer().params().getFont().fontInfo(); mi.base.font = mi.base.bv->buffer().params().getFont().fontInfo();
labelwidth_ = theFontMetrics(mi.base.font).width(full_label_); labelwidth_ = theFontMetrics(mi.base.font).width(full_label_);
// add some space to separate the label from the inset text // 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_; dim.wid = labelwidth_;
Dimension textdim; Dimension textdim;
// Correct for button and label width // 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 void InsetCaption::drawBackground(PainterInfo & pi, int x, int y) const
{ {
TextMetrics & tm = pi.base.bv->textMetrics(&text()); TextMetrics & tm = pi.base.bv->textMetrics(&text());
int const h = tm.height() + 2 * TEXT_TO_INSET_OFFSET; int const h = tm.height() + topOffset(pi.base.bv) + bottomOffset(pi.base.bv);
int const yy = y - TEXT_TO_INSET_OFFSET - tm.ascent(); int const yy = y - topOffset(pi.base.bv) - tm.ascent();
pi.pain.fillRectangle(x, yy, labelwidth_, h, pi.backgroundColor(this)); 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; FontInfo tmpfont = pi.base.font;
pi.base.font = pi.base.bv->buffer().params().getFont().fontInfo(); pi.base.font = pi.base.bv->buffer().params().getFont().fontInfo();
pi.base.font.setColor(pi.textColor(pi.base.font.color()).baseColor); 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); pi.pain.text(xx, y, full_label_, pi.base.font);
InsetText::draw(pi, x + labelwidth_, y); InsetText::draw(pi, x + labelwidth_, y);
pi.base.font = tmpfont; pi.base.font = tmpfont;

View File

@ -164,7 +164,7 @@ Dimension InsetCollapsible::dimensionCollapsed(BufferView const & bv) const
FontInfo labelfont(getLabelfont()); FontInfo labelfont(getLabelfont());
labelfont.realize(sane_font); labelfont.realize(sane_font);
theFontMetrics(labelfont).buttonText( 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; 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.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
dim.asc = textdim.asc; dim.asc = textdim.asc;
} else { } else {
dim.des += textdim.height() + TEXT_TO_INSET_OFFSET; dim.des += textdim.height() + topOffset(mi.base.bv);
dim.wid = max(dim.wid, textdim.wid); 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); labelfont.realize(pi.base.font);
pi.pain.buttonText(x, y, buttonLabel(bv), labelfont, pi.pain.buttonText(x, y, buttonLabel(bv), labelfont,
view_[&bv].mouse_hover_ ? Color_buttonhoverbg : Color_buttonbg, 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 // Draw the change tracking cue on the label, unless RowPainter already
// takes care of it. // takes care of it.
if (canPaintChange(bv)) 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.) // Colour the frame according to the change type. (Like for tables.)
Color colour = pi.change.changed() ? pi.change.color() Color colour = pi.change.changed() ? pi.change.color()
: Color_foreground; : Color_foreground;
const int xx1 = x + TEXT_TO_INSET_OFFSET - 1; const int xx1 = x + leftOffset(pi.base.bv) - 1;
const int xx2 = x + textdim.wid - TEXT_TO_INSET_OFFSET + 1; const int xx2 = x + textdim.wid - rightOffset(pi.base.bv) + 1;
pi.pain.line(xx1, y + desc - 4, pi.pain.line(xx1, y + desc - 4,
xx1, y + desc, colour); xx1, y + desc, colour);
if (status_ == Open) if (status_ == Open)

View File

@ -184,8 +184,8 @@ void InsetIPA::metrics(MetricsInfo & mi, Dimension & dim) const
dim.wid = max(dim.wid, 4); dim.wid = max(dim.wid, 4);
dim.asc = max(dim.asc, 4); dim.asc = max(dim.asc, 4);
dim.asc += TEXT_TO_INSET_OFFSET; dim.asc += topOffset(mi.base.bv);
dim.des += TEXT_TO_INSET_OFFSET; dim.des += bottomOffset(mi.base.bv);
// insert a one pixel gap // insert a one pixel gap
dim.wid += 1; dim.wid += 1;
Dimension dim_dummy; Dimension dim_dummy;

View File

@ -194,10 +194,10 @@ void InsetPhantom::draw(PainterInfo & pi, int x, int y) const
// | | | | // | | | |
// x1 x2 x3 x4 // x1 x2 x3 x4
x = x + TEXT_TO_INSET_OFFSET; x += leftOffset(pi.base.bv);
int const x1 = x; int const x1 = x;
int const x2 = x + arrow_size; 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 x3 = x4 - arrow_size;
int const y2 = y + (dim.des - dim.asc) / 2; int const y2 = y + (dim.des - dim.asc) / 2;

View File

@ -170,8 +170,8 @@ void InsetPreview::metrics(MetricsInfo & mi, Dimension & dim) const
dim.wid = max(dim.wid, 4); dim.wid = max(dim.wid, 4);
dim.asc = max(dim.asc, 4); dim.asc = max(dim.asc, 4);
dim.asc += TEXT_TO_INSET_OFFSET; dim.asc += topOffset(mi.base.bv);
dim.des += TEXT_TO_INSET_OFFSET; dim.des += bottomOffset(mi.base.bv);
// insert a one pixel gap // insert a one pixel gap
dim.wid += 1; dim.wid += 1;
Dimension dim_dummy; Dimension dim_dummy;

View File

@ -4180,10 +4180,11 @@ docstring InsetTableCell::xhtml(XHTMLStream & xs, OutputParams const & rp) const
void InsetTableCell::metrics(MetricsInfo & mi, Dimension & dim) const void InsetTableCell::metrics(MetricsInfo & mi, Dimension & dim) const
{ {
TextMetrics & tm = mi.base.bv->textMetrics(&text()); 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: // Hand font through to contained lyxtext:
tm.font_.fontInfo() = mi.base.font; 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, // 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 // 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); tm.metrics(mi, dim, mi.base.textwidth, false);
else else
tm.metrics(mi, dim, 0, false); tm.metrics(mi, dim, 0, false);
mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET; mi.base.textwidth += horiz_offset;
dim.asc += TEXT_TO_INSET_OFFSET; dim.asc += topOffset(mi.base.bv);
dim.des += TEXT_TO_INSET_OFFSET; dim.des += bottomOffset(mi.base.bv);
dim.wid += 2 * TEXT_TO_INSET_OFFSET; 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; tabular.cell_info[r][c].decimal_width = decimal_width;
// with LYX_VALIGN_BOTTOM the descent is relative to the last par // 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() int const lastpardes = tm.last().second->descent()
+ TEXT_TO_INSET_OFFSET; + bottomOffset(mi.base.bv);
int offset = 0; int offset = 0;
switch (tabular.getVAlignment(cell)) { switch (tabular.getVAlignment(cell)) {
case Tabular::LYX_VALIGN_TOP: case Tabular::LYX_VALIGN_TOP:

View File

@ -141,9 +141,9 @@ Dimension const InsetText::dimensionHelper(BufferView const & bv) const
{ {
TextMetrics const & tm = bv.textMetrics(&text_); TextMetrics const & tm = bv.textMetrics(&text_);
Dimension dim = tm.dim(); Dimension dim = tm.dim();
dim.wid += 2 * TEXT_TO_INSET_OFFSET; dim.wid += leftOffset(&bv) + rightOffset(&bv);
dim.des += TEXT_TO_INSET_OFFSET; dim.des += bottomOffset(&bv);
dim.asc += TEXT_TO_INSET_OFFSET; dim.asc += topOffset(&bv);
return dim; return dim;
} }
@ -190,9 +190,11 @@ void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
//lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl; //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: // Hand font through to contained lyxtext:
tm.font_.fontInfo() = mi.base.font; 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, // 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 // 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); tm.metrics(mi, dim, mi.base.textwidth);
else else
tm.metrics(mi, dim); tm.metrics(mi, dim);
mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET; mi.base.textwidth += horiz_offset;
dim.asc += TEXT_TO_INSET_OFFSET; dim.asc += topOffset(mi.base.bv);
dim.des += TEXT_TO_INSET_OFFSET; dim.des += bottomOffset(mi.base.bv);
dim.wid += 2 * TEXT_TO_INSET_OFFSET; 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_); TextMetrics & tm = pi.base.bv->textMetrics(&text_);
int const w = tm.width() + TEXT_TO_INSET_OFFSET; int const horiz_offset = leftOffset(pi.base.bv) + rightOffset(pi.base.bv);
int const yframe = y - TEXT_TO_INSET_OFFSET - tm.ascent(); int const w = tm.width() + (horiz_offset - horiz_offset / 2);
int const h = tm.height() + 2 * TEXT_TO_INSET_OFFSET; int const yframe = y - topOffset(pi.base.bv) - tm.ascent();
int const xframe = x + TEXT_TO_INSET_OFFSET / 2; 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; bool change_drawn = false;
if (pi.full_repaint) if (pi.full_repaint)
pi.pain.fillRectangle(xframe, yframe, w, h, 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)); pi.backgroundColor(this, false));
// The change tracking cue must not be inherited // The change tracking cue must not be inherited
Changer dummy2 = make_change(pi.change, Change()); 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_) { if (drawFrame_) {
@ -699,7 +702,7 @@ void InsetText::getArgs(otexstream & os, OutputParams const & runparams_in,
void InsetText::cursorPos(BufferView const & bv, void InsetText::cursorPos(BufferView const & bv,
CursorSlice const & sl, bool boundary, int & x, int & y) const 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); y = bv.textMetrics(&text_).cursorY(sl, boundary);
} }

View File

@ -47,7 +47,7 @@ void RenderButton::metrics(MetricsInfo & mi, Dimension & dim) const
font.decSize(); font.decSize();
frontend::FontMetrics const & fm = theFontMetrics(font); 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; dim_ = dim;
} }
@ -63,11 +63,11 @@ void RenderButton::draw(PainterInfo & pi, int x, int y) const
if (editable_) { if (editable_) {
pi.pain.buttonText(x, y, text_, font, pi.pain.buttonText(x, y, text_, font,
renderState() ? Color_buttonhoverbg : Color_buttonbg, renderState() ? Color_buttonhoverbg : Color_buttonbg,
Color_buttonframe, Inset::TEXT_TO_INSET_OFFSET); Color_buttonframe, Inset::textOffset(pi.base.bv));
} else { } else {
pi.pain.buttonText(x, y, text_, font, pi.pain.buttonText(x, y, text_, font,
Color_commandbg, Color_commandframe, Color_commandbg, Color_commandframe,
Inset::TEXT_TO_INSET_OFFSET); Inset::textOffset(pi.base.bv));
} }
} }

View File

@ -147,7 +147,7 @@ void RenderGraphic::metrics(MetricsInfo & mi, Dimension & dim) const
bool const image_ready = displayGraphic(params_) && readyToDisplay(loader_); bool const image_ready = displayGraphic(params_) && readyToDisplay(loader_);
if (image_ready) { 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.asc = loader_.image()->height();
dim_ = dim; dim_ = dim;
return; 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 // This will draw the graphics. If the graphics has not been
// loaded yet, we draw just a rectangle. // 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 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(); int const h = dim_.height();
if (displayGraphic(params_) && readyToDisplay(loader_)) if (displayGraphic(params_) && readyToDisplay(loader_))

View 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(), pi.pain.image(x, y - dim_.asc, dim_.wid, dim_.height(),
*image); *image);
} else { } else {
int const offset = Inset::TEXT_TO_INSET_OFFSET; int const offset = Inset::textOffset(pi.base.bv);
pi.pain.rectangle(x + offset, pi.pain.rectangle(x + offset,
y - dim_.asc, y - dim_.asc,