]> git.lyx.org Git - features.git/commitdiff
Use the same code for editable and non-editable buttons
authorJean-Marc Lasgouttes <lasgouttes@lyx.org>
Thu, 15 Jun 2017 13:30:23 +0000 (15:30 +0200)
committerJean-Marc Lasgouttes <lasgouttes@lyx.org>
Thu, 15 Jun 2017 13:40:30 +0000 (15:40 +0200)
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.

src/frontends/FontMetrics.h
src/frontends/Painter.h
src/frontends/qt4/GuiFontMetrics.cpp
src/frontends/qt4/GuiFontMetrics.h
src/frontends/qt4/GuiPainter.cpp
src/frontends/qt4/GuiPainter.h
src/insets/InsetCollapsable.cpp
src/insets/RenderButton.cpp

index 8358e2eb1eadbf70204c7a3443006c4a828d3319..c02606c4384f3536c0759b13503bbc4413e30022 100644 (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;
index 64d83107b9a52caa209ccc5299cd8f4076f6b2ca..e515c5fe484dd1b89ffe4707d1533f5596313197 100644 (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,
index 8381dd1ee38396353cdb8f07290f38db4b3289ba..d1ee09a184f9378b872b81b3bcfcb424bf324975 100644 (file)
 #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;
 }
 
 
index 35f66fc5029b68173ccab6e23e79b4ffebe28a7c..3f50dfe5d29902d0516e2b27c3ca2ec32fa4d4e4 100644 (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;
index 93bf16c414e7bfc80b0af09295b7b321bcf67645..9daa8c13d3df7905957e92cd966810cabaa4a67a 100644 (file)
 #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);
 }
 
 
index fc61fa0a4b570d84829bd16aa104733769d60cd0..9657ed9770c401fe17063c8f62581b39567908dd 100644 (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);
index 92802b96d4bd0fb44b462694e00f1ebaffd12a16..fb3f23f8cbdea056edc6cab1bf21e66fda4f0a97 100644 (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))
index 7f983c5705a27ee2a162d746e5eb4592607dd32f..ed6e26b51e6d1d8c7bb9fd71ad0afaa351c823fd 100644 (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);
        }
 }