Use the same code for editable and non-editable buttons

This removes the use of rectText in RenderButton. The fact that this
gave different spacing than buttonText was a problem.

Now buttonText requires to specify
* the offset, so that INSET_TO_TEXT_OFFSET is not used anymore in
  src/frontends/, which will be useful later.
* the background and frame color, in replacement for the hover state.

Remove the methods button() and buttonFrame() from GuiPainter.

Remove some unused header files.

Fixes bug #10704.
This commit is contained in:
Jean-Marc Lasgouttes 2017-06-15 15:30:23 +02:00
parent 530fec453f
commit 62b06c64ed
8 changed files with 34 additions and 67 deletions

View File

@ -125,9 +125,10 @@ public:
int & descent) const = 0;
/**
* fill in width,ascent,descent with the values for the
* given string in the font for a button.
* given string in the font for a button with given offset.
*/
virtual void buttonText(docstring const & str,
const int offset,
int & width,
int & ascent,
int & descent) const = 0;

View File

@ -123,9 +123,6 @@ public:
/// draw a pixel
virtual void point(int x, int y, Color) = 0;
/// draw a filled rectangle with the shape of a 3D button
virtual void button(int x, int y, int w, int h, bool mouseHover) = 0;
/// draw an image from the image cache
virtual void image(int x, int y, int w, int h,
graphics::Image const & image) = 0;
@ -172,7 +169,7 @@ public:
/// draw a string and enclose it inside a button frame
virtual void buttonText(int x, int baseline, docstring const & s,
FontInfo const & font, bool mouseHover) = 0;
FontInfo const & font, Color back, Color frame, int offset) = 0;
/// draw a character of a preedit string for cjk support.
virtual int preeditText(int x, int y,

View File

@ -16,10 +16,6 @@
#include "qt_helpers.h"
#include "Dimension.h"
#include "Language.h"
#include "LyXRC.h"
#include "insets/Inset.h"
#include "support/convert.h"
#include "support/lassert.h"
@ -448,19 +444,20 @@ bool GuiFontMetrics::breakAt(docstring & s, int & x, bool const rtl, bool const
void GuiFontMetrics::rectText(docstring const & str,
int & w, int & ascent, int & descent) const
{
static int const d = Inset::TEXT_TO_INSET_OFFSET / 2;
// FIXME: let offset depend on font (this is Inset::TEXT_TO_OFFSET)
int const offset = 4;
w = width(str) + Inset::TEXT_TO_INSET_OFFSET;
ascent = metrics_.ascent() + d;
descent = metrics_.descent() + d;
w = width(str) + offset;
ascent = metrics_.ascent() + offset / 2;
descent = metrics_.descent() + offset / 2;
}
void GuiFontMetrics::buttonText(docstring const & str,
void GuiFontMetrics::buttonText(docstring const & str, const int offset,
int & w, int & ascent, int & descent) const
{
rectText(str, w, ascent, descent);
w += Inset::TEXT_TO_INSET_OFFSET;
w += offset;
}

View File

@ -58,6 +58,7 @@ public:
int & ascent,
int & descent) const;
virtual void buttonText(docstring const & str,
const int offset,
int & width,
int & ascent,
int & descent) const;

View File

@ -21,11 +21,8 @@
#include "qt_helpers.h"
#include "Font.h"
#include "Language.h"
#include "LyXRC.h"
#include "insets/Inset.h"
#include "support/debug.h"
#include "support/lassert.h"
#include "support/lyxlib.h"
@ -557,31 +554,10 @@ void GuiPainter::textDecoration(FontInfo const & f, int x, int y, int width)
static int max(int a, int b) { return a > b ? a : b; }
void GuiPainter::button(int x, int y, int w, int h, bool mouseHover)
{
if (mouseHover)
fillRectangle(x, y, w, h, Color_buttonhoverbg);
else
fillRectangle(x, y, w, h, Color_buttonbg);
buttonFrame(x, y, w, h);
}
void GuiPainter::buttonFrame(int x, int y, int w, int h)
{
line(x, y, x, y + h - 1, Color_buttonframe);
line(x - 1 + w, y, x - 1 + w, y + h - 1, Color_buttonframe);
line(x, y - 1, x - 1 + w, y - 1, Color_buttonframe);
line(x, y + h - 1, x - 1 + w, y + h - 1, Color_buttonframe);
}
void GuiPainter::rectText(int x, int y, docstring const & str,
FontInfo const & font, Color back, Color frame)
{
int width;
int ascent;
int descent;
int width, ascent, descent;
FontMetrics const & fm = theFontMetrics(font);
fm.rectText(str, width, ascent, descent);
@ -593,24 +569,25 @@ void GuiPainter::rectText(int x, int y, docstring const & str,
if (frame != Color_none)
rectangle(x, y - ascent, width, ascent + descent, frame);
// FIXME: let offset depend on font
text(x + 3, y, str, font);
}
void GuiPainter::buttonText(int x, int y, docstring const & str,
FontInfo const & font, bool mouseHover)
void GuiPainter::buttonText(int x, int baseline, docstring const & s,
FontInfo const & font, Color back, Color frame, int offset)
{
int width;
int ascent;
int descent;
int width, ascent, descent;
FontMetrics const & fm = theFontMetrics(font);
fm.buttonText(str, width, ascent, descent);
fm.buttonText(s, offset, width, ascent, descent);
static int const d = Inset::TEXT_TO_INSET_OFFSET / 2;
static int const d = offset / 2;
button(x + d, y - ascent, width - Inset::TEXT_TO_INSET_OFFSET, descent + ascent, mouseHover);
text(x + Inset::TEXT_TO_INSET_OFFSET, y, str, font);
fillRectangle(x + d + 1, baseline - ascent + 1, width - offset - 1,
ascent + descent - 1, back);
rectangle(x + d, baseline - ascent, width - offset, ascent + descent, frame);
text(x + offset, baseline, s, font);
}

View File

@ -130,7 +130,7 @@ public:
/// draw a string and enclose it inside a button frame
virtual void buttonText(int x, int baseline, docstring const & s,
FontInfo const & font, bool mouseHover);
FontInfo const & font, Color back, Color frame, int offset);
/// start monochrome painting mode, i.e. map every color into [min,max]
virtual void enterMonochromeMode(Color const & min,
@ -147,9 +147,6 @@ public:
virtual void rectText(int x, int baseline, docstring const & str,
FontInfo const & font, Color back, Color frame);
/// draw a filled rectangle with the shape of a 3D button
virtual void button(int x, int y, int w, int h, bool mouseHover);
/// draw a character of a preedit string for cjk support.
virtual int preeditText(int x, int y,
char_type c, FontInfo const & f, preedit_style style);
@ -177,9 +174,6 @@ private:
void doubleUnderline(FontInfo const & f,
int x, int y, int width);
/// draw a bevelled button border
void buttonFrame(int x, int y, int w, int h);
/// set pen parameters
void setQPainterPen(QColor const & col,
line_style ls = line_solid, int lw = thin_line);

View File

@ -148,7 +148,7 @@ Dimension InsetCollapsable::dimensionCollapsed(BufferView const & bv) const
FontInfo labelfont(getLabelfont());
labelfont.realize(sane_font);
theFontMetrics(labelfont).buttonText(
buttonLabel(bv), dim.wid, dim.asc, dim.des);
buttonLabel(bv), TEXT_TO_INSET_OFFSET, dim.wid, dim.asc, dim.des);
return dim;
}
@ -247,7 +247,8 @@ void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
labelfont.setColor(labelColor());
labelfont.realize(pi.base.font);
pi.pain.buttonText(x, y, buttonLabel(bv), labelfont,
view_[&bv].mouse_hover_);
view_[&bv].mouse_hover_ ? Color_buttonhoverbg : Color_buttonbg,
Color_buttonframe, TEXT_TO_INSET_OFFSET);
// Draw the change tracking cue on the label, unless RowPainter already
// takes care of it.
if (canPaintChange(bv))

