ButtonStyler.h
1 /*
2  Copyright (C) 2016 Laurentiu-Gheorghe Crisan
3  Copyright (C) 2016 Marc Boris Duerner
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  As a special exception, you may use this file as part of a free
11  software library without restriction. Specifically, if other files
12  instantiate templates or use macros or inline functions from this
13  file, or you compile this file and link it with other files to
14  produce an executable, this file does not by itself cause the
15  resulting executable to be covered by the GNU General Public
16  License. This exception does not however invalidate any other
17  reasons why the executable file might be covered by the GNU Library
18  General Public License.
19 
20  This library is distributed in the hope that it will be useful,
21  but WITHOUT ANY WARRANTY; without even the implied warranty of
22  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23  Lesser General Public License for more details.
24 
25  You should have received a copy of the GNU Lesser General Public
26  License along with this library; if not, write to the:
27  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
28  Boston, MA 02110-1301 USA
29 */
30 
31 #ifndef Pt_Forms_ButtonStyler_h
32 #define Pt_Forms_ButtonStyler_h
33 
34 #include <Pt/Forms/Styler.h>
35 
36 namespace Pt {
37 
38 namespace Forms {
39 
45 class PT_FORMS_API ButtonState
46 {
47  public:
51 
54  bool isEnabled() const;
55 
58  void setEnabled(bool value);
59 
62  bool isHovered() const;
63 
66  void setHovered(bool value);
67 
70  bool isFocused() const;
71 
74  void setFocused(bool value);
75 
78  bool isPressed() const;
79 
82  void setPressed(bool value);
83 
86  bool isFlat() const;
87 
90  void setFlat(bool value);
91 
92  private:
93  bool _enabled;
94  bool _hovered;
95  bool _focused;
96  bool _pressed;
97  bool _flat;
98 };
99 
105 class PT_FORMS_API ButtonRenderer : public Renderer
106 {
107  public:
108  explicit ButtonRenderer(std::size_t refs = 0);
109 
110  virtual ~ButtonRenderer();
111 
115 
116  public:
123  Direction direction,
124  const Gfx::SizeF& iconSize,
125  const Gfx::SizeF& textSize);
126 
130  const Gfx::SizeF& contentSize);
131 
135  const Gfx::RectF& frameRect);
136 
140  const String& text,
141  const Gfx::PointF& textPos,
142  const Gfx::FontMetrics& fontMetrics,
143  String::size_type mnemonicIndex);
144 
147  const Painter& textPainter(PaintSurface& surface);
148 
155  const Gfx::RectF& contentRect,
156  Direction direction,
157  const Gfx::SizeF& iconSize,
158  const Gfx::SizeF& textSize,
159  Gfx::RectF& iconRect,
160  Gfx::RectF& textRect);
161 
164  void prepareIcon(const Gfx::Image& icon,
165  Pixmap& picture,
166  const ButtonState& state) const;
167 
174  const Gfx::RectF& rect,
175  const ButtonState& state);
176 
179  void renderChrome(PaintContext& context,
180  const Gfx::RectF& rect,
181  const ButtonState& state);
182 
185  void renderText(PaintContext& context,
186  const Gfx::RectF& rect,
187  const String& text,
188  const Gfx::PointF& pos,
189  const ButtonState& state);
190 
194  const Gfx::RectF& rect,
195  const Gfx::RectF& mnemonic,
196  const ButtonState& state);
197 
200  void renderIcon(PaintContext& context,
201  const Gfx::RectF& rect,
202  const Pixmap& picture,
203  const Gfx::PointF& pos,
204  const ButtonState& state);
205 
206  protected:
207  virtual ButtonRenderer* onCreate() const = 0;
208 
211  virtual void onReset(const StyleOptions& options) = 0;
212 
213  virtual Gfx::SizeF onMeasureFrame(PaintSurface& surface,
214  const Gfx::SizeF& contentSize) = 0;
215 
216  virtual Gfx::SizeF onMeasureContent(PaintSurface& surface,
217  Direction direction,
218  const Gfx::SizeF& iconSize,
219  const Gfx::SizeF& textSize) = 0;
220 
221  virtual Gfx::RectF onLayoutFrame(PaintSurface& surface,
222  const Gfx::RectF& frameRect) = 0;
223 
224  virtual Gfx::RectF onLayoutMnemonic(PaintSurface& surface,
225  const String& text,
226  const Gfx::PointF& textPos,
227  const Gfx::FontMetrics& fontMetrics,
228  String::size_type mnemonicIndex) = 0;
229 
230  virtual const Painter& onGetTextPainter(PaintSurface& surface) = 0;
231 
232  virtual void onLayoutContent(PaintSurface& surface,
233  const Gfx::RectF& contentRect,
234  Direction direction,
235  const Gfx::SizeF& iconSize,
236  const Gfx::SizeF& textSize,
237  Gfx::RectF& iconRect,
238  Gfx::RectF& textRect) = 0;
239 
240  virtual void onRenderBackground(PaintContext& context,
241  const Gfx::RectF& rect,
242  const ButtonState& state) = 0;
243 
244  virtual void onPrepareIcon(const Gfx::Image& icon,
245  Pixmap& picture,
246  const ButtonState& state) const = 0;
247 
248  virtual void onRenderChrome(PaintContext& context,
249  const Gfx::RectF& rect,
250  const ButtonState& state) = 0;
251 
252  virtual void onRenderText(PaintContext& context,
253  const Gfx::RectF& rect,
254  const String& text,
255  const Gfx::PointF& pos,
256  const ButtonState& state) = 0;
257 
258  virtual void onRenderMnemonic(PaintContext& context,
259  const Gfx::RectF& rect,
260  const Gfx::RectF& mnemonic,
261  const ButtonState& state) = 0;
262 
263  virtual void onRenderIcon(PaintContext& context,
264  const Gfx::RectF& rect,
265  const Pixmap& picture,
266  const Gfx::PointF& pos,
267  const ButtonState& state) = 0;
268 };
269 
272 class PT_FORMS_API ButtonStyler : public Styler
273 {
274  public:
278 
281  const Gfx::Brush& foreground() const;
282 
285  void setForeground(const Gfx::Brush& brush);
286 
289  const Gfx::Pen& contour() const;
290 
293  void setContour(const Gfx::Pen& pen);
294 
297  const Gfx::Color& accentColor() const;
298 
301  void setAccentColor(const Gfx::Color& color);
302 
305  const Gfx::Color& highlightColor() const;
306 
309  void setHighlightColor(const Gfx::Color& color);
310 
313  const Gfx::Color& textColor() const;
314 
317  void setTextColor(const Gfx::Color& color);
318 
321  Gfx::Font font() const;
322 
325  void setFont(const Gfx::Font& font);
326 
329  void setFontSize(std::size_t size);
330 
334 
338 
343  bool prepareIcon(const Gfx::Image& icon,
344  Pixmap& picture,
345  const ButtonState& state) const;
346 
349  void measureText(PaintSurface& surface,
350  const String& text,
351  Gfx::TextMetrics& textMetrics,
352  Gfx::FontMetrics& fontMetrics) const;
353 
357  Direction direction,
358  const Gfx::SizeF& iconSize,
359  const Gfx::SizeF& textSize) const;
360 
364  const Gfx::SizeF& contentSize) const;
365 
369  const Gfx::RectF& frameRect) const;
370 
374  const Gfx::RectF& contentRect,
375  Direction direction,
376  const Gfx::SizeF& iconSize,
377  const Gfx::SizeF& textSize,
378  Gfx::RectF& iconRect,
379  Gfx::RectF& textRect) const;
380 
384  const String& text,
385  const Gfx::PointF& textPos,
386  const Gfx::FontMetrics& fontMetrics,
387  String::size_type mnemonicIndex) const;
388 
392  const Gfx::RectF& rect,
393  const ButtonState& state) const;
394 
397  void renderChrome(PaintContext& context,
398  const Gfx::RectF& rect,
399  const ButtonState& state) const;
400 
403  void renderText(PaintContext& context,
404  const Gfx::RectF& rect,
405  const String& text,
406  const Gfx::PointF& pos,
407  const ButtonState& state) const;
408 
412  const Gfx::RectF& rect,
413  const Gfx::RectF& mnemonic,
414  const ButtonState& state) const;
415 
418  void renderIcon(PaintContext& context,
419  const Gfx::RectF& rect,
420  const Pixmap& picture,
421  const Gfx::PointF& pos,
422  const ButtonState& state) const;
423 
426  void setRenderer(ButtonRenderer* renderer = 0);
427 
433 
436  const StyleOptions& options() const;
437 
438  protected:
439  virtual StyleOptions& onBindOptions(const StyleOptions& styleOptions);
440 
441  virtual Renderer* onStyleRenderer(const Style& style);
442 
443  virtual Renderer* onCreateRenderer(const Style& style);
444 
445  private:
446  FacetPtr<ButtonRenderer> _renderer;
447  StyleOptions _options;
448 };
449 
450 } // namespace
451 
452 } // namespace
453 
454 #endif
Core module.
Definition: Allocator.h:33
Stores the widget-local visual state for a push button.
Definition: ButtonStyler.h:46
bool prepareIcon(const Gfx::Image &icon, Pixmap &picture, const ButtonState &state) const
Prepares picture from icon for state.
void setHighlightColor(const Gfx::Color &color)
Sets the widget-local highlight color to color.
void setFlat(bool value)
Sets whether the button uses flat rendering.
void renderIcon(PaintContext &context, const Gfx::RectF &rect, const Pixmap &picture, const Gfx::PointF &pos, const ButtonState &state) const
Renders picture at pos within rect for state.
Attributes for the drawing of outlines.
Definition: Pen.h:54
void renderMnemonic(PaintContext &context, const Gfx::RectF &rect, const Gfx::RectF &mnemonic, const ButtonState &state)
Draws the mnemonic underline within mnemonic, clipped to the button bounding rect.
void layoutContent(PaintSurface &surface, const Gfx::RectF &contentRect, Direction direction, const Gfx::SizeF &iconSize, const Gfx::SizeF &textSize, Gfx::RectF &iconRect, Gfx::RectF &textRect)
Computes the icon and text sub-rectangles within the content rect.
void prepareIcon(const Gfx::Image &icon, Pixmap &picture, const ButtonState &state) const
Converts icon to a state-appropriate pixmap written to picture.
void renderMnemonic(PaintContext &context, const Gfx::RectF &rect, const Gfx::RectF &mnemonic, const ButtonState &state) const
Renders the mnemonic underline within mnemonic for state.
void setContour(const Gfx::Pen &pen)
Sets the widget-local contour pen to pen.
Gfx::RectF layoutMnemonic(PaintSurface &surface, const String &text, const Gfx::PointF &textPos, const Gfx::FontMetrics &fontMetrics, String::size_type mnemonicIndex)
Computes the underline rectangle for the mnemonic character at mnemonicIndex.
Gfx::SizeF measureContent(PaintSurface &surface, Direction direction, const Gfx::SizeF &iconSize, const Gfx::SizeF &textSize) const
Measures icon and text content arranged by direction.
void setFontSize(std::size_t size)
Sets the widget-local font size to size.
void setForeground(const Gfx::Brush &brush)
Sets the widget-local foreground brush to brush.
Metrics that describe a font face at a given size.
Definition: FontMetrics.h:46
void renderIcon(PaintContext &context, const Gfx::RectF &rect, const Pixmap &picture, const Gfx::PointF &pos, const ButtonState &state)
Draws picture at pos, clipped to the button bounding rect.
Paint surface.
Definition: PaintSurface.h:44
bool isEnabled() const
Returns true if the button is currently enabled.
Slant
Describes the requested font slant.
Definition: Font.h:76
Styler.
Definition: Styler.h:47
bool isPressed() const
Returns true if the button is currently pressed.
2D painter for Forms paint surfaces.
Definition: Painter.h:46
Paint context.
Definition: PaintContext.h:45
virtual Renderer * onCreateRenderer(const Style &style)
Creates an independant style renderer.
const StyleOptions & options() const
Returns the bound effective button options.
Gfx::SizeF measureFrame(PaintSurface &surface, const Gfx::SizeF &contentSize)
Returns the outer size including button decoration for contentSize on surface.
bool isHovered() const
Returns true if the pointer is currently hovering the button.
Size with floating-point width and height.
Definition: Size.h:47
void setFontWeight(Gfx::Font::Weight weight)
Sets the widget-local font weight to weight.
void renderText(PaintContext &context, const Gfx::RectF &rect, const String &text, const Gfx::PointF &pos, const ButtonState &state)
Draws text at pos, clipped to the button bounding rect.
Point with floating-point X and Y coordinates.
Definition: Point.h:47
void setPressed(bool value)
Sets whether the button is pressed.
Gfx::RectF layoutFrame(PaintSurface &surface, const Gfx::RectF &frameRect) const
Returns the frame content rectangle within frameRect.
Metrics for a measured line of text.
Definition: TextMetrics.h:46
Button styler.
Definition: ButtonStyler.h:273
Weight
Describes the requested font weight.
Definition: Font.h:61
Style for widgets.
Definition: Style.h:155
void setEnabled(bool value)
Sets whether the button is enabled.
StyleOptions & options()
Returns the bound effective button options.
const Painter & textPainter(PaintSurface &surface)
Returns a painter with the current font and text color applied for surface.
Renders the visual appearance of a push button.
Definition: ButtonStyler.h:106
virtual void onReset(const StyleOptions &options)=0
Gfx::SizeF measureFrame(PaintSurface &surface, const Gfx::SizeF &contentSize) const
Measures the frame enclosing contentSize.
Gfx::RectF layoutFrame(PaintSurface &surface, const Gfx::RectF &frameRect)
Returns the content rectangle within the outer frameRect.
Gfx::SizeF measureContent(PaintSurface &surface, Direction direction, const Gfx::SizeF &iconSize, const Gfx::SizeF &textSize)
Returns the combined content size for icon and text arranged by direction.
void renderText(PaintContext &context, const Gfx::RectF &rect, const String &text, const Gfx::PointF &pos, const ButtonState &state) const
Renders text at pos within rect for state.
const Gfx::Brush & foreground() const
Returns the effective foreground brush.
const Gfx::Color & textColor() const
Returns the effective text color.
Cloneable style facet used by widget renderers.
Definition: Renderer.h:48
Font request used for text drawing and measurement.
Definition: Font.h:56
void setTextColor(const Gfx::Color &color)
Sets the widget-local text color to color.
Basic image.
Definition: Image.h:52
Style options container.
Definition: StyleOptions.h:535
const Gfx::Pen & contour() const
Returns the effective contour pen.
bool isFlat() const
Returns true if the button uses flat rendering.
void renderBackground(PaintContext &context, const Gfx::RectF &rect, const ButtonState &state) const
Renders the button background within rect for state.
void setRenderer(ButtonRenderer *renderer=0)
Assigns a specific button renderer.
Standard color type.
Definition: Color.h:47
void layoutContent(PaintSurface &surface, const Gfx::RectF &contentRect, Direction direction, const Gfx::SizeF &iconSize, const Gfx::SizeF &textSize, Gfx::RectF &iconRect, Gfx::RectF &textRect) const
Lays out icon and text rectangles within contentRect.
virtual ButtonRenderer * onCreate() const =0
Hook that allocates a new renderer of the same concrete type.
ButtonStyler()
Constructs an unbound button styler.
Unicode capable basic_string.
Definition: Api-String.h:44
void setFontSlant(Gfx::Font::Slant slant)
Sets the widget-local font slant to slant.
Fill description for shapes and text.
Definition: Brush.h:145
Gfx::Font font() const
Returns the effective font.
virtual Renderer * onStyleRenderer(const Style &style)
Resolves the shared style renderer for the current style.
ButtonState()
Constructs an empty button state.
bool isFocused() const
Returns true if the button currently has focus.
void setAccentColor(const Gfx::Color &color)
Sets the widget-local accent color to color.
Rect with floating-point coordinates.
Definition: Rect.h:47
Gfx::RectF layoutMnemonic(PaintSurface &surface, const String &text, const Gfx::PointF &textPos, const Gfx::FontMetrics &fontMetrics, String::size_type mnemonicIndex) const
Returns the mnemonic underline rectangle for mnemonicIndex.
Back buffer drawing surface.
Definition: Pixmap.h:77
void setFocused(bool value)
Sets whether the button has focus.
virtual StyleOptions & onBindOptions(const StyleOptions &styleOptions)
Returns the specific style options.
ButtonRenderer * create() const
Creates a new default-constructed instance that the caller owns.
void setHovered(bool value)
Sets whether the pointer is currently hovering the button.
void measureText(PaintSurface &surface, const String &text, Gfx::TextMetrics &textMetrics, Gfx::FontMetrics &fontMetrics) const
Measures text and stores its metrics for surface.
void renderChrome(PaintContext &context, const Gfx::RectF &rect, const ButtonState &state) const
Renders the button chrome within rect for state.
void setFont(const Gfx::Font &font)
Sets the widget-local font to font.
const Gfx::Color & highlightColor() const
Returns the effective highlight color.
void renderChrome(PaintContext &context, const Gfx::RectF &rect, const ButtonState &state)
Paints the button background and border within the button bounding rect.
const Gfx::Color & accentColor() const
Returns the effective accent color.
void renderBackground(PaintContext &context, const Gfx::RectF &rect, const ButtonState &state)
Paints the background fill within the widget rect.