View File

@ -45,13 +45,9 @@ void RenderButton::metrics(MetricsInfo & mi, Dimension & dim) const
{
FontInfo font = inherit_font_ ? mi.base.font : sane_font;
font.decSize();
frontend::FontMetrics const & fm =
theFontMetrics(font);
frontend::FontMetrics const & fm = theFontMetrics(font);
if (editable_)
fm.buttonText(text_, dim.wid, dim.asc, dim.des);
else
fm.rectText(text_, dim.wid, dim.asc, dim.des);
fm.buttonText(text_, Inset::TEXT_TO_INSET_OFFSET, dim.wid, dim.asc, dim.des);
dim_ = dim;
}
@ -65,10 +61,13 @@ void RenderButton::draw(PainterInfo & pi, int x, int y) const
font.decSize();
if (editable_) {
pi.pain.buttonText(x, y, text_, font, renderState());
pi.pain.buttonText(x, y, text_, font,
renderState() ? Color_buttonhoverbg : Color_buttonbg,
Color_buttonframe, Inset::TEXT_TO_INSET_OFFSET);
} else {
pi.pain.rectText(x, y, text_, font,
Color_commandbg, Color_commandframe);
pi.pain.buttonText(x, y, text_, font,
Color_commandbg, Color_commandframe,
Inset::TEXT_TO_INSET_OFFSET);
}
}